Commit Graph

459 Commits

Author SHA1 Message Date
Caslyn Tonelli
95ca2e2cf9 [libc] Fix swab placement
Per https://reviews.llvm.org/D147970#4256889, swab.cpp is moved out of
the /linux subdirectory and cmake specifications are amended to reflect
that swab is not OS-specific.

Differential Revision: https://reviews.llvm.org/D147988
2023-04-11 18:06:13 +00:00
Alex Brachet
9c7a370505 Reland "[libc] Add {,r}index"
Differential Revision: https://reviews.llvm.org/D147464
2023-04-11 04:30:50 +00:00
Alex Brachet
42294bf16f Revert "[libc] Add {,r}index"
This reverts commit a0a141fcbe.
2023-04-11 01:26:42 +00:00
Alex Brachet
a0a141fcbe [libc] Add {,r}index
Differential Revision: https://reviews.llvm.org/D147464
2023-04-11 01:17:11 +00:00
Caslyn Tonelli
1a3e760eda [libc] Add swab implementation
Swab implementation is added to libc/src/unistd.

Differential Revision: https://reviews.llvm.org/D147970
2023-04-10 23:37:51 +00:00
Caslyn Tonelli
a1e4f1d006 [libc] Add strxfrm Implementation
Introduce `strxfrm` and unit tests. The current implementation is
introduced without locale support.

The simplified function performs a `memcpy` if the `n` value is large
enough to store the source len + '\0', otherwise `dest` is unmodified.

Ticket: https://fxbug.dev/124217

Differential Revision: https://reviews.llvm.org/D147478
2023-04-07 00:09:22 +00:00
Alex Brachet
b4ab398cb1 [libc] Implement strsep
Differential Revision: https://reviews.llvm.org/D147503
2023-04-06 17:48:28 +00:00
Caslyn Tonelli
bc2b161408 [libc] Add strchrnul implementation
Introduce strchrnul implementation and unit tests.

Submitting on behalf of Caslyn@

Differential Revision: https://reviews.llvm.org/D147346
2023-04-03 11:08:28 -07:00
Mikhail R. Gadelha
b1e5d0d671 [libc] Enable more headers for riscv
Reviewed By: sivachandra

Differential Revision: https://reviews.llvm.org/D146203
2023-03-31 13:25:04 -03:00
Michael Jones
e0de24cb0d [libc] Re-enable wctob with fixes
The stdio test failures were due to headers potentially not being built
in the correct order. This should set up the dependencies correctly.

Reviewed By: sivachandra

Differential Revision: https://reviews.llvm.org/D146551
2023-03-29 12:49:29 -07:00
Mikhail R. Gadelha
0f6fd1b704 [libc] Add support for setjmp and longjmp in riscv
This patch implements setjmp and longjmp in riscv using inline asm. The
following changes were required:

* Omit frame pointer: otherwise gcc won't allow us to use s0
* Use __attribute__((naked)): otherwise both gcc and clang will generate
function prologue and epilogue in both functions. This doesn't happen
in x86_64, so we guard it to only riscv

Furthermore, using __attribute__((naked)) causes two problems: we
can't use `return 0` (both gcc and clang) and the function arguments in
the function body (clang only), so we had to use a0 and a1 directly.

Reviewed By: sivachandra

Differential Revision: https://reviews.llvm.org/D145584
2023-03-24 16:16:31 -03:00
Michael Jones
de939c6cd8 [libc] enable printf using system FILE
The printf and fprintf implementations use our internal implementation
to improve performance when it's available, but this patch enables using
the public FILE API for overlay mode.

Reviewed By: sivachandra, lntue

Differential Revision: https://reviews.llvm.org/D146001
2023-03-23 09:56:45 -07:00
Michael Jones
9e2b164570 [libc] temporarily disable wctob entrypoint
Differential Revision: https://reviews.llvm.org/D146484
2023-03-20 17:42:00 -07:00
Michael Jones
46b5087227 [libc] add basic wide char functions
This patch adds the wchar header, as well as the functions to convert to
and from wide chars. The header also sets up the definitions for wint
and wchar.

Reviewed By: lntue

Differential Revision: https://reviews.llvm.org/D145995
2023-03-20 16:36:21 -07:00
Mikhail R. Gadelha
1990ce74dc [libc] Enable __llvm_libc_syscall and fork
This patch enables the remaining calls from unistd.

The test cases had to be updated to:
1. Use SYS_symlinkat if SYS_symlink is not available
2. Use SYS_readlinkat if SYS_readlink is not available
3. Use SYS_unlinkat if SYS_unlink is not available
4. Use SYS_openat if SYS_open is not available

We also abort compilation if neither of the syscalls mentioned above are
available.

Differential Revision: https://reviews.llvm.org/D146161
2023-03-16 06:16:14 -03:00
Mikhail R. Gadelha
fe99de31d9 [libc] Enable spawn lib in riscv
In this patch we add support for the spawn lib in riscv.

Only small changes were required, the biggest one was to use of dup3
instead of dup2, if the latter is not available. This follows our
implementation of dup2.

Differential Revision: https://reviews.llvm.org/D146145
2023-03-16 06:16:14 -03:00
Mikhail R. Gadelha
63ed8ab4db [libc] Update supported riscv libs
This patch removes some duplicated libs added to entrypoints.txt, adds
new libs supported to entrypoints.txt and updates header.txt

Differential Revision: https://reviews.llvm.org/D146065
2023-03-16 06:16:14 -03:00
Siva Chandra
537e6e7095 [libc] Enable more functions on riscv64.
The list of headers has also been updated. Some duplicated entrypoints
have been removed.
2023-03-14 06:33:08 +00:00
Siva Chandra Reddy
71825a889a [libc][NFC] Add string.h header to various platform headers.txt. 2023-03-13 15:34:58 +00:00
Siva Chandra
cf90633cf2 [libc] Extend the linux thread implementation for riscv64.
Also, added riscv64 startup code for static linking which is used
by the integration tests. Functions from the C standard threads
library have been enabled.

Reviewed By: mikhail.ramalho

Differential Revision: https://reviews.llvm.org/D145670
2023-03-09 16:58:48 +00:00
Siva Chandra
6822156a58 [libc] Widen the riscv64 full build config. 2023-03-08 23:05:00 +00:00
Siva Chandra
dea96e7dca [libc] Remove log10 from the list of riscv64 entrypoints.
Its test is currently failing of real riscv64 hardware.
2023-03-08 20:05:13 +00:00
Mikhail R. Gadelha
4d94a0080e [libc] Support more functions in riscv
Reviewed By: sivachandra

Differential Revision: https://reviews.llvm.org/D145444
2023-03-08 14:58:51 -03:00
Siva Chandra
6f8dfeee06 [libc] Add riscv64 fenv implementation and enable the fenv.h functions.
Reviewed By: mikhail.ramalho

Differential Revision: https://reviews.llvm.org/D145560
2023-03-08 10:49:24 -03:00
Mikhail R. Gadelha
0ffea21893 [libc] Small improvements to libc cmake
This patch includes:
1. Better error message when cmake finds incompatible triple
2. Added missing header dependencies libc/include/CMakeLists.txt as per
   app.td
3. Removed unused $LLVM_LIBC_INCLUDE_DIRS cmake variable

Reviewed By: sivachandra

Differential Revision: https://reviews.llvm.org/D145496
2023-03-08 09:02:02 -03:00
Siva Chandra
9ca2fb8217 [libc] Add riscv64 syscall implementation.
All syscall wrapper functions which have unit tests have been enabled.

Reviewed By: kito-cheng

Differential Revision: https://reviews.llvm.org/D145452
2023-03-07 21:47:58 -03:00
Siva Chandra Reddy
439eebab81 [libc] Add fenv functions to arm32 baremetal config.
Also, an "arm" subfolder for baremetal config has been added.

Reviewed By: lntue

Differential Revision: https://reviews.llvm.org/D145476
2023-03-07 18:11:20 +00:00
Siva Chandra Reddy
9edef3e93d [libc] Move math.h and fenv.h macro definitions to llvm-libc-macros.
Reviewed By: lntue

Differential Revision: https://reviews.llvm.org/D145475
2023-03-07 18:08:52 +00:00
Siva Chandra Reddy
f3e4ece4e0 [libc] Add a headers.txt for linux/arm config. 2023-03-07 07:56:15 +00:00
Siva Chandra
f7e91f2b82 [libc] Add riscv64 config.
Memory functions get the basic implementation. They can be tuned
as a follow up.

Reviewed By: michaelrj, lntue

Differential Revision: https://reviews.llvm.org/D145433
2023-03-06 23:03:43 +00:00
Siva Chandra Reddy
ae8e1b8fc3 [libc] Add arm 32 FEnvImpl.
Reviewed By: lntue

Differential Revision: https://reviews.llvm.org/D145347
2023-03-06 15:57:37 +00:00
Tue Ly
3735d209a2 [libc][Obvious] Add errno entrypoint for macOS ARM64. 2023-03-06 00:06:03 -05:00
Siva Chandra Reddy
4052bc8674 [libc] Make errno an entrypoint.
The entrypoint has been added to the various entrypoint lists. The libc
code style doc has been updated with information on how errno should be
set from the libc runtime code.

Reviewed By: lntue

Differential Revision: https://reviews.llvm.org/D145179
2023-03-03 06:59:19 +00:00
Michael Jones
effd56b0a0 [libc] add basic Intel MacOS configuration
The config is based on the ARM MacOS config, but with fenv and math
functions disabled.

This should unblock this bug: https://github.com/llvm/llvm-project/issues/60910

Reviewed By: sivachandra

Differential Revision: https://reviews.llvm.org/D145099
2023-03-01 15:33:16 -08:00
Jeff Bailey
8aec3b126b [libc] Introduce sys/socket.h
This adds sys/socket.h and some definitions on Linux.

Reviewed By: sivachandra

Differential Revision: https://reviews.llvm.org/D144415
2023-02-23 07:11:20 +00:00
Raman Tenneti
d0d6d78bbd [libc] Implement ntohl and ntohs
Per spec:

https://pubs.opengroup.org/onlinepubs/9699919799/functions/ntohl.html
https://pubs.opengroup.org/onlinepubs/9699919799/functions/ntohs.html

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

Reviewed By: sivachandra

Differential Revision: https://reviews.llvm.org/D144506
2023-02-22 10:40:38 -08:00
Raman Tenneti
fbe210dc7a [libc] Implement htonl and htons
Per spec:
* https://pubs.opengroup.org/onlinepubs/9699919799/functions/htonl.html
* https://pubs.opengroup.org/onlinepubs/9699919799/functions/htons.html

Also adds UInt16Type and UInt32Type to spec.td

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

Reviewed By: sivachandra, jeffbailey, rtenneti

Differential Revision: https://reviews.llvm.org/D143795
2023-02-16 10:12:18 -08:00
Guillaume Chatelet
f100ec2517 [libc][NFC] Move architectures.h to properties subfolder 2023-02-09 09:20:46 +00:00
Renyi Chen
6cb14adbfa [libc][math] Implement scalbn, scalbnf, scalbnl.
Implement scalbn via `fptuil::ldexp` for `FLT_RADIX==2` case.
"unimplemented" otherwise.

Reviewed By: lntue, sivachandra

Differential Revision: https://reviews.llvm.org/D143116
2023-02-09 05:55:34 +00:00
Guillaume Chatelet
a2569a76e0 [libc][NFC] Rename macros 2023-02-07 20:43:33 +00:00
Guillaume Chatelet
1637351fd1 [libc][NFC] Rename architecture macros and move to macros folder 2023-02-07 10:20:22 +00:00
Tue Ly
a48c4a45e0 [libc][Obvious] Change ninja llvmlibc to ninja libc in the documentations. 2023-02-03 15:16:01 -05:00
Tue Ly
9b30f6b6d7 [libc][math] Implement acoshf function correctly rounded to all rounding modes.
Implement acoshf function correctly rounded to all rounding modes.

Reviewed By: zimmermann6

Differential Revision: https://reviews.llvm.org/D142781
2023-02-01 11:35:15 -05:00
Tue Ly
46b15fd19e [libc][math] Implement asinhf function correctly rounded for all rounding modes.
Implement asinhf function correctly rounded for all rounding modes.

Reviewed By: zimmermann6

Differential Revision: https://reviews.llvm.org/D142681
2023-01-27 11:12:27 -05:00
Alex Brachet
741021de32 [libc] Implement strcasestr
Differential Revision: https://reviews.llvm.org/D142518
2023-01-25 17:58:13 +00:00
Alex Brachet
e9d571d3b6 [libc] Implement str{,n}casecmp
Differential Revision: https://reviews.llvm.org/D141236
2023-01-11 05:38:33 +00: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
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
Siva Chandra Reddy
740db97ad1 [libc][NFC] Simplify how the definiton of NULL macro is added to string.h.
We do not list macro definitions in api.td files anymore. Not all macros
definitions have been moved out. This change moves the definition of the
NULL macro out.

Reviewed By: lntue, jhuber6

Differential Revision: https://reviews.llvm.org/D140376
2022-12-20 19:14:27 +00:00
Siva Chandra Reddy
5e750b86ca [libc] Simplify generation of errno.h.
Reviewed By: lntue

Differential Revision: https://reviews.llvm.org/D140375
2022-12-20 19:04:13 +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
55151e138d [libc] Add initial support for a libc implementation for the GPU
This patch contains the initial support for building LLVM's libc as a
target for the GPU. Currently this only supports a handful of very basic
functions that can be implemented without an operating system. The GPU
code is build using the existing OpenMP toolchain. This allows us to
minimally change the existing codebase and get a functioning static
library. This patch allows users to create a static library called
`libcgpu.a` that contains fat binaries containing device IR.

Current limitations are the lack of test support and the fact that only
one target OS can be built at a time. That is, the user cannot get a
`libc` for Linux and one for the GPU simultaneously.

This introduces two new CMake variables to control the behavior
`LLVM_LIBC_TARET_OS` is exported so the user can now specify it to equal
`"gpu"`. `LLVM_LIBC_GPU_ARCHITECTURES` is also used to configure how
many targets to build for at once.

Depends on D138607

Reviewed By: sivachandra

Differential Revision: https://reviews.llvm.org/D138608
2022-11-29 14:51:54 -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
Joseph Huber
dabb7514f5 [libc] Fix assert.h and ctype.h not being built
The `assert.h` and `ctype.h` headers are never built despite their
entrypoints being present in the generated library. This patch adds a
dependency on these headers so that they will be built properly.

Reviewed By: sivachandra

Differential Revision: https://reviews.llvm.org/D138142
2022-11-16 12:00:41 -06: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
Raman Tenneti
35155ee7d3 Removed tabs. 2022-11-11 18:11:06 -08: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
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
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
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
Siva Chandra Reddy
15ae08c1a1 [libc] Add definitions of a few missing macros and types. 2022-11-02 07:17:33 +00:00
Siva Chandra Reddy
3b82b4fbd5 [libc] Add x86_64 implementation of setjmp and longjmp.
Reviewed By: michaelrj

Differential Revision: https://reviews.llvm.org/D137147
2022-11-01 22:58:35 +00:00
Tue Ly
45233cc1ca [libc][math] Add place-holder implementation for pow function.
Add place-holder implementation for pow function to unblock libc demo
examples.

Reviewed By: michaelrj

Differential Revision: https://reviews.llvm.org/D137109
2022-10-31 17:23:33 -04:00
Tue Ly
97b4cc83e1 [libc][math] Add place-holder implementation for asin to unblock demo examples.
Add a place-holder implementation for asin to unblock libc demo
examples.

Reviewed By: michaelrj

Differential Revision: https://reviews.llvm.org/D137105
2022-10-31 17:22:12 -04:00
Alex Brachet
5fd03c8176 [libc] Implement getopt
Differential Revision: https://reviews.llvm.org/D133487
2022-10-31 16:55:53 +00:00
Michael Jones
f418f88824 [libc] add locale free strcoll
The strcoll function is intended to compare strings based on their
ordering in the current locale. Since the locale facilities have not yet
been added, a simple implementation that is the same as strcmp has been
added as a placeholder.

Reviewed By: sivachandra

Differential Revision: https://reviews.llvm.org/D136802
2022-10-28 11:13:07 -07:00
Michael Jones
2d52283391 [libc] add fgets
This adds the fgets function and its unit tests.

Reviewed By: sivachandra

Differential Revision: https://reviews.llvm.org/D136785
2022-10-27 10:27:24 -07:00
Alex Brachet
d6ac84bce8 Revert "[libc] Implement getopt"
This reverts commit a678f86351.
2022-10-27 06:47:24 +00:00
Alex Brachet
a678f86351 [libc] Implement getopt
Differential Revision: https://reviews.llvm.org/D133487
2022-10-27 06:23:33 +00:00
Siva Chandra
07b7023181 [libc] Enable more entrypoints on aarch64. 2022-10-26 14:03:15 -07:00
Siva Chandra
f8490601c2 [libc] Enable a few entrypoints on aarch64 already available on x86_64. 2022-10-25 15:41:41 -07:00
Siva Chandra Reddy
22ea0e5d9b [libc] Add Linux implementations of time and clock functions.
Reviewed By: michaelrj

Differential Revision: https://reviews.llvm.org/D136666
2022-10-25 18:06:05 +00:00
Siva Chandra Reddy
6faf40bb75 [libc] Add the header sys/types.h.
Reviewed By: michaelrj

Differential Revision: https://reviews.llvm.org/D136642
2022-10-25 00:24:49 +00:00
Raman Tenneti
12204429f2 [libc] Add implementation of difftime function.
The difftime function computes the difference between two calendar
times: time1 - time0 as per as per 7.27.2.2 section in
http://www.open-std.org/jtc1/sc22/wg14/www/docs/n2478.pdf.

  double difftime(time_t time1, time_t time0);

Tested:
Unit tests

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

Reviewed By: jeffbailey

Differential Revision: https://reviews.llvm.org/D136631
2022-10-24 15:14:26 -07:00
Siva Chandra Reddy
0480b45e9e [libc] Add implementation of fgetc.
Reviewed By: michaelrj

Differential Revision: https://reviews.llvm.org/D136421
2022-10-22 03:34:44 +00:00
Siva Chandra Reddy
2a038f9138 [libc] Add stdin definition.
Reviewed By: michaelrj

Differential Revision: https://reviews.llvm.org/D136398
2022-10-22 03:27:31 +00:00
Siva Chandra Reddy
be4e425758 [libc] Add select.h and the implementation of the select function for Linux.
Reviewed By: michaelrj

Differential Revision: https://reviews.llvm.org/D136375
2022-10-22 03:17:48 +00:00
Siva Chandra Reddy
9ec0ad75f5 [libc][Obvious] Fix incomplete spec definition of sys/random.h. 2022-10-20 06:57:00 +00:00
Siva Chandra Reddy
c03ea285bd [libc][Obvious] Add termios.h to the list of x86_64 linux headers. 2022-10-19 23:00:15 +00:00
Siva Chandra Reddy
67957368ae [libc] Add implementation of sigaltstack for linux.
Reviewed By: michaelrj

Differential Revision: https://reviews.llvm.org/D135949
2022-10-18 22:04:30 +00:00
Siva Chandra Reddy
b2a294bcf8 [libc] Add termios.h and the implementation of functions declared in it.
Reviewed By: lntue, michaelrj

Differential Revision: https://reviews.llvm.org/D136143
2022-10-18 20:53:00 +00:00
Michael Jones
40b494396b [libc] add putc, fputc, and putchar
These three functions are simple, but needed for libc build testing.

Reviewed By: lntue

Differential Revision: https://reviews.llvm.org/D135990
2022-10-17 16:29:04 -07:00
Siva Chandra Reddy
8b0e84a6f6 [libc] Add implementation of the POSIX getcwd function.
Reviewed By: michaelrj

Differential Revision: https://reviews.llvm.org/D135905
2022-10-14 16:20:59 +00:00
Siva Chandra Reddy
02a543db66 [libc] Add a simple implementation of the posix_spawn function.
The implementation currently ignores all spawn attributes. Support for
them will be added in future changes.

A simple allocator for integration tests has been added so that the
integration test for posix_spawn can use the
posix_spawn_file_actions_add* functions.

Reviewed By: michaelrj

Differential Revision: https://reviews.llvm.org/D135752
2022-10-13 18:47:47 +00:00
Michael Jones
35a4fe4c0b [libc] add headers sys/auxv sys/prctl and sys/time
These headers are uncommonly used, and from extensions, but some basic
support is needed. Macros have been added where available.

Reviewed By: sivachandra

Differential Revision: https://reviews.llvm.org/D135731
2022-10-12 13:19:50 -07:00
Siva Chandra Reddy
28943d617a [libc] Add POSIX functions posix_spawn_file_actions_*.
Namely, posix_spawn_file_actions_addclose,
posix_spawn_file_actions_adddup2, posix_spawn_file_actions_addopen,
posix_spawn_file_actions_destroy, posix_spawn_file_actions_init have
been added.

Reviewed By: michaelrj, lntue

Differential Revision: https://reviews.llvm.org/D135603
2022-10-11 04:54:44 +00:00
Michael Jones
a6abcfee8a [libc] fix header list for x86_64
Some headers hadn't been added, this fixes that and improves the
ordering.

Reviewed By: sivachandra

Differential Revision: https://reviews.llvm.org/D135629
2022-10-10 16:05:53 -07:00
Michael Jones
7639ba6906 [libc] add isatty
The isatty function uses the side effects of an ioctl call to determine
if a specific file descriptor is a terminal. I chose TIOCGETD (get line
discipline of terminal) because it didn't require any new structs.

Reviewed By: sivachandra, lntue

Differential Revision: https://reviews.llvm.org/D135618
2022-10-10 15:20:46 -07:00
Michael Jones
1ac525b28b [libc] add sysconf with pagesize
The sysconf function has many options, this patch adds the basic funtion
and the pagesize option. More options will be added in future patches.

Reviewed By: sivachandra

Differential Revision: https://reviews.llvm.org/D135409
2022-10-10 14:51:45 -07:00
Siva Chandra Reddy
438e59182b [libc] Add implementation of pthread_atfork.
Reviewed By: michaelrj

Differential Revision: https://reviews.llvm.org/D135432
2022-10-10 18:28:43 +00:00
Schrodinger ZHU Yifan
9eb11856cf Implement getrandom function for linux targets.
Reviewed By: michaelrj, sivachandra

Differential Revision: https://reviews.llvm.org/D134665
2022-10-10 11:00:35 -07:00
Michael Jones
07793f95c4 [libc] add strsignal and refactor message mapping
The logic for strsignal and strerror is very similar, so I've moved them
both to use a shared utility (MessageMapper) for the basic
functionality.

Reviewed By: sivachandra

Differential Revision: https://reviews.llvm.org/D135322
2022-10-07 11:11:53 -07:00
Michael Jones
a9f95b769e [libc] add strerror_r function
I've implemente the gnu variant of strerror_r since that seems to be the
one more relevant to what we're trying to do.

Differential Revision: https://reviews.llvm.org/D135227
2022-10-07 11:07:06 -07:00
Michael Jones
6d65ac5009 [libc][obvious] move kill into fullbuild only
Signal is currently fullbuild only, so all functions inside it should be
as well.

Differential Revision: https://reviews.llvm.org/D135410
2022-10-06 15:35:45 -07:00
Michael Jones
9015810648 [libc] add kill
Add the kill syscall wrapper and tests.

Reviewed By: sivachandra

Differential Revision: https://reviews.llvm.org/D135406
2022-10-06 15:25:56 -07:00
Siva Chandra Reddy
3f965818b6 [libc] Add POSIX execv and execve functions.
The POSIX global variable environ has also been added.

Reviewed By: michaelrj

Differential Revision: https://reviews.llvm.org/D135351
2022-10-06 19:50:23 +00:00
Siva Chandra Reddy
3e1986682f [libc][Obvious] Add "__" prefix to sched_getcpucount in the spec and elsewhere.
Without this fix, the declaration in sched.h will not have the "__" prefix and
will cause a compile failure.

Reviewed By: michaelrj

Differential Revision: https://reviews.llvm.org/D135286
2022-10-05 18:01:13 +00:00
Siva Chandra Reddy
995105de1b [libc] Add the POSIX waitpid function and the BSD wait4 function.
Reviewed By: lntue, michaelrj

Differential Revision: https://reviews.llvm.org/D135225
2022-10-05 07:38:55 +00:00
Michael Jones
a9c7d0e1ae [libc] fix fullbuild handling for macro functions
Now they're only included at all if the mode is fullbuild.

Differential Revision: https://reviews.llvm.org/D135221
2022-10-04 15:37:31 -07:00
Michael Jones
0b790afb06 [libc] add CPU_COUNT macro and backing function
Add the macro CPU_COUNT as well as a backing function to implement the
functionality.

Reviewed By: sivachandra

Differential Revision: https://reviews.llvm.org/D135179
2022-10-04 15:22:55 -07:00
Michael Jones
38b6f58e33 [libc] implement basic rand and srand
This provides the reference implementation of rand and srand. In future
this will likely be upgraded to something that supports full ints.

Reviewed By: sivachandra

Differential Revision: https://reviews.llvm.org/D135187
2022-10-04 13:31:26 -07:00
Siva Chandra Reddy
e3638e83db [libc] Add a minimal implementation of the POSIX fork function.
A very simple and minimal implementation of fork is added. Future
changes will add more functionality to satisfy POSIX and Linux
requirements.

An implementation of wait and a few support macros in sys/wait.h
have also been added to help with testing the fork function.

Reviewed By: lntue, michaelrj

Differential Revision: https://reviews.llvm.org/D135131
2022-10-04 19:12:08 +00:00
Michael Jones
1801c356f6 [libc] add syscall function
Add the syscall wrapper function and tests. It's implemented using a
macro to guarantee the minimum number of arguments.

Reviewed By: sivachandra, lntue

Differential Revision: https://reviews.llvm.org/D134919
2022-09-30 15:46:28 -07:00
Siva Chandra Reddy
215c9fa4de [libc] Re-enable functions from signal.h and re-enable abort.
They were disabled because we were including linux/signal.h from our
signal.h. Linux's signal.h is not designed to be included from user
programs as it causes a lot of non-standard name pollution. Also, it is
not self-contained. This change defines types and macros relevant for
signal related syscalls within libc's headers and removes inclusion of
Linux headers.

This patch enables the funtions only for x86_64. They will be enabled
for aarch64 also in a follow up patch after testing.

Reviewed By: abrachet, lntue

Differential Revision: https://reviews.llvm.org/D134567
2022-09-30 07:31:50 +00:00
Siva Chandra
5e56e294ae [libc][Obvious] Enable some of the recently added functions on aarch64. 2022-09-29 15:06:44 -07:00
Siva Chandra Reddy
545b954251 [libc] Add GNU extension functions sched_getaffinity and sched_setaffinity.
Reviewed By: michaelrj

Differential Revision: https://reviews.llvm.org/D134858
2022-09-29 20:31:46 +00:00
Michael Jones
b49d626cb4 [libc] add clock_gettime
Add the clock_gettime syscall wrapper and tests.

Reviewed By: sivachandra

Differential Revision: https://reviews.llvm.org/D134773
2022-09-29 10:23:21 -07:00
Siva Chandra Reddy
3367539010 [libc] Add implementation of pthread_once.
The existing thrd_once function has been refactored so that the
implementation can be shared between thrd_once and pthread_once
functions.

Reviewed By: michaelrj

Differential Revision: https://reviews.llvm.org/D134716
2022-09-28 06:54:48 +00:00
Raman Tenneti
8f1e362ee9 Implement nanosleep per https://pubs.opengroup.org/onlinepubs/009695399/basedefs/time.h.html
Tested:
Limited unit test: This makes a call and checks that no error was
returned, but we currently don't have the ability to ensure that
time has elapsed as expected.

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

Reviewed By: sivachandra, jeffbailey

Differential Revision: https://reviews.llvm.org/D134095
2022-09-24 00:13:58 +00:00
Michael Jones
85c70da732 [libc] add madvise and posix_madvise
Add the madvise and posix_madvise syscall wrappers and tests.

Reviewed By: sivachandra

Differential Revision: https://reviews.llvm.org/D134498
2022-09-23 09:36:40 -07:00
Michael Jones
47b724048b [libc] add mprotect
Add the mprotect syscall wrapper and tests.

Reviewed By: sivachandra

Differential Revision: https://reviews.llvm.org/D134497
2022-09-23 09:36:37 -07:00
Michael Jones
8d615a5e8a [libc] move sys/mman macros to /include
Previously the mman macros were in api.td, but platform differences are
easier to handle with preprocessor macros so they have been moved to
include. Also I completed the list of macros (at least for what I need
soon) and fixed some previously incorrect values.

Reviewed By: sivachandra, lntue

Differential Revision: https://reviews.llvm.org/D134491
2022-09-23 09:36:34 -07:00
Siva Chandra Reddy
4f1474daec [libc] Add implementations of POSIX getpid, getppid, getuid, geteuid functions.
Reviewed By: lntue

Differential Revision: https://reviews.llvm.org/D134338
2022-09-21 18:41:20 +00:00
Siva Chandra Reddy
e310f8bddf [libc] Add implementation of functions stat, fstat and lstat.
All supporting type and macro definitions have also been added.

Reviewed By: lntue

Differential Revision: https://reviews.llvm.org/D134262
2022-09-21 18:35:02 +00:00
Michael Jones
a9e0dbefdd [libc] add fputs and puts
add fputs, puts, and the EOF macro that they use.

Reviewed By: sivachandra

Differential Revision: https://reviews.llvm.org/D134328
2022-09-21 11:10:20 -07:00
Michael Jones
42bcb35c0f [libc] add strerror
Strerror maps error numbers to strings. Additionally, a utility for
mapping errors to strings was added so that it could be reused for
perror and similar.

Reviewed By: sivachandra

Differential Revision: https://reviews.llvm.org/D134074
2022-09-20 16:23:36 -07:00
Tue Ly
a752460d73 [libc][math] Implement exp10f function correctly rounded to all rounding modes.
Implement exp10f function correctly rounded to all rounding modes.

Algorithm: perform range reduction to reduce
```
  10^x = 2^(hi + mid) * 10^lo
```
where:
```
  hi is an integer,
  0 <= mid * 2^5 < 2^5
  -log10(2) / 2^6 <= lo <= log10(2) / 2^6
```
Then `2^mid` is stored in a table of 32 entries and the product `2^hi * 2^mid` is
performed by adding `hi` into the exponent field of `2^mid`.
`10^lo` is then approximated by a degree-5 minimax polynomials generated by Sollya with:
```
  > P = fpminimax((10^x - 1)/x, 4, [|D...|], [-log10(2)/64. log10(2)/64]);
```
Performance benchmark using perf tool from the CORE-MATH project on Ryzen 1700:
```
$ CORE_MATH_PERF_MODE="rdtsc" ./perf.sh exp10f
GNU libc version: 2.35
GNU libc release: stable
CORE-MATH reciprocal throughput   : 10.215
System LIBC reciprocal throughput : 7.944

LIBC reciprocal throughput        : 38.538
LIBC reciprocal throughput        : 12.175   (with `-msse4.2` flag)
LIBC reciprocal throughput        : 9.862    (with `-mfma` flag)

$ CORE_MATH_PERF_MODE="rdtsc" ./perf.sh exp10f --latency
GNU libc version: 2.35
GNU libc release: stable
CORE-MATH latency   : 40.744
System LIBC latency : 37.546

BEFORE
LIBC latency        : 48.989
LIBC latency        : 44.486   (with `-msse4.2` flag)
LIBC latency        : 40.221   (with `-mfma` flag)
```
This patch relies on https://reviews.llvm.org/D134002

Reviewed By: orex, zimmermann6

Differential Revision: https://reviews.llvm.org/D134104
2022-09-19 10:01:40 -04:00
Siva Chandra Reddy
7fb96fb5d3 [libc] Add implementation of POSIX "uname" function.
Reviewed By: lntue

Differential Revision: https://reviews.llvm.org/D134065
2022-09-16 21:21:29 +00:00
Siva Chandra Reddy
9050a59c66 [libc][Obvious] Fix typo in struct rlimit name - remove the "_t" suffix. 2022-09-16 21:07:17 +00:00
Siva Chandra Reddy
f5cbbb9988 [libc] Add implementation of POSIX setrlimit and getrlimit functions.
Reviewed By: lntue

Differential Revision: https://reviews.llvm.org/D134016
2022-09-16 20:50:28 +00:00
Siva Chandra Reddy
d23d858d04 [libc] Add the implementation of the "remove" function.
Reviewed By: lntue

Differential Revision: https://reviews.llvm.org/D133922
2022-09-15 17:32:02 +00:00
Siva Chandra Reddy
6e675fba3a [libc] Add POSIX functions pread and pwrite.
Reviewed By: michaelrj

Differential Revision: https://reviews.llvm.org/D133888
2022-09-14 20:52:20 +00:00
Siva Chandra Reddy
419580c699 [libc] Add implementation of POSIX function "access".
Reviewed By: lntue

Differential Revision: https://reviews.llvm.org/D133814
2022-09-14 07:44:47 +00:00
Siva Chandra Reddy
8989aa003f [libc] Add POSIX functions dup, dup2, and GNU extension function dup3.
Reviewed By: lntue

Differential Revision: https://reviews.llvm.org/D133748
2022-09-13 18:06:30 +00:00
Tue Ly
463dcc8749 [libc][math] Implement acosf function correctly rounded for all rounding modes.
Implement acosf function correctly rounded for all rounding modes.

We perform range reduction as follows:

- When `|x| < 2^(-10)`, we use cubic Taylor polynomial:
```
  acos(x) = pi/2 - asin(x) ~ pi/2 - x - x^3 / 6.
```
- When `2^(-10) <= |x| <= 0.5`, we use the same approximation that is used for `asinf(x)` when `|x| <= 0.5`:
```
  acos(x) = pi/2 - asin(x) ~ pi/2 - x - x^3 * P(x^2).
```
- When `0.5 < x <= 1`, we use the double angle formula: `cos(2y) = 1 - 2 * sin^2 (y)` to reduce to:
```
  acos(x) = 2 * asin( sqrt( (1 - x)/2 ) )
```
- When `-1 <= x < -0.5`, we reduce to the positive case above using the formula:
```
  acos(x) = pi - acos(-x)
```

Performance benchmark using perf tool from the CORE-MATH project on Ryzen 1700:
```
$ CORE_MATH_PERF_MODE="rdtsc" ./perf.sh acosf
GNU libc version: 2.35
GNU libc release: stable
CORE-MATH reciprocal throughput   : 28.613
System LIBC reciprocal throughput : 29.204
LIBC reciprocal throughput        : 24.271

$ CORE_MATH_PERF_MODE="rdtsc" ./perf.sh asinf --latency
GNU libc version: 2.35
GNU libc release: stable
CORE-MATH latency   : 55.554
System LIBC latency : 76.879
LIBC latency        : 62.118
```

Reviewed By: orex, zimmermann6

Differential Revision: https://reviews.llvm.org/D133550
2022-09-09 09:55:30 -04:00
Tue Ly
e2f065c2a3 [libc][math] Implement asinf function correctly rounded for all rounding modes.
Implement asinf function correctly rounded for all rounding modes.

For `|x| <= 0.5`, we approximate `asin(x)` by
```
  asin(x) = x * P(x^2)
```
where `P(X^2) = Q(X)` is a degree-20 minimax even polynomial approximating
`asin(x)/x` on `[0, 0.5]` generated by Sollya with:
```
  > Q = fpminimax(asin(x)/x, [|0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20|],
                 [|1, D...|], [0, 0.5]);
```

When `|x| > 0.5`, we perform range reduction as follow:
Assume further that `0.5 < x <= 1`, and let:
```
  y = asin(x)
```
We will use the double angle formula:
```
  cos(2X) = 1 - 2 sin^2(X)
```
and the complement angle identity:
```
  x = sin(y) = cos(pi/2 - y)
              = 1 - 2 sin^2 (pi/4 - y/2)
```
So:
```
  sin(pi/4 - y/2) = sqrt( (1 - x)/2 )
```
And hence:
```
  pi/4 - y/2 = asin( sqrt( (1 - x)/2 ) )
```
Equivalently:
```
  asin(x) = y = pi/2 - 2 * asin( sqrt( (1 - x)/2 ) )
```
Let `u = (1 - x)/2`, then
```
  asin(x) = pi/2 - 2 * asin(u)
```
Moreover, since `0.5 < x <= 1`,
```
  0 <= u < 1/4, and 0 <= sqrt(u) < 0.5.
```
And hence we can reuse the same polynomial approximation of `asin(x)` when
`|x| <= 0.5`:
```
  asin(x) = pi/2 - 2 * u * P(u^2).
```

Performance benchmark using `perf` tool from the CORE-MATH project on Ryzen 1700:
```
$ CORE_MATH_PERF_MODE="rdtsc" ./perf.sh asinf
CORE-MATH reciprocal throughput   : 23.418
System LIBC reciprocal throughput : 27.310
LIBC reciprocal throughput        : 22.741

$ CORE_MATH_PERF_MODE="rdtsc" ./perf.sh asinf --latency
GNU libc version: 2.35
GNU libc release: stable
CORE-MATH latency   : 58.884
System LIBC latency : 62.055
LIBC latency        : 62.037
```

Reviewed By: orex, zimmermann6

Differential Revision: https://reviews.llvm.org/D133400
2022-09-07 19:27:47 -04:00
Siva Chandra Reddy
4c810ecb68 [libc] Add arm-32 syscall implementation.
The implementation currently supports only non-thumb mode. As a test for
the implementation, mmap and munmap functions have been enabled.

Reviewed By: michaelrj

Differential Revision: https://reviews.llvm.org/D132825
2022-08-31 18:10:40 +00:00
Kirill Okhotnikov
77e1d9beed [libc][math] Added atanf function.
Performance by core-math (core-math/glibc 2.31/current llvm-14):
28.879/20.843/20.15

Differential Revision: https://reviews.llvm.org/D132842
2022-08-30 22:39:54 +02:00
Kirill Okhotnikov
6c1fc7e430 [libc][math] Added atanhf function.
Performance by core-math (core-math/glibc 2.31/current llvm-14):
10.845/43.174/13.467

The review is done on top of D132809.

Differential Revision: https://reviews.llvm.org/D132811
2022-08-30 22:39:54 +02:00
Siva Chandra Reddy
f6506ec443 [libc] Implement POSIX truncate and ftruncate functions for Linux.
Reviewed By: michaelrj

Differential Revision: https://reviews.llvm.org/D132705
2022-08-26 19:27:24 +00:00
Siva Chandra Reddy
b8be3dabde [libc] Add Linux implementation of GNU extension function sendfile.
Reviewed By: michaelrj

Differential Revision: https://reviews.llvm.org/D132721
2022-08-26 19:13:40 +00:00
Siva Chandra Reddy
00e51f04e8 [libc] Implement linux link, linkat, symlink, symlinkat, readlink, readlinkat.
Reviewed By: michaelrj

Differential Revision: https://reviews.llvm.org/D132619
2022-08-25 18:50:39 +00:00
Siva Chandra Reddy
85dff76416 [libc] Add linux implementation of POSIX fchmodat function.
Reviewed By: michaelrj

Differential Revision: https://reviews.llvm.org/D132533
2022-08-24 18:46:29 +00:00
Siva Chandra
d00e97df0f [libc][Obvious] Fix typo is chmod implementation.
This now allows enabling the chmod function on aarch64.
2022-08-23 15:01:21 -07:00
Siva Chandra
8856137ce7 [libc] Enable a few entrypoints on aarch64 which are now available on x86_64. 2022-08-23 12:31:42 -07:00
Siva Chandra Reddy
055322891c [libc] Add Linux implementations of POSIX chdir, fchdir, chmod and fchmod.
Reviewed By: michaelrj

Differential Revision: https://reviews.llvm.org/D132445
2022-08-23 17:16:49 +00:00
Siva Chandra Reddy
84517b6fb1 [libc] Add more headers to the linux x86_64 and aarch64 configs. 2022-08-19 07:15:08 +00:00
Siva Chandra Reddy
e17ff7dd2a [libc][Obvious] Convert an add_header target to add_header_library target. 2022-08-16 20:33:24 +00:00
Michael Jones
e0e7fa36d3 [libc] enable s(n)printf without fullbuild
To use the FILE data structure, LLVM-libc must be in fullbuild mode
since it expects its own implementation. This means that (f)printf can't
be used without fullbuild, but s(n)printf only uses strings. This patch
adjusts the CMake to allow for this.

Reviewed By: sivachandra, lntue

Differential Revision: https://reviews.llvm.org/D131913
2022-08-15 13:45:34 -07:00
Tue Ly
af59bac4ca [libc] Add missing header and Windows entrypoints for tanf. 2022-08-12 09:34:33 -04:00
Tue Ly
82d6e77048 [libc] Implement tanf function correctly rounded for all rounding modes.
Implement tanf function correctly rounded for all rounding modes.

We use the range reduction that is shared with `sinf`, `cosf`, and `sincosf`:
```
  k = round(x * 32/pi) and y = x * (32/pi) - k.
```
Then we use the tangent of sum formula:
```
  tan(x) = tan((k + y)* pi/32) = tan((k mod 32) * pi / 32 + y * pi/32)
         = (tan((k mod 32) * pi/32) + tan(y * pi/32)) / (1 - tan((k mod 32) * pi/32) * tan(y * pi/32))
```
We need to make a further reduction when `k mod 32 >= 16` due to the pole at `pi/2` of `tan(x)` function:
```
  if (k mod 32 >= 16): k = k - 31, y = y - 1.0
```
And to compute the final result, we store `tan(k * pi/32)` for `k = -15..15` in a table of 32 double values,
and evaluate `tan(y * pi/32)` with a degree-11 minimax odd polynomial generated by Sollya with:
```
>  P = fpminimax(tan(y * pi/32)/y, [|0, 2, 4, 6, 8, 10|], [|D...|], [0, 1.5]);
```

Performance benchmark using `perf` tool from the CORE-MATH project on Ryzen 1700:
```
$ CORE_MATH_PERF_MODE="rdtsc" ./perf.sh tanf
CORE-MATH reciprocal throughput   : 18.586
System LIBC reciprocal throughput : 50.068

LIBC reciprocal throughput        : 33.823
LIBC reciprocal throughput        : 25.161     (with `-msse4.2` flag)
LIBC reciprocal throughput        : 19.157     (with `-mfma` flag)

$ CORE_MATH_PERF_MODE="rdtsc" ./perf.sh tanf --latency
GNU libc version: 2.31
GNU libc release: stable
CORE-MATH latency   : 55.630
System LIBC latency : 106.264

LIBC latency        : 96.060
LIBC latency        : 90.727    (with `-msse4.2` flag)
LIBC latency        : 82.361    (with `-mfma` flag)
```

Reviewed By: orex

Differential Revision: https://reviews.llvm.org/D131715
2022-08-12 09:21:05 -04:00
Siva Chandra Reddy
4a738ee822 [libc] Add implemementations of thread specific data related API.
Specifically, POSIX functions pthread_key_create, pthread_key_delete,
pthread_setspecific and pthread_getspecific have been added. The C
standard equivalents tss_create, tss_delete, tss_set and tss_get have
also been added.

Reviewed By: lntue, michaelrj

Differential Revision: https://reviews.llvm.org/D131647
2022-08-12 05:28:40 +00:00
Siva Chandra Reddy
0071a79532 [libc] Add implementation of pthread_exit and thrd_exit.
Reviewed By: michaelrj

Differential Revision: https://reviews.llvm.org/D131451
2022-08-10 06:28:47 +00:00
Siva Chandra Reddy
658c84e415 [libc] Add GNU extension functions pthread_setname_np and pthread_getname_np.
Reviewed By: michaelrj, lntue

Differential Revision: https://reviews.llvm.org/D130872
2022-08-02 01:57:03 +00:00
Kirill Okhotnikov
5ef987c985 [libc][math] Added tanhf function.
Correct rounding function. Performance ~2x faster than glibc analog.

Performance (llvm 12 intel):
```
CORE_MATH_PERF_MODE=rdtsc PERF_ARGS='' ./perf.sh tanhf
GNU libc version: 2.31
GNU libc release: stable
13.279
37.492
18.145
CORE_MATH_PERF_MODE=rdtsc PERF_ARGS='--latency' ./perf.sh tanhf
GNU libc version: 2.31
GNU libc release: stable
40.658
109.582
66.568
```

Differential Revision: https://reviews.llvm.org/D130780
2022-08-01 22:43:00 +02:00
Kirill Okhotnikov
a7f55f0805 [libc][math] Added sinhf function.
Differential Revision: https://reviews.llvm.org/D129278
2022-07-29 17:20:53 +02:00