Perform some code cleanup

This commit is contained in:
Justine Tunney 2023-05-15 16:32:10 -07:00
parent cc1732bc42
commit 210187cf77
No known key found for this signature in database
GPG key ID: BE714B4575D6E328
205 changed files with 1748 additions and 2595 deletions

View file

View file

@ -19,7 +19,6 @@
#include "libc/atomic.h"
#include "libc/calls/struct/timespec.h"
#include "libc/intrin/atomic.h"
#include "libc/intrin/intrin.h"
#include "libc/intrin/kprintf.h"
#include "libc/testlib/testlib.h"
#include "libc/thread/thread.h"

View file

@ -544,32 +544,32 @@ BENCH(bench_00_strcmp, bench) {
EZBENCH2("strcmp [2 diff]", donothing,
EXPROPRIATE(strcmp(VEIL("r", "hi"), VEIL("r", "there"))));
EZBENCH2("strcmp_pure [2 diff]", donothing,
EZBENCH2("scmppure [2 diff]", donothing,
EXPROPRIATE(strcmp_pure(VEIL("r", "hi"), VEIL("r", "there"))));
EZBENCH2("strcmp [2 dupe]", randomize_buf2str_dupe(2, data, dupe),
EXPROPRIATE(strcmp(VEIL("r", data), VEIL("r", dupe))));
EZBENCH2("strcmp_pure [2 dupe]", randomize_buf2str_dupe(2, data, dupe),
EZBENCH2("scmp_pure [2 dupe]", randomize_buf2str_dupe(2, data, dupe),
EXPROPRIATE(strcmp_pure(VEIL("r", data), VEIL("r", dupe))));
EZBENCH2("strcmp [4 dupe]", randomize_buf2str_dupe(4, data, dupe),
EXPROPRIATE(strcmp(VEIL("r", data), VEIL("r", dupe))));
EZBENCH2("strcmp_pure [4 dupe]", randomize_buf2str_dupe(4, data, dupe),
EZBENCH2("scmp_pure [4 dupe]", randomize_buf2str_dupe(4, data, dupe),
EXPROPRIATE(strcmp_pure(VEIL("r", data), VEIL("r", dupe))));
EZBENCH2("strcmp [8 dupe]", randomize_buf2str_dupe(8, data, dupe),
EXPROPRIATE(strcmp(VEIL("r", data), VEIL("r", dupe))));
EZBENCH2("strcmp_pure [8 dupe]", randomize_buf2str_dupe(8, data, dupe),
EZBENCH2("scmp_pure [8 dupe]", randomize_buf2str_dupe(8, data, dupe),
EXPROPRIATE(strcmp_pure(VEIL("r", data), VEIL("r", dupe))));
EZBENCH2("strcmp [sdupe]", randomize_buf2str_dupe(size, data, dupe),
EXPROPRIATE(strcmp(VEIL("r", data), VEIL("r", dupe))));
EZBENCH2("strcmp_pure [sdupe]", randomize_buf2str_dupe(size, data, dupe),
EZBENCH2("scmp_pure [sdupe]", randomize_buf2str_dupe(size, data, dupe),
EXPROPRIATE(strcmp_pure(VEIL("r", data), VEIL("r", dupe))));
EZBENCH2("strcmp [ldupe]", longstringislong_dupe(size, data, dupe),
EXPROPRIATE(strcmp(VEIL("r", data), VEIL("r", dupe))));
EZBENCH2("strcmp_pure [ldupe]", longstringislong_dupe(size, data, dupe),
EZBENCH2("scmp_pure [ldupe]", longstringislong_dupe(size, data, dupe),
EXPROPRIATE(strcmp_pure(VEIL("r", data), VEIL("r", dupe))));
}
@ -580,17 +580,17 @@ BENCH(bench_01_strcasecmp, bench) {
data = gc(malloc(size));
dupe = gc(malloc(size));
EZBENCH2("strcasecmp [identity]", longstringislong(size, data),
EZBENCH2("strcasecmp [iden]", longstringislong(size, data),
EXPROPRIATE(strcasecmp(VEIL("r", data), data)));
EZBENCH2("strcasecmp [sdupe]", randomize_buf2str_dupe(size, data, dupe),
EXPROPRIATE(strcasecmp(VEIL("r", data), VEIL("r", dupe))));
EZBENCH2("strcasecmp_pure [sdupe]", randomize_buf2str_dupe(size, data, dupe),
EZBENCH2("sccmp_pure [sdupe]", randomize_buf2str_dupe(size, data, dupe),
EXPROPRIATE(strcasecmp_pure(VEIL("r", data), VEIL("r", dupe))));
EZBENCH2("strcasecmp [ldupe]", longstringislong_dupe(size, data, dupe),
EXPROPRIATE(strcasecmp(VEIL("r", data), VEIL("r", dupe))));
EZBENCH2("strcasecmp_pure [ldupe]", longstringislong_dupe(size, data, dupe),
EZBENCH2("sccmp_pure [ldupe]", longstringislong_dupe(size, data, dupe),
EXPROPRIATE(strcasecmp_pure(VEIL("r", data), VEIL("r", dupe))));
}
@ -602,3 +602,49 @@ BENCH(memcmp, bench) {
EZBENCH2("memcmp 18", donothing,
EXPROPRIATE(memcmp("tough little ship", copy, 18)));
}
/* jart
* strcmp [identity] l: 3c 1ns m: 30c 10ns
* strcmp [2 diff] l: 4c 1ns m: 30c 10ns
* scmppure [2 diff] l: 3c 1ns m: 31c 10ns
* strcmp [2 dupe] l: 8c 3ns m: 39c 13ns
* scmp_pure [2 dupe] l: 6c 2ns m: 34c 11ns
* strcmp [4 dupe] l: 9c 3ns m: 40c 13ns
* scmp_pure [4 dupe] l: 9c 3ns m: 38c 12ns
* strcmp [8 dupe] l: 10c 3ns m: 40c 13ns
* scmp_pure [8 dupe] l: 11c 4ns m: 41c 13ns
* strcmp [sdupe] l: 87c 28ns m: 121c 39ns
* scmp_pure [sdupe] l: 188c 61ns m: 294c 95ns
* strcmp [ldupe] l: 3,458c 1,117ns m: 3,486c 1,126ns
* scmp_pure [ldupe] l: 11,441c 3,695ns m: 11,520c 3,721ns
* strcasecmp [iden] l: 3c 1ns m: 30c 10ns
* strcasecmp [sdupe] l: 105c 34ns m: 156c 50ns
* sccmp_pure [sdupe] l: 644c 208ns m: 963c 311ns
* strcasecmp [ldupe] l: 36,527c 11,798ns m: 36,582c 11,816ns
* sccmp_pure [ldupe] l: 365,880c 118,177ns m: 365,721c 118,125ns
* memcmp big l: 2,050c 662ns m: 2,093c 676ns
* memcmp 18 l: 6c 2ns m: 35c 11ns
*/
/* jart+intel
* strcmp [identity] l: 1c 0ns m: 28c 9ns
* strcmp [2 diff] l: 2c 1ns m: 29c 9ns
* scmppure [2 diff] l: 2c 1ns m: 29c 9ns
* strcmp [2 dupe] l: 8c 3ns m: 40c 13ns
* scmp_pure [2 dupe] l: 5c 2ns m: 32c 10ns
* strcmp [4 dupe] l: 9c 3ns m: 41c 13ns
* scmp_pure [4 dupe] l: 7c 2ns m: 34c 11ns
* strcmp [8 dupe] l: 10c 3ns m: 40c 13ns
* scmp_pure [8 dupe] l: 10c 3ns m: 39c 13ns
* strcmp [sdupe] l: 57c 18ns m: 87c 28ns
* scmp_pure [sdupe] l: 191c 62ns m: 224c 72ns
* strcmp [ldupe] l: 1,667c 538ns m: 1,708c 552ns
* scmp_pure [ldupe] l: 10,988c 3,549ns m: 11,055c 3,571ns
* strcasecmp [iden] l: 2c 1ns m: 31c 10ns
* strcasecmp [sdupe] l: 121c 39ns m: 160c 52ns
* sccmp_pure [sdupe] l: 684c 221ns m: 702c 227ns
* strcasecmp [ldupe] l: 34,325c 11,087ns m: 35,954c 11,613ns
* sccmp_pure [ldupe] l: 361,449c 116,746ns m: 366,022c 118,223ns
* memcmp big l: 2,040c 659ns m: 2,083c 673ns
* memcmp 18 l: 5c 2ns m: 35c 11ns
*/

View file

@ -18,6 +18,7 @@
*/
#include "libc/math.h"
#include "libc/mem/gc.internal.h"
#include "libc/testlib/ezbench.h"
#include "libc/testlib/testlib.h"
#include "libc/x/x.h"
@ -60,3 +61,9 @@ TEST(coshf, test) {
EXPECT_STREQ("INFINITY", gc(xdtoaf(_coshf(INFINITY))));
EXPECT_STREQ("INFINITY", gc(xdtoaf(_coshf(-INFINITY))));
}
BENCH(cosh, bench) {
EZBENCH2("cosh", donothing, _cosh(.7)); // ~6ns
EZBENCH2("coshf", donothing, _coshf(.7)); // ~5ns
EZBENCH2("coshl", donothing, _coshl(.7)); // ~25ns
}

View file

@ -64,7 +64,7 @@ TEST(exp, fun) {
ASSERT_STREQ("6.389056", _gc(xasprintf("%f", _exp(2.0) - 1.0)));
}
BENCH(_expl, bench) {
BENCH(expl, bench) {
EZBENCH2("exp", donothing, _exp(.7)); /* ~6ns */
EZBENCH2("expf", donothing, _expf(.7)); /* ~5ns */
EZBENCH2("expl", donothing, _expl(.7)); /* ~20ns */

View file

@ -0,0 +1,112 @@
/*-*- 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 2023 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/assert.h"
#include "libc/fmt/conv.h"
#include "libc/intrin/kprintf.h"
#include "libc/math.h"
#include "libc/mem/gc.h"
#include "libc/runtime/runtime.h"
#include "libc/testlib/ezbench.h"
#include "libc/testlib/testlib.h"
#include "libc/tinymath/tinymath.h"
#include "libc/x/x.h"
float remainderf2(float, float);
double _remainder(double, double) asm("remainder");
float _remainderf(float, float) asm("remainderf");
long double _remainderl(long double, long double) asm("remainderl");
TEST(remainder, test) {
EXPECT_TRUE(isnan(_remainder(7, NAN)));
EXPECT_TRUE(isnan(_remainder(NAN, 7)));
EXPECT_TRUE(isnan(_remainder(INFINITY, 7)));
EXPECT_TRUE(isnan(_remainder(7, 0)));
EXPECT_STREQ("-1", _gc(xdtoa(_remainder(7, 4))));
}
TEST(remainderf, test) {
EXPECT_TRUE(isnan(_remainderf(7, NAN)));
EXPECT_TRUE(isnan(_remainderf(NAN, 7)));
EXPECT_TRUE(isnan(_remainderf(INFINITY, 7)));
EXPECT_TRUE(isnan(_remainderf(7, 0)));
EXPECT_STREQ("-1", _gc(xdtoaf(_remainderf(7, 4))));
}
TEST(remainderl, test) {
EXPECT_TRUE(isnan(_remainderl(7, NAN)));
EXPECT_TRUE(isnan(_remainderl(NAN, 7)));
EXPECT_TRUE(isnan(_remainderl(INFINITY, 7)));
EXPECT_TRUE(isnan(_remainderl(7, 0)));
EXPECT_STREQ("-1", _gc(xdtoal(_remainderl(7, 4))));
}
static inline float i2f(uint32_t i) {
union {
uint32_t i;
float f;
} u = {i};
return u.f;
}
static inline uint32_t f2i(float f) {
union {
float f;
uint32_t i;
} u = {f};
return u.i;
}
BENCH(remainder, bench) {
EZBENCH2("remainder", donothing, _remainder(7, 3)); // ~20ns
EZBENCH2("remainderf", donothing, _remainderf(7, 3)); // ~20ns
EZBENCH2("remainderl", donothing, _remainderl(7, 3)); // ~169ns
}
#if 0
TEST(remainderf, brute) {
long i, j;
float a, b;
int lim = 100;
uint32_t x, y;
for (i = 0; i <= 0x100000000; i += 1048573) {
for (j = 0; j <= 0x100000000; j += 1048573) {
a = remainderf(i2f(i), i2f(j));
b = remainderf2(i2f(i), i2f(j));
if (isunordered(a, b)) {
assert(isnan(a) == isnan(b));
assert(isfinite(a) == isfinite(b));
continue;
}
x = f2i(a);
y = f2i(b);
if (abs(x - y) > 1) {
kprintf("bah %#lx %s %#lx %d\n", i, _gc(xdtoaf(i2f(i))), j,
_gc(xdtoaf(i2f(j))), abs(x - y));
kprintf(" %-12s %#x\n", _gc(xdtoaf(i2f(x))), x);
kprintf(" %-12s %#x\n", _gc(xdtoaf(i2f(y))), y);
if (!--lim) break;
}
}
kprintf("%ld\r", i);
}
if (lim != 100) {
exit(1);
}
}
#endif