This is the mail archive of the newlib@sourceware.org mailing list for the newlib project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: [PATCH] Add timegm POSIX call


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.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]