There is a really simple way to compute log2 bit-by-bit. It is "efficient" in the way that it only requires basic integer operations and has no dependencies. Below you find a go at it in C99. It is written in one chunk, but in practice you would split the code into header, C-file and main.c as is indicated in the code.
It implements three functions:
// Input: x in [1, 2) in Q-Format 1.15.
// Output: log2(x) in [0, 1) in Q-Format 0.16.
extern uint16_t simple_log2_Q15 (uint16_t x);
As said, it only uses integer arithmetic. The implementation is so simple that it's immeadiately clear how to write it in assembly. It uses: multplication, addition, comparison and shift by constant offsets of unsigned 32-bit (or less) integers.
// A wrapper for the above for convenience. Can be used just like log2f()
// except that it expects an ordinary, positive number x as input.
float simple_log2f (float x);
This can be used during transition. It uses simple_log2_Q15 from above, frexpf, ldexpf, and float addition.
void test_simple_log2f (void);
This is just for testing. You can run it on a host to get a feel for the approximation error. With the default setting from the source, it expands 15 fractional bits of log2.
Running in godbolt Compiler Explorer, it prints:
error(simple_log2f) = 1.025200e-04 = 12.251807 bits
Full Code (C99)
////////////////////////////////////////////////////////////////////////////
// This goes to simple-log2.h
#ifndef SIMPLE_LOG2_H
#define SIMPLE_LOG2_H
#include <stdint.h>
// Input: x in [1, 2) in Q-Format 1.15.
// Output: log2(x) in [0, 1) in Q-Format 0.16.
extern uint16_t simple_log2_Q15 (uint16_t x);
// Returns log2(x) where x > 0 is an ordinary number.
extern float simple_log2f (float x);
#endif /* SIMPLE_LOG2_H */
////////////////////////////////////////////////////////////////////////////
// This goes to simple-log2.c
// #include "simple-log2.h"
#define FBIT 15
// Number of computed fractional bits in the result of simple_log2_Q15().
// May range from 0...16.
#define N_BITS 15
// Input: x in [1, 2) in Q-Format 1.15.
// Output: log2(x) in [0, 1) in Q-Format 0.16.
uint16_t simple_log2_Q15 (uint16_t x)
{
const uint32_t Two = UINT32_C (0x80000000);
// Expand N_BITS fractional bits of logarithmus dualis one by one.
uint16_t ld = 0;
for (uint8_t i = 1; i <= N_BITS; ++i)
{
ld <<= 1;
// Squaring shifts log2(x) one bit to the left: log2(x^2) = 2 log2(x).
// The result x2 stands in Q2.30, and log2(x^2) >= 1 iff x2 >= 2.
uint32_t x2 = (uint32_t) x * x;
if (x2 >= Two)
{
// Shift FBITs to the right to convert from Q2.30 to Q1.15.
// One additional shift subtracts 1 from log2(x).
x = (uint16_t) (x2 >> (1 + FBIT));
ld |= 1;
}
else
{
// Shift FBITs to the right to convert from Q2.30 to Q1.15.
x = (uint16_t) (x2 >> FBIT);
}
}
// Return logarithmus dualis as Q0.16 in [0, 1).
return ld << (16 - N_BITS);
}
////////////////////////////////////////////////////////////////////////////
#include <math.h>
// A wrapper for the above for convenience. Can be used just like log2f()
// except that it expects an ordinary, positive number x as input.
float simple_log2f (float x)
{
// Decompose x = mant * 2^ex with mant in [0.5, 1).
int ex;
float mant = frexpf (x, &ex);
// Turn mant in [0.5, 1) into Q-format 1.15 in [1, 2). This introduces
// a multiplication by 2, which we will account for at the end.
uint16_t mantQ15 = (uint16_t) ldexpf (mant, FBIT + 1);
// Compute log2(mant) in Q-format 0.16.
uint16_t logQ16 = simple_log2_Q15 (mantQ15);
// Turn Q0.16 back into float. "ex - 1" to undo the mul-by-2 from above.
return (ex - 1) + ldexpf ((float) logQ16, -FBIT - 1);
}
////////////////////////////////////////////////////////////////////////////
// This is for testing only and goes to, say, main.c.
// #include "simple-log2.h"
#include <math.h>
#include <stdio.h>
void test_simple_log2f (void)
{
float max_dy = 0;
float step_x = ldexpf (1.0f, -17);
for (float x = 0.5f; x <= 2.5f; x += step_x)
{
float y = simple_log2f (x);
float dy = fabsf (y - logf (x) / logf (2.0f));
if (dy > max_dy)
max_dy = dy;
}
printf ("error(simple_log2f) = %e = %f bits\n", max_dy, -1-log2f (max_dy));
}
int main (void)
{
test_simple_log2f ();
return 0;
}