Skip to content

Commit

Permalink
Make quality improvements
Browse files Browse the repository at this point in the history
- Write some more unit tests
- memcpy() on ARM is now faster
- Address the Musl complex math FIXME comments
- Some libm funcs like pow() now support setting errno
- Import the latest and greatest math functions from ARM
- Use more accurate atan2f() and log1pf() implementations
- atoi() and atol() will no longer saturate or clobber errno
  • Loading branch information
jart committed Feb 26, 2024
1 parent af8f2bd commit 592f6eb
Show file tree
Hide file tree
Showing 122 changed files with 6,292 additions and 3,846 deletions.
3 changes: 2 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ endif
.UNVEIL += \
libc/integral \
libc/stdbool.h \
rwc:/dev/shm \
rwc:/dev/shm \
rx:.cosmocc \
rx:build/bootstrap \
r:build/portcosmo.h \
Expand Down Expand Up @@ -297,6 +297,7 @@ include third_party/nsync/testing/BUILD.mk
include libc/testlib/BUILD.mk
include tool/viz/lib/BUILD.mk
include tool/args/BUILD.mk
include test/math/BUILD.mk
include test/posix/BUILD.mk
include test/libcxx/BUILD.mk
include test/tool/args/BUILD.mk
Expand Down
1 change: 0 additions & 1 deletion build/definitions.mk
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,6 @@ DEFAULT_CCFLAGS += \
DEFAULT_COPTS ?= \
-fno-ident \
-fno-common \
-fno-math-errno \
-fno-gnu-unique \
-fstrict-aliasing \
-fstrict-overflow \
Expand Down
40 changes: 12 additions & 28 deletions libc/fmt/atoi.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,44 +16,28 @@
│ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │
│ PERFORMANCE OF THIS SOFTWARE. │
╚─────────────────────────────────────────────────────────────────────────────*/
#include "libc/errno.h"
#include "libc/fmt/conv.h"
#include "libc/limits.h"
#include "libc/stdckdint.h"
#include "libc/str/str.h"

/**
* Decodes decimal integer from ASCII string.
* Turns string into int.
*
* atoi 10⁸ 22𝑐 7𝑛𝑠
* strtol 10⁸ 37𝑐 12𝑛𝑠
* strtoul 10⁸ 35𝑐 11𝑛𝑠
* wcstol 10⁸ 30𝑐 10𝑛𝑠
* wcstoul 10⁸ 30𝑐 10𝑛𝑠
* strtoimax 10⁸ 80𝑐 26𝑛𝑠
* strtoumax 10⁸ 78𝑐 25𝑛𝑠
* wcstoimax 10⁸ 77𝑐 25𝑛𝑠
* wcstoumax 10⁸ 76𝑐 25𝑛𝑠
* Decimal is the only radix supported. Leading whitespace (as specified
* by the isspace() function) is skipped over. Unlike strtol(), the atoi
* function has undefined behavior on error and it never changes `errno`
*
* @param s is a non-null nul-terminated string
* @param nptr is a non-null nul-terminated string
* @return the decoded signed saturated integer
* @raise ERANGE on overflow
*/
int atoi(const char *s) {
int atoi(const char *nptr) {
int x, c, d;
do c = *s++;
while (c == ' ' || c == '\t');
do c = *nptr++;
while (isspace(c));
d = c == '-' ? -1 : 1;
if (c == '-' || c == '+') c = *s++;
for (x = 0; isdigit(c); c = *s++) {
if (ckd_mul(&x, x, 10) || ckd_add(&x, x, (c - '0') * d)) {
errno = ERANGE;
if (d > 0) {
return INT_MAX;
} else {
return INT_MIN;
}
}
if (c == '-' || c == '+') c = *nptr++;
for (x = 0; isdigit(c); c = *nptr++) {
x *= 10;
x += (c - '0') * d;
}
return x;
}
31 changes: 13 additions & 18 deletions libc/fmt/atol.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,34 +16,29 @@
│ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │
│ PERFORMANCE OF THIS SOFTWARE. │
╚─────────────────────────────────────────────────────────────────────────────*/
#include "libc/errno.h"
#include "libc/fmt/conv.h"
#include "libc/limits.h"
#include "libc/stdckdint.h"
#include "libc/str/str.h"

/**
* Decodes decimal integer from ASCII string.
* Turns string into long.
*
* @param s is a non-null nul-terminated string
* Decimal is the only radix supported. Leading whitespace (as specified
* by the isspace() function) is skipped over. Unlike strtol(), the atoi
* function has undefined behavior on error and it never changes `errno`
*
* @param nptr is a non-null nul-terminated string
* @return the decoded signed saturated integer
*/
long atol(const char *s) {
long atol(const char *nptr) {
long x;
int c, d;
do c = *s++;
while (c == ' ' || c == '\t');
do c = *nptr++;
while (isspace(c));
d = c == '-' ? -1 : 1;
if (c == '-' || c == '+') c = *s++;
for (x = 0; isdigit(c); c = *s++) {
if (ckd_mul(&x, x, 10) || ckd_add(&x, x, (c - '0') * d)) {
errno = ERANGE;
if (d > 0) {
return LONG_MAX;
} else {
return LONG_MIN;
}
}
if (c == '-' || c == '+') c = *nptr++;
for (x = 0; isdigit(c); c = *nptr++) {
x *= 10;
x += (c - '0') * d;
}
return x;
}
Expand Down
2 changes: 1 addition & 1 deletion libc/intrin/aarch64/memchr.S
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
╚──────────────────────────────────────────────────────────────────────────────╝
│ │
│ Optimized Routines │
│ Copyright (c) 1999-2022, Arm Limited. │
│ Copyright (c) 2018-2024, Arm Limited. │
│ │
│ Permission is hereby granted, free of charge, to any person obtaining │
│ a copy of this software and associated documentation files (the │
Expand Down
62 changes: 34 additions & 28 deletions libc/intrin/aarch64/memcpy.S
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
╚──────────────────────────────────────────────────────────────────────────────╝
│ │
│ Optimized Routines │
│ Copyright (c) 1999-2022, Arm Limited. │
│ Copyright (c) 2018-2024, Arm Limited. │
│ │
│ Permission is hereby granted, free of charge, to any person obtaining │
│ a copy of this software and associated documentation files (the │
Expand Down Expand Up @@ -80,11 +80,12 @@ ENTRY (__memcpy_aarch64_simd)
PTR_ARG (1)
SIZE_ARG (2)
add srcend, src, count
add dstend, dstin, count
cmp count, 128
b.hi L(copy_long)
add dstend, dstin, count
cmp count, 32
b.hi L(copy32_128)
nop

/* Small copies: 0..32 bytes. */
cmp count, 16
Expand All @@ -95,6 +96,18 @@ ENTRY (__memcpy_aarch64_simd)
str B_q, [dstend, -16]
ret

.p2align 4
/* Medium copies: 33..128 bytes. */
L(copy32_128):
ldp A_q, B_q, [src]
ldp C_q, D_q, [srcend, -32]
cmp count, 64
b.hi L(copy128)
stp A_q, B_q, [dstin]
stp C_q, D_q, [dstend, -32]
ret

.p2align 4
/* Copy 8-15 bytes. */
L(copy16):
tbz count, 3, L(copy8)
Expand All @@ -104,7 +117,6 @@ L(copy16):
str A_h, [dstend, -8]
ret

.p2align 3
/* Copy 4-7 bytes. */
L(copy8):
tbz count, 2, L(copy4)
Expand All @@ -114,31 +126,6 @@ L(copy8):
str B_lw, [dstend, -4]
ret

/* Copy 0..3 bytes using a branchless sequence. */
L(copy4):
cbz count, L(copy0)
lsr tmp1, count, 1
ldrb A_lw, [src]
ldrb C_lw, [srcend, -1]
ldrb B_lw, [src, tmp1]
strb A_lw, [dstin]
strb B_lw, [dstin, tmp1]
strb C_lw, [dstend, -1]
L(copy0):
ret

.p2align 4
/* Medium copies: 33..128 bytes. */
L(copy32_128):
ldp A_q, B_q, [src]
ldp C_q, D_q, [srcend, -32]
cmp count, 64
b.hi L(copy128)
stp A_q, B_q, [dstin]
stp C_q, D_q, [dstend, -32]
ret

.p2align 4
/* Copy 65..128 bytes. */
L(copy128):
ldp E_q, F_q, [src, 32]
Expand All @@ -152,8 +139,24 @@ L(copy96):
stp C_q, D_q, [dstend, -32]
ret

/* Copy 0..3 bytes using a branchless sequence. */
L(copy4):
cbz count, L(copy0)
lsr tmp1, count, 1
ldrb A_lw, [src]
ldrb C_lw, [srcend, -1]
ldrb B_lw, [src, tmp1]
strb A_lw, [dstin]
strb B_lw, [dstin, tmp1]
strb C_lw, [dstend, -1]
L(copy0):
ret

.p2align 3
/* Copy more than 128 bytes. */
L(copy_long):
add dstend, dstin, count

/* Use backwards copy if there is an overlap. */
sub tmp1, dstin, src
cmp tmp1, count
Expand Down Expand Up @@ -190,6 +193,9 @@ L(copy64_from_end):
stp A_q, B_q, [dstend, -32]
ret

.p2align 4
nop

/* Large backwards copy for overlapping copies.
Copy 16 bytes and then align srcend to 16-byte alignment. */
L(copy_long_backwards):
Expand Down
2 changes: 1 addition & 1 deletion libc/intrin/aarch64/memrchr.S
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
╚──────────────────────────────────────────────────────────────────────────────╝
│ │
│ Optimized Routines │
│ Copyright (c) 1999-2022, Arm Limited. │
│ Copyright (c) 2018-2024, Arm Limited. │
│ │
│ Permission is hereby granted, free of charge, to any person obtaining │
│ a copy of this software and associated documentation files (the │
Expand Down
2 changes: 1 addition & 1 deletion libc/intrin/aarch64/memset.S
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
╚──────────────────────────────────────────────────────────────────────────────╝
│ │
│ Optimized Routines │
│ Copyright (c) 1999-2022, Arm Limited. │
│ Copyright (c) 2018-2024, Arm Limited. │
│ │
│ Permission is hereby granted, free of charge, to any person obtaining │
│ a copy of this software and associated documentation files (the │
Expand Down
2 changes: 1 addition & 1 deletion libc/intrin/aarch64/stpcpy.S
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
╚──────────────────────────────────────────────────────────────────────────────╝
│ │
│ Optimized Routines │
│ Copyright (c) 1999-2022, Arm Limited. │
│ Copyright (c) 2018-2024, Arm Limited. │
│ │
│ Permission is hereby granted, free of charge, to any person obtaining │
│ a copy of this software and associated documentation files (the │
Expand Down
2 changes: 1 addition & 1 deletion libc/intrin/aarch64/strchr.S
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
╚──────────────────────────────────────────────────────────────────────────────╝
│ │
│ Optimized Routines │
│ Copyright (c) 1999-2022, Arm Limited. │
│ Copyright (c) 2018-2024, Arm Limited. │
│ │
│ Permission is hereby granted, free of charge, to any person obtaining │
│ a copy of this software and associated documentation files (the │
Expand Down
2 changes: 1 addition & 1 deletion libc/intrin/aarch64/strchrnul.S
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
╚──────────────────────────────────────────────────────────────────────────────╝
│ │
│ Optimized Routines │
│ Copyright (c) 1999-2022, Arm Limited. │
│ Copyright (c) 2018-2024, Arm Limited. │
│ │
│ Permission is hereby granted, free of charge, to any person obtaining │
│ a copy of this software and associated documentation files (the │
Expand Down
2 changes: 1 addition & 1 deletion libc/intrin/aarch64/strcmp.S
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
╚──────────────────────────────────────────────────────────────────────────────╝
│ │
│ Optimized Routines │
│ Copyright (c) 1999-2022, Arm Limited. │
│ Copyright (c) 2018-2024, Arm Limited. │
│ │
│ Permission is hereby granted, free of charge, to any person obtaining │
│ a copy of this software and associated documentation files (the │
Expand Down
2 changes: 1 addition & 1 deletion libc/intrin/aarch64/strcpy.S
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
╚──────────────────────────────────────────────────────────────────────────────╝
│ │
│ Optimized Routines │
│ Copyright (c) 1999-2022, Arm Limited. │
│ Copyright (c) 2018-2024, Arm Limited. │
│ │
│ Permission is hereby granted, free of charge, to any person obtaining │
│ a copy of this software and associated documentation files (the │
Expand Down
2 changes: 1 addition & 1 deletion libc/intrin/aarch64/strlen.S
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
╚──────────────────────────────────────────────────────────────────────────────╝
│ │
│ Optimized Routines │
│ Copyright (c) 1999-2022, Arm Limited. │
│ Copyright (c) 2018-2024, Arm Limited. │
│ │
│ Permission is hereby granted, free of charge, to any person obtaining │
│ a copy of this software and associated documentation files (the │
Expand Down
2 changes: 1 addition & 1 deletion libc/intrin/aarch64/strncmp.S
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
╚──────────────────────────────────────────────────────────────────────────────╝
│ │
│ Optimized Routines │
│ Copyright (c) 1999-2022, Arm Limited. │
│ Copyright (c) 2018-2024, Arm Limited. │
│ │
│ Permission is hereby granted, free of charge, to any person obtaining │
│ a copy of this software and associated documentation files (the │
Expand Down
2 changes: 1 addition & 1 deletion libc/intrin/aarch64/strnlen.S
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
╚──────────────────────────────────────────────────────────────────────────────╝
│ │
│ Optimized Routines │
│ Copyright (c) 1999-2022, Arm Limited. │
│ Copyright (c) 2018-2024, Arm Limited. │
│ │
│ Permission is hereby granted, free of charge, to any person obtaining │
│ a copy of this software and associated documentation files (the │
Expand Down
2 changes: 1 addition & 1 deletion libc/intrin/aarch64/strrchr.S
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
╚──────────────────────────────────────────────────────────────────────────────╝
│ │
│ Optimized Routines │
│ Copyright (c) 1999-2022, Arm Limited. │
│ Copyright (c) 2018-2024, Arm Limited. │
│ │
│ Permission is hereby granted, free of charge, to any person obtaining │
│ a copy of this software and associated documentation files (the │
Expand Down
2 changes: 2 additions & 0 deletions libc/intrin/fbclibm.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
__notice(freebsd_complex_notice, "FreeBSD Complex Math (BSD-2 License)\n\
Copyright (c) 2012 Stephen Montgomery-Smith <stephen@FreeBSD.ORG>");
Loading

0 comments on commit 592f6eb

Please sign in to comment.