Chris@69: /*********************************************************************** Chris@69: Copyright (c) 2017 Google Inc. Chris@69: Redistribution and use in source and binary forms, with or without Chris@69: modification, are permitted provided that the following conditions Chris@69: are met: Chris@69: - Redistributions of source code must retain the above copyright notice, Chris@69: this list of conditions and the following disclaimer. Chris@69: - Redistributions in binary form must reproduce the above copyright Chris@69: notice, this list of conditions and the following disclaimer in the Chris@69: documentation and/or other materials provided with the distribution. Chris@69: - Neither the name of Internet Society, IETF or IETF Trust, nor the Chris@69: names of specific contributors, may be used to endorse or promote Chris@69: products derived from this software without specific prior written Chris@69: permission. Chris@69: THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" Chris@69: AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE Chris@69: IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE Chris@69: ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE Chris@69: LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR Chris@69: CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF Chris@69: SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS Chris@69: INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN Chris@69: CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) Chris@69: ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE Chris@69: POSSIBILITY OF SUCH DAMAGE. Chris@69: ***********************************************************************/ Chris@69: Chris@69: #ifdef HAVE_CONFIG_H Chris@69: #include "config.h" Chris@69: #endif Chris@69: Chris@69: #include Chris@69: #include "pitch.h" Chris@69: Chris@69: #ifdef FIXED_POINT Chris@69: Chris@69: opus_val32 celt_inner_prod_neon(const opus_val16 *x, const opus_val16 *y, int N) Chris@69: { Chris@69: int i; Chris@69: opus_val32 xy; Chris@69: int16x8_t x_s16x8, y_s16x8; Chris@69: int32x4_t xy_s32x4 = vdupq_n_s32(0); Chris@69: int64x2_t xy_s64x2; Chris@69: int64x1_t xy_s64x1; Chris@69: Chris@69: for (i = 0; i < N - 7; i += 8) { Chris@69: x_s16x8 = vld1q_s16(&x[i]); Chris@69: y_s16x8 = vld1q_s16(&y[i]); Chris@69: xy_s32x4 = vmlal_s16(xy_s32x4, vget_low_s16 (x_s16x8), vget_low_s16 (y_s16x8)); Chris@69: xy_s32x4 = vmlal_s16(xy_s32x4, vget_high_s16(x_s16x8), vget_high_s16(y_s16x8)); Chris@69: } Chris@69: Chris@69: if (N - i >= 4) { Chris@69: const int16x4_t x_s16x4 = vld1_s16(&x[i]); Chris@69: const int16x4_t y_s16x4 = vld1_s16(&y[i]); Chris@69: xy_s32x4 = vmlal_s16(xy_s32x4, x_s16x4, y_s16x4); Chris@69: i += 4; Chris@69: } Chris@69: Chris@69: xy_s64x2 = vpaddlq_s32(xy_s32x4); Chris@69: xy_s64x1 = vadd_s64(vget_low_s64(xy_s64x2), vget_high_s64(xy_s64x2)); Chris@69: xy = vget_lane_s32(vreinterpret_s32_s64(xy_s64x1), 0); Chris@69: Chris@69: for (; i < N; i++) { Chris@69: xy = MAC16_16(xy, x[i], y[i]); Chris@69: } Chris@69: Chris@69: #ifdef OPUS_CHECK_ASM Chris@69: celt_assert(celt_inner_prod_c(x, y, N) == xy); Chris@69: #endif Chris@69: Chris@69: return xy; Chris@69: } Chris@69: Chris@69: void dual_inner_prod_neon(const opus_val16 *x, const opus_val16 *y01, const opus_val16 *y02, Chris@69: int N, opus_val32 *xy1, opus_val32 *xy2) Chris@69: { Chris@69: int i; Chris@69: opus_val32 xy01, xy02; Chris@69: int16x8_t x_s16x8, y01_s16x8, y02_s16x8; Chris@69: int32x4_t xy01_s32x4 = vdupq_n_s32(0); Chris@69: int32x4_t xy02_s32x4 = vdupq_n_s32(0); Chris@69: int64x2_t xy01_s64x2, xy02_s64x2; Chris@69: int64x1_t xy01_s64x1, xy02_s64x1; Chris@69: Chris@69: for (i = 0; i < N - 7; i += 8) { Chris@69: x_s16x8 = vld1q_s16(&x[i]); Chris@69: y01_s16x8 = vld1q_s16(&y01[i]); Chris@69: y02_s16x8 = vld1q_s16(&y02[i]); Chris@69: xy01_s32x4 = vmlal_s16(xy01_s32x4, vget_low_s16 (x_s16x8), vget_low_s16 (y01_s16x8)); Chris@69: xy02_s32x4 = vmlal_s16(xy02_s32x4, vget_low_s16 (x_s16x8), vget_low_s16 (y02_s16x8)); Chris@69: xy01_s32x4 = vmlal_s16(xy01_s32x4, vget_high_s16(x_s16x8), vget_high_s16(y01_s16x8)); Chris@69: xy02_s32x4 = vmlal_s16(xy02_s32x4, vget_high_s16(x_s16x8), vget_high_s16(y02_s16x8)); Chris@69: } Chris@69: Chris@69: if (N - i >= 4) { Chris@69: const int16x4_t x_s16x4 = vld1_s16(&x[i]); Chris@69: const int16x4_t y01_s16x4 = vld1_s16(&y01[i]); Chris@69: const int16x4_t y02_s16x4 = vld1_s16(&y02[i]); Chris@69: xy01_s32x4 = vmlal_s16(xy01_s32x4, x_s16x4, y01_s16x4); Chris@69: xy02_s32x4 = vmlal_s16(xy02_s32x4, x_s16x4, y02_s16x4); Chris@69: i += 4; Chris@69: } Chris@69: Chris@69: xy01_s64x2 = vpaddlq_s32(xy01_s32x4); Chris@69: xy02_s64x2 = vpaddlq_s32(xy02_s32x4); Chris@69: xy01_s64x1 = vadd_s64(vget_low_s64(xy01_s64x2), vget_high_s64(xy01_s64x2)); Chris@69: xy02_s64x1 = vadd_s64(vget_low_s64(xy02_s64x2), vget_high_s64(xy02_s64x2)); Chris@69: xy01 = vget_lane_s32(vreinterpret_s32_s64(xy01_s64x1), 0); Chris@69: xy02 = vget_lane_s32(vreinterpret_s32_s64(xy02_s64x1), 0); Chris@69: Chris@69: for (; i < N; i++) { Chris@69: xy01 = MAC16_16(xy01, x[i], y01[i]); Chris@69: xy02 = MAC16_16(xy02, x[i], y02[i]); Chris@69: } Chris@69: *xy1 = xy01; Chris@69: *xy2 = xy02; Chris@69: Chris@69: #ifdef OPUS_CHECK_ASM Chris@69: { Chris@69: opus_val32 xy1_c, xy2_c; Chris@69: dual_inner_prod_c(x, y01, y02, N, &xy1_c, &xy2_c); Chris@69: celt_assert(xy1_c == *xy1); Chris@69: celt_assert(xy2_c == *xy2); Chris@69: } Chris@69: #endif Chris@69: } Chris@69: Chris@69: #else /* !FIXED_POINT */ Chris@69: Chris@69: /* ========================================================================== */ Chris@69: Chris@69: #ifdef OPUS_CHECK_ASM Chris@69: Chris@69: /* This part of code simulates floating-point NEON operations. */ Chris@69: Chris@69: /* celt_inner_prod_neon_float_c_simulation() simulates the floating-point */ Chris@69: /* operations of celt_inner_prod_neon(), and both functions should have bit */ Chris@69: /* exact output. */ Chris@69: static opus_val32 celt_inner_prod_neon_float_c_simulation(const opus_val16 *x, const opus_val16 *y, int N) Chris@69: { Chris@69: int i; Chris@69: opus_val32 xy, xy0 = 0, xy1 = 0, xy2 = 0, xy3 = 0; Chris@69: for (i = 0; i < N - 3; i += 4) { Chris@69: xy0 = MAC16_16(xy0, x[i + 0], y[i + 0]); Chris@69: xy1 = MAC16_16(xy1, x[i + 1], y[i + 1]); Chris@69: xy2 = MAC16_16(xy2, x[i + 2], y[i + 2]); Chris@69: xy3 = MAC16_16(xy3, x[i + 3], y[i + 3]); Chris@69: } Chris@69: xy0 += xy2; Chris@69: xy1 += xy3; Chris@69: xy = xy0 + xy1; Chris@69: for (; i < N; i++) { Chris@69: xy = MAC16_16(xy, x[i], y[i]); Chris@69: } Chris@69: return xy; Chris@69: } Chris@69: Chris@69: /* dual_inner_prod_neon_float_c_simulation() simulates the floating-point */ Chris@69: /* operations of dual_inner_prod_neon(), and both functions should have bit */ Chris@69: /* exact output. */ Chris@69: static void dual_inner_prod_neon_float_c_simulation(const opus_val16 *x, const opus_val16 *y01, const opus_val16 *y02, Chris@69: int N, opus_val32 *xy1, opus_val32 *xy2) Chris@69: { Chris@69: int i; Chris@69: opus_val32 xy01, xy02, xy01_0 = 0, xy01_1 = 0, xy01_2 = 0, xy01_3 = 0, xy02_0 = 0, xy02_1 = 0, xy02_2 = 0, xy02_3 = 0; Chris@69: for (i = 0; i < N - 3; i += 4) { Chris@69: xy01_0 = MAC16_16(xy01_0, x[i + 0], y01[i + 0]); Chris@69: xy01_1 = MAC16_16(xy01_1, x[i + 1], y01[i + 1]); Chris@69: xy01_2 = MAC16_16(xy01_2, x[i + 2], y01[i + 2]); Chris@69: xy01_3 = MAC16_16(xy01_3, x[i + 3], y01[i + 3]); Chris@69: xy02_0 = MAC16_16(xy02_0, x[i + 0], y02[i + 0]); Chris@69: xy02_1 = MAC16_16(xy02_1, x[i + 1], y02[i + 1]); Chris@69: xy02_2 = MAC16_16(xy02_2, x[i + 2], y02[i + 2]); Chris@69: xy02_3 = MAC16_16(xy02_3, x[i + 3], y02[i + 3]); Chris@69: } Chris@69: xy01_0 += xy01_2; Chris@69: xy02_0 += xy02_2; Chris@69: xy01_1 += xy01_3; Chris@69: xy02_1 += xy02_3; Chris@69: xy01 = xy01_0 + xy01_1; Chris@69: xy02 = xy02_0 + xy02_1; Chris@69: for (; i < N; i++) { Chris@69: xy01 = MAC16_16(xy01, x[i], y01[i]); Chris@69: xy02 = MAC16_16(xy02, x[i], y02[i]); Chris@69: } Chris@69: *xy1 = xy01; Chris@69: *xy2 = xy02; Chris@69: } Chris@69: Chris@69: #endif /* OPUS_CHECK_ASM */ Chris@69: Chris@69: /* ========================================================================== */ Chris@69: Chris@69: opus_val32 celt_inner_prod_neon(const opus_val16 *x, const opus_val16 *y, int N) Chris@69: { Chris@69: int i; Chris@69: opus_val32 xy; Chris@69: float32x4_t xy_f32x4 = vdupq_n_f32(0); Chris@69: float32x2_t xy_f32x2; Chris@69: Chris@69: for (i = 0; i < N - 7; i += 8) { Chris@69: float32x4_t x_f32x4, y_f32x4; Chris@69: x_f32x4 = vld1q_f32(&x[i]); Chris@69: y_f32x4 = vld1q_f32(&y[i]); Chris@69: xy_f32x4 = vmlaq_f32(xy_f32x4, x_f32x4, y_f32x4); Chris@69: x_f32x4 = vld1q_f32(&x[i + 4]); Chris@69: y_f32x4 = vld1q_f32(&y[i + 4]); Chris@69: xy_f32x4 = vmlaq_f32(xy_f32x4, x_f32x4, y_f32x4); Chris@69: } Chris@69: Chris@69: if (N - i >= 4) { Chris@69: const float32x4_t x_f32x4 = vld1q_f32(&x[i]); Chris@69: const float32x4_t y_f32x4 = vld1q_f32(&y[i]); Chris@69: xy_f32x4 = vmlaq_f32(xy_f32x4, x_f32x4, y_f32x4); Chris@69: i += 4; Chris@69: } Chris@69: Chris@69: xy_f32x2 = vadd_f32(vget_low_f32(xy_f32x4), vget_high_f32(xy_f32x4)); Chris@69: xy_f32x2 = vpadd_f32(xy_f32x2, xy_f32x2); Chris@69: xy = vget_lane_f32(xy_f32x2, 0); Chris@69: Chris@69: for (; i < N; i++) { Chris@69: xy = MAC16_16(xy, x[i], y[i]); Chris@69: } Chris@69: Chris@69: #ifdef OPUS_CHECK_ASM Chris@69: celt_assert(ABS32(celt_inner_prod_neon_float_c_simulation(x, y, N) - xy) <= VERY_SMALL); Chris@69: #endif Chris@69: Chris@69: return xy; Chris@69: } Chris@69: Chris@69: void dual_inner_prod_neon(const opus_val16 *x, const opus_val16 *y01, const opus_val16 *y02, Chris@69: int N, opus_val32 *xy1, opus_val32 *xy2) Chris@69: { Chris@69: int i; Chris@69: opus_val32 xy01, xy02; Chris@69: float32x4_t xy01_f32x4 = vdupq_n_f32(0); Chris@69: float32x4_t xy02_f32x4 = vdupq_n_f32(0); Chris@69: float32x2_t xy01_f32x2, xy02_f32x2; Chris@69: Chris@69: for (i = 0; i < N - 7; i += 8) { Chris@69: float32x4_t x_f32x4, y01_f32x4, y02_f32x4; Chris@69: x_f32x4 = vld1q_f32(&x[i]); Chris@69: y01_f32x4 = vld1q_f32(&y01[i]); Chris@69: y02_f32x4 = vld1q_f32(&y02[i]); Chris@69: xy01_f32x4 = vmlaq_f32(xy01_f32x4, x_f32x4, y01_f32x4); Chris@69: xy02_f32x4 = vmlaq_f32(xy02_f32x4, x_f32x4, y02_f32x4); Chris@69: x_f32x4 = vld1q_f32(&x[i + 4]); Chris@69: y01_f32x4 = vld1q_f32(&y01[i + 4]); Chris@69: y02_f32x4 = vld1q_f32(&y02[i + 4]); Chris@69: xy01_f32x4 = vmlaq_f32(xy01_f32x4, x_f32x4, y01_f32x4); Chris@69: xy02_f32x4 = vmlaq_f32(xy02_f32x4, x_f32x4, y02_f32x4); Chris@69: } Chris@69: Chris@69: if (N - i >= 4) { Chris@69: const float32x4_t x_f32x4 = vld1q_f32(&x[i]); Chris@69: const float32x4_t y01_f32x4 = vld1q_f32(&y01[i]); Chris@69: const float32x4_t y02_f32x4 = vld1q_f32(&y02[i]); Chris@69: xy01_f32x4 = vmlaq_f32(xy01_f32x4, x_f32x4, y01_f32x4); Chris@69: xy02_f32x4 = vmlaq_f32(xy02_f32x4, x_f32x4, y02_f32x4); Chris@69: i += 4; Chris@69: } Chris@69: Chris@69: xy01_f32x2 = vadd_f32(vget_low_f32(xy01_f32x4), vget_high_f32(xy01_f32x4)); Chris@69: xy02_f32x2 = vadd_f32(vget_low_f32(xy02_f32x4), vget_high_f32(xy02_f32x4)); Chris@69: xy01_f32x2 = vpadd_f32(xy01_f32x2, xy01_f32x2); Chris@69: xy02_f32x2 = vpadd_f32(xy02_f32x2, xy02_f32x2); Chris@69: xy01 = vget_lane_f32(xy01_f32x2, 0); Chris@69: xy02 = vget_lane_f32(xy02_f32x2, 0); Chris@69: Chris@69: for (; i < N; i++) { Chris@69: xy01 = MAC16_16(xy01, x[i], y01[i]); Chris@69: xy02 = MAC16_16(xy02, x[i], y02[i]); Chris@69: } Chris@69: *xy1 = xy01; Chris@69: *xy2 = xy02; Chris@69: Chris@69: #ifdef OPUS_CHECK_ASM Chris@69: { Chris@69: opus_val32 xy1_c, xy2_c; Chris@69: dual_inner_prod_neon_float_c_simulation(x, y01, y02, N, &xy1_c, &xy2_c); Chris@69: celt_assert(ABS32(xy1_c - *xy1) <= VERY_SMALL); Chris@69: celt_assert(ABS32(xy2_c - *xy2) <= VERY_SMALL); Chris@69: } Chris@69: #endif Chris@69: } Chris@69: Chris@69: #endif /* FIXED_POINT */