def pow_cc_c(gen,t,srcs): nonzero = gen.symbols.newLabel() done = gen.symbols.newLabel() dst_re = gen.newTemp(Float) dst_im = gen.newTemp(Float) gen.emit_cjump(srcs[0].re,nonzero) gen.emit_cjump(srcs[0].im,nonzero) # 0^foo = 0 gen.emit_move(ConstFloatArg(0.0),dst_re) gen.emit_move(ConstFloatArg(0.0),dst_im) gen.emit_jump(done) gen.emit_label(nonzero) # exp(y*log(x)) logx = log_c_c(gen,t,[srcs[0]]) ylogx = mul_cc_c(gen,t,[srcs[1],logx]) xtoy = exp_c_c(gen,t,[ylogx]) gen.emit_move(xtoy.re,dst_re) gen.emit_move(xtoy.im,dst_im) gen.emit_label(done) return ComplexArg(dst_re,dst_im)
def clamp_f_f(gen,t,srcs): # ensure f is in the range [0,1.0] smaller_than_one = gen.symbols.newLabel() done = gen.symbols.newLabel() one= ConstFloatArg(1.0) zero = ConstFloatArg(0.0) src = srcs[0] dst = TempArg(gen.symbols.newTemp(Float),Float) gen.emit_move(src,dst) lte1 = gen.emit_binop('<=',[src,one], Float) gen.emit_cjump(lte1,smaller_than_one) # larger than 1: use one instead gen.emit_move(one, dst) gen.emit_jump(done) gen.emit_label(smaller_than_one) gte0 = gen.emit_binop('>=', [src, zero], Float) gen.emit_cjump(gte0, done) # smaller than 0: use 0 instead gen.emit_move(zero, dst) gen.emit_label(done) return dst
def blend_CCf_C(gen,t,srcs): (a, b, factor) = srcs one_m_factor = gen.emit_binop('-',[ConstFloatArg(1.0), factor], Float) return add_CC_C( gen,t, [ mul_Cf_C(gen,t,[a,one_m_factor]), mul_Cf_C(gen,t,[b,factor])])
def sqr_c_c(gen,t,srcs): # sqr(a+ib) = a2 - b2 + i(2ab) src = srcs[0] a2 = gen.emit_binop('*', [src.re, src.re], Float) b2 = gen.emit_binop('*', [src.im, src.im], Float) ab = gen.emit_binop('*', [src.re, src.im], Float) dst = ComplexArg( gen.emit_binop('-', [a2, b2], Float), gen.emit_binop('*', [ ConstFloatArg(2.0), ab], Float)) return dst
def genfunc(gen,t,srcs): src = srcs[0] ax = gen.emit_binop('-', [src.parts[0], src.parts[3]], Float) ay = gen.emit_binop('+', [src.parts[1], src.parts[2]], Float) bx = gen.emit_binop('+', [src.parts[0], src.parts[3]], Float) by = gen.emit_binop('-', [src.parts[1], src.parts[2]], Float) res_a = f(gen,t, [ComplexArg(ax,ay)]) res_b = f(gen,t, [ComplexArg(bx,by)]) outa = gen.emit_binop('+', [res_a.re, res_b.re], Float) outb = gen.emit_binop('+', [res_a.im, res_b.im], Float) outc = gen.emit_binop('-', [res_a.im, res_b.im], Float) outd = gen.emit_binop('-', [res_b.re, res_a.re], Float) # divide by 2 return mul_hf_h(gen,t,[HyperArg(outa, outb, outc, outd), ConstFloatArg(0.5)])
def atanh_c_c(gen,t,srcs): # 1/2(log(1+z)-log(1-z)) one_m_z = log_c_c(gen,t,[sub_cc_c(gen,t,[const.one,srcs[0]])]) one_p_z = log_c_c(gen,t,[add_cc_c(gen,t,[const.one,srcs[0]])]) return mul_cf_c(gen,t,[sub_cc_c(gen,t,[one_p_z, one_m_z]), ConstFloatArg(0.5)])
def zero_c_c(gen,t,srcs): return ComplexArg(ConstFloatArg(0.0),ConstFloatArg(0.0))
def zero_f_f(gen,t,srcs): return ConstFloatArg(0.0)
def round_f_i(gen,t,srcs): return trunc_f_i(gen, t, [ gen.emit_binop('+',[ConstFloatArg(0.5), srcs[0]], Float)])
def sqrt_c_c(gen,t,srcs): xnonzero = gen.symbols.newLabel() done = gen.symbols.newLabel() dst_re = gen.newTemp(Float) dst_im = gen.newTemp(Float) gen.emit_cjump(srcs[0].re,xnonzero) # only an imaginary part : # temp = sqrt(abs(z.im) / 2); # return (temp, __y < 0 ? -__temp : __temp); temp = sqrt_f_f(gen, t, [ abs_f_f(gen,t, [ gen.emit_binop('/',[srcs[0].im, ConstFloatArg(2.0)],Float)])]) gen.emit_move(temp,dst_re) # y >= 0? ypos = gen.emit_binop('>=',[srcs[0].im,ConstFloatArg(0.0)], Float) ygtzero = gen.symbols.newLabel() gen.emit_cjump(ypos,ygtzero) nt = neg_f_f(gen,t, [temp]) gen.emit_move(nt,temp) gen.emit_label(ygtzero) gen.emit_move(temp,dst_im) gen.emit_jump(done) gen.emit_label(xnonzero) # both real and imaginary # temp = sqrt(2 * (cabs(z) + abs(z.re))); # u = temp/2 temp = sqrt_f_f( gen,t, [ gen.emit_binop( '*', [ConstFloatArg(2.0), gen.emit_binop( '+', [cabs_c_f(gen,t,[srcs[0]]), abs_f_f(gen,t,[srcs[0].re])], Float) ], Float) ]) u = gen.emit_binop('/',[temp,ConstFloatArg(2.0)], Float) #x > 0? xpos = gen.emit_binop('>',[srcs[0].re,ConstFloatArg(0.0)], Float) xgtzero = gen.symbols.newLabel() gen.emit_cjump(xpos,xgtzero) # x < 0: # x = abs(im)/temp gen.emit_move(gen.emit_binop( '/', [abs_f_f(gen,t,[srcs[0].im]), temp], Float) , dst_re) # y < 0 ? -u : u ypos2 = gen.emit_binop('>=',[srcs[0].im,ConstFloatArg(0.0)], Float) ygtzero2 = gen.symbols.newLabel() gen.emit_cjump(ypos2,ygtzero2) gen.emit_move(neg_f_f(gen,t,[u]), dst_im) gen.emit_jump(done) gen.emit_label(ygtzero2) gen.emit_move(u, dst_im) gen.emit_jump(done) # x > 0: gen.emit_label(xgtzero) # (u, im/temp) gen.emit_move(u,dst_re) gen.emit_move(gen.emit_binop('/',[srcs[0].im, temp], Float),dst_im) gen.emit_label(done) return ComplexArg(dst_re,dst_im)
def recip_c_c(gen,t,srcs): return div_cc_c(gen, None, [ComplexArg(ConstFloatArg(1.0), ConstFloatArg(0.0)), srcs[0]])
def recip_f_f(gen,t,srcs): # reciprocal return gen.emit_binop('/', [ConstFloatArg(1.0), srcs[0]], Float)
def conj_c_c(gen,t,srcs): # conj (a+ib) = a-ib b = gen.emit_binop('-', [ ConstFloatArg(0.0), srcs[0].im], Float) return ComplexArg(srcs[0].re,b)
def pow_ff_c(gen,t,srcs): arg = ComplexArg(srcs[0], ConstFloatArg(0.0)) return pow_cf_c(gen,t,[arg,srcs[1]])
def rgb_fff_C(gen,t,srcs): return ColorArg(srcs[0], srcs[1], srcs[2], ConstFloatArg(1.0))
def __init__(self): self.i = ComplexArg(ConstFloatArg(0.0),ConstFloatArg(1.0)) self.iby2 = ComplexArg(ConstFloatArg(0.0),ConstFloatArg(0.5)) self.minus_i = ComplexArg(ConstFloatArg(0.0),ConstFloatArg(-1.0)) self.one = ComplexArg(ConstFloatArg(1.0),ConstFloatArg(0.0))
def hsv_fff_C(gen,t,srcs): [d1,d2,d3] = gen.emit_func3_3("hsv_to_rgb", srcs, Float) return ColorArg(d1,d2,d3,ConstFloatArg(1.0))
def acos_c_c(gen,t,srcs): # acos(z) = pi/2 - asin(z) pi_by_2 = ComplexArg(ConstFloatArg(math.pi/2.0),ConstFloatArg(0.0)) return sub_cc_c(gen,t,[pi_by_2, asin_c_c(gen,t,srcs)])
def gradient_Gf_C(gen,t,srcs): [d1,d2,d3] = gen.emit_func2_3("gradient", srcs, Float) # fixme get alpha from gradient return ColorArg(d1,d2,d3,ConstFloatArg(1.0))
def _image_Ic_C(gen,t,srcs): c = srcs[1] [d1,d2,d3] = gen.emit_func3_3("image_lookup", [srcs[0], c.re, c.im], Float) return ColorArg(d1,d2,d3,ConstFloatArg(1.0))