| |
|
|
| 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+");") |
|
|
|
|
| |
| def add(out, a, b, shift): |
| print_op2("add", out, a, b, shift) |
|
|
|
|
| |
| def sub(out, a, b, shift): |
| print_op2("sub", out, a, b, shift) |
|
|
|
|
| |
| def copy(out, a): |
| add(out, a, a, 0) |
|
|
|
|
| |
| def mul(out, a, b, shift): |
| print_op2("mul", out, a, b, shift) |
|
|
|
|
| |
| 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") |
|
|
|
|
| |
| 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") |
|
|
|
|
| |
| def cpx_copy(out, a): |
| copy(out+"_r", a+"_r") |
| copy(out+"_i", a+"_i") |
|
|
|
|
| |
| 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") |
|
|
|
|
| |
| 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") |
|
|
|
|
| |
| 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") |
|
|
|
|
| |
| 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) |
|
|
|
|
| |
| 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) |
|
|
|
|
| |
| 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") |
|
|
|
|
| |
| 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) |
|
|
|
|
| |
| |
| |
| |
| |
| 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") |
|
|
|
|
| |
| |
| 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") |
|
|
|
|
| |
| |
| 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") |
|
|