X-Git-Url: http://git.onelab.eu/?a=blobdiff_plain;f=lib%2Fsat-math.h;h=3d1c50bdccc09ce9d919c0fd0c6ab72e8d353e3f;hb=ec988646afe6aee6a63d6894a3e9b50f715d5941;hp=cae956b5e9440b47ff59f2d140a7837512d34a77;hpb=34e63086edddcae06d7c1a4fa84fec0861e50758;p=sliver-openvswitch.git diff --git a/lib/sat-math.h b/lib/sat-math.h index cae956b5e..3d1c50bdc 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. @@ -17,7 +17,6 @@ #ifndef SAT_MATH_H #define SAT_MATH_H 1 -#include #include /* Saturating addition: overflow yields UINT_MAX. */ @@ -34,13 +33,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 */