Skip site navigation (1)Skip section navigation (2)
Date:      Thu, 14 Nov 1996 12:21:48 -0500
From:      Garrett Wollman <wollman@lcs.mit.edu>
To:        Jason Thorpe <thorpej@nas.nasa.gov>
Cc:        Bruce Evans <bde@zeta.org.au>, freebsd-bugs@freefall.freebsd.org
Subject:   Re: misc/2007: /usr/include/sys lacking timer arithmetic functions.. 
Message-ID:  <9611141721.AA28126@halloran-eldar.lcs.mit.edu>
In-Reply-To: <199611141643.IAA18936@lestat.nas.nasa.gov>
References:  <199611141643.IAA18936@lestat.nas.nasa.gov>

next in thread | previous in thread | raw e-mail | index | archive | help
<<On Thu, 14 Nov 1996 08:43:24 -0800, Jason Thorpe <thorpej@nas.nasa.gov> said:

> The nice things about those macros (and using them consistently everywhere)
> is that you can change the implementation of timeval math just once (such
> as the floating point hack you suggest) and have it magically fixed
> everywhere.  (I'd hesitate to use the floating point hack, but that's
> not the point. :-)

For timeval subtraction, the allegedly best implementation (assuming
both positive and negative differences are permitted) is:

static inline long
TV_SUB(struct timeval *a, struct timeval *b)
{
	if (a->tv_sec == b->tv_sec)	/* by far the most common case */
		return a->tv_usec - b->tv_usec;
	if (a->tv_sec - b->tv_sec == 1) /* second-most-common */
		return 1000000 + a->tv_usec - b->tv_usec;
	if (a->tv_sec - b->tv_usec == -1) /* less common */
		return -(1000000 + b->tv_usec - a->tv_usec);
	return 1000000 * (a->tv_sec - b->tv_sec) + (a->tv_usec - b->tv_usec);
}

I have written this as an inline function, but it appears in places as
a macro with ?: operators and without the less common -1 case,
credited there to (who else?) Van Jacobson.

-GAWollman

--
Garrett A. Wollman   | O Siem / We are all family / O Siem / We're all the same
wollman@lcs.mit.edu  | O Siem / The fires of freedom 
Opinions not those of| Dance in the burning flame
MIT, LCS, ANA, or NSA|                     - Susan Aglukark and Chad Irschick



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?9611141721.AA28126>