Add chibicc

This program popped up on Hacker News recently. It's the only modern
compiler I've ever seen that doesn't have dependencies and is easily
modified. So I added all of the missing GNU extensions I like to use
which means it might be possible soon to build on non-Linux and have
third party not vendor gcc binaries.
This commit is contained in:
Justine Tunney 2020-12-05 12:20:41 -08:00
parent e44a0cf6f8
commit 8da931a7f6
298 changed files with 19493 additions and 11950 deletions

View file

@ -1,88 +0,0 @@
/*-*- 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 "dsp/core/core.h"
#include "libc/math.h"
#include "libc/stdio/stdio.h"
#include "libc/str/str.h"
#include "libc/testlib/ezbench.h"
#include "libc/testlib/testlib.h"
#include "libc/time/time.h"
#include "tool/viz/lib/formatstringtable-testlib.h"
TEST(dct, test) {
float M[8][8] /* clang-format off */ = {
{.509804, .513725, .094118, .219608, .027451, .294118, .172549, .658824},
{.019608, .070588, .196078, .015686, .172549, .458824, .713725, .294118},
{.380392, .341176, .235294, .737255, .741176, .968627, .607843, .12549},
{.560784, .843137, .639216, .929412, .756863, .113725, .643137, .435294},
{.878431, .576471, .737255, .356863, .8, .878431, .682353, .866667},
{.780392, .070588, .866667, .607843, .792157, .47451, .427451, .043137},
{.133333, .976471, .698039, .662745, .035294, .207843, .831373, .627451},
{.313725, .580392, .858824, .631373, .784314, .972549, .27451, .94902},
} /* clang-format on */;
dctjpeg(M);
EXPECT_FLTMATRIXEQ(5, rint, 8, 8, M, "\n\
32.86666 -1.46274 -1.4456 -.43895 -1.17255 .19084 .05736 .01672\n\
-9.41551 -2.72135 3.7228 5.47448 .74604 .91144 -1.22542 -.41829\n\
-6.32875 -4.21755 4.42546 -3.86307 -1.93691 -2.1173 1.00377 -1.0752\n\
-2.58232 3.67887 5.65331 -.25753 .89732 1.09837 .93163 .61133\n\
4.23922 1.36747 3.29469 -1.63407 2.78039 -3.0021 .7602 -.21367\n\
-.11643 3.93022 .80678 -3.70514 .13347 .54381 -2.15087 -.52343\n\
.64248 1.19093 -2.94494 2.66037 1.6624 .04414 .99807 .00514\n\
.61622 -.76318 .75918 .41939 -.38075 -.30623 .09867 -.19237");
}
/* TEST(dct, test2) { */
/* float M[8][8] /\* clang-format off *\/ = { */
/* {.5,.5,.5,.5,.5,.5,.5,.5}, */
/* {.5,.5,.5,.5,.5,.5,.5,.5}, */
/* {.5,.5,.5,.5,.5,.5,.5,.5}, */
/* {.5,.5,.5,.5,.5,.5,.5,.5}, */
/* {.5,.5,.5,.5,.5,.5,.5,.5}, */
/* {.5,.5,.5,.5,.5,.5,.5,.5}, */
/* {.5,.5,.5,.5,.5,.5,.5,.5}, */
/* {.5,.5,.5,.5,.5,.5,.5,.5}, */
/* } /\* clang-format on *\/; */
/* dctjpeg(M); */
/* EXPECT_FLTMATRIXEQ(5, rint, 8, 8, M, "\n\ */
/* 32.86666 -1.46274 -1.4456 -.43895 -1.17255 .19084 .05736 .01672\n\ */
/* -9.41551 -2.72135 3.7228 5.47448 .74604 .91144 -1.22542 -.41829\n\ */
/* -6.32875 -4.21755 4.42546 -3.86307 -1.93691 -2.1173 1.00377 -1.0752\n\ */
/* -2.58232 3.67887 5.65331 -.25753 .89732 1.09837 .93163 .61133\n\ */
/* 4.23922 1.36747 3.29469 -1.63407 2.78039 -3.0021 .7602 -.21367\n\ */
/* -.11643 3.93022 .80678 -3.70514 .13347 .54381 -2.15087 -.52343\n\ */
/* .64248 1.19093 -2.94494 2.66037 1.6624 .04414 .99807 .00514\n\ */
/* .61622 -.76318 .75918 .41939 -.38075 -.30623 .09867 -.19237"); */
/* } */
BENCH(dct, bench) {
float M[8][8] /* clang-format off */ = {
{.101961, .486275, .082353, .082353, .937255, .321569, .14902, .270588},
{.384314, .062745, .152941, .003922, .921569, .015686, .247059, 0},
{.760784, .023529, .411765, .443137, .862745, .85098, .435294, .631373},
{.309804, .141176, .54902, .984314, .478431, .6, .364706, .643137},
{.780392, .811765, .458824, .964706, .439216, .941176, .321569, .313725},
{.596078, .207843, .133333, .345098, .278431, .192157, .52549, .627451},
{.952941, .090196, .290196, .717647, .686275, .713725, .54902, .411765},
{.109804, .121569, .403922, .27451, .470588, .007843, .168627, .105882},
} /* clang-format on */;
void *data = tgc(tmalloc(sizeof(M)));
EZBENCH2("dct", memcpy(data, M, sizeof(M)), EXPROPRIATE(dctjpeg(data)));
}

View file

@ -29,18 +29,18 @@ TEST(GetChromaticAdaptationMatrix, testSuperiorIlluminationBannedInCalifornia) {
double M[3][3];
GetChromaticAdaptationMatrix(M, kIlluminantD65, kIlluminantA);
EXPECT_DBLMATRIXEQ(5, rint, 3, 3, M, "\n\
1.21646 .11099 -.15493\n\
.15333 .91523 -.056\n\
-.02395 .0359 .31475");
1.2165 .11099 -.15493\n\
.15333 .91523 -.055995\n\
-.023947 .035898 .31475");
}
TEST(GetChromaticAdaptationMatrix, testD65ToD50_soWeCanCieLab) {
double M[3][3];
GetChromaticAdaptationMatrix(M, kIlluminantD65, kIlluminantD50);
EXPECT_DBLMATRIXEQ(6, rint, 3, 3, M, "\n\
1.047811 .022887 -.050127\n\
.029542 .990484 -.017049\n\
-.009234 .015044 .752132");
1.04781 .0228866 -.050127\n\
.0295424 .990484 -.0170491\n\
-.00923449 .0150436 .752132");
}
BENCH(GetChromaticAdaptationMatrix, bench) {

View file

@ -28,9 +28,9 @@ TEST(inv3, test) {
double M[3][3];
inv3(M, kBradford, det3(kBradford));
EXPECT_DBLMATRIXEQ(7, rint, 3, 3, M, "\n\
.9869929 -.1470543 .1599627\n\
.4323053 .5183603 .0492912\n\
-.0085287 .0400428 .9684867");
.9869929 -.1470543 .1599627\n\
.4323053 .5183603 .04929123\n\
-.008528665 .04004282 .9684867");
}
BENCH(inv3, bench) {

View file

@ -31,7 +31,7 @@ unsigned windex$k8(short *, size_t) hidden;
unsigned windex$avx2(short *, size_t) hidden;
unsigned windex$sse4(short *, size_t) hidden;
const short kW[64] aligned(32) = {
const short kW[64] forcealign(32) = {
8281, 3883, 1365, 1786, 9006, 3681, 5563, 8013, 5787, 9063, 2923,
3564, 6122, 32, 1436, 0741, 7957, 9219, 1320, 2083, 1904, 8905,
2465, 9122, 9563, 1290, 4474, 3988, 9920, 8325, 1088, 2915, 33,
@ -40,13 +40,13 @@ const short kW[64] aligned(32) = {
1730, 2041, 7707, 5096, 6876, 1324, 1242, 5283, 0x7fff,
};
const short kW2[32] aligned(32) = {
const short kW2[32] forcealign(32) = {
8281, 1, 1365, 1786, 9006, 3681, 5563, 8013, 5787, 9063, 2923,
3564, 6122, 32, 1436, 0741, 7957, 9219, 1320, 2083, 1904, 8905,
2465, 9122, 9563, 1290, 4474, 3988, 9920, 8325, 1088, 2915,
};
const short kW3[64] aligned(32) = {
const short kW3[64] forcealign(32) = {
8281, 0x7fff, 1365, 1786, 9006, 3681, 5563, 8013, 5787, 9063, 2923,
3564, 6122, 32, 1436, 0741, 7957, 9219, 1320, 2083, 1904, 8905,
2465, 9122, 9563, 1290, 4474, 3988, 9920, 8325, 1088, 2915, 33,
@ -64,7 +64,7 @@ const short kW3[64] aligned(32) = {
})
TEST(windex, testRealWorldPicks) {
const short kPicks[96] aligned(32) = {
const short kPicks[96] forcealign(32) = {
103, 85, 145, 146, 121, 103, 145, 187, 146, 189,
121, 103, 139, 121, 63, 105, 105, 147, 60, 103,
103, 146, 121, 103, 139, 121, 139, 121, 157, 139,

View file

@ -21,10 +21,21 @@
#include "libc/conv/conv.h"
#include "libc/testlib/testlib.h"
TEST(strtoimax, testZero) { EXPECT_EQ(0, strtoimax("0", NULL, 0)); }
TEST(strtoimax, testDecimal) { EXPECT_EQ(-123, strtoimax("-123", NULL, 0)); }
TEST(strtoimax, testHex) { EXPECT_EQ(-255, strtoimax("-0xff", NULL, 0)); }
TEST(strtoimax, testOctal) { EXPECT_EQ(-123, strtoimax("-0173", NULL, 0)); }
TEST(strtoimax, testZero) {
EXPECT_EQ(0, strtoimax("0", NULL, 0));
}
TEST(strtoimax, testDecimal) {
EXPECT_EQ(-123, strtoimax("-123", NULL, 0));
}
TEST(strtoimax, testHex) {
EXPECT_EQ(-255, strtoimax("-0xff", NULL, 0));
}
TEST(strtoimax, testOctal) {
EXPECT_EQ(-123, strtoimax("-0173", NULL, 0));
}
TEST(strtoimax, testLimits) {
EXPECT_EQ(
@ -35,12 +46,6 @@ TEST(strtoimax, testLimits) {
strtoimax("0x7fffffffffffffffffffffffffffffff", NULL, 0));
}
TEST(strtoimax, testZeroExtend) {
EXPECT_EQ(-1, strtoimax("-1", NULL, 0));
EXPECT_EQ(0xffffffff, strtoimax("-1u", NULL, 0));
EXPECT_EQ(0xffffffffffffffff, strtoimax("-1ul", NULL, 0));
}
TEST(strtoimax, testTwosBane) {
EXPECT_EQ(((uintmax_t)0x8000000000000000) << 64 | 0x0000000000000000,
strtoimax("0x80000000000000000000000000000000", NULL, 0));

View file

@ -34,7 +34,8 @@ TEST_LIBC_FMT_DIRECTDEPS = \
LIBC_SYSV \
LIBC_TESTLIB \
LIBC_UNICODE \
LIBC_X
LIBC_X \
THIRD_PARTY_GDTOA
TEST_LIBC_FMT_DEPS := \
$(call uniq,$(foreach x,$(TEST_LIBC_FMT_DIRECTDEPS),$($(x))))

View file

@ -33,10 +33,10 @@ TEST(round, test) {
TEST(round, testCornerCases) {
EXPECT_STREQ("-0", gc(xdtoa(round(-0.0))));
EXPECT_STREQ("nan", gc(xdtoa(round(NAN))));
EXPECT_STREQ("-nan", gc(xdtoa(round(-NAN))));
EXPECT_STREQ("inf", gc(xdtoa(round(INFINITY))));
EXPECT_STREQ("-inf", gc(xdtoa(round(-INFINITY))));
EXPECT_STREQ("NAN", gc(xdtoa(round(NAN))));
EXPECT_STREQ("-NAN", gc(xdtoa(round(-NAN))));
EXPECT_STREQ("INFINITY", gc(xdtoa(round(INFINITY))));
EXPECT_STREQ("-INFINITY", gc(xdtoa(round(-INFINITY))));
}
TEST(lround, test) {
@ -60,10 +60,10 @@ TEST(roundf, test) {
TEST(roundf, testCornerCases) {
EXPECT_STREQ("-0", gc(xdtoa(roundf(-0.0))));
EXPECT_STREQ("nan", gc(xdtoa(roundf(NAN))));
EXPECT_STREQ("-nan", gc(xdtoa(roundf(-NAN))));
EXPECT_STREQ("inf", gc(xdtoa(roundf(INFINITY))));
EXPECT_STREQ("-inf", gc(xdtoa(roundf(-INFINITY))));
EXPECT_STREQ("NAN", gc(xdtoa(roundf(NAN))));
EXPECT_STREQ("-NAN", gc(xdtoa(roundf(-NAN))));
EXPECT_STREQ("INFINITY", gc(xdtoa(roundf(INFINITY))));
EXPECT_STREQ("-INFINITY", gc(xdtoa(roundf(-INFINITY))));
}
TEST(lroundf, test) {

View file

@ -20,10 +20,11 @@ o/$(MODE)/test/libc/release/smoke.com: \
-o $@ \
-Os \
-static \
-fno-pie \
-no-pie \
-fno-pie \
-nostdlib \
-nostdinc \
-Wl,--gc-sections \
-Wl,--oformat=binary \
-Wl,-z,max-page-size=0x1000 \
-Wl,-T,o/$(MODE)/ape/ape.lds \

View file

@ -25,6 +25,7 @@ TEST_LIBC_STR_CHECKS = \
TEST_LIBC_STR_DIRECTDEPS = \
LIBC_CALLS_HEFTY \
LIBC_ALG \
LIBC_FMT \
LIBC_NEXGEN32E \
LIBC_STDIO \

View file

@ -23,7 +23,6 @@
#include "libc/testlib/testlib.h"
#include "libc/tinymath/tinymath.h"
#include "libc/x/x.h"
#include "third_party/dtoa/dtoa.h"
TEST(powl, testLongDouble) {
/* .4248496805467504836322459796959084815827285786480897 */

View file

@ -34,18 +34,18 @@ FIXTURE(intrin, disableHardwareExtensions) {
TEST(round, testCornerCases) {
EXPECT_STREQ("-0", gc(xdtoa(tinymath_round(-0.0))));
EXPECT_STREQ("nan", gc(xdtoa(tinymath_round(NAN))));
EXPECT_STREQ("-nan", gc(xdtoa(tinymath_round(-NAN))));
EXPECT_STREQ("inf", gc(xdtoa(tinymath_round(INFINITY))));
EXPECT_STREQ("-inf", gc(xdtoa(tinymath_round(-INFINITY))));
EXPECT_STREQ("NAN", gc(xdtoa(tinymath_round(NAN))));
EXPECT_STREQ("-NAN", gc(xdtoa(tinymath_round(-NAN))));
EXPECT_STREQ("INFINITY", gc(xdtoa(tinymath_round(INFINITY))));
EXPECT_STREQ("-INFINITY", gc(xdtoa(tinymath_round(-INFINITY))));
}
TEST(roundl, testCornerCases) {
EXPECT_STREQ("-0", gc(xdtoa(tinymath_roundl(-0.0))));
EXPECT_STREQ("nan", gc(xdtoa(tinymath_roundl(NAN))));
EXPECT_STREQ("-nan", gc(xdtoa(tinymath_roundl(-NAN))));
EXPECT_STREQ("inf", gc(xdtoa(tinymath_roundl(INFINITY))));
EXPECT_STREQ("-inf", gc(xdtoa(tinymath_roundl(-INFINITY))));
EXPECT_STREQ("NAN", gc(xdtoa(tinymath_roundl(NAN))));
EXPECT_STREQ("-NAN", gc(xdtoa(tinymath_roundl(-NAN))));
EXPECT_STREQ("INFINITY", gc(xdtoa(tinymath_roundl(INFINITY))));
EXPECT_STREQ("-INFINITY", gc(xdtoa(tinymath_roundl(-INFINITY))));
}
TEST(round, test) {
@ -149,10 +149,10 @@ TEST(rintl, test) {
TEST(roundf, testCornerCases) {
EXPECT_STREQ("-0", gc(xdtoa(tinymath_roundf(-0.0))));
EXPECT_STREQ("nan", gc(xdtoa(tinymath_roundf(NAN))));
EXPECT_STREQ("-nan", gc(xdtoa(tinymath_roundf(-NAN))));
EXPECT_STREQ("inf", gc(xdtoa(tinymath_roundf(INFINITY))));
EXPECT_STREQ("-inf", gc(xdtoa(tinymath_roundf(-INFINITY))));
EXPECT_STREQ("NAN", gc(xdtoa(tinymath_roundf(NAN))));
EXPECT_STREQ("-NAN", gc(xdtoa(tinymath_roundf(-NAN))));
EXPECT_STREQ("INFINITY", gc(xdtoa(tinymath_roundf(INFINITY))));
EXPECT_STREQ("-INFINITY", gc(xdtoa(tinymath_roundf(-INFINITY))));
}
TEST(lroundf, test) {

View file

@ -18,25 +18,26 @@
02110-1301 USA
*/
#include "libc/math.h"
#include "libc/runtime/gc.h"
#include "libc/testlib/ezbench.h"
#include "libc/testlib/testlib.h"
#include "third_party/dtoa/dtoa.h"
#include "libc/x/x.h"
char buf[32];
TEST(sinl, testLongDouble) {
EXPECT_STREQ(".479425538604203", g_fmt(buf, sinl(0.5)));
EXPECT_STREQ("-.479425538604203", g_fmt(buf, sinl(-0.5)));
EXPECT_STREQ(".479425538604203", gc(xdtoa(sinl(.5))));
EXPECT_STREQ("-.479425538604203", gc(xdtoa(sinl(-.5))));
}
TEST(sinl, testDouble) {
EXPECT_STREQ(".479425538604203", g_fmt(buf, sin(0.5)));
EXPECT_STREQ("-.479425538604203", g_fmt(buf, sin(-0.5)));
EXPECT_STREQ(".479425538604203", gc(xdtoa(sin(.5))));
EXPECT_STREQ("-.479425538604203", gc(xdtoa(sin(-.5))));
}
TEST(sinl, testFloat) {
EXPECT_STARTSWITH(".4794255", g_fmt(buf, sinf(0.5f)));
EXPECT_STARTSWITH("-.4794255", g_fmt(buf, sinf(-0.5f)));
EXPECT_STARTSWITH(".4794255", gc(xdtoa(sinf(.5f))));
EXPECT_STARTSWITH("-.4794255", gc(xdtoa(sinf(-.5f))));
}
BENCH(sinl, bench) {

View file

@ -32,8 +32,7 @@ TEST_LIBC_TINYMATH_DIRECTDEPS = \
LIBC_NEXGEN32E \
LIBC_STUBS \
LIBC_TESTLIB \
LIBC_X \
THIRD_PARTY_DTOA
LIBC_X
TEST_LIBC_TINYMATH_DEPS := \
$(call uniq,$(foreach x,$(TEST_LIBC_TINYMATH_DIRECTDEPS),$($(x))))

View file

@ -34,7 +34,7 @@ TEST_LIBC_X_DIRECTDEPS = \
LIBC_X \
LIBC_STUBS \
LIBC_TESTLIB \
THIRD_PARTY_DTOA
THIRD_PARTY_GDTOA
TEST_LIBC_X_DEPS := \
$(call uniq,$(foreach x,$(TEST_LIBC_X_DIRECTDEPS),$($(x))))

View file

@ -37,7 +37,7 @@ char *render(struct Pty *pty) {
return b.p;
}
const char widelatin[] aligned(16) = "\
const char widelatin[] forcealign(16) = "\
-\r\n\
-\r\n\
-\r\n\
@ -82,7 +82,7 @@ TEST(pty, testFunWidth) {
FreePty(pty);
}
const char hyperion[] aligned(16) = "\
const char hyperion[] forcealign(16) = "\
Fanatics have their dreams, wherewith they weave \
A paradise for a sect; the savage too \
From forth the loftiest fashion of his sleep \