Make quality improvements

- 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
This commit is contained in:
Justine Tunney 2024-02-25 14:57:28 -08:00
parent af8f2bd19f
commit 592f6ebc20
No known key found for this signature in database
GPG key ID: BE714B4575D6E328
122 changed files with 6305 additions and 3859 deletions

View file

@ -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;
}