blend_mask6_sse4.c 38.4 KB
Newer Older
Geza Lore's avatar
Geza Lore committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
/*
 *  Copyright (c) 2016 The WebM project authors. All Rights Reserved.
 *
 *  Use of this source code is governed by a BSD-style license
 *  that can be found in the LICENSE file in the root of the source
 *  tree. An additional intellectual property rights grant can be found
 *  in the file PATENTS.  All contributing project authors may
 *  be found in the AUTHORS file in the root of the source tree.
 */

#include <smmintrin.h>  // SSE4.1

#include <assert.h>

#include "vpx/vpx_integer.h"
#include "vpx_ports/mem.h"
#include "vpx_dsp/vpx_dsp_common.h"

19
20
#include "vpx_dsp/x86/synonyms.h"

Geza Lore's avatar
Geza Lore committed
21
22
23
24
25
26
27
28
29
30
#include "./vpx_dsp_rtcd.h"

#define MASK_BITS 6

//////////////////////////////////////////////////////////////////////////////
// Common kernels
//////////////////////////////////////////////////////////////////////////////

static INLINE __m128i blend_4(uint8_t*src0, uint8_t *src1,
                              const __m128i v_m0_w, const __m128i v_m1_w) {
31
32
  const __m128i v_s0_b = xx_loadl_32(src0);
  const __m128i v_s1_b = xx_loadl_32(src1);
Geza Lore's avatar
Geza Lore committed
33
34
35
36
37
38
39
40
  const __m128i v_s0_w = _mm_cvtepu8_epi16(v_s0_b);
  const __m128i v_s1_w = _mm_cvtepu8_epi16(v_s1_b);

  const __m128i v_p0_w = _mm_mullo_epi16(v_s0_w, v_m0_w);
  const __m128i v_p1_w = _mm_mullo_epi16(v_s1_w, v_m1_w);

  const __m128i v_sum_w = _mm_add_epi16(v_p0_w, v_p1_w);

41
  const __m128i v_res_w = xx_roundn_epu16(v_sum_w, MASK_BITS);
Geza Lore's avatar
Geza Lore committed
42
43
44
45
46
47

  return v_res_w;
}

static INLINE __m128i blend_8(uint8_t*src0, uint8_t *src1,
                              const __m128i v_m0_w, const __m128i v_m1_w) {
48
49
  const __m128i v_s0_b = xx_loadl_64(src0);
  const __m128i v_s1_b = xx_loadl_64(src1);
Geza Lore's avatar
Geza Lore committed
50
51
52
53
54
55
56
57
  const __m128i v_s0_w = _mm_cvtepu8_epi16(v_s0_b);
  const __m128i v_s1_w = _mm_cvtepu8_epi16(v_s1_b);

  const __m128i v_p0_w = _mm_mullo_epi16(v_s0_w, v_m0_w);
  const __m128i v_p1_w = _mm_mullo_epi16(v_s1_w, v_m1_w);

  const __m128i v_sum_w = _mm_add_epi16(v_p0_w, v_p1_w);

58
  const __m128i v_res_w = xx_roundn_epu16(v_sum_w, MASK_BITS);
Geza Lore's avatar
Geza Lore committed
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77

  return v_res_w;
}

//////////////////////////////////////////////////////////////////////////////
// No sub-sampling
//////////////////////////////////////////////////////////////////////////////

static void blend_mask6_w4_sse4_1(
    uint8_t *dst, uint32_t dst_stride,
    uint8_t *src0, uint32_t src0_stride,
    uint8_t *src1, uint32_t src1_stride,
    const uint8_t *mask, uint32_t mask_stride,
    int h, int w) {
  const __m128i v_maxval_w = _mm_set1_epi16(1 << MASK_BITS);

  (void)w;

  do {
78
    const __m128i v_m0_b = xx_loadl_32(mask);
Geza Lore's avatar
Geza Lore committed
79
80
81
82
83
84
85
    const __m128i v_m0_w = _mm_cvtepu8_epi16(v_m0_b);
    const __m128i v_m1_w = _mm_sub_epi16(v_maxval_w, v_m0_w);

    const __m128i v_res_w = blend_4(src0, src1, v_m0_w, v_m1_w);

    const __m128i v_res_b = _mm_packus_epi16(v_res_w, v_res_w);

86
    xx_storel_32(dst, v_res_b);
Geza Lore's avatar
Geza Lore committed
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105

    dst += dst_stride;
    src0 += src0_stride;
    src1 += src1_stride;
    mask += mask_stride;
  } while (--h);
}

static void blend_mask6_w8_sse4_1(
    uint8_t *dst, uint32_t dst_stride,
    uint8_t *src0, uint32_t src0_stride,
    uint8_t *src1, uint32_t src1_stride,
    const uint8_t *mask, uint32_t mask_stride,
    int h, int w) {
  const __m128i v_maxval_w = _mm_set1_epi16(1 << MASK_BITS);

  (void)w;

  do {
106
    const __m128i v_m0_b = xx_loadl_64(mask);
Geza Lore's avatar
Geza Lore committed
107
108
109
110
111
112
113
    const __m128i v_m0_w = _mm_cvtepu8_epi16(v_m0_b);
    const __m128i v_m1_w = _mm_sub_epi16(v_maxval_w, v_m0_w);

    const __m128i v_res_w = blend_8(src0, src1, v_m0_w, v_m1_w);

    const __m128i v_res_b = _mm_packus_epi16(v_res_w, v_res_w);

114
    xx_storel_64(dst, v_res_b);
Geza Lore's avatar
Geza Lore committed
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133

    dst += dst_stride;
    src0 += src0_stride;
    src1 += src1_stride;
    mask += mask_stride;
  } while (--h);
}

static void blend_mask6_w16n_sse4_1(
    uint8_t *dst, uint32_t dst_stride,
    uint8_t *src0, uint32_t src0_stride,
    uint8_t *src1, uint32_t src1_stride,
    const uint8_t *mask, uint32_t mask_stride,
    int h, int w) {
  const __m128i v_maxval_w = _mm_set1_epi16(1 << MASK_BITS);

  do {
    int c;
    for (c = 0; c < w; c += 16) {
134
135
      const __m128i v_m0l_b = xx_loadl_64(mask + c);
      const __m128i v_m0h_b = xx_loadl_64(mask + c + 8);
Geza Lore's avatar
Geza Lore committed
136
137
138
139
140
141
142
143
144
145
146
147
      const __m128i v_m0l_w = _mm_cvtepu8_epi16(v_m0l_b);
      const __m128i v_m0h_w = _mm_cvtepu8_epi16(v_m0h_b);
      const __m128i v_m1l_w = _mm_sub_epi16(v_maxval_w, v_m0l_w);
      const __m128i v_m1h_w = _mm_sub_epi16(v_maxval_w, v_m0h_w);

      const __m128i v_resl_w = blend_8(src0 + c, src1 + c,
                                       v_m0l_w, v_m1l_w);
      const __m128i v_resh_w = blend_8(src0 + c + 8, src1 + c + 8,
                                       v_m0h_w, v_m1h_w);

      const __m128i v_res_b = _mm_packus_epi16(v_resl_w, v_resh_w);

148
      xx_storeu_128(dst + c, v_res_b);
Geza Lore's avatar
Geza Lore committed
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
    }
    dst += dst_stride;
    src0 += src0_stride;
    src1 += src1_stride;
    mask += mask_stride;
  } while (--h);
}

//////////////////////////////////////////////////////////////////////////////
// Horizontal sub-sampling
//////////////////////////////////////////////////////////////////////////////

static void blend_mask6_sx_w4_sse4_1(
    uint8_t *dst, uint32_t dst_stride,
    uint8_t *src0, uint32_t src0_stride,
    uint8_t *src1, uint32_t src1_stride,
    const uint8_t *mask, uint32_t mask_stride,
    int h, int w) {
  const __m128i v_zmask_b = _mm_set_epi8(0, 0xff, 0, 0xff, 0, 0xff, 0, 0xff,
                                         0, 0xff, 0, 0xff, 0, 0xff, 0, 0xff);
  const __m128i v_maxval_w = _mm_set1_epi16(1 << MASK_BITS);

  (void)w;

  do {
174
    const __m128i v_r_b = xx_loadl_64(mask);
Geza Lore's avatar
Geza Lore committed
175
176
177
178
179
180
181
182
183
    const __m128i v_a_b = _mm_avg_epu8(v_r_b, _mm_srli_si128(v_r_b, 1));

    const __m128i v_m0_w = _mm_and_si128(v_a_b, v_zmask_b);
    const __m128i v_m1_w = _mm_sub_epi16(v_maxval_w, v_m0_w);

    const __m128i v_res_w = blend_4(src0, src1, v_m0_w, v_m1_w);

    const __m128i v_res_b = _mm_packus_epi16(v_res_w, v_res_w);

184
    xx_storel_32(dst, v_res_b);
Geza Lore's avatar
Geza Lore committed
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205

    dst += dst_stride;
    src0 += src0_stride;
    src1 += src1_stride;
    mask += mask_stride;
  } while (--h);
}

static void blend_mask6_sx_w8_sse4_1(
    uint8_t *dst, uint32_t dst_stride,
    uint8_t *src0, uint32_t src0_stride,
    uint8_t *src1, uint32_t src1_stride,
    const uint8_t *mask, uint32_t mask_stride,
    int h, int w) {
  const __m128i v_zmask_b = _mm_set_epi8(0, 0xff, 0, 0xff, 0, 0xff, 0, 0xff,
                                         0, 0xff, 0, 0xff, 0, 0xff, 0, 0xff);
  const __m128i v_maxval_w = _mm_set1_epi16(1 << MASK_BITS);

  (void)w;

  do {
206
    const __m128i v_r_b = xx_loadu_128(mask);
Geza Lore's avatar
Geza Lore committed
207
208
209
210
211
212
213
214
215
    const __m128i v_a_b = _mm_avg_epu8(v_r_b, _mm_srli_si128(v_r_b, 1));

    const __m128i v_m0_w = _mm_and_si128(v_a_b, v_zmask_b);
    const __m128i v_m1_w = _mm_sub_epi16(v_maxval_w, v_m0_w);

    const __m128i v_res_w = blend_8(src0, src1, v_m0_w, v_m1_w);

    const __m128i v_res_b = _mm_packus_epi16(v_res_w, v_res_w);

216
    xx_storel_64(dst, v_res_b);
Geza Lore's avatar
Geza Lore committed
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237

    dst += dst_stride;
    src0 += src0_stride;
    src1 += src1_stride;
    mask += mask_stride;
  } while (--h);
}

static void blend_mask6_sx_w16n_sse4_1(
    uint8_t *dst, uint32_t dst_stride,
    uint8_t *src0, uint32_t src0_stride,
    uint8_t *src1, uint32_t src1_stride,
    const uint8_t *mask, uint32_t mask_stride,
    int h, int w) {
  const __m128i v_zmask_b = _mm_set_epi8(0, 0xff, 0, 0xff, 0, 0xff, 0, 0xff,
                                         0, 0xff, 0, 0xff, 0, 0xff, 0, 0xff);
  const __m128i v_maxval_w = _mm_set1_epi16(1 << MASK_BITS);

  do {
    int c;
    for (c = 0; c < w; c += 16) {
238
239
      const __m128i v_rl_b = xx_loadu_128(mask + 2 * c);
      const __m128i v_rh_b = xx_loadu_128(mask + 2 * c + 16);
Geza Lore's avatar
Geza Lore committed
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
      const __m128i v_al_b = _mm_avg_epu8(v_rl_b, _mm_srli_si128(v_rl_b, 1));
      const __m128i v_ah_b = _mm_avg_epu8(v_rh_b, _mm_srli_si128(v_rh_b, 1));

      const __m128i v_m0l_w = _mm_and_si128(v_al_b, v_zmask_b);
      const __m128i v_m0h_w = _mm_and_si128(v_ah_b, v_zmask_b);
      const __m128i v_m1l_w = _mm_sub_epi16(v_maxval_w, v_m0l_w);
      const __m128i v_m1h_w = _mm_sub_epi16(v_maxval_w, v_m0h_w);

      const __m128i v_resl_w = blend_8(src0 + c, src1 + c,
                                       v_m0l_w, v_m1l_w);
      const __m128i v_resh_w = blend_8(src0 + c + 8, src1 + c + 8,
                                       v_m0h_w, v_m1h_w);

      const __m128i v_res_b = _mm_packus_epi16(v_resl_w, v_resh_w);

255
      xx_storeu_128(dst + c, v_res_b);
Geza Lore's avatar
Geza Lore committed
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
    }
    dst += dst_stride;
    src0 += src0_stride;
    src1 += src1_stride;
    mask += mask_stride;
  } while (--h);
}

//////////////////////////////////////////////////////////////////////////////
// Vertical sub-sampling
//////////////////////////////////////////////////////////////////////////////

static void blend_mask6_sy_w4_sse4_1(
    uint8_t *dst, uint32_t dst_stride,
    uint8_t *src0, uint32_t src0_stride,
    uint8_t *src1, uint32_t src1_stride,
    const uint8_t *mask, uint32_t mask_stride,
    int h, int w) {
  const __m128i v_maxval_w = _mm_set1_epi16(1 << MASK_BITS);

  (void)w;

  do {
279
280
    const __m128i v_ra_b = xx_loadl_32(mask);
    const __m128i v_rb_b = xx_loadl_32(mask + mask_stride);
Geza Lore's avatar
Geza Lore committed
281
282
283
284
285
286
287
288
289
    const __m128i v_a_b = _mm_avg_epu8(v_ra_b, v_rb_b);

    const __m128i v_m0_w = _mm_cvtepu8_epi16(v_a_b);
    const __m128i v_m1_w = _mm_sub_epi16(v_maxval_w, v_m0_w);

    const __m128i v_res_w = blend_4(src0, src1, v_m0_w, v_m1_w);

    const __m128i v_res_b = _mm_packus_epi16(v_res_w, v_res_w);

290
    xx_storel_32(dst, v_res_b);
Geza Lore's avatar
Geza Lore committed
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309

    dst += dst_stride;
    src0 += src0_stride;
    src1 += src1_stride;
    mask += 2 * mask_stride;
  } while (--h);
}

static void blend_mask6_sy_w8_sse4_1(
    uint8_t *dst, uint32_t dst_stride,
    uint8_t *src0, uint32_t src0_stride,
    uint8_t *src1, uint32_t src1_stride,
    const uint8_t *mask, uint32_t mask_stride,
    int h, int w) {
  const __m128i v_maxval_w = _mm_set1_epi16(1 << MASK_BITS);

  (void)w;

  do {
310
311
    const __m128i v_ra_b = xx_loadl_64(mask);
    const __m128i v_rb_b = xx_loadl_64(mask + mask_stride);
Geza Lore's avatar
Geza Lore committed
312
313
314
315
316
317
318
319
320
    const __m128i v_a_b = _mm_avg_epu8(v_ra_b, v_rb_b);

    const __m128i v_m0_w = _mm_cvtepu8_epi16(v_a_b);
    const __m128i v_m1_w = _mm_sub_epi16(v_maxval_w, v_m0_w);

    const __m128i v_res_w = blend_8(src0, src1, v_m0_w, v_m1_w);

    const __m128i v_res_b = _mm_packus_epi16(v_res_w, v_res_w);

321
    xx_storel_64(dst, v_res_b);
Geza Lore's avatar
Geza Lore committed
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341

    dst += dst_stride;
    src0 += src0_stride;
    src1 += src1_stride;
    mask += 2 * mask_stride;
  } while (--h);
}

static void blend_mask6_sy_w16n_sse4_1(
    uint8_t *dst, uint32_t dst_stride,
    uint8_t *src0, uint32_t src0_stride,
    uint8_t *src1, uint32_t src1_stride,
    const uint8_t *mask, uint32_t mask_stride,
    int h, int w) {
  const __m128i v_zero = _mm_setzero_si128();
  const __m128i v_maxval_w = _mm_set1_epi16(1 << MASK_BITS);

  do {
    int c;
    for (c = 0; c < w; c += 16) {
342
343
      const __m128i v_ra_b = xx_loadu_128(mask + c);
      const __m128i v_rb_b = xx_loadu_128(mask + c + mask_stride);
Geza Lore's avatar
Geza Lore committed
344
345
346
347
348
349
350
351
352
353
354
355
356
357
      const __m128i v_a_b = _mm_avg_epu8(v_ra_b, v_rb_b);

      const __m128i v_m0l_w = _mm_cvtepu8_epi16(v_a_b);
      const __m128i v_m0h_w = _mm_unpackhi_epi8(v_a_b, v_zero);
      const __m128i v_m1l_w = _mm_sub_epi16(v_maxval_w, v_m0l_w);
      const __m128i v_m1h_w = _mm_sub_epi16(v_maxval_w, v_m0h_w);

      const __m128i v_resl_w = blend_8(src0 + c, src1 + c,
                                       v_m0l_w, v_m1l_w);
      const __m128i v_resh_w = blend_8(src0 + c + 8, src1 + c + 8,
                                       v_m0h_w, v_m1h_w);

      const __m128i v_res_b = _mm_packus_epi16(v_resl_w, v_resh_w);

358
      xx_storeu_128(dst + c, v_res_b);
Geza Lore's avatar
Geza Lore committed
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
    }
    dst += dst_stride;
    src0 += src0_stride;
    src1 += src1_stride;
    mask += 2 * mask_stride;
  } while (--h);
}

//////////////////////////////////////////////////////////////////////////////
// Horizontal and Vertical sub-sampling
//////////////////////////////////////////////////////////////////////////////

static void blend_mask6_sx_sy_w4_sse4_1(
    uint8_t *dst, uint32_t dst_stride,
    uint8_t *src0, uint32_t src0_stride,
    uint8_t *src1, uint32_t src1_stride,
    const uint8_t *mask, uint32_t mask_stride,
    int h, int w) {
  const __m128i v_zmask_b = _mm_set_epi8(0, 0xff, 0, 0xff, 0, 0xff, 0, 0xff,
                                         0, 0xff, 0, 0xff, 0, 0xff, 0, 0xff);
  const __m128i v_maxval_w = _mm_set1_epi16(1 << MASK_BITS);

  (void)w;

  do {
384
385
    const __m128i v_ra_b = xx_loadl_64(mask);
    const __m128i v_rb_b = xx_loadl_64(mask + mask_stride);
Geza Lore's avatar
Geza Lore committed
386
387
388
389
390
391
    const __m128i v_rvs_b = _mm_add_epi8(v_ra_b, v_rb_b);
    const __m128i v_rvsa_w = _mm_and_si128(v_rvs_b, v_zmask_b);
    const __m128i v_rvsb_w = _mm_and_si128(_mm_srli_si128(v_rvs_b, 1),
                                           v_zmask_b);
    const __m128i v_rs_w = _mm_add_epi16(v_rvsa_w, v_rvsb_w);

392
    const __m128i v_m0_w = xx_roundn_epu16(v_rs_w, 2);
Geza Lore's avatar
Geza Lore committed
393
394
395
396
397
398
    const __m128i v_m1_w = _mm_sub_epi16(v_maxval_w, v_m0_w);

    const __m128i v_res_w = blend_4(src0, src1, v_m0_w, v_m1_w);

    const __m128i v_res_b = _mm_packus_epi16(v_res_w, v_res_w);

399
    xx_storel_32(dst, v_res_b);
Geza Lore's avatar
Geza Lore committed
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420

    dst += dst_stride;
    src0 += src0_stride;
    src1 += src1_stride;
    mask += 2 * mask_stride;
  } while (--h);
}

static void blend_mask6_sx_sy_w8_sse4_1(
    uint8_t *dst, uint32_t dst_stride,
    uint8_t *src0, uint32_t src0_stride,
    uint8_t *src1, uint32_t src1_stride,
    const uint8_t *mask, uint32_t mask_stride,
    int h, int w) {
  const __m128i v_zmask_b = _mm_set_epi8(0, 0xff, 0, 0xff, 0, 0xff, 0, 0xff,
                                         0, 0xff, 0, 0xff, 0, 0xff, 0, 0xff);
  const __m128i v_maxval_w = _mm_set1_epi16(1 << MASK_BITS);

  (void)w;

  do {
421
422
    const __m128i v_ra_b = xx_loadu_128(mask);
    const __m128i v_rb_b = xx_loadu_128(mask + mask_stride);
Geza Lore's avatar
Geza Lore committed
423
424
425
426
427
428
    const __m128i v_rvs_b = _mm_add_epi8(v_ra_b, v_rb_b);
    const __m128i v_rvsa_w = _mm_and_si128(v_rvs_b, v_zmask_b);
    const __m128i v_rvsb_w = _mm_and_si128(_mm_srli_si128(v_rvs_b, 1),
                                           v_zmask_b);
    const __m128i v_rs_w = _mm_add_epi16(v_rvsa_w, v_rvsb_w);

429
    const __m128i v_m0_w = xx_roundn_epu16(v_rs_w, 2);
Geza Lore's avatar
Geza Lore committed
430
431
432
433
434
435
    const __m128i v_m1_w = _mm_sub_epi16(v_maxval_w, v_m0_w);

    const __m128i v_res_w = blend_8(src0, src1, v_m0_w, v_m1_w);

    const __m128i v_res_b = _mm_packus_epi16(v_res_w, v_res_w);

436
    xx_storel_64(dst, v_res_b);
Geza Lore's avatar
Geza Lore committed
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457

    dst += dst_stride;
    src0 += src0_stride;
    src1 += src1_stride;
    mask += 2 * mask_stride;
  } while (--h);
}

static void blend_mask6_sx_sy_w16n_sse4_1(
    uint8_t *dst, uint32_t dst_stride,
    uint8_t *src0, uint32_t src0_stride,
    uint8_t *src1, uint32_t src1_stride,
    const uint8_t *mask, uint32_t mask_stride,
    int h, int w) {
  const __m128i v_zmask_b = _mm_set_epi8(0, 0xff, 0, 0xff, 0, 0xff, 0, 0xff,
                                         0, 0xff, 0, 0xff, 0, 0xff, 0, 0xff);
  const __m128i v_maxval_w = _mm_set1_epi16(1 << MASK_BITS);

  do {
    int c;
    for (c = 0; c < w; c += 16) {
458
459
460
461
      const __m128i v_ral_b = xx_loadu_128(mask + 2 * c);
      const __m128i v_rah_b = xx_loadu_128(mask + 2 * c + 16);
      const __m128i v_rbl_b = xx_loadu_128(mask + mask_stride + 2 * c);
      const __m128i v_rbh_b = xx_loadu_128(mask + mask_stride + 2 * c + 16);
Geza Lore's avatar
Geza Lore committed
462
463
464
465
466
467
468
469
470
471
472
      const __m128i v_rvsl_b = _mm_add_epi8(v_ral_b, v_rbl_b);
      const __m128i v_rvsh_b = _mm_add_epi8(v_rah_b, v_rbh_b);
      const __m128i v_rvsal_w = _mm_and_si128(v_rvsl_b, v_zmask_b);
      const __m128i v_rvsah_w = _mm_and_si128(v_rvsh_b, v_zmask_b);
      const __m128i v_rvsbl_w = _mm_and_si128(_mm_srli_si128(v_rvsl_b, 1),
                                              v_zmask_b);
      const __m128i v_rvsbh_w = _mm_and_si128(_mm_srli_si128(v_rvsh_b, 1),
                                              v_zmask_b);
      const __m128i v_rsl_w = _mm_add_epi16(v_rvsal_w, v_rvsbl_w);
      const __m128i v_rsh_w = _mm_add_epi16(v_rvsah_w, v_rvsbh_w);

473
474
      const __m128i v_m0l_w = xx_roundn_epu16(v_rsl_w, 2);
      const __m128i v_m0h_w = xx_roundn_epu16(v_rsh_w, 2);
Geza Lore's avatar
Geza Lore committed
475
476
477
478
479
480
481
482
483
484
      const __m128i v_m1l_w = _mm_sub_epi16(v_maxval_w, v_m0l_w);
      const __m128i v_m1h_w = _mm_sub_epi16(v_maxval_w, v_m0h_w);

      const __m128i v_resl_w = blend_8(src0 + c, src1 + c,
                                       v_m0l_w, v_m1l_w);
      const __m128i v_resh_w = blend_8(src0 + c + 8, src1 + c + 8,
                                       v_m0h_w, v_m1h_w);

      const __m128i v_res_b = _mm_packus_epi16(v_resl_w, v_resh_w);

485
      xx_storeu_128(dst + c, v_res_b);
Geza Lore's avatar
Geza Lore committed
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
    }
    dst += dst_stride;
    src0 += src0_stride;
    src1 += src1_stride;
    mask += 2 * mask_stride;
  } while (--h);
}

//////////////////////////////////////////////////////////////////////////////
// Dispatch
//////////////////////////////////////////////////////////////////////////////

void vpx_blend_mask6_sse4_1(uint8_t *dst, uint32_t dst_stride,
                            uint8_t *src0, uint32_t src0_stride,
                            uint8_t *src1, uint32_t src1_stride,
                            const uint8_t *mask, uint32_t mask_stride,
                            int h, int w, int suby, int subx) {
  typedef  void (*blend_fn)(uint8_t *dst, uint32_t dst_stride,
                            uint8_t *src0, uint32_t src0_stride,
                            uint8_t *src1, uint32_t src1_stride,
                            const uint8_t *mask, uint32_t mask_stride,
                            int h, int w);

  static blend_fn blend[3][2][2] = {  // width_index X subx X suby
    {     // w % 16 == 0
      {blend_mask6_w16n_sse4_1, blend_mask6_sy_w16n_sse4_1},
      {blend_mask6_sx_w16n_sse4_1, blend_mask6_sx_sy_w16n_sse4_1}
    }, {  // w == 4
      {blend_mask6_w4_sse4_1, blend_mask6_sy_w4_sse4_1},
      {blend_mask6_sx_w4_sse4_1, blend_mask6_sx_sy_w4_sse4_1}
    }, {  // w == 8
      {blend_mask6_w8_sse4_1, blend_mask6_sy_w8_sse4_1},
      {blend_mask6_sx_w8_sse4_1, blend_mask6_sx_sy_w8_sse4_1}
    }
  };

  assert(IMPLIES(src0 == dst, src0_stride == dst_stride));
  assert(IMPLIES(src1 == dst, src1_stride == dst_stride));

  assert(h >= 4);
  assert(w >= 4);
  assert(IS_POWER_OF_TWO(h));
  assert(IS_POWER_OF_TWO(w));

  blend[(w >> 2) & 3][subx != 0][suby != 0](dst, dst_stride,
                                            src0, src0_stride,
                                            src1, src1_stride,
                                            mask, mask_stride,
                                            h, w);
}

#if CONFIG_VP9_HIGHBITDEPTH
//////////////////////////////////////////////////////////////////////////////
// Common kernels
//////////////////////////////////////////////////////////////////////////////

typedef __m128i (*blend_unit_fn)(uint16_t*src0, uint16_t *src1,
                                 const __m128i v_m0_w, const __m128i v_m1_w);

static INLINE __m128i blend_4_b10(uint16_t*src0, uint16_t *src1,
                                  const __m128i v_m0_w, const __m128i v_m1_w) {
547
548
  const __m128i v_s0_w = xx_loadl_64(src0);
  const __m128i v_s1_w = xx_loadl_64(src1);
Geza Lore's avatar
Geza Lore committed
549
550
551
552
553
554

  const __m128i v_p0_w = _mm_mullo_epi16(v_s0_w, v_m0_w);
  const __m128i v_p1_w = _mm_mullo_epi16(v_s1_w, v_m1_w);

  const __m128i v_sum_w = _mm_add_epi16(v_p0_w, v_p1_w);

555
  const __m128i v_res_w = xx_roundn_epu16(v_sum_w, MASK_BITS);
Geza Lore's avatar
Geza Lore committed
556
557
558
559
560
561

  return v_res_w;
}

static INLINE __m128i blend_8_b10(uint16_t*src0, uint16_t *src1,
                                  const __m128i v_m0_w, const __m128i v_m1_w) {
562
563
  const __m128i v_s0_w = xx_loadu_128(src0);
  const __m128i v_s1_w = xx_loadu_128(src1);
Geza Lore's avatar
Geza Lore committed
564
565
566
567
568
569

  const __m128i v_p0_w = _mm_mullo_epi16(v_s0_w, v_m0_w);
  const __m128i v_p1_w = _mm_mullo_epi16(v_s1_w, v_m1_w);

  const __m128i v_sum_w = _mm_add_epi16(v_p0_w, v_p1_w);

570
  const __m128i v_res_w = xx_roundn_epu16(v_sum_w, MASK_BITS);
Geza Lore's avatar
Geza Lore committed
571
572
573
574
575
576

  return v_res_w;
}

static INLINE __m128i blend_4_b12(uint16_t*src0, uint16_t *src1,
                                  const __m128i v_m0_w, const __m128i v_m1_w) {
577
578
  const __m128i v_s0_w = xx_loadl_64(src0);
  const __m128i v_s1_w = xx_loadl_64(src1);
Geza Lore's avatar
Geza Lore committed
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593

  // Interleave
  const __m128i v_m01_w = _mm_unpacklo_epi16(v_m0_w, v_m1_w);
  const __m128i v_s01_w = _mm_unpacklo_epi16(v_s0_w, v_s1_w);

  // Multiply-Add
  const __m128i v_sum_d = _mm_madd_epi16(v_s01_w, v_m01_w);

  // Scale
  const __m128i v_ssum_d = _mm_srli_epi32(v_sum_d, MASK_BITS - 1);

  // Pack
  const __m128i v_pssum_d = _mm_packs_epi32(v_ssum_d, v_ssum_d);

  // Round
594
  const __m128i v_res_w = xx_round_epu16(v_pssum_d);
Geza Lore's avatar
Geza Lore committed
595
596
597
598
599
600

  return v_res_w;
}

static INLINE __m128i blend_8_b12(uint16_t*src0, uint16_t *src1,
                                  const __m128i v_m0_w, const __m128i v_m1_w) {
601
602
  const __m128i v_s0_w = xx_loadu_128(src0);
  const __m128i v_s1_w = xx_loadu_128(src1);
Geza Lore's avatar
Geza Lore committed
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621

  // Interleave
  const __m128i v_m01l_w = _mm_unpacklo_epi16(v_m0_w, v_m1_w);
  const __m128i v_m01h_w = _mm_unpackhi_epi16(v_m0_w, v_m1_w);
  const __m128i v_s01l_w = _mm_unpacklo_epi16(v_s0_w, v_s1_w);
  const __m128i v_s01h_w = _mm_unpackhi_epi16(v_s0_w, v_s1_w);

  // Multiply-Add
  const __m128i v_suml_d = _mm_madd_epi16(v_s01l_w, v_m01l_w);
  const __m128i v_sumh_d = _mm_madd_epi16(v_s01h_w, v_m01h_w);

  // Scale
  const __m128i v_ssuml_d = _mm_srli_epi32(v_suml_d, MASK_BITS - 1);
  const __m128i v_ssumh_d = _mm_srli_epi32(v_sumh_d, MASK_BITS - 1);

  // Pack
  const __m128i v_pssum_d = _mm_packs_epi32(v_ssuml_d, v_ssumh_d);

  // Round
622
  const __m128i v_res_w = xx_round_epu16(v_pssum_d);
Geza Lore's avatar
Geza Lore committed
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639

  return v_res_w;
}

//////////////////////////////////////////////////////////////////////////////
// No sub-sampling
//////////////////////////////////////////////////////////////////////////////

static INLINE void blend_mask6_bn_w4_sse4_1(
    uint16_t *dst, uint32_t dst_stride,
    uint16_t *src0, uint32_t src0_stride,
    uint16_t *src1, uint32_t src1_stride,
    const uint8_t *mask, uint32_t mask_stride,
    int h, blend_unit_fn blend) {
  const __m128i v_maxval_w = _mm_set1_epi16(1 << MASK_BITS);

  do {
640
    const __m128i v_m0_b = xx_loadl_32(mask);
Geza Lore's avatar
Geza Lore committed
641
642
643
644
645
    const __m128i v_m0_w = _mm_cvtepu8_epi16(v_m0_b);
    const __m128i v_m1_w = _mm_sub_epi16(v_maxval_w, v_m0_w);

    const __m128i v_res_w = blend(src0, src1, v_m0_w, v_m1_w);

646
    xx_storel_64(dst, v_res_w);
Geza Lore's avatar
Geza Lore committed
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

    dst += dst_stride;
    src0 += src0_stride;
    src1 += src1_stride;
    mask += mask_stride;
  } while (--h);
}

static void blend_mask6_b10_w4_sse4_1(
    uint16_t *dst, uint32_t dst_stride,
    uint16_t *src0, uint32_t src0_stride,
    uint16_t *src1, uint32_t src1_stride,
    const uint8_t *mask, uint32_t mask_stride,
    int h, int w) {
  (void)w;
  blend_mask6_bn_w4_sse4_1(dst, dst_stride, src0, src0_stride, src1,
                           src1_stride, mask, mask_stride, h,
                           blend_4_b10);
}

static void blend_mask6_b12_w4_sse4_1(
    uint16_t *dst, uint32_t dst_stride,
    uint16_t *src0, uint32_t src0_stride,
    uint16_t *src1, uint32_t src1_stride,
    const uint8_t *mask, uint32_t mask_stride,
    int h, int w) {
  (void)w;
  blend_mask6_bn_w4_sse4_1(dst, dst_stride, src0, src0_stride, src1,
                           src1_stride, mask, mask_stride, h,
                           blend_4_b12);
}

static inline void blend_mask6_bn_w8n_sse4_1(
    uint16_t *dst, uint32_t dst_stride,
    uint16_t *src0, uint32_t src0_stride,
    uint16_t *src1, uint32_t src1_stride,
    const uint8_t *mask, uint32_t mask_stride,
    int h, int w, blend_unit_fn blend) {
  const __m128i v_maxval_w = _mm_set1_epi16(1 << MASK_BITS);

  do {
    int c;
    for (c = 0; c < w; c += 8) {
690
      const __m128i v_m0_b = xx_loadl_64(mask + c);
Geza Lore's avatar
Geza Lore committed
691
692
693
694
695
      const __m128i v_m0_w = _mm_cvtepu8_epi16(v_m0_b);
      const __m128i v_m1_w = _mm_sub_epi16(v_maxval_w, v_m0_w);

      const __m128i v_res_w = blend(src0 + c, src1 + c, v_m0_w, v_m1_w);

696
      xx_storeu_128(dst + c, v_res_w);
Geza Lore's avatar
Geza Lore committed
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
    }
    dst += dst_stride;
    src0 += src0_stride;
    src1 += src1_stride;
    mask += mask_stride;
  } while (--h);
}

static void blend_mask6_b10_w8n_sse4_1(
    uint16_t *dst, uint32_t dst_stride,
    uint16_t *src0, uint32_t src0_stride,
    uint16_t *src1, uint32_t src1_stride,
    const uint8_t *mask, uint32_t mask_stride,
    int h, int w) {
  blend_mask6_bn_w8n_sse4_1(dst, dst_stride, src0, src0_stride, src1,
                            src1_stride, mask, mask_stride, h, w,
                            blend_8_b10);
}

static void blend_mask6_b12_w8n_sse4_1(
    uint16_t *dst, uint32_t dst_stride,
    uint16_t *src0, uint32_t src0_stride,
    uint16_t *src1, uint32_t src1_stride,
    const uint8_t *mask, uint32_t mask_stride,
    int h, int w) {
  blend_mask6_bn_w8n_sse4_1(dst, dst_stride, src0, src0_stride, src1,
                            src1_stride, mask, mask_stride, h, w,
                            blend_8_b12);
}

//////////////////////////////////////////////////////////////////////////////
// Horizontal sub-sampling
//////////////////////////////////////////////////////////////////////////////

static INLINE void blend_mask6_bn_sx_w4_sse4_1(
    uint16_t *dst, uint32_t dst_stride,
    uint16_t *src0, uint32_t src0_stride,
    uint16_t *src1, uint32_t src1_stride,
    const uint8_t *mask, uint32_t mask_stride,
    int h, blend_unit_fn blend) {
  const __m128i v_zmask_b = _mm_set_epi8(0, 0xff, 0, 0xff, 0, 0xff, 0, 0xff,
                                         0, 0xff, 0, 0xff, 0, 0xff, 0, 0xff);
  const __m128i v_maxval_w = _mm_set1_epi16(1 << MASK_BITS);

  do {
742
    const __m128i v_r_b = xx_loadl_64(mask);
Geza Lore's avatar
Geza Lore committed
743
744
745
746
747
748
749
    const __m128i v_a_b = _mm_avg_epu8(v_r_b, _mm_srli_si128(v_r_b, 1));

    const __m128i v_m0_w = _mm_and_si128(v_a_b, v_zmask_b);
    const __m128i v_m1_w = _mm_sub_epi16(v_maxval_w, v_m0_w);

    const __m128i v_res_w = blend(src0, src1, v_m0_w, v_m1_w);

750
    xx_storel_64(dst, v_res_w);
Geza Lore's avatar
Geza Lore committed
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

    dst += dst_stride;
    src0 += src0_stride;
    src1 += src1_stride;
    mask += mask_stride;
  } while (--h);
}

static void blend_mask6_b10_sx_w4_sse4_1(
    uint16_t *dst, uint32_t dst_stride,
    uint16_t *src0, uint32_t src0_stride,
    uint16_t *src1, uint32_t src1_stride,
    const uint8_t *mask, uint32_t mask_stride,
    int h, int w) {
  (void)w;
  blend_mask6_bn_sx_w4_sse4_1(dst, dst_stride, src0, src0_stride, src1,
                              src1_stride, mask, mask_stride, h,
                              blend_4_b10);
}

static void blend_mask6_b12_sx_w4_sse4_1(
    uint16_t *dst, uint32_t dst_stride,
    uint16_t *src0, uint32_t src0_stride,
    uint16_t *src1, uint32_t src1_stride,
    const uint8_t *mask, uint32_t mask_stride,
    int h, int w) {
  (void)w;
  blend_mask6_bn_sx_w4_sse4_1(dst, dst_stride, src0,  src0_stride, src1,
                              src1_stride, mask, mask_stride, h,
                              blend_4_b12);
}

static INLINE void blend_mask6_bn_sx_w8n_sse4_1(
    uint16_t *dst, uint32_t dst_stride,
    uint16_t *src0, uint32_t src0_stride,
    uint16_t *src1, uint32_t src1_stride,
    const uint8_t *mask, uint32_t mask_stride,
    int h, int w, blend_unit_fn blend) {
  const __m128i v_zmask_b = _mm_set_epi8(0, 0xff, 0, 0xff, 0, 0xff, 0, 0xff,
                                         0, 0xff, 0, 0xff, 0, 0xff, 0, 0xff);
  const __m128i v_maxval_w = _mm_set1_epi16(1 << MASK_BITS);

  do {
    int c;
    for (c = 0; c < w; c += 8) {
796
      const __m128i v_r_b = xx_loadu_128(mask + 2 * c);
Geza Lore's avatar
Geza Lore committed
797
798
799
800
801
802
803
      const __m128i v_a_b = _mm_avg_epu8(v_r_b, _mm_srli_si128(v_r_b, 1));

      const __m128i v_m0_w = _mm_and_si128(v_a_b, v_zmask_b);
      const __m128i v_m1_w = _mm_sub_epi16(v_maxval_w, v_m0_w);

      const __m128i v_res_w = blend(src0 + c, src1 + c, v_m0_w, v_m1_w);

804
      xx_storeu_128(dst + c, v_res_w);
Geza Lore's avatar
Geza Lore committed
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
    }
    dst += dst_stride;
    src0 += src0_stride;
    src1 += src1_stride;
    mask += mask_stride;
  } while (--h);
}

static void blend_mask6_b10_sx_w8n_sse4_1(
    uint16_t *dst, uint32_t dst_stride,
    uint16_t *src0, uint32_t src0_stride,
    uint16_t *src1, uint32_t src1_stride,
    const uint8_t *mask, uint32_t mask_stride,
    int h, int w) {
  blend_mask6_bn_sx_w8n_sse4_1(dst, dst_stride, src0, src0_stride, src1,
                               src1_stride, mask, mask_stride, h, w,
                               blend_8_b10);
}

static void blend_mask6_b12_sx_w8n_sse4_1(
    uint16_t *dst, uint32_t dst_stride,
    uint16_t *src0, uint32_t src0_stride,
    uint16_t *src1, uint32_t src1_stride,
    const uint8_t *mask, uint32_t mask_stride,
    int h, int w) {
  blend_mask6_bn_sx_w8n_sse4_1(dst, dst_stride, src0, src0_stride, src1,
                               src1_stride, mask, mask_stride, h, w,
                               blend_8_b12);
}

//////////////////////////////////////////////////////////////////////////////
// Vertical sub-sampling
//////////////////////////////////////////////////////////////////////////////

static INLINE void blend_mask6_bn_sy_w4_sse4_1(
    uint16_t *dst, uint32_t dst_stride,
    uint16_t *src0, uint32_t src0_stride,
    uint16_t *src1, uint32_t src1_stride,
    const uint8_t *mask, uint32_t mask_stride,
    int h, blend_unit_fn blend) {
  const __m128i v_maxval_w = _mm_set1_epi16(1 << MASK_BITS);

  do {
848
849
    const __m128i v_ra_b = xx_loadl_32(mask);
    const __m128i v_rb_b = xx_loadl_32(mask + mask_stride);
Geza Lore's avatar
Geza Lore committed
850
851
852
853
854
855
856
    const __m128i v_a_b = _mm_avg_epu8(v_ra_b, v_rb_b);

    const __m128i v_m0_w = _mm_cvtepu8_epi16(v_a_b);
    const __m128i v_m1_w = _mm_sub_epi16(v_maxval_w, v_m0_w);

    const __m128i v_res_w = blend(src0, src1, v_m0_w, v_m1_w);

857
    xx_storel_64(dst, v_res_w);
Geza Lore's avatar
Geza Lore committed
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

    dst += dst_stride;
    src0 += src0_stride;
    src1 += src1_stride;
    mask += 2 * mask_stride;
  } while (--h);
}

static void blend_mask6_b10_sy_w4_sse4_1(
    uint16_t *dst, uint32_t dst_stride,
    uint16_t *src0, uint32_t src0_stride,
    uint16_t *src1, uint32_t src1_stride,
    const uint8_t *mask, uint32_t mask_stride,
    int h, int w) {
  (void)w;
  blend_mask6_bn_sy_w4_sse4_1(dst, dst_stride, src0, src0_stride, src1,
                              src1_stride, mask, mask_stride, h,
                              blend_4_b10);
}

static void blend_mask6_b12_sy_w4_sse4_1(
    uint16_t *dst, uint32_t dst_stride,
    uint16_t *src0, uint32_t src0_stride,
    uint16_t *src1, uint32_t src1_stride,
    const uint8_t *mask, uint32_t mask_stride,
    int h, int w) {
  (void)w;
  blend_mask6_bn_sy_w4_sse4_1(dst, dst_stride, src0, src0_stride, src1,
                              src1_stride, mask, mask_stride, h,
                              blend_4_b12);
}

static INLINE void blend_mask6_bn_sy_w8n_sse4_1(
    uint16_t *dst, uint32_t dst_stride,
    uint16_t *src0, uint32_t src0_stride,
    uint16_t *src1, uint32_t src1_stride,
    const uint8_t *mask, uint32_t mask_stride,
    int h, int w, blend_unit_fn blend) {
  const __m128i v_maxval_w = _mm_set1_epi16(1 << MASK_BITS);

  do {
    int c;
    for (c = 0; c < w; c += 8) {
901
902
      const __m128i v_ra_b = xx_loadl_64(mask + c);
      const __m128i v_rb_b = xx_loadl_64(mask + c + mask_stride);
Geza Lore's avatar
Geza Lore committed
903
904
905
906
907
908
909
      const __m128i v_a_b = _mm_avg_epu8(v_ra_b, v_rb_b);

      const __m128i v_m0_w = _mm_cvtepu8_epi16(v_a_b);
      const __m128i v_m1_w = _mm_sub_epi16(v_maxval_w, v_m0_w);

      const __m128i v_res_w = blend(src0 + c, src1 + c, v_m0_w, v_m1_w);

910
      xx_storeu_128(dst + c, v_res_w);
Geza Lore's avatar
Geza Lore committed
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
    }
    dst += dst_stride;
    src0 += src0_stride;
    src1 += src1_stride;
    mask += 2 * mask_stride;
  } while (--h);
}

static void blend_mask6_b10_sy_w8n_sse4_1(
    uint16_t *dst, uint32_t dst_stride,
    uint16_t *src0, uint32_t src0_stride,
    uint16_t *src1, uint32_t src1_stride,
    const uint8_t *mask, uint32_t mask_stride,
    int h, int w) {
  blend_mask6_bn_sy_w8n_sse4_1(dst, dst_stride, src0, src0_stride, src1,
                               src1_stride, mask, mask_stride, h, w,
                               blend_8_b10);
}

static void blend_mask6_b12_sy_w8n_sse4_1(
    uint16_t *dst, uint32_t dst_stride,
    uint16_t *src0, uint32_t src0_stride,
    uint16_t *src1, uint32_t src1_stride,
    const uint8_t *mask, uint32_t mask_stride,
    int h, int w) {
  blend_mask6_bn_sy_w8n_sse4_1(dst, dst_stride, src0, src0_stride, src1,
                               src1_stride, mask, mask_stride, h, w,
                               blend_8_b12);
}

//////////////////////////////////////////////////////////////////////////////
// Horizontal and Vertical sub-sampling
//////////////////////////////////////////////////////////////////////////////

static INLINE void blend_mask6_bn_sx_sy_w4_sse4_1(
    uint16_t *dst, uint32_t dst_stride,
    uint16_t *src0, uint32_t src0_stride,
    uint16_t *src1, uint32_t src1_stride,
    const uint8_t *mask, uint32_t mask_stride,
    int h, blend_unit_fn blend) {
  const __m128i v_zmask_b = _mm_set_epi8(0, 0xff, 0, 0xff, 0, 0xff, 0, 0xff,
                                         0, 0xff, 0, 0xff, 0, 0xff, 0, 0xff);
  const __m128i v_maxval_w = _mm_set1_epi16(1 << MASK_BITS);

  do {
956
957
    const __m128i v_ra_b = xx_loadl_64(mask);
    const __m128i v_rb_b = xx_loadl_64(mask + mask_stride);
Geza Lore's avatar
Geza Lore committed
958
959
960
961
962
963
    const __m128i v_rvs_b = _mm_add_epi8(v_ra_b, v_rb_b);
    const __m128i v_rvsa_w = _mm_and_si128(v_rvs_b, v_zmask_b);
    const __m128i v_rvsb_w = _mm_and_si128(_mm_srli_si128(v_rvs_b, 1),
                                           v_zmask_b);
    const __m128i v_rs_w = _mm_add_epi16(v_rvsa_w, v_rvsb_w);

964
    const __m128i v_m0_w = xx_roundn_epu16(v_rs_w, 2);
Geza Lore's avatar
Geza Lore committed
965
966
967
968
    const __m128i v_m1_w = _mm_sub_epi16(v_maxval_w, v_m0_w);

    const __m128i v_res_w = blend(src0, src1, v_m0_w, v_m1_w);

969
    xx_storel_64(dst, v_res_w);
Geza Lore's avatar
Geza Lore committed
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014

    dst += dst_stride;
    src0 += src0_stride;
    src1 += src1_stride;
    mask += 2 * mask_stride;
  } while (--h);
}

static void blend_mask6_b10_sx_sy_w4_sse4_1(
    uint16_t *dst, uint32_t dst_stride,
    uint16_t *src0, uint32_t src0_stride,
    uint16_t *src1, uint32_t src1_stride,
    const uint8_t *mask, uint32_t mask_stride,
    int h, int w) {
  (void)w;
  blend_mask6_bn_sx_sy_w4_sse4_1(dst, dst_stride, src0, src0_stride, src1,
                                 src1_stride, mask, mask_stride, h,
                                 blend_4_b10);
}

static void blend_mask6_b12_sx_sy_w4_sse4_1(
    uint16_t *dst, uint32_t dst_stride,
    uint16_t *src0, uint32_t src0_stride,
    uint16_t *src1, uint32_t src1_stride,
    const uint8_t *mask, uint32_t mask_stride,
    int h, int w) {
  (void)w;
  blend_mask6_bn_sx_sy_w4_sse4_1(dst, dst_stride, src0, src0_stride, src1,
                                 src1_stride, mask, mask_stride, h,
                                 blend_4_b12);
}

static INLINE void blend_mask6_bn_sx_sy_w8n_sse4_1(
    uint16_t *dst, uint32_t dst_stride,
    uint16_t *src0, uint32_t src0_stride,
    uint16_t *src1, uint32_t src1_stride,
    const uint8_t *mask, uint32_t mask_stride,
    int h, int w, blend_unit_fn blend) {
  const __m128i v_zmask_b = _mm_set_epi8(0, 0xff, 0, 0xff, 0, 0xff, 0, 0xff,
                                         0, 0xff, 0, 0xff, 0, 0xff, 0, 0xff);
  const __m128i v_maxval_w = _mm_set1_epi16(1 << MASK_BITS);

  do {
    int c;
    for (c = 0; c < w; c += 8) {
1015
1016
      const __m128i v_ra_b = xx_loadu_128(mask + 2 * c);
      const __m128i v_rb_b = xx_loadu_128(mask + 2 * c + mask_stride);
Geza Lore's avatar
Geza Lore committed
1017
1018
1019
1020
1021
1022
      const __m128i v_rvs_b = _mm_add_epi8(v_ra_b, v_rb_b);
      const __m128i v_rvsa_w = _mm_and_si128(v_rvs_b, v_zmask_b);
      const __m128i v_rvsb_w = _mm_and_si128(_mm_srli_si128(v_rvs_b, 1),
                                             v_zmask_b);
      const __m128i v_rs_w = _mm_add_epi16(v_rvsa_w, v_rvsb_w);

1023
      const __m128i v_m0_w = xx_roundn_epu16(v_rs_w, 2);
Geza Lore's avatar
Geza Lore committed
1024
1025
1026
1027
      const __m128i v_m1_w = _mm_sub_epi16(v_maxval_w, v_m0_w);

      const __m128i v_res_w = blend(src0 + c, src1 + c, v_m0_w, v_m1_w);

1028
      xx_storeu_128(dst + c, v_res_w);
Geza Lore's avatar
Geza Lore committed
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
    }
    dst += dst_stride;
    src0 += src0_stride;
    src1 += src1_stride;
    mask += 2 * mask_stride;
  } while (--h);
}

static void blend_mask6_b10_sx_sy_w8n_sse4_1(
    uint16_t *dst, uint32_t dst_stride,
    uint16_t *src0, uint32_t src0_stride,
    uint16_t *src1, uint32_t src1_stride,
    const uint8_t *mask, uint32_t mask_stride,
    int h, int w) {
  blend_mask6_bn_sx_sy_w8n_sse4_1(dst, dst_stride, src0, src0_stride, src1,
                                  src1_stride, mask, mask_stride, h, w,
                                  blend_8_b10);
}

static void blend_mask6_b12_sx_sy_w8n_sse4_1(
    uint16_t *dst, uint32_t dst_stride,
    uint16_t *src0, uint32_t src0_stride,
    uint16_t *src1, uint32_t src1_stride,
    const uint8_t *mask, uint32_t mask_stride,
    int h, int w) {
  blend_mask6_bn_sx_sy_w8n_sse4_1(dst, dst_stride, src0, src0_stride, src1,
                                  src1_stride, mask, mask_stride, h, w,
                                  blend_8_b12);
}

//////////////////////////////////////////////////////////////////////////////
// Dispatch
//////////////////////////////////////////////////////////////////////////////

void vpx_highbd_blend_mask6_sse4_1(uint8_t *dst_8, uint32_t dst_stride,
                                   uint8_t *src0_8, uint32_t src0_stride,
                                   uint8_t *src1_8, uint32_t src1_stride,
                                   const uint8_t *mask, uint32_t mask_stride,
                                   int h, int w, int suby, int subx, int bd) {
  uint16_t *const dst = CONVERT_TO_SHORTPTR(dst_8);
  uint16_t *const src0 = CONVERT_TO_SHORTPTR(src0_8);
  uint16_t *const src1 = CONVERT_TO_SHORTPTR(src1_8);

  typedef  void (*blend_fn)(uint16_t *dst, uint32_t dst_stride,
                            uint16_t *src0, uint32_t src0_stride,
                            uint16_t *src1, uint32_t src1_stride,
                            const uint8_t *mask, uint32_t mask_stride,
                            int h, int w);

  static blend_fn blend[2][2][2][2] = {  // bd_index X width_index X subx X suby
    {   // bd == 8 or 10
      {     // w % 8 == 0
        {blend_mask6_b10_w8n_sse4_1, blend_mask6_b10_sy_w8n_sse4_1},
        {blend_mask6_b10_sx_w8n_sse4_1, blend_mask6_b10_sx_sy_w8n_sse4_1}
      }, {  // w == 4
        {blend_mask6_b10_w4_sse4_1, blend_mask6_b10_sy_w4_sse4_1},
        {blend_mask6_b10_sx_w4_sse4_1, blend_mask6_b10_sx_sy_w4_sse4_1}
      }
    },
    {   // bd == 12
      {     // w % 8 == 0
        {blend_mask6_b12_w8n_sse4_1, blend_mask6_b12_sy_w8n_sse4_1},
        {blend_mask6_b12_sx_w8n_sse4_1, blend_mask6_b12_sx_sy_w8n_sse4_1}
      }, {  // w == 4
        {blend_mask6_b12_w4_sse4_1, blend_mask6_b12_sy_w4_sse4_1},
        {blend_mask6_b12_sx_w4_sse4_1, blend_mask6_b12_sx_sy_w4_sse4_1}
      }
    }
  };

  assert(IMPLIES(src0 == dst, src0_stride == dst_stride));
  assert(IMPLIES(src1 == dst, src1_stride == dst_stride));

  assert(h >= 4);
  assert(w >= 4);
  assert(IS_POWER_OF_TWO(h));
  assert(IS_POWER_OF_TWO(w));

  assert(bd == 8 || bd == 10 || bd == 12);

  blend[bd == 12][(w >> 2) & 1][subx != 0][suby != 0](dst, dst_stride,
                                                      src0, src0_stride,
                                                      src1, src1_stride,
                                                      mask, mask_stride,
                                                      h, w);
}
#endif  // CONFIG_VP9_HIGHBITDEPTH