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,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/bits/bits.h"
#include "libc/testlib/ezbench.h"
#include "libc/testlib/testlib.h"
TEST(bitreverse, test) {
EXPECT_EQ(0xde, bitreverse8(123));
EXPECT_EQ(0xde, (bitreverse8)(123));
EXPECT_EQ(0xde00, bitreverse16(123));
EXPECT_EQ(0xde00, (bitreverse16)(123));
EXPECT_EQ(0xde000000u, bitreverse32(123));
EXPECT_EQ(0xde000000u, (bitreverse32)(123));
}
BENCH(bitreverse, bench) {
EZBENCH2("bitreverse8 mac", donothing,
EXPROPRIATE(bitreverse8(CONCEAL("r", 123))));
EZBENCH2("bitreverse8 fun", donothing,
EXPROPRIATE((bitreverse8)(CONCEAL("r", 123))));
EZBENCH2("bitreverse16 mac", donothing,
EXPROPRIATE(bitreverse16(CONCEAL("r", 123))));
EZBENCH2("bitreverse16 fun", donothing,
EXPROPRIATE((bitreverse16)(CONCEAL("r", 123))));
EZBENCH2("bitreverse32 mac", donothing,
EXPROPRIATE(bitreverse32(CONCEAL("r", 123))));
EZBENCH2("bitreverse32 fun", donothing,
EXPROPRIATE((bitreverse32)(CONCEAL("r", (123)))));
}

66
test/libc/bits/bt_test.c Normal file
View file

@ -0,0 +1,66 @@
/*-*- 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/bits/bits.h"
#include "libc/testlib/testlib.h"
TEST(bt_uint16x4, test) {
uint16_t v[4] = {0};
EXPECT_FALSE(bt(v, 0));
EXPECT_FALSE(bt(v, 63));
EXPECT_FALSE(bts(v, 63));
EXPECT_TRUE(bt(v, 63));
EXPECT_TRUE(bts(v, 63));
}
TEST(bt_uint32x2, test) {
uint32_t v[2] = {0};
EXPECT_FALSE(bt(v, 0));
EXPECT_FALSE(bt(v, 63));
EXPECT_FALSE(bts(v, 63));
EXPECT_TRUE(bt(v, 63));
EXPECT_TRUE(bts(v, 63));
}
TEST(bt_uint64x1, test) {
uint64_t v = 0;
EXPECT_FALSE(bt(&v, 0));
EXPECT_FALSE(bt(&v, 63));
EXPECT_FALSE(bts(&v, 63));
EXPECT_TRUE(bt(&v, 63));
EXPECT_TRUE(bts(&v, 63));
}
TEST(bt_uint64, testPresent) {
uint64_t v = 1;
EXPECT_TRUE(bt(&v, 0));
EXPECT_FALSE(bt(&v, 63));
v = 0x8000000000000001;
EXPECT_TRUE(bt(&v, 0));
EXPECT_TRUE(bt(&v, 63));
}
TEST(bt_uint64, testPresent_avoidingDeadCodeElimination) {
volatile uint64_t v = 1;
EXPECT_TRUE(bt(&v, 0));
EXPECT_FALSE(bt(&v, 63));
v = 0x8000000000000001;
EXPECT_TRUE(bt(&v, 0));
EXPECT_TRUE(bt(&v, 63));
}

View file

@ -0,0 +1,50 @@
/*-*- 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/limits.h"
#include "libc/macros.h"
#include "libc/testlib/testlib.h"
TEST(TwosComplementBane, LiteralsThatAreLiterallyTheSameNumber) {
EXPECT_EQ(4, sizeof(INT_MIN));
EXPECT_EQ(8, sizeof(-2147483648));
EXPECT_TRUE(TYPE_SIGNED(-2147483648));
EXPECT_FALSE(TYPE_SIGNED(0x80000000));
EXPECT_FALSE(TYPE_SIGNED(-0x80000000));
}
TEST(ShiftArithmeticRight, DeclassifiedByGuySteele) {
EXPECT_EQ(-1u, SAR(-2u, 1u));
EXPECT_EQ(-1u, SAR(-1u, 1u));
EXPECT_EQ(0xc0000000u, SAR(0x80000000u, 1u));
}
TEST(RotateRight, Test) {
EXPECT_EQ(0x41122334u, ROR(0x11223344u, 4));
EXPECT_EQ(0x44112233u, ROR(0x11223344u, 8));
EXPECT_EQ(0x34411223u, ROR(0x11223344u, 12));
EXPECT_EQ(0x33441122u, ROR(0x11223344u, 16));
}
TEST(RotateLeft, Test) {
EXPECT_EQ(0x12233441u, ROL(0x11223344u, 4));
EXPECT_EQ(0x22334411u, ROL(0x11223344u, 8));
EXPECT_EQ(0x23344112u, ROL(0x11223344u, 12));
EXPECT_EQ(0x33441122u, ROL(0x11223344u, 16));
}

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/bits/morton.h"
#include "libc/nexgen32e/kcpuids.h"
#include "libc/str/str.h"
#include "libc/testlib/ezbench.h"
#include "libc/testlib/testlib.h"
TEST(morton, test) {
EXPECT_EQ(0, morton(0, 0));
EXPECT_EQ(1, morton(0, 1));
EXPECT_EQ(2, morton(1, 0));
EXPECT_EQ(3, morton(1, 1));
EXPECT_EQ(4, morton(0, 2));
EXPECT_EQ(~0ul, morton(~0ul, ~0ul));
EXPECT_EQ(0x7ffffffffffffffdul, morton(0x7ffffffeul, 0xfffffffful));
EXPECT_EQ(0b1010101000010101010, morton(0b0000001111, 0b1111000000));
}
TEST(unmorton, test) {
EXPECT_EQ(0, unmorton(0).ax);
EXPECT_EQ(0, unmorton(0).dx);
EXPECT_EQ(0, unmorton(1).ax);
EXPECT_EQ(1, unmorton(1).dx);
EXPECT_EQ(1, unmorton(2).ax);
EXPECT_EQ(0, unmorton(2).dx);
EXPECT_EQ(1, unmorton(3).ax);
EXPECT_EQ(1, unmorton(3).dx);
EXPECT_EQ(0, unmorton(4).ax);
EXPECT_EQ(2, unmorton(4).dx);
EXPECT_EQ(0xffffffffu, unmorton(~0ul).ax);
EXPECT_EQ(0xffffffffu, unmorton(~0ul).dx);
EXPECT_EQ(0x7ffffffeul, unmorton(0x7ffffffffffffffdul).ax);
EXPECT_EQ(0xfffffffful, unmorton(0x7ffffffffffffffdul).dx);
EXPECT_EQ(0b0000001111000000, unmorton(0b010101010000001010101).ax);
EXPECT_EQ(0b0000000000001111, unmorton(0b010101010000001010101).dx);
}
BENCH(morton, bench) {
EZBENCH2("morton", donothing,
EXPROPRIATE(morton(CONCEAL("r", 123), CONCEAL("r", 123))));
EZBENCH2("unmorton", donothing, EXPROPRIATE(unmorton(CONCEAL("r", 123))));
}

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 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/bits/bits.h"
#include "libc/log/check.h"
#include "libc/macros.h"
#include "libc/math.h"
#include "libc/nexgen32e/bsr.h"
#include "libc/testlib/testlib.h"
TEST(rounddown2pow, test) {
EXPECT_EQ(0, rounddown2pow(0));
EXPECT_EQ(1, rounddown2pow(1));
EXPECT_EQ(2, rounddown2pow(2));
EXPECT_EQ(2, rounddown2pow(3));
EXPECT_EQ(4, rounddown2pow(4));
EXPECT_EQ(PAGESIZE / 2, rounddown2pow(PAGESIZE - 1));
EXPECT_EQ(PAGESIZE, rounddown2pow(PAGESIZE));
EXPECT_EQ(PAGESIZE, rounddown2pow(PAGESIZE + 1));
EXPECT_EQ(PAGESIZE / 2, rounddown2pow(PAGESIZE - 1));
EXPECT_EQ(PAGESIZE, rounddown2pow(PAGESIZE));
EXPECT_EQ(PAGESIZE, rounddown2pow(PAGESIZE + 1));
}

View file

@ -0,0 +1,35 @@
/*-*- 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/bits/bits.h"
#include "libc/testlib/testlib.h"
TEST(roundup2log, test) {
EXPECT_EQ(0, roundup2log(0));
EXPECT_EQ(1, roundup2log(1));
EXPECT_EQ(1, roundup2log(2));
EXPECT_EQ(2, roundup2log(3));
EXPECT_EQ(2, roundup2log(4));
EXPECT_EQ(12, roundup2log(PAGESIZE - 1));
EXPECT_EQ(12, roundup2log(PAGESIZE));
EXPECT_EQ(13, roundup2log(PAGESIZE + 1));
EXPECT_EQ(12, roundup2log(PAGESIZE - 1));
EXPECT_EQ(12, roundup2log(PAGESIZE));
EXPECT_EQ(13, roundup2log(PAGESIZE + 1));
}

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 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/bits/bits.h"
#include "libc/log/check.h"
#include "libc/macros.h"
#include "libc/math.h"
#include "libc/nexgen32e/bsr.h"
#include "libc/testlib/testlib.h"
TEST(roundup2pow, test) {
EXPECT_EQ(0, roundup2pow(0));
EXPECT_EQ(1, roundup2pow(1));
EXPECT_EQ(2, roundup2pow(2));
EXPECT_EQ(4, roundup2pow(3));
EXPECT_EQ(4, roundup2pow(4));
EXPECT_EQ(PAGESIZE, roundup2pow(PAGESIZE - 1));
EXPECT_EQ(PAGESIZE, roundup2pow(PAGESIZE));
EXPECT_EQ(PAGESIZE * 2, roundup2pow(PAGESIZE + 1));
EXPECT_EQ(PAGESIZE, roundup2pow(PAGESIZE - 1));
EXPECT_EQ(PAGESIZE, roundup2pow(PAGESIZE));
EXPECT_EQ(PAGESIZE * 2, roundup2pow(PAGESIZE + 1));
}

54
test/libc/bits/test.mk Normal file
View file

@ -0,0 +1,54 @@
#-*-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_BITS
TEST_LIBC_BITS_SRCS := $(wildcard test/libc/bits/*.c)
TEST_LIBC_BITS_SRCS_TEST = $(filter %_test.c,$(TEST_LIBC_BITS_SRCS))
TEST_LIBC_BITS_COMS = $(TEST_LIBC_BITS_OBJS:%.o=%.com)
TEST_LIBC_BITS_OBJS = \
$(TEST_LIBC_BITS_SRCS:%=o/$(MODE)/%.zip.o) \
$(TEST_LIBC_BITS_SRCS:%.c=o/$(MODE)/%.o)
TEST_LIBC_BITS_BINS = \
$(TEST_LIBC_BITS_COMS) \
$(TEST_LIBC_BITS_COMS:%=%.dbg)
TEST_LIBC_BITS_TESTS = \
$(TEST_LIBC_BITS_SRCS_TEST:%.c=o/$(MODE)/%.com.ok)
TEST_LIBC_BITS_CHECKS = \
$(TEST_LIBC_BITS_SRCS_TEST:%.c=o/$(MODE)/%.com.runs)
TEST_LIBC_BITS_DIRECTDEPS = \
LIBC_X \
LIBC_BITS \
LIBC_NEXGEN32E \
LIBC_STUBS \
LIBC_TESTLIB
TEST_LIBC_BITS_DEPS := \
$(call uniq,$(foreach x,$(TEST_LIBC_BITS_DIRECTDEPS),$($(x))))
o/$(MODE)/test/libc/bits/bits.pkg: \
$(TEST_LIBC_BITS_OBJS) \
$(foreach x,$(TEST_LIBC_BITS_DIRECTDEPS),$($(x)_A).pkg)
o/$(MODE)/test/libc/bits/%.com.dbg: \
$(TEST_LIBC_BITS_DEPS) \
o/$(MODE)/test/libc/bits/%.o \
o/$(MODE)/test/libc/bits/bits.pkg \
$(LIBC_TESTMAIN) \
$(CRT) \
$(APE)
@$(APELINK)
$(TEST_LIBC_BITS_OBJS): \
DEFAULT_CCFLAGS += \
-fno-builtin
.PHONY: o/$(MODE)/test/libc/bits
o/$(MODE)/test/libc/bits: \
$(TEST_LIBC_BITS_BINS) \
$(TEST_LIBC_BITS_CHECKS)

View file

@ -0,0 +1,31 @@
/*-*- 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/bits/safemacros.h"
#include "libc/testlib/testlib.h"
TEST(unsignedsubtract, testMacro) {
EXPECT_EQ(5ul, unsignedsubtract(7ul, 2ul));
EXPECT_EQ(18ul, unsignedsubtract(2ul, 0xfffffffffffffff0ul));
}
TEST(unsignedsubtract, testLinked) {
EXPECT_EQ(5ul, (unsignedsubtract)(7ul, 2ul));
EXPECT_EQ(18ul, (unsignedsubtract)(2ul, 0xfffffffffffffff0ul));
}