From bfef17eb6d23a76be2afb6ae4a39448d7c9a0e49 Mon Sep 17 00:00:00 2001 From: Justine Tunney Date: Sat, 6 Mar 2021 11:41:01 -0800 Subject: [PATCH] Add more math fixes --- Makefile | 4 +- libc/tinymath/acosf.S | 1 - libc/tinymath/acosl.S | 29 ++++--------- libc/tinymath/asin.S | 1 - libc/tinymath/asinf.S | 1 - libc/tinymath/asinl.S | 26 ++++-------- libc/tinymath/atan.S | 1 - libc/tinymath/atanl.S | 1 - libc/tinymath/pow.S | 2 - libc/tinymath/powf.S | 1 - libc/tinymath/{powlfinite.c => powfin.c} | 11 ++++- libc/tinymath/scalb.S | 1 - libc/tinymath/sin.S | 1 - libc/tinymath/sincos.S | 1 - libc/tinymath/sincosf.S | 1 - libc/tinymath/sincosl.S | 1 - libc/tinymath/sinf.S | 1 - libc/tinymath/tan.S | 1 - libc/tinymath/tanl.S | 1 - libc/tinymath/tinymath.mk | 4 ++ test/libc/tinymath/acos_test.c | 39 ++++++++++++++++++ test/libc/tinymath/asin_test.c | 40 ++++++++++++++++++ test/libc/tinymath/atan_test.c | 40 ++++++++++++++++++ test/libc/tinymath/cbrt_test.c | 41 +++++++++++++++++++ test/libc/tinymath/powl_test.c | 6 +-- .../libc/tinymath/{sinl_test.c => sin_test.c} | 0 test/libc/tinymath/tan_test.c | 41 +++++++++++++++++++ 27 files changed, 236 insertions(+), 61 deletions(-) rename libc/tinymath/{powlfinite.c => powfin.c} (90%) create mode 100644 test/libc/tinymath/acos_test.c create mode 100644 test/libc/tinymath/asin_test.c create mode 100644 test/libc/tinymath/atan_test.c create mode 100644 test/libc/tinymath/cbrt_test.c rename test/libc/tinymath/{sinl_test.c => sin_test.c} (100%) create mode 100644 test/libc/tinymath/tan_test.c diff --git a/Makefile b/Makefile index c82c7c8c..efe2482c 100644 --- a/Makefile +++ b/Makefile @@ -234,7 +234,6 @@ COSMOPOLITAN_OBJECTS = \ LIBC_NT_MSWSOCK \ LIBC_OHMYPLUS \ LIBC_X \ - THIRD_PARTY_GDTOA \ THIRD_PARTY_GETOPT \ LIBC_LOG \ LIBC_UNICODE \ @@ -243,6 +242,7 @@ COSMOPOLITAN_OBJECTS = \ THIRD_PARTY_ZLIB \ THIRD_PARTY_MUSL \ LIBC_STDIO \ + THIRD_PARTY_GDTOA \ THIRD_PARTY_REGEX \ LIBC_ALG \ LIBC_MEM \ @@ -265,8 +265,8 @@ COSMOPOLITAN_OBJECTS = \ LIBC_FMT \ THIRD_PARTY_COMPILER_RT \ LIBC_TINYMATH \ - LIBC_SYSV \ LIBC_STR \ + LIBC_SYSV \ LIBC_INTRIN \ LIBC_NT_KERNEL32 \ LIBC_NEXGEN32E diff --git a/libc/tinymath/acosf.S b/libc/tinymath/acosf.S index e743d07d..144c6743 100644 --- a/libc/tinymath/acosf.S +++ b/libc/tinymath/acosf.S @@ -17,7 +17,6 @@ │ PERFORMANCE OF THIS SOFTWARE. │ ╚─────────────────────────────────────────────────────────────────────────────*/ #include "libc/macros.internal.h" -.source __FILE__ // Returns arc cosine of 𝑥. // diff --git a/libc/tinymath/acosl.S b/libc/tinymath/acosl.S index 0950e95c..2a15a999 100644 --- a/libc/tinymath/acosl.S +++ b/libc/tinymath/acosl.S @@ -17,38 +17,27 @@ │ PERFORMANCE OF THIS SOFTWARE. │ ╚─────────────────────────────────────────────────────────────────────────────*/ #include "libc/macros.internal.h" -.source __FILE__ // Returns arc cosine of 𝑥. // // @param 𝑥 is an 80-bit long double passed on stack in 16-bytes // @return result of computation on FPU stack in %st -// @define atan2(abs(sqrt((1-𝑥)*(1+𝑥))),𝑥) +// @define atan2(fabs(sqrt((1-𝑥)*(1+𝑥))),𝑥) // @domain -1 ≤ 𝑥 ≤ 1 -// @mode long,legacy -acosl: push %rbp +acosl: pushq %rbp mov %rsp,%rbp .profilable - fldl 16(%rbp) - fld %st -#ifdef __FAST_MATH__ - fmul %st(1),%st - fsubrs .Lone(%rip) - fsqrt -#else + fldt 16(%rbp) fld1 - fsubp - fld1 - fadd %st(2) + fld %st + fsub %st(2) + fxch + fadd %st(2) fmulp fsqrt - fabs # needed in downward rounding mode -#endif + fabs fxch fpatan - pop %rbp + leave ret .endfn acosl,globl - - .rodata.cst4 -.Lone: .float 1.0 diff --git a/libc/tinymath/asin.S b/libc/tinymath/asin.S index 88952dcd..5dbfd769 100644 --- a/libc/tinymath/asin.S +++ b/libc/tinymath/asin.S @@ -17,7 +17,6 @@ │ PERFORMANCE OF THIS SOFTWARE. │ ╚─────────────────────────────────────────────────────────────────────────────*/ #include "libc/macros.internal.h" -.source __FILE__ // Returns arc sine of 𝑥. // diff --git a/libc/tinymath/asinf.S b/libc/tinymath/asinf.S index fe4a6f69..3f65532f 100644 --- a/libc/tinymath/asinf.S +++ b/libc/tinymath/asinf.S @@ -17,7 +17,6 @@ │ PERFORMANCE OF THIS SOFTWARE. │ ╚─────────────────────────────────────────────────────────────────────────────*/ #include "libc/macros.internal.h" -.source __FILE__ // Returns arc sine of 𝑥. // diff --git a/libc/tinymath/asinl.S b/libc/tinymath/asinl.S index e37eb1cc..f8623ec2 100644 --- a/libc/tinymath/asinl.S +++ b/libc/tinymath/asinl.S @@ -17,7 +17,6 @@ │ PERFORMANCE OF THIS SOFTWARE. │ ╚─────────────────────────────────────────────────────────────────────────────*/ #include "libc/macros.internal.h" -.source __FILE__ // Returns arc sine of 𝑥. // @@ -25,27 +24,18 @@ // @return result of computation on FPU stack in %st // @define atan2(𝑥,sqrt((1-𝑥)*(1+𝑥))) // @domain -1 ≤ 𝑥 ≤ 1 -// @mode long,legacy -asinl: push %rbp +asinl: pushq %rbp mov %rsp,%rbp .profilable - fldl 16(%rbp) - fld %st -#ifdef __FAST_MATH__ - fmul %st(1),%st - fsubrs .Lone(%rip) -#else + fldt 16(%rbp) fld1 - fsubp - fld1 - fadd %st(2) + fld %st + fsub %st(2) + fxch + fadd %st(2) fmulp -#endif fsqrt - fpatan - pop %rbp + fpatan + leave ret .endfn asinl,globl - - .rodata.cst4 -.Lone: .float 1.0 diff --git a/libc/tinymath/atan.S b/libc/tinymath/atan.S index 6945b9bd..d50db143 100644 --- a/libc/tinymath/atan.S +++ b/libc/tinymath/atan.S @@ -17,7 +17,6 @@ │ PERFORMANCE OF THIS SOFTWARE. │ ╚─────────────────────────────────────────────────────────────────────────────*/ #include "libc/macros.internal.h" -.source __FILE__ // Returns arc tangent of 𝑥. // diff --git a/libc/tinymath/atanl.S b/libc/tinymath/atanl.S index a1963876..d962f7f6 100644 --- a/libc/tinymath/atanl.S +++ b/libc/tinymath/atanl.S @@ -17,7 +17,6 @@ │ PERFORMANCE OF THIS SOFTWARE. │ ╚─────────────────────────────────────────────────────────────────────────────*/ #include "libc/macros.internal.h" -.source __FILE__ // Returns arc tangent of 𝑥. // diff --git a/libc/tinymath/pow.S b/libc/tinymath/pow.S index a4158f41..8c4c0709 100644 --- a/libc/tinymath/pow.S +++ b/libc/tinymath/pow.S @@ -17,7 +17,6 @@ │ PERFORMANCE OF THIS SOFTWARE. │ ╚─────────────────────────────────────────────────────────────────────────────*/ #include "libc/macros.internal.h" -.source __FILE__ // Returns 𝑥^𝑦. // @@ -27,4 +26,3 @@ pow: ezlea powl,ax jmp _d2ld2 .endfn pow,globl - .alias pow,__pow_finite diff --git a/libc/tinymath/powf.S b/libc/tinymath/powf.S index d2e03491..7ff1ac8b 100644 --- a/libc/tinymath/powf.S +++ b/libc/tinymath/powf.S @@ -26,4 +26,3 @@ powf: ezlea powl,ax jmp _f2ld2 .endfn powf,globl - .alias powf,__powf_finite diff --git a/libc/tinymath/powlfinite.c b/libc/tinymath/powfin.c similarity index 90% rename from libc/tinymath/powlfinite.c rename to libc/tinymath/powfin.c index 0b95b9bc..308a5488 100644 --- a/libc/tinymath/powlfinite.c +++ b/libc/tinymath/powfin.c @@ -18,6 +18,13 @@ ╚─────────────────────────────────────────────────────────────────────────────*/ #include "libc/math.h" -long double __powl_finite(long double x, long double y) { - return powl(x, y); +#define powl __powl_finite +#include "libc/tinymath/powl.c" + +double __pow_finite(double x, double y) { + return __powl_finite(x, y); +} + +float __powf_finite(float x, float y) { + return __powl_finite(x, y); } diff --git a/libc/tinymath/scalb.S b/libc/tinymath/scalb.S index 53333b6d..d835b65f 100644 --- a/libc/tinymath/scalb.S +++ b/libc/tinymath/scalb.S @@ -17,7 +17,6 @@ │ PERFORMANCE OF THIS SOFTWARE. │ ╚─────────────────────────────────────────────────────────────────────────────*/ #include "libc/macros.internal.h" -.source __FILE__ // Returns 𝑥 × 2ʸ. // diff --git a/libc/tinymath/sin.S b/libc/tinymath/sin.S index 3a46f190..a0750846 100644 --- a/libc/tinymath/sin.S +++ b/libc/tinymath/sin.S @@ -17,7 +17,6 @@ │ PERFORMANCE OF THIS SOFTWARE. │ ╚─────────────────────────────────────────────────────────────────────────────*/ #include "libc/macros.internal.h" -.source __FILE__ // Returns sine of 𝑥. // diff --git a/libc/tinymath/sincos.S b/libc/tinymath/sincos.S index 38a3203e..bbc992a6 100644 --- a/libc/tinymath/sincos.S +++ b/libc/tinymath/sincos.S @@ -17,7 +17,6 @@ │ PERFORMANCE OF THIS SOFTWARE. │ ╚─────────────────────────────────────────────────────────────────────────────*/ #include "libc/macros.internal.h" -.source __FILE__ // Returns sine and cosine of 𝑥. // diff --git a/libc/tinymath/sincosf.S b/libc/tinymath/sincosf.S index 41cf7838..57177755 100644 --- a/libc/tinymath/sincosf.S +++ b/libc/tinymath/sincosf.S @@ -17,7 +17,6 @@ │ PERFORMANCE OF THIS SOFTWARE. │ ╚─────────────────────────────────────────────────────────────────────────────*/ #include "libc/macros.internal.h" -.source __FILE__ // Returns sine and cosine of 𝑥. // diff --git a/libc/tinymath/sincosl.S b/libc/tinymath/sincosl.S index 2f0d491e..b1ac2806 100644 --- a/libc/tinymath/sincosl.S +++ b/libc/tinymath/sincosl.S @@ -18,7 +18,6 @@ ╚─────────────────────────────────────────────────────────────────────────────*/ #include "libc/runtime/pc.internal.h" #include "libc/macros.internal.h" -.source __FILE__ // Returns sine and cosine of 𝑥. // diff --git a/libc/tinymath/sinf.S b/libc/tinymath/sinf.S index 6f5051f6..4c4214f5 100644 --- a/libc/tinymath/sinf.S +++ b/libc/tinymath/sinf.S @@ -17,7 +17,6 @@ │ PERFORMANCE OF THIS SOFTWARE. │ ╚─────────────────────────────────────────────────────────────────────────────*/ #include "libc/macros.internal.h" -.source __FILE__ // Returns sine of 𝑥. // diff --git a/libc/tinymath/tan.S b/libc/tinymath/tan.S index 3df3bc76..631c786a 100644 --- a/libc/tinymath/tan.S +++ b/libc/tinymath/tan.S @@ -17,7 +17,6 @@ │ PERFORMANCE OF THIS SOFTWARE. │ ╚─────────────────────────────────────────────────────────────────────────────*/ #include "libc/macros.internal.h" -.source __FILE__ // Returns tangent of 𝑥. // diff --git a/libc/tinymath/tanl.S b/libc/tinymath/tanl.S index 40d707fd..f65b6303 100644 --- a/libc/tinymath/tanl.S +++ b/libc/tinymath/tanl.S @@ -18,7 +18,6 @@ ╚─────────────────────────────────────────────────────────────────────────────*/ #include "libc/runtime/pc.internal.h" #include "libc/macros.internal.h" -.source __FILE__ // Returns tangent of 𝑥. // diff --git a/libc/tinymath/tinymath.mk b/libc/tinymath/tinymath.mk index 077118ed..fd039c88 100644 --- a/libc/tinymath/tinymath.mk +++ b/libc/tinymath/tinymath.mk @@ -40,6 +40,10 @@ $(LIBC_TINYMATH_A).pkg: \ $(LIBC_TINYMATH_A_OBJS) \ $(foreach x,$(LIBC_TINYMATH_A_DIRECTDEPS),$($(x)_A).pkg) +o/$(MODE)/libc/tinymath/powfin.o: \ + OVERRIDE_CFLAGS += \ + -ffast-math + LIBC_TINYMATH_LIBS = $(foreach x,$(LIBC_TINYMATH_ARTIFACTS),$($(x))) LIBC_TINYMATH_HDRS = $(foreach x,$(LIBC_TINYMATH_ARTIFACTS),$($(x)_HDRS)) LIBC_TINYMATH_SRCS = $(foreach x,$(LIBC_TINYMATH_ARTIFACTS),$($(x)_SRCS)) diff --git a/test/libc/tinymath/acos_test.c b/test/libc/tinymath/acos_test.c new file mode 100644 index 00000000..be8da3b6 --- /dev/null +++ b/test/libc/tinymath/acos_test.c @@ -0,0 +1,39 @@ +/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│ +│vi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi│ +╞══════════════════════════════════════════════════════════════════════════════╡ +│ Copyright 2021 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 "libc/math.h" +#include "libc/runtime/gc.h" +#include "libc/testlib/testlib.h" +#include "libc/x/x.h" + +TEST(acos, test) { + EXPECT_STREQ("1.5707963267949", gc(xasprintf("%.15g", acos(0.)))); + EXPECT_STREQ("1.5707963267949", gc(xasprintf("%.15g", acos(-0.)))); + EXPECT_STREQ("1.0471975511966", gc(xasprintf("%.15g", acos(.5)))); + EXPECT_STREQ("2.0943951023932", gc(xasprintf("%.15g", acos(-.5)))); + EXPECT_STREQ("0", gc(xasprintf("%.15g", acos(1.)))); + EXPECT_STREQ("3.14159265358979", gc(xasprintf("%.15g", acos(-1.)))); + EXPECT_TRUE(isnan(acos(1.5))); + EXPECT_TRUE(isnan(acos(-1.5))); + EXPECT_TRUE(isnan(acos(NAN))); + EXPECT_TRUE(isnan(acos(-NAN))); + EXPECT_TRUE(isnan(acos(INFINITY))); + EXPECT_TRUE(isnan(acos(-INFINITY))); + EXPECT_STREQ("1.5707963267949", gc(xasprintf("%.15g", acos(__DBL_MIN__)))); + EXPECT_TRUE(isnan(acos(__LDBL_MAX__))); +} diff --git a/test/libc/tinymath/asin_test.c b/test/libc/tinymath/asin_test.c new file mode 100644 index 00000000..870cf305 --- /dev/null +++ b/test/libc/tinymath/asin_test.c @@ -0,0 +1,40 @@ +/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│ +│vi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi│ +╞══════════════════════════════════════════════════════════════════════════════╡ +│ Copyright 2021 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 "libc/math.h" +#include "libc/runtime/gc.h" +#include "libc/testlib/testlib.h" +#include "libc/x/x.h" + +TEST(asin, test) { + EXPECT_STREQ("0", gc(xasprintf("%.15g", asin(0.)))); + EXPECT_STREQ("-0", gc(xasprintf("%.15g", asin(-0.)))); + EXPECT_STREQ("0.523598775598299", gc(xasprintf("%.15g", asin(.5)))); + EXPECT_STREQ("-0.523598775598299", gc(xasprintf("%.15g", asin(-.5)))); + EXPECT_STREQ("1.5707963267949", gc(xasprintf("%.15g", asin(1.)))); + EXPECT_STREQ("-1.5707963267949", gc(xasprintf("%.15g", asin(-1.)))); + EXPECT_TRUE(isnan(asin(1.5))); + EXPECT_TRUE(isnan(asin(-1.5))); + EXPECT_TRUE(isnan(asin(NAN))); + EXPECT_TRUE(isnan(asin(-NAN))); + EXPECT_TRUE(isnan(asin(INFINITY))); + EXPECT_TRUE(isnan(asin(-INFINITY))); + EXPECT_STREQ("2.2250738585072e-308", + gc(xasprintf("%.15g", asin(__DBL_MIN__)))); + EXPECT_TRUE(isnan(asin(__DBL_MAX__))); +} diff --git a/test/libc/tinymath/atan_test.c b/test/libc/tinymath/atan_test.c new file mode 100644 index 00000000..bc51fefd --- /dev/null +++ b/test/libc/tinymath/atan_test.c @@ -0,0 +1,40 @@ +/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│ +│vi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi│ +╞══════════════════════════════════════════════════════════════════════════════╡ +│ Copyright 2021 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 "libc/math.h" +#include "libc/runtime/gc.h" +#include "libc/testlib/testlib.h" +#include "libc/x/x.h" + +TEST(atan, test) { + EXPECT_STREQ("0", gc(xasprintf("%.15g", atan(0.)))); + EXPECT_STREQ("-0", gc(xasprintf("%.15g", atan(-0.)))); + EXPECT_STREQ("0.463647609000806", gc(xasprintf("%.15g", atan(.5)))); + EXPECT_STREQ("-0.463647609000806", gc(xasprintf("%.15g", atan(-.5)))); + EXPECT_STREQ("0.785398163397448", gc(xasprintf("%.15g", atan(1.)))); + EXPECT_STREQ("-0.785398163397448", gc(xasprintf("%.15g", atan(-1.)))); + EXPECT_STREQ("0.982793723247329", gc(xasprintf("%.15g", atan(1.5)))); + EXPECT_STREQ("-0.982793723247329", gc(xasprintf("%.15g", atan(-1.5)))); + EXPECT_STREQ("nan", gc(xasprintf("%.15g", atan(NAN)))); + EXPECT_STREQ("-nan", gc(xasprintf("%.15g", atan(-NAN)))); + EXPECT_STREQ("1.5707963267949", gc(xasprintf("%.15g", atan(INFINITY)))); + EXPECT_STREQ("-1.5707963267949", gc(xasprintf("%.15g", atan(-INFINITY)))); + EXPECT_STREQ("2.2250738585072e-308", + gc(xasprintf("%.15g", atan(__DBL_MIN__)))); + EXPECT_STREQ("1.5707963267949", gc(xasprintf("%.15g", atan(__DBL_MAX__)))); +} diff --git a/test/libc/tinymath/cbrt_test.c b/test/libc/tinymath/cbrt_test.c new file mode 100644 index 00000000..e8b70be0 --- /dev/null +++ b/test/libc/tinymath/cbrt_test.c @@ -0,0 +1,41 @@ +/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│ +│vi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi│ +╞══════════════════════════════════════════════════════════════════════════════╡ +│ Copyright 2021 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 "libc/math.h" +#include "libc/runtime/gc.h" +#include "libc/testlib/testlib.h" +#include "libc/x/x.h" + +TEST(cbrt, test) { + EXPECT_STREQ("0", gc(xasprintf("%.15g", cbrt(0.)))); + EXPECT_STREQ("-0", gc(xasprintf("%.15g", cbrt(-0.)))); + EXPECT_STREQ("0.7937005259841", gc(xasprintf("%.15g", cbrt(.5)))); + EXPECT_STREQ("-0.7937005259841", gc(xasprintf("%.15g", cbrt(-.5)))); + EXPECT_STREQ("1", gc(xasprintf("%.15g", cbrt(1.)))); + EXPECT_STREQ("-1", gc(xasprintf("%.15g", cbrt(-1.)))); + EXPECT_STREQ("1.14471424255333", gc(xasprintf("%.15g", cbrt(1.5)))); + EXPECT_STREQ("-1.14471424255333", gc(xasprintf("%.15g", cbrt(-1.5)))); + EXPECT_STREQ("nan", gc(xasprintf("%.15g", cbrt(NAN)))); + EXPECT_STREQ("-nan", gc(xasprintf("%.15g", cbrt(-NAN)))); + EXPECT_STREQ("inf", gc(xasprintf("%.15g", cbrt(INFINITY)))); + EXPECT_STREQ("-inf", gc(xasprintf("%.15g", cbrt(-INFINITY)))); + EXPECT_STREQ("2.81264428523626e-103", + gc(xasprintf("%.15g", cbrt(__DBL_MIN__)))); + EXPECT_STREQ("5.64380309412236e+102", + gc(xasprintf("%.15g", cbrt(__DBL_MAX__)))); +} diff --git a/test/libc/tinymath/powl_test.c b/test/libc/tinymath/powl_test.c index 0b576c9d..ca2cfc1a 100644 --- a/test/libc/tinymath/powl_test.c +++ b/test/libc/tinymath/powl_test.c @@ -192,7 +192,7 @@ BENCH(powl, bench) { double _pow(double, double) asm("pow"); float _powf(float, float) asm("powf"); long double _powl(long double, long double) asm("powl"); - EZBENCH2("pow", donothing, _pow(.7, .2)); /* ~51ns */ - EZBENCH2("powf", donothing, _powf(.7, .2)); /* ~52ns */ - EZBENCH2("powl", donothing, _powl(.7, .2)); /* ~53ns */ + EZBENCH2("pow", donothing, _pow(.7, .2)); /* ~56ns */ + EZBENCH2("powf", donothing, _powf(.7, .2)); /* ~56ns */ + EZBENCH2("powl", donothing, _powl(.7, .2)); /* ~56ns */ } diff --git a/test/libc/tinymath/sinl_test.c b/test/libc/tinymath/sin_test.c similarity index 100% rename from test/libc/tinymath/sinl_test.c rename to test/libc/tinymath/sin_test.c diff --git a/test/libc/tinymath/tan_test.c b/test/libc/tinymath/tan_test.c new file mode 100644 index 00000000..06f87ed5 --- /dev/null +++ b/test/libc/tinymath/tan_test.c @@ -0,0 +1,41 @@ +/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│ +│vi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi│ +╞══════════════════════════════════════════════════════════════════════════════╡ +│ Copyright 2021 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 "libc/math.h" +#include "libc/runtime/gc.h" +#include "libc/testlib/testlib.h" +#include "libc/x/x.h" + +TEST(tan, test) { + EXPECT_STREQ("0", gc(xasprintf("%.15g", tan(0.)))); + EXPECT_STREQ("-0", gc(xasprintf("%.15g", tan(-0.)))); + EXPECT_STREQ("0.54630248984379", gc(xasprintf("%.15g", tan(.5)))); + EXPECT_STREQ("-0.54630248984379", gc(xasprintf("%.15g", tan(-.5)))); + EXPECT_STREQ("1.5574077246549", gc(xasprintf("%.15g", tan(1.)))); + EXPECT_STREQ("-1.5574077246549", gc(xasprintf("%.15g", tan(-1.)))); + EXPECT_STREQ("14.1014199471717", gc(xasprintf("%.15g", tan(1.5)))); + EXPECT_STREQ("-14.1014199471717", gc(xasprintf("%.15g", tan(-1.5)))); + EXPECT_STREQ("nan", gc(xasprintf("%.15g", tan(NAN)))); + EXPECT_STREQ("-nan", gc(xasprintf("%.15g", tan(-NAN)))); + EXPECT_STREQ("-nan", gc(xasprintf("%.15g", tan(INFINITY)))); + EXPECT_STREQ("-nan", gc(xasprintf("%.15g", tan(-INFINITY)))); + EXPECT_STREQ("2.2250738585072e-308", + gc(xasprintf("%.15g", tan(__DBL_MIN__)))); + /* EXPECT_STREQ("-0.0049620158744449", */ + /* gc(xasprintf("%.15g", tan(__DBL_MAX__)))); */ +}