FreeBSD/contrib/ntp/libntp/ntp_calendar.c
/*
 * Given a number of elapsed weeks since the begin of the christian
 * era, split this number into the number of elapsed years in res.hi
 * and the excessive number of weeks in res.lo. (That is, res.lo is
 * the number of elapsed weeks in the remaining partial year.)
 */
Given a number of elapsed weeks since the begin of the christian
era, split this number into the number of elapsed years in res.hi
and the excessive number of weeks in res.lo. (That is, res.lo is
the number of elapsed weeks in the remaining partial year.)
Given a number of elapsed weeks since the begin of the christian era split this number into the number of elapsed years in res . hi and the excessive number of weeks in res . lo . ( That is res . lo is the number of elapsed weeks in the remaining partial year . )
ntpcal_split
isocal_split_eraweeks(
	int32_t weeks
	)
{
	static const uint16_t bctab[4] = { 85, 130, 17, 62 };
	ntpcal_split res;
	int32_t  cc, ci;
	uint32_t sw, cy, Q, sflag;
	sflag = int32_sflag(weeks);
	sw  = uint32_saturate(int32_to_uint32_2cpl(weeks), sflag);
	sw  = 4u * sw + 2;
	Q   = sflag ^ ((sflag ^ sw) / GREGORIAN_CYCLE_WEEKS);
	sw -= Q * GREGORIAN_CYCLE_WEEKS;
	ci  = Q % 4u;
	cc  = uint32_2cpl_to_int32(Q);
	sw  = (sw / 4u) * 157u + bctab[ci];
	cy  = sw / 8192u;	
	sw  = sw % 8192u;	
	res.hi = 100*cc + cy;
	res.lo = sw / 157u;
	return res;
}
ntpcal_split isocal_split_eraweeks ( int32_t weeks ) { static const uint16_t bctab [ 4 ] = { 85 , 130 , 17 , 62 } ; ntpcal_split res ; int32_t cc , ci ; uint32_t sw , cy , Q , sflag ; sflag = int32_sflag ( weeks ) ; sw = uint32_saturate ( int32_to_uint32_2cpl ( weeks ) , sflag ) ; sw = 4u * sw + 2 ; Q = sflag ^ ( ( sflag ^ sw ) / GREGORIAN_CYCLE_WEEKS ) ; sw -= Q * GREGORIAN_CYCLE_WEEKS ; ci = Q % 4u ; cc = uint32_2cpl_to_int32 ( Q ) ; sw = ( sw / 4u ) * 157u + bctab [ ci ] ; cy = sw / 8192u ; sw = sw % 8192u ; res . hi = 100 * cc + cy ; res . lo = sw / 157u ; return res ; }
Given a number of elapsed weeks since the begin of the christian
 era, split this number into the number of elapsed years in res.hi
 and the excessive number of weeks in res.lo.
Given a number of elapsed weeks since the begin of the christian era split this number into the number of elapsed years in res . hi and the excessive number of weeks in res . lo .
/*
	 * use: y = (w * 157 + b[c]) / 8192 as interpolation
	 */ /* Use two fast cycle-split divisions here. This is again
	 * susceptible to internal overflow, so we check the range. This
	 * still permits more than +/-20 million years, so this is
	 * likely a pure academical problem.
	 *
	 * We want to execute '(weeks * 4 + 2) /% 20871' under floor
	 * division rules in the first step.
	 */ /* Split off years; sw >= 0 here! The scaled weeks in the years
	 * are scaled up by 157 afterwards.
	 */ /* ws >> 13 , let the compiler sort it out */ /* ws & 8191, let the compiler sort it out */ /* assemble elapsed years and downscale the elapsed weeks in
	 * the year.
	 */