Implement better hypot function

This commit is contained in:
Justine Tunney
2021-02-21 11:06:18 -08:00
parent 6c7b8facaa
commit 4a5698b5c9
14 changed files with 293 additions and 62 deletions

39
test/libc/str/a64l_test.c Normal file
View File

@ -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/str/str.h"
#include "libc/testlib/ezbench.h"
#include "libc/testlib/testlib.h"
TEST(l64a, test) {
EXPECT_STREQ("", l64a(0));
EXPECT_STREQ("zzzzz1", l64a(-1));
EXPECT_STREQ("zzzzz/", l64a(0x7fffffff));
}
TEST(a64l, test) {
EXPECT_EQ(0, a64l(""));
EXPECT_EQ(-1, a64l("zzzzz1"));
EXPECT_EQ(0x7fffffff, a64l("zzzzz/"));
}
BENCH(a64l, bench) {
EZBENCH2(
"a64l", donothing,
a64l("./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"));
}

View File

@ -0,0 +1,75 @@
/*-*- 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/testlib/ezbench.h"
#include "libc/testlib/testlib.h"
#include "libc/x/x.h"
TEST(hypot, test) {
EXPECT_STREQ("0", gc(xdtoa(hypot(0, 0))));
EXPECT_STREQ("5", gc(xdtoa(hypot(3, 4))));
EXPECT_STREQ("5", gc(xdtoa(hypot(-3, -4))));
EXPECT_STREQ("1.414213562373095e+154", gc(xdtoa(hypot(1e154, 1e154))));
EXPECT_STREQ("NAN", gc(xdtoa(hypot(0, NAN))));
EXPECT_STREQ("NAN", gc(xdtoa(hypot(NAN, 0))));
EXPECT_STREQ("NAN", gc(xdtoa(hypot(NAN, NAN))));
EXPECT_STREQ("INFINITY", gc(xdtoa(hypot(INFINITY, 0))));
EXPECT_STREQ("INFINITY", gc(xdtoa(hypot(0, INFINITY))));
EXPECT_STREQ("INFINITY", gc(xdtoa(hypot(INFINITY, NAN))));
EXPECT_STREQ("INFINITY", gc(xdtoa(hypot(NAN, INFINITY))));
EXPECT_STREQ("INFINITY", gc(xdtoa(hypot(INFINITY, INFINITY))));
}
TEST(hypotf, test) {
EXPECT_STREQ("0", gc(xdtoa(hypotf(0, 0))));
EXPECT_STREQ("5", gc(xdtoa(hypotf(3, 4))));
EXPECT_STREQ("5", gc(xdtoa(hypotf(-3, -4))));
EXPECT_STREQ("1.414214e+38", gc(xdtoaf(hypotf(1e38, 1e38))));
EXPECT_STREQ("NAN", gc(xdtoa(hypotf(0, NAN))));
EXPECT_STREQ("NAN", gc(xdtoa(hypotf(NAN, 0))));
EXPECT_STREQ("NAN", gc(xdtoa(hypotf(NAN, NAN))));
EXPECT_STREQ("INFINITY", gc(xdtoa(hypotf(INFINITY, 0))));
EXPECT_STREQ("INFINITY", gc(xdtoa(hypotf(0, INFINITY))));
EXPECT_STREQ("INFINITY", gc(xdtoa(hypotf(INFINITY, NAN))));
EXPECT_STREQ("INFINITY", gc(xdtoa(hypotf(NAN, INFINITY))));
EXPECT_STREQ("INFINITY", gc(xdtoa(hypotf(INFINITY, INFINITY))));
}
TEST(hypotl, test) {
EXPECT_STREQ("0", gc(xdtoa(hypotl(0, 0))));
EXPECT_STREQ("5", gc(xdtoa(hypotl(3, 4))));
EXPECT_STREQ("5", gc(xdtoa(hypotl(-3, -4))));
EXPECT_STREQ("1.414213562373095e+4931", gc(xdtoa(hypotl(1e4931L, 1e4931L))));
EXPECT_STREQ("NAN", gc(xdtoa(hypotl(0, NAN))));
EXPECT_STREQ("NAN", gc(xdtoa(hypotl(NAN, 0))));
EXPECT_STREQ("NAN", gc(xdtoa(hypotl(NAN, NAN))));
EXPECT_STREQ("INFINITY", gc(xdtoa(hypotl(INFINITY, 0))));
EXPECT_STREQ("INFINITY", gc(xdtoa(hypotl(0, INFINITY))));
EXPECT_STREQ("INFINITY", gc(xdtoa(hypotl(INFINITY, NAN))));
EXPECT_STREQ("INFINITY", gc(xdtoa(hypotl(NAN, INFINITY))));
EXPECT_STREQ("INFINITY", gc(xdtoa(hypotl(INFINITY, INFINITY))));
}
BENCH(hypot, bench) {
volatile double a = 2;
volatile double b = 3;
EZBENCH2("hypotf", donothing, EXPROPRIATE(hypotf(a, b)));
EZBENCH2("hypot", donothing, EXPROPRIATE(hypot(a, b)));
EZBENCH2("hypotl", donothing, EXPROPRIATE(hypotl(a, b)));
}