Merge branch 'master' into cairocanvas
[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 #include "pbd/libpbd_visibility.h"
14
15 static inline float fast_log2 (float val)
16 {
17         /* don't use reinterpret_cast<> because that prevents this
18            from being used by pure C code (for example, GnomeCanvasItems)
19         */
20         union {float f; int i;} t;
21         t.f = val;
22         int * const    exp_ptr =  &t.i;
23         int            x = *exp_ptr;
24         const int      log_2 = ((x >> 23) & 255) - 128;
25         x &= ~(255 << 23);
26         x += 127 << 23;
27         *exp_ptr = x;
28         
29         val = ((-1.0f/3) * t.f + 2) * t.f - 2.0f/3;
30         
31         return (val + log_2);
32 }
33
34 static inline float fast_log (const float val)
35 {
36         return (fast_log2 (val) * 0.69314718f);
37 }
38
39 static inline float fast_log10 (const float val)
40 {
41         return fast_log2(val) / 3.312500f;
42 }
43
44 static inline float minus_infinity(void) { return -HUGE_VAL; }
45
46 #endif /* __pbd_fastlog_h__ */