Exemplo n.º 1
0
def wrap_u64(vt, val):
    if vt == ValTypeI32:
        return int32(val)
    elif vt == ValTypeI64:
        return int64(val)
    elif vt == ValTypeF32:
        try:
            cov_val = struct.unpack('>f', struct.pack('>l', int64(val)))[0]
        except struct.error:
            cov_val = struct.unpack('>f', struct.pack('>L', int64(val)))[0]
        if math.isnan(cov_val):
            return float32(val)
        else:
            return float32(cov_val)
    elif vt == ValTypeF64:
        try:
            cov_val = struct.unpack('>d', struct.pack('>q', int64(val)))[0]
        except struct.error:
            cov_val = struct.unpack('>d', struct.pack('>Q', int64(val)))[0]
        if math.isnan(cov_val):
            return float64(val)
        else:
            return float64(cov_val)
    else:
        raise Exception("unreachable")
Exemplo n.º 2
0
 def test_nan(self):
     self.assertEqual(uint32(0x7fc00000), float32(str(math.nan)))
     self.assertEqual(uint32(0xffc00000), float32('-nan'))
     self.assertEqual(uint32(0x7f800000), float32(str(math.inf)))
     self.assertEqual(uint32(0xff800000), float32('-inf'))
     self.assertEqual(uint64(0x7ff8000000000001), float64(str(math.nan)))
     self.assertEqual(uint64(0xfff8000000000001), float64('-nan'))
     self.assertEqual(uint64(0x7ff0000000000000), float64(str(math.inf)))
     self.assertEqual(uint64(0xfff0000000000000), float64('-inf'))
Exemplo n.º 3
0
def parse_nan32(s: str):
    s = s.replace("_", "")
    f = float32(math.nan)
    if s[0] == '-':
        f = -f
        s = s[1:]
    elif s[0] == '+':
        s = s[1:]

    if s.startswith("nan:0x"):
        payload = int(s[6:], 16)
        bits = struct.unpack('>l', struct.pack('>f', f))[0] & 0xFFBFFFFF
        try:
            f = float32(
                struct.unpack('>f',
                              struct.pack('>l',
                                          int64(bits | uint32(payload))))[0])
        except struct.error:
            f = float32(math.nan)
    return f
Exemplo n.º 4
0
 def visitExpected(self, ctx: WASTParser.ExpectedContext):
     if ctx.nan is not None:
         instr = new_instruction(ctx.op.text)
         opcode = instr.opcode
         if opcode == F32Const:
             instr.args = float32('nan')
         elif opcode == F64Const:
             instr.args = float64('nan')
         else:
             raise Exception("TODO:NAN")
         return instr
     return ctx.constInstr().accept(self)
Exemplo n.º 5
0
def parse_float(s: str, bit_size):
    s = s.replace("_", "")
    if s.find("0x") >= 0 > s.find('P') and s.find('p') < 0:
        s += "p0"
        f = float.fromhex(s)
    elif s.find('P') > 0 or s.find('p') > 0:
        f = float.fromhex(s)
    else:
        f = 0.0 if float(s) == 0 else float(s)
    if bit_size == 32:
        return float32(f)
    else:
        return float64(f)
Exemplo n.º 6
0
def get_consts(expr):
    if len(expr) == 0:
        vals = []
    else:
        vals = [None] * len(expr)
    for i, instr in enumerate(expr):
        opcode = instr.opcode
        if opcode == I32Const:
            vals[i] = int32(instr.args)
        elif opcode == I64Const:
            vals[i] = int64(instr.args)
        elif opcode == F32Const:
            vals[i] = float32(instr.args)
        elif opcode == F64Const:
            vals[i] = float64(instr.args)
        else:
            raise Exception("TODO")
    return vals
Exemplo n.º 7
0
    def test_operand_stack(self):
        stack = OperandStack()
        stack.push_bool(True)
        stack.push_bool(False)
        stack.push_u32(1)
        stack.push_s32(-2)
        stack.push_u64(3)
        stack.push_s64(-4)
        stack.push_f32(5.5)
        stack.push_f64(6.5)

        self.assertEqual(6.5, stack.pop_f64())
        self.assertEqual(float32(5.5), stack.pop_f32())
        self.assertEqual(int64(-4), stack.pop_s64())
        self.assertEqual(uint64(3), stack.pop_u64())
        self.assertEqual(int32(-2), stack.pop_s32())
        self.assertEqual(uint32(1), stack.pop_u32())
        self.assertEqual(False, stack.pop_bool())
        self.assertEqual(True, stack.pop_bool())
        self.assertEqual(0, len(stack.slots))
Exemplo n.º 8
0
def f32_demote_f64(vm, _):
    vm.push_f32(float32(vm.pop_f64()))
Exemplo n.º 9
0
def f32_convert_i64u(vm, _):
    vm.push_f32(float32(vm.pop_u64()))
Exemplo n.º 10
0
def f32_convert_i64s(vm, _):
    vm.push_f32(float32(vm.pop_s64()))
Exemplo n.º 11
0
def f32_convert_i32u(vm, _):
    vm.push_f32(float32(vm.pop_u32()))
Exemplo n.º 12
0
def f32_convert_i32s(vm, _):
    vm.push_f32(float32(vm.pop_s32()))
Exemplo n.º 13
0
def f32_const(vm, args):
    vm.push_f32(float32(args))
Exemplo n.º 14
0
 def pop_f32(self):
     val = self.pop_numeric()
     val = struct.unpack('>f', struct.pack('>l', int64(val)))[0]
     return float32(val)