FreeBSD/contrib/ntp/libntp/ntp_calendar.c
/*
 *---------------------------------------------------------------------
 * Given the number of elapsed days in the calendar era, split this
 * number into the number of elapsed years in 'res.hi' and the number
 * of elapsed days of that year in 'res.lo'.
 *
 * if 'isleapyear' is not NULL, it will receive an integer that is 0 for
 * regular years and a non-zero value for leap years.
 *---------------------------------------------------------------------
 */
Given the number of elapsed days in the calendar era, split this
number into the number of elapsed years in 'res.hi' and the number
of elapsed days of that year in 'res.lo'.

if 'isleapyear' is not NULL, it will receive an integer that is 0 for
regular years and a non-zero value for leap years.
Given the number of elapsed days in the calendar era split this number into the number of elapsed years in ' res . hi ' and the number of elapsed days of that year in ' res . lo ' . if ' isleapyear ' is not NULL it will receive an integer that is 0 for regular years and a non - zero value for leap years .
ntpcal_split
ntpcal_split_eradays(
	int32_t days,
	int  *isleapyear
	)
{
	ntpcal_split res;
	int32_t	 n100, n001; 
	uint32_t uday, Q, sflag;
	sflag = int32_sflag(days);
	uday  = uint32_saturate(int32_to_uint32_2cpl(days), sflag);
	uday  = (4u * uday) | 3u;
	Q    = sflag ^ ((sflag ^ uday) / GREGORIAN_CYCLE_DAYS);
	uday = uday - Q * GREGORIAN_CYCLE_DAYS;
	n100 = uint32_2cpl_to_int32(Q);
	uday |= 3;
	n001 = uday / GREGORIAN_NORMAL_LEAP_CYCLE_DAYS;
	uday = uday % GREGORIAN_NORMAL_LEAP_CYCLE_DAYS;
	res.hi = n100 * 100 + n001;
	res.lo = uday / 4u;
	if (isleapyear)
		*isleapyear = !((n001+1) & 3)
		    && ((n001 != 99) || !((Q+1) & 3));
	return res;
}
ntpcal_split ntpcal_split_eradays ( int32_t days , int * isleapyear ) { ntpcal_split res ; int32_t n100 , n001 ; uint32_t uday , Q , sflag ; sflag = int32_sflag ( days ) ; uday = uint32_saturate ( int32_to_uint32_2cpl ( days ) , sflag ) ; uday = ( 4u * uday ) | 3u ; Q = sflag ^ ( ( sflag ^ uday ) / GREGORIAN_CYCLE_DAYS ) ; uday = uday - Q * GREGORIAN_CYCLE_DAYS ; n100 = uint32_2cpl_to_int32 ( Q ) ; uday |= 3 ; n001 = uday / GREGORIAN_NORMAL_LEAP_CYCLE_DAYS ; uday = uday % GREGORIAN_NORMAL_LEAP_CYCLE_DAYS ; res . hi = n100 * 100 + n001 ; res . lo = uday / 4u ; if ( isleapyear ) * isleapyear = ! ( ( n001 + 1 ) & 3 ) && ( ( n001 != 99 ) || ! ( ( Q + 1 ) & 3 ) ) ; return res ; }
Given the number of elapsed days in the calendar era, split this
 number into the number of elapsed years in 'res.hi' and the number
 of elapsed days of that year in 'res.lo'.
Given the number of elapsed days in the calendar era split this number into the number of elapsed years in ' res . hi ' and the number of elapsed days of that year in ' res . lo ' .
/* Use the fast cyclesplit algorithm here, to calculate the
	 * centuries and years in a century with one division each. This
	 * reduces the number of division operations to two, but is
	 * susceptible to internal range overflow. We make sure the
	 * input operands are in the safe range; this still gives us
	 * approx +/-2.9 million years.
	 */ /* calendar year cycles */ /* split off centuries first */ /* Split off years in century -- days >= 0 here, and we're far
	 * away from integer overflow trouble now. */ /* Assemble the year and day in year */ /* Eventually set the leap year flag. Note: 0 <= n001 <= 99 and
	 * Q is still the two's complement representation of the
	 * centuries: The modulo 4 ops can be done with masking here.
	 * We also shift the year and the century by one, so the tests
	 * can be done against zero instead of 3.
	 */