Commit Graph

674 Commits

Author SHA1 Message Date
Tue Ly
9e5fde59a4 [libc][Obvious] Temporarily disable log10_test for further investigation. 2023-01-08 18:21:30 -05:00
Tue Ly
5814b7b279 [libc][math] Implement log10 function correctly rounded for all rounding modes
Implement double precision log10 function correctly rounded for all
rounding modes.  This implementation currently needs FMA instructions for
correctness.

Use 2 passes:
Fast pass:
- 1 step range reduction with a lookup table of `2^7 = 128` elements to reduce the ranges to `[-2^-7, 2^-7]`.
- Use a degree-7 minimax polynomial generated by Sollya, evaluated using a mixed of double-double and double precisions.
- Apply Ziv's test for accuracy.
Accurate pass:
- Apply 5 more range reduction steps to reduce the ranges further to [-2^-27, 2^-27].
- Use a degree-4 minimax polynomial generated by Sollya, evaluated using 192-bit precisions.
- By the result of Lefevre (add quote), this is more than enough for correct rounding to all rounding modes.

In progress: Adding detail documentations about the algorithm.

Depend on: https://reviews.llvm.org/D136799

Reviewed By: zimmermann6

Differential Revision: https://reviews.llvm.org/D139846
2023-01-08 17:41:54 -05:00
Michael Jones
9b8a64b88d [libc] add noexcept to external function headers
To improve code generation for C++ code that directly includes our
headers, the external function definitions will now be marked noexcept.
This may not be necessary for the internal definitions since we build
with the -fno-exceptions flag.

Reviewed By: sivachandra

Differential Revision: https://reviews.llvm.org/D141095
2023-01-06 11:43:43 -08:00
Siva Chandra Reddy
f9868aa729 [libc][NFC] Use operator delete to cleanup a File object.
The File API has been refactored to allow cleanup using operator delete.

Reviewed By: lntue

Differential Revision: https://reviews.llvm.org/D140574
2022-12-22 22:31:06 +00:00
Michael Jones
a484c9bd2e [libc][obvious] fix errno for 32 bit long test
one of the tests in StrtolTest.h is intended to detect that 32 bit longs
are handled correctly, but it wasn't using the correct value for errno
causing failures.

Differential Revision: https://reviews.llvm.org/D140577
2022-12-22 12:02:38 -08:00
Siva Chandra Reddy
d06308df8b [libc] Handle allocation failures gracefully in FILE related API.
Few uses of free have not yet been replaced by the custom operator
delete yet. They will be done in a follow up patch.

Reviewed By: lntue, michaelrj

Differential Revision: https://reviews.llvm.org/D140526
2022-12-22 19:30:16 +00:00
Michael Jones
39f370aefd [libc] change str to int tests to be templated
Previously the tests were copy/pasted into several files, this changes
them to be instead templated and sharing one file.

Reviewed By: lntue

Differential Revision: https://reviews.llvm.org/D140441
2022-12-22 10:40:24 -08:00
Michael Jones
5112651826 [libc] add exponent format to printf
Add support for the %e/E conversion in printf, as well as unit tests. It
does not yet support long doubles.

Reviewed By: sivachandra

Differential Revision: https://reviews.llvm.org/D140042
2022-12-22 10:20:59 -08:00
Siva Chandra Reddy
eb9cc253cb [libc] Gracefully handle allocation failures around BlockStore.
Reviewed By: lntue

Differential Revision: https://reviews.llvm.org/D140459
2022-12-21 21:05:09 +00:00
Siva Chandra Reddy
e5ae5cec06 [libc][Obvious] Remove the unused cpp::vector class.
This can be easily resurrected if required in future. It calls malloc
directly. So, instead of updating an unused piece to the newer pattern
of using the custom operator new, this patch just deletes it.
2022-12-20 23:26:12 +00:00
Siva Chandra Reddy
3beb054170 [libc] Add a baremetal config.
The config currently includes ctype, math, stdlib, inttypes and string
functions.

Reviewed By: lntue

Differential Revision: https://reviews.llvm.org/D140378
2022-12-20 19:43:45 +00:00
Michael Jones
ce4d6717f8 [libc][NFC] make atoi undefined cases match std
The standard describes atoi as:

"equivalent to atoi: (int)strtol(nptr, (char **)NULL, 10)"

Previously, our behavior was slightly different on numbers larger than
INT_MAX, but this patch changes it to just do the cast instead. Both of
these are valid since the standard says

"If the value of the result cannot be represented, the
behavior is undefined."

But matching existing behavior makes differential fuzzing easier.

Reviewed By: sivachandra

Differential Revision: https://reviews.llvm.org/D140350
2022-12-20 10:21:25 -08:00
Michael Jones
c4b7e8a035 [libc] fix shifting exact multiples of 64 in uint
The internal uint class had a bug introduced recently when optimizing
the shift routines. When calculating the value of a block, it would
shift an adjacent block by 64 - the shift amount. If the shift amount
was 0, this would be a shift of 64, which is undefined for a 64 bit
integer. This patch fixes this by adding a conditional to catch this
case.

Reviewed By: lntue

Differential Revision: https://reviews.llvm.org/D140070
2022-12-15 13:01:32 -08:00
Siva Chandra Reddy
9a07780687 [libc][NFC] Rename "loader" to "startup".
Reviewed By: lntue

Differential Revision: https://reviews.llvm.org/D140049
2022-12-15 19:54:21 +00:00
Alex Brachet
d07c54c2a5 [libc] Templatize ato* tests
This catches missing corner cases from atol tests.

Differential Revision: https://reviews.llvm.org/D140076
2022-12-15 17:30:00 +00:00
Tue Ly
d680258170 [libc] Implement a high-precision floating point class.
Implement a high-precision floating point class using UInt<> as its
mantissa.  This will be used in accurate pass for double precision math
functions.

Reviewed By: sivachandra

Differential Revision: https://reviews.llvm.org/D136799
2022-12-14 00:37:14 -05:00
Michael Jones
65f4cc6310 [libc] add scanf int conversions
This patch adds the integer conversions %d/i/u/o/x/X to scanf as well as
unit tests to check their correctness.

Reviewed By: sivachandra

Differential Revision: https://reviews.llvm.org/D138880
2022-12-13 12:51:29 -08:00
Guillaume Chatelet
f52ca0926c [libc] Add compiler, builtin and feature detection
This is a first step to support GCC. This patch adds support for builtin and feature detection.

Differential Revision: https://reviews.llvm.org/D139712
2022-12-13 13:01:11 +00:00
Michael Jones
816b5e546c [libc] remove use after free in tests.
There were some tests added that attempted to access files after they
were closed to test errno behavior, this caused sanitizer issues. Those
portions of the tests have been disabled.

Differential Revision: https://reviews.llvm.org/D139878
2022-12-12 13:22:56 -08:00
Michael Jones
9beb8d1109 [libc] move errno out of file internals
Now errno is only set by the terminal entrypoints, and not the internal
implementations. This patch is part of the larger effort to not set
errno in libc internal code: https://github.com/llvm/llvm-project/issues/59278

Reviewed By: sivachandra

Differential Revision: https://reviews.llvm.org/D139576
2022-12-12 13:11:45 -08:00
Joseph Huber
1dfb0cd703 [libc] Fix wrappergen_test.cpp on runtimes build
The `tools` directory depends on `llvm_gtest` which is not immediately
availible with a runtimes build. This patch builds the `llvm_gtest`
target if it isn't present. Additionally, we use the CMake binary
directory to find the tool binary, which is different when using a
runtimes build. Using the LLVM binary directory should match both build
conditions.

Reviewed By: lntue

Differential Revision: https://reviews.llvm.org/D139348
2022-12-11 16:43:06 -06:00
Siva Chandra Reddy
f340030577 [libc] Add custom operator new to handle allocation failures gracefully.
This patch adds the implementation of the custom operator new functions.
The implementation of the internal strdup has been updated to use
operator new for allocation.

We will make it a policy and document that all allocations have to go
through the libc's own operator new. A future change will also add
operator delete replacements and make it a policy that deallocations in
libc internal code have to go through those replacements.

Reviewed By: lntue

Differential Revision: https://reviews.llvm.org/D139584
2022-12-11 00:29:04 +00:00
Kazu Hirata
70fe44ca77 [libc] Use std::nullopt instead of None (NFC)
This is part of an effort to migrate from llvm::Optional to
std::optional:

https://discourse.llvm.org/t/deprecating-llvm-optional-x-hasvalue-getvalue-getvalueor/63716
2022-12-09 19:14:05 -08:00
Tue Ly
360b41c7ba [libc] Fix undefined behavior in UInt<>::shift_right.
Fix undefined behavior of left-shifting uint64_t by 64 in
`UInt<>::shift_right` implementation.

Reviewed By: michaelrj, sivachandra

Differential Revision: https://reviews.llvm.org/D139566
2022-12-07 18:38:08 -05:00
Tue Ly
28f13353b6 [libc][Obvious] Update error bounds for uint_test.QuickMulHiTests. 2022-12-02 18:13:35 -05:00
Tue Ly
b80f535879 [libc][math] Implement full multiplication and quick_mul_hi for UInt class.
Implement full multiplication `UInt<A> * UInt<B> -> UInt<A + B>` and
`quick_mul_hi` that returns the higher half of the product `UInt<A> * UInt<A>`.
These 2 functions will be used for dyadic floating point class.

Reviewed By: sivachandra

Differential Revision: https://reviews.llvm.org/D138541
2022-12-02 17:53:38 -05:00
Siva Chandra Reddy
5d6ed9ff0c [libc][Obvious] Fix the build after e748db0f7f.
That changed some parts of the Support API to use std::optional instead
of llvm::Optional. This patch updates the libc uses to the new API.
2022-12-01 23:39:09 +00:00
Guillaume Chatelet
436c8f4420 [reland][libc] Add bcopy
Differential Revision: https://reviews.llvm.org/D138994
2022-12-01 10:07:04 +00:00
Guillaume Chatelet
c5fe7eb216 Revert D138994 "[libc] Add bcopy"
Broke build bot

This reverts commit 186a15f7a9.
2022-12-01 09:55:36 +00:00
Guillaume Chatelet
186a15f7a9 [libc] Add bcopy
Differential Revision: https://reviews.llvm.org/D138994
2022-12-01 09:52:10 +00:00
Joseph Huber
21d9f725df [libc] Fix test not including 'free'
Summary:
A previous change removed a transient inclusion of `stdlib.h` from the
`string_utils.h` file which this test depended on. Include it directly
here.
2022-11-29 15:03:46 -06:00
Michael Jones
36991d8342 [libc] add scanf entrypoints
This patch adds scanf, sscanf, and fscanf entrypoints. It also adds unit
tests for sscanf and a basic test to fscanf. The scanf function is
basically impossible to test in an automated fashion due to it recieving
user input.

Reviewed By: sivachandra, lntue

Differential Revision: https://reviews.llvm.org/D138076
2022-11-17 15:37:52 -08:00
Guillaume Chatelet
534f4bca58 [libc] remove mem functions dead code
Let's wait a bit for https://reviews.llvm.org/D136595 to settle before
cleaning up. Just in case we would need to revert.

Differential Revision: https://reviews.llvm.org/D136673
2022-11-16 10:29:29 +00:00
Michael Jones
c46a465eb9 [libc] disable flakey tests
The mprotect test has been failing and causing significant buildbot
noise. Given that this is a simple functions it's more important that
the buildbot errors be useful than having more thorough tests for this
function.

Reviewed By: sivachandra, lntue

Differential Revision: https://reviews.llvm.org/D138061
2022-11-15 15:13:37 -08:00
Raman Tenneti
dbd31935ed [libc] cleanup changes to gettimeofday.
+ Deleted duplicate definitions of StructTimeVal and StructTimeValPtr.
+ Caled syscall  clock_gettime to get timespec data.
+ Added tests to test for sleeping 200 and 1000 microseconds.
+ Fixed comments from https://reviews.llvm.org/D137881

Reviewed By: sivachandra

Differential Revision: https://reviews.llvm.org/D138064
2022-11-15 15:06:35 -08:00
Michael Jones
7ffa52dfd1 [libc] re-enable assert
The assert functions were disabled while the signal functions were being
fixed. This patch re-enables them.

Reviewed By: sivachandra

Differential Revision: https://reviews.llvm.org/D138056
2022-11-15 13:39:34 -08:00
Tue Ly
bc10a41080 [libc][math] Improve the performance and error printing of UInt.
Use add_with_carry builtin to improve the performance of
addition and multiplication of UInt class.  For 128-bit, it is as
fast as using __uint128_t.

Microbenchmark for addition:
https://quick-bench.com/q/-5a6xM4T8rIXBhqMTtLE-DD2h8w

Microbenchmark for multiplication:
https://quick-bench.com/q/P2muLAzJ_W-VqWCuxEJ0CU0bLDg

Microbenchmark for shift right:
https://quick-bench.com/q/N-jkKXaVsGQ4AAv3k8VpsVkua5Y

Microbenchmark for shift left:
https://quick-bench.com/q/5-RzwF8UdslC-zuhNajXtXdzLRM

Reviewed By: sivachandra

Differential Revision: https://reviews.llvm.org/D137871
2022-11-15 14:17:59 -05:00
Raman Tenneti
78f172e45a [libc] Implement gettimeofday
Implement gettimeofday per
.../onlinepubs/9699919799/functions/gettimeofday.html.
This call clock_gettime to implement gettimeofday function.

Tested:
Limited unit test: This makes a call and checks that no error was
returned. Used nanosleep for 100 microseconds and verfified it
returns a value that elapses more than 100 microseconds and less
than 300 microseconds.

Co-authored-by: Jeff Bailey <jeffbailey@google.com>

Differential Revision: https://reviews.llvm.org/D137881
2022-11-11 18:02:33 -08:00
Michael Jones
430ca14af8 [libc][obvious] fix tests using wrong size for string
In the code
const char *str = "abc"
if you do sizeof(str) you get the size of the pointer, not the string.
This patch fixes that mistake.

Differential Revision: https://reviews.llvm.org/D137586
2022-11-07 14:04:39 -08:00
Michael Jones
1c40d5ec7d [libc] add scanf string converters
This patch adds the basic conversion facilities to scanf as well as unit
tests for them. It also adds scanf_main which will be used for the
eventual scanf entrypoints.

Reviewed By: sivachandra

Differential Revision: https://reviews.llvm.org/D137376
2022-11-07 13:49:01 -08:00
Michael Jones
0c8db885f6 [libc] add scanf reader
This is the interface that will be used to read from a file or string in
scanf. This patch also adds the string and file implementations of the
reader, although the file reader is not yet complete since ungetc has
not yet been implemented.

Reviewed By: sivachandra

Differential Revision: https://reviews.llvm.org/D137117
2022-11-07 13:48:58 -08:00
Siva Chandra Reddy
6a6101958a [libc] Add implementation of getc, getc_unlocked and fgetc_unlocked.
Reviewed By: michaelrj

Differential Revision: https://reviews.llvm.org/D137507
2022-11-07 21:47:29 +00:00
Siva Chandra Reddy
43e52ad553 [libc] Add implementations of ftell.
Reviewed By: michaelrj, lntue

Differential Revision: https://reviews.llvm.org/D137395
2022-11-07 21:38:53 +00:00
Michael Jones
28e312cbf0 [libc][obvious] fix printf failing to stop on %\0
Previously, the printf parser would treat "%\0" as a conversion with the
name "\0", and advance past the null byte causing a buffer overflow.
This patch corrects that in both printf and scanf.

Reviewed By: sivachandra

Differential Revision: https://reviews.llvm.org/D137367
2022-11-07 10:44:30 -08:00
Siva Chandra Reddy
4eea884959 [libc] Add implementation of setbuf and setvbuf.
Reviewed By: michaelrj

Differential Revision: https://reviews.llvm.org/D137356
2022-11-04 07:15:13 +00:00
Siva Chandra Reddy
c9da0352a3 [libc] Convert the api-test in to an integration test.
It used to pass but showed an annoying message about _start not being
available. That will not happen anymore.
2022-11-04 07:09:18 +00:00
Siva Chandra Reddy
1ceafe5e0f [libc] Add implementation of ungetc.
A bug in the file read logic has also been fixed along the way. Parts
of the ungetc tests will fail without that bug fixed.

Reviewed By: michaelrj

Differential Revision: https://reviews.llvm.org/D137286
2022-11-02 22:45:57 +00:00
Guillaume Chatelet
3635195e0d [libc] Improve testing of mem functions
This patch extracts the testing logic from `op_tests.cpp` into
`memory_check_utils.h` so we can reuse it for mem* function integration
tests.

This makes testing consistent and thorough.
For instance this catches a bug that got unnoticed during submission of
D136595 and D135134. Integration test for memcmp was only testing a
single size.

This also leverages ASAN to make sure that data is not read / written
outside permitted boundaries

Differential Revision: https://reviews.llvm.org/D136865
2022-11-02 08:55:46 +00:00
Siva Chandra Reddy
f357a41265 [libc][Obvious] Fix a typo in a CMake file. 2022-11-01 23:18:34 +00:00
Siva Chandra Reddy
3756227ef6 [libc] Skip setjmp tests under sanitizers. 2022-11-01 23:13:57 +00:00