fixed_intrin_sse[23].c : Simplify XMM -> int64 conversion.
[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 #define m128i_to_i64(dest, src) _mm_storel_epi64((__m128i*)&dest, src)
51 #else
52 #define m128i_to_i64(dest, src) dest = _mm_cvtsi128_si64(src)
53 #endif
54
55 FLAC__SSE_TARGET("sse2")
56 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])
57 {
58         FLAC__uint32 total_error_0, total_error_1, total_error_2, total_error_3, total_error_4;
59         unsigned i, order;
60
61         __m128i total_err0, total_err1, total_err2;
62
63         {
64                 FLAC__int32 itmp;
65                 __m128i last_error;
66
67                 last_error = _mm_cvtsi32_si128(data[-1]);                                                       // 0   0   0   le0
68                 itmp = data[-2];
69                 last_error = _mm_shuffle_epi32(last_error, _MM_SHUFFLE(2,1,0,0));
70                 last_error = _mm_sub_epi32(last_error, _mm_cvtsi32_si128(itmp));        // 0   0   le0 le1
71                 itmp -= data[-3];
72                 last_error = _mm_shuffle_epi32(last_error, _MM_SHUFFLE(2,1,0,0));
73                 last_error = _mm_sub_epi32(last_error, _mm_cvtsi32_si128(itmp));        // 0   le0 le1 le2
74                 itmp -= data[-3] - data[-4];
75                 last_error = _mm_shuffle_epi32(last_error, _MM_SHUFFLE(2,1,0,0));
76                 last_error = _mm_sub_epi32(last_error, _mm_cvtsi32_si128(itmp));        // le0 le1 le2 le3
77
78                 total_err0 = total_err1 = _mm_setzero_si128();
79                 for(i = 0; i < data_len; i++) {
80                         __m128i err0, err1, tmp;
81                         err0 = _mm_cvtsi32_si128(data[i]);                                                              // 0   0   0   e0
82                         err1 = _mm_shuffle_epi32(err0, _MM_SHUFFLE(0,0,0,0));                   // e0  e0  e0  e0
83                         err1 = _mm_sub_epi32(err1, last_error);
84                         last_error = _mm_srli_si128(last_error, 4);                                             // 0   le0 le1 le2
85                         err1 = _mm_sub_epi32(err1, last_error);
86                         last_error = _mm_srli_si128(last_error, 4);                                             // 0   0   le0 le1
87                         err1 = _mm_sub_epi32(err1, last_error);
88                         last_error = _mm_srli_si128(last_error, 4);                                             // 0   0   0   le0
89                         err1 = _mm_sub_epi32(err1, last_error);                                                 // e1  e2  e3  e4
90
91                         tmp = _mm_slli_si128(err0, 12);                                                                 // e0   0   0   0
92                         last_error = _mm_srli_si128(err1, 4);                                                   //  0  e0  e1  e2
93                         last_error = _mm_or_si128(last_error, tmp);                                             // e0  e1  e2  e3
94
95                         tmp = _mm_srai_epi32(err0, 31);
96                         err0 = _mm_xor_si128(err0, tmp);
97                         err0 = _mm_sub_epi32(err0, tmp);
98                         tmp = _mm_srai_epi32(err1, 31);
99                         err1 = _mm_xor_si128(err1, tmp);
100                         err1 = _mm_sub_epi32(err1, tmp);
101
102                         total_err0 = _mm_add_epi32(total_err0, err0);                                   // 0   0   0   te0
103                         total_err1 = _mm_add_epi32(total_err1, err1);                                   // te1 te2 te3 te4
104                 }
105         }
106         
107         total_error_0 = _mm_cvtsi128_si32(total_err0);
108         total_err2 = total_err1;                                                                                        // te1  te2  te3  te4
109         total_err1 = _mm_srli_si128(total_err1, 8);                                                     //  0    0   te1  te2
110         total_error_4 = _mm_cvtsi128_si32(total_err2);
111         total_error_2 = _mm_cvtsi128_si32(total_err1);
112         total_err2 = _mm_srli_si128(total_err2, 4);                                                     //  0   te1  te2  te3
113         total_err1 = _mm_srli_si128(total_err1, 4);                                                     //  0    0    0   te1
114         total_error_3 = _mm_cvtsi128_si32(total_err2);
115         total_error_1 = _mm_cvtsi128_si32(total_err1);
116
117         /* prefer higher order */
118         if(total_error_0 < flac_min(flac_min(flac_min(total_error_1, total_error_2), total_error_3), total_error_4))
119                 order = 0;
120         else if(total_error_1 < flac_min(flac_min(total_error_2, total_error_3), total_error_4))
121                 order = 1;
122         else if(total_error_2 < flac_min(total_error_3, total_error_4))
123                 order = 2;
124         else if(total_error_3 < total_error_4)
125                 order = 3;
126         else
127                 order = 4;
128
129         /* Estimate the expected number of bits per residual signal sample. */
130         /* 'total_error*' is linearly related to the variance of the residual */
131         /* signal, so we use it directly to compute E(|x|) */
132         FLAC__ASSERT(data_len > 0 || total_error_0 == 0);
133         FLAC__ASSERT(data_len > 0 || total_error_1 == 0);
134         FLAC__ASSERT(data_len > 0 || total_error_2 == 0);
135         FLAC__ASSERT(data_len > 0 || total_error_3 == 0);
136         FLAC__ASSERT(data_len > 0 || total_error_4 == 0);
137
138         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);
139         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);
140         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);
141         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);
142         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);
143
144         return order;
145 }
146
147 FLAC__SSE_TARGET("sse2")
148 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])
149 {
150         FLAC__uint64 total_error_0, total_error_1, total_error_2, total_error_3, total_error_4;
151         unsigned i, order;
152
153         __m128i total_err0, total_err1, total_err3;
154
155         {
156                 FLAC__int32 itmp;
157                 __m128i last_error, zero = _mm_setzero_si128();
158                 
159                 last_error = _mm_cvtsi32_si128(data[-1]);                                                       // 0   0   0   le0
160                 itmp = data[-2];
161                 last_error = _mm_shuffle_epi32(last_error, _MM_SHUFFLE(2,1,0,0));
162                 last_error = _mm_sub_epi32(last_error, _mm_cvtsi32_si128(itmp));        // 0   0   le0 le1
163                 itmp -= data[-3];
164                 last_error = _mm_shuffle_epi32(last_error, _MM_SHUFFLE(2,1,0,0));
165                 last_error = _mm_sub_epi32(last_error, _mm_cvtsi32_si128(itmp));        // 0   le0 le1 le2
166                 itmp -= data[-3] - data[-4];
167                 last_error = _mm_shuffle_epi32(last_error, _MM_SHUFFLE(2,1,0,0));
168                 last_error = _mm_sub_epi32(last_error, _mm_cvtsi32_si128(itmp));        // le0 le1 le2 le3
169
170                 total_err0 = total_err1 = total_err3 = _mm_setzero_si128();
171                 for(i = 0; i < data_len; i++) {
172                         __m128i err0, err1, tmp;
173                         err0 = _mm_cvtsi32_si128(data[i]);                                                              // 0   0   0   e0
174                         err1 = _mm_shuffle_epi32(err0, _MM_SHUFFLE(0,0,0,0));                   // e0  e0  e0  e0
175                         err1 = _mm_sub_epi32(err1, last_error);
176                         last_error = _mm_srli_si128(last_error, 4);                                             // 0   le0 le1 le2
177                         err1 = _mm_sub_epi32(err1, last_error);
178                         last_error = _mm_srli_si128(last_error, 4);                                             // 0   0   le0 le1
179                         err1 = _mm_sub_epi32(err1, last_error);
180                         last_error = _mm_srli_si128(last_error, 4);                                             // 0   0   0   le0
181                         err1 = _mm_sub_epi32(err1, last_error);                                                 // e1  e2  e3  e4
182
183                         tmp = _mm_slli_si128(err0, 12);                                                                 // e0   0   0   0
184                         last_error = _mm_srli_si128(err1, 4);                                                   //  0  e0  e1  e2
185                         last_error = _mm_or_si128(last_error, tmp);                                             // e0  e1  e2  e3
186
187                         tmp = _mm_srai_epi32(err0, 31);
188                         err0 = _mm_xor_si128(err0, tmp);
189                         err0 = _mm_sub_epi32(err0, tmp);
190                         tmp = _mm_srai_epi32(err1, 31);
191                         err1 = _mm_xor_si128(err1, tmp);
192                         err1 = _mm_sub_epi32(err1, tmp);
193
194                         total_err0 = _mm_add_epi64(total_err0, err0);                                   //        0       te0
195                         err0 = _mm_unpacklo_epi32(err1, zero);                                                  //   0  |e3|   0  |e4|
196                         err1 = _mm_unpackhi_epi32(err1, zero);                                                  //   0  |e1|   0  |e2|
197                         total_err3 = _mm_add_epi64(total_err3, err0);                                   //       te3      te4
198                         total_err1 = _mm_add_epi64(total_err1, err1);                                   //       te1      te2
199                 }
200         }
201         
202         m128i_to_i64(total_error_0, total_err0);
203         m128i_to_i64(total_error_4, total_err3);
204         m128i_to_i64(total_error_2, total_err1);
205         total_err3 = _mm_srli_si128(total_err3, 8);                                                     //         0      te3
206         total_err1 = _mm_srli_si128(total_err1, 8);                                                     //         0      te1
207         m128i_to_i64(total_error_3, total_err3);
208         m128i_to_i64(total_error_1, total_err1);
209
210         /* prefer higher order */
211         if(total_error_0 < flac_min(flac_min(flac_min(total_error_1, total_error_2), total_error_3), total_error_4))
212                 order = 0;
213         else if(total_error_1 < flac_min(flac_min(total_error_2, total_error_3), total_error_4))
214                 order = 1;
215         else if(total_error_2 < flac_min(total_error_3, total_error_4))
216                 order = 2;
217         else if(total_error_3 < total_error_4)
218                 order = 3;
219         else
220                 order = 4;
221
222         /* Estimate the expected number of bits per residual signal sample. */
223         /* 'total_error*' is linearly related to the variance of the residual */
224         /* signal, so we use it directly to compute E(|x|) */
225         FLAC__ASSERT(data_len > 0 || total_error_0 == 0);
226         FLAC__ASSERT(data_len > 0 || total_error_1 == 0);
227         FLAC__ASSERT(data_len > 0 || total_error_2 == 0);
228         FLAC__ASSERT(data_len > 0 || total_error_3 == 0);
229         FLAC__ASSERT(data_len > 0 || total_error_4 == 0);
230
231         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);
232         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);
233         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);
234         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);
235         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);
236
237         return order;
238 }
239
240 #endif /* FLAC__SSE2_SUPPORTED */
241 #endif /* (FLAC__CPU_IA32 || FLAC__CPU_X86_64) && FLAC__HAS_X86INTRIN */
242 #endif /* FLAC__NO_ASM */
243 #endif /* FLAC__INTEGER_ONLY_LIBRARY */