2021-02-21 19:06:18 +00:00
|
|
|
|
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│
|
2023-12-08 03:11:56 +00:00
|
|
|
|
│ vi: set et ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi │
|
2021-02-21 19:06:18 +00:00
|
|
|
|
╞══════════════════════════════════════════════════════════════════════════════╡
|
|
|
|
|
│ 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"
|
2024-01-08 18:07:35 +00:00
|
|
|
|
#include "libc/mem/gc.h"
|
2021-02-21 19:06:18 +00:00
|
|
|
|
#include "libc/testlib/ezbench.h"
|
|
|
|
|
#include "libc/testlib/testlib.h"
|
|
|
|
|
#include "libc/x/x.h"
|
|
|
|
|
|
2023-05-12 02:56:33 +00:00
|
|
|
|
double _hypot(double, double) asm("hypot");
|
|
|
|
|
float _hypotf(float, float) asm("hypotf");
|
|
|
|
|
long double _hypotl(long double, long double) asm("hypotl");
|
2021-02-26 02:30:17 +00:00
|
|
|
|
|
2021-02-21 19:06:18 +00:00
|
|
|
|
TEST(hypot, test) {
|
2023-05-12 02:56:33 +00:00
|
|
|
|
EXPECT_STREQ("0", gc(xdtoa(_hypot(0, 0))));
|
|
|
|
|
EXPECT_STREQ("3", gc(xdtoa(_hypot(3, 0))));
|
|
|
|
|
EXPECT_STREQ("3", gc(xdtoa(_hypot(0, 3))));
|
|
|
|
|
EXPECT_STREQ("5", gc(xdtoa(_hypot(3, 4))));
|
|
|
|
|
EXPECT_STREQ("5", gc(xdtoa(_hypot(4, 3))));
|
|
|
|
|
EXPECT_STREQ("5", gc(xdtoa(_hypot(3, -4))));
|
|
|
|
|
EXPECT_STREQ("5", gc(xdtoa(_hypot(-4, 3))));
|
|
|
|
|
EXPECT_STREQ("5", gc(xdtoa(_hypot(-3, 4))));
|
|
|
|
|
EXPECT_STREQ("5", gc(xdtoa(_hypot(4, -3))));
|
|
|
|
|
EXPECT_STREQ("5", gc(xdtoa(_hypot(-3, -4))));
|
|
|
|
|
EXPECT_STREQ("5", gc(xdtoa(_hypot(-4, -3))));
|
|
|
|
|
EXPECT_STREQ("1.4142135623731", gc(xdtoa(_hypot(1, 1))));
|
|
|
|
|
EXPECT_STREQ("1.4142135623731", gc(xdtoa(_hypot(1, -1))));
|
|
|
|
|
EXPECT_STREQ("1.4142135623731", gc(xdtoa(_hypot(-1, 1))));
|
|
|
|
|
EXPECT_STREQ("1.41421362601271", gc(xdtoa(_hypot(1.0000001, .99999999))));
|
|
|
|
|
EXPECT_STREQ("1.41421362601271", gc(xdtoa(_hypot(.99999999, 1.0000001))));
|
|
|
|
|
EXPECT_STREQ("1.4142135623731e+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))));
|
2021-02-21 19:06:18 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TEST(hypotf, test) {
|
2023-05-12 02:56:33 +00:00
|
|
|
|
EXPECT_STREQ("0", gc(xdtoa(_hypotf(0, 0))));
|
|
|
|
|
EXPECT_STREQ("3", gc(xdtoa(_hypotf(3, 0))));
|
|
|
|
|
EXPECT_STREQ("3", gc(xdtoa(_hypotf(0, 3))));
|
|
|
|
|
EXPECT_STREQ("5", gc(xdtoa(_hypotf(3, 4))));
|
|
|
|
|
EXPECT_STREQ("5", gc(xdtoa(_hypotf(4, 3))));
|
|
|
|
|
EXPECT_STREQ("5", gc(xdtoa(_hypotf(3, -4))));
|
|
|
|
|
EXPECT_STREQ("5", gc(xdtoa(_hypotf(-4, 3))));
|
|
|
|
|
EXPECT_STREQ("5", gc(xdtoa(_hypotf(-3, 4))));
|
|
|
|
|
EXPECT_STREQ("5", gc(xdtoa(_hypotf(4, -3))));
|
|
|
|
|
EXPECT_STREQ("5", gc(xdtoa(_hypotf(-3, -4))));
|
|
|
|
|
EXPECT_STREQ("5", gc(xdtoa(_hypotf(-4, -3))));
|
|
|
|
|
EXPECT_STREQ("1.41421", gc(xdtoaf(_hypotf(1, 1))));
|
|
|
|
|
EXPECT_STREQ("1.41421", gc(xdtoaf(_hypotf(1, -1))));
|
|
|
|
|
EXPECT_STREQ("1.41421", gc(xdtoaf(_hypotf(-1, 1))));
|
|
|
|
|
EXPECT_STREQ("1.41421", gc(xdtoaf(_hypotf(1.000001, 0.999999))));
|
|
|
|
|
EXPECT_STREQ("1.41421", gc(xdtoaf(_hypotf(0.999999, 1.000001))));
|
|
|
|
|
EXPECT_STREQ("1.41421e+38", gc(xdtoaf(_hypotf(1e38, 1e38))));
|
|
|
|
|
EXPECT_STREQ("NAN", gc(xdtoaf(_hypotf(0, NAN))));
|
|
|
|
|
EXPECT_STREQ("NAN", gc(xdtoaf(_hypotf(NAN, 0))));
|
|
|
|
|
EXPECT_STREQ("NAN", gc(xdtoaf(_hypotf(NAN, NAN))));
|
|
|
|
|
EXPECT_STREQ("INFINITY", gc(xdtoaf(_hypotf(INFINITY, 0))));
|
|
|
|
|
EXPECT_STREQ("INFINITY", gc(xdtoaf(_hypotf(0, INFINITY))));
|
|
|
|
|
EXPECT_STREQ("INFINITY", gc(xdtoaf(_hypotf(INFINITY, NAN))));
|
|
|
|
|
EXPECT_STREQ("INFINITY", gc(xdtoaf(_hypotf(NAN, INFINITY))));
|
|
|
|
|
EXPECT_STREQ("INFINITY", gc(xdtoaf(_hypotf(INFINITY, INFINITY))));
|
2021-02-21 19:06:18 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-02-21 20:53:01 +00:00
|
|
|
|
TEST(hypotll, test) {
|
2023-05-12 02:56:33 +00:00
|
|
|
|
EXPECT_STREQ("0", gc(xdtoal(_hypotl(0, 0))));
|
|
|
|
|
EXPECT_STREQ("3", gc(xdtoal(_hypotl(3, 0))));
|
|
|
|
|
EXPECT_STREQ("3", gc(xdtoal(_hypotl(0, 3))));
|
|
|
|
|
EXPECT_STREQ("5", gc(xdtoal(_hypotl(3, 4))));
|
|
|
|
|
EXPECT_STREQ("5", gc(xdtoal(_hypotl(4, 3))));
|
|
|
|
|
EXPECT_STREQ("5", gc(xdtoal(_hypotl(3, -4))));
|
|
|
|
|
EXPECT_STREQ("5", gc(xdtoal(_hypotl(-4, 3))));
|
|
|
|
|
EXPECT_STREQ("5", gc(xdtoal(_hypotl(-3, 4))));
|
|
|
|
|
EXPECT_STREQ("5", gc(xdtoal(_hypotl(4, -3))));
|
|
|
|
|
EXPECT_STREQ("5", gc(xdtoal(_hypotl(-3, -4))));
|
|
|
|
|
EXPECT_STREQ("5", gc(xdtoal(_hypotl(-4, -3))));
|
|
|
|
|
EXPECT_STREQ("1.414213562373095", gc(xdtoal(_hypotl(1, 1))));
|
|
|
|
|
EXPECT_STREQ("1.414213562373095", gc(xdtoal(_hypotl(1, -1))));
|
|
|
|
|
EXPECT_STREQ("1.414213562373095", gc(xdtoal(_hypotl(-1, 1))));
|
|
|
|
|
EXPECT_STREQ("1.414213626012708", gc(xdtoal(_hypotl(1.0000001, .99999999))));
|
|
|
|
|
EXPECT_STREQ("1.414213626012708", gc(xdtoal(_hypotl(.99999999, 1.0000001))));
|
|
|
|
|
EXPECT_STREQ("1.414213562373095e+4931",
|
|
|
|
|
gc(xdtoal(_hypotl(1e4931L, 1e4931L))));
|
|
|
|
|
EXPECT_STREQ("NAN", gc(xdtoal(_hypotl(0, NAN))));
|
|
|
|
|
EXPECT_STREQ("NAN", gc(xdtoal(_hypotl(NAN, 0))));
|
|
|
|
|
EXPECT_STREQ("NAN", gc(xdtoal(_hypotl(NAN, NAN))));
|
|
|
|
|
EXPECT_STREQ("INFINITY", gc(xdtoal(_hypotl(INFINITY, 0))));
|
|
|
|
|
EXPECT_STREQ("INFINITY", gc(xdtoal(_hypotl(0, INFINITY))));
|
|
|
|
|
EXPECT_STREQ("INFINITY", gc(xdtoal(_hypotl(INFINITY, NAN))));
|
|
|
|
|
EXPECT_STREQ("INFINITY", gc(xdtoal(_hypotl(NAN, INFINITY))));
|
|
|
|
|
EXPECT_STREQ("INFINITY", gc(xdtoal(_hypotl(INFINITY, INFINITY))));
|
2021-02-21 19:06:18 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-08-16 22:26:31 +00:00
|
|
|
|
/*
|
2023-05-12 02:56:33 +00:00
|
|
|
|
_hypot (musl) l: 53𝑐 17𝑛𝑠 m: 85𝑐 27𝑛𝑠
|
|
|
|
|
_hypot l: 39𝑐 13𝑛𝑠 m: 66𝑐 21𝑛𝑠
|
|
|
|
|
_hypotf l: 25𝑐 8𝑛𝑠 m: 55𝑐 18𝑛𝑠
|
|
|
|
|
_hypotl l: 43𝑐 14𝑛𝑠 m: 74𝑐 24𝑛𝑠
|
2021-08-16 22:26:31 +00:00
|
|
|
|
*/
|
|
|
|
|
|
2023-05-12 02:56:33 +00:00
|
|
|
|
BENCH(_hypot, bench) {
|
2021-02-21 19:06:18 +00:00
|
|
|
|
volatile double a = 2;
|
|
|
|
|
volatile double b = 3;
|
2023-07-26 20:54:49 +00:00
|
|
|
|
EZBENCH2("hypotf", donothing, __expropriate(_hypotf(a, b)));
|
|
|
|
|
EZBENCH2("hypot", donothing, __expropriate(_hypot(a, b)));
|
|
|
|
|
EZBENCH2("hypotl", donothing, __expropriate(_hypotl(a, b)));
|
2021-02-21 19:06:18 +00:00
|
|
|
|
}
|