xref: /freebsd/sys/tools/sound/snd_fxdiv_gen.awk (revision c697fb7f)
1#!/usr/bin/awk -f
2#
3# SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4#
5# Copyright (c) 2008-2009 Ariff Abdullah <ariff@FreeBSD.org>
6# All rights reserved.
7#
8# Redistribution and use in source and binary forms, with or without
9# modification, are permitted provided that the following conditions
10# are met:
11# 1. Redistributions of source code must retain the above copyright
12#    notice, this list of conditions and the following disclaimer.
13# 2. Redistributions in binary form must reproduce the above copyright
14#    notice, this list of conditions and the following disclaimer in the
15#    documentation and/or other materials provided with the distribution.
16#
17# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27# SUCH DAMAGE.
28#
29# $FreeBSD$
30#
31
32function floor(x, r)
33{
34	r = int(x);
35	if (r > x)
36		r--;
37	return (r + 0);
38}
39
40function shl(x, y)
41{
42	while (y > 0) {
43		x *= 2;
44		y--;
45	}
46	return (x);
47}
48
49function shr(x, y)
50{
51	while (y > 0 && x != 0) {
52		x = floor(x / 2);
53		y--;
54	}
55	return (x);
56}
57
58function calcdiv(r, x, y, z)
59{
60	y = floor(FXONE / x);
61	z = FXSHIFT;
62
63	while (shr((y * x), z) < 1)
64		y++;
65
66	while ((y % 2) == 0 && z > 0) {
67		y = floor(y / 2);
68		z--;
69	}
70
71	r["mul"] = y;
72	r["shift"] = z;
73}
74
75BEGIN {
76	FXSHIFT = 16;
77	FXONE   = shl(1, FXSHIFT);
78
79	SND_CHN_MAX = 127;
80
81	PCM_8_BPS  = 1;
82	PCM_16_BPS = 2;
83	PCM_24_BPS = 3;
84	PCM_32_BPS = 4;
85
86	SND_MAX_ALIGN = SND_CHN_MAX * PCM_32_BPS;
87
88	for (i = 1; i <= SND_CHN_MAX; i++) {
89		aligns[PCM_8_BPS * i]  = 1;
90		aligns[PCM_16_BPS * i] = 1;
91		aligns[PCM_24_BPS * i] = 1;
92		aligns[PCM_32_BPS * i] = 1;
93	}
94
95	printf("#ifndef _SND_FXDIV_GEN_H_\n");
96	printf("#define _SND_FXDIV_GEN_H_\n\n");
97
98	printf("/*\n");
99	printf(" * Generated using snd_fxdiv_gen.awk, heaven, wind and awesome.\n");
100	printf(" *\n");
101	printf(" * DO NOT EDIT!\n");
102	printf(" */\n\n");
103	printf("#ifdef SND_USE_FXDIV\n\n");
104
105	printf("/*\n");
106	printf(" * Fast unsigned 32bit integer division and rounding, accurate for\n");
107	printf(" * x = 1 - %d. This table should be enough to handle possible\n", FXONE);
108	printf(" * division for 1 - 508 (more can be generated though..).\n");
109	printf(" *\n");
110	printf(" * 508 = SND_CHN_MAX * PCM_32_BPS, which is why....\n");
111	printf(" */\n\n");
112
113	printf("extern const uint32_t snd_fxdiv_table[%d][2];\n\n", SND_MAX_ALIGN + 1);
114
115	printf("#ifdef SND_DECLARE_FXDIV\n");
116	printf("const uint32_t snd_fxdiv_table[%d][2] = {\n", SND_MAX_ALIGN + 1);
117
118	for (i = 1; i <= SND_MAX_ALIGN; i++) {
119		if (aligns[i] != 1)
120			continue;
121		calcdiv(r, i);
122		printf("\t[0x%02x] = { 0x%04x, 0x%02x },",		\
123		    i, r["mul"], r["shift"]);
124		printf("\t/* x / %-2d = (x * %-5d) >> %-2d */\n",	\
125		    i, r["mul"], r["shift"]);
126	}
127
128	printf("};\n#endif\n\n");
129
130	printf("#define SND_FXDIV_MAX\t\t0x%08x\n", FXONE);
131	printf("#define SND_FXDIV(x, y)\t\t(((uint32_t)(x) *\t\t\t\\\n");
132	printf("\t\t\t\t    snd_fxdiv_table[y][0]) >>\t\t\\\n");
133	printf("\t\t\t\t    snd_fxdiv_table[y][1])\n");
134	printf("#define SND_FXROUND(x, y)\t(SND_FXDIV(x, y) * (y))\n");
135	printf("#define SND_FXMOD(x, y)\t\t((x) - SND_FXROUND(x, y))\n\n");
136
137	printf("#else\t/* !SND_USE_FXDIV */\n\n");
138
139	printf("#define SND_FXDIV_MAX\t\t0x%08x\n", 131072);
140	printf("#define SND_FXDIV(x, y)\t\t((x) / (y))\n");
141	printf("#define SND_FXROUND(x, y)\t((x) - ((x) %% (y)))\n");
142	printf("#define SND_FXMOD(x, y)\t\t((x) %% (y))\n\n");
143
144	printf("#endif\t/* SND_USE_FXDIV */\n\n");
145
146	printf("#endif\t/* !_SND_FXDIV_GEN_H_ */\n");
147}
148