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

88
test/dsp/core/dct_test.c Normal file
View file

@ -0,0 +1,88 @@
/*-*- 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

@ -0,0 +1,65 @@
/*-*- 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/xmmintrin.h"
#include "libc/log/log.h"
#include "libc/macros.h"
#include "libc/math.h"
#include "libc/str/str.h"
#include "libc/testlib/ezbench.h"
#include "libc/testlib/testlib.h"
#include "third_party/blas/blas.h"
#include "tool/viz/lib/formatstringtable-testlib.h"
TEST(dgemm, test) {
double alpha, beta;
long m, n, k, lda, ldb, ldc;
double A[3][3] = {{1 / 8.}, {6 / 8.}, {1 / 8.}};
double B[1][3] = {{1 / 8., 6 / 8., 1 / 8.}};
double C[3][3] = {0};
m = 3;
n = 3;
k = 1;
lda = 3;
ldb = 3;
ldc = 3;
beta = 1;
alpha = 1;
dgemm_("T", "T", &m, &n, &k, &alpha, &A[0][0], &lda, &B[0][0], &ldb, &beta,
&C[0][0], &ldc);
EXPECT_DBLMATRIXEQ(6, rint, 3, 3, C, "\n\
.015625 .09375 .015625\n\
.09375 .5625 .09375\n\
.015625 .09375 .015625");
}
void dgemmer(long m, long n, long k, void *A, long lda, void *B, long ldb,
void *C, long ldc) {
double alpha, beta;
beta = 1;
alpha = 1;
dgemm_("N", "N", &m, &n, &k, &alpha, A, &lda, B, &ldb, &beta, C, &ldc);
}
BENCH(dgemm, bench) {
double(*A)[128][128] = tgc(tmalloc(128 * 128 * 8));
double(*B)[128][128] = tgc(tmalloc(128 * 128 * 8));
double(*C)[128][128] = tgc(tmalloc(128 * 128 * 8));
EZBENCH2("dgemm_", donothing, dgemmer(128, 128, 128, A, 128, B, 128, C, 128));
}

View file

@ -0,0 +1,84 @@
/*-*- 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 "dsp/mpeg/mpeg.h"
#include "libc/log/check.h"
#include "libc/macros.h"
#include "libc/rand/rand.h"
#include "libc/runtime/buffer.h"
#include "libc/testlib/ezbench.h"
#include "libc/testlib/testlib.h"
short pcm[8][8];
float binary32[8][8];
struct GuardedBuffer b1, b2;
TEST(float2short, test) {
binary32[0][0] = -0.5;
binary32[0][1] = 0.0;
binary32[0][2] = 0.5;
float2short(8, pcm, binary32);
EXPECT_EQ(-16384, pcm[0][0]);
EXPECT_EQ(0, pcm[0][1]);
EXPECT_EQ(16384, pcm[0][2]);
}
TEST(float2short, testOverflow) {
binary32[0][0] = -1.1;
binary32[0][1] = -1.0;
binary32[0][2] = 1.0;
binary32[0][3] = 1.1;
float2short(8, pcm, binary32);
EXPECT_EQ(-32768, pcm[0][0]);
EXPECT_EQ(-32768, pcm[0][1]);
EXPECT_EQ(32767, pcm[0][2]);
EXPECT_EQ(32767, pcm[0][3]);
}
void unclamped(size_t n, short pcm16[n][8], const float binary32[n][8]) {
size_t i, j;
for (i = 0; i < n; ++i) {
for (j = 0; j < 8; ++j) {
pcm16[i][j] = binary32[i][j] * 32768;
}
}
}
plm_samples_t samps;
void randomizeaudio(void) {
size_t i;
for (i = 0; i < ARRAYLEN(samps.interleaved); ++i) {
samps.interleaved[i] = randf();
}
}
void float2short_pure(void) {
float2short(ARRAYLEN(samps.interleaved) / 8, pcm, (void *)samps.interleaved);
}
void float2short_unclamped(void) {
unclamped(ARRAYLEN(samps.interleaved) / 8, pcm, (void *)samps.interleaved);
}
BENCH(float2short, audioframe) {
EZBENCH(randomizeaudio(), float2short_pure());
EZBENCH(randomizeaudio(), float2short_unclamped());
}

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 "dsp/core/core.h"
#include "libc/math.h"
#include "libc/testlib/testlib.h"
TEST(gamma, test) {
int i;
double g, x, a, b;
g = 2.4;
for (i = 0; i < 256; ++i) {
x = i;
x /= 255;
a = rgb2stdpc(x, g);
b = tv2pcgamma(rgb2stdtv(x), g);
ASSERT_EQ(true, fabs(a - b) < .000000001, "%d %f %f %f", i, x, a, b);
}
}

View file

@ -0,0 +1,62 @@
/*-*- 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 "dsp/core/q.h"
#include "libc/str/str.h"
#include "libc/testlib/ezbench.h"
#include "libc/testlib/testlib.h"
long I[8];
TEST(GetIntegerCoefficients8, testBt601Vectors) {
const struct {
int m, L, H;
double r[8];
long n[8];
} V[] = {
{8, 16, 235, {.299, .587, .114}, {77, 150, 29}},
{9, 16, 235, {.299, .587, .114}, {153, 301, 58}},
{10, 16, 235, {.299, .587, .114}, {306, 601, 117}},
{11,
16,
235,
{.299, .587, .114, 1, 1, 1},
{612, 1202, 234, 2048, 2048, 2048}},
{12, 16, 235, {.299, .587, .114}, {1225, 2404, 467}},
{13, 16, 235, {.299, .587, .114}, {2449, 4809, 934}},
{14, 16, 235, {.299, .587, .114}, {4899, 9617, 1868}},
{15, 16, 235, {.299, .587, .114}, {9798, 19235, 3735}},
{16, 16, 235, {.299, .587, .114}, {19595, 38470, 7471}},
};
long i, got[8];
for (i = 0; i < ARRAYLEN(V); ++i) {
GetIntegerCoefficients8(got, V[i].r, V[i].m, V[i].L, V[i].H);
EXPECT_EQ(0, memcmp(V[i].n, got, sizeof(got)),
"got={%ld,%ld,%ld,%ld,%ld,%ld}, want={%ld,%ld,%ld,%ld,%ld,%ld}",
got[0], got[1], got[2], got[3], got[4], got[5], V[i].n[0],
V[i].n[1], V[i].n[2], V[i].n[3], V[i].n[4], V[i].n[5]);
}
}
BENCH(GetIntegerCoefficients8, bench) {
double C[8] = {.299, .587, .114, .299, .587, .114, .114, .114};
EZBENCH2("GetIntegerCoefficients8", donothing,
GetIntegerCoefficients8(I, C, 11, 16, 232));
}

View file

@ -0,0 +1,89 @@
/*-*- 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 "dsp/core/q.h"
#include "libc/macros.h"
#include "libc/runtime/gc.h"
#include "libc/str/str.h"
#include "libc/testlib/ezbench.h"
#include "libc/testlib/testlib.h"
#include "libc/x/x.h"
long I[6];
TEST(GetIntegerCoefficients, testBt601Vectors) {
const struct {
int m, L, H;
double r[6];
long n[6];
} V[] = {
{8, 16, 235, {.299, .587, .114}, {77, 150, 29}},
{9, 16, 235, {.299, .587, .114}, {153, 301, 58}},
{10, 16, 235, {.299, .587, .114}, {306, 601, 117}},
{11,
16,
235,
{.299, .587, .114, 1, 1, 1},
{612, 1202, IsTiny() ? 233 : 234, 2048, 2048, 2048}},
{12, 16, 235, {.299, .587, .114}, {1225, 2404, 467}},
{13, 16, 235, {.299, .587, .114}, {2449, 4809, 934}},
{14, 16, 235, {.299, .587, .114}, {4899, 9617, 1868}},
{15, 16, 235, {.299, .587, .114}, {9798, 19235, IsTiny() ? 3736 : 3735}},
{16, 16, 235, {.299, .587, .114}, {19595, 38470, 7471}},
};
long i, got[6];
for (i = 0; i < ARRAYLEN(V); ++i) {
GetIntegerCoefficients(got, V[i].r, V[i].m, V[i].L, V[i].H);
EXPECT_EQ(0, memcmp(V[i].n, got, sizeof(got)),
"got={%ld,%ld,%ld,%ld,%ld,%ld}, want={%ld,%ld,%ld,%ld,%ld,%ld}",
got[0], got[1], got[2], got[3], got[4], got[5], V[i].n[0],
V[i].n[1], V[i].n[2], V[i].n[3], V[i].n[4], V[i].n[5]);
}
}
TEST(GetIntegerCoefficients, testForYCbCr2Rgb) {
double C[6] = {.299, .587, .114};
GetIntegerCoefficients(I, C, 11, 16, 232);
EXPECT_EQ(612, I[0]);
EXPECT_EQ(1202, I[1]);
EXPECT_EQ(IsTiny() ? 233 : 234, I[2]);
}
TEST(GetIntegerCoefficients, testForGaussian) {
#define G(A, B, C, D, E) lrint((A + 4 * B + 6 * C + 4 * D + E) / 16.)
double C[6] = {1 / 16., 4 / 16., 6 / 16., 4 / 16., 1 / 16.};
long M = 22, N[6], B[6] = {12, 191, 174, 205, 35};
GetIntegerCoefficients(N, C, M, 0, 255);
EXPECT_EQ(262144, N[0]);
EXPECT_EQ(1048576, N[1]);
EXPECT_EQ(1572864, N[2]);
EXPECT_EQ(1048576, N[3]);
EXPECT_EQ(262144, N[4]);
EXPECT_EQ(G(B[0], B[1], B[2], B[3], B[4]),
(N[0] * B[0] + N[1] * B[1] + N[2] * B[2] + N[3] * B[3] +
N[4] * B[4] + N[5] * B[5] + (1l << (M - 1))) >>
M);
}
BENCH(GetIntegerCoefficients, bench) {
double C[6] = {.299, .587, .114};
EZBENCH2("getintegercoefficients", donothing,
GetIntegerCoefficients(I, C, 11, 16, 232));
}

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 "dsp/core/core.h"
#include "dsp/core/illumination.h"
#include "libc/log/log.h"
#include "libc/math.h"
#include "libc/testlib/ezbench.h"
#include "libc/testlib/testlib.h"
#include "tool/viz/lib/formatstringtable-testlib.h"
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");
}
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");
}
BENCH(GetChromaticAdaptationMatrix, bench) {
double M[3][3];
EZBENCH2("GetChromaticAdaptationMatrix", donothing,
GetChromaticAdaptationMatrix(M, kIlluminantD65, kIlluminantA));
}

40
test/dsp/core/inv3_test.c Normal file
View file

@ -0,0 +1,40 @@
/*-*- 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 "dsp/core/illumination.h"
#include "libc/math.h"
#include "libc/testlib/ezbench.h"
#include "libc/testlib/testlib.h"
#include "tool/viz/lib/formatstringtable-testlib.h"
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");
}
BENCH(inv3, bench) {
double M[3][3], d;
EZBENCH2("det3", donothing, EXPROPRIATE((d = det3(kBradford))));
EZBENCH2("inv3", donothing, EXPROPRIATE(inv3(M, kBradford, d)));
}

View file

@ -0,0 +1,108 @@
/*-*- 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 "dsp/mpeg/mpeg.h"
#include "libc/limits.h"
#include "libc/log/check.h"
#include "libc/nexgen32e/x86feature.h"
#include "libc/rand/rand.h"
#include "libc/runtime/buffer.h"
#include "libc/testlib/ezbench.h"
#include "libc/testlib/testlib.h"
void addsw$pure(size_t n, short x[n][8], const short y[n][8]) {
size_t i, j;
for (i = 0; i < n; ++i) {
for (j = 0; j < 8; ++j) {
x[i][j] = MIN(MAX(x[i][j] + y[i][j], INT16_MIN), INT16_MAX);
}
}
}
short *pcm1;
short *pcm2;
struct GuardedBuffer b1, b2;
TEST(sad16x8n, test) {
CHECK_NOTNULL((pcm1 = balloc(&b1, 32, 128 * 2)));
CHECK_NOTNULL((pcm2 = balloc(&b2, 32, 128 * 2)));
pcm1[0] = 0;
pcm2[0] = 0;
pcm1[1] = 23;
pcm2[1] = 10;
pcm1[2] = 23;
pcm2[2] = -10;
pcm1[3] = 23;
pcm2[3] = -46;
pcm1[120 + 0] = 0;
pcm2[120 + 0] = 0;
pcm1[120 + 1] = 23;
pcm2[120 + 1] = 10;
pcm1[120 + 2] = 23;
pcm2[120 + 2] = -10;
pcm1[120 + 3] = 23;
pcm2[120 + 3] = -46;
sad16x8n(128 / 8, (void *)pcm1, (void *)pcm2);
EXPECT_EQ(0, pcm1[0]);
EXPECT_EQ(33, pcm1[1]);
EXPECT_EQ(13, pcm1[2]);
EXPECT_EQ(-23, pcm1[3]);
EXPECT_EQ(0, pcm1[120 + 0]);
EXPECT_EQ(33, pcm1[120 + 1]);
EXPECT_EQ(13, pcm1[120 + 2]);
EXPECT_EQ(-23, pcm1[120 + 3]);
bfree(&b1);
bfree(&b2);
}
//////////////////////////////////////////////////////////////////////
// audio frame mixing latency: ~60ns (sse2 10x faster than pure)
//
// 1f ~= 14ms (cd quality)
// (1.0 / 41000) * (1.0 / 2) * (1152 / 1.0)
//
// 1f ~= 845µs (dolby truehd)
// (1.0 / 192000) * (1.0 / 7.1) * (1152 / 1.0)
//
// @note plm frame always has exactly 1152 floats
void randomizeaudio(void) {
size_t i;
for (i = 0; i < PLM_AUDIO_SAMPLES_PER_FRAME; ++i) {
pcm1[i] = (rand() - INT_MAX / 2) % INT16_MAX;
pcm2[i] = (rand() - INT_MAX / 2) % INT16_MAX;
}
}
void sad16x8n_sse2(void) {
sad16x8n(PLM_AUDIO_SAMPLES_PER_FRAME / 8, (void *)pcm1, (void *)pcm2);
}
void sad16x8n_pure(void) {
addsw$pure(PLM_AUDIO_SAMPLES_PER_FRAME / 8, (void *)pcm1, (void *)pcm2);
}
BENCH(sad16x8n, audioframe) {
CHECK_NOTNULL((pcm1 = balloc(&b1, 32, PLM_AUDIO_SAMPLES_PER_FRAME * 2)));
CHECK_NOTNULL((pcm2 = balloc(&b2, 32, PLM_AUDIO_SAMPLES_PER_FRAME * 2)));
EZBENCH(randomizeaudio(), sad16x8n_pure());
EZBENCH(randomizeaudio(), sad16x8n_sse2());
bfree(&b1);
bfree(&b2);
}

View file

@ -0,0 +1,161 @@
/*-*- 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 "dsp/mpeg/mpeg.h"
#include "libc/limits.h"
#include "libc/log/check.h"
#include "libc/macros.h"
#include "libc/nexgen32e/x86feature.h"
#include "libc/rand/rand.h"
#include "libc/runtime/buffer.h"
#include "libc/testlib/ezbench.h"
#include "libc/testlib/testlib.h"
short pcm[8][8];
TEST(scalevolume, testIncreaseVolumeByOneIncrement) {
pcm[0][0] = INT16_MIN;
pcm[0][1] = INT16_MIN / 2;
pcm[0][2] = -2;
pcm[0][3] = -1;
pcm[0][4] = 0;
pcm[0][5] = 1;
pcm[0][6] = 2;
pcm[0][7] = INT16_MAX / 2;
pcm[1][0] = INT16_MAX;
scalevolume(ARRAYLEN(pcm), pcm, 1);
EXPECT_EQ(INT16_MIN, pcm[0][0]);
EXPECT_EQ(INT16_MIN, pcm[0][1]);
EXPECT_EQ(-4, pcm[0][2]);
EXPECT_EQ(-2, pcm[0][3]);
EXPECT_EQ(0, pcm[0][4]);
EXPECT_EQ(2, pcm[0][5]);
EXPECT_EQ(4, pcm[0][6]);
EXPECT_EQ(INT16_MAX - 1, pcm[0][7]);
EXPECT_EQ(INT16_MAX, pcm[1][0]);
}
TEST(scalevolume, testDecreaseVolumeByOneIncrement) {
pcm[0][0] = INT16_MIN;
pcm[0][1] = INT16_MIN / 2;
pcm[0][2] = -2;
pcm[0][3] = -1;
pcm[0][4] = 0;
pcm[0][5] = 1;
pcm[0][6] = 2;
pcm[0][7] = INT16_MAX / 2;
pcm[1][0] = INT16_MAX;
scalevolume(ARRAYLEN(pcm), pcm, -1);
EXPECT_EQ(INT16_MIN / 2, pcm[0][0]);
EXPECT_EQ(INT16_MIN / 4, pcm[0][1]);
EXPECT_EQ(-1, pcm[0][2]);
EXPECT_EQ(-1, pcm[0][3]);
EXPECT_EQ(0, pcm[0][4]);
EXPECT_EQ(0, pcm[0][5]);
EXPECT_EQ(1, pcm[0][6]);
EXPECT_EQ(INT16_MAX / 4, pcm[0][7]);
EXPECT_EQ(INT16_MAX / 2, pcm[1][0]);
}
TEST(scalevolume, testScaleByZero_doesNothing) {
pcm[0][0] = INT16_MIN;
pcm[0][1] = INT16_MIN / 2;
pcm[0][2] = -2;
pcm[0][3] = -1;
pcm[0][4] = 0;
pcm[0][5] = 1;
pcm[0][6] = 2;
pcm[0][7] = INT16_MAX / 2;
pcm[1][0] = INT16_MAX;
scalevolume(ARRAYLEN(pcm), pcm, 0);
EXPECT_EQ(INT16_MIN, pcm[0][0]);
EXPECT_EQ(INT16_MIN / 2, pcm[0][1]);
EXPECT_EQ(-2, pcm[0][2]);
EXPECT_EQ(-1, pcm[0][3]);
EXPECT_EQ(0, pcm[0][4]);
EXPECT_EQ(1, pcm[0][5]);
EXPECT_EQ(2, pcm[0][6]);
EXPECT_EQ(INT16_MAX / 2, pcm[0][7]);
EXPECT_EQ(INT16_MAX, pcm[1][0]);
}
TEST(scalevolume, testBiggestScale_justSaturates) {
pcm[0][0] = INT16_MIN;
pcm[0][1] = INT16_MIN / 2;
pcm[0][2] = -2;
pcm[0][3] = -1;
pcm[0][4] = 0;
pcm[0][5] = 1;
pcm[0][6] = 2;
pcm[0][7] = INT16_MAX / 2;
pcm[1][0] = INT16_MAX;
scalevolume(ARRAYLEN(pcm), pcm, 123);
EXPECT_EQ(INT16_MIN, pcm[0][0]);
EXPECT_EQ(INT16_MIN, pcm[0][1]);
EXPECT_EQ(INT16_MIN, pcm[0][2]);
EXPECT_EQ(INT16_MIN, pcm[0][3]);
EXPECT_EQ(0, pcm[0][4]);
EXPECT_EQ(INT16_MAX, pcm[0][5]);
EXPECT_EQ(INT16_MAX, pcm[0][6]);
EXPECT_EQ(INT16_MAX, pcm[0][7]);
EXPECT_EQ(INT16_MAX, pcm[1][0]);
}
TEST(scalevolume, testSmallestScale_justSaturates) {
pcm[0][0] = INT16_MIN;
pcm[0][1] = INT16_MIN / 2;
pcm[0][2] = -2;
pcm[0][3] = -1;
pcm[0][4] = 0;
pcm[0][5] = 1;
pcm[0][6] = 2;
pcm[0][7] = INT16_MAX / 2;
pcm[1][0] = INT16_MAX;
scalevolume(ARRAYLEN(pcm), pcm, -123);
EXPECT_EQ(-1, pcm[0][0]);
EXPECT_EQ(-1, pcm[0][1]);
EXPECT_EQ(-1, pcm[0][2]);
EXPECT_EQ(-1, pcm[0][3]);
EXPECT_EQ(0, pcm[0][4]);
EXPECT_EQ(0, pcm[0][5]);
EXPECT_EQ(0, pcm[0][6]);
EXPECT_EQ(0, pcm[0][7]);
EXPECT_EQ(0, pcm[1][0]);
}
//////////////////////////////////////////////////////////////////////
// audio volume adjustment latency:
// - ~1µs w/ -Os
// - ~500ns w/ -O3
// - ~200ns w/ -O3 -march=skylake MODE=rel
void randomizeaudio(void) {
rngset(pcm, sizeof(pcm), rand64, -1);
}
void scalevolume_pure(int amt) {
scalevolume(ARRAYLEN(pcm), pcm, amt);
}
BENCH(scalevolume, audioframe) {
EZBENCH(randomizeaudio(), scalevolume_pure(0));
EZBENCH(randomizeaudio(), scalevolume_pure(1));
EZBENCH(randomizeaudio(), scalevolume_pure(15));
}

51
test/dsp/core/test.mk Normal file
View file

@ -0,0 +1,51 @@
#-*-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_DSP_CORE
TEST_DSP_CORE_SRCS := $(wildcard test/dsp/core/*.c)
TEST_DSP_CORE_SRCS_TEST = $(filter %_test.c,$(TEST_DSP_CORE_SRCS))
TEST_DSP_CORE_COMS = $(TEST_DSP_CORE_OBJS:%.o=%.com)
TEST_DSP_CORE_BINS = $(TEST_DSP_CORE_COMS) $(TEST_DSP_CORE_COMS:%=%.dbg)
TEST_DSP_CORE_OBJS = \
$(TEST_DSP_CORE_SRCS:%=o/$(MODE)/%.zip.o) \
$(TEST_DSP_CORE_SRCS:%.c=o/$(MODE)/%.o)
TEST_DSP_CORE_TESTS = \
$(TEST_DSP_CORE_SRCS_TEST:%.c=o/$(MODE)/%.com.ok)
TEST_DSP_CORE_CHECKS = \
$(TEST_DSP_CORE_SRCS_TEST:%.c=o/$(MODE)/%.com.runs)
TEST_DSP_CORE_DIRECTDEPS = \
DSP_CORE \
DSP_MPEG \
LIBC_TINYMATH \
LIBC_LOG \
LIBC_RUNTIME \
LIBC_RAND \
LIBC_TESTLIB \
TOOL_VIZ_LIB \
THIRD_PARTY_BLAS \
THIRD_PARTY_COMPILER_RT
TEST_DSP_CORE_DEPS := \
$(call uniq,$(foreach x,$(TEST_DSP_CORE_DIRECTDEPS),$($(x))))
o/$(MODE)/test/dsp/core/core.pkg: \
$(TEST_DSP_CORE_OBJS) \
$(foreach x,$(TEST_DSP_CORE_DIRECTDEPS),$($(x)_A).pkg)
o/$(MODE)/test/dsp/core/%.com.dbg: \
$(TEST_DSP_CORE_DEPS) \
o/$(MODE)/test/dsp/core/%.o \
$(LIBC_TESTMAIN) \
$(CRT) \
$(APE)
@$(APELINK)
.PHONY: o/$(MODE)/test/dsp/core
o/$(MODE)/test/dsp/core: \
$(TEST_DSP_CORE_BINS) \
$(TEST_DSP_CORE_CHECKS)