MuseScore  3.4
Music composition and notation
fastlog.h
Go to the documentation of this file.
1 /* Copyright unknown. Code by Laurent de Soras <laurent@ohmforce.com>.
2  * This file is licensed under the WTFPL v2 license.
3  * http://www.wtfpl.net/about/
4  */
5 
6 #ifndef __FASTLOG_H__
7 #define __FASTLOG_H__
8 
9 #include <math.h> /* for HUGE_VAL */
10 
11 static inline float fast_log2 (float val)
12  {
13  /* don't use reinterpret_cast<> because that prevents this
14  from being used by pure C code (for example, GnomeCanvasItems)
15  */
16  union {float f; int i;} t;
17  t.f = val;
18  int* const exp_ptr = &t.i;
19  int x = *exp_ptr;
20  const int log_2 = ((x >> 23) & 255) - 128;
21  x &= ~(255 << 23);
22  x += 127 << 23;
23  *exp_ptr = x;
24  val = ((-1.0f/3) * t.f + 2) * t.f - 2.0f/3;
25  return (val + log_2);
26  }
27 
28 static inline float fast_log (const float val)
29  {
30  return (fast_log2 (val) * 0.69314718f);
31  }
32 
33 static inline float fast_log10 (const float val)
34  {
35  return fast_log2(val) / 3.312500f;
36  }
37 
38 static inline float minus_infinity() { return -HUGE_VAL; }
39 
40 #endif
41