182 Commits

Author SHA1 Message Date
Nick Desaulniers
330793c91d
[libc] fix clang-tidy llvm-header-guard warnings (#82679)
Towards the goal of getting `ninja libc-lint` back to green, fix the numerous
instances of:

    warning: header guard does not follow preferred style [llvm-header-guard]

This is because many of our header guards start with `__LLVM` rather than
`LLVM`.

To filter just these warnings:

    $ ninja -k2000 libc-lint 2>&1 | grep llvm-header-guard

To automatically apply fixits:

    $ find libc/src libc/include libc/test -name \*.h | \
        xargs -n1 -I {} clang-tidy {} -p build/compile_commands.json \
        -checks='-*,llvm-header-guard' --fix --quiet

Some manual cleanup is still necessary as headers that were missing header
guards outright will have them inserted before the license block (we prefer
them after).
2024-02-28 12:53:56 -08:00
Nick Desaulniers
06bcd9da16
[libc][stdbit] implement stdc_has_single_bit (C23) (#83168) 2024-02-27 13:45:37 -08:00
Joseph Huber
fd42044649
[libc] Re-Enable GPU tests and fix math exception handling (#83172)
Summary:
A lot of these tests failed previously and were disabled. However we
have fixed some things since then and many of these seem to pass.
Additionally, the last remaining math tests that failed seemed to be due
to the exception handling. For now we just set it to be 'errno'.

These pass locally when tested on a gfx1030, gfx90a, and sm_89
architecture. Hopefully these pass correctly on the sm_60 bot as I've
had things fail on that one only before.
2024-02-27 13:51:01 -06:00
lntue
aa95aa69b9
[libc][math][c23] Add C23 math functions ilogbf128, logbf128, and llogb(f|l|f128). (#82144) 2024-02-27 12:23:19 -05:00
Nick Desaulniers
d8406d48a8
[libc][stdbit] implement stdc_count_ones (C23) (#82444) 2024-02-26 09:25:24 -08:00
Nick Desaulniers
f70d5c0bc8
[libc][stdbit] implement stdc_count_zeros (C23) (#82437) 2024-02-26 09:23:15 -08:00
Petr Hosek
5bd0c44bd0
[libc] Match the names of BSD sys/queue.h member names (#82696)
While these names are technically internal implemenetation detail,
there's an existing code which relies on these details and using
different names makes LLVM libc implementation incompatible. Since our
goal is for LLVM libc to be a drop in replacement, use the same name as
BSD sys/queue.h version.
2024-02-22 15:22:49 -08:00
Schrodinger ZHU Yifan
96c5b8cbd1
[libc][c23] add definitions for stdckdint.h (#82059)
See docs at
- https://gustedt.gitlabpages.inria.fr/c23-library/#stdckdint
- https://www.open-std.org/jtc1/sc22/wg14/www/docs/n3047.pdf (Ch7.10)

Compiler header:
-
450462cbac/clang/lib/Headers/stdckdint.h
- New version of GCC
(cd503b0616/gcc/ginclude/stdckdint.h)
also provides this.
2024-02-20 11:20:15 -05:00
lntue
95e8a7c863
[libc][stdfix] Use __FRACT_FBIT__ for fixed point support detection instead of compiler version. (#81820)
Now that `__FRACT_FBIT__` and related macros are added in
https://github.com/llvm/llvm-project/pull/81207, it is safer to use them
for fixed point support detection, so that codes that do not use fixed
point types will not fail to build when `-ffixed-point` is missing.
2024-02-15 07:26:30 -06:00
lntue
1301bc46ae
[libc] Add is_fixed_point type trait. (#81263) 2024-02-14 14:44:09 -05:00
Nick Desaulniers
6297479ff0
[libc][stdbit] implement stdc_first_trailing_one (C23) (#81768) 2024-02-14 11:10:48 -08:00
Nick Desaulniers
16e7d6842e
[libc][stdbit] implement stdc_first_trailing_zero (C23) (#81526) 2024-02-14 10:34:28 -08:00
Nick Desaulniers
60596716c2
[libc][stdbit] implement stdc_first_leading_one (C23) (#81502) 2024-02-14 09:16:00 -08:00
lntue
84277fe90f
[libc][stdfix] Generate stdfix.h header with fixed point precision macros according to ISO/IEC TR 18037:2008 standard, and add fixed point type support detection. (#81255)
Fixed point extension standard:
https://standards.iso.org/ittf/PubliclyAvailableStandards/c051126_ISO_IEC_TR_18037_2008.zip
2024-02-13 16:48:14 -05:00
Nick Desaulniers
d2d6b368a1
[libc][stdbit] implement stdc_first_leading_zero (C23) (#81340) 2024-02-12 08:31:53 -08:00
Nick Desaulniers
7760006d8d
[libc][stdbit] implement stdc_trailing_ones (C23) (#80459) 2024-02-07 02:56:24 -08:00
Schrodinger ZHU Yifan
ca1da36aec
[libc] add inttypes macros (#80726)
Standard file:
https://pubs.opengroup.org/onlinepubs/9699919799.2018edition/

Notice that we are not quite the same as other implementations:
1. MUSL: https://github.com/bminor/musl/blob/master/include/inttypes.h
2. GLIBC:
bbd248ac0d/stdlib/inttypes.h (L57)
3. CheriBSD:
698d1636dd/sys/arm64/include/_inttypes.h

fixes #80186
2024-02-06 09:35:56 -05:00
Nick Desaulniers
d5a3de4aee
[libc][stdbit] implement stdc_trailing_zeros (C23) (#80344) 2024-02-06 06:27:03 -08:00
Nick Desaulniers
edbd93d370
[libc][stdbit] fix return types (#80337)
All of the functions I've previously implemented return an unsigned int; not
the parameter type.
2024-02-01 13:09:33 -08:00
Nick Desaulniers
6a3fde6d60
[libc] implement stdc_leading_ones (C23) (#80082) 2024-02-01 10:14:22 -08:00
Nick Desaulniers
5a7a8f73c7
[libc] fix type generic stdc_leading_zeros for GCC (#79917)
GCC does not support _Generic in C++ mode. Use inline function definitions with
function overloading in __cplusplus mode.
2024-01-29 16:40:03 -08:00
Nick Desaulniers
7bc5eaa02f
[libc] implement stdc_leading_zeros_u* for stdbit.h (#79669)
- stdbit.stdc_leading_zeros_uc
- stdbit.stdc_leading_zeros_us
- stdbit.stdc_leading_zeros_ui
- stdbit.stdc_leading_zeros_ul
- stdbit.stdc_leading_zeros_ull

Test via:
$ ninja libc-stdbit-tests libc_include_tests
2024-01-29 15:47:55 -08:00
Schrodinger ZHU Yifan
2e1e27c7c3
[libc] adjust linux's mman.h definitions (#79652)
Use definitions from `<linux/mman.h>` to dispatch arch-specific flag
values.
For example, `MCL_CURRENT/MCL_FUTURE/MCL_ONFAULT` are different on
different architectures.
2024-01-26 19:29:28 -05:00
lntue
e9355b1b3e
[libc] Use __SIZEOF_LONG__ to define LONG_WIDTH instead of sizeof(long). (#79391)
The standard requires `limits.h` constants to be used in preprocessors.
So we use `__SIZEOF_LONG__` instead of `sizeof(long)` to define
`LONG_WIDTH`. The macro `__SIZEOF_LONG__` seems to be available on both
clang and gcc since at least version 9.
2024-01-24 20:02:03 -05:00
lntue
3a92b20cb0
[libc] Add backup definition for LONG_WIDTH in limits-macros.h. (#79375) 2024-01-24 17:04:11 -05:00
lntue
72ce629415
[libc] Add C23 limits.h header. (#78887) 2024-01-24 16:08:56 -05:00
Petr Hosek
c0a74ad90c
[libc] Use QUEUE_TYPEOF in STAILQ_LAST (#79011)
This is to ensure this macro is compatible with both C and C++.
2024-01-22 10:25:06 -08:00
lntue
ce8fcad5f4
[libc] Fix float.h header to include the system float.h first and add more definitions. (#78857) 2024-01-20 14:44:51 -05:00
lntue
c80d68a676
[libc] Add float.h header. (#78737) 2024-01-19 12:04:34 -05:00
Petr Hosek
dbc0955314
[libc] Provide sys/queue.h (#78081)
This header first appeared in 4.4BSD and is provided by a number of C
libraries including Newlib. Several of our embedded projects use this
header and so to make LLVM libc a drop-in replacement, we need to
provide it as well.

For the initial commit, we only implement singly linked variants (SLIST
and STAILQ). The doubly linked variants (LIST, TAILQ and CIRCLEQ) can be
implemented in the future as needed.
2024-01-18 21:45:58 -08:00
Nick Desaulniers
eaa11526c8
[libc] fix -Wmacro-redefined (#75261)
When building with compiler-rt enabled, warnings such as the following
are
observed:


llvm-project/llvm/build/projects/compiler-rt/../libc/include/llvm-libc-macros/linux/sys-stat-macros.h:46:9:
    warning: 'S_IXOTH' macro redefined [-Wmacro-redefined]
    #define S_IXOTH 00001
            ^

llvm-project/llvm/build/projects/compiler-rt/../libc/include/llvm-libc-macros/linux/fcntl-macros.h:61:9:
    note: previous definition is here
    #define S_IXOTH 01
            ^
It looks like we have these multiply defined. Deduplicate these flags;
users
should expect to find them in sys/stat.h. S_FIFO was wrong anyways
(should
have been S_IFIFO).
2023-12-13 08:39:39 -08:00
alfredfo
74b0465fe9
[libc] Add simple features.h with implementation macro (#69402)
In the future this should probably be autogenerated so it defines
library version.

See: Discussion in #libc
https://discord.com/channels/636084430946959380/636732994891284500/1163979080979460176
2023-10-19 04:08:13 +02:00
Jeff Bailey
c618e13161
[libc] Pull more definitions from linux/stat.h (#67071)
For file handling, we need more definitions from
linux/stat.h, so this pulls them in. It also adjusts other definitions
to match the kernel's exactly [NFC] so that it's easy to verify that
there's been no divergence one day when it's time to use linux/stat.h
directly.

Tested:
check-libc
2023-09-21 22:34:54 -07:00
Joseph Huber
ef169f5707
[libc] Improve the implementation of the rand() function (#66131)
Summary:
This patch improves the implementation of the standard `rand()` function
by implementing it in terms of the xorshift64star pRNG as described in
https://en.wikipedia.org/wiki/Xorshift#xorshift*. This is a good,
general purpose random number generator that is sufficient for most
applications that do not require an extremely long period. This patch
also correctly initializes the seed to be `1` as described by the
standard. We also increase the `RAND_MAX` value to be `INT_MAX` as the
standard only specifies that it can be larger than 32768.
2023-09-12 16:52:20 -05:00
Joseph Huber
20f4f295da [libc] Fix 'clock()' testing on the GPU
Summary:
We should check for the GPU architectures first, since `__linux__` can
be set potentially during these compilations. Also the test needs to be
a hermetic test.
2023-08-30 16:58:11 -05:00
Joseph Huber
30307a7bb7 [libc] Implement the 'clock()' function on the GPU
This patch implements the `clock()` function on the GPU. This function
is supposed to return a timestamp that can be converted into seconds
using the `CLOCKS_PER_SEC` macro. The GPU has a fixed frequency timer
that can be used for this purpose. However, there are some
considerations.

First is that AMDGPU does not have a statically known fixed frequency. I
know internally that the gfx10xx and gfx11xx series use a 100 MHz clock
which will probably remain for the future. Gfx9xx typically uses a 25
MHz clock except for the Vega 10 GPU. The only way to know for sure is
to look it up from the runtime. For this purpose, I elected to default
it to some known values and assign these to an exteranlly visible symbol
that can be initialized if needed. If we do not have a good guess we
just return zero.

Second is that the `CLOCKS_PER_SEC` macro only gives about a microsecond
of resolution. POSIX demands that it's 1,000,000 so it's best that we
keep with this tradition as almost all targets seem to respect this. The
reason this is important is because on the GPU we will almost assuredly
be copying the host's macro value (see the wrapper header) so we should
go with the POSIX version that's most likely to be set. (We could
probably make a warning if the included header doesn't match the
expected value).

Reviewed By: jdoerfert

Differential Revision: https://reviews.llvm.org/D159118
2023-08-30 16:16:34 -05:00
Joseph Huber
6a6f3c99a2 [libc] Remove hard dependencies on linux from macros headers
Currently all of this logic expects to include and link with the headers
in the `linux/` directory. This patch adds a wrapper macro to optionally
add the appropriate dependency if it exists. This is preliminary to
adding some GPU specific versions of these files.

Reviewed By: lntue

Differential Revision: https://reviews.llvm.org/D159021
2023-08-29 11:38:34 -05:00
Alfred Persson Forsberg
d3e045934a
Revert "[libc] Add limits.h"
limits.h currently interferes with Clang's limits.h. include_next
emits a warning because it is a GNU extension. Will re add this once
we figure out a good solution.

This reverts commits 13bbca8d694b4ef2ad03ec1805f07da8640c1a31,
002cba03298a7e8f0c1cadcb7996379e9ea5feff, and
0fb3066873fdfbe28dbb6409c58a8bff7cf208c6.
2023-08-17 06:21:50 +02:00
Alfred Persson Forsberg
13bbca8d69
[libc] [NFC] explain compiler macros in limits.h
Differential Revision: https://reviews.llvm.org/D158128
2023-08-17 01:23:30 +02:00
Alfred Persson Forsberg
002cba0329 [libc] limits.h: include compiler limits if not already included
The LLVM-libc build itself will override include paths and prefer it's
own limits.h over the compiler's limits.h. Because we rely on the
compiler limits.h for numerical limits in LLVM-libc it needs to be
include_next:ed if not already included. The other method to work
around this is to define all numeric macros in place.

Signed-off-by: Alfred Persson Forsberg <cat@catcream.org>

Reviewed By: thesamesam

Differential Revision: https://reviews.llvm.org/D158040
2023-08-16 02:59:21 +01:00
Alfred Persson Forsberg
0fb3066873 [libc] Add limits.h
This header contains implementation specific constants.

The compiler already provides its own limits.h with numerical limits
conforming to freestanding ISO C. But it is missing extensions like
POSIX, and does for example not include <linux/limits.h> which is
expected on a Linux system, therefore, an LLVM libc implementation of
limits.h is needed for hosted (__STDC_HOSTED__) environments.

Reviewed By: michaelrj

Differential Revision: https://reviews.llvm.org/D156961
2023-08-14 01:35:44 +01:00
Mikhail R. Gadelha
c9783d2bda [libc] Add support to compile some syscalls on 32 bit platform
This patch adds a bunch of ifdefs to handle the 32 bit versions of
some syscalls, which often only append a 64 to the name of the syscall
(with exception of SYS_lseek -> SYS_llseek and SYS_futex ->
SYS_futex_time64)

This patch also tries to handle cases where wait4 is not available
(as in riscv32): to implement wait, wait4 and waitpid when wait4 is
not available, we check for alternative wait calls and ultimately rely
on waitid to implement them all.

In riscv32, only waitid is available, so we need it to support this
platform.

Reviewed By: michaelrj

Differential Revision: https://reviews.llvm.org/D148371
2023-08-03 10:08:01 -03:00
Alfred Persson Forsberg
cae84d8acf [libc] Correct usage of __unix__ and __linux__
Reviewed By: michaelrj, thesamesam

Differential Revision: https://reviews.llvm.org/D153729
2023-07-03 01:08:15 +01:00
Tue Ly
055be3c30c [libc] Enable hermetic floating point tests again.
Fixing an issue with LLVM libc's fenv.h defined rounding mode macros
differently from system libc, making get_round() return different values from
fegetround().  Also letting math tests to skip rounding modes that cannot be
set.  This should allow math tests to be run on platforms in which fenv.h is not
implemented yet.

This allows us to re-enable hermatic floating point tests in
https://reviews.llvm.org/D151123 and reverting https://reviews.llvm.org/D152742.

Reviewed By: jhuber6

Differential Revision: https://reviews.llvm.org/D152873
2023-06-14 10:53:35 -04:00
Siva Chandra Reddy
4f1fe19df3 [libc] Make ErrnoSetterMatcher handle logging floating point values.
Along the way, couple of additional things have been done:

1. Move `ErrnoSetterMatcher.h` to `test/UnitTest` as all other matchers live
   there now.
2. `ErrnoSetterMatcher` ignores matching `errno` on GPUs.

Reviewed By: jhuber6

Differential Revision: https://reviews.llvm.org/D151129
2023-05-26 06:24:51 +00:00
Michael Jones
ee17fd7d46 [libc] add socket function
This patch adds the function "socket" from the header "sys/socket". It's
a simple syscall wrapper, and I plan on adding the related functions in
a followup patch.

Reviewed By: sivachandra

Differential Revision: https://reviews.llvm.org/D149622
2023-05-03 11:01:11 -07:00
Noah Goldstein
0432b85d8e [LIBC] Implement remainder of posix 'sched.h' minus SCHED_SPORADIC
Includes macros:
    linux/SCHED_OTHER // posix req
    linux/SCHED_FIFO // posix req
    linux/SCHED_RR // posix req
    linux/SCHED_BATCH
    linux/SCHED_ISO
    linux/SCHED_IDLE
    linux/SCHED_DEADLINE

Includes types:
    struct sched_param { int sched_priority; }

Includes functions:
    sched_setparam
    sched_getparam
    sched_setscheduler
    sched_getscheduler
    sched_get_priority_max
    sched_get_priority_min
    sched_rr_get_interval

Reviewed By: michaelrj

Differential Revision: https://reviews.llvm.org/D148069
2023-04-20 14:53:41 -05: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
e9be85da8b [libc] Add fenv_t and signal macros in riscv
This patch now enables full build.

Reviewed By: sivachandra

Differential Revision: https://reviews.llvm.org/D145594
2023-03-08 17:31:58 -03: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