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