FreeBSD/contrib/ntp/libntp/ntp_calendar.c
/*
 *---------------------------------------------------------------------
 * Merge a number of days and a number of seconds into seconds,
 * expressed in 64 bits to avoid overflow.
 *---------------------------------------------------------------------
 */
Merge a number of days and a number of seconds into seconds,
expressed in 64 bits to avoid overflow.
Merge a number of days and a number of seconds into seconds expressed in 64 bits to avoid overflow .
vint64
ntpcal_dayjoin(
	int32_t days,
	int32_t secs
	)
{
	vint64 res;
#   if defined(HAVE_INT64)
	res.q_s	 = days;
	res.q_s *= SECSPERDAY;
	res.q_s += secs;
#   else
	uint32_t p1, p2;
	int	 isneg;
	isneg = (days < 0);
	if (isneg)
		days = -days;
	res.D_s.lo = (days & 0xFFFF) * 675u;
	res.D_s.hi = 0;
	p1 = (days >> 16) * 675u;
	p2 = p1 >> 16;
	p1 = p1 << 16;
	M_ADD(res.D_s.hi, res.D_s.lo, p2, p1);
	res.D_s.hi = (res.D_s.hi << 7) | (res.D_s.lo >> 25);
	res.D_s.lo = (res.D_s.lo << 7);
	if (isneg)
		M_NEG(res.D_s.hi, res.D_s.lo);
	p2 = 0;
	if (secs < 0) {
		p1 = (uint32_t)-secs;
		M_NEG(p2, p1);
	} else {
		p1 = (uint32_t)secs;
	}
	M_ADD(res.D_s.hi, res.D_s.lo, p2, p1);
#   endif
	return res;
}
vint64 ntpcal_dayjoin ( int32_t days , int32_t secs ) { vint64 res ; #   if defined ( HAVE_INT64 ) res . q_s = days ; res . q_s *= SECSPERDAY ; res . q_s += secs ; #   else uint32_t p1 , p2 ; int isneg ; isneg = ( days < 0 ) ; if ( isneg ) days = - days ; res . D_s . lo = ( days & 0xFFFF ) * 675u ; res . D_s . hi = 0 ; p1 = ( days >> 16 ) * 675u ; p2 = p1 >> 16 ; p1 = p1 << 16 ; M_ADD ( res . D_s . hi , res . D_s . lo , p2 , p1 ) ; res . D_s . hi = ( res . D_s . hi << 7 ) | ( res . D_s . lo >> 25 ) ; res . D_s . lo = ( res . D_s . lo << 7 ) ; if ( isneg ) M_NEG ( res . D_s . hi , res . D_s . lo ) ; p2 = 0 ; if ( secs < 0 ) { p1 = ( uint32_t ) - secs ; M_NEG ( p2 , p1 ) ; } else { p1 = ( uint32_t ) secs ; } M_ADD ( res . D_s . hi , res . D_s . lo , p2 , p1 ) ; #   endif return res ; }
Merge a number of days and a number of seconds into seconds,
 expressed in 64 bits to avoid overflow.
Merge a number of days and a number of seconds into seconds expressed in 64 bits to avoid overflow .
/*
	 * res = days *86400 + secs, using manual 16/32 bit
	 * multiplications and shifts.
	 */ /* assemble days * 675 */ /* mul by 128, using shift */ /* fix sign */ /* properly add seconds */