Mercurial > hg > beaglert
comparison 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 |
comparison
equal
deleted
inserted
replaced
| 162:c3e8226a5651 | 163:20b52283c7b4 |
|---|---|
| 1 /** | |
| 2 * Copyright (c) 2014, 2015, Enzien Audio Ltd. | |
| 3 * | |
| 4 * Permission to use, copy, modify, and/or distribute this software for any | |
| 5 * purpose with or without fee is hereby granted, provided that the above | |
| 6 * copyright notice and this permission notice appear in all copies. | |
| 7 * | |
| 8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH | |
| 9 * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY | |
| 10 * AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, | |
| 11 * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM | |
| 12 * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR | |
| 13 * OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR | |
| 14 * PERFORMANCE OF THIS SOFTWARE. | |
| 15 */ | |
| 16 | |
| 17 #ifndef _HEAVY_SIGNAL_TABWRITE_H_ | |
| 18 #define _HEAVY_SIGNAL_TABWRITE_H_ | |
| 19 | |
| 20 #include "HvBase.h" | |
| 21 #include "HvTable.h" | |
| 22 | |
| 23 #define HV_TABWRITE_STOPPED -1 // ~0x0 | |
| 24 | |
| 25 typedef struct SignalTabwrite { | |
| 26 HvTable *table; | |
| 27 hv_uint32_t head; // local write head. Where this object has most recently written to the table. | |
| 28 } SignalTabwrite; | |
| 29 | |
| 30 hv_size_t sTabwrite_init(SignalTabwrite *o, HvTable *table); | |
| 31 | |
| 32 void sTabwrite_onMessage(HvBase *_c, SignalTabwrite *o, int letIn, const HvMessage *const m, | |
| 33 void (*sendMessage)(HvBase *, int, const HvMessage *const)); | |
| 34 | |
| 35 // linear write to table | |
| 36 static inline void __hv_tabwrite_f(SignalTabwrite *o, hv_bInf_t bIn) { | |
| 37 hv_assert((o->head + HV_N_SIMD) <= hTable_getSize(o->table)); // assert that the table bounds are respected | |
| 38 hv_uint32_t head = o->head; | |
| 39 #if HV_SIMD_AVX | |
| 40 _mm256_store_ps(hTable_getBuffer(o->table) + head, bIn); | |
| 41 #elif HV_SIMD_SSE | |
| 42 _mm_store_ps(hTable_getBuffer(o->table) + head, bIn); | |
| 43 #elif HV_SIMD_NEON | |
| 44 vst1q_f32(hTable_getBuffer(o->table) + head, bIn); | |
| 45 #else // HV_SIMD_NONE | |
| 46 *(hTable_getBuffer(o->table) + head) = bIn; | |
| 47 #endif | |
| 48 head += HV_N_SIMD; | |
| 49 o->head = head; // update local write head | |
| 50 hTable_setHead(o->table, head); // update the remote write head (e.g. for use by vd~) | |
| 51 } | |
| 52 | |
| 53 // linear unaligned write to table | |
| 54 static inline void __hv_tabwriteu_f(SignalTabwrite *o, hv_bInf_t bIn) { | |
| 55 hv_uint32_t head = o->head; | |
| 56 #if HV_SIMD_AVX | |
| 57 _mm256_storeu_ps(hTable_getBuffer(o->table) + head, bIn); | |
| 58 #elif HV_SIMD_SSE | |
| 59 _mm_storeu_ps(hTable_getBuffer(o->table) + head, bIn); | |
| 60 #elif HV_SIMD_NEON | |
| 61 vst1q_f32(hTable_getBuffer(o->table) + head, bIn); | |
| 62 #else // HV_SIMD_NONE | |
| 63 *(hTable_getBuffer(o->table) + head) = bIn; | |
| 64 #endif | |
| 65 head += HV_N_SIMD; | |
| 66 o->head = head; // update local write head | |
| 67 hTable_setHead(o->table, head); // update remote write head | |
| 68 } | |
| 69 | |
| 70 // this tabread can be instructed to stop. It is mainly intended for linear reads that only process a portion of a buffer. | |
| 71 // Stores are unaligned, which can be slow but allows any indicies to be written to. | |
| 72 // TODO(mhroth): this is not stopping! | |
| 73 static inline void __hv_tabwrite_stoppable_f(SignalTabwrite *o, hv_bInf_t bIn) { | |
| 74 if (o->head != HV_TABWRITE_STOPPED) { | |
| 75 #if HV_SIMD_AVX | |
| 76 _mm256_storeu_ps(hTable_getBuffer(o->table) + o->head, bIn); | |
| 77 #elif HV_SIMD_SSE | |
| 78 _mm_storeu_ps(hTable_getBuffer(o->table) + o->head, bIn); | |
| 79 #elif HV_SIMD_NEON | |
| 80 vst1q_f32(hTable_getBuffer(o->table) + o->head, bIn); | |
| 81 #else // HV_SIMD_NONE | |
| 82 *(hTable_getBuffer(o->table) + o->head) = bIn; | |
| 83 #endif | |
| 84 o->head += HV_N_SIMD; | |
| 85 } | |
| 86 } | |
| 87 | |
| 88 // random write to table | |
| 89 static inline void __hv_tabwrite_if(SignalTabwrite *o, hv_bIni_t bIn0, hv_bInf_t bIn1) { | |
| 90 float *const b = hTable_getBuffer(o->table); | |
| 91 #if HV_SIMD_AVX | |
| 92 b[bIn0[0]] = bIn1[0]; | |
| 93 b[bIn0[1]] = bIn1[1]; | |
| 94 b[bIn0[2]] = bIn1[2]; | |
| 95 b[bIn0[3]] = bIn1[3]; | |
| 96 b[bIn0[4]] = bIn1[4]; | |
| 97 b[bIn0[5]] = bIn1[5]; | |
| 98 b[bIn0[6]] = bIn1[6]; | |
| 99 b[bIn0[7]] = bIn1[7]; | |
| 100 #elif HV_SIMD_SSE | |
| 101 b[bIn0[0]] = bIn1[0]; | |
| 102 b[bIn0[1]] = bIn1[1]; | |
| 103 b[bIn0[2]] = bIn1[2]; | |
| 104 b[bIn0[3]] = bIn1[3]; | |
| 105 #elif HV_SIMD_NEON | |
| 106 hv_assert((vgetq_lane_s32(bIn0,0) >= 0) && (vgetq_lane_s32(bIn0,0) < hTable_getSize(o->table))); | |
| 107 hv_assert((vgetq_lane_s32(bIn0,1) >= 0) && (vgetq_lane_s32(bIn0,1) < hTable_getSize(o->table))); | |
| 108 hv_assert((vgetq_lane_s32(bIn0,2) >= 0) && (vgetq_lane_s32(bIn0,2) < hTable_getSize(o->table))); | |
| 109 hv_assert((vgetq_lane_s32(bIn0,3) >= 0) && (vgetq_lane_s32(bIn0,3) < hTable_getSize(o->table))); | |
| 110 | |
| 111 vst1q_lane_f32(b + vgetq_lane_s32(bIn0, 0), bIn1, 0); | |
| 112 vst1q_lane_f32(b + vgetq_lane_s32(bIn0, 1), bIn1, 1); | |
| 113 vst1q_lane_f32(b + vgetq_lane_s32(bIn0, 2), bIn1, 2); | |
| 114 vst1q_lane_f32(b + vgetq_lane_s32(bIn0, 3), bIn1, 3); | |
| 115 #else // HV_SIMD_NONE | |
| 116 b[bIn0] = bIn1; | |
| 117 #endif | |
| 118 } | |
| 119 | |
| 120 #endif // _HEAVY_SIGNAL_TABWRITE_H_ |
