128 Commits

Author SHA1 Message Date
wldfngrs
f7bb12901e
[libc][math][c23] Add tanpif16 function (#115183)
- Implementation of `tan` for 16-bit floating point inputs scaled by pi.
i.e,. `tanpif16()`
- Implementation of Tanpi in MPFRWrapper for MPFR versions < 4.2
- Exhaustive tests for `tanpif16()`
2024-11-08 09:56:31 -05:00
OverMighty
faefedf7f8
[libc][math][c23] Add exp10m1f C23 math function (#87992)
Fixes #86503.
2024-11-07 23:56:00 +01:00
wldfngrs
7395ef5419
[libc][math][c23] Add cospif16 function (#113001)
Implementation of `cos` for half precision floating point inputs scaled
by pi (i.e., `cospi`), correctly rounded for all rounding modes.

---------

Co-authored-by: OverMighty <its.overmighty@gmail.com>
2024-10-29 03:39:57 -07:00
OverMighty
d97f6d1ae9
[libc][math][c23] Add sqrtf16 C23 math function (#112406)
Part of #95250.
2024-10-19 01:41:52 +02:00
OverMighty
69d3a44ede
[libc][math][c23] Add log10f16 C23 math function (#106091)
Part of #95250.
2024-10-19 01:40:40 +02:00
OverMighty
6d347fdfbd
[libc][math][c23] Add log2f16 C23 math function (#106084)
Part of #95250.
2024-10-19 01:10:32 +02:00
OverMighty
65cf7afb6d
[libc][math][c23] Add logf16 C23 math function (#106072)
Part of #95250.
2024-10-18 22:35:12 +02:00
OverMighty
fdd7c0353f
[libc][math][c23] Add tanhf16 C23 math function (#106006)
Part of #95250.
2024-10-18 14:22:45 +02:00
OverMighty
ed3d051782
[libc][math][c23] Add sinhf16 and coshf16 C23 math functions (#105947)
Part of #95250.
2024-10-17 20:44:23 +02:00
OverMighty
95c24cb9de
[libc][math][c23] Add exp10m1f16 C23 math function (#105706)
Part of #95250.
2024-10-16 16:33:13 +02:00
wldfngrs
ddc3f2dd26
[libc] Add sinpif16 function (#110994)
Half-precision floating point (16-bit) implementation of the
trigonometric function Sin for inputs scaled by pi
2024-10-15 18:40:08 -04:00
OverMighty
ce65d4e974
[libc][math][c23] Add exp2m1f16 C23 math function (#105690)
Part of #95250.
2024-10-12 19:31:12 +02:00
Shourya Goel
2d784b1946
[libc][math] Implement iscanonical[f|l] as a libc math function (#110565)
This PR implements the iscanonical function as part of the libc math
library.

The addition of this function is crucial for completing the
implementation of remaining math macros, as referenced in #109201
2024-10-02 10:14:04 -04:00
Shourya Goel
47e3d8dc7e
[libc][math] Implement issignaling[f|l] as a libc math function (#110556)
This PR implements the issignaling function as part of the libc math
library, addressing the TODO items mentioned in #110011

The addition of this function is crucial for completing the
implementation of remaining math macros, as referenced in #109201
2024-10-01 13:49:28 -04:00
OverMighty
560ed8ce3d
[libc][math][c23] Add expm1f16 C23 math function (#102387)
Part of #95250.
2024-08-13 16:48:28 +02:00
Job Henandez Lara
ff1cc5b97c
[libc][math][c23] Add totalorderl function. (#102564) 2024-08-09 10:18:00 -04:00
aaryanshukla
d0fe470fd2
[libc][math] Add scalbln{,f,l,f128} math functions (#102219)
Co-authored-by: OverMighty <its.overmighty@gmail.com>
2024-08-08 14:33:50 -07:00
lntue
eddfd504f8
[libc][math][c23] Add ddivl C23 math function. (#102468) 2024-08-08 11:41:17 -04:00
Job Henandez Lara
aae7c38827
[libc][math][c23] Add entrypoints and tests for setpayloadsig{,f,l,f128} and setpayloadl. (#102413) 2024-08-08 00:17:20 -04:00
aaryanshukla
2c74237c0f
[libc][math][c23] Add fsub{,l,f128} and remainderf128 C23 math functions (#101576)
Co-authored-by: OverMighty <its.overmighty@gmail.com>
2024-08-07 13:03:58 -07:00
OverMighty
59338ad8c5
[libc][math][c23] Add exp10f16 C23 math function (#101588)
Part of #95250.
2024-08-07 15:54:06 +02:00
Job Henandez Lara
e3778a5d0e
[libc][math] Add getpayloadl function. (#102214) 2024-08-07 08:27:45 -04:00
aaryanshukla
0395bf7636
[libc][math][c23] Add ffma{,l,f128} and fdiv{,l,f128} C23 math functions #101089 (#101253)
- added all variations of ffma and fdiv 
- will add all new headers into yaml  for next patch 
- only fsub is left then all basic operations for float is complete

---------

Co-authored-by: OverMighty <its.overmighty@gmail.com>
2024-08-06 10:19:54 -07:00
OverMighty
936515c7a5
[libc][math][c23] Add exp2f16 C23 math function (#101217)
Part of #95250.
2024-08-06 14:44:01 +02:00
lntue
9f6b440adf
[libc][math] Implement fast pass for double precision pow function with up to 1ULP error. (#101926) 2024-08-05 13:08:59 -04:00
aaryanshukla
8f33f1d829
[libc][math][c23] Add dadd{l,f128} and ddiv{l,f128} C23 math functions (#100456)
- fadd removed because I need to add for different input types
- finishing rest of basic operations
- noticed duplicates will remove

---------

Co-authored-by: OverMighty <its.overmighty@gmail.com>
2024-08-01 13:36:50 -07:00
Job Henandez Lara
ed12f80ff0
[libc][math][c23] add entrypoints and tests for getpayload{,f,f128} (#101285) 2024-07-31 23:16:42 -04:00
aaryanshukla
30b5d4a763
[libc][math][c23] Add dfma{l,f128} and dsub{l,f128} C23 math functions (#101089)
Co-authored-by: OverMighty <its.overmighty@gmail.com>
2024-07-31 13:07:03 -07:00
Job Henandez Lara
546e4a210c
[libc][math][c23] Add entrypoints and tests for setpayload{,f,f128} (#101122) 2024-07-30 19:52:47 -04:00
Job Henandez Lara
0813260aa8
[libc][math][c23] Add entrypoints and tests for totalorder{,f,f128} (#100593) 2024-07-30 18:58:44 +02:00
OverMighty
971a1ac445
[libc][math][c23] Add expf16 C23 math function (#100632)
Part of #95250.
2024-07-30 18:38:03 +02:00
lntue
ca8b14de51
[libc][math] Implement fast pass for double precision atan2 with 1 ULP errors. (#100648) 2024-07-26 09:56:46 -04:00
Mikhail R. Gadelha
e90d552c77
[libc][NFC] Update riscv documentation (#100578)
This adds linux-riscv32 to the documentation and fixes riscv's
entrypoint broken link.
2024-07-25 13:25:09 -03:00
Job Henandez Lara
7b51777ed8
[libc][math][c23] add entrypoints and tests for totalordermag{f,l,f128} (#100159)
Fixes https://github.com/llvm/llvm-project/issues/100139
2024-07-24 19:53:23 -04:00
Job Henandez Lara
c1562374c8
[libc][math][c23] Add entrypoints and tests for dsqrt{l,f128} (#99815) 2024-07-21 15:55:11 -04:00
Job Henandez Lara
af0f58cf14
[libc][math][c23] Add entrypoints and tests for fsqrt{,l,f128} (#99669) 2024-07-21 11:17:41 -04:00
aaryanshukla
a2f61ba08b
[libc][math]fadd implementation (#99694)
- **[libc] math fadd**
- **[libc][math] implemented fadd**
2024-07-19 14:40:34 -07:00
OverMighty
9fb049c8c6
[libc][math][c23] Add {f,d}mul{l,f128} and f16mul{,f,l,f128} C23 math functions (#98972)
Part of #93566.
                
Fixes #94833.
2024-07-18 19:50:49 +02:00
lntue
7fc9fb9f3f
[libc][math] Implement double precision cbrt correctly rounded to all rounding modes. (#99262)
Division-less Newton iterations algorithm for cube roots.

1. **Range reduction**

For `x = (-1)^s * 2^e * (1.m)`, we get 2 reduced arguments `x_r` and `a`
as:
```
  x_r = 1.m
  a   = (-1)^s * 2^(e % 3) * (1.m)
```
Then `cbrt(x) = x^(1/3)` can be computed as:
```
  x^(1/3) = 2^(e / 3) * a^(1/3).
```

In order to avoid division, we compute `a^(-2/3)` using Newton method
and then
multiply the results by a:
```
  a^(1/3) = a * a^(-2/3).
```

2. **First approximation to a^(-2/3)**

First, we use a degree-7 minimax polynomial generated by Sollya to
approximate `x_r^(-2/3)` for `1 <= x_r < 2`.
```
  p = P(x_r) ~ x_r^(-2/3),
```
with relative errors bounded by:
```
  | p / x_r^(-2/3) - 1 | < 1.16 * 2^-21.
```

Then we multiply with `2^(e % 3)` from a small lookup table to get:
```
  x_0 = 2^(-2*(e % 3)/3) * p
      ~ 2^(-2*(e % 3)/3) * x_r^(-2/3)
      = a^(-2/3)
```
with relative errors:
```
  | x_0 / a^(-2/3) - 1 | < 1.16 * 2^-21.
```
This step is done in double precision.

3. **First Newton iteration**

We follow the method described in:
Sibidanov, A. and Zimmermann, P., "Correctly rounded cubic root
evaluation
in double precision", https://core-math.gitlabpages.inria.fr/cbrt64.pdf
to derive multiplicative Newton iterations as below:
Let `x_n` be the nth approximation to `a^(-2/3)`. Define the n^th error
as:
```
  h_n = x_n^3 * a^2 - 1
```
Then:
```
  a^(-2/3) = x_n / (1 + h_n)^(1/3)
           = x_n * (1 - (1/3) * h_n + (2/9) * h_n^2 - (14/81) * h_n^3 + ...)
```
using the Taylor series expansion of `(1 + h_n)^(-1/3)`.

Apply to `x_0` above:
```
  h_0 = x_0^3 * a^2 - 1
      = a^2 * (x_0 - a^(-2/3)) * (x_0^2 + x_0 * a^(-2/3) + a^(-4/3)),
```
it's bounded by:
```
  |h_0| < 4 * 3 * 1.16 * 2^-21 * 4 < 2^-17.
```
So in the first iteration step, we use:
```
  x_1 = x_0 * (1 - (1/3) * h_n + (2/9) * h_n^2 - (14/81) * h_n^3)
```
Its relative error is bounded by:
```
  | x_1 / a^(-2/3) - 1 | < 35/242 * |h_0|^4 < 2^-70.
```
Then we perform Ziv's rounding test and check if the answer is exact.
This step is done in double-double precision.

4. **Second Newton iteration**

If the Ziv's rounding test from the previous step fails, we define the
error
term:
```
  h_1 = x_1^3 * a^2 - 1,
```
And perform another iteration:
```
  x_2 = x_1 * (1 - h_1 / 3)
```
with the relative errors exceed the precision of double-double.
We then check the Ziv's accuracy test with relative errors < 2^-102 to
compensate for rounding errors.

5. **Final iteration**
 
If the Ziv's accuracy test from the previous step fails, we perform
another
iteration in 128-bit precision and check for exact outputs.
2024-07-17 12:23:14 -04:00
Joseph Huber
60ff9c2ea5
[libc] Add support for powi as an LLVM libc extension on the GPU (#98236)
Summary:
This function is used by the CUDA / HIP / OpenMP headers and exists as
an NVIDIA extension basically. This function is implemented in the C23
standard as `pown`, but for now we need to provide `powi` for backwards
compatibility. In the future this entrypoint will just be a redirect to
`pown` once that is implemented.
2024-07-09 20:51:36 -05:00
lntue
c9ee6b1977
[libc][math] Implement cbrtf function correctly rounded to all rounding modes. (#97936)
Fixes https://github.com/llvm/llvm-project/issues/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.
2024-07-08 10:02:12 -04:00
Hendrik Hübner
f8834ed24b
[libc][C23][math] Implement cospif function correctly rounded for all rounding modes (#97464)
I also fixed a comment in sinpif.cpp in the first commit. Should this be
included in this PR?

All tests were passed, including the exhaustive test.

CC: @lntue
2024-07-06 09:24:05 -04:00
OverMighty
ac76ce2693
[libc][math][c23] Classify f16fma{,f,l} as LLVM libc extensions (#97728) 2024-07-05 09:58:01 -04:00
lntue
7d68d9d2f2
[libc][math] Implement correctly rounded double precision tan (#97489)
Using the same range reduction as `sin`, `cos`, and `sincos`:
1) Reducing `x = k*pi/128 + u`, with `|u| <= pi/256`, and `u` is in
double-double.
2) Approximate `tan(u)` using degree-9 Taylor polynomial.
3) Compute
```
   tan(x) ~ (sin(k*pi/128) + tan(u) * cos(k*pi/128)) / (cos(k*pi/128) - tan(u) * sin(k*pi/128))
```
using the fast double-double division algorithm in [the CORE-MATH
project](https://gitlab.inria.fr/core-math/core-math/-/blob/master/src/binary64/tan/tan.c#L1855).
4) Perform relative-error Ziv's accuracy test
5) If the accuracy tests failed, we redo the computations using 128-bit
precision `DyadicFloat`.

Fixes https://github.com/llvm/llvm-project/issues/96930
2024-07-03 18:05:24 -04:00
OverMighty
4e56724213
[libc][math][c23] Add f16{add,sub}{,l,f128} C23 math functions (#97072)
Part of #93566.
2024-07-02 19:27:09 -04:00
OverMighty
12a1e6dd12
[libc][math][c23] Add f16{add,sub}f C23 math functions (#96787)
Part of #93566.
2024-07-02 09:16:12 -04:00
Hendrik Hübner
ea93c538c7
[libc][math][c23] Implemented sinpif function correctly rounded for all rounding modes. (#97149)
This implements the sinpif function. An exhaustive test shows it's
correct for all rounding modes.

Issue:  #94895
2024-07-01 16:38:03 -04:00
OverMighty
6c1c451b86
[libc][math][c23] Add f16sqrt{,l,f128} C23 math functions (#96642)
Part of #95250.
2024-06-30 19:20:39 -04:00
OverMighty
56ef6a2eb2
[libc][math][c23] Add f16div{,l,f128} C23 math functions (#97054)
Part of #93566.
2024-06-29 18:48:12 -04:00
OverMighty
e34dbb127a
[libc][math][c23] Add f16fma{,l,f128} C23 math function (#96711)
Part of #93566.
2024-06-27 14:44:19 -04:00