chris@162: /** chris@162: * Copyright (c) 2014, 2015, Enzien Audio Ltd. chris@162: * chris@162: * Permission to use, copy, modify, and/or distribute this software for any chris@162: * purpose with or without fee is hereby granted, provided that the above chris@162: * copyright notice and this permission notice appear in all copies. chris@162: * chris@162: * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH chris@162: * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY chris@162: * AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, chris@162: * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM chris@162: * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR chris@162: * OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR chris@162: * PERFORMANCE OF THIS SOFTWARE. chris@162: */ chris@162: chris@162: #ifndef _HEAVY_SIGNAL_TABREAD_H_ chris@162: #define _HEAVY_SIGNAL_TABREAD_H_ chris@162: chris@162: #include "HvBase.h" chris@162: #include "HvTable.h" chris@162: chris@162: typedef struct SignalTabread { chris@162: HvTable *table; // the table to read chris@162: hv_uint32_t head; chris@162: bool forceAlignedLoads; // false by default, true if using __hv_tabread_f chris@162: } SignalTabread; chris@162: chris@162: // random access to a table chris@162: hv_size_t sTabread_init(SignalTabread *o, HvTable *table, bool forceAlignedLoads); chris@162: chris@162: chris@162: chris@162: #if HV_APPLE chris@162: #pragma mark - Tabread - Random Access chris@162: #endif chris@162: chris@162: static inline void __hv_tabread_if(SignalTabread *o, hv_bIni_t bIn, hv_bOutf_t bOut) { chris@162: const float *const b = hTable_getBuffer(o->table); chris@162: #if HV_SIMD_AVX chris@162: hv_assert((int) (bIn[0] & 0xFFFFFFFFL) >= 0 && (int) (bIn[0] & 0xFFFFFFFFL) < hTable_getAllocated(o->table)); chris@162: hv_assert((int) (bIn[0] >> 32) >= 0 && (int) ((bIn[0] & ~0xFFFFFFFFL) >> 32) < hTable_getAllocated(o->table)); chris@162: hv_assert((int) (bIn[1] & 0xFFFFFFFFL) >= 0 && (int) (bIn[1] & 0xFFFFFFFFL) < hTable_getAllocated(o->table)); chris@162: hv_assert((int) (bIn[1] >> 32) >= 0 && (int) ((bIn[1] & ~0xFFFFFFFFL) >> 32) < hTable_getAllocated(o->table)); chris@162: hv_assert((int) (bIn[2] & 0xFFFFFFFFL) >= 0 && (int) (bIn[2] & 0xFFFFFFFFL) < hTable_getAllocated(o->table)); chris@162: hv_assert((int) (bIn[2] >> 32) >= 0 && (int) ((bIn[2] & ~0xFFFFFFFFL) >> 32) < hTable_getAllocated(o->table)); chris@162: hv_assert((int) (bIn[3] & 0xFFFFFFFFL) >= 0 && (int) (bIn[3] & 0xFFFFFFFFL) < hTable_getAllocated(o->table)); chris@162: hv_assert((int) (bIn[3] >> 32) >= 0 && (int) ((bIn[3] & ~0xFFFFFFFFL) >> 32) < hTable_getAllocated(o->table)); chris@162: chris@162: *bOut = _mm256_set_ps( chris@162: b[(int) (bIn[3] >> 32)], chris@162: b[(int) (bIn[3] & 0xFFFFFFFFL)], chris@162: b[(int) (bIn[2] >> 32)], chris@162: b[(int) (bIn[2] & 0xFFFFFFFFL)], chris@162: b[(int) (bIn[1] >> 32)], chris@162: b[(int) (bIn[1] & 0xFFFFFFFFL)], chris@162: b[(int) (bIn[0] >> 32)], chris@162: b[(int) (bIn[0] & 0xFFFFFFFFL)]); chris@162: #elif HV_SIMD_SSE chris@162: hv_assert((int) (bIn[0] & 0xFFFFFFFFL) >= 0 && (int) (bIn[0] & 0xFFFFFFFFL) < hTable_getAllocated(o->table)); chris@162: hv_assert((int) (bIn[0] >> 32) >= 0 && (int) (bIn[0] >> 32) < hTable_getAllocated(o->table)); chris@162: hv_assert((int) (bIn[1] & 0xFFFFFFFFL) >= 0 && (int) (bIn[1] & 0xFFFFFFFFL) < hTable_getAllocated(o->table)); chris@162: hv_assert((int) (bIn[1] >> 32) >= 0 && (int) (bIn[1] >> 32) < hTable_getAllocated(o->table)); chris@162: chris@162: *bOut = _mm_set_ps( chris@162: b[(int) (bIn[1] >> 32)], chris@162: b[(int) (bIn[1] & 0xFFFFFFFFL)], chris@162: b[(int) (bIn[0] >> 32)], chris@162: b[(int) (bIn[0] & 0xFFFFFFFFL)]); chris@162: #elif HV_SIMD_NEON chris@162: hv_assert((bIn[0] >= 0) && (bIn[0] < hTable_getAllocated(o->table))); chris@162: hv_assert((bIn[1] >= 0) && (bIn[1] < hTable_getAllocated(o->table))); chris@162: hv_assert((bIn[2] >= 0) && (bIn[2] < hTable_getAllocated(o->table))); chris@162: hv_assert((bIn[3] >= 0) && (bIn[3] < hTable_getAllocated(o->table))); chris@162: chris@162: *bOut = (float32x4_t) {b[bIn[0]], b[bIn[1]], b[bIn[2]], b[bIn[3]]}; chris@162: #else // HV_SIMD_NONE chris@162: hv_assert(bIn >= 0 && ((hv_uint32_t) bIn < hTable_getAllocated(o->table))); chris@162: chris@162: *bOut = b[bIn]; chris@162: #endif chris@162: } chris@162: chris@162: chris@162: chris@162: #if HV_APPLE chris@162: #pragma mark - Tabread - Linear Access chris@162: #endif chris@162: chris@162: // this tabread never stops reading. It is mainly intended for linear reads that loop around a table. chris@162: static inline void __hv_tabread_f(SignalTabread *o, hv_bOutf_t bOut) { chris@162: hv_assert((o->head + HV_N_SIMD) <= hTable_getAllocated(o->table)); // assert that we always read within the table bounds chris@162: hv_uint32_t head = o->head; chris@162: #if HV_SIMD_AVX chris@162: *bOut = _mm256_load_ps(hTable_getBuffer(o->table) + head); chris@162: #elif HV_SIMD_SSE chris@162: *bOut = _mm_load_ps(hTable_getBuffer(o->table) + head); chris@162: #elif HV_SIMD_NEON chris@162: *bOut = vld1q_f32(hTable_getBuffer(o->table) + head); chris@162: #else // HV_SIMD_NONE chris@162: *bOut = *(hTable_getBuffer(o->table) + head); chris@162: #endif chris@162: o->head = head + HV_N_SIMD; chris@162: } chris@162: chris@162: // unaligned linear tabread, as above chris@162: static inline void __hv_tabreadu_f(SignalTabread *o, hv_bOutf_t bOut) { chris@162: hv_assert((o->head + HV_N_SIMD) <= hTable_getAllocated(o->table)); // assert that we always read within the table bounds chris@162: hv_uint32_t head = o->head; chris@162: #if HV_SIMD_AVX chris@162: *bOut = _mm256_loadu_ps(hTable_getBuffer(o->table) + head); chris@162: #elif HV_SIMD_SSE chris@162: *bOut = _mm_loadu_ps(hTable_getBuffer(o->table) + head); chris@162: #elif HV_SIMD_NEON chris@162: *bOut = vld1q_f32(hTable_getBuffer(o->table) + head); chris@162: #else // HV_SIMD_NONE chris@162: *bOut = *(hTable_getBuffer(o->table) + head); chris@162: #endif chris@162: o->head = head + HV_N_SIMD; chris@162: } chris@162: chris@162: // this tabread can be instructed to stop. It is mainly intended for linear reads that only process a portion of a buffer. chris@162: static inline void __hv_tabread_stoppable_f(SignalTabread *o, hv_bOutf_t bOut) { chris@162: #if HV_SIMD_AVX chris@162: if (o->head == ~0x0) { chris@162: *bOut = _mm256_setzero_ps(); chris@162: } else { chris@162: *bOut = _mm256_load_ps(hTable_getBuffer(o->table) + o->head); chris@162: o->head += HV_N_SIMD; chris@162: } chris@162: #elif HV_SIMD_SSE chris@162: if (o->head == ~0x0) { chris@162: *bOut = _mm_setzero_ps(); chris@162: } else { chris@162: *bOut = _mm_load_ps(hTable_getBuffer(o->table) + o->head); chris@162: o->head += HV_N_SIMD; chris@162: } chris@162: #elif HV_SIMD_NEON chris@162: if (o->head == ~0x0) { chris@162: *bOut = vdupq_n_f32(0.0f); chris@162: } else { chris@162: *bOut = vld1q_f32(hTable_getBuffer(o->table) + o->head); chris@162: o->head += HV_N_SIMD; chris@162: } chris@162: #else // HV_SIMD_NONE chris@162: if (o->head == ~0x0) { chris@162: *bOut = 0.0f; chris@162: } else { chris@162: *bOut = *(hTable_getBuffer(o->table) + o->head); chris@162: o->head += HV_N_SIMD; chris@162: } chris@162: #endif chris@162: } chris@162: chris@162: void sTabread_onMessage(HvBase *_c, SignalTabread *o, int letIn, const HvMessage *const m); chris@162: chris@162: chris@162: chris@162: #if HV_APPLE chris@162: #pragma mark - Tabhead chris@162: #endif chris@162: chris@162: typedef struct SignalTabhead { chris@162: HvTable *table; chris@162: } SignalTabhead; chris@162: chris@162: hv_size_t sTabhead_init(SignalTabhead *o, HvTable *table); chris@162: chris@162: static inline void __hv_tabhead_f(SignalTabhead *o, hv_bOutf_t bOut) { chris@162: #if HV_SIMD_AVX chris@162: *bOut = _mm256_set1_ps((float) hTable_getHead(o->table)); chris@162: #elif HV_SIMD_SSE chris@162: *bOut = _mm_set1_ps((float) hTable_getHead(o->table)); chris@162: #elif HV_SIMD_NEON chris@162: *bOut = vdupq_n_f32((float32_t) hTable_getHead(o->table)); chris@162: #else // HV_SIMD_NONE chris@162: *bOut = (float) hTable_getHead(o->table); chris@162: #endif chris@162: } chris@162: chris@162: void sTabhead_onMessage(HvBase *_c, SignalTabhead *o, const HvMessage *const m); chris@162: chris@162: #endif // _HEAVY_SIGNAL_TABREAD_H_