vp9_loopfilter_intrin_sse2.c 62.8 KB
Newer Older
John Koleszar's avatar
John Koleszar committed
1
/*
2
 *  Copyright (c) 2010 The WebM project authors. All Rights Reserved.
John Koleszar's avatar
John Koleszar committed
3
 *
4
 *  Use of this source code is governed by a BSD-style license
5
6
 *  that can be found in the LICENSE file in the root of the source
 *  tree. An additional intellectual property rights grant can be found
7
 *  in the file PATENTS.  All contributing project authors may
8
 *  be found in the AUTHORS file in the root of the source tree.
John Koleszar's avatar
John Koleszar committed
9
10
 */

Johann's avatar
Johann committed
11
#include <emmintrin.h>  /* SSE2 */
12
#include "vp9/common/vp9_loopfilter.h"
13
#include "vpx_ports/emmintrin_compat.h"
John Koleszar's avatar
John Koleszar committed
14

15
16
prototype_loopfilter(vp9_loop_filter_vertical_edge_sse2);
prototype_loopfilter(vp9_loop_filter_horizontal_edge_sse2);
John Koleszar's avatar
John Koleszar committed
17

18
19
extern loop_filter_uvfunction vp9_loop_filter_horizontal_edge_uv_sse2;
extern loop_filter_uvfunction vp9_loop_filter_vertical_edge_uv_sse2;
20

21
22
23
24
25
void vp9_mb_lpf_horizontal_edge_w_sse2_8(unsigned char *s,
                                         int p,
                                         const unsigned char *_blimit,
                                         const unsigned char *_limit,
                                         const unsigned char *_thresh) {
26
27
  DECLARE_ALIGNED(16, unsigned char, flat2_op[7][8]);
  DECLARE_ALIGNED(16, unsigned char, flat2_oq[7][8]);
28

29
30
  DECLARE_ALIGNED(16, unsigned char, flat_op[3][8]);
  DECLARE_ALIGNED(16, unsigned char, flat_oq[3][8]);
31

32
33
  DECLARE_ALIGNED(16, unsigned char, ap[8][8]);
  DECLARE_ALIGNED(16, unsigned char, aq[8][8]);
34
35


36
37
  __m128i mask, hev, flat, flat2;
  const __m128i zero = _mm_set1_epi16(0);
38
  const __m128i one = _mm_set1_epi8(1);
39
40
41
42
43
44
45
46
47
48
49
50
51
52
  __m128i p7, p6, p5;
  __m128i p4, p3, p2, p1, p0, q0, q1, q2, q3, q4;
  __m128i q5, q6, q7;
  int i = 0;
  const unsigned int extended_thresh = _thresh[0] * 0x01010101u;
  const unsigned int extended_limit  = _limit[0]  * 0x01010101u;
  const unsigned int extended_blimit = _blimit[0] * 0x01010101u;
  const __m128i thresh =
      _mm_shuffle_epi32(_mm_cvtsi32_si128((int)extended_thresh), 0);
  const __m128i limit =
      _mm_shuffle_epi32(_mm_cvtsi32_si128((int)extended_limit), 0);
  const __m128i blimit =
      _mm_shuffle_epi32(_mm_cvtsi32_si128((int)extended_blimit), 0);

53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
  p4 = _mm_loadl_epi64((__m128i *)(s - 5 * p));
  p3 = _mm_loadl_epi64((__m128i *)(s - 4 * p));
  p2 = _mm_loadl_epi64((__m128i *)(s - 3 * p));
  p1 = _mm_loadl_epi64((__m128i *)(s - 2 * p));
  p0 = _mm_loadl_epi64((__m128i *)(s - 1 * p));
  q0 = _mm_loadl_epi64((__m128i *)(s - 0 * p));
  q1 = _mm_loadl_epi64((__m128i *)(s + 1 * p));
  q2 = _mm_loadl_epi64((__m128i *)(s + 2 * p));
  q3 = _mm_loadl_epi64((__m128i *)(s + 3 * p));
  q4 = _mm_loadl_epi64((__m128i *)(s + 4 * p));

  _mm_storel_epi64((__m128i *)ap[4], p4);
  _mm_storel_epi64((__m128i *)ap[3], p3);
  _mm_storel_epi64((__m128i *)ap[2], p2);
  _mm_storel_epi64((__m128i *)ap[1], p1);
  _mm_storel_epi64((__m128i *)ap[0], p0);
  _mm_storel_epi64((__m128i *)aq[4], q4);
  _mm_storel_epi64((__m128i *)aq[3], q3);
  _mm_storel_epi64((__m128i *)aq[2], q2);
  _mm_storel_epi64((__m128i *)aq[1], q1);
  _mm_storel_epi64((__m128i *)aq[0], q0);
74
75


76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
  {
    const __m128i abs_p1p0 = _mm_or_si128(_mm_subs_epu8(p1, p0),
                                          _mm_subs_epu8(p0, p1));
    const __m128i abs_q1q0 = _mm_or_si128(_mm_subs_epu8(q1, q0),
                                          _mm_subs_epu8(q0, q1));
    const __m128i fe = _mm_set1_epi8(0xfe);
    const __m128i ff = _mm_cmpeq_epi8(abs_p1p0, abs_p1p0);
    __m128i abs_p0q0 = _mm_or_si128(_mm_subs_epu8(p0, q0),
                                    _mm_subs_epu8(q0, p0));
    __m128i abs_p1q1 = _mm_or_si128(_mm_subs_epu8(p1, q1),
                                    _mm_subs_epu8(q1, p1));
    __m128i work;
    flat = _mm_max_epu8(abs_p1p0, abs_q1q0);
    hev = _mm_subs_epu8(flat, thresh);
    hev = _mm_xor_si128(_mm_cmpeq_epi8(hev, zero), ff);

    abs_p0q0 =_mm_adds_epu8(abs_p0q0, abs_p0q0);
    abs_p1q1 = _mm_srli_epi16(_mm_and_si128(abs_p1q1, fe), 1);
    mask = _mm_subs_epu8(_mm_adds_epu8(abs_p0q0, abs_p1q1), blimit);
    mask = _mm_xor_si128(_mm_cmpeq_epi8(mask, zero), ff);
    // mask |= (abs(p0 - q0) * 2 + abs(p1 - q1) / 2  > blimit) * -1;
    mask = _mm_max_epu8(flat, mask);
    // mask |= (abs(p1 - p0) > limit) * -1;
    // mask |= (abs(q1 - q0) > limit) * -1;
    work = _mm_max_epu8(_mm_or_si128(_mm_subs_epu8(p2, p1),
                                     _mm_subs_epu8(p1, p2)),
                         _mm_or_si128(_mm_subs_epu8(p3, p2),
                                      _mm_subs_epu8(p2, p3)));
    mask = _mm_max_epu8(work, mask);
    work = _mm_max_epu8(_mm_or_si128(_mm_subs_epu8(q2, q1),
                                     _mm_subs_epu8(q1, q2)),
                         _mm_or_si128(_mm_subs_epu8(q3, q2),
                                      _mm_subs_epu8(q2, q3)));
    mask = _mm_max_epu8(work, mask);
    mask = _mm_subs_epu8(mask, limit);
    mask = _mm_cmpeq_epi8(mask, zero);
  }

  // lp filter
  {
    const __m128i t4 = _mm_set1_epi8(4);
    const __m128i t3 = _mm_set1_epi8(3);
    const __m128i t80 = _mm_set1_epi8(0x80);
    const __m128i te0 = _mm_set1_epi8(0xe0);
    const __m128i t1f = _mm_set1_epi8(0x1f);
    const __m128i t1 = _mm_set1_epi8(0x1);
    const __m128i t7f = _mm_set1_epi8(0x7f);

124
125
126
127
    __m128i ps1 = _mm_xor_si128(p1, t80);
    __m128i ps0 = _mm_xor_si128(p0, t80);
    __m128i qs0 = _mm_xor_si128(q0, t80);
    __m128i qs1 = _mm_xor_si128(q1, t80);
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
    __m128i filt;
    __m128i work_a;
    __m128i filter1, filter2;

    filt = _mm_and_si128(_mm_subs_epi8(ps1, qs1), hev);
    work_a = _mm_subs_epi8(qs0, ps0);
    filt = _mm_adds_epi8(filt, work_a);
    filt = _mm_adds_epi8(filt, work_a);
    filt = _mm_adds_epi8(filt, work_a);
    /* (vp9_filter + 3 * (qs0 - ps0)) & mask */
    filt = _mm_and_si128(filt, mask);

    filter1 = _mm_adds_epi8(filt, t4);
    filter2 = _mm_adds_epi8(filt, t3);

    /* Filter1 >> 3 */
    work_a = _mm_cmpgt_epi8(zero, filter1);
    filter1 = _mm_srli_epi16(filter1, 3);
    work_a = _mm_and_si128(work_a, te0);
    filter1 = _mm_and_si128(filter1, t1f);
    filter1 = _mm_or_si128(filter1, work_a);
149
    qs0 = _mm_xor_si128(_mm_subs_epi8(qs0, filter1), t80);
150
151
152
153
154
155
156

    /* Filter2 >> 3 */
    work_a = _mm_cmpgt_epi8(zero, filter2);
    filter2 = _mm_srli_epi16(filter2, 3);
    work_a = _mm_and_si128(work_a, te0);
    filter2 = _mm_and_si128(filter2, t1f);
    filter2 = _mm_or_si128(filter2, work_a);
157
    ps0 = _mm_xor_si128(_mm_adds_epi8(ps0, filter2), t80);
158
159
160
161
162
163
164
165
166
167
168

    /* filt >> 1 */
    filt = _mm_adds_epi8(filter1, t1);
    work_a = _mm_cmpgt_epi8(zero, filt);
    filt = _mm_srli_epi16(filt, 1);
    work_a = _mm_and_si128(work_a, t80);
    filt = _mm_and_si128(filt, t7f);
    filt = _mm_or_si128(filt, work_a);
    filt = _mm_andnot_si128(hev, filt);
    ps1 = _mm_xor_si128(_mm_adds_epi8(ps1, filt), t80);
    qs1 = _mm_xor_si128(_mm_subs_epi8(qs1, filt), t80);
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
    // loopfilter done

    {
      __m128i work;
      work = _mm_max_epu8(_mm_or_si128(_mm_subs_epu8(p2, p0),
                                       _mm_subs_epu8(p0, p2)),
                           _mm_or_si128(_mm_subs_epu8(q2, q0),
                                        _mm_subs_epu8(q0, q2)));
      flat = _mm_max_epu8(work, flat);
      work = _mm_max_epu8(_mm_or_si128(_mm_subs_epu8(p3, p0),
                                       _mm_subs_epu8(p0, p3)),
                           _mm_or_si128(_mm_subs_epu8(q3, q0),
                                        _mm_subs_epu8(q0, q3)));
      flat = _mm_max_epu8(work, flat);
      work = _mm_max_epu8(_mm_or_si128(_mm_subs_epu8(p4, p0),
                                       _mm_subs_epu8(p0, p4)),
                           _mm_or_si128(_mm_subs_epu8(q4, q0),
                                        _mm_subs_epu8(q0, q4)));
      flat = _mm_subs_epu8(flat, one);
      flat = _mm_cmpeq_epi8(flat, zero);
      flat = _mm_and_si128(flat, mask);

191
192
      p5 = _mm_loadl_epi64((__m128i *)(s - 6 * p));
      q5 = _mm_loadl_epi64((__m128i *)(s + 5 * p));
193
194
195
196
      flat2 = _mm_max_epu8(_mm_or_si128(_mm_subs_epu8(p5, p0),
                                       _mm_subs_epu8(p0, p5)),
                           _mm_or_si128(_mm_subs_epu8(q5, q0),
                                        _mm_subs_epu8(q0, q5)));
197
198
      _mm_storel_epi64((__m128i *)ap[5], p5);
      _mm_storel_epi64((__m128i *)aq[5], q5);
199
      flat2 = _mm_max_epu8(work, flat2);
200
201
      p6 = _mm_loadl_epi64((__m128i *)(s - 7 * p));
      q6 = _mm_loadl_epi64((__m128i *)(s + 6 * p));
202
203
204
205
      work = _mm_max_epu8(_mm_or_si128(_mm_subs_epu8(p6, p0),
                                       _mm_subs_epu8(p0, p6)),
                           _mm_or_si128(_mm_subs_epu8(q6, q0),
                                        _mm_subs_epu8(q0, q6)));
206
207
      _mm_storel_epi64((__m128i *)ap[6], p6);
      _mm_storel_epi64((__m128i *)aq[6], q6);
208
209
      flat2 = _mm_max_epu8(work, flat2);

210
211
      p7 = _mm_loadl_epi64((__m128i *)(s - 8 * p));
      q7 = _mm_loadl_epi64((__m128i *)(s + 7 * p));
212
213
214
215
      work = _mm_max_epu8(_mm_or_si128(_mm_subs_epu8(p7, p0),
                                       _mm_subs_epu8(p0, p7)),
                           _mm_or_si128(_mm_subs_epu8(q7, q0),
                                        _mm_subs_epu8(q0, q7)));
216
217
      _mm_storel_epi64((__m128i *)ap[7], p7);
      _mm_storel_epi64((__m128i *)aq[7], q7);
218
219
220
221
222
223
224
225
226
227
228
      flat2 = _mm_max_epu8(work, flat2);
      flat2 = _mm_subs_epu8(flat2, one);
      flat2 = _mm_cmpeq_epi8(flat2, zero);
      flat2 = _mm_and_si128(flat2, flat);  // flat2 & flat & mask
    }

    // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    // flat and wide flat calculations
    {
      const __m128i eight = _mm_set1_epi16(8);
      const __m128i four = _mm_set1_epi16(4);
229
      {
230
231
232
        __m128i workp_shft;
        __m128i a, b, c;

233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
        p7 = _mm_unpacklo_epi8(_mm_loadl_epi64((__m128i *)(ap[7])), zero);
        p6 = _mm_unpacklo_epi8(_mm_loadl_epi64((__m128i *)(ap[6])), zero);
        p5 = _mm_unpacklo_epi8(_mm_loadl_epi64((__m128i *)(ap[5])), zero);
        p4 = _mm_unpacklo_epi8(_mm_loadl_epi64((__m128i *)(ap[4])), zero);
        p3 = _mm_unpacklo_epi8(_mm_loadl_epi64((__m128i *)(ap[3])), zero);
        p2 = _mm_unpacklo_epi8(_mm_loadl_epi64((__m128i *)(ap[2])), zero);
        p1 = _mm_unpacklo_epi8(_mm_loadl_epi64((__m128i *)(ap[1])), zero);
        p0 = _mm_unpacklo_epi8(_mm_loadl_epi64((__m128i *)(ap[0])), zero);
        q0 = _mm_unpacklo_epi8(_mm_loadl_epi64((__m128i *)(aq[0])), zero);
        q1 = _mm_unpacklo_epi8(_mm_loadl_epi64((__m128i *)(aq[1])), zero);
        q2 = _mm_unpacklo_epi8(_mm_loadl_epi64((__m128i *)(aq[2])), zero);
        q3 = _mm_unpacklo_epi8(_mm_loadl_epi64((__m128i *)(aq[3])), zero);
        q4 = _mm_unpacklo_epi8(_mm_loadl_epi64((__m128i *)(aq[4])), zero);
        q5 = _mm_unpacklo_epi8(_mm_loadl_epi64((__m128i *)(aq[5])), zero);
        q6 = _mm_unpacklo_epi8(_mm_loadl_epi64((__m128i *)(aq[6])), zero);
        q7 = _mm_unpacklo_epi8(_mm_loadl_epi64((__m128i *)(aq[7])), zero);
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368

        c = _mm_sub_epi16(_mm_slli_epi16(p7, 3), p7);  // p7 * 7
        c = _mm_add_epi16(_mm_slli_epi16(p6, 1), _mm_add_epi16(p4, c));

        b = _mm_add_epi16(_mm_add_epi16(p3, four), _mm_add_epi16(p3, p2));
        a = _mm_add_epi16(p3, _mm_add_epi16(p2, p1));
        a = _mm_add_epi16(_mm_add_epi16(p0, q0), a);

        _mm_storel_epi64((__m128i *)&flat_op[2][i*8],
                         _mm_packus_epi16(_mm_srli_epi16(_mm_add_epi16(a, b), 3)
                                          , b));

        c = _mm_add_epi16(_mm_add_epi16(p5, eight), c);
        workp_shft = _mm_srli_epi16(_mm_add_epi16(a, c), 4);
        _mm_storel_epi64((__m128i *)&flat2_op[6][i*8],
                         _mm_packus_epi16(workp_shft, workp_shft));

        a = _mm_add_epi16(q1, a);
        b = _mm_add_epi16(_mm_sub_epi16(b, _mm_add_epi16(p3, p2)), p1);
        _mm_storel_epi64((__m128i *)&flat_op[1][i*8],
                         _mm_packus_epi16(_mm_srli_epi16(_mm_add_epi16(a, b), 3)
                                          , b));

        c = _mm_add_epi16(_mm_sub_epi16(c, _mm_add_epi16(p7, p6)), p5);
        workp_shft = _mm_srli_epi16(_mm_add_epi16(a, c), 4);
        _mm_storel_epi64((__m128i *)&flat2_op[5][i*8],
                         _mm_packus_epi16(workp_shft, workp_shft));

        a = _mm_add_epi16(q2, a);
        b = _mm_add_epi16(_mm_sub_epi16(b, _mm_add_epi16(p3, p1)), p0);
        _mm_storel_epi64((__m128i *)&flat_op[0][i*8],
                         _mm_packus_epi16(_mm_srli_epi16(_mm_add_epi16(a, b), 3)
                                          , b));

        c = _mm_add_epi16(_mm_sub_epi16(c, _mm_add_epi16(p7, p5)), p4);
        workp_shft = _mm_srli_epi16(_mm_add_epi16(a, c), 4);
        _mm_storel_epi64((__m128i *)&flat2_op[4][i*8],
                         _mm_packus_epi16(workp_shft, workp_shft));

        a = _mm_add_epi16(q3, a);
        b = _mm_add_epi16(_mm_sub_epi16(b, _mm_add_epi16(p3, p0)), q0);
        _mm_storel_epi64((__m128i *)&flat_oq[0][i*8],
                         _mm_packus_epi16(_mm_srli_epi16(_mm_add_epi16(a, b), 3)
                                          , b));

        c = _mm_add_epi16(_mm_sub_epi16(c, _mm_add_epi16(p7, p4)), p3);
        workp_shft = _mm_srli_epi16(_mm_add_epi16(a, c), 4);
        _mm_storel_epi64((__m128i *)&flat2_op[3][i*8],
                         _mm_packus_epi16(workp_shft, workp_shft));

        b = _mm_add_epi16(q3, b);
        b = _mm_add_epi16(_mm_sub_epi16(b, _mm_add_epi16(p2, q0)), q1);
        _mm_storel_epi64((__m128i *)&flat_oq[1][i*8],
                         _mm_packus_epi16(_mm_srli_epi16(_mm_add_epi16(a, b), 3)
                                          , b));

        c = _mm_add_epi16(q4, c);
        c = _mm_add_epi16(_mm_sub_epi16(c, _mm_add_epi16(p7, p3)), p2);
        workp_shft = _mm_srli_epi16(_mm_add_epi16(a, c), 4);
        _mm_storel_epi64((__m128i *)&flat2_op[2][i*8],
                         _mm_packus_epi16(workp_shft, workp_shft));

        b = _mm_add_epi16(q3, b);
        b = _mm_add_epi16(_mm_sub_epi16(b, _mm_add_epi16(p1, q1)), q2);
        _mm_storel_epi64((__m128i *)&flat_oq[2][i*8],
                         _mm_packus_epi16(_mm_srli_epi16(_mm_add_epi16(a, b), 3)
                                          , b));
        a = _mm_add_epi16(q5, a);
        c = _mm_add_epi16(_mm_sub_epi16(c, _mm_add_epi16(p7, p2)), p1);
        workp_shft = _mm_srli_epi16(_mm_add_epi16(a, c), 4);
        _mm_storel_epi64((__m128i *)&flat2_op[1][i*8],
                         _mm_packus_epi16(workp_shft, workp_shft));

        a = _mm_add_epi16(q6, a);
        c = _mm_add_epi16(_mm_sub_epi16(c, _mm_add_epi16(p7, p1)), p0);
        workp_shft = _mm_srli_epi16(_mm_add_epi16(a, c), 4);
        _mm_storel_epi64((__m128i *)&flat2_op[0][i*8],
                         _mm_packus_epi16(workp_shft, workp_shft));

        a = _mm_add_epi16(q7, a);
        c = _mm_add_epi16(_mm_sub_epi16(c, _mm_add_epi16(p7, p0)), q0);
        workp_shft = _mm_srli_epi16(_mm_add_epi16(a, c), 4);
        _mm_storel_epi64((__m128i *)&flat2_oq[0][i*8],
                         _mm_packus_epi16(workp_shft, workp_shft));

        a = _mm_add_epi16(q7, a);
        c = _mm_add_epi16(_mm_sub_epi16(c, _mm_add_epi16(p6, q0)), q1);
        workp_shft = _mm_srli_epi16(_mm_add_epi16(a, c), 4);
        _mm_storel_epi64((__m128i *)&flat2_oq[1][i*8],
                         _mm_packus_epi16(workp_shft, workp_shft));

        a = _mm_add_epi16(q7, a);
        c = _mm_add_epi16(_mm_sub_epi16(c, _mm_add_epi16(p5, q1)), q2);
        workp_shft = _mm_srli_epi16(_mm_add_epi16(a, c), 4);
        _mm_storel_epi64((__m128i *)&flat2_oq[2][i*8],
                         _mm_packus_epi16(workp_shft, workp_shft));

        a = _mm_add_epi16(q7, a);
        c = _mm_add_epi16(_mm_sub_epi16(c, _mm_add_epi16(p4, q2)), q3);
        workp_shft = _mm_srli_epi16(_mm_add_epi16(a, c), 4);
        _mm_storel_epi64((__m128i *)&flat2_oq[3][i*8],
                         _mm_packus_epi16(workp_shft, workp_shft));

        a = _mm_add_epi16(q7, a);
        c = _mm_add_epi16(_mm_sub_epi16(c, _mm_add_epi16(p3, q3)), q4);
        workp_shft = _mm_srli_epi16(_mm_add_epi16(a, c), 4);
        _mm_storel_epi64((__m128i *)&flat2_oq[4][i*8],
                         _mm_packus_epi16(workp_shft, workp_shft));

        a = _mm_add_epi16(q7, a);
        c = _mm_add_epi16(_mm_sub_epi16(c, _mm_add_epi16(p2, q4)), q5);
        workp_shft = _mm_srli_epi16(_mm_add_epi16(a, c), 4);
        _mm_storel_epi64((__m128i *)&flat2_oq[5][i*8],
                         _mm_packus_epi16(workp_shft, workp_shft));

        a = _mm_add_epi16(q7, a);
        c = _mm_add_epi16(_mm_sub_epi16(c, _mm_add_epi16(p1, q5)), q6);
        workp_shft = _mm_srli_epi16(_mm_add_epi16(a, c), 4);
        _mm_storel_epi64((__m128i *)&flat2_oq[6][i*8],
                         _mm_packus_epi16(workp_shft, workp_shft));
369
      }
370
371
372
373
    }
    // wide flat
    // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

374
375
    work_a = _mm_loadl_epi64((__m128i *)ap[2]);
    p2 = _mm_loadl_epi64((__m128i *)flat_op[2]);
376
377
378
    work_a = _mm_andnot_si128(flat, work_a);
    p2 = _mm_and_si128(flat, p2);
    p2 = _mm_or_si128(work_a, p2);
379
    _mm_storel_epi64((__m128i *)flat_op[2], p2);
380

381
    p1 = _mm_loadl_epi64((__m128i *)flat_op[1]);
382
383
384
    work_a = _mm_andnot_si128(flat, ps1);
    p1 = _mm_and_si128(flat, p1);
    p1 = _mm_or_si128(work_a, p1);
385
    _mm_storel_epi64((__m128i *)flat_op[1], p1);
386

387
    p0 = _mm_loadl_epi64((__m128i *)flat_op[0]);
388
389
390
    work_a = _mm_andnot_si128(flat, ps0);
    p0 = _mm_and_si128(flat, p0);
    p0 = _mm_or_si128(work_a, p0);
391
    _mm_storel_epi64((__m128i *)flat_op[0], p0);
392

393
    q0 = _mm_loadl_epi64((__m128i *)flat_oq[0]);
394
395
396
    work_a = _mm_andnot_si128(flat, qs0);
    q0 = _mm_and_si128(flat, q0);
    q0 = _mm_or_si128(work_a, q0);
397
    _mm_storel_epi64((__m128i *)flat_oq[0], q0);
398

399
    q1 = _mm_loadl_epi64((__m128i *)flat_oq[1]);
400
401
402
    work_a = _mm_andnot_si128(flat, qs1);
    q1 = _mm_and_si128(flat, q1);
    q1 = _mm_or_si128(work_a, q1);
403
    _mm_storel_epi64((__m128i *)flat_oq[1], q1);
404

405
406
    work_a = _mm_loadl_epi64((__m128i *)aq[2]);
    q2 = _mm_loadl_epi64((__m128i *)flat_oq[2]);
407
408
409
    work_a = _mm_andnot_si128(flat, work_a);
    q2 = _mm_and_si128(flat, q2);
    q2 = _mm_or_si128(work_a, q2);
410
    _mm_storel_epi64((__m128i *)flat_oq[2], q2);
411
412
413
414
415
416

    // write out op6 - op3
    {
      unsigned char *dst = (s - 7 * p);
      for (i = 6; i > 2; i--) {
        __m128i flat2_output;
417
418
        work_a = _mm_loadl_epi64((__m128i *)ap[i]);
        flat2_output = _mm_loadl_epi64((__m128i *)flat2_op[i]);
419
420
421
        work_a = _mm_andnot_si128(flat2, work_a);
        flat2_output = _mm_and_si128(flat2, flat2_output);
        work_a = _mm_or_si128(work_a, flat2_output);
422
        _mm_storel_epi64((__m128i *)dst, work_a);
423
424
425
426
        dst += p;
      }
    }

427
428
    work_a = _mm_loadl_epi64((__m128i *)flat_op[2]);
    p2 = _mm_loadl_epi64((__m128i *)flat2_op[2]);
429
430
431
    work_a = _mm_andnot_si128(flat2, work_a);
    p2 = _mm_and_si128(flat2, p2);
    p2 = _mm_or_si128(work_a, p2);
432
    _mm_storel_epi64((__m128i *)(s - 3 * p), p2);
433

434
435
    work_a = _mm_loadl_epi64((__m128i *)flat_op[1]);
    p1 = _mm_loadl_epi64((__m128i *)flat2_op[1]);
436
437
438
    work_a = _mm_andnot_si128(flat2, work_a);
    p1 = _mm_and_si128(flat2, p1);
    p1 = _mm_or_si128(work_a, p1);
439
    _mm_storel_epi64((__m128i *)(s - 2 * p), p1);
440

441
442
    work_a = _mm_loadl_epi64((__m128i *)flat_op[0]);
    p0 = _mm_loadl_epi64((__m128i *)flat2_op[0]);
443
444
445
    work_a = _mm_andnot_si128(flat2, work_a);
    p0 = _mm_and_si128(flat2, p0);
    p0 = _mm_or_si128(work_a, p0);
446
    _mm_storel_epi64((__m128i *)(s - 1 * p), p0);
447

448
449
    work_a = _mm_loadl_epi64((__m128i *)flat_oq[0]);
    q0 = _mm_loadl_epi64((__m128i *)flat2_oq[0]);
450
451
452
    work_a = _mm_andnot_si128(flat2, work_a);
    q0 = _mm_and_si128(flat2, q0);
    q0 = _mm_or_si128(work_a, q0);
453
    _mm_storel_epi64((__m128i *)(s - 0 * p), q0);
454

455
456
    work_a = _mm_loadl_epi64((__m128i *)flat_oq[1]);
    q1 = _mm_loadl_epi64((__m128i *)flat2_oq[1]);
457
458
459
    work_a = _mm_andnot_si128(flat2, work_a);
    q1 = _mm_and_si128(flat2, q1);
    q1 = _mm_or_si128(work_a, q1);
460
    _mm_storel_epi64((__m128i *)(s + 1 * p), q1);
461

462
463
    work_a = _mm_loadl_epi64((__m128i *)flat_oq[2]);
    q2 = _mm_loadl_epi64((__m128i *)flat2_oq[2]);
464
465
466
    work_a = _mm_andnot_si128(flat2, work_a);
    q2 = _mm_and_si128(flat2, q2);
    q2 = _mm_or_si128(work_a, q2);
467
    _mm_storel_epi64((__m128i *)(s + 2 * p), q2);
468
469
470
471
472
473

    // write out oq3 - oq7
    {
      unsigned char *dst = (s + 3 * p);
      for (i = 3; i < 7; i++) {
        __m128i flat2_output;
474
475
        work_a = _mm_loadl_epi64((__m128i *)aq[i]);
        flat2_output = _mm_loadl_epi64((__m128i *)flat2_oq[i]);
476
477
478
        work_a = _mm_andnot_si128(flat2, work_a);
        flat2_output = _mm_and_si128(flat2, flat2_output);
        work_a = _mm_or_si128(work_a, flat2_output);
479
        _mm_storel_epi64((__m128i *)dst, work_a);
480
481
482
483
484
485
        dst += p;
      }
    }
  }
}

486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
void vp9_mb_lpf_horizontal_edge_w_sse2_16(unsigned char *s,
                                          int p,
                                          const unsigned char *_blimit,
                                          const unsigned char *_limit,
                                          const unsigned char *_thresh) {
  DECLARE_ALIGNED(16, unsigned char, flat2_op[7][16]);
  DECLARE_ALIGNED(16, unsigned char, flat2_oq[7][16]);

  DECLARE_ALIGNED(16, unsigned char, flat_op[3][16]);
  DECLARE_ALIGNED(16, unsigned char, flat_oq[3][16]);

  DECLARE_ALIGNED(16, unsigned char, ap[8][16]);
  DECLARE_ALIGNED(16, unsigned char, aq[8][16]);


  __m128i mask, hev, flat, flat2;
  const __m128i zero = _mm_set1_epi16(0);
  const __m128i one = _mm_set1_epi8(1);
  __m128i p7, p6, p5;
  __m128i p4, p3, p2, p1, p0, q0, q1, q2, q3, q4;
  __m128i q5, q6, q7;
  int i = 0;
  const unsigned int extended_thresh = _thresh[0] * 0x01010101u;
  const unsigned int extended_limit  = _limit[0]  * 0x01010101u;
  const unsigned int extended_blimit = _blimit[0] * 0x01010101u;
  const __m128i thresh =
      _mm_shuffle_epi32(_mm_cvtsi32_si128((int)extended_thresh), 0);
  const __m128i limit =
      _mm_shuffle_epi32(_mm_cvtsi32_si128((int)extended_limit), 0);
  const __m128i blimit =
      _mm_shuffle_epi32(_mm_cvtsi32_si128((int)extended_blimit), 0);

  p4 = _mm_loadu_si128((__m128i *)(s - 5 * p));
  p3 = _mm_loadu_si128((__m128i *)(s - 4 * p));
  p2 = _mm_loadu_si128((__m128i *)(s - 3 * p));
  p1 = _mm_loadu_si128((__m128i *)(s - 2 * p));
  p0 = _mm_loadu_si128((__m128i *)(s - 1 * p));
  q0 = _mm_loadu_si128((__m128i *)(s - 0 * p));
  q1 = _mm_loadu_si128((__m128i *)(s + 1 * p));
  q2 = _mm_loadu_si128((__m128i *)(s + 2 * p));
  q3 = _mm_loadu_si128((__m128i *)(s + 3 * p));
  q4 = _mm_loadu_si128((__m128i *)(s + 4 * p));

  _mm_store_si128((__m128i *)ap[4], p4);
  _mm_store_si128((__m128i *)ap[3], p3);
  _mm_store_si128((__m128i *)ap[2], p2);
  _mm_store_si128((__m128i *)ap[1], p1);
  _mm_store_si128((__m128i *)ap[0], p0);
  _mm_store_si128((__m128i *)aq[4], q4);
  _mm_store_si128((__m128i *)aq[3], q3);
  _mm_store_si128((__m128i *)aq[2], q2);
  _mm_store_si128((__m128i *)aq[1], q1);
  _mm_store_si128((__m128i *)aq[0], q0);


  {
    const __m128i abs_p1p0 = _mm_or_si128(_mm_subs_epu8(p1, p0),
                                          _mm_subs_epu8(p0, p1));
    const __m128i abs_q1q0 = _mm_or_si128(_mm_subs_epu8(q1, q0),
                                          _mm_subs_epu8(q0, q1));
    const __m128i fe = _mm_set1_epi8(0xfe);
    const __m128i ff = _mm_cmpeq_epi8(abs_p1p0, abs_p1p0);
    __m128i abs_p0q0 = _mm_or_si128(_mm_subs_epu8(p0, q0),
                                    _mm_subs_epu8(q0, p0));
    __m128i abs_p1q1 = _mm_or_si128(_mm_subs_epu8(p1, q1),
                                    _mm_subs_epu8(q1, p1));
    __m128i work;
    flat = _mm_max_epu8(abs_p1p0, abs_q1q0);
    hev = _mm_subs_epu8(flat, thresh);
    hev = _mm_xor_si128(_mm_cmpeq_epi8(hev, zero), ff);

    abs_p0q0 =_mm_adds_epu8(abs_p0q0, abs_p0q0);
    abs_p1q1 = _mm_srli_epi16(_mm_and_si128(abs_p1q1, fe), 1);
    mask = _mm_subs_epu8(_mm_adds_epu8(abs_p0q0, abs_p1q1), blimit);
    mask = _mm_xor_si128(_mm_cmpeq_epi8(mask, zero), ff);
    // mask |= (abs(p0 - q0) * 2 + abs(p1 - q1) / 2  > blimit) * -1;
    mask = _mm_max_epu8(flat, mask);
    // mask |= (abs(p1 - p0) > limit) * -1;
    // mask |= (abs(q1 - q0) > limit) * -1;
    work = _mm_max_epu8(_mm_or_si128(_mm_subs_epu8(p2, p1),
                                     _mm_subs_epu8(p1, p2)),
                         _mm_or_si128(_mm_subs_epu8(p3, p2),
                                      _mm_subs_epu8(p2, p3)));
    mask = _mm_max_epu8(work, mask);
    work = _mm_max_epu8(_mm_or_si128(_mm_subs_epu8(q2, q1),
                                     _mm_subs_epu8(q1, q2)),
                         _mm_or_si128(_mm_subs_epu8(q3, q2),
                                      _mm_subs_epu8(q2, q3)));
    mask = _mm_max_epu8(work, mask);
    mask = _mm_subs_epu8(mask, limit);
    mask = _mm_cmpeq_epi8(mask, zero);
  }

  // lp filter
  {
    const __m128i t4 = _mm_set1_epi8(4);
    const __m128i t3 = _mm_set1_epi8(3);
    const __m128i t80 = _mm_set1_epi8(0x80);
    const __m128i te0 = _mm_set1_epi8(0xe0);
    const __m128i t1f = _mm_set1_epi8(0x1f);
    const __m128i t1 = _mm_set1_epi8(0x1);
    const __m128i t7f = _mm_set1_epi8(0x7f);

    __m128i ps1 = _mm_xor_si128(p1, t80);
    __m128i ps0 = _mm_xor_si128(p0, t80);
    __m128i qs0 = _mm_xor_si128(q0, t80);
    __m128i qs1 = _mm_xor_si128(q1, t80);
    __m128i filt;
    __m128i work_a;
    __m128i filter1, filter2;

    filt = _mm_and_si128(_mm_subs_epi8(ps1, qs1), hev);
    work_a = _mm_subs_epi8(qs0, ps0);
    filt = _mm_adds_epi8(filt, work_a);
    filt = _mm_adds_epi8(filt, work_a);
    filt = _mm_adds_epi8(filt, work_a);
    /* (vp9_filter + 3 * (qs0 - ps0)) & mask */
    filt = _mm_and_si128(filt, mask);

    filter1 = _mm_adds_epi8(filt, t4);
    filter2 = _mm_adds_epi8(filt, t3);

    /* Filter1 >> 3 */
    work_a = _mm_cmpgt_epi8(zero, filter1);
    filter1 = _mm_srli_epi16(filter1, 3);
    work_a = _mm_and_si128(work_a, te0);
    filter1 = _mm_and_si128(filter1, t1f);
    filter1 = _mm_or_si128(filter1, work_a);
    qs0 = _mm_xor_si128(_mm_subs_epi8(qs0, filter1), t80);

    /* Filter2 >> 3 */
    work_a = _mm_cmpgt_epi8(zero, filter2);
    filter2 = _mm_srli_epi16(filter2, 3);
    work_a = _mm_and_si128(work_a, te0);
    filter2 = _mm_and_si128(filter2, t1f);
    filter2 = _mm_or_si128(filter2, work_a);
    ps0 = _mm_xor_si128(_mm_adds_epi8(ps0, filter2), t80);

    /* filt >> 1 */
    filt = _mm_adds_epi8(filter1, t1);
    work_a = _mm_cmpgt_epi8(zero, filt);
    filt = _mm_srli_epi16(filt, 1);
    work_a = _mm_and_si128(work_a, t80);
    filt = _mm_and_si128(filt, t7f);
    filt = _mm_or_si128(filt, work_a);
    filt = _mm_andnot_si128(hev, filt);
    ps1 = _mm_xor_si128(_mm_adds_epi8(ps1, filt), t80);
    qs1 = _mm_xor_si128(_mm_subs_epi8(qs1, filt), t80);
    // loopfilter done

    {
      __m128i work;
      work = _mm_max_epu8(_mm_or_si128(_mm_subs_epu8(p2, p0),
                                       _mm_subs_epu8(p0, p2)),
                           _mm_or_si128(_mm_subs_epu8(q2, q0),
                                        _mm_subs_epu8(q0, q2)));
      flat = _mm_max_epu8(work, flat);
      work = _mm_max_epu8(_mm_or_si128(_mm_subs_epu8(p3, p0),
                                       _mm_subs_epu8(p0, p3)),
                           _mm_or_si128(_mm_subs_epu8(q3, q0),
                                        _mm_subs_epu8(q0, q3)));
      flat = _mm_max_epu8(work, flat);
      work = _mm_max_epu8(_mm_or_si128(_mm_subs_epu8(p4, p0),
                                       _mm_subs_epu8(p0, p4)),
                           _mm_or_si128(_mm_subs_epu8(q4, q0),
                                        _mm_subs_epu8(q0, q4)));
      flat = _mm_subs_epu8(flat, one);
      flat = _mm_cmpeq_epi8(flat, zero);
      flat = _mm_and_si128(flat, mask);

      p5 = _mm_loadu_si128((__m128i *)(s - 6 * p));
      q5 = _mm_loadu_si128((__m128i *)(s + 5 * p));
      flat2 = _mm_max_epu8(_mm_or_si128(_mm_subs_epu8(p5, p0),
                                       _mm_subs_epu8(p0, p5)),
                           _mm_or_si128(_mm_subs_epu8(q5, q0),
                                        _mm_subs_epu8(q0, q5)));
      _mm_store_si128((__m128i *)ap[5], p5);
      _mm_store_si128((__m128i *)aq[5], q5);
      flat2 = _mm_max_epu8(work, flat2);
      p6 = _mm_loadu_si128((__m128i *)(s - 7 * p));
      q6 = _mm_loadu_si128((__m128i *)(s + 6 * p));
      work = _mm_max_epu8(_mm_or_si128(_mm_subs_epu8(p6, p0),
                                       _mm_subs_epu8(p0, p6)),
                           _mm_or_si128(_mm_subs_epu8(q6, q0),
                                        _mm_subs_epu8(q0, q6)));
      _mm_store_si128((__m128i *)ap[6], p6);
      _mm_store_si128((__m128i *)aq[6], q6);
      flat2 = _mm_max_epu8(work, flat2);

      p7 = _mm_loadu_si128((__m128i *)(s - 8 * p));
      q7 = _mm_loadu_si128((__m128i *)(s + 7 * p));
      work = _mm_max_epu8(_mm_or_si128(_mm_subs_epu8(p7, p0),
                                       _mm_subs_epu8(p0, p7)),
                           _mm_or_si128(_mm_subs_epu8(q7, q0),
                                        _mm_subs_epu8(q0, q7)));
      _mm_store_si128((__m128i *)ap[7], p7);
      _mm_store_si128((__m128i *)aq[7], q7);
      flat2 = _mm_max_epu8(work, flat2);
      flat2 = _mm_subs_epu8(flat2, one);
      flat2 = _mm_cmpeq_epi8(flat2, zero);
      flat2 = _mm_and_si128(flat2, flat);  // flat2 & flat & mask
    }

    // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    // flat and wide flat calculations
    {
      const __m128i eight = _mm_set1_epi16(8);
      const __m128i four = _mm_set1_epi16(4);
      __m128i temp_flat2 = flat2;
      unsigned char *src = s;
      int i = 0;
      do {
        __m128i workp_shft;
        __m128i a, b, c;

        unsigned int off = i * 8;
        p7 = _mm_unpacklo_epi8(_mm_loadl_epi64((__m128i *)(ap[7] + off)), zero);
        p6 = _mm_unpacklo_epi8(_mm_loadl_epi64((__m128i *)(ap[6] + off)), zero);
        p5 = _mm_unpacklo_epi8(_mm_loadl_epi64((__m128i *)(ap[5] + off)), zero);
        p4 = _mm_unpacklo_epi8(_mm_loadl_epi64((__m128i *)(ap[4] + off)), zero);
        p3 = _mm_unpacklo_epi8(_mm_loadl_epi64((__m128i *)(ap[3] + off)), zero);
        p2 = _mm_unpacklo_epi8(_mm_loadl_epi64((__m128i *)(ap[2] + off)), zero);
        p1 = _mm_unpacklo_epi8(_mm_loadl_epi64((__m128i *)(ap[1] + off)), zero);
        p0 = _mm_unpacklo_epi8(_mm_loadl_epi64((__m128i *)(ap[0] + off)), zero);
        q0 = _mm_unpacklo_epi8(_mm_loadl_epi64((__m128i *)(aq[0] + off)), zero);
        q1 = _mm_unpacklo_epi8(_mm_loadl_epi64((__m128i *)(aq[1] + off)), zero);
        q2 = _mm_unpacklo_epi8(_mm_loadl_epi64((__m128i *)(aq[2] + off)), zero);
        q3 = _mm_unpacklo_epi8(_mm_loadl_epi64((__m128i *)(aq[3] + off)), zero);
        q4 = _mm_unpacklo_epi8(_mm_loadl_epi64((__m128i *)(aq[4] + off)), zero);
        q5 = _mm_unpacklo_epi8(_mm_loadl_epi64((__m128i *)(aq[5] + off)), zero);
        q6 = _mm_unpacklo_epi8(_mm_loadl_epi64((__m128i *)(aq[6] + off)), zero);
        q7 = _mm_unpacklo_epi8(_mm_loadl_epi64((__m128i *)(aq[7] + off)), zero);

        c = _mm_sub_epi16(_mm_slli_epi16(p7, 3), p7);  // p7 * 7
        c = _mm_add_epi16(_mm_slli_epi16(p6, 1), _mm_add_epi16(p4, c));

        b = _mm_add_epi16(_mm_add_epi16(p3, four), _mm_add_epi16(p3, p2));
        a = _mm_add_epi16(p3, _mm_add_epi16(p2, p1));
        a = _mm_add_epi16(_mm_add_epi16(p0, q0), a);

        _mm_storel_epi64((__m128i *)&flat_op[2][i*8],
                         _mm_packus_epi16(_mm_srli_epi16(_mm_add_epi16(a, b), 3)
                                          , b));

        c = _mm_add_epi16(_mm_add_epi16(p5, eight), c);
        workp_shft = _mm_srli_epi16(_mm_add_epi16(a, c), 4);
        _mm_storel_epi64((__m128i *)&flat2_op[6][i*8],
                         _mm_packus_epi16(workp_shft, workp_shft));

        a = _mm_add_epi16(q1, a);
        b = _mm_add_epi16(_mm_sub_epi16(b, _mm_add_epi16(p3, p2)), p1);
        _mm_storel_epi64((__m128i *)&flat_op[1][i*8],
                         _mm_packus_epi16(_mm_srli_epi16(_mm_add_epi16(a, b), 3)
                                          , b));

        c = _mm_add_epi16(_mm_sub_epi16(c, _mm_add_epi16(p7, p6)), p5);
        workp_shft = _mm_srli_epi16(_mm_add_epi16(a, c), 4);
        _mm_storel_epi64((__m128i *)&flat2_op[5][i*8],
                         _mm_packus_epi16(workp_shft, workp_shft));

        a = _mm_add_epi16(q2, a);
        b = _mm_add_epi16(_mm_sub_epi16(b, _mm_add_epi16(p3, p1)), p0);
        _mm_storel_epi64((__m128i *)&flat_op[0][i*8],
                         _mm_packus_epi16(_mm_srli_epi16(_mm_add_epi16(a, b), 3)
                                          , b));

        c = _mm_add_epi16(_mm_sub_epi16(c, _mm_add_epi16(p7, p5)), p4);
        workp_shft = _mm_srli_epi16(_mm_add_epi16(a, c), 4);
        _mm_storel_epi64((__m128i *)&flat2_op[4][i*8],
                         _mm_packus_epi16(workp_shft, workp_shft));

        a = _mm_add_epi16(q3, a);
        b = _mm_add_epi16(_mm_sub_epi16(b, _mm_add_epi16(p3, p0)), q0);
        _mm_storel_epi64((__m128i *)&flat_oq[0][i*8],
                         _mm_packus_epi16(_mm_srli_epi16(_mm_add_epi16(a, b), 3)
                                          , b));

        c = _mm_add_epi16(_mm_sub_epi16(c, _mm_add_epi16(p7, p4)), p3);
        workp_shft = _mm_srli_epi16(_mm_add_epi16(a, c), 4);
        _mm_storel_epi64((__m128i *)&flat2_op[3][i*8],
                         _mm_packus_epi16(workp_shft, workp_shft));

        b = _mm_add_epi16(q3, b);
        b = _mm_add_epi16(_mm_sub_epi16(b, _mm_add_epi16(p2, q0)), q1);
        _mm_storel_epi64((__m128i *)&flat_oq[1][i*8],
                         _mm_packus_epi16(_mm_srli_epi16(_mm_add_epi16(a, b), 3)
                                          , b));

        c = _mm_add_epi16(q4, c);
        c = _mm_add_epi16(_mm_sub_epi16(c, _mm_add_epi16(p7, p3)), p2);
        workp_shft = _mm_srli_epi16(_mm_add_epi16(a, c), 4);
        _mm_storel_epi64((__m128i *)&flat2_op[2][i*8],
                         _mm_packus_epi16(workp_shft, workp_shft));

        b = _mm_add_epi16(q3, b);
        b = _mm_add_epi16(_mm_sub_epi16(b, _mm_add_epi16(p1, q1)), q2);
        _mm_storel_epi64((__m128i *)&flat_oq[2][i*8],
                         _mm_packus_epi16(_mm_srli_epi16(_mm_add_epi16(a, b), 3)
                                          , b));
        a = _mm_add_epi16(q5, a);
        c = _mm_add_epi16(_mm_sub_epi16(c, _mm_add_epi16(p7, p2)), p1);
        workp_shft = _mm_srli_epi16(_mm_add_epi16(a, c), 4);
        _mm_storel_epi64((__m128i *)&flat2_op[1][i*8],
                         _mm_packus_epi16(workp_shft, workp_shft));

        a = _mm_add_epi16(q6, a);
        c = _mm_add_epi16(_mm_sub_epi16(c, _mm_add_epi16(p7, p1)), p0);
        workp_shft = _mm_srli_epi16(_mm_add_epi16(a, c), 4);
        _mm_storel_epi64((__m128i *)&flat2_op[0][i*8],
                         _mm_packus_epi16(workp_shft, workp_shft));

        a = _mm_add_epi16(q7, a);
        c = _mm_add_epi16(_mm_sub_epi16(c, _mm_add_epi16(p7, p0)), q0);
        workp_shft = _mm_srli_epi16(_mm_add_epi16(a, c), 4);
        _mm_storel_epi64((__m128i *)&flat2_oq[0][i*8],
                         _mm_packus_epi16(workp_shft, workp_shft));

        a = _mm_add_epi16(q7, a);
        c = _mm_add_epi16(_mm_sub_epi16(c, _mm_add_epi16(p6, q0)), q1);
        workp_shft = _mm_srli_epi16(_mm_add_epi16(a, c), 4);
        _mm_storel_epi64((__m128i *)&flat2_oq[1][i*8],
                         _mm_packus_epi16(workp_shft, workp_shft));

        a = _mm_add_epi16(q7, a);
        c = _mm_add_epi16(_mm_sub_epi16(c, _mm_add_epi16(p5, q1)), q2);
        workp_shft = _mm_srli_epi16(_mm_add_epi16(a, c), 4);
        _mm_storel_epi64((__m128i *)&flat2_oq[2][i*8],
                         _mm_packus_epi16(workp_shft, workp_shft));

        a = _mm_add_epi16(q7, a);
        c = _mm_add_epi16(_mm_sub_epi16(c, _mm_add_epi16(p4, q2)), q3);
        workp_shft = _mm_srli_epi16(_mm_add_epi16(a, c), 4);
        _mm_storel_epi64((__m128i *)&flat2_oq[3][i*8],
                         _mm_packus_epi16(workp_shft, workp_shft));

        a = _mm_add_epi16(q7, a);
        c = _mm_add_epi16(_mm_sub_epi16(c, _mm_add_epi16(p3, q3)), q4);
        workp_shft = _mm_srli_epi16(_mm_add_epi16(a, c), 4);
        _mm_storel_epi64((__m128i *)&flat2_oq[4][i*8],
                         _mm_packus_epi16(workp_shft, workp_shft));

        a = _mm_add_epi16(q7, a);
        c = _mm_add_epi16(_mm_sub_epi16(c, _mm_add_epi16(p2, q4)), q5);
        workp_shft = _mm_srli_epi16(_mm_add_epi16(a, c), 4);
        _mm_storel_epi64((__m128i *)&flat2_oq[5][i*8],
                         _mm_packus_epi16(workp_shft, workp_shft));

        a = _mm_add_epi16(q7, a);
        c = _mm_add_epi16(_mm_sub_epi16(c, _mm_add_epi16(p1, q5)), q6);
        workp_shft = _mm_srli_epi16(_mm_add_epi16(a, c), 4);
        _mm_storel_epi64((__m128i *)&flat2_oq[6][i*8],
                         _mm_packus_epi16(workp_shft, workp_shft));

        temp_flat2 = _mm_srli_si128(temp_flat2, 8);
        src += 8;
      } while (++i < 2);
    }
    // wide flat
    // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

    work_a = _mm_load_si128((__m128i *)ap[2]);
    p2 = _mm_load_si128((__m128i *)flat_op[2]);
    work_a = _mm_andnot_si128(flat, work_a);
    p2 = _mm_and_si128(flat, p2);
    p2 = _mm_or_si128(work_a, p2);
    _mm_store_si128((__m128i *)flat_op[2], p2);

    p1 = _mm_load_si128((__m128i *)flat_op[1]);
    work_a = _mm_andnot_si128(flat, ps1);
    p1 = _mm_and_si128(flat, p1);
    p1 = _mm_or_si128(work_a, p1);
    _mm_store_si128((__m128i *)flat_op[1], p1);

    p0 = _mm_load_si128((__m128i *)flat_op[0]);
    work_a = _mm_andnot_si128(flat, ps0);
    p0 = _mm_and_si128(flat, p0);
    p0 = _mm_or_si128(work_a, p0);
    _mm_store_si128((__m128i *)flat_op[0], p0);

    q0 = _mm_load_si128((__m128i *)flat_oq[0]);
    work_a = _mm_andnot_si128(flat, qs0);
    q0 = _mm_and_si128(flat, q0);
    q0 = _mm_or_si128(work_a, q0);
    _mm_store_si128((__m128i *)flat_oq[0], q0);

    q1 = _mm_load_si128((__m128i *)flat_oq[1]);
    work_a = _mm_andnot_si128(flat, qs1);
    q1 = _mm_and_si128(flat, q1);
    q1 = _mm_or_si128(work_a, q1);
    _mm_store_si128((__m128i *)flat_oq[1], q1);

    work_a = _mm_load_si128((__m128i *)aq[2]);
    q2 = _mm_load_si128((__m128i *)flat_oq[2]);
    work_a = _mm_andnot_si128(flat, work_a);
    q2 = _mm_and_si128(flat, q2);
    q2 = _mm_or_si128(work_a, q2);
    _mm_store_si128((__m128i *)flat_oq[2], q2);

    // write out op6 - op3
    {
      unsigned char *dst = (s - 7 * p);
      for (i = 6; i > 2; i--) {
        __m128i flat2_output;
        work_a = _mm_load_si128((__m128i *)ap[i]);
        flat2_output = _mm_load_si128((__m128i *)flat2_op[i]);
        work_a = _mm_andnot_si128(flat2, work_a);
        flat2_output = _mm_and_si128(flat2, flat2_output);
        work_a = _mm_or_si128(work_a, flat2_output);
        _mm_storeu_si128((__m128i *)dst, work_a);
        dst += p;
      }
    }

    work_a = _mm_load_si128((__m128i *)flat_op[2]);
    p2 = _mm_load_si128((__m128i *)flat2_op[2]);
    work_a = _mm_andnot_si128(flat2, work_a);
    p2 = _mm_and_si128(flat2, p2);
    p2 = _mm_or_si128(work_a, p2);
    _mm_storeu_si128((__m128i *)(s - 3 * p), p2);

    work_a = _mm_load_si128((__m128i *)flat_op[1]);
    p1 = _mm_load_si128((__m128i *)flat2_op[1]);
    work_a = _mm_andnot_si128(flat2, work_a);
    p1 = _mm_and_si128(flat2, p1);
    p1 = _mm_or_si128(work_a, p1);
    _mm_storeu_si128((__m128i *)(s - 2 * p), p1);

    work_a = _mm_load_si128((__m128i *)flat_op[0]);
    p0 = _mm_load_si128((__m128i *)flat2_op[0]);
    work_a = _mm_andnot_si128(flat2, work_a);
    p0 = _mm_and_si128(flat2, p0);
    p0 = _mm_or_si128(work_a, p0);
    _mm_storeu_si128((__m128i *)(s - 1 * p), p0);

    work_a = _mm_load_si128((__m128i *)flat_oq[0]);
    q0 = _mm_load_si128((__m128i *)flat2_oq[0]);
    work_a = _mm_andnot_si128(flat2, work_a);
    q0 = _mm_and_si128(flat2, q0);
    q0 = _mm_or_si128(work_a, q0);
    _mm_storeu_si128((__m128i *)(s - 0 * p), q0);

    work_a = _mm_load_si128((__m128i *)flat_oq[1]);
    q1 = _mm_load_si128((__m128i *)flat2_oq[1]);
    work_a = _mm_andnot_si128(flat2, work_a);
    q1 = _mm_and_si128(flat2, q1);
    q1 = _mm_or_si128(work_a, q1);
    _mm_storeu_si128((__m128i *)(s + 1 * p), q1);

    work_a = _mm_load_si128((__m128i *)flat_oq[2]);
    q2 = _mm_load_si128((__m128i *)flat2_oq[2]);
    work_a = _mm_andnot_si128(flat2, work_a);
    q2 = _mm_and_si128(flat2, q2);
    q2 = _mm_or_si128(work_a, q2);
    _mm_storeu_si128((__m128i *)(s + 2 * p), q2);

    // write out oq3 - oq7
    {
      unsigned char *dst = (s + 3 * p);
      for (i = 3; i < 7; i++) {
        __m128i flat2_output;
        work_a = _mm_load_si128((__m128i *)aq[i]);
        flat2_output = _mm_load_si128((__m128i *)flat2_oq[i]);
        work_a = _mm_andnot_si128(flat2, work_a);
        flat2_output = _mm_and_si128(flat2, flat2_output);
        work_a = _mm_or_si128(work_a, flat2_output);
        _mm_storeu_si128((__m128i *)dst, work_a);
        dst += p;
      }
    }
  }
}

void vp9_mb_lpf_horizontal_edge_w_sse2(unsigned char *s,
                                       int p,
                                       const unsigned char *_blimit,
                                       const unsigned char *_limit,
                                       const unsigned char *_thresh,
                                       int count) {
  if (count == 1)
    vp9_mb_lpf_horizontal_edge_w_sse2_8(s, p, _blimit, _limit, _thresh);
  else
    vp9_mb_lpf_horizontal_edge_w_sse2_16(s, p, _blimit, _limit, _thresh);
}

970
971
972
973
void vp9_mbloop_filter_horizontal_edge_sse2(unsigned char *s,
                                            int p,
                                            const unsigned char *_blimit,
                                            const unsigned char *_limit,
974
975
                                            const unsigned char *_thresh,
                                            int count) {
976
977
978
979
980
981
982
983
  DECLARE_ALIGNED(16, unsigned char, flat_op2[16]);
  DECLARE_ALIGNED(16, unsigned char, flat_op1[16]);
  DECLARE_ALIGNED(16, unsigned char, flat_op0[16]);
  DECLARE_ALIGNED(16, unsigned char, flat_oq2[16]);
  DECLARE_ALIGNED(16, unsigned char, flat_oq1[16]);
  DECLARE_ALIGNED(16, unsigned char, flat_oq0[16]);
  __m128i mask, hev, flat;
  const __m128i zero = _mm_set1_epi16(0);
984
  __m128i p3, p2, p1, p0, q0, q1, q2, q3;
985
986
987
988
989
990
991
992
993
  const unsigned int extended_thresh = _thresh[0] * 0x01010101u;
  const unsigned int extended_limit  = _limit[0]  * 0x01010101u;
  const unsigned int extended_blimit = _blimit[0] * 0x01010101u;
  const __m128i thresh =
      _mm_shuffle_epi32(_mm_cvtsi32_si128((int)extended_thresh), 0);
  const __m128i limit =
      _mm_shuffle_epi32(_mm_cvtsi32_si128((int)extended_limit), 0);
  const __m128i blimit =
      _mm_shuffle_epi32(_mm_cvtsi32_si128((int)extended_blimit), 0);
994

995
996
997
998
999
1000
  (void)count;
  p3 = _mm_loadl_epi64((__m128i *)(s - 4 * p));
  p2 = _mm_loadl_epi64((__m128i *)(s - 3 * p));
  p1 = _mm_loadl_epi64((__m128i *)(s - 2 * p));
  p0 = _mm_loadl_epi64((__m128i *)(s - 1 * p));
  q0 = _mm_loadl_epi64((__m128i *)(s - 0 * p));
For faster browsing, not all history is shown. View entire blame