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 __saved = __init; 339 do 340 { 341 __init = __b(__init, *__first); 342 *__result = __saved; 343 __saved = __init; 344 ++__result; 345 } while (++__first != __last); 346 } 347 return __result; 348} 349 350template <class _InputIterator, class _OutputIterator, class _Tp> 351_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17 352_OutputIterator 353exclusive_scan(_InputIterator __first, _InputIterator __last, 354 _OutputIterator __result, _Tp __init) 355{ 356 return _VSTD::exclusive_scan(__first, __last, __result, __init, _VSTD::plus<>()); 357} 358 359template <class _InputIterator, class _OutputIterator, class _Tp, class _BinaryOp> 360_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17 361_OutputIterator inclusive_scan(_InputIterator __first, _InputIterator __last, 362 _OutputIterator __result, _BinaryOp __b, _Tp __init) 363{ 364 for (; __first != __last; ++__first, (void) ++__result) { 365 __init = __b(__init, *__first); 366 *__result = __init; 367 } 368 return __result; 369} 370 371template <class _InputIterator, class _OutputIterator, class _BinaryOp> 372_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17 373_OutputIterator inclusive_scan(_InputIterator __first, _InputIterator __last, 374 _OutputIterator __result, _BinaryOp __b) 375{ 376 if (__first != __last) { 377 typename std::iterator_traits<_InputIterator>::value_type __init = *__first; 378 *__result++ = __init; 379 if (++__first != __last) 380 return _VSTD::inclusive_scan(__first, __last, __result, __b, __init); 381 } 382 383 return __result; 384} 385 386template <class _InputIterator, class _OutputIterator> 387_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17 388_OutputIterator inclusive_scan(_InputIterator __first, _InputIterator __last, 389 _OutputIterator __result) 390{ 391 return _VSTD::inclusive_scan(__first, __last, __result, std::plus<>()); 392} 393 394template <class _InputIterator, class _OutputIterator, class _Tp, 395 class _BinaryOp, class _UnaryOp> 396_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17 397_OutputIterator 398transform_exclusive_scan(_InputIterator __first, _InputIterator __last, 399 _OutputIterator __result, _Tp __init, 400 _BinaryOp __b, _UnaryOp __u) 401{ 402 if (__first != __last) 403 { 404 _Tp __saved = __init; 405 do 406 { 407 __init = __b(__init, __u(*__first)); 408 *__result = __saved; 409 __saved = __init; 410 ++__result; 411 } while (++__first != __last); 412 } 413 return __result; 414} 415 416template <class _InputIterator, class _OutputIterator, class _Tp, class _BinaryOp, class _UnaryOp> 417_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17 418_OutputIterator 419transform_inclusive_scan(_InputIterator __first, _InputIterator __last, 420 _OutputIterator __result, _BinaryOp __b, _UnaryOp __u, _Tp __init) 421{ 422 for (; __first != __last; ++__first, (void) ++__result) { 423 __init = __b(__init, __u(*__first)); 424 *__result = __init; 425 } 426 427 return __result; 428} 429 430template <class _InputIterator, class _OutputIterator, class _BinaryOp, class _UnaryOp> 431_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17 432_OutputIterator 433transform_inclusive_scan(_InputIterator __first, _InputIterator __last, 434 _OutputIterator __result, _BinaryOp __b, _UnaryOp __u) 435{ 436 if (__first != __last) { 437 typename std::iterator_traits<_InputIterator>::value_type __init = __u(*__first); 438 *__result++ = __init; 439 if (++__first != __last) 440 return _VSTD::transform_inclusive_scan(__first, __last, __result, __b, __u, __init); 441 } 442 443 return __result; 444} 445#endif 446 447template <class _InputIterator, class _OutputIterator> 448_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17 449_OutputIterator 450adjacent_difference(_InputIterator __first, _InputIterator __last, _OutputIterator __result) 451{ 452 if (__first != __last) 453 { 454 typename iterator_traits<_InputIterator>::value_type __acc(*__first); 455 *__result = __acc; 456 for (++__first, (void) ++__result; __first != __last; ++__first, (void) ++__result) 457 { 458 typename iterator_traits<_InputIterator>::value_type __val(*__first); 459#if _LIBCPP_STD_VER > 17 460 *__result = __val - _VSTD::move(__acc); 461#else 462 *__result = __val - __acc; 463#endif 464 __acc = _VSTD::move(__val); 465 } 466 } 467 return __result; 468} 469 470template <class _InputIterator, class _OutputIterator, class _BinaryOperation> 471_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17 472_OutputIterator 473adjacent_difference(_InputIterator __first, _InputIterator __last, _OutputIterator __result, 474 _BinaryOperation __binary_op) 475{ 476 if (__first != __last) 477 { 478 typename iterator_traits<_InputIterator>::value_type __acc(*__first); 479 *__result = __acc; 480 for (++__first, (void) ++__result; __first != __last; ++__first, (void) ++__result) 481 { 482 typename iterator_traits<_InputIterator>::value_type __val(*__first); 483#if _LIBCPP_STD_VER > 17 484 *__result = __binary_op(__val, _VSTD::move(__acc)); 485#else 486 *__result = __binary_op(__val, __acc); 487#endif 488 __acc = _VSTD::move(__val); 489 } 490 } 491 return __result; 492} 493 494template <class _ForwardIterator, class _Tp> 495_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17 496void 497iota(_ForwardIterator __first, _ForwardIterator __last, _Tp __value_) 498{ 499 for (; __first != __last; ++__first, (void) ++__value_) 500 *__first = __value_; 501} 502 503 504#if _LIBCPP_STD_VER > 14 505template <typename _Result, typename _Source, bool _IsSigned = is_signed<_Source>::value> struct __ct_abs; 506 507template <typename _Result, typename _Source> 508struct __ct_abs<_Result, _Source, true> { 509 _LIBCPP_CONSTEXPR _LIBCPP_INLINE_VISIBILITY 510 _Result operator()(_Source __t) const noexcept 511 { 512 if (__t >= 0) return __t; 513 if (__t == numeric_limits<_Source>::min()) return -static_cast<_Result>(__t); 514 return -__t; 515 } 516}; 517 518template <typename _Result, typename _Source> 519struct __ct_abs<_Result, _Source, false> { 520 _LIBCPP_CONSTEXPR _LIBCPP_INLINE_VISIBILITY 521 _Result operator()(_Source __t) const noexcept { return __t; } 522}; 523 524 525template<class _Tp> 526_LIBCPP_CONSTEXPR _LIBCPP_HIDDEN 527_Tp __gcd(_Tp __m, _Tp __n) 528{ 529 static_assert((!is_signed<_Tp>::value), ""); 530 return __n == 0 ? __m : _VSTD::__gcd<_Tp>(__n, __m % __n); 531} 532 533 534template<class _Tp, class _Up> 535_LIBCPP_CONSTEXPR _LIBCPP_INLINE_VISIBILITY 536common_type_t<_Tp,_Up> 537gcd(_Tp __m, _Up __n) 538{ 539 static_assert((is_integral<_Tp>::value && is_integral<_Up>::value), "Arguments to gcd must be integer types"); 540 static_assert((!is_same<typename remove_cv<_Tp>::type, bool>::value), "First argument to gcd cannot be bool" ); 541 static_assert((!is_same<typename remove_cv<_Up>::type, bool>::value), "Second argument to gcd cannot be bool" ); 542 using _Rp = common_type_t<_Tp,_Up>; 543 using _Wp = make_unsigned_t<_Rp>; 544 return static_cast<_Rp>(_VSTD::__gcd( 545 static_cast<_Wp>(__ct_abs<_Rp, _Tp>()(__m)), 546 static_cast<_Wp>(__ct_abs<_Rp, _Up>()(__n)))); 547} 548 549template<class _Tp, class _Up> 550_LIBCPP_CONSTEXPR _LIBCPP_INLINE_VISIBILITY 551common_type_t<_Tp,_Up> 552lcm(_Tp __m, _Up __n) 553{ 554 static_assert((is_integral<_Tp>::value && is_integral<_Up>::value), "Arguments to lcm must be integer types"); 555 static_assert((!is_same<typename remove_cv<_Tp>::type, bool>::value), "First argument to lcm cannot be bool" ); 556 static_assert((!is_same<typename remove_cv<_Up>::type, bool>::value), "Second argument to lcm cannot be bool" ); 557 if (__m == 0 || __n == 0) 558 return 0; 559 560 using _Rp = common_type_t<_Tp,_Up>; 561 _Rp __val1 = __ct_abs<_Rp, _Tp>()(__m) / _VSTD::gcd(__m, __n); 562 _Rp __val2 = __ct_abs<_Rp, _Up>()(__n); 563 _LIBCPP_ASSERT((numeric_limits<_Rp>::max() / __val1 > __val2), "Overflow in lcm"); 564 return __val1 * __val2; 565} 566 567#endif /* _LIBCPP_STD_VER > 14 */ 568 569#if _LIBCPP_STD_VER > 17 570template <class _Tp> 571_LIBCPP_INLINE_VISIBILITY constexpr 572enable_if_t<is_integral_v<_Tp> && !is_same_v<bool, _Tp> && !is_null_pointer_v<_Tp>, _Tp> 573midpoint(_Tp __a, _Tp __b) noexcept 574_LIBCPP_DISABLE_UBSAN_UNSIGNED_INTEGER_CHECK 575{ 576 using _Up = std::make_unsigned_t<_Tp>; 577 constexpr _Up __bitshift = std::numeric_limits<_Up>::digits - 1; 578 579 _Up __diff = _Up(__b) - _Up(__a); 580 _Up __sign_bit = __b < __a; 581 582 _Up __half_diff = (__diff / 2) + (__sign_bit << __bitshift) + (__sign_bit & __diff); 583 584 return __a + __half_diff; 585} 586 587 588template <class _TPtr> 589_LIBCPP_INLINE_VISIBILITY constexpr 590enable_if_t<is_pointer_v<_TPtr> 591 && is_object_v<remove_pointer_t<_TPtr>> 592 && ! is_void_v<remove_pointer_t<_TPtr>> 593 && (sizeof(remove_pointer_t<_TPtr>) > 0), _TPtr> 594midpoint(_TPtr __a, _TPtr __b) noexcept 595{ 596 return __a + _VSTD::midpoint(ptrdiff_t(0), __b - __a); 597} 598 599 600template <typename _Tp> 601constexpr int __sign(_Tp __val) { 602 return (_Tp(0) < __val) - (__val < _Tp(0)); 603} 604 605template <typename _Fp> 606constexpr _Fp __fp_abs(_Fp __f) { return __f >= 0 ? __f : -__f; } 607 608template <class _Fp> 609_LIBCPP_INLINE_VISIBILITY constexpr 610enable_if_t<is_floating_point_v<_Fp>, _Fp> 611midpoint(_Fp __a, _Fp __b) noexcept 612{ 613 constexpr _Fp __lo = numeric_limits<_Fp>::min()*2; 614 constexpr _Fp __hi = numeric_limits<_Fp>::max()/2; 615 return __fp_abs(__a) <= __hi && __fp_abs(__b) <= __hi ? // typical case: overflow is impossible 616 (__a + __b)/2 : // always correctly rounded 617 __fp_abs(__a) < __lo ? __a + __b/2 : // not safe to halve a 618 __fp_abs(__b) < __lo ? __a/2 + __b : // not safe to halve b 619 __a/2 + __b/2; // otherwise correctly rounded 620} 621 622#endif // _LIBCPP_STD_VER > 17 623 624_LIBCPP_END_NAMESPACE_STD 625 626_LIBCPP_POP_MACROS 627 628#if defined(_LIBCPP_HAS_PARALLEL_ALGORITHMS) && _LIBCPP_STD_VER >= 17 629# include <__pstl_numeric> 630#endif 631 632#endif // _LIBCPP_NUMERIC 633