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