When designing a library for floating-point calculations, a developer encountered a common challenge: identifying the lowest set bit in a float. While bit-twiddling is a standard approach, the straightforward algorithm fails for powers of two, necessitating an `if` statement. In high-performance computing, such branches can degrade performance by slowing down common cases and causing branch mispredictions. To eliminate the branch, the developer sought a single formula that could handle all cases automatically, but struggled to find a convincingly optimal one manually.

A stylized graphic representing the superoptimization of a floating-point unit emulator.
The developer used an inductive brute-force search to discover a novel five-operation formula for identifying the lowest set bit in a float.purplesyringa.moe

This image represents the project of superoptimizing an FPU emulator by discovering non-obvious mathematical shortcuts.

Rather than relying on manual reasoning or large language models (LLMs), the developer opted for a 'non-orthodox' brute-force approach. The goal was to automatically discover a formula by iterating through all possible combinations of operations. The search space was defined by a set of useful operations: integer addition/subtraction, bitwise AND/OR/XOR, and floating-point subtraction. By using an inductive search method, the developer built up formulas starting from zero operations and combining them into larger expressions. This allowed for the pruning of duplicate results—where different expressions yielded identical outputs—enabling the script to complete the exhaustive search in just four minutes.

The search successfully identified several five-operation formulas. One particularly notable result was a formula that, while appearing illogical at first glance, successfully avoided the power-of-two issue. Interestingly, the developer discovered that one of these five-operation formulas could be further optimized into a four-operation version by reusing a subexpression. This specific rewrite involved a clever manipulation where a floating-point subtraction merged with a negation to save an operation. This automated discovery highlighted how brute-force search can uncover non-obvious mathematical shortcuts that might elude human intuition or standard compiler optimizations. The resulting code is now integrated into a work-in-progress FPU (Floating Point Unit) emulator.