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