1739 Commits

Author SHA1 Message Date
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
Joseph Huber
6aed161cb9
[libc][NFC] Remove incorrect file level dependency in CMake (#132937)
Summary:
CMake doesn't support this type of dependency so it keeps emitting a
warning. Just remove it, because it's not doing anything currently
anyway. If we really wanted this to work you'd need to add a custom
target that has a dependency on the output of a custom command, but that
would only be worhwhile if we ever expected this file to change.
2025-03-25 09:33:25 -05:00
Alexey Samsonov
5a668bdb98
[libc] Migrate sys/epoll tests to use ErrnoCheckingTest. (#132823)
This is similar to PR #132107 but for tests for sys/epoll.h functions.

ErrnoCheckingTest ensures that errno is properly reset at the beginning
of the test case, and is validated at the end of it, so that the manual
code such as the one proposed in PR #131650 would not be necessary.
2025-03-24 13:54:46 -07:00
Roland McGrath
6f4ddefe5b
[libc] Remove extraneous ASSERT_ERRNO_* macro definitions (#132812)
These are no longer meant to be defined in each
framework-specific header, but ZxTest.h wasn't updated.
2025-03-24 12:25:43 -07:00
Tejas Vipin
1f96788701
[libc][math] Fix range and comments in exhaustive hypotf_test (#131769)
`((23U + 127U) << 23) + 1` evaluates to `(2^23)+1` as opposed to `2^24`,
so should instead be `(24U + 127U) << 23`. Additionally, range for both
inputs is inclusive of STOP. The comments have been updated reflecting
these changes.
2025-03-24 12:32:11 -04: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
Matheus Izvekov
4527085319
[clang] diagnose invalid member pointer class on instantiation (#132516) 2025-03-22 02:05:40 -03:00
lntue
17b5d6b242
[libc] Temporarily disable the type trait test affected by issue 132494. (#132501)
This test causes clang to crash due to #132494.
2025-03-21 21:57:37 -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
Alexey Samsonov
4c36c425ec
[libc] Migrate sys/socket tests to use ErrnoCheckingTest. (#132107)
Also use ErrnoSetterMatcher to verify the function return values and
verify/clear out errno values. Fix the bug in ErrnoSetterMatcher error
reporting machinery to properly convert errno values into errno names to
make error messages easier to debug.
2025-03-19 15:33:20 -07:00
Alexey Samsonov
2f7ad891a7
[libc] Migrate unistd tests to use ErrnoCheckingTest. (#132067)
Use ErrnoCheckingTest harness added in
d039af33096c0a83b03475a240d5e281e2271c44 for all unistd tests that
verify errno values. Stop explicitly setting it to zero in test code, as
harness does it.

It also verifies that the errno is zero at the end of each test case, so
update the ASSERT_ERRNO_EQ and ASSERT_ERRNO_FAILURE macro to clear out
its value after the verification (similar to how ErrnoSetterMatcher
does).

Update the CMake and Bazel rules for those tests. In the latter case,
remove commented out tests that are currently unsupported in Bazel,
since they get stale quickly.
2025-03-19 13:29:52 -07:00
Connector Switch
2d09028771
[libc] Fix size_t and unsigned long mismatch on riscv32 in test. (#132028)
Fix regression:
https://github.com/llvm/llvm-project/pull/131431#issuecomment-2735029529
2025-03-19 22:30:51 +08: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
Alexey Samsonov
d039af3309
[libc] Create ErrnoCheckingTest harness to simplify errno tests. (#131703)
See the discussion in PR
https://github.com/llvm/llvm-project/pull/131650 on why we need to clear
the errno at the beginning of some tests, and outlining the various solutions.

Introduce ErrnoCheckingTest base class and use it for unlink_test as an example.
2025-03-18 12:30:53 -07: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
lntue
d0a0de50f7
[libc] Fix implicit conversion warnings in tests. (#131362) 2025-03-14 14:24:11 -04: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
Connector Switch
426caf1182
[libc] add locale proxy header (#130621)
Address review comments in #130407.

This patch is already covered by existing locale test cases.
2025-03-11 08:46:29 +08:00
Vinay Deshmukh
257e483715
[libc] Add -Wno-sign-conversion & re-attempt -Wconversion (#129811)
Relates to
https://github.com/llvm/llvm-project/issues/119281#issuecomment-2699470459
2025-03-10 11:57:09 -04:00
Ishaan Verma
735afc8ceb
[libc] Added type-generic macros for fixed-point functions (#129371)
Adds macros `absfx`, `countlsfx` and `roundfx` .
ref:  #129111
2025-03-09 00:11:34 -05:00
Connector Switch
a5588b6d20
[libc] implement strings/ffs (#129892)
This patch adds the `strings/ffs` function.
ref: https://pubs.opengroup.org/onlinepubs/9799919799/functions/ffs.html
Closes: #122054.
2025-03-08 09:31:18 +08:00
Alexey Samsonov
d90423e310
[libc][bazel] Minor cleanup to remove unused dependencies. (#130348)
* strcpy doesn't need to depend on memcpy
* qsort_test_helper has been generalized and doesn't need to depend on
qsort.

This is a small cleanup to unblock the work outlined in #130327.
2025-03-07 13:31:18 -08:00
Connector Switch
dd9a2f0677
[libc] first_trailing_one(0) should be 0. (#130155)
Fix this minor bug. See more context at #129892.
2025-03-08 00:11:43 +08:00
Augie Fackler
da61b0ddc5 Revert "[libc] Enable -Wconversion for tests. (#127523)"
This reverts commit 1e6e845d49a336e9da7ca6c576ec45c0b419b5f6 because it
changed the 1st parameter of adjust() to be unsigned, but libc itself
calls adjust() with a negative argument in align_backward() in
op_generic.h.
2025-03-05 16:42:40 -05:00
Vinay Deshmukh
1e6e845d49
[libc] Enable -Wconversion for tests. (#127523)
Relates to: #119281
2025-03-04 10:24:35 -05:00
Michael Jones
64ae0a102f
[libc] implement l64a (#129099)
Adds l64a, which generates the base 64 string expected by a64l.
2025-02-27 12:48:39 -08:00
lntue
4fd762caa6
[libc] Fix sqrtf128 smoke test for riscv32. (#129094) 2025-02-27 13:55:22 -05:00
Krishna Pandey
f6bfa33cdb
[libc][stdfix] Implement fixed point bitsfx functions in llvm libc (#128413)
Fixes #113359

---------

Signed-off-by: krishna2803 <kpandey81930@gmail.com>
2025-02-27 13:05:27 -05:00
Michael Jones
8beec9fc48
[libc] implement a64l (#128758)
Implement the posix function a64l.
Standard:
https://pubs.opengroup.org/onlinepubs/9799919799/functions/a64l.html
2025-02-25 13:57:13 -08:00
wldfngrs
3e284554a8
[libc][math][c23] Add acosf16() function (#127731)
- Implementation of acosf16 (inverse cosine) function for 16-bit inputs.
- Exhaustive tests across the 16-bit input range.
2025-02-23 19:46:46 +01:00
lntue
8ea6b735a6
[libc] Fix alignment issue for HermeticTestUtils.cpp. (#128426)
Full build precommit bots were failing due to mis-alignment of atomics
in hermetic tests. This PR enforces the alignment for the bump allocator
of hermetic test framework.

Fixes https://github.com/llvm/llvm-project/issues/128185.
2025-02-23 13:41:51 -05:00
Joseph Huber
559cb6ba63 [libc] Fix scanf dependencies on the GPU build
Summary:
The GPU build doesn't have `file` as a struct but does use these
targets. We need to add an escape for this.
2025-02-22 13:14:20 -06:00
Michael Jones
3fad640f06
[libc] Skip scanf internals when no file available (#128097)
A temporary fix based on discussions in #128079

Currently, baremetal targets are failing to build because the scanf
internals require FILE* (either from the system's libc or our libc).
Normally we'd just turn off the broken entrypoint, but since the scanf
internals are built separately that doesn't work. This patch adds extra
conditions to building those internals, which we can hopefully remove
once we have a proper way to build scanf for embedded.
2025-02-21 12:26:23 -08:00
Michael Jones
5981335d75
[libc] Fix scanf cmake for targets without FILE (#128056)
Another followup fix to #121215

The new cmake wouldn't define the readerat all if the target wasn't GPU
or didn't have a definition of FILE. This patch rewrites the cmake to be
more general.

As a followup, I'd like to make `use_system_file` consistent between
/test and /src. Currently in /src it includes the `COMPILE_OPTIONS` and
in /test it does not.
2025-02-20 13:47:45 -08:00
Joseph Huber
6cc7ca084a
[Clang] Fix cross-lane scan when given divergent lanes (#127703)
Summary:
The scan operation implemented here only works if there are contiguous
ones in the executation mask that can be used to propagate the result.
There are two solutions to this, one is to enter 'whole-wave-mode' and
forcibly turn them back on, or to do this serially. This implementation
does the latter because it's more portable, but checks to see if the
parallel fast-path is applicable.

Needs to be backported for correct behavior and because it fixes a
failing libc test.
2025-02-19 16:46:59 -06:00
Zaky Hermawan
ef49760fa9
[libc][POSIX][unistd] implement getsid (#127341)
Fixes https://github.com/llvm/llvm-project/issues/126603

---------

Signed-off-by: ZakyHermawan <zaky.hermawan9615@gmail.com>
2025-02-19 14:26:18 -08:00
Joseph Huber
9a584b07d7
[Clang] Add handlers for 'match_any' and 'match_all' to gpuintrin.h (#127504)
Summary:
These helpers are very useful but currently absent. They allow the user
to get a bitmask representing the matches within the warp. I have made
an executive decision to drop the `predicate` return from `match_all`
because it's easily testable with `match_all() == __activemask()`.
2025-02-17 14:06:24 -06:00
Michael Jones
398f865499
[libc] Implement strftime (#122556)
Implements the posix-specified strftime conversions for the default
locale, along with comprehensive unit tests. This reuses a lot of design
from printf, as well as the printf writer.

Roughly based on #111305, but with major rewrites.
2025-02-14 15:56:55 -08:00
Roland McGrath
e477e568e6
[libc] Make test macros suppress the -Wdangling-else warnings (#127149)
Use the trick from gtest to allow `ASSERT_...` and `EXPECT_...`
macros to be used in braceless `if` without producing warnings
about the nested `if`-`else` that results.
2025-02-13 20:05:24 -08:00
lntue
5ee9787730
[libc][math] Improve the performance of sqrtf128. (#122578)
Use a combination of polynomial approximation and Newton-Raphson
iterations in 64-bit and 128-bit integers to improve the performance of
sqrtf128. The correct rounding is provided by squaring the result and
comparing it with the argument.

Performance improvement using the newly added perf test:
- My function = the improved implementation from this PR
- Other function = current implementation using
`libc/src/__support/FPUtil/generic/sqrt.h`
```
 Performance tests with inputs in denormal range:
-- My function --
     Total time      : 1260765265 ns 
     Average runtime : 125.951 ns/op 
     Ops per second  : 7939623 op/s 
-- Other function --
     Total time      : 7160726518 ns 
     Average runtime : 715.357 ns/op 
     Ops per second  : 1397902 op/s 
-- Average runtime ratio --
     Mine / Other's  : 0.176067 

 Performance tests with inputs in normal range:
-- My function --
     Total time      : 373003808 ns 
     Average runtime : 37.2631 ns/op 
     Ops per second  : 26836189 op/s 
-- Other function --
     Total time      : 7353398916 ns 
     Average runtime : 734.605 ns/op 
     Ops per second  : 1361275 op/s 
-- Average runtime ratio --
     Mine / Other's  : 0.0507254 
```

---------

Co-authored-by: Alexei Sibidanov <sibid@uvic.ca>
2025-02-13 17:49:52 -05:00
Nick Desaulniers
3e02069afe
[libc][pthread] fix -Wmissing-field-initializers (#126314)
Fixes:


llvm-project/libc/test/integration/src/pthread/pthread_rwlock_test.cpp:59:29:
    warning: missing field '__preference' initializer
    [-Wmissing-field-initializers]
       59 |   pthread_rwlock_t rwlock = PTHREAD_RWLOCK_INITIALIZER;
          |                             ^

Also, add a test that demonstrates the same issue for
PTHREAD_MUTEX_INITIALIZER, and fix that, too.

PTHREAD_ONCE_INIT does not have this issue and does have test coverage.
2025-02-12 14:28:29 -08:00
Krishna Pandey
1f51038036
[libc][stdfix] Implement countlsfx functions in libc. (#126597)
fixes #113357

---------

Signed-off-by: krishna2803 <kpandey81930@gmail.com>
2025-02-12 14:12:44 -05:00
Nick Desaulniers
a3e2075e50
[libc][bit_test] fix -Wimplicit-int-conversion (#126317)
Fixes:

    llvm-project/libc/src/__support/CPP/bit.h:235:28: error: implicit
    conversion loses integer precision: 'int' to
'cpp::enable_if_t<cpp::is_unsigned_v<unsigned short>, unsigned short>'
(aka
    'unsigned short') [-Werror,-Wimplicit-int-conversion]
      235 |   return (value << rotate) | (value >> (N - rotate));
          |   ~~~~~~ ~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
    llvm-project/libc/src/__support/CPP/bit.h:247:28: error: implicit
    conversion loses integer precision: 'int' to
'cpp::enable_if_t<cpp::is_unsigned_v<unsigned short>, unsigned short>'
(aka
    'unsigned short') [-Werror,-Wimplicit-int-conversion]
      247 |   return (value >> rotate) | (value << (N - rotate));
          |   ~~~~~~ ~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
    llvm-project/libc/test/src/__support/CPP/bit_test.cpp:45:36: error:
implicit conversion loses integer precision: 'int' to 'unsigned char'
    [-Werror,-Wimplicit-int-conversion]
       45 |     EXPECT_FALSE(has_single_bit<T>(two_bits_value));
          |                  ~~~~~~~~~~~~~~    ^~~~~~~~~~~~~~

Via the libc-cpp-utils-tests ninja target.
2025-02-12 08:59:02 -08:00
Nick Desaulniers
b8ba266820
[libc][test][stdbit] fix -Wimplicit-int-conversion (#126616)
When cross compiling the libc-stdbit-tests, the existing tests trigger numerous
instances of -Wimplicit-int-conversion. The truncation of these implicit
promotions is intentional.
2025-02-11 09:31:01 -08:00
wldfngrs
7ee56b9afc
[libc][math][c23] Add asinf16() function (#124212)
Co-authored-by: OverMighty <its.overmighty@gmail.com>
2025-02-10 12:38:55 +01:00
Petr Hosek
6dbe542290
[libc] Use ${libc_opt_high_flag} instead of -O3 (#123233)
This is preferable since `${libc_opt_high_flag}` will be set correctly
for the compiler used.
2025-02-07 13:36:06 -08:00
Joseph Huber
7623d91784 Revert "[libc][stdfix] Fix buildbot failure because of a typo. (#126291)"
This reverts commit bada9220b87e73c0f4a498b82f883e17eda928d1.

Revert "[libc][stdfix] Implement fixed point `countlsfx` functions in llvm-libc (#125356)"

This reverts commit f2a1103b323492160d7d27a1575fbda709b49036.
2025-02-07 14:34:40 -06:00