X-Git-Url: http://git.onelab.eu/?a=blobdiff_plain;f=lib%2Fsat-math.h;h=f4287fc250685d0320d8fbced9a1e716657dafcd;hb=1e3f34c7693bcabae8e443ac1b246680ef9b60e2;hp=cae956b5e9440b47ff59f2d140a7837512d34a77;hpb=a14bc59fb8f27db193d74662dc9c5cb8237177ef;p=sliver-openvswitch.git diff --git a/lib/sat-math.h b/lib/sat-math.h index cae956b5e..f4287fc25 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 */