1

This is my code for printing just the mantissa of a floating point. The value stored in $t1 is the value 0xBEDCFFFF, which has a mantissa of 10111001111111111111111. My code prints this without the one in the beginning. How do I write the clause to input a hidden bit of one or zero when it is necessary?

li $t4, 1                   # Reset counters
li $t3, 23                  
mantloop:                   # Loop to mask and print each bit
    ble $t3, $t4, finish            # escape clause
    subi $t3, $t3, 1            # subtract from the counter 
    srl $t2, $t2, 1             # shifting mask
    and $t0, $t1, $t2           # ANDing registers
    bnez $t0, printOneee            # Print one or zero
printZerooo:
    li $v0, 1
    li $a0, 0
    syscall
    j mantloop              # loop reset
printOneee:
    li  $v0, 1
    li $a0, 1
    syscall
    j mantloop              # loop reset

finish:                     # method complete
James
  • 13
  • 5

1 Answers1

2

It's not clear what you mean by "account for". If you want to print the binary representation of an IEEE754 float, then the implied bit isn't part of it.

If you want the actual value represented by the mantissa, you need to know the exponent.

The implicit leading bit of the mantissa is 0 for denormals (when the exponent field is zero). Otherwise the implied bit is 1. https://en.wikipedia.org/wiki/Single-precision_floating-point_format. This includes +- 0.0, which is represented by exponent=0 mantissa=0.

If you don't have any denormals, then the leading bit is always 1. (But 0.0 counts as subnormal for this).

https://www.h-schmidt.net/FloatConverter/IEEE754.html is useful: it shows you the bits and the exponent / mantissa separately, for any input bit-pattern (hex) or decimal value like 1.234.


+- Infinity is represented by exponent=all-ones, mantissa=0. It's infinity, and there isn't really a meaning for the implied bit.

NaN is represented by exponent=all-ones, mantissa=non-zero (sign bit = either). The mantissa is the "payload" of the NaN, and is arbitrary. It only makes sense to talk about the bits that are actually there, not an implied bit.

Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
  • 1
    IEEE 754 preferred terms: “mantissa” → “significand”, “denormal” → “subnormal”. – Eric Postpischil Jun 09 '18 at 18:08
  • @EricPostpischil: There's even a convincing argument that "mantissa" is not an appropriate term: https://en.wikipedia.org/wiki/Significand#Use_of_%22mantissa%22 points out that "mantissa" properly refers to the fractional part of a logarithm, but a significand is linear and only the exponent is logarithmic. In this answer I decided not to tilt at that windmill because at least "mantissa" doesn't create any confusion. As far as "subnormal", I wasn't aware of that. Is there any argument for "denormal" actually being wrong? Denormal is widely used, e.g. x86's DAZ (denormals are zero) flag. – Peter Cordes Jun 09 '18 at 18:17
  • @PeterCordes: I do not have any recollection of why “subnormal” is preferred, but I will hazard a guess that any number can be denormalized by scaling it to something other than canonical form, and this occurs naturally in the computation of intermediate results (such as a subtraction with cancellation before the result is normalized), so “subnormal” specifically refers to numbers below the range where they can be represented normally. – Eric Postpischil Jun 09 '18 at 19:17
  • 1
    Curious condition of "... but mantissa is non-zero". `printf("%d\n", isnormal(0.0));` reports 0 on my platform. Perhaps 0.0 is a denormal/subnormal and that condition is not needed. IAC, 0.0 does not have an implied 1 as the 3rd paragraph could be read. – chux - Reinstate Monica Jun 12 '18 at 13:56
  • 1
    @chux: great point, zero is a subnormal. I guess I was mixed up because setting FTZ / DAZ still produces 0.0, and we don't typically think of it as weird. I guess it's the only non-weird subnormal. :P Updated, and added +-Inf (where mantissa=0 distinguishes from NaN) – Peter Cordes Jun 12 '18 at 16:33