std::midpoint? How Hard Could it Be?
summary
P0811 adds std::midpoint to C++20 and the actual work is careful engineering: integer midpoint uses make_unsigned, swap-if-a> b and unsigned difference to avoid signed overflow, then produces a branchless unsigned arithmetic sequence; early libc++ revisions hit an LLVM codegen branch bug that gets fixed. Char signedness and promotions bite implementations, pointer overloads misselect on literal 0/null-pointer ambiguity, and floating-point handling must guard denormals, infinities and call nextafter (tests even uncover a PowerPC nextafter bug). libc++ reaches a correct, tested implementation after several revisions.
AI-written summary · about
chapters (18)
- 0:00Introduction
- 1:13Standard library additions
- 4:42Introducing std::midpoint
- 8:40Numeric types vs math
- 12:51Integral types explained
- 17:40First midpoint implementation
- 21:47Howard Hinnant’s version
- 22:29The char type problem
- 28:48Midpoint four and codegen
- 31:58Pointer midpoint implementation
- 37:10Pointer subtraction issues
- 46:21Floating-point midpoint
- 51:05Testing floating-point equality
- 55:23Testing on PowerPC long double
- 57:35Branch prediction hints
- 58:36Why not use same algorithm for floats
- 1:00:15Order sensitivity of midpoint
- 1:01:54Q&A
related talks
58mPortable floating-point calculations
59mHow to Implement the C++ Standard Library (Part 2): An Excursion into libc++
07mISO/IEC 14882
31mBringing C++ 17 Parallel Algorithms to a standard library near you
57mHow To Implement the C++ Standard Library - (Part 1 of 2)
01h 05mType Punning in C++17: Avoiding Pun-defined Behavior
06mLightning Talk: A Compiler Optimization and the 2-Adic Numbers
46mComparison is not simple, but it can be simpler
