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