• Great article. You really start to appreciate floating point when you have to squeeze some arbitrary level of performance out of an underpowered (say embedded) CPU and you decide to use fixed point. Suddenly all those nasty little edge cases that the floating point library would have handled silently, reliably and hopefully correctly for you need to be dealt with.

    Just keeping track of the shifts during a chain of multiplications and additions can really ruin your day. And the good code will look exactly the same as the bad code. I'm doing something like that right now and have moved from doubles to fixed point 64 bit ints (32.32), it works, but it took me much longer than I thought it would (phase angle estimator for SDR output).

  • Well, that escalated quickly. From skimming the first few screenfuls of the page, I thought it was going to be about a beginner programmer's first time dissecting the low-level bit structure of floating-point numbers and implementing some of the arithmetic and logic from scratch using integer operations.

    And then she writes about logic cells, hardware pipeline architectures, RTL (register-transfer level), ASIC floorplans, and tapeout. Building hardware is hard, as iteration cycles are long and fabricating stuff costs real money. That was quite a journey.

    Her "about" page adds more context that helps everything make sense:

    > Professionally, I have a Masters in Electrical and Computer Engineering, have previously worked as a CPU designer at ARM and an FPGA engineer at Optiver. -- https://essenceia.github.io/about/

  • Nice write-up.

    Let me offer a nitpck: in the "Gradual underflow" section it says this about subnormal numbers:

        Bonus: we have now acquired extra armour against a division by zero:
    
        if ( x != y ) z = 1.0 / ( x - y );
    
    But that's not that useful: just because you're not dividing by zero doesn't mean the result won't overflow to infinity, which is what you get when you do divide by zero.

    Think about it this way: the smallest subnormal double is on the order of 10^-324, but the largest double is on the order of 10^308. If `x - y` is smaller than 10^-308, `1.0 / (x - y)` will be larger than 10^308, which can't be represented and must overflow to infinity.

    This C program demonstrates this:

        #include <stdio.h>
        #include <float.h>
        #include <math.h>
    
        // return a (subnormal) number that results in zero when divided by 2:
        double calc_tiny(void)
        {
            double x = DBL_MIN; // the normal number closest to 0.0
            while (1) {
                double next = x / 2.0;
                if (next == 0.0) {
                    return x;
                }
                x = next;
            }
        }
    
        int main(void)
        {
            double y = calc_tiny();
            double x = 2.0 * y;
            if (x != y) {
                double z = 1.0 / (x - y);
                printf("division is safe: %g\n", z);
            } else {
                printf("refusing to divide by zero\n");
            }
        }
    
    (It will print something like "division is safe: inf", or however else your libc prints infinity)
    • Other things worth noting about denormal numbers:

      - It’s not just ‘old’ FPUs that handle them verrry slowly. Benchmark this aspect of your target processors if this detail matters.

      - Most modern processors provide a “flush to zero” denormal handling mode (and sometimes you can separately specify “denormals are zero”, relevant when e.g. loading old data). However, math libraries usually haven’t been written with this mode in mind, so you need to be careful with this.

  • I had to do an FP8 adder as my final project for my FPGA lab. It was at least a full page of state machine. And I write small. I ended up just not doing the rounding and truncating instead because I was so done with it.

    Consider me educated on the mantissa. That's a nifty pedantry.

    Typo in the c++?

    > cout << "x =/= x: " << ((x=x) ? "true" : "false") << endl;

    Should be x != x?

    For the leading 0 counter, I've found it's even better for the tool if I use a for loop. Keeps things scalable and it's even less code. I'm not understanding this takeaway though

    > Sometimes a good design is about more than just performance.

    The good design (unless the author's note that it's easier to read and maintain makes it worse design?) was better performing. So sometimes a good design creates performance.

    Likewise for pipelining: it would have been interesting to know if the tools can reorder operations if you give them some pipeline registers to play with. In Xilinx land it'll move your logic around and utilize the flops as it sees fit.

    • Worked with Retiming using DC Compiler in an ASIC implementation. Remember a lot of back & forth, sometimes the tool just doesn't add enough registers to meet the constraint, had to test variable register depths; this was a design that used Synopsys DesignWare for FP ops lol.
  • I think the thing that truly scares me about floating point is not IEEE-754 or even the weird custom floating points that people come up with but the horrible, horrible things that some people think passes for a floating point implementation in hardware, who think that things like infinities or precision in the last place are kind of overrated.
  • Everyone who has ever had to build a floating point unit has hated it with a passion, I've watched in done from afar, and done it myself
    • And anyone implementing numerical algorithms is thankful for the tremendous amount of thought put into the fp spec. The complexity is worth it and makes the code much safer.
      • imo they were wrong almost as much as they were right. -0.0, the plethora of NaNs, and having separate Inf and NaN all make the life of people writing algorithms a lot more annoying for very little benefit.
      • There was actually no "thought" being put into the IEEE spec as such. It was merely a codification of the design of the Intel FPU (only one of many, very different implementations of FP units pre-standardisation). There was thought put into that implementation, but the "standard" is merely a codification of that design.

        It has many many warts, and many design choices were made given the constraints of hardware of that time, not by considerations in terms of a standard.

    • I think I would find it very challenging but fun. Certainly more fun than writing a date/time library (way more inconsistent cases; daylight savings time horrors; leap seconds; date jumps when moving from Julian to Gregorian) or a file system (also fun, I think, but thoroughly testing it scares me of)
    • I just wish there were a widespread decimal-based floating point standard & units.
      • Would it help though? IMHO, being binary is one of the least confusing sides of IEEE 754.
      • doesn't ieee754 define a decimal format? Specifically "decimal64"
  • I love this article, the edge cases are where the seeming “simplicity” of floating-point numbers breaks down

    Recently wrote a chapter in tiny-vllm course about floats in context of LLM inference, much shorter and not that deep as this one, for anyone interested in topic you might like it too https://github.com/jmaczan/tiny-vllm?tab=readme-ov-file#how-...

  • Super cool to tape out a nearly 500MHz systolic array!!! I wish sometimes I had gone far enough with my hardware education to be able to do more than simple FOGA designs.

    A really cool thing to see would be the newer block scaled fp4/fp8 data types. Maybe for their next asic they can try it haha - https://arxiv.org/abs/2310.10537

  • I wish I knew more people like this author. Then, maybe, I would have maintained my faith in humanity.