SAIFIINDUSTRIES's picture
Add Batch 8 with 10 repos
7df9186 verified
Raw
History Blame Contribute Delete
4.57 kB
#!/usr/bin/python
n_given = 0
state_vars = {}
def cgen_init(name):
print("// machine-generated by " + name)
print("#define ZZZ")
def setup(a):
v = "int"
if a in state_vars:
v = "ZZZ"
return v+" "+a
def given(a):
global n_given
print(setup(a) + " = given[" + str(n_given) + "];")
n_given += 1
def persist(a, init=0):
print("static int " + a + "; if (init) " + a + " = persist_get(\"" + a + "\");")
state_vars[a] = 1
def cpx_persist(a, init=0):
persist(a + "_r", init=init.real)
persist(a + "_i", init=init.imag)
def comment(s):
print("if (VERBOSE) printf(\""+s+"\\n\");")
def print_op2(op, out, a, b, shift):
print(setup(out)+" = "+op+"("+a+", "+b+", "+str(shift)+");")
def inv(out, a, shift):
print(setup(out)+" = inv("+a+", "+str(shift)+");")
def set_result(o, a, b):
print("set_result_"+o+"("+a+", "+b+");")
# out = (a + b) * 2^{shift}
def add(out, a, b, shift):
print_op2("add", out, a, b, shift)
# out = (a - b) * 2^{shift}
def sub(out, a, b, shift):
print_op2("sub", out, a, b, shift)
# out = a
def copy(out, a):
add(out, a, a, 0)
# out = a * b * 2^{shift}
def mul(out, a, b, shift):
print_op2("mul", out, a, b, shift)
# out = (a + b) * 2^{shift-1}
def cpx_add(out, a, b, shift):
add(out+"_r", a+"_r", b+"_r", shift)
add(out+"_i", a+"_i", b+"_i", shift)
comment("done with cpx_add")
# out = (a - b) * 2^{shift-1}
def cpx_sub(out, a, b, shift):
sub(out+"_r", a+"_r", b+"_r", shift)
sub(out+"_i", a+"_i", b+"_i", shift)
comment("done with cpx_sub")
# out = a
def cpx_copy(out, a):
copy(out+"_r", a+"_r")
copy(out+"_i", a+"_i")
# out = a * b * 2^{shift+shift2-1}
def cpx_mul(out, a, b, shift, shift2):
mul(out+"_t2", a+"_r", b+"_i", shift)
mul(out+"_t3", a+"_i", b+"_r", shift)
mul(out+"_t1", a+"_r", b+"_r", shift)
mul(out+"_t4", a+"_i", b+"_i", shift)
add(out+"_i", out+"_t2", out+"_t3", shift2)
sub(out+"_r", out+"_t1", out+"_t4", shift2)
comment("done with cpx_mul")
# out = a * conj(b) * 2^{shift+shift2-1}
def cpx_mul_conj(out, a, b, shift, shift2):
mul(out+"_t2", a+"_r", b+"_i", shift)
mul(out+"_t3", a+"_i", b+"_r", shift)
mul(out+"_t4", a+"_i", b+"_i", shift)
mul(out+"_t1", a+"_r", b+"_r", shift)
sub(out+"_i", out+"_t3", out+"_t2", shift2)
add(out+"_r", out+"_t1", out+"_t4", shift2)
comment("done with cpx_mul_conj")
# out = a * a * 2^{shift}
def cpx_sqr(out, a, shift):
mul(out+"_t2", a+"_i", a+"_i", shift)
mul(out+"_t1", a+"_r", a+"_r", shift)
mul(out+"_i", a+"_r", a+"_i", shift+1)
sub(out+"_r", out+"_t1", out+"_t2", 1)
comment("done with cpx_sqr")
# out = a * conj(a) * 2^{shift}
def cpx_mag(out, a, shift):
mul(out+"_t2", a+"_i", a+"_i", shift)
mul(out+"_t1", a+"_r", a+"_r", shift)
add(out, out+"_t1", out+"_t2", 1)
# out = Re( a * b ) * 2^{shift}
def cpx_dot(out, a, b, shift):
mul(out+"_t2", a+"_i", b+"_i", shift)
mul(out+"_t1", a+"_r", b+"_r", shift)
add(out, out+"_t1", out+"_t2", 1)
# out = a * b * 2^{shift} (b is the scalar)
def cpx_scale(out, a, b, shift):
mul(out+"_r", a+"_r", b, shift)
mul(out+"_i", a+"_i", b, shift)
comment("done with cpx_scale")
# one iteration of iterative refinement of 1/a (prev is the previous estimate)
def inv_iter(out, a, prev):
mul(out+"_e", prev, a, 3)
sub(out+"_f", "two", out+"_e", 3)
mul(out, prev, out+"_f", 3)
# out = 2^{-8}/a
# Carefully tuned interaction between the inv primitive,
# inv_iter, and the constant value "two"
# Accuracy is mediocre at the moment (0.4%), could either add resolution
# to inv primitive, or add another inv_iter cycle.
def full_inv(out, a):
inv(out+"_guess", a, 0)
inv_iter(out+"_r1", a, out+"_guess")
inv_iter(out, a, out+"_r1")
comment("done with full_inv")
# out = conj(1/a) * 2^{shift2-8-shift}
# probably want to keep shift as 0
def cpx_inv_conj(out, a, shift, shift2):
mul(out+"_r1", a+"_r", a+"_r", shift)
mul(out+"_r2", a+"_i", a+"_i", shift)
add(out+"_m", out+"_r1", out+"_r2", 1)
full_inv(out+"_s", out+"_m")
mul(out+"_r", out+"_s", a+"_r", shift2)
mul(out+"_i", out+"_s", a+"_i", shift2)
comment("done with cpx_inv_conj")
# out = a / ( b^2 * conj(c) )
# scaling is probably messed up
def cpx_triad(out, a, b, c):
cpx_sqr(out+"_t1", b, 1)
cpx_mul_conj(out+"_t2", out+"_t1", c, 2, 1)
cpx_inv_conj(out+"_t3", out+"_t2", 0, 0)
cpx_mul_conj(out, a, out+"_t3", 2, 1)
comment("done with cpx_triad")