summaryrefslogtreecommitdiff
path: root/libc/src
AgeCommit message (Collapse)Author
2025-11-20[libc][math] Add float-only implementation for atanf. (#167004)lntue
Algorithm: ``` 1) atan(x) = sign(x) * atan(|x|) 2) If |x| > 1 + 1/32, atan(|x|) = pi/2 - atan(1/|x|) 3) For 1/16 < |x| < 1 + 1/32, we find k such that: | |x| - k/16 | <= 1/32. Let y = |x| - k/16, then using the angle summation formula, we have: atan(|x|) = atan(k/16) + atan( (|x| - k/16) / (1 + |x| * k/16) ) = atan(k/16) + atan( y / (1 + (y + k/16) * k/16 ) = atan(k/16) + atan( y / ((1 + k^2/256) + y * k/16) ) 4) Let u = y / (1 + k^2/256), then we can rewritten the above as: atan(|x|) = atan(k/16) + atan( u / (1 + u * k/16) ) ~ atan(k/16) + (u - k/16 * u^2 + (k^2/256 - 1/3) * u^3 + + (k/16 - (k/16)^3) * u^4) + O(u^5) ``` With all the computations are done in single precision (float), the total of approximation errors and rounding errors is bounded by 4 ULPs.
2025-11-18[libc] Implement pkey_alloc/free/get/set/mprotect for x86_64 linux (#162362)Jackson Stogel
This patch provides definitions for `pkey_*` functions for linux x86_64. `pkey_alloc`, `pkey_free`, and `pkey_mprotect` are simple syscall wrappers. `pkey_set` and `pkey_get` modify architecture-specific registers. The logic for these live in architecture specific directories: * `libc/src/sys/mman/linux/x86_64/pkey_common.h` has a real implementation * `libc/src/sys/mman/linux/generic/pkey_common.h` contains stubs that just return `ENOSYS`.
2025-11-18[libc] implement inet_addr (#167708)Connector Switch
This patch adds the posix function `inet_addr`. Since most of the parsing logic is delegated to `inet_aton`, I have only included some basic smoke tests for testing purposes.
2025-11-17[libc] Move mbtowc, mbstowcs and inverse functions to stdlib.h (#168455)Alexey Samsonov
These functions should be declared in `stdlib.h`, not `wchar.h`, as confusing as it is. Move them to the proper header file and matching directories in src/ and test/ trees. This was discovered while testing libc++ build against llvm-libc, which re-declares functions like mbtowc in std-namespace in `<cstdlib>` header, and then uses those functions in its locale implementation.
2025-11-17[libc] Implement wcstod and wcstold. (#168020)Alexey Samsonov
These are simply implemented as specializations of strtofloatingpoint for double / long double and for wchar_t. The unit tests are copied from the strtod / strtold ones.
2025-11-14[libc] Allow user-defined LIBC_ASSERT macro. (#168087)lntue
By only defining it if LIBC_ASSERT macro is not defined. Fixes https://github.com/llvm/llvm-project/issues/162392
2025-11-14[libc] replace for loops with a call to memcpy in File (#165219)Shreeyash Pandey
Addresses `TODO`s in file.cpp by replacing data copies via for loops with calls to inline_memcpy. Signed-off-by: Shreeyash Pandey <shreeyash335@gmail.com>
2025-11-13[libc] Templatize strtofloatingpoint and implement wcstof. (#167755)Alexey Samsonov
This change follows the pattern of 315dfe5865962d8a3d60e21d1fffce5214fe54ef by making strtofloat also accept wchar_t* strings (in addition to regular char*). It uses overloads from wctype_utils or specialized functions to ensure comparison with literal characters (or literal strings) pick char or wchar_t variants based on the argument type. The wcstof implementation is added, with unit test cases copied from strtof test suite.
2025-11-13[libc][NFC] Fix warnings in RPC server codeJoseph Huber
2025-11-11[libc] Use function overloads to make string parsing code more generic. ↵Alexey Samsonov
(#167417) ctype_utils/wctype_utils were chaged in 120689e46679c6db37cd9e839ec0721e80a22d4f and e7f7973899f76773ae6e9a6b1e8c7e9f9cc5cb56, respectively to operate on char/wchar_t. Now we can switch to the overloaded names (e.g. have noth `isspace(char` and `isspace(wchar_t)`) to simplify the templatized strtointeger implementation from 315dfe5865962d8a3d60e21d1fffce5214fe54ef and make it easier to potentially add templatized strtofloat implementation.
2025-11-11[libc][POSIX] Add clock_settime() function for Linux (#161729)Anton Shepelev
Closes #161461 - This is my first time contributing to libc's POSIX, so for reference I used `clock_gettime` implementation for Linux. For convenience, here is the description of `clock_settime` function [behavior](https://www.man7.org/linux/man-pages/man3/clock_settime.3.html)
2025-11-11[libc] Implement fchown (#167286)Anshul Nigham
Implements fchown fixes: #166856
2025-11-11[libc] Refactor strftime internals to handle size_t return values (#166901)Marcell Leleszi
Now that https://github.com/llvm/llvm-project/pull/166517 has landed and [Writer](https://github.com/llvm/llvm-project/blob/main/libc/src/stdio/printf_core/writer.h#L130) has been refactored to track bytes written as size_t, strftime can be refactored as well to handle size_t return values. Can't think of a proper way to test this without creating a 2GB+ string, but existing tests cover most cases.
2025-11-10[libc] add an SVE implementation of strlen (#167259)Schrodinger ZHU Yifan
This PR creates an SVE-based implementation for strlen by translating from the AOR code in tree. Microbenchmark shows improvements against NEON when N>=64. Although both implementations fall behind glibc by a large margin, this may be a good start point to explore SVE implementations. Together with the PR: 1. Added two more tests of strlen with special nul symbols. 2. Added strlen's fuzzer and fix a typo in previous heap fuzzer. ``` === strlen(16 bytes) === libc: 1.56115 ns/call, 9.54499 GiB/s neon: 1.59393 ns/call, 9.34867 GiB/s sve: 1.66097 ns/call, 8.97134 GiB/s === strlen(64 bytes) === libc: 2.06967 ns/call, 28.7991 GiB/s neon: 2.59914 ns/call, 22.9325 GiB/s sve: 2.58628 ns/call, 23.0465 GiB/s === strlen(256 bytes) === libc: 3.74165 ns/call, 63.7202 GiB/s neon: 8.98243 ns/call, 26.5428 GiB/s sve: 7.36426 ns/call, 32.3751 GiB/s === strlen(1024 bytes) === libc: 10.5327 ns/call, 90.5438 GiB/s neon: 34.363 ns/call, 27.7529 GiB/s sve: 26.9329 ns/call, 35.4092 GiB/s === strlen(4096 bytes) === libc: 37.7304 ns/call, 101.104 GiB/s neon: 145.911 ns/call, 26.144 GiB/s sve: 103.208 ns/call, 36.9612 GiB/s === strlen(1048576 bytes) === libc: 9623.4 ns/call, 101.478 GiB/s neon: 36138.2 ns/call, 27.023 GiB/s sve: 26605.6 ns/call, 36.7051 GiB/s ```
2025-11-10[libc] fwrite_unlocked: only return errno if an actual error occurred. (#167350)Sterling-Augustine
fwrite and friends don't modify errno if no error occurred. Therefore frite_unlocked's return value shouldn't be constructed from errno without checking if an error actually occurred. This fixes an error introduced by https://github.com/llvm/llvm-project/commit/9e2f73fe9052a4fbf382a06e30b2441c6d99fb7e
2025-11-10Reapply "[libc] Return errno from OFD failure paths in fcntl." (#166658) ↵Jackson Stogel
(#166846) The previous implementation in #166252 (rolled back in #166658) caused buildbot failures due to a bug in an added test. The modified `UseAfterClose` did not pass a `struct flock` value to `fcntl`: https://github.com/llvm/llvm-project/blob/2d5170594147b42a37698760d6e0194eec4f1390/libc/test/src/fcntl/fcntl_test.cpp#L175 Which ASAN caught and errored in the `fcntl` implementation when the unspecified argument was accessed: https://github.com/llvm/llvm-project/blob/c12cb2892c808af459eaa270b8738a2b375ecc9b/libc/src/__support/OSUtil/linux/fcntl.cpp#L59
2025-11-07[libc] add cpu feature flags for SVE/SVE2/MOPS (#166884)Schrodinger ZHU Yifan
Add in SVE/SVE2/MOPS features for aarch64 cpus. These features may be interesting for future memory/math routines. SVE/SVE2 are now being accepted in more implementations: ``` ❯ echo | clang-21 -dM -E - -march=native | grep -i ARM_FEAT #define __ARM_FEATURE_ATOMICS 1 #define __ARM_FEATURE_BF16 1 #define __ARM_FEATURE_BF16_SCALAR_ARITHMETIC 1 #define __ARM_FEATURE_BF16_VECTOR_ARITHMETIC 1 #define __ARM_FEATURE_BTI 1 #define __ARM_FEATURE_CLZ 1 #define __ARM_FEATURE_COMPLEX 1 #define __ARM_FEATURE_CRC32 1 #define __ARM_FEATURE_DIRECTED_ROUNDING 1 #define __ARM_FEATURE_DIV 1 #define __ARM_FEATURE_DOTPROD 1 #define __ARM_FEATURE_FMA 1 #define __ARM_FEATURE_FP16_SCALAR_ARITHMETIC 1 #define __ARM_FEATURE_FP16_VECTOR_ARITHMETIC 1 #define __ARM_FEATURE_FRINT 1 #define __ARM_FEATURE_IDIV 1 #define __ARM_FEATURE_JCVT 1 #define __ARM_FEATURE_LDREX 0xF #define __ARM_FEATURE_MATMUL_INT8 1 #define __ARM_FEATURE_NUMERIC_MAXMIN 1 #define __ARM_FEATURE_PAUTH 1 #define __ARM_FEATURE_QRDMX 1 #define __ARM_FEATURE_RCPC 1 #define __ARM_FEATURE_SVE 1 #define __ARM_FEATURE_SVE2 1 #define __ARM_FEATURE_SVE_BF16 1 #define __ARM_FEATURE_SVE_MATMUL_INT8 1 #define __ARM_FEATURE_SVE_VECTOR_OPERATORS 2 #define __ARM_FEATURE_UNALIGNED 1 ``` MOPS is another set of extension for string operations, but may not be generally available for now: ``` ❯ echo | clang-21 -dM -E - -march=armv9.2a+mops | grep -i MOPS #define __ARM_FEATURE_MOPS 1 ```
2025-11-06[libc] Add localtime_r to baremetal entrypoints (#166677)Prabhu Rajasekaran
2025-11-06[libc] Fix stale char_ptr for find_first_character_wide read (#166594)Sterling-Augustine
On exit from the loop, char_ptr had not been updated to match block_ptr, resulting in erroneous results. Moving all updates out of the loop fixes that. Adjust derefences to always be inside bounds checks.
2025-11-06Fix bazel build issue caused in #166517 (#166734)Karlo Basioli
2025-11-05Revert "[libc] Return errno from OFD failure paths in fcntl." (#166658)Jackson Stogel
Reverts llvm/llvm-project#166252 Causing buildbot failures on `libc-x86_64-debian-dbg-asan`.
2025-11-05[libc] Return errno from OFD failure paths in fcntl. (#166252)Jackson Stogel
This patch also configures fcntl lock tests to run with F_OFD_* command variants, as all existing lock tests do not exercise process-associated- or OFD-specific functionality.
2025-11-05[libc] Allow openat and creat to return fd 0. (#166466)Jackson Stogel
Previously, if the `open` or `openat` syscalls returned 0 as a (valid) file descriptor, the `creat` and `openat` wrappers would erroneously return -1.
2025-11-05[libc] Add printf error handling (with fixes #2) (#166517)Marcell Leleszi
https://github.com/llvm/llvm-project/issues/159474 Another try of trying to land https://github.com/llvm/llvm-project/pull/166382 - Fix some leftover tests checking for specific errnos - Guard errno checking tests to not run on the GPU @michaelrj-google
2025-11-05[libc] Migrate wctype_utils to use wchar_t where applicable. (#166234)Alexey Samsonov
This is a counterpart of https://github.com/llvm/llvm-project/pull/166225 but for wctype_utils (which are not yet widely used). For now, I'm just changing the types from wint_t to wchar_t to match the regular ctype_utils change. The next change may rename most of the functions to match the name of ctype_utils variants, so that we could be calling them from the templated code operating on "const char*" and "const wchar_t*" strings, and the right function signature would be picked up.
2025-11-05[libc] Migrate ctype_utils to use char instead of int where applicable. ↵Alexey Samsonov
(#166225) Functions like isalpha / tolower can operate on chars internally. This allows us to get rid of unnecessary casts and open a way to creating wchar_t overloads with the same names (e.g. for isalpha), that would simplify templated code for conversion functions (see 315dfe5865962d8a3d60e21d1fffce5214fe54ef). Add the int->char converstion to public entrypoints implementation instead. We also need to introduce bounds check on the input argument values - these functions' behavior is unspecified if the argument is neither EOF nor fits in "unsigned char" range, but the tests we've had verified that they always return false for small negative values. To preserve this behavior, cover it explicitly.
2025-11-05[libc][math] Refactor exp2m1f16 implementation to header-only in ↵Muhammad Bassiouni
src/__support/math folder. (#162019) Part of #147386 in preparation for: https://discourse.llvm.org/t/rfc-make-clang-builtin-math-functions-constexpr-with-llvm-libc-to-support-c-23-constexpr-math-functions/86450
2025-11-04Revert commit d8e5698 and 15b19c7 (#166498)Kewen Meng
2025-11-04[libc] Fix printf long double bugs (#166474)Michael Jones
Found in testing against abseil. Two bugs were found: 1) SHIFT_AMOUNT in float_converter<long double> would sometimes be negative causing an underflow when passed as the amount to left shift by for BigInt. 2) is_lowest_block had an off-by-one because it was adding 1 to the block index. Both are fixed and there are new tests to catch regressions.
2025-11-04[libc] Add printf error handling (with fixes) (#166382)Marcell Leleszi
https://github.com/llvm/llvm-project/issues/159474 Resubmitting https://github.com/llvm/llvm-project/pull/162876 with fixes as it broke some buildbots: - Fix comparisons of integer expressions of different signedness - Not check for specific errnos in tests, as they might not be available on all platforms
2025-11-04[libc] Add chown and getgid implementations (#166434)Shubh Pachchigar
Implements chown and getgid per the POSIX specification and adds corresponding unit tests. getgid is added as it is required by the chown unit tests. This PR will address #165785 Co-authored-by: shubh@DOE <shubhp@mbm3a24.local>
2025-11-03Revert "[libc] Add printf error handling" (#166232)Kewen Meng
2025-11-03[libc] Add printf error handling (#162876)Marcell Leleszi
[#159474](https://github.com/llvm/llvm-project/issues/159474) - All printf variants set errno and consistently return -1 on error, instead of returning various predefined error codes - Return value overflow handling is added
2025-10-31[libc] Templatize strtointeger implementation. (#165884)Alexey Samsonov
* Removes the copy-pasta implementation of wcstointeger, and migrate the wcsto* family of functions to use a template version of strtointeger. * Fixes the out-of-bound read in the original implementation(s) when the entire input string consists of whitespaces (then the sign check can access OOB memory) The code is currently slightly peppered with "if constexpr" statements to distinguish between char and wchar_t. We can probably simplify it in subsequent changes by: * using overrides, so that internal::isalnum() is overriden for both char and wchar_t (since C++ luckily allows us to reuse names). * this wouldn't help for direct comparison with literals - for this as a somewhat ugly workaround like is_char_literal(c, '0', L'0')
2025-10-30[libc] Remove optimization flags on entrypoints (#165782)Michael Jones
Optimization flags are now handled through a common flag. These are no longer necessary. Fixes #112409
2025-10-30[libc] Fix off by one error in strftime (#165711)Marcell Leleszi
This patch fixes a bug in strftime's return value when the formatted output exactly fills the buffer, not including the null terminator. The previous check failed to account for the null terminator in this case, incorrectly returning the written count instead of 0.
2025-10-27Move LIBC_CONF_STRING_UNSAFE_WIDE_READ to top-level libc-configuration (#165046)Sterling-Augustine
This options sets a compile option when building sources inside the string directory, and this option affects string_utils.h. But string_utils.h is #included from more places than just the string directory (such as from __support/CPP/string.h), leading to both narrow-reads in those cases, but more seriously, ODR violations when the two different string_length implementations are included int he same program. Having this option at the top level avoids this problem.
2025-10-26[libc] fix architecture guarding for 32bit sigsetjmp (#164923)Shreeyash Pandey
Fixes: https://github.com/llvm/llvm-project/issues/164653 Signed-off-by: Shreeyash Pandey <shreeyash335@gmail.com>
2025-10-24[libc] add missing headers in stdfix (#162078)Shreeyash Pandey
Fixes https://github.com/llvm/llvm-project/issues/129361 @michaelrj-google @PiJoules --------- Signed-off-by: Shreeyash Pandey <shreeyash335@gmail.com> Co-authored-by: Michael Jones <michaelrj@google.com>
2025-10-22[libc] Fix a couple issues in <wchar.h> header (#164666)Alexey Samsonov
* Add FILE type declaration, as it should be presented in `<wchar.h>`, as well as in `<stdio.h>` * Fix argument type in `wcsrtombs` / `wcsnrtombs` function - it should be restrict pointer to `mbstate_t`. Add restrict qualifier to internal implementation as well. This brings us closer to being able to build libcxx with wide-character support against llvm-libc headers.
2025-10-21[libc][math] Add tolerance to math tests so that they still work when ↵lntue
accurate path is skipped. (#164522)
2025-10-21[libc] Stub out message catalog functions from <nl_types.h> (#164360)Alexey Samsonov
Create a POSIX `<nl_types.h>` header with `catopen`, `catclose`, and `catgets` function declarations. Provide the stub/placeholder implementations which always return error. This is consistent with the way locales are currently (un-)implemented in llvm-libc. Notably, providing `<nl_types.h>` fixes the last remaining issue with building libc++ against llvm-libc (on certain configuration of x86_64 Linux) after disabling threads and wide-characters in libc++.
2025-10-21[libc][math] Add float-only implementation for sinf / cosf. (#161680)lntue
Based on the double precision's sin/cos fast path algorithm: Step 1: Perform range reduction `y = x mod pi/8` with target errors < 2^-54. This is because the worst case mod pi/8 for single precision is ~2^-31, so to have up to 1 ULP errors from the range reduction, the targeted errors should `be 2^(-31 - 23) = 2^-54`. Step 2: Polynomial approximation We use degree-5 and degree-4 polynomials to approximate sin and cos of the reduced angle respectively. Step 3: Combine the results using trig identities ```math \begin{align*} \sin(x) &= \sin(y) \cdot \cos(k \cdot \frac{\pi}{8}) + \cos(y) \cdot \sin(k \cdot \frac{\pi}{8}) \\ \cos(x) &= \cos(y) \cdot \cos(k \cdot \frac{\pi}{8}) - \sin(y) \cdot \sin(k \cdot \frac{\pi}{8}) \end{align*} ``` Overall errors: <= 3 ULPs for default rounding modes (tested exhaustively). Current limitation: large range reduction requires FMA instructions for binary32. This restriction will be removed in the followup PR. --------- Co-authored-by: Petr Hosek <phosek@google.com>
2025-10-21[libc] implement `inet_aton` (#162651)Connector Switch
This patch adds the implementation for `inet_aton` function. Since this function is not explicitly included in POSIX, I have marked it with `llvm_libc_ext`. It is widely available and commonly used, and can also be used to implement `inet_addr`, which is included in POSIX.
2025-10-21[libc] Capture 'POLY_COEFFS' by reference in asinpi_polyeval lambda. (#164404)Ren Hiyama
Closes #164403 GCC fails to compile libc currently. This PR fixes this by capturing 'POLY_COEFFS' by reference.
2025-10-17Revert "[libc] add basic lifetime annotations for support data structures" ↵Schrodinger ZHU Yifan
(#164012) Reverts llvm/llvm-project#145933 due to broken aarch64 buildbots.
2025-10-17[libc] add basic lifetime annotations for support data structures (#145933)Schrodinger ZHU Yifan
fix #145932 --------- Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
2025-10-17[libc][math] Refactor exp2m1f implementation to header-only in ↵Muhammad Bassiouni
src/__support/math folder. (#162017) Part of #147386 in preparation for: https://discourse.llvm.org/t/rfc-make-clang-builtin-math-functions-constexpr-with-llvm-libc-to-support-c-23-constexpr-math-functions/86450
2025-10-16[libc] Fix temporary types for fputil::split template. (#163813)lntue
Fix #163711.
2025-10-13Revert "[libc] Implement branchless head-tail comparison for bcmp" (#162859)Guillaume Chatelet
Reverts llvm/llvm-project#107540 This PR demonstrated improvements on micro-benchmarks but the gains did not seem to materialize in production. We are reverting this change for now to get more data. This PR might be reintegrated later once we're more confident in its effects.