Skip to content

Commit 2be7225

Browse files
authored
[libc][math] Implement atan2f correctly rounded to all rounding modes. (#86716)
We compute atan2f(y, x) in 2 stages: - Fast step: perform computations in double precision , with relative errors < 2^-50 - Accurate step: if the result from the Fast step fails Ziv's rounding test, then we perform computations in double-double precision, with relative errors < 2^-100. On Ryzen 5900X, worst-case latency is ~ 200 clocks, compared to average latency ~ 60 clocks, and average reciprocal throughput ~ 20 clocks.
1 parent a54930e commit 2be7225

File tree

22 files changed

+634
-73
lines changed

22 files changed

+634
-73
lines changed

libc/config/baremetal/arm/entrypoints.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,7 @@ set(TARGET_LIBM_ENTRYPOINTS
207207
libc.src.math.acoshf
208208
libc.src.math.asinf
209209
libc.src.math.asinhf
210+
libc.src.math.atan2f
210211
libc.src.math.atanf
211212
libc.src.math.atanhf
212213
libc.src.math.ceil

libc/config/baremetal/riscv/entrypoints.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,7 @@ set(TARGET_LIBM_ENTRYPOINTS
207207
libc.src.math.acoshf
208208
libc.src.math.asinf
209209
libc.src.math.asinhf
210+
libc.src.math.atan2f
210211
libc.src.math.atanf
211212
libc.src.math.atanhf
212213
libc.src.math.ceil

libc/config/darwin/arm/entrypoints.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@ set(TARGET_LIBM_ENTRYPOINTS
118118
libc.src.math.acoshf
119119
libc.src.math.asinf
120120
libc.src.math.asinhf
121+
libc.src.math.atan2f
121122
libc.src.math.atanf
122123
libc.src.math.atanhf
123124
libc.src.math.copysign

libc/config/linux/aarch64/entrypoints.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -330,6 +330,7 @@ set(TARGET_LIBM_ENTRYPOINTS
330330
libc.src.math.acoshf
331331
libc.src.math.asinf
332332
libc.src.math.asinhf
333+
libc.src.math.atan2f
333334
libc.src.math.atanf
334335
libc.src.math.atanhf
335336
libc.src.math.copysign

libc/config/linux/arm/entrypoints.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,7 @@ set(TARGET_LIBM_ENTRYPOINTS
198198
libc.src.math.acoshf
199199
libc.src.math.asinf
200200
libc.src.math.asinhf
201+
libc.src.math.atan2f
201202
libc.src.math.atanf
202203
libc.src.math.atanhf
203204
libc.src.math.ceil

libc/config/linux/riscv/entrypoints.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -338,6 +338,7 @@ set(TARGET_LIBM_ENTRYPOINTS
338338
libc.src.math.acoshf
339339
libc.src.math.asinf
340340
libc.src.math.asinhf
341+
libc.src.math.atan2f
341342
libc.src.math.atanf
342343
libc.src.math.atanhf
343344
libc.src.math.copysign

libc/config/linux/x86_64/entrypoints.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -348,6 +348,7 @@ set(TARGET_LIBM_ENTRYPOINTS
348348
libc.src.math.acoshf
349349
libc.src.math.asinf
350350
libc.src.math.asinhf
351+
libc.src.math.atan2f
351352
libc.src.math.atanf
352353
libc.src.math.atanhf
353354
libc.src.math.canonicalize

libc/config/windows/entrypoints.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,7 @@ set(TARGET_LIBM_ENTRYPOINTS
116116
libc.src.math.acoshf
117117
libc.src.math.asinf
118118
libc.src.math.asinhf
119+
libc.src.math.atan2f
119120
libc.src.math.atanf
120121
libc.src.math.atanhf
121122
libc.src.math.copysign

libc/docs/math/index.rst

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -422,7 +422,7 @@ Higher Math Functions
422422
+------------+---------+---------+---------+---------+---------+---------+---------+---------+---------+---------+---------+---------+
423423
| atan2 | | | | | | | | | | | | |
424424
+------------+---------+---------+---------+---------+---------+---------+---------+---------+---------+---------+---------+---------+
425-
| atan2f | | | | | | | | | | | | |
425+
| atan2f | |check| | |check| | |check| | |check| | |check| | | | |check| | |check| | |check| | | |
426426
+------------+---------+---------+---------+---------+---------+---------+---------+---------+---------+---------+---------+---------+
427427
| atan2l | | | | | | | | | | | | |
428428
+------------+---------+---------+---------+---------+---------+---------+---------+---------+---------+---------+---------+---------+
@@ -591,6 +591,7 @@ acosh |check|
591591
asin |check|
592592
asinh |check|
593593
atan |check|
594+
atan2 |check|
594595
atanh |check|
595596
cos |check| large
596597
cosh |check|

libc/spec/stdc.td

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -620,10 +620,14 @@ def StdC : StandardSpec<"stdc"> {
620620
FunctionSpec<"tanhf", RetValSpec<FloatType>, [ArgSpec<FloatType>]>,
621621

622622
FunctionSpec<"acosf", RetValSpec<FloatType>, [ArgSpec<FloatType>]>,
623+
623624
FunctionSpec<"asinf", RetValSpec<FloatType>, [ArgSpec<FloatType>]>,
624625
FunctionSpec<"asin", RetValSpec<DoubleType>, [ArgSpec<DoubleType>]>,
626+
625627
FunctionSpec<"atanf", RetValSpec<FloatType>, [ArgSpec<FloatType>]>,
626628

629+
FunctionSpec<"atan2f", RetValSpec<FloatType>, [ArgSpec<FloatType>, ArgSpec<FloatType>]>,
630+
627631
FunctionSpec<"acoshf", RetValSpec<FloatType>, [ArgSpec<FloatType>]>,
628632
FunctionSpec<"asinhf", RetValSpec<FloatType>, [ArgSpec<FloatType>]>,
629633
FunctionSpec<"atanhf", RetValSpec<FloatType>, [ArgSpec<FloatType>]>,

libc/src/math/generic/CMakeLists.txt

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2854,6 +2854,24 @@ add_entrypoint_object(
28542854
-O3
28552855
)
28562856

2857+
add_entrypoint_object(
2858+
atan2f
2859+
SRCS
2860+
atan2f.cpp
2861+
HDRS
2862+
../atan2f.h
2863+
COMPILE_OPTIONS
2864+
-O3
2865+
DEPENDS
2866+
.inv_trigf_utils
2867+
libc.src.__support.FPUtil.fp_bits
2868+
libc.src.__support.FPUtil.multiply_add
2869+
libc.src.__support.FPUtil.nearest_integer
2870+
libc.src.__support.FPUtil.polyeval
2871+
libc.src.__support.FPUtil.rounding_mode
2872+
libc.src.__support.macros.optimization
2873+
)
2874+
28572875
add_entrypoint_object(
28582876
scalbn
28592877
SRCS

0 commit comments

Comments
 (0)