mirror of
https://github.com/jart/cosmopolitan.git
synced 2025-07-12 05:59:10 +00:00
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:
parent
af8f2bd19f
commit
592f6ebc20
122 changed files with 6305 additions and 3859 deletions
41
test/math/BUILD.mk
Normal file
41
test/math/BUILD.mk
Normal file
|
@ -0,0 +1,41 @@
|
|||
#-*-mode:makefile-gmake;indent-tabs-mode:t;tab-width:8;coding:utf-8-*-┐
|
||||
#── vi: set noet ft=make ts=8 sw=8 fenc=utf-8 :vi ────────────────────┘
|
||||
|
||||
PKGS += TEST_MATH
|
||||
|
||||
TEST_MATH_SRCS := $(wildcard test/math/*.c)
|
||||
TEST_MATH_SRCS_TEST = $(filter %_test.c,$(TEST_MATH_SRCS))
|
||||
TEST_MATH_OBJS = $(TEST_MATH_SRCS:%.c=o/$(MODE)/%.o)
|
||||
TEST_MATH_COMS = $(TEST_MATH_SRCS_TEST:%.c=o/$(MODE)/%.com)
|
||||
TEST_MATH_BINS = $(TEST_MATH_COMS) $(TEST_MATH_COMS:%=%.dbg)
|
||||
TEST_MATH_TESTS = $(TEST_MATH_SRCS_TEST:%.c=o/$(MODE)/%.com.ok)
|
||||
TEST_MATH_CHECKS = $(TEST_MATH_SRCS_TEST:%.c=o/$(MODE)/%.com.runs)
|
||||
|
||||
TEST_MATH_DIRECTDEPS = \
|
||||
LIBC_INTRIN \
|
||||
LIBC_RUNTIME \
|
||||
LIBC_SYSV \
|
||||
LIBC_TINYMATH \
|
||||
THIRD_PARTY_COMPILER_RT
|
||||
|
||||
TEST_MATH_DEPS := \
|
||||
$(call uniq,$(foreach x,$(TEST_MATH_DIRECTDEPS),$($(x))))
|
||||
|
||||
o/$(MODE)/test/math/math.pkg: \
|
||||
$(TEST_MATH_OBJS) \
|
||||
$(foreach x,$(TEST_MATH_DIRECTDEPS),$($(x)_A).pkg)
|
||||
|
||||
o/$(MODE)/test/math/%.com.dbg: \
|
||||
$(TEST_MATH_DEPS) \
|
||||
o/$(MODE)/test/math/%.o \
|
||||
o/$(MODE)/test/math/math.pkg \
|
||||
$(CRT) \
|
||||
$(APE_NO_MODIFY_SELF)
|
||||
@$(APELINK)
|
||||
|
||||
$(TEST_MATH_OBJS): private CFLAGS += -fno-builtin
|
||||
|
||||
.PHONY: o/$(MODE)/test/math
|
||||
o/$(MODE)/test/math: \
|
||||
$(TEST_MATH_BINS) \
|
||||
$(TEST_MATH_CHECKS)
|
92
test/math/hypot_test.c
Normal file
92
test/math/hypot_test.c
Normal file
|
@ -0,0 +1,92 @@
|
|||
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│
|
||||
│ vi: set et ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi │
|
||||
╞══════════════════════════════════════════════════════════════════════════════╡
|
||||
│ Copyright 2024 Justine Alexandra Roberts Tunney │
|
||||
│ │
|
||||
│ Permission to use, copy, modify, and/or distribute this software for │
|
||||
│ any purpose with or without fee is hereby granted, provided that the │
|
||||
│ above copyright notice and this permission notice appear in all copies. │
|
||||
│ │
|
||||
│ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL │
|
||||
│ WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED │
|
||||
│ WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE │
|
||||
│ AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL │
|
||||
│ DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR │
|
||||
│ PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER │
|
||||
│ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │
|
||||
│ PERFORMANCE OF THIS SOFTWARE. │
|
||||
╚─────────────────────────────────────────────────────────────────────────────*/
|
||||
#include <errno.h>
|
||||
#include <limits.h>
|
||||
#include <math.h>
|
||||
|
||||
#define OK 0
|
||||
|
||||
#define MAX_ERROR_ULP 1
|
||||
|
||||
#define CHECK(x) \
|
||||
if (!(x)) return __LINE__
|
||||
|
||||
#define TEST(e, x) \
|
||||
errno = 0; \
|
||||
CHECK(x); \
|
||||
CHECK(errno == e)
|
||||
|
||||
long lemur(void) {
|
||||
static unsigned __int128 s = 2131259787901769494;
|
||||
return (s *= 15750249268501108917ul) >> 64;
|
||||
}
|
||||
|
||||
int main() {
|
||||
|
||||
// test base cases
|
||||
TEST(OK, hypot(3, 4) == 5);
|
||||
TEST(OK, hypot(0, 0) == 0);
|
||||
TEST(OK, hypot(5, 12) == 13);
|
||||
TEST(OK, hypot(-5, -12) == 13);
|
||||
TEST(OK, hypot(-3, 4) == 5);
|
||||
|
||||
// test with zeros
|
||||
TEST(OK, hypot(0, 0) == 0);
|
||||
TEST(OK, hypot(0, 3) == 3);
|
||||
TEST(OK, hypot(3, 0) == 3);
|
||||
|
||||
// test with NAN
|
||||
TEST(OK, isnan(hypot(NAN, 1)));
|
||||
TEST(OK, isnan(hypot(1, NAN)));
|
||||
TEST(OK, isnan(hypot(NAN, NAN)));
|
||||
|
||||
// test with INFINITY
|
||||
TEST(OK, hypot(INFINITY, 1) == INFINITY);
|
||||
TEST(OK, hypot(1, INFINITY) == INFINITY);
|
||||
TEST(OK, hypot(-INFINITY, -INFINITY) == INFINITY);
|
||||
|
||||
// test underflow avoidance
|
||||
TEST(OK, hypot(2e-308, 3e-308) > 0);
|
||||
|
||||
// test what happens on overflow
|
||||
// TODO(jart): This should raise ERANGE.
|
||||
TEST(OK, hypot(DBL_MAX, DBL_MAX) == INFINITY);
|
||||
|
||||
// test accuracy assuming hypotl() is correct
|
||||
union {
|
||||
long i;
|
||||
double f;
|
||||
} x, y, a, b;
|
||||
int n = 1000;
|
||||
for (int i = 0; i < n; ++i) {
|
||||
for (int j = 0; j < n; ++j) {
|
||||
x.i = lemur();
|
||||
y.i = lemur();
|
||||
a.f = hypotl(x.f, y.f);
|
||||
b.f = hypot(x.f, y.f);
|
||||
if (isnan(a.f) || isnan(b.f)) {
|
||||
CHECK(isnan(a.f) == isnan(b.f));
|
||||
continue;
|
||||
}
|
||||
long e = b.i - a.i;
|
||||
if (e < 0) e = -e;
|
||||
CHECK(e <= MAX_ERROR_ULP);
|
||||
}
|
||||
}
|
||||
}
|
94
test/math/hypotf_test.c
Normal file
94
test/math/hypotf_test.c
Normal file
|
@ -0,0 +1,94 @@
|
|||
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│
|
||||
│ vi: set et ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi │
|
||||
╞══════════════════════════════════════════════════════════════════════════════╡
|
||||
│ Copyright 2024 Justine Alexandra Roberts Tunney │
|
||||
│ │
|
||||
│ Permission to use, copy, modify, and/or distribute this software for │
|
||||
│ any purpose with or without fee is hereby granted, provided that the │
|
||||
│ above copyright notice and this permission notice appear in all copies. │
|
||||
│ │
|
||||
│ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL │
|
||||
│ WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED │
|
||||
│ WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE │
|
||||
│ AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL │
|
||||
│ DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR │
|
||||
│ PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER │
|
||||
│ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │
|
||||
│ PERFORMANCE OF THIS SOFTWARE. │
|
||||
╚─────────────────────────────────────────────────────────────────────────────*/
|
||||
#include <errno.h>
|
||||
#include <limits.h>
|
||||
#include <math.h>
|
||||
|
||||
#define OK 0
|
||||
|
||||
#define MAX_ERROR_ULP 1
|
||||
|
||||
#define CHECK(x) \
|
||||
if (!(x)) return __LINE__
|
||||
|
||||
#define TEST(e, x) \
|
||||
errno = 0; \
|
||||
CHECK(x); \
|
||||
CHECK(errno == e)
|
||||
|
||||
int rando(void) {
|
||||
static unsigned long s;
|
||||
s *= 6364136223846793005;
|
||||
s += 1442695040888963407;
|
||||
return s >> 32;
|
||||
}
|
||||
|
||||
int main() {
|
||||
|
||||
// test base cases
|
||||
TEST(OK, hypotf(3, 4) == 5);
|
||||
TEST(OK, hypotf(0, 0) == 0);
|
||||
TEST(OK, hypotf(5, 12) == 13);
|
||||
TEST(OK, hypotf(-5, -12) == 13);
|
||||
TEST(OK, hypotf(-3, 4) == 5);
|
||||
|
||||
// test with zeros
|
||||
TEST(OK, hypotf(0, 0) == 0);
|
||||
TEST(OK, hypotf(0, 3) == 3);
|
||||
TEST(OK, hypotf(3, 0) == 3);
|
||||
|
||||
// test with NAN
|
||||
TEST(OK, isnan(hypotf(NAN, 1)));
|
||||
TEST(OK, isnan(hypotf(1, NAN)));
|
||||
TEST(OK, isnan(hypotf(NAN, NAN)));
|
||||
|
||||
// test underflow avoidance
|
||||
TEST(OK, hypotf(2e-38, 3e-38) > 0);
|
||||
|
||||
// test what happens on overflow
|
||||
// TODO(jart): This should raise ERANGE.
|
||||
TEST(OK, hypotf(FLT_MAX, FLT_MAX) == INFINITY);
|
||||
|
||||
// test with INFINITY
|
||||
TEST(OK, hypotf(INFINITY, 1) == INFINITY);
|
||||
TEST(OK, hypotf(1, INFINITY) == INFINITY);
|
||||
TEST(OK, hypotf(-INFINITY, -INFINITY) == INFINITY);
|
||||
|
||||
// test accuracy assuming hypotl() is correct
|
||||
union {
|
||||
int i;
|
||||
float f;
|
||||
} x, y, a, b;
|
||||
int n = 1000;
|
||||
for (int i = 0; i < n; ++i) {
|
||||
for (int j = 0; j < n; ++j) {
|
||||
x.i = rando();
|
||||
y.i = rando();
|
||||
a.f = hypotl(x.f, y.f);
|
||||
b.f = hypotf(x.f, y.f);
|
||||
if (isnan(a.f) || isnan(b.f)) {
|
||||
CHECK(isnan(a.f) == isnan(b.f));
|
||||
continue;
|
||||
}
|
||||
long e = b.i - a.i;
|
||||
if (e < 0) e = -e;
|
||||
CHECK(e <= MAX_ERROR_ULP);
|
||||
}
|
||||
}
|
||||
}
|
104
test/math/powf_test.c
Normal file
104
test/math/powf_test.c
Normal file
|
@ -0,0 +1,104 @@
|
|||
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│
|
||||
│ vi: set et ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi │
|
||||
╞══════════════════════════════════════════════════════════════════════════════╡
|
||||
│ Copyright 2024 Justine Alexandra Roberts Tunney │
|
||||
│ │
|
||||
│ Permission to use, copy, modify, and/or distribute this software for │
|
||||
│ any purpose with or without fee is hereby granted, provided that the │
|
||||
│ above copyright notice and this permission notice appear in all copies. │
|
||||
│ │
|
||||
│ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL │
|
||||
│ WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED │
|
||||
│ WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE │
|
||||
│ AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL │
|
||||
│ DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR │
|
||||
│ PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER │
|
||||
│ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │
|
||||
│ PERFORMANCE OF THIS SOFTWARE. │
|
||||
╚─────────────────────────────────────────────────────────────────────────────*/
|
||||
#include <errno.h>
|
||||
#include <float.h>
|
||||
#include <math.h>
|
||||
|
||||
#define OK 0
|
||||
|
||||
#define MAX_ERROR_ULP 1
|
||||
|
||||
#define CHECK(x) \
|
||||
if (!(x)) return __LINE__
|
||||
|
||||
#define TEST(e, x) \
|
||||
errno = 0; \
|
||||
CHECK(x); \
|
||||
CHECK(errno == e)
|
||||
|
||||
int rando(void) {
|
||||
static unsigned long s;
|
||||
s *= 6364136223846793005;
|
||||
s += 1442695040888963407;
|
||||
return s >> 32;
|
||||
}
|
||||
|
||||
int main() {
|
||||
|
||||
// test base cases
|
||||
TEST(OK, !powf(0, 5));
|
||||
TEST(OK, powf(0, 0) == 1);
|
||||
TEST(OK, powf(1, 0) == 1);
|
||||
TEST(OK, powf(2, 0) == 1);
|
||||
TEST(OK, powf(0, 2) == 0);
|
||||
TEST(OK, powf(5, 0) == 1);
|
||||
TEST(OK, powf(1, 5) == 1);
|
||||
TEST(OK, powf(2, 3) == 8);
|
||||
TEST(OK, powf(-2, 3) == -8);
|
||||
TEST(OK, powf(-2, 2) == 4);
|
||||
TEST(OK, powf(2, -2) == 0.25);
|
||||
|
||||
// test edge cases
|
||||
TEST(OK, powf(-2, -2) == 0.25);
|
||||
TEST(OK, powf(2, .5) == sqrtf(2));
|
||||
TEST(OK, powf(2, -.5) == M_SQRT1_2f);
|
||||
|
||||
// test special values
|
||||
TEST(OK, powf(NAN, 0) == 1);
|
||||
TEST(OK, !powf(INFINITY, -2));
|
||||
TEST(OK, isnan(powf(NAN, 2)));
|
||||
TEST(OK, isnan(powf(2, NAN)));
|
||||
TEST(OK, powf(INFINITY, -1) == 0);
|
||||
TEST(OK, powf(INFINITY, 2) == INFINITY);
|
||||
TEST(OK, powf(-INFINITY, 2) == INFINITY);
|
||||
TEST(OK, powf(-INFINITY, 3) == -INFINITY);
|
||||
|
||||
// test domain errors
|
||||
TEST(EDOM, isnan(powf(-1, 0.5)));
|
||||
|
||||
// test pole errors
|
||||
TEST(ERANGE, isinf(powf(0, -1)));
|
||||
TEST(ERANGE, isinf(powf(0, -.5)));
|
||||
|
||||
// test underflow and overflow
|
||||
TEST(ERANGE, powf(1e-38, 2) == 0);
|
||||
TEST(ERANGE, powf(FLT_MAX, 2) == INFINITY);
|
||||
|
||||
// test accuracy assuming pow() is correct
|
||||
union {
|
||||
int i;
|
||||
float f;
|
||||
} x, y, a, b;
|
||||
int n = 1000;
|
||||
for (int i = 0; i < n; ++i) {
|
||||
for (int j = 0; j < n; ++j) {
|
||||
x.i = rando();
|
||||
y.i = rando();
|
||||
a.f = pow(x.f, y.f);
|
||||
b.f = powf(x.f, y.f);
|
||||
if (isnan(a.f) || isnan(b.f)) {
|
||||
CHECK(isnan(a.f) == isnan(b.f));
|
||||
continue;
|
||||
}
|
||||
int e = b.i - a.i;
|
||||
if (e < 0) e = -e;
|
||||
CHECK(e <= MAX_ERROR_ULP);
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue