Skip to content

Instantly share code, notes, and snippets.

@iscgar
iscgar / fixed-log2.hpp
Created May 10, 2024 15:05
Fixed-point compile-time log2
// A quick and dirty hack to caluclate 32-bit fixed-point log2 at compile-time
// for defining constants and doing template metaprogramming where a fractional
// result is required.
// Apart from the static assertion, this code can be used even in C++98.
//
// NOTE: The first argument to `FixedPointLog2` should already be in fixed-point
// form (e.g. in order to calculate the log2 of 3 with the result in the
// form s15.16, you should use `FixedPointLog2<(3 << 16), 16>`.
#include <stddef.h>