llvm-c166/libc/shared/math
Hoàng Minh Thiên a454a3cdaf
[libc][math] Integer-only, statically rounded implementation of expf (#209406)
Integer-only, statically rounded implementation of `expf`, using 1
single `Frac64`-based pipeline

# Accuracy

All unit, smoke, and exhaustive tests against
`LIBC_NAMESPACE::shared::expf` pass for all rounding modes.

# Code size:

> [!NOTE]
> The report below are from the`armv8m.main-none-eabi` triple.

## Before the patch

```sh
$ ls -lB libc/src/math/generic/CMakeFiles/libc.src.math.generic.expf.dir/
total 16
-rw-r-----@ 1 davidhoang  staff  5520 Jul 24 15:59 expf.cpp.obj

$ llvm-nm --radix=d --print-size --size-sort --reverse-sort libc/src/math/generic/CMakeFiles/libc.src.math.generic.expf.dir/expf.cpp.obj
00000000 00001560 V _ZN22__llvm_libc_24_0_0_git4math6EXP_M1E
00000000 00001024 V _ZN22__llvm_libc_24_0_0_git4math6EXP_M2E
00000000 00000628 W _ZN22__llvm_libc_24_0_0_git4math4expfEf
00000000 00000012 T expf
00000000 00000012 T _ZN22__llvm_libc_24_0_0_git4expfEf
00000000 00000004 V _ZZN22__llvm_libc_24_0_0_git6fputil7generic16fenv_is_round_upEvE1x
00000000 00000004 V _ZZN22__llvm_libc_24_0_0_git6fputil7generic15quick_get_roundEvE1x
                  U __aeabi_f2d
                  U __aeabi_dmul
                  U __aeabi_dadd
                  U __aeabi_d2f
```

## After the patch and with `LIBC_MATH_FAST`

```sh
$ ls -lB libc/src/math/generic/CMakeFiles/libc.src.math.generic.expf.dir/
total 8
-rw-r-----@ 1 davidhoang  staff  3244 Aug 18 14:29 expf.cpp.obj

$ llvm-nm --radix=d --print-size --size-sort --reverse-sort libc/src/math/generic/CMakeFiles/libc.src.math.generic.expf.dir/expf.cpp.obj
00000000 00001916 W _ZN22__llvm_libc_24_0_0_git6shared4math15static_rounding4expfEfi
00000000 00000014 T expf
00000000 00000014 T _ZN22__llvm_libc_24_0_0_git4expfEf
```

# Latency


[bench.zip](https://github.com/user-attachments/files/31171370/bench.zip),
containing `bench.cc` with compilation instruction.

```
====================LIBC_MATH_FAST is ON====================
==========================baseline==========================
overflow [88,100]              51.67 ns/call  (19M ops/sec)
underflow [-110,-100]          55.50 ns/call  (18M ops/sec)
normal [-10,10]                32.09 ns/call  (31M ops/sec)
subnormal-result [-103,-87]    40.14 ns/call  (25M ops/sec)
near-underflow [-100,-80]      40.84 ns/call  (24M ops/sec)
======================static_rounding=======================
overflow [88,100]              142.13 ns/call  (7M ops/sec)
underflow [-110,-100]          431.57 ns/call  (2M ops/sec)
normal [-10,10]                925.12 ns/call  (1M ops/sec)
subnormal-result [-103,-87]    965.58 ns/call  (1M ops/sec)
near-underflow [-100,-80]      955.55 ns/call  (1M ops/sec)
```

```
===================LIBC_MATH_FAST is OFF====================
==========================baseline==========================
overflow [88,100]              55.99 ns/call  (18M ops/sec)
underflow [-110,-100]          58.80 ns/call  (17M ops/sec)
normal [-10,10]                32.70 ns/call  (31M ops/sec)
subnormal-result [-103,-87]    40.90 ns/call  (24M ops/sec)
near-underflow [-100,-80]      40.46 ns/call  (25M ops/sec)
======================static_rounding=======================
overflow [88,100]              141.72 ns/call  (7M ops/sec)
underflow [-110,-100]          432.78 ns/call  (2M ops/sec)
normal [-10,10]                935.92 ns/call  (1M ops/sec)
subnormal-result [-103,-87]    971.07 ns/call  (1M ops/sec)
near-underflow [-100,-80]      966.82 ns/call  (1M ops/sec)
```

Benchmarking system: MacBook Air M3, macOS 26.5.1
2026-08-23 11:34:41 -04:00
..
check
static_rounding [libc][math] Integer-only, statically rounded implementation of expf (#209406) 2026-08-23 11:34:41 -04:00
acos.h
acosbf16.h
acosf.h
acosf16.h
acoshf.h
acoshf16.h
acospif.h
acospif16.h
asin.h
asinbf16.h
asinf.h
asinf16.h
asinhf.h
asinhf16.h
asinpi.h
asinpif.h
asinpif16.h
atan.h
atan2.h
atan2f.h
atan2f16.h
atan2f128.h [libc] Modify atan2f128 to use emulated Float128 type (#216508) 2026-08-19 09:44:21 +00:00
atanbf16.h
atanf.h
atanf16.h
atanhf.h
atanhf16.h
atanpif16.h
bf16add.h
bf16addf.h
bf16addf128.h
bf16addl.h
bf16div.h
bf16divf.h
bf16divf128.h
bf16divl.h
bf16fma.h
bf16fmaf.h
bf16fmaf128.h
bf16fmal.h
bf16mul.h
bf16mulf.h
bf16mulf128.h
bf16mull.h
bf16sub.h
bf16subf.h
bf16subf128.h
bf16subl.h
canonicalize.h
canonicalizebf16.h
canonicalizef.h
canonicalizef16.h
canonicalizef128.h
canonicalizel.h
cbrt.h
cbrtbf16.h
cbrtf.h
cbrtf16.h
ceil.h
ceilbf16.h
ceilf.h
ceilf16.h
ceilf128.h
ceill.h
copysign.h
copysignbf16.h
copysignf.h
copysignf16.h
copysignf128.h
copysignl.h
cos.h
cosf.h
cosf16.h
coshf.h
coshf16.h
cospif.h
cospif16.h
daddf128.h
daddl.h
ddivf128.h
ddivl.h
dfmaf128.h
dfmal.h
dmulf128.h
dmull.h
dsqrtf128.h
dsqrtl.h
dsubf128.h
dsubl.h
erfcf16.h
erff.h
erff16.h
exp.h
exp2.h
exp2f.h
exp2f16.h
exp2m1f.h
exp2m1f16.h
exp10.h
exp10f.h
exp10f16.h
exp10m1f.h
exp10m1f16.h
expbf16.h
expf.h
expf16.h
expm1.h
expm1f.h
expm1f16.h
f16add.h
f16addf.h
f16addf128.h
f16addl.h
f16div.h
f16divf.h
f16divf128.h
f16divl.h
f16fma.h
f16fmaf.h
f16fmaf128.h
f16fmal.h
f16mul.h
f16mulf.h
f16mulf128.h
f16mull.h
f16sqrt.h
f16sqrtf.h
f16sqrtf128.h
f16sqrtl.h
f16sub.h
f16subf.h
f16subf128.h
f16subl.h
fabs.h
fabsbf16.h
fabsf.h
fabsf16.h
fabsf128.h
fabsl.h
fadd.h [libc][math] Cleanup shared/math (#183971) 2026-03-01 04:08:52 +02:00
faddf128.h
faddl.h
fdim.h
fdimbf16.h
fdimf.h
fdimf16.h
fdimf128.h
fdiml.h
fdiv.h
fdivf128.h
fdivl.h
ffma.h
ffmaf128.h
ffmal.h
floor.h
floorbf16.h
floorf.h
floorf16.h
floorf128.h
floorl.h
fma.h
fmabf16.h
fmaf.h
fmaf16.h
fmax.h
fmaxbf16.h
fmaxf.h
fmaxf16.h
fmaxf128.h [libc] Modify fmaxf128 and fminf128 to use emulated Float128 type (#216575) 2026-08-19 18:45:40 +05:30
fmaximum.h
fmaximum_mag.h
fmaximum_mag_num.h
fmaximum_mag_numbf16.h
fmaximum_mag_numf.h
fmaximum_mag_numf16.h
fmaximum_mag_numf128.h
fmaximum_mag_numl.h
fmaximum_magbf16.h
fmaximum_magf.h
fmaximum_magf16.h
fmaximum_magf128.h
fmaximum_magl.h
fmaximum_num.h
fmaximum_numbf16.h
fmaximum_numf.h
fmaximum_numf16.h
fmaximum_numf128.h
fmaximum_numl.h
fmaximumbf16.h
fmaximumf.h
fmaximumf16.h
fmaximumf128.h
fmaximuml.h
fmaxl.h
fmin.h
fminbf16.h
fminf.h
fminf16.h
fminf128.h [libc] Modify fmaxf128 and fminf128 to use emulated Float128 type (#216575) 2026-08-19 18:45:40 +05:30
fminimum.h
fminimum_mag.h
fminimum_mag_num.h
fminimum_mag_numbf16.h
fminimum_mag_numf.h
fminimum_mag_numf16.h
fminimum_mag_numf128.h
fminimum_mag_numl.h
fminimum_magbf16.h
fminimum_magf.h
fminimum_magf16.h
fminimum_magf128.h
fminimum_magl.h
fminimum_num.h
fminimum_numbf16.h
fminimum_numf.h
fminimum_numf16.h
fminimum_numf128.h
fminimum_numl.h
fminimumbf16.h
fminimumf.h
fminimumf16.h
fminimumf128.h
fminimuml.h
fminl.h
fmod.h
fmodbf16.h
fmodf.h
fmodf16.h
fmodf128.h
fmodl.h
fmul.h
fmulf128.h
fmull.h
frexp.h
frexpbf16.h
frexpf.h
frexpf16.h
frexpf128.h
frexpl.h
fromfp.h
fromfpbf16.h
fromfpf.h
fromfpf16.h
fromfpf128.h
fromfpl.h
fromfpx.h
fromfpxbf16.h
fromfpxf.h
fromfpxf16.h
fromfpxf128.h
fromfpxl.h
fsqrt.h
fsqrtf128.h
fsqrtl.h
fsub.h
fsubf128.h
fsubl.h
getpayload.h
getpayloadbf16.h
getpayloadf.h
getpayloadf16.h
getpayloadf128.h
getpayloadl.h
hypot.h
hypotbf16.h
hypotf.h
hypotf16.h
ilogb.h
ilogbbf16.h
ilogbf.h
ilogbf16.h
ilogbf128.h
ilogbl.h
iscanonical.h
iscanonicalbf16.h
iscanonicalf.h
iscanonicalf16.h
iscanonicalf128.h [libc] Make classification functions use the emulated float128 type (#217134) 2026-08-21 10:55:47 +00:00
iscanonicall.h
isnan.h
isnanf.h
isnanf16.h
isnanf128.h [libc] Make classification functions use the emulated float128 type (#217134) 2026-08-21 10:55:47 +00:00
isnanl.h
issignaling.h
issignalingbf16.h
issignalingf.h
issignalingf16.h
issignalingf128.h [libc] Make classification functions use the emulated float128 type (#217134) 2026-08-21 10:55:47 +00:00
issignalingl.h
ldexp.h
ldexpbf16.h
ldexpf.h
ldexpf16.h
ldexpf128.h
ldexpl.h
lgammaf16.h
llogb.h
llogbbf16.h
llogbf.h
llogbf16.h
llogbf128.h
llogbl.h
llrint.h
llrintbf16.h
llrintf.h
llrintf16.h
llrintf128.h [libc] Modify rint functions to use emulated Float128 type (#217070) 2026-08-20 22:29:35 +00:00
llrintl.h
llround.h
llroundbf16.h
llroundf.h
llroundf16.h
llroundf128.h [libc] Make lroundf128 and llroundf128 use the emulated float128 type (#216829) 2026-08-19 12:29:43 +00:00
llroundl.h
log.h
log1p.h
log1pf.h
log2.h
log2f.h
log2f16.h
log2p1f16.h
log10.h
log10f.h
log10f16.h
log10p1f16.h
log_bf16.h
logb.h
logbbf16.h
logbf.h
logbf16.h
logbf128.h
logbl.h
logf.h
logf16.h
lrint.h
lrintbf16.h
lrintf.h
lrintf16.h
lrintf128.h [libc] Modify rint functions to use emulated Float128 type (#217070) 2026-08-20 22:29:35 +00:00
lrintl.h
lround.h
lroundbf16.h
lroundf.h
lroundf16.h
lroundf128.h [libc] Make lroundf128 and llroundf128 use the emulated float128 type (#216829) 2026-08-19 12:29:43 +00:00
lroundl.h
modf.h
modfbf16.h
modff.h
modff16.h
modff128.h
modfl.h
nan.h
nanbf16.h
nanf.h
nanf16.h
nanf128.h
nanl.h
nearbyint.h
nearbyintbf16.h
nearbyintf.h
nearbyintf16.h
nearbyintf128.h [libc] Modify nearbyintf128 to use emulated Float128 type (#217025) 2026-08-19 11:47:52 +00:00
nearbyintl.h
nextafter.h
nextafterbf16.h
nextafterf.h
nextafterf16.h
nextafterf128.h
nextafterl.h
nextdown.h
nextdownbf16.h
nextdownf.h
nextdownf16.h
nextdownf128.h
nextdownl.h
nexttoward.h
nexttowardbf16.h
nexttowardf.h
nexttowardf16.h
nexttowardl.h
nextup.h
nextupbf16.h
nextupf.h
nextupf16.h
nextupf128.h
nextupl.h
pow.h
powf.h
remainder.h
remainderbf16.h
remainderf.h
remainderf16.h
remainderf128.h
remainderl.h
remquo.h
remquobf16.h
remquof.h
remquof16.h
remquof128.h
remquol.h
rint.h
rintbf16.h
rintf.h
rintf16.h
rintf128.h [libc] Modify rint functions to use emulated Float128 type (#217070) 2026-08-20 22:29:35 +00:00
rintl.h
round.h
roundbf16.h
roundeven.h
roundevenbf16.h
roundevenf.h
roundevenf16.h
roundevenf128.h [libc] Modify roundevenf128 to use emulated Float128 type (#216568) 2026-08-19 21:32:34 +05:30
roundevenl.h
roundf.h
roundf16.h
roundf128.h [libc] Modify roundf128 to use emulated Float128 type (#216563) 2026-08-19 10:20:24 +00:00
roundl.h
rsqrtf.h
rsqrtf16.h
scalbln.h
scalblnbf16.h
scalblnf.h
scalblnf16.h
scalblnf128.h
scalblnl.h
scalbn.h
scalbnbf16.h
scalbnf.h
scalbnf16.h
scalbnf128.h
scalbnl.h
setpayload.h
setpayloadbf16.h
setpayloadf.h
setpayloadf16.h
setpayloadf128.h
setpayloadl.h
setpayloadsig.h
setpayloadsigbf16.h
setpayloadsigf.h
setpayloadsigf16.h
setpayloadsigf128.h
setpayloadsigl.h
sin.h
sincos.h
sincosf.h
sinf.h
sinf16.h
sinhf.h
sinhf16.h
sinpif.h
sinpif16.h
sqrt.h
sqrtbf16.h
sqrtf.h
sqrtf16.h
sqrtf128.h [libc] Make sqrtf128 use emulated float128 (#216500) 2026-08-19 06:14:20 +00:00
sqrtl.h
tan.h
tanbf16.h
tanf.h
tanf16.h
tanhf.h
tanhf16.h
tanpif.h
tanpif16.h
totalorder.h
totalorderbf16.h
totalorderf.h
totalorderf16.h
totalorderf128.h
totalorderl.h
totalordermag.h
totalordermagbf16.h
totalordermagf.h
totalordermagf16.h
totalordermagf128.h
totalordermagl.h
trunc.h
truncbf16.h
truncf.h
truncf16.h
truncf128.h [libc] Modify truncf128 to use emulated Float128 type (#216560) 2026-08-21 10:10:01 +00:00
truncl.h
ufromfp.h
ufromfpbf16.h
ufromfpf.h
ufromfpf16.h
ufromfpf128.h
ufromfpl.h
ufromfpx.h
ufromfpxbf16.h
ufromfpxf.h
ufromfpxf16.h
ufromfpxf128.h
ufromfpxl.h