X-Git-Url: http://git.onelab.eu/?a=blobdiff_plain;f=lib%2Fsat-math.h;h=3d1c50bdccc09ce9d919c0fd0c6ab72e8d353e3f;hb=003ce655b7116d18c86a74c50391e54990346931;hp=ae504ba143b8e6fc1e1c763e81d937019617dd34;hpb=e0edde6fee279cdbbf3c179f5f50adaf0c7c7f1e;p=sliver-openvswitch.git diff --git a/lib/sat-math.h b/lib/sat-math.h index ae504ba14..3d1c50bdc 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. @@ -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 */