2 Copyright © 2013 Laurent de Soras <laurent.de.soras@free.fr>
4 This work is free. You can redistribute it and/or modify it under the
5 terms of the Do What The Fuck You Want To Public License, Version 2,
6 as published by Sam Hocevar. See http://www.wtfpl.net/ for more details.
8 #ifndef __pbd_fastlog_h__
9 #define __pbd_fastlog_h__
11 #include <math.h> /* for HUGE_VAL */
13 #include "pbd/libpbd_visibility.h"
15 static inline float fast_log2 (float val)
17 /* don't use reinterpret_cast<> because that prevents this
18 from being used by pure C code (for example, GnomeCanvasItems)
20 union {float f; int i;} t;
22 int * const exp_ptr = &t.i;
24 const int log_2 = ((x >> 23) & 255) - 128;
29 val = ((-1.0f/3) * t.f + 2) * t.f - 2.0f/3;
34 static inline float fast_log (const float val)
36 return (fast_log2 (val) * 0.69314718f);
39 static inline float fast_log10 (const float val)
41 return fast_log2(val) / 3.312500f;
44 static inline float minus_infinity(void) { return -HUGE_VAL; }
46 #endif /* __pbd_fastlog_h__ */