#!/usr/bin/env python3 """writes src/tables/cq_q15_tables.c, the q15 closed-loop quantizer's int16 images of the PRBA codebooks""" import os import re import struct ROOT = os.path.join(os.path.dirname(os.path.abspath(__file__)), '..') OUT = os.path.join(ROOT, 'src', 'tables', 'cq_q15_tables.c') SETS = [ # name, file, prba24, prba58, ok_prba24, ok_prba58 ('ambe2', 'tables.c', 'xombe__prba24', 'xombe__prba58', None, None), ('dstar', 'dstar_tables.c', 'xombe__ds_prba24', 'xombe__ds_prba58', 'xombe__ds_ok_prba24', 'xombe__ds_ok_prba58'), ] PAIRS24 = [(0, 0), (1, 1), (2, 2), (0, 1), (0, 2), (1, 2)] PAIRS58 = [(0, 0), (1, 1), (2, 2), (3, 3), (0, 1), (0, 2), (0, 3), (1, 2), (1, 3), (2, 3)] def fl32(x): return struct.unpack('f', struct.pack('f', x))[0] def f32(x): """x rounded to the nearest float, printed to read back exactly""" v = fl32(x) s = '%.9g' % v if 'e' not in s and '.' not in s: s += '.0' return s + 'f' def f32_up(x): """the smallest float >= x, x >= 0""" v = fl32(x) if v < x: v = struct.unpack('f', struct.pack('I', struct.unpack('I', struct.pack('f', v))[0] + 1))[0] return v def table(src, name, cols): body = re.search(name + r'\[\d+\]\[%d\] = \{(.*?)\};' % cols, src, re.S).group(1) v = [fl32(float(t.rstrip('f'))) for t in re.findall(r'[-+0-9.eE]+f', body)] return [v[i:i + cols] for i in range(0, len(v), cols)] def mask(src, name, n): if not name: return [1] * n body = re.search(name + r'\[%d\] = \{(.*?)\};' % n, src, re.S).group(1) return [int(t) for t in body.replace(',', ' ').split()] def rnd(v): """nearest integer, halves away from zero: |v - rnd(v)| <= 1/2""" return int(v + 0.5) if v >= 0 else -int(-v + 0.5) def scale(maxabs): """largest t with maxabs * 2^t <= 32767""" t = 0 while maxabs * 2.0 ** (t + 1) <= 32767: t += 1 while maxabs * 2.0 ** t > 32767: t -= 1 return t def image(rows, pairs, words): """monomials x_a x_b (a < b doubled) at 2^-tm, then x at 2^-tx, as int16 pairs padded to words""" dim = len(rows[0]) mono = [[(1.0 if a == b else 2.0) * r[a] * r[b] for a, b in pairs] for r in rows] # exact in double tm = scale(max(abs(v) for m in mono for v in m)) tx = scale(max(abs(v) for r in rows for v in r)) ints = [] for r, m in zip(rows, mono): h = [rnd(v * 2.0 ** tm) for v in m] + [rnd(v * 2.0 ** tx) for v in r] h += [0] * (2 * words - len(h)) ints.append(h) mmax = [max(abs(h[k]) for h in ints) / 2.0 ** tm for k in range(len(pairs))] xmax = [max(abs(h[len(pairs) + a]) for h in ints) / 2.0 ** tx for a in range(dim)] return ints, tm, tx, mmax, xmax def words_c(h): return '{' + ', '.join('0x%08xu' % ((h[2 * k] & 0xffff) | (h[2 * k + 1] & 0xffff) << 16) for k in range(len(h) // 2)) + '}' def floats(v): return '{' + ', '.join(f32(f32_up(x)) for x in v) + '}' def emit(out, name, src, n24, n58, ok24, ok58): p24, p58 = table(src, n24, 3), table(src, n58, 4) u24, u58 = mask(src, ok24, len(p24)), mask(src, ok58, len(p58)) i24, tm24, tx24, m24, x24 = image(p24, PAIRS24, 5) i58, tm58, tx58, m58, x58 = image(p58, PAIRS58, 8) # n3, n4 as the float code forms them, rounded up past its rounding n3 = max(sum(v * v for v in r) for r, u in zip(p24, u24) if u) * (1 + 2.0 ** -20) n4 = max(sum(v * v for v in r) for r, u in zip(p58, u58) if u) * (1 + 2.0 ** -20) used = [r for r, u in zip(p58, u58) if u] sx = [sum(r[a] for r in used) for a in range(4)] sxx = [[sum(r[a] * r[b] for r in used) for b in range(4)] for a in range(4)] out.append('static const uint32_t r24_%s[%d][5] = {' % (name, len(i24))) out += [' ' + words_c(h) + ',' for h in i24] out.append('};') out.append('static const uint32_t r58_%s[%d][8] = {' % (name, len(i58))) out += [' ' + words_c(h) + ',' for h in i58] out.append('};') out.append('const xombe_cq_q15_tab xombe__cq_q15_%s = {' % name) out.append(' r24_%s, r58_%s, %d, %d, %d, %d,' % (name, name, tm24, tx24, tm58, tx58)) tail = [floats(m24), floats(x24), floats(m58), floats(x58), f32(f32_up(n3)), f32(f32_up(n4)), str(len(used)), '{' + ', '.join(f32(v) for v in sx) + '}', '{' + ', '.join('{' + ', '.join(f32(v) for v in row) + '}' for row in sxx) + '}'] line = ' ' for i, t in enumerate(tail): for tok in t.split(' '): if len(line) + 1 + len(tok) > 116: out.append(line) line = ' ' line += ' ' + tok line += ',' if i < len(tail) - 1 else '};' out.append(line) def main(): out = ['/* generated by scripts/gen_cq_tables.py, do not edit */', '#include "cq_q15_tables.h"', '', '#if XOMBE_KERNELS_Q15 && (XOMBE_HAVE_ENCODER || XOMBE_HAVE_DSTAR)'] for name, fn, n24, n58, ok24, ok58 in SETS: src = open(os.path.join(ROOT, 'src', 'tables', fn)).read() if name == 'dstar': out.append('#if XOMBE_HAVE_DSTAR') emit(out, name, src, n24, n58, ok24, ok58) if name == 'dstar': out.append('#endif') out.append('#else') out.append('typedef int xombe__cq_q15_tables_unused;') out.append('#endif') with open(OUT, 'w') as f: f.write('\n'.join(out) + '\n') if __name__ == '__main__': main()