From owner-freebsd-bugs Thu Nov 14 09:22:45 1996 Return-Path: owner-bugs Received: (from root@localhost) by freefall.freebsd.org (8.7.5/8.7.3) id JAA19407 for bugs-outgoing; Thu, 14 Nov 1996 09:22:45 -0800 (PST) Received: from halloran-eldar.lcs.mit.edu (halloran-eldar.lcs.mit.edu [18.26.0.159]) by freefall.freebsd.org (8.7.5/8.7.3) with SMTP id JAA19374 for ; Thu, 14 Nov 1996 09:22:22 -0800 (PST) Received: by halloran-eldar.lcs.mit.edu; (5.65v3.2/1.1.8.2/19Aug95-0530PM) id AA28126; Thu, 14 Nov 1996 12:21:48 -0500 Date: Thu, 14 Nov 1996 12:21:48 -0500 From: Garrett Wollman Message-Id: <9611141721.AA28126@halloran-eldar.lcs.mit.edu> To: Jason Thorpe Cc: Bruce Evans , freebsd-bugs@freefall.freebsd.org Subject: Re: misc/2007: /usr/include/sys lacking timer arithmetic functions.. In-Reply-To: <199611141643.IAA18936@lestat.nas.nasa.gov> References: <199611141643.IAA18936@lestat.nas.nasa.gov> Sender: owner-bugs@FreeBSD.ORG X-Loop: FreeBSD.org Precedence: bulk < 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