Commit graph

117 commits

Author SHA1 Message Date
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
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
Kirill Okhotnikov
b8e8012aa2 [libc][math] fmod/fmodf implementation.
This is a implementation of find remainder fmod function from standard libm.
The underline algorithm is developed by myself, but probably it was first
invented before.
Some features of the implementation:
1. The code is written on more-or-less modern C++.
2. One general implementation for both float and double precision numbers.
3. Spitted platform/architecture dependent and independent code and tests.
4. Tests covers 100% of the code for both float and double numbers. Tests cases with NaN/Inf etc is copied from glibc.
5. The new implementation in general 2-4 times faster for “regular” x,y values. It can be 20 times faster for x/y huge value, but can also be 2 times slower for double denormalized range (according to perf tests provided).
6. Two different implementation of division loop are provided. In some platforms division can be very time consuming operation. Depend on platform it can be 3-10 times slower than multiplication.

Performance tests:

The test is based on core-math project (https://gitlab.inria.fr/core-math/core-math). By Tue Ly suggestion I took hypot function and use it as template for fmod. Preserving all test cases.

`./check.sh <--special|--worst> fmodf` passed.
`CORE_MATH_PERF_MODE=rdtsc ./perf.sh fmodf` results are

```
GNU libc version: 2.35
GNU libc release: stable
21.166 <-- FPU
51.031 <-- current glibc
37.659 <-- this fmod version.
```
2022-06-24 23:09:14 +02:00
Siva Chandra Reddy
5db4177817 [libc] Add pthread_detach and thrd_detach.
Tests for pthread_detach and thrd_detach have not been added. Instead, a
test for the underlying implementation has been added as it makes use of
an internal wait method to synchronize with detached threads.

Reviewed By: lntue, michaelrj

Differential Revision: https://reviews.llvm.org/D127479
2022-06-11 05:29:40 +00:00
Michael Jones
6ce490e5a6 [libc] add buffering to FILE writes
Previously all FILE objects were fully buffered, this patch adds line
buffering and unbuffered output, as well as applying them to stdout and
stderr.

Reviewed By: sivachandra

Differential Revision: https://reviews.llvm.org/D126829
2022-06-10 09:58:46 -07:00
Siva Chandra Reddy
70c8d12b79 [libc] Add pthread_create and pthread_join functions.
They do not yet support all the feature/attributes in pthread_attr_t.
Future changes will add such support.

Reviewed By: lntue

Differential Revision: https://reviews.llvm.org/D126718
2022-06-02 01:47:24 +00:00
Siva Chandra Reddy
9b8ca3c1f1 [libc] Add global stdout and stderr objects.
They are added as entrypoint object targets. The header-gen
infrastructure has been extended to enable handling standard required
global objects. The libc-api-test has also been extended to verify the
global object declarations.

Reviewed By: lntue

Differential Revision: https://reviews.llvm.org/D126329
2022-05-27 05:43:49 +00:00
Siva Chandra Reddy
2a5d5078d5 [libc] Add the pthread_mutex_t type.
Simple implementations of the functions pthread_mutex_init,
pthread_mutex_destroy, pthread_mutex_lock and pthread_mutex_unlock have
have also been added. Future patches will extend these functions to add
features required by the POSIX specification.

Reviewed By: lntue

Differential Revision: https://reviews.llvm.org/D126235
2022-05-24 22:48:14 +00:00
Alex Brachet
b1183305f8 [libc] Add strlcat
Differential Revision: https://reviews.llvm.org/D125978
2022-05-19 21:48:39 +00:00
Michael Jones
f8ae591fc9 [libc] fix missing semicolon in bsd_ext.td
Fix typo in previous commit

Differential Revision: https://reviews.llvm.org/D125913
2022-05-18 11:22:26 -07:00
Alex Brachet
6adbcd2b10 [libc] Add String to bsd headers 2022-05-18 18:01:02 +00:00
Alex Brachet
fc2c8b2371 [libc] Add strlcpy
Differential Revision: https://reviews.llvm.org/D125806
2022-05-18 17:45:05 +00:00
Michael Jones
9f1d905f39 [libc] add snprintf
After adding sprintf, snprintf is simple. The functions are very
similar. The tests only cover the behavior of the max length since the
sprintf tests should cover the other behavior.

Reviewed By: lntue

Differential Revision: https://reviews.llvm.org/D125826
2022-05-17 13:32:59 -07:00
Michael Jones
ff6fe39eca [libc] add sprintf
This adds the sprintf entrypoint, as well as unit tests. Currently
sprintf only supports %%, %s, and %c, but the other conversions are on
the way.

Reviewed By: sivachandra, lntue

Differential Revision: https://reviews.llvm.org/D125573
2022-05-17 11:32:20 -07:00
Siva Chandra Reddy
9db0037bf1 [libc] Add implementations of feof, ferror and clearerr.
The corresponding _unlocked functions have also been added.

Reviewed By: lntue, michaelrj

Differential Revision: https://reviews.llvm.org/D124311
2022-04-29 23:04:35 +00:00
Siva Chandra Reddy
19a6dd33ee [libc] Add the implementation of the GNU extension function fopencookie.
Reviewed By: lntue, michaelrj

Differential Revision: https://reviews.llvm.org/D124141
2022-04-22 08:02:25 +00:00
Siva Chandra Reddy
22f9dca113 [libc] Add the implementation of the fflush function.
Note that the underlying flush implementation does not yet fully implement
the POSIX standard. It is complete with respect to the C standard
however. A future change will add the POSIX behavior. It should not affect
the implementation of the fflush function however as the POSIX behavior
will be added in a lower layer.

Reviewed By: lntue

Differential Revision: https://reviews.llvm.org/D124073
2022-04-20 19:43:39 +00:00
Siva Chandra Reddy
945e0220fd [libc] Add GNU extention functions fread_unlocked and fwrite_unlocked.
POSIX locking and unlocking functions flockfile and funlockfile have
also been added. The locking is not recursive yet. A future patch will
make the underlying lock a recursive lock.

Reviewed By: lntue

Differential Revision: https://reviews.llvm.org/D123986
2022-04-20 15:39:27 +00:00
Siva Chandra Reddy
0258f56646 [libc] Add a definition of pthread_attr_t and its getters and setters.
Not all attributes have been added to phtread_attr_t in this patch. They
will be added gradually in future patches.

Reviewed By: lntue

Differential Revision: https://reviews.llvm.org/D123423
2022-04-11 16:08:49 +00:00
Siva Chandra Reddy
83f153ce34 [libc] Add pthread_mutexattr_t type and its setters and getters.
A simple implementation of the getters and setters has been added. More
logic can be added to them in future as required.

Reviewed By: michaelrj

Differential Revision: https://reviews.llvm.org/D122969
2022-04-04 18:11:12 +00:00
Siva Chandra Reddy
441606f5ff [libc] Add implementations of fopen, flose, fread, fwrite and fseek.
A follow up patch will add feof and ferror.

Reviewed By: lntue

Differential Revision: https://reviews.llvm.org/D122327
2022-03-24 04:20:12 +00:00
Michael Jones
6d0f5d95ad [libc][obvious] add aligned_alloc as entrypoint
This patch adds aligned_alloc as an entrypoint. Previously it was being
included implicitly.

Reviewed By: sivachandra

Differential Revision: https://reviews.llvm.org/D122362
2022-03-23 16:44:15 -07:00
Siva Chandra Reddy
827575a7f8 [libc] Add implementation of POSIX lseek function.
Reviewed By: lntue

Differential Revision: https://reviews.llvm.org/D121676
2022-03-15 16:24:48 +00:00
Siva Chandra Reddy
dd33f9cdef [libc] Make the errno macro resolve to the thread local variable directly.
With modern architectures having a thread pointer and language supporting
thread locals, there is no reason to use a function intermediary to access
the thread local errno value.

The entrypoint corresponding to errno has been replaced with an object
library as there is no formal entrypoint for errno anymore.

Reviewed By: jeffbailey, michaelrj

Differential Revision: https://reviews.llvm.org/D120920
2022-03-04 17:29:49 +00:00
Alex Brachet
d66983861a [libc] Add exit and atexit
Often atexit is implemented using __cxa_atexit. I have not implemented __cxa_atexit here because it potentially requires more discussion. It is unique for llvm-libc (I think) that it is an exported symbol that wouldn’t be defined in any spec file because it doesn’t have a header. Implementing it will be trivial given what is here already, but I figured it would be more contentious so it can be implemented later.

Reviewed By: lntue

Differential Revision: https://reviews.llvm.org/D119512
2022-02-17 17:21:55 +00:00
Raman Tenneti
f2a7f83595 Introduce getenv to LLVM libc
Add support for getenv as defined by the Open Group's "System Interface &
 Header" in https://pubs.opengroup.org/onlinepubs/7908799/xsh/getenv.html

getenv requires a standard way of accessing the environment,
so a pointer to the environment is added to the startup in crt1.
Consquently, this function is not usable on top of other libcs.

Added starts_with method to StringView. getenv function uses it.

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

Reviewed By: sivachandra, rtenneti

Differential Revision: https://reviews.llvm.org/D119403
2022-02-14 10:10:13 -08:00
Tue Ly
9e7688c71e [libc] Implement log1pf correctly rounded to all rounding modes.
Implement log1pf correctly rounded to all rounding modes relying on logf implementation for exponent > 2^(-8).

Reviewed By: sivachandra, zimmermann6

Differential Revision: https://reviews.llvm.org/D118962
2022-02-07 16:17:18 -05:00
Siva Chandra Reddy
b8385162c2 [libc] Add implementations of POSIX mkdir, mkdirat, rmdir, unlink and unlinkat.
Reviewed By: michaelrj

Differential Revision: https://reviews.llvm.org/D118641
2022-02-01 05:17:10 +00:00
Siva Chandra Reddy
4abfe47e1f [libc] Add implementations of the POSIX creat and openat functions.
Reviewed By: michaelrj

Differential Revision: https://reviews.llvm.org/D118435
2022-01-28 19:28:12 +00:00
Siva Chandra Reddy
dc2b01b3f7 [libc] Add POSIX close, fsync, open, read and write functions.
They are implemented as simple syscall wrappers. The file creation
macros have been put in a header file as a temporary solution until we
have a cleaner approach to listing platform relevant macros.

Reviewed By: abrachet

Differential Revision: https://reviews.llvm.org/D118396
2022-01-27 20:32:02 +00:00
Siva Chandra Reddy
07c9903a8b [libc][NFC] Let var args be treated as a named type in the spec.
The type names in the spec are just sugar used by the header generator to
generate the function prototype. Giving the VarArgType a name of "..."
allows the header generator to treat and generate the "type name" for var
arg parameters similar to how it does for any other type.
2022-01-27 17:07:21 +00:00
Tue Ly
e581841e8c [libc] Implement log10f correctly rounded for all rounding modes.
Based on RLIBM implementation similar to logf and log2f.  Most of the exceptional inputs are the exact powers of 10.

Reviewed By: sivachandra, zimmermann6, santoshn, jpl169

Differential Revision: https://reviews.llvm.org/D118093
2022-01-25 10:33:39 -05:00
Tue Ly
63d2df003e [libc] Implement correctly rounded log2f based on RLIBM library.
Implement log2f based on RLIBM library correctly rounded for all rounding modes.

Reviewed By: sivachandra, michaelrj, santoshn, jpl169, zimmermann6

Differential Revision: https://reviews.llvm.org/D115828
2022-01-14 12:40:49 -05:00
Tue Ly
d08a801b5f [libc] Implement correctly rounded logf based on RLIBM library.
Implement correctly rounded logf based on RLIBM library: https://people.cs.rutgers.edu/~sn349/rlibm/.

Reviewed By: sivachandra, santoshn, jpl169, zimmermann6

Differential Revision: https://reviews.llvm.org/D115408
2021-12-16 13:43:15 -05:00
Michael Jones
aa1902f917 [libc] add basic strtold implementation
Due to the differences between the types of long double, this function
is effectively three functions in one. This patch adds basic support for
the types of long double, although it's just using the fast path and the
fallback for the moment. I still need to implement a version of
Eisel-Lemire for performance, but the existing algorithms should be
correct.

Reviewed By: sivachandra, lntue

Differential Revision: https://reviews.llvm.org/D113710
2021-12-13 10:40:44 -08:00
Michael Jones
9b6f8b985c [libc] add stpcpy and stpncpy
Adds an implementation for stpcpy and stpncpy, which are posix extension
functions.

Reviewed By: sivachandra, lntue

Differential Revision: https://reviews.llvm.org/D111913
2021-11-04 11:39:26 -07:00
Michael Jones
3bbbec1ae7 [libc] add strndup
add an implementation of strndup

Reviewed By: sivachandra

Differential Revision: https://reviews.llvm.org/D112846
2021-11-01 12:52:51 -07:00
Michael Jones
65bb6593e5 [libc] add strdup implementation
Add an implementation for strdup.

Reviewed By: lntue, sivachandra

Differential Revision: https://reviews.llvm.org/D111584
2021-10-27 10:21:04 -07:00
Michael Jones
7dcdbabb3b [libc] add malloc funcs as external entrypoints
malloc, calloc, realloc, and free are all functions that other libc
functions depend on, but are pulled from external sources, instead of
having an internal implementation. This patch adds a way to include
functions like that as entrypoints in the list of external entrypoints,
and includes the malloc functions using this new path.

Reviewed By: sivachandra

Differential Revision: https://reviews.llvm.org/D112104
2021-10-27 10:21:01 -07:00
Michael Jones
87c016078a [libc] add atof, strtof and strtod
Add the string to floating point conversion functions.
Long doubles aren't supported yet, but floats and doubles are. The
primary algorithm used is the Eisel-Lemire ParseNumberF64 algorithm,
with the Simple Decimal Conversion algorithm as backup.

Links for more information on the algorithms:

Number Parsing at a Gigabyte per Second, Software: Practice and
Experience 51 (8), 2021 (https://arxiv.org/abs/2101.11408)
https://nigeltao.github.io/blog/2020/eisel-lemire.html
https://nigeltao.github.io/blog/2020/parse-number-f64-simple.html

Differential Revision: https://reviews.llvm.org/D109261
2021-10-18 16:10:03 -07:00
Michael Jones
db8a88fef8 [libc] add memccpy and mempcpy
Add an implementation for memccpy and mempcpy. These functions are
posix extensions for the moment.

Reviewed By: lntue

Differential Revision: https://reviews.llvm.org/D111762
2021-10-14 16:38:00 -07:00
Siva Chandra Reddy
5c3c716bb1 [libc] Add FE_DFL_ENV and handle it in fesetenv.
Reviewed By: michaelrj

Differential Revision: https://reviews.llvm.org/D110611
2021-09-28 18:09:52 +00:00
Siva Chandra Reddy
5eb6b82729 [libc] Add an implementation of qsort.
A fuzzer for qsort has also been added.

Reviewed By: michaelrj

Differential Revision: https://reviews.llvm.org/D110382
2021-09-24 19:22:45 +00:00
Siva Chandra Reddy
32a5007865 [libc] Add an implementation of bsearch.
Reviewed By: michaelrj

Differential Revision: https://reviews.llvm.org/D110222
2021-09-22 16:37:03 +00:00
Siva Chandra Reddy
74670e79b0 [libc] Add implementations of div, ldiv, lldiv and imaxdiv.
Reviewed By: michaelrj

Differential Revision: https://reviews.llvm.org/D109952
2021-09-20 04:51:42 +00:00
Siva Chandra Reddy
0da5ac1a75 [libc] Add extension functions fedisableexcept, feenableexcept and fegetexcept.
Reviewed By: michaelrj

Differential Revision: https://reviews.llvm.org/D109613
2021-09-10 18:44:53 +00:00
Siva Chandra Reddy
2f4f452f16 [libc] Add a skeleton for C standard condition variable functions.
This patch adds a skeleton as a preparatory step for the next patch which
adds the actual implementations of the condition variable functions.

Reviewed By: michaelrj

Differential Revision: https://reviews.llvm.org/D108947
2021-09-01 19:41:52 +00:00
Siva Chandra Reddy
7a2a765745 [libc] Add mtx_destroy which does nothing.
There is not cleanup to be done for the mutex type so mtx_destroy does
nothing.
2021-08-30 20:43:46 +00:00
Michael Jones
035325275c [libc] add inttypes header
Add inttypes.h to llvm libc. As its first functions strtoimax and
strtoumax are included.

Reviewed By: sivachandra

Differential Revision: https://reviews.llvm.org/D108736
2021-08-26 18:04:21 +00:00
Siva Chandra Reddy
aeee014428 [libc][Obvious] Fix llvm_libc_ext.td. 2021-08-19 18:54:43 +00:00