File: fastlog.h

package info (click to toggle)
musescore 2.0.3%2Bdfsg1-2
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 202,532 kB
  • ctags: 58,769
  • sloc: cpp: 257,595; xml: 172,226; ansic: 139,931; python: 6,565; sh: 6,383; perl: 423; makefile: 290; awk: 142; pascal: 67; sed: 3
file content (41 lines) | stat: -rw-r--r-- 990 bytes parent folder | download | duplicates (12)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
/* Copyright unknown. Code by Laurent de Soras <laurent@ohmforce.com>.
 * This file is licensed under the WTFPL v2 license.
 * http://www.wtfpl.net/about/
 */

#ifndef __FASTLOG_H__
#define __FASTLOG_H__

#include <math.h> /* for HUGE_VAL */

static inline float fast_log2 (float val)
      {
	/* don't use reinterpret_cast<> because that prevents this
	   from being used by pure C code (for example, GnomeCanvasItems)
	*/
      union {float f; int i;} t;
	t.f = val;
      int* const exp_ptr = &t.i;
	int x              = *exp_ptr;
	const int log_2    = ((x >> 23) & 255) - 128;
	x &= ~(255 << 23);
	x += 127 << 23;
	*exp_ptr = x;
	val = ((-1.0f/3) * t.f + 2) * t.f - 2.0f/3;
	return (val + log_2);
      }

static inline float fast_log (const float val)
      {
      return (fast_log2 (val) * 0.69314718f);
      }

static inline float fast_log10 (const float val)
      {
	return fast_log2(val) / 3.312500f;
      }

static inline float minus_infinity() { return -HUGE_VAL; }

#endif