Sinclair ZX81 Integer vs Floating Point Math Performance
This article explores the mathematical architecture of the Sinclair ZX81, highlighting its reliance on software-based floating-point routines for all numeric operations. It details the five-byte number format used by Sinclair BASIC and explains why true integer mathematics were unavailable to standard users without machine code. Readers will understand the performance implications of this design choice and how the Z80 processor handled calculations without a dedicated math coprocessor.
The Z80 Architecture and Lack of FPU
The Sinclair ZX81 was powered by the Zilog Z80A microprocessor running at 3.25 MHz. Unlike modern computers, this 8-bit CPU did not include a Floating Point Unit (FPU) or dedicated hardware for mathematical operations. Every calculation, from simple addition to complex trigonometric functions, had to be executed through software routines stored in the system’s Read-Only Memory (ROM). This architectural decision kept manufacturing costs low but placed a significant burden on the processor, resulting in notoriously slow mathematical performance compared to contemporaries with more robust math capabilities.
Sinclair BASIC and the Five-Byte Format
A critical distinction in understanding the ZX81’s math performance is its handling of data types. In Sinclair BASIC, there was no distinct integer variable type. Every numeric value, whether it was a whole number like 10 or a decimal like 10.5, was stored using the same five-byte floating-point format. This format consisted of one byte for the exponent and four bytes for the mantissa. Because the system did not differentiate between integers and floating-point numbers at the variable level, the CPU processed whole number calculations using the same complex floating-point algorithms used for decimals.
Performance Comparison in BASIC
Since all numbers were treated as floating-point values, there was no performance gain when performing integer mathematics within Sinclair BASIC. Adding two whole numbers required the same normalization, exponent alignment, and mantissa processing as adding two decimal numbers. Consequently, a loop counting from 1 to 1000 ran at essentially the same speed as a loop incrementing by 0.1, provided the number of operations remained constant. The overhead of the floating-point routines dominated execution time, rendering the concept of optimized integer math irrelevant for standard BASIC programmers.
Machine Code and True Integer Mathematics
The only scenario where integer mathematics outperformed floating-point mathematics on the ZX81 was when programmers bypassed BASIC entirely and wrote in machine code. By utilizing the Z80’s native 8-bit integer registers and arithmetic commands, developers could execute whole number calculations significantly faster than the ROM-based floating-point routines. This technique was commonly used in fast-loading games and demos where speed was critical. However, this required advanced technical knowledge and was not accessible to the average user working within the BASIC environment.
Conclusion
The Sinclair ZX81 did not perform integer mathematics differently from floating-point mathematics when operating in Sinclair BASIC. Due to the absence of an integer data type, all numeric operations utilized slow, software-driven floating-point routines. While machine code programmers could leverage the Z80 processor’s native integer capabilities for speed, the standard user experience was defined by a uniform, floating-point computational model that prioritized memory efficiency over calculation speed.