Re: [PATCH] Add timegm POSIX call

"Richard Earnshaw (lists)" <[email protected]>
Newsgroups gmane.comp.lib.newlib
Message-ID <[email protected]>
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.
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.