-
Notifications
You must be signed in to change notification settings - Fork 741
rsz: Reduce error in lerp formula #7604
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
rsz: Reduce error in lerp formula #7604
Conversation
Signed-off-by: Martin Povišer <[email protected]>
Signed-off-by: Martin Povišer <[email protected]>
|
clang-tidy review says "All clean, LGTM! 👍" |
|
Secure CI came back green |
| return slack - (ref_slack_ - slack) * 2e-6; | ||
| } | ||
|
|
||
| float lerp(float a, float b, float t) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Use std::lerp
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I purposely open-coded it so we can see the formula and reason about the rounding errors (though I'm not fully sure compiler cannot rewrite it in optimization)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Note the std version handles more corner cases but perhaps they don't arise here.
template<typename _Fp>
constexpr _Fp
__lerp(_Fp __a, _Fp __b, _Fp __t) noexcept
{
if ((__a <= 0 && __b >= 0) || (__a >= 0 && __b <= 0))
return __t * __b + (1 - __t) * __a;
if (__t == 1)
return __b; // exact
// Exact at __t=0, monotonic except near __t=1,
// bounded, determinate, and consistent:
const _Fp __x = __a + __t * (__b - __a);
return (__t > 1) == (__b > __a)
? (__b < __x ? __x : __b)
: (__b > __x ? __x : __b); // monotonic near __t=1
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Interesting they have different cases. I assume that's just one way it may be implemented.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
They handle overflow for one. The core formula is similar.
c394f51
into
The-OpenROAD-Project:master
Adjust lerp formula to reduce the introduced rounding error and fix the failure seen at The-OpenROAD-Project/OpenROAD-flow-scripts#3242 (comment)