Write an FP32 multiplier in Python, input two 32-bit binary strings, output a 32-bit…
- User
Write an FP32 multiplier in Python, input two 32-bit binary strings, output a 32-bit binary string, do not use Python's built-in floating-point multiplication, and require support for non-standard number calculations and up overflow.
- Assistant
Certainly! Below is a Python implementation of an IEEE 754 single-precision (FP32) floating-point multiplier that:
- Takes two 32-bit binary strings as input.
- Outputs a 32-bit binary string.
- Does not use Python's built-in floating-point multiplication.
- Supports special cases like NaN, infinity, zero, and handles overflow.
- Handles subnormal numbers and rounding (to nearest even).
- Handles sign, exponent, and mantissa manually.
Explanation
IEEE 754 single-precision format (32 bits):
- 1 bit sign (S)
- 8 bits exponent (E) with bias 127
- 23 bits fraction (F) (mantissa without the implicit leading 1 for normalized numbers)
Value = (-1)^S × 1.F × 2^(E-127) for normalized numbers
Value = (-1)^S × 0.F × 2^(1-127) for subnormal numbers
Steps for multiplication:
- Parse inputs into sign, exponent, mantissa.
- Handle special cases (zero, infinity, NaN).
- Calculate sign of result = XOR of input signs.
- Add exponents and subtract bias.
- Multiply mantissas (including implicit leading 1 for normalized).
- Normalize the result mantissa.
- Handle overflow/underflow of exponent.
- Round mantissa to 23 bits.
- Construct the final 32-bit binary string.
Code
def fp32_multiply(bin1: str, bin2: str) -> str: assert len(bin1) == 32 and len(bin2) == 32, "Inputs must be 32-bit binary strings" def to_int(b): return int(b, 2) def is_nan(exp, frac): return exp == 0xFF and frac != 0 def is_inf(exp, frac): return exp == 0xFF and frac == 0 def is_zero(exp, frac): return exp == 0 and frac == 0 # Parse inputs s1 = int(bin1[0], 2) e1 = to_int(bin1[1:9]) f1 = to_int(bin1[9:]) s2 = int(bin2[0], 2) e2 = to_int(bin2[1:9]) f2 = to_int(bin2[9:]) # Handle special cases # NaN propagation if is_nan(e1, f1): return '0' + '1'*8 + '1' + '0'*22 # Quiet NaN if is_nan(e2, f2): return '0' + '1'*8 + '1' + '0'*22 # Inf * 0 = NaN if (is_inf(e1, f1) and is_zero(e2, f2)) or (is_inf(e2, f2) and is_zero(e1, f1)): return '0' + '1'*8 + '1' + '0'*22 # Quiet NaN # Inf * anything else = Inf if is_inf(e1, f1) or is_inf(e2, f2): sign = s1 ^ s2 return str(sign) + '1'*8 + '0'*23 # Zero * anything = zero if is_zero(e1, f1) or is_zero(e2, f2): sign = s1 ^ s2 return str(sign) + '0'*31 # Calculate sign sign = s1 ^ s2 # Normalize mantissas: add implicit leading 1 for normalized numbers # For subnormal numbers, leading bit is 0 if e1 == 0: mant1 = f1 exp1 = 1 - 127 # subnormal exponent else: mant1 = (1 << 23) | f1 exp1 = e1 - 127 if e2 == 0: mant2 = f2 exp2 = 1 - 127 else: mant2 = (1 << 23) | f2 exp2 = e2 - 127 # Multiply mantissas: 24 bits * 24 bits = 48 bits product = mant1 * mant2 # up to 48 bits # Add exponents exp = exp1 + exp2 # Normalize product # product is 48 bits, highest possible bit is bit 47 # The product mantissa should be normalized to 24 bits with leading 1 at bit 23 # Check if bit 47 is set (means product >= 2^47) if (product & (1 << 47)) != 0: # Shift right by 24 to get 24 bits mantissa (including leading 1) # exponent increases by 1 mantissa = product >> 24 exp += 1 else: # Shift right by 23 to get 24 bits mantissa mantissa = product >> 23 # mantissa now has 24 bits: leading 1 + 23 fraction bits # Rounding: round to nearest even # Check the bit just below the 23 fraction bits (bit 0 of the shifted out part) # and bits below it for tie-breaking # We have already shifted product, so let's get the bits we discarded for rounding # Calculate remainder bits for rounding # If we shifted by 24, remainder bits are product & ((1 << 24) - 1) # If shifted by 23, remainder bits are product & ((1 << 23) - 1) if (product & (1 << 47)) != 0: remainder = product & ((1 << 24) - 1) round_bit = (remainder >> 23) & 1 sticky_bits = remainder & ((1 << 23) - 1) else: remainder = product & ((1 << 23) - 1) round_bit = (remainder >> 22) & 1 sticky_bits = remainder & ((1 << 22) - 1) # Round to nearest even if round_bit == 1 and (sticky_bits > 0 or (mantissa & 1) == 1): mantissa += 1 # Check if mantissa overflowed (24 bits) if mantissa == (1 << 24): mantissa >>= 1 exp += 1 # Remove implicit leading 1 for normalized numbers # If exponent > -127, number is normalized if exp > -127: # Normalized number e = exp + 127 if e >= 255: # Overflow to infinity return str(sign) + '1'*8 + '0'*23 mantissa &= ~(1 << 23) # clear implicit leading 1 f = mantissa else: # Subnormal number or underflow to zero # Shift mantissa right by (-127 - exp + 1) to denormalize shift = (-127 - exp + 1) if shift < 24: f = mantissa >> shift e = 0 else: # Underflow to zero f = 0 e = 0 # Compose final binary string result = (sign << 31) | (e << 23) | f return f"{result:032b}" # Example usage: a = "01000000101000000000000000000000" # 5.0 b = "01000000010000000000000000000000" # 3.0 print(fp32_multiply(a, b)) # Should be close to 15.0 in FP32 binary
How to test
- Convert decimal floats to FP32 binary strings (you can use online converters or Python's
structmodule). - Multiply using this function.
- Convert the output binary string back to float to verify correctness.
If you want, I can also provide helper functions to convert between float and FP32 binary strings for easier testing. Would you like that?
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.