Commit Graph

4304 Commits

Author SHA1 Message Date
Joseph Huber
10445acfa6 [libc] Efficiently implement 'realloc' for AMDGPU devices (#145960)
Summary:
Now that we have `malloc` we can implement `realloc` efficiently. This
uses the known chunk sizes to avoid unnecessary allocations. We just
return nullptr for NVPTX. I'd remove the list for the entrypoint but
then the libc++ code would stop working. When someone writes the NVPTX
support this will be trivial.
2025-06-30 08:39:40 -05:00
sribee8
f58caed1bc [libc] Changed CharacterConverter returns (#146130)
changed internal CharacterConverter returns to return errno macro when
necessary for consistency.

---------

Co-authored-by: Sriya Pratipati <sriyap@google.com>
2025-06-27 21:50:09 +00:00
Joseph Huber
d34214a85e [libc] Add and use 'cpp::launder' to guard placement new (#146123)
Summary:
In the GPU allocator we reinterpret cast from a void pointer. We know
that an actual object was constructed there according to the C++ object
model, but to make it fully standards compliant we need to 'launder' it
to forward that information to the compiler. Add this function and call
it as appropriate.
2025-06-27 14:34:33 -05:00
sribee8
ac7e391035 [libc] Implemented wcsnlen (#145610)
Implemented wcsnlen and tests for the function.

---------

Co-authored-by: Sriya Pratipati <sriyap@google.com>
2025-06-27 16:51:37 +00:00
Hans Wennborg
58b7d200aa [libc] Fix the risc-v half precison float feature check (#145894)
This is a follow-up to #145169, which would break compiles when
__riscv_zfhmin is not defined.
2025-06-26 16:32:21 +02:00
Guillaume Chatelet
7289b6789e [libc] Improve memcpy for ARM Cortex-M supporting unaligned accesses. (#144872)
This implementation has been compiled with the [pigweed toolchain](https://pigweed.dev/toolchain.html) and tested on:
 - Raspberry Pi Pico 2 with the following options\
`--target=armv8m.main-none-eabi`
`-march=armv8m.main+fp+dsp`
`-mcpu=cortex-m33` 
 - Raspberry Pi Pico with the following options\
`--target=armv6m-none-eabi`
`-march=armv6m`
`-mcpu=cortex-m0+` 

They both compile down to a little bit more than 200 bytes and are between 2 and 10 times faster than byte per byte copies.

For best performance the following options can be set in the `libc/config/baremetal/arm/config.json`
```
{
  "codegen": {
    "LIBC_CONF_KEEP_FRAME_POINTER": {
      "value": false
    }
  },
  "general": {
    "LIBC_ADD_NULL_CHECKS": {
      "value": false
    }
  }
}
```
2025-06-26 10:18:02 +02:00
Joseph Huber
46c8cc7e51 [libc] Enable all f16 math functions on the GPU (#145813)
Summary:
This patch adds all the new f16 math functions to the GPU build. These
should all pass except exp2m1f16 on AMDGPU for some reason. I'll
investigate that later.
2025-06-25 20:03:40 -05:00
sribee8
bd86fc9252 [libc] Error fixes for mbrtowc and wcrtomb (#145785)
Invalid mbstate_t should set errno to EINVAL.
Changed Error return for the internal functions and added tests for the
public functions.

Co-authored-by: Sriya Pratipati <sriyap@google.com>
2025-06-25 21:37:41 +00:00
Uzair Nawaz
2db0289abe [libc] Implemented wctomb (#145554)
Implemented wctomb by calling internal wcrtomb function
Added tests
2025-06-25 13:23:47 -07:00
Uzair Nawaz
1c56d101a2 [libc] Added isValidState to CharacterConverter class to ensure a provided mbstate is valid (#145564)
Added isValidState to CharacterConverter class to ensure a provided
mbstate is valid
2025-06-25 12:06:22 -07:00
sribee8
bc5e5c0114 [libc] wcpncpy implementation (#145430)
Implemented wcpncpy and tests.

---------

Co-authored-by: Sriya Pratipati <sriyap@google.com>
2025-06-23 23:35:28 +00:00
sribee8
10d46cf0d5 [libc] mbtowc implementation (#145405)
Implemented mbtowcs and tests for the function.

---------

Co-authored-by: Sriya Pratipati <sriyap@google.com>
2025-06-23 23:25:13 +00:00
Shubh Pachchigar
98e8ef2273 [libc] Fix broken links in libc (#145199)
This PR fixes broken links in all files describing libc usage modes.
Please let me know if there are any other places that need updating.

---------

Co-authored-by: shubhp@perlmutter <shubhp@perlmutter.com>
2025-06-23 15:51:43 -07:00
sribee8
b215c8e18f [libc] wcpcpy implementation (#144802)
Implemented wcpcpy and tests.

---------

Co-authored-by: Sriya Pratipati <sriyap@google.com>
2025-06-23 18:31:13 +00:00
Mikhail R. Gadelha
6c8c816b17 [libc] Fix feature check for riscv (#145169)
This PR fixes the feature detection for RISC-V floating-point support in
LLVM's libc implementation.

The `__riscv_flen` macro represents the floating-point register width in
bits (32, 64, or 128). Since Extension D is specifically documented as
implying F, we can use simple >= comparisons to detect them.

For half-precision support, the implementation checks for the Zfhmin
extension as RVA22 and RVA23 profiles only require Zfhmin rather than
the full Zfh extension. Zfh also implies Zfhmin, so checking for Zfhmin
should cover all cases.
2025-06-21 14:42:45 -03:00
Uzair Nawaz
a911543437 [libc] Implemented wcrtomb internal function and public libc function (#144596)
Implemented internal wcrtomb function using the CharacterConverter class
public libc function calls this internal function to perform the
conversion
2025-06-20 14:43:00 -07:00
sribee8
4c97a91dc0 [libc] Added closing quote (#145101)
Error message was missing a closing quote, added it.

Co-authored-by: Sriya Pratipati <sriyap@google.com>
2025-06-20 21:00:56 +00:00
sribee8
d078ce7c98 [libc] mbrtowc implementation (#144760)
implemented the internal and public mbrtowc as well as tests for the
public function.

---------

Co-authored-by: Sriya Pratipati <sriyap@google.com>
2025-06-20 20:00:59 +00:00
Uzair Nawaz
8d6e29d0d3 [libc] Reworked CharacterConverter isComplete into isFull and isEmpty (#144799)
isComplete previously meant different things for different conversion
directions.
Refactored bytes_processed to bytes_stored which now consistently
increments on every push and decrements on pop making both directions
more consistent with each other
2025-06-20 16:59:30 +00:00
lntue
d3a2931d8a [libc] Use raise_except_if_required for log2f. (#144961) 2025-06-19 17:51:01 -04:00
lntue
2f3a8fd0b3 [libc] Skip x87 floating point register and only update mxcsr for x86_64 targets when raising exceptions inside math functions. (#144951)
Updating x87 floating point register significantly affect the
performance of the functions.
All the floating point exception reads will merge the results from both
mxcsr and x87 registers anyway.
2025-06-19 17:06:52 -04:00
William Huynh
baf35d7a82 [libc] Fix bug in LIBC_CONF_ERRNO_MODE being undefined (#144896)
A typo, set() instead of list() would cause the build to not define
LIBC_CONF_ERRNO_MODE, which would cause the wrong configuration to be
used.
2025-06-19 14:50:07 -04:00
lntue
b8337349d9 [libc][math] Skip setting errno and floating point exception for math functions when LIBC_MATH flag has LIBC_MATH_NO_ERRNO and LIBC_MATH_NO_EXCEPT. (#144920) 2025-06-19 12:18:39 -04:00
lntue
c1ac87b327 [libc] Fix setjmp build order. (#144917)
Fix build order issue from
https://github.com/llvm/llvm-project/pull/139555.
2025-06-19 12:05:58 -04:00
Aly ElAshram
5645d67109 Implement sigsetjmp and siglongjmp for darwin/aarch64 (#139555) 2025-06-19 09:15:59 -04:00
sribee8
6f4e4ea177 [libc] Internal getrandom implementation (#144427)
Implemented an internal getrandom to avoid calls to the public one in
table.h

---------

Co-authored-by: Sriya Pratipati <sriyap@google.com>
2025-06-18 17:56:57 +00:00
Simon Tatham
49df87e71b [libc][printf] Fix out-of-range shift in float320 printf (#144542)
If you enable `LIBC_CONF_PRINTF_FLOAT_TO_STR_USE_FLOAT320` and use a
`%f` style printf format directive to print a nonzero number too small
to show up in the output digits, e.g. `printf("%.2f", 0.001)`, then the
output would be intermittently incorrect, because
`DyadicFloat::as_mantissa_type_rounded` would try to shift the 320-bit
mantissa right by more than 320 bits, invoking the 'undefined behavior'
clause commented in the `shift()` function in `big_int.h`.

There were already tests in the libc test suite exercising this case,
e.g. the subnormal tests in `LlvmLibcSPrintfTest.FloatDecimalConv` use
`%f` at the default precision of 6 decimal places on tiny numbers such
as 2^-1027. But because the behavior is undefined, they don't visibly
fail all the time, and in all previous test runs we'd tried with
USE_FLOAT320, they had got lucky.

The fix is simply to detect an out-of-range right shift before doing it,
and instead just set the output value to zero.
2025-06-18 08:57:51 +01:00
sribee8
844e41c2ac [libc] Moved shared constexpr to the top (#144569)
Some conversions shared constexpr so moved to the top.

---------

Co-authored-by: Sriya Pratipati <sriyap@google.com>
2025-06-17 21:12:35 +00:00
Alexey Samsonov
7ea710fafa Fix/reapply "[libc] Migrate stdio tests to ErrnoCheckingTest. (#144134)
This reverts commit 92a116c4ef with a fix
for fgets test - convert nullptr to fgets return type (char*), since the
matcher is pedantic.
2025-06-17 10:44:21 -07:00
sribee8
e6a41399cb Reland "[libc] utf8 to 32 CharacterConverter" (#144450)
Reverts llvm/llvm-project#144446
Figured out the issue, so creating a new pull request.

---------

Co-authored-by: Sriya Pratipati <sriyap@google.com>
2025-06-17 16:24:01 +00:00
sribee8
6e12442354 Revert "[libc] utf8 to 32 CharacterConverter" (#144446)
Reverts llvm/llvm-project#143973
This merge broke the build and I'm currently looking into the issue to
fix it.
2025-06-16 22:33:32 +00:00
sribee8
98eee4b554 [libc] utf8 to 32 CharacterConverter (#143973)
Implemented push and pop for utf8 to 32 conversion and tests.

---------

Co-authored-by: Sriya Pratipati <sriyap@google.com>
2025-06-16 22:20:08 +00:00
Uzair Nawaz
38daa6d4ef [libc] build fix: always use our char8_t headers even in overlay mode (#144433)
Build fix caused by certain platforms not providing char8_t when
expected
Temporary fix to just always use our own definition, even in overlay
mode.
2025-06-16 21:28:51 +00:00
Uzair Nawaz
8adccaee2a [libc] Implemented CharacterConverter push/pop for utf32->utf8 conversions (#143971)
Implemented CharacterConverter methods for conversion between utf32 ->
utf8
Added tests

---------

Co-authored-by: Michael Jones <michaelrj@google.com>
2025-06-16 20:06:46 +00:00
William Huynh
402c376daa [libc] Change default behaviour of baremetal/printf to use stdout (#143703)
In #94078, `write_to_stdout` had not been fully implemented. However,
now that it has been implemented, to conform with the C standard
(7.23.6.3. The printf function, specifically point 2), we use `stdout`.
This issue is tracked in #94685.

- Also prefer `static constexpr`
- Made it explicit that we are writing to `stdout`
2025-06-16 12:22:58 -07:00
sribee8
4cd3e41bce [libc] Removed public function calls in table.h (#144168)
Removed strcmp, strlen, and memset calls from table.h and replaced them
with internal functions.

---------

Co-authored-by: Sriya Pratipati <sriyap@google.com>
2025-06-16 18:03:21 +00:00
Amy Huang
ca38027c03 Revert "Turn LIBC_COPT_STRING_UNSAFE_WIDE_READ on by default" (#144167)
Reverts llvm/llvm-project#144163 because for some reason I didn't
realize there are ASan tests.
2025-06-13 15:30:28 -07:00
Amy Huang
a591bd222b Turn LIBC_COPT_STRING_UNSAFE_WIDE_READ on by default (#144163)
Configure strlen to use unsafe implementation because it is faster.

Because this is undefined behavior it could cause sanitizers to fail.
2025-06-13 15:09:57 -07:00
William Huynh
473dea9b0b [libc] Output all headers with LIBC_CONF_OUTPUT_ALL_HEADERS (#144114)
Following discussion from
https://discourse.llvm.org/t/missing-declarations-in-header-files/86678,
we decided to add a flag to output all headers. Requires #144049.

- Allows outputting all headers
- Minor whitespace change for alignment

---------

Co-authored-by: Michael Jones <michaelrj@google.com>
2025-06-13 14:37:25 -07:00
Amy Huang
c42912b8c9 Fix string_length function so that it always returns. (#144148)
Previously setting LIBC_COPT_STRING_UNSAFE_WIDE_READ would cause a build
error because there is a path in the ifdef that doesn't return anything.
2025-06-13 13:07:39 -07:00
Michael Jones
452276ecc0 [libc] Fix missing errno include in fuzzer (#144132)
The printf parser uses errno for setting up the %m conversion. It was
presumably getting this include indirectly until a recent change. This
patch adds a direct dependency to fix it.
2025-06-13 11:00:08 -07:00
Alexey Samsonov
92a116c4ef Revert "Fix/reapply "[libc] Migrate stdio tests to ErrnoCheckingTest."" (#144129)
Reverts llvm/llvm-project#143972 - matcher seems to be pedantic for
fgets tests, reverting to verify and fix.
2025-06-13 10:48:34 -07:00
Michael Jones
51689c9df2 [libc][NFC] clean internal fd handling (#143991)
The previous internal fcntl implementation modified errno directly, this
patch fixes that. This patch also moves open and close into OSUtil since
they are used in multiple places. There are more places that need
similar cleanup but only got comments in this patch to keep it
relatively reviewable.

Related to: https://github.com/llvm/llvm-project/issues/143937
2025-06-13 10:31:47 -07:00
William Huynh
fd432151a6 [libc] Fix bugs found when testing with all headers (#144049)
Fixes a couple of bugs found when building. The PR to enable the headers
can be found here: #144114.

- math.yaml: float128 guard
- wchar.yaml: __restrict keyword order
2025-06-13 10:26:40 -07:00
Alexey Samsonov
c609112a53 Fix/reapply "[libc] Migrate stdio tests to ErrnoCheckingTest." (#143972)
This reverts commit a93e55e57e and fixes
build and test failures:

* Proper include added to setvbuf_test.cpp
* fgetc/fgetc_unlocked/fgets tests are ported to ErrnoSetterMatcher and
are made more precise. This fixes inconsistencies between expectations
in regular and GPU builds - ErrnoSetterMatcher is configured to omit
errno matching on GPUs, as fgetc implementation on GPU doesn't set
errno, in contrast to Linux.
2025-06-13 10:25:26 -07:00
Uzair Nawaz
b184672ec7 [libc] Implemented wmemmove (#142245)
Implemented wmemmove and added tests
2025-06-13 16:48:24 +00:00
Michael Jones
70f44ec6fe [libc][NFC] Accept doc fix (#143996)
Docgen updates the docs when the config options are changed. This update
has been waiting since https://github.com/llvm/llvm-project/pull/143187.
2025-06-12 16:49:25 -07:00
Michael Jones
28c14d475f [libc] Independent strcat/strncat/stpcpy (#142643)
The previous implementations called other entrypoints. This patch fixes
strcat, strncat, and stpcpy to be properly independent.
2025-06-12 15:57:58 -07:00
Uzair Nawaz
2ee8fdbfdd [libc] Prevent building wchar on MacOS (#143978)
Prevent building wchar on macos as it depends on uchar.h which isn't
available
2025-06-12 22:14:28 +00:00
sribee8
f94950db89 [libc] Changed mbstate struct (#143942)
Changed the mbstate variable from bits processed to bytes processed and
implemented isComplete().

Co-authored-by: Sriya Pratipati <sriyap@google.com>
2025-06-12 18:04:26 +00:00