Computing Trig in Hardware
Every calculator, GPU, and DSP chip computes sine and cosine without a multiplier — using an algorithm called CORDIC that needs nothing more than bit shifts and addition. Build one from logic gates and watch it converge in your browser.
Division Without a Divider
The key insight behind CORDIC is that you never need to multiply or divide. Shifting a binary number right by n positions is the same as dividing by 2n. Shift right by 1 and you divide by 2. Shift by 3 and you divide by 8. Hardware can do this in a single gate delay — it just rewires the bits.
Try changing the value and shift inputs below. The RightShifter component divides instantly, with no clock cycles and no iteration.
The Rotation Formula
To rotate a vector (x, y) by a small angle, CORDIC uses:
x_next = x - (y >> iteration) y_next = y + (x >> iteration)
That’s it — a shift and an add for each coordinate. Subtraction is done in two’s complement: invert all bits with a BusNot, then add 1 via the carry input of a SignedAdder. The circuit below computes both x − (y >> shift) and x + (y >> shift) simultaneously.
Which Way to Rotate?
CORDIC tracks the remaining rotation angle in a register called z. Each iteration, it checks the sign of z: if z is positive (we still need to rotate counterclockwise), it rotates one way; if z is negative (we overshot), it rotates back.
A SignedComparator checks whether z ≥ 0 and drives a Mux that selects between addition and subtraction. Try setting the angle input to different values — values above 127 are treated as negative in signed 8-bit arithmetic.
Counting Iterations
CORDIC converges quickly — each iteration adds about one bit of precision. For our 8-bit values, 8 iterations are enough. A Register counts from 0 to 7, an Incrementer bumps it each tick, and a Comparator disables the write enable when the count reaches 8.
Click Tick to step the counter. The Done LED lights when all 8 iterations are complete.
The Angle Lookup Table
Each CORDIC iteration rotates by a pre-computed angle: atan(2−i). These values are baked into Constant nodes and selected by a cascaded Mux tree that uses the iteration index bits as selectors.
The table starts at 32 (representing 45°) and shrinks: 19 (≈ 26.6°), 10 (≈ 14°), 5 (≈ 7.1°), 3, 1, 1, 0. Change the iteration input from 0 to 7 and watch the selected angle change. Three layers of muxes select one of eight values using just three bits.
The Full CORDIC Engine
Everything we’ve built — right shifters, signed arithmetic, direction detection, iteration control, and the angle lookup table — comes together in one circuit. The full CORDICIteration engine starts with a vector (80, 0) pointing right and rotates it 45°.
Click Run or step through the iterations one by one. Watch x decrease and y increase as the vector rotates. After 8 iterations, both x and y should be approximately equal — confirming that cos(45°) ≈ sin(45°).
The final values are scaled by the CORDIC gain factor (K ≈ 1.647), so the raw outputs are larger than the true sine and cosine. In production hardware, a single constant multiplication at the end corrects for this. But the core computation — 8 iterations of shift-and-add — runs with zero multipliers.
This is the same algorithm inside your scientific calculator, the trigonometric units of early GPUs, and every DSP chip that needs fast angle computation. No Taylor series, no lookup table interpolation — just wires, registers, and a shifter.