jitcraft/Utility.h
changeset 55 e073a9fe8e57
parent 54 8e22792a3921
child 59 b7d61ddb3c8d
     1.1 --- a/jitcraft/Utility.h	Sat Jul 07 20:28:29 2012 -0700
     1.2 +++ b/jitcraft/Utility.h	Sat Jul 07 20:53:10 2012 -0700
     1.3 @@ -163,6 +163,8 @@
     1.4  static inline bool
     1.5  IsUint32AddSafe(unsigned a, unsigned b)
     1.6  {
     1.7 +    if (!a || !b)
     1.8 +        return true;
     1.9      unsigned log2_a = Log2(a);
    1.10      unsigned log2_b = Log2(b);
    1.11      return (log2_a < sizeof(unsigned) * 8) &&
    1.12 @@ -170,6 +172,17 @@
    1.13  }
    1.14  
    1.15  static inline bool
    1.16 +IsUintPtrAddSafe(size_t a, size_t b)
    1.17 +{
    1.18 +    if (!a || !b)
    1.19 +        return true;
    1.20 +    size_t log2_a = Log2(a);
    1.21 +    size_t log2_b = Log2(b);
    1.22 +    return (log2_a < sizeof(size_t) * 8) &&
    1.23 +           (log2_b < sizeof(size_t) * 8);
    1.24 +}
    1.25 +
    1.26 +static inline bool
    1.27  IsUint32MultiplySafe(unsigned a, unsigned b)
    1.28  {
    1.29      if (a <= 1 || b <= 1)
    1.30 @@ -180,6 +193,17 @@
    1.31      return log2_a + log2_b <= sizeof(unsigned) * 8;
    1.32  }
    1.33  
    1.34 +static inline bool
    1.35 +IsUintPtrMultiplySafe(size_t a, size_t b)
    1.36 +{
    1.37 +    if (a <= 1 || b <= 1)
    1.38 +        return true;
    1.39 +
    1.40 +    size_t log2_a = Log2(a);
    1.41 +    size_t log2_b = Log2(b);
    1.42 +    return log2_a + log2_b <= sizeof(size_t) * 8;
    1.43 +}
    1.44 +
    1.45  #define ARRAY_LENGTH(array) (sizeof(array) / sizeof(array[0]))
    1.46  #define STATIC_ASSERT(cond) extern int static_assert_f(int a[(cond) ? 1 : -1])
    1.47