"""Bit-exact IEEE 754 single precision, the way RISC-V's F extension does it. The reference for rtl/rv32_fpu.v and the arithmetic of tools/rv32emu.py. Values are 32-bit patterns (ints); every operation returns (bits, flags), flags being fflags: NV 16, DZ 8, OF 4, UF 2, NX 1. Rounding modes are RISC-V's: 0 RNE, 1 RTZ, 2 RDN, 3 RUP, 4 RMM. Tininess is detected after rounding; NaN results are the canonical 7FC00000. Arithmetic is exact (Fraction), then rounded once. """ from fractions import Fraction NV, DZ, OF, UF, NX = 16, 8, 4, 2, 1 RNE, RTZ, RDN, RUP, RMM = 0, 1, 2, 3, 4 QNAN = 0x7FC00000 M32 = 0xFFFFFFFF def sign(x): return x >> 31 def expo(x): return (x >> 23) & 0xFF def frac(x): return x & 0x7FFFFF def is_nan(x): return expo(x) == 255 and frac(x) != 0 def is_snan(x): return is_nan(x) and not (x >> 22) & 1 def is_inf(x): return (x & 0x7FFFFFFF) == 0x7F800000 def is_zero(x): return (x & 0x7FFFFFFF) == 0 def value(x): """The exact value of a finite pattern, without its sign.""" e, f = expo(x), frac(x) if e == 0: return Fraction(f, 1 << 149) m = f | 0x800000 k = e - 150 return Fraction(m << k) if k >= 0 else Fraction(m, 1 << -k) def _round_int(q, s, rm, neg): """Round q (a non-negative Fraction) to an integer; s marks a value a hair above q (an inexact square root). Returns (n, inexact).""" n = q.numerator // q.denominator rem = q - n if rem == 0 and not s: return n, False half = Fraction(1, 2) if rm == RNE: up = rem > half or (rem == half and (s or n & 1)) elif rm == RTZ: up = False elif rm == RDN: up = neg elif rm == RUP: up = not neg else: # RMM up = rem >= half return n + up, True def round_f32(neg, x, rm, s=False): """Round the exact value (-1)^neg * x (x a non-negative Fraction; s: the true value is a hair above x) to single precision. Returns (bits, flags).""" sb = int(neg) << 31 if x == 0 and not s: return sb, 0 # exponent e with 2^e <= x < 2^(e+1) e = x.numerator.bit_length() - x.denominator.bit_length() if Fraction(2) ** e > x: e -= 1 # rounding with an unbounded exponent (for tininess after rounding) n_u, _ = _round_int(x * Fraction(2) ** (23 - e), s, rm, neg) tiny = n_u * Fraction(2) ** (e - 23) < Fraction(1, 1 << 126) if e < -126: n, inex = _round_int(x * Fraction(1 << 149), s, rm, neg) flags = (NX | (UF if tiny else 0)) if inex else 0 if n >= 1 << 23: # rounded up to the smallest normal return sb | (1 << 23) | (n - (1 << 23)), flags return sb | n, flags n, inex = _round_int(x * Fraction(2) ** (23 - e), s, rm, neg) if n == 1 << 24: n >>= 1 e += 1 if e > 127: big = rm in (RNE, RMM) or (rm == RDN and neg) or (rm == RUP and not neg) return sb | (0x7F800000 if big else 0x7F7FFFFF), OF | NX return sb | ((e + 127) << 23) | (n - (1 << 23)), NX if inex else 0 def fma(a, b, c, rm, negp=False, negc=False, use_c=True): """(-1)^negp * a*b + (-1)^negc * c; use_c False is a plain multiply.""" sp = sign(a) ^ sign(b) ^ negp sc = sign(c) ^ negc nans = is_nan(a) or is_nan(b) or (use_c and is_nan(c)) inv = is_snan(a) or is_snan(b) or (use_c and is_snan(c)) inv |= (is_inf(a) and is_zero(b)) or (is_zero(a) and is_inf(b)) pinf = (is_inf(a) or is_inf(b)) and not nans if pinf and use_c and is_inf(c) and sp != sc: inv = True if inv or nans: return QNAN, NV if inv else 0 if pinf: return (sp << 31) | 0x7F800000, 0 if use_c and is_inf(c): return (sc << 31) | 0x7F800000, 0 p = value(a) * value(b) * (-1 if sp else 1) if not use_c: if p == 0: return sp << 31, 0 return round_f32(p < 0, abs(p), rm) q = value(c) * (-1 if sc else 1) r = p + q if r == 0: if p == 0 and q == 0 and sp == sc: return sp << 31, 0 return (1 << 31 if rm == RDN else 0), 0 return round_f32(r < 0, abs(r), rm) ONE = 0x3F800000 def add(a, b, rm): return fma(a, ONE, b, rm) def sub(a, b, rm): return fma(a, ONE, b, rm, negc=True) def mul(a, b, rm): return fma(a, b, 0, rm, use_c=False) def div(a, b, rm): s = sign(a) ^ sign(b) if is_nan(a) or is_nan(b): return QNAN, NV if is_snan(a) or is_snan(b) else 0 if (is_inf(a) and is_inf(b)) or (is_zero(a) and is_zero(b)): return QNAN, NV if is_inf(a): return (s << 31) | 0x7F800000, 0 if is_inf(b): return s << 31, 0 if is_zero(b): return (s << 31) | 0x7F800000, DZ if is_zero(a): return s << 31, 0 return round_f32(s, value(a) / value(b), rm) def isqrt(n): if n == 0: return 0 x = 1 << ((n.bit_length() + 1) // 2) while True: y = (x + n // x) // 2 if y >= x: return x x = y def sqrt(a, rm): if is_nan(a): return QNAN, NV if is_snan(a) else 0 if is_zero(a): return a, 0 if sign(a): return QNAN, NV if is_inf(a): return a, 0 n = value(a) * Fraction(4) ** 150 # an integer n = n.numerator r = isqrt(n) return round_f32(0, Fraction(r, 1 << 150), rm, s=r * r != n) def sgnj(a, b, f3): s = [sign(b), sign(b) ^ 1, sign(a) ^ sign(b)][f3] return (s << 31) | (a & 0x7FFFFFFF), 0 def _lt(a, b): if is_zero(a) and is_zero(b): return False if sign(a) != sign(b): return sign(a) == 1 if sign(a): return (a & 0x7FFFFFFF) > (b & 0x7FFFFFFF) return (a & 0x7FFFFFFF) < (b & 0x7FFFFFFF) def _eq(a, b): return a == b or (is_zero(a) and is_zero(b)) def minmax(a, b, is_max): fl = NV if is_snan(a) or is_snan(b) else 0 if is_nan(a) and is_nan(b): return QNAN, fl if is_nan(a): return b, fl if is_nan(b): return a, fl if is_zero(a) and is_zero(b): if is_max: return (a if not sign(a) else b), fl return (a if sign(a) else b), fl if is_max: return (b if _lt(a, b) else a), fl return (a if _lt(a, b) else b), fl def cmp(a, b, f3): """f3: 0 FLE, 1 FLT, 2 FEQ. Returns (0 or 1, flags).""" if is_nan(a) or is_nan(b): if f3 == 2: return 0, NV if is_snan(a) or is_snan(b) else 0 return 0, NV if f3 == 2: return int(_eq(a, b)), 0 if f3 == 1: return int(_lt(a, b)), 0 return int(_lt(a, b) or _eq(a, b)), 0 def fclass(a): s, e, f = sign(a), expo(a), frac(a) if e == 255: if f == 0: return 1 << 0 if s else 1 << 7 return 1 << 9 if f >> 22 else 1 << 8 if e == 0: if f == 0: return 1 << 3 if s else 1 << 4 return 1 << 2 if s else 1 << 5 return 1 << 1 if s else 1 << 6 def cvt_w(a, rm, unsigned): """float -> int32 / uint32: (bits, flags).""" if is_nan(a): return (M32 if unsigned else 0x7FFFFFFF), NV if is_inf(a): if unsigned: return (0 if sign(a) else M32), NV return (0x80000000 if sign(a) else 0x7FFFFFFF), NV neg = sign(a) n, inex = _round_int(value(a), False, rm, neg) v = -n if neg else n lo, hi = (0, M32) if unsigned else (-(1 << 31), (1 << 31) - 1) if v < lo or v > hi: if unsigned: return (0 if neg else M32), NV return (0x80000000 if neg else 0x7FFFFFFF), NV return v & M32, NX if inex else 0 def cvt_s(x, rm, unsigned): """int32 / uint32 (as a 32-bit pattern) -> float.""" v = x if unsigned or not x >> 31 else x - (1 << 32) if v == 0: return 0, 0 return round_f32(v < 0, Fraction(abs(v)), rm) def execute(ins, rm, a, b, c): """Run one F instruction (the 32-bit encoding) on operand patterns a, b, c (a is the integer register for FMV.W.X and FCVT.S.W[U]); rm is the resolved rounding mode. Returns (result, flags, to_int).""" opc = ins & 0x7F f3 = (ins >> 12) & 7 f5 = ins >> 27 r2 = (ins >> 20) & 31 if opc in (0x43, 0x47, 0x4B, 0x4F): r, fl = fma(a, b, c, rm, negp=bool(opc & 8), negc=bool(opc & 4)) return r, fl, False if f5 == 0: return (*add(a, b, rm), False) if f5 == 1: return (*sub(a, b, rm), False) if f5 == 2: return (*mul(a, b, rm), False) if f5 == 3: return (*div(a, b, rm), False) if f5 == 0x0B: return (*sqrt(a, rm), False) if f5 == 0x04: return (*sgnj(a, b, f3), False) if f5 == 0x05: return (*minmax(a, b, f3 == 1), False) if f5 == 0x18: return (*cvt_w(a, rm, r2 & 1), True) if f5 == 0x1C: return (fclass(a) if f3 == 1 else a, 0, True) if f5 == 0x14: return (*cmp(a, b, f3), True) if f5 == 0x1A: return (*cvt_s(a, rm, r2 & 1), False) if f5 == 0x1E: return a, 0, False raise ValueError("not an F instruction: %08x" % ins)