Skip site navigation (1)Skip section navigation (2)
Date:      Sat, 15 Dec 2012 20:01:05 +1100 (EST)
From:      Bruce Evans <brde@optusnet.com.au>
To:        Bruce Evans <brde@optusnet.com.au>
Cc:        Davide Italiano <davide@FreeBSD.org>, freebsd-arch@FreeBSD.org, freebsd-current <freebsd-current@FreeBSD.org>, Oliver Pinter <oliver.pntr@gmail.com>
Subject:   Re: [RFC/RFT] calloutng
Message-ID:  <20121215194819.C1778@besplex.bde.org>
In-Reply-To: <20121215183409.U1603@besplex.bde.org>
References:  <CACYV=-F7_imU-JsPfeOZEyEPGKO2PVm1w1W3VdsH3jGiDvnmBg@mail.gmail.com> <CA%2BhQ2%2BgyhRHkB9Y%2BeGADvbjvJxSNSjYC%2BTQX8-0mf9LUD1V2HA@mail.gmail.com> <CACYV=-G9sG1Oo%2Bgz3kXmdeK85P7%2BZZg1CnAPLvwCuAbNftmv6A@mail.gmail.com> <CACYV=-EQ=G3JZOQ-9ExGT9spbEGtH5bJOrrgN2oeE2Qh3_rKag@mail.gmail.com> <CAPjTQNGL_7LnffWB5bbEgW0b6ekOrVzH6QQ6e2=fCFW4%2BmF6FA@mail.gmail.com> <20121215183409.U1603@besplex.bde.org>

next in thread | previous in thread | raw e-mail | index | archive | help
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
% @@ -118,6 +118,5 @@
% 
%  	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



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?20121215194819.C1778>