]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/tools/sound/snd_fxdiv_gen.awk
makesyscalls: add COMPAT14 support
[FreeBSD/FreeBSD.git] / sys / tools / sound / snd_fxdiv_gen.awk
1 #!/usr/bin/awk -f
2 #
3 # SPDX-License-Identifier: BSD-2-Clause
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 #
30
31 function floor(x, r)
32 {
33         r = int(x);
34         if (r > x)
35                 r--;
36         return (r + 0);
37 }
38
39 function shl(x, y)
40 {
41         while (y > 0) {
42                 x *= 2;
43                 y--;
44         }
45         return (x);
46 }
47
48 function shr(x, y)
49 {
50         while (y > 0 && x != 0) {
51                 x = floor(x / 2);
52                 y--;
53         }
54         return (x);
55 }
56
57 function calcdiv(r, x, y, z)
58 {
59         y = floor(FXONE / x);
60         z = FXSHIFT;
61
62         while (shr((y * x), z) < 1)
63                 y++;
64
65         while ((y % 2) == 0 && z > 0) {
66                 y = floor(y / 2);
67                 z--;
68         }
69
70         r["mul"] = y;
71         r["shift"] = z;
72 }
73
74 BEGIN {
75         FXSHIFT = 16;
76         FXONE   = shl(1, FXSHIFT);
77
78         SND_CHN_MAX = 127;
79
80         PCM_8_BPS  = 1;
81         PCM_16_BPS = 2;
82         PCM_24_BPS = 3;
83         PCM_32_BPS = 4;
84
85         SND_MAX_ALIGN = SND_CHN_MAX * PCM_32_BPS;
86
87         for (i = 1; i <= SND_CHN_MAX; i++) {
88                 aligns[PCM_8_BPS * i]  = 1;
89                 aligns[PCM_16_BPS * i] = 1;
90                 aligns[PCM_24_BPS * i] = 1;
91                 aligns[PCM_32_BPS * i] = 1;
92         }
93
94         printf("#ifndef _SND_FXDIV_GEN_H_\n");
95         printf("#define _SND_FXDIV_GEN_H_\n\n");
96
97         printf("/*\n");
98         printf(" * Generated using snd_fxdiv_gen.awk, heaven, wind and awesome.\n");
99         printf(" *\n");
100         printf(" * DO NOT EDIT!\n");
101         printf(" */\n\n");
102         printf("#ifdef SND_USE_FXDIV\n\n");
103
104         printf("/*\n");
105         printf(" * Fast unsigned 32bit integer division and rounding, accurate for\n");
106         printf(" * x = 1 - %d. This table should be enough to handle possible\n", FXONE);
107         printf(" * division for 1 - 508 (more can be generated though..).\n");
108         printf(" *\n");
109         printf(" * 508 = SND_CHN_MAX * PCM_32_BPS, which is why....\n");
110         printf(" */\n\n");
111
112         printf("extern const uint32_t snd_fxdiv_table[%d][2];\n\n", SND_MAX_ALIGN + 1);
113
114         printf("#ifdef SND_DECLARE_FXDIV\n");
115         printf("const uint32_t snd_fxdiv_table[%d][2] = {\n", SND_MAX_ALIGN + 1);
116
117         for (i = 1; i <= SND_MAX_ALIGN; i++) {
118                 if (aligns[i] != 1)
119                         continue;
120                 calcdiv(r, i);
121                 printf("\t[0x%02x] = { 0x%04x, 0x%02x },",              \
122                     i, r["mul"], r["shift"]);
123                 printf("\t/* x / %-2d = (x * %-5d) >> %-2d */\n",       \
124                     i, r["mul"], r["shift"]);
125         }
126
127         printf("};\n#endif\n\n");
128
129         printf("#define SND_FXDIV_MAX\t\t0x%08x\n", FXONE);
130         printf("#define SND_FXDIV(x, y)\t\t(((uint32_t)(x) *\t\t\t\\\n");
131         printf("\t\t\t\t    snd_fxdiv_table[y][0]) >>\t\t\\\n");
132         printf("\t\t\t\t    snd_fxdiv_table[y][1])\n");
133         printf("#define SND_FXROUND(x, y)\t(SND_FXDIV(x, y) * (y))\n");
134         printf("#define SND_FXMOD(x, y)\t\t((x) - SND_FXROUND(x, y))\n\n");
135
136         printf("#else\t/* !SND_USE_FXDIV */\n\n");
137
138         printf("#define SND_FXDIV_MAX\t\t0x%08x\n", 131072);
139         printf("#define SND_FXDIV(x, y)\t\t((x) / (y))\n");
140         printf("#define SND_FXROUND(x, y)\t((x) - ((x) %% (y)))\n");
141         printf("#define SND_FXMOD(x, y)\t\t((x) %% (y))\n\n");
142
143         printf("#endif\t/* SND_USE_FXDIV */\n\n");
144
145         printf("#endif\t/* !_SND_FXDIV_GEN_H_ */\n");
146 }