/* Copyright (c) 2018 David Rowe 2018 Mozilla 2008-2011 Octasic Inc. 2012-2017 Jean-Marc Valin */ /* Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: - Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. - Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ /* NEON support for ARM machines */ #ifndef VEC_NEON_H #define VEC_NEON_H #include #include "os_support.h" #ifndef LPCNET_TEST static inline float32x4_t exp4_approx(float32x4_t x) { int32x4_t i; float32x4_t xf; x = vmaxq_f32(vminq_f32(x, vdupq_n_f32(88.f)), vdupq_n_f32(-88.f)); /* express exp(x) as exp2(x/log(2)), add 127 for the exponent later */ x = vmlaq_f32(vdupq_n_f32(127.f), x, vdupq_n_f32(1.44269504f)); /* split into integer and fractional parts */ i = vcvtq_s32_f32(x); xf = vcvtq_f32_s32(i); x = vsubq_f32(x, xf); float32x4_t K0 = vdupq_n_f32(0.99992522f); float32x4_t K1 = vdupq_n_f32(0.69583354f); float32x4_t K2 = vdupq_n_f32(0.22606716f); float32x4_t K3 = vdupq_n_f32(0.078024523f); float32x4_t Y = vmlaq_f32(K0, x, vmlaq_f32(K1, x, vmlaq_f32(K2, K3, x))); /* compute 2^i */ float32x4_t exponent = vreinterpretq_f32_s32(vshlq_n_s32(i, 23)); Y = vmulq_f32(Y, exponent); return Y; } static inline float32x4_t tanh4_approx(float32x4_t X) { const float32x4_t N0 = vdupq_n_f32(952.52801514f); const float32x4_t N1 = vdupq_n_f32(96.39235687f); const float32x4_t N2 = vdupq_n_f32(0.60863042f); const float32x4_t D0 = vdupq_n_f32(952.72399902f); const float32x4_t D1 = vdupq_n_f32(413.36801147f); const float32x4_t D2 = vdupq_n_f32(11.88600922f); const float32x4_t max_out = vdupq_n_f32(1.f); const float32x4_t min_out = vdupq_n_f32(-1.f); float32x4_t X2, num, den; X2 = vmulq_f32(X, X); num = vmlaq_f32(N0, X2, vmlaq_f32(N1, N2, X2)); den = vmlaq_f32(D0, X2, vmlaq_f32(D1, D2, X2)); num = vmulq_f32(num, X); den = vrecpeq_f32(den); num = vmulq_f32(num, den); return vmaxq_f32(min_out, vminq_f32(max_out, num)); } static inline float32x4_t sigmoid4_approx(float32x4_t X) { const float32x4_t N0 = vdupq_n_f32(238.13200378f); const float32x4_t N1 = vdupq_n_f32(6.02452230f); const float32x4_t N2 = vdupq_n_f32(0.00950985f); const float32x4_t D0 = vdupq_n_f32(952.72399902f); const float32x4_t D1 = vdupq_n_f32(103.34200287f); const float32x4_t D2 = vdupq_n_f32(0.74287558f); const float32x4_t half = vdupq_n_f32(0.5f); const float32x4_t max_out = vdupq_n_f32(1.f); const float32x4_t min_out = vdupq_n_f32(0.f); float32x4_t X2, num, den; X2 = vmulq_f32(X, X); num = vmlaq_f32(N0, X2, vmlaq_f32(N1, N2, X2)); den = vmlaq_f32(D0, X2, vmlaq_f32(D1, D2, X2)); num = vmulq_f32(num, X); den = vrecpeq_f32(den); num = vmlaq_f32(half, num, den); return vmaxq_f32(min_out, vminq_f32(max_out, num)); } static inline float lpcnet_exp(float x) { float out[4]; float32x4_t X, Y; X = vdupq_n_f32(x); Y = exp4_approx(X); vst1q_f32(out, Y); return out[0]; } static inline float tanh_approx(float x) { float out[4]; float32x4_t X, Y; X = vdupq_n_f32(x); Y = tanh4_approx(X); vst1q_f32(out, Y); return out[0]; } static inline float sigmoid_approx(float x) { float out[4]; float32x4_t X, Y; X = vdupq_n_f32(x); Y = sigmoid4_approx(X); vst1q_f32(out, Y); return out[0]; } static inline void softmax(float *y, const float *x, int N) { int i; for (i=0;i