Skip to content

Commit

Permalink
Convert our Python files to Python3.
Browse files Browse the repository at this point in the history
This mostly involved switching 'prints' to the function syntax, but in
make_converters.py this process revealed a bug where the generated code
epended on the iteration order of a dictionary.
  • Loading branch information
SiegeLordEx authored and SiegeLord committed Sep 2, 2019
1 parent 878420f commit d5202b7
Show file tree
Hide file tree
Showing 9 changed files with 184 additions and 155 deletions.
9 changes: 8 additions & 1 deletion contrib/luajit/generate_luajit_ffi.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/usr/bin/env python
#!/usr/bin/env python3

"""
This script will use the prototypes from "checkdocs.py -s" to concoct
Expand Down Expand Up @@ -80,10 +80,17 @@ def parse_protos(self, filename):
if "=" in field:
fname, val = field.split("=", 1)
fname = fname.strip()
# replace any 'X' (an integer value in C) with
# ord('X') to match up in Python
val = re.sub("('.')", "ord(\\1)", val)
try:
i = int(eval(val, globals(), self.constants))
except NameError:
i = val
except Exception:
raise ValueError(
"Exception while parsing '{}'".format(
val))
else:
fname = field.strip()
if not fname:
Expand Down
78 changes: 47 additions & 31 deletions misc/make_converters.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#!/usr/bin/env python
import optparse, re, sys
#!/usr/bin/env python3
import collections, optparse, re, sys

formats_by_name = {}
formats_list = []
Expand Down Expand Up @@ -32,46 +32,58 @@ def parse_format(format):
separator = format.find("_")
class Info: pass
class Component: pass

Info = collections.namedtuple("Info", "components, name, little_endian,"
"float, single_channel, size")
Component = collections.namedtuple("Component", "color, size,"
"position_from_left, position")

pos = 0
info = Info()
info.components = {}
info.name = format
info.little_endian = "_LE" in format
info.float = False
info.single_channel = False
info = Info(
components={},
name=format,
little_endian="_LE" in format,
float=False,
single_channel=False,
size=None,
)

if "F32" in format:
info.float = True
info.size = 128
return info

return info._replace(
float=True,
size=128,
)

if "SINGLE_CHANNEL" in format:
info.single_channel = True
info.size = 8
return info

return info._replace(
single_channel=True,
size=8,
)

for i in range(separator):
c = Component()
c.color = format[i]
c.size = int(format[separator + 1 + i])
c.position_from_left = pos
c = Component(
color=format[i],
size=int(format[separator + 1 + i]),
position_from_left=pos,
position=None,
)
info.components[c.color] = c
pos += c.size
size = pos
for c in info.components.values():
c.position = size - c.position_from_left - c.size
info.size = size

info.float = False
return info
return info._replace(
components={k: c._replace(position=size - c.position_from_left - c.size)
for k, c in info.components.items()},
size=size,
float=False,
)

def macro_lines(info_a, info_b):
"""
Write out the lines of a conversion macro.
"""
r = ""

names = info_b.components.keys()
names = list(info_b.components.keys())
names.sort()

if info_a.float:
Expand Down Expand Up @@ -174,20 +186,24 @@ def macro_lines(info_a, info_b):
# Collapse multiple components if possible.
common_shifts = {}
for name, (mask, shift, add, size_a, size_b, mask_pos) in ops.items():
if not add:
if not add and not (size_a != 8 and size_b == 8):
if shift in common_shifts: common_shifts[shift].append(name)
else: common_shifts[shift] = [name]
for newshift, colors in common_shifts.items():
if len(colors) == 1: continue
newname = ""
newmask = 0
colors.sort()
masks_pos = []
for name in colors:
mask, shift, add, size_a, size_b, mask_pos = ops[name]

names.remove(name)
newname += name
newmask |= ops[name][0]
newmask |= mask
masks_pos.append(mask_pos)
names.append(newname)
ops[newname] = (newmask, shift, 0, size_a, size_b, mask_pos)
ops[newname] = (newmask, newshift, 0, size_a, size_b, min(masks_pos))

# Write out a line for each remaining operation.
lines = []
Expand Down Expand Up @@ -441,7 +457,7 @@ def main(argv):

# Read in color.h to get the available formats.
formats = read_color_h("include/allegro5/color.h")

print(formats)

# Parse the component info for each format.
Expand Down
56 changes: 28 additions & 28 deletions misc/make_mixer_helpers.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#!/usr/bin/env python2
#!/usr/bin/env python3
#
# Run:
# python2 misc/make_resamplers.py | indent -kr -i3 -l0
# misc/make_resamplers.py | indent -kr -i3 -l0

import sys, re

Expand Down Expand Up @@ -88,7 +88,7 @@ def index_s16(self, buf, index):
]

def make_point_interpolator(name, fmt):
print interp("""\
print(interp("""\
static INLINE const void *
#{name}
(SAMP_BUF *samp_buf,
Expand All @@ -99,27 +99,27 @@ def make_point_interpolator(name, fmt):
unsigned int i;
switch (spl->spl_data.depth) {
""")
"""))

for depth in depths:
buf_index = depth.index(fmt)("spl->spl_data.buffer", "i0 + i")
print interp("""\
print(interp("""\
case #{depth.constant()}:
for (i = 0; i < maxc; i++) {
samp_buf-> #{fmt} [i] = #{buf_index};
}
break;
""")
"""))

print interp("""\
print(interp("""\
}
return samp_buf-> #{fmt} ;
}""")
}"""))

def make_linear_interpolator(name, fmt):
assert fmt == "f32" or fmt == "s16"

print interp("""\
print(interp("""\
static INLINE const void *
#{name}
(SAMP_BUF *samp_buf,
Expand Down Expand Up @@ -164,50 +164,50 @@ def make_linear_interpolator(name, fmt):
p1 *= maxc;
switch (spl->spl_data.depth) {
""")
"""))

for depth in depths:
x0 = depth.index(fmt)("spl->spl_data.buffer", "p0 + i")
x1 = depth.index(fmt)("spl->spl_data.buffer", "p1 + i")
print interp("""\
print(interp("""\
case #{depth.constant()}:
{""")
{"""))

if fmt == "f32":
print interp("""\
print(interp("""\
const float t = (float)spl->pos_bresenham_error / spl->step_denom;
int i;
for (i = 0; i < (int)maxc; i++) {
const float x0 = #{x0};
const float x1 = #{x1};
const float s = (x0 * (1.0f - t)) + (x1 * t);
samp_buf->f32[i] = s;
}""")
}"""))
elif fmt == "s16":
print interp("""\
print(interp("""\
const int32_t t = 256 * spl->pos_bresenham_error / spl->step_denom;
int i;
for (i = 0; i < (int)maxc; i++) {
const int32_t x0 = #{x0};
const int32_t x1 = #{x1};
const int32_t s = ((x0 * (256 - t))>>8) + ((x1 * t)>>8);
samp_buf->s16[i] = (int16_t)s;
}""")
}"""))

print interp("""\
print(interp("""\
}
break;
""")
"""))

print interp("""\
print(interp("""\
}
return samp_buf-> #{fmt};
}""")
}"""))

def make_cubic_interpolator(name, fmt):
assert fmt == "f32"

print interp("""\
print(interp("""\
static INLINE const void *
#{name}
(SAMP_BUF *samp_buf,
Expand Down Expand Up @@ -256,7 +256,7 @@ def make_cubic_interpolator(name, fmt):
p3 *= maxc;
switch (spl->spl_data.depth) {
""")
"""))

for depth in depths:
value0 = depth.index(fmt)("spl->spl_data.buffer", "p0 + i")
Expand All @@ -267,7 +267,7 @@ def make_cubic_interpolator(name, fmt):
# Code transcribed from "Polynomial Interpolators for High-Quality
# Resampling of Oversampled Audio" by Olli Niemitalo
# http://yehar.com/blog/?p=197
print interp("""\
print(interp("""\
case #{depth.constant()}:
{
const float t = (float)spl->pos_bresenham_error / spl->step_denom;
Expand All @@ -286,16 +286,16 @@ def make_cubic_interpolator(name, fmt):
}
}
break;
""")
"""))

print interp("""\
print(interp("""\
}
return samp_buf-> #{fmt} ;
}""")
}"""))

if __name__ == "__main__":
print "// Warning: This file was created by make_resamplers.py - do not edit."
print "// vim: set ft=c:"
print("// Warning: This file was created by make_resamplers.py - do not edit.")
print("// vim: set ft=c:")

make_point_interpolator("point_spl32", "f32")
make_point_interpolator("point_spl16", "s16")
Expand Down
Loading

0 comments on commit d5202b7

Please sign in to comment.