mirror of
https://github.com/jart/cosmopolitan.git
synced 2025-05-23 05:42:29 +00:00
Apply fixes and speedups
This commit is contained in:
parent
7521bf9e73
commit
725f4d79f6
36 changed files with 682 additions and 334 deletions
112
test/libc/bits/bextra_test.c
Normal file
112
test/libc/bits/bextra_test.c
Normal 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 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/bits/bits.h"
|
||||
#include "libc/testlib/ezbench.h"
|
||||
#include "libc/testlib/testlib.h"
|
||||
|
||||
unsigned P[] = {
|
||||
// 33333222222222111111111000000000
|
||||
0b000011000000010000000001000000000, //
|
||||
// 76666666665555555554444444443333
|
||||
0b010000001100000001010000001000000, //
|
||||
0b000101000000100100000100000000011, //
|
||||
0b010000001101000001100000001011000, //
|
||||
0b000100010000100000000011110000011, //
|
||||
0b010100001010000001001100001001000, //
|
||||
0b000011000000010111000010110000010, //
|
||||
0b011000000110110000110100000110010, //
|
||||
0b000001111100001111000001110100001, //
|
||||
0b000011000100010000100001000100000, //
|
||||
0b010001001100001001010001001000001, //
|
||||
0b010101000010100100010100000010011, //
|
||||
0b010000101101000101100000101011000, //
|
||||
0b001100010001100000001011110001011, //
|
||||
0b010100011010000011001100011001000, //
|
||||
0b000111000000110111000110110000110, //
|
||||
0b011000001110110001110100001110010, //
|
||||
0b000011111100011111000011110100011, //
|
||||
0b000011001000010001000001001000000, //
|
||||
0b010010001100010001010010001000010, //
|
||||
0b000101000100100100100100000100011, //
|
||||
0b010001001101001001100001001011001, //
|
||||
0b010100010010100000010011110010011, //
|
||||
0b010100101010000101001100101001000, //
|
||||
0b001011000001010111001010110001010, //
|
||||
0b011000010110110010110100010110010, //
|
||||
0b000101111100101111000101110100101, //
|
||||
0b000011001100010001100001001100000, //
|
||||
0b010011001100011001010011001000011, //
|
||||
0b010101000110100100110100000110011, //
|
||||
0b010001101101001101100001101011001, //
|
||||
0b011100010011100000011011110011011, //
|
||||
0b010100111010000111001100111001000, //
|
||||
0b001111000001110111001110110001110, //
|
||||
0b011000011110110011110100011110010, //
|
||||
0b000111111100111111000111110100111, //
|
||||
0b000011010000010010000001010000000, //
|
||||
0b010100001100100001010100001000100, //
|
||||
0b000101001000100101000100001000011, //
|
||||
0b010010001101010001100010001011010, //
|
||||
0b000100010100100000100011110100011, //
|
||||
0b010101001010001001001101001001001, //
|
||||
0b010011000010010111010010110010010, //
|
||||
0b011000100110110100110100100110010, //
|
||||
0b001001111101001111001001110101001, //
|
||||
0b000011010100010010100001010100000, //
|
||||
0b010101001100101001010101001000101, //
|
||||
0b010101001010100101010100001010011, //
|
||||
0b010010101101010101100010101011010, //
|
||||
0b001100010101100000101011110101011, //
|
||||
0b010101011010001011001101011001001, //
|
||||
0b010111000010110111010110110010110, //
|
||||
0b011000101110110101110100101110010, //
|
||||
0b001011111101011111001011110101011, //
|
||||
0b000011011000010011000001011000000, //
|
||||
0b010110001100110001010110001000110, //
|
||||
0b000101001100100101100100001100011, //
|
||||
0b010011001101011001100011001011011, //
|
||||
0b010100010110100000110011110110011, //
|
||||
0b010101101010001101001101101001001, //
|
||||
0b011011000011010111011010110011010, //
|
||||
0b011000110110110110110100110110010, //
|
||||
0b001101111101101111001101110101101, //
|
||||
0b000011011100010011100001011100000, //
|
||||
0b010111001100111001010111001000111, //
|
||||
0b010101001110100101110100001110011, //
|
||||
0b010011101101011101100011101011011, //
|
||||
0b011100010111100000111011110111011, //
|
||||
0b010101111010001111001101111001001, //
|
||||
0b011111000011110111011110110011110, //
|
||||
0b011000111110110111110100111110010, //
|
||||
0b001111111101111111001111110101111, //
|
||||
0b000000000000000000000000100000000, //
|
||||
};
|
||||
|
||||
TEST(bextra, 9bit) {
|
||||
int i;
|
||||
for (i = 4; i < 257; ++i) {
|
||||
ASSERT_EQ(i, bextra(P, i, 9));
|
||||
}
|
||||
}
|
||||
|
||||
BENCH(bextra, bench) {
|
||||
EZBENCH2("bextra 0/32", donothing, bextra(P, 0, 32));
|
||||
EZBENCH2("bextra 1/31", donothing, bextra(P, 1, 31));
|
||||
EZBENCH2("bextra 1/32", donothing, bextra(P, 1, 32));
|
||||
}
|
|
@ -74,7 +74,7 @@ TEST(memmove, bighug) {
|
|||
rngset(a, 6291456, 0, 0);
|
||||
memcpy(b, a, 6291456);
|
||||
ASSERT_EQ(a + o2, golden(a + o2, a + o1, N[i]));
|
||||
ASSERT_EQ(b + o2, memmove(b + o2, b + o1, N[i]));
|
||||
ASSERT_EQ(b + o2, memmove(b + o2, b + o1, N[i]), "%d", N[i]);
|
||||
ASSERT_EQ(0, timingsafe_bcmp(a, b, 6291456), "%d %d %d", o1, o2, i);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -20,9 +20,40 @@
|
|||
#include "libc/bits/bits.h"
|
||||
#include "libc/macros.internal.h"
|
||||
#include "libc/mem/mem.h"
|
||||
#include "libc/nexgen32e/bsr.h"
|
||||
#include "libc/rand/rand.h"
|
||||
#include "libc/runtime/gc.internal.h"
|
||||
#include "libc/str/str.h"
|
||||
#include "libc/testlib/ezbench.h"
|
||||
#include "libc/testlib/testlib.h"
|
||||
|
||||
int CompareLong(const void *a, const void *b) {
|
||||
const long *x = a;
|
||||
const long *y = b;
|
||||
if (*x < *y) return -1;
|
||||
if (*x > *y) return +1;
|
||||
return 0;
|
||||
}
|
||||
|
||||
unsigned long doge(unsigned long x) {
|
||||
unsigned long t = 1;
|
||||
while (t < x - t) {
|
||||
t += t;
|
||||
}
|
||||
return t;
|
||||
}
|
||||
|
||||
unsigned long B(unsigned long x) {
|
||||
return 1ul << bsrl(x - 1);
|
||||
}
|
||||
|
||||
TEST(eh, eu) {
|
||||
int i;
|
||||
for (i = 2; i < 9999; ++i) {
|
||||
ASSERT_EQ(doge(i), B(i), "%d", i);
|
||||
}
|
||||
}
|
||||
|
||||
TEST(qsort, test) {
|
||||
const int32_t A[][2] = {{4, 'a'}, {65, 'b'}, {2, 'c'}, {-31, 'd'},
|
||||
{0, 'e'}, {99, 'f'}, {2, 'g'}, {83, 'h'},
|
||||
|
@ -36,3 +67,24 @@ TEST(qsort, test) {
|
|||
EXPECT_EQ(0, memcmp(M, B, sizeof(B)));
|
||||
free(M);
|
||||
}
|
||||
|
||||
TEST(longsort, test) {
|
||||
size_t n = 5000;
|
||||
long *a = gc(calloc(n, sizeof(long)));
|
||||
long *b = gc(calloc(n, sizeof(long)));
|
||||
rngset(a, n * sizeof(long), 0, 0);
|
||||
memcpy(b, a, n * sizeof(long));
|
||||
qsort(a, n, sizeof(long), CompareLong);
|
||||
longsort(b, n);
|
||||
ASSERT_EQ(0, memcmp(b, a, n * sizeof(long)));
|
||||
}
|
||||
|
||||
BENCH(qsort, bench) {
|
||||
size_t n = 1000;
|
||||
long *p1 = gc(malloc(n * sizeof(long)));
|
||||
long *p2 = gc(malloc(n * sizeof(long)));
|
||||
rngset(p1, n * sizeof(long), 0, 0);
|
||||
EZBENCH2("qsort", memcpy(p2, p1, n * sizeof(long)),
|
||||
qsort(p2, n, sizeof(long), CompareLong));
|
||||
EZBENCH2("longsort", memcpy(p2, p1, n * sizeof(long)), longsort(p2, n));
|
||||
}
|
|
@ -24,6 +24,7 @@ TEST_LIBC_RUNTIME_CHECKS = \
|
|||
|
||||
TEST_LIBC_RUNTIME_DIRECTDEPS = \
|
||||
LIBC_ALG \
|
||||
LIBC_BITS \
|
||||
LIBC_CALLS \
|
||||
LIBC_FMT \
|
||||
LIBC_INTRIN \
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue