Deluge Firmware 1.3.0
Build date: 2025.09.27
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*)((uint32_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 auto [valueA1, valueA2] = ArgonHalf<int16_t>::LoadGatherInterleaved<2>(table, indicesA);
64
65 Argon<uint32_t> indicesB = phaseLater >> (32 - tableSizeMagnitude);
66 ArgonHalf<int16_t> rshiftedB =
67 indicesB.ShiftRightNarrow<16>().BitwiseAnd(std::numeric_limits<int16_t>::max()).As<int16_t>();
68 auto [valueB1, valueB2] = ArgonHalf<int16_t>::LoadGatherInterleaved<2>(table, indicesB);
69
70 /* Sneakily do this backwards to flip the polarity of the output, which we need to do anyway */
71 ArgonHalf<int16_t> strengthA1 = rshiftedA | std::numeric_limits<int16_t>::min();
72 ArgonHalf<int16_t> strengthA2 = std::numeric_limits<int16_t>::min() - strengthA1;
73
74 Argon<int32_t> outputA = strengthA2 //<
75 .MultiplyDoubleSaturateLong(valueA2) //<
76 .MultiplyDoubleAddSaturateLong(strengthA1, valueA1);
77
78 ArgonHalf<int16_t> strengthB2 = rshiftedB & std::numeric_limits<int16_t>::max();
79 ArgonHalf<int16_t> strengthB1 = std::numeric_limits<int16_t>::max() - strengthB2;
80
81 Argon<int32_t> outputB = strengthB2 //<
82 .MultiplyDoubleSaturateLong(valueB2) //<
83 .MultiplyDoubleAddSaturateLong(strengthB1, valueB1);
84
85 auto output = outputA.MultiplyRoundFixedPoint(outputB) << 1; // (a *. b) << 1 (average?)
86 return {output, phase + phaseIncrement * 4};
87}