LWN.net Logo

Signed overflow optimization hazards in the kernel

Signed overflow optimization hazards in the kernel

Posted Aug 18, 2012 22:51 UTC (Sat) by jzbiciak (✭ supporter ✭, #5246)
Parent article: Signed overflow optimization hazards in the kernel

This seems like it argues for a macro to hide the ugliness. Name it something obvious such as "before()". It makes the intent obvious and ensures the ugly math is done correctly. Something like:

/* return true if timestamp 'a' is earlier than timestamp 'b' */
#define before(a,b) (ULONG_MAX/2 > ((unsigned long)(a) - (unsigned long)(b)))

Or is this just an invitation to more problems?


(Log in to post comments)

Signed overflow optimization hazards in the kernel

Posted Aug 18, 2012 23:04 UTC (Sat) by jzbiciak (✭ supporter ✭, #5246) [Link]

I see I missed ppisa's impressive macros upthread. Those certainly seem to take into account a range of integer types as well!

Copyright © 2013, Eklektix, Inc.
Comments and public postings are copyrighted by their creators.
Linux is a registered trademark of Linus Torvalds