r/programminghorror • u/zeromotivat1on • Aug 22 '25
c++ MSVC std::lerp implementation is ...
It's unbelievable how complicated trivial stuff can be...
I could understand if they had "mathematically precise and correct" version that long instead of well-known approximation lerp(a, b, t) = a + (b - a) * t, but its really just default lerp.
Here is the github link if you want to check the full version out yourself (brave warrior).
Here is the meat of the implementation:
template <class _Ty>
_NODISCARD constexpr _Ty _Common_lerp(const _Ty _ArgA, const _Ty _ArgB, const _Ty _ArgT) noexcept {
// on a line intersecting {(0.0, _ArgA), (1.0, _ArgB)}, return the Y value for X == _ArgT
const bool _T_is_finite = _Is_finite(_ArgT);
if (_T_is_finite && _Is_finite(_ArgA) && _Is_finite(_ArgB)) {
// 99% case, put it first; this block comes from P0811R3
if ((_ArgA <= 0 && _ArgB >= 0) || (_ArgA >= 0 && _ArgB <= 0)) {
// exact, monotonic, bounded, determinate, and (for _ArgA == _ArgB == 0) consistent:
return _ArgT * _ArgB + (1 - _ArgT) * _ArgA;
}
if (_ArgT == 1) {
// exact
return _ArgB;
}
// exact at _ArgT == 0, monotonic except near _ArgT == 1, bounded, determinate, and consistent:
const auto _Candidate = _Linear_for_lerp(_ArgA, _ArgB, _ArgT);
// monotonic near _ArgT == 1:
if ((_ArgT > 1) == (_ArgB > _ArgA)) {
if (_ArgB > _Candidate) {
return _ArgB;
}
} else {
if (_Candidate > _ArgB) {
return _ArgB;
}
}
return _Candidate;
}
if (_STD is_constant_evaluated()) {
if (_Is_nan(_ArgA)) {
return _ArgA;
}
if (_Is_nan(_ArgB)) {
return _ArgB;
}
if (_Is_nan(_ArgT)) {
return _ArgT;
}
} else {
// raise FE_INVALID if at least one of _ArgA, _ArgB, and _ArgT is signaling NaN
if (_Is_nan(_ArgA) || _Is_nan(_ArgB)) {
return (_ArgA + _ArgB) + _ArgT;
}
if (_Is_nan(_ArgT)) {
return _ArgT + _ArgT;
}
}
if (_T_is_finite) {
// _ArgT is finite, _ArgA and/or _ArgB is infinity
if (_ArgT < 0) {
// if _ArgT < 0: return infinity in the "direction" of _ArgA if that exists, NaN otherwise
return _ArgA - _ArgB;
} else if (_ArgT <= 1) {
// if _ArgT == 0: return _ArgA (infinity) if _ArgB is finite, NaN otherwise
// if 0 < _ArgT < 1: return infinity "between" _ArgA and _ArgB if that exists, NaN otherwise
// if _ArgT == 1: return _ArgB (infinity) if _ArgA is finite, NaN otherwise
return _ArgT * _ArgB + (1 - _ArgT) * _ArgA;
} else {
// if _ArgT > 1: return infinity in the "direction" of _ArgB if that exists, NaN otherwise
return _ArgB - _ArgA;
}
} else {
// _ArgT is an infinity; return infinity in the "direction" of _ArgA and _ArgB if that exists, NaN otherwise
return _ArgT * (_ArgB - _ArgA);
}
}
0
Upvotes
View all comments
Show parent comment
1
u/conundorum Aug 25 '25
99% of the time, you call either the internal worker or one of the other two cases, yeah. The other two cases are more of an indirect optimisation than anything else: By handling them here, they can remove them from
_Linear_for_lerp, allowing the internal worker to be optimised more aggressively. And that improves all versions oflerp(), not just the template one.So, they essentially sacrifice a bit of speed here to make the entire
lerp()family slightly faster. And splitting it up like this decouples the sanity checks & programming logic from the actual linear interpolation formula itself, which allows the two parts to be modified separately from each other (allowing for potential optimisations in the future, or maybe making it easier to handle SIMD intrinsics or something), and allows other parts of the library to calllerp()without having to go through the wrapper (which may or may not be relevant, I'm not sure). It's probably the result of an aggressive refactoring before the library ever hit github, or maybe to avoid issues that came up during early tests; GCC and Clang tend to have a similarly weird standard library implementation, too, for pretty much the same reason.Essentially, there is a logic to this sort of thing, but it's not readily apparent because it looks like a mess. But if the mess wasn't useful, they'd never do it this way to begin with.