#!/usr/bin/env python3

"""

Generate lookup tables.

"""

import struct
import math
import sys

def float2str(x):
  return '%e' % x

def gen_ydy_table(name, bits, func):
  """output a table with y and dy values"""
  n = 1 << bits
  print('/* generated by ./tools/lut.py */')
  print('#define %s_BITS (%dU)' % (name, bits))
  print('#define %s_SIZE (1U << %s_BITS)' % (name, name))
  print('static const float %s_data[%s_SIZE << 1] = {' % (name, name))
  for i in range(n):
    if i == 0:
      sys.stdout.write('\t')
    if i != 0 and i % 4 == 0:
      sys.stdout.write('\r\n\t')
    (y, dy) = func(i)
    sys.stdout.write('%s, %s, ' % (y, dy))
  sys.stdout.write('\r\n};\r\n')

def cos_func(i, n):
  k = float(1 << n)
  y0 = math.cos(float(i) * 2.0 * math.pi / k)
  y1 = math.cos(float(i+1) * 2.0 * math.pi / k)
  y = float2str(y0)
  dy = float2str(y1 - y0)
  return (y, dy)

def main():
  n = 7
  gen_ydy_table('COS_LUT', n, lambda i: cos_func(i, n))

main()


#!/usr/bin/env python3

"""

Generate lookup tables.

"""

import struct
import math
import sys

def float2str(x):
  return '%e' % x

def gen_ydy_table(name, bits, func):
  """output a table with y and dy values"""
  n = 1 << bits
  print('/* generated by ./tools/lut.py */')
  print('#define %s_BITS (%dU)' % (name, bits))
  print('#define %s_SIZE (1U << %s_BITS)' % (name, name))
  print('static const float %s_data[%s_SIZE << 1] = {' % (name, name))
  for i in range(n):
    if i == 0:
      sys.stdout.write('\t')
    if i != 0 and i % 4 == 0:
      sys.stdout.write('\r\n\t')
    (y, dy) = func(i)
    sys.stdout.write('%s, %s, ' % (y, dy))
  sys.stdout.write('\r\n};\r\n')

def cos_func(i, n):
  k = float(1 << n)
  y0 = math.cos(float(i) * 2.0 * math.pi / k)
  y1 = math.cos(float(i+1) * 2.0 * math.pi / k)
  y = float2str(y0)
  dy = float2str(y1 - y0)
  return (y, dy)

def main():
  n = 7
  gen_ydy_table('COS_LUT', n, lambda i: cos_func(i, n))

main()


#!/usr/bin/env python3 Generate lookup tables. output a table with y and dy values