4192 Commits

Author SHA1 Message Date
Kazu Hirata
c5229e92ce
[libc] Fix typos in documentation (#139852) 2025-05-14 07:14:43 -07:00
OverMighty
6a125afdd5
[libc] Fix DyadicFloat::generic_as() using FPBits<float16> not FPBits<T> (#139899) 2025-05-14 15:59:24 +02:00
Joseph Huber
4e604d4668 [libc][NFC] Add u64 shuffle helpers to GPU
Summary:
Precommit for later use.
2025-05-13 18:00:53 -05:00
Mikhail R. Gadelha
36b4ffeb7e
[libc] Enable utimes function for riscv (#139181)
RV32 uses SYS_utimensat_time64 instead of SYS_utimensat but the call is
the same.
2025-05-13 18:59:49 -03:00
Mikhail R. Gadelha
66bb445d5c
[libc] Enable poll function for riscv (#139180)
RV32 uses SYS_ppoll_time64 instead of SYS_ppoll, but the call is the
same.
2025-05-13 18:59:28 -03:00
lntue
79e8e27bfe
[libc] Add proxy header for ACTION type. (#139796)
https://github.com/llvm/llvm-project/issues/139561
2025-05-13 17:50:02 -04:00
lntue
3b62a3369a
[libc] Add struct entry definition in addition to ENTRY according to POSIX. (#139773) 2025-05-13 15:48:30 -04:00
lntue
e581f1cc9a
[libc] Add proxy header for ENTRY type. (#139746)
https://github.com/llvm/llvm-project/issues/139561
2025-05-13 14:07:21 -04:00
Raul Tambre
fc3b67aee8
[cmake] Normalize TARGET_SUBDIR paths (#138524)
Some code paths normalize ".." and thus don't create the directory. But some execute in a
shell thus requiring the directory to exist to be able to take the parent directory.

This patch normalizes all the `TARGET_SUBDIR` variables to avoid this issue.
2025-05-12 15:59:39 -04:00
Tristan Ross
865fb9c1a5
[libc][uefi] add crt1 (#132150)
Adds `crt1.o` for the UEFI platform in the LLVM C library. This makes
things start to become useful.
2025-05-09 09:02:33 -07:00
lntue
a179a1b3a8
[libc][math] Add x86-64 sqrt specialization for 80-bit long double. (#139203) 2025-05-09 10:31:31 -04:00
lntue
78cc822aa6
[libc][math] Implement double precision acos correctly rounded for all rounding modes. (#138308)
We reduce computation of `acos` to `asin` as follow:

When `|x| < 0.5`:
```math
acos(x) = \frac{\pi}{2} - asin(x).
```
For `0.5 <= |x| < 1`, let
```math
u = \frac{1 - \left| x \right|}{2},
```
then
```math
acos(x) = \begin{cases}
  2 \cdot asin \left( \sqrt{u} \right) &, 0.5 \leq x < 1 \\
  \pi - 2 \cdot asin \left( \sqrt{u} \right) &, -1 < x \leq 0.5 
\end{cases}
```
2025-05-08 23:23:09 -04:00
Mikhail R. Gadelha
52e5889d0e
[libc] Enable exp10m1f on RISC-V (#138768)
Previously, the test failed due to isnan() and isinf() not being
defined.

This patch follows other tests in the same directory and calls isnan and
isinf from the FBits class.

---------

Co-authored-by: OverMighty <its.overmighty@gmail.com>
2025-05-07 13:22:09 -03:00
Mikhail R. Gadelha
ca0c9bcf10
[libc] Update riscv entrypoints to be on par with x86 (#138597)
This patch updates the riscv entrypoints with almost all functions that
are currently supported on x86. I left six functions commented, as I'll
send separate PRs to enable them:

* poll: needs a new syscall implementation
* utimes: needs a new syscall implementation
* setitimer: test never finished in rv32
* getitimer: test fails in rv32
* exp10m1f: test case doesn't build but it's an easy fix
* sqrtulk: needs to be implemented

I also added the 16-bit fp functions, however, they are not enabled due
to a cmake check in float16-macros.h. To enable 16-bit fp in riscv, we
need to update the buildbots with a clang version that includes the fix
from commit PR #119481
2025-05-06 13:56:58 -03:00
Mikhail R. Gadelha
2dd296aa47
[libc] Fix implicit conversion error on rv32 (#138622)
This patch fixes the following error on rv32 (and possibly other 32-bit
archs):

FPBits.h:760:40: error: implicit conversion loses integer precision:
'int' to 'size_t' (aka 'unsigned int')
[-Werror,-Wimplicit-int-conversion]
  760 |       result.set_significand(number >> -ep);

Fixes #138425.
2025-05-06 11:31:09 -03:00
enh-google
e79dc76b5f
stdlib.yaml: qsort_r() is in POSIX 2024 (aka issue 8). (#138572) 2025-05-06 09:36:10 -04:00
Mikhail R. Gadelha
230f332cf0
[libc] Swap order of syscall on chmod (#138427)
We define SYS_fchmodat2 on libc but the syscall is not available on old
kernels, so prefer the SYS_fchmodat version when possible.
2025-05-05 18:49:33 -03:00
enh-google
673047ea2b
qsort_r.h: qsort_r() is POSIX now. (#138545) 2025-05-05 16:19:58 -04:00
Howard Chu
c275fdc0bc
[libc][gpu][docs] Fix typos in GPU libc's documentation (#138565)
Fix typos in GPU libc's documentation about the rpc implementation.
2025-05-05 14:06:45 -05:00
Joseph Huber
7f5bf77c65
[libc] Temporarily disable hanging test on sm_60 (#138561) 2025-05-05 13:17:17 -05:00
Schrodinger ZHU Yifan
a1803ea063
[libc] implement sigsetjmp/siglongjmp for riscv (#137992)
See https://godbolt.org/z/jo7s6j7sq for compiled code.

```c++
#if __riscv_xlen == 64
#define STORE(A, B, C) "sd " #A ", %c[" #B "](" #C ")\n\t"
#define LOAD(A, B, C) "ld " #A ", %c[" #B "](" #C ")\n\t"
#elif __riscv_xlen == 32
#define STORE(A, B, C) "sw " #A ", %c[" #B "](" #C ")\n\t"
#define LOAD(A, B, C) "lw " #A ", %c[" #B "](" #C ")\n\t"
#else
#error "Unsupported RISC-V architecture"
#endif

namespace LIBC_NAMESPACE_DECL {
[[gnu::naked]]
LLVM_LIBC_FUNCTION(int, sigsetjmp, (sigjmp_buf, int)) {
  // clang-format off
  asm("beqz a1, .Lnosave\n\t"
      STORE(ra, retaddr, a0)
      STORE(s0, extra, a0)
      "mv s0, a0\n\t"
      "call %c[setjmp]\n\t"
      "mv a1, a0\n\t"
      "mv a0, s0\n\t"
      LOAD(s0, extra, a0)
      LOAD(ra, retaddr, a0)
      "tail %c[epilogue]\n"
".Lnosave:\n\t"
      "tail %c[setjmp]"
      // clang-format on
      ::[retaddr] "i"(offsetof(__jmp_buf, sig_retaddr)),
      [extra] "i"(offsetof(__jmp_buf, sig_extra)), [setjmp] "i"(setjmp),
      [epilogue] "i"(sigsetjmp_epilogue)
      : "a0", "a1", "s0");
}
```
2025-05-04 16:21:48 -04:00
Mikhail R. Gadelha
ebdf2e06ff
[libc] Add missing dependency for test target (#138423)
It seems we were missing a dependency when adding a new test target,
e.g., test libc.test.src.math.sqrt_test.__unit__ would create a
custom target in the form of:

```
  add_custom_target(
    libc.test.src.__support.FPUtil.dyadic_float_test.__unit__
    COMMAND  ${LIBC_UNITTEST_ENV} ${CMAKE_CROSSCOMPILING_EMULATOR} libc.test.src.__support.FPUtil.dyadic_float_test.__unit__.__build__
    COMMENT Running unit test libc.test.src.__support.FPUtil.dyadic_float_test.__unit__ )
``` 

but it wouldn't set that it depends on
libc.test.src.__support.FPUtil.dyadic_float_test.__unit__.__build__
being built.

For some reason, it would break the rv32 buildbot, as it would try to
run a test but the __build__ is nowhere to be found, since it wasn't
built in the first place.
2025-05-03 18:10:13 -03:00
William
da3022577e
[libc] Add support for string/memory_utils functions for AArch64 without HW FP/SIMD (#137592)
Add conditional compilation to add support for AArch64 without vector
registers and/or hardware FPUs by using the generic implementation.

**Context:**

A few functions were hard-coded to use vector registers/hardware FPUs.
This meant that libc would not compile on architectures that did not
support these features. This fix falls back on the generic
implementation if a feature is not supported.
2025-05-02 13:36:00 +02:00
Joseph Huber
927cdd2c73
[libc] Fix typo in uint128 type for PPC printf handling (#138157) 2025-05-01 10:52:41 -05:00
Schrodinger ZHU Yifan
80233751f9
[libc][NFC] add remarks to the setjmp implementation (#137066)
We use naked functions to avoid compiler-generated prologue and
epilogue.

Despite GCC documentation listing this as an unsupported case for
extended asm, the generated code is not wrong as we only pass in
constant operands to extended asm.

See https://github.com/llvm/llvm-project/issues/137055 for related
remarks.

---------

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
2025-05-01 10:41:33 -04:00
Joseph Huber
21dcf6bcd1 [libc] Make sure we use the right linker for bitcode linking
Summary:
We need to use the default arguments to ensure this uses the right
linker for the toolchain given cross-compiling contexts.
2025-04-30 16:25:28 -05:00
enh-google
0e9fb5202c
[libc] lsearch_test.cpp: put helpers in anonymous namespace. (#137964) 2025-04-30 11:55:35 -04:00
enh-google
a24457eb99
[libc] lfind_test.cpp: put helpers in an anonymous namespace. (#137821)
This matches other tests and allows the tests to be built together (as
Android is doing).
2025-04-29 18:16:59 -04:00
Schrodinger ZHU Yifan
9ebaa9d483
[libc] implement aarch64 sigsetjmp (#136706)
- **[libc][aarch64] implement sigsetjmp**

On top of https://github.com/llvm/llvm-project/pull/136072
See also https://github.com/llvm/llvm-project/issues/137055 for remarks
on naked attributes.

```c++
//===-- Implementation of setjmp ------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#include "src/setjmp/sigsetjmp.h"
#include "hdr/offsetof_macros.h"
#include "src/__support/common.h"
#include "src/__support/macros/config.h"
#include "src/setjmp/setjmp_impl.h"
#include "src/setjmp/sigsetjmp_epilogue.h"

namespace LIBC_NAMESPACE_DECL {
[[gnu::naked]]
LLVM_LIBC_FUNCTION(int, sigsetjmp, (sigjmp_buf, int)) {
  asm(R"(
      cbz w1, %c[setjmp]
      str x30, [x0, %c[retaddr]]
      str x19, [x0, %c[extra]]
      mov x19, x0
      bl %c[setjmp]
      mov w1, w0
      mov x0, x19
      ldr x30, [x0, %c[retaddr]]
      ldr x19, [x0, %c[extra]]
      b %c[epilogue])" ::[retaddr] "i"(offsetof(__jmp_buf, sig_retaddr)),
      [extra] "i"(offsetof(__jmp_buf, sig_extra)), [setjmp] "i"(setjmp),
      [epilogue] "i"(sigsetjmp_epilogue)
      : "x0", "x1", "x19", "x30");
}
} // namespace LIBC_NAMESPACE_DECL
```
2025-04-29 16:37:25 -04:00
Schrodinger ZHU Yifan
6695976d16
Reland "[libc] build fix for sigsetjmp (#137047)" (#137214)
Reland `sigsetjmp` patches with build fixes.

We wrap every target replying on the epilogue library into conditional
checks.

---------

Co-authored-by: Petr Hosek <phosek@google.com>
2025-04-29 09:28:42 -04:00
Michael Jones
9124e963a3
[libc] Update assert for C23 (#137402)
Previously the assert macro took one argument named "e", but this led to
possible errors if the caller had commas in their input. C23 changed the
definition of assert to use `__VA_ARGS__` to ensure comma cases are
handled properly. This patch doesn't introduce the enforcement function
mentioned in the standard update, though that may be done in a followup.

Fixes #136184
2025-04-28 15:06:52 -04:00
Krishna Pandey
d8e81756b3
[libc][stdfix] Fix riscv entrypoints for idivfx (#137499)
Fixes a typo in riscv entrypoints that caused buildbot failures.

https://lab.llvm.org/buildbot/#/builders/196/builds/7352

Signed-off-by: krishna2803 <kpandey81930@gmail.com>
2025-04-27 06:54:01 -04:00
lntue
ade502a8c4
[libc][math] Implement double precision asin correctly rounded for all rounding modes. (#134401)
Main algorithm:

The Taylor series expansion of `asin(x)` is:
```math
\begin{align*}
  asin(x) &= x + x^3 / 6 + 3x^5 / 40 + ... \\
       &= x \cdot P(x^2) \\
       &= x \cdot P(u) &\text{, where } u = x^2.
\end{align*}
```
For the fast path, we perform range reduction mod 1/64 and use degree-7
(minimax + Taylor) polynomials to approximate `P(x^2)`.

When `|x| >= 0.5`, we use the transformation:
```math
  u = \frac{1 + x}{2}
```
and apply half-angle formula to reduce `asin(x)` to:
```math
\begin{align*}
  asin(x) &= sign(x) \cdot \left( \frac{\pi}{2} - 2 \cdot asin(\sqrt{u}) \right) \\
       &= sign(x) \cdot \left( \frac{\pi}{2} - 2 \cdot \sqrt{u} \cdot P(u) \right).
\end{align*}
```
Since `0.5 <= |x| <= 1`, `|u| <= 0.5`. So we can reuse the polynomial
evaluation of `P(u)` when `|x| < 0.5`.

For the accurate path, we redo the computations in 128-bit precision
with degree-15 (minimax + Taylor) polynomials to approximate `P(u)`.
2025-04-25 09:55:21 -04:00
Krishna Pandey
5ff277462d
[libc][stdfix] Implement idivfx functions in LLVM libc (#133005)
This PR implements the following 8 functions along with the tests.

```c++
int idivr(fract, fract);
long int idivlr(long fract, long fract);
int idivk(accum, accum);
long int idivlk(long accum, long accum);

unsigned int idivur(unsigned fract, unsigned fract);
unsigned long int idivulr(unsigned long fract, unsigned long fract);
unsigned int idivuk(unsigned accum, unsigned accum);
unsigned long int idivulk(unsigned long accum, unsigned long accum);
```

ref: https://www.iso.org/standard/51126.html

Fixes #129125

---------

Signed-off-by: krishna2803 <kpandey81930@gmail.com>
2025-04-25 07:58:16 -04:00
Harrison Hao
accee2b553
[libc][math][c23] Add atanhf16 C23 math function. (#132612)
Implementation of atanhf16 function for 16-bit inputs.

Closes: https://github.com/llvm/llvm-project/issues/132209
2025-04-25 07:53:52 -04:00
Anton
851f7c7421
[libc][math][c23] Add acospif16() function (#134664)
Addresses #132211  #132754
Part of #95250
2025-04-24 18:03:24 -04:00
Tejas Vipin
dde00f5e22
[libc][math] Improve performance test framework (#134501)
- Merges `BinaryOpSingleOutputPerf.h` and
`SingleInputSingleOutputPerf.h` files into a unified `PerfTest.h` and
update all performance tests to use this.
- Improve the output printed to log file for tests.
- Removes unused `run_diff` method and redundant `run_perf` call in
`BINARY_INPUT_SINGLE_OUTPUT_PERF_EX` (previously
`BINARY_OP_SINGLE_OUTPUT_PERF_EX`)
- Change `BINARY_INPUT_SINGLE_OUTPUT_PERF_EX` and
`SINGLE_INPUT_SINGLE_OUTPUT_PERF` to not define `main`
2025-04-24 07:22:21 -04:00
gulfemsavrun
0d00b6bc3b
Revert "[libc] build fix for sigsetjmp (#137047)" (#137077)
This reverts commit f07511a0e0d2ac9bee9ae12a9ad68e279e352634.

This reverts commit 5bb4cf9d9189c41de50adffd960eb2188140eb9c.

It caused a CMake configuration issue.
2025-04-23 15:49:55 -07:00
Schrodinger ZHU Yifan
f07511a0e0
[libc] build fix for sigsetjmp (#137047)
This PR fixes the build failure due to the `sigsetjmp` implementation.

1. Use a most relaxed input constraint to fix `clang` build.
2. Avoid create alias target if os directory for `sigsetjmp_epilogue`
does not exist.
2025-04-23 15:50:55 -04:00
Schrodinger ZHU Yifan
5bb4cf9d91
[libc] implement sigsetjmp/siglongjmp for x86-64 (#136072) 2025-04-23 14:19:47 -04:00
Joseph Huber
cc6def4b75
[libc] Special case PPC double double for print (#136614)
Summary:
We use the storage class for `long double` in the printing
implementations. We don't fully support the PPC double double type,
which that maps to, but we can stub out just the support needed for the
print interface to works. This required using the internal interface for
storage type, but it should be good enough.

Fixes: https://github.com/llvm/llvm-project/issues/136596
2025-04-23 10:36:14 -05:00
lntue
7547ad3a7b
[libc][math] Skip checking for exceptional values in expm1f when LIBC_MATH_SKIP_ACCURATE_PASS is set. (#130968) 2025-04-23 12:04:21 +07:00
Daniel Thornburgh
710ffb69bf
[libc] Fix warnings for freelist_heap_test/fuzz (#136634)
Fixes #122367
2025-04-22 11:11:31 -07:00
Joseph Huber
a5cdbef5f0 Revert "[LLVM] Replace use of LLVM_RUNTIMES_TARGET with LLVM_DEFAULT_TARGET_TRIPLE (#136208)"
This reverts commit 2e145f11c0bcfa2052416d96d682c75f33971a8c.

Somehow causes some static assertions to fail?
2025-04-22 08:08:51 -05:00
Joseph Huber
2e145f11c0
[LLVM] Replace use of LLVM_RUNTIMES_TARGET with LLVM_DEFAULT_TARGET_TRIPLE (#136208)
Summary:
For purposes of determining the triple, it's more correct to use
`LLVM_DEFAULT_TARGET_TRIPLE`.
2025-04-22 07:59:54 -05:00
lntue
f351172d4a
[libc] Export standard C symbols in the public packages for MacOS instead of namespaced C++ symbols. (#136100) 2025-04-17 16:02:44 +08:00
Wu Yingcong
6b8d072cfd
[libc] Fix incorrect unsigned comparison (#135595)
There is a problem with such unsigned comparison pattern:
```
if(unsigned_a - unsigned_b > 0) { /* only NOT go here when unsigned_a==unsigned_b */ }
```
When `unsigned_a` < `unsigned_b`, the result will still be `>0` due to
underflow.
This patch fixes two of the occurrences I found.
Also remove two redundant `if` where its condition is guaranteed by
outer `if`.
2025-04-17 09:33:48 +08:00
Vinay Deshmukh
77f0708b9d
[libc]: Remove -Wglobal-constructors for libc tests (#131485)
* Relates to: https://github.com/llvm/llvm-project/issues/119281
* Removes `-Wglobal-constructors` as per:
https://github.com/llvm/llvm-project/pull/131485#pullrequestreview-2728020622
2025-04-15 19:24:07 -05:00
Tsz Chan
f5c5f9f926
[libc] Implement getitimer and setitimer, add proxy headers for itimerval (#134773)
#133983
2025-04-14 13:39:42 -07:00
Petr Hosek
a43ff0ec8a
[libc] Include extra baremetal headers and entrypoints (#135462)
These are all usable in baremetal environments.
2025-04-14 12:26:03 -07:00