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