How to get the calculate the binary logarithm of a fixed-point number with 18 decimals?
I figured it out, eventually, by making the following observations:

$\log_2{(x)} = -log_2{(\frac{1}{x})}$
To accurately calculate $\frac{1}{x}$, multiply $1e18$ by itself then divide by $x$.

My implementation looks something like this (written in Solidity):
pragma solidity ^0.8.0;

function log2(uint256 x) internal pure returns (int256 result) {
    require(x &gt; 0);
    unchecked {
        // This works because log2(x) = -log2(1/x).
        int256 sign;
        if (x &gt;= 1e18) {
            sign = 1;
        } else {
            sign = -1;
            unchecked {
                x = 1e36 / x;
            }
        }

        // Calculate the integer part of the logarithm and add it to the result and finally calculate y = x * 2^(-n).
        uint256 n = mostSignificantBit(uint256(x / 1e18));

        // The integer part of the logarithm as a signed 59.18-decimal fixed-point number. The operation can't overflow
        // beacuse n is maximum 255, SCALE is 1e18 and sign is either 1 or -1.
        result = int256(n) * 1e18;

        // This is y = x * 2^(-n).
        int256 y = int256(x) &gt;&gt; n;

        // If y = 1, the fractional part is zero.
        if (y == 1e18) {
            return result * sign;
        }

        // Calculate the fractional part via the iterative approximation.
        // The &quot;delta &gt;&gt;= 1&quot; part is equivalent to &quot;delta /= 2&quot;, but shifting bits is faster.
        for (int256 delta = 5e17; delta &gt; 0; delta &gt;&gt;= 1) {
            y = (y * y) / 1e18;

            // Is y^2 &gt; 2 and so in the range [2,4)?
            if (y &gt;= 2 * 1e18) {
                // Add the 2^(-m) factor to the logarithm.
                result += delta;

                // Corresponds to z/2 on Wikipedia.
                y &gt;&gt;= 1;
            }
        }
        result *= sign;
    }
}

Where mostSignificantBit is a function that calculates the most significant bit of a number that can go as high as $2^{256} -1 $.