Repository navigation
Compute sinh, cosh, tanh, expm1 and exp2 in fixed point and round once - #1562
Merged
Merged
Conversation
ibmibmibm
force-pushed
the
fixed-point-hyperbolic
branch
from
November 16, 2026 01:30
6410b07 to
5d15603
Compare
Codecov Report❌ Patch coverage is Additional details and impacted files@@ Coverage Diff @@
## develop #1562 +/- ##
=========================================
- Coverage 98.6% 98.4% -0.1%
=========================================
Files 344 346 +2
Lines 29218 30181 +963
Branches 2405 2495 +90
=========================================
+ Hits 28782 29697 +915
- Misses 436 484 +48
... and 3 files with indirect coverage changes Continue to review full report in Codecov by Harness.
🚀 New features to boost your workflow:
|
ibmibmibm
force-pushed
the
fixed-point-hyperbolic
branch
from
November 16, 2026 08:45
5d15603 to
24e14c1
Compare
ibmibmibm
marked this pull request as ready for review
November 16, 2026 09:55
ibmibmibm
force-pushed
the
fixed-point-hyperbolic
branch
8 times, most recently
from
November 20, 2026 08:35
03c29dc to
225c71b
Compare
sinh and cosh rounded at each step in the decimal type, so the last digit was often wrong. sinh(887) overflowed for decimal64_t, and a subnormal x gave 0. - Use the fixed point of exp: the Taylor series for |x| < 1/16, else exp(|x|) and its reciprocal. - Round the result once in the current mode. - For decimal128_t, try two words first, and use three words only next to a rounding boundary. - Next to a boundary, compute the terms after the exact lead term in their own precision. - Give the quiet NaN of a signaling NaN x. - Add fx_recip and the helpers that the hyperbolic functions share. - Remove impl/sinh_impl.hpp and impl/cosh_impl.hpp. - Add test_cmath_bound_cases with inputs next to the error bounds. Fixes boostorg#1561
tanh rounded at each step in the decimal type, so the last digit was often wrong. A subnormal x gave 0. - Use the fixed point of exp: the series of sinh and cosh for |x| < 1/16, else (1 - t) / (1 + t) with t = exp(-2|x|). - Round the result once in the current mode. - For decimal128_t, try two words first, and use three words only next to a rounding boundary. - Next to a boundary, compute the terms after the exact lead term in their own precision. For a large |x|, do the same for the small term next to 1. - Give the quiet NaN of a signaling NaN x. - Add fx_div. Remove impl/tanh_impl.hpp. Fixes boostorg#1561
expm1 rounded at each step in the decimal type, so the last digit was often wrong. A subnormal x gave 0. - Use the fixed point of exp: the series of sinh and cosh for |x| < 1/16, else exp(x) - 1 with the 1 at the scale of exp(x). - Round the result once in the current mode. - For decimal128_t and |x| < 1/16, try two words first, and use three words only next to a rounding boundary. - Next to a boundary, compute the terms after the exact lead term in their own precision. For a large negative x, do the same for exp(x) next to -1. - Give the quiet NaN of a signaling NaN x. - Remove impl/expm1_impl.hpp. Fixes boostorg#1561
exp2(x) was pow(2, x) in the decimal type, so the last digits were wrong for a large |x|. - Use the fixed point of exp with log10(2), and round once in the current mode. 2^n for an integer n is exact. - For decimal32_t and decimal64_t, give exp, exp2, sinh, cosh, tanh and expm1 a first try in one word with an error bound. More words run only next to a rounding boundary. - For decimal128_t, give the same functions a first try in two words. - Next to a boundary for a small x, compute the terms after the exact 1 in their own precision, also for decimal64_t. - For decimal64_t, use a longer series of exp(-z) in two words, as exp(-5.000000000000001E-17) gave 1. - Give the quiet NaN of a signaling NaN x. - Add test_first_try_bounds, which compares the first tries with three words. Fixes boostorg#1561
ibmibmibm
force-pushed
the
fixed-point-hyperbolic
branch
from
November 24, 2026 08:30
225c71b to
e7a1a79
Compare
mborland
approved these changes
Nov 25, 2026
mborland
left a comment
Member
There was a problem hiding this comment.
Looks good. Thanks as always!
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
exp(|x|)in the fixed point of exp, andexp(-|x|)from the reciprocal of its mantissa. The two parts arescaled to the same decimal exponent, then added or subtracted. For
|x| < 1/16, they use the Taylor series inx^2. The result roundsonce in the current mode.
(1 - t) / (1 + t)witht = exp(-2|x|). Wheretisbelow the last bit, the result is a value just below 1. For
|x| < 1/16, it divides the two Taylor series. The result roundsonce in the current mode.
exp(x) - 1forx >= 1/16, with the 1 scaled to thedecimal exponent of
exp(x), and1 - exp(x)forx <= -1/16.Where
exp(x)is below the last bit, the result is a value justabove -1. For
|x| < 1/16, it uses the Taylor series ofsinh(x) + cosh(x) - 1. The result rounds once in the current mode.log10(2)in place oflog10(e).2^nfor an integernis exact. A result in thesubnormal range is no longer 0, for example
exp2(-1300_DD).exp(|x|)overflows but its halfdoes not, for example
sinh(887_DD).signaling NaN gives the quiet NaN with the sign and payload of x.
x^2is below the last digit of the result, sinh, cosh andtanh need no series: the result rounds as a value next to
|x|, ornext to 1 for cosh. expm1 does the same for
|x| < 10^-p, withpthe precision of the type.
decimal64_tand two words fordecimal128_t. If the result cannotbe next to a rounding boundary, it rounds once. If not, the full
computation runs. For
decimal128_t, tanh and expm1 have this firsttry for
|x| < 1/16and for|x| >= 1/2.decimal32_tanddecimal64_t, the first try of exp, exp2,sinh, cosh and tanh takes the argument reduction from one product of
the significand with
log10(e) * 10^-korlog10(2) * 10^-kfrom atable. The product gives the decimal exponent and 128 bits of the
fraction, with no division. sinh (for
|x| >= 1) and cosh (for|x| >= 1/16) getexp(-|x|)from the same tables. tanh (for|x| >= 0.11) getst = exp(-2|x|)this way and computes1 - 2t / (1 + t)with one division. The error bound of the 19digits is 2 units for exp and exp2, and 3 units for sinh, cosh and
tanh, so few results need the full computation. This also makes exp
faster for these two types.
decimal32_t, this first try keeps 64 bits of the fraction andstops the series at
z^3. The error bound is then 2^25 units of the19 digits, of which
decimal32_tdrops 12. sinh takes this try from|x| >= 0.1.decimal64_texp and exp2 takethe two-word series with more terms. Next to a tie,
1 + x + x^2 / 2for a smallxneeds 32 digits, and the shorterseries gave 30:
exp(-5.000000000000001E-17_DD)gave1, and nowgives
0.9999999999999999. develop has the same defect.test_cmath_bound_cases.cppgets these inputs.decimal128_tcomputes the terms afterthe exact first term (
x, or 1 for cosh, exp and exp2) in their ownprecision. This also corrects
expfordecimal128_tnext to arounding tie, for example
exp(-1e-17_DL)in round to nearest.github_issue_1557.cppgets this input.fx_recipandfx_divdivide in fixed point.fx_shr,fx_horner,fx_int,fx_mul_shrandfx_extendare new helpers. For asignificand of two words,
fx_rounddivides one time and gets therounding digit from the rest.
test_fixed_point.cppcomparesfx_recipwith schoolbook arithmetic for one to five words.impl/tan_impl.hppgets the namerecip_below_one, so that its name is different fromfx_recip.impl/sinh_impl.hpp,impl/cosh_impl.hpp,impl/tanh_impl.hppandimpl/expm1_impl.hpphave no more users andare removed.
github_issue_1561_sinh.cpp,github_issue_1561_tanh.cpp,github_issue_1561_expm1.cppandgithub_issue_1561_exp2.cppcheck hard casesin all five modes for each type.
test_cmath_bound_cases.cppholds inputs that give a wrong result ifan error bound of a first try gets smaller, in all rounding modes. A
program checked the expected values against MPFR. The test needs no
MPFR.
test_first_try_bounds.cppcompares the first tries of exp, exp2,sinh, cosh and tanh with three words, against the error bounds in
the library.
Fixes #1561
Accuracy
The test takes 20000 random
xper range, with a full significand and a random sign, for each function, each type and each of the five rounding modes. The ranges of|x|are those of the speed tables, and the subnormal range. MPFR gives the exact value.developgives up to 100% of incorrect results in a range. The branch gives 0 incorrect results in all ranges, types and modes.Speed
ns per call, best of 3 runs, GCC 16.2.1
-O2, AMD Ryzen 9 3900X. The inputs have a full significand and a random sign.sinh:cosh:tanh:expm1:exp2:exp: