Remaining changes needed for building libpdb on Windows (except for adding the extra...
[ardour.git] / libs / pbd / pbd / fastlog.h
1 /*
2 Copyright © 2013 Laurent de Soras <laurent.de.soras@free.fr>
3
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.
7 */
8 #ifndef __pbd_fastlog_h__
9 #define __pbd_fastlog_h__
10
11 #include <math.h> /* for HUGE_VAL */
12
13 static inline float fast_log2 (float val)
14 {
15         /* don't use reinterpret_cast<> because that prevents this
16            from being used by pure C code (for example, GnomeCanvasItems)
17         */
18         union {float f; int i;} t;
19         t.f = val;
20         int * const    exp_ptr =  &t.i;
21         int            x = *exp_ptr;
22         const int      log_2 = ((x >> 23) & 255) - 128;
23         x &= ~(255 << 23);
24         x += 127 << 23;
25         *exp_ptr = x;
26         
27         val = ((-1.0f/3) * t.f + 2) * t.f - 2.0f/3;
28         
29         return (val + log_2);
30 }
31
32 static inline float fast_log (const float val)
33 {
34         return (fast_log2 (val) * 0.69314718f);
35 }
36
37 static inline float fast_log10 (const float val)
38 {
39         return fast_log2(val) / 3.312500f;
40 }
41
42 static inline float minus_infinity(void) { return -HUGE_VAL; }
43
44 #endif /* __pbd_fastlog_h__ */