-
Notifications
You must be signed in to change notification settings - Fork 15.2k
[libc][math] Improve performance of double precision trig functions. #111793
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
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
fee360a
[libc][math] Improve performance of double precision trig functions.
lntue 2f10f9c
Remove excessive `NO_FMA`.
lntue 6c1cc4c
Fix formatting.
lntue cbced2b
Address comments.
lntue ed5f066
Remove extra ; in comments.
lntue 56d5c65
Fix quick_mult.
lntue 4450590
Fix exact_mult call in pow.cpp.
lntue File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -18,6 +18,8 @@ | |
| namespace LIBC_NAMESPACE_DECL { | ||
| namespace fputil { | ||
|
|
||
| #define DEFAULT_DOUBLE_SPLIT 27 | ||
|
|
||
| using DoubleDouble = LIBC_NAMESPACE::NumberPair<double>; | ||
|
|
||
| // The output of Dekker's FastTwoSum algorithm is correct, i.e.: | ||
|
|
@@ -61,7 +63,8 @@ LIBC_INLINE constexpr DoubleDouble add(const DoubleDouble &a, double b) { | |
| // Zimmermann, P., "Note on the Veltkamp/Dekker Algorithms with Directed | ||
| // Roundings," https://inria.hal.science/hal-04480440. | ||
| // Default splitting constant = 2^ceil(prec(double)/2) + 1 = 2^27 + 1. | ||
| template <size_t N = 27> LIBC_INLINE constexpr DoubleDouble split(double a) { | ||
| template <size_t N = DEFAULT_DOUBLE_SPLIT> | ||
| LIBC_INLINE constexpr DoubleDouble split(double a) { | ||
| DoubleDouble r{0.0, 0.0}; | ||
| // CN = 2^N. | ||
| constexpr double CN = static_cast<double>(1 << N); | ||
|
|
@@ -73,14 +76,30 @@ template <size_t N = 27> LIBC_INLINE constexpr DoubleDouble split(double a) { | |
| return r; | ||
| } | ||
|
|
||
| // Helper for non-fma exact mult where the first number is already split. | ||
| template <size_t SPLIT_B = DEFAULT_DOUBLE_SPLIT> | ||
| LIBC_INLINE DoubleDouble exact_mult(const DoubleDouble &as, double a, | ||
| double b) { | ||
| DoubleDouble bs = split<SPLIT_B>(b); | ||
| DoubleDouble r{0.0, 0.0}; | ||
|
|
||
| r.hi = a * b; | ||
| double t1 = as.hi * bs.hi - r.hi; | ||
| double t2 = as.hi * bs.lo + t1; | ||
| double t3 = as.lo * bs.hi + t2; | ||
| r.lo = as.lo * bs.lo + t3; | ||
|
|
||
| return r; | ||
| } | ||
|
|
||
| // Note: When FMA instruction is not available, the `exact_mult` function is | ||
| // only correct for round-to-nearest mode. See: | ||
| // Zimmermann, P., "Note on the Veltkamp/Dekker Algorithms with Directed | ||
| // Roundings," https://inria.hal.science/hal-04480440. | ||
| // Using Theorem 1 in the paper above, without FMA instruction, if we restrict | ||
| // the generated constants to precision <= 51, and splitting it by 2^28 + 1, | ||
| // then a * b = r.hi + r.lo is exact for all rounding modes. | ||
| template <bool NO_FMA_ALL_ROUNDINGS = false> | ||
| template <size_t SPLIT_B = 27> | ||
| LIBC_INLINE DoubleDouble exact_mult(double a, double b) { | ||
| DoubleDouble r{0.0, 0.0}; | ||
|
|
||
|
|
@@ -90,18 +109,8 @@ LIBC_INLINE DoubleDouble exact_mult(double a, double b) { | |
| #else | ||
| // Dekker's Product. | ||
| DoubleDouble as = split(a); | ||
| DoubleDouble bs; | ||
|
|
||
| if constexpr (NO_FMA_ALL_ROUNDINGS) | ||
| bs = split<28>(b); | ||
| else | ||
| bs = split(b); | ||
|
|
||
| r.hi = a * b; | ||
| double t1 = as.hi * bs.hi - r.hi; | ||
| double t2 = as.hi * bs.lo + t1; | ||
| double t3 = as.lo * bs.hi + t2; | ||
| r.lo = as.lo * bs.lo + t3; | ||
| r = exact_mult<SPLIT_B>(as, a, b); | ||
| #endif // LIBC_TARGET_CPU_HAS_FMA | ||
|
|
||
| return r; | ||
|
|
@@ -113,10 +122,10 @@ LIBC_INLINE DoubleDouble quick_mult(double a, const DoubleDouble &b) { | |
| return r; | ||
| } | ||
|
|
||
| template <bool NO_FMA_ALL_ROUNDINGS = false> | ||
| template <size_t SPLIT_B = 27> | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. template <size_t SPLIT_B = DEFAULT_DOUBLE_SPLIT> |
||
| LIBC_INLINE DoubleDouble quick_mult(const DoubleDouble &a, | ||
| const DoubleDouble &b) { | ||
| DoubleDouble r = exact_mult<NO_FMA_ALL_ROUNDINGS>(a.hi, b.hi); | ||
| DoubleDouble r = exact_mult<SPLIT_B>(a.hi, b.hi); | ||
| double t1 = multiply_add(a.hi, b.lo, r.lo); | ||
| double t2 = multiply_add(a.lo, b.hi, t1); | ||
| r.lo = t2; | ||
|
|
@@ -157,8 +166,8 @@ LIBC_INLINE DoubleDouble div(const DoubleDouble &a, const DoubleDouble &b) { | |
| double e_hi = fputil::multiply_add(b.hi, -r.hi, a.hi); | ||
| double e_lo = fputil::multiply_add(b.lo, -r.hi, a.lo); | ||
| #else | ||
| DoubleDouble b_hi_r_hi = fputil::exact_mult</*NO_FMA=*/true>(b.hi, -r.hi); | ||
| DoubleDouble b_lo_r_hi = fputil::exact_mult</*NO_FMA=*/true>(b.lo, -r.hi); | ||
| DoubleDouble b_hi_r_hi = fputil::exact_mult(b.hi, -r.hi); | ||
| DoubleDouble b_lo_r_hi = fputil::exact_mult(b.lo, -r.hi); | ||
| double e_hi = (a.hi + b_hi_r_hi.hi) + b_hi_r_hi.lo; | ||
| double e_lo = (a.lo + b_lo_r_hi.hi) + b_lo_r_hi.lo; | ||
| #endif // LIBC_TARGET_CPU_HAS_FMA | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.