def SimpleTest(): """ Just make sure things are working... """ from corepy.arch.x86.platform import Processor, InstructionStream code = InstructionStream() proc = Processor() # Without active code a = SignedWord(11, code) b = SignedWord(31, reg=code.acquire_register()) c = SignedWord(reg=code.gp_return) byte_mask = Bits(0xFF, code) code.add(x86.addi(code.gp_return, 0, 31)) # c.v = a + SignedWord.cast(b & byte_mask) + 12 c.v = a + (byte_mask & b) + 12 if True: r = proc.execute(code) assert (r == (42 + 12)) # With active code code.reset() x86.set_active_code(code) a = SignedWord(11) b = SignedWord(31) c = SignedWord(reg=code.gp_return) byte_mask = Bits(0xFF) c.v = a + (b & byte_mask) x86.set_active_code(None) r = proc.execute(code) # code.print_code() assert (r == 42) return
def SimpleTest(): """ Just make sure things are working... """ from corepy.arch.x86.platform import Processor, InstructionStream code = InstructionStream() proc = Processor() # Without active code a = SignedWord(11, code) b = SignedWord(31, reg = code.acquire_register()) c = SignedWord(reg = code.gp_return) byte_mask = Bits(0xFF, code) code.add(x86.addi(code.gp_return, 0, 31)) # c.v = a + SignedWord.cast(b & byte_mask) + 12 c.v = a + (byte_mask & b) + 12 if True: r = proc.execute(code) assert(r == (42 + 12)) # With active code code.reset() x86.set_active_code(code) a = SignedWord(11) b = SignedWord(31) c = SignedWord(reg = code.gp_return) byte_mask = Bits(0xFF) c.v = a + (b & byte_mask) x86.set_active_code(None) r = proc.execute(code) # code.print_code() assert(r == 42) return
def TestBits(): from corepy.arch.x86.platform import Processor, InstructionStream code = InstructionStream() proc = Processor() x86.set_active_code(code) b = Bits(0xB0) e = Bits(0xE0000) a = Bits(0xCA) f = Bits(0x5) x = Bits(0, reg=code.gp_return) mask = Bits(0xF) byte = Bits(8) # 8 bits halfbyte = Bits(4) f.v = (a & mask) ^ f x.v = (b << byte) | (e >> byte) | ((a & mask) << halfbyte) | (f | mask) r = proc.execute(code) assert (r == 0xBEAF) return
def TestBits(): from corepy.arch.x86.platform import Processor, InstructionStream code = InstructionStream() proc = Processor() x86.set_active_code(code) b = Bits(0xB0) e = Bits(0xE0000) a = Bits(0xCA) f = Bits(0x5) x = Bits(0, reg = code.gp_return) mask = Bits(0xF) byte = Bits(8) # 8 bits halfbyte = Bits(4) f.v = (a & mask) ^ f x.v = (b << byte) | (e >> byte) | ((a & mask) << halfbyte) | (f | mask) r = proc.execute(code) assert(r == 0xBEAF) return
import corepy.arch.x86_64.lib.util as util import time ITERS = 1000000 THREADS = 4 data = extarray.extarray('l', 1) dbi = data.buffer_info() # This first case is intentionally wrong to show what happens w/o locking. data[0] = 0 prgm = env.Program() code = prgm.get_stream() x86.set_active_code(code) x86.mov(rax, 1) x86.mov(rcx, ITERS) x86.mov(rdi, dbi[0]) lbl_loop = prgm.get_unique_label("loop") code.add(lbl_loop) x86.add(MemRef(rdi), rax) x86.dec(rcx) x86.jnz(lbl_loop) prgm += code prgm.print_code(hex = True)
def _set_active_code(self, code): return x86.set_active_code(code)
def TestFloatingPoint(float_type): from corepy.arch.x86.platform import Processor, InstructionStream code = InstructionStream() proc = Processor() x86.set_active_code(code) x = float_type(1.0) y = float_type(2.0) z = float_type(3.0) a = float_type() b = float_type() c = float_type() d = float_type() # Create some data data = array.array('d', (1.0, 2.0, 3.0, 4.0)) addr = data.buffer_info()[0] # Load from addr a.load(addr) # Load from addr with idx in register offset = Bits(8) b.load(data.buffer_info()[0], offset) # Load from addr with constant idx c.load(data.buffer_info()[0], 8 * 2) # Load from addr with addr as a register reg_addr = Bits(addr) d.load(reg_addr) r = float_type(reg=code.fp_return) r.v = (x + y) / y r.v = fmadd(a, y, z + z) + fnmadd(a, y, z + z) + fmsub(x, y, z) + fnmsub( x, y, z) x.v = -x r.v = r + x - x + a + b - c + d - d # Store from addr a.v = 11.0 a.store(addr) # Store from addr with idx in register offset = Bits(8) b.v = 12.0 b.store(data.buffer_info()[0], offset) # Store from addr with constant idx c.v = 13.0 c.store(data.buffer_info()[0], 8 * 2) # Store from addr with addr as a register d.v = 14.0 reg_addr = UnsignedWord(addr) reg_addr.v = reg_addr + 8 * 3 d.store(reg_addr) r = proc.execute(code, mode='fp') assert (r == 0.0) assert (data[0] == 11.0) assert (data[1] == 12.0) assert (data[2] == 13.0) assert (data[3] == 14.0) return
import corepy.lib.extarray as extarray import corepy.arch.x86_64.lib.util as util import time ITERS = 1000000 THREADS = 4 data = extarray.extarray('l', 1) dbi = data.buffer_info() # This first case is intentionally wrong to show what happens w/o locking. data[0] = 0 prgm = env.Program() code = prgm.get_stream() x86.set_active_code(code) x86.mov(rax, 1) x86.mov(rcx, ITERS) x86.mov(rdi, dbi[0]) lbl_loop = prgm.get_unique_label("loop") code.add(lbl_loop) x86.add(MemRef(rdi), rax) x86.dec(rcx) x86.jnz(lbl_loop) prgm += code prgm.print_code(hex=True)
def _set_active_code(self, code): return x86.set_active_code(code)
def TestFloatingPoint(float_type): from corepy.arch.x86.platform import Processor, InstructionStream code = InstructionStream() proc = Processor() x86.set_active_code(code) x = float_type(1.0) y = float_type(2.0) z = float_type(3.0) a = float_type() b = float_type() c = float_type() d = float_type() # Create some data data = array.array('d', (1.0, 2.0, 3.0, 4.0)) addr = data.buffer_info()[0] # Load from addr a.load(addr) # Load from addr with idx in register offset = Bits(8) b.load(data.buffer_info()[0], offset) # Load from addr with constant idx c.load(data.buffer_info()[0], 8*2) # Load from addr with addr as a register reg_addr = Bits(addr) d.load(reg_addr) r = float_type(reg = code.fp_return) r.v = (x + y) / y r.v = fmadd(a, y, z + z) + fnmadd(a, y, z + z) + fmsub(x, y, z) + fnmsub(x, y, z) x.v = -x r.v = r + x - x + a + b - c + d - d # Store from addr a.v = 11.0 a.store(addr) # Store from addr with idx in register offset = Bits(8) b.v = 12.0 b.store(data.buffer_info()[0], offset) # Store from addr with constant idx c.v = 13.0 c.store(data.buffer_info()[0], 8*2) # Store from addr with addr as a register d.v = 14.0 reg_addr = UnsignedWord(addr) reg_addr.v = reg_addr + 8 * 3 d.store(reg_addr) r = proc.execute(code, mode='fp') assert(r == 0.0) assert(data[0] == 11.0) assert(data[1] == 12.0) assert(data[2] == 13.0) assert(data[3] == 14.0) return