# [libc][math] Implement double precision exp function correctly rounded for all rounding modes.ClosedPublic

Authored by lntue on Aug 22 2023, 2:07 PM.

# Details

Reviewers
 michaelrj sivachandra cqlauter zimmermann6
Commits
rG434bf1608445: [libc][math] Implement double precision exp function correctly rounded for all…
Summary

Implement double precision exp function correctly rounded for all
rounding modes. Using 4 stages:

• Range reduction: reduce to exp(x) = 2^hi * 2^mid1 * 2^mid2 * exp(lo).
• Use 64 + 64 LUT for 2^mid1 and 2^mid2, and use cubic Taylor polynomial to

approximate (exp(lo) - 1) / lo in double precision. Relative error in this
step is bounded by 1.5 * 2^-63.

• If the rounding test fails, use degree-6 Taylor polynomial to approximate

exp(lo) in double-double precision. Relative error in this step is bounded by
2^-99.

• If the rounding test still fails, use degree-7 Taylor polynomial to compute

exp(lo) in ~128-bit precision.

# Diff Detail

### Event Timeline

lntue created this revision.Aug 22 2023, 2:07 PM
Herald added projects: Restricted Project, Restricted Project. Aug 22 2023, 2:07 PM
Herald added subscribers: luke, sunshaoce, frasercrmck and 21 others.
lntue requested review of this revision.Aug 22 2023, 2:07 PM
Herald added a subscriber: wangpc. Aug 22 2023, 2:07 PM
lntue updated this revision to Diff 552512.Aug 22 2023, 2:38 PM

Remove unused template.

zimmermann6 requested changes to this revision.Aug 23 2023, 1:21 AM

when I apply this patch on main (commit 96b5ea6), and I build libc.a, I don't find the symbol exp into libc.a:

```zimmerma@biscotte:/localdisk/zimmerma/llvm-project\$ nm /localdisk/zimmerma/llvm-project/build/projects/libc/lib/libc.a | grep -w exp
zimmerma@biscotte:/localdisk/zimmerma/llvm-project\$```

Maybe I did something wrong?

This revision now requires changes to proceed.Aug 23 2023, 1:21 AM
lntue added a comment.Aug 23 2023, 6:39 AM

Hi Paul, do you mind sending me the build commands that you used and their
logs, and/or attaching the generated libc.a.

Thanks,

`Tue`

Hi Paul, do you mind sending me the build commands that you used and their
logs, and/or attaching the generated libc.a.

Thanks,

`Tue`

sure, I'll do that in a private mail.

zimmermann6 accepted this revision.Aug 24 2023, 1:07 AM

after help from Tue I was able to use this patch with the core-math tools. All tests pass, and on my machine (AMD EPYC 7282) I get the following timings:

```zimmerma@biscotte:/tmp/core-math\$ LIBM=/localdisk/zimmerma/llvm-project/build/projects/libc/lib/libm.a CORE_MATH_PERF_MODE=rdtsc ./perf.sh exp
GNU libc version: 2.37
GNU libc release: stable
[####################] 100 %
Ntrial = 20 ; Min = 18.639 + 0.385 clc/call; Median-Min = 0.327 clc/call; Max = 21.116 clc/call;
[####################] 100 %
Ntrial = 20 ; Min = 13.164 + 0.253 clc/call; Median-Min = 0.280 clc/call; Max = 13.590 clc/call;
[####################] 100 %
Ntrial = 20 ; Min = 19.464 + 0.307 clc/call; Median-Min = 0.308 clc/call; Max = 19.828 clc/call;```

and for latency:

```zimmerma@biscotte:/tmp/core-math\$ LIBM=/localdisk/zimmerma/llvm-project/build/projects/libc/lib/libm.a CORE_MATH_PERF_MODE=rdtsc PERF_ARGS=--latency ./perf.sh exp
GNU libc version: 2.37
GNU libc release: stable
[####################] 100 %
Ntrial = 20 ; Min = 49.458 + 0.339 clc/call; Median-Min = 0.269 clc/call; Max = 50.109 clc/call;
[####################] 100 %
Ntrial = 20 ; Min = 39.380 + 0.326 clc/call; Median-Min = 0.299 clc/call; Max = 39.948 clc/call;
[####################] 100 %
Ntrial = 20 ; Min = 54.919 + 0.347 clc/call; Median-Min = 0.310 clc/call; Max = 55.561 clc/call;```
This revision is now accepted and ready to land.Aug 24 2023, 1:07 AM
This revision was automatically updated to reflect the committed changes.