[tz] localtime.c issue

Ian Abbott abbotti at mev.co.uk
Wed Jul 10 12:03:24 UTC 2013


On 2013-07-08 22:19, Alan Barrett wrote:
> On Mon, 08 Jul 2013, Arthur David Olson wrote:
>   [localtime.c]
>> --- 1514,1521 ----
>>      }
>>      {
>>          register int_fast32_t    seconds;
>>
>> !         seconds = tdays * SECSPERDAY + 0.5;
>>          tdays = seconds / SECSPERDAY;
>>          rem += seconds - tdays * SECSPERDAY;
>>      }
>
> If time_t is an integer type, then that will multiply tdays * SECSPERDAY
> using time_t arithmetic, convert to double to add 0.5, then convert to
> int_fast32_t for the assignment.  The conversion to and from double may
> lose precision, if double has less than 32 bits of mantissa precision
> (but that's unlikely).
>
> Perhaps this will work without either a compiler warning or potential
> loss of precision:
>
>             seconds = tdays * SECSPERDAY + (time_t)0.5;

For the record, that works fine in clang with no all warnings on, and 
the assembler output contains no floating point conversions even with 
optimization turned off.  With optimization off, the generated code is 
also slightly smaller than my version that retained the 'half_second' 
variable with its initial value cast to type 'type_t'.

-- 
-=( Ian Abbott @ MEV Ltd.    E-mail: <abbotti at mev.co.uk>        )=-
-=( Tel: +44 (0)161 477 1898   FAX: +44 (0)161 718 3587         )=-


More information about the tz mailing list