Commit Graph

2299 Commits

Author SHA1 Message Date
Drew Lewis
d07a2164e7 Add generic sqrt root headers to libc sqrt specializations (#135237)
This header is needed to provide the declaration for the sqrt template.
You can build without these in the CMake build, but not having this
include in the architecture specific headers makes them not self
contained.
2025-04-10 12:04:41 -07:00
lntue
c5e07fb861 [libc] Use correct instruction for arm32 sqrt inline asm. (#134968)
https://godbolt.org/z/3jT7jdrs9
2025-04-09 00:01:45 -04:00
lntue
438ade1dfc [libc] Fix wrong #ifdef for riscv's sqrt. (#134964) 2025-04-08 22:40:20 -04:00
lntue
cf7d34a54d [libc] Extend fputil::sqrt to use floating point instructions for arm32. (#134499) 2025-04-08 22:14:38 -04:00
wldfngrs
fdf20941a8 [libc][math] Fix signaling NaN handling for math functions. (#133347)
Add tests for signaling NaNs, and fix function behavior for handling
signaling NaN input.

Fixes https://github.com/llvm/llvm-project/issues/124812
2025-04-08 15:23:38 +02:00
jobhdez
1d7bd3bc5c [libc] Remove extra parenthesis in sin.cpp comments (#134477) 2025-04-04 23:51:56 -04:00
Michael Jones
e8b52acca2 [libc][NFC] replace NULL with nullptr (#134464)
Simple cleanup
2025-04-04 16:55:43 -07:00
Aditya Tejpaul
d33ae41c62 [libc] Implemented utimes (Issue #133953) (#134167)
This pull request implements the `utimes` command in libc ([Issue
#133953](https://github.com/llvm/llvm-project/issues/133953)).

- [x] Add the implementation of `utimes` in `/src/sys/time`.
- [x] Add tests for `utimes` in `/test/src/sys/time`. 
- [x] Add `utimes` to
[entrypoints.txt](https://github.com/llvm/llvm-project/blob/main/libc/config/linux/x86_64/entrypoints.txt)
for at least x86_64 and whatever you're building on
- [x] Add `utimes` to
[include/sys/time.yaml](https://github.com/llvm/llvm-project/blob/main/libc/include/sys/time.yaml)
2025-04-03 16:19:12 -07:00
Abhinav Kumar
51d1c72886 [libc] Added support for fixed-points in `is_signed and is_unsigned`. (#133371)
Fixes #133365

## Changes Done
- Changed the signed checking to 
```cpp
struct is_signed : bool_constant<((is_fixed_point<T> || is_arithmetic_v<T>) && (T(-1) < T(0)))>
```
in ``/libc/src/__support/CPP/type_traits/is_signed.h``. Added check for
fixed-points.
- But, got to know that this will fail for ``unsigned _Fract`` or any
unsigned fixed-point because ``unsigned _Fract`` can’t represent -1 in
T(-1), while ``unsigned int`` can handle it via wrapping.
- That's why I explicity added ``is_signed`` check for ``unsigned``
fixed-points.
- Same changes to ``/libc/src/__support/CPP/type_traits/is_unsigned.h``.
- Added tests for ``is_signed`` and ``is_unsigned``.
2025-04-02 16:41:47 -04:00
Alexey Samsonov
07504afc42 [libc] Stop depending on .cpp files in libcxx_shared_headers library. (#133999)
Fix two instances of libcxx_shared_headers depending on .cpp files (in
Bazel build):

* Don't depend on exit syscall in LIBC_ASSERT implementation. This
dependency is not used, since LIBC_ASSERT always uses system <assert.h>
in the overlay mode, which is the only mode supported by Bazel.
* Don't depend on libc_errno in str-to-float and str-to-integer
conversions. We only need the ERANGE value, which can be obtained from
the proxy header instead.
2025-04-01 16:23:19 -07:00
lntue
8741412bdf [libc][math] Implement a fast pass for atan2f128 with 1ULP error using DyadicFloat<128>. (#133150)
Part of https://github.com/llvm/llvm-project/issues/131642.
2025-04-01 10:57:32 -04:00
Tejas Vipin
8078665bca [libc][math][c23] Add hypotf16 function (#131991)
Implement hypot for Float16 along with tests.
2025-03-31 10:06:28 -04:00
Tejas Vipin
d22e35be17 [libc][math][c23] Add asinhf16 function (#131351)
Implement asinh for Float16 along with tests. Closes #131001
2025-03-29 13:54:52 +01:00
Mohamed Emad
4840895467 [libc] implement memalignment (#132493)
This patch adds the `memalignment` function to LLVM-libc, following its
description in [WG14 N3220,
§7.24.2.1](https://www.open-std.org/jtc1/sc22/wg14/www/docs/n3220.pdf#page=387).

- [x] Add the implementation of `memalignment` in
[`/src/stdlib`](https://github.com/llvm/llvm-project/tree/main/libc/src/stdlib)
- [x] Add tests for `memalignment` in
[`/test/src/stdlib`](https://github.com/llvm/llvm-project/tree/main/libc/test/src/stdlib)
- [x] Add `memalignment` to
[`entrypoints.txt`](https://github.com/llvm/llvm-project/blob/main/libc/config/linux/x86_64/entrypoints.txt)
for at least x86_64 and whatever you're building on
- [x] Add `memalignment` to
[`include/stdlib.yaml`](https://github.com/llvm/llvm-project/blob/main/libc/include/stdlib.yaml)




Closes #132300

---------

Co-authored-by: Joseph Huber <huberjn@outlook.com>
2025-03-28 16:07:57 -07:00
Qinkun Bao
91d2ecf0d5 [NFC] Fix some typos in libc and mlir comments (#133374) 2025-03-28 15:52:37 -04:00
Jesse D
f76254d9b2 [libc] Fix implicit conversion error in DyadicFloat::as_mantissa_type(). (#133383)
This is the same fix that was recently applied to
as_mantissa_type_rounded(), but for as_mantissa_type().
2025-03-28 08:31:17 -04:00
Michael Jones
3a5d77608b [libc] Update headers on aarch64 (#133180)
The entrypoints for aarch64 are mostly up to date, but the headers are
not. This patch fixes that, and also makes explicit the dependency from
OSUtils/linux on sys/syscalls.h
2025-03-27 09:05:24 -07:00
Schrodinger ZHU Yifan
7b130f4aa8 [libc][CPP] correct cmpxchg failure ordering inference (#133127)
See https://en.cppreference.com/w/cpp/atomic/atomic/compare_exchange.
The failure order should be inferred from the success order if it is not
explicitly specified.
2025-03-26 15:29:08 -04:00
Amy Huang
69274839c3 Change check for "shift > Type::BITS" to avoid offset greater than total bits. (#133016)
nexttowardf16_test is resulting in calling shift and for some reason not
meeting the invariant where offset is less than bits. Change the if
statement to directly check if shift - 1 meets the conditions.
2025-03-25 23:41:35 -04:00
Michael Jones
c995db3e34 [libc] Add exceptional values for sinhf16/coshf16 (#133002)
The rounding of the result when using an FMA instruction for hyperbolic
sin/cos on float16 was off by 1 bit for a few cases. This patch adds
extra exceptional cases to handle these.
2025-03-25 16:29:48 -07:00
Joseph Huber
9243f99d17 [libc] Add support for C++20 'atomic_ref' type (#132302)
Summary:
C++20 introduced an atomic reference type, which more easily wraps
around the standard way of dealing with atomics. Instead of a dedicated
type, it allows you to treat an existing allocation as atomic.

This has no users yet, but I'm hoping to use it when I start finalizing
my GPU allocation interface, as it will need to handle atomic values
in-place that can't be done with placement new. Hopefully this is small
enough that we can just keep it in-tree until it's needed, but I'll
accept holding it here until it has a user.

I added one extension to allow implicit conversion and CTAD.
2025-03-25 13:28:49 -05:00
Paul Kirth
9fb7924966 [libc][time] Fix -Wshorten-64-to-32 warning (#132947)
This breaks builds of libc with top of tree clang under -Werror.
2025-03-25 08:53:08 -07:00
Leslie
ba46c37f9c [libc] change the return value type of mktime_internal to time_t (#132231)
This pr is to resovle #126948

---------

Co-authored-by: Joseph Huber <huberjn@outlook.com>
2025-03-25 09:09:15 -05:00
Paul Kirth
0aa4c35beb [libc][__support] Fix -Wimplicit-int-conversion warning (#132839)
Newer versions of clang now warn about these, so use explicit
conversion instead.
2025-03-24 16:47:07 -07:00
Harrison Hao
445837a363 [libc][math][c23] Add fmaf16 C23 math function. (#130757)
Implementation of fmaf16 function for 16-bit inputs.
2025-03-23 10:48:56 +08:00
Harrison Hao
ee3e17d67f [libc][math][c23] Add acoshf16 C23 math function. (#130588)
Implementation of acoshf16 function for 16-bit inputs.
2025-03-22 22:08:34 -04:00
lntue
a17b03f0e4 [libc][math] Implement fast pass for double precision atan function. (#132333)
Implement fast pass for double precision `atan` using range reduction
modulo 1/64 and degree-9 Taylor polynomial.
Relative error is bounded by 2^-66.
2025-03-21 14:12:06 -04:00
Joseph Huber
bd6df0fe21 Reapply "[LLVM] Make the GPU loader utilities an LLVM tool (#132096)" (#132277)
Summary:
There were a few issues with the first one, leading to some errors and
warnings. Most importantly, this was building on MSVC which isn't
supported.
2025-03-21 11:05:32 -05:00
Joseph Huber
d4ce57cb98 [libc] Fix unresolved stdio symbols (#132403)
Summary:
We have 'generic' implementations for some functions stdio functions.
The current logic mandates that all generic functions are implemented by
the target. This obviously isn't true and this caused the GPU builds to
fail once baremtal added some extra ones.

This patch changes the logic to always include the generic sources only
if they aren't already defined. This can probably be cleaned up and
formalized later, since this pattern is copied in many places, but for
now this fixes the failing GPU build bots.
2025-03-21 10:06:57 -05:00
Krishna Pandey
902078350e [libc][math] Add missing parenthesis in sollya command (#132298)
This PR adds the missing opening parenthesis for sollya command comment
in `libc/src/math/generic/cbrtf.cpp#L28`.

Signed-off-by: krishna2803 <kpandey81930@gmail.com>
2025-03-20 21:26:09 -04:00
Petr Hosek
afae7c91e1 [libc] Support for scanf on baremetal (#131043)
This uses the templatized scanf Reader interface introduced in #131037.
2025-03-20 15:54:38 -07:00
Krishna Pandey
bda87e0a09 [libc][sched] Implement CPU_ZERO, CPU_ISSET, CPU_SET macros (#131524)
This PR implements the following macros for `sched.h`:
- `CPU_ZERO`
- `CPU_ISSET`
- `CPU_SET`

Fixes #124642

---------

Signed-off-by: krishna2803 <kpandey81930@gmail.com>
2025-03-19 13:44:41 -04:00
Iris
67a01131a8 [libc] Replace __builtin_popcountl with cpp::popcount in sched_getcpucount (#131952)
Closes #128781.
2025-03-19 10:35:50 -04:00
Connector Switch
af7c8c475a [libc] Implement search/lsearch (#131431)
ref:
- https://man7.org/linux/man-pages/man3/lsearch.3.html
- https://pubs.opengroup.org/onlinepubs/009696699/functions/lsearch.html
2025-03-19 08:40:59 +08:00
Roland McGrath
123c0040d4 [libc] Define (stub) dl_iterate_phdr (#131436)
This fleshes out the <link.h> a little more, including the
`struct dl_phdr_info` type and declaring the dl_iterate_phdr
function.  There is only a no-op implementation without tests, as
for the existing dlfcn functions.
2025-03-18 11:38:33 -07:00
Joseph Huber
04ab8a5fba [libc] Fix the GPU build after scanf rework
Summary:
Needs these to be included.
2025-03-18 12:09:30 -05:00
OverMighty
1bb8b656a9 [libc][math] Fix incorrect logic in fputil::generic::add_or_sub (#116129)
Fixes incorrect logic that went unnoticed until the function was tested
with output and input types that have the same underlying floating-point
format.
2025-03-18 15:04:18 +01:00
Petr Hosek
1fbfef9b8a [libc] Templatize the scanf Reader interface (#131037)
This allows specializing the implementation for different targets
without including unnecessary logic and is similar to #111559 which did
the same for printf Writer interface.
2025-03-17 23:51:24 -07:00
Tristan Ross
bb694998d2 [libc] init uefi (#131246)
Initial UEFI OS target support after the headers. This just defines
enough that stuff might try and compile. Test with:
```
$ cmake -S llvm -B build -G Ninja -DLLVM_RUNTIME_TARGETS=x86_64-unknown-uefi-llvm -DRUNTIMES_x86_64-unknown-uefi-llvm_LLVM_ENABLE_RUNTIMES=libc -DRUNTIMES_x86_64-unknown-uefi-llvm_LLVM_LIBC_FULL_BUILD=true -DCMAKE_C_COMPILER_WORKS=true -DCMAKE_CXX_COMPILER_WORKS=true -DLLVM_ENABLE_PROJECTS="clang;lld" -DCMAKE_BUILD_TYPE=Debug -DLLVM_ENABLE_LIBCXX=true -DLLVM_HOST_TRIPLE=aarch64-unknown-linux-gnu -DLLVM_DEFAULT_TARGET_TRIPLE=x86_64-unknown-uefi-llvm -DCMAKE_INSTALL_LIBDIR=build/target/lib
$ ninja -C build
```
2025-03-14 20:15:24 -07:00
lntue
d0a0de50f7 [libc] Fix implicit conversion warnings in tests. (#131362) 2025-03-14 14:24:11 -04:00
Joseph Huber
fdb4b89bc0 [libc] Fix memmove macros for unreocognized targets 2025-03-14 10:25:42 -05:00
Joseph Huber
e8117026a9 [libc] Default to byte_per_byte instead of erroring (#131340)
Summary:
Right now a lot of the memory functions error if we don't have specific
handling for them. This is weird because we have a generic
implementation that should just be used whenever someone hasn't written
a more optimized version. This allows us to use the `libc` headers with
more architectures from the `shared/` directory without worrying about
it breaking.
2025-03-14 10:10:53 -05:00
Simon Tatham
80079c9c2f [libc] Fix new warning in DyadicFloat::as_mantissa_type_rounded (#131148)
The affected line of code converts a float's exponent from `int` to
`size_t`, negating it in the process. Following clang commit
773e88f9d6, this provokes a warning, presumably because the
conversion goes wrong if `size_t` is wider than `int` and the input
value is `INT_MIN`: negating it within the `int` type is undefined
behavior, with the likely (though not guaranteed) effect of leaving it
still at `INT_MIN` and then sign-extending that on promotion to
`size_t`.

This fix adds a cast so that the promotion to `size_t` happens _before_
the negation, so that the negative input value will _always_ be
sign-extended, and then the negation will make it positive again.

(I don't believe this case will actually come up. `DyadicFloat` is a
helper system used in processing ordinary float formats, so nothing is
expected to generate an exponent with even a 16-bit absolute value, let
alone 31. But it's as easy to fix it to be robust as it is to just
suppress the warning!)
2025-03-14 11:57:25 +00:00
Joseph Huber
8437b7f558 [libc] Make RPC server handling header only (#131205)
Summary:
This patch moves the RPC server handling to be a header only utility
stored in the `shared/` directory. This is intended to be shared within
LLVM for the loaders and `offload/` handling.

Generally, this makes it easier to share code without weird
cross-project binaries being plucked out of the build system. It also
allows us to soon move the loader interface out of the `libc` project so
that we don't need to bootstrap those and can build them in LLVM.
2025-03-13 19:23:21 -05:00
PiJoules
662bd4ca47 [stdio][baremetal] Fix templating for print functions (#131232)
This addresses build errors from https://github.com/llvm/llvm-project/pull/111559.
2025-03-13 15:30:45 -07:00
Simon Tatham
6fea340023 [libc] Fix non-templated uses of printf_core::Writer (#131149)
Commit 598e882ee8 turned `Writer` into a template, and updated most
of the call sites that use it. But not all. The alternative FP printf
system in `float_dec_converter_limited.h` wasn't updated, and neither
was `baremetal/printf.cpp` or `baremetal/vprintf.cpp`.

This patch updates `float_dec_converter_limited.h` in the same way that
the previous commit updated `float_dec_converter.h`: propagate the
templatedness through everything in the header, so that anything using a
`Writer` at all has a `write_mode` template parameter to pass on to it.

`printf.cpp` and `vprintf.cpp` are updated in the same way that the
previous commit updated `printf_core/vfprintf_internal.h`: the
`WriteBuffer` has parameter `WriteMode::FLUSH_TO_STREAM`, and `Writer`
picks it up implicitly from that.
2025-03-13 13:37:18 -05:00
Tejas Vipin
b910610eea [libc][math] Fix Sollya command (#131091)
Fix a minor syntax error in the Sollya command in the comments of
asinhf.
2025-03-13 07:36:06 -04:00
Connector Switch
f291ec692e [libc] Use proxy header in the locale implementation. (#130982)
Address review comments in
https://github.com/llvm/llvm-project/pull/130621#pullrequestreview-2671843932.

Some unused headers are also removed.
2025-03-13 11:35:41 +08:00
Joseph Huber
598e882ee8 [libc] Template the writing mode for the writer class (#111559)
Summary:
Currently we dispatch the writing mode off of a runtime enum passed in
by the constructor. This causes very unfortunate codegen for the GPU
targets where we get worst-case codegen because of the unused function
pointer for `sprintf`. Instead, this patch moves all of this to a
template so it can be masked out. This results in no dynamic stack and
uses 60 VGPRs instead of 117. It also compiles about 5x as fast.
2025-03-12 13:51:44 -05:00
Connector Switch
ab22f652a4 [libc] implement strings/str{n}casecmp_l (#130407)
ref:
https://pubs.opengroup.org/onlinepubs/9799919799/functions/strcasecmp_l.html

This patch introduces the `strcasecmp_l` function. At present, the
locale parameter is ignored, making it a stub implementation. This is
consistent with how other locale-related functions, such as `islower_l`,
are treated in our codebase as well as in
[musl](https://github.com/bminor/musl/blob/master/src/string/strcasecmp.c)
and
[bionic](https://cs.android.com/android/platform/superproject/main/+/main:bionic/libc/bionic/strings_l.cpp).

---------

Co-authored-by: Michael Jones <michaelrj@google.com>
2025-03-12 10:49:04 +08:00