Skip to content

Commit c9ee6b1

Browse files
authored
[libc][math] Implement cbrtf function correctly rounded to all rounding modes. (#97936)
Fixes #92874 Algorithm: Let `x = (-1)^s * 2^e * (1 + m)`. - Step 1: Range reduction: reduce the exponent with: ``` y = cbrt(x) = (-1)^s * 2^(floor(e/3)) * 2^((e % 3)/3) * (1 + m)^(1/3) ``` - Step 2: Use the first 4 bit fractional bits of `m` to look up for a degree-7 polynomial approximation to: ``` (1 + m)^(1/3) ~ 1 + m * P(m). ``` - Step 3: Perform the multiplication: ``` 2^((e % 3)/3) * (1 + m)^(1/3). ``` - Step 4: Check for exact cases to prevent rounding and clear `FE_INEXACT` floating point exception. - Step 5: Combine with the exponent and sign before converting down to `float` and return.
1 parent 7e054c3 commit c9ee6b1

File tree

24 files changed

+374
-8
lines changed

24 files changed

+374
-8
lines changed

libc/config/darwin/arm/entrypoints.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,7 @@ set(TARGET_LIBM_ENTRYPOINTS
123123
libc.src.math.atan2f
124124
libc.src.math.atanf
125125
libc.src.math.atanhf
126+
libc.src.math.cbrtf
126127
libc.src.math.copysign
127128
libc.src.math.copysignf
128129
libc.src.math.copysignl

libc/config/gpu/entrypoints.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,7 @@ set(TARGET_LIBM_ENTRYPOINTS
242242
libc.src.math.atanf
243243
libc.src.math.atanh
244244
libc.src.math.atanhf
245+
libc.src.math.cbrtf
245246
libc.src.math.ceil
246247
libc.src.math.ceilf
247248
libc.src.math.copysign

libc/config/linux/aarch64/entrypoints.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -345,6 +345,7 @@ set(TARGET_LIBM_ENTRYPOINTS
345345
libc.src.math.atan2f
346346
libc.src.math.atanf
347347
libc.src.math.atanhf
348+
libc.src.math.cbrtf
348349
libc.src.math.ceil
349350
libc.src.math.ceilf
350351
libc.src.math.ceill

libc/config/linux/arm/entrypoints.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,7 @@ set(TARGET_LIBM_ENTRYPOINTS
216216
libc.src.math.atan2f
217217
libc.src.math.atanf
218218
libc.src.math.atanhf
219+
libc.src.math.cbrtf
219220
libc.src.math.ceil
220221
libc.src.math.ceilf
221222
libc.src.math.ceill

libc/config/linux/riscv/entrypoints.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -347,6 +347,7 @@ set(TARGET_LIBM_ENTRYPOINTS
347347
libc.src.math.atan2f
348348
libc.src.math.atanf
349349
libc.src.math.atanhf
350+
libc.src.math.cbrtf
350351
libc.src.math.ceil
351352
libc.src.math.ceilf
352353
libc.src.math.ceill

libc/config/linux/x86_64/entrypoints.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -370,6 +370,7 @@ set(TARGET_LIBM_ENTRYPOINTS
370370
libc.src.math.canonicalize
371371
libc.src.math.canonicalizef
372372
libc.src.math.canonicalizel
373+
libc.src.math.cbrtf
373374
libc.src.math.ceil
374375
libc.src.math.ceilf
375376
libc.src.math.ceill

libc/config/windows/entrypoints.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,7 @@ set(TARGET_LIBM_ENTRYPOINTS
121121
libc.src.math.atan2f
122122
libc.src.math.atanf
123123
libc.src.math.atanhf
124+
libc.src.math.cbrtf
124125
libc.src.math.copysign
125126
libc.src.math.copysignf
126127
libc.src.math.copysignl

libc/docs/math/index.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -266,7 +266,7 @@ Higher Math Functions
266266
+-----------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
267267
| atanpi | | | | | | 7.12.4.10 | F.10.1.10 |
268268
+-----------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
269-
| cbrt | | | | | | 7.12.7.1 | F.10.4.1 |
269+
| cbrt | |check| | | | | | 7.12.7.1 | F.10.4.1 |
270270
+-----------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
271271
| compoundn | | | | | | 7.12.7.2 | F.10.4.2 |
272272
+-----------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+

libc/spec/stdc.td

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -382,6 +382,8 @@ def StdC : StandardSpec<"stdc"> {
382382
],
383383
[], // Enumerations
384384
[
385+
FunctionSpec<"cbrtf", RetValSpec<FloatType>, [ArgSpec<FloatType>]>,
386+
385387
FunctionSpec<"copysign", RetValSpec<DoubleType>, [ArgSpec<DoubleType>, ArgSpec<DoubleType>]>,
386388
FunctionSpec<"copysignf", RetValSpec<FloatType>, [ArgSpec<FloatType>, ArgSpec<FloatType>]>,
387389
FunctionSpec<"copysignl", RetValSpec<LongDoubleType>, [ArgSpec<LongDoubleType>, ArgSpec<LongDoubleType>]>,

libc/src/__support/FPUtil/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,8 @@ add_header_library(
155155
multiply_add.h
156156
DEPENDS
157157
libc.src.__support.common
158+
FLAGS
159+
FMA_OPT
158160
)
159161

160162
add_header_library(

0 commit comments

Comments
 (0)