[PATCH] Add timegm POSIX call

Richard Earnshaw (lists) Richard.Earnshaw@arm.com
Thu Aug 16 22:11:00 GMT 2018


On 15/08/18 20:18, Freddie Chopin wrote:
> On Wed, 2018-08-15 at 11:01 -0400, Craig Howland wrote:
>>> +/* returns either 0 or 1 */
>>> +static
>>> +int
>>> +__is_leap_year (int year)
>>> +{
>>> +  return (year % 4) == 0 && ((year % 100) != 0 || (((year / 100) &
>>> 3)
>>> == (-(YEAR_BASE / 100)  & 3)));
>>
>> How about "(year & 3) == 0" instead of the % to save time? (Should
>> not matter 
>> with a good optimizer, but it is not necessarily on.)
> 
> On the other hand % is exactly 1:1 with the intent, while binary
> operations are not.
> 
> godbolt suggests that for x86-64 "(x % 4) == 0" and "(x & 3) == 0" give
> exactly the same code (binary operations) when optimization is
> explicitly _disabled_ (-O0).
> 
> Also do note that year may be negative, and "& 3" is not the same as "%
> 4" for negative numbers. In this particular case this probably doesn't
> matter anyway, but still (;
> 
> Regards,
> FCh
> 

"year" is signed, so % and & do not generate the same code - you need a
negative value correction.  So it all depends on whether you expect
negative years (BC) to be relevant for such calculations!

R.



More information about the Newlib mailing list