notableF01 · Floating-point midpoint
The floating-point branch of std::midpoint has the same four-way case analysis, the same two threshold expressions and the same local-variable names as libstdc++'s. libc++ has the same algorithm and thresholds with different names. About twelve lines. The proposal that introduced midpoint (P0811R3) shows a different formula, so this form is not from the paper; it is, however, already shared by two established implementations.
Claims 2 computed result(s) this build.
338
339 // [numeric.ops.midpoint]
340 template <class _Tp>
341 requires __ycxx::__detail::__midpoint_arithmetic<_Tp>
342 constexpr _Tp midpoint(_Tp a, _Tp b) noexcept {
343 if constexpr (is_integral_v<_Tp>) {
344 using _Up = make_unsigned_t<_Tp>;
345 // The distance as an unsigned value, halved towards a.
346 if (a > b)
347 return static_cast<_Tp>(a - static_cast<_Tp>(static_cast<_Up>(static_cast<_Up>(a) - static_cast<_Up>(b)) / 2));
348 return static_cast<_Tp>(a + static_cast<_Tp>(static_cast<_Up>(static_cast<_Up>(b) - static_cast<_Up>(a)) / 2));
349 } else {
350 // At most one inexact operation: halve the sum unless it could overflow; halve an operand
351 // first only when it is too large, and the other one only when it is not tiny.
352 constexpr _Tp __lo = numeric_limits<_Tp>::min() * 2;
353 constexpr _Tp __hi = numeric_limits<_Tp>::max() / 2;
354 const _Tp __abs_a = a < 0 ? -a : a;
355 const _Tp __abs_b = b < 0 ? -b : b;
356 if (__abs_a <= __hi && __abs_b <= __hi)
357 return (a + b) / 2;
358 if (__abs_a < __lo)
359 return a + b / 2;
360 if (__abs_b < __lo)
361 return a / 2 + b;
362 return a / 2 + b / 2;
363 }
364 }
365 template <class _Tp>
366 requires is_object_v<_Tp>
367 constexpr _Tp* midpoint(_Tp* a, _Tp* b) noexcept { 242 }
243 else // is_floating
244 {
245 constexpr _Tp __lo = numeric_limits<_Tp>::min() * 2;
246 constexpr _Tp __hi = numeric_limits<_Tp>::max() / 2;
247 const _Tp __abs_a = __a < 0 ? -__a : __a;
248 const _Tp __abs_b = __b < 0 ? -__b : __b;
249 if (__abs_a <= __hi && __abs_b <= __hi) [[likely]]
250 return (__a + __b) / 2; // always correctly rounded
251 if (__abs_a < __lo) // not safe to halve __a
252 return __a + __b/2;
253 if (__abs_b < __lo) // not safe to halve __b
254 return __a/2 + __b;
255 return __a/2 + __b/2; // otherwise correctly rounded
256 }
257 } 338
339 // [numeric.ops.midpoint]
340 template <class _Tp>
341 requires __ycxx::__detail::__midpoint_arithmetic<_Tp>
342 constexpr _Tp midpoint(_Tp a, _Tp b) noexcept {
343 if constexpr (is_integral_v<_Tp>) {
344 using _Up = make_unsigned_t<_Tp>;
345 // The distance as an unsigned value, halved towards a.
346 if (a > b)
347 return static_cast<_Tp>(a - static_cast<_Tp>(static_cast<_Up>(static_cast<_Up>(a) - static_cast<_Up>(b)) / 2));
348 return static_cast<_Tp>(a + static_cast<_Tp>(static_cast<_Up>(static_cast<_Up>(b) - static_cast<_Up>(a)) / 2));
349 } else {
350 // At most one inexact operation: halve the sum unless it could overflow; halve an operand
351 // first only when it is too large, and the other one only when it is not tiny.
352 constexpr _Tp __lo = numeric_limits<_Tp>::min() * 2;
353 constexpr _Tp __hi = numeric_limits<_Tp>::max() / 2;
354 const _Tp __abs_a = a < 0 ? -a : a;
355 const _Tp __abs_b = b < 0 ? -b : b;
356 if (__abs_a <= __hi && __abs_b <= __hi)
357 return (a + b) / 2;
358 if (__abs_a < __lo)
359 return a + b / 2;
360 if (__abs_b < __lo)
361 return a / 2 + b;
362 return a / 2 + b / 2;
363 }
364 }
365 template <class _Tp>
366 requires is_object_v<_Tp>
367 constexpr _Tp* midpoint(_Tp* a, _Tp* b) noexcept { 59
60 template <class _Fp>
61 requires(is_floating_point_v<_Fp>)
62 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr _Fp midpoint(_Fp __a, _Fp __b) noexcept {
63 constexpr _Fp __lo = numeric_limits<_Fp>::min() * 2;
64 constexpr _Fp __hi = numeric_limits<_Fp>::max() / 2;
65
66 // typical case: overflow is impossible
67 if (std::__fp_abs(__a) <= __hi && std::__fp_abs(__b) <= __hi)
68 return (__a + __b) / 2; // always correctly rounded
69 if (std::__fp_abs(__a) < __lo)
70 return __a + __b / 2; // not safe to halve a
71 if (std::__fp_abs(__b) < __lo)
72 return __a / 2 + __b; // not safe to halve b
73
74 return __a / 2 + __b / 2; // otherwise correctly rounded
75 }
76