numeric 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593
  1. // -*- C++ -*-
  2. //===---------------------------- numeric ---------------------------------===//
  3. //
  4. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  5. // See https://llvm.org/LICENSE.txt for license information.
  6. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  7. //
  8. //===----------------------------------------------------------------------===//
  9. #ifndef _LIBCPP_NUMERIC
  10. #define _LIBCPP_NUMERIC
  11. /*
  12. numeric synopsis
  13. namespace std
  14. {
  15. template <class InputIterator, class T>
  16. T
  17. accumulate(InputIterator first, InputIterator last, T init);
  18. template <class InputIterator, class T, class BinaryOperation>
  19. T
  20. accumulate(InputIterator first, InputIterator last, T init, BinaryOperation binary_op);
  21. template<class InputIterator>
  22. typename iterator_traits<InputIterator>::value_type
  23. reduce(InputIterator first, InputIterator last); // C++17
  24. template<class InputIterator, class T>
  25. T
  26. reduce(InputIterator first, InputIterator last, T init); // C++17
  27. template<class InputIterator, class T, class BinaryOperation>
  28. T
  29. reduce(InputIterator first, InputIterator last, T init, BinaryOperation binary_op); // C++17
  30. template <class InputIterator1, class InputIterator2, class T>
  31. T
  32. inner_product(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, T init);
  33. template <class InputIterator1, class InputIterator2, class T, class BinaryOperation1, class BinaryOperation2>
  34. T
  35. inner_product(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2,
  36. T init, BinaryOperation1 binary_op1, BinaryOperation2 binary_op2);
  37. template<class InputIterator1, class InputIterator2, class T>
  38. T
  39. transform_reduce(InputIterator1 first1, InputIterator1 last1,
  40. InputIterator2 first2, T init); // C++17
  41. template<class InputIterator1, class InputIterator2, class T, class BinaryOperation1, class BinaryOperation2>
  42. T
  43. transform_reduce(InputIterator1 first1, InputIterator1 last1,
  44. InputIterator2 first2, T init,
  45. BinaryOperation1 binary_op1, BinaryOperation2 binary_op2); // C++17
  46. template<class InputIterator, class T, class BinaryOperation, class UnaryOperation>
  47. T
  48. transform_reduce(InputIterator first, InputIterator last, T init,
  49. BinaryOperation binary_op, UnaryOperation unary_op); // C++17
  50. template <class InputIterator, class OutputIterator>
  51. OutputIterator
  52. partial_sum(InputIterator first, InputIterator last, OutputIterator result);
  53. template <class InputIterator, class OutputIterator, class BinaryOperation>
  54. OutputIterator
  55. partial_sum(InputIterator first, InputIterator last, OutputIterator result, BinaryOperation binary_op);
  56. template<class InputIterator, class OutputIterator, class T>
  57. OutputIterator
  58. exclusive_scan(InputIterator first, InputIterator last,
  59. OutputIterator result, T init); // C++17
  60. template<class InputIterator, class OutputIterator, class T, class BinaryOperation>
  61. OutputIterator
  62. exclusive_scan(InputIterator first, InputIterator last,
  63. OutputIterator result, T init, BinaryOperation binary_op); // C++17
  64. template<class InputIterator, class OutputIterator>
  65. OutputIterator
  66. inclusive_scan(InputIterator first, InputIterator last, OutputIterator result); // C++17
  67. template<class InputIterator, class OutputIterator, class BinaryOperation>
  68. OutputIterator
  69. inclusive_scan(InputIterator first, InputIterator last,
  70. OutputIterator result, BinaryOperation binary_op); // C++17
  71. template<class InputIterator, class OutputIterator, class BinaryOperation, class T>
  72. OutputIterator
  73. inclusive_scan(InputIterator first, InputIterator last,
  74. OutputIterator result, BinaryOperation binary_op, T init); // C++17
  75. template<class InputIterator, class OutputIterator, class T,
  76. class BinaryOperation, class UnaryOperation>
  77. OutputIterator
  78. transform_exclusive_scan(InputIterator first, InputIterator last,
  79. OutputIterator result, T init,
  80. BinaryOperation binary_op, UnaryOperation unary_op); // C++17
  81. template<class InputIterator, class OutputIterator,
  82. class BinaryOperation, class UnaryOperation>
  83. OutputIterator
  84. transform_inclusive_scan(InputIterator first, InputIterator last,
  85. OutputIterator result,
  86. BinaryOperation binary_op, UnaryOperation unary_op); // C++17
  87. template<class InputIterator, class OutputIterator,
  88. class BinaryOperation, class UnaryOperation, class T>
  89. OutputIterator
  90. transform_inclusive_scan(InputIterator first, InputIterator last,
  91. OutputIterator result,
  92. BinaryOperation binary_op, UnaryOperation unary_op,
  93. T init); // C++17
  94. template <class InputIterator, class OutputIterator>
  95. OutputIterator
  96. adjacent_difference(InputIterator first, InputIterator last, OutputIterator result);
  97. template <class InputIterator, class OutputIterator, class BinaryOperation>
  98. OutputIterator
  99. adjacent_difference(InputIterator first, InputIterator last, OutputIterator result, BinaryOperation binary_op);
  100. template <class ForwardIterator, class T>
  101. void iota(ForwardIterator first, ForwardIterator last, T value);
  102. template <class M, class N>
  103. constexpr common_type_t<M,N> gcd(M m, N n); // C++17
  104. template <class M, class N>
  105. constexpr common_type_t<M,N> lcm(M m, N n); // C++17
  106. integer midpoint(integer a, integer b); // C++20
  107. pointer midpoint(pointer a, pointer b); // C++20
  108. floating_point midpoint(floating_point a, floating_point b); // C++20
  109. } // std
  110. */
  111. #include <__config>
  112. #include <iterator>
  113. #include <limits> // for numeric_limits
  114. #include <functional>
  115. #include <cmath> // for isnormal
  116. #include <version>
  117. #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
  118. #pragma GCC system_header
  119. #endif
  120. _LIBCPP_PUSH_MACROS
  121. #include <__undef_macros>
  122. _LIBCPP_BEGIN_NAMESPACE_STD
  123. template <class _InputIterator, class _Tp>
  124. inline _LIBCPP_INLINE_VISIBILITY
  125. _Tp
  126. accumulate(_InputIterator __first, _InputIterator __last, _Tp __init)
  127. {
  128. for (; __first != __last; ++__first)
  129. __init = __init + *__first;
  130. return __init;
  131. }
  132. template <class _InputIterator, class _Tp, class _BinaryOperation>
  133. inline _LIBCPP_INLINE_VISIBILITY
  134. _Tp
  135. accumulate(_InputIterator __first, _InputIterator __last, _Tp __init, _BinaryOperation __binary_op)
  136. {
  137. for (; __first != __last; ++__first)
  138. __init = __binary_op(__init, *__first);
  139. return __init;
  140. }
  141. #if _LIBCPP_STD_VER > 14
  142. template <class _InputIterator, class _Tp, class _BinaryOp>
  143. inline _LIBCPP_INLINE_VISIBILITY
  144. _Tp
  145. reduce(_InputIterator __first, _InputIterator __last, _Tp __init, _BinaryOp __b)
  146. {
  147. for (; __first != __last; ++__first)
  148. __init = __b(__init, *__first);
  149. return __init;
  150. }
  151. template <class _InputIterator, class _Tp>
  152. inline _LIBCPP_INLINE_VISIBILITY
  153. _Tp
  154. reduce(_InputIterator __first, _InputIterator __last, _Tp __init)
  155. {
  156. return _VSTD::reduce(__first, __last, __init, _VSTD::plus<>());
  157. }
  158. template <class _InputIterator>
  159. inline _LIBCPP_INLINE_VISIBILITY
  160. typename iterator_traits<_InputIterator>::value_type
  161. reduce(_InputIterator __first, _InputIterator __last)
  162. {
  163. return _VSTD::reduce(__first, __last,
  164. typename iterator_traits<_InputIterator>::value_type{});
  165. }
  166. #endif
  167. template <class _InputIterator1, class _InputIterator2, class _Tp>
  168. inline _LIBCPP_INLINE_VISIBILITY
  169. _Tp
  170. inner_product(_InputIterator1 __first1, _InputIterator1 __last1, _InputIterator2 __first2, _Tp __init)
  171. {
  172. for (; __first1 != __last1; ++__first1, (void) ++__first2)
  173. __init = __init + *__first1 * *__first2;
  174. return __init;
  175. }
  176. template <class _InputIterator1, class _InputIterator2, class _Tp, class _BinaryOperation1, class _BinaryOperation2>
  177. inline _LIBCPP_INLINE_VISIBILITY
  178. _Tp
  179. inner_product(_InputIterator1 __first1, _InputIterator1 __last1, _InputIterator2 __first2,
  180. _Tp __init, _BinaryOperation1 __binary_op1, _BinaryOperation2 __binary_op2)
  181. {
  182. for (; __first1 != __last1; ++__first1, (void) ++__first2)
  183. __init = __binary_op1(__init, __binary_op2(*__first1, *__first2));
  184. return __init;
  185. }
  186. #if _LIBCPP_STD_VER > 14
  187. template <class _InputIterator, class _Tp, class _BinaryOp, class _UnaryOp>
  188. inline _LIBCPP_INLINE_VISIBILITY
  189. _Tp
  190. transform_reduce(_InputIterator __first, _InputIterator __last,
  191. _Tp __init, _BinaryOp __b, _UnaryOp __u)
  192. {
  193. for (; __first != __last; ++__first)
  194. __init = __b(__init, __u(*__first));
  195. return __init;
  196. }
  197. template <class _InputIterator1, class _InputIterator2,
  198. class _Tp, class _BinaryOp1, class _BinaryOp2>
  199. inline _LIBCPP_INLINE_VISIBILITY
  200. _Tp
  201. transform_reduce(_InputIterator1 __first1, _InputIterator1 __last1,
  202. _InputIterator2 __first2, _Tp __init, _BinaryOp1 __b1, _BinaryOp2 __b2)
  203. {
  204. for (; __first1 != __last1; ++__first1, (void) ++__first2)
  205. __init = __b1(__init, __b2(*__first1, *__first2));
  206. return __init;
  207. }
  208. template <class _InputIterator1, class _InputIterator2, class _Tp>
  209. inline _LIBCPP_INLINE_VISIBILITY
  210. _Tp
  211. transform_reduce(_InputIterator1 __first1, _InputIterator1 __last1,
  212. _InputIterator2 __first2, _Tp __init)
  213. {
  214. return _VSTD::transform_reduce(__first1, __last1, __first2, _VSTD::move(__init),
  215. _VSTD::plus<>(), _VSTD::multiplies<>());
  216. }
  217. #endif
  218. template <class _InputIterator, class _OutputIterator>
  219. inline _LIBCPP_INLINE_VISIBILITY
  220. _OutputIterator
  221. partial_sum(_InputIterator __first, _InputIterator __last, _OutputIterator __result)
  222. {
  223. if (__first != __last)
  224. {
  225. typename iterator_traits<_InputIterator>::value_type __t(*__first);
  226. *__result = __t;
  227. for (++__first, (void) ++__result; __first != __last; ++__first, (void) ++__result)
  228. {
  229. __t = __t + *__first;
  230. *__result = __t;
  231. }
  232. }
  233. return __result;
  234. }
  235. template <class _InputIterator, class _OutputIterator, class _BinaryOperation>
  236. inline _LIBCPP_INLINE_VISIBILITY
  237. _OutputIterator
  238. partial_sum(_InputIterator __first, _InputIterator __last, _OutputIterator __result,
  239. _BinaryOperation __binary_op)
  240. {
  241. if (__first != __last)
  242. {
  243. typename iterator_traits<_InputIterator>::value_type __t(*__first);
  244. *__result = __t;
  245. for (++__first, (void) ++__result; __first != __last; ++__first, (void) ++__result)
  246. {
  247. __t = __binary_op(__t, *__first);
  248. *__result = __t;
  249. }
  250. }
  251. return __result;
  252. }
  253. #if _LIBCPP_STD_VER > 14
  254. template <class _InputIterator, class _OutputIterator, class _Tp, class _BinaryOp>
  255. inline _LIBCPP_INLINE_VISIBILITY
  256. _OutputIterator
  257. exclusive_scan(_InputIterator __first, _InputIterator __last,
  258. _OutputIterator __result, _Tp __init, _BinaryOp __b)
  259. {
  260. if (__first != __last)
  261. {
  262. _Tp __saved = __init;
  263. do
  264. {
  265. __init = __b(__init, *__first);
  266. *__result = __saved;
  267. __saved = __init;
  268. ++__result;
  269. } while (++__first != __last);
  270. }
  271. return __result;
  272. }
  273. template <class _InputIterator, class _OutputIterator, class _Tp>
  274. inline _LIBCPP_INLINE_VISIBILITY
  275. _OutputIterator
  276. exclusive_scan(_InputIterator __first, _InputIterator __last,
  277. _OutputIterator __result, _Tp __init)
  278. {
  279. return _VSTD::exclusive_scan(__first, __last, __result, __init, _VSTD::plus<>());
  280. }
  281. template <class _InputIterator, class _OutputIterator, class _Tp, class _BinaryOp>
  282. _OutputIterator inclusive_scan(_InputIterator __first, _InputIterator __last,
  283. _OutputIterator __result, _BinaryOp __b, _Tp __init)
  284. {
  285. for (; __first != __last; ++__first, (void) ++__result) {
  286. __init = __b(__init, *__first);
  287. *__result = __init;
  288. }
  289. return __result;
  290. }
  291. template <class _InputIterator, class _OutputIterator, class _BinaryOp>
  292. _OutputIterator inclusive_scan(_InputIterator __first, _InputIterator __last,
  293. _OutputIterator __result, _BinaryOp __b)
  294. {
  295. if (__first != __last) {
  296. typename std::iterator_traits<_InputIterator>::value_type __init = *__first;
  297. *__result++ = __init;
  298. if (++__first != __last)
  299. return _VSTD::inclusive_scan(__first, __last, __result, __b, __init);
  300. }
  301. return __result;
  302. }
  303. template <class _InputIterator, class _OutputIterator>
  304. _OutputIterator inclusive_scan(_InputIterator __first, _InputIterator __last,
  305. _OutputIterator __result)
  306. {
  307. return _VSTD::inclusive_scan(__first, __last, __result, std::plus<>());
  308. }
  309. template <class _InputIterator, class _OutputIterator, class _Tp,
  310. class _BinaryOp, class _UnaryOp>
  311. inline _LIBCPP_INLINE_VISIBILITY
  312. _OutputIterator
  313. transform_exclusive_scan(_InputIterator __first, _InputIterator __last,
  314. _OutputIterator __result, _Tp __init,
  315. _BinaryOp __b, _UnaryOp __u)
  316. {
  317. if (__first != __last)
  318. {
  319. _Tp __saved = __init;
  320. do
  321. {
  322. __init = __b(__init, __u(*__first));
  323. *__result = __saved;
  324. __saved = __init;
  325. ++__result;
  326. } while (++__first != __last);
  327. }
  328. return __result;
  329. }
  330. template <class _InputIterator, class _OutputIterator, class _Tp, class _BinaryOp, class _UnaryOp>
  331. _OutputIterator transform_inclusive_scan(_InputIterator __first, _InputIterator __last,
  332. _OutputIterator __result, _BinaryOp __b, _UnaryOp __u, _Tp __init)
  333. {
  334. for (; __first != __last; ++__first, (void) ++__result) {
  335. __init = __b(__init, __u(*__first));
  336. *__result = __init;
  337. }
  338. return __result;
  339. }
  340. template <class _InputIterator, class _OutputIterator, class _BinaryOp, class _UnaryOp>
  341. _OutputIterator transform_inclusive_scan(_InputIterator __first, _InputIterator __last,
  342. _OutputIterator __result, _BinaryOp __b, _UnaryOp __u)
  343. {
  344. if (__first != __last) {
  345. typename std::iterator_traits<_InputIterator>::value_type __init = __u(*__first);
  346. *__result++ = __init;
  347. if (++__first != __last)
  348. return _VSTD::transform_inclusive_scan(__first, __last, __result, __b, __u, __init);
  349. }
  350. return __result;
  351. }
  352. #endif
  353. template <class _InputIterator, class _OutputIterator>
  354. inline _LIBCPP_INLINE_VISIBILITY
  355. _OutputIterator
  356. adjacent_difference(_InputIterator __first, _InputIterator __last, _OutputIterator __result)
  357. {
  358. if (__first != __last)
  359. {
  360. typename iterator_traits<_InputIterator>::value_type __t1(*__first);
  361. *__result = __t1;
  362. for (++__first, (void) ++__result; __first != __last; ++__first, (void) ++__result)
  363. {
  364. typename iterator_traits<_InputIterator>::value_type __t2(*__first);
  365. *__result = __t2 - __t1;
  366. __t1 = _VSTD::move(__t2);
  367. }
  368. }
  369. return __result;
  370. }
  371. template <class _InputIterator, class _OutputIterator, class _BinaryOperation>
  372. inline _LIBCPP_INLINE_VISIBILITY
  373. _OutputIterator
  374. adjacent_difference(_InputIterator __first, _InputIterator __last, _OutputIterator __result,
  375. _BinaryOperation __binary_op)
  376. {
  377. if (__first != __last)
  378. {
  379. typename iterator_traits<_InputIterator>::value_type __t1(*__first);
  380. *__result = __t1;
  381. for (++__first, (void) ++__result; __first != __last; ++__first, (void) ++__result)
  382. {
  383. typename iterator_traits<_InputIterator>::value_type __t2(*__first);
  384. *__result = __binary_op(__t2, __t1);
  385. __t1 = _VSTD::move(__t2);
  386. }
  387. }
  388. return __result;
  389. }
  390. template <class _ForwardIterator, class _Tp>
  391. inline _LIBCPP_INLINE_VISIBILITY
  392. void
  393. iota(_ForwardIterator __first, _ForwardIterator __last, _Tp __value_)
  394. {
  395. for (; __first != __last; ++__first, (void) ++__value_)
  396. *__first = __value_;
  397. }
  398. #if _LIBCPP_STD_VER > 14
  399. template <typename _Result, typename _Source, bool _IsSigned = is_signed<_Source>::value> struct __ct_abs;
  400. template <typename _Result, typename _Source>
  401. struct __ct_abs<_Result, _Source, true> {
  402. _LIBCPP_CONSTEXPR _LIBCPP_INLINE_VISIBILITY
  403. _Result operator()(_Source __t) const noexcept
  404. {
  405. if (__t >= 0) return __t;
  406. if (__t == numeric_limits<_Source>::min()) return -static_cast<_Result>(__t);
  407. return -__t;
  408. }
  409. };
  410. template <typename _Result, typename _Source>
  411. struct __ct_abs<_Result, _Source, false> {
  412. _LIBCPP_CONSTEXPR _LIBCPP_INLINE_VISIBILITY
  413. _Result operator()(_Source __t) const noexcept { return __t; }
  414. };
  415. template<class _Tp>
  416. _LIBCPP_CONSTEXPR _LIBCPP_HIDDEN
  417. _Tp __gcd(_Tp __m, _Tp __n)
  418. {
  419. static_assert((!is_signed<_Tp>::value), "");
  420. return __n == 0 ? __m : _VSTD::__gcd<_Tp>(__n, __m % __n);
  421. }
  422. template<class _Tp, class _Up>
  423. _LIBCPP_CONSTEXPR _LIBCPP_INLINE_VISIBILITY
  424. common_type_t<_Tp,_Up>
  425. gcd(_Tp __m, _Up __n)
  426. {
  427. static_assert((is_integral<_Tp>::value && is_integral<_Up>::value), "Arguments to gcd must be integer types");
  428. static_assert((!is_same<typename remove_cv<_Tp>::type, bool>::value), "First argument to gcd cannot be bool" );
  429. static_assert((!is_same<typename remove_cv<_Up>::type, bool>::value), "Second argument to gcd cannot be bool" );
  430. using _Rp = common_type_t<_Tp,_Up>;
  431. using _Wp = make_unsigned_t<_Rp>;
  432. return static_cast<_Rp>(_VSTD::__gcd(
  433. static_cast<_Wp>(__ct_abs<_Rp, _Tp>()(__m)),
  434. static_cast<_Wp>(__ct_abs<_Rp, _Up>()(__n))));
  435. }
  436. template<class _Tp, class _Up>
  437. _LIBCPP_CONSTEXPR _LIBCPP_INLINE_VISIBILITY
  438. common_type_t<_Tp,_Up>
  439. lcm(_Tp __m, _Up __n)
  440. {
  441. static_assert((is_integral<_Tp>::value && is_integral<_Up>::value), "Arguments to lcm must be integer types");
  442. static_assert((!is_same<typename remove_cv<_Tp>::type, bool>::value), "First argument to lcm cannot be bool" );
  443. static_assert((!is_same<typename remove_cv<_Up>::type, bool>::value), "Second argument to lcm cannot be bool" );
  444. if (__m == 0 || __n == 0)
  445. return 0;
  446. using _Rp = common_type_t<_Tp,_Up>;
  447. _Rp __val1 = __ct_abs<_Rp, _Tp>()(__m) / _VSTD::gcd(__m, __n);
  448. _Rp __val2 = __ct_abs<_Rp, _Up>()(__n);
  449. _LIBCPP_ASSERT((numeric_limits<_Rp>::max() / __val1 > __val2), "Overflow in lcm");
  450. return __val1 * __val2;
  451. }
  452. #endif /* _LIBCPP_STD_VER > 14 */
  453. #if _LIBCPP_STD_VER > 17
  454. template <class _Tp>
  455. _LIBCPP_INLINE_VISIBILITY constexpr
  456. enable_if_t<is_integral_v<_Tp> && !is_same_v<bool, _Tp> && !is_null_pointer_v<_Tp>, _Tp>
  457. midpoint(_Tp __a, _Tp __b) noexcept
  458. _LIBCPP_DISABLE_UBSAN_UNSIGNED_INTEGER_CHECK
  459. {
  460. using _Up = std::make_unsigned_t<_Tp>;
  461. int __sign = 1;
  462. _Up __m = __a;
  463. _Up __M = __b;
  464. if (__a > __b)
  465. {
  466. __sign = -1;
  467. __m = __b;
  468. __M = __a;
  469. }
  470. return __a + __sign * _Tp(_Up(__M-__m) >> 1);
  471. }
  472. template <class _TPtr>
  473. _LIBCPP_INLINE_VISIBILITY constexpr
  474. enable_if_t<is_pointer_v<_TPtr>
  475. && is_object_v<remove_pointer_t<_TPtr>>
  476. && ! is_void_v<remove_pointer_t<_TPtr>>
  477. && (sizeof(remove_pointer_t<_TPtr>) > 0), _TPtr>
  478. midpoint(_TPtr __a, _TPtr __b) noexcept
  479. {
  480. return __a + _VSTD::midpoint(ptrdiff_t(0), __b - __a);
  481. }
  482. template <typename _Tp>
  483. constexpr int __sign(_Tp __val) {
  484. return (_Tp(0) < __val) - (__val < _Tp(0));
  485. }
  486. template <typename _Fp>
  487. constexpr _Fp __fp_abs(_Fp __f) { return __f >= 0 ? __f : -__f; }
  488. template <class _Fp>
  489. _LIBCPP_INLINE_VISIBILITY constexpr
  490. enable_if_t<is_floating_point_v<_Fp>, _Fp>
  491. midpoint(_Fp __a, _Fp __b) noexcept
  492. {
  493. constexpr _Fp __lo = numeric_limits<_Fp>::min()*2;
  494. constexpr _Fp __hi = numeric_limits<_Fp>::max()/2;
  495. return __fp_abs(__a) <= __hi && __fp_abs(__b) <= __hi ? // typical case: overflow is impossible
  496. (__a + __b)/2 : // always correctly rounded
  497. __fp_abs(__a) < __lo ? __a + __b/2 : // not safe to halve a
  498. __fp_abs(__a) < __lo ? __a/2 + __b : // not safe to halve b
  499. __a/2 + __b/2; // otherwise correctly rounded
  500. }
  501. #endif // _LIBCPP_STD_VER > 17
  502. _LIBCPP_END_NAMESPACE_STD
  503. _LIBCPP_POP_MACROS
  504. #if defined(_LIBCPP_HAS_PARALLEL_ALGORITHMS) && _LIBCPP_STD_VER >= 17
  505. # include <__pstl_numeric>
  506. #endif
  507. #endif // _LIBCPP_NUMERIC