[libc][math] Implement powf function correctly rounded to all rounding modes. (#71188)
commitbc7a3bd864be696217c4d11eddf16bed7646b60f
authorlntue <35648136+lntue@users.noreply.github.com>
Mon, 6 Nov 2023 21:54:25 +0000 (6 16:54 -0500)
committerGitHub <noreply@github.com>
Mon, 6 Nov 2023 21:54:25 +0000 (6 16:54 -0500)
tree40f29249601d28f090708ace1611811bb9534f13
parent0156b6ed6561f67f1b86ceef027908dd425e0615
[libc][math] Implement powf function correctly rounded to all rounding modes. (#71188)

We compute `pow(x, y)` using the formula
```
  pow(x, y) = x^y = 2^(y * log2(x))
```
We follow similar steps as in `log2f(x)` and `exp2f(x)`, by breaking
down into `hi + mid + lo` parts, in which `hi` parts are computed using
the exponent field directly, `mid` parts will use look-up tables, and
`lo` parts are approximated by polynomials.

We add some speedup for common use-cases:
```
  pow(2, y) = exp2(y)
  pow(10, y) = exp10(y)
  pow(x, 2) = x * x
  pow(x, 1/2) = sqrt(x)
  pow(x, -1/2) = rsqrt(x) - to be added
```
23 files changed:
libc/config/darwin/arm/entrypoints.txt
libc/config/linux/aarch64/entrypoints.txt
libc/config/linux/riscv/entrypoints.txt
libc/config/linux/x86_64/entrypoints.txt
libc/config/windows/entrypoints.txt
libc/docs/math/index.rst
libc/spec/stdc.td
libc/src/math/generic/CMakeLists.txt
libc/src/math/generic/common_constants.cpp
libc/src/math/generic/common_constants.h
libc/src/math/generic/exp10f.cpp
libc/src/math/generic/exp10f_impl.h [copied from libc/src/math/generic/exp10f.cpp with 94% similarity]
libc/src/math/generic/exp2f.cpp
libc/src/math/generic/exp2f_impl.h [copied from libc/src/math/generic/exp2f.cpp with 93% similarity]
libc/src/math/generic/log2f.cpp
libc/src/math/generic/powf.cpp [new file with mode: 0644]
libc/test/UnitTest/FPMatcher.h
libc/test/src/math/CMakeLists.txt
libc/test/src/math/powf_test.cpp [new file with mode: 0644]
libc/test/src/math/smoke/CMakeLists.txt
libc/test/src/math/smoke/powf_test.cpp [new file with mode: 0644]
libc/utils/MPFRWrapper/MPFRUtils.cpp
libc/utils/MPFRWrapper/MPFRUtils.h