forked from pichenettes/avril
-
Notifications
You must be signed in to change notification settings - Fork 0
/
op.h
executable file
·594 lines (530 loc) · 15.4 KB
/
op.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
// Copyright 2009 Olivier Gillet.
//
// Author: Olivier Gillet ([email protected])
//
// 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 3 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, see <http://www.gnu.org/licenses/>.
//
// -----------------------------------------------------------------------------
//
// A set of basic operands, especially useful for fixed-point arithmetic, with
// fast ASM implementations.
#ifndef AVRLIB_OP_H_
#define AVRLIB_OP_H_
#define USE_OPTIMIZED_OP
#include <avr/pgmspace.h>
#include "avrlib/base.h"
namespace avrlib {
static inline int16_t Clip(int16_t value, int16_t min, int16_t max) {
return value < min ? min : (value > max ? max : value);
}
static inline int16_t Clip14(int16_t value) {
uint8_t msb = static_cast<uint16_t>(value) >> 8;
if (msb & 0x80) {
return 0;
} if (msb & 0x40) {
return 16383;
}
return value;
}
static inline uint8_t AddClip(uint8_t value, uint8_t increment, uint8_t max) {
value += increment;
if (value > max) {
value = max;
}
return value;
}
// Optimized for 0-16384 range.
static inline uint8_t ShiftRight8(int16_t value) {
return static_cast<uint16_t>(value) >> 8;
}
#ifdef USE_OPTIMIZED_OP
static inline uint24c_t Add24Carry(uint24_t a, uint24_t b) {
uint16_t a_int = a.integral;
uint16_t b_int = b.integral;
uint8_t a_frac = a.fractional;
uint8_t b_frac = b.fractional;
uint8_t a_carry = 0;
uint24c_t result;
asm(
"add %0, %6" "\n\t"
"adc %A1, %A7" "\n\t"
"adc %B1, %B7" "\n\t"
"adc %2, r1" "\n\t"
: "=r" (a_frac), "=r" (a_int), "=r" (a_carry)
: "0" (a_frac), "1" (a_int), "2" (a_carry), "a" (b_frac), "a" (b_int)
);
result.integral = a_int;
result.fractional = a_frac;
result.carry = a_carry;
return result;
}
static inline uint24_t Add24(uint24_t a, uint24_t b) {
uint16_t a_int = a.integral;
uint16_t b_int = b.integral;
uint8_t a_frac = a.fractional;
uint8_t b_frac = b.fractional;
uint24_t result;
asm(
"add %0, %4" "\n\t"
"adc %A1, %A5" "\n\t"
"adc %B1, %B5" "\n\t"
: "=r" (a_frac), "=r" (a_int)
: "0" (a_frac), "1" (a_int), "a" (b_frac), "a" (b_int)
);
result.integral = a_int;
result.fractional = a_frac;
return result;
}
static inline uint24_t Lsr24(uint24_t a) {
uint16_t a_int = a.integral;
uint8_t a_frac = a.fractional;
uint24_t result;
asm(
"lsr %B1" "\n\t"
"ror %A1" "\n\t"
"ror %0" "\n\t"
: "=r" (a_frac), "=r" (a_int)
: "0" (a_frac), "1" (a_int)
);
result.integral = a_int;
result.fractional = a_frac;
return result;
}
static inline uint24_t Lsl24(uint24_t a) {
uint16_t a_int = a.integral;
uint8_t a_frac = a.fractional;
uint24_t result;
asm(
"lsl %0" "\n\t"
"rol %A1" "\n\t"
"rol %B1" "\n\t"
: "=r" (a_frac), "=r" (a_int)
: "0" (a_frac), "1" (a_int)
);
result.integral = a_int;
result.fractional = a_frac;
return result;
}
static inline uint8_t Clip8(int16_t value) {
uint8_t result;
asm(
"mov %0, %A1" "\n\t" // by default, copy the value.
"or %B1, %B1" "\n\t" // load H to set flags.
"brpl .+4" "\n\t" // if positive, skip
"ldi %0, 0" "\n\t" // set to 0.
"rjmp .+4" "\n\t" // and jump.
"breq .+2" "\n\t" // if null, skip
"ldi %0, 255" "\n\t" // set to 255
: "=r" (result)
: "a" (value)
);
return result;
}
static inline int8_t SignedClip8(int16_t value) {
return Clip8(value + 128) + 128;
}
static inline uint8_t Mix(uint8_t a, uint8_t b, uint8_t balance) {
Word sum;
asm(
"mul %3, %2" "\n\t" // b * balance
"movw %A0, r0" "\n\t" // to sum
"com %2" "\n\t" // 255 - balance
"mul %1, %2" "\n\t" // a * (255 - balance)
"com %2" "\n\t" // reset balance to its previous value
"add %A0, r0" "\n\t" // add to sum L
"adc %B0, r1" "\n\t" // add to sum H
"eor r1, r1" "\n\t" // reset r1 after multiplication
: "&=r" (sum)
: "a" (a), "a" (balance), "a" (b)
);
return sum.bytes[1];
}
static inline uint8_t Mix(
uint8_t a, uint8_t b,
uint8_t gain_a, uint8_t gain_b) {
Word sum;
asm(
"mul %3, %4" "\n\t" // b * gain_b
"movw %A0, r0" "\n\t" // to sum
"mul %1, %2" "\n\t" // a * gain_a
"add %A0, r0" "\n\t" // add to sum L
"adc %B0, r1" "\n\t" // add to sum H
"eor r1, r1" "\n\t" // reset r1 after multiplication
: "&=r" (sum)
: "a" (a), "a" (gain_a), "a" (b), "a" (gain_b)
);
return sum.bytes[1];
}
static inline int8_t SignedMix(
int8_t a, int8_t b,
uint8_t gain_a, uint8_t gain_b) {
Word sum;
asm(
"mulsu %3, %4" "\n\t" // b * gain_b
"movw %A0, r0" "\n\t" // to sum
"mulsu %1, %2" "\n\t" // a * gain_a
"add %A0, r0" "\n\t" // add to sum L
"adc %B0, r1" "\n\t" // add to sum H
"eor r1, r1" "\n\t" // reset r1 after multiplication
: "&=r" (sum)
: "a" (a), "a" (gain_a), "a" (b), "a" (gain_b)
);
return sum.bytes[1];
}
static inline uint16_t Mix16(uint8_t a, uint8_t b, uint8_t balance) {
Word sum;
asm(
"mul %3, %2" "\n\t" // b * balance
"movw %A0, r0" "\n\t" // to sum
"com %2" "\n\t" // 255 - balance
"mul %1, %2" "\n\t" // a * (255 - balance)
"com %2" "\n\t" // reset balance to its previous value
"add %A0, r0" "\n\t" // add to sum L
"adc %B0, r1" "\n\t" // add to sum H
"eor r1, r1" "\n\t" // reset r1 after multiplication
: "&=r" (sum)
: "a" (a), "a" (balance), "a" (b)
);
return sum.value;
}
static inline uint8_t Mix4(uint8_t a, uint8_t b, uint8_t balance) {
uint16_t sum;
asm(
"mul %2, %1" "\n\t" // b * balance
"movw %A3, r0" "\n\t" // to sum
"com %1" "\n\t" // 255 - balance
"subi %1, 240" "\n\t" // 15 - balance
"mul %0, %1" "\n\t" // a * (15 - balance)
"subi %1, 16" "\n\t"
"com %1" "\n\t" // reset balance to its previous value
"add %A3, r0" "\n\t" // add to sum L
"adc %B3, r1" "\n\t" // add to sum H
"eor r1, r1" "\n\t" // reset r1 after multiplication
"andi %B3, 15" "\n\t" // keep 4 lowest bits of H
"andi %A3, 240" "\n\t" // keep 4 highest bits of L
"or %B3, %A3" "\n\t" // copy 4 high bits of L to H -> LLLLHHHH
"swap %B3" "\n\t" // swap to get HHHHLLLL
"mov %0, %B3" "\n\t" // move to output
: "=r" (a)
: "a" (balance), "a" (b), "a" (sum)
);
return a;
}
static inline uint16_t UnscaledMix4(uint8_t a, uint8_t b, uint8_t balance) {
uint16_t sum;
asm(
"mul %3, %2" "\n\t" // b * balance
"movw %A0, r0" "\n\t" // to sum
"com %2" "\n\t" // 255 - balance
"subi %2, 240" "\n\t" // 15 - balance
"mul %1, %2" "\n\t" // a * (15 - balance)
"subi %2, 16" "\n\t"
"com %2" "\n\t" // reset balance to its previous value
"add %A0, r0" "\n\t" // add to sum L
"adc %B0, r1" "\n\t" // add to sum H
"eor r1, r1" "\n\t" // reset r1 after multiplication
: "&=r" (sum)
: "a" (a), "a" (balance), "a" (b)
);
return sum;
}
static inline uint8_t ShiftLeft4(uint8_t a) {
uint8_t result;
asm(
"mov %0, %1" "\n\t"
"swap %0" "\n\t"
"andi %0, 240" "\n\t"
: "=r" (result)
: "a" (a)
);
return result;
}
static inline uint8_t Swap4(uint8_t a) {
uint8_t result;
asm(
"mov %0, %1" "\n\t"
"swap %0" "\n\t"
: "=r" (result)
: "a" (a)
);
return result;
}
static inline uint8_t ShiftRight4(uint8_t a) {
uint8_t result;
asm(
"mov %0, %1" "\n\t"
"swap %0" "\n\t"
"andi %0, 15" "\n\t"
: "=r" (result)
: "a" (a)
);
return result;
}
static inline uint16_t To12Bits(uint16_t a) {
uint16_t result;
asm(
"movw %A0, %A1" "\n\t"
"lsr %B0" "\n\t"
"ror %A0" "\n\t"
"lsr %B0" "\n\t"
"ror %A0" "\n\t"
"lsr %B0" "\n\t"
"ror %A0" "\n\t"
"lsr %B0" "\n\t"
"ror %A0" "\n\t"
: "=r" (result)
: "a" (a)
);
return result;
}
static inline uint8_t MulScale8(uint8_t a, uint8_t b) {
uint8_t result;
asm(
"mul %1, %2" "\n\t"
"mov %0, r1" "\n\t"
"eor r1, r1" "\n\t"
: "=r" (result)
: "a" (a), "a" (b)
);
return result;
}
static inline int8_t SignedUnsignedMulScale8(int8_t a, uint8_t b) {
uint8_t result;
asm(
"mulsu %1, %2" "\n\t"
"mov %0, r1" "\n\t"
"eor r1, r1" "\n\t"
: "=r" (result)
: "a" (a), "a" (b)
);
return result;
}
static inline int16_t SignedUnsignedMul(int8_t a, uint8_t b) {
int16_t result;
asm(
"mulsu %1, %2" "\n\t"
"movw %0, r0" "\n\t"
"eor r1, r1" "\n\t"
: "=r" (result)
: "a" (a), "a" (b)
);
return result;
}
static inline uint16_t UnsignedUnsignedMul(uint8_t a, uint8_t b) {
uint16_t result;
asm(
"mul %1, %2" "\n\t"
"movw %0, r0" "\n\t"
"eor r1, r1" "\n\t"
: "=r" (result)
: "a" (a), "a" (b)
);
return result;
}
static inline int8_t SignedSignedMulScale8(int8_t a, int8_t b) {
uint8_t result;
asm(
"muls %1, %2" "\n\t"
"mov %0, r1" "\n\t"
"eor r1, r1" "\n\t"
: "=r" (result)
: "a" (a), "a" (b)
);
return result;
}
static inline uint16_t Mul16Scale8(uint16_t a, uint16_t b) {
uint16_t result;
uint32_t product;
asm(
"mul %A2, %A3" "\n\t"
"movw %A1, r0" "\n\t"
"mul %B2, %B3" "\n\t"
"movw %C1, r0" "\n\t"
"mul %B3, %A2" "\n\t"
"add %B1, r0" "\n\t"
"adc %C1, r1" "\n\t"
"eor r1, r1" "\n\t"
"adc %D1, r1" "\n\t"
"mul %B2, %A3" "\n\t"
"add %B1, r0" "\n\t"
"adc %C1, r1" "\n\t"
"eor r1, r1" "\n\t"
"adc %D1, r1" "\n\t"
"mov %A0, %B1" "\n\t"
"mov %B0, %C1" "\n\t"
: "=r" (result), "=&r" (product)
: "a" (a), "a" (b)
);
return result;
}
// The code generated by gcc for >> 6 is short but uses a loop. This saves
// a couple of cycles. Note that this solution only works for operands with
// a 14-bits resolution.
static inline uint8_t ShiftRight6(uint16_t value) {
uint8_t b = value >> 8;
uint8_t a = value & 0xff;
uint8_t result;
asm(
"add %1, %1" "\n\t"
"adc %2, %2" "\n\t"
"add %1, %1" "\n\t"
"adc %2, %2" "\n\t"
: "=r" (result)
: "a" (a), "0" (b)
);
return result;
}
static inline uint8_t ShiftRight7(uint16_t value) {
uint8_t b = value >> 8;
uint8_t a = value & 0xff;
uint8_t result;
asm(
"add %1, %1" "\n\t"
"adc %2, %2" "\n\t"
: "=r" (result)
: "a" (a), "0" (b)
);
return result;
}
static inline uint8_t InterpolateSample(
const prog_uint8_t* table,
uint16_t phase) __attribute__((always_inline));
static inline uint8_t InterpolateSample(
const prog_uint8_t* table,
uint16_t phase) {
uint8_t result;
uint8_t work;
asm(
"movw r30, %A2" "\n\t" // copy base address to r30:r31
"add r30, %B3" "\n\t" // increment table address by phaseH
"adc r31, r1" "\n\t" // just carry
"mov %1, %A3" "\n\t" // move phaseL to working register
"lpm %0, z+" "\n\t" // load sample[n]
"lpm r1, z+" "\n\t" // load sample[n+1]
"mul %1, r1" "\n\t" // multiply second sample by phaseL
"movw r30, r0" "\n\t" // result to accumulator
"com %1" "\n\t" // 255 - phaseL -> phaseL
"mul %1, %0" "\n\t" // multiply first sample by phaseL
"add r30, r0" "\n\t" // accumulate L
"adc r31, r1" "\n\t" // accumulate H
"eor r1, r1" "\n\t" // reset r1 after multiplication
"mov %0, r31" "\n\t" // use sum H as output
: "=r" (result), "=r" (work)
: "r" (table), "r" (phase)
: "r30", "r31"
);
return result;
}
static inline int16_t SignedUnsignedMul16Scale15(int16_t a, uint16_t b) {
int16_t result;
int16_t tmp;
asm(
"eor %A1, %A1" "\n\t"
"mul %A2, %A3" "\n\t"
"mov %B1, r1" "\n\t"
"mulsu %B2, %B3" "\n\t"
"movw %A0, r0" "\n\t"
"mul %B3, %A2" "\n\t"
"add %B1, r0" "\n\t"
"adc %A0, r1" "\n\t"
"adc %B0, %A1" "\n\t"
"mulsu %B2, %A3" "\n\t"
"sbc %B0, %A1" "\n\t"
"add %B1, r0" "\n\t"
"adc %A0, r1" "\n\t"
"adc %B0, %A1" "\n\t"
"eor r1, r1" "\n\t"
"add %B1, %B1" "\n\t"
"adc %A0, %A0" "\n\t"
"adc %B0, %B0" "\n\t"
: "=&r" (result), "=&r" (tmp)
: "a" (a), "a" (b)
);
return result;
}
#else
static inline uint8_t Clip8(int16_t value) {
return value < 0 ? 0 : (value > 255 ? 255 : value);
}
static inline int8_t SignedClip8(int16_t value) {
return value < -128 ? -128 : (value > 127 ? 127 : value);
}
static inline uint8_t Mix(uint8_t a, uint8_t b, uint8_t balance) {
return a * (255 - balance) + b * balance >> 8;
}
static inline uint8_t Mix(uint8_t a, uint8_t b, uint8_t gain_a, uint8_t gain_b) {
return a * gain_a + b * gain_b >> 8;
}
static inline int8_t SignedMix(
int8_t a, int8_t b,
uint8_t gain_a, uint8_t gain_b) {
return a * gain_a + b * gain_b >> 8;
}
static inline uint16_t Mix16(uint8_t a, uint8_t b, uint8_t balance) {
return a * (255 - balance) + b * balance;
}
static inline uint8_t Mix4(uint8_t a, uint8_t b, uint8_t balance) {
return a * (15 - balance) + b * balance >> 4;
}
static inline uint8_t UnscaledMix4(uint8_t a, uint8_t b, uint8_t balance) {
return a * (15 - balance) + b * balance;
}
static inline uint8_t ShiftRight4(uint8_t a) {
return a >> 4;
}
static inline uint8_t ShiftLeft4(uint8_t a) {
return a << 4;
}
static inline uint8_t Swap4(uint8_t a) {
return (a << 4) | (a >> 4);
}
static inline uint8_t MulScale8(uint8_t a, uint8_t b) {
return a * b >> 8;
}
static inline int8_t SignedUnsignedMulScale8(int8_t a, uint8_t b) {
return a * b >> 8;
}
static inline int16_t SignedUnsignedMul(int8_t a, uint8_t b) {
return a * b;
}
static inline uint16_t UnsignedUnsignedMul(uint8_t a, uint8_t b) {
return a * b;
}
static inline int8_t SignedSignedMulScale8(int8_t a, int8_t b) {
return a * b >> 8;
}
static inline uint16_t Mul16Scale8(uint16_t a, uint16_t b) {
return static_cast<uint32_t>(a) * b >> 8;
}
static inline uint8_t ShiftRight6(uint16_t value) {
return value >> 6;
}
static inline uint8_t ShiftRight7(uint16_t value) {
return value >> 7;
}
static inline uint8_t InterpolateSample(
const prog_uint8_t* table,
uint16_t phase) {
return Mix(
pgm_read_byte(table + (phase >> 8)),
pgm_read_byte(1 + table + (phase >> 8)),
phase & 0xff);
}
static inline uint16_t To12Bits(uint16_t a) {
return a >> 4;
}
static inline int16_t SignedUnsignedMul16Scale15(int16_t a, uint16_t b) {
return (static_cast<int32_t>(a) * static_cast<uint32_t>(a)) >> 15;
}
#endif // USE_OPTIMIZED_OP
} // namespace avrlib
#endif // AVRLIB_OP_H_