1/* $NetBSD: cosdata.c,v 1.3 2017/01/30 15:50:21 christos Exp $ */
2
3/*-
4 * Copyright (c) 2015 - 2016 Nathanial Sloss <nathanialsloss@yahoo.com.au>
5 * All rights reserved.
6 *
7 *		This software is dedicated to the memory of -
8 *	   Baron James Anlezark (Barry) - 1 Jan 1949 - 13 May 2012.
9 *
10 *		Barry was a man who loved his music.
11 *
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
14 * are met:
15 * 1. Redistributions of source code must retain the above copyright
16 *    notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 *    notice, this list of conditions and the following disclaimer in the
19 *    documentation and/or other materials provided with the distribution.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
22 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
23 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
24 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
25 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31 * POSSIBILITY OF SUCH DAMAGE.
32 */
33
34#include <math.h>
35#include <stdio.h>
36
37static const double sbc_coeffs8[] = {
38	0.00000000e+00, 1.56575398e-04, 3.43256425e-04, 5.54620202e-04,
39	8.23919506e-04, 1.13992507e-03, 1.47640169e-03, 1.78371725e-03,
40	2.01182542e-03, 2.10371989e-03, 1.99454554e-03, 1.61656283e-03,
41	9.02154502e-04, -1.78805361e-04, -1.64973098e-03, -3.49717454e-03,
42	5.65949473e-03, 8.02941163e-03, 1.04584443e-02, 1.27472335e-02,
43	1.46525263e-02, 1.59045603e-02, 1.62208471e-02, 1.53184106e-02,
44	1.29371806e-02, 8.85757540e-03, 2.92408442e-03, -4.91578024e-03,
45	-1.46404076e-02, -2.61098752e-02, -3.90751381e-02, -5.31873032e-02,
46	6.79989431e-02, 8.29847578e-02, 9.75753918e-02, 1.11196689e-01,
47	1.23264548e-01, 1.33264415e-01, 1.40753505e-01, 1.45389847e-01,
48	1.46955068e-01, 1.45389847e-01, 1.40753505e-01, 1.33264415e-01,
49	1.23264548e-01, 1.11196689e-01, 9.75753918e-02, 8.29847578e-02,
50	-6.79989431e-02, -5.31873032e-02, -3.90751381e-02, -2.61098752e-02,
51	-1.46404076e-02, -4.91578024e-03, 2.92408442e-03, 8.85757540e-03,
52	1.29371806e-02, 1.53184106e-02, 1.62208471e-02, 1.59045603e-02,
53	1.46525263e-02, 1.27472335e-02, 1.04584443e-02, 8.02941163e-03,
54	-5.65949473e-03, -3.49717454e-03, -1.64973098e-03, -1.78805361e-04,
55	9.02154502e-04, 1.61656283e-03, 1.99454554e-03, 2.10371989e-03,
56	2.01182542e-03, 1.78371725e-03, 1.47640169e-03, 1.13992507e-03,
57	8.23919506e-04, 5.54620202e-04, 3.43256425e-04, 1.56575398e-04,
58};
59
60static const double sbc_coeffs4[] = {
61	 0.00000000e+00, 5.36548976e-04, 1.49188357e-03, 2.73370904e-03,
62	 3.83720193e-03, 3.89205149e-03, 1.86581691e-03,-3.06012286e-03,
63	 1.09137620e-02, 2.04385087e-02, 2.88757392e-02, 3.21939290e-02,
64	 2.58767811e-02, 6.13245186e-03,-2.88217274e-02,-7.76463494e-02,
65	 1.35593274e-01, 1.94987841e-01, 2.46636662e-01, 2.81828203e-01,
66	 2.94315332e-01, 2.81828203e-01, 2.46636662e-01, 1.94987841e-01,
67	-1.35593274e-01,-7.76463494e-02,-2.88217274e-02, 6.13245186e-03,
68	 2.58767811e-02, 3.21939290e-02, 2.88757392e-02, 2.04385087e-02,
69	-1.09137620e-02,-3.06012286e-03, 1.86581691e-03, 3.89205149e-03,
70	 3.83720193e-03, 2.73370904e-03, 1.49188357e-03, 5.36548976e-04,
71};
72
73#define COEFFS_MULTI	(1 << 12)
74#define SI_MULTI	512
75
76static void
77arrayprint(const char *name, const double *arr, size_t len)
78{
79	size_t k, count;
80
81	printf("static const int32_t %s[] = {\n\t", name);
82	for (count = k = 0; k < len; k++, count++) {
83		if (count % 8 == 0 && count != 0)
84			printf("\n\t");
85		printf("%d, ", (int)roundf((float)arr[k] * COEFFS_MULTI));
86	}
87	printf("\n};\n");
88}
89
90#define ARRAYPRINT(a)	arrayprint(#a, a, sizeof(a) / sizeof(a[0]))
91
92static void
93cosprint(int lim, int sgn)
94{
95	size_t count, i, k;
96	size_t mi = lim * 4;
97	size_t mk = mi * 2;
98	int dk = sgn * lim * 2;
99	double mp = M_PI_4 / lim;
100	float val;
101
102	printf("static const int32_t cos%sdata%zu[%zu][%zu] = {\n\t",
103	    sgn == -1 ? "" : "dec", mi, mi, mk);
104
105	for (count = 0, i = 0; i < mi; i++) {
106		for (k = 0; k < mk; k++, count++) {
107			if (count % 8 == 0 && count != 0)
108				printf("\n\t");
109			if (k == 0)
110				printf("{ ");
111
112			val = cosf((i + 0.5) * ((int)k + dk) * mp);
113			printf("%d, ", (int)roundf(val * SI_MULTI));
114
115			if (k == mk - 1)
116				printf("},");
117		}
118	}
119	printf("\n};\n");
120}
121
122int
123main(void)
124{
125	printf("/* sbc_coeffs.h - Automatically generated by cosdata.c. */\n"
126	    "\n");
127
128	printf("#define\tSIMULTI\t%d\n\n", SI_MULTI);
129	printf("#define\tCOEFFSMULTI\t%d\n\n", COEFFS_MULTI);
130
131	ARRAYPRINT(sbc_coeffs8);
132	ARRAYPRINT(sbc_coeffs4);
133
134	cosprint(2, -1);
135	cosprint(1, -1);
136	cosprint(2, 1);
137	cosprint(1, 1);
138
139	return 0;
140}
141