annotate projects/heavy/circularBuffer/SignalTabwrite.h @ 163:20b52283c7b4 heavy-updated

- added circular buffer pd/heavy example (works but process needs to be killed manually if launched via ssh?)
author chnrx <chris.heinrichs@gmail.com>
date Thu, 12 Nov 2015 15:55:30 +0000
parents
children
rev   line source
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_TABWRITE_H_
chris@163 18 #define _HEAVY_SIGNAL_TABWRITE_H_
chris@163 19
chris@163 20 #include "HvBase.h"
chris@163 21 #include "HvTable.h"
chris@163 22
chris@163 23 #define HV_TABWRITE_STOPPED -1 // ~0x0
chris@163 24
chris@163 25 typedef struct SignalTabwrite {
chris@163 26 HvTable *table;
chris@163 27 hv_uint32_t head; // local write head. Where this object has most recently written to the table.
chris@163 28 } SignalTabwrite;
chris@163 29
chris@163 30 hv_size_t sTabwrite_init(SignalTabwrite *o, HvTable *table);
chris@163 31
chris@163 32 void sTabwrite_onMessage(HvBase *_c, SignalTabwrite *o, int letIn, const HvMessage *const m,
chris@163 33 void (*sendMessage)(HvBase *, int, const HvMessage *const));
chris@163 34
chris@163 35 // linear write to table
chris@163 36 static inline void __hv_tabwrite_f(SignalTabwrite *o, hv_bInf_t bIn) {
chris@163 37 hv_assert((o->head + HV_N_SIMD) <= hTable_getSize(o->table)); // assert that the table bounds are respected
chris@163 38 hv_uint32_t head = o->head;
chris@163 39 #if HV_SIMD_AVX
chris@163 40 _mm256_store_ps(hTable_getBuffer(o->table) + head, bIn);
chris@163 41 #elif HV_SIMD_SSE
chris@163 42 _mm_store_ps(hTable_getBuffer(o->table) + head, bIn);
chris@163 43 #elif HV_SIMD_NEON
chris@163 44 vst1q_f32(hTable_getBuffer(o->table) + head, bIn);
chris@163 45 #else // HV_SIMD_NONE
chris@163 46 *(hTable_getBuffer(o->table) + head) = bIn;
chris@163 47 #endif
chris@163 48 head += HV_N_SIMD;
chris@163 49 o->head = head; // update local write head
chris@163 50 hTable_setHead(o->table, head); // update the remote write head (e.g. for use by vd~)
chris@163 51 }
chris@163 52
chris@163 53 // linear unaligned write to table
chris@163 54 static inline void __hv_tabwriteu_f(SignalTabwrite *o, hv_bInf_t bIn) {
chris@163 55 hv_uint32_t head = o->head;
chris@163 56 #if HV_SIMD_AVX
chris@163 57 _mm256_storeu_ps(hTable_getBuffer(o->table) + head, bIn);
chris@163 58 #elif HV_SIMD_SSE
chris@163 59 _mm_storeu_ps(hTable_getBuffer(o->table) + head, bIn);
chris@163 60 #elif HV_SIMD_NEON
chris@163 61 vst1q_f32(hTable_getBuffer(o->table) + head, bIn);
chris@163 62 #else // HV_SIMD_NONE
chris@163 63 *(hTable_getBuffer(o->table) + head) = bIn;
chris@163 64 #endif
chris@163 65 head += HV_N_SIMD;
chris@163 66 o->head = head; // update local write head
chris@163 67 hTable_setHead(o->table, head); // update remote write head
chris@163 68 }
chris@163 69
chris@163 70 // this tabread can be instructed to stop. It is mainly intended for linear reads that only process a portion of a buffer.
chris@163 71 // Stores are unaligned, which can be slow but allows any indicies to be written to.
chris@163 72 // TODO(mhroth): this is not stopping!
chris@163 73 static inline void __hv_tabwrite_stoppable_f(SignalTabwrite *o, hv_bInf_t bIn) {
chris@163 74 if (o->head != HV_TABWRITE_STOPPED) {
chris@163 75 #if HV_SIMD_AVX
chris@163 76 _mm256_storeu_ps(hTable_getBuffer(o->table) + o->head, bIn);
chris@163 77 #elif HV_SIMD_SSE
chris@163 78 _mm_storeu_ps(hTable_getBuffer(o->table) + o->head, bIn);
chris@163 79 #elif HV_SIMD_NEON
chris@163 80 vst1q_f32(hTable_getBuffer(o->table) + o->head, bIn);
chris@163 81 #else // HV_SIMD_NONE
chris@163 82 *(hTable_getBuffer(o->table) + o->head) = bIn;
chris@163 83 #endif
chris@163 84 o->head += HV_N_SIMD;
chris@163 85 }
chris@163 86 }
chris@163 87
chris@163 88 // random write to table
chris@163 89 static inline void __hv_tabwrite_if(SignalTabwrite *o, hv_bIni_t bIn0, hv_bInf_t bIn1) {
chris@163 90 float *const b = hTable_getBuffer(o->table);
chris@163 91 #if HV_SIMD_AVX
chris@163 92 b[bIn0[0]] = bIn1[0];
chris@163 93 b[bIn0[1]] = bIn1[1];
chris@163 94 b[bIn0[2]] = bIn1[2];
chris@163 95 b[bIn0[3]] = bIn1[3];
chris@163 96 b[bIn0[4]] = bIn1[4];
chris@163 97 b[bIn0[5]] = bIn1[5];
chris@163 98 b[bIn0[6]] = bIn1[6];
chris@163 99 b[bIn0[7]] = bIn1[7];
chris@163 100 #elif HV_SIMD_SSE
chris@163 101 b[bIn0[0]] = bIn1[0];
chris@163 102 b[bIn0[1]] = bIn1[1];
chris@163 103 b[bIn0[2]] = bIn1[2];
chris@163 104 b[bIn0[3]] = bIn1[3];
chris@163 105 #elif HV_SIMD_NEON
chris@163 106 hv_assert((vgetq_lane_s32(bIn0,0) >= 0) && (vgetq_lane_s32(bIn0,0) < hTable_getSize(o->table)));
chris@163 107 hv_assert((vgetq_lane_s32(bIn0,1) >= 0) && (vgetq_lane_s32(bIn0,1) < hTable_getSize(o->table)));
chris@163 108 hv_assert((vgetq_lane_s32(bIn0,2) >= 0) && (vgetq_lane_s32(bIn0,2) < hTable_getSize(o->table)));
chris@163 109 hv_assert((vgetq_lane_s32(bIn0,3) >= 0) && (vgetq_lane_s32(bIn0,3) < hTable_getSize(o->table)));
chris@163 110
chris@163 111 vst1q_lane_f32(b + vgetq_lane_s32(bIn0, 0), bIn1, 0);
chris@163 112 vst1q_lane_f32(b + vgetq_lane_s32(bIn0, 1), bIn1, 1);
chris@163 113 vst1q_lane_f32(b + vgetq_lane_s32(bIn0, 2), bIn1, 2);
chris@163 114 vst1q_lane_f32(b + vgetq_lane_s32(bIn0, 3), bIn1, 3);
chris@163 115 #else // HV_SIMD_NONE
chris@163 116 b[bIn0] = bIn1;
chris@163 117 #endif
chris@163 118 }
chris@163 119
chris@163 120 #endif // _HEAVY_SIGNAL_TABWRITE_H_