summaryrefslogtreecommitdiff
path: root/muse2/muse/fastlog.h
blob: bbcf18cc2135ceb2cee86e31a3bf1d6aa53bf0c8 (plain)
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
//=========================================================
//  MusE
//  Linux Music Editor
//  $Id: ./muse/fastlog.h $
//
//  Copyright (C) 1999-2011 by Werner Schweer and others
//
//  This program is free software; you can redistribute it and/or
//  modify it under the terms of the GNU General Public License
//  as published by the Free Software Foundation; version 2 of
//  the License, or (at your option) any later version.
//
//  This program is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//  GNU General Public License for more details.
//
//  You should have received a copy of the GNU General Public License
//  along with this program; if not, write to the Free Software
//  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
//
//=========================================================
/* Copyright unknown. Code by Laurent de Soras <laurent@ohmforce.com>.
 */

#ifndef __pbd_fastlog_h__
#define __pbd_fastlog_h__

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

namespace MusECore {

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; }

} // namespace MusECore

#endif /* __pbd_fastlog_h__ */