FreeBSD/contrib/ntp/libntp/ntp_calendar.c
/*
 *---------------------------------------------------------------------
 * get leap years since epoch in elapsed years
 *---------------------------------------------------------------------
 */
get leap years since epoch in elapsed years
get leap years since epoch in elapsed years
int32_t
ntpcal_leapyears_in_years(
	int32_t years
	)
{
	uint32_t sflag, sum, uyear;
	sflag = int32_sflag(years);
	uyear = int32_to_uint32_2cpl(years);
	uyear ^= sflag;
	sum  = (uyear /=  4u);	
	sum -= (uyear /= 25u);	
	sum += (uyear /=  4u);	
	return uint32_2cpl_to_int32(sflag ^ sum);
}
int32_t ntpcal_leapyears_in_years ( int32_t years ) { uint32_t sflag , sum , uyear ; sflag = int32_sflag ( years ) ; uyear = int32_to_uint32_2cpl ( years ) ; uyear ^= sflag ; sum = ( uyear /= 4u ) ; sum -= ( uyear /= 25u ) ; sum += ( uyear /= 4u ) ; return uint32_2cpl_to_int32 ( sflag ^ sum ) ; }
get leap years since epoch in elapsed years
get leap years since epoch in elapsed years
/* We use the in-out-in algorithm here, using the one's
	 * complement division trick for negative numbers. The chained
	 * division sequence by 4/25/4 gives the compiler the chance to
	 * get away with only one true division and doing shifts otherwise.
	 */ /*   4yr rule --> IN  */ /* 100yr rule --> OUT */ /* 400yr rule --> IN  */ /* Thanks to the alternation of IN/OUT/IN we can do the sum
	 * directly and have a single one's complement operation
	 * here. (Only if the years are negative, of course.) Otherwise
	 * the one's complement would have to be done when
	 * adding/subtracting the terms.
	 */