Commit Graph

952 Commits

Author SHA1 Message Date
Siva Chandra Reddy 12df3080fe [libc] Compile integration tests with -ffreestanding to avoid mixup with system libc. 2022-07-30 03:06:08 +00:00
Tue Ly 2ff187fbc9 [libc] Implement cosf function that is correctly rounded to all rounding modes.
Implement cosf function that is correctly rounded to all rounding
modes.

Performance benchmark using perf tool from CORE-MATH project

(https://gitlab.inria.fr/core-math/core-math/-/tree/master) on Ryzen 1700:
Before this patch (not correctly rounded):
```
$ CORE_MATH_PERF_MODE="rdtsc" ./perf.sh cosf
CORE-MATH reciprocal throughput   : 19.043
System LIBC reciprocal throughput : 26.328
LIBC reciprocal throughput        : 30.955

$ CORE_MATH_PERF_MODE="rdtsc" ./perf.sh cosf --latency
GNU libc version: 2.31
GNU libc release: stable
CORE-MATH latency   : 49.995
System LIBC latency : 59.286
LIBC latency        : 60.174

```
After this patch (correctly rounded):
```
$ CORE_MATH_PERF_MODE="rdtsc" ./perf.sh cosf
GNU libc version: 2.31
GNU libc release: stable
CORE-MATH reciprocal throughput   : 19.072
System LIBC reciprocal throughput : 26.286
LIBC reciprocal throughput        : 13.631

$ CORE_MATH_PERF_MODE="rdtsc" ./perf.sh cosf --latency
GNU libc version: 2.31
GNU libc release: stable
CORE-MATH latency   : 49.872
System LIBC latency : 59.468
LIBC latency        : 56.119
```

Reviewed By: orex, zimmermann6

Differential Revision: https://reviews.llvm.org/D130644
2022-07-29 21:08:31 -04:00
Kirill Okhotnikov a7f55f0805 [libc][math] Added sinhf function.
Differential Revision: https://reviews.llvm.org/D129278
2022-07-29 17:20:53 +02:00
Kirill Okhotnikov fcb9d7e2cf [libc][math] Added coshf function.
Differential Revision: https://reviews.llvm.org/D129275
2022-07-29 16:57:28 +02:00
Guillaume Chatelet 0c3037dfc5 [libc] Fix prototype_test_gen 2022-07-29 10:18:54 +00:00
Guillaume Chatelet e5e0e7963c [libc] Fix prototype_test_gen 2022-07-29 10:07:34 +00:00
Guillaume Chatelet 039fb3e5a1 Fix typo in FPUtil/aarch64/FMA.h 2022-07-29 10:04:11 +00:00
Guillaume Chatelet f72261508a [libc][NFC] Use STL case for type_traits
Migrating all private STL code to the standard STL case but keeping it under the CPP namespace to avoid confusion. Starting with the type_traits header.

Differential Revision: https://reviews.llvm.org/D130727
2022-07-29 09:57:03 +00:00
Kirill Okhotnikov 4b41e7b436 [libc][math] Universal exp function for cosh/sinh calculation.
Added a function and test, which can be used later for cosh/sinh
and possibly for expf/expm1f.

Differential Revision: https://reviews.llvm.org/D129215
2022-07-28 11:24:09 +02:00
Kirill Okhotnikov c78144e1c7 [libc][math] Improved performance of exp2f function.
New exp2 function algorithm:
1) Improved performance: 8.176 vs 15.270 by core-math perf tool.
2) Improved accuracy. Only two special values left.
3) Lookup table size reduced twice.

Differential Revision: https://reviews.llvm.org/D129005
2022-07-28 10:57:16 +02:00
Tue Ly 15b9380dfd [libc] Change sinf range reduction to mod pi/16 to be shared with cosf.
Change `sinf` range reduction to mod pi/16 to be shared with `cosf`.

Previously, `sinf` used range reduction `mod pi`, but this cannot be used to implement `cosf` since the minimax algorithm for `cosf` does not converge due to critical points at `pi/2`.  In order to be able to share the same range reduction functions for both `sinf` and `cosf`, we change the range reduction to `mod pi/16` for the following reasons:
- The table size is sufficiently small: 32 entries for `sin(k * pi/16)` with `k = 0..31`.  It could be reduced to 16 entries if we treat the final sign separately, with an extra multiplication at the end.
- The polynomials' degrees are reduced to 7/8 from 15, with extra computations to combine `sin` and `cos` with trig sum equality.
- The number of exceptional cases reduced to 2 (with FMA) and 3 (without FMA).
- The latency is reduced while maintaining similar throughput as before.

Reviewed By: zimmermann6

Differential Revision: https://reviews.llvm.org/D130629
2022-07-27 12:23:36 -04:00
Tue Ly 628fbbef81 [libc] Use nearest_integer instructions to improve expm1f performance.
Use nearest_integer instructions to improve expf performance.

Performance tests with CORE-MATH's perf tool:

Before the patch:
```
$ ./perf.sh expm1f
LIBC-location: /home/lnt/experiment/llvm/llvm-project/build/projects/libc/lib/libllvmlibc.a
GNU libc version: 2.31
GNU libc release: stable
CORE-MATH reciprocal throughput   : 10.096
System LIBC reciprocal throughput : 44.036
LIBC reciprocal throughput        : 11.575

$ ./perf.sh expm1f --latency
LIBC-location: /home/lnt/experiment/llvm/llvm-project/build/projects/libc/lib/libllvmlibc.a
GNU libc version: 2.31
GNU libc release: stable
CORE-MATH latency   : 42.239
System LIBC latency : 122.815
LIBC latency        : 50.122
```
After the patch:
```
$ ./perf.sh expm1f
LIBC-location: /home/lnt/experiment/llvm/llvm-project/build/projects/libc/lib/libllvmlibc.a
GNU libc version: 2.31
GNU libc release: stable
CORE-MATH reciprocal throughput   : 10.046
System LIBC reciprocal throughput : 43.899
LIBC reciprocal throughput        : 9.179

$ ./perf.sh expm1f --latency
LIBC-location: /home/lnt/experiment/llvm/llvm-project/build/projects/libc/lib/libllvmlibc.a
GNU libc version: 2.31
GNU libc release: stable
CORE-MATH latency   : 42.078
System LIBC latency : 120.488
LIBC latency        : 41.528
```

Reviewed By: zimmermann6

Differential Revision: https://reviews.llvm.org/D130502
2022-07-26 09:12:37 -04:00
Tue Ly 91ee672062 [libc] Use nearest_integer instructions to improve expf performance.
Use nearest_integer instructions to improve expf performance.

Performance tests with CORE-MATH's perf tool:

Before the patch:
```
$ ./perf.sh expf
LIBC-location: /home/lnt/experiment/llvm-project/build/projects/libc/lib/libllvmlibc.a
GNU libc version: 2.31
GNU libc release: stable
CORE-MATH reciprocal throughput   : 9.860
System LIBC reciprocal throughput : 7.728
LIBC reciprocal throughput        : 12.363

$ ./perf.sh expf --latency
LIBC-location: /home/lnt/experiment/llvm-project/build/projects/libc/lib/libllvmlibc.a
GNU libc version: 2.31
GNU libc release: stable
CORE-MATH latency   : 42.802
System LIBC latency : 35.941
LIBC latency        : 49.808
```

After the patch:
```
$ ./perf.sh expf
LIBC-location: /home/lnt/experiment/llvm/llvm-project/build/projects/libc/lib/libllvmlibc.a
GNU libc version: 2.31
GNU libc release: stable
CORE-MATH reciprocal throughput   : 9.441
System LIBC reciprocal throughput : 7.382
LIBC reciprocal throughput        : 8.843

$ ./perf.sh expf --latency
LIBC-location: /home/lnt/experiment/llvm/llvm-project/build/projects/libc/lib/libllvmlibc.a
GNU libc version: 2.31
GNU libc release: stable
CORE-MATH latency   : 44.192
System LIBC latency : 37.693
LIBC latency        : 44.145
```

Reviewed By: zimmermann6

Differential Revision: https://reviews.llvm.org/D130498
2022-07-26 09:11:27 -04:00
Siva Chandra Reddy 8fbc3c1179 [libc][Obvious] Use the correct StringView constructor in dirent_test. 2022-07-25 20:47:17 +00:00
Siva Chandra Reddy 35ea84ad6a [libc] Add dirent.h functions opendir, readdir, closedir and dirfd.
Reviewed By: lntue

Differential Revision: https://reviews.llvm.org/D130459
2022-07-25 20:23:25 +00:00
Siva Chandra Reddy 5edc7ce235 [libc] Add a simple StringStream class.
This class will be used in future changes to construct simple strings.

Reviewed By: michaelrj

Differential Revision: https://reviews.llvm.org/D130334
2022-07-25 18:27:29 +00:00
Tue Ly 83193a5e72 [libc] Raise FE_INVALID for sinf with +- inf inputs. 2022-07-25 10:20:32 -04:00
Michael Jones 3b0c78fe3b [libc][nfc] move printf inf/nan to separate function
The floating point functions all use the same inf and nan formatting. By
separating this functionality out of the %a conversion I make it
available for reuse by %f/e/g.

Reviewed By: lntue, sivachandra

Differential Revision: https://reviews.llvm.org/D129665
2022-07-22 10:29:35 -07:00
Alex Brachet 5e2d5071ff [libc] Don't call user comparator function for equal pointers
The standard says two equal pointers must compare equal
so there is no need to call the user comparator function
in this case.

Differential Revision: https://reviews.llvm.org/D130310
2022-07-22 17:03:16 +00:00
Tue Ly 600172a72b [libc] Temporarily disable arm32's sinf, cosf, sincosf entrypoints.
With correctly rounded implementations, these functions will be tested for all
rounding modes.  Since fegetround and fesetround are not implemented for arm32,
these tests will fail in all non-default rounding modes.  We will re-enable
these entrypoints and tests once fegetround and fesetround are implemented for
arm32.
2022-07-22 10:46:23 -04:00
Tue Ly d883a4ad02 [libc] Implement sinf function that is correctly rounded to all rounding modes.
Implement sinf function that is correctly rounded to all rounding modes.

- We use a simple range reduction for `pi/16 < |x|` :
    Let `k = round(x / pi)` and `y = (x/pi) - k`.
    So `k` is an integer and `-0.5 <= y <= 0.5`.
Then
```
sin(x) = sin(y*pi + k*pi)
          = (-1)^(k & 1) * sin(y*pi)
          ~ (-1)^(k & 1) * y * P(y^2)
```
    where `y*P(y^2)` is a degree-15 minimax polynomial generated by Sollya with:
```
> P = fpminimax(sin(x*pi)/x, [|0, 2, 4, 6, 8, 10, 12, 14|], [|D...|], [0, 0.5]);
```

- Performance benchmark using perf tool from CORE-MATH project
(https://gitlab.inria.fr/core-math/core-math/-/tree/master) on Ryzen 1700:
Before this patch (not correctly rounded):
```
$ CORE_MATH_PERF_MODE="rdtsc" ./perf.sh sinf
CORE-MATH reciprocal throughput   : 17.892
System LIBC reciprocal throughput : 25.559
LIBC reciprocal throughput        : 29.381
```
After this patch (correctly rounded):
```
$ CORE_MATH_PERF_MODE="rdtsc" ./perf.sh sinf
CORE-MATH reciprocal throughput   : 17.896
System LIBC reciprocal throughput : 25.740

LIBC reciprocal throughput        : 27.872
LIBC reciprocal throughput        : 20.012     (with `-msse4.2` flag)
LIBC reciprocal throughput        : 14.244     (with `-mfma` flag)
```

Reviewed By: zimmermann6

Differential Revision: https://reviews.llvm.org/D123154
2022-07-22 10:07:31 -04:00
Tue Ly ed261e7106 [libc] Add float type and flag for nearest_integer to enable SSE4.2.
Add float type and flag for nearest integer to automatically test with
and without SSE4.2 flag.

Reviewed By: sivachandra

Differential Revision: https://reviews.llvm.org/D129916
2022-07-22 09:29:41 -04:00
Siva Chandra Reddy 7c666c14f8 [libc] Add a convenience class and function for integer to string conversion.
Printf's integer converter has been modified to use the new converter. In
future, it will be used to implement other parts of the libc.

Reviewed By: michaelrj

Differential Revision: https://reviews.llvm.org/D130227
2022-07-21 20:18:59 +00:00
Siva Chandra Reddy f8322d1351 [libc] Add a method `find_last_of` to StringView.
Reviewed By: jeffbailey

Differential Revision: https://reviews.llvm.org/D130112
2022-07-19 22:43:43 +00:00
Michael Jones bf7f01d857 [libc] fix strtofloatingpoint on rare edge case
Currently, there are two string parsers that can be used in a call to
strtofloatingpoint. There is the main parser used by Clinger's fast path
and Eisel-Lemire, and the backup parser used by Simple Decimal
Conversion. There was a bug in the backup parser where if the number had
more than 800 digits (the size of the SDC buffer) before the decimal
point, it would just ignore the digits after the 800th and not count
them into the exponent. This patch fixes that issue and adds regression
tests.

Reviewed By: lntue

Differential Revision: https://reviews.llvm.org/D130032
2022-07-18 14:23:33 -07:00
Jeff Bailey 8aad330eeb [libc] Fix API for remove_{prefix, suffix}
The API in StringView.h for remove_prefix was incorrect and was returning a
new StringView rather than just altering the view.

As part of this, also removed some of the safety features.  The comment
correctly noted that the behaviour is undefined in some cases,
but the code and test cases checked for that.

One caller was relying on the old behaviour, so fixed it and added some
comments.

Tested:
check-libc
llvmlibc
libc-loader-tests

Reviewed By: gchatelet

Differential Revision: https://reviews.llvm.org/D129950
2022-07-18 14:40:09 +00:00
Kazu Hirata 8dfdb80f72 Ensure newlines at the end of files (NFC) 2022-07-17 15:37:45 -07:00
Michael Jones 3b3b816f29 [libc] add rounding modes to printf float conv
This adds functionality for rounding towards negative inf, positive inf,
and zero to the float hex conversion (%a).

Reviewed By: lntue

Differential Revision: https://reviews.llvm.org/D129702
2022-07-15 11:11:32 -07:00
Michael Jones 9fa6a88a16 [libc][arm32] add string stdlib & math entrypoints
This patch adds all the string and stdlib entrypoints, as well as a few
math entrypoints to the arm32 build.

Reviewed By: sivachandra

Differential Revision: https://reviews.llvm.org/D129821
2022-07-15 10:10:58 -07:00
Siva Chandra 98fdabecf5 [libc] Enable a few stdlib and time functions on aarch64. 2022-07-14 14:37:50 -07:00
Siva Chandra 75a628925e [libc] Enable few stdio functions on aarch64. 2022-07-14 13:40:48 -07:00
Siva Chandra edee61b55c [libc] Enable few pthread and threads functions on aarch64. 2022-07-14 13:25:21 -07:00
Siva Chandra Reddy 8dc42802f7 [libc] Add implementations of pthread_equal and pthread_self.
Reviewed By: michaelrj, lntue

Differential Revision: https://reviews.llvm.org/D129729
2022-07-14 20:12:35 +00:00
Tue Ly 0f782b84cb [libc] Add nearest integer instructions to fputil.
Add round to nearest integer instructions to fputil.  This will be
used in sinf implementation https://reviews.llvm.org/D123154

Reviewed By: michaelrj

Differential Revision: https://reviews.llvm.org/D129776
2022-07-14 13:20:35 -04:00
Jeff Bailey 897b80147d Add support for three more string_view functions
Add support for three more string_view functions

1) starts_with(char)
2) ends_with(char)
3) find_first_of(char, size_t)

Reimplemented trim in terms of the new starts_with and ends_with.

Tested:
New unit tests.

Reviewed By: gchatelet

Differential Revision: https://reviews.llvm.org/D129618
2022-07-14 14:20:20 +00:00
Siva Chandra Reddy 5e61b9c556 [libc][NFC] Make all integration tests depend on the threads implementation.
The integration tests use the loader which sets up the main thread's
self object. So, all integration tests have to depend on the threads
implementation.
2022-07-13 20:51:12 +00:00
Siva Chandra Reddy 859c189727 [libc] Linux threads - Setup TLS area of a new thread and cleanup at exit.
Reviewed By: lntue

Differential Revision: https://reviews.llvm.org/D129543
2022-07-13 20:46:18 +00:00
Alex Brachet 04c681d195 [libc] Specify rounding mode for strto[f|d] tests
The specified rounding mode will be used and restored
to what it was before the test ran.

Additionally, it moves ForceRoundingMode and RoundingMode
out of MPFRUtils to be used in more places.

Differential Revision: https://reviews.llvm.org/D129685
2022-07-13 20:20:30 +00:00
Alex Brachet 31cccebb5c [libc][NFC] Make explicit casts for gcc 2022-07-13 16:53:39 +00:00
Alex Brachet 2e8fa86e09 [libc] Add explicit casts for gcc 2022-07-13 16:52:13 +00:00
Alex Brachet 4f281fa2a8 [libc] Reset rounding mode after fsetround tests
Differential Revision: https://reviews.llvm.org/D129619
2022-07-13 15:42:47 +00:00
Siva Chandra Reddy 3c5d6312c4 [libc][NFC] Move thread platform data pointer to thread attributes.
Along the way, added constexpr constructors to the Thread data
structures.
2022-07-13 07:09:40 +00:00
Guillaume Chatelet e0aece276f [libc][utils] Add more methods to StringView
Differential Revision: https://reviews.llvm.org/D128908
2022-07-12 07:42:29 +00:00
Michael Jones 9e421a1633 [libc] clean up printf error codes
Move the constants for printf's return values into core_structs, and
update the converters to match.

Reviewed By: lntue

Differential Revision: https://reviews.llvm.org/D128767
2022-07-11 16:49:47 -07:00
Alex Brachet c179bcc151 [libc] Add imaxabs
Differential Revision: https://reviews.llvm.org/D129517
2022-07-11 21:28:21 +00:00
Siva Chandra Reddy 9b9ff63b03 [libc][NFC] Make thread_detach_test an integration test.
This is simple switch from a unittest to an integration test. It is
being done as a preparatory step to adding TLS support to thread
creation. TLS setup and initialization is tightly coupled with the
loader and hence all thread related tests should be integration tests.
2022-07-11 06:48:54 +00:00
Siva Chandra Reddy badda4ac3c [libc] Linux threads - Set CLEAR_TID addr to 0 when exiting a detached thread.
A detached thread cleans itself up at completion. So, the CLEAR_TID memory is
also gone by the time the kernel tries to signal potential waiters. By nulling
the CLEAR_TID address, we prevent the kernel from signalling at a non-existent
futex location.
2022-07-11 04:42:59 +00:00
Siva Chandra Reddy 379428c2ac [libc] Linux threads - store a ptr to the thread attribs in the start args.
Previosly, a pointer to the thread data structure was stored in the
start args. However, the thread data structure need not have the
lifetime of the thread. On the the other hand, thread attributes are
stored on the thread stack so they live as long as the thread lives.
2022-07-10 05:02:45 +00:00
Siva Chandra Reddy 9c78d92557 [libc][NFC] Remove the now used thread_attrib target. 2022-07-09 18:28:28 +00:00
Michael Jones f9f8693be3 [libc] add printf hexadecimal float conversion
This patch adds the %a/A conversions to printf, as well as the compiler
flag to disable floating point handling entirely. This will allow our
printf implementation to display every type of argument allowed by
printf, although some formats are still incomplete.

Reviewed By: sivachandra

Differential Revision: https://reviews.llvm.org/D129240
2022-07-08 15:58:20 -07:00