Re: [RFC/RFT] calloutng

From: Bruce Evans <brde_at_optusnet.com.au>
Date: Sat, 15 Dec 2012 20:01:05 +1100 (EST)
On Sat, 15 Dec 2012, Bruce Evans wrote:

> On Fri, 14 Dec 2012, Oliver Pinter wrote:
>> 
>> What is this 1844674407309000LL constant?
>
> This is
>
>    2**64 / 10**6 * 10**3
>
> obfuscated by printing it in hex and doing the scaling by powers of
> 10 manually, and then giving it a bogus type using the abominable long
> long misfeature.  I try to kill this obfuscation and the abimination
> whenever I see them.  In sys/time.h, this resulted in a related binary
> conversion using a scale factor of
>
>    ((uint64_t)1 << 63) / (1000000000 >> 1).
>
> Here the power of 2 term is 2**63.   2**64 cannot be used since it exceeds
> uintmax_t.  The power of 10 term is 10**9.  This is divided by 2 to
> compensate for dividing 2**64 by 2.  The abomination is avoided by using
> smaller literal values and expandling them to 64-bit values using shifts.

Bah, this is only de-obfuscated and de-abominated in my version:

% Index: time.h
% ===================================================================
% RCS file: /home/ncvs/src/sys/sys/time.h,v
% retrieving revision 1.65
% diff -u -2 -r1.65 time.h
% --- time.h	7 Apr 2004 04:19:49 -0000	1.65
% +++ time.h	7 Apr 2004 11:28:54 -0000
% _at__at_ -118,6 +118,5 _at__at_
% 
%  	bt->sec = ts->tv_sec;
% -	/* 18446744073 = int(2^64 / 1000000000) */
% -	bt->frac = ts->tv_nsec * (uint64_t)18446744073LL; 
% +	bt->frac = ts->tv_nsec * (((uint64_t)1 << 63) / (1000000000 >> 1));
%  }
%

The magic 1844... in time.h is at least commented on.  This makes it
less obscure, but takes twice as many source lines and risks the comment
getting out of date with the code.  The comment is also sloppy with types
and uses the '^' operator without saying that it is exponentiation and
nothing like the C '^' operator.  The types are especially critical in
the shift exprression.  I like to use the Fortran '**' operator in C
comments without saying what it is instead.

In another reply to this thread, the value in the explanation is off
by a factor of 1000 and the rounding to a multiple of 1000 is not
explained.  It is easy to have such errors in comments, while the code
tends to be more correct since it gets checked by running it.

Bruce
Received on Sat Dec 15 2012 - 08:01:16 UTC

This archive was generated by hypermail 2.4.0 : Wed May 19 2021 - 11:40:33 UTC