/src/FreeImage/Source/OpenEXR/IlmImf/ImfCheckedArithmetic.h

https://bitbucket.org/cabalistic/ogredeps/ · C++ Header · 161 lines · 62 code · 35 blank · 64 comment · 8 complexity · 8fa36114205dea568a53570036022d75 MD5 · raw file

  1. ///////////////////////////////////////////////////////////////////////////
  2. //
  3. // Copyright (c) 2009, Industrial Light & Magic, a division of Lucas
  4. // Digital Ltd. LLC
  5. //
  6. // All rights reserved.
  7. //
  8. // Redistribution and use in source and binary forms, with or without
  9. // modification, are permitted provided that the following conditions are
  10. // met:
  11. // * Redistributions of source code must retain the above copyright
  12. // notice, this list of conditions and the following disclaimer.
  13. // * Redistributions in binary form must reproduce the above
  14. // copyright notice, this list of conditions and the following disclaimer
  15. // in the documentation and/or other materials provided with the
  16. // distribution.
  17. // * Neither the name of Industrial Light & Magic nor the names of
  18. // its contributors may be used to endorse or promote products derived
  19. // from this software without specific prior written permission.
  20. //
  21. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  22. // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  23. // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  24. // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  25. // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  26. // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  27. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  28. // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  29. // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  30. // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  31. // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  32. //
  33. ///////////////////////////////////////////////////////////////////////////
  34. #ifndef INCLUDED_IMF_CHECKED_ARITHMETIC_H
  35. #define INCLUDED_IMF_CHECKED_ARITHMETIC_H
  36. //-----------------------------------------------------------------------------
  37. //
  38. // Integer arithmetic operations that throw exceptions
  39. // on overflow, underflow or division by zero.
  40. //
  41. //-----------------------------------------------------------------------------
  42. #include <limits>
  43. #include <IexMathExc.h>
  44. namespace Imf {
  45. template <bool b> struct StaticAssertionFailed;
  46. template <> struct StaticAssertionFailed <true> {};
  47. #define IMF_STATIC_ASSERT(x) \
  48. do {StaticAssertionFailed <x> staticAssertionFailed;} while (false)
  49. template <class T>
  50. T
  51. uiMult (T a, T b)
  52. {
  53. //
  54. // Unsigned integer multiplication
  55. //
  56. IMF_STATIC_ASSERT (!std::numeric_limits<T>::is_signed &&
  57. std::numeric_limits<T>::is_integer);
  58. if (a > 0 && b > std::numeric_limits<T>::max() / a)
  59. throw Iex::OverflowExc ("Integer multiplication overflow.");
  60. return a * b;
  61. }
  62. template <class T>
  63. T
  64. uiDiv (T a, T b)
  65. {
  66. //
  67. // Unsigned integer division
  68. //
  69. IMF_STATIC_ASSERT (!std::numeric_limits<T>::is_signed &&
  70. std::numeric_limits<T>::is_integer);
  71. if (b == 0)
  72. throw Iex::DivzeroExc ("Integer division by zero.");
  73. return a / b;
  74. }
  75. template <class T>
  76. T
  77. uiAdd (T a, T b)
  78. {
  79. //
  80. // Unsigned integer addition
  81. //
  82. IMF_STATIC_ASSERT (!std::numeric_limits<T>::is_signed &&
  83. std::numeric_limits<T>::is_integer);
  84. if (a > std::numeric_limits<T>::max() - b)
  85. throw Iex::OverflowExc ("Integer addition overflow.");
  86. return a + b;
  87. }
  88. template <class T>
  89. T
  90. uiSub (T a, T b)
  91. {
  92. //
  93. // Unsigned integer subtraction
  94. //
  95. IMF_STATIC_ASSERT (!std::numeric_limits<T>::is_signed &&
  96. std::numeric_limits<T>::is_integer);
  97. if (a < b)
  98. throw Iex::UnderflowExc ("Integer subtraction underflow.");
  99. return a - b;
  100. }
  101. template <class T>
  102. size_t
  103. checkArraySize (T n, size_t s)
  104. {
  105. //
  106. // Verify that the size, in bytes, of an array with n elements
  107. // of size s can be computed without overflowing:
  108. //
  109. // If computing
  110. //
  111. // size_t (n) * s
  112. //
  113. // would overflow, then throw an Iex::OverflowExc exception.
  114. // Otherwise return
  115. //
  116. // size_t (n).
  117. //
  118. IMF_STATIC_ASSERT (!std::numeric_limits<T>::is_signed &&
  119. std::numeric_limits<T>::is_integer);
  120. IMF_STATIC_ASSERT (sizeof (T) <= sizeof (size_t));
  121. if (size_t (n) > std::numeric_limits<size_t>::max() / s)
  122. throw Iex::OverflowExc ("Integer multiplication overflow.");
  123. return size_t (n);
  124. }
  125. } // namespace Imf
  126. #endif