25f7d369d65df04647c2ea7ac7d1826e227165ec
[flac.git] / src / libFLAC / fixed_intrin_sse2.c
1 /* libFLAC - Free Lossless Audio Codec library
2  * Copyright (C) 2000-2009  Josh Coalson
3  * Copyright (C) 2011-2014  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 #ifndef FLAC__INTEGER_ONLY_LIBRARY
38 #ifndef FLAC__NO_ASM
39 #if (defined FLAC__CPU_IA32 || defined FLAC__CPU_X86_64) && defined FLAC__HAS_X86INTRIN
40 #include "private/fixed.h"
41 #ifdef FLAC__SSE2_SUPPORTED
42
43 #include <emmintrin.h> /* SSE2 */
44 #include <math.h>
45 #include "private/macros.h"
46 #include "share/compat.h"
47 #include "FLAC/assert.h"
48
49 #ifdef FLAC__CPU_IA32
50 union zz_cvt
51 {
52         int64_t i64;
53         int32_t i32[2];
54 };
55
56 FLAC__SSE_TARGET("sse2")
57 static inline int64_t _zz_cvtsi128_si64(__m128i a)
58 {
59         union zz_cvt z;
60         z.i32[0] = _mm_cvtsi128_si32(a);
61         z.i32[1] = _mm_cvtsi128_si32(_mm_srli_si128(a, 4));
62         return z.i64;
63 }
64
65 #define _mm_cvtsi128_si64 _zz_cvtsi128_si64
66 #endif
67
68 FLAC__SSE_TARGET("sse2")
69 unsigned FLAC__fixed_compute_best_predictor_intrin_sse2(const FLAC__int32 data[], unsigned data_len, FLAC__float residual_bits_per_sample[FLAC__MAX_FIXED_ORDER + 1])
70 {
71         FLAC__uint32 total_error_0, total_error_1, total_error_2, total_error_3, total_error_4;
72         unsigned i, order;
73
74         __m128i total_err0, total_err1, total_err2;
75
76         {
77                 FLAC__int32 itmp;
78                 __m128i last_error;
79
80                 last_error = _mm_cvtsi32_si128(data[-1]);                                                       // 0   0   0   le0
81                 itmp = data[-2];
82                 last_error = _mm_shuffle_epi32(last_error, _MM_SHUFFLE(2,1,0,0));
83                 last_error = _mm_sub_epi32(last_error, _mm_cvtsi32_si128(itmp));        // 0   0   le0 le1
84                 itmp -= data[-3];
85                 last_error = _mm_shuffle_epi32(last_error, _MM_SHUFFLE(2,1,0,0));
86                 last_error = _mm_sub_epi32(last_error, _mm_cvtsi32_si128(itmp));        // 0   le0 le1 le2
87                 itmp -= data[-3] - data[-4];
88                 last_error = _mm_shuffle_epi32(last_error, _MM_SHUFFLE(2,1,0,0));
89                 last_error = _mm_sub_epi32(last_error, _mm_cvtsi32_si128(itmp));        // le0 le1 le2 le3
90
91                 total_err0 = total_err1 = _mm_setzero_si128();
92                 for(i = 0; i < data_len; i++) {
93                         __m128i err0, err1, tmp;
94                         err0 = _mm_cvtsi32_si128(data[i]);                                                              // 0   0   0   e0
95                         err1 = _mm_shuffle_epi32(err0, _MM_SHUFFLE(0,0,0,0));                   // e0  e0  e0  e0
96                         err1 = _mm_sub_epi32(err1, last_error);
97                         last_error = _mm_srli_si128(last_error, 4);                                             // 0   le0 le1 le2
98                         err1 = _mm_sub_epi32(err1, last_error);
99                         last_error = _mm_srli_si128(last_error, 4);                                             // 0   0   le0 le1
100                         err1 = _mm_sub_epi32(err1, last_error);
101                         last_error = _mm_srli_si128(last_error, 4);                                             // 0   0   0   le0
102                         err1 = _mm_sub_epi32(err1, last_error);                                                 // e1  e2  e3  e4
103
104                         tmp = _mm_slli_si128(err0, 12);                                                                 // e0   0   0   0
105                         last_error = _mm_srli_si128(err1, 4);                                                   //  0  e0  e1  e2
106                         last_error = _mm_or_si128(last_error, tmp);                                             // e0  e1  e2  e3
107
108                         tmp = _mm_srai_epi32(err0, 31);
109                         err0 = _mm_xor_si128(err0, tmp);
110                         err0 = _mm_sub_epi32(err0, tmp);
111                         tmp = _mm_srai_epi32(err1, 31);
112                         err1 = _mm_xor_si128(err1, tmp);
113                         err1 = _mm_sub_epi32(err1, tmp);
114
115                         total_err0 = _mm_add_epi32(total_err0, err0);                                   // 0   0   0   te0
116                         total_err1 = _mm_add_epi32(total_err1, err1);                                   // te1 te2 te3 te4
117                 }
118         }
119         
120         total_error_0 = _mm_cvtsi128_si32(total_err0);
121         total_err2 = total_err1;                                                                                        // te1  te2  te3  te4
122         total_err1 = _mm_srli_si128(total_err1, 8);                                                     //  0    0   te1  te2
123         total_error_4 = _mm_cvtsi128_si32(total_err2);
124         total_error_2 = _mm_cvtsi128_si32(total_err1);
125         total_err2 = _mm_srli_si128(total_err2, 4);                                                     //  0   te1  te2  te3
126         total_err1 = _mm_srli_si128(total_err1, 4);                                                     //  0    0    0   te1
127         total_error_3 = _mm_cvtsi128_si32(total_err2);
128         total_error_1 = _mm_cvtsi128_si32(total_err1);
129
130         /* prefer higher order */
131         if(total_error_0 < flac_min(flac_min(flac_min(total_error_1, total_error_2), total_error_3), total_error_4))
132                 order = 0;
133         else if(total_error_1 < flac_min(flac_min(total_error_2, total_error_3), total_error_4))
134                 order = 1;
135         else if(total_error_2 < flac_min(total_error_3, total_error_4))
136                 order = 2;
137         else if(total_error_3 < total_error_4)
138                 order = 3;
139         else
140                 order = 4;
141
142         /* Estimate the expected number of bits per residual signal sample. */
143         /* 'total_error*' is linearly related to the variance of the residual */
144         /* signal, so we use it directly to compute E(|x|) */
145         FLAC__ASSERT(data_len > 0 || total_error_0 == 0);
146         FLAC__ASSERT(data_len > 0 || total_error_1 == 0);
147         FLAC__ASSERT(data_len > 0 || total_error_2 == 0);
148         FLAC__ASSERT(data_len > 0 || total_error_3 == 0);
149         FLAC__ASSERT(data_len > 0 || total_error_4 == 0);
150
151         residual_bits_per_sample[0] = (FLAC__float)((total_error_0 > 0) ? log(M_LN2 * (FLAC__double)total_error_0 / (FLAC__double)data_len) / M_LN2 : 0.0);
152         residual_bits_per_sample[1] = (FLAC__float)((total_error_1 > 0) ? log(M_LN2 * (FLAC__double)total_error_1 / (FLAC__double)data_len) / M_LN2 : 0.0);
153         residual_bits_per_sample[2] = (FLAC__float)((total_error_2 > 0) ? log(M_LN2 * (FLAC__double)total_error_2 / (FLAC__double)data_len) / M_LN2 : 0.0);
154         residual_bits_per_sample[3] = (FLAC__float)((total_error_3 > 0) ? log(M_LN2 * (FLAC__double)total_error_3 / (FLAC__double)data_len) / M_LN2 : 0.0);
155         residual_bits_per_sample[4] = (FLAC__float)((total_error_4 > 0) ? log(M_LN2 * (FLAC__double)total_error_4 / (FLAC__double)data_len) / M_LN2 : 0.0);
156
157         return order;
158 }
159
160 FLAC__SSE_TARGET("sse2")
161 unsigned FLAC__fixed_compute_best_predictor_wide_intrin_sse2(const FLAC__int32 data[], unsigned data_len, FLAC__float residual_bits_per_sample[FLAC__MAX_FIXED_ORDER + 1])
162 {
163         FLAC__uint64 total_error_0, total_error_1, total_error_2, total_error_3, total_error_4;
164         unsigned i, order;
165
166         __m128i total_err0, total_err1, total_err3;
167
168         {
169                 FLAC__int32 itmp;
170                 __m128i last_error, zero = _mm_setzero_si128();
171                 
172                 last_error = _mm_cvtsi32_si128(data[-1]);                                                       // 0   0   0   le0
173                 itmp = data[-2];
174                 last_error = _mm_shuffle_epi32(last_error, _MM_SHUFFLE(2,1,0,0));
175                 last_error = _mm_sub_epi32(last_error, _mm_cvtsi32_si128(itmp));        // 0   0   le0 le1
176                 itmp -= data[-3];
177                 last_error = _mm_shuffle_epi32(last_error, _MM_SHUFFLE(2,1,0,0));
178                 last_error = _mm_sub_epi32(last_error, _mm_cvtsi32_si128(itmp));        // 0   le0 le1 le2
179                 itmp -= data[-3] - data[-4];
180                 last_error = _mm_shuffle_epi32(last_error, _MM_SHUFFLE(2,1,0,0));
181                 last_error = _mm_sub_epi32(last_error, _mm_cvtsi32_si128(itmp));        // le0 le1 le2 le3
182
183                 total_err0 = total_err1 = total_err3 = _mm_setzero_si128();
184                 for(i = 0; i < data_len; i++) {
185                         __m128i err0, err1, tmp;
186                         err0 = _mm_cvtsi32_si128(data[i]);                                                              // 0   0   0   e0
187                         err1 = _mm_shuffle_epi32(err0, _MM_SHUFFLE(0,0,0,0));                   // e0  e0  e0  e0
188                         err1 = _mm_sub_epi32(err1, last_error);
189                         last_error = _mm_srli_si128(last_error, 4);                                             // 0   le0 le1 le2
190                         err1 = _mm_sub_epi32(err1, last_error);
191                         last_error = _mm_srli_si128(last_error, 4);                                             // 0   0   le0 le1
192                         err1 = _mm_sub_epi32(err1, last_error);
193                         last_error = _mm_srli_si128(last_error, 4);                                             // 0   0   0   le0
194                         err1 = _mm_sub_epi32(err1, last_error);                                                 // e1  e2  e3  e4
195
196                         tmp = _mm_slli_si128(err0, 12);                                                                 // e0   0   0   0
197                         last_error = _mm_srli_si128(err1, 4);                                                   //  0  e0  e1  e2
198                         last_error = _mm_or_si128(last_error, tmp);                                             // e0  e1  e2  e3
199
200                         tmp = _mm_srai_epi32(err0, 31);
201                         err0 = _mm_xor_si128(err0, tmp);
202                         err0 = _mm_sub_epi32(err0, tmp);
203                         tmp = _mm_srai_epi32(err1, 31);
204                         err1 = _mm_xor_si128(err1, tmp);
205                         err1 = _mm_sub_epi32(err1, tmp);
206
207                         total_err0 = _mm_add_epi64(total_err0, err0);                                   //        0       te0
208                         err0 = _mm_unpacklo_epi32(err1, zero);                                                  //   0  |e3|   0  |e4|
209                         err1 = _mm_unpackhi_epi32(err1, zero);                                                  //   0  |e1|   0  |e2|
210                         total_err3 = _mm_add_epi64(total_err3, err0);                                   //       te3      te4
211                         total_err1 = _mm_add_epi64(total_err1, err1);                                   //       te1      te2
212                 }
213         }
214         
215         total_error_0 = _mm_cvtsi128_si64(total_err0);
216         total_error_4 = _mm_cvtsi128_si64(total_err3);
217         total_error_2 = _mm_cvtsi128_si64(total_err1);
218         total_err3 = _mm_srli_si128(total_err3, 8);                                                     //         0      te3
219         total_err1 = _mm_srli_si128(total_err1, 8);                                                     //         0      te1
220         total_error_3 = _mm_cvtsi128_si64(total_err3);
221         total_error_1 = _mm_cvtsi128_si64(total_err1);
222
223         /* prefer higher order */
224         if(total_error_0 < flac_min(flac_min(flac_min(total_error_1, total_error_2), total_error_3), total_error_4))
225                 order = 0;
226         else if(total_error_1 < flac_min(flac_min(total_error_2, total_error_3), total_error_4))
227                 order = 1;
228         else if(total_error_2 < flac_min(total_error_3, total_error_4))
229                 order = 2;
230         else if(total_error_3 < total_error_4)
231                 order = 3;
232         else
233                 order = 4;
234
235         /* Estimate the expected number of bits per residual signal sample. */
236         /* 'total_error*' is linearly related to the variance of the residual */
237         /* signal, so we use it directly to compute E(|x|) */
238         FLAC__ASSERT(data_len > 0 || total_error_0 == 0);
239         FLAC__ASSERT(data_len > 0 || total_error_1 == 0);
240         FLAC__ASSERT(data_len > 0 || total_error_2 == 0);
241         FLAC__ASSERT(data_len > 0 || total_error_3 == 0);
242         FLAC__ASSERT(data_len > 0 || total_error_4 == 0);
243
244         residual_bits_per_sample[0] = (FLAC__float)((total_error_0 > 0) ? log(M_LN2 * (FLAC__double)total_error_0 / (FLAC__double)data_len) / M_LN2 : 0.0);
245         residual_bits_per_sample[1] = (FLAC__float)((total_error_1 > 0) ? log(M_LN2 * (FLAC__double)total_error_1 / (FLAC__double)data_len) / M_LN2 : 0.0);
246         residual_bits_per_sample[2] = (FLAC__float)((total_error_2 > 0) ? log(M_LN2 * (FLAC__double)total_error_2 / (FLAC__double)data_len) / M_LN2 : 0.0);
247         residual_bits_per_sample[3] = (FLAC__float)((total_error_3 > 0) ? log(M_LN2 * (FLAC__double)total_error_3 / (FLAC__double)data_len) / M_LN2 : 0.0);
248         residual_bits_per_sample[4] = (FLAC__float)((total_error_4 > 0) ? log(M_LN2 * (FLAC__double)total_error_4 / (FLAC__double)data_len) / M_LN2 : 0.0);
249
250         return order;
251 }
252
253 #endif /* FLAC__SSE2_SUPPORTED */
254 #endif /* (FLAC__CPU_IA32 || FLAC__CPU_X86_64) && FLAC__HAS_X86INTRIN */
255 #endif /* FLAC__NO_ASM */
256 #endif /* FLAC__INTEGER_ONLY_LIBRARY */