-
Notifications
You must be signed in to change notification settings - Fork 13
/
fixpoint.h
84 lines (71 loc) · 2.71 KB
/
fixpoint.h
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
/*
* fixpoint.h - Fixed point routines.
*
* Written by
* Andreas Dehmel <[email protected]>
*
* This file is part of VICE, the Versatile Commodore Emulator.
* See README for copyright notice.
*
* 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; either 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., 59 Temple Place, Suite 330, Boston, MA
* 02111-1307 USA.
*
*/
#ifndef _FIXPOINT_H
#define _FIXPOINT_H
#define INLINE_FIXPOINT_FUNCTIONS
#ifdef FIXPOINT_ARITHMETIC
#define FIXPOINT_PREC 12
typedef int vreal_t;
#define REAL_VALUE(x) ((vreal_t)((x) * (1 << FIXPOINT_PREC)))
#define REAL_MULT(x, y) fixpoint_mult(x, y)
#define REAL_TO_INT(x) ((int)((x) >> FIXPOINT_PREC))
/* sound clk specific */
typedef unsigned long soundclk_t;
#define SOUNDCLK_PREC 10
#define SOUNDCLK_CONSTANT(x) ((soundclk_t)(x * (1 << SOUNDCLK_PREC)))
#define SOUNDCLK_MULT(a,b) soundclk_mult(a, b)
#define SOUNDCLK_LONG(a) ((long)((a) >> SOUNDCLK_PREC))
#define SOUNDCLK_LONG_RAW(a) SOUNDCLK_LONG(a)
/* inline these functions or not? */
#ifdef INLINE_FIXPOINT_FUNCTIONS
#include "fixpoint.c"
#else /* INLINE_FIXPOINT_FUNCTIONS */
extern vreal_t fixpoint_mult(vreal_t x, vreal_t y);
extern soundclk_t soundclk_mult(soundclk_t a, soundclk_t b);
#endif /* INLINE_FIXPOINT_FUNCTIONS */
#else /* FIXPOINT_ARITHMETIC */
typedef float vreal_t;
#define REAL_VALUE(x) (x)
#define REAL_MULT(x,y) (x * y)
#define REAL_TO_INT(x) ((int)(x))
/* sound clk specific */
typedef double soundclk_t;
#define SOUNDCLK_CONSTANT(x) ((soundclk_t)(x))
#define SOUNDCLK_MULT(a,b) (a) * (b)
#define SOUNDCLK_LONG(a) ((long)BIG_FLOAT_TO_INT(a))
#define SOUNDCLK_LONG_RAW(a) ((long)(a))
#endif /* FIXPOINT_ARITHMETIC */
/* Some platforms (e.g. RISC OS) throw floating exceptions when attempting
a float-to-int cast where the floating point value is outside the range of
an int. Use these macros when casting potentially large FP-values. */
#if defined(__riscos)
#define BIG_FLOAT_TO_INT(f) fmod(f, 2147483648.0)
#define BIG_FLOAT_TO_UINT(f) fmod(f, 4294967296.0)
#else
#define BIG_FLOAT_TO_INT(f) (f)
#define BIG_FLOAT_TO_UINT(f) (f)
#endif
#endif