comparison projects/heavy/envelopeTrigger/SignalTabread.h @ 162:c3e8226a5651 heavy-updated

- added additional flags to C rules (-DNDEBUG, -mfpu=neon) - sample-accurate envelope triggering pd/heavy example
author chnrx <chris.heinrichs@gmail.com>
date Thu, 12 Nov 2015 14:59:46 +0000
parents
children
comparison
equal deleted inserted replaced
161:07735c9d95c8 162:c3e8226a5651
1 /**
2 * Copyright (c) 2014, 2015, Enzien Audio Ltd.
3 *
4 * Permission to use, copy, modify, and/or distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
7 *
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
9 * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
10 * AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
11 * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
12 * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
13 * OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
14 * PERFORMANCE OF THIS SOFTWARE.
15 */
16
17 #ifndef _HEAVY_SIGNAL_TABREAD_H_
18 #define _HEAVY_SIGNAL_TABREAD_H_
19
20 #include "HvBase.h"
21 #include "HvTable.h"
22
23 typedef struct SignalTabread {
24 HvTable *table; // the table to read
25 hv_uint32_t head;
26 bool forceAlignedLoads; // false by default, true if using __hv_tabread_f
27 } SignalTabread;
28
29 // random access to a table
30 hv_size_t sTabread_init(SignalTabread *o, HvTable *table, bool forceAlignedLoads);
31
32
33
34 #if HV_APPLE
35 #pragma mark - Tabread - Random Access
36 #endif
37
38 static inline void __hv_tabread_if(SignalTabread *o, hv_bIni_t bIn, hv_bOutf_t bOut) {
39 const float *const b = hTable_getBuffer(o->table);
40 #if HV_SIMD_AVX
41 hv_assert((int) (bIn[0] & 0xFFFFFFFFL) >= 0 && (int) (bIn[0] & 0xFFFFFFFFL) < hTable_getAllocated(o->table));
42 hv_assert((int) (bIn[0] >> 32) >= 0 && (int) ((bIn[0] & ~0xFFFFFFFFL) >> 32) < hTable_getAllocated(o->table));
43 hv_assert((int) (bIn[1] & 0xFFFFFFFFL) >= 0 && (int) (bIn[1] & 0xFFFFFFFFL) < hTable_getAllocated(o->table));
44 hv_assert((int) (bIn[1] >> 32) >= 0 && (int) ((bIn[1] & ~0xFFFFFFFFL) >> 32) < hTable_getAllocated(o->table));
45 hv_assert((int) (bIn[2] & 0xFFFFFFFFL) >= 0 && (int) (bIn[2] & 0xFFFFFFFFL) < hTable_getAllocated(o->table));
46 hv_assert((int) (bIn[2] >> 32) >= 0 && (int) ((bIn[2] & ~0xFFFFFFFFL) >> 32) < hTable_getAllocated(o->table));
47 hv_assert((int) (bIn[3] & 0xFFFFFFFFL) >= 0 && (int) (bIn[3] & 0xFFFFFFFFL) < hTable_getAllocated(o->table));
48 hv_assert((int) (bIn[3] >> 32) >= 0 && (int) ((bIn[3] & ~0xFFFFFFFFL) >> 32) < hTable_getAllocated(o->table));
49
50 *bOut = _mm256_set_ps(
51 b[(int) (bIn[3] >> 32)],
52 b[(int) (bIn[3] & 0xFFFFFFFFL)],
53 b[(int) (bIn[2] >> 32)],
54 b[(int) (bIn[2] & 0xFFFFFFFFL)],
55 b[(int) (bIn[1] >> 32)],
56 b[(int) (bIn[1] & 0xFFFFFFFFL)],
57 b[(int) (bIn[0] >> 32)],
58 b[(int) (bIn[0] & 0xFFFFFFFFL)]);
59 #elif HV_SIMD_SSE
60 hv_assert((int) (bIn[0] & 0xFFFFFFFFL) >= 0 && (int) (bIn[0] & 0xFFFFFFFFL) < hTable_getAllocated(o->table));
61 hv_assert((int) (bIn[0] >> 32) >= 0 && (int) (bIn[0] >> 32) < hTable_getAllocated(o->table));
62 hv_assert((int) (bIn[1] & 0xFFFFFFFFL) >= 0 && (int) (bIn[1] & 0xFFFFFFFFL) < hTable_getAllocated(o->table));
63 hv_assert((int) (bIn[1] >> 32) >= 0 && (int) (bIn[1] >> 32) < hTable_getAllocated(o->table));
64
65 *bOut = _mm_set_ps(
66 b[(int) (bIn[1] >> 32)],
67 b[(int) (bIn[1] & 0xFFFFFFFFL)],
68 b[(int) (bIn[0] >> 32)],
69 b[(int) (bIn[0] & 0xFFFFFFFFL)]);
70 #elif HV_SIMD_NEON
71 hv_assert((bIn[0] >= 0) && (bIn[0] < hTable_getAllocated(o->table)));
72 hv_assert((bIn[1] >= 0) && (bIn[1] < hTable_getAllocated(o->table)));
73 hv_assert((bIn[2] >= 0) && (bIn[2] < hTable_getAllocated(o->table)));
74 hv_assert((bIn[3] >= 0) && (bIn[3] < hTable_getAllocated(o->table)));
75
76 *bOut = (float32x4_t) {b[bIn[0]], b[bIn[1]], b[bIn[2]], b[bIn[3]]};
77 #else // HV_SIMD_NONE
78 hv_assert(bIn >= 0 && ((hv_uint32_t) bIn < hTable_getAllocated(o->table)));
79
80 *bOut = b[bIn];
81 #endif
82 }
83
84
85
86 #if HV_APPLE
87 #pragma mark - Tabread - Linear Access
88 #endif
89
90 // this tabread never stops reading. It is mainly intended for linear reads that loop around a table.
91 static inline void __hv_tabread_f(SignalTabread *o, hv_bOutf_t bOut) {
92 hv_assert((o->head + HV_N_SIMD) <= hTable_getAllocated(o->table)); // assert that we always read within the table bounds
93 hv_uint32_t head = o->head;
94 #if HV_SIMD_AVX
95 *bOut = _mm256_load_ps(hTable_getBuffer(o->table) + head);
96 #elif HV_SIMD_SSE
97 *bOut = _mm_load_ps(hTable_getBuffer(o->table) + head);
98 #elif HV_SIMD_NEON
99 *bOut = vld1q_f32(hTable_getBuffer(o->table) + head);
100 #else // HV_SIMD_NONE
101 *bOut = *(hTable_getBuffer(o->table) + head);
102 #endif
103 o->head = head + HV_N_SIMD;
104 }
105
106 // unaligned linear tabread, as above
107 static inline void __hv_tabreadu_f(SignalTabread *o, hv_bOutf_t bOut) {
108 hv_assert((o->head + HV_N_SIMD) <= hTable_getAllocated(o->table)); // assert that we always read within the table bounds
109 hv_uint32_t head = o->head;
110 #if HV_SIMD_AVX
111 *bOut = _mm256_loadu_ps(hTable_getBuffer(o->table) + head);
112 #elif HV_SIMD_SSE
113 *bOut = _mm_loadu_ps(hTable_getBuffer(o->table) + head);
114 #elif HV_SIMD_NEON
115 *bOut = vld1q_f32(hTable_getBuffer(o->table) + head);
116 #else // HV_SIMD_NONE
117 *bOut = *(hTable_getBuffer(o->table) + head);
118 #endif
119 o->head = head + HV_N_SIMD;
120 }
121
122 // this tabread can be instructed to stop. It is mainly intended for linear reads that only process a portion of a buffer.
123 static inline void __hv_tabread_stoppable_f(SignalTabread *o, hv_bOutf_t bOut) {
124 #if HV_SIMD_AVX
125 if (o->head == ~0x0) {
126 *bOut = _mm256_setzero_ps();
127 } else {
128 *bOut = _mm256_load_ps(hTable_getBuffer(o->table) + o->head);
129 o->head += HV_N_SIMD;
130 }
131 #elif HV_SIMD_SSE
132 if (o->head == ~0x0) {
133 *bOut = _mm_setzero_ps();
134 } else {
135 *bOut = _mm_load_ps(hTable_getBuffer(o->table) + o->head);
136 o->head += HV_N_SIMD;
137 }
138 #elif HV_SIMD_NEON
139 if (o->head == ~0x0) {
140 *bOut = vdupq_n_f32(0.0f);
141 } else {
142 *bOut = vld1q_f32(hTable_getBuffer(o->table) + o->head);
143 o->head += HV_N_SIMD;
144 }
145 #else // HV_SIMD_NONE
146 if (o->head == ~0x0) {
147 *bOut = 0.0f;
148 } else {
149 *bOut = *(hTable_getBuffer(o->table) + o->head);
150 o->head += HV_N_SIMD;
151 }
152 #endif
153 }
154
155 void sTabread_onMessage(HvBase *_c, SignalTabread *o, int letIn, const HvMessage *const m);
156
157
158
159 #if HV_APPLE
160 #pragma mark - Tabhead
161 #endif
162
163 typedef struct SignalTabhead {
164 HvTable *table;
165 } SignalTabhead;
166
167 hv_size_t sTabhead_init(SignalTabhead *o, HvTable *table);
168
169 static inline void __hv_tabhead_f(SignalTabhead *o, hv_bOutf_t bOut) {
170 #if HV_SIMD_AVX
171 *bOut = _mm256_set1_ps((float) hTable_getHead(o->table));
172 #elif HV_SIMD_SSE
173 *bOut = _mm_set1_ps((float) hTable_getHead(o->table));
174 #elif HV_SIMD_NEON
175 *bOut = vdupq_n_f32((float32_t) hTable_getHead(o->table));
176 #else // HV_SIMD_NONE
177 *bOut = (float) hTable_getHead(o->table);
178 #endif
179 }
180
181 void sTabhead_onMessage(HvBase *_c, SignalTabhead *o, const HvMessage *const m);
182
183 #endif // _HEAVY_SIGNAL_TABREAD_H_