4e9050cb8ba4a0b9aa1cb4f86c3bcf115b5b18f6
[ardour.git] / libs / pbd3 / pbd / fastlog.h
1 /* Copyright unknown. Code by Laurent de Soras <laurent@ohmforce.com>.
2  */
3
4 #ifndef __pbd_fastlog_h__
5 #define __pbd_fastlog_h__
6
7 #include <math.h> /* for HUGE_VAL */
8
9 static inline float fast_log2 (float val)
10 {
11         /* don't use reinterpret_cast<> because that prevents this
12            from being used by pure C code (for example, GnomeCanvasItems)
13         */
14         int * const    exp_ptr = (int *)(&val);
15         int            x = *exp_ptr;
16         const int      log_2 = ((x >> 23) & 255) - 128;
17         x &= ~(255 << 23);
18         x += 127 << 23;
19         *exp_ptr = x;
20         
21         val = ((-1.0f/3) * val + 2) * val - 2.0f/3;   // (1)
22         
23         return (val + log_2);
24 }
25
26 static inline float fast_log (const float val)
27 {
28         return (fast_log2 (val) * 0.69314718f);
29 }
30
31 static inline float fast_log10 (const float val)
32 {
33         return fast_log2(val) / 3.312500f;
34 }
35
36 static inline float minus_infinity() { return -HUGE_VAL; }
37
38 #endif /* __pbd_fastlog_h__ */