Deluge Firmware 1.3.0
Build date: 2025.04.16
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> phaseVector = Argon<uint32_t>{phase}.MultiplyAdd(Argon<uint32_t>{1U, 2U, 3U, 4U}, phaseIncrement);
28 Argon<uint32_t> indices = phaseVector >> (32 - tableSizeMagnitude);
29 ArgonHalf<uint16_t> strength2 = indices.ShiftRightNarrow<16>() >> 1;
30 auto [value1, value2] = ArgonHalf<int16_t>::LoadGatherInterleaved<2>(table, indices);
31
32 // this is a standard linear interpolation of a + (b - a) * fractional
33 auto output = value1.ShiftLeftLong<16>().MultiplyDoubleAddSaturateLong(value2 - value1, strength2.As<int16_t>());
34 return {output, phase + phaseIncrement * 4};
35}
36
37// Renders 4 wave values (a "vector") together in one go - special case for pulse waves with variable width.
38[[gnu::always_inline]] static inline std::pair<Argon<int32_t>, uint32_t> //<
39waveRenderingFunctionPulse(uint32_t phase, int32_t phaseIncrement, uint32_t phaseToAdd, const int16_t* table,
40 int32_t tableSizeMagnitude) {
41
42 const int32_t rshiftAmount = (32 - tableSizeMagnitude - 16);
43
44 Argon<uint32_t> phaseVector = Argon<uint32_t>{phase}.MultiplyAdd(Argon<uint32_t>{1U, 2U, 3U, 4U}, phaseIncrement);
45 Argon<uint32_t> phaseLater = phaseVector + phaseToAdd;
46
47 Argon<uint32_t> indicesA = phaseVector >> (32 - tableSizeMagnitude);
48 ArgonHalf<int16_t> rshiftedA =
49 indicesA.ShiftRightNarrow<16>().BitwiseAnd(std::numeric_limits<int16_t>::max()).As<int16_t>();
50 auto [valueA1, valueA2] = ArgonHalf<int16_t>::LoadGatherInterleaved<2>(table, indicesA);
51
52 Argon<uint32_t> indicesB = phaseLater >> (32 - tableSizeMagnitude);
53 ArgonHalf<int16_t> rshiftedB =
54 indicesB.ShiftRightNarrow<16>().BitwiseAnd(std::numeric_limits<int16_t>::max()).As<int16_t>();
55 auto [valueB1, valueB2] = ArgonHalf<int16_t>::LoadGatherInterleaved<2>(table, indicesB);
56
57 /* Sneakily do this backwards to flip the polarity of the output, which we need to do anyway */
58 ArgonHalf<int16_t> strengthA1 = rshiftedA | std::numeric_limits<int16_t>::min();
59 ArgonHalf<int16_t> strengthA2 = std::numeric_limits<int16_t>::min() - strengthA1;
60
61 Argon<int32_t> outputA = strengthA2 //<
62 .MultiplyDoubleSaturateLong(valueA2) //<
63 .MultiplyDoubleAddSaturateLong(strengthA1, valueA1);
64
65 ArgonHalf<int16_t> strengthB2 = rshiftedB & std::numeric_limits<int16_t>::max();
66 ArgonHalf<int16_t> strengthB1 = std::numeric_limits<int16_t>::max() - strengthB2;
67
68 Argon<int32_t> outputB = strengthB2 //<
69 .MultiplyDoubleSaturateLong(valueB2) //<
70 .MultiplyDoubleAddSaturateLong(strengthB1, valueB1);
71
72 auto output = outputA.MultiplyRoundFixedPoint(outputB) << 1; // (a *. b) << 1 (average?)
73 return {output, phase + phaseIncrement * 4};
74}