checked_float.hpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. #ifndef BOOST_NUMERIC_CHECKED_FLOAT_HPP
  2. #define BOOST_NUMERIC_CHECKED_FLOAT_HPP
  3. // Copyright (c) 2017 Robert Ramey
  4. //
  5. // Distributed under the Boost Software License, Version 1.0. (See
  6. // accompanying file LICENSE_1_0.txt or copy at
  7. // http://www.boost.org/LICENSE_1_0.txt)
  8. // contains operation implementation of arithmetic operators
  9. // on built-in floating point types. The default implementation is to just
  10. // invoke the operation with no checking. These are overloaded
  11. // for specific types such as integer, etc.
  12. #include <type_traits> // std::is_floating_point, make_unsigned
  13. namespace boost {
  14. namespace safe_numerics {
  15. namespace checked {
  16. ////////////////////////////////////////////////////
  17. // layer 0 - implement safe operations for floating
  18. template<typename R, typename T>
  19. struct heterogeneous_checked_operation<R, T, F,
  20. typename std::enable_if<
  21. std::is_floating_point<R>::value
  22. && std::is_floating_point<T>::value
  23. >::type
  24. >{
  25. constexpr static checked_result<R>
  26. cast(const T & t) noexcept {
  27. return t;
  28. };
  29. }; // checked_unary_operation
  30. template<typename R, typename T, typename U>
  31. struct checked_operation<R, T, U, F,
  32. typename std::enable_if<
  33. std::is_floating_point<R>::value
  34. >::type
  35. >{
  36. constexpr static checked_result<R> add(const T & t, const U & u) noexcept {
  37. return t + u;
  38. }
  39. constexpr static checked_result<R> subtract(
  40. const T & t,
  41. const U & u
  42. ) noexcept {
  43. return t - u;
  44. }
  45. constexpr static checked_result<R> multiply(
  46. const T & t,
  47. const U & u
  48. ) noexcept {
  49. return t * u;
  50. }
  51. constexpr static checked_result<R> divide(
  52. const T & t,
  53. const U & u
  54. ) noexcept {
  55. return t / u;
  56. }
  57. constexpr static checked_result<R> modulus(
  58. const T & t,
  59. const U & u
  60. ) noexcept {
  61. return t % u;
  62. }
  63. constexpr static bool less_than(const T & t, const U & u) noexcept {
  64. return t < u;
  65. }
  66. constexpr static bool greater_than(const T & t, const U & u) noexcept {
  67. return t > u;
  68. }
  69. constexpr static bool equal(const T & t, const U & u) noexcept {
  70. return t < u;
  71. }
  72. }; // checked_binary_operation
  73. template<class R, class T, class U>
  74. typename std::enable_if<
  75. std::is_floating_point<R>::value
  76. && std::is_floating_point<T>::value
  77. && std::is_floating_point<U>::value,
  78. checked_result<R>
  79. >::type
  80. constexpr bool less_than(const T & t, const U & u) noexcept {
  81. return t < u;
  82. }
  83. template<class R, class T, class U>
  84. typename std::enable_if<
  85. std::is_floating_point<R>::value
  86. && std::is_floating_point<T>::value
  87. && std::is_floating_point<U>::value,
  88. checked_result<R>
  89. >::type
  90. constexpr bool equal(const T & t, const U & u) noexcept {
  91. return t < u;
  92. }
  93. template<class R, class T, class U>
  94. typename std::enable_if<
  95. std::is_floating_point<R>::value
  96. && std::is_floating_point<T>::value
  97. && std::is_floating_point<U>::value,
  98. checked_result<R>
  99. >::type
  100. constexpr checked_result<R> left_shift(const T & t, const U & u) noexcept {
  101. return t << u;
  102. }
  103. template<class R, class T, class U>
  104. typename std::enable_if<
  105. std::is_floating_point<R>::value
  106. && std::is_floating_point<T>::value
  107. && std::is_floating_point<U>::value,
  108. checked_result<R>
  109. >::type
  110. constexpr checked_result<R> right_shift(const T & t, const U & u) noexcept {
  111. return t >> u;
  112. }
  113. template<class R, class T, class U>
  114. typename std::enable_if<
  115. std::is_floating_point<R>::value
  116. && std::is_floating_point<T>::value
  117. && std::is_floating_point<U>::value,
  118. checked_result<R>
  119. >::type
  120. constexpr checked_result<R> bitwise_or(const T & t, const U & u) noexcept {
  121. return t | u;
  122. }
  123. template<class R, class T, class U>
  124. typename std::enable_if<
  125. std::is_floating_point<R>::value
  126. && std::is_floating_point<T>::value
  127. && std::is_floating_point<U>::value,
  128. checked_result<R>
  129. >::type
  130. constexpr checked_result<R> bitwise_xor(const T & t, const U & u) noexcept {
  131. return t ^ u;
  132. }
  133. template<class R, class T, class U>
  134. typename std::enable_if<
  135. std::is_floating_point<R>::value
  136. && std::is_floating_point<T>::value
  137. && std::is_floating_point<U>::value,
  138. checked_result<R>
  139. >::type
  140. constexpr checked_result<R> bitwise_and(const T & t, const U & u) noexcept {
  141. return t & u;
  142. }
  143. } // checked
  144. } // safe_numerics
  145. } // boost
  146. #endif // BOOST_NUMERIC_CHECKED_DEFAULT_HPP