Ardour  8.12
fastlog.h
Go to the documentation of this file.
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 
26  x &= ~(255 << 23);
27  x += 127 << 23;
28 
29  *exp_ptr = x;
30 
31  val = ((-1.0f/3) * t.f + 2) * t.f - 2.0f/3;
32 
33  return (val + log_2);
34 }
35 
36 static inline float fast_log (const float val)
37 {
38  return (fast_log2 (val) * 0.69314718f);
39 }
40 
41 static inline float fast_log10 (const float val)
42 {
43  return fast_log2(val) / 3.312500f;
44 }
45 
46 static inline float minus_infinity(void) { return -HUGE_VAL; }
47 
48 #endif /* __pbd_fastlog_h__ */
static float fast_log2(float val)
Definition: fastlog.h:15
static float fast_log(const float val)
Definition: fastlog.h:36
static float fast_log10(const float val)
Definition: fastlog.h:41
static float minus_infinity(void)
Definition: fastlog.h:46