The Multiplicative Mirror: Squaring, Roots, and the Geometry of Halving
Before we can dive into arbitrary exponentiation and dynamic logarithms, we have to formally cross the boundary into the highest foundational level of the Mercury engine: The Power Tier.
To understand exactly where that boundary lies, we need to look at how we scale numbers by a constant of $2$.
In the Multiplicative Tier, if you want to scale a number by $2$, you use Double ($A + A$) or Half ($A / 2$). Notice the mechanics: you are relying on the Additive Tier’s operations to achieve a Multiplicative result.
But what happens when you need to scale a number by an exponent of $2$? You use Square ($A \cdot A$) and Square Root ($A^{0.5}$). These functions rely on the Multiplicative Tier’s mechanics to achieve a Power result.
Squaring and square roots are not the end of the Multiplicative Tier. They are the absolute foundational constants of the Power Tier. They are the base-2 geometric primitives that the rest of the tier (mercuryPow and mercuryLog) must rely on to survive. Let’s look at how the engine optimizes these foundational operations.
The Geometry of Self-Scaling (mercurySqr)
Squaring is simply multiplication looking in a mirror.
In our earlier post on geometric scaling, we established that multiplying two places physically adds their exponents:
$$B^i \cdot B^j = B^{i+j}$$
When a number is squared, the factors are identical. The spatial footprint perfectly doubles:
$$B^i \cdot B^i = B^{2i}$$
Because of this rigid geometric rule, mercurySqr acts as a highly streamlined version of our standard multiplication engine. Because the factors are identical, the engine only needs to read from one input array (a). The underlying geometric expansion—the nested loops combining places and the 64-bit register capturing the native hardware carry—remains exactly the same. It is the Multiplicative Tier operating at maximum efficiency to serve the Power Tier.
Halving the Footprint (The highbit Payoff)
If squaring a number perfectly doubles its spatial footprint ($2i$), then reversing the operation (taking the square root) must perfectly halve it.
This geometric certainty unlocks a brilliant hardware optimization. When mercurySqrt initializes, it does not need to execute a complex search loop to figure out where its starting bit should be. It mathematically knows exactly where the highest possible bit of the root must live.
Look at the first three active lines of mercurySqrt:
// Extract the highest 32-bit place of our target number
int h = (int)a[1];
// Calculate the absolute highest possible bit of the root
int highbit = ((int) ((h + 1) * 32)) / 2;
int lowbit = highbit - Precision * 32;
By taking the highest base-$2^{32}$ place (h), translating it into raw bits (* 32), and simply dividing by $2$, the engine instantly pinpoints the ceiling of the answer.
This is a zero-cost optimization. It requires no loops, no comparisons, and no trial-and-error. It is an instant jump to the correct binary location, and it only works because the engine strictly respects the spatial geometry established in the tiers below it.
The Constructive Binary Search
Once the ceiling is found, mercurySqrt must construct the rest of the root. It does this through a highly optimized, automated binary search.
Starting from highbit and working down to lowbit, the engine tests each possible binary place to see if it belongs in the final answer:
for (int i = highbit; i >= lowbit; i--) {
// Generate the test bit
mercury_2Pow(stack, Precision+1, i, mark);
// Add the test bit to our running guess
mercuryAdd(stack, Precision+1, m, mark, q);
// Square the new guess
mercurySqr(stack, Precision+1, q, x);
// Compare the squared guess against our original target
int s = mercuryCmp(Precision+1, x, A);
if (s <= 0) {
// It fits! Keep the bit in our running root.
mercuryLoadMercury(stack, Precision+1, q, m);
// If it is a perfect match, exit early.
if (s == 0)
break;
}
}
This loop is the definition of a constructive algorithm. It tests a single bit, geometrically expands it (mercurySqr), and compares it to the target (mercuryCmp). If it overshoots the target, the bit is discarded. If it fits, the bit is permanently locked into the running root (m).
It is a flawlessly automated descent that relies entirely on the established geometry of the Multiplicative Tier to dynamically construct its inverse.
Up Next: The Exponentiation Engine
With the base-$2$ constants of the Power Tier firmly established, we are now ready to unleash them.
Next time, we will explore mercuryPow, and see exactly how the engine uses these squaring mechanics to skip millions of redundant multiplications and rapidly calculate massive, arbitrary geometric growth.