X-Git-Url: https://pintos-os.org/cgi-bin/gitweb.cgi?a=blobdiff_plain;f=lib%2Fsat-math.h;h=f4287fc250685d0320d8fbced9a1e716657dafcd;hb=8b97ad35e5a36375a357b0c0830ecba3ab0a1b1a;hp=cae956b5e9440b47ff59f2d140a7837512d34a77;hpb=34e63086edddcae06d7c1a4fa84fec0861e50758;p=openvswitch diff --git a/lib/sat-math.h b/lib/sat-math.h index cae956b5..f4287fc2 100644 --- a/lib/sat-math.h +++ b/lib/sat-math.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2008 Nicira Networks. + * 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 */