protected override void Compile(CodeContext c) { var v1 = c.Int32("v1"); var v2 = c.Int32("v2"); c.SetArgument(0, v1); c.SetArgument(1, v2); var counter = c.Int32("counter"); var l1 = c.Label(); var loop = c.Label(); var exit = c.Label(); c.Cmp(v1, v2); c.Jg(l1); c.Mov(counter, 0); c.Bind(loop); c.Mov(v1, counter); c.Inc(counter); c.Cmp(counter, 1); c.Jle(loop); c.Jmp(exit); c.Bind(l1); c.Mov(v1, 2); c.Bind(exit); c.Ret(v1); }
protected override void Compile(CodeContext c) { var stack = c.Stack(256, 1).SetSize(1); var i = c.IntPtr("i"); var a = c.Int32("a"); var b = c.Int32("b"); var l1 = c.Label(); var l2 = c.Label(); // Fill stack by sequence [0, 1, 2, 3 ... 255]. c.Xor(i, i); c.Bind(l1); c.Mov(stack.Clone().SetIndex(i), i.As8()); c.Inc(i); c.Cmp(i, 255); c.Jle(l1); // Sum sequence in stack. c.Xor(i, i); c.Xor(a, a); c.Bind(l2); c.Movzx(b, stack.Clone().SetIndex(i)); c.Add(a, b); c.Inc(i); c.Cmp(i, 255); c.Jle(l2); c.Ret(a); }
protected override void Compile(CodeContext c) { var op = c.Int32("op"); var a = c.Int32("a"); var b = c.Int32("b"); var zero = c.Label(); var add = c.Label(); var sub = c.Label(); var mul = c.Label(); var div = c.Label(); c.SetArgument(0, op); c.SetArgument(1, a); c.SetArgument(2, b); c.Cmp(op, 0); c.Jz(add); c.Cmp(op, 1); c.Jz(sub); c.Cmp(op, 2); c.Jz(mul); c.Cmp(op, 3); c.Jz(div); c.Bind(zero); c.Xor(a, a); c.Ret(a); c.Bind(add); c.Add(a, b); c.Ret(a); c.Bind(sub); c.Sub(a, b); c.Ret(a); c.Bind(mul); c.Imul(a, b); c.Ret(a); c.Bind(div); c.Cmp(b, 0); c.Jz(zero); var z = c.Int32("z"); c.Xor(z, z); c.Idiv(z, a, b); c.Ret(a); }
protected override void Compile(CodeContext c) { var v1 = c.Int32("v1"); var v2 = c.Int32("v2"); c.SetArgument(0, v1); c.SetArgument(1, v2); var l1 = c.Label(); var l2 = c.Label(); var l3 = c.Label(); var l4 = c.Label(); c.Jmp(l1); c.Bind(l2); c.Jmp(l4); c.Bind(l1); c.Cmp(v1, v2); c.Jg(l3); c.Mov(v1, 1); c.Jmp(l2); c.Bind(l3); c.Mov(v1, 2); c.Jmp(l2); c.Bind(l4); c.Ret(v1); }
// [UnmanagedFunctionPointer(CallingConvention.Cdecl)] // delegate int CalledFunctionDel(int x, int y); protected override void Compile(CodeContext c) { var x = c.Int32("x"); var y = c.Int32("y"); var op = c.Int32("op"); c.SetArgument(0, x); c.SetArgument(1, y); c.SetArgument(2, op); var opAdd = c.Label(); var opMul = c.Label(); c.Cmp(op, 0); c.Jz(opAdd); c.Cmp(op, 1); c.Jz(opMul); var result = c.Int32("result_0"); c.Mov(result, 0); c.Ret(result); c.Bind(opAdd); result = c.Int32("result_1"); // CalledFunctionDel f = CalledFunctionAdd; // var call = c.Call(Memory.Fn(f)); var call = c.Call(Memory.Fn(new Func <int, int, int>(CalledFunctionAdd))); call.SetArgument(0, x); call.SetArgument(1, y); call.SetReturn(0, result); c.Ret(result); c.Bind(opMul); result = c.Int32("result_2"); // f = CalledFunctionMul; // call = c.Call(Memory.Fn(f)); call = c.Call(Memory.Fn(new Func <int, int, int>(CalledFunctionMul))); call.SetArgument(0, x); call.SetArgument(1, y); call.SetReturn(0, result); c.Ret(result); }
protected override void Compile(CodeContext c) { var src0 = c.Int32("src0"); var src1 = c.Int32("src1"); var dst0 = c.IntPtr("dst0"); c.SetArgument(0, src0); c.SetArgument(1, src1); c.SetArgument(2, dst0); c.Cmp(src0, src1); c.Setz(Memory.Byte(dst0)); }
protected override void Compile(CodeContext c) { var l1 = c.Label(); var l2 = c.Label(); var v0 = c.UInt32("v0"); var v1 = c.UInt32("v1"); c.Jmp(l1); c.Bind(l2); c.Mov(v0, 1); c.Mov(v1, 2); c.Cmp(v0, v1); c.Jz(l2); c.Jmp(l1); c.Bind(l1); c.Ret(); }
protected override void Compile(CodeContext c) { var l1 = c.Label(); var iIdx = c.Int32("iIdx"); var iEnd = c.Int32("iEnd"); var aIdx = c.Int32("aIdx"); var aEnd = c.Int32("aEnd"); c.SetArgument(0, aIdx); c.SetArgument(1, aEnd); c.Mov(iIdx, aIdx); c.Mov(iEnd, aEnd); c.Spill(iEnd); c.Bind(l1); c.Inc(iIdx); c.Cmp(iIdx, iEnd.ToMemory()); c.Jne(l1); c.Ret(iIdx); }
protected override void Compile(CodeContext c) { var dst = c.IntPtr("dst"); var src = c.IntPtr("src"); var i = c.IntPtr("i"); var j = c.IntPtr("j"); var t = c.IntPtr("t"); var cZero = c.Xmm("cZero"); var cMul255A = c.Xmm("cMul255A"); var cMul255M = c.Xmm("cMul255M"); var x0 = c.Xmm("x0"); var x1 = c.Xmm("x1"); var y0 = c.Xmm("y0"); var a0 = c.Xmm("a0"); var a1 = c.Xmm("a1"); var smallLoop = c.Label(); var smallEnd = c.Label(); var largeLoop = c.Label(); var largeEnd = c.Label(); var data = c.Label(); c.SetArgument(0, dst); c.SetArgument(1, src); c.SetArgument(2, i); c.Allocate(dst); c.Allocate(src); c.Allocate(i); // How many pixels have to be processed to make the loop aligned. c.Lea(t, Memory.Ptr(data)); c.Xor(j, j); c.Xorps(cZero, cZero); c.Sub(j, dst); c.Movaps(cMul255A, Memory.Ptr(t, 0)); c.And(j, 15); c.Movaps(cMul255M, Memory.Ptr(t, 16)); c.Shr(j, 2); c.Jz(smallEnd); // j = min(i, j). c.Cmp(j, i); c.Cmovg(j, i); // i -= j. c.Sub(i, j); // Small loop. c.Bind(smallLoop); c.Pcmpeqb(a0, a0); c.Movd(y0, Memory.Ptr(src)); c.Pxor(a0, y0); c.Movd(x0, Memory.Ptr(dst)); c.Psrlw(a0, 8); c.Punpcklbw(x0, cZero); c.Pshuflw(a0, a0, AsmJit.Common.Utils.Shuffle(1, 1, 1, 1)); c.Punpcklbw(y0, cZero); c.Pmullw(x0, a0); c.Paddsw(x0, cMul255A); c.Pmulhuw(x0, cMul255M); c.Paddw(x0, y0); c.Packuswb(x0, x0); c.Movd(Memory.Ptr(dst), x0); c.Add(dst, 4); c.Add(src, 4); c.Dec(j); c.Jnz(smallLoop); // Second section, prepare for an aligned loop. c.Bind(smallEnd); c.Test(i, i); c.Mov(j, i); c.Jz(c.Exit); c.And(j, 3); c.Shr(i, 2); c.Jz(largeEnd); // Aligned loop. c.Bind(largeLoop); c.Movups(y0, Memory.Ptr(src)); c.Pcmpeqb(a0, a0); c.Movaps(x0, Memory.Ptr(dst)); c.Xorps(a0, y0); c.Movaps(x1, x0); c.Psrlw(a0, 8); c.Punpcklbw(x0, cZero); c.Movaps(a1, a0); c.Punpcklwd(a0, a0); c.Punpckhbw(x1, cZero); c.Punpckhwd(a1, a1); c.Pshufd(a0, a0, AsmJit.Common.Utils.Shuffle(3, 3, 1, 1)); c.Pshufd(a1, a1, AsmJit.Common.Utils.Shuffle(3, 3, 1, 1)); c.Pmullw(x0, a0); c.Pmullw(x1, a1); c.Paddsw(x0, cMul255A); c.Paddsw(x1, cMul255A); c.Pmulhuw(x0, cMul255M); c.Pmulhuw(x1, cMul255M); c.Add(src, 16); c.Packuswb(x0, x1); c.Paddw(x0, y0); c.Movaps(Memory.Ptr(dst), x0); c.Add(dst, 16); c.Dec(i); c.Jnz(largeLoop); c.Bind(largeEnd); c.Test(j, j); c.Jnz(smallLoop); // Data c.Data(data, 16, Data.Of(0x0080008000800080, 0x0080008000800080), Data.Of(0x0101010101010101, 0x0080008000800080)); }