Initial import

This commit is contained in:
Justine Tunney 2020-06-15 07:18:57 -07:00
commit c91b3c5006
14915 changed files with 590219 additions and 0 deletions

View file

@ -0,0 +1,60 @@
/*-*- 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 2020 Justine Alexandra Roberts Tunney
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; version 2 of the License.
This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301 USA
*/
#include "libc/math.h"
#include "libc/runtime/gc.h"
#include "libc/testlib/ezbench.h"
#include "libc/testlib/testlib.h"
#include "libc/tinymath/tinymath.h"
#include "libc/x/x.h"
#include "third_party/dtoa/dtoa.h"
TEST(powl, testLongDouble) {
/* .4248496805467504836322459796959084815827285786480897 */
EXPECT_STARTSWITH(".4248496805467504", gc(xdtoa(powl(0.7, 2.4))));
EXPECT_STARTSWITH(".4248496805467504", gc(xdtoa(tinymath_powl(0.7, 2.4))));
}
TEST(powl, testDouble) {
EXPECT_STARTSWITH(".4248496805467504", gc(xdtoa(pow(0.7, 2.4))));
EXPECT_STARTSWITH(".4248496805467504", gc(xdtoa(tinymath_pow(0.7, 2.4))));
}
TEST(powl, testFloat) {
EXPECT_STARTSWITH(".4248496", gc(xdtoa(powf(0.7f, 2.4f))));
EXPECT_STARTSWITH(".4248496", gc(xdtoa(tinymath_powf(0.7f, 2.4f))));
}
static long double do_powl(void) {
return CONCEAL("t", tinymath_powl(CONCEAL("t", 0.7), CONCEAL("t", 0.2)));
}
static double do_pow(void) {
return CONCEAL("x", tinymath_pow(CONCEAL("x", 0.7), CONCEAL("x", 0.2)));
}
static float do_powf(void) {
return CONCEAL("x", tinymath_powf(CONCEAL("x", 0.7f), CONCEAL("x", 0.2f)));
}
BENCH(powl, bench) {
EZBENCH2("powl", donothing, do_powl()); /* ~61ns */
EZBENCH2("pow", donothing, do_pow()); /* ~64ns */
EZBENCH2("powf", donothing, do_powf()); /* ~64ns */
}

View file

@ -0,0 +1,120 @@
/*-*- 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 2020 Justine Alexandra Roberts Tunney
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; version 2 of the License.
This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301 USA
*/
#include "libc/math.h"
#include "libc/nexgen32e/x86feature.h"
#include "libc/runtime/gc.h"
#include "libc/testlib/testlib.h"
#include "libc/tinymath/tinymath.h"
#include "libc/x/x.h"
float tinymath_roundf$k8(float);
double tinymath_round$k8(double);
TEST(round, test) {
EXPECT_STREQ("-3", gc(xdtoa(tinymath_round(-2.5))));
EXPECT_STREQ("-2", gc(xdtoa(tinymath_round(-1.5))));
EXPECT_STREQ("-1", gc(xdtoa(tinymath_round(-.5))));
EXPECT_STREQ("1", gc(xdtoa(tinymath_round(.5))));
EXPECT_STREQ("2", gc(xdtoa(tinymath_round(1.5))));
EXPECT_STREQ("3", gc(xdtoa(tinymath_round(2.5))));
}
TEST(round, testCornerCases) {
EXPECT_STREQ("-0", gc(xdtoa(tinymath_round(-0.0))));
EXPECT_STREQ("NAN", gc(xdtoa(tinymath_round(NAN))));
EXPECT_STREQ("-NAN", gc(xdtoa(tinymath_round(-NAN))));
EXPECT_STREQ("INFINITY", gc(xdtoa(tinymath_round(INFINITY))));
EXPECT_STREQ("-INFINITY", gc(xdtoa(tinymath_round(-INFINITY))));
}
TEST(lround, test) {
EXPECT_EQ(-3, tinymath_lround(-2.5));
EXPECT_EQ(-2, tinymath_lround(-1.5));
EXPECT_EQ(-1, tinymath_lround(-.5));
EXPECT_EQ(0, tinymath_lround(-.0));
EXPECT_EQ(1, tinymath_lround(.5));
EXPECT_EQ(2, tinymath_lround(1.5));
EXPECT_EQ(3, tinymath_lround(2.5));
}
TEST(roundf, test) {
EXPECT_STREQ("-3", gc(xdtoa(tinymath_roundf(-2.5))));
EXPECT_STREQ("-2", gc(xdtoa(tinymath_roundf(-1.5))));
EXPECT_STREQ("-1", gc(xdtoa(tinymath_roundf(-.5))));
EXPECT_STREQ("1", gc(xdtoa(tinymath_roundf(.5))));
EXPECT_STREQ("2", gc(xdtoa(tinymath_roundf(1.5))));
EXPECT_STREQ("3", gc(xdtoa(tinymath_roundf(2.5))));
}
TEST(roundf, testCornerCases) {
EXPECT_STREQ("-0", gc(xdtoa(tinymath_roundf(-0.0))));
EXPECT_STREQ("NAN", gc(xdtoa(tinymath_roundf(NAN))));
EXPECT_STREQ("-NAN", gc(xdtoa(tinymath_roundf(-NAN))));
EXPECT_STREQ("INFINITY", gc(xdtoa(tinymath_roundf(INFINITY))));
EXPECT_STREQ("-INFINITY", gc(xdtoa(tinymath_roundf(-INFINITY))));
}
TEST(lroundf, test) {
EXPECT_EQ(-3, tinymath_lroundf(-2.5));
EXPECT_EQ(-2, tinymath_lroundf(-1.5));
EXPECT_EQ(-1, tinymath_lroundf(-.5));
EXPECT_EQ(0, tinymath_lroundf(-.0));
EXPECT_EQ(1, tinymath_lroundf(.5));
EXPECT_EQ(2, tinymath_lroundf(1.5));
EXPECT_EQ(3, tinymath_lroundf(2.5));
}
#if !X86_NEED(SSE4_2)
TEST(round$k8, test) {
EXPECT_STREQ("-3", gc(xdtoa(tinymath_round$k8(-2.5))));
EXPECT_STREQ("-2", gc(xdtoa(tinymath_round$k8(-1.5))));
EXPECT_STREQ("-1", gc(xdtoa(tinymath_round$k8(-.5))));
EXPECT_STREQ("1", gc(xdtoa(tinymath_round$k8(.5))));
EXPECT_STREQ("2", gc(xdtoa(tinymath_round$k8(1.5))));
EXPECT_STREQ("3", gc(xdtoa(tinymath_round$k8(2.5))));
}
TEST(roundf$k8, test) {
EXPECT_STREQ("-3", gc(xdtoa(tinymath_roundf$k8(-2.5))));
EXPECT_STREQ("-2", gc(xdtoa(tinymath_roundf$k8(-1.5))));
EXPECT_STREQ("-1", gc(xdtoa(tinymath_roundf$k8(-.5))));
EXPECT_STREQ("1", gc(xdtoa(tinymath_roundf$k8(.5))));
EXPECT_STREQ("2", gc(xdtoa(tinymath_roundf$k8(1.5))));
EXPECT_STREQ("3", gc(xdtoa(tinymath_roundf$k8(2.5))));
}
TEST(round$k8, testCornerCases) {
EXPECT_STREQ("-0", gc(xdtoa(tinymath_round$k8(-0.0))));
EXPECT_STREQ("NAN", gc(xdtoa(tinymath_round$k8(NAN))));
EXPECT_STREQ("-NAN", gc(xdtoa(tinymath_round$k8(-NAN))));
EXPECT_STREQ("INFINITY", gc(xdtoa(tinymath_round$k8(INFINITY))));
EXPECT_STREQ("-INFINITY", gc(xdtoa(tinymath_round$k8(-INFINITY))));
}
TEST(roundf$k8, testCornerCases) {
EXPECT_STREQ("-0", gc(xdtoa(tinymath_roundf$k8(-0.0))));
EXPECT_STREQ("NAN", gc(xdtoa(tinymath_roundf$k8(NAN))));
EXPECT_STREQ("-NAN", gc(xdtoa(tinymath_roundf$k8(-NAN))));
EXPECT_STREQ("INFINITY", gc(xdtoa(tinymath_roundf$k8(INFINITY))));
EXPECT_STREQ("-INFINITY", gc(xdtoa(tinymath_roundf$k8(-INFINITY))));
}
#endif

View file

@ -0,0 +1,46 @@
/*-*- 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 2020 Justine Alexandra Roberts Tunney
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; version 2 of the License.
This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301 USA
*/
#include "libc/math.h"
#include "libc/testlib/ezbench.h"
#include "libc/testlib/testlib.h"
#include "third_party/dtoa/dtoa.h"
char buf[32];
TEST(sinl, testLongDouble) {
EXPECT_STREQ(".479425538604203", g_fmt(buf, sinl(0.5)));
EXPECT_STREQ("-.479425538604203", g_fmt(buf, sinl(-0.5)));
}
TEST(sinl, testDouble) {
EXPECT_STREQ(".479425538604203", g_fmt(buf, sin(0.5)));
EXPECT_STREQ("-.479425538604203", g_fmt(buf, sin(-0.5)));
}
TEST(sinl, testFloat) {
EXPECT_STARTSWITH(".4794255", g_fmt(buf, sinf(0.5f)));
EXPECT_STARTSWITH("-.4794255", g_fmt(buf, sinf(-0.5f)));
}
BENCH(sinl, bench) {
EZBENCH(donothing, sinl(0.7)); /* ~30ns */
EZBENCH(donothing, sin(0.7)); /* ~35ns */
EZBENCH(donothing, sinf(0.7f)); /* ~35ns */
}

View file

@ -0,0 +1,57 @@
#-*-mode:makefile-gmake;indent-tabs-mode:t;tab-width:8;coding:utf-8-*-┐
#───vi: set et ft=make ts=8 tw=8 fenc=utf-8 :vi───────────────────────┘
PKGS += TEST_LIBC_TINYMATH
TEST_LIBC_TINYMATH_SRCS := $(wildcard test/libc/tinymath/*.c)
TEST_LIBC_TINYMATH_SRCS_TEST = $(filter %_test.c,$(TEST_LIBC_TINYMATH_SRCS))
TEST_LIBC_TINYMATH_COMS = $(TEST_LIBC_TINYMATH_OBJS:%.o=%.com)
TEST_LIBC_TINYMATH_OBJS = \
$(TEST_LIBC_TINYMATH_SRCS:%=o/$(MODE)/%.zip.o) \
$(TEST_LIBC_TINYMATH_SRCS:%.c=o/$(MODE)/%.o)
TEST_LIBC_TINYMATH_BINS = \
$(TEST_LIBC_TINYMATH_COMS) \
$(TEST_LIBC_TINYMATH_COMS:%=%.dbg)
TEST_LIBC_TINYMATH_TESTS = $(TEST_LIBC_TINYMATH_SRCS_TEST:%.c=o/$(MODE)/%.com.ok)
TEST_LIBC_TINYMATH_CHECKS = \
$(TEST_LIBC_TINYMATH_SRCS_TEST:%.c=o/$(MODE)/%.com.runs)
TEST_LIBC_TINYMATH_DIRECTDEPS = \
LIBC_CALLS_HEFTY \
LIBC_FMT \
LIBC_TINYMATH \
LIBC_MEM \
LIBC_RUNTIME \
LIBC_STUBS \
LIBC_TESTLIB \
LIBC_X \
THIRD_PARTY_DTOA
TEST_LIBC_TINYMATH_DEPS := \
$(call uniq,$(foreach x,$(TEST_LIBC_TINYMATH_DIRECTDEPS),$($(x))))
o/$(MODE)/test/libc/tinymath/tinymath.pkg: \
$(TEST_LIBC_TINYMATH_OBJS) \
$(foreach x,$(TEST_LIBC_TINYMATH_DIRECTDEPS),$($(x)_A).pkg)
o/$(MODE)/test/libc/tinymath/%.com.dbg: \
$(TEST_LIBC_TINYMATH_DEPS) \
o/$(MODE)/test/libc/tinymath/%.o \
o/$(MODE)/test/libc/tinymath/tinymath.pkg \
$(LIBC_TESTMAIN) \
$(CRT) \
$(APE)
@$(APELINK)
$(TEST_LIBC_TINYMATH_OBJS): \
DEFAULT_CCFLAGS += \
-fno-builtin
.PHONY: o/$(MODE)/test/libc/tinymath
o/$(MODE)/test/libc/tinymath: \
$(TEST_LIBC_TINYMATH_BINS) \
$(TEST_LIBC_TINYMATH_CHECKS)