utility.hpp 7.4 KB

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