utility.hpp 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. #ifndef BOOST_NUMERIC_UTILITY_HPP
  2. #define BOOST_NUMERIC_UTILITY_HPP
  3. // Copyright (c) 2015 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. #include <cstdint> // intmax_t, uintmax_t, uint8_t, ...
  9. #include <algorithm>
  10. #include <type_traits> // conditional
  11. #include <limits>
  12. #include <cassert>
  13. #include <utility> // pair
  14. #include <boost/integer.hpp> // (u)int_t<>::least, exact
  15. namespace boost {
  16. namespace safe_numerics {
  17. namespace utility {
  18. ///////////////////////////////////////////////////////////////////////////////
  19. // used for debugging
  20. // provokes warning message with names of type T
  21. // usage - print_types<T, ...>;
  22. // see https://cukic.co/2019/02/19/tmp-testing-and-debugging-templates
  23. /*
  24. template<typename Tx>
  25. using print_type = typename Tx::error_message;
  26. */
  27. template <typename... Ts>
  28. struct [[deprecated]] print_types {};
  29. // display value of constexpr during compilation
  30. // usage print_value(N) pn;
  31. template<int N>
  32. struct print_value {
  33. enum test : char {
  34. value = N < 0 ? N - 256 : N + 256
  35. };
  36. };
  37. // static warning - same as static_assert but doesn't
  38. // stop compilation.
  39. template <typename T>
  40. struct static_test{};
  41. template <>
  42. struct static_test<std::false_type>{
  43. [[deprecated]] static_test(){}
  44. };
  45. template<typename T>
  46. using static_warning = static_test<T>;
  47. /*
  48. // can be called by constexpr to produce a compile time
  49. // trap of parameter passed is false.
  50. // usage constexpr_assert(bool)
  51. constexpr int constexpr_assert(const bool tf){
  52. return 1 / tf;
  53. }
  54. */
  55. ///////////////////////////////////////////////////////////////////////////////
  56. // return an integral constant equal to the the number of bits
  57. // held by some integer type (including the sign bit)
  58. template<typename T>
  59. using bits_type = std::integral_constant<
  60. int,
  61. std::numeric_limits<T>::digits
  62. + (std::numeric_limits<T>::is_signed ? 1 : 0)
  63. >;
  64. /*
  65. From http://graphics.stanford.edu/~seander/bithacks.html#IntegerLogObvious
  66. Find the log base 2 of an integer with a lookup table
  67. static const char LogTable256[256] =
  68. {
  69. #define LT(n) n, n, n, n, n, n, n, n, n, n, n, n, n, n, n, n
  70. -1, 0, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3,
  71. LT(4), LT(5), LT(5), LT(6), LT(6), LT(6), LT(6),
  72. LT(7), LT(7), LT(7), LT(7), LT(7), LT(7), LT(7), LT(7)
  73. };
  74. unsigned int v; // 32-bit word to find the log of
  75. unsigned r; // r will be lg(v)
  76. register unsigned int t, tt; // temporaries
  77. if (tt = v >> 16)
  78. {
  79. r = (t = tt >> 8) ? 24 + LogTable256[t] : 16 + LogTable256[tt];
  80. }
  81. else
  82. {
  83. r = (t = v >> 8) ? 8 + LogTable256[t] : LogTable256[v];
  84. }
  85. The lookup table method takes only about 7 operations to find the log of a 32-bit value.
  86. If extended for 64-bit quantities, it would take roughly 9 operations. Another operation
  87. can be trimmed off by using four tables, with the possible additions incorporated into each.
  88. Using int table elements may be faster, depending on your architecture.
  89. */
  90. namespace ilog2_detail {
  91. template<int N>
  92. constexpr inline unsigned int ilog2(const typename boost::uint_t<N>::exact & t){
  93. using half_type = typename boost::uint_t<N/2>::exact;
  94. const half_type upper_half = static_cast<half_type>(t >> N/2);
  95. const half_type lower_half = static_cast<half_type>(t);
  96. return upper_half == 0 ? ilog2<N/2>(lower_half) : N/2 + ilog2<N/2>(upper_half);
  97. }
  98. template<>
  99. constexpr inline unsigned int ilog2<8>(const typename boost::uint_t<8>::exact & t){
  100. #define LT(n) n, n, n, n, n, n, n, n, n, n, n, n, n, n, n, n
  101. const char LogTable256[256] = {
  102. static_cast<char>(-1), 0, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3,
  103. LT(4), LT(5), LT(5), LT(6), LT(6), LT(6), LT(6),
  104. LT(7), LT(7), LT(7), LT(7), LT(7), LT(7), LT(7), LT(7)
  105. };
  106. return LogTable256[t];
  107. }
  108. } // ilog2_detail
  109. template<typename T>
  110. constexpr inline unsigned int ilog2(const T & t){
  111. // log not defined for negative numbers
  112. // assert(t > 0);
  113. if(t == 0)
  114. return 0;
  115. return ilog2_detail::ilog2<bits_type<T>::value>(
  116. static_cast<
  117. typename boost::uint_t<
  118. bits_type<T>::value
  119. >::least
  120. >(t)
  121. );
  122. }
  123. // the number of bits required to render the value in x
  124. // including sign bit
  125. template<typename T>
  126. constexpr inline unsigned int significant_bits(const T & t){
  127. return 1 + ((t < 0) ? ilog2(~t) : ilog2(t));
  128. }
  129. /*
  130. // give the value t, return the number which corresponds
  131. // to all 1's which is higher than that number
  132. template<typename T>
  133. constexpr unsigned int bits_value(const T & t){
  134. const unsigned int sb = significant_bits(t);
  135. const unsigned int sb_max = significant_bits(std::numeric_limits<T>::max());
  136. return sb < sb_max ? ((sb << 1) - 1) : std::numeric_limits<T>::max();
  137. }
  138. */
  139. ///////////////////////////////////////////////////////////////////////////////
  140. // meta functions returning types
  141. // If we use std::max in here we get internal compiler errors
  142. // with MSVC (tested VC2017) ...
  143. // Notes from https://en.cppreference.com/w/cpp/algorithm/max
  144. // Capturing the result of std::max by reference if one of the parameters
  145. // is rvalue produces a dangling reference if that parameter is returned.
  146. template <class T>
  147. // turns out this problem crashes all versions of gcc compilers. So
  148. // make sure we return by value
  149. //constexpr const T & max(
  150. constexpr inline T max(
  151. const T & lhs,
  152. const T & rhs
  153. ){
  154. return lhs > rhs ? lhs : rhs;
  155. }
  156. // given a signed range, return type required to hold all the values
  157. // in the range
  158. template<
  159. std::intmax_t Min,
  160. std::intmax_t Max
  161. >
  162. using signed_stored_type = typename boost::int_t<
  163. max(
  164. significant_bits(Min),
  165. significant_bits(Max)
  166. ) + 1
  167. >::least ;
  168. // given an unsigned range, return type required to hold all the values
  169. // in the range
  170. template<
  171. std::uintmax_t Min,
  172. std::uintmax_t Max
  173. >
  174. // unsigned range
  175. using unsigned_stored_type = typename boost::uint_t<
  176. significant_bits(Max)
  177. >::least;
  178. ///////////////////////////////////////////////////////////////////////////////
  179. // constexpr functions
  180. // need our own version because official version
  181. // a) is not constexpr
  182. // b) is not guarenteed to handle non-assignable types
  183. template<typename T>
  184. constexpr inline std::pair<T, T>
  185. minmax(const std::initializer_list<T> & l){
  186. assert(l.size() > 0);
  187. const T * minimum = l.begin();
  188. const T * maximum = l.begin();
  189. for(const T * i = l.begin(); i != l.end(); ++i){
  190. if(*i < * minimum)
  191. minimum = i;
  192. else
  193. if(* maximum < *i)
  194. maximum = i;
  195. }
  196. return std::pair<T, T>{* minimum, * maximum};
  197. }
  198. // for any given t
  199. // a) figure number of significant bits
  200. // b) return a value with all significant bits set
  201. // so for example:
  202. // 3 == round_out(2) because
  203. // 2 == 10 and 3 == 11
  204. template<typename T>
  205. constexpr inline T round_out(const T & t){
  206. if(t >= 0){
  207. const std::uint8_t sb = utility::significant_bits(t);
  208. return (sb < sizeof(T) * 8)
  209. ? ((T)1 << sb) - 1
  210. : std::numeric_limits<T>::max();
  211. }
  212. else{
  213. const std::uint8_t sb = utility::significant_bits(~t);
  214. return (sb < sizeof(T) * 8)
  215. ? ~(((T)1 << sb) - 1)
  216. : std::numeric_limits<T>::min();
  217. }
  218. }
  219. } // utility
  220. } // safe_numerics
  221. } // boost
  222. #endif // BOOST_NUMERIC_UTILITY_HPP