Aussie AI
Example: Log2 of Floating-Point is the Exponent
-
Book Excerpt from "Generative AI in C++"
-
by David Spuler, Ph.D.
Example: Log2 of Floating-Point is the Exponent
The log2
function for float types is a non-linear function that is quite expensive to compute.
We already computed log2
of an integer with low-level bit fiddling methods
based on a count-leading-zeros algorithm in the previous chapter.
There's also a different bitwise trick for log2
of floating-point numbers.
This method computes the truncated integer version of the log2
algorithm
(e.g. for use in logarithmic power-of-two quantization).
There's a very easy way:
The base-2 logarithm is the exponent!
It's sitting right there, already calculated, hidden in plain sight
amongst the 32 bits of your friendly float
variables.
Here's some C++ code to extract it:
int ilog2_exponent(float f) // Log2 for 32-bit float { unsigned int u = *(unsigned int*)&f; int iexp = ((u >> 23) & 255); // 8-bit exponent iexp -= 127; // Remove the "offset" return iexp; }
Alternatively, for greater portability and probably extra speed, too,
there are some standardized builtin C++ functions available across various platforms (including Linux and Microsoft)
that can extract the exponent:
frexp
, ldexp
, ilogb
, and scalbn
, are some that come to mind.
• Next: • Up: Table of Contents |
The new AI programming book by Aussie AI co-founders:
Get your copy from Amazon: Generative AI in C++ |