Deluge Firmware 1.3.0
Build date: 2026.07.15
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 Argon<uint32_t> indicesA = phaseVector >> (32 - tableSizeMagnitude);
61 ArgonHalf<int16_t> rshiftedA =
62 indicesA.ShiftRightNarrow<16>().BitwiseAnd(std::numeric_limits<int16_t>::max()).As<int16_t>();
63
64 Argon<uint32_t> indicesB = phaseLater >> (32 - tableSizeMagnitude);
65 ArgonHalf<int16_t> rshiftedB =
66 indicesB.ShiftRightNarrow<16>().BitwiseAnd(std::numeric_limits<int16_t>::max()).As<int16_t>();
67
68 // Load the {table[i], table[i+1]} interpolation pairs as single u32 lane loads,
69 // the same pattern as waveRenderingFunctionGeneral above.
70 Argon<uint32_t> readValueA{0U};
71 Argon<uint32_t> readValueB{0U};
72 util::constexpr_for<0, 4, 1>([&]<int i>() {
73 uint32_t phaseHere = phase + phaseIncrement * (i + 1);
74 uint32_t whichValueA = phaseHere >> (32 - tableSizeMagnitude);
75 readValueA[i].Load((uint32_t*)((uintptr_t)table + (whichValueA << 1)));
76 uint32_t whichValueB = (phaseHere + phaseToAdd) >> (32 - tableSizeMagnitude);
77 readValueB[i].Load((uint32_t*)((uintptr_t)table + (whichValueB << 1)));
78 });
79 ArgonHalf<int16_t> valueA1 = readValueA.Narrow().As<int16_t>();
80 ArgonHalf<int16_t> valueA2 = readValueA.ShiftRightNarrow<16>().As<int16_t>();
81 ArgonHalf<int16_t> valueB1 = readValueB.Narrow().As<int16_t>();
82 ArgonHalf<int16_t> valueB2 = readValueB.ShiftRightNarrow<16>().As<int16_t>();
83
84 /* Sneakily do this backwards to flip the polarity of the output, which we need to do anyway */
85 ArgonHalf<int16_t> strengthA1 = rshiftedA | std::numeric_limits<int16_t>::min();
86 ArgonHalf<int16_t> strengthA2 = std::numeric_limits<int16_t>::min() - strengthA1;
87
88 Argon<int32_t> outputA = strengthA2 //<
89 .MultiplyDoubleSaturateLong(valueA2) //<
90 .MultiplyDoubleAddSaturateLong(strengthA1, valueA1);
91
92 ArgonHalf<int16_t> strengthB2 = rshiftedB & std::numeric_limits<int16_t>::max();
93 ArgonHalf<int16_t> strengthB1 = std::numeric_limits<int16_t>::max() - strengthB2;
94
95 Argon<int32_t> outputB = strengthB2 //<
96 .MultiplyDoubleSaturateLong(valueB2) //<
97 .MultiplyDoubleAddSaturateLong(strengthB1, valueB1);
98
99 auto output = outputA.MultiplyRoundFixedQMax(outputB) << 1; // (a *. b) << 1 (average?)
100 return {output, phase + phaseIncrement * 4};
101}