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