LMMS
Loading...
Searching...
No Matches
fixed_point.h
Go to the documentation of this file.
1/* Calf DSP Library
2 * DSP primitives.
3 *
4 * Copyright (C) 2001-2007 Krzysztof Foltman
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General
17 * Public License along with this program; if not, write to the
18 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19 * Boston, MA 02111-1307, USA.
20 */
21#ifndef __CALF_FIXED_POINT_H
22#define __CALF_FIXED_POINT_H
23
24namespace dsp {
25
26inline uint32_t shr(uint32_t v, int bits = 1) { return v>>bits; };
27inline int32_t shr(int32_t v, int bits = 1) { return v>>bits; };
28inline uint64_t shr(uint64_t v, int bits = 1) { return v>>bits; };
29inline int64_t shr(int64_t v, int bits = 1) { return v>>bits; };
30inline float shr(float v, int bits = 1) { return v*(1.0/(1<<bits)); };
31inline double shr(double v, int bits = 1) { return v*(1.0/(1<<bits)); };
32template<class T, int FracBits>
33inline T shr(T v, int bits = 1) {
34 v.set(v >> bits);
35 return v;
36}
37
38template<class T, int FracBits> class fixed_point {
40 enum { IntBits = (sizeof(T)*8) - FracBits };
41
42public:
44 inline fixed_point() {
45 }
46
48 template<class U, int FracBits2> inline fixed_point(const fixed_point<U, FracBits2> &v) {
49 if (FracBits == FracBits2) value = v.get();
50 else if (FracBits > FracBits2) value = v.get() << abs(FracBits - FracBits2);
51 else value = v.get() >> abs(FracBits - FracBits2);
52 }
53
54 /* this would be way too confusing, it wouldn't be obvious if it expects a whole fixed point or an integer part
55 explicit inline fixed_point(T v) {
56 this->value = v;
57 }
58 */
59 explicit inline fixed_point(double v) {
60 value = (T)(v*one());
61 }
62
64 static inline fixed_point from_base(const T &v)
65 {
67 result.value = v;
68 return result;
69 }
70
71 inline static T one() {
72 return (T)(1) << FracBits;
73 }
74
75 inline void set(T value) {
76 this->value = value;
77 }
78
79 inline T get() const {
80 return value;
81 }
82
83 inline operator double() const {
84 return value * (1.0/one());
85 }
86
87 inline fixed_point &operator=(double v) {
88 value = (T)(v*one());
89 return *this;
90 }
91
92 template<class U, int FracBits2> static inline T rebase(const fixed_point<U, FracBits2> &v) {
93 if (FracBits == FracBits2)
94 return v.get();
95 if (FracBits > FracBits2)
96 return v.get() << abs(FracBits - FracBits2);
97 return v.get() >> abs(FracBits2 - FracBits);
98 }
99
100 template<class U, int FracBits2> inline fixed_point &operator=(const fixed_point<U, FracBits2> &v) {
102 return *this;
103 }
104
105 template<class U, int FracBits2> inline fixed_point &operator+=(const fixed_point<U, FracBits2> &v) {
107 return *this;
108 }
109
110 template<class U, int FracBits2> inline fixed_point &operator-=(const fixed_point<U, FracBits2> &v) {
112 return *this;
113 }
114
115 template<class U, int FracBits2> inline fixed_point operator+(const fixed_point<U, FracBits2> &v) const {
116 fixed_point fpv;
118 return fpv;
119 }
120
121 template<class U, int FracBits2> inline fixed_point operator-(const fixed_point<U, FracBits2> &v) const {
122 fixed_point fpv;
124 return fpv;
125 }
126
128 template<class U, int FracBits2> inline fixed_point operator*(const fixed_point<U, FracBits2> &v) const {
129 fixed_point tmp;
130 tmp.set(((int64_t)value) * v.get() >> FracBits2);
131 return tmp;
132 }
133
135 template<class U, int FracBits2, class BigType> inline fixed_point& operator*=(const fixed_point<U, FracBits2> &v) {
136 value = (T)(((BigType)value) * v.get() >> FracBits2);
137 return *this;
138 }
139
140 inline fixed_point operator+(int v) const {
141 fixed_point tmp;
142 tmp.set(value + (v << FracBits));
143 return tmp;
144 }
145
146 inline fixed_point operator-(int v) const {
147 fixed_point tmp;
148 tmp.set(value - (v << FracBits));
149 return tmp;
150 }
151
152 inline fixed_point operator*(int v) const {
153 fixed_point tmp;
154 tmp.value = value*v;
155 return tmp;
156 }
157
158 inline fixed_point& operator+=(int v) {
159 value += (v << FracBits);
160 return *this;
161 }
162
163 inline fixed_point& operator-=(int v) {
164 value -= (v << FracBits);
165 return *this;
166 }
167
168 inline fixed_point& operator*=(int v) {
169 value *= v;
170 return *this;
171 }
172
174 inline T ipart() const {
175 return value >> FracBits;
176 }
177
179 inline unsigned int uipart() const {
180 return ((unsigned)value) >> FracBits;
181 }
182
184 inline unsigned int ui64part() const {
185 return ((uint64_t)value) >> FracBits;
186 }
187
189 inline T fpart() const {
190 return value & ((1 << FracBits)-1);
191 }
192
194 template<int Bits>
195 inline T fpart() const {
196 int fbits = value & ((1 << FracBits)-1);
197 if (Bits == FracBits) return fbits;
198 int shift = abs(Bits-FracBits);
199 return (Bits < FracBits) ? (fbits >> shift) : (fbits << shift);
200 }
201
203 inline double fpart_as_double() const {
204 return (value & ((1 << FracBits)-1)) * (1.0 / (1 << FracBits));
205 }
206
210 template<class U, int UseBits, class MulType>
211 inline U lerp_by_fract_int(U v1, U v2) const {
212 int fp = fpart<UseBits>();
213 assert ( fp >=0 && fp <= (1LL<<UseBits));
214 return v1 + shr(((MulType)(v2-v1) * fp), UseBits);
215 }
216
217 template<class U, int UseBits>
218 inline U lerp_table_lookup_int(U* data) const {
219 unsigned int pos = uipart();
220 return lerp_by_fract_int<U, UseBits>(data[pos], data[pos+1]);
221 }
222
225 template<class U, int UseBits>
226 inline U lerp_table_lookup_int_shift(U* data, unsigned int shift) {
227 unsigned int pos = (uipart() + shift) & ((1u << IntBits) - 1);
228 return lerp_by_fract_int<U, UseBits>(data[pos], data[pos+1]);
229 }
230
231 template<class U>
233 unsigned int pos = uipart();
234 return data[pos] + (data[pos+1]-data[pos]) * fpart_as_double();
235 }
236
237 template<class U>
238 inline U lerp_table_lookup_float_mask(U* data, unsigned int mask) const {
239 unsigned int pos = ui64part() & mask;
240 // printf("full = %lld pos = %d + %f\n", value, pos, fpart_as_double());
241 return data[pos] + (data[pos+1]-data[pos]) * fpart_as_double();
242 }
243
244 template<class U, int UseBits, class MulType>
245 inline U lerp_ptr_lookup_int(U *data) const {
246 unsigned int pos = ui64part();
248 }
249
250 template<class U>
251 inline U lerp_ptr_lookup_float(U *data) const {
252 unsigned int pos = ui64part();
253 return data[pos] + (data[pos+1]-data[pos]) * fpart_as_double();
254 }
255};
256
257template<class T, int FractBits>
259 v2 *= v;
260 return v2;
261}
262
265
266};
267
268#endif
assert(0)
float abs(const fft_t *freqs, off_t x)
Definition OscilGen.cpp:52
static const unsigned long mask[]
Definition bitwise.c:31
Definition fixed_point.h:38
unsigned long long int value
Definition fixed_point.h:39
fixed_point operator-(int v) const
Definition fixed_point.h:146
void set(T value)
Definition fixed_point.h:75
fixed_point & operator+=(const fixed_point< U, FracBits2 > &v)
Definition fixed_point.h:105
fixed_point operator*(int v) const
Definition fixed_point.h:152
fixed_point()
default constructor, does not initialize the value, just like - say - float doesn't
Definition fixed_point.h:44
fixed_point operator+(int v) const
Definition fixed_point.h:140
U lerp_table_lookup_float_mask(U *data, unsigned int mask) const
Definition fixed_point.h:238
static T rebase(const fixed_point< U, FracBits2 > &v)
Definition fixed_point.h:92
U lerp_ptr_lookup_float(U *data) const
Definition fixed_point.h:251
U lerp_table_lookup_int_shift(U *data, unsigned int shift)
Definition fixed_point.h:226
unsigned int uipart() const
return integer part as unsigned int
Definition fixed_point.h:179
fixed_point operator*(const fixed_point< U, FracBits2 > &v) const
multiply two fixed point values, using long long int to store the temporary multiplication result
Definition fixed_point.h:128
unsigned int ui64part() const
return integer part as unsigned int
Definition fixed_point.h:184
fixed_point & operator-=(const fixed_point< U, FracBits2 > &v)
Definition fixed_point.h:110
fixed_point(const fixed_point< U, FracBits2 > &v)
copy constructor from any other fixed_point value
Definition fixed_point.h:48
fixed_point operator+(const fixed_point< U, FracBits2 > &v) const
Definition fixed_point.h:115
T get() const
Definition fixed_point.h:79
fixed_point & operator+=(int v)
Definition fixed_point.h:158
fixed_point & operator-=(int v)
Definition fixed_point.h:163
T fpart() const
return fractional part as 0..(2^Bits-1)
Definition fixed_point.h:195
static T one()
Definition fixed_point.h:71
U lerp_table_lookup_float(U *data) const
Definition fixed_point.h:232
T fpart() const
return fractional part as 0..(2^FracBits-1)
Definition fixed_point.h:189
fixed_point & operator*=(const fixed_point< U, FracBits2 > &v)
multiply two fixed point values, using BigType (usually 64-bit int) to store the temporary multiplica...
Definition fixed_point.h:135
U lerp_table_lookup_int(U *data) const
Definition fixed_point.h:218
static fixed_point from_base(const T &v)
Makes an instance from a representation value (ie. same type of value as is used for internal storage...
Definition fixed_point.h:64
fixed_point & operator*=(int v)
Definition fixed_point.h:168
double fpart_as_double() const
return fractional part as 0..1
Definition fixed_point.h:203
T ipart() const
return integer part
Definition fixed_point.h:174
U lerp_ptr_lookup_int(U *data) const
Definition fixed_point.h:245
U lerp_by_fract_int(U v1, U v2) const
Definition fixed_point.h:211
fixed_point(double v)
Definition fixed_point.h:59
fixed_point & operator=(const fixed_point< U, FracBits2 > &v)
Definition fixed_point.h:100
fixed_point & operator=(double v)
Definition fixed_point.h:87
fixed_point operator-(const fixed_point< U, FracBits2 > &v) const
Definition fixed_point.h:121
unsigned v[N_MAX]
Definition inflate.c:1584
static void v2(register WDL_FFT_REAL *a)
Definition fft.c:1099
#define U(x)
Definition fmopl.c:132
int int32_t
Definition mid.cpp:97
unsigned int uint32_t
Definition mid.cpp:100
Definition audio_fx.h:36
fixed_point< T, FractBits > operator*(int v, fixed_point< T, FractBits > v2)
Definition fixed_point.h:258
T sine_table< T, N, Multiplier >::data[N+1]
Definition primitives.h:442
uint32_t shr(uint32_t v, int bits=1)
Definition fixed_point.h:26
fixed_point< unsigned long long int, 24 > wpos
wave position (unsigned 64-bit int including 24-bit fractional part)
Definition fixed_point.h:264
intptr_t Bits
Definition nukedopl.h:39
int result
Definition process.c:1455