root WRITEUP Scaled Log 16

Scaled Log 16

A 16-bit fixed-point logarithmic number system 2025-05-13

Project Readme • Project Files • GitHub Repo • sl16.c

When reversing Quake Ⅲ Arena’s fast inverse square root algorithm you come to realize that the bit representation of a positive normalized IEEE 754 floating-point number is a piecewise-linear approximation of its logarithm, up to constant scaling and shifting. So then here’s an idea: let’s cut the middleman and store numbers as their own logarithm. For the benefits of this approach, refer to Scaled Log 16’s readme.

Scaled Log 16 is a software number system, and it prioritizes performance over precision, which places constraints on the design of the number format. For instance,

  • The sign is stored in the rightmost bit of a Scaled Log 16 number so that it mostly either takes care of itself or becomes a rounding error;
  • The integral and fractional parts of logarithms split the 8086’s 16-bit registers AX CX DX BX in half so they can be accessed independently using the 8-bit registers AH CH DH BH and AL CL DL BL, respectively.

If SL16_LINEAR is defined, \(\log_2\) and \(\exp_2\) are implemented using piecewise-linear approximations that turn out to be the IEEE 754 trick in disguise (what a full-circle moment). If SL16_QUADRATIC is defined, \(\log_2\) and \(\exp_2\) are implemented using piecewise-quadratic approximations that I eyeballed on a graphing calculator (this is sufficient because we want coefficients to have short binary expansions so they can be implemented using bit shifts instead of multiplications).