ConstexprMath.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421
  1. /*
  2. * Copyright 2017-present Facebook, Inc.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #pragma once
  17. #include <cstdint>
  18. #include <limits>
  19. #include <type_traits>
  20. namespace folly {
  21. // TODO: Replace with std::equal_to, etc., after upgrading to C++14.
  22. template <typename T>
  23. struct constexpr_equal_to {
  24. constexpr bool operator()(T const& a, T const& b) const {
  25. return a == b;
  26. }
  27. };
  28. template <typename T>
  29. struct constexpr_not_equal_to {
  30. constexpr bool operator()(T const& a, T const& b) const {
  31. return a != b;
  32. }
  33. };
  34. template <typename T>
  35. struct constexpr_less {
  36. constexpr bool operator()(T const& a, T const& b) const {
  37. return a < b;
  38. }
  39. };
  40. template <typename T>
  41. struct constexpr_less_equal {
  42. constexpr bool operator()(T const& a, T const& b) const {
  43. return a <= b;
  44. }
  45. };
  46. template <typename T>
  47. struct constexpr_greater {
  48. constexpr bool operator()(T const& a, T const& b) const {
  49. return a > b;
  50. }
  51. };
  52. template <typename T>
  53. struct constexpr_greater_equal {
  54. constexpr bool operator()(T const& a, T const& b) const {
  55. return a >= b;
  56. }
  57. };
  58. // TLDR: Prefer using operator< for ordering. And when
  59. // a and b are equivalent objects, we return b to make
  60. // sorting stable.
  61. // See http://stepanovpapers.com/notes.pdf for details.
  62. template <typename T>
  63. constexpr T constexpr_max(T a) {
  64. return a;
  65. }
  66. template <typename T, typename... Ts>
  67. constexpr T constexpr_max(T a, T b, Ts... ts) {
  68. return b < a ? constexpr_max(a, ts...) : constexpr_max(b, ts...);
  69. }
  70. // When a and b are equivalent objects, we return a to
  71. // make sorting stable.
  72. template <typename T>
  73. constexpr T constexpr_min(T a) {
  74. return a;
  75. }
  76. template <typename T, typename... Ts>
  77. constexpr T constexpr_min(T a, T b, Ts... ts) {
  78. return b < a ? constexpr_min(b, ts...) : constexpr_min(a, ts...);
  79. }
  80. template <typename T, typename Less>
  81. constexpr T const&
  82. constexpr_clamp(T const& v, T const& lo, T const& hi, Less less) {
  83. return less(v, lo) ? lo : less(hi, v) ? hi : v;
  84. }
  85. template <typename T>
  86. constexpr T const& constexpr_clamp(T const& v, T const& lo, T const& hi) {
  87. return constexpr_clamp(v, lo, hi, constexpr_less<T>{});
  88. }
  89. namespace detail {
  90. template <typename T, typename = void>
  91. struct constexpr_abs_helper {};
  92. template <typename T>
  93. struct constexpr_abs_helper<
  94. T,
  95. typename std::enable_if<std::is_floating_point<T>::value>::type> {
  96. static constexpr T go(T t) {
  97. return t < static_cast<T>(0) ? -t : t;
  98. }
  99. };
  100. template <typename T>
  101. struct constexpr_abs_helper<
  102. T,
  103. typename std::enable_if<
  104. std::is_integral<T>::value && !std::is_same<T, bool>::value &&
  105. std::is_unsigned<T>::value>::type> {
  106. static constexpr T go(T t) {
  107. return t;
  108. }
  109. };
  110. template <typename T>
  111. struct constexpr_abs_helper<
  112. T,
  113. typename std::enable_if<
  114. std::is_integral<T>::value && !std::is_same<T, bool>::value &&
  115. std::is_signed<T>::value>::type> {
  116. static constexpr typename std::make_unsigned<T>::type go(T t) {
  117. return typename std::make_unsigned<T>::type(t < static_cast<T>(0) ? -t : t);
  118. }
  119. };
  120. } // namespace detail
  121. template <typename T>
  122. constexpr auto constexpr_abs(T t)
  123. -> decltype(detail::constexpr_abs_helper<T>::go(t)) {
  124. return detail::constexpr_abs_helper<T>::go(t);
  125. }
  126. namespace detail {
  127. template <typename T>
  128. constexpr T constexpr_log2_(T a, T e) {
  129. return e == T(1) ? a : constexpr_log2_(a + T(1), e / T(2));
  130. }
  131. template <typename T>
  132. constexpr T constexpr_log2_ceil_(T l2, T t) {
  133. return l2 + T(T(1) << l2 < t ? 1 : 0);
  134. }
  135. template <typename T>
  136. constexpr T constexpr_square_(T t) {
  137. return t * t;
  138. }
  139. } // namespace detail
  140. template <typename T>
  141. constexpr T constexpr_log2(T t) {
  142. return detail::constexpr_log2_(T(0), t);
  143. }
  144. template <typename T>
  145. constexpr T constexpr_log2_ceil(T t) {
  146. return detail::constexpr_log2_ceil_(constexpr_log2(t), t);
  147. }
  148. template <typename T>
  149. constexpr T constexpr_ceil(T t, T round) {
  150. return round == T(0)
  151. ? t
  152. : ((t + (t < T(0) ? T(0) : round - T(1))) / round) * round;
  153. }
  154. template <typename T>
  155. constexpr T constexpr_pow(T base, std::size_t exp) {
  156. return exp == 0
  157. ? T(1)
  158. : exp == 1 ? base
  159. : detail::constexpr_square_(constexpr_pow(base, exp / 2)) *
  160. (exp % 2 ? base : T(1));
  161. }
  162. /// constexpr_find_last_set
  163. ///
  164. /// Return the 1-based index of the most significant bit which is set.
  165. /// For x > 0, constexpr_find_last_set(x) == 1 + floor(log2(x)).
  166. template <typename T>
  167. constexpr std::size_t constexpr_find_last_set(T const t) {
  168. using U = std::make_unsigned_t<T>;
  169. return t == T(0) ? 0 : 1 + constexpr_log2(static_cast<U>(t));
  170. }
  171. namespace detail {
  172. template <typename U>
  173. constexpr std::size_t
  174. constexpr_find_first_set_(std::size_t s, std::size_t a, U const u) {
  175. return s == 0 ? a
  176. : constexpr_find_first_set_(
  177. s / 2, a + s * bool((u >> a) % (U(1) << s) == U(0)), u);
  178. }
  179. } // namespace detail
  180. /// constexpr_find_first_set
  181. ///
  182. /// Return the 1-based index of the least significant bit which is set.
  183. /// For x > 0, the exponent in the largest power of two which does not divide x.
  184. template <typename T>
  185. constexpr std::size_t constexpr_find_first_set(T t) {
  186. using U = std::make_unsigned_t<T>;
  187. using size = std::integral_constant<std::size_t, sizeof(T) * 4>;
  188. return t == T(0)
  189. ? 0
  190. : 1 + detail::constexpr_find_first_set_(size{}, 0, static_cast<U>(t));
  191. }
  192. template <typename T>
  193. constexpr T constexpr_add_overflow_clamped(T a, T b) {
  194. using L = std::numeric_limits<T>;
  195. using M = std::intmax_t;
  196. static_assert(
  197. !std::is_integral<T>::value || sizeof(T) <= sizeof(M),
  198. "Integral type too large!");
  199. // clang-format off
  200. return
  201. // don't do anything special for non-integral types.
  202. !std::is_integral<T>::value ? a + b :
  203. // for narrow integral types, just convert to intmax_t.
  204. sizeof(T) < sizeof(M)
  205. ? T(constexpr_clamp(M(a) + M(b), M(L::min()), M(L::max()))) :
  206. // when a >= 0, cannot add more than `MAX - a` onto a.
  207. !(a < 0) ? a + constexpr_min(b, T(L::max() - a)) :
  208. // a < 0 && b >= 0, `a + b` will always be in valid range of type T.
  209. !(b < 0) ? a + b :
  210. // a < 0 && b < 0, keep the result >= MIN.
  211. a + constexpr_max(b, T(L::min() - a));
  212. // clang-format on
  213. }
  214. template <typename T>
  215. constexpr T constexpr_sub_overflow_clamped(T a, T b) {
  216. using L = std::numeric_limits<T>;
  217. using M = std::intmax_t;
  218. static_assert(
  219. !std::is_integral<T>::value || sizeof(T) <= sizeof(M),
  220. "Integral type too large!");
  221. // clang-format off
  222. return
  223. // don't do anything special for non-integral types.
  224. !std::is_integral<T>::value ? a - b :
  225. // for unsigned type, keep result >= 0.
  226. std::is_unsigned<T>::value ? (a < b ? 0 : a - b) :
  227. // for narrow signed integral types, just convert to intmax_t.
  228. sizeof(T) < sizeof(M)
  229. ? T(constexpr_clamp(M(a) - M(b), M(L::min()), M(L::max()))) :
  230. // (a >= 0 && b >= 0) || (a < 0 && b < 0), `a - b` will always be valid.
  231. (a < 0) == (b < 0) ? a - b :
  232. // MIN < b, so `-b` should be in valid range (-MAX <= -b <= MAX),
  233. // convert subtraction to addition.
  234. L::min() < b ? constexpr_add_overflow_clamped(a, T(-b)) :
  235. // -b = -MIN = (MAX + 1) and a <= -1, result is in valid range.
  236. a < 0 ? a - b :
  237. // -b = -MIN = (MAX + 1) and a >= 0, result > MAX.
  238. L::max();
  239. // clang-format on
  240. }
  241. // clamp_cast<> provides sane numeric conversions from float point numbers to
  242. // integral numbers, and between different types of integral numbers. It helps
  243. // to avoid unexpected bugs introduced by bad conversion, and undefined behavior
  244. // like overflow when casting float point numbers to integral numbers.
  245. //
  246. // When doing clamp_cast<Dst>(value), if `value` is in valid range of Dst,
  247. // it will give correct result in Dst, equal to `value`.
  248. //
  249. // If `value` is outside the representable range of Dst, it will be clamped to
  250. // MAX or MIN in Dst, instead of being undefined behavior.
  251. //
  252. // Float NaNs are converted to 0 in integral type.
  253. //
  254. // Here's some comparision with static_cast<>:
  255. // (with FB-internal gcc-5-glibc-2.23 toolchain)
  256. //
  257. // static_cast<int32_t>(NaN) = 6
  258. // clamp_cast<int32_t>(NaN) = 0
  259. //
  260. // static_cast<int32_t>(9999999999.0f) = -348639895
  261. // clamp_cast<int32_t>(9999999999.0f) = 2147483647
  262. //
  263. // static_cast<int32_t>(2147483647.0f) = -348639895
  264. // clamp_cast<int32_t>(2147483647.0f) = 2147483647
  265. //
  266. // static_cast<uint32_t>(4294967295.0f) = 0
  267. // clamp_cast<uint32_t>(4294967295.0f) = 4294967295
  268. //
  269. // static_cast<uint32_t>(-1) = 4294967295
  270. // clamp_cast<uint32_t>(-1) = 0
  271. //
  272. // static_cast<int16_t>(32768u) = -32768
  273. // clamp_cast<int16_t>(32768u) = 32767
  274. template <typename Dst, typename Src>
  275. constexpr typename std::enable_if<std::is_integral<Src>::value, Dst>::type
  276. constexpr_clamp_cast(Src src) {
  277. static_assert(
  278. std::is_integral<Dst>::value && sizeof(Dst) <= sizeof(int64_t),
  279. "constexpr_clamp_cast can only cast into integral type (up to 64bit)");
  280. using L = std::numeric_limits<Dst>;
  281. // clang-format off
  282. return
  283. // Check if Src and Dst have same signedness.
  284. std::is_signed<Src>::value == std::is_signed<Dst>::value
  285. ? (
  286. // Src and Dst have same signedness. If sizeof(Src) <= sizeof(Dst),
  287. // we can safely convert Src to Dst without any loss of accuracy.
  288. sizeof(Src) <= sizeof(Dst) ? Dst(src) :
  289. // If Src is larger in size, we need to clamp it to valid range in Dst.
  290. Dst(constexpr_clamp(src, Src(L::min()), Src(L::max()))))
  291. // Src and Dst have different signedness.
  292. // Check if it's signed -> unsigend cast.
  293. : std::is_signed<Src>::value && std::is_unsigned<Dst>::value
  294. ? (
  295. // If src < 0, the result should be 0.
  296. src < 0 ? Dst(0) :
  297. // Otherwise, src >= 0. If src can fit into Dst, we can safely cast it
  298. // without loss of accuracy.
  299. sizeof(Src) <= sizeof(Dst) ? Dst(src) :
  300. // If Src is larger in size than Dst, we need to ensure the result is
  301. // at most Dst MAX.
  302. Dst(constexpr_min(src, Src(L::max()))))
  303. // It's unsigned -> signed cast.
  304. : (
  305. // Since Src is unsigned, and Dst is signed, Src can fit into Dst only
  306. // when sizeof(Src) < sizeof(Dst).
  307. sizeof(Src) < sizeof(Dst) ? Dst(src) :
  308. // If Src does not fit into Dst, we need to ensure the result is at most
  309. // Dst MAX.
  310. Dst(constexpr_min(src, Src(L::max()))));
  311. // clang-format on
  312. }
  313. namespace detail {
  314. // Upper/lower bound values that could be accurately represented in both
  315. // integral and float point types.
  316. constexpr double kClampCastLowerBoundDoubleToInt64F = -9223372036854774784.0;
  317. constexpr double kClampCastUpperBoundDoubleToInt64F = 9223372036854774784.0;
  318. constexpr double kClampCastUpperBoundDoubleToUInt64F = 18446744073709549568.0;
  319. constexpr float kClampCastLowerBoundFloatToInt32F = -2147483520.0f;
  320. constexpr float kClampCastUpperBoundFloatToInt32F = 2147483520.0f;
  321. constexpr float kClampCastUpperBoundFloatToUInt32F = 4294967040.0f;
  322. // This works the same as constexpr_clamp, but the comparision are done in Src
  323. // to prevent any implicit promotions.
  324. template <typename D, typename S>
  325. constexpr D constexpr_clamp_cast_helper(S src, S sl, S su, D dl, D du) {
  326. return src < sl ? dl : (src > su ? du : D(src));
  327. }
  328. } // namespace detail
  329. template <typename Dst, typename Src>
  330. constexpr typename std::enable_if<std::is_floating_point<Src>::value, Dst>::type
  331. constexpr_clamp_cast(Src src) {
  332. static_assert(
  333. std::is_integral<Dst>::value && sizeof(Dst) <= sizeof(int64_t),
  334. "constexpr_clamp_cast can only cast into integral type (up to 64bit)");
  335. using L = std::numeric_limits<Dst>;
  336. // clang-format off
  337. return
  338. // Special case: cast NaN into 0.
  339. // Using a trick here to portably check for NaN: f != f only if f is NaN.
  340. // see: https://stackoverflow.com/a/570694
  341. (src != src) ? Dst(0) :
  342. // using `sizeof(Src) > sizeof(Dst)` as a heuristic that Dst can be
  343. // represented in Src without loss of accuracy.
  344. // see: https://en.wikipedia.org/wiki/Floating-point_arithmetic
  345. sizeof(Src) > sizeof(Dst) ?
  346. detail::constexpr_clamp_cast_helper(
  347. src, Src(L::min()), Src(L::max()), L::min(), L::max()) :
  348. // sizeof(Src) < sizeof(Dst) only happens when doing cast of
  349. // 32bit float -> u/int64_t.
  350. // Losslessly promote float into double, change into double -> u/int64_t.
  351. sizeof(Src) < sizeof(Dst) ? (
  352. src >= 0.0
  353. ? constexpr_clamp_cast<Dst>(
  354. constexpr_clamp_cast<std::uint64_t>(double(src)))
  355. : constexpr_clamp_cast<Dst>(
  356. constexpr_clamp_cast<std::int64_t>(double(src)))) :
  357. // The following are for sizeof(Src) == sizeof(Dst).
  358. std::is_same<Src, double>::value && std::is_same<Dst, int64_t>::value ?
  359. detail::constexpr_clamp_cast_helper(
  360. double(src),
  361. detail::kClampCastLowerBoundDoubleToInt64F,
  362. detail::kClampCastUpperBoundDoubleToInt64F,
  363. L::min(),
  364. L::max()) :
  365. std::is_same<Src, double>::value && std::is_same<Dst, uint64_t>::value ?
  366. detail::constexpr_clamp_cast_helper(
  367. double(src),
  368. 0.0,
  369. detail::kClampCastUpperBoundDoubleToUInt64F,
  370. L::min(),
  371. L::max()) :
  372. std::is_same<Src, float>::value && std::is_same<Dst, int32_t>::value ?
  373. detail::constexpr_clamp_cast_helper(
  374. float(src),
  375. detail::kClampCastLowerBoundFloatToInt32F,
  376. detail::kClampCastUpperBoundFloatToInt32F,
  377. L::min(),
  378. L::max()) :
  379. detail::constexpr_clamp_cast_helper(
  380. float(src),
  381. 0.0f,
  382. detail::kClampCastUpperBoundFloatToUInt32F,
  383. L::min(),
  384. L::max());
  385. // clang-format on
  386. }
  387. } // namespace folly