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