annotate projects/heavy/hello-world/SignalPhasor.h @ 160:5bcf04234f80 heavy-updated

- added -std=c99 to Makefile for user-supplied C files (required for heavy files) - changed heavy core render.cpp file to use latest API and removed all redundant functions (e.g. foleyDesigner/touchkey stuff) - use build_pd.sh to compile and run pd files (-h for usage instructions)
author chnrx <chris.heinrichs@gmail.com>
date Thu, 05 Nov 2015 18:58:26 +0000
parents
children
rev   line source
chris@160 1 /**
chris@160 2 * Copyright (c) 2014, 2015, Enzien Audio Ltd.
chris@160 3 *
chris@160 4 * Permission to use, copy, modify, and/or distribute this software for any
chris@160 5 * purpose with or without fee is hereby granted, provided that the above
chris@160 6 * copyright notice and this permission notice appear in all copies.
chris@160 7 *
chris@160 8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
chris@160 9 * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
chris@160 10 * AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
chris@160 11 * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
chris@160 12 * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
chris@160 13 * OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
chris@160 14 * PERFORMANCE OF THIS SOFTWARE.
chris@160 15 */
chris@160 16
chris@160 17 #ifndef _HEAVY_SIGNAL_PHASOR_H_
chris@160 18 #define _HEAVY_SIGNAL_PHASOR_H_
chris@160 19
chris@160 20 #include "HvBase.h"
chris@160 21
chris@160 22 typedef struct SignalPhasor {
chris@160 23 #if HV_SIMD_AVX
chris@160 24 __m256 phase; // current phase
chris@160 25 __m256 inc; // phase increment
chris@160 26 #elif HV_SIMD_SSE
chris@160 27 __m128i phase;
chris@160 28 __m128i inc;
chris@160 29 #elif HV_SIMD_NEON
chris@160 30 uint32x4_t phase;
chris@160 31 int32x4_t inc;
chris@160 32 #else // HV_SIMD_NONE
chris@160 33 hv_uint32_t phase;
chris@160 34 hv_int32_t inc;
chris@160 35 #endif
chris@160 36 union {
chris@160 37 float f2sc; // float to step conversion (used for __phasor~f)
chris@160 38 hv_int32_t s; // step value (used for __phasor_k~f)
chris@160 39 } step;
chris@160 40 } SignalPhasor;
chris@160 41
chris@160 42 hv_size_t sPhasor_init(SignalPhasor *o, double samplerate);
chris@160 43
chris@160 44 hv_size_t sPhasor_k_init(SignalPhasor *o, float frequency, double samplerate);
chris@160 45
chris@160 46 void sPhasor_k_onMessage(HvBase *_c, SignalPhasor *o, int letIn, const HvMessage *m);
chris@160 47
chris@160 48 void sPhasor_onMessage(HvBase *_c, SignalPhasor *o, int letIn, const HvMessage *m);
chris@160 49
chris@160 50 static inline void __hv_phasor_f(SignalPhasor *o, hv_bInf_t bIn, hv_bOutf_t bOut) {
chris@160 51 #if HV_SIMD_AVX
chris@160 52 __m256 p = _mm256_mul_ps(bIn, _mm256_set1_ps(o->step.f2sc)); // a b c d e f g h
chris@160 53
chris@160 54 __m256 z = _mm256_setzero_ps();
chris@160 55
chris@160 56 // http://stackoverflow.com/questions/11906814/how-to-rotate-an-sse-avx-vector
chris@160 57 __m256 a = _mm256_permute_ps(p, _MM_SHUFFLE(2,1,0,3)); // d a b c h e f g
chris@160 58 __m256 b = _mm256_permute2f128_ps(a, a, 0x01); // h e f g d a b c
chris@160 59 __m256 c = _mm256_blend_ps(a, b, 0x10); // d a b c d e f g
chris@160 60 __m256 d = _mm256_blend_ps(c, z, 0x01); // 0 a b c d e f g
chris@160 61 __m256 e = _mm256_add_ps(p, d); // a (a+b) (b+c) (c+d) (d+e) (e+f) (f+g) (g+h)
chris@160 62
chris@160 63 __m256 f = _mm256_permute_ps(e, _MM_SHUFFLE(1,0,3,2)); // (b+c) (c+d) a (a+b) (f+g) (g+h) (d+e) (e+f)
chris@160 64 __m256 g = _mm256_permute2f128_ps(f, f, 0x01); // (f+g) (g+h) (d+e) (e+f) (b+c) (c+d) a (a+b)
chris@160 65 __m256 h = _mm256_blend_ps(f, g, 0x33); // (b+c) (c+d) a (a+b) (b+c) (c+d) (d+e) (e+f)
chris@160 66 __m256 i = _mm256_blend_ps(h, z, 0x03); // 0 0 a (a+b) (b+c) (c+d) (d+e) (e+f)
chris@160 67 __m256 j = _mm256_add_ps(e, i); // a (a+b) (a+b+c) (a+b+c+d) (b+c+d+e) (c+d+e+f) (d+e+f+g) (e+f+g+h)
chris@160 68
chris@160 69 __m256 k = _mm256_permute2f128_ps(j, z, 0x02); // 0 0 0 0 a (a+b) (a+b+c) (a+b+c+d) (b+c+d+e)
chris@160 70 __m256 m = _mm256_add_ps(j, k); // a (a+b) (a+b+c) (a+b+c+d) (a+b+c+d+e) (a+b+c+d+e+f) (a+b+c+d+e+f+g) (a+b+c+d+e+f+g+h)
chris@160 71
chris@160 72 __m256 n = _mm256_or_ps(_mm256_andnot_ps(
chris@160 73 _mm256_set1_ps(-INFINITY),
chris@160 74 _mm256_add_ps(o->phase, m)),
chris@160 75 _mm256_set1_ps(1.0f));
chris@160 76
chris@160 77 *bOut = _mm256_sub_ps(n, _mm256_set1_ps(1.0f));
chris@160 78
chris@160 79 __m256 x = _mm256_permute_ps(n, _MM_SHUFFLE(3,3,3,3));
chris@160 80 o->phase = _mm256_permute2f128_ps(x, x, 0x11);
chris@160 81 #elif HV_SIMD_SSE
chris@160 82 __m128i p = _mm_cvtps_epi32(_mm_mul_ps(bIn, _mm_set1_ps(o->step.f2sc))); // convert frequency to step
chris@160 83 p = _mm_add_epi32(p, _mm_slli_si128(p, 4)); // add incremental steps to phase (prefix sum)
chris@160 84 p = _mm_add_epi32(p, _mm_slli_si128(p, 8)); // http://stackoverflow.com/questions/10587598/simd-prefix-sum-on-intel-cpu?rq=1
chris@160 85 p = _mm_add_epi32(o->phase, p);
chris@160 86 *bOut = _mm_sub_ps(_mm_castsi128_ps(
chris@160 87 _mm_or_si128(_mm_srli_epi32(p, 9),
chris@160 88 (__m128i) {0x3F8000003F800000L, 0x3F8000003F800000L})),
chris@160 89 _mm_set1_ps(1.0f));
chris@160 90 o->phase = _mm_shuffle_epi32(p, _MM_SHUFFLE(3,3,3,3));
chris@160 91 #elif HV_SIMD_NEON
chris@160 92 int32x4_t p = vcvtq_s32_f32(vmulq_n_f32(bIn, o->step.f2sc));
chris@160 93 p = vaddq_s32(p, vextq_s32(vdupq_n_s32(0), p, 3)); // http://stackoverflow.com/questions/11259596/arm-neon-intrinsics-rotation
chris@160 94 p = vaddq_s32(p, vextq_s32(vdupq_n_s32(0), p, 2));
chris@160 95 uint32x4_t pp = vaddq_u32(o->phase, vreinterpretq_u32_s32(p));
chris@160 96 *bOut = vsubq_f32(vreinterpretq_f32_u32(vorrq_u32(vshrq_n_u32(pp, 9), vdupq_n_u32(0x3F800000))), vdupq_n_f32(1.0f));
chris@160 97 o->phase = vdupq_n_u32(pp[3]);
chris@160 98 #else // HV_SIMD_NONE
chris@160 99 const hv_uint32_t p = (o->phase >> 9) | 0x3F800000;
chris@160 100 *bOut = *((float *) (&p)) - 1.0f;
chris@160 101 o->phase += ((int) (bIn * o->step.f2sc));
chris@160 102 #endif
chris@160 103 }
chris@160 104
chris@160 105 static inline void __hv_phasor_k_f(SignalPhasor *o, hv_bOutf_t bOut) {
chris@160 106 #if HV_SIMD_AVX
chris@160 107 *bOut = _mm256_sub_ps(o->phase, _mm256_set1_ps(1.0f));
chris@160 108 o->phase = _mm256_or_ps(_mm256_andnot_ps(
chris@160 109 _mm256_set1_ps(-INFINITY),
chris@160 110 _mm256_add_ps(o->phase, o->inc)),
chris@160 111 _mm256_set1_ps(1.0f));
chris@160 112 #elif HV_SIMD_SSE
chris@160 113 *bOut = _mm_sub_ps(_mm_castsi128_ps(
chris@160 114 _mm_or_si128(_mm_srli_epi32(o->phase, 9),
chris@160 115 (__m128i) {0x3F8000003F800000L, 0x3F8000003F800000L})),
chris@160 116 _mm_set1_ps(1.0f));
chris@160 117 o->phase = _mm_add_epi32(o->phase, o->inc);
chris@160 118 #elif HV_SIMD_NEON
chris@160 119 *bOut = vsubq_f32(vreinterpretq_f32_u32(
chris@160 120 vorrq_u32(vshrq_n_u32(o->phase, 9),
chris@160 121 vdupq_n_u32(0x3F800000))),
chris@160 122 vdupq_n_f32(1.0f));
chris@160 123 o->phase = vaddq_u32(o->phase, vreinterpretq_u32_s32(o->inc));
chris@160 124 #else // HV_SIMD_NONE
chris@160 125 const hv_uint32_t p = (o->phase >> 9) | 0x3F800000;
chris@160 126 *bOut = *((float *) (&p)) - 1.0f;
chris@160 127 o->phase += o->inc;
chris@160 128 #endif
chris@160 129 }
chris@160 130
chris@160 131 #endif // _HEAVY_SIGNAL_PHASOR_H_