10f40c3f1SAriff Abdullah#!/usr/bin/awk -f 20f40c3f1SAriff Abdullah# 3*4d846d26SWarner Losh# SPDX-License-Identifier: BSD-2-Clause 4fe267a55SPedro F. Giffuni# 50f40c3f1SAriff Abdullah# Copyright (c) 2008-2009 Ariff Abdullah <ariff@FreeBSD.org> 60f40c3f1SAriff Abdullah# All rights reserved. 70f40c3f1SAriff Abdullah# 80f40c3f1SAriff Abdullah# Redistribution and use in source and binary forms, with or without 90f40c3f1SAriff Abdullah# modification, are permitted provided that the following conditions 100f40c3f1SAriff Abdullah# are met: 110f40c3f1SAriff Abdullah# 1. Redistributions of source code must retain the above copyright 120f40c3f1SAriff Abdullah# notice, this list of conditions and the following disclaimer. 130f40c3f1SAriff Abdullah# 2. Redistributions in binary form must reproduce the above copyright 140f40c3f1SAriff Abdullah# notice, this list of conditions and the following disclaimer in the 150f40c3f1SAriff Abdullah# documentation and/or other materials provided with the distribution. 160f40c3f1SAriff Abdullah# 170f40c3f1SAriff Abdullah# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 180f40c3f1SAriff Abdullah# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 190f40c3f1SAriff Abdullah# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 200f40c3f1SAriff Abdullah# ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 210f40c3f1SAriff Abdullah# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 220f40c3f1SAriff Abdullah# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 230f40c3f1SAriff Abdullah# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 240f40c3f1SAriff Abdullah# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 250f40c3f1SAriff Abdullah# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 260f40c3f1SAriff Abdullah# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 270f40c3f1SAriff Abdullah# SUCH DAMAGE. 280f40c3f1SAriff Abdullah# 290f40c3f1SAriff Abdullah# 300f40c3f1SAriff Abdullah 310f40c3f1SAriff Abdullahfunction floor(x, r) 320f40c3f1SAriff Abdullah{ 330f40c3f1SAriff Abdullah r = int(x); 340f40c3f1SAriff Abdullah if (r > x) 350f40c3f1SAriff Abdullah r--; 360f40c3f1SAriff Abdullah return (r + 0); 370f40c3f1SAriff Abdullah} 380f40c3f1SAriff Abdullah 390f40c3f1SAriff Abdullahfunction shl(x, y) 400f40c3f1SAriff Abdullah{ 410f40c3f1SAriff Abdullah while (y > 0) { 420f40c3f1SAriff Abdullah x *= 2; 430f40c3f1SAriff Abdullah y--; 440f40c3f1SAriff Abdullah } 450f40c3f1SAriff Abdullah return (x); 460f40c3f1SAriff Abdullah} 470f40c3f1SAriff Abdullah 480f40c3f1SAriff Abdullahfunction shr(x, y) 490f40c3f1SAriff Abdullah{ 500f40c3f1SAriff Abdullah while (y > 0 && x != 0) { 510f40c3f1SAriff Abdullah x = floor(x / 2); 520f40c3f1SAriff Abdullah y--; 530f40c3f1SAriff Abdullah } 540f40c3f1SAriff Abdullah return (x); 550f40c3f1SAriff Abdullah} 560f40c3f1SAriff Abdullah 570f40c3f1SAriff Abdullahfunction calcdiv(r, x, y, z) 580f40c3f1SAriff Abdullah{ 590f40c3f1SAriff Abdullah y = floor(FXONE / x); 600f40c3f1SAriff Abdullah z = FXSHIFT; 610f40c3f1SAriff Abdullah 620f40c3f1SAriff Abdullah while (shr((y * x), z) < 1) 630f40c3f1SAriff Abdullah y++; 640f40c3f1SAriff Abdullah 650f40c3f1SAriff Abdullah while ((y % 2) == 0 && z > 0) { 660f40c3f1SAriff Abdullah y = floor(y / 2); 670f40c3f1SAriff Abdullah z--; 680f40c3f1SAriff Abdullah } 690f40c3f1SAriff Abdullah 700f40c3f1SAriff Abdullah r["mul"] = y; 710f40c3f1SAriff Abdullah r["shift"] = z; 720f40c3f1SAriff Abdullah} 730f40c3f1SAriff Abdullah 740f40c3f1SAriff AbdullahBEGIN { 750f40c3f1SAriff Abdullah FXSHIFT = 16; 760f40c3f1SAriff Abdullah FXONE = shl(1, FXSHIFT); 770f40c3f1SAriff Abdullah 780f86d40bSHans Petter Selasky SND_CHN_MAX = 127; 790f40c3f1SAriff Abdullah 800f40c3f1SAriff Abdullah PCM_8_BPS = 1; 810f40c3f1SAriff Abdullah PCM_16_BPS = 2; 820f40c3f1SAriff Abdullah PCM_24_BPS = 3; 830f40c3f1SAriff Abdullah PCM_32_BPS = 4; 840f40c3f1SAriff Abdullah 850f40c3f1SAriff Abdullah SND_MAX_ALIGN = SND_CHN_MAX * PCM_32_BPS; 860f40c3f1SAriff Abdullah 870f40c3f1SAriff Abdullah for (i = 1; i <= SND_CHN_MAX; i++) { 880f40c3f1SAriff Abdullah aligns[PCM_8_BPS * i] = 1; 890f40c3f1SAriff Abdullah aligns[PCM_16_BPS * i] = 1; 900f40c3f1SAriff Abdullah aligns[PCM_24_BPS * i] = 1; 910f40c3f1SAriff Abdullah aligns[PCM_32_BPS * i] = 1; 920f40c3f1SAriff Abdullah } 930f40c3f1SAriff Abdullah 940f40c3f1SAriff Abdullah printf("#ifndef _SND_FXDIV_GEN_H_\n"); 950f40c3f1SAriff Abdullah printf("#define _SND_FXDIV_GEN_H_\n\n"); 960f40c3f1SAriff Abdullah 970f40c3f1SAriff Abdullah printf("/*\n"); 980f40c3f1SAriff Abdullah printf(" * Generated using snd_fxdiv_gen.awk, heaven, wind and awesome.\n"); 990f40c3f1SAriff Abdullah printf(" *\n"); 1000f40c3f1SAriff Abdullah printf(" * DO NOT EDIT!\n"); 1010f40c3f1SAriff Abdullah printf(" */\n\n"); 1020f40c3f1SAriff Abdullah printf("#ifdef SND_USE_FXDIV\n\n"); 1030f40c3f1SAriff Abdullah 1040f40c3f1SAriff Abdullah printf("/*\n"); 1050f40c3f1SAriff Abdullah printf(" * Fast unsigned 32bit integer division and rounding, accurate for\n"); 1060f40c3f1SAriff Abdullah printf(" * x = 1 - %d. This table should be enough to handle possible\n", FXONE); 1070f86d40bSHans Petter Selasky printf(" * division for 1 - 508 (more can be generated though..).\n"); 1080f40c3f1SAriff Abdullah printf(" *\n"); 1090f86d40bSHans Petter Selasky printf(" * 508 = SND_CHN_MAX * PCM_32_BPS, which is why....\n"); 1100f40c3f1SAriff Abdullah printf(" */\n\n"); 1110f40c3f1SAriff Abdullah 1123f9dcc58SHans Petter Selasky printf("extern const uint32_t snd_fxdiv_table[%d][2];\n\n", SND_MAX_ALIGN + 1); 1133f9dcc58SHans Petter Selasky 1143f9dcc58SHans Petter Selasky printf("#ifdef SND_DECLARE_FXDIV\n"); 1153f9dcc58SHans Petter Selasky printf("const uint32_t snd_fxdiv_table[%d][2] = {\n", SND_MAX_ALIGN + 1); 1160f40c3f1SAriff Abdullah 1170f40c3f1SAriff Abdullah for (i = 1; i <= SND_MAX_ALIGN; i++) { 1180f40c3f1SAriff Abdullah if (aligns[i] != 1) 1190f40c3f1SAriff Abdullah continue; 1200f40c3f1SAriff Abdullah calcdiv(r, i); 1210f40c3f1SAriff Abdullah printf("\t[0x%02x] = { 0x%04x, 0x%02x },", \ 1220f40c3f1SAriff Abdullah i, r["mul"], r["shift"]); 1230f40c3f1SAriff Abdullah printf("\t/* x / %-2d = (x * %-5d) >> %-2d */\n", \ 1240f40c3f1SAriff Abdullah i, r["mul"], r["shift"]); 1250f40c3f1SAriff Abdullah } 1260f40c3f1SAriff Abdullah 1273f9dcc58SHans Petter Selasky printf("};\n#endif\n\n"); 1280f40c3f1SAriff Abdullah 1290f40c3f1SAriff Abdullah printf("#define SND_FXDIV_MAX\t\t0x%08x\n", FXONE); 1300f40c3f1SAriff Abdullah printf("#define SND_FXDIV(x, y)\t\t(((uint32_t)(x) *\t\t\t\\\n"); 1310f40c3f1SAriff Abdullah printf("\t\t\t\t snd_fxdiv_table[y][0]) >>\t\t\\\n"); 1320f40c3f1SAriff Abdullah printf("\t\t\t\t snd_fxdiv_table[y][1])\n"); 1330f40c3f1SAriff Abdullah printf("#define SND_FXROUND(x, y)\t(SND_FXDIV(x, y) * (y))\n"); 1340f40c3f1SAriff Abdullah printf("#define SND_FXMOD(x, y)\t\t((x) - SND_FXROUND(x, y))\n\n"); 1350f40c3f1SAriff Abdullah 1360f40c3f1SAriff Abdullah printf("#else\t/* !SND_USE_FXDIV */\n\n"); 1370f40c3f1SAriff Abdullah 1380f40c3f1SAriff Abdullah printf("#define SND_FXDIV_MAX\t\t0x%08x\n", 131072); 1390f40c3f1SAriff Abdullah printf("#define SND_FXDIV(x, y)\t\t((x) / (y))\n"); 1400f40c3f1SAriff Abdullah printf("#define SND_FXROUND(x, y)\t((x) - ((x) %% (y)))\n"); 1410f40c3f1SAriff Abdullah printf("#define SND_FXMOD(x, y)\t\t((x) %% (y))\n\n"); 1420f40c3f1SAriff Abdullah 1430f40c3f1SAriff Abdullah printf("#endif\t/* SND_USE_FXDIV */\n\n"); 1440f40c3f1SAriff Abdullah 1450f40c3f1SAriff Abdullah printf("#endif\t/* !_SND_FXDIV_GEN_H_ */\n"); 1460f40c3f1SAriff Abdullah} 147