Coverage Report

Created: 2026-12-01 06:51

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/avm/avm_dsp/x86/highbd_convolve_sse2.c
Line
Count
Source
1
/*
2
 * Copyright (c) 2021, Alliance for Open Media. All rights reserved
3
 *
4
 * This source code is subject to the terms of the BSD 3-Clause Clear License
5
 * and the Alliance for Open Media Patent License 1.0. If the BSD 3-Clause Clear
6
 * License was not distributed with this source code in the LICENSE file, you
7
 * can obtain it at aomedia.org/license/software-license/bsd-3-c-c/.  If the
8
 * Alliance for Open Media Patent License 1.0 was not distributed with this
9
 * source code in the PATENTS file, you can obtain it at
10
 * aomedia.org/license/patent-license/.
11
 */
12
#include <emmintrin.h>
13
14
#include "config/avm_dsp_rtcd.h"
15
#include "avm_dsp/x86/convolve.h"
16
17
// -----------------------------------------------------------------------------
18
19
void avm_highbd_filter_block1d4_v4_sse2(const uint16_t *src_ptr,
20
                                        ptrdiff_t src_pitch, uint16_t *dst_ptr,
21
                                        ptrdiff_t dst_pitch, uint32_t height,
22
0
                                        const int16_t *filter, int bd) {
23
0
  __m128i filtersReg;
24
0
  __m128i srcReg2, srcReg3, srcReg4, srcReg5, srcReg6;
25
0
  __m128i srcReg23_lo, srcReg34_lo;
26
0
  __m128i srcReg45_lo, srcReg56_lo;
27
0
  __m128i resReg23_lo, resReg34_lo, resReg45_lo, resReg56_lo;
28
0
  __m128i resReg23_45_lo, resReg34_56_lo;
29
0
  __m128i resReg23_45, resReg34_56;
30
0
  __m128i addFilterReg64, secondFilters, thirdFilters;
31
0
  unsigned int i;
32
0
  ptrdiff_t src_stride, dst_stride;
33
34
0
  const __m128i max = _mm_set1_epi16((1 << bd) - 1);
35
0
  addFilterReg64 = _mm_set1_epi32(64);
36
0
  filtersReg = _mm_loadu_si128((const __m128i *)filter);
37
38
  // coeffs 0 1 0 1 2 3 2 3
39
0
  const __m128i tmp0 = _mm_unpacklo_epi32(filtersReg, filtersReg);
40
  // coeffs 4 5 4 5 6 7 6 7
41
0
  const __m128i tmp1 = _mm_unpackhi_epi32(filtersReg, filtersReg);
42
43
0
  secondFilters = _mm_unpackhi_epi64(tmp0, tmp0);  // coeffs 2 3 2 3 2 3 2 3
44
0
  thirdFilters = _mm_unpacklo_epi64(tmp1, tmp1);   // coeffs 4 5 4 5 4 5 4 5
45
46
  // multiply the size of the source and destination stride by two
47
0
  src_stride = src_pitch << 1;
48
0
  dst_stride = dst_pitch << 1;
49
50
0
  srcReg2 = _mm_loadl_epi64((const __m128i *)(src_ptr + src_pitch * 2));
51
0
  srcReg3 = _mm_loadl_epi64((const __m128i *)(src_ptr + src_pitch * 3));
52
0
  srcReg23_lo = _mm_unpacklo_epi16(srcReg2, srcReg3);
53
54
0
  srcReg4 = _mm_loadl_epi64((const __m128i *)(src_ptr + src_pitch * 4));
55
0
  srcReg34_lo = _mm_unpacklo_epi16(srcReg3, srcReg4);
56
57
0
  for (i = height; i > 1; i -= 2) {
58
0
    srcReg5 = _mm_loadl_epi64((const __m128i *)(src_ptr + src_pitch * 5));
59
0
    srcReg45_lo = _mm_unpacklo_epi16(srcReg4, srcReg5);
60
61
0
    srcReg6 = _mm_loadl_epi64((const __m128i *)(src_ptr + src_pitch * 6));
62
0
    srcReg56_lo = _mm_unpacklo_epi16(srcReg5, srcReg6);
63
64
    // multiply 2 adjacent elements with the filter and add the result
65
66
0
    resReg23_lo = _mm_madd_epi16(srcReg23_lo, secondFilters);
67
0
    resReg34_lo = _mm_madd_epi16(srcReg34_lo, secondFilters);
68
0
    resReg45_lo = _mm_madd_epi16(srcReg45_lo, thirdFilters);
69
0
    resReg56_lo = _mm_madd_epi16(srcReg56_lo, thirdFilters);
70
71
0
    resReg23_45_lo = _mm_add_epi32(resReg23_lo, resReg45_lo);
72
0
    resReg34_56_lo = _mm_add_epi32(resReg34_lo, resReg56_lo);
73
74
    // shift by 7 bit each 32 bit
75
0
    resReg23_45_lo = _mm_add_epi32(resReg23_45_lo, addFilterReg64);
76
0
    resReg34_56_lo = _mm_add_epi32(resReg34_56_lo, addFilterReg64);
77
0
    resReg23_45_lo = _mm_srai_epi32(resReg23_45_lo, 7);
78
0
    resReg34_56_lo = _mm_srai_epi32(resReg34_56_lo, 7);
79
80
    // shrink to 16 bit each 32 bits, the first lane contain the first
81
    // convolve result and the second lane contain the second convolve
82
    // result
83
0
    resReg23_45 = _mm_packs_epi32(resReg23_45_lo, _mm_setzero_si128());
84
0
    resReg34_56 = _mm_packs_epi32(resReg34_56_lo, _mm_setzero_si128());
85
86
0
    resReg23_45 = _mm_max_epi16(resReg23_45, _mm_setzero_si128());
87
0
    resReg23_45 = _mm_min_epi16(resReg23_45, max);
88
0
    resReg34_56 = _mm_max_epi16(resReg34_56, _mm_setzero_si128());
89
0
    resReg34_56 = _mm_min_epi16(resReg34_56, max);
90
91
0
    src_ptr += src_stride;
92
93
0
    _mm_storel_epi64((__m128i *)dst_ptr, (resReg23_45));
94
0
    _mm_storel_epi64((__m128i *)(dst_ptr + dst_pitch), (resReg34_56));
95
96
0
    dst_ptr += dst_stride;
97
98
    // save part of the registers for next strides
99
0
    srcReg23_lo = srcReg45_lo;
100
0
    srcReg34_lo = srcReg56_lo;
101
0
    srcReg4 = srcReg6;
102
0
  }
103
0
}
104
105
void avm_highbd_filter_block1d4_h4_sse2(const uint16_t *src_ptr,
106
                                        ptrdiff_t src_pitch, uint16_t *dst_ptr,
107
                                        ptrdiff_t dst_pitch, uint32_t height,
108
0
                                        const int16_t *filter, int bd) {
109
0
  __m128i filtersReg;
110
0
  __m128i addFilterReg64;
111
0
  __m128i secondFilters, thirdFilters;
112
0
  __m128i srcRegFilt32b1_1;
113
0
  __m128i srcReg32b1;
114
0
  unsigned int i;
115
0
  src_ptr -= 3;
116
0
  addFilterReg64 = _mm_set1_epi32(64);
117
0
  filtersReg = _mm_loadu_si128((const __m128i *)filter);
118
0
  const __m128i max = _mm_set1_epi16((1 << bd) - 1);
119
120
  // coeffs 0 1 0 1 2 3 2 3
121
0
  const __m128i tmp_0 = _mm_unpacklo_epi32(filtersReg, filtersReg);
122
  // coeffs 4 5 4 5 6 7 6 7
123
0
  const __m128i tmp_1 = _mm_unpackhi_epi32(filtersReg, filtersReg);
124
125
0
  secondFilters = _mm_unpackhi_epi64(tmp_0, tmp_0);  // coeffs 2 3 2 3 2 3 2 3
126
0
  thirdFilters = _mm_unpacklo_epi64(tmp_1, tmp_1);   // coeffs 4 5 4 5 4 5 4 5
127
128
0
  for (i = height; i > 0; i -= 1) {
129
0
    srcReg32b1 = _mm_loadu_si128((const __m128i *)(src_ptr + 2));
130
131
0
    __m128i ss_3_1 = _mm_srli_si128(srcReg32b1, 2);
132
0
    __m128i ss_4_1 = _mm_srli_si128(srcReg32b1, 4);
133
0
    __m128i ss_5_1 = _mm_srli_si128(srcReg32b1, 6);
134
0
    __m128i ss_23 = _mm_unpacklo_epi32(srcReg32b1, ss_3_1);
135
0
    __m128i ss_45 = _mm_unpacklo_epi32(ss_4_1, ss_5_1);
136
137
0
    ss_23 = _mm_madd_epi16(ss_23, secondFilters);
138
0
    ss_45 = _mm_madd_epi16(ss_45, thirdFilters);
139
0
    srcRegFilt32b1_1 = _mm_add_epi32(ss_23, ss_45);
140
141
    // shift by 7 bit each 32 bit
142
0
    srcRegFilt32b1_1 = _mm_add_epi32(srcRegFilt32b1_1, addFilterReg64);
143
0
    srcRegFilt32b1_1 = _mm_srai_epi32(srcRegFilt32b1_1, 7);
144
145
0
    srcRegFilt32b1_1 = _mm_packs_epi32(srcRegFilt32b1_1, _mm_setzero_si128());
146
0
    srcRegFilt32b1_1 = _mm_max_epi16(srcRegFilt32b1_1, _mm_setzero_si128());
147
0
    srcRegFilt32b1_1 = _mm_min_epi16(srcRegFilt32b1_1, max);
148
149
0
    src_ptr += src_pitch;
150
151
0
    _mm_storel_epi64((__m128i *)dst_ptr, srcRegFilt32b1_1);
152
153
0
    dst_ptr += dst_pitch;
154
0
  }
155
0
}
156
157
void avm_highbd_filter_block1d8_v4_sse2(const uint16_t *src_ptr,
158
                                        ptrdiff_t src_pitch, uint16_t *dst_ptr,
159
                                        ptrdiff_t dst_pitch, uint32_t height,
160
0
                                        const int16_t *filter, int bd) {
161
0
  __m128i filtersReg;
162
0
  __m128i srcReg2, srcReg3, srcReg4, srcReg5, srcReg6;
163
0
  __m128i srcReg23_lo, srcReg23_hi, srcReg34_lo, srcReg34_hi;
164
0
  __m128i srcReg45_lo, srcReg45_hi, srcReg56_lo, srcReg56_hi;
165
0
  __m128i resReg23_lo, resReg34_lo, resReg45_lo, resReg56_lo;
166
0
  __m128i resReg23_hi, resReg34_hi, resReg45_hi, resReg56_hi;
167
0
  __m128i resReg23_45_lo, resReg34_56_lo, resReg23_45_hi, resReg34_56_hi;
168
0
  __m128i resReg23_45, resReg34_56;
169
0
  __m128i addFilterReg64, secondFilters, thirdFilters;
170
0
  unsigned int i;
171
0
  ptrdiff_t src_stride, dst_stride;
172
173
0
  const __m128i max = _mm_set1_epi16((1 << bd) - 1);
174
0
  addFilterReg64 = _mm_set1_epi32(64);
175
0
  filtersReg = _mm_loadu_si128((const __m128i *)filter);
176
177
  // coeffs 0 1 0 1 2 3 2 3
178
0
  const __m128i tmp0 = _mm_unpacklo_epi32(filtersReg, filtersReg);
179
  // coeffs 4 5 4 5 6 7 6 7
180
0
  const __m128i tmp1 = _mm_unpackhi_epi32(filtersReg, filtersReg);
181
182
0
  secondFilters = _mm_unpackhi_epi64(tmp0, tmp0);  // coeffs 2 3 2 3 2 3 2 3
183
0
  thirdFilters = _mm_unpacklo_epi64(tmp1, tmp1);   // coeffs 4 5 4 5 4 5 4 5
184
185
  // multiple the size of the source and destination stride by two
186
0
  src_stride = src_pitch << 1;
187
0
  dst_stride = dst_pitch << 1;
188
189
0
  srcReg2 = _mm_loadu_si128((const __m128i *)(src_ptr + src_pitch * 2));
190
0
  srcReg3 = _mm_loadu_si128((const __m128i *)(src_ptr + src_pitch * 3));
191
0
  srcReg23_lo = _mm_unpacklo_epi16(srcReg2, srcReg3);
192
0
  srcReg23_hi = _mm_unpackhi_epi16(srcReg2, srcReg3);
193
194
0
  srcReg4 = _mm_loadu_si128((const __m128i *)(src_ptr + src_pitch * 4));
195
0
  srcReg34_lo = _mm_unpacklo_epi16(srcReg3, srcReg4);
196
0
  srcReg34_hi = _mm_unpackhi_epi16(srcReg3, srcReg4);
197
198
0
  for (i = height; i > 1; i -= 2) {
199
0
    srcReg5 = _mm_loadu_si128((const __m128i *)(src_ptr + src_pitch * 5));
200
201
0
    srcReg45_lo = _mm_unpacklo_epi16(srcReg4, srcReg5);
202
0
    srcReg45_hi = _mm_unpackhi_epi16(srcReg4, srcReg5);
203
204
0
    srcReg6 = _mm_loadu_si128((const __m128i *)(src_ptr + src_pitch * 6));
205
206
0
    srcReg56_lo = _mm_unpacklo_epi16(srcReg5, srcReg6);
207
0
    srcReg56_hi = _mm_unpackhi_epi16(srcReg5, srcReg6);
208
209
    // multiply 2 adjacent elements with the filter and add the result
210
211
0
    resReg23_lo = _mm_madd_epi16(srcReg23_lo, secondFilters);
212
0
    resReg34_lo = _mm_madd_epi16(srcReg34_lo, secondFilters);
213
0
    resReg45_lo = _mm_madd_epi16(srcReg45_lo, thirdFilters);
214
0
    resReg56_lo = _mm_madd_epi16(srcReg56_lo, thirdFilters);
215
216
0
    resReg23_45_lo = _mm_add_epi32(resReg23_lo, resReg45_lo);
217
0
    resReg34_56_lo = _mm_add_epi32(resReg34_lo, resReg56_lo);
218
219
    // multiply 2 adjacent elements with the filter and add the result
220
221
0
    resReg23_hi = _mm_madd_epi16(srcReg23_hi, secondFilters);
222
0
    resReg34_hi = _mm_madd_epi16(srcReg34_hi, secondFilters);
223
0
    resReg45_hi = _mm_madd_epi16(srcReg45_hi, thirdFilters);
224
0
    resReg56_hi = _mm_madd_epi16(srcReg56_hi, thirdFilters);
225
226
0
    resReg23_45_hi = _mm_add_epi32(resReg23_hi, resReg45_hi);
227
0
    resReg34_56_hi = _mm_add_epi32(resReg34_hi, resReg56_hi);
228
229
    // shift by 7 bit each 32 bit
230
0
    resReg23_45_lo = _mm_add_epi32(resReg23_45_lo, addFilterReg64);
231
0
    resReg34_56_lo = _mm_add_epi32(resReg34_56_lo, addFilterReg64);
232
0
    resReg23_45_hi = _mm_add_epi32(resReg23_45_hi, addFilterReg64);
233
0
    resReg34_56_hi = _mm_add_epi32(resReg34_56_hi, addFilterReg64);
234
0
    resReg23_45_lo = _mm_srai_epi32(resReg23_45_lo, 7);
235
0
    resReg34_56_lo = _mm_srai_epi32(resReg34_56_lo, 7);
236
0
    resReg23_45_hi = _mm_srai_epi32(resReg23_45_hi, 7);
237
0
    resReg34_56_hi = _mm_srai_epi32(resReg34_56_hi, 7);
238
239
    // shrink to 16 bit each 32 bits, the first lane contain the first
240
    // convolve result and the second lane contain the second convolve
241
    // result
242
0
    resReg23_45 = _mm_packs_epi32(resReg23_45_lo, resReg23_45_hi);
243
0
    resReg34_56 = _mm_packs_epi32(resReg34_56_lo, resReg34_56_hi);
244
245
0
    resReg23_45 = _mm_max_epi16(resReg23_45, _mm_setzero_si128());
246
0
    resReg23_45 = _mm_min_epi16(resReg23_45, max);
247
0
    resReg34_56 = _mm_max_epi16(resReg34_56, _mm_setzero_si128());
248
0
    resReg34_56 = _mm_min_epi16(resReg34_56, max);
249
250
0
    src_ptr += src_stride;
251
252
0
    _mm_store_si128((__m128i *)dst_ptr, (resReg23_45));
253
0
    _mm_store_si128((__m128i *)(dst_ptr + dst_pitch), (resReg34_56));
254
255
0
    dst_ptr += dst_stride;
256
257
    // save part of the registers for next strides
258
0
    srcReg23_lo = srcReg45_lo;
259
0
    srcReg23_hi = srcReg45_hi;
260
0
    srcReg34_lo = srcReg56_lo;
261
0
    srcReg34_hi = srcReg56_hi;
262
0
    srcReg4 = srcReg6;
263
0
  }
264
0
}
265
266
void avm_highbd_filter_block1d8_h4_sse2(const uint16_t *src_ptr,
267
                                        ptrdiff_t src_pitch, uint16_t *dst_ptr,
268
                                        ptrdiff_t dst_pitch, uint32_t height,
269
0
                                        const int16_t *filter, int bd) {
270
0
  __m128i filtersReg;
271
0
  __m128i addFilterReg64;
272
0
  __m128i secondFilters, thirdFilters;
273
0
  __m128i srcRegFilt32b1_1, srcRegFilt32b1_2;
274
0
  __m128i srcReg32b1, srcReg32b2;
275
0
  unsigned int i;
276
0
  src_ptr -= 3;
277
0
  addFilterReg64 = _mm_set1_epi32(64);
278
0
  filtersReg = _mm_loadu_si128((const __m128i *)filter);
279
0
  const __m128i max = _mm_set1_epi16((1 << bd) - 1);
280
281
  // coeffs 0 1 0 1 2 3 2 3
282
0
  const __m128i tmp_0 = _mm_unpacklo_epi32(filtersReg, filtersReg);
283
  // coeffs 4 5 4 5 6 7 6 7
284
0
  const __m128i tmp_1 = _mm_unpackhi_epi32(filtersReg, filtersReg);
285
286
0
  secondFilters = _mm_unpackhi_epi64(tmp_0, tmp_0);  // coeffs 2 3 2 3 2 3 2 3
287
0
  thirdFilters = _mm_unpacklo_epi64(tmp_1, tmp_1);   // coeffs 4 5 4 5 4 5 4 5
288
289
0
  for (i = height; i > 0; i -= 1) {
290
0
    srcReg32b1 = _mm_loadu_si128((const __m128i *)(src_ptr + 2));
291
0
    srcReg32b2 = _mm_loadu_si128((const __m128i *)(src_ptr + 6));
292
293
0
    __m128i ss_4_1 = _mm_srli_si128(srcReg32b1, 4);
294
0
    __m128i ss_4_2 = _mm_srli_si128(srcReg32b2, 4);
295
0
    __m128i ss_4 = _mm_unpacklo_epi64(ss_4_1, ss_4_2);
296
297
0
    __m128i d1 = _mm_madd_epi16(srcReg32b1, secondFilters);
298
0
    __m128i d2 = _mm_madd_epi16(ss_4, thirdFilters);
299
0
    srcRegFilt32b1_1 = _mm_add_epi32(d1, d2);
300
301
0
    __m128i ss_3_1 = _mm_srli_si128(srcReg32b1, 2);
302
0
    __m128i ss_5_1 = _mm_srli_si128(srcReg32b1, 6);
303
0
    __m128i ss_3_2 = _mm_srli_si128(srcReg32b2, 2);
304
0
    __m128i ss_5_2 = _mm_srli_si128(srcReg32b2, 6);
305
0
    __m128i ss_3 = _mm_unpacklo_epi64(ss_3_1, ss_3_2);
306
0
    __m128i ss_5 = _mm_unpacklo_epi64(ss_5_1, ss_5_2);
307
308
0
    d1 = _mm_madd_epi16(ss_3, secondFilters);
309
0
    d2 = _mm_madd_epi16(ss_5, thirdFilters);
310
0
    srcRegFilt32b1_2 = _mm_add_epi32(d1, d2);
311
312
0
    __m128i res_lo_1 = _mm_unpacklo_epi32(srcRegFilt32b1_1, srcRegFilt32b1_2);
313
0
    __m128i res_hi_1 = _mm_unpackhi_epi32(srcRegFilt32b1_1, srcRegFilt32b1_2);
314
315
    // shift by 7 bit each 32 bit
316
0
    res_lo_1 = _mm_add_epi32(res_lo_1, addFilterReg64);
317
0
    res_hi_1 = _mm_add_epi32(res_hi_1, addFilterReg64);
318
0
    res_lo_1 = _mm_srai_epi32(res_lo_1, 7);
319
0
    res_hi_1 = _mm_srai_epi32(res_hi_1, 7);
320
321
0
    srcRegFilt32b1_1 = _mm_packs_epi32(res_lo_1, res_hi_1);
322
323
0
    srcRegFilt32b1_1 = _mm_max_epi16(srcRegFilt32b1_1, _mm_setzero_si128());
324
0
    srcRegFilt32b1_1 = _mm_min_epi16(srcRegFilt32b1_1, max);
325
326
0
    src_ptr += src_pitch;
327
328
0
    _mm_store_si128((__m128i *)dst_ptr, srcRegFilt32b1_1);
329
330
0
    dst_ptr += dst_pitch;
331
0
  }
332
0
}
333
334
void avm_highbd_filter_block1d16_v4_sse2(const uint16_t *src_ptr,
335
                                         ptrdiff_t src_pitch, uint16_t *dst_ptr,
336
                                         ptrdiff_t dst_pitch, uint32_t height,
337
0
                                         const int16_t *filter, int bd) {
338
0
  avm_highbd_filter_block1d8_v4_sse2(src_ptr, src_pitch, dst_ptr, dst_pitch,
339
0
                                     height, filter, bd);
340
0
  avm_highbd_filter_block1d8_v4_sse2((src_ptr + 8), src_pitch, (dst_ptr + 8),
341
0
                                     dst_pitch, height, filter, bd);
342
0
}
343
344
void avm_highbd_filter_block1d16_h4_sse2(const uint16_t *src_ptr,
345
                                         ptrdiff_t src_pitch, uint16_t *dst_ptr,
346
                                         ptrdiff_t dst_pitch, uint32_t height,
347
0
                                         const int16_t *filter, int bd) {
348
0
  avm_highbd_filter_block1d8_h4_sse2(src_ptr, src_pitch, dst_ptr, dst_pitch,
349
0
                                     height, filter, bd);
350
0
  avm_highbd_filter_block1d8_h4_sse2((src_ptr + 8), src_pitch, (dst_ptr + 8),
351
0
                                     dst_pitch, height, filter, bd);
352
0
}