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