Add `asinpi`, `acospi` and `atanpi` #63031
All Vouch review confidence checks passed
Residual risk 0.32 = Change Risk (0.57) × (1 − Review Confidence (0.45)).
Review depth and reviewer attention state are adequate.
Model 1 · Change Risk (XGBoost)
Score: 0.57 · 631 lines diff across 7 files
Model 2 · Review Depth (DistilBERT)
Depth Score: 0.02 · 1 substantive comment(s)
Model 3 · Review Timing & Attention
Session adequacy: 100% · Reviewer familiarity: 35%
Bedrock Claude 3 · Risk Narration
(disconnected)
Groq Risk Narration
AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
Model 1 · Change Risk Attribution
SHAP Weights
Diff Size
diff_size
0.386
Shallow Review Depth
shallow_review_depth
0.344
Real PR Conversation Timeline
1 comments
"Review active on #63031"
general
weight: 0.35
PatrickHaecker
This PR changed had an interesting course. It started from
asinpi,acospi,atanpishould be more efficient than their non-pi counterparts. It is now at: No, they are slower, but more accurate. And it turned out that the tested approaches to get the improved accuracy with at least similar performance are actually useful for the non-pi functions, too. So there are some interesting changes forasin,acosandatanincluded, too.This PR is therefore probably more a collection of some changes with interesting properties all related to inverse-trig where it makes sense to decide on a case-by-case basis whether they should be included.
:robot: with :person_bald: :
What
asinpi(x),acospi(x),atanpi(y)andatanpi(y, x)compute the inverse trigonometric functions in half-turns. They complete thesinpi/cospi/tanpifamily and correspond to the operations of the same names in C23 and IEEE 754-2019.Without that, code working in half-turns (τ/2 = π) has to write
asin(x)/pi. That leads to a larger error:Why it is worth having
Writing
asin(x)/pirounds twice, once in the function and once in the division, and reaches 1.7 ulp. Scaling by a double-double 1/π inside the kernel rounds once.The measurements use
Float64, 200 000 random arguments and a reference computed at 320-bit precision. The baseline isf(x)/pibuilt on the improved kernels in this PR, i.e. what is available without the pi-variants. The measurements use unsorted and sorted data, as the branch predictor has a heavy influence and there are probably use cases where angles are very similar from call to call and other uses cases where they change randomly.| function | max ulp | misrounded / 200 000 | ns unsorted | ns sorted |
|---|---|---|---|---|
|
asinpi| 1.520 → 0.695 | 65 971 → 2 762 (24x fewer) | 7.839 → 8.345 | 4.166 → 4.729 ||
acospi| 1.653 → 0.568 | 68 038 → 1 183 (58x fewer) | 9.411 → 10.025 | 4.279 → 4.302 ||
atanpi| 1.722 → 0.865 | 72 211 → 6 242 (12x fewer) | 8.131 → 8.307 | 5.335 → 5.371 |The two-argument
atanpi(y, x)is exact across the whole quadrant table, because the π/2, π and atan(1) that the radian version adds become 1/2, 1 and 1/4, all exactly representable.What it costs
0 to 14% more time. And the three new exported names in
Base, which IEEE recommends.Also included
BigFloatdispatches to MPFR, which has provided these since 4.2.two_sqrt(a double-double square root) and its use inasinandacos, which the new functions build on, plus a branchless argument reduction foratan.Same method and sample as above;
asinandacosare measured on [-1, 1] andatanon |x| ∈ [1/64, 64] on a log scale, which spreads points over the small-|x|branch and all four reduction centres. Noise floor 0.1% unsorted, 2.5% sorted.| function | max ulp | misrounded / 200 000 | ns unsorted | ns sorted |
|---|---|---|---|---|
|
asin| 0.813 → 0.623 | 11 669 → 2 771 | 6.560 → 6.144 | 4.493 → 4.271 ||
acos| 0.853 → 0.769 | 15 439 → 15 420 | 7.599 → 7.419 | 3.106 → 3.156 ||
atan| 0.814 → 0.814 | 7 246 → 7 180 | 8.995 → 7.222 | 4.349 → 4.834 |asinimproves on every axis.acostightens only its worst case: the misrounding count is hardly changed and its timings sit within the noise floor, so it is probably no speedup.atanis accuracy-neutral, and that commit is purely a throughput trade. The reduction itself is bit-identical, verified on all 4 278 190 082 non-NaN Float32 arguments. The commit also fuses two multiply-adds, which changes the result on roughly 0.2% of arguments without moving the maximum error. The two-argumentatan(y, x)is not modified, but it evaluatesatan(abs(y/x))and so inherits both effects, diluted by the division and quadrant handling around the call:| function | differing results | ns unsorted | ns sorted |
|---|---|---|---|
|
atan| ~0.2% | 8.995 → 7.222 (−20%) | 4.349 → 4.834 (+11%) ||
atan(y, x)| 500 / 400 000 | 20.468 → 18.265 (−10.8%) | 12.627 → 13.081 (+3.6%) |Everything layered on the one-argument form, such as
atandandacot, inherits the same trade. That commit can be dropped. However,atanpiuses its helper, so dropping needs a small follow-up.On the measurements
Timings are the median of 41 rounds; each round uses a freshly generated input array and is preceded by a pass of unpredictable branches that scrambles the branch history, with the data cache re-warmed before timing. Measuring the same function under two names puts the noise floor below 3% throughout. This matters here: taking the minimum over repeated passes of one fixed array lets the predictor memorise the sequence, which reverses the sign of the
atanresult.Claude 5 supported in creating this PR.