Commit Graph

307 Commits

Author SHA1 Message Date
Joseph Huber
4b53cbb069 [libc] Export configs for scanf support (#98066)
Summary:
This patch adds the options for configuring floating point and index
mode for scanf just like `printf`. Not enabling it on the GPU yet, need
to fix something else first.
2024-07-08 15:11:52 -05:00
Joseph Huber
12e47aabd4 [libc] Add config option for fast math optimizations (#98029)
Summary:
This patch adds `LIBC_COPT_MATH_OPTIMIZATIONS` that allows users to
configure the
different math optimizations.
2024-07-08 11:01:03 -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
Izaak Schroeder
b151c7e36a [libc] Add dlfcn.h placeholder (#97501)
Adds `dlopen` and friends. This is needed as part of the effort to
compile `libunwind` + `libc` without baremetal mode. This is part of
https://github.com/llvm/llvm-project/issues/97191. This should still be
spec compliant, since `dlopen` always returns `NULL` and `dlerror`
always returns an error message.

> If dlopen() fails for any reason, it returns NULL.

> The function dlclose() returns 0 on success, and nonzero on error.

> Since the value of the symbol could actually be NULL (so that a NULL
return from dlsym() need not indicate an error), the correct way to test
for an error is to call dlerror() to clear any old error conditions,
then call dlsym(), and then call dlerror() again, saving its return
value into a variable, and check whether this saved value is not NULL.


See:
- https://linux.die.net/man/3/dlopen
2024-07-06 16:01:59 -07: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
PiJoules
665efe8967 [libc] Add LIBC_NAMESPACE_DECL macro (#97109)
This defines to LIBC_NAMESPACE with
`__attribute__((visibility("hidden")))` so all the symbols under it have
hidden visibility. This new macro should be used when declaring a new
namespace that will have internal functions/globals and LIBC_NAMESPACE
should be used as a means of accessing functions/globals declared within
LIBC_NAMESPACE_DECL.
2024-07-03 17:02:57 -07:00
Michael Jones
2ef5b8227a [libc][docs] Update full host build docs (#97643)
Add a note explaining how to fix the missing `asm` folder, as well as a
warning about installing without setting a sysroot.
2024-07-03 16:29:19 -07: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
f1a8f94bba [libc][docs] Add doc for using containers to test on a different arch (#97431) 2024-07-03 11:07:49 -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
Joseph Huber
3c64a98180 [libc] Partially implement 'errno' on the GPU (#97107)
Summary:
The `errno` variable is expected to be `thread_local` by the standard.
However, the GPU targets do not support `thread_local` and implementing
that would be a large endeavor. Because of that, we previously didn't
provide the `errno` symbol at all. However, to build some programs we at
least need to be able to link against `errno`. Many things that would
normally set `errno` completely ignore it currently (i.e. stdio) but
some programs still need to be able to link against correct C programs.

For this purpose this patch exports the `errno` symbol as a simple
global. Internally, this will be updated atomically so it's at least not
racy. Externally, this will be on the user. I've updated the
documentation to state as such. This is required to get `libc++` to
build.
2024-07-01 06:30:15 -05:00
Joseph Huber
ec0e6ef09b [libc] Implement the 'remove' function on the GPU (#97096)
Summary:
Straightforward RPC implementation of the `remove` function for the GPU.
Copies over the string and calls `remove` on it, passing the result
back. This is required for building some `libc++` functionality.
2024-07-01 06:29:48 -05: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
lntue
4080f174ab [libc][math] Implement double precision sincos correctly rounded to all rounding modes. (#96719)
Sharing the same algorithm as double precision sin:
https://github.com/llvm/llvm-project/pull/95736 and cos:
https://github.com/llvm/llvm-project/pull/96591
2024-06-27 10:15:22 -04:00
Joseph Huber
e1015ae55d [libc][docs] List rand and srand as supported on the GPU (#96757)
Summary:
I initially didn't report these as supported because they didn't provide
expected behavior and were very wasteful. The recent patch moved them to
a lock-free atomic implementation so they can now actually be used.
2024-06-26 11:45:47 -05:00
lntue
88f80aeb0c [libc][math] Implement double precision cos correctly rounded to all rounding modes. (#96591)
Sharing the same algorithm as double precision sin:
https://github.com/llvm/llvm-project/pull/95736
2024-06-25 16:51:31 -04:00
OverMighty
edbe698ead [libc][math][c23] Add f16divf C23 math function (#96131)
Part of #93566.
2024-06-25 08:48:28 -04:00
lntue
16903ace18 [libc][math] Implement double precision sin correctly rounded to all rounding modes. (#95736)
- Algorithm:
- Step 1 - Range reduction: for a double precision input `x`, return `k`
and `u` such that
    - k is an integer
    - u = x - k * pi / 128, and |u| < pi/256
- Step 2 - Calculate `sin(u)` and `cos(u)` in double-double using Taylor
polynomials with errors < 2^-70 with FMA or < 2^-66 w/o FMA.
- Step 3 - Calculate `sin(x) = sin(k*pi/128) * cos(u) + cos(k*pi/128) *
sin(u)` using look-up table for `sin(k*pi/128)` and `cos(k*pi/128)`.
- Step 4 - Use Ziv's rounding test to decide if the result is correctly
rounded.
- Step 4' - If the Ziv's rounding test failed, redo step 1-3 using
128-bit precision.
- Currently, without FMA instructions, the large range reduction only
works correctly for the default rounding mode (FE_TONEAREST).
- Provide `LIBC_MATH` flag so that users can set `LIBC_MATH =
LIBC_MATH_SKIP_ACCURATE_PASS` to build the `sin` function without step 4
and 4'.
2024-06-24 17:57:08 -04:00
OverMighty
b5efd21429 [libc][math][c23] Add {ldexp,scalbn,scalbln}f16 C23 math functions (#94797)
Part of #93566.
2024-06-21 09:01:47 -04:00
PiJoules
d809152266 [libc] Control freelist malloc buffer size with a config (#96248)
Rather than propgating a compile define, add an explicit cmake flag for
controlling the size. The default for baremetal is 100KB and the default
for others is 1GB.
2024-06-20 16:43:47 -07:00
OverMighty
1107575c95 [libc][math][c23] Add {getpayload,setpayload,setpayloadsig}f16 C23 math functions (#95159)
Part of #93566.
2024-06-20 13:33:34 -04:00
Schrodinger ZHU Yifan
41fecca97b [libc] add rwlock (#94156) 2024-06-14 13:34:28 -07:00
OverMighty
f3aceeee8a [libc][math][c23] Add f16fmaf C23 math function (#95483)
Part of #93566.
2024-06-14 12:31:32 -04:00
OverMighty
a239343521 [libc][math][c23] Add f16sqrtf C23 math function (#95251)
Part of #95250.
2024-06-13 12:57:24 -04:00
OverMighty
f5dcfb9968 [libc][math][c23] Add {totalorder,totalordermag}f16 C23 math functions (#95014)
Part of #93566.
2024-06-11 11:04:48 -04:00
OverMighty
7683a16dbf [libc][math][c23] Add {remainder,remquo}f16 C23 math functions (#94773)
Part of #93566.
2024-06-10 11:02:09 -04:00
OverMighty
10cd96dd33 [libc][math][c23] Add {frexp,ilogb,llogb,logb,modf}f16 C23 math functions (#94758)
Part of #93566.
2024-06-10 08:38:47 -04:00
OverMighty
cb1a727dea [libc][math][c23] Add nanf16 C23 math function (#94767)
Part of #93566.
2024-06-10 00:19:22 -04:00
Hendrik Hübner
44aecca020 [libc][math][C23] Implemented remquof128 function (#94809)
Added remquof128 function. Closes #94312
2024-06-08 15:08:45 -04:00
Job Henandez Lara
263be9fb00 [libc][math][c23] fmul correcly rounded to all rounding modes (#91537)
This is an implementation of floating point multiplication:

It will consist of 
   - `double x double -> float`
2024-06-08 15:07:27 -04:00
OverMighty
0cdb0b7473 [libc][math][c23] Add fmodf16 C23 math function (#94629)
Part of #93566.
2024-06-07 18:26:58 -04:00
OverMighty
dd1cd02a43 [libc][math][c23] Add {fmaximum,fminimum}{,_mag,_mag_num,_num} C23 math functions (#94510)
#93566
2024-06-06 11:20:29 -04:00
OverMighty
63cda2d19c [libc][math][c23] Add {nextafter,nexttoward,nextup,nextdown}f16 C23 math functions (#94535)
#93566
2024-06-05 23:06:48 -04:00
Hendrik Hübner
8e67495326 [libc][math][c23] Implement fmaxf16 and fminf16 function (#94131)
Implements fmaxf16 and fminf16, which are two missing functions listed
here: #93566
2024-06-05 22:44:44 -04:00
OverMighty
c537f35646 [libc][math][c23] Add fdimf16 C23 math function (#94354)
#93566
2024-06-05 10:37:55 -04:00
OverMighty
6c97303681 [libc][math][c23] Add copysignf16 C23 math function (#94351)
#93566
2024-06-05 09:46:36 -04:00
OverMighty
3614beede1 [libc][math][c23] Add canonicalizef16 C23 math function (#94341)
#93566
2024-06-05 08:24:23 -04:00
Joachim Meyer
8896f21ca9 [NFC] Fix typo in libc/docs/gpu/using.rst 2024-06-04 15:40:45 -07:00
OverMighty
6b5ae148e5 [libc][math][c23] Add {fromfp,fromfpx,ufromfp,ufromfpx}f16 C23 math functions (#94254)
https://github.com/llvm/llvm-project/issues/93566
2024-06-04 18:29:53 -04:00
OverMighty
2635d0419e [libc][math][c23] Add {nearbyint,rint,lrint,llrint,lround,llround}f16 C23 math functions (#94218)
https://github.com/llvm/llvm-project/issues/93566
2024-06-04 10:03:31 -04:00
OverMighty
25b037bdb5 [libc][math][c23] Add {ceil,floor,round,roundeven,trunc}f16 C23 math functions (#94001) 2024-06-03 14:28:51 -04:00
Schrodinger ZHU Yifan
142afde0eb [libc] rework mutex (#92168) 2024-05-31 18:57:18 -07:00
OverMighty
0eb9e021b1 [libc][math][c23] Add fabsf16 C23 math function (#93567)
cc @lntue
2024-05-30 15:37:15 -04:00
Michael Flanders
0f6c4d8b06 [libc][docs] adds macro handling, POSIX status, and validation to docgen (#89421)
docgen now lists macro implementation status in the generated rst files.

Adds POSIX definition link property to docgen json API (`posix-definition`) and
changes the `defined` property of docgen json API to `c-definition`. Now that
docgen's api is getting more specified, adds validation checks to docgen to
start codifying the docgen api spec.

To make sure this all looks good, I've added POSIX definition links to signal.h
as a tester.
2024-05-23 10:48:16 -07:00
Fabian Keßler
cd7a7a56fc Add basic char*_t support for libc (partial WG14 N2653) (#90360)
This PR implements a part of WG14 N2653:
 - Define C23 char8_t
 - Define C11 char16_t
 - Define C11 char32_t
 
 Missing goals are:
- The type of UTF-8 character literals is changed from unsigned char to
char8_t. (Since UTF-8 character literals already have type unsigned
char, this is not a semantic change).
- New mbrtoc8() and c8rtomb() functions declared in <uchar.h> enable
conversions between multibyte characters and UTF-8.
    - A new ATOMIC_CHAR8_T_LOCK_FREE macro.
    - A new atomic_char8_t typedef name.
2024-04-30 15:08:38 -07:00