| Mozzi version v1.1.0 sound synthesis library for Arduino |

mozzi_fixmath.h
1 /*
2 * mozzi_fixmath.h
3 *
4 * Copyright 2012 Tim Barrass.
5 *
6 * This file is part of Mozzi.
7 *
8 * Mozzi is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.
9 *
10 */
11
12 #ifndef FIXEDMATH_H_
13 #define FIXEDMATH_H_
14
15 #if ARDUINO >= 100
16 #include "Arduino.h"
17 #else
18 #include "WProgram.h"
19 #endif
20
21 /**@ingroup fixmath
22 @{
23 */
24 // types
25 typedef int8_t Q0n7; /**< signed fractional number using 7 fractional bits, represents -0.5 to 0.496*/
26 typedef int8_t Q7n0; /**< ordinary old signed Q7n0 int8_t with 0 fractional bits, represents -128 to 127*/
27 typedef uint8_t Q0n8; /**< unsigned fractional number using 8 fractional bits, represents 0.0 to 0.996*/
28 typedef uint8_t Q8n0; /**< normal uint8_t with 0 fractional bits, represents 0.0 to 255.0*/
29 typedef uint16_t Q0n16; /**< unsigned fractional number using 16 fractional bits, represents 0.0 to 0.999*/
30 typedef uint32_t Q0n31; /**< signed number using 0 integer bits and 31 fractional bits, represents -0.2147483648 to 0.2147483647*/
31 typedef int16_t Q7n8; /**< signed fractional number using 7 integer bits and 8 fractional bits, represents -127.996 to 127.996*/
32 typedef uint16_t Q3n13; /**< unsigned fractional number using 3 integer bits and 13 fractional bits, represents 0 to 7.999*/
33 typedef int16_t Q1n14; /**< signed fractional number using 1 integer bit and 14 fractional bits, represents -1.999 to 1.999*/
34 typedef int16_t Q15n0; /**< signed number using 15 integer bits and 0 fractional bits, represents -2147483648 to 2147483647*/
35 typedef uint16_t Q8n8; /**< unsigned fractional number using 8 integer bits and 8 fractional bits, represents 0 to 255.996*/
36 typedef int16_t Q0n15; /**< signed fractional number using 0 integer bits and 15 fractional bits, represents -0.32768 to 0.32767*/
37 typedef uint16_t Q1n15; /**< unsigned fractional number using 1 integer bit and 15 fractional bits, represents 0 to 1.999*/
38 typedef uint16_t Q16n0; /**< unsigned number using 16 integer bits and 0 fractional bits, represents 0 to 65536.0*/
39 typedef int32_t Q23n8; /**< signed fractional number using 23 integer bits and 8 fractional bits, represents -8388607.996 to 8388607.996*/
40 typedef int32_t Q15n16; /**< signed fractional number using 15 integer bits and 16 fractional bits, represents -32767.999 to 32767.999*/
41 typedef int32_t Q31n0; /**< signed (normal int32_t int16_t) number using 31 integer bits and 0 fractional bits, represents -2147483648 to 2147483647*/
42 typedef uint32_t Q32n0; /**< unsigned (normal uint32_t int16_t) number using 32 integer bits and 0 fractional bits, represents 0 to 4294967295*/
43 typedef uint32_t Q0n32; /**< unsigned fractional number using 0 integer bits and 32 fractional bits, represents 0 to 0.999999999767169*/
44 typedef uint32_t Q8n24; /**< signed fractional number using 8 integer bits and 24 fractional bits, represents 0 to 255.999*/
45 typedef uint32_t Q24n8; /**< unsigned fractional number using 24 integer bits and 8 fractional bits, represents 0 to 16777215*/
46 typedef uint32_t Q16n16; /**< unsigned fractional number using 16 integer bits and 16 fractional bits, represents 0 to 65535.999*/
47 /** @}*/
48
49 /** @ingroup fixmath
50 @{
51 */
52 // macros to save runtime calculations for representations of 1
53 #define Q0n7_FIX1 ((Q0n7) 127) /**< 0.992 in Q0n7 format*/
54 #define Q7n8_FIX1 ((Q7n8) 256) /**< 1 in Q7n8 format*/
55 #define Q8n8_FIX1 ((Q8n8) 256) /**< 1 in Q8n8 format*/
56 #define Q23n8_FIX1 ((Q23n8) 256) /**< 1 in Q23n8 format*/
57 #define Q1n14_FIX1 ((Q1n14) 16384) /**< 1 in Q1n14 format*/
58 #define Q1n15_FIX1 ((Q1n15) 32768) /**< 1 in Q1n15 format*/
59 #define Q16n16_FIX1 ((Q16n16) 65536) /**< 1 in Q16n16 format*/
60 #define Q0n15_FIX1 ((Q0n15) 32767) /**< 0.999 in Q0n15 format*/
61 #define Q0n16_FIX1 ((Q0n16) 65535) /**< 0.999 in Q0n16 format*/
62 #define Q15n0_FIX1 ((Q15n0) 16384) /**< 1 in Q15n0 format*/
63 #define Q15n16_FIX1 ((Q15n16) 65536) /**< 1 in Q15n16 format*/
64 #define Q8n24_FIX1 ((Q8n24) 16777216) /**< 1 in Q8n24 format*/
65 #define Q0n32_FIX1 ((Q0n32) 4294967295) /**< 0.999999999767169 in Q0n32 format*/
66
67 #define Q16n16_PI ((Q16n16) 205887) /**< PI in Q16n16 format*/
68 #define Q3n13_2PI ((Q3n13) 411775) /**< 2*PI in Q3n13 format*/
69 #define Q16n16_2PI ((Q16n16) 411775) /**< 2*PI in Q16n16 format*/
70
71 #define low15bits ((Q1n15) 32767) /**< Useful for keeping the lower 15 bits of a Q1n15 number, using &*/
72 /** @}*/
73
74
75 /*
76 Type conversions: Float to Q
77
78 To convert a number from floating point to Qm.n format:
79
80 Multiply the floating point number by 2^n
81 Round to the nearest integer
82
83 Q to float
84
85 To convert a number from Qm.n format to floating point:
86
87 Convert the number to floating point as if it were an integer
88 Multiply by 2^-n
89 */
90 /** @ingroup fixmath
91 @{
92 */
93 inline
94 Q0n7 float_to_Q0n7(float a) { return static_cast<Q0n7>(a*256); } /**<Convert float to Q0n7 fix. @param a is a float*/
95
96 inline
97 Q0n8 float_to_Q0n8(float a) { return static_cast<Q0n8>(a*256); } /**<Convert float to Q0n8 fix. @param a is a float*/
98
99 inline
100 Q7n8 float_to_Q7n8(float a) { return static_cast<Q7n8>(a*256); } /**<Convert float to Q7n8 fix. @param a is a float*/
101
102 inline
103 Q8n8 float_to_Q8n8(float a) { return static_cast<Q8n8>(a*256); } /**<Convert float to Q8n8 fix. @param a is a float*/
104
105 inline
106 Q1n14 float_to_Q1n14(float a) { return static_cast<Q1n14>(a*16384); } /**<Convert float to Q1n14 fix. @param a is a float*/
107
108 inline
109 Q1n15 float_to_Q1n15(float a) { return static_cast<Q1n15>(a*32768); } /**<Convert float to Q1n15 fix. @param a is a float*/
110
111 inline
112 Q8n24 float_to_Q8n24(float a) { return static_cast<Q8n24>(a*16777216); } /**<Convert float to Q8n24 fix. @param a is a float*/
113
114 inline
115 Q23n8 float_to_Q23n8(float a) { return static_cast<Q23n8>(a*256); } /**<Convert float to Q23n8 fix. @param a is a float*/
116
117 inline
118 Q24n8 float_to_Q24n8(float a) { return static_cast<Q24n8>(a*256); } /**<Convert float to Q24n8 fix. @param a is a float*/
119
120 inline
121 Q16n16 float_to_Q16n16(float a) { return static_cast<Q16n16>(a*65536); } /**<Convert float to Q16n16 fix. @param a is a float*/
122
123 inline
124 Q0n16 float_to_Q0n16(float a) { return static_cast<Q0n16>(a*65536); } /**<Convert float to Q0n16 fix. @param a is a float*/
125
126 inline
127 Q15n16 float_to_Q15n16(float a) { return static_cast<Q15n16>(a*65536); } /**<Convert float to Q15n16 fix. @param a is a float*/
128
129 inline
130 Q1n14 Q0n7_to_Q1n14(Q0n7 a) { return (static_cast<Q1n14>(a))<<7; } /**<Convert Q0n7 int8_t to Q1n14 fix. @param a is a Q0n7 int8_t */
131
132 inline
133 Q15n16 Q0n7_to_Q15n16(Q0n7 a) { return (static_cast<Q15n16>(a))<<8; } /**<Convert Q0n7 signed int8_t to Q15n16 fix. @param a is a Q0n7 signed int8_t */
134
135 inline
136 float Q0n7_to_float(Q0n7 a) { return (static_cast<float>(a))/256; } /**<Convert Q0n7 fix to float. @param a is a Q0n7 int8_t*/
137
138 inline
139 Q1n15 Q0n8_to_Q1n15(Q0n8 a) { return (static_cast<Q1n15>(a))<<7; } /**<Convert Q0n8 uint8_t to Q1n15 fix. @param a is a Q0n8 uint8_t */
140
141 inline
142 Q8n8 Q0n8_to_Q8n8(Q0n8 a) { return (static_cast<Q8n8>(a))<<8; } /**<Convert Q0n8 uint8_t to Q8n8 fix. @param a is a Q0n8 uint8_t */
143
144 inline
145 Q8n24 Q0n8_to_Q8n24(Q0n8 a) { return (static_cast<Q8n24>(a))<<16; } /**<Convert Q0n8 uint8_t to Q8n24 fix. @param a is a Q0n8 uint8_t */
146
147 inline
148 Q24n8 Q0n8_to_Q24n8(Q0n8 a) { return (static_cast<Q24n8>(a))<<8; } /**<Convert Q0n8 uint8_t to Q24n8 fix. @param a is a Q0n8 uint8_t */
149
150 inline
151 Q15n16 Q0n8_to_Q15n16(Q0n8 a) { return (static_cast<Q15n16>(a))<<8; } /**<Convert Q0n8 uint8_t to Q15n16 fix. @param a is a Q0n8 uint8_t */
152
153 inline
154 Q16n16 Q0n8_to_Q16n16(Q0n8 a) { return (static_cast<Q16n16>(a))<<8; } /**<Convert Q0n8 uint8_t to Q16n16 fix. @param a is a Q0n8 uint8_t */
155
156 inline
157 float Q0n8_to_float(Q0n8 a) { return (static_cast<float>(a))/256; } /**<Convert Q0n8 fix to float. @param a is a Q0n8 uint8_t*/
158
159 inline
160 Q7n8 Q7n0_to_Q7n8(Q7n0 a) { return (static_cast<Q7n8>(a))<<8; } /**<Convert Q7n0 int8_t to Q7n8 fix. @param a is a int8_t*/
161
162 inline
163 Q15n16 Q7n0_to_Q15n16(Q7n0 a) { return (static_cast<Q15n16>(a))<<16; } /**<Convert Q7n0 int8_t to Q15n16 fix. @param a is a int8_t*/
164
165 inline
166 Q7n8 Q8n0_to_Q7n8(Q8n0 a) { return (static_cast<Q7n8>(a))<<8; } /**<Convert Q8n0 uint8_t to Q7n8 fix. @param a is a Q8n0 uint8_t*. Beware of overflow. */
167
168 inline
169 Q8n8 Q8n0_to_Q8n8(Q8n0 a) { return (static_cast<Q8n8>(a))<<8; } /**<Convert uint8_t to Q8n8 fix. @param a is a Q8n0 uint8_t*/
170
171 inline
172 Q15n16 Q8n0_to_Q15n16(Q8n0 a) { return (static_cast<Q15n16>(a))<<16; } /**<Convert Q8n0 uint8_t to Q15n16 fix. @param a is a Q8n0 uint8_t */
173
174 inline
175 Q16n16 Q8n0_to_Q16n16(Q8n0 a) { return (static_cast<Q16n16>(a))<<16; } /**<Convert Q8n0 uint8_t to Q16n16 fix. @param a is a Q8n0 uint8_t */
176
177 inline
178 Q7n0 Q7n8_to_Q7n0(Q7n8 a) { return static_cast<Q7n0>((Q7n8)a>>8); } /**<Convert Q7n8 fix to Q7n0. @param a is a Q7n8 int16_t*/
179
180 inline
181 Q15n16 Q7n8_to_Q15n16(Q7n8 a) { return (static_cast<Q15n16>(a))<<8; } /**<Convert Q7n8 fix to Q15n16. @param a is a Q7n8 int16_t*/
182
183 inline
184 float Q7n8_to_float(Q7n8 a) { return (static_cast<float>(a))/256; } /**<Convert Q7n8 fix to float. @param a is a Q7n8 int16_t*/
185
186 inline
187 Q8n0 Q8n8_to_Q8n0(Q8n8 a) { return static_cast<Q8n0>((Q8n8)a>>8); } /**<Convert Q8n8 fix to Q8n0 uint8_t. @param a is a Q8n8 uint16_t*/
188
189 inline
190 Q16n16 Q8n8_to_Q16n16(Q8n8 a) { return (static_cast<Q16n16>(a))<<8; } /**<Convert Q8n8 fix to Q16n16 uint32_t. @param a is a Q8n8 uint16_t*/
191
192 inline
193 float Q8n8_to_float(Q8n8 a) { return (static_cast<float>(a))/256; } /**<Convert Q8n8 fix to float. @param a is a Q8n8 uint16_t*/
194
195 inline
196 Q0n7 Q1n14_to_Q0n7(Q1n14 a) { return static_cast<Q0n7>((Q1n14)a>>7); } /**<Convert Q1n14 fixed to Q0n7 int8_t. @param a is a Q1n14 int16_t*/
197
198 inline
199 float Q1n14_to_float(Q1n14 a) { return (static_cast<float>(a))/16384; } /**<Convert fix to float. @param a is an int16_t*/
200
201 inline
202 Q0n8 Q1n15_to_Q0n8(Q1n15 a) { return static_cast<Q0n8>((Q1n15)a>>7); } /**<Convert Q1n15 fixed to Q0n8 uint8_t. Only for positive values! @param a is a Q1n15 uint16_t*/
203
204 inline
205 float Q1n15_to_float(Q1n15 a) { return (static_cast<float>(a))/32768; } /**<Convert fix to float. @param a is a Q1n15 uint16_t*/
206
207 inline
208 float Q0n16_to_float(Q0n16 a) { return (static_cast<float>(a))/65536; } /**<Convert fix to float. @param a is a Q0n16 uint16_t*/
209
210 inline
211 Q15n16 Q15n0_to_Q15n16(Q15n0 a) { return (static_cast<Q15n16>(a))<<16; } /**<Convert Q15n0 int16_t to Q15n16 fix. @param a is a Q15n0 int16_t */
212
213 inline
214 Q15n16 Q16n0_to_Q15n16(Q16n0 a) { return (static_cast<Q15n16>(a))<<16; } /**<Convert Q16n0 uint16_t to Q15n16 fix. @param a is a Q16n0 uint16_t */
215
216 inline
217 Q23n8 Q16n0_to_Q23n8(Q16n0 a) { return (static_cast<Q23n8>(a))<<8; } /**<Convert Q16n0 uint16_t to Q23n8 fixed point signed int32_t. @param a is a Q16n0 uint16_t*/
218
219 inline
220 Q24n8 Q16n0_to_Q24n8(Q16n0 a) { return (static_cast<Q24n8>(a))<<8; } /**<Convert Q16n0 uint16_t to Q24n8 fixed point uint32_t. @param a is a Q16n0 uint16_t*/
221
222 inline
223 Q16n16 Q16n0_to_Q16n16(Q16n0 a) { return (static_cast<Q16n16>(a))<<16; } /**<Convert Q16n0 uint16_t to Q16n16 fixed point uint32_t. @param a is a Q16n0 uint16_t*/
224
225 inline
226 float Q16n0_to_float(Q16n0 a) { return (static_cast<float>(a)); } /**<Convert Q16n0 uint16_t to float. @param a is a Q16n0 uint16_t*/
227
228 inline
229 Q0n8 Q8n24_to_Q0n8(Q8n24 a) { return static_cast<Q0n8>((Q8n24)a>>16); } /**<Convert Q8n24 fixed to Q0n8 uint8_t. @param a is a Q8n24 uint32_t*/
230
231 inline
232 float Q8n24_to_float(Q8n24 a) { return (static_cast<float>(a))/16777216; } /**<Convert fix to float. @param a is a Q8n24 uint32_t*/
233
234
235 inline
236 Q31n0 Q23n8_to_Q31n0(Q23n8 a) { return static_cast<Q31n0>((Q23n8)a>>8); } /**<Convert Q23n8 fixed to Q31n0 int32_t. @param a is a Q23n8 int32_t*/
237
238 inline
239 Q16n0 Q23n8_to_Q16n0(Q23n8 a) { return static_cast<Q16n0>((Q23n8)a>>8); } /**<Convert Q23n8 fixed to Q16n0 uint16_t. Positive values only. @param a is a Q23n8 int32_t*/
240
241 inline
242 Q15n0 Q23n8_to_Q15n0(Q23n8 a) { return static_cast<Q15n0>((Q23n8)a>>8); } /**<Convert Q23n8 fixed to Q15n0 signed int16_t. @param a is a Q23n8 int32_t*/
243
244 inline
245 Q7n8 Q23n8_to_Q7n8(Q23n8 a) { return static_cast<Q7n8>(a); } /**<Convert Q23n8 fixed to Q7n8 signed int16_t, losing most significant bits. @param a is a Q23n8 signed int32_t.*/
246
247
248 inline
249 float Q23n8_to_float(Q23n8 a) { return (static_cast<float>(a))/256; } /**<Convert fix to float. @param a is a Q23n8 signed int32_t*/
250
251 inline
252 Q0n8 Q24n8_to_Q0n8(Q24n8 a) { return static_cast<Q0n8>(a); } /**<Convert Q24n8 fixed to Q0n8 uint8_t. @param a is a Q24n8 uint32_t*/
253
254 inline
255 Q16n16 Q24n8_to_Q16n0(Q24n8 a) { return (static_cast<Q16n0>((Q24n8)a))>>8; } /**<Convert Q24n8 fixed to Q16n0 uint16_t. @param a is a Q24n8 uint32_t*/
256
257 inline
258 Q32n0 Q24n8_to_Q32n0(Q24n8 a) { return static_cast<Q32n0>((Q24n8)a>>8); } /**<Convert Q24n8 fixed to Q32n0 uint32_t. @param a is a Q24n8 uint32_t*/
259
260 inline
261 Q16n16 Q24n8_to_Q16n16(Q24n8 a) { return (static_cast<Q16n16>(a))<<8; } /**<Convert Q24n8 fixed to Q16n16 uint32_t. @param a is a Q24n8 uint32_t*/
262
263 inline
264 float Q24n8_to_float(Q24n8 a) { return (static_cast<float>(a))/256; } /**<Convert fix to float. @param a is a Q24n8 uint32_t*/
265
266 inline
267 Q0n8 Q15n16_to_Q0n8(Q15n16 a) { return static_cast<Q0n8>((Q15n16)a>>8); } /**<Convert Q15n16 fixed to Q0n8 uint8_t. Only for positive values! @param a is a Q15n16 signed int32_t*/
268
269 inline
270 Q8n0 Q15n16_to_Q8n0(Q15n16 a) { return static_cast<Q8n0>((Q15n16)a>>16); } /**<Convert Q15n16 fixed to Q8n0 uint8_t. Only for positive values! @param a is a Q15n16 signed int32_t*/
271
272 inline
273 Q15n0 Q15n16_to_Q15n0(Q15n16 a) { return static_cast<Q15n0>((Q15n16)a>>16); } /**<Convert Q15n16 fixed to Q15n0 signed int16_t. @param a is a Q15n16 signed int32_t*/
274
275 inline
276 Q7n8 Q15n16_to_Q7n8(Q15n16 a) { return static_cast<Q7n8>((Q15n16)a>>8); } /**<Convert Q15n16 fixed to Q7n8 signed int16_t, keeping middle bits only. @param a is a Q15n16 signed int32_t.*/
277
278 inline
279 Q8n8 Q15n16_to_Q8n8(Q15n16 a) { return static_cast<Q8n8>((Q15n16)a>>8); } /**<Convert Q15n16 fixed to Q8n8 signed int16_t, keeping middle bits only. @param a is a Q15n16 signed int32_t.*/
280
281 inline
282 Q23n8 Q15n16_to_Q23n8(Q15n16 a) { return static_cast<Q23n8>((Q15n16)a>>8); } /**<Convert Q15n16 fixed to Q23n8 signed int32_t. @param a is a Q15n16 signed int32_t.*/
283
284 inline
285 float Q15n16_to_float(Q15n16 a) { return (static_cast<float>(a))/65536; } /**<Convert fix to float. @param a is a Q15n16 signed int32_t*/
286
287 inline
288 Q0n8 Q16n16_to_Q0n8(Q16n16 a) { return static_cast<Q0n8>((Q16n16)a>>8); } /**<Convert Q16n16 fixed to Q0n8 uint8_t. @param a is a Q16n16 uint32_t*/
289
290 inline
291 Q8n8 Q16n16_to_Q8n8(Q8n8 a) { return static_cast<Q8n8>((Q16n16)a>>16); } /**<Convert Q16n16 fixed to Q8n8 uint16_t. @param a is a Q16n16 uint32_t*/
292
293 inline
294 Q16n0 Q16n16_to_Q16n0(Q16n16 a) { return static_cast<Q16n0>((Q16n16)a>>16); } /**<Convert Q16n16 fixed to Q16n0 uint16_t. @param a is a Q16n16 uint32_t*/
295
296 inline
297 Q24n8 Q16n16_to_Q24n8(Q16n16 a) { return static_cast<Q24n8>((Q16n16)a>>8); } /**<Convert Q16n16 fixed to Q24n8 uint32_t. @param a is a Q16n16 uint32_t*/
298
299 inline
300 float Q16n16_to_float(Q16n16 a) { return (static_cast<float>(a))/65536; } /**<Convert fix to float. @param a is a Q16n16 uint32_t*/
301 /** @}*/
302
303 /* @ingroup fixmath
304 Fast (?) fixed point multiply for Q7n8 fractional numbers.
305 The c version below is 3 times faster, and not subject to the same overflow limitations (+-3.99, or +-2048)
306 @param a Q7n8 format multiplicand
307 @param b Q7n8 format multiplier
308 @return a Q7n8 format product
309 */
310 /*
311 #define Q7n8_mult(a,b) \
312 ({ \
313 int16_t prod, val1=a, val2=b ; \
314 __asm__ __volatile__ ( \
315 "muls %B1, %B2 \n\t" \
316 "mov %B0, r0 \n\t" \
317 "mul %A1, %A2\n\t" \
318 "mov %A0, r1 \n\t" \
319 "mulsu %B1, %A2 \n\t" \
320 "add %A0, r0 \n\t" \
321 "adc %B0, r1 \n\t" \
322 "mulsu %B2, %A1 \n\t" \
323 "add %A0, r0 \n\t" \
324 "adc %B0, r1 \n\t" \
325 "clr r1 \n\t" \
326 : "=&d" (prod) \
327 : "a" (val1), "a" (val2) \
328 ); \
329 prod; \
330 })
331 */
332
333 /** @ingroup fixmath
334 Fast fixed point multiply for Q7n8 fractional numbers.
335 @param a Q7n8 format multiplicand
336 @param b Q7n8 format multiplier
337 @return a Q7n8 format product
338 */
339 inline
340 Q7n8 Q7n8_mult(Q7n8 a, Q7n8 b) {
341 return ((int16_t)((((int32_t)(a))*(b))>>8));
342 }
343
344
345 /*
346 #define FMULS8(v1, v2) \
347 ({ \
348 uint8_t res; \
349 uint8_t val1 = v1; \
350 uint8_t val2 = v2; \
351 __asm__ __volatile__ \
352 ( \
353 "fmuls $1, $2" "\n\t" \
354 "mov $0, r1" "\n\t" \
355 "clr r1" "\n\t" \
356 : "=&d" (res) \
357 : "a" (val1), "a" (val2) \
358 ); \
359 res; \
360 }) */
361
362
363 /*
364 // from octosynth, Joe Marshall 2011:
365
366 // multiply 2 16 bit numbers together and shift 8 without precision loss
367 // requires assembler really
368 volatile uint8_t zeroReg=0;
369 volatile uint16_t multipliedCounter=oscillators[c].phaseStep;
370 asm volatile
371 (
372 // high uint8_ts mult together = high uint8_t
373 "ldi %A[outVal],0" "\n\t"
374 "mul %B[phaseStep],%B[pitchB}]" "\n\t"
375 "mov %B[outVal],r0" "\n\t"
376 // ignore overflow into r1 (should never overflow)
377 // low uint8_t * high uint8_t -> both uint8_ts
378 "mul %A[phaseStep],%B[pitchB}]" "\n\t"
379 "add %A[outVal],r0" "\n\t"
380 // carry into high uint8_t
381 "adc %B[outVal],r1" "\n\t"
382 // high uint8_t* low uint8_t -> both uint8_ts
383 "mul %B[phaseStep],%A[pitchB}]" "\n\t"
384 "add %A[outVal],r0" "\n\t"
385 // carry into high uint8_t
386 "adc %B[outVal],r1" "\n\t"
387 // low uint8_t * low uint8_t -> round
388 "mul %A[phaseStep],%A[pitchB}]" "\n\t"
389 // the adc below is to round up based on high bit of low*low:
390 "adc %A[outVal],r1" "\n\t"
391 "adc %B[outVal],%[ZERO]" "\n\t"
392 "clr r1" "\n\t"
393 :[outVal] "=&d" (multipliedCounter)
394 :[phaseStep] "d" (oscillators[c].phaseStep),[pitchB}] "d"( pitchB}Multiplier),[ZERO] "d" (zeroReg)
395 :"r1","r0"
396 );
397 oscillators[c].phaseStep=multipliedCounter;
398
399 */
400
401
402
403 int16_t ipow(int16_t base, int16_t exp); /**< dangerous overflow-prone int16_t power function */
404
405 Q16n16 Q16n16_pow2(Q8n8 exponent);
406
407 uint8_t uint8_tMod(uint8_t n, uint8_t d);
408 uint8_t uint8_tDiv(uint8_t n, uint8_t d);
409 uint8_t uint8_tRnd(uint8_t min, uint8_t max);
410 uint16_t isqrt16(uint16_t n);
411 uint32_t isqrt32(uint32_t n);
412
413 #endif /* FIXEDMATH_H_ */
Q24n8_to_Q0n8
Q0n8 Q24n8_to_Q0n8(Q24n8 a)
Convert Q24n8 fixed to Q0n8 uint8_t.
Definition: mozzi_fixmath.h:252
Q16n0_to_float
float Q16n0_to_float(Q16n0 a)
Convert Q16n0 uint16_t to float.
Definition: mozzi_fixmath.h:226
Q16n16_to_Q8n8
Q8n8 Q16n16_to_Q8n8(Q8n8 a)
Convert Q16n16 fixed to Q8n8 uint16_t.
Definition: mozzi_fixmath.h:291
Q15n16_to_Q23n8
Q23n8 Q15n16_to_Q23n8(Q15n16 a)
Convert Q15n16 fixed to Q23n8 signed int32_t.
Definition: mozzi_fixmath.h:282
Q8n0_to_Q7n8
Q7n8 Q8n0_to_Q7n8(Q8n0 a)
Convert Q8n0 uint8_t to Q7n8 fix.
Definition: mozzi_fixmath.h:166
Q24n8_to_float
float Q24n8_to_float(Q24n8 a)
Convert fix to float.
Definition: mozzi_fixmath.h:264
Q16n0_to_Q16n16
Q16n16 Q16n0_to_Q16n16(Q16n0 a)
Convert Q16n0 uint16_t to Q16n16 fixed point uint32_t.
Definition: mozzi_fixmath.h:223
float_to_Q0n7
Q0n7 float_to_Q0n7(float a)
Convert float to Q0n7 fix.
Definition: mozzi_fixmath.h:94
Q0n7
int8_t Q0n7
signed fractional number using 7 fractional bits, represents -0.5 to 0.496
Definition: mozzi_fixmath.h:25
Q16n16_to_Q0n8
Q0n8 Q16n16_to_Q0n8(Q16n16 a)
Convert Q16n16 fixed to Q0n8 uint8_t.
Definition: mozzi_fixmath.h:288
Q15n16_to_Q15n0
Q15n0 Q15n16_to_Q15n0(Q15n16 a)
Convert Q15n16 fixed to Q15n0 signed int16_t.
Definition: mozzi_fixmath.h:273
Q15n16
int32_t Q15n16
signed fractional number using 15 integer bits and 16 fractional bits, represents -32767...
Definition: mozzi_fixmath.h:40
Q0n8_to_Q15n16
Q15n16 Q0n8_to_Q15n16(Q0n8 a)
Convert Q0n8 uint8_t to Q15n16 fix.
Definition: mozzi_fixmath.h:151
float_to_Q8n24
Q8n24 float_to_Q8n24(float a)
Convert float to Q8n24 fix.
Definition: mozzi_fixmath.h:112
float_to_Q1n15
Q1n15 float_to_Q1n15(float a)
Convert float to Q1n15 fix.
Definition: mozzi_fixmath.h:109
float_to_Q1n14
Q1n14 float_to_Q1n14(float a)
Convert float to Q1n14 fix.
Definition: mozzi_fixmath.h:106
Q7n0
int8_t Q7n0
ordinary old signed Q7n0 int8_t with 0 fractional bits, represents -128 to 127
Definition: mozzi_fixmath.h:26
Q8n8
uint16_t Q8n8
unsigned fractional number using 8 integer bits and 8 fractional bits, represents 0 to 255...
Definition: mozzi_fixmath.h:35
Q7n8_to_Q15n16
Q15n16 Q7n8_to_Q15n16(Q7n8 a)
Convert Q7n8 fix to Q15n16.
Definition: mozzi_fixmath.h:181
Q32n0
uint32_t Q32n0
unsigned (normal uint32_t int16_t) number using 32 integer bits and 0 fractional bits, represents 0 to 4294967295
Definition: mozzi_fixmath.h:42
float_to_Q15n16
Q15n16 float_to_Q15n16(float a)
Convert float to Q15n16 fix.
Definition: mozzi_fixmath.h:127
Q0n8_to_float
float Q0n8_to_float(Q0n8 a)
Convert Q0n8 fix to float.
Definition: mozzi_fixmath.h:157
Q8n24_to_float
float Q8n24_to_float(Q8n24 a)
Convert fix to float.
Definition: mozzi_fixmath.h:232
Q1n14_to_Q0n7
Q0n7 Q1n14_to_Q0n7(Q1n14 a)
Convert Q1n14 fixed to Q0n7 int8_t.
Definition: mozzi_fixmath.h:196
Q8n8_to_float
float Q8n8_to_float(Q8n8 a)
Convert Q8n8 fix to float.
Definition: mozzi_fixmath.h:193
Q16n0_to_Q15n16
Q15n16 Q16n0_to_Q15n16(Q16n0 a)
Convert Q16n0 uint16_t to Q15n16 fix.
Definition: mozzi_fixmath.h:214
Q15n16_to_Q7n8
Q7n8 Q15n16_to_Q7n8(Q15n16 a)
Convert Q15n16 fixed to Q7n8 signed int16_t, keeping middle bits only.
Definition: mozzi_fixmath.h:276
Q16n0_to_Q24n8
Q24n8 Q16n0_to_Q24n8(Q16n0 a)
Convert Q16n0 uint16_t to Q24n8 fixed point uint32_t.
Definition: mozzi_fixmath.h:220
Q24n8_to_Q16n0
Q16n16 Q24n8_to_Q16n0(Q24n8 a)
Convert Q24n8 fixed to Q16n0 uint16_t.
Definition: mozzi_fixmath.h:255
Q15n16_to_Q8n0
Q8n0 Q15n16_to_Q8n0(Q15n16 a)
Convert Q15n16 fixed to Q8n0 uint8_t.
Definition: mozzi_fixmath.h:270
Q7n8_to_Q7n0
Q7n0 Q7n8_to_Q7n0(Q7n8 a)
Convert Q7n8 fix to Q7n0.
Definition: mozzi_fixmath.h:178
Q15n0_to_Q15n16
Q15n16 Q15n0_to_Q15n16(Q15n0 a)
Convert Q15n0 int16_t to Q15n16 fix.
Definition: mozzi_fixmath.h:211
Q0n7_to_Q15n16
Q15n16 Q0n7_to_Q15n16(Q0n7 a)
Convert Q0n7 signed int8_t to Q15n16 fix.
Definition: mozzi_fixmath.h:133
Q0n8_to_Q8n24
Q8n24 Q0n8_to_Q8n24(Q0n8 a)
Convert Q0n8 uint8_t to Q8n24 fix.
Definition: mozzi_fixmath.h:145
Q16n0_to_Q23n8
Q23n8 Q16n0_to_Q23n8(Q16n0 a)
Convert Q16n0 uint16_t to Q23n8 fixed point signed int32_t.
Definition: mozzi_fixmath.h:217
Q7n8_mult
Q7n8 Q7n8_mult(Q7n8 a, Q7n8 b)
Fast fixed point multiply for Q7n8 fractional numbers.
Definition: mozzi_fixmath.h:340
Q1n14
int16_t Q1n14
signed fractional number using 1 integer bit and 14 fractional bits, represents -1.999 to 1.999
Definition: mozzi_fixmath.h:33
Q24n8_to_Q32n0
Q32n0 Q24n8_to_Q32n0(Q24n8 a)
Convert Q24n8 fixed to Q32n0 uint32_t.
Definition: mozzi_fixmath.h:258
float_to_Q16n16
Q16n16 float_to_Q16n16(float a)
Convert float to Q16n16 fix.
Definition: mozzi_fixmath.h:121
Q16n16_to_float
float Q16n16_to_float(Q16n16 a)
Convert fix to float.
Definition: mozzi_fixmath.h:300
Q0n32
uint32_t Q0n32
unsigned fractional number using 0 integer bits and 32 fractional bits, represents 0 to 0...
Definition: mozzi_fixmath.h:43
Q15n16_to_Q0n8
Q0n8 Q15n16_to_Q0n8(Q15n16 a)
Convert Q15n16 fixed to Q0n8 uint8_t.
Definition: mozzi_fixmath.h:267
Q24n8_to_Q16n16
Q16n16 Q24n8_to_Q16n16(Q24n8 a)
Convert Q24n8 fixed to Q16n16 uint32_t.
Definition: mozzi_fixmath.h:261
Q31n0
int32_t Q31n0
signed (normal int32_t int16_t) number using 31 integer bits and 0 fractional bits, represents -2147483648 to 2147483647
Definition: mozzi_fixmath.h:41
Q23n8_to_Q31n0
Q31n0 Q23n8_to_Q31n0(Q23n8 a)
Convert Q23n8 fixed to Q31n0 int32_t.
Definition: mozzi_fixmath.h:236
Q7n0_to_Q15n16
Q15n16 Q7n0_to_Q15n16(Q7n0 a)
Convert Q7n0 int8_t to Q15n16 fix.
Definition: mozzi_fixmath.h:163
Q0n31
uint32_t Q0n31
signed number using 0 integer bits and 31 fractional bits, represents -0.2147483648 to 0...
Definition: mozzi_fixmath.h:30
Q0n7_to_float
float Q0n7_to_float(Q0n7 a)
Convert Q0n7 fix to float.
Definition: mozzi_fixmath.h:136
float_to_Q8n8
Q8n8 float_to_Q8n8(float a)
Convert float to Q8n8 fix.
Definition: mozzi_fixmath.h:103
Q8n0_to_Q15n16
Q15n16 Q8n0_to_Q15n16(Q8n0 a)
Convert Q8n0 uint8_t to Q15n16 fix.
Definition: mozzi_fixmath.h:172
Q16n0
uint16_t Q16n0
unsigned number using 16 integer bits and 0 fractional bits, represents 0 to 65536.0
Definition: mozzi_fixmath.h:38
Q0n15
int16_t Q0n15
signed fractional number using 0 integer bits and 15 fractional bits, represents -0.32768 to 0.32767
Definition: mozzi_fixmath.h:36
Q23n8_to_float
float Q23n8_to_float(Q23n8 a)
Convert fix to float.
Definition: mozzi_fixmath.h:249
Q7n0_to_Q7n8
Q7n8 Q7n0_to_Q7n8(Q7n0 a)
Convert Q7n0 int8_t to Q7n8 fix.
Definition: mozzi_fixmath.h:160
Q3n13
uint16_t Q3n13
unsigned fractional number using 3 integer bits and 13 fractional bits, represents 0 to 7...
Definition: mozzi_fixmath.h:32
float_to_Q0n8
Q0n8 float_to_Q0n8(float a)
Convert float to Q0n8 fix.
Definition: mozzi_fixmath.h:97
float_to_Q23n8
Q23n8 float_to_Q23n8(float a)
Convert float to Q23n8 fix.
Definition: mozzi_fixmath.h:115
Q0n16
uint16_t Q0n16
unsigned fractional number using 16 fractional bits, represents 0.0 to 0.999
Definition: mozzi_fixmath.h:29
Q16n16_to_Q24n8
Q24n8 Q16n16_to_Q24n8(Q16n16 a)
Convert Q16n16 fixed to Q24n8 uint32_t.
Definition: mozzi_fixmath.h:297
Q0n16_to_float
float Q0n16_to_float(Q0n16 a)
Convert fix to float.
Definition: mozzi_fixmath.h:208
Q8n8_to_Q8n0
Q8n0 Q8n8_to_Q8n0(Q8n8 a)
Convert Q8n8 fix to Q8n0 uint8_t.
Definition: mozzi_fixmath.h:187
Q1n14_to_float
float Q1n14_to_float(Q1n14 a)
Convert fix to float.
Definition: mozzi_fixmath.h:199
Q15n16_to_float
float Q15n16_to_float(Q15n16 a)
Convert fix to float.
Definition: mozzi_fixmath.h:285
Q1n15_to_float
float Q1n15_to_float(Q1n15 a)
Convert fix to float.
Definition: mozzi_fixmath.h:205
float_to_Q7n8
Q7n8 float_to_Q7n8(float a)
Convert float to Q7n8 fix.
Definition: mozzi_fixmath.h:100
uint8_tMod
uint8_t uint8_tMod(uint8_t n, uint8_t d)
fast uint8_t modulus
Definition: mozzi_fixmath.cpp:25
Q1n15
uint16_t Q1n15
unsigned fractional number using 1 integer bit and 15 fractional bits, represents 0 to 1...
Definition: mozzi_fixmath.h:37
Q0n8_to_Q1n15
Q1n15 Q0n8_to_Q1n15(Q0n8 a)
Convert Q0n8 uint8_t to Q1n15 fix.
Definition: mozzi_fixmath.h:139
Q0n8_to_Q16n16
Q16n16 Q0n8_to_Q16n16(Q0n8 a)
Convert Q0n8 uint8_t to Q16n16 fix.
Definition: mozzi_fixmath.h:154
Q24n8
uint32_t Q24n8
unsigned fractional number using 24 integer bits and 8 fractional bits, represents 0 to 16777215 ...
Definition: mozzi_fixmath.h:45
Q0n8_to_Q8n8
Q8n8 Q0n8_to_Q8n8(Q0n8 a)
Convert Q0n8 uint8_t to Q8n8 fix.
Definition: mozzi_fixmath.h:142
Q16n16_to_Q16n0
Q16n0 Q16n16_to_Q16n0(Q16n16 a)
Convert Q16n16 fixed to Q16n0 uint16_t.
Definition: mozzi_fixmath.h:294
Q0n8_to_Q24n8
Q24n8 Q0n8_to_Q24n8(Q0n8 a)
Convert Q0n8 uint8_t to Q24n8 fix.
Definition: mozzi_fixmath.h:148
Q0n7_to_Q1n14
Q1n14 Q0n7_to_Q1n14(Q0n7 a)
Convert Q0n7 int8_t to Q1n14 fix.
Definition: mozzi_fixmath.h:130
Q15n0
int16_t Q15n0
signed number using 15 integer bits and 0 fractional bits, represents -2147483648 to 2147483647 ...
Definition: mozzi_fixmath.h:34
Q16n16
uint32_t Q16n16
unsigned fractional number using 16 integer bits and 16 fractional bits, represents 0 to 65535...
Definition: mozzi_fixmath.h:46
float_to_Q24n8
Q24n8 float_to_Q24n8(float a)
Convert float to Q24n8 fix.
Definition: mozzi_fixmath.h:118
Q8n0_to_Q8n8
Q8n8 Q8n0_to_Q8n8(Q8n0 a)
Convert uint8_t to Q8n8 fix.
Definition: mozzi_fixmath.h:169
float_to_Q0n16
Q0n16 float_to_Q0n16(float a)
Convert float to Q0n16 fix.
Definition: mozzi_fixmath.h:124
Q7n8
int16_t Q7n8
signed fractional number using 7 integer bits and 8 fractional bits, represents -127.996 to 127.996
Definition: mozzi_fixmath.h:31
Q8n0
uint8_t Q8n0
normal uint8_t with 0 fractional bits, represents 0.0 to 255.0
Definition: mozzi_fixmath.h:28
Q8n0_to_Q16n16
Q16n16 Q8n0_to_Q16n16(Q8n0 a)
Convert Q8n0 uint8_t to Q16n16 fix.
Definition: mozzi_fixmath.h:175
Q1n15_to_Q0n8
Q0n8 Q1n15_to_Q0n8(Q1n15 a)
Convert Q1n15 fixed to Q0n8 uint8_t.
Definition: mozzi_fixmath.h:202
Q8n24
uint32_t Q8n24
signed fractional number using 8 integer bits and 24 fractional bits, represents 0 to 255...
Definition: mozzi_fixmath.h:44
Q0n8
uint8_t Q0n8
unsigned fractional number using 8 fractional bits, represents 0.0 to 0.996
Definition: mozzi_fixmath.h:27
Q23n8_to_Q16n0
Q16n0 Q23n8_to_Q16n0(Q23n8 a)
Convert Q23n8 fixed to Q16n0 uint16_t.
Definition: mozzi_fixmath.h:239
Q23n8_to_Q15n0
Q15n0 Q23n8_to_Q15n0(Q23n8 a)
Convert Q23n8 fixed to Q15n0 signed int16_t.
Definition: mozzi_fixmath.h:242
Q8n24_to_Q0n8
Q0n8 Q8n24_to_Q0n8(Q8n24 a)
Convert Q8n24 fixed to Q0n8 uint8_t.
Definition: mozzi_fixmath.h:229
Q23n8
int32_t Q23n8
signed fractional number using 23 integer bits and 8 fractional bits, represents -8388607.996 to 8388607.996
Definition: mozzi_fixmath.h:39
Q8n8_to_Q16n16
Q16n16 Q8n8_to_Q16n16(Q8n8 a)
Convert Q8n8 fix to Q16n16 uint32_t.
Definition: mozzi_fixmath.h:190
Q15n16_to_Q8n8
Q8n8 Q15n16_to_Q8n8(Q15n16 a)
Convert Q15n16 fixed to Q8n8 signed int16_t, keeping middle bits only.
Definition: mozzi_fixmath.h:279
Q23n8_to_Q7n8
Q7n8 Q23n8_to_Q7n8(Q23n8 a)
Convert Q23n8 fixed to Q7n8 signed int16_t, losing most significant bits.
Definition: mozzi_fixmath.h:245
Q7n8_to_float
float Q7n8_to_float(Q7n8 a)
Convert Q7n8 fix to float.
Definition: mozzi_fixmath.h:184