Recently, I've been working on a RISC-V software implementation as a personal project. I have most of the basics working, and right now I'm adding support for floating point operations. Because I wanted the code to be fully portable, and I didn't want to dig into inline assembly to directly control rounding modes, exception flags, and so on, I decided to make a full software implementation of floats (plus, hey, it's great practice too!)
Making everything from scratch, with minimal guides (and, of course, no AI help), by just reading the specification and spending lots of time thinking about it, helped me gain a much better understanding of how floats work. I think I now have much better intuition for the exponent and mantissa, too. Also, it simply feels great to suddenly come to a realization while going downstairs into the kitchen - you just don't get that when you use an existing library or have the thinking done for you by someone (or something) else.
Anyway, I'm writing this because I just realized that what I previously wrote using a whole lot of special-cased if blocks ends up following quite naturally thanks to the structure of floats as a simple result of addition overflow.
First, a quick refresher: a floating point number consists of three parts: the sign, the exponent, and the mantissa (aka significand). The sign is always one bit, and for a 32-bit float, the exponent takes up 8 bits, and the mantissa 23 bits. float.exposed demonstrates it well. To interpret this number, we begin by taking the mantissa, and prepending an implicit 1 bit to the left. Imagine a fractional point between the implicit bit and the rest of the mantissa - that's our starting value (in base 2). Next, we apply the exponent: a 32-bit float is set to have a bias of 127, so we take the exponent, subtract that amount, and raise 2 to that power. Finally, multiply the implicit-bit-extended mantissa, the exponent, apply the sign, and that's your number.
Floating point operations have several defined rounding modes. For example, when you multiply two floats together, chances are that some precision will be lost. However, it is important to note that this precision loss happens only as a result of having to store the result back into a 32-bit representation! The two input mantissas are multiplied together, which produces a much wider number, and this has to be truncated at the end - but, during the process, you can still round up or down based on the bits that will get lost.
The standard defines five rounding modes, but they all boil down to one of two actions: round up by adding 1 to the resulting mantissa; or round down by simply truncating bits. However, there's one small catch: if you were to round up a mantissa that consists entirely of 1's, the value would overflow, and you'll have to add 1 to the exponent as well. In my code, at first, I basically just special-cased this: if we need to round up, add 1 to the mantissa; if the mantissa overflows, add 1 to the exponent. I kept the values for the mantissa and exponent separate, because it was more convenient to work with them that way. However, tonight I realized that this behavior of adding 1 to the exponent upon overflow follows naturally from how floats are arranged: because the exponent comes after the mantissa, and because the highest 1 of the mantissa is implicit, an overflow caused by rounding is naturally propagated into the exponent.
It's the little things like this that feel great when learning something new. I might be stuck for hours at a time, trying to wrap my head around a convoluted spec, trying to visualize the abstract representation of a floating point number in base 2, but when a realization then hits, it feels amazing.
I might do a more proper writeup about what I learned from implementing RISC-V at a later point. For now though, I'll continue working on the floating point ops, and then probably tackle more extensions.