numeric 22 KB

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