Deluge Firmware 1.3.0
Build date: 2026.11.28
Loading...
Searching...
No Matches
vector_rendering_function.h
1/*
2 * Copyright © 2017-2023 Synthstrom Audible Limited
3 *
4 * This file is part of The Synthstrom Audible Deluge Firmware.
5 *
6 * The Synthstrom Audible Deluge Firmware is free software: you can redistribute it and/or modify it under the
7 * terms of the GNU General Public License as published by the Free Software Foundation,
8 * either version 3 of the License, or (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
11 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12 * See the GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License along with this program.
15 * If not, see <https://www.gnu.org/licenses/>.
16 */
17#pragma once
18#include "util/misc.h"
19#include <argon.hpp>
20#include <limits>
21#include <utility>
22
23// Renders 4 wave values (a "vector") together in one go.
24[[gnu::always_inline]] static inline std::pair<Argon<int32_t>, uint32_t> //<
25waveRenderingFunctionGeneral(uint32_t phase, int32_t phaseIncrement, uint32_t _phaseToAdd, const int16_t* table,
26 int32_t tableSizeMagnitude) {
27 Argon<uint32_t> readValue = 0U;
28 ArgonHalf<uint16_t> strength2 = 0U;
29
30 util::constexpr_for<0, 4, 1>([&]<int i>() {
31 phase += phaseIncrement;
32 uint32_t rshifted = phase >> (32 - 16 - tableSizeMagnitude);
33 strength2[i] = rshifted;
34
35 uint32_t whichValue = phase >> (32 - tableSizeMagnitude);
36 uint32_t* readAddress = (uint32_t*)((uintptr_t)table + (whichValue << 1));
37 readValue[i].Load(readAddress);
38 });
39
40 strength2 = strength2 >> 1;
41 ArgonHalf<int16_t> value1 = readValue.Narrow().As<int16_t>();
42 ArgonHalf<int16_t> value2 = readValue.ShiftRightNarrow<16>().As<int16_t>();
43 Argon<int32_t> value1Big = value1.ShiftLeftLong<16>();
44
45 ArgonHalf<int16_t> difference = value2 - value1;
46 auto output = value1Big.MultiplyDoubleAddSaturateLong(difference, strength2.As<int16_t>());
47 return {output, phase};
48}
49
50// Renders 4 wave values (a "vector") together in one go - special case for pulse waves with variable width.
51[[gnu::always_inline]] static inline std::pair<Argon<int32_t>, uint32_t> //<
52waveRenderingFunctionPulse(uint32_t phase, int32_t phaseIncrement, uint32_t phaseToAdd, const int16_t* table,
53 int32_t tableSizeMagnitude) {
54
55 const int32_t rshiftAmount = (32 - tableSizeMagnitude - 16);
56
57 Argon<uint32_t> phaseVector = Argon<uint32_t>{phase}.MultiplyAdd(Argon<uint32_t>{1U, 2U, 3U, 4U}, phaseIncrement);
58 Argon<uint32_t> phaseLater = phaseVector + phaseToAdd;
59
60 // Keep the fractional phase bits below the table index for interpolation.
61 ArgonHalf<int16_t> rshiftedA = (phaseVector >> rshiftAmount).Narrow().As<int16_t>();
62 ArgonHalf<int16_t> rshiftedB = (phaseLater >> rshiftAmount).Narrow().As<int16_t>();
63
64 // Load the {table[i], table[i+1]} interpolation pairs as single u32 lane loads,
65 // the same pattern as waveRenderingFunctionGeneral above.
66 Argon<uint32_t> readValueA{0U};
67 Argon<uint32_t> readValueB{0U};
68 util::constexpr_for<0, 4, 1>([&]<int i>() {
69 uint32_t phaseHere = phase + phaseIncrement * (i + 1);
70 uint32_t whichValueA = phaseHere >> (32 - tableSizeMagnitude);
71 readValueA[i].Load((uint32_t*)((uintptr_t)table + (whichValueA << 1)));
72 uint32_t whichValueB = (phaseHere + phaseToAdd) >> (32 - tableSizeMagnitude);
73 readValueB[i].Load((uint32_t*)((uintptr_t)table + (whichValueB << 1)));
74 });
75 ArgonHalf<int16_t> valueA1 = readValueA.Narrow().As<int16_t>();
76 ArgonHalf<int16_t> valueA2 = readValueA.ShiftRightNarrow<16>().As<int16_t>();
77 ArgonHalf<int16_t> valueB1 = readValueB.Narrow().As<int16_t>();
78 ArgonHalf<int16_t> valueB2 = readValueB.ShiftRightNarrow<16>().As<int16_t>();
79
80 /* Sneakily do this backwards to flip the polarity of the output, which we need to do anyway */
81 ArgonHalf<int16_t> strengthA1 = rshiftedA | std::numeric_limits<int16_t>::min();
82 ArgonHalf<int16_t> strengthA2 = std::numeric_limits<int16_t>::min() - strengthA1;
83
84 Argon<int32_t> outputA = strengthA2 //<
85 .MultiplyDoubleSaturateLong(valueA2) //<
86 .MultiplyDoubleAddSaturateLong(strengthA1, valueA1);
87
88 ArgonHalf<int16_t> strengthB2 = rshiftedB & std::numeric_limits<int16_t>::max();
89 ArgonHalf<int16_t> strengthB1 = std::numeric_limits<int16_t>::max() - strengthB2;
90
91 Argon<int32_t> outputB = strengthB2 //<
92 .MultiplyDoubleSaturateLong(valueB2) //<
93 .MultiplyDoubleAddSaturateLong(strengthB1, valueB1);
94
95 auto output = outputA.MultiplyRoundFixedQMax(outputB) << 1; // (a *. b) << 1 (average?)
96 return {output, phase + phaseIncrement * 4};
97}