FreeBSD/contrib/ntp/libntp/ntp_calendar.c
/*
 * Given a number of elapsed (ISO-)years since the begin of the
 * christian era, return the number of elapsed weeks corresponding to
 * the number of years.
 */
Given a number of elapsed (ISO-)years since the begin of the
christian era, return the number of elapsed weeks corresponding to
the number of years.
Given a number of elapsed ( ISO - ) years since the begin of the christian era return the number of elapsed weeks corresponding to the number of years .
int32_t
isocal_weeks_in_years(
	int32_t years
	)
{	
	static const uint16_t bctab[4] = { 157, 449, 597, 889 };
	int32_t  cs, cw;
	uint32_t cc, ci, yu, sflag;
	sflag = int32_sflag(years);
	yu    = int32_to_uint32_2cpl(years);
	cc  = sflag ^ ((sflag ^ yu) / 100u);
	yu -= cc * 100u;
	ci = cc * 3u + 1;
	cs = uint32_2cpl_to_int32(sflag ^ ((sflag ^ ci) / 4u));
	ci = ci % 4u;
	cw = (yu * 53431u + bctab[ci]) / 1024u;
	return uint32_2cpl_to_int32(cc) * 5217 + cs + cw;
}
int32_t isocal_weeks_in_years ( int32_t years ) { static const uint16_t bctab [ 4 ] = { 157 , 449 , 597 , 889 } ; int32_t cs , cw ; uint32_t cc , ci , yu , sflag ; sflag = int32_sflag ( years ) ; yu = int32_to_uint32_2cpl ( years ) ; cc = sflag ^ ( ( sflag ^ yu ) / 100u ) ; yu -= cc * 100u ; ci = cc * 3u + 1 ; cs = uint32_2cpl_to_int32 ( sflag ^ ( ( sflag ^ ci ) / 4u ) ) ; ci = ci % 4u ; cw = ( yu * 53431u + bctab [ ci ] ) / 1024u ; return uint32_2cpl_to_int32 ( cc ) * 5217 + cs + cw ; }
Given a number of elapsed (ISO-)years since the begin of the
 christian era, return the number of elapsed weeks corresponding to
 the number of years.
Given a number of elapsed ( ISO - ) years since the begin of the christian era return the number of elapsed weeks corresponding to the number of years .
/*
	 * use: w = (y * 53431 + b[c]) / 1024 as interpolation
	 */ /* split off centuries, using floor division */ /* calculate century cycles shift and cycle index:
	 * Assuming a century is 5217 weeks, we have to add a cycle
	 * shift that is 3 for every 4 centuries, because 3 of the four
	 * centuries have 5218 weeks. So '(cc*3 + 1) / 4' is the actual
	 * correction, and the second century is the defective one.
	 *
	 * Needs floor division by 4, which is done with masking and
	 * shifting.
	 */ /* Get weeks in century. Can use plain division here as all ops
	 * are >= 0,  and let the compiler sort out the possible
	 * optimisations.
	 */