blob: ed01978c383cb16120b309544b0df5e17f537db1 [file] [log] [blame]
Erik de Castro Lopoecd0acb2013-10-04 01:38:00 +10001/* libFLAC - Free Lossless Audio Codec library
2 * Copyright (C) 2000-2009 Josh Coalson
Erik de Castro Lopo14373912014-11-24 22:07:15 +11003 * Copyright (C) 2011-2014 Xiph.Org Foundation
Erik de Castro Lopoecd0acb2013-10-04 01:38:00 +10004 *
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
Erik de Castro Lopo006b8352014-03-23 21:59:46 +110033#ifdef HAVE_CONFIG_H
Erik de Castro Lopoecd0acb2013-10-04 01:38:00 +100034# include <config.h>
35#endif
36
37#ifndef FLAC__NO_ASM
Erik de Castro Lopob8d58e32014-06-15 20:29:34 +100038#if (defined FLAC__CPU_IA32 || defined FLAC__CPU_X86_64) && defined FLAC__HAS_X86INTRIN
Erik de Castro Lopod40e9862014-01-30 21:49:51 +110039#include "private/stream_encoder.h"
Miroslav Lichvarf0815242014-06-19 13:04:33 +020040#include "private/bitmath.h"
Erik de Castro Lopod40e9862014-01-30 21:49:51 +110041#ifdef FLAC__SSE2_SUPPORTED
Erik de Castro Lopoecd0acb2013-10-04 01:38:00 +100042
43#include <stdlib.h> /* for abs() */
44#include <emmintrin.h> /* SSE2 */
45#include "FLAC/assert.h"
Erik de Castro Lopo3c566882015-11-18 19:23:02 +110046#include "share/compat.h"
47
48FLAC__SSE_TARGET("sse2")
49static inline __m128i local_abs_epi32(__m128i val)
50{
51 __m128i mask = _mm_srai_epi32(val, 31);
52 val = _mm_xor_si128(val, mask);
53 val = _mm_sub_epi32(val, mask);
54 return val;
55}
56
Erik de Castro Lopoecd0acb2013-10-04 01:38:00 +100057
Erik de Castro Lopod40e9862014-01-30 21:49:51 +110058FLAC__SSE_TARGET("sse2")
Erik de Castro Lopo6cd8b422014-01-07 21:14:55 +110059void FLAC__precompute_partition_info_sums_intrin_sse2(const FLAC__int32 residual[], FLAC__uint64 abs_residual_partition_sums[],
Erik de Castro Lopoecd0acb2013-10-04 01:38:00 +100060 unsigned residual_samples, unsigned predictor_order, unsigned min_partition_order, unsigned max_partition_order, unsigned bps)
61{
62 const unsigned default_partition_samples = (residual_samples + predictor_order) >> max_partition_order;
63 unsigned partitions = 1u << max_partition_order;
64
65 FLAC__ASSERT(default_partition_samples > predictor_order);
66
67 /* first do max_partition_order */
68 {
Erik de Castro Lopo86b36d92015-11-03 18:08:49 +110069 const unsigned threshold = 32 - FLAC__bitmath_ilog2(default_partition_samples);
Erik de Castro Lopoecd0acb2013-10-04 01:38:00 +100070 unsigned partition, residual_sample, end = (unsigned)(-(int)predictor_order);
Erik de Castro Lopoecd0acb2013-10-04 01:38:00 +100071
Erik de Castro Lopo86b36d92015-11-03 18:08:49 +110072 if(bps + FLAC__MAX_EXTRA_RESIDUAL_BPS < threshold) {
Erik de Castro Lopoecd0acb2013-10-04 01:38:00 +100073 for(partition = residual_sample = 0; partition < partitions; partition++) {
Erik de Castro Lopo86b36d92015-11-03 18:08:49 +110074 __m128i mm_sum = _mm_setzero_si128();
75 unsigned e1, e3;
Erik de Castro Lopoecd0acb2013-10-04 01:38:00 +100076 end += default_partition_samples;
Erik de Castro Lopoecd0acb2013-10-04 01:38:00 +100077
78 e1 = (residual_sample + 3) & ~3; e3 = end & ~3;
79 if(e1 > end)
80 e1 = end; /* try flac -l 1 -b 16 and you'll be here */
81
Erik de Castro Lopo57297ee2014-01-30 21:53:37 +110082 /* assumption: residual[] is properly aligned so (residual + e1) is properly aligned too and _mm_loadu_si128() is fast */
Erik de Castro Lopo59cfca02014-01-31 20:39:26 +110083 for( ; residual_sample < e1; residual_sample++) {
Erik de Castro Lopo3fdb87b2015-11-19 18:32:31 +110084 __m128i mm_res = local_abs_epi32(_mm_cvtsi32_si128(residual[residual_sample]));
Erik de Castro Lopo59cfca02014-01-31 20:39:26 +110085 mm_sum = _mm_add_epi32(mm_sum, mm_res);
86 }
Erik de Castro Lopoecd0acb2013-10-04 01:38:00 +100087
88 for( ; residual_sample < e3; residual_sample+=4) {
Erik de Castro Lopo3fdb87b2015-11-19 18:32:31 +110089 __m128i mm_res = local_abs_epi32(_mm_loadu_si128((const __m128i*)(residual+residual_sample)));
Erik de Castro Lopo59cfca02014-01-31 20:39:26 +110090 mm_sum = _mm_add_epi32(mm_sum, mm_res);
91 }
Erik de Castro Lopoecd0acb2013-10-04 01:38:00 +100092
Erik de Castro Lopo59cfca02014-01-31 20:39:26 +110093 for( ; residual_sample < end; residual_sample++) {
Erik de Castro Lopo3fdb87b2015-11-19 18:32:31 +110094 __m128i mm_res = local_abs_epi32(_mm_cvtsi32_si128(residual[residual_sample]));
Erik de Castro Lopoecd0acb2013-10-04 01:38:00 +100095 mm_sum = _mm_add_epi32(mm_sum, mm_res);
96 }
97
98 mm_sum = _mm_add_epi32(mm_sum, _mm_srli_si128(mm_sum, 8));
99 mm_sum = _mm_add_epi32(mm_sum, _mm_srli_si128(mm_sum, 4));
Erik de Castro Lopo6abc4802014-09-21 08:48:17 +1000100 abs_residual_partition_sums[partition] = (FLAC__uint32)_mm_cvtsi128_si32(mm_sum);
Erik de Castro Lopoecd0acb2013-10-04 01:38:00 +1000101 }
102 }
103 else { /* have to pessimistically use 64 bits for accumulator */
Erik de Castro Lopoecd0acb2013-10-04 01:38:00 +1000104 for(partition = residual_sample = 0; partition < partitions; partition++) {
Erik de Castro Lopo86b36d92015-11-03 18:08:49 +1100105 __m128i mm_sum = _mm_setzero_si128();
106 unsigned e1, e3;
Erik de Castro Lopoecd0acb2013-10-04 01:38:00 +1000107 end += default_partition_samples;
Erik de Castro Lopoecd0acb2013-10-04 01:38:00 +1000108
109 e1 = (residual_sample + 1) & ~1; e3 = end & ~1;
110 FLAC__ASSERT(e1 <= end);
111
Erik de Castro Lopo59cfca02014-01-31 20:39:26 +1100112 for( ; residual_sample < e1; residual_sample++) {
Erik de Castro Lopo3fdb87b2015-11-19 18:32:31 +1100113 __m128i mm_res = local_abs_epi32(_mm_cvtsi32_si128(residual[residual_sample])); /* 0 0 0 |r0| == 00 |r0_64| */
Erik de Castro Lopo59cfca02014-01-31 20:39:26 +1100114 mm_sum = _mm_add_epi64(mm_sum, mm_res);
115 }
Erik de Castro Lopoecd0acb2013-10-04 01:38:00 +1000116
117 for( ; residual_sample < e3; residual_sample+=2) {
Erik de Castro Lopo3fdb87b2015-11-19 18:32:31 +1100118 __m128i mm_res = local_abs_epi32(_mm_loadl_epi64((const __m128i*)(residual+residual_sample))); /* 0 0 |r1| |r0| */
Erik de Castro Lopoecd0acb2013-10-04 01:38:00 +1000119 mm_res = _mm_shuffle_epi32(mm_res, _MM_SHUFFLE(3,1,2,0)); /* 0 |r1| 0 |r0| == |r1_64| |r0_64| */
120 mm_sum = _mm_add_epi64(mm_sum, mm_res);
121 }
122
Erik de Castro Lopo59cfca02014-01-31 20:39:26 +1100123 for( ; residual_sample < end; residual_sample++) {
Erik de Castro Lopo3fdb87b2015-11-19 18:32:31 +1100124 __m128i mm_res = local_abs_epi32(_mm_cvtsi32_si128(residual[residual_sample]));
Erik de Castro Lopo59cfca02014-01-31 20:39:26 +1100125 mm_sum = _mm_add_epi64(mm_sum, mm_res);
Erik de Castro Lopoecd0acb2013-10-04 01:38:00 +1000126 }
Erik de Castro Lopoecd0acb2013-10-04 01:38:00 +1000127
Erik de Castro Lopo59cfca02014-01-31 20:39:26 +1100128 mm_sum = _mm_add_epi64(mm_sum, _mm_srli_si128(mm_sum, 8));
129 _mm_storel_epi64((__m128i*)(abs_residual_partition_sums+partition), mm_sum);
Erik de Castro Lopoecd0acb2013-10-04 01:38:00 +1000130 }
131 }
132 }
133
134 /* now merge partitions for lower orders */
135 {
136 unsigned from_partition = 0, to_partition = partitions;
137 int partition_order;
138 for(partition_order = (int)max_partition_order - 1; partition_order >= (int)min_partition_order; partition_order--) {
139 unsigned i;
140 partitions >>= 1;
141 for(i = 0; i < partitions; i++) {
142 abs_residual_partition_sums[to_partition++] =
143 abs_residual_partition_sums[from_partition ] +
144 abs_residual_partition_sums[from_partition+1];
145 from_partition += 2;
146 }
147 }
148 }
149}
150
Erik de Castro Lopod40e9862014-01-30 21:49:51 +1100151#endif /* FLAC__SSE2_SUPPORTED */
Erik de Castro Lopob8d58e32014-06-15 20:29:34 +1000152#endif /* (FLAC__CPU_IA32 || FLAC__CPU_X86_64) && FLAC__HAS_X86INTRIN */
Erik de Castro Lopoecd0acb2013-10-04 01:38:00 +1000153#endif /* FLAC__NO_ASM */