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