1 /* libFLAC - Free Lossless Audio Codec library
2 * Copyright (C) 2000-2009 Josh Coalson
3 * Copyright (C) 2011-2016 Xiph.Org Foundation
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * - Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 *
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.
15 *
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.
19 *
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.
31 */
32
33 #ifdef HAVE_CONFIG_H
34 # include <config.h>
35 #endif
36
37 #include "private/cpu.h"
38
39 #ifndef FLAC__NO_ASM
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__SSE2_SUPPORTED
44
45 #include <stdlib.h> /* for abs() */
46 #include <emmintrin.h> /* SSE2 */
47 #include "FLAC/assert.h"
48 #include "share/compat.h"
49
50 FLAC__SSE_TARGET("sse2")
local_abs_epi32(__m128i val)51 static inline __m128i local_abs_epi32(__m128i val)
52 {
53 __m128i mask = _mm_srai_epi32(val, 31);
54 val = _mm_xor_si128(val, mask);
55 val = _mm_sub_epi32(val, mask);
56 return val;
57 }
58
59
60 FLAC__SSE_TARGET("sse2")
FLAC__precompute_partition_info_sums_intrin_sse2(const FLAC__int32 residual[],FLAC__uint64 abs_residual_partition_sums[],uint32_t residual_samples,uint32_t predictor_order,uint32_t min_partition_order,uint32_t max_partition_order,uint32_t bps)61 void FLAC__precompute_partition_info_sums_intrin_sse2(const FLAC__int32 residual[], FLAC__uint64 abs_residual_partition_sums[],
62 uint32_t residual_samples, uint32_t predictor_order, uint32_t min_partition_order, uint32_t max_partition_order, uint32_t bps)
63 {
64 const uint32_t default_partition_samples = (residual_samples + predictor_order) >> max_partition_order;
65 uint32_t partitions = 1u << max_partition_order;
66
67 FLAC__ASSERT(default_partition_samples > predictor_order);
68
69 /* first do max_partition_order */
70 {
71 const uint32_t threshold = 32 - FLAC__bitmath_ilog2(default_partition_samples);
72 uint32_t partition, residual_sample, end = (uint32_t)(-(int32_t)predictor_order);
73
74 if(bps + FLAC__MAX_EXTRA_RESIDUAL_BPS < threshold) {
75 for(partition = residual_sample = 0; partition < partitions; partition++) {
76 __m128i mm_sum = _mm_setzero_si128();
77 uint32_t e1, e3;
78 end += default_partition_samples;
79
80 e1 = (residual_sample + 3) & ~3; e3 = end & ~3;
81 if(e1 > end)
82 e1 = end; /* try flac -l 1 -b 16 and you'll be here */
83
84 /* assumption: residual[] is properly aligned so (residual + e1) is properly aligned too and _mm_loadu_si128() is fast */
85 for( ; residual_sample < e1; residual_sample++) {
86 __m128i mm_res = local_abs_epi32(_mm_cvtsi32_si128(residual[residual_sample]));
87 mm_sum = _mm_add_epi32(mm_sum, mm_res);
88 }
89
90 for( ; residual_sample < e3; residual_sample+=4) {
91 __m128i mm_res = local_abs_epi32(_mm_loadu_si128((const __m128i*)(residual+residual_sample)));
92 mm_sum = _mm_add_epi32(mm_sum, mm_res);
93 }
94
95 for( ; residual_sample < end; residual_sample++) {
96 __m128i mm_res = local_abs_epi32(_mm_cvtsi32_si128(residual[residual_sample]));
97 mm_sum = _mm_add_epi32(mm_sum, mm_res);
98 }
99
100 mm_sum = _mm_add_epi32(mm_sum, _mm_shuffle_epi32(mm_sum, _MM_SHUFFLE(1,0,3,2)));
101 mm_sum = _mm_add_epi32(mm_sum, _mm_shufflelo_epi16(mm_sum, _MM_SHUFFLE(1,0,3,2)));
102 abs_residual_partition_sums[partition] = (FLAC__uint32)_mm_cvtsi128_si32(mm_sum);
103 /* workaround for MSVC bugs (at least versions 2015 and 2017 are affected) */
104 #if (defined _MSC_VER) && (defined FLAC__CPU_X86_64)
105 abs_residual_partition_sums[partition] &= 0xFFFFFFFF;
106 #endif
107 }
108 }
109 else { /* have to pessimistically use 64 bits for accumulator */
110 for(partition = residual_sample = 0; partition < partitions; partition++) {
111 __m128i mm_sum = _mm_setzero_si128();
112 uint32_t e1, e3;
113 end += default_partition_samples;
114
115 e1 = (residual_sample + 1) & ~1; e3 = end & ~1;
116 FLAC__ASSERT(e1 <= end);
117
118 for( ; residual_sample < e1; residual_sample++) {
119 __m128i mm_res = local_abs_epi32(_mm_cvtsi32_si128(residual[residual_sample])); /* 0 0 0 |r0| == 00 |r0_64| */
120 mm_sum = _mm_add_epi64(mm_sum, mm_res);
121 }
122
123 for( ; residual_sample < e3; residual_sample+=2) {
124 __m128i mm_res = local_abs_epi32(_mm_loadl_epi64((const __m128i*)(residual+residual_sample))); /* 0 0 |r1| |r0| */
125 mm_res = _mm_shuffle_epi32(mm_res, _MM_SHUFFLE(3,1,2,0)); /* 0 |r1| 0 |r0| == |r1_64| |r0_64| */
126 mm_sum = _mm_add_epi64(mm_sum, mm_res);
127 }
128
129 for( ; residual_sample < end; residual_sample++) {
130 __m128i mm_res = local_abs_epi32(_mm_cvtsi32_si128(residual[residual_sample]));
131 mm_sum = _mm_add_epi64(mm_sum, mm_res);
132 }
133
134 mm_sum = _mm_add_epi64(mm_sum, _mm_srli_si128(mm_sum, 8));
135 _mm_storel_epi64((__m128i*)(abs_residual_partition_sums+partition), mm_sum);
136 }
137 }
138 }
139
140 /* now merge partitions for lower orders */
141 {
142 uint32_t from_partition = 0, to_partition = partitions;
143 int partition_order;
144 for(partition_order = (int)max_partition_order - 1; partition_order >= (int)min_partition_order; partition_order--) {
145 uint32_t i;
146 partitions >>= 1;
147 for(i = 0; i < partitions; i++) {
148 abs_residual_partition_sums[to_partition++] =
149 abs_residual_partition_sums[from_partition ] +
150 abs_residual_partition_sums[from_partition+1];
151 from_partition += 2;
152 }
153 }
154 }
155 }
156
157 #endif /* FLAC__SSE2_SUPPORTED */
158 #endif /* (FLAC__CPU_IA32 || FLAC__CPU_X86_64) && FLAC__HAS_X86INTRIN */
159 #endif /* FLAC__NO_ASM */
160