blob: d384dc03eaede7aaee32135a6de1dd3af3370077 [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 Lopo6a5fe432016-12-05 06:35:39 +11003 * Copyright (C) 2011-2016 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
Tim Blechmann912bff42016-05-11 14:05:49 +020037#include "private/cpu.h"
38
Erik de Castro Lopoecd0acb2013-10-04 01:38:00 +100039#ifndef FLAC__NO_ASM
Erik de Castro Lopo36a0ab12016-06-20 20:29:59 +100040#if (defined FLAC__CPU_IA32 || defined FLAC__CPU_X86_64) && FLAC__HAS_X86INTRIN
Erik de Castro Lopod40e9862014-01-30 21:49:51 +110041#include "private/stream_encoder.h"
Miroslav Lichvarf0815242014-06-19 13:04:33 +020042#include "private/bitmath.h"
Erik de Castro Lopoecd0acb2013-10-04 01:38:00 +100043#ifdef FLAC__SSSE3_SUPPORTED
44
45#include <stdlib.h> /* for abs() */
46#include <tmmintrin.h> /* SSSE3 */
47#include "FLAC/assert.h"
Erik de Castro Lopoecd0acb2013-10-04 01:38:00 +100048
Erik de Castro Lopod40e9862014-01-30 21:49:51 +110049FLAC__SSE_TARGET("ssse3")
Erik de Castro Lopo6cd8b422014-01-07 21:14:55 +110050void FLAC__precompute_partition_info_sums_intrin_ssse3(const FLAC__int32 residual[], FLAC__uint64 abs_residual_partition_sums[],
Erik de Castro Lopoc6318e92017-01-14 17:26:39 +110051 uint32_t residual_samples, uint32_t predictor_order, uint32_t min_partition_order, uint32_t max_partition_order, uint32_t bps)
Erik de Castro Lopoecd0acb2013-10-04 01:38:00 +100052{
Erik de Castro Lopoc6318e92017-01-14 17:26:39 +110053 const uint32_t default_partition_samples = (residual_samples + predictor_order) >> max_partition_order;
54 uint32_t partitions = 1u << max_partition_order;
Erik de Castro Lopoecd0acb2013-10-04 01:38:00 +100055
56 FLAC__ASSERT(default_partition_samples > predictor_order);
57
58 /* first do max_partition_order */
59 {
Erik de Castro Lopoc6318e92017-01-14 17:26:39 +110060 const uint32_t threshold = 32 - FLAC__bitmath_ilog2(default_partition_samples);
61 uint32_t partition, residual_sample, end = (uint32_t)(-(int32_t)predictor_order);
Erik de Castro Lopoecd0acb2013-10-04 01:38:00 +100062
Erik de Castro Lopo86b36d92015-11-03 18:08:49 +110063 if(bps + FLAC__MAX_EXTRA_RESIDUAL_BPS < threshold) {
Erik de Castro Lopoecd0acb2013-10-04 01:38:00 +100064 for(partition = residual_sample = 0; partition < partitions; partition++) {
Erik de Castro Lopo86b36d92015-11-03 18:08:49 +110065 __m128i mm_sum = _mm_setzero_si128();
Erik de Castro Lopoc6318e92017-01-14 17:26:39 +110066 uint32_t e1, e3;
Erik de Castro Lopoecd0acb2013-10-04 01:38:00 +100067 end += default_partition_samples;
Erik de Castro Lopoecd0acb2013-10-04 01:38:00 +100068
69 e1 = (residual_sample + 3) & ~3; e3 = end & ~3;
70 if(e1 > end)
71 e1 = end; /* try flac -l 1 -b 16 and you'll be here */
72
Erik de Castro Lopo57297ee2014-01-30 21:53:37 +110073 /* 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 +110074 for( ; residual_sample < e1; residual_sample++) {
Erik de Castro Lopo2319a682015-11-18 19:24:44 +110075 __m128i mm_res = _mm_abs_epi32(_mm_cvtsi32_si128(residual[residual_sample]));
Erik de Castro Lopo59cfca02014-01-31 20:39:26 +110076 mm_sum = _mm_add_epi32(mm_sum, mm_res);
77 }
Erik de Castro Lopoecd0acb2013-10-04 01:38:00 +100078
79 for( ; residual_sample < e3; residual_sample+=4) {
Erik de Castro Lopo2319a682015-11-18 19:24:44 +110080 __m128i mm_res = _mm_abs_epi32(_mm_loadu_si128((const __m128i*)(residual+residual_sample)));
Erik de Castro Lopo59cfca02014-01-31 20:39:26 +110081 mm_sum = _mm_add_epi32(mm_sum, mm_res);
82 }
Erik de Castro Lopoecd0acb2013-10-04 01:38:00 +100083
Erik de Castro Lopo59cfca02014-01-31 20:39:26 +110084 for( ; residual_sample < end; residual_sample++) {
Erik de Castro Lopo2319a682015-11-18 19:24:44 +110085 __m128i mm_res = _mm_abs_epi32(_mm_cvtsi32_si128(residual[residual_sample]));
Erik de Castro Lopoecd0acb2013-10-04 01:38:00 +100086 mm_sum = _mm_add_epi32(mm_sum, mm_res);
87 }
88
lvqcl421961f2018-09-19 20:03:37 +030089 mm_sum = _mm_add_epi32(mm_sum, _mm_shuffle_epi32(mm_sum, _MM_SHUFFLE(1,0,3,2)));
90 mm_sum = _mm_add_epi32(mm_sum, _mm_shufflelo_epi16(mm_sum, _MM_SHUFFLE(1,0,3,2)));
Erik de Castro Lopo6abc4802014-09-21 08:48:17 +100091 abs_residual_partition_sums[partition] = (FLAC__uint32)_mm_cvtsi128_si32(mm_sum);
Erik de Castro Lopoaaaaa0d2018-05-06 15:25:11 +100092/* workaround for MSVC bugs (at least versions 2015 and 2017 are affected) */
93#if (defined _MSC_VER) && (defined FLAC__CPU_X86_64)
Erik de Castro Lopo94a61242016-05-05 17:21:20 +100094 abs_residual_partition_sums[partition] &= 0xFFFFFFFF;
95#endif
Erik de Castro Lopoecd0acb2013-10-04 01:38:00 +100096 }
97 }
98 else { /* have to pessimistically use 64 bits for accumulator */
Erik de Castro Lopoecd0acb2013-10-04 01:38:00 +100099 for(partition = residual_sample = 0; partition < partitions; partition++) {
Erik de Castro Lopo86b36d92015-11-03 18:08:49 +1100100 __m128i mm_sum = _mm_setzero_si128();
Erik de Castro Lopoc6318e92017-01-14 17:26:39 +1100101 uint32_t e1, e3;
Erik de Castro Lopoecd0acb2013-10-04 01:38:00 +1000102 end += default_partition_samples;
Erik de Castro Lopoecd0acb2013-10-04 01:38:00 +1000103
104 e1 = (residual_sample + 1) & ~1; e3 = end & ~1;
105 FLAC__ASSERT(e1 <= end);
106
Erik de Castro Lopo59cfca02014-01-31 20:39:26 +1100107 for( ; residual_sample < e1; residual_sample++) {
Erik de Castro Lopo2319a682015-11-18 19:24:44 +1100108 __m128i mm_res = _mm_abs_epi32(_mm_cvtsi32_si128(residual[residual_sample])); /* 0 0 0 |r0| == 00 |r0_64| */
Erik de Castro Lopo59cfca02014-01-31 20:39:26 +1100109 mm_sum = _mm_add_epi64(mm_sum, mm_res);
110 }
Erik de Castro Lopoecd0acb2013-10-04 01:38:00 +1000111
112 for( ; residual_sample < e3; residual_sample+=2) {
Erik de Castro Lopo2319a682015-11-18 19:24:44 +1100113 __m128i mm_res = _mm_abs_epi32(_mm_loadl_epi64((const __m128i*)(residual+residual_sample))); /* 0 0 |r1| |r0| */
Erik de Castro Lopoecd0acb2013-10-04 01:38:00 +1000114 mm_res = _mm_shuffle_epi32(mm_res, _MM_SHUFFLE(3,1,2,0)); /* 0 |r1| 0 |r0| == |r1_64| |r0_64| */
115 mm_sum = _mm_add_epi64(mm_sum, mm_res);
116 }
117
Erik de Castro Lopo59cfca02014-01-31 20:39:26 +1100118 for( ; residual_sample < end; residual_sample++) {
Erik de Castro Lopo2319a682015-11-18 19:24:44 +1100119 __m128i mm_res = _mm_abs_epi32(_mm_cvtsi32_si128(residual[residual_sample]));
Erik de Castro Lopo59cfca02014-01-31 20:39:26 +1100120 mm_sum = _mm_add_epi64(mm_sum, mm_res);
Erik de Castro Lopoecd0acb2013-10-04 01:38:00 +1000121 }
Erik de Castro Lopoecd0acb2013-10-04 01:38:00 +1000122
Erik de Castro Lopo59cfca02014-01-31 20:39:26 +1100123 mm_sum = _mm_add_epi64(mm_sum, _mm_srli_si128(mm_sum, 8));
124 _mm_storel_epi64((__m128i*)(abs_residual_partition_sums+partition), mm_sum);
Erik de Castro Lopoecd0acb2013-10-04 01:38:00 +1000125 }
126 }
127 }
128
129 /* now merge partitions for lower orders */
130 {
Erik de Castro Lopoc6318e92017-01-14 17:26:39 +1100131 uint32_t from_partition = 0, to_partition = partitions;
Erik de Castro Lopoecd0acb2013-10-04 01:38:00 +1000132 int partition_order;
133 for(partition_order = (int)max_partition_order - 1; partition_order >= (int)min_partition_order; partition_order--) {
Erik de Castro Lopoc6318e92017-01-14 17:26:39 +1100134 uint32_t i;
Erik de Castro Lopoecd0acb2013-10-04 01:38:00 +1000135 partitions >>= 1;
136 for(i = 0; i < partitions; i++) {
137 abs_residual_partition_sums[to_partition++] =
138 abs_residual_partition_sums[from_partition ] +
139 abs_residual_partition_sums[from_partition+1];
140 from_partition += 2;
141 }
142 }
143 }
144}
145
146#endif /* FLAC__SSSE3_SUPPORTED */
Erik de Castro Lopob8d58e32014-06-15 20:29:34 +1000147#endif /* (FLAC__CPU_IA32 || FLAC__CPU_X86_64) && FLAC__HAS_X86INTRIN */
Erik de Castro Lopoecd0acb2013-10-04 01:38:00 +1000148#endif /* FLAC__NO_ASM */