1 /* libFLAC - Free Lossless Audio Codec library
2 * Copyright (C) 2000-2009 Josh Coalson
3 * Copyright (C) 2011-2016 Xiph.Org Foundation
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
9 * - Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
12 * - Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * - Neither the name of the Xiph.org Foundation nor the names of its
17 * contributors may be used to endorse or promote products derived from
18 * this software without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR
24 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
27 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
28 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
29 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
30 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37 #include "private/cpu.h"
40 #if (defined FLAC__CPU_IA32 || defined FLAC__CPU_X86_64) && FLAC__HAS_X86INTRIN
41 #include "private/stream_encoder.h"
42 #include "private/bitmath.h"
43 #ifdef FLAC__AVX2_SUPPORTED
45 #include <stdlib.h> /* for abs() */
46 #include <immintrin.h> /* AVX2 */
47 #include "FLAC/assert.h"
49 FLAC__SSE_TARGET("avx2")
50 void FLAC__precompute_partition_info_sums_intrin_avx2(const FLAC__int32 residual[], FLAC__uint64 abs_residual_partition_sums[],
51 unsigned residual_samples, unsigned predictor_order, unsigned min_partition_order, unsigned max_partition_order, unsigned bps)
53 const unsigned default_partition_samples = (residual_samples + predictor_order) >> max_partition_order;
54 unsigned partitions = 1u << max_partition_order;
56 FLAC__ASSERT(default_partition_samples > predictor_order);
58 /* first do max_partition_order */
60 const unsigned threshold = 32 - FLAC__bitmath_ilog2(default_partition_samples);
61 unsigned partition, residual_sample, end = (unsigned)(-(int)predictor_order);
63 if(bps + FLAC__MAX_EXTRA_RESIDUAL_BPS < threshold) {
64 for(partition = residual_sample = 0; partition < partitions; partition++) {
65 __m256i sum256 = _mm256_setzero_si256();
67 end += default_partition_samples;
69 for( ; (int)residual_sample < (int)end-7; residual_sample+=8) {
70 __m256i res256 = _mm256_abs_epi32(_mm256_loadu_si256((const __m256i*)(residual+residual_sample)));
71 sum256 = _mm256_add_epi32(sum256, res256);
74 sum128 = _mm_add_epi32(_mm256_extracti128_si256(sum256, 1), _mm256_castsi256_si128(sum256));
76 for( ; (int)residual_sample < (int)end-3; residual_sample+=4) {
77 __m128i res128 = _mm_abs_epi32(_mm_loadu_si128((const __m128i*)(residual+residual_sample)));
78 sum128 = _mm_add_epi32(sum128, res128);
81 for( ; residual_sample < end; residual_sample++) {
82 __m128i res128 = _mm_abs_epi32(_mm_cvtsi32_si128(residual[residual_sample]));
83 sum128 = _mm_add_epi32(sum128, res128);
86 sum128 = _mm_hadd_epi32(sum128, sum128);
87 sum128 = _mm_hadd_epi32(sum128, sum128);
88 abs_residual_partition_sums[partition] = (FLAC__uint32)_mm_cvtsi128_si32(sum128);
89 /* workaround for a bug in MSVC2015U2 - see https://connect.microsoft.com/VisualStudio/feedback/details/2659191/incorrect-code-generation-for-x86-64 */
90 #if (defined _MSC_VER) && (_MSC_FULL_VER == 190023918) && (defined FLAC__CPU_X86_64)
91 abs_residual_partition_sums[partition] &= 0xFFFFFFFF; /**/
95 else { /* have to pessimistically use 64 bits for accumulator */
96 for(partition = residual_sample = 0; partition < partitions; partition++) {
97 __m256i sum256 = _mm256_setzero_si256();
99 end += default_partition_samples;
101 for( ; (int)residual_sample < (int)end-3; residual_sample+=4) {
102 __m128i res128 = _mm_abs_epi32(_mm_loadu_si128((const __m128i*)(residual+residual_sample)));
103 __m256i res256 = _mm256_cvtepu32_epi64(res128);
104 sum256 = _mm256_add_epi64(sum256, res256);
107 sum128 = _mm_add_epi64(_mm256_extracti128_si256(sum256, 1), _mm256_castsi256_si128(sum256));
109 for( ; (int)residual_sample < (int)end-1; residual_sample+=2) {
110 __m128i res128 = _mm_abs_epi32(_mm_loadl_epi64((const __m128i*)(residual+residual_sample)));
111 res128 = _mm_cvtepu32_epi64(res128);
112 sum128 = _mm_add_epi64(sum128, res128);
115 for( ; residual_sample < end; residual_sample++) {
116 __m128i res128 = _mm_abs_epi32(_mm_cvtsi32_si128(residual[residual_sample]));
117 sum128 = _mm_add_epi64(sum128, res128);
120 sum128 = _mm_add_epi64(sum128, _mm_srli_si128(sum128, 8));
121 _mm_storel_epi64((__m128i*)(abs_residual_partition_sums+partition), sum128);
126 /* now merge partitions for lower orders */
128 unsigned from_partition = 0, to_partition = partitions;
130 for(partition_order = (int)max_partition_order - 1; partition_order >= (int)min_partition_order; partition_order--) {
133 for(i = 0; i < partitions; i++) {
134 abs_residual_partition_sums[to_partition++] =
135 abs_residual_partition_sums[from_partition ] +
136 abs_residual_partition_sums[from_partition+1];
144 #endif /* FLAC__AVX2_SUPPORTED */
145 #endif /* (FLAC__CPU_IA32 || FLAC__CPU_X86_64) && FLAC__HAS_X86INTRIN */
146 #endif /* FLAC__NO_ASM */