From: Ben Pfaff Date: Thu, 31 May 2012 00:05:34 +0000 (-0700) Subject: sat-math: Introduce macro version of SAT_MUL. X-Git-Url: https://pintos-os.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=8d2835d8c9fde763d3dd9790f38eac0595d5024a;p=openvswitch sat-math: Introduce macro version of SAT_MUL. The macro version can be used in a constant expression, such as an initializer for a variable with static lifetime. (Otherwise, it's better to use the function.) Signed-off-by: Ben Pfaff --- diff --git a/lib/sat-math.h b/lib/sat-math.h index ae504ba1..f4287fc2 100644 --- a/lib/sat-math.h +++ b/lib/sat-math.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2008 Nicira, Inc. + * Copyright (c) 2008, 2012 Nicira, Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -34,13 +34,15 @@ sat_sub(unsigned int x, unsigned int y) return x >= y ? x - y : 0; } -/* Saturating multiplication: overflow yields UINT_MAX. */ +/* Saturating multiplication of "unsigned int"s: overflow yields UINT_MAX. */ +#define SAT_MUL(X, Y) \ + ((Y) == 0 ? 0 \ + : (X) <= UINT_MAX / (Y) ? (unsigned int) (X) * (unsigned int) (Y) \ + : UINT_MAX) static inline unsigned int sat_mul(unsigned int x, unsigned int y) { - return (!y ? 0 - : x <= UINT_MAX / y ? x * y - : UINT_MAX); + return SAT_MUL(x, y); } #endif /* sat-math.h */