The following C function is from fastapprox project.
static inline float
fasterlog2 (float x)
{
union { float f; uint32_t i; } vx = { x };
float y = vx.i;
y *= 1.1920928955078125e-7f;
return y - 126.94269504f;
}
I know that C union can be translated to Delphi variant record, but I still experienced difficulty in translating such low-level C code to Delphi. I hope Delphi experts here are willing to help.
More Information
I add this section later, which is not a part of the question. This section gives information to readers that expect better accuracy.
- In fastapprox,
fasterlog2()
was deliberately designed to be simpler, faster but less accurate Log2 function. Anyone who expect better accuracy can use the more-accurate function they provide, namelyfastlog2()
. - They included a Mathematica notebook with an explanation of their algorithms as well as the origin of some mysterious values, e.g.
126.94269504
. Mathematica website provides a free viewer for the.nb
files. - See also: Why the IEEE-754 exponent bias used in this C code is 126.94269504 instead of 127?