row_gcc.cc 285 KB
Newer Older
mikhal@webrtc.org's avatar
mikhal@webrtc.org committed
1
/*
2
 *  Copyright 2011 The LibYuv Project Authors. All rights reserved.
mikhal@webrtc.org's avatar
mikhal@webrtc.org committed
3 4 5 6
 *
 *  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
7
 *  in the file PATENTS. All contributing project authors may
mikhal@webrtc.org's avatar
mikhal@webrtc.org committed
8 9 10
 *  be found in the AUTHORS file in the root of the source tree.
 */

11
#include "libyuv/row.h"
mikhal@webrtc.org's avatar
mikhal@webrtc.org committed
12

13 14
#ifdef __cplusplus
namespace libyuv {
mikhal@webrtc.org's avatar
mikhal@webrtc.org committed
15
extern "C" {
16
#endif
mikhal@webrtc.org's avatar
mikhal@webrtc.org committed
17

18
// This module is for GCC x86 and x64.
19 20
#if !defined(LIBYUV_DISABLE_X86) && \
    (defined(__x86_64__) || (defined(__i386__) && !defined(_MSC_VER)))
21

22
#if defined(HAS_ARGBTOYROW_SSSE3) || defined(HAS_ARGBGRAYROW_SSSE3)
23 24

// Constants for ARGB
25 26
static const vec8 kARGBToY = {13, 65, 33, 0, 13, 65, 33, 0,
                              13, 65, 33, 0, 13, 65, 33, 0};
27

28
// JPeg full range.
29 30
static const vec8 kARGBToYJ = {15, 75, 38, 0, 15, 75, 38, 0,
                               15, 75, 38, 0, 15, 75, 38, 0};
31 32 33
#endif  // defined(HAS_ARGBTOYROW_SSSE3) || defined(HAS_ARGBGRAYROW_SSSE3)

#if defined(HAS_ARGBTOYROW_SSSE3) || defined(HAS_I422TOARGBROW_SSSE3)
34

35 36
static const vec8 kARGBToU = {112, -74, -38, 0, 112, -74, -38, 0,
                              112, -74, -38, 0, 112, -74, -38, 0};
37

38 39
static const vec8 kARGBToUJ = {127, -84, -43, 0, 127, -84, -43, 0,
                               127, -84, -43, 0, 127, -84, -43, 0};
40

41 42
static const vec8 kARGBToV = {-18, -94, 112, 0, -18, -94, 112, 0,
                              -18, -94, 112, 0, -18, -94, 112, 0};
43

44 45
static const vec8 kARGBToVJ = {-20, -107, 127, 0, -20, -107, 127, 0,
                               -20, -107, 127, 0, -20, -107, 127, 0};
46

47
// Constants for BGRA
48 49
static const vec8 kBGRAToY = {0, 33, 65, 13, 0, 33, 65, 13,
                              0, 33, 65, 13, 0, 33, 65, 13};
50

51 52
static const vec8 kBGRAToU = {0, -38, -74, 112, 0, -38, -74, 112,
                              0, -38, -74, 112, 0, -38, -74, 112};
53

54 55
static const vec8 kBGRAToV = {0, 112, -94, -18, 0, 112, -94, -18,
                              0, 112, -94, -18, 0, 112, -94, -18};
56 57

// Constants for ABGR
58 59
static const vec8 kABGRToY = {33, 65, 13, 0, 33, 65, 13, 0,
                              33, 65, 13, 0, 33, 65, 13, 0};
60

61 62
static const vec8 kABGRToU = {-38, -74, 112, 0, -38, -74, 112, 0,
                              -38, -74, 112, 0, -38, -74, 112, 0};
63

64 65
static const vec8 kABGRToV = {112, -94, -18, 0, 112, -94, -18, 0,
                              112, -94, -18, 0, 112, -94, -18, 0};
66

67
// Constants for RGBA.
68 69
static const vec8 kRGBAToY = {0, 13, 65, 33, 0, 13, 65, 33,
                              0, 13, 65, 33, 0, 13, 65, 33};
70

71 72
static const vec8 kRGBAToU = {0, 112, -74, -38, 0, 112, -74, -38,
                              0, 112, -74, -38, 0, 112, -74, -38};
73

74 75
static const vec8 kRGBAToV = {0, -18, -94, 112, 0, -18, -94, 112,
                              0, -18, -94, 112, 0, -18, -94, 112};
76

77 78
static const uvec8 kAddY16 = {16u, 16u, 16u, 16u, 16u, 16u, 16u, 16u,
                              16u, 16u, 16u, 16u, 16u, 16u, 16u, 16u};
79

80
// 7 bit fixed point 0.5.
81
static const vec16 kAddYJ64 = {64, 64, 64, 64, 64, 64, 64, 64};
82

83 84
static const uvec8 kAddUV128 = {128u, 128u, 128u, 128u, 128u, 128u, 128u, 128u,
                                128u, 128u, 128u, 128u, 128u, 128u, 128u, 128u};
85

86 87
static const uvec16 kAddUVJ128 = {0x8080u, 0x8080u, 0x8080u, 0x8080u,
                                  0x8080u, 0x8080u, 0x8080u, 0x8080u};
88 89 90
#endif  // defined(HAS_ARGBTOYROW_SSSE3) || defined(HAS_I422TOARGBROW_SSSE3)

#ifdef HAS_RGB24TOARGBROW_SSSE3
91

92
// Shuffle table for converting RGB24 to ARGB.
93 94
static const uvec8 kShuffleMaskRGB24ToARGB = {
    0u, 1u, 2u, 12u, 3u, 4u, 5u, 13u, 6u, 7u, 8u, 14u, 9u, 10u, 11u, 15u};
95 96

// Shuffle table for converting RAW to ARGB.
97 98
static const uvec8 kShuffleMaskRAWToARGB = {2u, 1u, 0u, 12u, 5u,  4u,  3u, 13u,
                                            8u, 7u, 6u, 14u, 11u, 10u, 9u, 15u};
99

100 101
// Shuffle table for converting RAW to RGB24.  First 8.
static const uvec8 kShuffleMaskRAWToRGB24_0 = {
102 103
    2u,   1u,   0u,   5u,   4u,   3u,   8u,   7u,
    128u, 128u, 128u, 128u, 128u, 128u, 128u, 128u};
104 105 106

// Shuffle table for converting RAW to RGB24.  Middle 8.
static const uvec8 kShuffleMaskRAWToRGB24_1 = {
107 108
    2u,   7u,   6u,   5u,   10u,  9u,   8u,   13u,
    128u, 128u, 128u, 128u, 128u, 128u, 128u, 128u};
109 110 111

// Shuffle table for converting RAW to RGB24.  Last 8.
static const uvec8 kShuffleMaskRAWToRGB24_2 = {
112 113
    8u,   7u,   12u,  11u,  10u,  15u,  14u,  13u,
    128u, 128u, 128u, 128u, 128u, 128u, 128u, 128u};
114

fbarchard@google.com's avatar
fbarchard@google.com committed
115
// Shuffle table for converting ARGB to RGB24.
116
static const uvec8 kShuffleMaskARGBToRGB24 = {
117
    0u, 1u, 2u, 4u, 5u, 6u, 8u, 9u, 10u, 12u, 13u, 14u, 128u, 128u, 128u, 128u};
fbarchard@google.com's avatar
fbarchard@google.com committed
118 119

// Shuffle table for converting ARGB to RAW.
120
static const uvec8 kShuffleMaskARGBToRAW = {
121
    2u, 1u, 0u, 6u, 5u, 4u, 10u, 9u, 8u, 14u, 13u, 12u, 128u, 128u, 128u, 128u};
fbarchard@google.com's avatar
fbarchard@google.com committed
122

123
// Shuffle table for converting ARGBToRGB24 for I422ToRGB24.  First 8 + next 4
124
static const uvec8 kShuffleMaskARGBToRGB24_0 = {
125
    0u, 1u, 2u, 4u, 5u, 6u, 8u, 9u, 128u, 128u, 128u, 128u, 10u, 12u, 13u, 14u};
126

127
// YUY2 shuf 16 Y to 32 Y.
128 129 130
static const lvec8 kShuffleYUY2Y = {0,  0,  2,  2,  4,  4,  6,  6,  8,  8, 10,
                                    10, 12, 12, 14, 14, 0,  0,  2,  2,  4, 4,
                                    6,  6,  8,  8,  10, 10, 12, 12, 14, 14};
131 132

// YUY2 shuf 8 UV to 16 UV.
133 134 135
static const lvec8 kShuffleYUY2UV = {1,  3,  1,  3,  5,  7,  5,  7,  9,  11, 9,
                                     11, 13, 15, 13, 15, 1,  3,  1,  3,  5,  7,
                                     5,  7,  9,  11, 9,  11, 13, 15, 13, 15};
136 137

// UYVY shuf 16 Y to 32 Y.
138 139 140
static const lvec8 kShuffleUYVYY = {1,  1,  3,  3,  5,  5,  7,  7,  9,  9, 11,
                                    11, 13, 13, 15, 15, 1,  1,  3,  3,  5, 5,
                                    7,  7,  9,  9,  11, 11, 13, 13, 15, 15};
141 142

// UYVY shuf 8 UV to 16 UV.
143 144 145
static const lvec8 kShuffleUYVYUV = {0,  2,  0,  2,  4,  6,  4,  6,  8,  10, 8,
                                     10, 12, 14, 12, 14, 0,  2,  0,  2,  4,  6,
                                     4,  6,  8,  10, 8,  10, 12, 14, 12, 14};
146 147 148

// NV21 shuf 8 VU to 16 UV.
static const lvec8 kShuffleNV21 = {
149 150
    1, 0, 1, 0, 3, 2, 3, 2, 5, 4, 5, 4, 7, 6, 7, 6,
    1, 0, 1, 0, 3, 2, 3, 2, 5, 4, 5, 4, 7, 6, 7, 6,
151
};
152
#endif  // HAS_RGB24TOARGBROW_SSSE3
153

154
#ifdef HAS_J400TOARGBROW_SSE2
Frank Barchard's avatar
Frank Barchard committed
155
void J400ToARGBRow_SSE2(const uint8_t* src_y, uint8_t* dst_argb, int width) {
156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179
  asm volatile(
      "pcmpeqb   %%xmm5,%%xmm5                   \n"
      "pslld     $0x18,%%xmm5                    \n"

      LABELALIGN
      "1:                                        \n"
      "movq      (%0),%%xmm0                     \n"
      "lea       0x8(%0),%0                      \n"
      "punpcklbw %%xmm0,%%xmm0                   \n"
      "movdqa    %%xmm0,%%xmm1                   \n"
      "punpcklwd %%xmm0,%%xmm0                   \n"
      "punpckhwd %%xmm1,%%xmm1                   \n"
      "por       %%xmm5,%%xmm0                   \n"
      "por       %%xmm5,%%xmm1                   \n"
      "movdqu    %%xmm0,(%1)                     \n"
      "movdqu    %%xmm1,0x10(%1)                 \n"
      "lea       0x20(%1),%1                     \n"
      "sub       $0x8,%2                         \n"
      "jg        1b                              \n"
      : "+r"(src_y),     // %0
        "+r"(dst_argb),  // %1
        "+r"(width)      // %2
        ::"memory",
        "cc", "xmm0", "xmm1", "xmm5");
180
}
181
#endif  // HAS_J400TOARGBROW_SSE2
182

183
#ifdef HAS_RGB24TOARGBROW_SSSE3
184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221
void RGB24ToARGBRow_SSSE3(const uint8_t* src_rgb24,
                          uint8_t* dst_argb,
                          int width) {
  asm volatile(
      "pcmpeqb   %%xmm5,%%xmm5                   \n"  // 0xff000000
      "pslld     $0x18,%%xmm5                    \n"
      "movdqa    %3,%%xmm4                       \n"

      LABELALIGN
      "1:                                        \n"
      "movdqu    (%0),%%xmm0                     \n"
      "movdqu    0x10(%0),%%xmm1                 \n"
      "movdqu    0x20(%0),%%xmm3                 \n"
      "lea       0x30(%0),%0                     \n"
      "movdqa    %%xmm3,%%xmm2                   \n"
      "palignr   $0x8,%%xmm1,%%xmm2              \n"
      "pshufb    %%xmm4,%%xmm2                   \n"
      "por       %%xmm5,%%xmm2                   \n"
      "palignr   $0xc,%%xmm0,%%xmm1              \n"
      "pshufb    %%xmm4,%%xmm0                   \n"
      "movdqu    %%xmm2,0x20(%1)                 \n"
      "por       %%xmm5,%%xmm0                   \n"
      "pshufb    %%xmm4,%%xmm1                   \n"
      "movdqu    %%xmm0,(%1)                     \n"
      "por       %%xmm5,%%xmm1                   \n"
      "palignr   $0x4,%%xmm3,%%xmm3              \n"
      "pshufb    %%xmm4,%%xmm3                   \n"
      "movdqu    %%xmm1,0x10(%1)                 \n"
      "por       %%xmm5,%%xmm3                   \n"
      "movdqu    %%xmm3,0x30(%1)                 \n"
      "lea       0x40(%1),%1                     \n"
      "sub       $0x10,%2                        \n"
      "jg        1b                              \n"
      : "+r"(src_rgb24),              // %0
        "+r"(dst_argb),               // %1
        "+r"(width)                   // %2
      : "m"(kShuffleMaskRGB24ToARGB)  // %3
      : "memory", "cc", "xmm0", "xmm1", "xmm2", "xmm3", "xmm4", "xmm5");
222 223
}

Frank Barchard's avatar
Frank Barchard committed
224
void RAWToARGBRow_SSSE3(const uint8_t* src_raw, uint8_t* dst_argb, int width) {
225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259
  asm volatile(
      "pcmpeqb   %%xmm5,%%xmm5                   \n"  // 0xff000000
      "pslld     $0x18,%%xmm5                    \n"
      "movdqa    %3,%%xmm4                       \n"

      LABELALIGN
      "1:                                        \n"
      "movdqu    (%0),%%xmm0                     \n"
      "movdqu    0x10(%0),%%xmm1                 \n"
      "movdqu    0x20(%0),%%xmm3                 \n"
      "lea       0x30(%0),%0                     \n"
      "movdqa    %%xmm3,%%xmm2                   \n"
      "palignr   $0x8,%%xmm1,%%xmm2              \n"
      "pshufb    %%xmm4,%%xmm2                   \n"
      "por       %%xmm5,%%xmm2                   \n"
      "palignr   $0xc,%%xmm0,%%xmm1              \n"
      "pshufb    %%xmm4,%%xmm0                   \n"
      "movdqu    %%xmm2,0x20(%1)                 \n"
      "por       %%xmm5,%%xmm0                   \n"
      "pshufb    %%xmm4,%%xmm1                   \n"
      "movdqu    %%xmm0,(%1)                     \n"
      "por       %%xmm5,%%xmm1                   \n"
      "palignr   $0x4,%%xmm3,%%xmm3              \n"
      "pshufb    %%xmm4,%%xmm3                   \n"
      "movdqu    %%xmm1,0x10(%1)                 \n"
      "por       %%xmm5,%%xmm3                   \n"
      "movdqu    %%xmm3,0x30(%1)                 \n"
      "lea       0x40(%1),%1                     \n"
      "sub       $0x10,%2                        \n"
      "jg        1b                              \n"
      : "+r"(src_raw),              // %0
        "+r"(dst_argb),             // %1
        "+r"(width)                 // %2
      : "m"(kShuffleMaskRAWToARGB)  // %3
      : "memory", "cc", "xmm0", "xmm1", "xmm2", "xmm3", "xmm4", "xmm5");
fbarchard@google.com's avatar
fbarchard@google.com committed
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
void RAWToRGB24Row_SSSE3(const uint8_t* src_raw,
                         uint8_t* dst_rgb24,
                         int width) {
  asm volatile(
      "movdqa     %3,%%xmm3                       \n"
      "movdqa     %4,%%xmm4                       \n"
      "movdqa     %5,%%xmm5                       \n"

      LABELALIGN
      "1:                                        \n"
      "movdqu    (%0),%%xmm0                     \n"
      "movdqu    0x4(%0),%%xmm1                  \n"
      "movdqu    0x8(%0),%%xmm2                  \n"
      "lea       0x18(%0),%0                     \n"
      "pshufb    %%xmm3,%%xmm0                   \n"
      "pshufb    %%xmm4,%%xmm1                   \n"
      "pshufb    %%xmm5,%%xmm2                   \n"
      "movq      %%xmm0,(%1)                     \n"
      "movq      %%xmm1,0x8(%1)                  \n"
      "movq      %%xmm2,0x10(%1)                 \n"
      "lea       0x18(%1),%1                     \n"
      "sub       $0x8,%2                         \n"
      "jg        1b                              \n"
      : "+r"(src_raw),                  // %0
        "+r"(dst_rgb24),                // %1
        "+r"(width)                     // %2
      : "m"(kShuffleMaskRAWToRGB24_0),  // %3
        "m"(kShuffleMaskRAWToRGB24_1),  // %4
        "m"(kShuffleMaskRAWToRGB24_2)   // %5
      : "memory", "cc", "xmm0", "xmm1", "xmm2", "xmm3", "xmm4", "xmm5");
292 293
}

Frank Barchard's avatar
Frank Barchard committed
294
void RGB565ToARGBRow_SSE2(const uint8_t* src, uint8_t* dst, int width) {
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
  asm volatile(
      "mov       $0x1080108,%%eax                \n"
      "movd      %%eax,%%xmm5                    \n"
      "pshufd    $0x0,%%xmm5,%%xmm5              \n"
      "mov       $0x20802080,%%eax               \n"
      "movd      %%eax,%%xmm6                    \n"
      "pshufd    $0x0,%%xmm6,%%xmm6              \n"
      "pcmpeqb   %%xmm3,%%xmm3                   \n"
      "psllw     $0xb,%%xmm3                     \n"
      "pcmpeqb   %%xmm4,%%xmm4                   \n"
      "psllw     $0xa,%%xmm4                     \n"
      "psrlw     $0x5,%%xmm4                     \n"
      "pcmpeqb   %%xmm7,%%xmm7                   \n"
      "psllw     $0x8,%%xmm7                     \n"
      "sub       %0,%1                           \n"
      "sub       %0,%1                           \n"

      LABELALIGN
      "1:                                        \n"
      "movdqu    (%0),%%xmm0                     \n"
      "movdqa    %%xmm0,%%xmm1                   \n"
      "movdqa    %%xmm0,%%xmm2                   \n"
      "pand      %%xmm3,%%xmm1                   \n"
      "psllw     $0xb,%%xmm2                     \n"
      "pmulhuw   %%xmm5,%%xmm1                   \n"
      "pmulhuw   %%xmm5,%%xmm2                   \n"
      "psllw     $0x8,%%xmm1                     \n"
      "por       %%xmm2,%%xmm1                   \n"
      "pand      %%xmm4,%%xmm0                   \n"
      "pmulhuw   %%xmm6,%%xmm0                   \n"
      "por       %%xmm7,%%xmm0                   \n"
      "movdqa    %%xmm1,%%xmm2                   \n"
      "punpcklbw %%xmm0,%%xmm1                   \n"
      "punpckhbw %%xmm0,%%xmm2                   \n"
      "movdqu    %%xmm1,0x00(%1,%0,2)            \n"
      "movdqu    %%xmm2,0x10(%1,%0,2)            \n"
      "lea       0x10(%0),%0                     \n"
      "sub       $0x8,%2                         \n"
      "jg        1b                              \n"
      : "+r"(src),   // %0
        "+r"(dst),   // %1
        "+r"(width)  // %2
      :
      : "memory", "cc", "eax", "xmm0", "xmm1", "xmm2", "xmm3", "xmm4", "xmm5",
        "xmm6", "xmm7");
fbarchard@google.com's avatar
fbarchard@google.com committed
340 341
}

Frank Barchard's avatar
Frank Barchard committed
342
void ARGB1555ToARGBRow_SSE2(const uint8_t* src, uint8_t* dst, int width) {
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 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390
  asm volatile(
      "mov       $0x1080108,%%eax                \n"
      "movd      %%eax,%%xmm5                    \n"
      "pshufd    $0x0,%%xmm5,%%xmm5              \n"
      "mov       $0x42004200,%%eax               \n"
      "movd      %%eax,%%xmm6                    \n"
      "pshufd    $0x0,%%xmm6,%%xmm6              \n"
      "pcmpeqb   %%xmm3,%%xmm3                   \n"
      "psllw     $0xb,%%xmm3                     \n"
      "movdqa    %%xmm3,%%xmm4                   \n"
      "psrlw     $0x6,%%xmm4                     \n"
      "pcmpeqb   %%xmm7,%%xmm7                   \n"
      "psllw     $0x8,%%xmm7                     \n"
      "sub       %0,%1                           \n"
      "sub       %0,%1                           \n"

      LABELALIGN
      "1:                                        \n"
      "movdqu    (%0),%%xmm0                     \n"
      "movdqa    %%xmm0,%%xmm1                   \n"
      "movdqa    %%xmm0,%%xmm2                   \n"
      "psllw     $0x1,%%xmm1                     \n"
      "psllw     $0xb,%%xmm2                     \n"
      "pand      %%xmm3,%%xmm1                   \n"
      "pmulhuw   %%xmm5,%%xmm2                   \n"
      "pmulhuw   %%xmm5,%%xmm1                   \n"
      "psllw     $0x8,%%xmm1                     \n"
      "por       %%xmm2,%%xmm1                   \n"
      "movdqa    %%xmm0,%%xmm2                   \n"
      "pand      %%xmm4,%%xmm0                   \n"
      "psraw     $0x8,%%xmm2                     \n"
      "pmulhuw   %%xmm6,%%xmm0                   \n"
      "pand      %%xmm7,%%xmm2                   \n"
      "por       %%xmm2,%%xmm0                   \n"
      "movdqa    %%xmm1,%%xmm2                   \n"
      "punpcklbw %%xmm0,%%xmm1                   \n"
      "punpckhbw %%xmm0,%%xmm2                   \n"
      "movdqu    %%xmm1,0x00(%1,%0,2)            \n"
      "movdqu    %%xmm2,0x10(%1,%0,2)            \n"
      "lea       0x10(%0),%0                     \n"
      "sub       $0x8,%2                         \n"
      "jg        1b                              \n"
      : "+r"(src),   // %0
        "+r"(dst),   // %1
        "+r"(width)  // %2
      :
      : "memory", "cc", "eax", "xmm0", "xmm1", "xmm2", "xmm3", "xmm4", "xmm5",
        "xmm6", "xmm7");
fbarchard@google.com's avatar
fbarchard@google.com committed
391 392
}

Frank Barchard's avatar
Frank Barchard committed
393
void ARGB4444ToARGBRow_SSE2(const uint8_t* src, uint8_t* dst, int width) {
394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427
  asm volatile(
      "mov       $0xf0f0f0f,%%eax                \n"
      "movd      %%eax,%%xmm4                    \n"
      "pshufd    $0x0,%%xmm4,%%xmm4              \n"
      "movdqa    %%xmm4,%%xmm5                   \n"
      "pslld     $0x4,%%xmm5                     \n"
      "sub       %0,%1                           \n"
      "sub       %0,%1                           \n"

      LABELALIGN
      "1:                                        \n"
      "movdqu    (%0),%%xmm0                     \n"
      "movdqa    %%xmm0,%%xmm2                   \n"
      "pand      %%xmm4,%%xmm0                   \n"
      "pand      %%xmm5,%%xmm2                   \n"
      "movdqa    %%xmm0,%%xmm1                   \n"
      "movdqa    %%xmm2,%%xmm3                   \n"
      "psllw     $0x4,%%xmm1                     \n"
      "psrlw     $0x4,%%xmm3                     \n"
      "por       %%xmm1,%%xmm0                   \n"
      "por       %%xmm3,%%xmm2                   \n"
      "movdqa    %%xmm0,%%xmm1                   \n"
      "punpcklbw %%xmm2,%%xmm0                   \n"
      "punpckhbw %%xmm2,%%xmm1                   \n"
      "movdqu    %%xmm0,0x00(%1,%0,2)            \n"
      "movdqu    %%xmm1,0x10(%1,%0,2)            \n"
      "lea       0x10(%0),%0                     \n"
      "sub       $0x8,%2                         \n"
      "jg        1b                              \n"
      : "+r"(src),   // %0
        "+r"(dst),   // %1
        "+r"(width)  // %2
      :
      : "memory", "cc", "eax", "xmm0", "xmm1", "xmm2", "xmm3", "xmm4", "xmm5");
428 429
}

Frank Barchard's avatar
Frank Barchard committed
430
void ARGBToRGB24Row_SSSE3(const uint8_t* src, uint8_t* dst, int width) {
431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466
  asm volatile(

      "movdqa    %3,%%xmm6                       \n"

      LABELALIGN
      "1:                                        \n"
      "movdqu    (%0),%%xmm0                     \n"
      "movdqu    0x10(%0),%%xmm1                 \n"
      "movdqu    0x20(%0),%%xmm2                 \n"
      "movdqu    0x30(%0),%%xmm3                 \n"
      "lea       0x40(%0),%0                     \n"
      "pshufb    %%xmm6,%%xmm0                   \n"
      "pshufb    %%xmm6,%%xmm1                   \n"
      "pshufb    %%xmm6,%%xmm2                   \n"
      "pshufb    %%xmm6,%%xmm3                   \n"
      "movdqa    %%xmm1,%%xmm4                   \n"
      "psrldq    $0x4,%%xmm1                     \n"
      "pslldq    $0xc,%%xmm4                     \n"
      "movdqa    %%xmm2,%%xmm5                   \n"
      "por       %%xmm4,%%xmm0                   \n"
      "pslldq    $0x8,%%xmm5                     \n"
      "movdqu    %%xmm0,(%1)                     \n"
      "por       %%xmm5,%%xmm1                   \n"
      "psrldq    $0x8,%%xmm2                     \n"
      "pslldq    $0x4,%%xmm3                     \n"
      "por       %%xmm3,%%xmm2                   \n"
      "movdqu    %%xmm1,0x10(%1)                 \n"
      "movdqu    %%xmm2,0x20(%1)                 \n"
      "lea       0x30(%1),%1                     \n"
      "sub       $0x10,%2                        \n"
      "jg        1b                              \n"
      : "+r"(src),                    // %0
        "+r"(dst),                    // %1
        "+r"(width)                   // %2
      : "m"(kShuffleMaskARGBToRGB24)  // %3
      : "memory", "cc", "xmm0", "xmm1", "xmm2", "xmm3", "xmm4", "xmm5", "xmm6");
fbarchard@google.com's avatar
fbarchard@google.com committed
467 468
}

Frank Barchard's avatar
Frank Barchard committed
469
void ARGBToRAWRow_SSSE3(const uint8_t* src, uint8_t* dst, int width) {
470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505
  asm volatile(

      "movdqa    %3,%%xmm6                       \n"

      LABELALIGN
      "1:                                        \n"
      "movdqu    (%0),%%xmm0                     \n"
      "movdqu    0x10(%0),%%xmm1                 \n"
      "movdqu    0x20(%0),%%xmm2                 \n"
      "movdqu    0x30(%0),%%xmm3                 \n"
      "lea       0x40(%0),%0                     \n"
      "pshufb    %%xmm6,%%xmm0                   \n"
      "pshufb    %%xmm6,%%xmm1                   \n"
      "pshufb    %%xmm6,%%xmm2                   \n"
      "pshufb    %%xmm6,%%xmm3                   \n"
      "movdqa    %%xmm1,%%xmm4                   \n"
      "psrldq    $0x4,%%xmm1                     \n"
      "pslldq    $0xc,%%xmm4                     \n"
      "movdqa    %%xmm2,%%xmm5                   \n"
      "por       %%xmm4,%%xmm0                   \n"
      "pslldq    $0x8,%%xmm5                     \n"
      "movdqu    %%xmm0,(%1)                     \n"
      "por       %%xmm5,%%xmm1                   \n"
      "psrldq    $0x8,%%xmm2                     \n"
      "pslldq    $0x4,%%xmm3                     \n"
      "por       %%xmm3,%%xmm2                   \n"
      "movdqu    %%xmm1,0x10(%1)                 \n"
      "movdqu    %%xmm2,0x20(%1)                 \n"
      "lea       0x30(%1),%1                     \n"
      "sub       $0x10,%2                        \n"
      "jg        1b                              \n"
      : "+r"(src),                  // %0
        "+r"(dst),                  // %1
        "+r"(width)                 // %2
      : "m"(kShuffleMaskARGBToRAW)  // %3
      : "memory", "cc", "xmm0", "xmm1", "xmm2", "xmm3", "xmm4", "xmm5", "xmm6");
fbarchard@google.com's avatar
fbarchard@google.com committed
506 507
}

508
#ifdef HAS_ARGBTORGB24ROW_AVX2
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
// vpermd for 12+12 to 24
static const lvec32 kPermdRGB24_AVX = {0, 1, 2, 4, 5, 6, 3, 7};

void ARGBToRGB24Row_AVX2(const uint8_t* src, uint8_t* dst, int width) {
  asm volatile(
      "vbroadcastf128 %3,%%ymm6                  \n"
      "vmovdqa    %4,%%ymm7                      \n"

      LABELALIGN
      "1:                                        \n"
      "vmovdqu    (%0),%%ymm0                    \n"
      "vmovdqu    0x20(%0),%%ymm1                \n"
      "vmovdqu    0x40(%0),%%ymm2                \n"
      "vmovdqu    0x60(%0),%%ymm3                \n"
      "lea        0x80(%0),%0                    \n"
      "vpshufb    %%ymm6,%%ymm0,%%ymm0           \n"  // xxx0yyy0
      "vpshufb    %%ymm6,%%ymm1,%%ymm1           \n"
      "vpshufb    %%ymm6,%%ymm2,%%ymm2           \n"
      "vpshufb    %%ymm6,%%ymm3,%%ymm3           \n"
      "vpermd     %%ymm0,%%ymm7,%%ymm0           \n"  // pack to 24 bytes
      "vpermd     %%ymm1,%%ymm7,%%ymm1           \n"
      "vpermd     %%ymm2,%%ymm7,%%ymm2           \n"
      "vpermd     %%ymm3,%%ymm7,%%ymm3           \n"
      "vpermq     $0x3f,%%ymm1,%%ymm4            \n"  // combine 24 + 8
      "vpor       %%ymm4,%%ymm0,%%ymm0           \n"
      "vmovdqu    %%ymm0,(%1)                    \n"
      "vpermq     $0xf9,%%ymm1,%%ymm1            \n"  // combine 16 + 16
      "vpermq     $0x4f,%%ymm2,%%ymm4            \n"
      "vpor       %%ymm4,%%ymm1,%%ymm1           \n"
      "vmovdqu    %%ymm1,0x20(%1)                \n"
      "vpermq     $0xfe,%%ymm2,%%ymm2            \n"  // combine 8 + 24
      "vpermq     $0x93,%%ymm3,%%ymm3            \n"
      "vpor       %%ymm3,%%ymm2,%%ymm2           \n"
      "vmovdqu    %%ymm2,0x40(%1)                \n"
      "lea        0x60(%1),%1                    \n"
      "sub        $0x20,%2                       \n"
      "jg         1b                             \n"
      "vzeroupper                                \n"
      : "+r"(src),                     // %0
        "+r"(dst),                     // %1
        "+r"(width)                    // %2
      : "m"(kShuffleMaskARGBToRGB24),  // %3
        "m"(kPermdRGB24_AVX)           // %4
      : "memory", "cc", "xmm0", "xmm1", "xmm2", "xmm3", "xmm4", "xmm5", "xmm6",
        "xmm7");
}
555
#endif
556

557
#ifdef HAS_ARGBTORGB24ROW_AVX512VBMI
558 559 560 561 562 563 564 565 566 567 568 569 570 571
// Shuffle table for converting ARGBToRGB24
static const ulvec8 kPermARGBToRGB24_0 = {
    0u,  1u,  2u,  4u,  5u,  6u,  8u,  9u,  10u, 12u, 13u,
    14u, 16u, 17u, 18u, 20u, 21u, 22u, 24u, 25u, 26u, 28u,
    29u, 30u, 32u, 33u, 34u, 36u, 37u, 38u, 40u, 41u};
static const ulvec8 kPermARGBToRGB24_1 = {
    10u, 12u, 13u, 14u, 16u, 17u, 18u, 20u, 21u, 22u, 24u,
    25u, 26u, 28u, 29u, 30u, 32u, 33u, 34u, 36u, 37u, 38u,
    40u, 41u, 42u, 44u, 45u, 46u, 48u, 49u, 50u, 52u};
static const ulvec8 kPermARGBToRGB24_2 = {
    21u, 22u, 24u, 25u, 26u, 28u, 29u, 30u, 32u, 33u, 34u,
    36u, 37u, 38u, 40u, 41u, 42u, 44u, 45u, 46u, 48u, 49u,
    50u, 52u, 53u, 54u, 56u, 57u, 58u, 60u, 61u, 62u};

572
void ARGBToRGB24Row_AVX512VBMI(const uint8_t* src, uint8_t* dst, int width) {
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
  asm volatile(
      "vmovdqa    %3,%%ymm5                      \n"
      "vmovdqa    %4,%%ymm6                      \n"
      "vmovdqa    %5,%%ymm7                      \n"

      LABELALIGN
      "1:                                        \n"
      "vmovdqu    (%0),%%ymm0                    \n"
      "vmovdqu    0x20(%0),%%ymm1                \n"
      "vmovdqu    0x40(%0),%%ymm2                \n"
      "vmovdqu    0x60(%0),%%ymm3                \n"
      "lea        0x80(%0),%0                    \n"
      "vpermt2b   %%ymm1,%%ymm5,%%ymm0           \n"
      "vpermt2b   %%ymm2,%%ymm6,%%ymm1           \n"
      "vpermt2b   %%ymm3,%%ymm7,%%ymm2           \n"
      "vmovdqu    %%ymm0,(%1)                    \n"
      "vmovdqu    %%ymm1,0x20(%1)                \n"
      "vmovdqu    %%ymm2,0x40(%1)                \n"
      "lea        0x60(%1),%1                    \n"
      "sub        $0x20,%2                       \n"
      "jg         1b                             \n"
      "vzeroupper                                \n"
      : "+r"(src),                // %0
        "+r"(dst),                // %1
        "+r"(width)               // %2
      : "m"(kPermARGBToRGB24_0),  // %3
        "m"(kPermARGBToRGB24_1),  // %4
        "m"(kPermARGBToRGB24_2)   // %5
      : "memory", "cc", "xmm0", "xmm1", "xmm2", "xmm3", "xmm5", "xmm6", "xmm7");
}
603
#endif
604

605
#ifdef HAS_ARGBTORAWROW_AVX2
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
void ARGBToRAWRow_AVX2(const uint8_t* src, uint8_t* dst, int width) {
  asm volatile(
      "vbroadcastf128 %3,%%ymm6                  \n"
      "vmovdqa    %4,%%ymm7                      \n"

      LABELALIGN
      "1:                                        \n"
      "vmovdqu    (%0),%%ymm0                    \n"
      "vmovdqu    0x20(%0),%%ymm1                \n"
      "vmovdqu    0x40(%0),%%ymm2                \n"
      "vmovdqu    0x60(%0),%%ymm3                \n"
      "lea        0x80(%0),%0                    \n"
      "vpshufb    %%ymm6,%%ymm0,%%ymm0           \n"  // xxx0yyy0
      "vpshufb    %%ymm6,%%ymm1,%%ymm1           \n"
      "vpshufb    %%ymm6,%%ymm2,%%ymm2           \n"
      "vpshufb    %%ymm6,%%ymm3,%%ymm3           \n"
      "vpermd     %%ymm0,%%ymm7,%%ymm0           \n"  // pack to 24 bytes
      "vpermd     %%ymm1,%%ymm7,%%ymm1           \n"
      "vpermd     %%ymm2,%%ymm7,%%ymm2           \n"
      "vpermd     %%ymm3,%%ymm7,%%ymm3           \n"
      "vpermq     $0x3f,%%ymm1,%%ymm4            \n"  // combine 24 + 8
      "vpor       %%ymm4,%%ymm0,%%ymm0           \n"
      "vmovdqu    %%ymm0,(%1)                    \n"
      "vpermq     $0xf9,%%ymm1,%%ymm1            \n"  // combine 16 + 16
      "vpermq     $0x4f,%%ymm2,%%ymm4            \n"
      "vpor       %%ymm4,%%ymm1,%%ymm1           \n"
      "vmovdqu    %%ymm1,0x20(%1)                \n"
      "vpermq     $0xfe,%%ymm2,%%ymm2            \n"  // combine 8 + 24
      "vpermq     $0x93,%%ymm3,%%ymm3            \n"
      "vpor       %%ymm3,%%ymm2,%%ymm2           \n"
      "vmovdqu    %%ymm2,0x40(%1)                \n"
      "lea        0x60(%1),%1                    \n"
      "sub        $0x20,%2                       \n"
      "jg         1b                             \n"
      "vzeroupper                                \n"
641 642 643 644 645
      : "+r"(src),                   // %0
        "+r"(dst),                   // %1
        "+r"(width)                  // %2
      : "m"(kShuffleMaskARGBToRAW),  // %3
        "m"(kPermdRGB24_AVX)         // %4
646 647 648
      : "memory", "cc", "xmm0", "xmm1", "xmm2", "xmm3", "xmm4", "xmm5", "xmm6",
        "xmm7");
}
649
#endif
650

Frank Barchard's avatar
Frank Barchard committed
651
void ARGBToRGB565Row_SSE2(const uint8_t* src, uint8_t* dst, int width) {
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
  asm volatile(
      "pcmpeqb   %%xmm3,%%xmm3                   \n"
      "psrld     $0x1b,%%xmm3                    \n"
      "pcmpeqb   %%xmm4,%%xmm4                   \n"
      "psrld     $0x1a,%%xmm4                    \n"
      "pslld     $0x5,%%xmm4                     \n"
      "pcmpeqb   %%xmm5,%%xmm5                   \n"
      "pslld     $0xb,%%xmm5                     \n"

      LABELALIGN
      "1:                                        \n"
      "movdqu    (%0),%%xmm0                     \n"
      "movdqa    %%xmm0,%%xmm1                   \n"
      "movdqa    %%xmm0,%%xmm2                   \n"
      "pslld     $0x8,%%xmm0                     \n"
      "psrld     $0x3,%%xmm1                     \n"
      "psrld     $0x5,%%xmm2                     \n"
      "psrad     $0x10,%%xmm0                    \n"
      "pand      %%xmm3,%%xmm1                   \n"
      "pand      %%xmm4,%%xmm2                   \n"
      "pand      %%xmm5,%%xmm0                   \n"
      "por       %%xmm2,%%xmm1                   \n"
      "por       %%xmm1,%%xmm0                   \n"
      "packssdw  %%xmm0,%%xmm0                   \n"
      "lea       0x10(%0),%0                     \n"
      "movq      %%xmm0,(%1)                     \n"
      "lea       0x8(%1),%1                      \n"
      "sub       $0x4,%2                         \n"
      "jg        1b                              \n"
      : "+r"(src),   // %0
        "+r"(dst),   // %1
        "+r"(width)  // %2
        ::"memory",
        "cc", "xmm0", "xmm1", "xmm2", "xmm3", "xmm4", "xmm5");
686 687
}

Frank Barchard's avatar
Frank Barchard committed
688 689 690
void ARGBToRGB565DitherRow_SSE2(const uint8_t* src,
                                uint8_t* dst,
                                const uint32_t dither4,
691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706
                                int width) {
  asm volatile(
      "movd       %3,%%xmm6                      \n"
      "punpcklbw  %%xmm6,%%xmm6                  \n"
      "movdqa     %%xmm6,%%xmm7                  \n"
      "punpcklwd  %%xmm6,%%xmm6                  \n"
      "punpckhwd  %%xmm7,%%xmm7                  \n"
      "pcmpeqb    %%xmm3,%%xmm3                  \n"
      "psrld      $0x1b,%%xmm3                   \n"
      "pcmpeqb    %%xmm4,%%xmm4                  \n"
      "psrld      $0x1a,%%xmm4                   \n"
      "pslld      $0x5,%%xmm4                    \n"
      "pcmpeqb    %%xmm5,%%xmm5                  \n"
      "pslld      $0xb,%%xmm5                    \n"

      LABELALIGN
Frank Barchard's avatar
Frank Barchard committed
707
      "1:                                        \n"
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
      "movdqu     (%0),%%xmm0                    \n"
      "paddusb    %%xmm6,%%xmm0                  \n"
      "movdqa     %%xmm0,%%xmm1                  \n"
      "movdqa     %%xmm0,%%xmm2                  \n"
      "pslld      $0x8,%%xmm0                    \n"
      "psrld      $0x3,%%xmm1                    \n"
      "psrld      $0x5,%%xmm2                    \n"
      "psrad      $0x10,%%xmm0                   \n"
      "pand       %%xmm3,%%xmm1                  \n"
      "pand       %%xmm4,%%xmm2                  \n"
      "pand       %%xmm5,%%xmm0                  \n"
      "por        %%xmm2,%%xmm1                  \n"
      "por        %%xmm1,%%xmm0                  \n"
      "packssdw   %%xmm0,%%xmm0                  \n"
      "lea        0x10(%0),%0                    \n"
      "movq       %%xmm0,(%1)                    \n"
      "lea        0x8(%1),%1                     \n"
      "sub        $0x4,%2                        \n"
      "jg        1b                              \n"
      : "+r"(src),    // %0
        "+r"(dst),    // %1
        "+r"(width)   // %2
      : "m"(dither4)  // %3
      : "memory", "cc", "xmm0", "xmm1", "xmm2", "xmm3", "xmm4", "xmm5", "xmm6",
        "xmm7");
fbarchard@google.com's avatar
fbarchard@google.com committed
733 734
}

735
#ifdef HAS_ARGBTORGB565DITHERROW_AVX2
Frank Barchard's avatar
Frank Barchard committed
736 737 738
void ARGBToRGB565DitherRow_AVX2(const uint8_t* src,
                                uint8_t* dst,
                                const uint32_t dither4,
739 740 741 742 743 744 745 746 747 748 749 750 751 752
                                int width) {
  asm volatile(
      "vbroadcastss %3,%%xmm6                    \n"
      "vpunpcklbw %%xmm6,%%xmm6,%%xmm6           \n"
      "vpermq     $0xd8,%%ymm6,%%ymm6            \n"
      "vpunpcklwd %%ymm6,%%ymm6,%%ymm6           \n"
      "vpcmpeqb   %%ymm3,%%ymm3,%%ymm3           \n"
      "vpsrld     $0x1b,%%ymm3,%%ymm3            \n"
      "vpcmpeqb   %%ymm4,%%ymm4,%%ymm4           \n"
      "vpsrld     $0x1a,%%ymm4,%%ymm4            \n"
      "vpslld     $0x5,%%ymm4,%%ymm4             \n"
      "vpslld     $0xb,%%ymm3,%%ymm5             \n"

      LABELALIGN
Frank Barchard's avatar
Frank Barchard committed
753
      "1:                                        \n"
754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777
      "vmovdqu    (%0),%%ymm0                    \n"
      "vpaddusb   %%ymm6,%%ymm0,%%ymm0           \n"
      "vpsrld     $0x5,%%ymm0,%%ymm2             \n"
      "vpsrld     $0x3,%%ymm0,%%ymm1             \n"
      "vpsrld     $0x8,%%ymm0,%%ymm0             \n"
      "vpand      %%ymm4,%%ymm2,%%ymm2           \n"
      "vpand      %%ymm3,%%ymm1,%%ymm1           \n"
      "vpand      %%ymm5,%%ymm0,%%ymm0           \n"
      "vpor       %%ymm2,%%ymm1,%%ymm1           \n"
      "vpor       %%ymm1,%%ymm0,%%ymm0           \n"
      "vpackusdw  %%ymm0,%%ymm0,%%ymm0           \n"
      "vpermq     $0xd8,%%ymm0,%%ymm0            \n"
      "lea        0x20(%0),%0                    \n"
      "vmovdqu    %%xmm0,(%1)                    \n"
      "lea        0x10(%1),%1                    \n"
      "sub        $0x8,%2                        \n"
      "jg         1b                             \n"
      "vzeroupper                                \n"
      : "+r"(src),    // %0
        "+r"(dst),    // %1
        "+r"(width)   // %2
      : "m"(dither4)  // %3
      : "memory", "cc", "xmm0", "xmm1", "xmm2", "xmm3", "xmm4", "xmm5", "xmm6",
        "xmm7");
778 779 780
}
#endif  // HAS_ARGBTORGB565DITHERROW_AVX2

Frank Barchard's avatar
Frank Barchard committed
781
void ARGBToARGB1555Row_SSE2(const uint8_t* src, uint8_t* dst, int width) {
782 783 784 785 786 787 788 789 790
  asm volatile(
      "pcmpeqb   %%xmm4,%%xmm4                   \n"
      "psrld     $0x1b,%%xmm4                    \n"
      "movdqa    %%xmm4,%%xmm5                   \n"
      "pslld     $0x5,%%xmm5                     \n"
      "movdqa    %%xmm4,%%xmm6                   \n"
      "pslld     $0xa,%%xmm6                     \n"
      "pcmpeqb   %%xmm7,%%xmm7                   \n"
      "pslld     $0xf,%%xmm7                     \n"
Frank Barchard's avatar
Frank Barchard committed
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
      LABELALIGN
      "1:                                        \n"
      "movdqu    (%0),%%xmm0                     \n"
      "movdqa    %%xmm0,%%xmm1                   \n"
      "movdqa    %%xmm0,%%xmm2                   \n"
      "movdqa    %%xmm0,%%xmm3                   \n"
      "psrad     $0x10,%%xmm0                    \n"
      "psrld     $0x3,%%xmm1                     \n"
      "psrld     $0x6,%%xmm2                     \n"
      "psrld     $0x9,%%xmm3                     \n"
      "pand      %%xmm7,%%xmm0                   \n"
      "pand      %%xmm4,%%xmm1                   \n"
      "pand      %%xmm5,%%xmm2                   \n"
      "pand      %%xmm6,%%xmm3                   \n"
      "por       %%xmm1,%%xmm0                   \n"
      "por       %%xmm3,%%xmm2                   \n"
      "por       %%xmm2,%%xmm0                   \n"
      "packssdw  %%xmm0,%%xmm0                   \n"
      "lea       0x10(%0),%0                     \n"
      "movq      %%xmm0,(%1)                     \n"
      "lea       0x8(%1),%1                      \n"
      "sub       $0x4,%2                         \n"
      "jg        1b                              \n"
      : "+r"(src),   // %0
        "+r"(dst),   // %1
        "+r"(width)  // %2
        ::"memory",
        "cc", "xmm0", "xmm1", "xmm2", "xmm3", "xmm4", "xmm5", "xmm6", "xmm7");
fbarchard@google.com's avatar
fbarchard@google.com committed
820 821
}

Frank Barchard's avatar
Frank Barchard committed
822
void ARGBToARGB4444Row_SSE2(const uint8_t* src, uint8_t* dst, int width) {
823 824 825 826 827
  asm volatile(
      "pcmpeqb   %%xmm4,%%xmm4                   \n"
      "psllw     $0xc,%%xmm4                     \n"
      "movdqa    %%xmm4,%%xmm3                   \n"
      "psrlw     $0x8,%%xmm3                     \n"
Frank Barchard's avatar
Frank Barchard committed
828

829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848
      LABELALIGN
      "1:                                        \n"
      "movdqu    (%0),%%xmm0                     \n"
      "movdqa    %%xmm0,%%xmm1                   \n"
      "pand      %%xmm3,%%xmm0                   \n"
      "pand      %%xmm4,%%xmm1                   \n"
      "psrlq     $0x4,%%xmm0                     \n"
      "psrlq     $0x8,%%xmm1                     \n"
      "por       %%xmm1,%%xmm0                   \n"
      "packuswb  %%xmm0,%%xmm0                   \n"
      "lea       0x10(%0),%0                     \n"
      "movq      %%xmm0,(%1)                     \n"
      "lea       0x8(%1),%1                      \n"
      "sub       $0x4,%2                         \n"
      "jg        1b                              \n"
      : "+r"(src),   // %0
        "+r"(dst),   // %1
        "+r"(width)  // %2
        ::"memory",
        "cc", "xmm0", "xmm1", "xmm2", "xmm3", "xmm4");
fbarchard@google.com's avatar
fbarchard@google.com committed
849
}
850
#endif  // HAS_RGB24TOARGBROW_SSSE3
851

852
/*
853 854 855

ARGBToAR30Row:

856
Red Blue
857 858 859 860
With the 8 bit value in the upper bits of a short, vpmulhuw by (1024+4) will
produce a 10 bit value in the low 10 bits of each 16 bit value. This is whats
wanted for the blue channel. The red needs to be shifted 4 left, so multiply by
(1024+4)*16 for red.
861 862 863 864 865 866 867 868 869 870 871

Alpha Green
Alpha and Green are already in the high bits so vpand can zero out the other
bits, keeping just 2 upper bits of alpha and 8 bit green. The same multiplier
could be used for Green - (1024+4) putting the 10 bit green in the lsb.  Alpha
would be a simple multiplier to shift it into position.  It wants a gap of 10
above the green.  Green is 10 bits, so there are 6 bits in the low short.  4
more are needed, so a multiplier of 4 gets the 2 bits into the upper 16 bits,
and then a shift of 4 is a multiply of 16, so (4*16) = 64.  Then shift the
result left 10 to position the A and G channels.
*/
fbarchard@google.com's avatar
fbarchard@google.com committed
872

873 874 875
// Shuffle table for converting RAW to RGB24.  Last 8.
static const uvec8 kShuffleRB30 = {128u, 0u, 128u, 2u,  128u, 4u,  128u, 6u,
                                   128u, 8u, 128u, 10u, 128u, 12u, 128u, 14u};
876 877 878 879

static const uvec8 kShuffleBR30 = {128u, 2u,  128u, 0u, 128u, 6u,  128u, 4u,
                                   128u, 10u, 128u, 8u, 128u, 14u, 128u, 12u};

Frank Barchard's avatar
Frank Barchard committed
880 881 882 883
static const uint32_t kMulRB10 = 1028 * 16 * 65536 + 1028;
static const uint32_t kMaskRB10 = 0x3ff003ff;
static const uint32_t kMaskAG10 = 0xc000ff00;
static const uint32_t kMulAG10 = 64 * 65536 + 1028;
884

Frank Barchard's avatar
Frank Barchard committed
885
void ARGBToAR30Row_SSSE3(const uint8_t* src, uint8_t* dst, int width) {
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
  asm volatile(
      "movdqa     %3,%%xmm2                     \n"  // shuffler for RB
      "movd       %4,%%xmm3                     \n"  // multipler for RB
      "movd       %5,%%xmm4                     \n"  // mask for R10 B10
      "movd       %6,%%xmm5                     \n"  // mask for AG
      "movd       %7,%%xmm6                     \n"  // multipler for AG
      "pshufd     $0x0,%%xmm3,%%xmm3            \n"
      "pshufd     $0x0,%%xmm4,%%xmm4            \n"
      "pshufd     $0x0,%%xmm5,%%xmm5            \n"
      "pshufd     $0x0,%%xmm6,%%xmm6            \n"
      "sub        %0,%1                         \n"

      "1:                                       \n"
      "movdqu     (%0),%%xmm0                   \n"  // fetch 4 ARGB pixels
      "movdqa     %%xmm0,%%xmm1                 \n"
      "pshufb     %%xmm2,%%xmm1                 \n"  // R0B0
      "pand       %%xmm5,%%xmm0                 \n"  // A0G0
      "pmulhuw    %%xmm3,%%xmm1                 \n"  // X2 R16 X4  B10
      "pmulhuw    %%xmm6,%%xmm0                 \n"  // X10 A2 X10 G10
      "pand       %%xmm4,%%xmm1                 \n"  // X2 R10 X10 B10
      "pslld      $10,%%xmm0                    \n"  // A2 x10 G10 x10
      "por        %%xmm1,%%xmm0                 \n"  // A2 R10 G10 B10
      "movdqu     %%xmm0,(%1,%0)                \n"  // store 4 AR30 pixels
      "add        $0x10,%0                      \n"
      "sub        $0x4,%2                       \n"
      "jg         1b                            \n"

      : "+r"(src),          // %0
        "+r"(dst),          // %1
        "+r"(width)         // %2
      : "m"(kShuffleRB30),  // %3
        "m"(kMulRB10),      // %4
        "m"(kMaskRB10),     // %5
        "m"(kMaskAG10),     // %6
        "m"(kMulAG10)       // %7
      : "memory", "cc", "xmm0", "xmm1", "xmm2", "xmm3", "xmm4", "xmm5", "xmm6");
}

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
void ABGRToAR30Row_SSSE3(const uint8_t* src, uint8_t* dst, int width) {
  asm volatile(
      "movdqa     %3,%%xmm2                     \n"  // shuffler for RB
      "movd       %4,%%xmm3                     \n"  // multipler for RB
      "movd       %5,%%xmm4                     \n"  // mask for R10 B10
      "movd       %6,%%xmm5                     \n"  // mask for AG
      "movd       %7,%%xmm6                     \n"  // multipler for AG
      "pshufd     $0x0,%%xmm3,%%xmm3            \n"
      "pshufd     $0x0,%%xmm4,%%xmm4            \n"
      "pshufd     $0x0,%%xmm5,%%xmm5            \n"
      "pshufd     $0x0,%%xmm6,%%xmm6            \n"
      "sub        %0,%1                         \n"

      "1:                                       \n"
      "movdqu     (%0),%%xmm0                   \n"  // fetch 4 ABGR pixels
      "movdqa     %%xmm0,%%xmm1                 \n"
      "pshufb     %%xmm2,%%xmm1                 \n"  // R0B0
      "pand       %%xmm5,%%xmm0                 \n"  // A0G0
      "pmulhuw    %%xmm3,%%xmm1                 \n"  // X2 R16 X4  B10
      "pmulhuw    %%xmm6,%%xmm0                 \n"  // X10 A2 X10 G10
      "pand       %%xmm4,%%xmm1                 \n"  // X2 R10 X10 B10
      "pslld      $10,%%xmm0                    \n"  // A2 x10 G10 x10
      "por        %%xmm1,%%xmm0                 \n"  // A2 R10 G10 B10
      "movdqu     %%xmm0,(%1,%0)                \n"  // store 4 AR30 pixels
      "add        $0x10,%0                      \n"
      "sub        $0x4,%2                       \n"
      "jg         1b                            \n"

      : "+r"(src),          // %0
        "+r"(dst),          // %1
        "+r"(width)         // %2
      : "m"(kShuffleBR30),  // %3  reversed shuffler
        "m"(kMulRB10),      // %4
        "m"(kMaskRB10),     // %5
        "m"(kMaskAG10),     // %6
        "m"(kMulAG10)       // %7
      : "memory", "cc", "xmm0", "xmm1", "xmm2", "xmm3", "xmm4", "xmm5", "xmm6");
}
962

963
#ifdef HAS_ARGBTOAR30ROW_AVX2
Frank Barchard's avatar
Frank Barchard committed
964
void ARGBToAR30Row_AVX2(const uint8_t* src, uint8_t* dst, int width) {
965
  asm volatile(
966 967 968 969 970 971
      "vbroadcastf128 %3,%%ymm2                  \n"  // shuffler for RB
      "vbroadcastss  %4,%%ymm3                   \n"  // multipler for RB
      "vbroadcastss  %5,%%ymm4                   \n"  // mask for R10 B10
      "vbroadcastss  %6,%%ymm5                   \n"  // mask for AG
      "vbroadcastss  %7,%%ymm6                   \n"  // multipler for AG
      "sub        %0,%1                          \n"
972

973
      "1:                                        \n"
974 975 976 977 978 979 980 981 982
      "vmovdqu    (%0),%%ymm0                    \n"  // fetch 8 ARGB pixels
      "vpshufb    %%ymm2,%%ymm0,%%ymm1           \n"  // R0B0
      "vpand      %%ymm5,%%ymm0,%%ymm0           \n"  // A0G0
      "vpmulhuw   %%ymm3,%%ymm1,%%ymm1           \n"  // X2 R16 X4  B10
      "vpmulhuw   %%ymm6,%%ymm0,%%ymm0           \n"  // X10 A2 X10 G10
      "vpand      %%ymm4,%%ymm1,%%ymm1           \n"  // X2 R10 X10 B10
      "vpslld     $10,%%ymm0,%%ymm0              \n"  // A2 x10 G10 x10
      "vpor       %%ymm1,%%ymm0,%%ymm0           \n"  // A2 R10 G10 B10
      "vmovdqu    %%ymm0,(%1,%0)                 \n"  // store 8 AR30 pixels
983 984 985 986
      "add        $0x20,%0                       \n"
      "sub        $0x8,%2                        \n"
      "jg         1b                             \n"
      "vzeroupper                                \n"
987

988 989 990 991 992 993 994 995
      : "+r"(src),          // %0
        "+r"(dst),          // %1
        "+r"(width)         // %2
      : "m"(kShuffleRB30),  // %3
        "m"(kMulRB10),      // %4
        "m"(kMaskRB10),     // %5
        "m"(kMaskAG10),     // %6
        "m"(kMulAG10)       // %7
996
      : "memory", "cc", "xmm0", "xmm1", "xmm2", "xmm3", "xmm4", "xmm5", "xmm6");
997 998 999
}
#endif

1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036
#ifdef HAS_ABGRTOAR30ROW_AVX2
void ABGRToAR30Row_AVX2(const uint8_t* src, uint8_t* dst, int width) {
  asm volatile(
      "vbroadcastf128 %3,%%ymm2                  \n"  // shuffler for RB
      "vbroadcastss  %4,%%ymm3                   \n"  // multipler for RB
      "vbroadcastss  %5,%%ymm4                   \n"  // mask for R10 B10
      "vbroadcastss  %6,%%ymm5                   \n"  // mask for AG
      "vbroadcastss  %7,%%ymm6                   \n"  // multipler for AG
      "sub        %0,%1                          \n"

      "1:                                        \n"
      "vmovdqu    (%0),%%ymm0                    \n"  // fetch 8 ABGR pixels
      "vpshufb    %%ymm2,%%ymm0,%%ymm1           \n"  // R0B0
      "vpand      %%ymm5,%%ymm0,%%ymm0           \n"  // A0G0
      "vpmulhuw   %%ymm3,%%ymm1,%%ymm1           \n"  // X2 R16 X4  B10
      "vpmulhuw   %%ymm6,%%ymm0,%%ymm0           \n"  // X10 A2 X10 G10
      "vpand      %%ymm4,%%ymm1,%%ymm1           \n"  // X2 R10 X10 B10
      "vpslld     $10,%%ymm0,%%ymm0              \n"  // A2 x10 G10 x10
      "vpor       %%ymm1,%%ymm0,%%ymm0           \n"  // A2 R10 G10 B10
      "vmovdqu    %%ymm0,(%1,%0)                 \n"  // store 8 AR30 pixels
      "add        $0x20,%0                       \n"
      "sub        $0x8,%2                        \n"
      "jg         1b                             \n"
      "vzeroupper                                \n"

      : "+r"(src),          // %0
        "+r"(dst),          // %1
        "+r"(width)         // %2
      : "m"(kShuffleBR30),  // %3  reversed shuffler
        "m"(kMulRB10),      // %4
        "m"(kMaskRB10),     // %5
        "m"(kMaskAG10),     // %6
        "m"(kMulAG10)       // %7
      : "memory", "cc", "xmm0", "xmm1", "xmm2", "xmm3", "xmm4", "xmm5", "xmm6");
}
#endif

1037
#ifdef HAS_ARGBTOYROW_SSSE3
1038
// Convert 16 ARGB pixels (64 bytes) to 16 Y values.
Frank Barchard's avatar
Frank Barchard committed
1039
void ARGBToYRow_SSSE3(const uint8_t* src_argb, uint8_t* dst_y, int width) {
1040 1041 1042
  asm volatile(
      "movdqa    %3,%%xmm4                       \n"
      "movdqa    %4,%%xmm5                       \n"
Frank Barchard's avatar
Frank Barchard committed
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
      LABELALIGN
      "1:                                        \n"
      "movdqu    (%0),%%xmm0                     \n"
      "movdqu    0x10(%0),%%xmm1                 \n"
      "movdqu    0x20(%0),%%xmm2                 \n"
      "movdqu    0x30(%0),%%xmm3                 \n"
      "pmaddubsw %%xmm4,%%xmm0                   \n"
      "pmaddubsw %%xmm4,%%xmm1                   \n"
      "pmaddubsw %%xmm4,%%xmm2                   \n"
      "pmaddubsw %%xmm4,%%xmm3                   \n"
      "lea       0x40(%0),%0                     \n"
      "phaddw    %%xmm1,%%xmm0                   \n"
      "phaddw    %%xmm3,%%xmm2                   \n"
      "psrlw     $0x7,%%xmm0                     \n"
      "psrlw     $0x7,%%xmm2                     \n"
      "packuswb  %%xmm2,%%xmm0                   \n"
      "paddb     %%xmm5,%%xmm0                   \n"
      "movdqu    %%xmm0,(%1)                     \n"
      "lea       0x10(%1),%1                     \n"
      "sub       $0x10,%2                        \n"
      "jg        1b                              \n"
      : "+r"(src_argb),  // %0
        "+r"(dst_y),     // %1
        "+r"(width)      // %2
      : "m"(kARGBToY),   // %3
        "m"(kAddY16)     // %4
      : "memory", "cc", "xmm0", "xmm1", "xmm2", "xmm3", "xmm4", "xmm5");
1071
}
1072
#endif  // HAS_ARGBTOYROW_SSSE3
1073

1074
#ifdef HAS_ARGBTOYJROW_SSSE3
1075 1076
// Convert 16 ARGB pixels (64 bytes) to 16 YJ values.
// Same as ARGBToYRow but different coefficients, no add 16, but do rounding.
Frank Barchard's avatar
Frank Barchard committed
1077
void ARGBToYJRow_SSSE3(const uint8_t* src_argb, uint8_t* dst_y, int width) {
1078 1079 1080
  asm volatile(
      "movdqa    %3,%%xmm4                       \n"
      "movdqa    %4,%%xmm5                       \n"
Frank Barchard's avatar
Frank Barchard committed
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
      LABELALIGN
      "1:                                        \n"
      "movdqu    (%0),%%xmm0                     \n"
      "movdqu    0x10(%0),%%xmm1                 \n"
      "movdqu    0x20(%0),%%xmm2                 \n"
      "movdqu    0x30(%0),%%xmm3                 \n"
      "pmaddubsw %%xmm4,%%xmm0                   \n"
      "pmaddubsw %%xmm4,%%xmm1                   \n"
      "pmaddubsw %%xmm4,%%xmm2                   \n"
      "pmaddubsw %%xmm4,%%xmm3                   \n"
      "lea       0x40(%0),%0                     \n"
      "phaddw    %%xmm1,%%xmm0                   \n"
      "phaddw    %%xmm3,%%xmm2                   \n"
      "paddw     %%xmm5,%%xmm0                   \n"
      "paddw     %%xmm5,%%xmm2                   \n"
      "psrlw     $0x7,%%xmm0                     \n"
      "psrlw     $0x7,%%xmm2                     \n"
      "packuswb  %%xmm2,%%xmm0                   \n"
      "movdqu    %%xmm0,(%1)                     \n"
      "lea       0x10(%1),%1                     \n"
      "sub       $0x10,%2                        \n"
      "jg        1b                              \n"
      : "+r"(src_argb),  // %0
        "+r"(dst_y),     // %1
        "+r"(width)      // %2
      : "m"(kARGBToYJ),  // %3
        "m"(kAddYJ64)    // %4
      : "memory", "cc", "xmm0", "xmm1", "xmm2", "xmm3", "xmm4", "xmm5");
1110
}
1111
#endif  // HAS_ARGBTOYJROW_SSSE3
1112

1113 1114
#ifdef HAS_ARGBTOYROW_AVX2
// vpermd for vphaddw + vpackuswb vpermd.
1115
static const lvec32 kPermdARGBToY_AVX = {0, 4, 1, 5, 2, 6, 3, 7};
1116

1117
// Convert 32 ARGB pixels (128 bytes) to 32 Y values.
Frank Barchard's avatar
Frank Barchard committed
1118
void ARGBToYRow_AVX2(const uint8_t* src_argb, uint8_t* dst_y, int width) {
1119 1120 1121 1122
  asm volatile(
      "vbroadcastf128 %3,%%ymm4                  \n"
      "vbroadcastf128 %4,%%ymm5                  \n"
      "vmovdqu    %5,%%ymm6                      \n"
Frank Barchard's avatar
Frank Barchard committed
1123

1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153
      LABELALIGN
      "1:                                        \n"
      "vmovdqu    (%0),%%ymm0                    \n"
      "vmovdqu    0x20(%0),%%ymm1                \n"
      "vmovdqu    0x40(%0),%%ymm2                \n"
      "vmovdqu    0x60(%0),%%ymm3                \n"
      "vpmaddubsw %%ymm4,%%ymm0,%%ymm0           \n"
      "vpmaddubsw %%ymm4,%%ymm1,%%ymm1           \n"
      "vpmaddubsw %%ymm4,%%ymm2,%%ymm2           \n"
      "vpmaddubsw %%ymm4,%%ymm3,%%ymm3           \n"
      "lea       0x80(%0),%0                     \n"
      "vphaddw    %%ymm1,%%ymm0,%%ymm0           \n"  // mutates.
      "vphaddw    %%ymm3,%%ymm2,%%ymm2           \n"
      "vpsrlw     $0x7,%%ymm0,%%ymm0             \n"
      "vpsrlw     $0x7,%%ymm2,%%ymm2             \n"
      "vpackuswb  %%ymm2,%%ymm0,%%ymm0           \n"  // mutates.
      "vpermd     %%ymm0,%%ymm6,%%ymm0           \n"  // unmutate.
      "vpaddb     %%ymm5,%%ymm0,%%ymm0           \n"  // add 16 for Y
      "vmovdqu    %%ymm0,(%1)                    \n"
      "lea       0x20(%1),%1                     \n"
      "sub       $0x20,%2                        \n"
      "jg        1b                              \n"
      "vzeroupper                                \n"
      : "+r"(src_argb),         // %0
        "+r"(dst_y),            // %1
        "+r"(width)             // %2
      : "m"(kARGBToY),          // %3
        "m"(kAddY16),           // %4
        "m"(kPermdARGBToY_AVX)  // %5
      : "memory", "cc", "xmm0", "xmm1", "xmm2", "xmm3", "xmm4", "xmm5", "xmm6");
1154 1155 1156 1157
}
#endif  // HAS_ARGBTOYROW_AVX2

#ifdef HAS_ARGBTOYJROW_AVX2
1158
// Convert 32 ARGB pixels (128 bytes) to 32 Y values.
Frank Barchard's avatar
Frank Barchard committed
1159
void ARGBToYJRow_AVX2(const uint8_t* src_argb, uint8_t* dst_y, int width) {
1160 1161 1162 1163
  asm volatile(
      "vbroadcastf128 %3,%%ymm4                  \n"
      "vbroadcastf128 %4,%%ymm5                  \n"
      "vmovdqu    %5,%%ymm6                      \n"
Frank Barchard's avatar
Frank Barchard committed
1164

1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195
      LABELALIGN
      "1:                                        \n"
      "vmovdqu    (%0),%%ymm0                    \n"
      "vmovdqu    0x20(%0),%%ymm1                \n"
      "vmovdqu    0x40(%0),%%ymm2                \n"
      "vmovdqu    0x60(%0),%%ymm3                \n"
      "vpmaddubsw %%ymm4,%%ymm0,%%ymm0           \n"
      "vpmaddubsw %%ymm4,%%ymm1,%%ymm1           \n"
      "vpmaddubsw %%ymm4,%%ymm2,%%ymm2           \n"
      "vpmaddubsw %%ymm4,%%ymm3,%%ymm3           \n"
      "lea       0x80(%0),%0                     \n"
      "vphaddw    %%ymm1,%%ymm0,%%ymm0           \n"  // mutates.
      "vphaddw    %%ymm3,%%ymm2,%%ymm2           \n"
      "vpaddw     %%ymm5,%%ymm0,%%ymm0           \n"  // Add .5 for rounding.
      "vpaddw     %%ymm5,%%ymm2,%%ymm2           \n"
      "vpsrlw     $0x7,%%ymm0,%%ymm0             \n"
      "vpsrlw     $0x7,%%ymm2,%%ymm2             \n"
      "vpackuswb  %%ymm2,%%ymm0,%%ymm0           \n"  // mutates.
      "vpermd     %%ymm0,%%ymm6,%%ymm0           \n"  // unmutate.
      "vmovdqu    %%ymm0,(%1)                    \n"
      "lea       0x20(%1),%1                     \n"
      "sub       $0x20,%2                        \n"
      "jg        1b                              \n"
      "vzeroupper                                \n"
      : "+r"(src_argb),         // %0
        "+r"(dst_y),            // %1
        "+r"(width)             // %2
      : "m"(kARGBToYJ),         // %3
        "m"(kAddYJ64),          // %4
        "m"(kPermdARGBToY_AVX)  // %5
      : "memory", "cc", "xmm0", "xmm1", "xmm2", "xmm3", "xmm4", "xmm5", "xmm6");
1196 1197 1198
}
#endif  // HAS_ARGBTOYJROW_AVX2

1199
#ifdef HAS_ARGBTOUVROW_SSSE3
Frank Barchard's avatar
Frank Barchard committed
1200
void ARGBToUVRow_SSSE3(const uint8_t* src_argb0,
1201
                       int src_stride_argb,
Frank Barchard's avatar
Frank Barchard committed
1202 1203
                       uint8_t* dst_u,
                       uint8_t* dst_v,
1204
                       int width) {
1205 1206 1207 1208 1209
  asm volatile(
      "movdqa    %5,%%xmm3                       \n"
      "movdqa    %6,%%xmm4                       \n"
      "movdqa    %7,%%xmm5                       \n"
      "sub       %1,%2                           \n"
Frank Barchard's avatar
Frank Barchard committed
1210

1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260
      LABELALIGN
      "1:                                        \n"
      "movdqu    (%0),%%xmm0                     \n"
      "movdqu    0x00(%0,%4,1),%%xmm7            \n"
      "pavgb     %%xmm7,%%xmm0                   \n"
      "movdqu    0x10(%0),%%xmm1                 \n"
      "movdqu    0x10(%0,%4,1),%%xmm7            \n"
      "pavgb     %%xmm7,%%xmm1                   \n"
      "movdqu    0x20(%0),%%xmm2                 \n"
      "movdqu    0x20(%0,%4,1),%%xmm7            \n"
      "pavgb     %%xmm7,%%xmm2                   \n"
      "movdqu    0x30(%0),%%xmm6                 \n"
      "movdqu    0x30(%0,%4,1),%%xmm7            \n"
      "pavgb     %%xmm7,%%xmm6                   \n"

      "lea       0x40(%0),%0                     \n"
      "movdqa    %%xmm0,%%xmm7                   \n"
      "shufps    $0x88,%%xmm1,%%xmm0             \n"
      "shufps    $0xdd,%%xmm1,%%xmm7             \n"
      "pavgb     %%xmm7,%%xmm0                   \n"
      "movdqa    %%xmm2,%%xmm7                   \n"
      "shufps    $0x88,%%xmm6,%%xmm2             \n"
      "shufps    $0xdd,%%xmm6,%%xmm7             \n"
      "pavgb     %%xmm7,%%xmm2                   \n"
      "movdqa    %%xmm0,%%xmm1                   \n"
      "movdqa    %%xmm2,%%xmm6                   \n"
      "pmaddubsw %%xmm4,%%xmm0                   \n"
      "pmaddubsw %%xmm4,%%xmm2                   \n"
      "pmaddubsw %%xmm3,%%xmm1                   \n"
      "pmaddubsw %%xmm3,%%xmm6                   \n"
      "phaddw    %%xmm2,%%xmm0                   \n"
      "phaddw    %%xmm6,%%xmm1                   \n"
      "psraw     $0x8,%%xmm0                     \n"
      "psraw     $0x8,%%xmm1                     \n"
      "packsswb  %%xmm1,%%xmm0                   \n"
      "paddb     %%xmm5,%%xmm0                   \n"
      "movlps    %%xmm0,(%1)                     \n"
      "movhps    %%xmm0,0x00(%1,%2,1)            \n"
      "lea       0x8(%1),%1                      \n"
      "sub       $0x10,%3                        \n"
      "jg        1b                              \n"
      : "+r"(src_argb0),                   // %0
        "+r"(dst_u),                       // %1
        "+r"(dst_v),                       // %2
        "+rm"(width)                       // %3
      : "r"((intptr_t)(src_stride_argb)),  // %4
        "m"(kARGBToV),                     // %5
        "m"(kARGBToU),                     // %6
        "m"(kAddUV128)                     // %7
      : "memory", "cc", "xmm0", "xmm1", "xmm2", "xmm6", "xmm7");
1261
}
1262
#endif  // HAS_ARGBTOUVROW_SSSE3
1263

1264 1265 1266
#ifdef HAS_ARGBTOUVROW_AVX2
// vpshufb for vphaddw + vpackuswb packed to shorts.
static const lvec8 kShufARGBToUV_AVX = {
1267 1268
    0, 1, 8, 9, 2, 3, 10, 11, 4, 5, 12, 13, 6, 7, 14, 15,
    0, 1, 8, 9, 2, 3, 10, 11, 4, 5, 12, 13, 6, 7, 14, 15};
Frank Barchard's avatar
Frank Barchard committed
1269
void ARGBToUVRow_AVX2(const uint8_t* src_argb0,
1270
                      int src_stride_argb,
Frank Barchard's avatar
Frank Barchard committed
1271 1272
                      uint8_t* dst_u,
                      uint8_t* dst_v,
1273
                      int width) {
1274 1275 1276 1277 1278
  asm volatile(
      "vbroadcastf128 %5,%%ymm5                  \n"
      "vbroadcastf128 %6,%%ymm6                  \n"
      "vbroadcastf128 %7,%%ymm7                  \n"
      "sub        %1,%2                          \n"
Frank Barchard's avatar
Frank Barchard committed
1279

1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327
      LABELALIGN
      "1:                                        \n"
      "vmovdqu    (%0),%%ymm0                    \n"
      "vmovdqu    0x20(%0),%%ymm1                \n"
      "vmovdqu    0x40(%0),%%ymm2                \n"
      "vmovdqu    0x60(%0),%%ymm3                \n"
      "vpavgb    0x00(%0,%4,1),%%ymm0,%%ymm0     \n"
      "vpavgb    0x20(%0,%4,1),%%ymm1,%%ymm1     \n"
      "vpavgb    0x40(%0,%4,1),%%ymm2,%%ymm2     \n"
      "vpavgb    0x60(%0,%4,1),%%ymm3,%%ymm3     \n"
      "lea        0x80(%0),%0                    \n"
      "vshufps    $0x88,%%ymm1,%%ymm0,%%ymm4     \n"
      "vshufps    $0xdd,%%ymm1,%%ymm0,%%ymm0     \n"
      "vpavgb     %%ymm4,%%ymm0,%%ymm0           \n"
      "vshufps    $0x88,%%ymm3,%%ymm2,%%ymm4     \n"
      "vshufps    $0xdd,%%ymm3,%%ymm2,%%ymm2     \n"
      "vpavgb     %%ymm4,%%ymm2,%%ymm2           \n"

      "vpmaddubsw %%ymm7,%%ymm0,%%ymm1           \n"
      "vpmaddubsw %%ymm7,%%ymm2,%%ymm3           \n"
      "vpmaddubsw %%ymm6,%%ymm0,%%ymm0           \n"
      "vpmaddubsw %%ymm6,%%ymm2,%%ymm2           \n"
      "vphaddw    %%ymm3,%%ymm1,%%ymm1           \n"
      "vphaddw    %%ymm2,%%ymm0,%%ymm0           \n"
      "vpsraw     $0x8,%%ymm1,%%ymm1             \n"
      "vpsraw     $0x8,%%ymm0,%%ymm0             \n"
      "vpacksswb  %%ymm0,%%ymm1,%%ymm0           \n"
      "vpermq     $0xd8,%%ymm0,%%ymm0            \n"
      "vpshufb    %8,%%ymm0,%%ymm0               \n"
      "vpaddb     %%ymm5,%%ymm0,%%ymm0           \n"

      "vextractf128 $0x0,%%ymm0,(%1)             \n"
      "vextractf128 $0x1,%%ymm0,0x0(%1,%2,1)     \n"
      "lea        0x10(%1),%1                    \n"
      "sub        $0x20,%3                       \n"
      "jg         1b                             \n"
      "vzeroupper                                \n"
      : "+r"(src_argb0),                   // %0
        "+r"(dst_u),                       // %1
        "+r"(dst_v),                       // %2
        "+rm"(width)                       // %3
      : "r"((intptr_t)(src_stride_argb)),  // %4
        "m"(kAddUV128),                    // %5
        "m"(kARGBToV),                     // %6
        "m"(kARGBToU),                     // %7
        "m"(kShufARGBToUV_AVX)             // %8
      : "memory", "cc", "xmm0", "xmm1", "xmm2", "xmm3", "xmm4", "xmm5", "xmm6",
        "xmm7");
1328 1329 1330
}
#endif  // HAS_ARGBTOUVROW_AVX2

1331
#ifdef HAS_ARGBTOUVJROW_AVX2
Frank Barchard's avatar
Frank Barchard committed
1332
void ARGBToUVJRow_AVX2(const uint8_t* src_argb0,
1333
                       int src_stride_argb,
Frank Barchard's avatar
Frank Barchard committed
1334 1335
                       uint8_t* dst_u,
                       uint8_t* dst_v,
1336
                       int width) {
1337 1338 1339 1340 1341
  asm volatile(
      "vbroadcastf128 %5,%%ymm5                  \n"
      "vbroadcastf128 %6,%%ymm6                  \n"
      "vbroadcastf128 %7,%%ymm7                  \n"
      "sub        %1,%2                          \n"
Frank Barchard's avatar
Frank Barchard committed
1342

1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391
      LABELALIGN
      "1:                                        \n"
      "vmovdqu    (%0),%%ymm0                    \n"
      "vmovdqu    0x20(%0),%%ymm1                \n"
      "vmovdqu    0x40(%0),%%ymm2                \n"
      "vmovdqu    0x60(%0),%%ymm3                \n"
      "vpavgb    0x00(%0,%4,1),%%ymm0,%%ymm0     \n"
      "vpavgb    0x20(%0,%4,1),%%ymm1,%%ymm1     \n"
      "vpavgb    0x40(%0,%4,1),%%ymm2,%%ymm2     \n"
      "vpavgb    0x60(%0,%4,1),%%ymm3,%%ymm3     \n"
      "lea       0x80(%0),%0                     \n"
      "vshufps    $0x88,%%ymm1,%%ymm0,%%ymm4     \n"
      "vshufps    $0xdd,%%ymm1,%%ymm0,%%ymm0     \n"
      "vpavgb     %%ymm4,%%ymm0,%%ymm0           \n"
      "vshufps    $0x88,%%ymm3,%%ymm2,%%ymm4     \n"
      "vshufps    $0xdd,%%ymm3,%%ymm2,%%ymm2     \n"
      "vpavgb     %%ymm4,%%ymm2,%%ymm2           \n"

      "vpmaddubsw %%ymm7,%%ymm0,%%ymm1           \n"
      "vpmaddubsw %%ymm7,%%ymm2,%%ymm3           \n"
      "vpmaddubsw %%ymm6,%%ymm0,%%ymm0           \n"
      "vpmaddubsw %%ymm6,%%ymm2,%%ymm2           \n"
      "vphaddw    %%ymm3,%%ymm1,%%ymm1           \n"
      "vphaddw    %%ymm2,%%ymm0,%%ymm0           \n"
      "vpaddw     %%ymm5,%%ymm0,%%ymm0           \n"
      "vpaddw     %%ymm5,%%ymm1,%%ymm1           \n"
      "vpsraw     $0x8,%%ymm1,%%ymm1             \n"
      "vpsraw     $0x8,%%ymm0,%%ymm0             \n"
      "vpacksswb  %%ymm0,%%ymm1,%%ymm0           \n"
      "vpermq     $0xd8,%%ymm0,%%ymm0            \n"
      "vpshufb    %8,%%ymm0,%%ymm0               \n"

      "vextractf128 $0x0,%%ymm0,(%1)             \n"
      "vextractf128 $0x1,%%ymm0,0x0(%1,%2,1)     \n"
      "lea       0x10(%1),%1                     \n"
      "sub       $0x20,%3                        \n"
      "jg        1b                              \n"
      "vzeroupper                                \n"
      : "+r"(src_argb0),                   // %0
        "+r"(dst_u),                       // %1
        "+r"(dst_v),                       // %2
        "+rm"(width)                       // %3
      : "r"((intptr_t)(src_stride_argb)),  // %4
        "m"(kAddUVJ128),                   // %5
        "m"(kARGBToVJ),                    // %6
        "m"(kARGBToUJ),                    // %7
        "m"(kShufARGBToUV_AVX)             // %8
      : "memory", "cc", "xmm0", "xmm1", "xmm2", "xmm3", "xmm4", "xmm5", "xmm6",
        "xmm7");
1392 1393 1394
}
#endif  // HAS_ARGBTOUVJROW_AVX2

1395
#ifdef HAS_ARGBTOUVJROW_SSSE3
Frank Barchard's avatar
Frank Barchard committed
1396
void ARGBToUVJRow_SSSE3(const uint8_t* src_argb0,
1397
                        int src_stride_argb,
Frank Barchard's avatar
Frank Barchard committed
1398 1399
                        uint8_t* dst_u,
                        uint8_t* dst_v,
1400
                        int width) {
1401 1402 1403 1404 1405
  asm volatile(
      "movdqa    %5,%%xmm3                       \n"
      "movdqa    %6,%%xmm4                       \n"
      "movdqa    %7,%%xmm5                       \n"
      "sub       %1,%2                           \n"
Frank Barchard's avatar
Frank Barchard committed
1406

1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458
      LABELALIGN
      "1:                                        \n"
      "movdqu    (%0),%%xmm0                     \n"
      "movdqu    0x00(%0,%4,1),%%xmm7            \n"
      "pavgb     %%xmm7,%%xmm0                   \n"
      "movdqu    0x10(%0),%%xmm1                 \n"
      "movdqu    0x10(%0,%4,1),%%xmm7            \n"
      "pavgb     %%xmm7,%%xmm1                   \n"
      "movdqu    0x20(%0),%%xmm2                 \n"
      "movdqu    0x20(%0,%4,1),%%xmm7            \n"
      "pavgb     %%xmm7,%%xmm2                   \n"
      "movdqu    0x30(%0),%%xmm6                 \n"
      "movdqu    0x30(%0,%4,1),%%xmm7            \n"
      "pavgb     %%xmm7,%%xmm6                   \n"

      "lea       0x40(%0),%0                     \n"
      "movdqa    %%xmm0,%%xmm7                   \n"
      "shufps    $0x88,%%xmm1,%%xmm0             \n"
      "shufps    $0xdd,%%xmm1,%%xmm7             \n"
      "pavgb     %%xmm7,%%xmm0                   \n"
      "movdqa    %%xmm2,%%xmm7                   \n"
      "shufps    $0x88,%%xmm6,%%xmm2             \n"
      "shufps    $0xdd,%%xmm6,%%xmm7             \n"
      "pavgb     %%xmm7,%%xmm2                   \n"
      "movdqa    %%xmm0,%%xmm1                   \n"
      "movdqa    %%xmm2,%%xmm6                   \n"
      "pmaddubsw %%xmm4,%%xmm0                   \n"
      "pmaddubsw %%xmm4,%%xmm2                   \n"
      "pmaddubsw %%xmm3,%%xmm1                   \n"
      "pmaddubsw %%xmm3,%%xmm6                   \n"
      "phaddw    %%xmm2,%%xmm0                   \n"
      "phaddw    %%xmm6,%%xmm1                   \n"
      "paddw     %%xmm5,%%xmm0                   \n"
      "paddw     %%xmm5,%%xmm1                   \n"
      "psraw     $0x8,%%xmm0                     \n"
      "psraw     $0x8,%%xmm1                     \n"
      "packsswb  %%xmm1,%%xmm0                   \n"
      "movlps    %%xmm0,(%1)                     \n"
      "movhps    %%xmm0,0x00(%1,%2,1)            \n"
      "lea       0x8(%1),%1                      \n"
      "sub       $0x10,%3                        \n"
      "jg        1b                              \n"
      : "+r"(src_argb0),                   // %0
        "+r"(dst_u),                       // %1
        "+r"(dst_v),                       // %2
        "+rm"(width)                       // %3
      : "r"((intptr_t)(src_stride_argb)),  // %4
        "m"(kARGBToVJ),                    // %5
        "m"(kARGBToUJ),                    // %6
        "m"(kAddUVJ128)                    // %7
      : "memory", "cc", "xmm0", "xmm1", "xmm2", "xmm6", "xmm7");
}
1459
#endif  // HAS_ARGBTOUVJROW_SSSE3
1460

1461
#ifdef HAS_ARGBTOUV444ROW_SSSE3
Frank Barchard's avatar
Frank Barchard committed
1462 1463 1464
void ARGBToUV444Row_SSSE3(const uint8_t* src_argb,
                          uint8_t* dst_u,
                          uint8_t* dst_v,
1465
                          int width) {
1466 1467 1468 1469 1470
  asm volatile(
      "movdqa    %4,%%xmm3                       \n"
      "movdqa    %5,%%xmm4                       \n"
      "movdqa    %6,%%xmm5                       \n"
      "sub       %1,%2                           \n"
Frank Barchard's avatar
Frank Barchard committed
1471

1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515
      LABELALIGN
      "1:                                        \n"
      "movdqu    (%0),%%xmm0                     \n"
      "movdqu    0x10(%0),%%xmm1                 \n"
      "movdqu    0x20(%0),%%xmm2                 \n"
      "movdqu    0x30(%0),%%xmm6                 \n"
      "pmaddubsw %%xmm4,%%xmm0                   \n"
      "pmaddubsw %%xmm4,%%xmm1                   \n"
      "pmaddubsw %%xmm4,%%xmm2                   \n"
      "pmaddubsw %%xmm4,%%xmm6                   \n"
      "phaddw    %%xmm1,%%xmm0                   \n"
      "phaddw    %%xmm6,%%xmm2                   \n"
      "psraw     $0x8,%%xmm0                     \n"
      "psraw     $0x8,%%xmm2                     \n"
      "packsswb  %%xmm2,%%xmm0                   \n"
      "paddb     %%xmm5,%%xmm0                   \n"
      "movdqu    %%xmm0,(%1)                     \n"
      "movdqu    (%0),%%xmm0                     \n"
      "movdqu    0x10(%0),%%xmm1                 \n"
      "movdqu    0x20(%0),%%xmm2                 \n"
      "movdqu    0x30(%0),%%xmm6                 \n"
      "pmaddubsw %%xmm3,%%xmm0                   \n"
      "pmaddubsw %%xmm3,%%xmm1                   \n"
      "pmaddubsw %%xmm3,%%xmm2                   \n"
      "pmaddubsw %%xmm3,%%xmm6                   \n"
      "phaddw    %%xmm1,%%xmm0                   \n"
      "phaddw    %%xmm6,%%xmm2                   \n"
      "psraw     $0x8,%%xmm0                     \n"
      "psraw     $0x8,%%xmm2                     \n"
      "packsswb  %%xmm2,%%xmm0                   \n"
      "paddb     %%xmm5,%%xmm0                   \n"
      "lea       0x40(%0),%0                     \n"
      "movdqu    %%xmm0,0x00(%1,%2,1)            \n"
      "lea       0x10(%1),%1                     \n"
      "sub       $0x10,%3                        \n"
      "jg        1b                              \n"
      : "+r"(src_argb),  // %0
        "+r"(dst_u),     // %1
        "+r"(dst_v),     // %2
        "+rm"(width)     // %3
      : "m"(kARGBToV),   // %4
        "m"(kARGBToU),   // %5
        "m"(kAddUV128)   // %6
      : "memory", "cc", "xmm0", "xmm1", "xmm2", "xmm6");
1516
}
1517
#endif  // HAS_ARGBTOUV444ROW_SSSE3
1518

Frank Barchard's avatar
Frank Barchard committed
1519
void BGRAToYRow_SSSE3(const uint8_t* src_bgra, uint8_t* dst_y, int width) {
1520 1521 1522
  asm volatile(
      "movdqa    %4,%%xmm5                       \n"
      "movdqa    %3,%%xmm4                       \n"
Frank Barchard's avatar
Frank Barchard committed
1523

1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550
      LABELALIGN
      "1:                                        \n"
      "movdqu    (%0),%%xmm0                     \n"
      "movdqu    0x10(%0),%%xmm1                 \n"
      "movdqu    0x20(%0),%%xmm2                 \n"
      "movdqu    0x30(%0),%%xmm3                 \n"
      "pmaddubsw %%xmm4,%%xmm0                   \n"
      "pmaddubsw %%xmm4,%%xmm1                   \n"
      "pmaddubsw %%xmm4,%%xmm2                   \n"
      "pmaddubsw %%xmm4,%%xmm3                   \n"
      "lea       0x40(%0),%0                     \n"
      "phaddw    %%xmm1,%%xmm0                   \n"
      "phaddw    %%xmm3,%%xmm2                   \n"
      "psrlw     $0x7,%%xmm0                     \n"
      "psrlw     $0x7,%%xmm2                     \n"
      "packuswb  %%xmm2,%%xmm0                   \n"
      "paddb     %%xmm5,%%xmm0                   \n"
      "movdqu    %%xmm0,(%1)                     \n"
      "lea       0x10(%1),%1                     \n"
      "sub       $0x10,%2                        \n"
      "jg        1b                              \n"
      : "+r"(src_bgra),  // %0
        "+r"(dst_y),     // %1
        "+r"(width)      // %2
      : "m"(kBGRAToY),   // %3
        "m"(kAddY16)     // %4
      : "memory", "cc", "xmm0", "xmm1", "xmm2", "xmm3", "xmm4", "xmm5");
1551 1552
}

Frank Barchard's avatar
Frank Barchard committed
1553
void BGRAToUVRow_SSSE3(const uint8_t* src_bgra0,
1554
                       int src_stride_bgra,
Frank Barchard's avatar
Frank Barchard committed
1555 1556
                       uint8_t* dst_u,
                       uint8_t* dst_v,
1557
                       int width) {
1558 1559 1560 1561 1562
  asm volatile(
      "movdqa    %5,%%xmm3                       \n"
      "movdqa    %6,%%xmm4                       \n"
      "movdqa    %7,%%xmm5                       \n"
      "sub       %1,%2                           \n"
Frank Barchard's avatar
Frank Barchard committed
1563

1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613
      LABELALIGN
      "1:                                        \n"
      "movdqu    (%0),%%xmm0                     \n"
      "movdqu    0x00(%0,%4,1),%%xmm7            \n"
      "pavgb     %%xmm7,%%xmm0                   \n"
      "movdqu    0x10(%0),%%xmm1                 \n"
      "movdqu    0x10(%0,%4,1),%%xmm7            \n"
      "pavgb     %%xmm7,%%xmm1                   \n"
      "movdqu    0x20(%0),%%xmm2                 \n"
      "movdqu    0x20(%0,%4,1),%%xmm7            \n"
      "pavgb     %%xmm7,%%xmm2                   \n"
      "movdqu    0x30(%0),%%xmm6                 \n"
      "movdqu    0x30(%0,%4,1),%%xmm7            \n"
      "pavgb     %%xmm7,%%xmm6                   \n"

      "lea       0x40(%0),%0                     \n"
      "movdqa    %%xmm0,%%xmm7                   \n"
      "shufps    $0x88,%%xmm1,%%xmm0             \n"
      "shufps    $0xdd,%%xmm1,%%xmm7             \n"
      "pavgb     %%xmm7,%%xmm0                   \n"
      "movdqa    %%xmm2,%%xmm7                   \n"
      "shufps    $0x88,%%xmm6,%%xmm2             \n"
      "shufps    $0xdd,%%xmm6,%%xmm7             \n"
      "pavgb     %%xmm7,%%xmm2                   \n"
      "movdqa    %%xmm0,%%xmm1                   \n"
      "movdqa    %%xmm2,%%xmm6                   \n"
      "pmaddubsw %%xmm4,%%xmm0                   \n"
      "pmaddubsw %%xmm4,%%xmm2                   \n"
      "pmaddubsw %%xmm3,%%xmm1                   \n"
      "pmaddubsw %%xmm3,%%xmm6                   \n"
      "phaddw    %%xmm2,%%xmm0                   \n"
      "phaddw    %%xmm6,%%xmm1                   \n"
      "psraw     $0x8,%%xmm0                     \n"
      "psraw     $0x8,%%xmm1                     \n"
      "packsswb  %%xmm1,%%xmm0                   \n"
      "paddb     %%xmm5,%%xmm0                   \n"
      "movlps    %%xmm0,(%1)                     \n"
      "movhps    %%xmm0,0x00(%1,%2,1)            \n"
      "lea       0x8(%1),%1                      \n"
      "sub       $0x10,%3                        \n"
      "jg        1b                              \n"
      : "+r"(src_bgra0),                   // %0
        "+r"(dst_u),                       // %1
        "+r"(dst_v),                       // %2
        "+rm"(width)                       // %3
      : "r"((intptr_t)(src_stride_bgra)),  // %4
        "m"(kBGRAToV),                     // %5
        "m"(kBGRAToU),                     // %6
        "m"(kAddUV128)                     // %7
      : "memory", "cc", "xmm0", "xmm1", "xmm2", "xmm6", "xmm7");
1614 1615
}

Frank Barchard's avatar
Frank Barchard committed
1616
void ABGRToYRow_SSSE3(const uint8_t* src_abgr, uint8_t* dst_y, int width) {
1617 1618 1619
  asm volatile(
      "movdqa    %4,%%xmm5                       \n"
      "movdqa    %3,%%xmm4                       \n"
Frank Barchard's avatar
Frank Barchard committed
1620

1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647
      LABELALIGN
      "1:                                        \n"
      "movdqu    (%0),%%xmm0                     \n"
      "movdqu    0x10(%0),%%xmm1                 \n"
      "movdqu    0x20(%0),%%xmm2                 \n"
      "movdqu    0x30(%0),%%xmm3                 \n"
      "pmaddubsw %%xmm4,%%xmm0                   \n"
      "pmaddubsw %%xmm4,%%xmm1                   \n"
      "pmaddubsw %%xmm4,%%xmm2                   \n"
      "pmaddubsw %%xmm4,%%xmm3                   \n"
      "lea       0x40(%0),%0                     \n"
      "phaddw    %%xmm1,%%xmm0                   \n"
      "phaddw    %%xmm3,%%xmm2                   \n"
      "psrlw     $0x7,%%xmm0                     \n"
      "psrlw     $0x7,%%xmm2                     \n"
      "packuswb  %%xmm2,%%xmm0                   \n"
      "paddb     %%xmm5,%%xmm0                   \n"
      "movdqu    %%xmm0,(%1)                     \n"
      "lea       0x10(%1),%1                     \n"
      "sub       $0x10,%2                        \n"
      "jg        1b                              \n"
      : "+r"(src_abgr),  // %0
        "+r"(dst_y),     // %1
        "+r"(width)      // %2
      : "m"(kABGRToY),   // %3
        "m"(kAddY16)     // %4
      : "memory", "cc", "xmm0", "xmm1", "xmm2", "xmm3", "xmm4", "xmm5");
1648 1649
}

Frank Barchard's avatar
Frank Barchard committed
1650
void RGBAToYRow_SSSE3(const uint8_t* src_rgba, uint8_t* dst_y, int width) {
1651 1652 1653
  asm volatile(
      "movdqa    %4,%%xmm5                       \n"
      "movdqa    %3,%%xmm4                       \n"
Frank Barchard's avatar
Frank Barchard committed
1654

1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681
      LABELALIGN
      "1:                                        \n"
      "movdqu    (%0),%%xmm0                     \n"
      "movdqu    0x10(%0),%%xmm1                 \n"
      "movdqu    0x20(%0),%%xmm2                 \n"
      "movdqu    0x30(%0),%%xmm3                 \n"
      "pmaddubsw %%xmm4,%%xmm0                   \n"
      "pmaddubsw %%xmm4,%%xmm1                   \n"
      "pmaddubsw %%xmm4,%%xmm2                   \n"
      "pmaddubsw %%xmm4,%%xmm3                   \n"
      "lea       0x40(%0),%0                     \n"
      "phaddw    %%xmm1,%%xmm0                   \n"
      "phaddw    %%xmm3,%%xmm2                   \n"
      "psrlw     $0x7,%%xmm0                     \n"
      "psrlw     $0x7,%%xmm2                     \n"
      "packuswb  %%xmm2,%%xmm0                   \n"
      "paddb     %%xmm5,%%xmm0                   \n"
      "movdqu    %%xmm0,(%1)                     \n"
      "lea       0x10(%1),%1                     \n"
      "sub       $0x10,%2                        \n"
      "jg        1b                              \n"
      : "+r"(src_rgba),  // %0
        "+r"(dst_y),     // %1
        "+r"(width)      // %2
      : "m"(kRGBAToY),   // %3
        "m"(kAddY16)     // %4
      : "memory", "cc", "xmm0", "xmm1", "xmm2", "xmm3", "xmm4", "xmm5");
1682
}
1683

Frank Barchard's avatar
Frank Barchard committed
1684
void ABGRToUVRow_SSSE3(const uint8_t* src_abgr0,
1685
                       int src_stride_abgr,
Frank Barchard's avatar
Frank Barchard committed
1686 1687
                       uint8_t* dst_u,
                       uint8_t* dst_v,
1688
                       int width) {
1689 1690 1691 1692 1693
  asm volatile(
      "movdqa    %5,%%xmm3                       \n"
      "movdqa    %6,%%xmm4                       \n"
      "movdqa    %7,%%xmm5                       \n"
      "sub       %1,%2                           \n"
Frank Barchard's avatar
Frank Barchard committed
1694

1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744
      LABELALIGN
      "1:                                        \n"
      "movdqu    (%0),%%xmm0                     \n"
      "movdqu    0x00(%0,%4,1),%%xmm7            \n"
      "pavgb     %%xmm7,%%xmm0                   \n"
      "movdqu    0x10(%0),%%xmm1                 \n"
      "movdqu    0x10(%0,%4,1),%%xmm7            \n"
      "pavgb     %%xmm7,%%xmm1                   \n"
      "movdqu    0x20(%0),%%xmm2                 \n"
      "movdqu    0x20(%0,%4,1),%%xmm7            \n"
      "pavgb     %%xmm7,%%xmm2                   \n"
      "movdqu    0x30(%0),%%xmm6                 \n"
      "movdqu    0x30(%0,%4,1),%%xmm7            \n"
      "pavgb     %%xmm7,%%xmm6                   \n"

      "lea       0x40(%0),%0                     \n"
      "movdqa    %%xmm0,%%xmm7                   \n"
      "shufps    $0x88,%%xmm1,%%xmm0             \n"
      "shufps    $0xdd,%%xmm1,%%xmm7             \n"
      "pavgb     %%xmm7,%%xmm0                   \n"
      "movdqa    %%xmm2,%%xmm7                   \n"
      "shufps    $0x88,%%xmm6,%%xmm2             \n"
      "shufps    $0xdd,%%xmm6,%%xmm7             \n"
      "pavgb     %%xmm7,%%xmm2                   \n"
      "movdqa    %%xmm0,%%xmm1                   \n"
      "movdqa    %%xmm2,%%xmm6                   \n"
      "pmaddubsw %%xmm4,%%xmm0                   \n"
      "pmaddubsw %%xmm4,%%xmm2                   \n"
      "pmaddubsw %%xmm3,%%xmm1                   \n"
      "pmaddubsw %%xmm3,%%xmm6                   \n"
      "phaddw    %%xmm2,%%xmm0                   \n"
      "phaddw    %%xmm6,%%xmm1                   \n"
      "psraw     $0x8,%%xmm0                     \n"
      "psraw     $0x8,%%xmm1                     \n"
      "packsswb  %%xmm1,%%xmm0                   \n"
      "paddb     %%xmm5,%%xmm0                   \n"
      "movlps    %%xmm0,(%1)                     \n"
      "movhps    %%xmm0,0x00(%1,%2,1)            \n"
      "lea       0x8(%1),%1                      \n"
      "sub       $0x10,%3                        \n"
      "jg        1b                              \n"
      : "+r"(src_abgr0),                   // %0
        "+r"(dst_u),                       // %1
        "+r"(dst_v),                       // %2
        "+rm"(width)                       // %3
      : "r"((intptr_t)(src_stride_abgr)),  // %4
        "m"(kABGRToV),                     // %5
        "m"(kABGRToU),                     // %6
        "m"(kAddUV128)                     // %7
      : "memory", "cc", "xmm0", "xmm1", "xmm2", "xmm6", "xmm7");
1745 1746
}

Frank Barchard's avatar
Frank Barchard committed
1747
void RGBAToUVRow_SSSE3(const uint8_t* src_rgba0,
1748
                       int src_stride_rgba,
Frank Barchard's avatar
Frank Barchard committed
1749 1750
                       uint8_t* dst_u,
                       uint8_t* dst_v,
1751
                       int width) {
1752 1753 1754 1755 1756
  asm volatile(
      "movdqa    %5,%%xmm3                       \n"
      "movdqa    %6,%%xmm4                       \n"
      "movdqa    %7,%%xmm5                       \n"
      "sub       %1,%2                           \n"
Frank Barchard's avatar
Frank Barchard committed
1757

1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807
      LABELALIGN
      "1:                                        \n"
      "movdqu    (%0),%%xmm0                     \n"
      "movdqu    0x00(%0,%4,1),%%xmm7            \n"
      "pavgb     %%xmm7,%%xmm0                   \n"
      "movdqu    0x10(%0),%%xmm1                 \n"
      "movdqu    0x10(%0,%4,1),%%xmm7            \n"
      "pavgb     %%xmm7,%%xmm1                   \n"
      "movdqu    0x20(%0),%%xmm2                 \n"
      "movdqu    0x20(%0,%4,1),%%xmm7            \n"
      "pavgb     %%xmm7,%%xmm2                   \n"
      "movdqu    0x30(%0),%%xmm6                 \n"
      "movdqu    0x30(%0,%4,1),%%xmm7            \n"
      "pavgb     %%xmm7,%%xmm6                   \n"

      "lea       0x40(%0),%0                     \n"
      "movdqa    %%xmm0,%%xmm7                   \n"
      "shufps    $0x88,%%xmm1,%%xmm0             \n"
      "shufps    $0xdd,%%xmm1,%%xmm7             \n"
      "pavgb     %%xmm7,%%xmm0                   \n"
      "movdqa    %%xmm2,%%xmm7                   \n"
      "shufps    $0x88,%%xmm6,%%xmm2             \n"
      "shufps    $0xdd,%%xmm6,%%xmm7             \n"
      "pavgb     %%xmm7,%%xmm2                   \n"
      "movdqa    %%xmm0,%%xmm1                   \n"
      "movdqa    %%xmm2,%%xmm6                   \n"
      "pmaddubsw %%xmm4,%%xmm0                   \n"
      "pmaddubsw %%xmm4,%%xmm2                   \n"
      "pmaddubsw %%xmm3,%%xmm1                   \n"
      "pmaddubsw %%xmm3,%%xmm6                   \n"
      "phaddw    %%xmm2,%%xmm0                   \n"
      "phaddw    %%xmm6,%%xmm1                   \n"
      "psraw     $0x8,%%xmm0                     \n"
      "psraw     $0x8,%%xmm1                     \n"
      "packsswb  %%xmm1,%%xmm0                   \n"
      "paddb     %%xmm5,%%xmm0                   \n"
      "movlps    %%xmm0,(%1)                     \n"
      "movhps    %%xmm0,0x00(%1,%2,1)            \n"
      "lea       0x8(%1),%1                      \n"
      "sub       $0x10,%3                        \n"
      "jg        1b                              \n"
      : "+r"(src_rgba0),                   // %0
        "+r"(dst_u),                       // %1
        "+r"(dst_v),                       // %2
        "+rm"(width)                       // %3
      : "r"((intptr_t)(src_stride_rgba)),  // %4
        "m"(kRGBAToV),                     // %5
        "m"(kRGBAToU),                     // %6
        "m"(kAddUV128)                     // %7
      : "memory", "cc", "xmm0", "xmm1", "xmm2", "xmm6", "xmm7");
1808
}
1809

1810
#if defined(HAS_I422TOARGBROW_SSSE3) || defined(HAS_I422TOARGBROW_AVX2)
1811

1812
// Read 8 UV from 444
1813 1814 1815 1816 1817 1818 1819 1820
#define READYUV444                                                \
  "movq       (%[u_buf]),%%xmm0                               \n" \
  "movq       0x00(%[u_buf],%[v_buf],1),%%xmm1                \n" \
  "lea        0x8(%[u_buf]),%[u_buf]                          \n" \
  "punpcklbw  %%xmm1,%%xmm0                                   \n" \
  "movq       (%[y_buf]),%%xmm4                               \n" \
  "punpcklbw  %%xmm4,%%xmm4                                   \n" \
  "lea        0x8(%[y_buf]),%[y_buf]                          \n"
1821

1822
// Read 4 UV from 422, upsample to 8 UV
1823 1824 1825 1826 1827 1828 1829 1830 1831
#define READYUV422                                                \
  "movd       (%[u_buf]),%%xmm0                               \n" \
  "movd       0x00(%[u_buf],%[v_buf],1),%%xmm1                \n" \
  "lea        0x4(%[u_buf]),%[u_buf]                          \n" \
  "punpcklbw  %%xmm1,%%xmm0                                   \n" \
  "punpcklwd  %%xmm0,%%xmm0                                   \n" \
  "movq       (%[y_buf]),%%xmm4                               \n" \
  "punpcklbw  %%xmm4,%%xmm4                                   \n" \
  "lea        0x8(%[y_buf]),%[y_buf]                          \n"
1832

1833 1834
// Read 4 UV from 422 10 bit, upsample to 8 UV
// TODO(fbarchard): Consider shufb to replace pack/unpack
1835 1836
// TODO(fbarchard): Consider pmulhuw to replace psraw
// TODO(fbarchard): Consider pmullw to replace psllw and allow different bits.
1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847
#define READYUV210                                                \
  "movq       (%[u_buf]),%%xmm0                               \n" \
  "movq       0x00(%[u_buf],%[v_buf],1),%%xmm1                \n" \
  "lea        0x8(%[u_buf]),%[u_buf]                          \n" \
  "punpcklwd  %%xmm1,%%xmm0                                   \n" \
  "psraw      $0x2,%%xmm0                                     \n" \
  "packuswb   %%xmm0,%%xmm0                                   \n" \
  "punpcklwd  %%xmm0,%%xmm0                                   \n" \
  "movdqu     (%[y_buf]),%%xmm4                               \n" \
  "psllw      $0x6,%%xmm4                                     \n" \
  "lea        0x10(%[y_buf]),%[y_buf]                         \n"
1848

1849
// Read 4 UV from 422, upsample to 8 UV.  With 8 Alpha.
1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860
#define READYUVA422                                               \
  "movd       (%[u_buf]),%%xmm0                               \n" \
  "movd       0x00(%[u_buf],%[v_buf],1),%%xmm1                \n" \
  "lea        0x4(%[u_buf]),%[u_buf]                          \n" \
  "punpcklbw  %%xmm1,%%xmm0                                   \n" \
  "punpcklwd  %%xmm0,%%xmm0                                   \n" \
  "movq       (%[y_buf]),%%xmm4                               \n" \
  "punpcklbw  %%xmm4,%%xmm4                                   \n" \
  "lea        0x8(%[y_buf]),%[y_buf]                          \n" \
  "movq       (%[a_buf]),%%xmm5                               \n" \
  "lea        0x8(%[a_buf]),%[a_buf]                          \n"
1861

1862
// Read 4 UV from NV12, upsample to 8 UV
1863 1864 1865 1866 1867 1868 1869
#define READNV12                                                  \
  "movq       (%[uv_buf]),%%xmm0                              \n" \
  "lea        0x8(%[uv_buf]),%[uv_buf]                        \n" \
  "punpcklwd  %%xmm0,%%xmm0                                   \n" \
  "movq       (%[y_buf]),%%xmm4                               \n" \
  "punpcklbw  %%xmm4,%%xmm4                                   \n" \
  "lea        0x8(%[y_buf]),%[y_buf]                          \n"
1870

1871
// Read 4 VU from NV21, upsample to 8 UV
1872 1873 1874 1875 1876 1877 1878
#define READNV21                                                  \
  "movq       (%[vu_buf]),%%xmm0                              \n" \
  "lea        0x8(%[vu_buf]),%[vu_buf]                        \n" \
  "pshufb     %[kShuffleNV21], %%xmm0                         \n" \
  "movq       (%[y_buf]),%%xmm4                               \n" \
  "punpcklbw  %%xmm4,%%xmm4                                   \n" \
  "lea        0x8(%[y_buf]),%[y_buf]                          \n"
1879

1880
// Read 4 YUY2 with 8 Y and update 4 UV to 8 UV.
1881 1882 1883 1884 1885 1886
#define READYUY2                                                  \
  "movdqu     (%[yuy2_buf]),%%xmm4                            \n" \
  "pshufb     %[kShuffleYUY2Y], %%xmm4                        \n" \
  "movdqu     (%[yuy2_buf]),%%xmm0                            \n" \
  "pshufb     %[kShuffleYUY2UV], %%xmm0                       \n" \
  "lea        0x10(%[yuy2_buf]),%[yuy2_buf]                   \n"
1887 1888

// Read 4 UYVY with 8 Y and update 4 UV to 8 UV.
1889 1890 1891 1892 1893 1894
#define READUYVY                                                  \
  "movdqu     (%[uyvy_buf]),%%xmm4                            \n" \
  "pshufb     %[kShuffleUYVYY], %%xmm4                        \n" \
  "movdqu     (%[uyvy_buf]),%%xmm0                            \n" \
  "pshufb     %[kShuffleUYVYUV], %%xmm0                       \n" \
  "lea        0x10(%[uyvy_buf]),%[uyvy_buf]                   \n"
1895

1896
#if defined(__x86_64__)
1897 1898 1899 1900 1901 1902 1903 1904
#define YUVTORGB_SETUP(yuvconstants)                              \
  "movdqa     (%[yuvconstants]),%%xmm8                        \n" \
  "movdqa     32(%[yuvconstants]),%%xmm9                      \n" \
  "movdqa     64(%[yuvconstants]),%%xmm10                     \n" \
  "movdqa     96(%[yuvconstants]),%%xmm11                     \n" \
  "movdqa     128(%[yuvconstants]),%%xmm12                    \n" \
  "movdqa     160(%[yuvconstants]),%%xmm13                    \n" \
  "movdqa     192(%[yuvconstants]),%%xmm14                    \n"
1905
// Convert 8 pixels: 8 UV and 8 Y
1906
#define YUVTORGB16(yuvconstants)                                  \
1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921
  "movdqa     %%xmm0,%%xmm1                                   \n" \
  "movdqa     %%xmm0,%%xmm2                                   \n" \
  "movdqa     %%xmm0,%%xmm3                                   \n" \
  "movdqa     %%xmm11,%%xmm0                                  \n" \
  "pmaddubsw  %%xmm8,%%xmm1                                   \n" \
  "psubw      %%xmm1,%%xmm0                                   \n" \
  "movdqa     %%xmm12,%%xmm1                                  \n" \
  "pmaddubsw  %%xmm9,%%xmm2                                   \n" \
  "psubw      %%xmm2,%%xmm1                                   \n" \
  "movdqa     %%xmm13,%%xmm2                                  \n" \
  "pmaddubsw  %%xmm10,%%xmm3                                  \n" \
  "psubw      %%xmm3,%%xmm2                                   \n" \
  "pmulhuw    %%xmm14,%%xmm4                                  \n" \
  "paddsw     %%xmm4,%%xmm0                                   \n" \
  "paddsw     %%xmm4,%%xmm1                                   \n" \
1922
  "paddsw     %%xmm4,%%xmm2                                   \n"
1923
#define YUVTORGB_REGS \
1924
  "xmm8", "xmm9", "xmm10", "xmm11", "xmm12", "xmm13", "xmm14",
1925 1926 1927

#else
#define YUVTORGB_SETUP(yuvconstants)
1928
// Convert 8 pixels: 8 UV and 8 Y
1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944
#define YUVTORGB16(yuvconstants)                                  \
  "movdqa     %%xmm0,%%xmm1                                   \n" \
  "movdqa     %%xmm0,%%xmm2                                   \n" \
  "movdqa     %%xmm0,%%xmm3                                   \n" \
  "movdqa     96(%[yuvconstants]),%%xmm0                      \n" \
  "pmaddubsw  (%[yuvconstants]),%%xmm1                        \n" \
  "psubw      %%xmm1,%%xmm0                                   \n" \
  "movdqa     128(%[yuvconstants]),%%xmm1                     \n" \
  "pmaddubsw  32(%[yuvconstants]),%%xmm2                      \n" \
  "psubw      %%xmm2,%%xmm1                                   \n" \
  "movdqa     160(%[yuvconstants]),%%xmm2                     \n" \
  "pmaddubsw  64(%[yuvconstants]),%%xmm3                      \n" \
  "psubw      %%xmm3,%%xmm2                                   \n" \
  "pmulhuw    192(%[yuvconstants]),%%xmm4                     \n" \
  "paddsw     %%xmm4,%%xmm0                                   \n" \
  "paddsw     %%xmm4,%%xmm1                                   \n" \
1945
  "paddsw     %%xmm4,%%xmm2                                   \n"
1946 1947
#define YUVTORGB_REGS
#endif
1948

1949 1950
#define YUVTORGB(yuvconstants)                                    \
  YUVTORGB16(yuvconstants)                                        \
1951 1952 1953 1954 1955 1956 1957
  "psraw      $0x6,%%xmm0                                     \n" \
  "psraw      $0x6,%%xmm1                                     \n" \
  "psraw      $0x6,%%xmm2                                     \n" \
  "packuswb   %%xmm0,%%xmm0                                   \n" \
  "packuswb   %%xmm1,%%xmm1                                   \n" \
  "packuswb   %%xmm2,%%xmm2                                   \n"

1958
// Store 8 ARGB values.
1959 1960 1961 1962 1963 1964 1965 1966 1967
#define STOREARGB                                                  \
  "punpcklbw  %%xmm1,%%xmm0                                    \n" \
  "punpcklbw  %%xmm5,%%xmm2                                    \n" \
  "movdqa     %%xmm0,%%xmm1                                    \n" \
  "punpcklwd  %%xmm2,%%xmm0                                    \n" \
  "punpckhwd  %%xmm2,%%xmm1                                    \n" \
  "movdqu     %%xmm0,(%[dst_argb])                             \n" \
  "movdqu     %%xmm1,0x10(%[dst_argb])                         \n" \
  "lea        0x20(%[dst_argb]), %[dst_argb]                   \n"
1968

1969
// Store 8 RGBA values.
1970 1971 1972 1973 1974 1975 1976 1977 1978 1979
#define STORERGBA                                                  \
  "pcmpeqb   %%xmm5,%%xmm5                                     \n" \
  "punpcklbw %%xmm2,%%xmm1                                     \n" \
  "punpcklbw %%xmm0,%%xmm5                                     \n" \
  "movdqa    %%xmm5,%%xmm0                                     \n" \
  "punpcklwd %%xmm1,%%xmm5                                     \n" \
  "punpckhwd %%xmm1,%%xmm0                                     \n" \
  "movdqu    %%xmm5,(%[dst_rgba])                              \n" \
  "movdqu    %%xmm0,0x10(%[dst_rgba])                          \n" \
  "lea       0x20(%[dst_rgba]),%[dst_rgba]                     \n"
1980

1981
// Store 8 AR30 values.
1982
#define STOREAR30                                                  \
1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006
  "psraw      $0x4,%%xmm0                                      \n" \
  "psraw      $0x4,%%xmm1                                      \n" \
  "psraw      $0x4,%%xmm2                                      \n" \
  "pminsw     %%xmm7,%%xmm0                                    \n" \
  "pminsw     %%xmm7,%%xmm1                                    \n" \
  "pminsw     %%xmm7,%%xmm2                                    \n" \
  "pmaxsw     %%xmm6,%%xmm0                                    \n" \
  "pmaxsw     %%xmm6,%%xmm1                                    \n" \
  "pmaxsw     %%xmm6,%%xmm2                                    \n" \
  "psllw      $0x4,%%xmm2                                      \n" \
  "movdqa     %%xmm0,%%xmm3                                    \n" \
  "punpcklwd  %%xmm2,%%xmm0                                    \n" \
  "punpckhwd  %%xmm2,%%xmm3                                    \n" \
  "movdqa     %%xmm1,%%xmm2                                    \n" \
  "punpcklwd  %%xmm5,%%xmm1                                    \n" \
  "punpckhwd  %%xmm5,%%xmm2                                    \n" \
  "pslld      $0xa,%%xmm1                                      \n" \
  "pslld      $0xa,%%xmm2                                      \n" \
  "por        %%xmm1,%%xmm0                                    \n" \
  "por        %%xmm2,%%xmm3                                    \n" \
  "movdqu     %%xmm0,(%[dst_ar30])                             \n" \
  "movdqu     %%xmm3,0x10(%[dst_ar30])                         \n" \
  "lea        0x20(%[dst_ar30]), %[dst_ar30]                   \n"

Frank Barchard's avatar
Frank Barchard committed
2007 2008 2009 2010
void OMITFP I444ToARGBRow_SSSE3(const uint8_t* y_buf,
                                const uint8_t* u_buf,
                                const uint8_t* v_buf,
                                uint8_t* dst_argb,
2011
                                const struct YuvConstants* yuvconstants,
2012
                                int width) {
fbarchard@google.com's avatar
fbarchard@google.com committed
2013
  asm volatile (
2014
    YUVTORGB_SETUP(yuvconstants)
2015
    "sub       %[u_buf],%[v_buf]               \n"
2016
    "pcmpeqb   %%xmm5,%%xmm5                   \n"
Frank Barchard's avatar
Frank Barchard committed
2017

2018
    LABELALIGN
Frank Barchard's avatar
Frank Barchard committed
2019
    "1:                                        \n"
2020
    READYUV444
2021
    YUVTORGB(yuvconstants)
2022
    STOREARGB
2023
    "sub       $0x8,%[width]                   \n"
2024
    "jg        1b                              \n"
2025 2026 2027
  : [y_buf]"+r"(y_buf),    // %[y_buf]
    [u_buf]"+r"(u_buf),    // %[u_buf]
    [v_buf]"+r"(v_buf),    // %[v_buf]
2028
    [dst_argb]"+r"(dst_argb),  // %[dst_argb]
2029
    [width]"+rm"(width)    // %[width]
2030
  : [yuvconstants]"r"(yuvconstants)  // %[yuvconstants]
Frank Barchard's avatar
Frank Barchard committed
2031
  : "memory", "cc", YUVTORGB_REGS
2032
    "xmm0", "xmm1", "xmm2", "xmm3", "xmm4", "xmm5"
2033 2034 2035
  );
}

Frank Barchard's avatar
Frank Barchard committed
2036 2037 2038 2039
void OMITFP I422ToRGB24Row_SSSE3(const uint8_t* y_buf,
                                 const uint8_t* u_buf,
                                 const uint8_t* v_buf,
                                 uint8_t* dst_rgb24,
2040
                                 const struct YuvConstants* yuvconstants,
2041 2042
                                 int width) {
  asm volatile (
2043
    YUVTORGB_SETUP(yuvconstants)
2044 2045 2046
    "movdqa    %[kShuffleMaskARGBToRGB24_0],%%xmm5 \n"
    "movdqa    %[kShuffleMaskARGBToRGB24],%%xmm6   \n"
    "sub       %[u_buf],%[v_buf]               \n"
Frank Barchard's avatar
Frank Barchard committed
2047

2048
    LABELALIGN
Frank Barchard's avatar
Frank Barchard committed
2049
    "1:                                        \n"
2050
    READYUV422
2051
    YUVTORGB(yuvconstants)
2052
    "punpcklbw %%xmm1,%%xmm0                   \n"
2053
    "punpcklbw %%xmm2,%%xmm2                   \n"
2054 2055 2056
    "movdqa    %%xmm0,%%xmm1                   \n"
    "punpcklwd %%xmm2,%%xmm0                   \n"
    "punpckhwd %%xmm2,%%xmm1                   \n"
2057 2058 2059
    "pshufb    %%xmm5,%%xmm0                   \n"
    "pshufb    %%xmm6,%%xmm1                   \n"
    "palignr   $0xc,%%xmm0,%%xmm1              \n"
2060 2061 2062
    "movq      %%xmm0,(%[dst_rgb24])           \n"
    "movdqu    %%xmm1,0x8(%[dst_rgb24])        \n"
    "lea       0x18(%[dst_rgb24]),%[dst_rgb24] \n"
2063
    "subl      $0x8,%[width]                   \n"
2064
    "jg        1b                              \n"
2065
  : [y_buf]"+r"(y_buf),    // %[y_buf]
2066 2067 2068
    [u_buf]"+r"(u_buf),    // %[u_buf]
    [v_buf]"+r"(v_buf),    // %[v_buf]
    [dst_rgb24]"+r"(dst_rgb24),  // %[dst_rgb24]
2069
#if defined(__i386__)
2070
    [width]"+m"(width)     // %[width]
2071
#else
2072
    [width]"+rm"(width)    // %[width]
2073
#endif
2074
  : [yuvconstants]"r"(yuvconstants),  // %[yuvconstants]
2075 2076
    [kShuffleMaskARGBToRGB24_0]"m"(kShuffleMaskARGBToRGB24_0),
    [kShuffleMaskARGBToRGB24]"m"(kShuffleMaskARGBToRGB24)
Frank Barchard's avatar
Frank Barchard committed
2077
  : "memory", "cc", YUVTORGB_REGS
2078
    "xmm0", "xmm1", "xmm2", "xmm3", "xmm4", "xmm5", "xmm6"
2079 2080 2081
  );
}

Frank Barchard's avatar
Frank Barchard committed
2082 2083 2084 2085
void OMITFP I422ToARGBRow_SSSE3(const uint8_t* y_buf,
                                const uint8_t* u_buf,
                                const uint8_t* v_buf,
                                uint8_t* dst_argb,
2086
                                const struct YuvConstants* yuvconstants,
2087
                                int width) {
2088
  asm volatile (
2089
    YUVTORGB_SETUP(yuvconstants)
2090
    "sub       %[u_buf],%[v_buf]               \n"
2091
    "pcmpeqb   %%xmm5,%%xmm5                   \n"
Frank Barchard's avatar
Frank Barchard committed
2092

2093
    LABELALIGN
Frank Barchard's avatar
Frank Barchard committed
2094
    "1:                                        \n"
2095
    READYUV422
2096
    YUVTORGB(yuvconstants)
2097
    STOREARGB
2098
    "sub       $0x8,%[width]                   \n"
2099
    "jg        1b                              \n"
2100 2101
  : [y_buf]"+r"(y_buf),    // %[y_buf]
    [u_buf]"+r"(u_buf),    // %[u_buf]
2102 2103 2104 2105
    [v_buf]"+r"(v_buf),    // %[v_buf]
    [dst_argb]"+r"(dst_argb),  // %[dst_argb]
    [width]"+rm"(width)    // %[width]
  : [yuvconstants]"r"(yuvconstants)  // %[yuvconstants]
Frank Barchard's avatar
Frank Barchard committed
2106
  : "memory", "cc", YUVTORGB_REGS
2107 2108 2109 2110
    "xmm0", "xmm1", "xmm2", "xmm3", "xmm4", "xmm5"
  );
}

Frank Barchard's avatar
Frank Barchard committed
2111 2112 2113 2114
void OMITFP I422ToAR30Row_SSSE3(const uint8_t* y_buf,
                                const uint8_t* u_buf,
                                const uint8_t* v_buf,
                                uint8_t* dst_ar30,
2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144
                                const struct YuvConstants* yuvconstants,
                                int width) {
  asm volatile (
    YUVTORGB_SETUP(yuvconstants)
    "sub       %[u_buf],%[v_buf]               \n"
    "pcmpeqb   %%xmm5,%%xmm5                   \n"  // AR30 constants
    "psrlw     $14,%%xmm5                      \n"
    "psllw     $4,%%xmm5                       \n"  // 2 alpha bits
    "pxor      %%xmm6,%%xmm6                   \n"
    "pcmpeqb   %%xmm7,%%xmm7                   \n"  // 0 for min
    "psrlw     $6,%%xmm7                       \n"  // 1023 for max

    LABELALIGN
    "1:                                        \n"
    READYUV422
    YUVTORGB16(yuvconstants)
    STOREAR30
    "sub       $0x8,%[width]                   \n"
    "jg        1b                              \n"
  : [y_buf]"+r"(y_buf),    // %[y_buf]
    [u_buf]"+r"(u_buf),    // %[u_buf]
    [v_buf]"+r"(v_buf),    // %[v_buf]
    [dst_ar30]"+r"(dst_ar30),  // %[dst_ar30]
    [width]"+rm"(width)    // %[width]
  : [yuvconstants]"r"(yuvconstants)  // %[yuvconstants]
  : "memory", "cc", YUVTORGB_REGS
    "xmm0", "xmm1", "xmm2", "xmm3", "xmm4", "xmm5", "xmm6", "xmm7"
  );
}

2145
// 10 bit YUV to ARGB
Frank Barchard's avatar
Frank Barchard committed
2146 2147 2148 2149
void OMITFP I210ToARGBRow_SSSE3(const uint16_t* y_buf,
                                const uint16_t* u_buf,
                                const uint16_t* v_buf,
                                uint8_t* dst_argb,
2150 2151 2152 2153 2154 2155 2156 2157 2158
                                const struct YuvConstants* yuvconstants,
                                int width) {
  asm volatile (
    YUVTORGB_SETUP(yuvconstants)
    "sub       %[u_buf],%[v_buf]               \n"
    "pcmpeqb   %%xmm5,%%xmm5                   \n"

    LABELALIGN
    "1:                                        \n"
2159
    READYUV210
2160 2161 2162 2163 2164 2165
    YUVTORGB(yuvconstants)
    STOREARGB
    "sub       $0x8,%[width]                   \n"
    "jg        1b                              \n"
  : [y_buf]"+r"(y_buf),    // %[y_buf]
    [u_buf]"+r"(u_buf),    // %[u_buf]
2166
    [v_buf]"+r"(v_buf),    // %[v_buf]
2167
    [dst_argb]"+r"(dst_argb),  // %[dst_argb]
2168
    [width]"+rm"(width)    // %[width]
2169
  : [yuvconstants]"r"(yuvconstants)  // %[yuvconstants]
Frank Barchard's avatar
Frank Barchard committed
2170
  : "memory", "cc", YUVTORGB_REGS
2171
    "xmm0", "xmm1", "xmm2", "xmm3", "xmm4", "xmm5"
2172 2173 2174
  );
}

2175
// 10 bit YUV to AR30
Frank Barchard's avatar
Frank Barchard committed
2176 2177 2178 2179
void OMITFP I210ToAR30Row_SSSE3(const uint16_t* y_buf,
                                const uint16_t* u_buf,
                                const uint16_t* v_buf,
                                uint8_t* dst_ar30,
2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209
                                const struct YuvConstants* yuvconstants,
                                int width) {
  asm volatile (
    YUVTORGB_SETUP(yuvconstants)
    "sub       %[u_buf],%[v_buf]               \n"
    "pcmpeqb   %%xmm5,%%xmm5                   \n"
    "psrlw     $14,%%xmm5                      \n"
    "psllw     $4,%%xmm5                       \n"  // 2 alpha bits
    "pxor      %%xmm6,%%xmm6                   \n"
    "pcmpeqb   %%xmm7,%%xmm7                   \n"  // 0 for min
    "psrlw     $6,%%xmm7                       \n"  // 1023 for max

    LABELALIGN
    "1:                                        \n"
    READYUV210
    YUVTORGB16(yuvconstants)
    STOREAR30
    "sub       $0x8,%[width]                   \n"
    "jg        1b                              \n"
  : [y_buf]"+r"(y_buf),    // %[y_buf]
    [u_buf]"+r"(u_buf),    // %[u_buf]
    [v_buf]"+r"(v_buf),    // %[v_buf]
    [dst_ar30]"+r"(dst_ar30),  // %[dst_ar30]
    [width]"+rm"(width)    // %[width]
  : [yuvconstants]"r"(yuvconstants)  // %[yuvconstants]
  : "memory", "cc", YUVTORGB_REGS
    "xmm0", "xmm1", "xmm2", "xmm3", "xmm4", "xmm5", "xmm6", "xmm7"
  );
}

2210
#ifdef HAS_I422ALPHATOARGBROW_SSSE3
Frank Barchard's avatar
Frank Barchard committed
2211 2212 2213 2214 2215
void OMITFP I422AlphaToARGBRow_SSSE3(const uint8_t* y_buf,
                                     const uint8_t* u_buf,
                                     const uint8_t* v_buf,
                                     const uint8_t* a_buf,
                                     uint8_t* dst_argb,
2216
                                     const struct YuvConstants* yuvconstants,
2217
                                     int width) {
2218
  // clang-format off
2219
  asm volatile (
2220
    YUVTORGB_SETUP(yuvconstants)
2221
    "sub       %[u_buf],%[v_buf]               \n"
Frank Barchard's avatar
Frank Barchard committed
2222

2223
    LABELALIGN
Frank Barchard's avatar
Frank Barchard committed
2224
    "1:                                        \n"
2225 2226 2227
    READYUVA422
    YUVTORGB(yuvconstants)
    STOREARGB
2228
    "subl      $0x8,%[width]                   \n"
2229 2230 2231 2232 2233 2234
    "jg        1b                              \n"
  : [y_buf]"+r"(y_buf),    // %[y_buf]
    [u_buf]"+r"(u_buf),    // %[u_buf]
    [v_buf]"+r"(v_buf),    // %[v_buf]
    [a_buf]"+r"(a_buf),    // %[a_buf]
    [dst_argb]"+r"(dst_argb),  // %[dst_argb]
2235
#if defined(__i386__)
2236 2237
    [width]"+m"(width)     // %[width]
#else
2238
    [width]"+rm"(width)    // %[width]
2239
#endif
2240
  : [yuvconstants]"r"(yuvconstants)  // %[yuvconstants]
Frank Barchard's avatar
Frank Barchard committed
2241
  : "memory", "cc", YUVTORGB_REGS
2242 2243
    "xmm0", "xmm1", "xmm2", "xmm3", "xmm4", "xmm5"
  );
2244
  // clang-format on
2245
}
2246
#endif  // HAS_I422ALPHATOARGBROW_SSSE3
2247

Frank Barchard's avatar
Frank Barchard committed
2248 2249 2250
void OMITFP NV12ToARGBRow_SSSE3(const uint8_t* y_buf,
                                const uint8_t* uv_buf,
                                uint8_t* dst_argb,
2251
                                const struct YuvConstants* yuvconstants,
2252
                                int width) {
2253
  // clang-format off
2254
  asm volatile (
2255
    YUVTORGB_SETUP(yuvconstants)
2256
    "pcmpeqb   %%xmm5,%%xmm5                   \n"
Frank Barchard's avatar
Frank Barchard committed
2257

2258
    LABELALIGN
Frank Barchard's avatar
Frank Barchard committed
2259
    "1:                                        \n"
2260
    READNV12
2261
    YUVTORGB(yuvconstants)
2262
    STOREARGB
2263 2264 2265
    "sub       $0x8,%[width]                   \n"
    "jg        1b                              \n"
  : [y_buf]"+r"(y_buf),    // %[y_buf]
2266 2267
    [uv_buf]"+r"(uv_buf),    // %[uv_buf]
    [dst_argb]"+r"(dst_argb),  // %[dst_argb]
2268
    [width]"+rm"(width)    // %[width]
2269
  : [yuvconstants]"r"(yuvconstants)  // %[yuvconstants]
2270
    : "memory", "cc", YUVTORGB_REGS
2271
      "xmm0", "xmm1", "xmm2", "xmm3", "xmm4", "xmm5"
2272
  );
2273
  // clang-format on
2274 2275
}

Frank Barchard's avatar
Frank Barchard committed
2276 2277 2278
void OMITFP NV21ToARGBRow_SSSE3(const uint8_t* y_buf,
                                const uint8_t* vu_buf,
                                uint8_t* dst_argb,
2279
                                const struct YuvConstants* yuvconstants,
2280
                                int width) {
2281
  // clang-format off
2282
  asm volatile (
2283
    YUVTORGB_SETUP(yuvconstants)
2284
    "pcmpeqb   %%xmm5,%%xmm5                   \n"
Frank Barchard's avatar
Frank Barchard committed
2285

2286
    LABELALIGN
Frank Barchard's avatar
Frank Barchard committed
2287
    "1:                                        \n"
2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298
    READNV21
    YUVTORGB(yuvconstants)
    STOREARGB
    "sub       $0x8,%[width]                   \n"
    "jg        1b                              \n"
  : [y_buf]"+r"(y_buf),    // %[y_buf]
    [vu_buf]"+r"(vu_buf),    // %[vu_buf]
    [dst_argb]"+r"(dst_argb),  // %[dst_argb]
    [width]"+rm"(width)    // %[width]
  : [yuvconstants]"r"(yuvconstants), // %[yuvconstants]
    [kShuffleNV21]"m"(kShuffleNV21)
2299
    : "memory", "cc", YUVTORGB_REGS
2300
      "xmm0", "xmm1", "xmm2", "xmm3", "xmm4", "xmm5"
2301
  );
2302
  // clang-format on
2303 2304
}

Frank Barchard's avatar
Frank Barchard committed
2305 2306
void OMITFP YUY2ToARGBRow_SSSE3(const uint8_t* yuy2_buf,
                                uint8_t* dst_argb,
2307
                                const struct YuvConstants* yuvconstants,
2308
                                int width) {
2309
  // clang-format off
2310
  asm volatile (
2311
    YUVTORGB_SETUP(yuvconstants)
2312
    "pcmpeqb   %%xmm5,%%xmm5                   \n"
Frank Barchard's avatar
Frank Barchard committed
2313

2314
    LABELALIGN
Frank Barchard's avatar
Frank Barchard committed
2315
    "1:                                        \n"
2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326
    READYUY2
    YUVTORGB(yuvconstants)
    STOREARGB
    "sub       $0x8,%[width]                   \n"
    "jg        1b                              \n"
  : [yuy2_buf]"+r"(yuy2_buf),    // %[yuy2_buf]
    [dst_argb]"+r"(dst_argb),  // %[dst_argb]
    [width]"+rm"(width)    // %[width]
  : [yuvconstants]"r"(yuvconstants), // %[yuvconstants]
    [kShuffleYUY2Y]"m"(kShuffleYUY2Y),
    [kShuffleYUY2UV]"m"(kShuffleYUY2UV)
2327
    : "memory", "cc", YUVTORGB_REGS
2328
      "xmm0", "xmm1", "xmm2", "xmm3", "xmm4", "xmm5"
2329
  );
2330
  // clang-format on
2331 2332
}

Frank Barchard's avatar
Frank Barchard committed
2333 2334
void OMITFP UYVYToARGBRow_SSSE3(const uint8_t* uyvy_buf,
                                uint8_t* dst_argb,
2335
                                const struct YuvConstants* yuvconstants,
2336
                                int width) {
2337
  // clang-format off
2338
  asm volatile (
2339
    YUVTORGB_SETUP(yuvconstants)
2340
    "pcmpeqb   %%xmm5,%%xmm5                   \n"
Frank Barchard's avatar
Frank Barchard committed
2341

2342
    LABELALIGN
Frank Barchard's avatar
Frank Barchard committed
2343
    "1:                                        \n"
2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354
    READUYVY
    YUVTORGB(yuvconstants)
    STOREARGB
    "sub       $0x8,%[width]                   \n"
    "jg        1b                              \n"
  : [uyvy_buf]"+r"(uyvy_buf),    // %[uyvy_buf]
    [dst_argb]"+r"(dst_argb),  // %[dst_argb]
    [width]"+rm"(width)    // %[width]
  : [yuvconstants]"r"(yuvconstants), // %[yuvconstants]
    [kShuffleUYVYY]"m"(kShuffleUYVYY),
    [kShuffleUYVYUV]"m"(kShuffleUYVYUV)
2355
    : "memory", "cc", YUVTORGB_REGS
2356
      "xmm0", "xmm1", "xmm2", "xmm3", "xmm4", "xmm5"
2357
  );
2358
  // clang-format on
2359 2360
}

Frank Barchard's avatar
Frank Barchard committed
2361 2362 2363 2364
void OMITFP I422ToRGBARow_SSSE3(const uint8_t* y_buf,
                                const uint8_t* u_buf,
                                const uint8_t* v_buf,
                                uint8_t* dst_rgba,
2365
                                const struct YuvConstants* yuvconstants,
2366 2367
                                int width) {
  asm volatile (
2368
    YUVTORGB_SETUP(yuvconstants)
2369 2370
    "sub       %[u_buf],%[v_buf]               \n"
    "pcmpeqb   %%xmm5,%%xmm5                   \n"
Frank Barchard's avatar
Frank Barchard committed
2371

2372
    LABELALIGN
Frank Barchard's avatar
Frank Barchard committed
2373
    "1:                                        \n"
2374
    READYUV422
2375
    YUVTORGB(yuvconstants)
2376
    STORERGBA
2377 2378 2379 2380 2381
    "sub       $0x8,%[width]                   \n"
    "jg        1b                              \n"
  : [y_buf]"+r"(y_buf),    // %[y_buf]
    [u_buf]"+r"(u_buf),    // %[u_buf]
    [v_buf]"+r"(v_buf),    // %[v_buf]
2382
    [dst_rgba]"+r"(dst_rgba),  // %[dst_rgba]
2383
    [width]"+rm"(width)    // %[width]
2384
  : [yuvconstants]"r"(yuvconstants)  // %[yuvconstants]
Frank Barchard's avatar
Frank Barchard committed
2385
  : "memory", "cc", YUVTORGB_REGS
2386
    "xmm0", "xmm1", "xmm2", "xmm3", "xmm4", "xmm5"
2387 2388 2389
  );
}

2390
#endif  // HAS_I422TOARGBROW_SSSE3
2391

2392
// Read 16 UV from 444
2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403
#define READYUV444_AVX2                                               \
  "vmovdqu    (%[u_buf]),%%xmm0                                   \n" \
  "vmovdqu    0x00(%[u_buf],%[v_buf],1),%%xmm1                    \n" \
  "lea        0x10(%[u_buf]),%[u_buf]                             \n" \
  "vpermq     $0xd8,%%ymm0,%%ymm0                                 \n" \
  "vpermq     $0xd8,%%ymm1,%%ymm1                                 \n" \
  "vpunpcklbw %%ymm1,%%ymm0,%%ymm0                                \n" \
  "vmovdqu    (%[y_buf]),%%xmm4                                   \n" \
  "vpermq     $0xd8,%%ymm4,%%ymm4                                 \n" \
  "vpunpcklbw %%ymm4,%%ymm4,%%ymm4                                \n" \
  "lea        0x10(%[y_buf]),%[y_buf]                             \n"
2404

2405
// Read 8 UV from 422, upsample to 16 UV.
2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416
#define READYUV422_AVX2                                               \
  "vmovq      (%[u_buf]),%%xmm0                                   \n" \
  "vmovq      0x00(%[u_buf],%[v_buf],1),%%xmm1                    \n" \
  "lea        0x8(%[u_buf]),%[u_buf]                              \n" \
  "vpunpcklbw %%ymm1,%%ymm0,%%ymm0                                \n" \
  "vpermq     $0xd8,%%ymm0,%%ymm0                                 \n" \
  "vpunpcklwd %%ymm0,%%ymm0,%%ymm0                                \n" \
  "vmovdqu    (%[y_buf]),%%xmm4                                   \n" \
  "vpermq     $0xd8,%%ymm4,%%ymm4                                 \n" \
  "vpunpcklbw %%ymm4,%%ymm4,%%ymm4                                \n" \
  "lea        0x10(%[y_buf]),%[y_buf]                             \n"
2417

2418 2419 2420
// Read 8 UV from 210 10 bit, upsample to 16 UV
// TODO(fbarchard): Consider vshufb to replace pack/unpack
// TODO(fbarchard): Consider vunpcklpd to combine the 2 registers into 1.
2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433
#define READYUV210_AVX2                                            \
  "vmovdqu    (%[u_buf]),%%xmm0                                \n" \
  "vmovdqu    0x00(%[u_buf],%[v_buf],1),%%xmm1                 \n" \
  "lea        0x10(%[u_buf]),%[u_buf]                          \n" \
  "vpermq     $0xd8,%%ymm0,%%ymm0                              \n" \
  "vpermq     $0xd8,%%ymm1,%%ymm1                              \n" \
  "vpunpcklwd %%ymm1,%%ymm0,%%ymm0                             \n" \
  "vpsraw     $0x2,%%ymm0,%%ymm0                               \n" \
  "vpackuswb  %%ymm0,%%ymm0,%%ymm0                             \n" \
  "vpunpcklwd %%ymm0,%%ymm0,%%ymm0                             \n" \
  "vmovdqu    (%[y_buf]),%%ymm4                                \n" \
  "vpsllw     $0x6,%%ymm4,%%ymm4                               \n" \
  "lea        0x20(%[y_buf]),%[y_buf]                          \n"
2434

2435
// Read 8 UV from 422, upsample to 16 UV.  With 16 Alpha.
2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449
#define READYUVA422_AVX2                                              \
  "vmovq      (%[u_buf]),%%xmm0                                   \n" \
  "vmovq      0x00(%[u_buf],%[v_buf],1),%%xmm1                    \n" \
  "lea        0x8(%[u_buf]),%[u_buf]                              \n" \
  "vpunpcklbw %%ymm1,%%ymm0,%%ymm0                                \n" \
  "vpermq     $0xd8,%%ymm0,%%ymm0                                 \n" \
  "vpunpcklwd %%ymm0,%%ymm0,%%ymm0                                \n" \
  "vmovdqu    (%[y_buf]),%%xmm4                                   \n" \
  "vpermq     $0xd8,%%ymm4,%%ymm4                                 \n" \
  "vpunpcklbw %%ymm4,%%ymm4,%%ymm4                                \n" \
  "lea        0x10(%[y_buf]),%[y_buf]                             \n" \
  "vmovdqu    (%[a_buf]),%%xmm5                                   \n" \
  "vpermq     $0xd8,%%ymm5,%%ymm5                                 \n" \
  "lea        0x10(%[a_buf]),%[a_buf]                             \n"
2450

2451
// Read 8 UV from NV12, upsample to 16 UV.
2452 2453 2454 2455 2456 2457 2458 2459 2460
#define READNV12_AVX2                                                 \
  "vmovdqu    (%[uv_buf]),%%xmm0                                  \n" \
  "lea        0x10(%[uv_buf]),%[uv_buf]                           \n" \
  "vpermq     $0xd8,%%ymm0,%%ymm0                                 \n" \
  "vpunpcklwd %%ymm0,%%ymm0,%%ymm0                                \n" \
  "vmovdqu    (%[y_buf]),%%xmm4                                   \n" \
  "vpermq     $0xd8,%%ymm4,%%ymm4                                 \n" \
  "vpunpcklbw %%ymm4,%%ymm4,%%ymm4                                \n" \
  "lea        0x10(%[y_buf]),%[y_buf]                             \n"
2461

2462
// Read 8 VU from NV21, upsample to 16 UV.
2463 2464 2465 2466 2467 2468 2469 2470 2471
#define READNV21_AVX2                                                 \
  "vmovdqu    (%[vu_buf]),%%xmm0                                  \n" \
  "lea        0x10(%[vu_buf]),%[vu_buf]                           \n" \
  "vpermq     $0xd8,%%ymm0,%%ymm0                                 \n" \
  "vpshufb     %[kShuffleNV21], %%ymm0, %%ymm0                    \n" \
  "vmovdqu    (%[y_buf]),%%xmm4                                   \n" \
  "vpermq     $0xd8,%%ymm4,%%ymm4                                 \n" \
  "vpunpcklbw %%ymm4,%%ymm4,%%ymm4                                \n" \
  "lea        0x10(%[y_buf]),%[y_buf]                             \n"
2472

2473
// Read 8 YUY2 with 16 Y and upsample 8 UV to 16 UV.
2474 2475 2476 2477 2478 2479
#define READYUY2_AVX2                                                 \
  "vmovdqu    (%[yuy2_buf]),%%ymm4                                \n" \
  "vpshufb    %[kShuffleYUY2Y], %%ymm4, %%ymm4                    \n" \
  "vmovdqu    (%[yuy2_buf]),%%ymm0                                \n" \
  "vpshufb    %[kShuffleYUY2UV], %%ymm0, %%ymm0                   \n" \
  "lea        0x20(%[yuy2_buf]),%[yuy2_buf]                       \n"
2480

2481
// Read 8 UYVY with 16 Y and upsample 8 UV to 16 UV.
2482 2483 2484 2485 2486 2487
#define READUYVY_AVX2                                                 \
  "vmovdqu    (%[uyvy_buf]),%%ymm4                                \n" \
  "vpshufb    %[kShuffleUYVYY], %%ymm4, %%ymm4                    \n" \
  "vmovdqu    (%[uyvy_buf]),%%ymm0                                \n" \
  "vpshufb    %[kShuffleUYVYUV], %%ymm0, %%ymm0                   \n" \
  "lea        0x20(%[uyvy_buf]),%[uyvy_buf]                       \n"
2488

2489
#if defined(__x86_64__)
2490 2491 2492 2493 2494 2495 2496 2497
#define YUVTORGB_SETUP_AVX2(yuvconstants)                            \
  "vmovdqa     (%[yuvconstants]),%%ymm8                          \n" \
  "vmovdqa     32(%[yuvconstants]),%%ymm9                        \n" \
  "vmovdqa     64(%[yuvconstants]),%%ymm10                       \n" \
  "vmovdqa     96(%[yuvconstants]),%%ymm11                       \n" \
  "vmovdqa     128(%[yuvconstants]),%%ymm12                      \n" \
  "vmovdqa     160(%[yuvconstants]),%%ymm13                      \n" \
  "vmovdqa     192(%[yuvconstants]),%%ymm14                      \n"
Frank Barchard's avatar
Frank Barchard committed
2498

2499
#define YUVTORGB16_AVX2(yuvconstants)                                 \
2500 2501 2502 2503 2504 2505 2506 2507 2508
  "vpmaddubsw  %%ymm10,%%ymm0,%%ymm2                              \n" \
  "vpmaddubsw  %%ymm9,%%ymm0,%%ymm1                               \n" \
  "vpmaddubsw  %%ymm8,%%ymm0,%%ymm0                               \n" \
  "vpsubw      %%ymm2,%%ymm13,%%ymm2                              \n" \
  "vpsubw      %%ymm1,%%ymm12,%%ymm1                              \n" \
  "vpsubw      %%ymm0,%%ymm11,%%ymm0                              \n" \
  "vpmulhuw    %%ymm14,%%ymm4,%%ymm4                              \n" \
  "vpaddsw     %%ymm4,%%ymm0,%%ymm0                               \n" \
  "vpaddsw     %%ymm4,%%ymm1,%%ymm1                               \n" \
2509
  "vpaddsw     %%ymm4,%%ymm2,%%ymm2                               \n"
Frank Barchard's avatar
Frank Barchard committed
2510

2511
#define YUVTORGB_REGS_AVX2 \
2512
  "xmm8", "xmm9", "xmm10", "xmm11", "xmm12", "xmm13", "xmm14",
Frank Barchard's avatar
Frank Barchard committed
2513

2514
#else  // Convert 16 pixels: 16 UV and 16 Y.
Frank Barchard's avatar
Frank Barchard committed
2515

2516
#define YUVTORGB_SETUP_AVX2(yuvconstants)
2517
#define YUVTORGB16_AVX2(yuvconstants)                                 \
2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529
  "vpmaddubsw  64(%[yuvconstants]),%%ymm0,%%ymm2                  \n" \
  "vpmaddubsw  32(%[yuvconstants]),%%ymm0,%%ymm1                  \n" \
  "vpmaddubsw  (%[yuvconstants]),%%ymm0,%%ymm0                    \n" \
  "vmovdqu     160(%[yuvconstants]),%%ymm3                        \n" \
  "vpsubw      %%ymm2,%%ymm3,%%ymm2                               \n" \
  "vmovdqu     128(%[yuvconstants]),%%ymm3                        \n" \
  "vpsubw      %%ymm1,%%ymm3,%%ymm1                               \n" \
  "vmovdqu     96(%[yuvconstants]),%%ymm3                         \n" \
  "vpsubw      %%ymm0,%%ymm3,%%ymm0                               \n" \
  "vpmulhuw    192(%[yuvconstants]),%%ymm4,%%ymm4                 \n" \
  "vpaddsw     %%ymm4,%%ymm0,%%ymm0                               \n" \
  "vpaddsw     %%ymm4,%%ymm1,%%ymm1                               \n" \
2530 2531 2532 2533 2534 2535
  "vpaddsw     %%ymm4,%%ymm2,%%ymm2                               \n"
#define YUVTORGB_REGS_AVX2
#endif

#define YUVTORGB_AVX2(yuvconstants)                                   \
  YUVTORGB16_AVX2(yuvconstants)                                       \
2536 2537 2538 2539 2540 2541
  "vpsraw      $0x6,%%ymm0,%%ymm0                                 \n" \
  "vpsraw      $0x6,%%ymm1,%%ymm1                                 \n" \
  "vpsraw      $0x6,%%ymm2,%%ymm2                                 \n" \
  "vpackuswb   %%ymm0,%%ymm0,%%ymm0                               \n" \
  "vpackuswb   %%ymm1,%%ymm1,%%ymm1                               \n" \
  "vpackuswb   %%ymm2,%%ymm2,%%ymm2                               \n"
2542

2543
// Store 16 ARGB values.
2544 2545 2546 2547 2548 2549 2550 2551 2552 2553
#define STOREARGB_AVX2                                                \
  "vpunpcklbw %%ymm1,%%ymm0,%%ymm0                                \n" \
  "vpermq     $0xd8,%%ymm0,%%ymm0                                 \n" \
  "vpunpcklbw %%ymm5,%%ymm2,%%ymm2                                \n" \
  "vpermq     $0xd8,%%ymm2,%%ymm2                                 \n" \
  "vpunpcklwd %%ymm2,%%ymm0,%%ymm1                                \n" \
  "vpunpckhwd %%ymm2,%%ymm0,%%ymm0                                \n" \
  "vmovdqu    %%ymm1,(%[dst_argb])                                \n" \
  "vmovdqu    %%ymm0,0x20(%[dst_argb])                            \n" \
  "lea       0x40(%[dst_argb]), %[dst_argb]                       \n"
2554

2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581
// Store 16 AR30 values.
#define STOREAR30_AVX2                                                \
  "vpsraw     $0x4,%%ymm0,%%ymm0                                  \n" \
  "vpsraw     $0x4,%%ymm1,%%ymm1                                  \n" \
  "vpsraw     $0x4,%%ymm2,%%ymm2                                  \n" \
  "vpminsw    %%ymm7,%%ymm0,%%ymm0                                \n" \
  "vpminsw    %%ymm7,%%ymm1,%%ymm1                                \n" \
  "vpminsw    %%ymm7,%%ymm2,%%ymm2                                \n" \
  "vpmaxsw    %%ymm6,%%ymm0,%%ymm0                                \n" \
  "vpmaxsw    %%ymm6,%%ymm1,%%ymm1                                \n" \
  "vpmaxsw    %%ymm6,%%ymm2,%%ymm2                                \n" \
  "vpsllw     $0x4,%%ymm2,%%ymm2                                  \n" \
  "vpermq     $0xd8,%%ymm0,%%ymm0                                 \n" \
  "vpermq     $0xd8,%%ymm1,%%ymm1                                 \n" \
  "vpermq     $0xd8,%%ymm2,%%ymm2                                 \n" \
  "vpunpckhwd %%ymm2,%%ymm0,%%ymm3                                \n" \
  "vpunpcklwd %%ymm2,%%ymm0,%%ymm0                                \n" \
  "vpunpckhwd %%ymm5,%%ymm1,%%ymm2                                \n" \
  "vpunpcklwd %%ymm5,%%ymm1,%%ymm1                                \n" \
  "vpslld     $0xa,%%ymm1,%%ymm1                                  \n" \
  "vpslld     $0xa,%%ymm2,%%ymm2                                  \n" \
  "vpor       %%ymm1,%%ymm0,%%ymm0                                \n" \
  "vpor       %%ymm2,%%ymm3,%%ymm3                                \n" \
  "vmovdqu    %%ymm0,(%[dst_ar30])                                \n" \
  "vmovdqu    %%ymm3,0x20(%[dst_ar30])                            \n" \
  "lea        0x40(%[dst_ar30]), %[dst_ar30]                      \n"

2582 2583 2584
#ifdef HAS_I444TOARGBROW_AVX2
// 16 pixels
// 16 UV values with 16 Y producing 16 ARGB (64 bytes).
Frank Barchard's avatar
Frank Barchard committed
2585 2586 2587 2588
void OMITFP I444ToARGBRow_AVX2(const uint8_t* y_buf,
                               const uint8_t* u_buf,
                               const uint8_t* v_buf,
                               uint8_t* dst_argb,
2589 2590 2591 2592 2593
                               const struct YuvConstants* yuvconstants,
                               int width) {
  asm volatile (
    YUVTORGB_SETUP_AVX2(yuvconstants)
    "sub       %[u_buf],%[v_buf]               \n"
2594
    "vpcmpeqb  %%ymm5,%%ymm5,%%ymm5            \n"
Frank Barchard's avatar
Frank Barchard committed
2595

2596
    LABELALIGN
Frank Barchard's avatar
Frank Barchard committed
2597
    "1:                                        \n"
2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609
    READYUV444_AVX2
    YUVTORGB_AVX2(yuvconstants)
    STOREARGB_AVX2
    "sub       $0x10,%[width]                  \n"
    "jg        1b                              \n"
    "vzeroupper                                \n"
  : [y_buf]"+r"(y_buf),    // %[y_buf]
    [u_buf]"+r"(u_buf),    // %[u_buf]
    [v_buf]"+r"(v_buf),    // %[v_buf]
    [dst_argb]"+r"(dst_argb),  // %[dst_argb]
    [width]"+rm"(width)    // %[width]
  : [yuvconstants]"r"(yuvconstants)  // %[yuvconstants]
Frank Barchard's avatar
Frank Barchard committed
2610
  : "memory", "cc", YUVTORGB_REGS_AVX2
2611 2612 2613 2614 2615
    "xmm0", "xmm1", "xmm2", "xmm3", "xmm4", "xmm5"
  );
}
#endif  // HAS_I444TOARGBROW_AVX2

2616
#if defined(HAS_I422TOARGBROW_AVX2)
2617 2618
// 16 pixels
// 8 UV values upsampled to 16 UV, mixed with 16 Y producing 16 ARGB (64 bytes).
Frank Barchard's avatar
Frank Barchard committed
2619 2620 2621 2622
void OMITFP I422ToARGBRow_AVX2(const uint8_t* y_buf,
                               const uint8_t* u_buf,
                               const uint8_t* v_buf,
                               uint8_t* dst_argb,
2623
                               const struct YuvConstants* yuvconstants,
2624
                               int width) {
2625
  asm volatile (
2626
    YUVTORGB_SETUP_AVX2(yuvconstants)
2627
    "sub       %[u_buf],%[v_buf]               \n"
2628
    "vpcmpeqb  %%ymm5,%%ymm5,%%ymm5            \n"
Frank Barchard's avatar
Frank Barchard committed
2629

2630
    LABELALIGN
Frank Barchard's avatar
Frank Barchard committed
2631
    "1:                                        \n"
2632
    READYUV422_AVX2
2633
    YUVTORGB_AVX2(yuvconstants)
2634
    STOREARGB_AVX2
2635 2636
    "sub       $0x10,%[width]                  \n"
    "jg        1b                              \n"
2637

2638 2639 2640 2641 2642 2643
    "vzeroupper                                \n"
  : [y_buf]"+r"(y_buf),    // %[y_buf]
    [u_buf]"+r"(u_buf),    // %[u_buf]
    [v_buf]"+r"(v_buf),    // %[v_buf]
    [dst_argb]"+r"(dst_argb),  // %[dst_argb]
    [width]"+rm"(width)    // %[width]
2644
  : [yuvconstants]"r"(yuvconstants)  // %[yuvconstants]
Frank Barchard's avatar
Frank Barchard committed
2645
  : "memory", "cc", YUVTORGB_REGS_AVX2
2646
    "xmm0", "xmm1", "xmm2", "xmm3", "xmm4", "xmm5"
2647 2648
  );
}
2649
#endif  // HAS_I422TOARGBROW_AVX2
2650

2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690
#if defined(HAS_I422TOAR30ROW_AVX2)
// 16 pixels
// 8 UV values upsampled to 16 UV, mixed with 16 Y producing 16 AR30 (64 bytes).
void OMITFP I422ToAR30Row_AVX2(const uint8_t* y_buf,
                               const uint8_t* u_buf,
                               const uint8_t* v_buf,
                               uint8_t* dst_ar30,
                               const struct YuvConstants* yuvconstants,
                               int width) {
  asm volatile (
    YUVTORGB_SETUP_AVX2(yuvconstants)
    "sub       %[u_buf],%[v_buf]               \n"
    "vpcmpeqb  %%ymm5,%%ymm5,%%ymm5            \n"  // AR30 constants
    "vpsrlw    $14,%%ymm5,%%ymm5               \n"
    "vpsllw    $4,%%ymm5,%%ymm5                \n"  // 2 alpha bits
    "vpxor     %%ymm6,%%ymm6,%%ymm6            \n"  // 0 for min
    "vpcmpeqb  %%ymm7,%%ymm7,%%ymm7            \n"  // 1023 for max
    "vpsrlw    $6,%%ymm7,%%ymm7                \n"

    LABELALIGN
    "1:                                        \n"
    READYUV422_AVX2
    YUVTORGB16_AVX2(yuvconstants)
    STOREAR30_AVX2
    "sub       $0x10,%[width]                  \n"
    "jg        1b                              \n"

    "vzeroupper                                \n"
  : [y_buf]"+r"(y_buf),    // %[y_buf]
    [u_buf]"+r"(u_buf),    // %[u_buf]
    [v_buf]"+r"(v_buf),    // %[v_buf]
    [dst_ar30]"+r"(dst_ar30),  // %[dst_ar30]
    [width]"+rm"(width)    // %[width]
  : [yuvconstants]"r"(yuvconstants)  // %[yuvconstants]
  : "memory", "cc", YUVTORGB_REGS_AVX2
    "xmm0", "xmm1", "xmm2", "xmm3", "xmm4", "xmm5", "xmm6", "xmm7"
  );
}
#endif  // HAS_I422TOAR30ROW_AVX2

2691 2692 2693
#if defined(HAS_I210TOARGBROW_AVX2)
// 16 pixels
// 8 UV values upsampled to 16 UV, mixed with 16 Y producing 16 ARGB (64 bytes).
Frank Barchard's avatar
Frank Barchard committed
2694 2695 2696 2697
void OMITFP I210ToARGBRow_AVX2(const uint16_t* y_buf,
                               const uint16_t* u_buf,
                               const uint16_t* v_buf,
                               uint8_t* dst_argb,
2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725
                               const struct YuvConstants* yuvconstants,
                               int width) {
  asm volatile (
    YUVTORGB_SETUP_AVX2(yuvconstants)
    "sub       %[u_buf],%[v_buf]               \n"
    "vpcmpeqb  %%ymm5,%%ymm5,%%ymm5            \n"

    LABELALIGN
    "1:                                        \n"
    READYUV210_AVX2
    YUVTORGB_AVX2(yuvconstants)
    STOREARGB_AVX2
    "sub       $0x10,%[width]                  \n"
    "jg        1b                              \n"

    "vzeroupper                                \n"
  : [y_buf]"+r"(y_buf),    // %[y_buf]
    [u_buf]"+r"(u_buf),    // %[u_buf]
    [v_buf]"+r"(v_buf),    // %[v_buf]
    [dst_argb]"+r"(dst_argb),  // %[dst_argb]
    [width]"+rm"(width)    // %[width]
  : [yuvconstants]"r"(yuvconstants)  // %[yuvconstants]
  : "memory", "cc", YUVTORGB_REGS_AVX2
    "xmm0", "xmm1", "xmm2", "xmm3", "xmm4", "xmm5"
  );
}
#endif  // HAS_I210TOARGBROW_AVX2

2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765
#if defined(HAS_I210TOAR30ROW_AVX2)
// 16 pixels
// 8 UV values upsampled to 16 UV, mixed with 16 Y producing 16 AR30 (64 bytes).
void OMITFP I210ToAR30Row_AVX2(const uint16_t* y_buf,
                               const uint16_t* u_buf,
                               const uint16_t* v_buf,
                               uint8_t* dst_ar30,
                               const struct YuvConstants* yuvconstants,
                               int width) {
  asm volatile (
    YUVTORGB_SETUP_AVX2(yuvconstants)
    "sub       %[u_buf],%[v_buf]               \n"
    "vpcmpeqb  %%ymm5,%%ymm5,%%ymm5            \n"  // AR30 constants
    "vpsrlw    $14,%%ymm5,%%ymm5               \n"
    "vpsllw    $4,%%ymm5,%%ymm5                \n"  // 2 alpha bits
    "vpxor     %%ymm6,%%ymm6,%%ymm6            \n"  // 0 for min
    "vpcmpeqb  %%ymm7,%%ymm7,%%ymm7            \n"  // 1023 for max
    "vpsrlw    $6,%%ymm7,%%ymm7                \n"

    LABELALIGN
    "1:                                        \n"
    READYUV210_AVX2
    YUVTORGB16_AVX2(yuvconstants)
    STOREAR30_AVX2
    "sub       $0x10,%[width]                  \n"
    "jg        1b                              \n"

    "vzeroupper                                \n"
  : [y_buf]"+r"(y_buf),    // %[y_buf]
    [u_buf]"+r"(u_buf),    // %[u_buf]
    [v_buf]"+r"(v_buf),    // %[v_buf]
    [dst_ar30]"+r"(dst_ar30),  // %[dst_ar30]
    [width]"+rm"(width)    // %[width]
  : [yuvconstants]"r"(yuvconstants)  // %[yuvconstants]
  : "memory", "cc", YUVTORGB_REGS_AVX2
    "xmm0", "xmm1", "xmm2", "xmm3", "xmm4", "xmm5"
  );
}
#endif  // HAS_I210TOAR30ROW_AVX2

2766 2767 2768
#if defined(HAS_I422ALPHATOARGBROW_AVX2)
// 16 pixels
// 8 UV values upsampled to 16 UV, mixed with 16 Y and 16 A producing 16 ARGB.
Frank Barchard's avatar
Frank Barchard committed
2769 2770 2771 2772 2773
void OMITFP I422AlphaToARGBRow_AVX2(const uint8_t* y_buf,
                                    const uint8_t* u_buf,
                                    const uint8_t* v_buf,
                                    const uint8_t* a_buf,
                                    uint8_t* dst_argb,
2774 2775 2776
                                    const struct YuvConstants* yuvconstants,
                                    int width) {
  // clang-format off
2777
  asm volatile (
2778
    YUVTORGB_SETUP_AVX2(yuvconstants)
2779
    "sub       %[u_buf],%[v_buf]               \n"
Frank Barchard's avatar
Frank Barchard committed
2780

2781
    LABELALIGN
Frank Barchard's avatar
Frank Barchard committed
2782
    "1:                                        \n"
2783 2784 2785
    READYUVA422_AVX2
    YUVTORGB_AVX2(yuvconstants)
    STOREARGB_AVX2
2786
    "subl      $0x10,%[width]                  \n"
2787 2788 2789 2790 2791 2792 2793
    "jg        1b                              \n"
    "vzeroupper                                \n"
  : [y_buf]"+r"(y_buf),    // %[y_buf]
    [u_buf]"+r"(u_buf),    // %[u_buf]
    [v_buf]"+r"(v_buf),    // %[v_buf]
    [a_buf]"+r"(a_buf),    // %[a_buf]
    [dst_argb]"+r"(dst_argb),  // %[dst_argb]
2794
#if defined(__i386__)
2795 2796
    [width]"+m"(width)     // %[width]
#else
2797
    [width]"+rm"(width)    // %[width]
2798
#endif
2799
  : [yuvconstants]"r"(yuvconstants)  // %[yuvconstants]
Frank Barchard's avatar
Frank Barchard committed
2800
  : "memory", "cc", YUVTORGB_REGS_AVX2
2801 2802
    "xmm0", "xmm1", "xmm2", "xmm3", "xmm4", "xmm5"
  );
2803
  // clang-format on
2804 2805 2806
}
#endif  // HAS_I422ALPHATOARGBROW_AVX2

2807 2808 2809
#if defined(HAS_I422TORGBAROW_AVX2)
// 16 pixels
// 8 UV values upsampled to 16 UV, mixed with 16 Y producing 16 RGBA (64 bytes).
Frank Barchard's avatar
Frank Barchard committed
2810 2811 2812 2813
void OMITFP I422ToRGBARow_AVX2(const uint8_t* y_buf,
                               const uint8_t* u_buf,
                               const uint8_t* v_buf,
                               uint8_t* dst_argb,
2814
                               const struct YuvConstants* yuvconstants,
2815 2816
                               int width) {
  asm volatile (
2817
    YUVTORGB_SETUP_AVX2(yuvconstants)
2818 2819
    "sub       %[u_buf],%[v_buf]               \n"
    "vpcmpeqb   %%ymm5,%%ymm5,%%ymm5           \n"
Frank Barchard's avatar
Frank Barchard committed
2820

2821
    LABELALIGN
Frank Barchard's avatar
Frank Barchard committed
2822
    "1:                                        \n"
2823
    READYUV422_AVX2
2824
    YUVTORGB_AVX2(yuvconstants)
2825 2826 2827 2828 2829 2830 2831 2832

    // Step 3: Weave into RGBA
    "vpunpcklbw %%ymm2,%%ymm1,%%ymm1           \n"
    "vpermq     $0xd8,%%ymm1,%%ymm1            \n"
    "vpunpcklbw %%ymm0,%%ymm5,%%ymm2           \n"
    "vpermq     $0xd8,%%ymm2,%%ymm2            \n"
    "vpunpcklwd %%ymm1,%%ymm2,%%ymm0           \n"
    "vpunpckhwd %%ymm1,%%ymm2,%%ymm1           \n"
2833 2834 2835 2836 2837
    "vmovdqu    %%ymm0,(%[dst_argb])           \n"
    "vmovdqu    %%ymm1,0x20(%[dst_argb])       \n"
    "lea        0x40(%[dst_argb]),%[dst_argb]  \n"
    "sub        $0x10,%[width]                 \n"
    "jg         1b                             \n"
2838 2839 2840 2841 2842 2843
    "vzeroupper                                \n"
  : [y_buf]"+r"(y_buf),    // %[y_buf]
    [u_buf]"+r"(u_buf),    // %[u_buf]
    [v_buf]"+r"(v_buf),    // %[v_buf]
    [dst_argb]"+r"(dst_argb),  // %[dst_argb]
    [width]"+rm"(width)    // %[width]
2844
  : [yuvconstants]"r"(yuvconstants)  // %[yuvconstants]
Frank Barchard's avatar
Frank Barchard committed
2845
  : "memory", "cc", YUVTORGB_REGS_AVX2
2846
    "xmm0", "xmm1", "xmm2", "xmm3", "xmm4", "xmm5"
2847 2848 2849
  );
}
#endif  // HAS_I422TORGBAROW_AVX2
2850

2851 2852 2853
#if defined(HAS_NV12TOARGBROW_AVX2)
// 16 pixels.
// 8 UV values upsampled to 16 UV, mixed with 16 Y producing 16 ARGB (64 bytes).
Frank Barchard's avatar
Frank Barchard committed
2854 2855 2856
void OMITFP NV12ToARGBRow_AVX2(const uint8_t* y_buf,
                               const uint8_t* uv_buf,
                               uint8_t* dst_argb,
2857
                               const struct YuvConstants* yuvconstants,
2858
                               int width) {
2859
  // clang-format off
2860
  asm volatile (
2861
    YUVTORGB_SETUP_AVX2(yuvconstants)
2862
    "vpcmpeqb   %%ymm5,%%ymm5,%%ymm5           \n"
Frank Barchard's avatar
Frank Barchard committed
2863

2864
    LABELALIGN
Frank Barchard's avatar
Frank Barchard committed
2865
    "1:                                        \n"
2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876
    READNV12_AVX2
    YUVTORGB_AVX2(yuvconstants)
    STOREARGB_AVX2
    "sub       $0x10,%[width]                  \n"
    "jg        1b                              \n"
    "vzeroupper                                \n"
  : [y_buf]"+r"(y_buf),    // %[y_buf]
    [uv_buf]"+r"(uv_buf),    // %[uv_buf]
    [dst_argb]"+r"(dst_argb),  // %[dst_argb]
    [width]"+rm"(width)    // %[width]
  : [yuvconstants]"r"(yuvconstants)  // %[yuvconstants]
2877
    : "memory", "cc", YUVTORGB_REGS_AVX2
2878
    "xmm0", "xmm0", "xmm1", "xmm2", "xmm3", "xmm4", "xmm5"
2879
  );
2880
  // clang-format on
2881
}
2882
#endif  // HAS_NV12TOARGBROW_AVX2
2883

2884 2885 2886
#if defined(HAS_NV21TOARGBROW_AVX2)
// 16 pixels.
// 8 VU values upsampled to 16 UV, mixed with 16 Y producing 16 ARGB (64 bytes).
Frank Barchard's avatar
Frank Barchard committed
2887 2888 2889
void OMITFP NV21ToARGBRow_AVX2(const uint8_t* y_buf,
                               const uint8_t* vu_buf,
                               uint8_t* dst_argb,
2890
                               const struct YuvConstants* yuvconstants,
2891
                               int width) {
2892
  // clang-format off
2893
  asm volatile (
2894
    YUVTORGB_SETUP_AVX2(yuvconstants)
2895
    "vpcmpeqb   %%ymm5,%%ymm5,%%ymm5           \n"
Frank Barchard's avatar
Frank Barchard committed
2896

2897
    LABELALIGN
Frank Barchard's avatar
Frank Barchard committed
2898
    "1:                                        \n"
2899 2900 2901 2902 2903 2904 2905 2906 2907 2908 2909 2910
    READNV21_AVX2
    YUVTORGB_AVX2(yuvconstants)
    STOREARGB_AVX2
    "sub       $0x10,%[width]                  \n"
    "jg        1b                              \n"
    "vzeroupper                                \n"
  : [y_buf]"+r"(y_buf),    // %[y_buf]
    [vu_buf]"+r"(vu_buf),    // %[vu_buf]
    [dst_argb]"+r"(dst_argb),  // %[dst_argb]
    [width]"+rm"(width)    // %[width]
  : [yuvconstants]"r"(yuvconstants), // %[yuvconstants]
    [kShuffleNV21]"m"(kShuffleNV21)
2911
    : "memory", "cc", YUVTORGB_REGS_AVX2
2912
      "xmm0", "xmm0", "xmm1", "xmm2", "xmm3", "xmm4", "xmm5"
2913
  );
2914
  // clang-format on
2915 2916
}
#endif  // HAS_NV21TOARGBROW_AVX2
2917

2918 2919 2920
#if defined(HAS_YUY2TOARGBROW_AVX2)
// 16 pixels.
// 8 YUY2 values with 16 Y and 8 UV producing 16 ARGB (64 bytes).
Frank Barchard's avatar
Frank Barchard committed
2921 2922
void OMITFP YUY2ToARGBRow_AVX2(const uint8_t* yuy2_buf,
                               uint8_t* dst_argb,
2923
                               const struct YuvConstants* yuvconstants,
2924
                               int width) {
2925
  // clang-format off
2926
  asm volatile (
2927
    YUVTORGB_SETUP_AVX2(yuvconstants)
2928
    "vpcmpeqb   %%ymm5,%%ymm5,%%ymm5           \n"
Frank Barchard's avatar
Frank Barchard committed
2929

2930
    LABELALIGN
Frank Barchard's avatar
Frank Barchard committed
2931
    "1:                                        \n"
2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943
    READYUY2_AVX2
    YUVTORGB_AVX2(yuvconstants)
    STOREARGB_AVX2
    "sub       $0x10,%[width]                  \n"
    "jg        1b                              \n"
    "vzeroupper                                \n"
  : [yuy2_buf]"+r"(yuy2_buf),    // %[yuy2_buf]
    [dst_argb]"+r"(dst_argb),  // %[dst_argb]
    [width]"+rm"(width)    // %[width]
  : [yuvconstants]"r"(yuvconstants), // %[yuvconstants]
    [kShuffleYUY2Y]"m"(kShuffleYUY2Y),
    [kShuffleYUY2UV]"m"(kShuffleYUY2UV)
2944
    : "memory", "cc", YUVTORGB_REGS_AVX2
2945
      "xmm0", "xmm0", "xmm1", "xmm2", "xmm3", "xmm4", "xmm5"
2946
  );
2947
  // clang-format on
2948 2949 2950 2951 2952 2953
}
#endif  // HAS_YUY2TOARGBROW_AVX2

#if defined(HAS_UYVYTOARGBROW_AVX2)
// 16 pixels.
// 8 UYVY values with 16 Y and 8 UV producing 16 ARGB (64 bytes).
Frank Barchard's avatar
Frank Barchard committed
2954 2955
void OMITFP UYVYToARGBRow_AVX2(const uint8_t* uyvy_buf,
                               uint8_t* dst_argb,
2956
                               const struct YuvConstants* yuvconstants,
2957
                               int width) {
2958
  // clang-format off
2959
  asm volatile (
2960
    YUVTORGB_SETUP_AVX2(yuvconstants)
2961
    "vpcmpeqb   %%ymm5,%%ymm5,%%ymm5           \n"
Frank Barchard's avatar
Frank Barchard committed
2962

2963
    LABELALIGN
Frank Barchard's avatar
Frank Barchard committed
2964
    "1:                                        \n"
2965 2966 2967 2968 2969 2970 2971 2972 2973 2974 2975 2976
    READUYVY_AVX2
    YUVTORGB_AVX2(yuvconstants)
    STOREARGB_AVX2
    "sub       $0x10,%[width]                  \n"
    "jg        1b                              \n"
    "vzeroupper                                \n"
  : [uyvy_buf]"+r"(uyvy_buf),    // %[uyvy_buf]
    [dst_argb]"+r"(dst_argb),  // %[dst_argb]
    [width]"+rm"(width)    // %[width]
  : [yuvconstants]"r"(yuvconstants), // %[yuvconstants]
    [kShuffleUYVYY]"m"(kShuffleUYVYY),
    [kShuffleUYVYUV]"m"(kShuffleUYVYUV)
2977
    : "memory", "cc", YUVTORGB_REGS_AVX2
2978
      "xmm0", "xmm1", "xmm2", "xmm3", "xmm4", "xmm5"
2979
  );
2980
  // clang-format on
2981 2982 2983
}
#endif  // HAS_UYVYTOARGBROW_AVX2

2984
#ifdef HAS_I400TOARGBROW_SSE2
Frank Barchard's avatar
Frank Barchard committed
2985
void I400ToARGBRow_SSE2(const uint8_t* y_buf, uint8_t* dst_argb, int width) {
2986 2987 2988 2989 2990 2991 2992 2993 2994 2995
  asm volatile(
      "mov       $0x4a354a35,%%eax               \n"  // 4a35 = 18997 = 1.164
      "movd      %%eax,%%xmm2                    \n"
      "pshufd    $0x0,%%xmm2,%%xmm2              \n"
      "mov       $0x04880488,%%eax               \n"  // 0488 = 1160 = 1.164 *
                                                      // 16
      "movd      %%eax,%%xmm3                    \n"
      "pshufd    $0x0,%%xmm3,%%xmm3              \n"
      "pcmpeqb   %%xmm4,%%xmm4                   \n"
      "pslld     $0x18,%%xmm4                    \n"
Frank Barchard's avatar
Frank Barchard committed
2996

2997 2998 2999 3000 3001 3002 3003 3004 3005 3006 3007 3008 3009 3010 3011 3012 3013 3014 3015 3016 3017 3018 3019 3020 3021 3022 3023 3024 3025
      LABELALIGN
      "1:                                        \n"
      // Step 1: Scale Y contribution to 8 G values. G = (y - 16) * 1.164
      "movq      (%0),%%xmm0                     \n"
      "lea       0x8(%0),%0                      \n"
      "punpcklbw %%xmm0,%%xmm0                   \n"
      "pmulhuw   %%xmm2,%%xmm0                   \n"
      "psubusw   %%xmm3,%%xmm0                   \n"
      "psrlw     $6, %%xmm0                      \n"
      "packuswb  %%xmm0,%%xmm0                   \n"

      // Step 2: Weave into ARGB
      "punpcklbw %%xmm0,%%xmm0                   \n"
      "movdqa    %%xmm0,%%xmm1                   \n"
      "punpcklwd %%xmm0,%%xmm0                   \n"
      "punpckhwd %%xmm1,%%xmm1                   \n"
      "por       %%xmm4,%%xmm0                   \n"
      "por       %%xmm4,%%xmm1                   \n"
      "movdqu    %%xmm0,(%1)                     \n"
      "movdqu    %%xmm1,0x10(%1)                 \n"
      "lea       0x20(%1),%1                     \n"

      "sub       $0x8,%2                         \n"
      "jg        1b                              \n"
      : "+r"(y_buf),     // %0
        "+r"(dst_argb),  // %1
        "+rm"(width)     // %2
      :
      : "memory", "cc", "eax", "xmm0", "xmm1", "xmm2", "xmm3", "xmm4");
3026
}
3027
#endif  // HAS_I400TOARGBROW_SSE2
mikhal@webrtc.org's avatar
mikhal@webrtc.org committed
3028

3029
#ifdef HAS_I400TOARGBROW_AVX2
3030 3031
// 16 pixels of Y converted to 16 pixels of ARGB (64 bytes).
// note: vpunpcklbw mutates and vpackuswb unmutates.
Frank Barchard's avatar
Frank Barchard committed
3032
void I400ToARGBRow_AVX2(const uint8_t* y_buf, uint8_t* dst_argb, int width) {
3033 3034 3035 3036 3037 3038 3039 3040 3041 3042
  asm volatile(
      "mov        $0x4a354a35,%%eax              \n"  // 0488 = 1160 = 1.164 *
                                                      // 16
      "vmovd      %%eax,%%xmm2                   \n"
      "vbroadcastss %%xmm2,%%ymm2                \n"
      "mov        $0x4880488,%%eax               \n"  // 4a35 = 18997 = 1.164
      "vmovd      %%eax,%%xmm3                   \n"
      "vbroadcastss %%xmm3,%%ymm3                \n"
      "vpcmpeqb   %%ymm4,%%ymm4,%%ymm4           \n"
      "vpslld     $0x18,%%ymm4,%%ymm4            \n"
3043

3044 3045 3046 3047 3048 3049 3050 3051 3052 3053 3054 3055 3056 3057 3058 3059 3060 3061 3062 3063 3064 3065 3066 3067 3068 3069 3070 3071
      LABELALIGN
      "1:                                        \n"
      // Step 1: Scale Y contribution to 16 G values. G = (y - 16) * 1.164
      "vmovdqu    (%0),%%xmm0                    \n"
      "lea        0x10(%0),%0                    \n"
      "vpermq     $0xd8,%%ymm0,%%ymm0            \n"
      "vpunpcklbw %%ymm0,%%ymm0,%%ymm0           \n"
      "vpmulhuw   %%ymm2,%%ymm0,%%ymm0           \n"
      "vpsubusw   %%ymm3,%%ymm0,%%ymm0           \n"
      "vpsrlw     $0x6,%%ymm0,%%ymm0             \n"
      "vpackuswb  %%ymm0,%%ymm0,%%ymm0           \n"
      "vpunpcklbw %%ymm0,%%ymm0,%%ymm1           \n"
      "vpermq     $0xd8,%%ymm1,%%ymm1            \n"
      "vpunpcklwd %%ymm1,%%ymm1,%%ymm0           \n"
      "vpunpckhwd %%ymm1,%%ymm1,%%ymm1           \n"
      "vpor       %%ymm4,%%ymm0,%%ymm0           \n"
      "vpor       %%ymm4,%%ymm1,%%ymm1           \n"
      "vmovdqu    %%ymm0,(%1)                    \n"
      "vmovdqu    %%ymm1,0x20(%1)                \n"
      "lea       0x40(%1),%1                     \n"
      "sub        $0x10,%2                       \n"
      "jg        1b                              \n"
      "vzeroupper                                \n"
      : "+r"(y_buf),     // %0
        "+r"(dst_argb),  // %1
        "+rm"(width)     // %2
      :
      : "memory", "cc", "eax", "xmm0", "xmm1", "xmm2", "xmm3", "xmm4");
3072
}
3073
#endif  // HAS_I400TOARGBROW_AVX2
3074

3075
#ifdef HAS_MIRRORROW_SSSE3
3076
// Shuffle table for reversing the bytes.
3077 3078
static const uvec8 kShuffleMirror = {15u, 14u, 13u, 12u, 11u, 10u, 9u, 8u,
                                     7u,  6u,  5u,  4u,  3u,  2u,  1u, 0u};
3079

Frank Barchard's avatar
Frank Barchard committed
3080
void MirrorRow_SSSE3(const uint8_t* src, uint8_t* dst, int width) {
3081
  intptr_t temp_width = (intptr_t)(width);
3082
  asm volatile(
Frank Barchard's avatar
Frank Barchard committed
3083

3084 3085 3086 3087 3088 3089 3090 3091 3092 3093 3094 3095 3096 3097 3098
      "movdqa    %3,%%xmm5                       \n"

      LABELALIGN
      "1:                                        \n"
      "movdqu    -0x10(%0,%2,1),%%xmm0           \n"
      "pshufb    %%xmm5,%%xmm0                   \n"
      "movdqu    %%xmm0,(%1)                     \n"
      "lea       0x10(%1),%1                     \n"
      "sub       $0x10,%2                        \n"
      "jg        1b                              \n"
      : "+r"(src),           // %0
        "+r"(dst),           // %1
        "+r"(temp_width)     // %2
      : "m"(kShuffleMirror)  // %3
      : "memory", "cc", "xmm0", "xmm5");
3099
}
3100
#endif  // HAS_MIRRORROW_SSSE3
3101

3102
#ifdef HAS_MIRRORROW_AVX2
Frank Barchard's avatar
Frank Barchard committed
3103
void MirrorRow_AVX2(const uint8_t* src, uint8_t* dst, int width) {
3104
  intptr_t temp_width = (intptr_t)(width);
3105
  asm volatile(
Frank Barchard's avatar
Frank Barchard committed
3106

3107 3108 3109 3110 3111 3112 3113 3114 3115 3116 3117 3118 3119 3120 3121 3122 3123
      "vbroadcastf128 %3,%%ymm5                  \n"

      LABELALIGN
      "1:                                        \n"
      "vmovdqu    -0x20(%0,%2,1),%%ymm0          \n"
      "vpshufb    %%ymm5,%%ymm0,%%ymm0           \n"
      "vpermq     $0x4e,%%ymm0,%%ymm0            \n"
      "vmovdqu    %%ymm0,(%1)                    \n"
      "lea       0x20(%1),%1                     \n"
      "sub       $0x20,%2                        \n"
      "jg        1b                              \n"
      "vzeroupper                                \n"
      : "+r"(src),           // %0
        "+r"(dst),           // %1
        "+r"(temp_width)     // %2
      : "m"(kShuffleMirror)  // %3
      : "memory", "cc", "xmm0", "xmm5");
3124 3125 3126
}
#endif  // HAS_MIRRORROW_AVX2

3127
#ifdef HAS_MIRRORUVROW_SSSE3
3128
// Shuffle table for reversing the bytes of UV channels.
3129 3130
static const uvec8 kShuffleMirrorUV = {14u, 12u, 10u, 8u, 6u, 4u, 2u, 0u,
                                       15u, 13u, 11u, 9u, 7u, 5u, 3u, 1u};
Frank Barchard's avatar
Frank Barchard committed
3131 3132 3133
void MirrorUVRow_SSSE3(const uint8_t* src,
                       uint8_t* dst_u,
                       uint8_t* dst_v,
3134
                       int width) {
3135
  intptr_t temp_width = (intptr_t)(width);
3136 3137 3138 3139
  asm volatile(
      "movdqa    %4,%%xmm1                       \n"
      "lea       -0x10(%0,%3,2),%0               \n"
      "sub       %1,%2                           \n"
Frank Barchard's avatar
Frank Barchard committed
3140

3141 3142 3143 3144 3145 3146 3147 3148 3149 3150 3151 3152 3153 3154 3155 3156
      LABELALIGN
      "1:                                        \n"
      "movdqu    (%0),%%xmm0                     \n"
      "lea       -0x10(%0),%0                    \n"
      "pshufb    %%xmm1,%%xmm0                   \n"
      "movlpd    %%xmm0,(%1)                     \n"
      "movhpd    %%xmm0,0x00(%1,%2,1)            \n"
      "lea       0x8(%1),%1                      \n"
      "sub       $8,%3                           \n"
      "jg        1b                              \n"
      : "+r"(src),             // %0
        "+r"(dst_u),           // %1
        "+r"(dst_v),           // %2
        "+r"(temp_width)       // %3
      : "m"(kShuffleMirrorUV)  // %4
      : "memory", "cc", "xmm0", "xmm1");
3157
}
3158
#endif  // HAS_MIRRORUVROW_SSSE3
3159

3160
#ifdef HAS_ARGBMIRRORROW_SSE2
3161

Frank Barchard's avatar
Frank Barchard committed
3162
void ARGBMirrorRow_SSE2(const uint8_t* src, uint8_t* dst, int width) {
3163
  intptr_t temp_width = (intptr_t)(width);
3164
  asm volatile(
Frank Barchard's avatar
Frank Barchard committed
3165

3166 3167 3168 3169 3170 3171 3172 3173 3174 3175 3176 3177 3178 3179 3180 3181
      "lea       -0x10(%0,%2,4),%0               \n"

      LABELALIGN
      "1:                                        \n"
      "movdqu    (%0),%%xmm0                     \n"
      "pshufd    $0x1b,%%xmm0,%%xmm0             \n"
      "lea       -0x10(%0),%0                    \n"
      "movdqu    %%xmm0,(%1)                     \n"
      "lea       0x10(%1),%1                     \n"
      "sub       $0x4,%2                         \n"
      "jg        1b                              \n"
      : "+r"(src),        // %0
        "+r"(dst),        // %1
        "+r"(temp_width)  // %2
      :
      : "memory", "cc", "xmm0");
3182
}
3183
#endif  // HAS_ARGBMIRRORROW_SSE2
3184

3185 3186
#ifdef HAS_ARGBMIRRORROW_AVX2
// Shuffle table for reversing the bytes.
3187
static const ulvec32 kARGBShuffleMirror_AVX2 = {7u, 6u, 5u, 4u, 3u, 2u, 1u, 0u};
Frank Barchard's avatar
Frank Barchard committed
3188
void ARGBMirrorRow_AVX2(const uint8_t* src, uint8_t* dst, int width) {
3189
  intptr_t temp_width = (intptr_t)(width);
3190
  asm volatile(
Frank Barchard's avatar
Frank Barchard committed
3191

3192 3193 3194 3195 3196 3197 3198 3199 3200 3201 3202 3203 3204 3205 3206
      "vmovdqu    %3,%%ymm5                      \n"

      LABELALIGN
      "1:                                        \n"
      "vpermd    -0x20(%0,%2,4),%%ymm5,%%ymm0    \n"
      "vmovdqu    %%ymm0,(%1)                    \n"
      "lea        0x20(%1),%1                    \n"
      "sub        $0x8,%2                        \n"
      "jg         1b                             \n"
      "vzeroupper                                \n"
      : "+r"(src),                    // %0
        "+r"(dst),                    // %1
        "+r"(temp_width)              // %2
      : "m"(kARGBShuffleMirror_AVX2)  // %3
      : "memory", "cc", "xmm0", "xmm5");
3207 3208 3209
}
#endif  // HAS_ARGBMIRRORROW_AVX2

3210
#ifdef HAS_SPLITUVROW_AVX2
Frank Barchard's avatar
Frank Barchard committed
3211 3212 3213
void SplitUVRow_AVX2(const uint8_t* src_uv,
                     uint8_t* dst_u,
                     uint8_t* dst_v,
3214
                     int width) {
3215 3216 3217 3218
  asm volatile(
      "vpcmpeqb   %%ymm5,%%ymm5,%%ymm5           \n"
      "vpsrlw     $0x8,%%ymm5,%%ymm5             \n"
      "sub        %1,%2                          \n"
Frank Barchard's avatar
Frank Barchard committed
3219

3220 3221 3222 3223 3224 3225 3226 3227 3228 3229 3230 3231 3232 3233 3234 3235 3236 3237 3238 3239 3240 3241 3242 3243 3244
      LABELALIGN
      "1:                                        \n"
      "vmovdqu    (%0),%%ymm0                    \n"
      "vmovdqu    0x20(%0),%%ymm1                \n"
      "lea        0x40(%0),%0                    \n"
      "vpsrlw     $0x8,%%ymm0,%%ymm2             \n"
      "vpsrlw     $0x8,%%ymm1,%%ymm3             \n"
      "vpand      %%ymm5,%%ymm0,%%ymm0           \n"
      "vpand      %%ymm5,%%ymm1,%%ymm1           \n"
      "vpackuswb  %%ymm1,%%ymm0,%%ymm0           \n"
      "vpackuswb  %%ymm3,%%ymm2,%%ymm2           \n"
      "vpermq     $0xd8,%%ymm0,%%ymm0            \n"
      "vpermq     $0xd8,%%ymm2,%%ymm2            \n"
      "vmovdqu    %%ymm0,(%1)                    \n"
      "vmovdqu    %%ymm2,0x00(%1,%2,1)            \n"
      "lea        0x20(%1),%1                    \n"
      "sub        $0x20,%3                       \n"
      "jg         1b                             \n"
      "vzeroupper                                \n"
      : "+r"(src_uv),  // %0
        "+r"(dst_u),   // %1
        "+r"(dst_v),   // %2
        "+r"(width)    // %3
      :
      : "memory", "cc", "xmm0", "xmm1", "xmm2", "xmm3", "xmm5");
3245 3246 3247
}
#endif  // HAS_SPLITUVROW_AVX2

3248
#ifdef HAS_SPLITUVROW_SSE2
Frank Barchard's avatar
Frank Barchard committed
3249 3250 3251
void SplitUVRow_SSE2(const uint8_t* src_uv,
                     uint8_t* dst_u,
                     uint8_t* dst_v,
3252
                     int width) {
3253 3254 3255 3256
  asm volatile(
      "pcmpeqb    %%xmm5,%%xmm5                  \n"
      "psrlw      $0x8,%%xmm5                    \n"
      "sub        %1,%2                          \n"
Frank Barchard's avatar
Frank Barchard committed
3257

3258 3259 3260 3261 3262 3263 3264 3265 3266 3267 3268 3269 3270 3271 3272 3273 3274 3275 3276 3277 3278 3279 3280 3281
      LABELALIGN
      "1:                                        \n"
      "movdqu     (%0),%%xmm0                    \n"
      "movdqu     0x10(%0),%%xmm1                \n"
      "lea        0x20(%0),%0                    \n"
      "movdqa     %%xmm0,%%xmm2                  \n"
      "movdqa     %%xmm1,%%xmm3                  \n"
      "pand       %%xmm5,%%xmm0                  \n"
      "pand       %%xmm5,%%xmm1                  \n"
      "packuswb   %%xmm1,%%xmm0                  \n"
      "psrlw      $0x8,%%xmm2                    \n"
      "psrlw      $0x8,%%xmm3                    \n"
      "packuswb   %%xmm3,%%xmm2                  \n"
      "movdqu     %%xmm0,(%1)                    \n"
      "movdqu    %%xmm2,0x00(%1,%2,1)            \n"
      "lea        0x10(%1),%1                    \n"
      "sub        $0x10,%3                       \n"
      "jg         1b                             \n"
      : "+r"(src_uv),  // %0
        "+r"(dst_u),   // %1
        "+r"(dst_v),   // %2
        "+r"(width)    // %3
      :
      : "memory", "cc", "xmm0", "xmm1", "xmm2", "xmm3", "xmm5");
3282
}
3283
#endif  // HAS_SPLITUVROW_SSE2
3284

3285
#ifdef HAS_MERGEUVROW_AVX2
Frank Barchard's avatar
Frank Barchard committed
3286 3287 3288
void MergeUVRow_AVX2(const uint8_t* src_u,
                     const uint8_t* src_v,
                     uint8_t* dst_uv,
3289
                     int width) {
3290
  asm volatile(
Frank Barchard's avatar
Frank Barchard committed
3291

3292 3293 3294 3295 3296 3297 3298 3299 3300 3301 3302 3303 3304 3305 3306 3307 3308 3309 3310 3311 3312 3313 3314
      "sub       %0,%1                           \n"

      LABELALIGN
      "1:                                        \n"
      "vmovdqu   (%0),%%ymm0                     \n"
      "vmovdqu    0x00(%0,%1,1),%%ymm1           \n"
      "lea       0x20(%0),%0                     \n"
      "vpunpcklbw %%ymm1,%%ymm0,%%ymm2           \n"
      "vpunpckhbw %%ymm1,%%ymm0,%%ymm0           \n"
      "vextractf128 $0x0,%%ymm2,(%2)             \n"
      "vextractf128 $0x0,%%ymm0,0x10(%2)         \n"
      "vextractf128 $0x1,%%ymm2,0x20(%2)         \n"
      "vextractf128 $0x1,%%ymm0,0x30(%2)         \n"
      "lea       0x40(%2),%2                     \n"
      "sub       $0x20,%3                        \n"
      "jg        1b                              \n"
      "vzeroupper                                \n"
      : "+r"(src_u),   // %0
        "+r"(src_v),   // %1
        "+r"(dst_uv),  // %2
        "+r"(width)    // %3
      :
      : "memory", "cc", "xmm0", "xmm1", "xmm2");
3315 3316 3317
}
#endif  // HAS_MERGEUVROW_AVX2

3318
#ifdef HAS_MERGEUVROW_SSE2
Frank Barchard's avatar
Frank Barchard committed
3319 3320 3321
void MergeUVRow_SSE2(const uint8_t* src_u,
                     const uint8_t* src_v,
                     uint8_t* dst_uv,
3322
                     int width) {
3323
  asm volatile(
Frank Barchard's avatar
Frank Barchard committed
3324

3325 3326 3327 3328 3329 3330 3331 3332 3333 3334 3335 3336 3337 3338 3339 3340 3341 3342 3343 3344 3345
      "sub       %0,%1                           \n"

      LABELALIGN
      "1:                                        \n"
      "movdqu    (%0),%%xmm0                     \n"
      "movdqu    0x00(%0,%1,1),%%xmm1            \n"
      "lea       0x10(%0),%0                     \n"
      "movdqa    %%xmm0,%%xmm2                   \n"
      "punpcklbw %%xmm1,%%xmm0                   \n"
      "punpckhbw %%xmm1,%%xmm2                   \n"
      "movdqu    %%xmm0,(%2)                     \n"
      "movdqu    %%xmm2,0x10(%2)                 \n"
      "lea       0x20(%2),%2                     \n"
      "sub       $0x10,%3                        \n"
      "jg        1b                              \n"
      : "+r"(src_u),   // %0
        "+r"(src_v),   // %1
        "+r"(dst_uv),  // %2
        "+r"(width)    // %3
      :
      : "memory", "cc", "xmm0", "xmm1", "xmm2");
3346
}
3347
#endif  // HAS_MERGEUVROW_SSE2
3348

3349 3350 3351 3352 3353 3354
// Use scale to convert lsb formats to msb, depending how many bits there are:
// 128 = 9 bits
// 64 = 10 bits
// 16 = 12 bits
// 1 = 16 bits
#ifdef HAS_MERGEUVROW_16_AVX2
Frank Barchard's avatar
Frank Barchard committed
3355 3356 3357
void MergeUVRow_16_AVX2(const uint16_t* src_u,
                        const uint16_t* src_v,
                        uint16_t* dst_uv,
3358 3359
                        int scale,
                        int width) {
3360
  // clang-format off
3361
  asm volatile (
3362 3363 3364
    "vmovd      %4,%%xmm3                      \n"
    "vpunpcklwd %%xmm3,%%xmm3,%%xmm3           \n"
    "vbroadcastss %%xmm3,%%ymm3                \n"
3365 3366
    "sub       %0,%1                           \n"

3367
    // 16 pixels per loop.
3368 3369 3370 3371 3372
    LABELALIGN
    "1:                                        \n"
    "vmovdqu   (%0),%%ymm0                     \n"
    "vmovdqu   (%0,%1,1),%%ymm1                \n"
    "add        $0x20,%0                       \n"
3373 3374 3375

    "vpmullw   %%ymm3,%%ymm0,%%ymm0            \n"
    "vpmullw   %%ymm3,%%ymm1,%%ymm1            \n"
3376
    "vpunpcklwd %%ymm1,%%ymm0,%%ymm2           \n"  // mutates
3377 3378 3379 3380 3381 3382 3383 3384 3385
    "vpunpckhwd %%ymm1,%%ymm0,%%ymm0           \n"
    "vextractf128 $0x0,%%ymm2,(%2)             \n"
    "vextractf128 $0x0,%%ymm0,0x10(%2)         \n"
    "vextractf128 $0x1,%%ymm2,0x20(%2)         \n"
    "vextractf128 $0x1,%%ymm0,0x30(%2)         \n"
    "add       $0x40,%2                        \n"
    "sub       $0x10,%3                        \n"
    "jg        1b                              \n"
    "vzeroupper                                \n"
3386 3387 3388 3389
  : "+r"(src_u),   // %0
    "+r"(src_v),   // %1
    "+r"(dst_uv),  // %2
    "+r"(width)    // %3
3390 3391
  : "r"(scale)     // %4
  : "memory", "cc", "xmm0", "xmm1", "xmm2", "xmm3");
3392
  // clang-format on
3393 3394 3395
}
#endif  // HAS_MERGEUVROW_AVX2

3396 3397 3398 3399 3400
// Use scale to convert lsb formats to msb, depending how many bits there are:
// 128 = 9 bits
// 64 = 10 bits
// 16 = 12 bits
// 1 = 16 bits
3401
#ifdef HAS_MULTIPLYROW_16_AVX2
Frank Barchard's avatar
Frank Barchard committed
3402 3403
void MultiplyRow_16_AVX2(const uint16_t* src_y,
                         uint16_t* dst_y,
3404 3405 3406 3407 3408 3409 3410 3411 3412 3413 3414 3415 3416 3417 3418 3419 3420 3421 3422 3423 3424 3425 3426 3427 3428 3429 3430 3431 3432 3433 3434
                         int scale,
                         int width) {
  // clang-format off
  asm volatile (
    "vmovd      %3,%%xmm3                      \n"
    "vpunpcklwd %%xmm3,%%xmm3,%%xmm3           \n"
    "vbroadcastss %%xmm3,%%ymm3                \n"
    "sub       %0,%1                           \n"

    // 16 pixels per loop.
    LABELALIGN
    "1:                                        \n"
    "vmovdqu   (%0),%%ymm0                     \n"
    "vmovdqu   0x20(%0),%%ymm1                 \n"
    "vpmullw   %%ymm3,%%ymm0,%%ymm0            \n"
    "vpmullw   %%ymm3,%%ymm1,%%ymm1            \n"
    "vmovdqu   %%ymm0,(%0,%1)                  \n"
    "vmovdqu   %%ymm1,0x20(%0,%1)              \n"
    "add        $0x40,%0                       \n"
    "sub       $0x20,%2                        \n"
    "jg        1b                              \n"
    "vzeroupper                                \n"
  : "+r"(src_y),   // %0
    "+r"(dst_y),   // %1
    "+r"(width)    // %2
  : "r"(scale)     // %3
  : "memory", "cc", "xmm0", "xmm1", "xmm3");
  // clang-format on
}
#endif  // HAS_MULTIPLYROW_16_AVX2

3435 3436 3437 3438 3439
// Use scale to convert lsb formats to msb, depending how many bits there are:
// 32768 = 9 bits
// 16384 = 10 bits
// 4096 = 12 bits
// 256 = 16 bits
Frank Barchard's avatar
Frank Barchard committed
3440 3441
void Convert16To8Row_SSSE3(const uint16_t* src_y,
                           uint8_t* dst_y,
3442 3443 3444 3445
                           int scale,
                           int width) {
  // clang-format off
  asm volatile (
3446 3447 3448
    "movd      %3,%%xmm2                      \n"
    "punpcklwd %%xmm2,%%xmm2                  \n"
    "pshufd    $0x0,%%xmm2,%%xmm2             \n"
3449 3450 3451 3452 3453 3454

    // 32 pixels per loop.
    LABELALIGN
    "1:                                       \n"
    "movdqu    (%0),%%xmm0                    \n"
    "movdqu    0x10(%0),%%xmm1                \n"
3455 3456 3457
    "add       $0x20,%0                       \n"
    "pmulhuw   %%xmm2,%%xmm0                  \n"
    "pmulhuw   %%xmm2,%%xmm1                  \n"
3458 3459 3460 3461 3462 3463 3464 3465 3466
    "packuswb  %%xmm1,%%xmm0                  \n"
    "movdqu    %%xmm0,(%1)                    \n"
    "add       $0x10,%1                       \n"
    "sub       $0x10,%2                       \n"
    "jg        1b                             \n"
  : "+r"(src_y),   // %0
    "+r"(dst_y),   // %1
    "+r"(width)    // %2
  : "r"(scale)     // %3
3467
  : "memory", "cc", "xmm0", "xmm1", "xmm2");
3468 3469 3470
  // clang-format on
}

3471
#ifdef HAS_CONVERT16TO8ROW_AVX2
Frank Barchard's avatar
Frank Barchard committed
3472 3473
void Convert16To8Row_AVX2(const uint16_t* src_y,
                          uint8_t* dst_y,
3474 3475 3476 3477
                          int scale,
                          int width) {
  // clang-format off
  asm volatile (
3478 3479 3480
    "vmovd      %3,%%xmm2                      \n"
    "vpunpcklwd %%xmm2,%%xmm2,%%xmm2           \n"
    "vbroadcastss %%xmm2,%%ymm2                \n"
3481 3482 3483 3484 3485 3486

    // 32 pixels per loop.
    LABELALIGN
    "1:                                        \n"
    "vmovdqu   (%0),%%ymm0                     \n"
    "vmovdqu   0x20(%0),%%ymm1                 \n"
3487 3488 3489
    "add       $0x40,%0                        \n"
    "vpmulhuw  %%ymm2,%%ymm0,%%ymm0            \n"
    "vpmulhuw  %%ymm2,%%ymm1,%%ymm1            \n"
3490 3491 3492 3493 3494 3495 3496 3497 3498 3499 3500
    "vpackuswb %%ymm1,%%ymm0,%%ymm0            \n"  // mutates
    "vpermq    $0xd8,%%ymm0,%%ymm0             \n"
    "vmovdqu   %%ymm0,(%1)                     \n"
    "add       $0x20,%1                        \n"
    "sub       $0x20,%2                        \n"
    "jg        1b                              \n"
    "vzeroupper                                \n"
  : "+r"(src_y),   // %0
    "+r"(dst_y),   // %1
    "+r"(width)    // %2
  : "r"(scale)     // %3
3501
  : "memory", "cc", "xmm0", "xmm1", "xmm2");
3502 3503
  // clang-format on
}
3504 3505 3506 3507 3508 3509 3510
#endif  // HAS_CONVERT16TO8ROW_AVX2

// Use scale to convert to lsb formats depending how many bits there are:
// 512 = 9 bits
// 1024 = 10 bits
// 4096 = 12 bits
// TODO(fbarchard): reduce to SSE2
Frank Barchard's avatar
Frank Barchard committed
3511 3512
void Convert8To16Row_SSE2(const uint8_t* src_y,
                          uint16_t* dst_y,
3513 3514 3515 3516 3517 3518 3519 3520 3521 3522 3523 3524 3525 3526 3527 3528 3529 3530 3531 3532 3533 3534 3535 3536 3537 3538 3539 3540 3541 3542 3543 3544
                          int scale,
                          int width) {
  // clang-format off
  asm volatile (
    "movd      %3,%%xmm2                      \n"
    "punpcklwd %%xmm2,%%xmm2                  \n"
    "pshufd    $0x0,%%xmm2,%%xmm2             \n"

    // 32 pixels per loop.
    LABELALIGN
    "1:                                       \n"
    "movdqu    (%0),%%xmm0                    \n"
    "movdqa    %%xmm0,%%xmm1                  \n"
    "punpcklbw %%xmm0,%%xmm0                  \n"
    "punpckhbw %%xmm1,%%xmm1                  \n"
    "add       $0x10,%0                       \n"
    "pmulhuw   %%xmm2,%%xmm0                  \n"
    "pmulhuw   %%xmm2,%%xmm1                  \n"
    "movdqu    %%xmm0,(%1)                    \n"
    "movdqu    %%xmm1,0x10(%1)                \n"
    "add       $0x20,%1                       \n"
    "sub       $0x10,%2                       \n"
    "jg        1b                             \n"
  : "+r"(src_y),   // %0
    "+r"(dst_y),   // %1
    "+r"(width)    // %2
  : "r"(scale)     // %3
  : "memory", "cc", "xmm0", "xmm1", "xmm2");
  // clang-format on
}

#ifdef HAS_CONVERT8TO16ROW_AVX2
Frank Barchard's avatar
Frank Barchard committed
3545 3546
void Convert8To16Row_AVX2(const uint8_t* src_y,
                          uint16_t* dst_y,
3547 3548 3549 3550 3551 3552 3553 3554 3555 3556 3557 3558 3559 3560 3561 3562 3563 3564 3565 3566 3567 3568 3569 3570 3571 3572 3573 3574 3575 3576 3577 3578
                          int scale,
                          int width) {
  // clang-format off
  asm volatile (
    "vmovd      %3,%%xmm2                      \n"
    "vpunpcklwd %%xmm2,%%xmm2,%%xmm2           \n"
    "vbroadcastss %%xmm2,%%ymm2                \n"

    // 32 pixels per loop.
    LABELALIGN
    "1:                                        \n"
    "vmovdqu   (%0),%%ymm0                     \n"
    "vpermq    $0xd8,%%ymm0,%%ymm0             \n"
    "add       $0x20,%0                        \n"
    "vpunpckhbw %%ymm0,%%ymm0,%%ymm1           \n"
    "vpunpcklbw %%ymm0,%%ymm0,%%ymm0           \n"
    "vpmulhuw  %%ymm2,%%ymm0,%%ymm0            \n"
    "vpmulhuw  %%ymm2,%%ymm1,%%ymm1            \n"
    "vmovdqu   %%ymm0,(%1)                     \n"
    "vmovdqu   %%ymm1,0x20(%1)                 \n"
    "add       $0x40,%1                        \n"
    "sub       $0x20,%2                        \n"
    "jg        1b                              \n"
    "vzeroupper                                \n"
  : "+r"(src_y),   // %0
    "+r"(dst_y),   // %1
    "+r"(width)    // %2
  : "r"(scale)     // %3
  : "memory", "cc", "xmm0", "xmm1", "xmm2");
  // clang-format on
}
#endif  // HAS_CONVERT8TO16ROW_AVX2
3579

3580 3581 3582
#ifdef HAS_SPLITRGBROW_SSSE3

// Shuffle table for converting RGB to Planar.
3583 3584 3585 3586 3587 3588 3589 3590 3591 3592 3593 3594 3595 3596 3597 3598 3599 3600 3601 3602 3603 3604 3605 3606 3607 3608 3609 3610 3611
static const uvec8 kShuffleMaskRGBToR0 = {0u,   3u,   6u,   9u,   12u,  15u,
                                          128u, 128u, 128u, 128u, 128u, 128u,
                                          128u, 128u, 128u, 128u};
static const uvec8 kShuffleMaskRGBToR1 = {128u, 128u, 128u, 128u, 128u, 128u,
                                          2u,   5u,   8u,   11u,  14u,  128u,
                                          128u, 128u, 128u, 128u};
static const uvec8 kShuffleMaskRGBToR2 = {128u, 128u, 128u, 128u, 128u, 128u,
                                          128u, 128u, 128u, 128u, 128u, 1u,
                                          4u,   7u,   10u,  13u};

static const uvec8 kShuffleMaskRGBToG0 = {1u,   4u,   7u,   10u,  13u,  128u,
                                          128u, 128u, 128u, 128u, 128u, 128u,
                                          128u, 128u, 128u, 128u};
static const uvec8 kShuffleMaskRGBToG1 = {128u, 128u, 128u, 128u, 128u, 0u,
                                          3u,   6u,   9u,   12u,  15u,  128u,
                                          128u, 128u, 128u, 128u};
static const uvec8 kShuffleMaskRGBToG2 = {128u, 128u, 128u, 128u, 128u, 128u,
                                          128u, 128u, 128u, 128u, 128u, 2u,
                                          5u,   8u,   11u,  14u};

static const uvec8 kShuffleMaskRGBToB0 = {2u,   5u,   8u,   11u,  14u,  128u,
                                          128u, 128u, 128u, 128u, 128u, 128u,
                                          128u, 128u, 128u, 128u};
static const uvec8 kShuffleMaskRGBToB1 = {128u, 128u, 128u, 128u, 128u, 1u,
                                          4u,   7u,   10u,  13u,  128u, 128u,
                                          128u, 128u, 128u, 128u};
static const uvec8 kShuffleMaskRGBToB2 = {128u, 128u, 128u, 128u, 128u, 128u,
                                          128u, 128u, 128u, 128u, 0u,   3u,
                                          6u,   9u,   12u,  15u};
3612

Frank Barchard's avatar
Frank Barchard committed
3613 3614 3615 3616
void SplitRGBRow_SSSE3(const uint8_t* src_rgb,
                       uint8_t* dst_r,
                       uint8_t* dst_g,
                       uint8_t* dst_b,
3617
                       int width) {
3618 3619 3620 3621 3622 3623 3624 3625 3626 3627 3628 3629 3630 3631 3632 3633 3634 3635 3636 3637 3638 3639 3640 3641 3642 3643 3644 3645 3646 3647 3648 3649 3650 3651 3652 3653 3654 3655 3656 3657 3658 3659 3660 3661 3662 3663 3664 3665 3666 3667 3668 3669 3670 3671 3672 3673 3674 3675
  asm volatile(

      LABELALIGN
      "1:                                        \n"
      "movdqu     (%0),%%xmm0                    \n"
      "movdqu     0x10(%0),%%xmm1                \n"
      "movdqu     0x20(%0),%%xmm2                \n"
      "pshufb     %5, %%xmm0                     \n"
      "pshufb     %6, %%xmm1                     \n"
      "pshufb     %7, %%xmm2                     \n"
      "por        %%xmm1,%%xmm0                  \n"
      "por        %%xmm2,%%xmm0                  \n"
      "movdqu     %%xmm0,(%1)                    \n"
      "lea        0x10(%1),%1                    \n"

      "movdqu     (%0),%%xmm0                    \n"
      "movdqu     0x10(%0),%%xmm1                \n"
      "movdqu     0x20(%0),%%xmm2                \n"
      "pshufb     %8, %%xmm0                     \n"
      "pshufb     %9, %%xmm1                     \n"
      "pshufb     %10, %%xmm2                    \n"
      "por        %%xmm1,%%xmm0                  \n"
      "por        %%xmm2,%%xmm0                  \n"
      "movdqu     %%xmm0,(%2)                    \n"
      "lea        0x10(%2),%2                    \n"

      "movdqu     (%0),%%xmm0                    \n"
      "movdqu     0x10(%0),%%xmm1                \n"
      "movdqu     0x20(%0),%%xmm2                \n"
      "pshufb     %11, %%xmm0                    \n"
      "pshufb     %12, %%xmm1                    \n"
      "pshufb     %13, %%xmm2                    \n"
      "por        %%xmm1,%%xmm0                  \n"
      "por        %%xmm2,%%xmm0                  \n"
      "movdqu     %%xmm0,(%3)                    \n"
      "lea        0x10(%3),%3                    \n"
      "lea        0x30(%0),%0                    \n"
      "sub        $0x10,%4                       \n"
      "jg         1b                             \n"
      : "+r"(src_rgb),             // %0
        "+r"(dst_r),               // %1
        "+r"(dst_g),               // %2
        "+r"(dst_b),               // %3
        "+r"(width)                // %4
      : "m"(kShuffleMaskRGBToR0),  // %5
        "m"(kShuffleMaskRGBToR1),  // %6
        "m"(kShuffleMaskRGBToR2),  // %7
        "m"(kShuffleMaskRGBToG0),  // %8
        "m"(kShuffleMaskRGBToG1),  // %9
        "m"(kShuffleMaskRGBToG2),  // %10
        "m"(kShuffleMaskRGBToB0),  // %11
        "m"(kShuffleMaskRGBToB1),  // %12
        "m"(kShuffleMaskRGBToB2)   // %13
      : "memory", "cc", "xmm0", "xmm1", "xmm2");
}
#endif  // HAS_SPLITRGBROW_SSSE3

#ifdef HAS_MERGERGBROW_SSSE3
3676 3677

// Shuffle table for converting RGB to Planar.
3678 3679 3680 3681 3682 3683 3684 3685 3686 3687 3688 3689 3690 3691 3692 3693 3694 3695 3696 3697 3698 3699 3700 3701 3702 3703 3704 3705 3706
static const uvec8 kShuffleMaskRToRGB0 = {0u, 128u, 128u, 1u, 128u, 128u,
                                          2u, 128u, 128u, 3u, 128u, 128u,
                                          4u, 128u, 128u, 5u};
static const uvec8 kShuffleMaskGToRGB0 = {128u, 0u, 128u, 128u, 1u, 128u,
                                          128u, 2u, 128u, 128u, 3u, 128u,
                                          128u, 4u, 128u, 128u};
static const uvec8 kShuffleMaskBToRGB0 = {128u, 128u, 0u, 128u, 128u, 1u,
                                          128u, 128u, 2u, 128u, 128u, 3u,
                                          128u, 128u, 4u, 128u};

static const uvec8 kShuffleMaskGToRGB1 = {5u, 128u, 128u, 6u, 128u, 128u,
                                          7u, 128u, 128u, 8u, 128u, 128u,
                                          9u, 128u, 128u, 10u};
static const uvec8 kShuffleMaskBToRGB1 = {128u, 5u, 128u, 128u, 6u, 128u,
                                          128u, 7u, 128u, 128u, 8u, 128u,
                                          128u, 9u, 128u, 128u};
static const uvec8 kShuffleMaskRToRGB1 = {128u, 128u, 6u,  128u, 128u, 7u,
                                          128u, 128u, 8u,  128u, 128u, 9u,
                                          128u, 128u, 10u, 128u};

static const uvec8 kShuffleMaskBToRGB2 = {10u, 128u, 128u, 11u, 128u, 128u,
                                          12u, 128u, 128u, 13u, 128u, 128u,
                                          14u, 128u, 128u, 15u};
static const uvec8 kShuffleMaskRToRGB2 = {128u, 11u, 128u, 128u, 12u, 128u,
                                          128u, 13u, 128u, 128u, 14u, 128u,
                                          128u, 15u, 128u, 128u};
static const uvec8 kShuffleMaskGToRGB2 = {128u, 128u, 11u, 128u, 128u, 12u,
                                          128u, 128u, 13u, 128u, 128u, 14u,
                                          128u, 128u, 15u, 128u};
3707

Frank Barchard's avatar
Frank Barchard committed
3708 3709 3710 3711
void MergeRGBRow_SSSE3(const uint8_t* src_r,
                       const uint8_t* src_g,
                       const uint8_t* src_b,
                       uint8_t* dst_rgb,
3712
                       int width) {
3713 3714 3715 3716 3717 3718 3719 3720 3721 3722 3723 3724 3725 3726 3727 3728 3729 3730 3731 3732 3733 3734 3735 3736 3737 3738 3739 3740 3741 3742 3743 3744 3745 3746 3747 3748 3749 3750 3751 3752 3753 3754 3755 3756 3757 3758 3759 3760 3761 3762 3763 3764 3765 3766 3767
  asm volatile(

      LABELALIGN
      "1:                                        \n"
      "movdqu     (%0),%%xmm0                    \n"
      "movdqu     (%1),%%xmm1                    \n"
      "movdqu     (%2),%%xmm2                    \n"
      "pshufb     %5, %%xmm0                     \n"
      "pshufb     %6, %%xmm1                     \n"
      "pshufb     %7, %%xmm2                     \n"
      "por        %%xmm1,%%xmm0                  \n"
      "por        %%xmm2,%%xmm0                  \n"
      "movdqu     %%xmm0,(%3)                    \n"

      "movdqu     (%0),%%xmm0                    \n"
      "movdqu     (%1),%%xmm1                    \n"
      "movdqu     (%2),%%xmm2                    \n"
      "pshufb     %8, %%xmm0                     \n"
      "pshufb     %9, %%xmm1                     \n"
      "pshufb     %10, %%xmm2                    \n"
      "por        %%xmm1,%%xmm0                  \n"
      "por        %%xmm2,%%xmm0                  \n"
      "movdqu     %%xmm0,16(%3)                  \n"

      "movdqu     (%0),%%xmm0                    \n"
      "movdqu     (%1),%%xmm1                    \n"
      "movdqu     (%2),%%xmm2                    \n"
      "pshufb     %11, %%xmm0                    \n"
      "pshufb     %12, %%xmm1                    \n"
      "pshufb     %13, %%xmm2                    \n"
      "por        %%xmm1,%%xmm0                  \n"
      "por        %%xmm2,%%xmm0                  \n"
      "movdqu     %%xmm0,32(%3)                  \n"

      "lea        0x10(%0),%0                    \n"
      "lea        0x10(%1),%1                    \n"
      "lea        0x10(%2),%2                    \n"
      "lea        0x30(%3),%3                    \n"
      "sub        $0x10,%4                       \n"
      "jg         1b                             \n"
      : "+r"(src_r),               // %0
        "+r"(src_g),               // %1
        "+r"(src_b),               // %2
        "+r"(dst_rgb),             // %3
        "+r"(width)                // %4
      : "m"(kShuffleMaskRToRGB0),  // %5
        "m"(kShuffleMaskGToRGB0),  // %6
        "m"(kShuffleMaskBToRGB0),  // %7
        "m"(kShuffleMaskRToRGB1),  // %8
        "m"(kShuffleMaskGToRGB1),  // %9
        "m"(kShuffleMaskBToRGB1),  // %10
        "m"(kShuffleMaskRToRGB2),  // %11
        "m"(kShuffleMaskGToRGB2),  // %12
        "m"(kShuffleMaskBToRGB2)   // %13
      : "memory", "cc", "xmm0", "xmm1", "xmm2");
3768 3769 3770
}
#endif  // HAS_MERGERGBROW_SSSE3

3771
#ifdef HAS_COPYROW_SSE2
3772
void CopyRow_SSE2(const uint8_t* src, uint8_t* dst, int width) {
3773 3774 3775 3776 3777
  asm volatile(
      "test       $0xf,%0                        \n"
      "jne        2f                             \n"
      "test       $0xf,%1                        \n"
      "jne        2f                             \n"
Frank Barchard's avatar
Frank Barchard committed
3778

3779 3780 3781 3782 3783 3784 3785 3786 3787 3788 3789
      LABELALIGN
      "1:                                        \n"
      "movdqa    (%0),%%xmm0                     \n"
      "movdqa    0x10(%0),%%xmm1                 \n"
      "lea       0x20(%0),%0                     \n"
      "movdqa    %%xmm0,(%1)                     \n"
      "movdqa    %%xmm1,0x10(%1)                 \n"
      "lea       0x20(%1),%1                     \n"
      "sub       $0x20,%2                        \n"
      "jg        1b                              \n"
      "jmp       9f                              \n"
Frank Barchard's avatar
Frank Barchard committed
3790

3791 3792 3793 3794 3795 3796 3797 3798 3799 3800 3801 3802 3803 3804
      LABELALIGN
      "2:                                        \n"
      "movdqu    (%0),%%xmm0                     \n"
      "movdqu    0x10(%0),%%xmm1                 \n"
      "lea       0x20(%0),%0                     \n"
      "movdqu    %%xmm0,(%1)                     \n"
      "movdqu    %%xmm1,0x10(%1)                 \n"
      "lea       0x20(%1),%1                     \n"
      "sub       $0x20,%2                        \n"
      "jg        2b                              \n"

      LABELALIGN "9:                             \n"
      : "+r"(src),   // %0
        "+r"(dst),   // %1
3805
        "+r"(width)  // %2
3806 3807
      :
      : "memory", "cc", "xmm0", "xmm1");
3808 3809 3810
}
#endif  // HAS_COPYROW_SSE2

3811
#ifdef HAS_COPYROW_AVX
3812
void CopyRow_AVX(const uint8_t* src, uint8_t* dst, int width) {
3813 3814 3815 3816 3817 3818 3819 3820 3821 3822 3823 3824 3825 3826
  asm volatile(

      LABELALIGN
      "1:                                        \n"
      "vmovdqu   (%0),%%ymm0                     \n"
      "vmovdqu   0x20(%0),%%ymm1                 \n"
      "lea       0x40(%0),%0                     \n"
      "vmovdqu   %%ymm0,(%1)                     \n"
      "vmovdqu   %%ymm1,0x20(%1)                 \n"
      "lea       0x40(%1),%1                     \n"
      "sub       $0x40,%2                        \n"
      "jg        1b                              \n"
      : "+r"(src),   // %0
        "+r"(dst),   // %1
3827
        "+r"(width)  // %2
3828 3829
      :
      : "memory", "cc", "xmm0", "xmm1");
3830 3831 3832
}
#endif  // HAS_COPYROW_AVX

3833
#ifdef HAS_COPYROW_ERMS
3834
// Multiple of 1.
Frank Barchard's avatar
Frank Barchard committed
3835
void CopyRow_ERMS(const uint8_t* src, uint8_t* dst, int width) {
3836
  size_t width_tmp = (size_t)(width);
3837 3838 3839 3840 3841 3842 3843 3844
  asm volatile(

      "rep movsb                      \n"
      : "+S"(src),       // %0
        "+D"(dst),       // %1
        "+c"(width_tmp)  // %2
      :
      : "memory", "cc");
3845
}
3846
#endif  // HAS_COPYROW_ERMS
3847

3848 3849
#ifdef HAS_ARGBCOPYALPHAROW_SSE2
// width in pixels
Frank Barchard's avatar
Frank Barchard committed
3850
void ARGBCopyAlphaRow_SSE2(const uint8_t* src, uint8_t* dst, int width) {
3851 3852 3853 3854 3855
  asm volatile(
      "pcmpeqb   %%xmm0,%%xmm0                   \n"
      "pslld     $0x18,%%xmm0                    \n"
      "pcmpeqb   %%xmm1,%%xmm1                   \n"
      "psrld     $0x8,%%xmm1                     \n"
Frank Barchard's avatar
Frank Barchard committed
3856

3857 3858 3859 3860 3861 3862 3863 3864 3865 3866 3867 3868 3869 3870 3871 3872 3873 3874 3875 3876 3877 3878 3879
      LABELALIGN
      "1:                                        \n"
      "movdqu    (%0),%%xmm2                     \n"
      "movdqu    0x10(%0),%%xmm3                 \n"
      "lea       0x20(%0),%0                     \n"
      "movdqu    (%1),%%xmm4                     \n"
      "movdqu    0x10(%1),%%xmm5                 \n"
      "pand      %%xmm0,%%xmm2                   \n"
      "pand      %%xmm0,%%xmm3                   \n"
      "pand      %%xmm1,%%xmm4                   \n"
      "pand      %%xmm1,%%xmm5                   \n"
      "por       %%xmm4,%%xmm2                   \n"
      "por       %%xmm5,%%xmm3                   \n"
      "movdqu    %%xmm2,(%1)                     \n"
      "movdqu    %%xmm3,0x10(%1)                 \n"
      "lea       0x20(%1),%1                     \n"
      "sub       $0x8,%2                         \n"
      "jg        1b                              \n"
      : "+r"(src),   // %0
        "+r"(dst),   // %1
        "+r"(width)  // %2
      :
      : "memory", "cc", "xmm0", "xmm1", "xmm2", "xmm3", "xmm4", "xmm5");
3880 3881 3882 3883 3884
}
#endif  // HAS_ARGBCOPYALPHAROW_SSE2

#ifdef HAS_ARGBCOPYALPHAROW_AVX2
// width in pixels
Frank Barchard's avatar
Frank Barchard committed
3885
void ARGBCopyAlphaRow_AVX2(const uint8_t* src, uint8_t* dst, int width) {
3886 3887 3888
  asm volatile(
      "vpcmpeqb  %%ymm0,%%ymm0,%%ymm0            \n"
      "vpsrld    $0x8,%%ymm0,%%ymm0              \n"
Frank Barchard's avatar
Frank Barchard committed
3889

3890 3891 3892 3893 3894 3895 3896 3897 3898 3899 3900 3901 3902 3903 3904 3905 3906 3907
      LABELALIGN
      "1:                                        \n"
      "vmovdqu   (%0),%%ymm1                     \n"
      "vmovdqu   0x20(%0),%%ymm2                 \n"
      "lea       0x40(%0),%0                     \n"
      "vpblendvb %%ymm0,(%1),%%ymm1,%%ymm1       \n"
      "vpblendvb %%ymm0,0x20(%1),%%ymm2,%%ymm2   \n"
      "vmovdqu   %%ymm1,(%1)                     \n"
      "vmovdqu   %%ymm2,0x20(%1)                 \n"
      "lea       0x40(%1),%1                     \n"
      "sub       $0x10,%2                        \n"
      "jg        1b                              \n"
      "vzeroupper                                \n"
      : "+r"(src),   // %0
        "+r"(dst),   // %1
        "+r"(width)  // %2
      :
      : "memory", "cc", "xmm0", "xmm1", "xmm2");
3908 3909 3910
}
#endif  // HAS_ARGBCOPYALPHAROW_AVX2

3911 3912
#ifdef HAS_ARGBEXTRACTALPHAROW_SSE2
// width in pixels
3913 3914 3915 3916 3917 3918 3919 3920 3921 3922 3923 3924 3925 3926 3927 3928 3929 3930 3931 3932 3933 3934 3935
void ARGBExtractAlphaRow_SSE2(const uint8_t* src_argb,
                              uint8_t* dst_a,
                              int width) {
  asm volatile(

      LABELALIGN
      "1:                                        \n"
      "movdqu    (%0), %%xmm0                    \n"
      "movdqu    0x10(%0), %%xmm1                \n"
      "lea       0x20(%0), %0                    \n"
      "psrld     $0x18, %%xmm0                   \n"
      "psrld     $0x18, %%xmm1                   \n"
      "packssdw  %%xmm1, %%xmm0                  \n"
      "packuswb  %%xmm0, %%xmm0                  \n"
      "movq      %%xmm0,(%1)                     \n"
      "lea       0x8(%1), %1                     \n"
      "sub       $0x8, %2                        \n"
      "jg        1b                              \n"
      : "+r"(src_argb),  // %0
        "+r"(dst_a),     // %1
        "+rm"(width)     // %2
      :
      : "memory", "cc", "xmm0", "xmm1");
3936 3937 3938
}
#endif  // HAS_ARGBEXTRACTALPHAROW_SSE2

3939 3940
#ifdef HAS_ARGBEXTRACTALPHAROW_AVX2
static const uvec8 kShuffleAlphaShort_AVX2 = {
3941 3942
    3u,  128u, 128u, 128u, 7u,  128u, 128u, 128u,
    11u, 128u, 128u, 128u, 15u, 128u, 128u, 128u};
3943

3944 3945 3946 3947 3948 3949
void ARGBExtractAlphaRow_AVX2(const uint8_t* src_argb,
                              uint8_t* dst_a,
                              int width) {
  asm volatile(
      "vmovdqa    %3,%%ymm4                      \n"
      "vbroadcastf128 %4,%%ymm5                  \n"
Frank Barchard's avatar
Frank Barchard committed
3950

3951 3952 3953 3954 3955 3956 3957 3958 3959 3960 3961 3962 3963 3964 3965 3966 3967 3968 3969 3970 3971 3972 3973 3974 3975 3976
      LABELALIGN
      "1:                                        \n"
      "vmovdqu   (%0), %%ymm0                    \n"
      "vmovdqu   0x20(%0), %%ymm1                \n"
      "vpshufb    %%ymm5,%%ymm0,%%ymm0           \n"  // vpsrld $0x18, %%ymm0
      "vpshufb    %%ymm5,%%ymm1,%%ymm1           \n"
      "vmovdqu   0x40(%0), %%ymm2                \n"
      "vmovdqu   0x60(%0), %%ymm3                \n"
      "lea       0x80(%0), %0                    \n"
      "vpackssdw  %%ymm1, %%ymm0, %%ymm0         \n"  // mutates
      "vpshufb    %%ymm5,%%ymm2,%%ymm2           \n"
      "vpshufb    %%ymm5,%%ymm3,%%ymm3           \n"
      "vpackssdw  %%ymm3, %%ymm2, %%ymm2         \n"  // mutates
      "vpackuswb  %%ymm2,%%ymm0,%%ymm0           \n"  // mutates.
      "vpermd     %%ymm0,%%ymm4,%%ymm0           \n"  // unmutate.
      "vmovdqu    %%ymm0,(%1)                    \n"
      "lea       0x20(%1),%1                     \n"
      "sub        $0x20, %2                      \n"
      "jg         1b                             \n"
      "vzeroupper                                \n"
      : "+r"(src_argb),               // %0
        "+r"(dst_a),                  // %1
        "+rm"(width)                  // %2
      : "m"(kPermdARGBToY_AVX),       // %3
        "m"(kShuffleAlphaShort_AVX2)  // %4
      : "memory", "cc", "xmm0", "xmm1", "xmm2", "xmm3", "xmm4", "xmm5");
3977 3978 3979
}
#endif  // HAS_ARGBEXTRACTALPHAROW_AVX2

3980 3981
#ifdef HAS_ARGBCOPYYTOALPHAROW_SSE2
// width in pixels
Frank Barchard's avatar
Frank Barchard committed
3982
void ARGBCopyYToAlphaRow_SSE2(const uint8_t* src, uint8_t* dst, int width) {
3983 3984 3985 3986 3987
  asm volatile(
      "pcmpeqb   %%xmm0,%%xmm0                   \n"
      "pslld     $0x18,%%xmm0                    \n"
      "pcmpeqb   %%xmm1,%%xmm1                   \n"
      "psrld     $0x8,%%xmm1                     \n"
Frank Barchard's avatar
Frank Barchard committed
3988

3989 3990 3991 3992 3993 3994 3995 3996 3997 3998 3999 4000 4001 4002 4003 4004 4005 4006 4007 4008 4009 4010 4011 4012 4013
      LABELALIGN
      "1:                                        \n"
      "movq      (%0),%%xmm2                     \n"
      "lea       0x8(%0),%0                      \n"
      "punpcklbw %%xmm2,%%xmm2                   \n"
      "punpckhwd %%xmm2,%%xmm3                   \n"
      "punpcklwd %%xmm2,%%xmm2                   \n"
      "movdqu    (%1),%%xmm4                     \n"
      "movdqu    0x10(%1),%%xmm5                 \n"
      "pand      %%xmm0,%%xmm2                   \n"
      "pand      %%xmm0,%%xmm3                   \n"
      "pand      %%xmm1,%%xmm4                   \n"
      "pand      %%xmm1,%%xmm5                   \n"
      "por       %%xmm4,%%xmm2                   \n"
      "por       %%xmm5,%%xmm3                   \n"
      "movdqu    %%xmm2,(%1)                     \n"
      "movdqu    %%xmm3,0x10(%1)                 \n"
      "lea       0x20(%1),%1                     \n"
      "sub       $0x8,%2                         \n"
      "jg        1b                              \n"
      : "+r"(src),   // %0
        "+r"(dst),   // %1
        "+r"(width)  // %2
      :
      : "memory", "cc", "xmm0", "xmm1", "xmm2", "xmm3", "xmm4", "xmm5");
4014 4015 4016 4017 4018
}
#endif  // HAS_ARGBCOPYYTOALPHAROW_SSE2

#ifdef HAS_ARGBCOPYYTOALPHAROW_AVX2
// width in pixels
Frank Barchard's avatar
Frank Barchard committed
4019
void ARGBCopyYToAlphaRow_AVX2(const uint8_t* src, uint8_t* dst, int width) {
4020 4021 4022
  asm volatile(
      "vpcmpeqb  %%ymm0,%%ymm0,%%ymm0            \n"
      "vpsrld    $0x8,%%ymm0,%%ymm0              \n"
Frank Barchard's avatar
Frank Barchard committed
4023

4024 4025 4026 4027 4028 4029 4030 4031 4032 4033 4034 4035 4036 4037 4038 4039 4040 4041 4042 4043
      LABELALIGN
      "1:                                        \n"
      "vpmovzxbd (%0),%%ymm1                     \n"
      "vpmovzxbd 0x8(%0),%%ymm2                  \n"
      "lea       0x10(%0),%0                     \n"
      "vpslld    $0x18,%%ymm1,%%ymm1             \n"
      "vpslld    $0x18,%%ymm2,%%ymm2             \n"
      "vpblendvb %%ymm0,(%1),%%ymm1,%%ymm1       \n"
      "vpblendvb %%ymm0,0x20(%1),%%ymm2,%%ymm2   \n"
      "vmovdqu   %%ymm1,(%1)                     \n"
      "vmovdqu   %%ymm2,0x20(%1)                 \n"
      "lea       0x40(%1),%1                     \n"
      "sub       $0x10,%2                        \n"
      "jg        1b                              \n"
      "vzeroupper                                \n"
      : "+r"(src),   // %0
        "+r"(dst),   // %1
        "+r"(width)  // %2
      :
      : "memory", "cc", "xmm0", "xmm1", "xmm2");
4044 4045 4046
}
#endif  // HAS_ARGBCOPYYTOALPHAROW_AVX2

4047
#ifdef HAS_SETROW_X86
Frank Barchard's avatar
Frank Barchard committed
4048
void SetRow_X86(uint8_t* dst, uint8_t v8, int width) {
4049
  size_t width_tmp = (size_t)(width >> 2);
Frank Barchard's avatar
Frank Barchard committed
4050
  const uint32_t v32 = v8 * 0x01010101u;  // Duplicate byte to all bytes.
4051 4052 4053 4054 4055 4056 4057
  asm volatile(

      "rep stosl                      \n"
      : "+D"(dst),       // %0
        "+c"(width_tmp)  // %1
      : "a"(v32)         // %2
      : "memory", "cc");
4058 4059
}

Frank Barchard's avatar
Frank Barchard committed
4060
void SetRow_ERMS(uint8_t* dst, uint8_t v8, int width) {
4061
  size_t width_tmp = (size_t)(width);
4062 4063 4064 4065 4066 4067 4068
  asm volatile(

      "rep stosb                      \n"
      : "+D"(dst),       // %0
        "+c"(width_tmp)  // %1
      : "a"(v8)          // %2
      : "memory", "cc");
4069 4070
}

Frank Barchard's avatar
Frank Barchard committed
4071
void ARGBSetRow_X86(uint8_t* dst_argb, uint32_t v32, int width) {
4072
  size_t width_tmp = (size_t)(width);
4073 4074 4075 4076 4077 4078 4079
  asm volatile(

      "rep stosl                      \n"
      : "+D"(dst_argb),  // %0
        "+c"(width_tmp)  // %1
      : "a"(v32)         // %2
      : "memory", "cc");
4080 4081 4082
}
#endif  // HAS_SETROW_X86

4083
#ifdef HAS_YUY2TOYROW_SSE2
Frank Barchard's avatar
Frank Barchard committed
4084
void YUY2ToYRow_SSE2(const uint8_t* src_yuy2, uint8_t* dst_y, int width) {
4085 4086 4087
  asm volatile(
      "pcmpeqb   %%xmm5,%%xmm5                   \n"
      "psrlw     $0x8,%%xmm5                     \n"
Frank Barchard's avatar
Frank Barchard committed
4088

4089 4090 4091 4092 4093 4094 4095 4096 4097 4098 4099 4100 4101 4102 4103 4104 4105
      LABELALIGN
      "1:                                        \n"
      "movdqu    (%0),%%xmm0                     \n"
      "movdqu    0x10(%0),%%xmm1                 \n"
      "lea       0x20(%0),%0                     \n"
      "pand      %%xmm5,%%xmm0                   \n"
      "pand      %%xmm5,%%xmm1                   \n"
      "packuswb  %%xmm1,%%xmm0                   \n"
      "movdqu    %%xmm0,(%1)                     \n"
      "lea       0x10(%1),%1                     \n"
      "sub       $0x10,%2                        \n"
      "jg        1b                              \n"
      : "+r"(src_yuy2),  // %0
        "+r"(dst_y),     // %1
        "+r"(width)      // %2
      :
      : "memory", "cc", "xmm0", "xmm1", "xmm5");
4106 4107
}

Frank Barchard's avatar
Frank Barchard committed
4108
void YUY2ToUVRow_SSE2(const uint8_t* src_yuy2,
4109
                      int stride_yuy2,
Frank Barchard's avatar
Frank Barchard committed
4110 4111
                      uint8_t* dst_u,
                      uint8_t* dst_v,
4112
                      int width) {
4113 4114 4115 4116
  asm volatile(
      "pcmpeqb   %%xmm5,%%xmm5                   \n"
      "psrlw     $0x8,%%xmm5                     \n"
      "sub       %1,%2                           \n"
Frank Barchard's avatar
Frank Barchard committed
4117

4118 4119 4120 4121 4122 4123 4124 4125 4126 4127 4128 4129 4130 4131 4132 4133 4134 4135 4136 4137 4138 4139 4140 4141 4142 4143 4144 4145
      LABELALIGN
      "1:                                        \n"
      "movdqu    (%0),%%xmm0                     \n"
      "movdqu    0x10(%0),%%xmm1                 \n"
      "movdqu    0x00(%0,%4,1),%%xmm2            \n"
      "movdqu    0x10(%0,%4,1),%%xmm3            \n"
      "lea       0x20(%0),%0                     \n"
      "pavgb     %%xmm2,%%xmm0                   \n"
      "pavgb     %%xmm3,%%xmm1                   \n"
      "psrlw     $0x8,%%xmm0                     \n"
      "psrlw     $0x8,%%xmm1                     \n"
      "packuswb  %%xmm1,%%xmm0                   \n"
      "movdqa    %%xmm0,%%xmm1                   \n"
      "pand      %%xmm5,%%xmm0                   \n"
      "packuswb  %%xmm0,%%xmm0                   \n"
      "psrlw     $0x8,%%xmm1                     \n"
      "packuswb  %%xmm1,%%xmm1                   \n"
      "movq      %%xmm0,(%1)                     \n"
      "movq    %%xmm1,0x00(%1,%2,1)              \n"
      "lea       0x8(%1),%1                      \n"
      "sub       $0x10,%3                        \n"
      "jg        1b                              \n"
      : "+r"(src_yuy2),               // %0
        "+r"(dst_u),                  // %1
        "+r"(dst_v),                  // %2
        "+r"(width)                   // %3
      : "r"((intptr_t)(stride_yuy2))  // %4
      : "memory", "cc", "xmm0", "xmm1", "xmm2", "xmm3", "xmm5");
4146 4147
}

Frank Barchard's avatar
Frank Barchard committed
4148 4149 4150
void YUY2ToUV422Row_SSE2(const uint8_t* src_yuy2,
                         uint8_t* dst_u,
                         uint8_t* dst_v,
4151
                         int width) {
4152 4153 4154 4155
  asm volatile(
      "pcmpeqb   %%xmm5,%%xmm5                   \n"
      "psrlw     $0x8,%%xmm5                     \n"
      "sub       %1,%2                           \n"
Frank Barchard's avatar
Frank Barchard committed
4156

4157 4158 4159 4160 4161 4162 4163 4164 4165 4166 4167 4168 4169 4170 4171 4172 4173 4174 4175 4176 4177 4178 4179 4180
      LABELALIGN
      "1:                                        \n"
      "movdqu    (%0),%%xmm0                     \n"
      "movdqu    0x10(%0),%%xmm1                 \n"
      "lea       0x20(%0),%0                     \n"
      "psrlw     $0x8,%%xmm0                     \n"
      "psrlw     $0x8,%%xmm1                     \n"
      "packuswb  %%xmm1,%%xmm0                   \n"
      "movdqa    %%xmm0,%%xmm1                   \n"
      "pand      %%xmm5,%%xmm0                   \n"
      "packuswb  %%xmm0,%%xmm0                   \n"
      "psrlw     $0x8,%%xmm1                     \n"
      "packuswb  %%xmm1,%%xmm1                   \n"
      "movq      %%xmm0,(%1)                     \n"
      "movq    %%xmm1,0x00(%1,%2,1)              \n"
      "lea       0x8(%1),%1                      \n"
      "sub       $0x10,%3                        \n"
      "jg        1b                              \n"
      : "+r"(src_yuy2),  // %0
        "+r"(dst_u),     // %1
        "+r"(dst_v),     // %2
        "+r"(width)      // %3
      :
      : "memory", "cc", "xmm0", "xmm1", "xmm5");
4181 4182
}

Frank Barchard's avatar
Frank Barchard committed
4183
void UYVYToYRow_SSE2(const uint8_t* src_uyvy, uint8_t* dst_y, int width) {
4184 4185 4186 4187 4188 4189 4190 4191 4192 4193 4194 4195 4196 4197 4198 4199 4200 4201 4202
  asm volatile(

      LABELALIGN
      "1:                                        \n"
      "movdqu    (%0),%%xmm0                     \n"
      "movdqu    0x10(%0),%%xmm1                 \n"
      "lea       0x20(%0),%0                     \n"
      "psrlw     $0x8,%%xmm0                     \n"
      "psrlw     $0x8,%%xmm1                     \n"
      "packuswb  %%xmm1,%%xmm0                   \n"
      "movdqu    %%xmm0,(%1)                     \n"
      "lea       0x10(%1),%1                     \n"
      "sub       $0x10,%2                        \n"
      "jg        1b                              \n"
      : "+r"(src_uyvy),  // %0
        "+r"(dst_y),     // %1
        "+r"(width)      // %2
      :
      : "memory", "cc", "xmm0", "xmm1");
4203 4204
}

Frank Barchard's avatar
Frank Barchard committed
4205
void UYVYToUVRow_SSE2(const uint8_t* src_uyvy,
4206
                      int stride_uyvy,
Frank Barchard's avatar
Frank Barchard committed
4207 4208
                      uint8_t* dst_u,
                      uint8_t* dst_v,
4209
                      int width) {
4210 4211 4212 4213
  asm volatile(
      "pcmpeqb   %%xmm5,%%xmm5                   \n"
      "psrlw     $0x8,%%xmm5                     \n"
      "sub       %1,%2                           \n"
Frank Barchard's avatar
Frank Barchard committed
4214

4215 4216 4217 4218 4219 4220 4221 4222 4223 4224 4225 4226 4227 4228 4229 4230 4231 4232 4233 4234 4235 4236 4237 4238 4239 4240 4241 4242
      LABELALIGN
      "1:                                        \n"
      "movdqu    (%0),%%xmm0                     \n"
      "movdqu    0x10(%0),%%xmm1                 \n"
      "movdqu    0x00(%0,%4,1),%%xmm2            \n"
      "movdqu    0x10(%0,%4,1),%%xmm3            \n"
      "lea       0x20(%0),%0                     \n"
      "pavgb     %%xmm2,%%xmm0                   \n"
      "pavgb     %%xmm3,%%xmm1                   \n"
      "pand      %%xmm5,%%xmm0                   \n"
      "pand      %%xmm5,%%xmm1                   \n"
      "packuswb  %%xmm1,%%xmm0                   \n"
      "movdqa    %%xmm0,%%xmm1                   \n"
      "pand      %%xmm5,%%xmm0                   \n"
      "packuswb  %%xmm0,%%xmm0                   \n"
      "psrlw     $0x8,%%xmm1                     \n"
      "packuswb  %%xmm1,%%xmm1                   \n"
      "movq      %%xmm0,(%1)                     \n"
      "movq    %%xmm1,0x00(%1,%2,1)              \n"
      "lea       0x8(%1),%1                      \n"
      "sub       $0x10,%3                        \n"
      "jg        1b                              \n"
      : "+r"(src_uyvy),               // %0
        "+r"(dst_u),                  // %1
        "+r"(dst_v),                  // %2
        "+r"(width)                   // %3
      : "r"((intptr_t)(stride_uyvy))  // %4
      : "memory", "cc", "xmm0", "xmm1", "xmm2", "xmm3", "xmm5");
4243
}
4244

Frank Barchard's avatar
Frank Barchard committed
4245 4246 4247
void UYVYToUV422Row_SSE2(const uint8_t* src_uyvy,
                         uint8_t* dst_u,
                         uint8_t* dst_v,
4248
                         int width) {
4249 4250 4251 4252
  asm volatile(
      "pcmpeqb   %%xmm5,%%xmm5                   \n"
      "psrlw     $0x8,%%xmm5                     \n"
      "sub       %1,%2                           \n"
Frank Barchard's avatar
Frank Barchard committed
4253

4254 4255 4256 4257 4258 4259 4260 4261 4262 4263 4264 4265 4266 4267 4268 4269 4270 4271 4272 4273 4274 4275 4276 4277
      LABELALIGN
      "1:                                        \n"
      "movdqu    (%0),%%xmm0                     \n"
      "movdqu    0x10(%0),%%xmm1                 \n"
      "lea       0x20(%0),%0                     \n"
      "pand      %%xmm5,%%xmm0                   \n"
      "pand      %%xmm5,%%xmm1                   \n"
      "packuswb  %%xmm1,%%xmm0                   \n"
      "movdqa    %%xmm0,%%xmm1                   \n"
      "pand      %%xmm5,%%xmm0                   \n"
      "packuswb  %%xmm0,%%xmm0                   \n"
      "psrlw     $0x8,%%xmm1                     \n"
      "packuswb  %%xmm1,%%xmm1                   \n"
      "movq      %%xmm0,(%1)                     \n"
      "movq    %%xmm1,0x00(%1,%2,1)              \n"
      "lea       0x8(%1),%1                      \n"
      "sub       $0x10,%3                        \n"
      "jg        1b                              \n"
      : "+r"(src_uyvy),  // %0
        "+r"(dst_u),     // %1
        "+r"(dst_v),     // %2
        "+r"(width)      // %3
      :
      : "memory", "cc", "xmm0", "xmm1", "xmm5");
4278
}
4279
#endif  // HAS_YUY2TOYROW_SSE2
4280

4281
#ifdef HAS_YUY2TOYROW_AVX2
Frank Barchard's avatar
Frank Barchard committed
4282
void YUY2ToYRow_AVX2(const uint8_t* src_yuy2, uint8_t* dst_y, int width) {
4283 4284 4285
  asm volatile(
      "vpcmpeqb  %%ymm5,%%ymm5,%%ymm5            \n"
      "vpsrlw    $0x8,%%ymm5,%%ymm5              \n"
Frank Barchard's avatar
Frank Barchard committed
4286

4287 4288 4289 4290 4291 4292 4293 4294 4295 4296 4297 4298 4299 4300 4301 4302 4303 4304 4305
      LABELALIGN
      "1:                                        \n"
      "vmovdqu   (%0),%%ymm0                     \n"
      "vmovdqu   0x20(%0),%%ymm1                 \n"
      "lea       0x40(%0),%0                     \n"
      "vpand     %%ymm5,%%ymm0,%%ymm0            \n"
      "vpand     %%ymm5,%%ymm1,%%ymm1            \n"
      "vpackuswb %%ymm1,%%ymm0,%%ymm0            \n"
      "vpermq    $0xd8,%%ymm0,%%ymm0             \n"
      "vmovdqu   %%ymm0,(%1)                     \n"
      "lea      0x20(%1),%1                      \n"
      "sub       $0x20,%2                        \n"
      "jg        1b                              \n"
      "vzeroupper                                \n"
      : "+r"(src_yuy2),  // %0
        "+r"(dst_y),     // %1
        "+r"(width)      // %2
      :
      : "memory", "cc", "xmm0", "xmm1", "xmm5");
4306 4307
}

Frank Barchard's avatar
Frank Barchard committed
4308
void YUY2ToUVRow_AVX2(const uint8_t* src_yuy2,
4309
                      int stride_yuy2,
Frank Barchard's avatar
Frank Barchard committed
4310 4311
                      uint8_t* dst_u,
                      uint8_t* dst_v,
4312
                      int width) {
4313 4314 4315 4316
  asm volatile(
      "vpcmpeqb  %%ymm5,%%ymm5,%%ymm5            \n"
      "vpsrlw    $0x8,%%ymm5,%%ymm5              \n"
      "sub       %1,%2                           \n"
Frank Barchard's avatar
Frank Barchard committed
4317

4318 4319 4320 4321 4322 4323 4324 4325 4326 4327 4328 4329 4330 4331 4332 4333 4334 4335 4336 4337 4338 4339 4340 4341 4342 4343 4344 4345 4346
      LABELALIGN
      "1:                                        \n"
      "vmovdqu   (%0),%%ymm0                     \n"
      "vmovdqu   0x20(%0),%%ymm1                 \n"
      "vpavgb    0x00(%0,%4,1),%%ymm0,%%ymm0     \n"
      "vpavgb    0x20(%0,%4,1),%%ymm1,%%ymm1     \n"
      "lea       0x40(%0),%0                     \n"
      "vpsrlw    $0x8,%%ymm0,%%ymm0              \n"
      "vpsrlw    $0x8,%%ymm1,%%ymm1              \n"
      "vpackuswb %%ymm1,%%ymm0,%%ymm0            \n"
      "vpermq    $0xd8,%%ymm0,%%ymm0             \n"
      "vpand     %%ymm5,%%ymm0,%%ymm1            \n"
      "vpsrlw    $0x8,%%ymm0,%%ymm0              \n"
      "vpackuswb %%ymm1,%%ymm1,%%ymm1            \n"
      "vpackuswb %%ymm0,%%ymm0,%%ymm0            \n"
      "vpermq    $0xd8,%%ymm1,%%ymm1             \n"
      "vpermq    $0xd8,%%ymm0,%%ymm0             \n"
      "vextractf128 $0x0,%%ymm1,(%1)             \n"
      "vextractf128 $0x0,%%ymm0,0x00(%1,%2,1)    \n"
      "lea      0x10(%1),%1                      \n"
      "sub       $0x20,%3                        \n"
      "jg        1b                              \n"
      "vzeroupper                                \n"
      : "+r"(src_yuy2),               // %0
        "+r"(dst_u),                  // %1
        "+r"(dst_v),                  // %2
        "+r"(width)                   // %3
      : "r"((intptr_t)(stride_yuy2))  // %4
      : "memory", "cc", "xmm0", "xmm1", "xmm5");
4347 4348
}

Frank Barchard's avatar
Frank Barchard committed
4349 4350 4351
void YUY2ToUV422Row_AVX2(const uint8_t* src_yuy2,
                         uint8_t* dst_u,
                         uint8_t* dst_v,
4352
                         int width) {
4353 4354 4355 4356
  asm volatile(
      "vpcmpeqb  %%ymm5,%%ymm5,%%ymm5            \n"
      "vpsrlw    $0x8,%%ymm5,%%ymm5              \n"
      "sub       %1,%2                           \n"
Frank Barchard's avatar
Frank Barchard committed
4357

4358 4359 4360 4361 4362 4363 4364 4365 4366 4367 4368 4369 4370 4371 4372 4373 4374 4375 4376 4377 4378 4379 4380 4381 4382 4383 4384
      LABELALIGN
      "1:                                        \n"
      "vmovdqu   (%0),%%ymm0                     \n"
      "vmovdqu   0x20(%0),%%ymm1                 \n"
      "lea       0x40(%0),%0                     \n"
      "vpsrlw    $0x8,%%ymm0,%%ymm0              \n"
      "vpsrlw    $0x8,%%ymm1,%%ymm1              \n"
      "vpackuswb %%ymm1,%%ymm0,%%ymm0            \n"
      "vpermq    $0xd8,%%ymm0,%%ymm0             \n"
      "vpand     %%ymm5,%%ymm0,%%ymm1            \n"
      "vpsrlw    $0x8,%%ymm0,%%ymm0              \n"
      "vpackuswb %%ymm1,%%ymm1,%%ymm1            \n"
      "vpackuswb %%ymm0,%%ymm0,%%ymm0            \n"
      "vpermq    $0xd8,%%ymm1,%%ymm1             \n"
      "vpermq    $0xd8,%%ymm0,%%ymm0             \n"
      "vextractf128 $0x0,%%ymm1,(%1)             \n"
      "vextractf128 $0x0,%%ymm0,0x00(%1,%2,1)    \n"
      "lea      0x10(%1),%1                      \n"
      "sub       $0x20,%3                        \n"
      "jg        1b                              \n"
      "vzeroupper                                \n"
      : "+r"(src_yuy2),  // %0
        "+r"(dst_u),     // %1
        "+r"(dst_v),     // %2
        "+r"(width)      // %3
      :
      : "memory", "cc", "xmm0", "xmm1", "xmm5");
4385 4386
}

Frank Barchard's avatar
Frank Barchard committed
4387
void UYVYToYRow_AVX2(const uint8_t* src_uyvy, uint8_t* dst_y, int width) {
4388 4389 4390 4391 4392 4393 4394 4395 4396 4397 4398 4399 4400 4401 4402 4403 4404 4405 4406 4407 4408
  asm volatile(

      LABELALIGN
      "1:                                        \n"
      "vmovdqu   (%0),%%ymm0                     \n"
      "vmovdqu   0x20(%0),%%ymm1                 \n"
      "lea       0x40(%0),%0                     \n"
      "vpsrlw    $0x8,%%ymm0,%%ymm0              \n"
      "vpsrlw    $0x8,%%ymm1,%%ymm1              \n"
      "vpackuswb %%ymm1,%%ymm0,%%ymm0            \n"
      "vpermq    $0xd8,%%ymm0,%%ymm0             \n"
      "vmovdqu   %%ymm0,(%1)                     \n"
      "lea      0x20(%1),%1                      \n"
      "sub       $0x20,%2                        \n"
      "jg        1b                              \n"
      "vzeroupper                                \n"
      : "+r"(src_uyvy),  // %0
        "+r"(dst_y),     // %1
        "+r"(width)      // %2
      :
      : "memory", "cc", "xmm0", "xmm1", "xmm5");
4409
}
Frank Barchard's avatar
Frank Barchard committed
4410
void UYVYToUVRow_AVX2(const uint8_t* src_uyvy,
4411
                      int stride_uyvy,
Frank Barchard's avatar
Frank Barchard committed
4412 4413
                      uint8_t* dst_u,
                      uint8_t* dst_v,
4414
                      int width) {
4415 4416 4417 4418
  asm volatile(
      "vpcmpeqb  %%ymm5,%%ymm5,%%ymm5            \n"
      "vpsrlw    $0x8,%%ymm5,%%ymm5              \n"
      "sub       %1,%2                           \n"
4419

4420 4421 4422 4423 4424 4425 4426 4427 4428 4429 4430 4431 4432 4433 4434 4435 4436 4437 4438 4439 4440 4441 4442 4443 4444 4445 4446 4447 4448
      LABELALIGN
      "1:                                        \n"
      "vmovdqu   (%0),%%ymm0                     \n"
      "vmovdqu   0x20(%0),%%ymm1                 \n"
      "vpavgb    0x00(%0,%4,1),%%ymm0,%%ymm0     \n"
      "vpavgb    0x20(%0,%4,1),%%ymm1,%%ymm1     \n"
      "lea       0x40(%0),%0                     \n"
      "vpand     %%ymm5,%%ymm0,%%ymm0            \n"
      "vpand     %%ymm5,%%ymm1,%%ymm1            \n"
      "vpackuswb %%ymm1,%%ymm0,%%ymm0            \n"
      "vpermq    $0xd8,%%ymm0,%%ymm0             \n"
      "vpand     %%ymm5,%%ymm0,%%ymm1            \n"
      "vpsrlw    $0x8,%%ymm0,%%ymm0              \n"
      "vpackuswb %%ymm1,%%ymm1,%%ymm1            \n"
      "vpackuswb %%ymm0,%%ymm0,%%ymm0            \n"
      "vpermq    $0xd8,%%ymm1,%%ymm1             \n"
      "vpermq    $0xd8,%%ymm0,%%ymm0             \n"
      "vextractf128 $0x0,%%ymm1,(%1)             \n"
      "vextractf128 $0x0,%%ymm0,0x00(%1,%2,1)    \n"
      "lea      0x10(%1),%1                      \n"
      "sub       $0x20,%3                        \n"
      "jg        1b                              \n"
      "vzeroupper                                \n"
      : "+r"(src_uyvy),               // %0
        "+r"(dst_u),                  // %1
        "+r"(dst_v),                  // %2
        "+r"(width)                   // %3
      : "r"((intptr_t)(stride_uyvy))  // %4
      : "memory", "cc", "xmm0", "xmm1", "xmm5");
4449 4450
}

Frank Barchard's avatar
Frank Barchard committed
4451 4452 4453
void UYVYToUV422Row_AVX2(const uint8_t* src_uyvy,
                         uint8_t* dst_u,
                         uint8_t* dst_v,
4454
                         int width) {
4455 4456 4457 4458
  asm volatile(
      "vpcmpeqb   %%ymm5,%%ymm5,%%ymm5           \n"
      "vpsrlw     $0x8,%%ymm5,%%ymm5             \n"
      "sub       %1,%2                           \n"
Frank Barchard's avatar
Frank Barchard committed
4459

4460 4461 4462 4463 4464 4465 4466 4467 4468 4469 4470 4471 4472 4473 4474 4475 4476 4477 4478 4479 4480 4481 4482 4483 4484 4485 4486
      LABELALIGN
      "1:                                        \n"
      "vmovdqu   (%0),%%ymm0                     \n"
      "vmovdqu   0x20(%0),%%ymm1                 \n"
      "lea       0x40(%0),%0                     \n"
      "vpand     %%ymm5,%%ymm0,%%ymm0            \n"
      "vpand     %%ymm5,%%ymm1,%%ymm1            \n"
      "vpackuswb %%ymm1,%%ymm0,%%ymm0            \n"
      "vpermq    $0xd8,%%ymm0,%%ymm0             \n"
      "vpand     %%ymm5,%%ymm0,%%ymm1            \n"
      "vpsrlw    $0x8,%%ymm0,%%ymm0              \n"
      "vpackuswb %%ymm1,%%ymm1,%%ymm1            \n"
      "vpackuswb %%ymm0,%%ymm0,%%ymm0            \n"
      "vpermq    $0xd8,%%ymm1,%%ymm1             \n"
      "vpermq    $0xd8,%%ymm0,%%ymm0             \n"
      "vextractf128 $0x0,%%ymm1,(%1)             \n"
      "vextractf128 $0x0,%%ymm0,0x00(%1,%2,1)    \n"
      "lea      0x10(%1),%1                      \n"
      "sub       $0x20,%3                        \n"
      "jg        1b                              \n"
      "vzeroupper                                \n"
      : "+r"(src_uyvy),  // %0
        "+r"(dst_u),     // %1
        "+r"(dst_v),     // %2
        "+r"(width)      // %3
      :
      : "memory", "cc", "xmm0", "xmm1", "xmm5");
4487
}
4488 4489
#endif  // HAS_YUY2TOYROW_AVX2

4490
#ifdef HAS_ARGBBLENDROW_SSSE3
4491
// Shuffle table for isolating alpha.
4492 4493
static const uvec8 kShuffleAlpha = {3u,  0x80, 3u,  0x80, 7u,  0x80, 7u,  0x80,
                                    11u, 0x80, 11u, 0x80, 15u, 0x80, 15u, 0x80};
4494 4495

// Blend 8 pixels at a time
Frank Barchard's avatar
Frank Barchard committed
4496 4497 4498
void ARGBBlendRow_SSSE3(const uint8_t* src_argb0,
                        const uint8_t* src_argb1,
                        uint8_t* dst_argb,
4499
                        int width) {
4500 4501 4502 4503 4504 4505 4506 4507 4508 4509 4510 4511 4512 4513 4514 4515 4516 4517 4518 4519 4520 4521 4522 4523 4524 4525 4526 4527 4528 4529 4530 4531 4532 4533 4534 4535 4536 4537 4538 4539 4540 4541 4542 4543 4544 4545 4546 4547 4548 4549 4550 4551 4552 4553 4554 4555 4556 4557 4558 4559 4560 4561 4562 4563 4564 4565 4566 4567 4568 4569 4570 4571 4572 4573
  asm volatile(
      "pcmpeqb   %%xmm7,%%xmm7                   \n"
      "psrlw     $0xf,%%xmm7                     \n"
      "pcmpeqb   %%xmm6,%%xmm6                   \n"
      "psrlw     $0x8,%%xmm6                     \n"
      "pcmpeqb   %%xmm5,%%xmm5                   \n"
      "psllw     $0x8,%%xmm5                     \n"
      "pcmpeqb   %%xmm4,%%xmm4                   \n"
      "pslld     $0x18,%%xmm4                    \n"
      "sub       $0x4,%3                         \n"
      "jl        49f                             \n"

      // 4 pixel loop.
      LABELALIGN
      "40:                                       \n"
      "movdqu    (%0),%%xmm3                     \n"
      "lea       0x10(%0),%0                     \n"
      "movdqa    %%xmm3,%%xmm0                   \n"
      "pxor      %%xmm4,%%xmm3                   \n"
      "movdqu    (%1),%%xmm2                     \n"
      "pshufb    %4,%%xmm3                       \n"
      "pand      %%xmm6,%%xmm2                   \n"
      "paddw     %%xmm7,%%xmm3                   \n"
      "pmullw    %%xmm3,%%xmm2                   \n"
      "movdqu    (%1),%%xmm1                     \n"
      "lea       0x10(%1),%1                     \n"
      "psrlw     $0x8,%%xmm1                     \n"
      "por       %%xmm4,%%xmm0                   \n"
      "pmullw    %%xmm3,%%xmm1                   \n"
      "psrlw     $0x8,%%xmm2                     \n"
      "paddusb   %%xmm2,%%xmm0                   \n"
      "pand      %%xmm5,%%xmm1                   \n"
      "paddusb   %%xmm1,%%xmm0                   \n"
      "movdqu    %%xmm0,(%2)                     \n"
      "lea       0x10(%2),%2                     \n"
      "sub       $0x4,%3                         \n"
      "jge       40b                             \n"

      "49:                                       \n"
      "add       $0x3,%3                         \n"
      "jl        99f                             \n"

      // 1 pixel loop.
      "91:                                       \n"
      "movd      (%0),%%xmm3                     \n"
      "lea       0x4(%0),%0                      \n"
      "movdqa    %%xmm3,%%xmm0                   \n"
      "pxor      %%xmm4,%%xmm3                   \n"
      "movd      (%1),%%xmm2                     \n"
      "pshufb    %4,%%xmm3                       \n"
      "pand      %%xmm6,%%xmm2                   \n"
      "paddw     %%xmm7,%%xmm3                   \n"
      "pmullw    %%xmm3,%%xmm2                   \n"
      "movd      (%1),%%xmm1                     \n"
      "lea       0x4(%1),%1                      \n"
      "psrlw     $0x8,%%xmm1                     \n"
      "por       %%xmm4,%%xmm0                   \n"
      "pmullw    %%xmm3,%%xmm1                   \n"
      "psrlw     $0x8,%%xmm2                     \n"
      "paddusb   %%xmm2,%%xmm0                   \n"
      "pand      %%xmm5,%%xmm1                   \n"
      "paddusb   %%xmm1,%%xmm0                   \n"
      "movd      %%xmm0,(%2)                     \n"
      "lea       0x4(%2),%2                      \n"
      "sub       $0x1,%3                         \n"
      "jge       91b                             \n"
      "99:                                       \n"
      : "+r"(src_argb0),    // %0
        "+r"(src_argb1),    // %1
        "+r"(dst_argb),     // %2
        "+r"(width)         // %3
      : "m"(kShuffleAlpha)  // %4
      : "memory", "cc", "xmm0", "xmm1", "xmm2", "xmm3", "xmm4", "xmm5", "xmm6",
        "xmm7");
4574
}
4575
#endif  // HAS_ARGBBLENDROW_SSSE3
4576

4577 4578
#ifdef HAS_BLENDPLANEROW_SSSE3
// Blend 8 pixels at a time.
4579 4580 4581 4582
// unsigned version of math
// =((A2*C2)+(B2*(255-C2))+255)/256
// signed version of math
// =(((A2-128)*C2)+((B2-128)*(255-C2))+32768+127)/256
Frank Barchard's avatar
Frank Barchard committed
4583 4584 4585 4586
void BlendPlaneRow_SSSE3(const uint8_t* src0,
                         const uint8_t* src1,
                         const uint8_t* alpha,
                         uint8_t* dst,
4587 4588 4589 4590 4591 4592 4593 4594 4595 4596 4597 4598 4599 4600 4601 4602
                         int width) {
  asm volatile(
      "pcmpeqb    %%xmm5,%%xmm5                  \n"
      "psllw      $0x8,%%xmm5                    \n"
      "mov        $0x80808080,%%eax              \n"
      "movd       %%eax,%%xmm6                   \n"
      "pshufd     $0x0,%%xmm6,%%xmm6             \n"
      "mov        $0x807f807f,%%eax              \n"
      "movd       %%eax,%%xmm7                   \n"
      "pshufd     $0x0,%%xmm7,%%xmm7             \n"
      "sub        %2,%0                          \n"
      "sub        %2,%1                          \n"
      "sub        %2,%3                          \n"

      // 8 pixel loop.
      LABELALIGN
Frank Barchard's avatar
Frank Barchard committed
4603
      "1:                                        \n"
4604 4605 4606 4607 4608 4609 4610 4611 4612 4613 4614 4615 4616 4617 4618 4619 4620 4621 4622 4623 4624 4625
      "movq       (%2),%%xmm0                    \n"
      "punpcklbw  %%xmm0,%%xmm0                  \n"
      "pxor       %%xmm5,%%xmm0                  \n"
      "movq       (%0,%2,1),%%xmm1               \n"
      "movq       (%1,%2,1),%%xmm2               \n"
      "punpcklbw  %%xmm2,%%xmm1                  \n"
      "psubb      %%xmm6,%%xmm1                  \n"
      "pmaddubsw  %%xmm1,%%xmm0                  \n"
      "paddw      %%xmm7,%%xmm0                  \n"
      "psrlw      $0x8,%%xmm0                    \n"
      "packuswb   %%xmm0,%%xmm0                  \n"
      "movq       %%xmm0,(%3,%2,1)               \n"
      "lea        0x8(%2),%2                     \n"
      "sub        $0x8,%4                        \n"
      "jg        1b                              \n"
      : "+r"(src0),   // %0
        "+r"(src1),   // %1
        "+r"(alpha),  // %2
        "+r"(dst),    // %3
        "+rm"(width)  // %4
        ::"memory",
        "cc", "eax", "xmm0", "xmm1", "xmm2", "xmm5", "xmm6", "xmm7");
4626 4627 4628
}
#endif  // HAS_BLENDPLANEROW_SSSE3

4629
#ifdef HAS_BLENDPLANEROW_AVX2
4630 4631 4632 4633 4634
// Blend 32 pixels at a time.
// unsigned version of math
// =((A2*C2)+(B2*(255-C2))+255)/256
// signed version of math
// =(((A2-128)*C2)+((B2-128)*(255-C2))+32768+127)/256
Frank Barchard's avatar
Frank Barchard committed
4635 4636 4637 4638
void BlendPlaneRow_AVX2(const uint8_t* src0,
                        const uint8_t* src1,
                        const uint8_t* alpha,
                        uint8_t* dst,
4639 4640 4641 4642 4643 4644 4645 4646 4647 4648 4649 4650 4651 4652 4653 4654
                        int width) {
  asm volatile(
      "vpcmpeqb   %%ymm5,%%ymm5,%%ymm5           \n"
      "vpsllw     $0x8,%%ymm5,%%ymm5             \n"
      "mov        $0x80808080,%%eax              \n"
      "vmovd      %%eax,%%xmm6                   \n"
      "vbroadcastss %%xmm6,%%ymm6                \n"
      "mov        $0x807f807f,%%eax              \n"
      "vmovd      %%eax,%%xmm7                   \n"
      "vbroadcastss %%xmm7,%%ymm7                \n"
      "sub        %2,%0                          \n"
      "sub        %2,%1                          \n"
      "sub        %2,%3                          \n"

      // 32 pixel loop.
      LABELALIGN
Frank Barchard's avatar
Frank Barchard committed
4655
      "1:                                        \n"
4656 4657 4658 4659 4660 4661 4662 4663 4664 4665 4666 4667 4668 4669 4670 4671 4672 4673 4674 4675 4676 4677 4678 4679 4680 4681 4682 4683 4684 4685 4686
      "vmovdqu    (%2),%%ymm0                    \n"
      "vpunpckhbw %%ymm0,%%ymm0,%%ymm3           \n"
      "vpunpcklbw %%ymm0,%%ymm0,%%ymm0           \n"
      "vpxor      %%ymm5,%%ymm3,%%ymm3           \n"
      "vpxor      %%ymm5,%%ymm0,%%ymm0           \n"
      "vmovdqu    (%0,%2,1),%%ymm1               \n"
      "vmovdqu    (%1,%2,1),%%ymm2               \n"
      "vpunpckhbw %%ymm2,%%ymm1,%%ymm4           \n"
      "vpunpcklbw %%ymm2,%%ymm1,%%ymm1           \n"
      "vpsubb     %%ymm6,%%ymm4,%%ymm4           \n"
      "vpsubb     %%ymm6,%%ymm1,%%ymm1           \n"
      "vpmaddubsw %%ymm4,%%ymm3,%%ymm3           \n"
      "vpmaddubsw %%ymm1,%%ymm0,%%ymm0           \n"
      "vpaddw     %%ymm7,%%ymm3,%%ymm3           \n"
      "vpaddw     %%ymm7,%%ymm0,%%ymm0           \n"
      "vpsrlw     $0x8,%%ymm3,%%ymm3             \n"
      "vpsrlw     $0x8,%%ymm0,%%ymm0             \n"
      "vpackuswb  %%ymm3,%%ymm0,%%ymm0           \n"
      "vmovdqu    %%ymm0,(%3,%2,1)               \n"
      "lea        0x20(%2),%2                    \n"
      "sub        $0x20,%4                       \n"
      "jg        1b                              \n"
      "vzeroupper                                \n"
      : "+r"(src0),   // %0
        "+r"(src1),   // %1
        "+r"(alpha),  // %2
        "+r"(dst),    // %3
        "+rm"(width)  // %4
        ::"memory",
        "cc", "eax", "xmm0", "xmm1", "xmm2", "xmm3", "xmm4", "xmm5", "xmm6",
        "xmm7");
4687 4688
}
#endif  // HAS_BLENDPLANEROW_AVX2
4689

4690
#ifdef HAS_ARGBATTENUATEROW_SSSE3
4691
// Shuffle table duplicating alpha
4692 4693 4694 4695
static const uvec8 kShuffleAlpha0 = {3u, 3u, 3u, 3u, 3u, 3u, 128u, 128u,
                                     7u, 7u, 7u, 7u, 7u, 7u, 128u, 128u};
static const uvec8 kShuffleAlpha1 = {11u, 11u, 11u, 11u, 11u, 11u, 128u, 128u,
                                     15u, 15u, 15u, 15u, 15u, 15u, 128u, 128u};
4696
// Attenuate 4 pixels at a time.
4697 4698 4699 4700 4701 4702 4703 4704
void ARGBAttenuateRow_SSSE3(const uint8_t* src_argb,
                            uint8_t* dst_argb,
                            int width) {
  asm volatile(
      "pcmpeqb   %%xmm3,%%xmm3                   \n"
      "pslld     $0x18,%%xmm3                    \n"
      "movdqa    %3,%%xmm4                       \n"
      "movdqa    %4,%%xmm5                       \n"
4705

4706 4707 4708 4709 4710 4711 4712 4713 4714 4715 4716 4717 4718 4719 4720 4721 4722 4723 4724 4725 4726 4727 4728 4729 4730 4731 4732 4733 4734 4735
      // 4 pixel loop.
      LABELALIGN
      "1:                                        \n"
      "movdqu    (%0),%%xmm0                     \n"
      "pshufb    %%xmm4,%%xmm0                   \n"
      "movdqu    (%0),%%xmm1                     \n"
      "punpcklbw %%xmm1,%%xmm1                   \n"
      "pmulhuw   %%xmm1,%%xmm0                   \n"
      "movdqu    (%0),%%xmm1                     \n"
      "pshufb    %%xmm5,%%xmm1                   \n"
      "movdqu    (%0),%%xmm2                     \n"
      "punpckhbw %%xmm2,%%xmm2                   \n"
      "pmulhuw   %%xmm2,%%xmm1                   \n"
      "movdqu    (%0),%%xmm2                     \n"
      "lea       0x10(%0),%0                     \n"
      "pand      %%xmm3,%%xmm2                   \n"
      "psrlw     $0x8,%%xmm0                     \n"
      "psrlw     $0x8,%%xmm1                     \n"
      "packuswb  %%xmm1,%%xmm0                   \n"
      "por       %%xmm2,%%xmm0                   \n"
      "movdqu    %%xmm0,(%1)                     \n"
      "lea       0x10(%1),%1                     \n"
      "sub       $0x4,%2                         \n"
      "jg        1b                              \n"
      : "+r"(src_argb),       // %0
        "+r"(dst_argb),       // %1
        "+r"(width)           // %2
      : "m"(kShuffleAlpha0),  // %3
        "m"(kShuffleAlpha1)   // %4
      : "memory", "cc", "xmm0", "xmm1", "xmm2", "xmm3", "xmm4", "xmm5");
4736
}
4737
#endif  // HAS_ARGBATTENUATEROW_SSSE3
4738

4739 4740
#ifdef HAS_ARGBATTENUATEROW_AVX2
// Shuffle table duplicating alpha.
4741 4742 4743
static const uvec8 kShuffleAlpha_AVX2 = {6u,   7u,   6u,   7u,  6u,  7u,
                                         128u, 128u, 14u,  15u, 14u, 15u,
                                         14u,  15u,  128u, 128u};
4744
// Attenuate 8 pixels at a time.
4745 4746 4747 4748 4749 4750 4751 4752
void ARGBAttenuateRow_AVX2(const uint8_t* src_argb,
                           uint8_t* dst_argb,
                           int width) {
  asm volatile(
      "vbroadcastf128 %3,%%ymm4                  \n"
      "vpcmpeqb   %%ymm5,%%ymm5,%%ymm5           \n"
      "vpslld     $0x18,%%ymm5,%%ymm5            \n"
      "sub        %0,%1                          \n"
4753

4754 4755 4756 4757 4758 4759 4760 4761 4762 4763 4764 4765 4766 4767 4768 4769 4770 4771 4772 4773 4774 4775 4776 4777 4778
      // 8 pixel loop.
      LABELALIGN
      "1:                                        \n"
      "vmovdqu    (%0),%%ymm6                    \n"
      "vpunpcklbw %%ymm6,%%ymm6,%%ymm0           \n"
      "vpunpckhbw %%ymm6,%%ymm6,%%ymm1           \n"
      "vpshufb    %%ymm4,%%ymm0,%%ymm2           \n"
      "vpshufb    %%ymm4,%%ymm1,%%ymm3           \n"
      "vpmulhuw   %%ymm2,%%ymm0,%%ymm0           \n"
      "vpmulhuw   %%ymm3,%%ymm1,%%ymm1           \n"
      "vpand      %%ymm5,%%ymm6,%%ymm6           \n"
      "vpsrlw     $0x8,%%ymm0,%%ymm0             \n"
      "vpsrlw     $0x8,%%ymm1,%%ymm1             \n"
      "vpackuswb  %%ymm1,%%ymm0,%%ymm0           \n"
      "vpor       %%ymm6,%%ymm0,%%ymm0           \n"
      "vmovdqu    %%ymm0,0x00(%0,%1,1)           \n"
      "lea       0x20(%0),%0                     \n"
      "sub        $0x8,%2                        \n"
      "jg        1b                              \n"
      "vzeroupper                                \n"
      : "+r"(src_argb),          // %0
        "+r"(dst_argb),          // %1
        "+r"(width)              // %2
      : "m"(kShuffleAlpha_AVX2)  // %3
      : "memory", "cc", "xmm0", "xmm1", "xmm2", "xmm3", "xmm4", "xmm5", "xmm6");
4779 4780 4781
}
#endif  // HAS_ARGBATTENUATEROW_AVX2

4782
#ifdef HAS_ARGBUNATTENUATEROW_SSE2
4783
// Unattenuate 4 pixels at a time.
Frank Barchard's avatar
Frank Barchard committed
4784 4785
void ARGBUnattenuateRow_SSE2(const uint8_t* src_argb,
                             uint8_t* dst_argb,
4786
                             int width) {
4787
  uintptr_t alpha;
4788 4789 4790 4791 4792 4793 4794 4795 4796 4797 4798 4799 4800 4801 4802 4803 4804 4805 4806 4807 4808 4809 4810 4811 4812 4813 4814 4815 4816 4817 4818 4819 4820 4821 4822 4823
  asm volatile(
      // 4 pixel loop.
      LABELALIGN
      "1:                                        \n"
      "movdqu    (%0),%%xmm0                     \n"
      "movzb     0x03(%0),%3                     \n"
      "punpcklbw %%xmm0,%%xmm0                   \n"
      "movd      0x00(%4,%3,4),%%xmm2            \n"
      "movzb     0x07(%0),%3                     \n"
      "movd      0x00(%4,%3,4),%%xmm3            \n"
      "pshuflw   $0x40,%%xmm2,%%xmm2             \n"
      "pshuflw   $0x40,%%xmm3,%%xmm3             \n"
      "movlhps   %%xmm3,%%xmm2                   \n"
      "pmulhuw   %%xmm2,%%xmm0                   \n"
      "movdqu    (%0),%%xmm1                     \n"
      "movzb     0x0b(%0),%3                     \n"
      "punpckhbw %%xmm1,%%xmm1                   \n"
      "movd      0x00(%4,%3,4),%%xmm2            \n"
      "movzb     0x0f(%0),%3                     \n"
      "movd      0x00(%4,%3,4),%%xmm3            \n"
      "pshuflw   $0x40,%%xmm2,%%xmm2             \n"
      "pshuflw   $0x40,%%xmm3,%%xmm3             \n"
      "movlhps   %%xmm3,%%xmm2                   \n"
      "pmulhuw   %%xmm2,%%xmm1                   \n"
      "lea       0x10(%0),%0                     \n"
      "packuswb  %%xmm1,%%xmm0                   \n"
      "movdqu    %%xmm0,(%1)                     \n"
      "lea       0x10(%1),%1                     \n"
      "sub       $0x4,%2                         \n"
      "jg        1b                              \n"
      : "+r"(src_argb),     // %0
        "+r"(dst_argb),     // %1
        "+r"(width),        // %2
        "=&r"(alpha)        // %3
      : "r"(fixed_invtbl8)  // %4
      : "memory", "cc", "xmm0", "xmm1", "xmm2", "xmm3", "xmm4", "xmm5");
4824
}
4825
#endif  // HAS_ARGBUNATTENUATEROW_SSE2
4826

4827 4828 4829
#ifdef HAS_ARGBUNATTENUATEROW_AVX2
// Shuffle table duplicating alpha.
static const uvec8 kUnattenShuffleAlpha_AVX2 = {
4830
    0u, 1u, 0u, 1u, 0u, 1u, 6u, 7u, 8u, 9u, 8u, 9u, 8u, 9u, 14u, 15u};
4831
// Unattenuate 8 pixels at a time.
Frank Barchard's avatar
Frank Barchard committed
4832 4833
void ARGBUnattenuateRow_AVX2(const uint8_t* src_argb,
                             uint8_t* dst_argb,
4834
                             int width) {
4835
  uintptr_t alpha;
4836 4837 4838
  asm volatile(
      "sub        %0,%1                          \n"
      "vbroadcastf128 %5,%%ymm5                  \n"
4839

4840 4841 4842 4843 4844 4845 4846 4847 4848 4849 4850 4851 4852 4853 4854 4855 4856 4857 4858 4859 4860 4861 4862 4863 4864 4865 4866 4867 4868 4869 4870 4871 4872 4873 4874 4875 4876 4877 4878 4879 4880 4881 4882 4883 4884 4885 4886 4887 4888 4889 4890 4891
      // 8 pixel loop.
      LABELALIGN
      "1:                                        \n"
      // replace VPGATHER
      "movzb     0x03(%0),%3                     \n"
      "vmovd     0x00(%4,%3,4),%%xmm0            \n"
      "movzb     0x07(%0),%3                     \n"
      "vmovd     0x00(%4,%3,4),%%xmm1            \n"
      "movzb     0x0b(%0),%3                     \n"
      "vpunpckldq %%xmm1,%%xmm0,%%xmm6           \n"
      "vmovd     0x00(%4,%3,4),%%xmm2            \n"
      "movzb     0x0f(%0),%3                     \n"
      "vmovd     0x00(%4,%3,4),%%xmm3            \n"
      "movzb     0x13(%0),%3                     \n"
      "vpunpckldq %%xmm3,%%xmm2,%%xmm7           \n"
      "vmovd     0x00(%4,%3,4),%%xmm0            \n"
      "movzb     0x17(%0),%3                     \n"
      "vmovd     0x00(%4,%3,4),%%xmm1            \n"
      "movzb     0x1b(%0),%3                     \n"
      "vpunpckldq %%xmm1,%%xmm0,%%xmm0           \n"
      "vmovd     0x00(%4,%3,4),%%xmm2            \n"
      "movzb     0x1f(%0),%3                     \n"
      "vmovd     0x00(%4,%3,4),%%xmm3            \n"
      "vpunpckldq %%xmm3,%%xmm2,%%xmm2           \n"
      "vpunpcklqdq %%xmm7,%%xmm6,%%xmm3          \n"
      "vpunpcklqdq %%xmm2,%%xmm0,%%xmm0          \n"
      "vinserti128 $0x1,%%xmm0,%%ymm3,%%ymm3     \n"
      // end of VPGATHER

      "vmovdqu    (%0),%%ymm6                    \n"
      "vpunpcklbw %%ymm6,%%ymm6,%%ymm0           \n"
      "vpunpckhbw %%ymm6,%%ymm6,%%ymm1           \n"
      "vpunpcklwd %%ymm3,%%ymm3,%%ymm2           \n"
      "vpunpckhwd %%ymm3,%%ymm3,%%ymm3           \n"
      "vpshufb    %%ymm5,%%ymm2,%%ymm2           \n"
      "vpshufb    %%ymm5,%%ymm3,%%ymm3           \n"
      "vpmulhuw   %%ymm2,%%ymm0,%%ymm0           \n"
      "vpmulhuw   %%ymm3,%%ymm1,%%ymm1           \n"
      "vpackuswb  %%ymm1,%%ymm0,%%ymm0           \n"
      "vmovdqu    %%ymm0,0x00(%0,%1,1)           \n"
      "lea       0x20(%0),%0                     \n"
      "sub        $0x8,%2                        \n"
      "jg        1b                              \n"
      "vzeroupper                                \n"
      : "+r"(src_argb),                 // %0
        "+r"(dst_argb),                 // %1
        "+r"(width),                    // %2
        "=&r"(alpha)                    // %3
      : "r"(fixed_invtbl8),             // %4
        "m"(kUnattenShuffleAlpha_AVX2)  // %5
      : "memory", "cc", "xmm0", "xmm1", "xmm2", "xmm3", "xmm4", "xmm5", "xmm6",
        "xmm7");
4892 4893 4894
}
#endif  // HAS_ARGBUNATTENUATEROW_AVX2

4895 4896
#ifdef HAS_ARGBGRAYROW_SSSE3
// Convert 8 ARGB pixels (64 bytes) to 8 Gray ARGB pixels
Frank Barchard's avatar
Frank Barchard committed
4897
void ARGBGrayRow_SSSE3(const uint8_t* src_argb, uint8_t* dst_argb, int width) {
4898 4899 4900
  asm volatile(
      "movdqa    %3,%%xmm4                       \n"
      "movdqa    %4,%%xmm5                       \n"
4901

4902 4903 4904 4905 4906 4907 4908 4909 4910 4911 4912 4913 4914 4915 4916 4917 4918 4919 4920 4921 4922 4923 4924 4925 4926 4927 4928 4929 4930 4931 4932 4933 4934 4935 4936
      // 8 pixel loop.
      LABELALIGN
      "1:                                        \n"
      "movdqu    (%0),%%xmm0                     \n"
      "movdqu    0x10(%0),%%xmm1                 \n"
      "pmaddubsw %%xmm4,%%xmm0                   \n"
      "pmaddubsw %%xmm4,%%xmm1                   \n"
      "phaddw    %%xmm1,%%xmm0                   \n"
      "paddw     %%xmm5,%%xmm0                   \n"
      "psrlw     $0x7,%%xmm0                     \n"
      "packuswb  %%xmm0,%%xmm0                   \n"
      "movdqu    (%0),%%xmm2                     \n"
      "movdqu    0x10(%0),%%xmm3                 \n"
      "lea       0x20(%0),%0                     \n"
      "psrld     $0x18,%%xmm2                    \n"
      "psrld     $0x18,%%xmm3                    \n"
      "packuswb  %%xmm3,%%xmm2                   \n"
      "packuswb  %%xmm2,%%xmm2                   \n"
      "movdqa    %%xmm0,%%xmm3                   \n"
      "punpcklbw %%xmm0,%%xmm0                   \n"
      "punpcklbw %%xmm2,%%xmm3                   \n"
      "movdqa    %%xmm0,%%xmm1                   \n"
      "punpcklwd %%xmm3,%%xmm0                   \n"
      "punpckhwd %%xmm3,%%xmm1                   \n"
      "movdqu    %%xmm0,(%1)                     \n"
      "movdqu    %%xmm1,0x10(%1)                 \n"
      "lea       0x20(%1),%1                     \n"
      "sub       $0x8,%2                         \n"
      "jg        1b                              \n"
      : "+r"(src_argb),  // %0
        "+r"(dst_argb),  // %1
        "+r"(width)      // %2
      : "m"(kARGBToYJ),  // %3
        "m"(kAddYJ64)    // %4
      : "memory", "cc", "xmm0", "xmm1", "xmm2", "xmm3", "xmm4", "xmm5");
4937 4938
}
#endif  // HAS_ARGBGRAYROW_SSSE3
4939 4940 4941 4942 4943 4944

#ifdef HAS_ARGBSEPIAROW_SSSE3
//    b = (r * 35 + g * 68 + b * 17) >> 7
//    g = (r * 45 + g * 88 + b * 22) >> 7
//    r = (r * 50 + g * 98 + b * 24) >> 7
// Constant for ARGB color to sepia tone
4945 4946
static const vec8 kARGBToSepiaB = {17, 68, 35, 0, 17, 68, 35, 0,
                                   17, 68, 35, 0, 17, 68, 35, 0};
4947

4948 4949
static const vec8 kARGBToSepiaG = {22, 88, 45, 0, 22, 88, 45, 0,
                                   22, 88, 45, 0, 22, 88, 45, 0};
4950

4951 4952
static const vec8 kARGBToSepiaR = {24, 98, 50, 0, 24, 98, 50, 0,
                                   24, 98, 50, 0, 24, 98, 50, 0};
4953

4954
// Convert 8 ARGB pixels (32 bytes) to 8 Sepia ARGB pixels.
Frank Barchard's avatar
Frank Barchard committed
4955
void ARGBSepiaRow_SSSE3(uint8_t* dst_argb, int width) {
4956 4957 4958 4959
  asm volatile(
      "movdqa    %2,%%xmm2                       \n"
      "movdqa    %3,%%xmm3                       \n"
      "movdqa    %4,%%xmm4                       \n"
4960

4961 4962 4963 4964 4965 4966 4967 4968 4969 4970 4971 4972 4973 4974 4975 4976 4977 4978 4979 4980 4981 4982 4983 4984 4985 4986 4987 4988 4989 4990 4991 4992 4993 4994 4995 4996 4997 4998 4999 5000 5001 5002 5003 5004 5005 5006
      // 8 pixel loop.
      LABELALIGN
      "1:                                        \n"
      "movdqu    (%0),%%xmm0                     \n"
      "movdqu    0x10(%0),%%xmm6                 \n"
      "pmaddubsw %%xmm2,%%xmm0                   \n"
      "pmaddubsw %%xmm2,%%xmm6                   \n"
      "phaddw    %%xmm6,%%xmm0                   \n"
      "psrlw     $0x7,%%xmm0                     \n"
      "packuswb  %%xmm0,%%xmm0                   \n"
      "movdqu    (%0),%%xmm5                     \n"
      "movdqu    0x10(%0),%%xmm1                 \n"
      "pmaddubsw %%xmm3,%%xmm5                   \n"
      "pmaddubsw %%xmm3,%%xmm1                   \n"
      "phaddw    %%xmm1,%%xmm5                   \n"
      "psrlw     $0x7,%%xmm5                     \n"
      "packuswb  %%xmm5,%%xmm5                   \n"
      "punpcklbw %%xmm5,%%xmm0                   \n"
      "movdqu    (%0),%%xmm5                     \n"
      "movdqu    0x10(%0),%%xmm1                 \n"
      "pmaddubsw %%xmm4,%%xmm5                   \n"
      "pmaddubsw %%xmm4,%%xmm1                   \n"
      "phaddw    %%xmm1,%%xmm5                   \n"
      "psrlw     $0x7,%%xmm5                     \n"
      "packuswb  %%xmm5,%%xmm5                   \n"
      "movdqu    (%0),%%xmm6                     \n"
      "movdqu    0x10(%0),%%xmm1                 \n"
      "psrld     $0x18,%%xmm6                    \n"
      "psrld     $0x18,%%xmm1                    \n"
      "packuswb  %%xmm1,%%xmm6                   \n"
      "packuswb  %%xmm6,%%xmm6                   \n"
      "punpcklbw %%xmm6,%%xmm5                   \n"
      "movdqa    %%xmm0,%%xmm1                   \n"
      "punpcklwd %%xmm5,%%xmm0                   \n"
      "punpckhwd %%xmm5,%%xmm1                   \n"
      "movdqu    %%xmm0,(%0)                     \n"
      "movdqu    %%xmm1,0x10(%0)                 \n"
      "lea       0x20(%0),%0                     \n"
      "sub       $0x8,%1                         \n"
      "jg        1b                              \n"
      : "+r"(dst_argb),      // %0
        "+r"(width)          // %1
      : "m"(kARGBToSepiaB),  // %2
        "m"(kARGBToSepiaG),  // %3
        "m"(kARGBToSepiaR)   // %4
      : "memory", "cc", "xmm0", "xmm1", "xmm2", "xmm3", "xmm4", "xmm5", "xmm6");
5007 5008 5009
}
#endif  // HAS_ARGBSEPIAROW_SSSE3

5010 5011 5012
#ifdef HAS_ARGBCOLORMATRIXROW_SSSE3
// Tranform 8 ARGB pixels (32 bytes) with color matrix.
// Same as Sepia except matrix is provided.
Frank Barchard's avatar
Frank Barchard committed
5013 5014 5015
void ARGBColorMatrixRow_SSSE3(const uint8_t* src_argb,
                              uint8_t* dst_argb,
                              const int8_t* matrix_argb,
5016
                              int width) {
5017 5018 5019 5020 5021 5022
  asm volatile(
      "movdqu    (%3),%%xmm5                     \n"
      "pshufd    $0x00,%%xmm5,%%xmm2             \n"
      "pshufd    $0x55,%%xmm5,%%xmm3             \n"
      "pshufd    $0xaa,%%xmm5,%%xmm4             \n"
      "pshufd    $0xff,%%xmm5,%%xmm5             \n"
5023

5024 5025 5026 5027 5028 5029 5030 5031 5032 5033 5034 5035 5036 5037 5038 5039 5040 5041 5042 5043 5044 5045 5046 5047 5048 5049 5050 5051 5052 5053 5054 5055 5056 5057 5058 5059 5060 5061 5062 5063 5064 5065 5066 5067 5068 5069 5070 5071
      // 8 pixel loop.
      LABELALIGN
      "1:                                        \n"
      "movdqu    (%0),%%xmm0                     \n"
      "movdqu    0x10(%0),%%xmm7                 \n"
      "pmaddubsw %%xmm2,%%xmm0                   \n"
      "pmaddubsw %%xmm2,%%xmm7                   \n"
      "movdqu    (%0),%%xmm6                     \n"
      "movdqu    0x10(%0),%%xmm1                 \n"
      "pmaddubsw %%xmm3,%%xmm6                   \n"
      "pmaddubsw %%xmm3,%%xmm1                   \n"
      "phaddsw   %%xmm7,%%xmm0                   \n"
      "phaddsw   %%xmm1,%%xmm6                   \n"
      "psraw     $0x6,%%xmm0                     \n"
      "psraw     $0x6,%%xmm6                     \n"
      "packuswb  %%xmm0,%%xmm0                   \n"
      "packuswb  %%xmm6,%%xmm6                   \n"
      "punpcklbw %%xmm6,%%xmm0                   \n"
      "movdqu    (%0),%%xmm1                     \n"
      "movdqu    0x10(%0),%%xmm7                 \n"
      "pmaddubsw %%xmm4,%%xmm1                   \n"
      "pmaddubsw %%xmm4,%%xmm7                   \n"
      "phaddsw   %%xmm7,%%xmm1                   \n"
      "movdqu    (%0),%%xmm6                     \n"
      "movdqu    0x10(%0),%%xmm7                 \n"
      "pmaddubsw %%xmm5,%%xmm6                   \n"
      "pmaddubsw %%xmm5,%%xmm7                   \n"
      "phaddsw   %%xmm7,%%xmm6                   \n"
      "psraw     $0x6,%%xmm1                     \n"
      "psraw     $0x6,%%xmm6                     \n"
      "packuswb  %%xmm1,%%xmm1                   \n"
      "packuswb  %%xmm6,%%xmm6                   \n"
      "punpcklbw %%xmm6,%%xmm1                   \n"
      "movdqa    %%xmm0,%%xmm6                   \n"
      "punpcklwd %%xmm1,%%xmm0                   \n"
      "punpckhwd %%xmm1,%%xmm6                   \n"
      "movdqu    %%xmm0,(%1)                     \n"
      "movdqu    %%xmm6,0x10(%1)                 \n"
      "lea       0x20(%0),%0                     \n"
      "lea       0x20(%1),%1                     \n"
      "sub       $0x8,%2                         \n"
      "jg        1b                              \n"
      : "+r"(src_argb),   // %0
        "+r"(dst_argb),   // %1
        "+r"(width)       // %2
      : "r"(matrix_argb)  // %3
      : "memory", "cc", "xmm0", "xmm1", "xmm2", "xmm3", "xmm4", "xmm5", "xmm6",
        "xmm7");
5072 5073 5074
}
#endif  // HAS_ARGBCOLORMATRIXROW_SSSE3

5075 5076
#ifdef HAS_ARGBQUANTIZEROW_SSE2
// Quantize 4 ARGB pixels (16 bytes).
Frank Barchard's avatar
Frank Barchard committed
5077
void ARGBQuantizeRow_SSE2(uint8_t* dst_argb,
5078 5079 5080 5081
                          int scale,
                          int interval_size,
                          int interval_offset,
                          int width) {
5082 5083 5084 5085 5086 5087 5088 5089 5090 5091 5092 5093 5094 5095 5096 5097 5098 5099 5100 5101 5102 5103 5104 5105 5106 5107 5108 5109 5110 5111 5112 5113 5114 5115 5116 5117 5118 5119 5120 5121 5122 5123
  asm volatile(
      "movd      %2,%%xmm2                       \n"
      "movd      %3,%%xmm3                       \n"
      "movd      %4,%%xmm4                       \n"
      "pshuflw   $0x40,%%xmm2,%%xmm2             \n"
      "pshufd    $0x44,%%xmm2,%%xmm2             \n"
      "pshuflw   $0x40,%%xmm3,%%xmm3             \n"
      "pshufd    $0x44,%%xmm3,%%xmm3             \n"
      "pshuflw   $0x40,%%xmm4,%%xmm4             \n"
      "pshufd    $0x44,%%xmm4,%%xmm4             \n"
      "pxor      %%xmm5,%%xmm5                   \n"
      "pcmpeqb   %%xmm6,%%xmm6                   \n"
      "pslld     $0x18,%%xmm6                    \n"

      // 4 pixel loop.
      LABELALIGN
      "1:                                        \n"
      "movdqu    (%0),%%xmm0                     \n"
      "punpcklbw %%xmm5,%%xmm0                   \n"
      "pmulhuw   %%xmm2,%%xmm0                   \n"
      "movdqu    (%0),%%xmm1                     \n"
      "punpckhbw %%xmm5,%%xmm1                   \n"
      "pmulhuw   %%xmm2,%%xmm1                   \n"
      "pmullw    %%xmm3,%%xmm0                   \n"
      "movdqu    (%0),%%xmm7                     \n"
      "pmullw    %%xmm3,%%xmm1                   \n"
      "pand      %%xmm6,%%xmm7                   \n"
      "paddw     %%xmm4,%%xmm0                   \n"
      "paddw     %%xmm4,%%xmm1                   \n"
      "packuswb  %%xmm1,%%xmm0                   \n"
      "por       %%xmm7,%%xmm0                   \n"
      "movdqu    %%xmm0,(%0)                     \n"
      "lea       0x10(%0),%0                     \n"
      "sub       $0x4,%1                         \n"
      "jg        1b                              \n"
      : "+r"(dst_argb),       // %0
        "+r"(width)           // %1
      : "r"(scale),           // %2
        "r"(interval_size),   // %3
        "r"(interval_offset)  // %4
      : "memory", "cc", "xmm0", "xmm1", "xmm2", "xmm3", "xmm4", "xmm5", "xmm6",
        "xmm7");
5124 5125 5126
}
#endif  // HAS_ARGBQUANTIZEROW_SSE2

5127 5128
#ifdef HAS_ARGBSHADEROW_SSE2
// Shade 4 pixels at a time by specified value.
Frank Barchard's avatar
Frank Barchard committed
5129 5130
void ARGBShadeRow_SSE2(const uint8_t* src_argb,
                       uint8_t* dst_argb,
5131
                       int width,
Frank Barchard's avatar
Frank Barchard committed
5132
                       uint32_t value) {
5133 5134 5135 5136
  asm volatile(
      "movd      %3,%%xmm2                       \n"
      "punpcklbw %%xmm2,%%xmm2                   \n"
      "punpcklqdq %%xmm2,%%xmm2                  \n"
5137

5138 5139 5140 5141 5142 5143 5144 5145 5146 5147 5148 5149 5150 5151 5152 5153 5154 5155 5156 5157 5158 5159
      // 4 pixel loop.
      LABELALIGN
      "1:                                        \n"
      "movdqu    (%0),%%xmm0                     \n"
      "lea       0x10(%0),%0                     \n"
      "movdqa    %%xmm0,%%xmm1                   \n"
      "punpcklbw %%xmm0,%%xmm0                   \n"
      "punpckhbw %%xmm1,%%xmm1                   \n"
      "pmulhuw   %%xmm2,%%xmm0                   \n"
      "pmulhuw   %%xmm2,%%xmm1                   \n"
      "psrlw     $0x8,%%xmm0                     \n"
      "psrlw     $0x8,%%xmm1                     \n"
      "packuswb  %%xmm1,%%xmm0                   \n"
      "movdqu    %%xmm0,(%1)                     \n"
      "lea       0x10(%1),%1                     \n"
      "sub       $0x4,%2                         \n"
      "jg        1b                              \n"
      : "+r"(src_argb),  // %0
        "+r"(dst_argb),  // %1
        "+r"(width)      // %2
      : "r"(value)       // %3
      : "memory", "cc", "xmm0", "xmm1", "xmm2");
5160 5161 5162
}
#endif  // HAS_ARGBSHADEROW_SSE2

5163
#ifdef HAS_ARGBMULTIPLYROW_SSE2
5164
// Multiply 2 rows of ARGB pixels together, 4 pixels at a time.
Frank Barchard's avatar
Frank Barchard committed
5165 5166 5167
void ARGBMultiplyRow_SSE2(const uint8_t* src_argb0,
                          const uint8_t* src_argb1,
                          uint8_t* dst_argb,
5168
                          int width) {
5169
  asm volatile(
5170

5171 5172 5173 5174 5175 5176 5177 5178 5179 5180 5181 5182 5183 5184 5185 5186 5187 5188 5189 5190 5191 5192 5193 5194 5195 5196 5197 5198
      "pxor      %%xmm5,%%xmm5                   \n"

      // 4 pixel loop.
      LABELALIGN
      "1:                                        \n"
      "movdqu    (%0),%%xmm0                     \n"
      "lea       0x10(%0),%0                     \n"
      "movdqu    (%1),%%xmm2                     \n"
      "lea       0x10(%1),%1                     \n"
      "movdqu    %%xmm0,%%xmm1                   \n"
      "movdqu    %%xmm2,%%xmm3                   \n"
      "punpcklbw %%xmm0,%%xmm0                   \n"
      "punpckhbw %%xmm1,%%xmm1                   \n"
      "punpcklbw %%xmm5,%%xmm2                   \n"
      "punpckhbw %%xmm5,%%xmm3                   \n"
      "pmulhuw   %%xmm2,%%xmm0                   \n"
      "pmulhuw   %%xmm3,%%xmm1                   \n"
      "packuswb  %%xmm1,%%xmm0                   \n"
      "movdqu    %%xmm0,(%2)                     \n"
      "lea       0x10(%2),%2                     \n"
      "sub       $0x4,%3                         \n"
      "jg        1b                              \n"
      : "+r"(src_argb0),  // %0
        "+r"(src_argb1),  // %1
        "+r"(dst_argb),   // %2
        "+r"(width)       // %3
      :
      : "memory", "cc", "xmm0", "xmm1", "xmm2", "xmm3", "xmm5");
5199 5200 5201
}
#endif  // HAS_ARGBMULTIPLYROW_SSE2

5202 5203
#ifdef HAS_ARGBMULTIPLYROW_AVX2
// Multiply 2 rows of ARGB pixels together, 8 pixels at a time.
Frank Barchard's avatar
Frank Barchard committed
5204 5205 5206
void ARGBMultiplyRow_AVX2(const uint8_t* src_argb0,
                          const uint8_t* src_argb1,
                          uint8_t* dst_argb,
5207
                          int width) {
5208
  asm volatile(
5209

5210 5211 5212 5213 5214 5215 5216 5217 5218 5219 5220 5221 5222 5223 5224 5225 5226 5227 5228 5229 5230 5231 5232 5233 5234 5235 5236
      "vpxor      %%ymm5,%%ymm5,%%ymm5           \n"

      // 4 pixel loop.
      LABELALIGN
      "1:                                        \n"
      "vmovdqu    (%0),%%ymm1                    \n"
      "lea        0x20(%0),%0                    \n"
      "vmovdqu    (%1),%%ymm3                    \n"
      "lea        0x20(%1),%1                    \n"
      "vpunpcklbw %%ymm1,%%ymm1,%%ymm0           \n"
      "vpunpckhbw %%ymm1,%%ymm1,%%ymm1           \n"
      "vpunpcklbw %%ymm5,%%ymm3,%%ymm2           \n"
      "vpunpckhbw %%ymm5,%%ymm3,%%ymm3           \n"
      "vpmulhuw   %%ymm2,%%ymm0,%%ymm0           \n"
      "vpmulhuw   %%ymm3,%%ymm1,%%ymm1           \n"
      "vpackuswb  %%ymm1,%%ymm0,%%ymm0           \n"
      "vmovdqu    %%ymm0,(%2)                    \n"
      "lea       0x20(%2),%2                     \n"
      "sub        $0x8,%3                        \n"
      "jg        1b                              \n"
      "vzeroupper                                \n"
      : "+r"(src_argb0),  // %0
        "+r"(src_argb1),  // %1
        "+r"(dst_argb),   // %2
        "+r"(width)       // %3
      :
      : "memory", "cc"
5237
#if defined(__AVX2__)
5238 5239
        ,
        "xmm0", "xmm1", "xmm2", "xmm3", "xmm5"
5240
#endif
5241
      );
5242 5243 5244
}
#endif  // HAS_ARGBMULTIPLYROW_AVX2

5245 5246
#ifdef HAS_ARGBADDROW_SSE2
// Add 2 rows of ARGB pixels together, 4 pixels at a time.
Frank Barchard's avatar
Frank Barchard committed
5247 5248 5249
void ARGBAddRow_SSE2(const uint8_t* src_argb0,
                     const uint8_t* src_argb1,
                     uint8_t* dst_argb,
5250
                     int width) {
5251 5252 5253 5254 5255 5256 5257 5258 5259 5260 5261 5262 5263 5264 5265 5266 5267 5268 5269
  asm volatile(
      // 4 pixel loop.
      LABELALIGN
      "1:                                        \n"
      "movdqu    (%0),%%xmm0                     \n"
      "lea       0x10(%0),%0                     \n"
      "movdqu    (%1),%%xmm1                     \n"
      "lea       0x10(%1),%1                     \n"
      "paddusb   %%xmm1,%%xmm0                   \n"
      "movdqu    %%xmm0,(%2)                     \n"
      "lea       0x10(%2),%2                     \n"
      "sub       $0x4,%3                         \n"
      "jg        1b                              \n"
      : "+r"(src_argb0),  // %0
        "+r"(src_argb1),  // %1
        "+r"(dst_argb),   // %2
        "+r"(width)       // %3
      :
      : "memory", "cc", "xmm0", "xmm1");
5270 5271 5272
}
#endif  // HAS_ARGBADDROW_SSE2

5273 5274
#ifdef HAS_ARGBADDROW_AVX2
// Add 2 rows of ARGB pixels together, 4 pixels at a time.
Frank Barchard's avatar
Frank Barchard committed
5275 5276 5277
void ARGBAddRow_AVX2(const uint8_t* src_argb0,
                     const uint8_t* src_argb1,
                     uint8_t* dst_argb,
5278
                     int width) {
5279 5280 5281 5282 5283 5284 5285 5286 5287 5288 5289 5290 5291 5292 5293 5294 5295 5296 5297
  asm volatile(
      // 4 pixel loop.
      LABELALIGN
      "1:                                        \n"
      "vmovdqu    (%0),%%ymm0                    \n"
      "lea        0x20(%0),%0                    \n"
      "vpaddusb   (%1),%%ymm0,%%ymm0             \n"
      "lea        0x20(%1),%1                    \n"
      "vmovdqu    %%ymm0,(%2)                    \n"
      "lea        0x20(%2),%2                    \n"
      "sub        $0x8,%3                        \n"
      "jg        1b                              \n"
      "vzeroupper                                \n"
      : "+r"(src_argb0),  // %0
        "+r"(src_argb1),  // %1
        "+r"(dst_argb),   // %2
        "+r"(width)       // %3
      :
      : "memory", "cc", "xmm0");
5298 5299 5300
}
#endif  // HAS_ARGBADDROW_AVX2

5301 5302
#ifdef HAS_ARGBSUBTRACTROW_SSE2
// Subtract 2 rows of ARGB pixels, 4 pixels at a time.
Frank Barchard's avatar
Frank Barchard committed
5303 5304 5305
void ARGBSubtractRow_SSE2(const uint8_t* src_argb0,
                          const uint8_t* src_argb1,
                          uint8_t* dst_argb,
5306
                          int width) {
5307 5308 5309 5310 5311 5312 5313 5314 5315 5316 5317 5318 5319 5320 5321 5322 5323 5324 5325
  asm volatile(
      // 4 pixel loop.
      LABELALIGN
      "1:                                        \n"
      "movdqu    (%0),%%xmm0                     \n"
      "lea       0x10(%0),%0                     \n"
      "movdqu    (%1),%%xmm1                     \n"
      "lea       0x10(%1),%1                     \n"
      "psubusb   %%xmm1,%%xmm0                   \n"
      "movdqu    %%xmm0,(%2)                     \n"
      "lea       0x10(%2),%2                     \n"
      "sub       $0x4,%3                         \n"
      "jg        1b                              \n"
      : "+r"(src_argb0),  // %0
        "+r"(src_argb1),  // %1
        "+r"(dst_argb),   // %2
        "+r"(width)       // %3
      :
      : "memory", "cc", "xmm0", "xmm1");
5326 5327 5328
}
#endif  // HAS_ARGBSUBTRACTROW_SSE2

5329 5330
#ifdef HAS_ARGBSUBTRACTROW_AVX2
// Subtract 2 rows of ARGB pixels, 8 pixels at a time.
Frank Barchard's avatar
Frank Barchard committed
5331 5332 5333
void ARGBSubtractRow_AVX2(const uint8_t* src_argb0,
                          const uint8_t* src_argb1,
                          uint8_t* dst_argb,
5334
                          int width) {
5335 5336 5337 5338 5339 5340 5341 5342 5343 5344 5345 5346 5347 5348 5349 5350 5351 5352 5353
  asm volatile(
      // 4 pixel loop.
      LABELALIGN
      "1:                                        \n"
      "vmovdqu    (%0),%%ymm0                    \n"
      "lea        0x20(%0),%0                    \n"
      "vpsubusb   (%1),%%ymm0,%%ymm0             \n"
      "lea        0x20(%1),%1                    \n"
      "vmovdqu    %%ymm0,(%2)                    \n"
      "lea        0x20(%2),%2                    \n"
      "sub        $0x8,%3                        \n"
      "jg         1b                             \n"
      "vzeroupper                                \n"
      : "+r"(src_argb0),  // %0
        "+r"(src_argb1),  // %1
        "+r"(dst_argb),   // %2
        "+r"(width)       // %3
      :
      : "memory", "cc", "xmm0");
5354 5355 5356
}
#endif  // HAS_ARGBSUBTRACTROW_AVX2

5357
#ifdef HAS_SOBELXROW_SSE2
5358 5359 5360 5361
// SobelX as a matrix is
// -1  0  1
// -2  0  2
// -1  0  1
Frank Barchard's avatar
Frank Barchard committed
5362 5363 5364 5365
void SobelXRow_SSE2(const uint8_t* src_y0,
                    const uint8_t* src_y1,
                    const uint8_t* src_y2,
                    uint8_t* dst_sobelx,
5366
                    int width) {
5367 5368 5369 5370 5371
  asm volatile(
      "sub       %0,%1                           \n"
      "sub       %0,%2                           \n"
      "sub       %0,%3                           \n"
      "pxor      %%xmm5,%%xmm5                   \n"
5372

5373 5374 5375 5376 5377 5378 5379 5380 5381 5382 5383 5384 5385 5386 5387 5388 5389 5390 5391 5392 5393 5394 5395 5396 5397 5398 5399 5400 5401 5402 5403 5404 5405 5406 5407 5408
      // 8 pixel loop.
      LABELALIGN
      "1:                                        \n"
      "movq      (%0),%%xmm0                     \n"
      "movq      0x2(%0),%%xmm1                  \n"
      "punpcklbw %%xmm5,%%xmm0                   \n"
      "punpcklbw %%xmm5,%%xmm1                   \n"
      "psubw     %%xmm1,%%xmm0                   \n"
      "movq      0x00(%0,%1,1),%%xmm1            \n"
      "movq      0x02(%0,%1,1),%%xmm2            \n"
      "punpcklbw %%xmm5,%%xmm1                   \n"
      "punpcklbw %%xmm5,%%xmm2                   \n"
      "psubw     %%xmm2,%%xmm1                   \n"
      "movq      0x00(%0,%2,1),%%xmm2            \n"
      "movq      0x02(%0,%2,1),%%xmm3            \n"
      "punpcklbw %%xmm5,%%xmm2                   \n"
      "punpcklbw %%xmm5,%%xmm3                   \n"
      "psubw     %%xmm3,%%xmm2                   \n"
      "paddw     %%xmm2,%%xmm0                   \n"
      "paddw     %%xmm1,%%xmm0                   \n"
      "paddw     %%xmm1,%%xmm0                   \n"
      "pxor      %%xmm1,%%xmm1                   \n"
      "psubw     %%xmm0,%%xmm1                   \n"
      "pmaxsw    %%xmm1,%%xmm0                   \n"
      "packuswb  %%xmm0,%%xmm0                   \n"
      "movq      %%xmm0,0x00(%0,%3,1)            \n"
      "lea       0x8(%0),%0                      \n"
      "sub       $0x8,%4                         \n"
      "jg        1b                              \n"
      : "+r"(src_y0),      // %0
        "+r"(src_y1),      // %1
        "+r"(src_y2),      // %2
        "+r"(dst_sobelx),  // %3
        "+r"(width)        // %4
      :
      : "memory", "cc", "xmm0", "xmm1", "xmm2", "xmm3", "xmm5");
5409
}
5410
#endif  // HAS_SOBELXROW_SSE2
5411

5412
#ifdef HAS_SOBELYROW_SSE2
5413 5414 5415 5416
// SobelY as a matrix is
// -1 -2 -1
//  0  0  0
//  1  2  1
Frank Barchard's avatar
Frank Barchard committed
5417 5418 5419
void SobelYRow_SSE2(const uint8_t* src_y0,
                    const uint8_t* src_y1,
                    uint8_t* dst_sobely,
5420
                    int width) {
5421 5422 5423 5424
  asm volatile(
      "sub       %0,%1                           \n"
      "sub       %0,%2                           \n"
      "pxor      %%xmm5,%%xmm5                   \n"
5425

5426 5427 5428 5429 5430 5431 5432 5433 5434 5435 5436 5437 5438 5439 5440 5441 5442 5443 5444 5445 5446 5447 5448 5449 5450 5451 5452 5453 5454 5455 5456 5457 5458 5459 5460
      // 8 pixel loop.
      LABELALIGN
      "1:                                        \n"
      "movq      (%0),%%xmm0                     \n"
      "movq      0x00(%0,%1,1),%%xmm1            \n"
      "punpcklbw %%xmm5,%%xmm0                   \n"
      "punpcklbw %%xmm5,%%xmm1                   \n"
      "psubw     %%xmm1,%%xmm0                   \n"
      "movq      0x1(%0),%%xmm1                  \n"
      "movq      0x01(%0,%1,1),%%xmm2            \n"
      "punpcklbw %%xmm5,%%xmm1                   \n"
      "punpcklbw %%xmm5,%%xmm2                   \n"
      "psubw     %%xmm2,%%xmm1                   \n"
      "movq      0x2(%0),%%xmm2                  \n"
      "movq      0x02(%0,%1,1),%%xmm3            \n"
      "punpcklbw %%xmm5,%%xmm2                   \n"
      "punpcklbw %%xmm5,%%xmm3                   \n"
      "psubw     %%xmm3,%%xmm2                   \n"
      "paddw     %%xmm2,%%xmm0                   \n"
      "paddw     %%xmm1,%%xmm0                   \n"
      "paddw     %%xmm1,%%xmm0                   \n"
      "pxor      %%xmm1,%%xmm1                   \n"
      "psubw     %%xmm0,%%xmm1                   \n"
      "pmaxsw    %%xmm1,%%xmm0                   \n"
      "packuswb  %%xmm0,%%xmm0                   \n"
      "movq      %%xmm0,0x00(%0,%2,1)            \n"
      "lea       0x8(%0),%0                      \n"
      "sub       $0x8,%3                         \n"
      "jg        1b                              \n"
      : "+r"(src_y0),      // %0
        "+r"(src_y1),      // %1
        "+r"(dst_sobely),  // %2
        "+r"(width)        // %3
      :
      : "memory", "cc", "xmm0", "xmm1", "xmm2", "xmm3", "xmm5");
5461
}
5462
#endif  // HAS_SOBELYROW_SSE2
5463 5464 5465 5466 5467 5468 5469

#ifdef HAS_SOBELROW_SSE2
// Adds Sobel X and Sobel Y and stores Sobel into ARGB.
// A = 255
// R = Sobel
// G = Sobel
// B = Sobel
Frank Barchard's avatar
Frank Barchard committed
5470 5471 5472
void SobelRow_SSE2(const uint8_t* src_sobelx,
                   const uint8_t* src_sobely,
                   uint8_t* dst_argb,
5473
                   int width) {
5474 5475 5476 5477
  asm volatile(
      "sub       %0,%1                           \n"
      "pcmpeqb   %%xmm5,%%xmm5                   \n"
      "pslld     $0x18,%%xmm5                    \n"
5478

5479 5480 5481 5482 5483 5484 5485 5486 5487 5488 5489 5490 5491 5492 5493 5494 5495 5496 5497 5498 5499 5500 5501 5502 5503 5504 5505 5506 5507 5508 5509 5510 5511
      // 8 pixel loop.
      LABELALIGN
      "1:                                        \n"
      "movdqu    (%0),%%xmm0                     \n"
      "movdqu    0x00(%0,%1,1),%%xmm1            \n"
      "lea       0x10(%0),%0                     \n"
      "paddusb   %%xmm1,%%xmm0                   \n"
      "movdqa    %%xmm0,%%xmm2                   \n"
      "punpcklbw %%xmm0,%%xmm2                   \n"
      "punpckhbw %%xmm0,%%xmm0                   \n"
      "movdqa    %%xmm2,%%xmm1                   \n"
      "punpcklwd %%xmm2,%%xmm1                   \n"
      "punpckhwd %%xmm2,%%xmm2                   \n"
      "por       %%xmm5,%%xmm1                   \n"
      "por       %%xmm5,%%xmm2                   \n"
      "movdqa    %%xmm0,%%xmm3                   \n"
      "punpcklwd %%xmm0,%%xmm3                   \n"
      "punpckhwd %%xmm0,%%xmm0                   \n"
      "por       %%xmm5,%%xmm3                   \n"
      "por       %%xmm5,%%xmm0                   \n"
      "movdqu    %%xmm1,(%2)                     \n"
      "movdqu    %%xmm2,0x10(%2)                 \n"
      "movdqu    %%xmm3,0x20(%2)                 \n"
      "movdqu    %%xmm0,0x30(%2)                 \n"
      "lea       0x40(%2),%2                     \n"
      "sub       $0x10,%3                        \n"
      "jg        1b                              \n"
      : "+r"(src_sobelx),  // %0
        "+r"(src_sobely),  // %1
        "+r"(dst_argb),    // %2
        "+r"(width)        // %3
      :
      : "memory", "cc", "xmm0", "xmm1", "xmm2", "xmm3", "xmm5");
5512 5513 5514
}
#endif  // HAS_SOBELROW_SSE2

5515 5516
#ifdef HAS_SOBELTOPLANEROW_SSE2
// Adds Sobel X and Sobel Y and stores Sobel into a plane.
Frank Barchard's avatar
Frank Barchard committed
5517 5518 5519
void SobelToPlaneRow_SSE2(const uint8_t* src_sobelx,
                          const uint8_t* src_sobely,
                          uint8_t* dst_y,
5520
                          int width) {
5521 5522 5523 5524
  asm volatile(
      "sub       %0,%1                           \n"
      "pcmpeqb   %%xmm5,%%xmm5                   \n"
      "pslld     $0x18,%%xmm5                    \n"
5525

5526 5527 5528 5529 5530 5531 5532 5533 5534 5535 5536 5537 5538 5539 5540 5541 5542
      // 8 pixel loop.
      LABELALIGN
      "1:                                        \n"
      "movdqu    (%0),%%xmm0                     \n"
      "movdqu    0x00(%0,%1,1),%%xmm1            \n"
      "lea       0x10(%0),%0                     \n"
      "paddusb   %%xmm1,%%xmm0                   \n"
      "movdqu    %%xmm0,(%2)                     \n"
      "lea       0x10(%2),%2                     \n"
      "sub       $0x10,%3                        \n"
      "jg        1b                              \n"
      : "+r"(src_sobelx),  // %0
        "+r"(src_sobely),  // %1
        "+r"(dst_y),       // %2
        "+r"(width)        // %3
      :
      : "memory", "cc", "xmm0", "xmm1");
5543 5544 5545
}
#endif  // HAS_SOBELTOPLANEROW_SSE2

5546 5547 5548 5549 5550 5551
#ifdef HAS_SOBELXYROW_SSE2
// Mixes Sobel X, Sobel Y and Sobel into ARGB.
// A = 255
// R = Sobel X
// G = Sobel
// B = Sobel Y
Frank Barchard's avatar
Frank Barchard committed
5552 5553 5554
void SobelXYRow_SSE2(const uint8_t* src_sobelx,
                     const uint8_t* src_sobely,
                     uint8_t* dst_argb,
5555
                     int width) {
5556 5557 5558
  asm volatile(
      "sub       %0,%1                           \n"
      "pcmpeqb   %%xmm5,%%xmm5                   \n"
5559

5560 5561 5562 5563 5564 5565 5566 5567 5568 5569 5570 5571 5572 5573 5574 5575 5576 5577 5578 5579 5580 5581 5582 5583 5584 5585 5586 5587 5588 5589 5590 5591 5592 5593
      // 8 pixel loop.
      LABELALIGN
      "1:                                        \n"
      "movdqu    (%0),%%xmm0                     \n"
      "movdqu    0x00(%0,%1,1),%%xmm1            \n"
      "lea       0x10(%0),%0                     \n"
      "movdqa    %%xmm0,%%xmm2                   \n"
      "paddusb   %%xmm1,%%xmm2                   \n"
      "movdqa    %%xmm0,%%xmm3                   \n"
      "punpcklbw %%xmm5,%%xmm3                   \n"
      "punpckhbw %%xmm5,%%xmm0                   \n"
      "movdqa    %%xmm1,%%xmm4                   \n"
      "punpcklbw %%xmm2,%%xmm4                   \n"
      "punpckhbw %%xmm2,%%xmm1                   \n"
      "movdqa    %%xmm4,%%xmm6                   \n"
      "punpcklwd %%xmm3,%%xmm6                   \n"
      "punpckhwd %%xmm3,%%xmm4                   \n"
      "movdqa    %%xmm1,%%xmm7                   \n"
      "punpcklwd %%xmm0,%%xmm7                   \n"
      "punpckhwd %%xmm0,%%xmm1                   \n"
      "movdqu    %%xmm6,(%2)                     \n"
      "movdqu    %%xmm4,0x10(%2)                 \n"
      "movdqu    %%xmm7,0x20(%2)                 \n"
      "movdqu    %%xmm1,0x30(%2)                 \n"
      "lea       0x40(%2),%2                     \n"
      "sub       $0x10,%3                        \n"
      "jg        1b                              \n"
      : "+r"(src_sobelx),  // %0
        "+r"(src_sobely),  // %1
        "+r"(dst_argb),    // %2
        "+r"(width)        // %3
      :
      : "memory", "cc", "xmm0", "xmm1", "xmm2", "xmm3", "xmm4", "xmm5", "xmm6",
        "xmm7");
5594 5595 5596
}
#endif  // HAS_SOBELXYROW_SSE2

fbarchard@google.com's avatar
fbarchard@google.com committed
5597 5598 5599
#ifdef HAS_COMPUTECUMULATIVESUMROW_SSE2
// Creates a table of cumulative sums where each value is a sum of all values
// above and to the left of the value, inclusive of the value.
Frank Barchard's avatar
Frank Barchard committed
5600 5601 5602
void ComputeCumulativeSumRow_SSE2(const uint8_t* row,
                                  int32_t* cumsum,
                                  const int32_t* previous_cumsum,
5603
                                  int width) {
5604 5605 5606 5607 5608 5609 5610 5611 5612 5613 5614 5615 5616 5617 5618 5619 5620 5621 5622 5623 5624 5625 5626 5627 5628 5629 5630 5631 5632 5633 5634 5635 5636 5637 5638 5639 5640 5641 5642 5643 5644 5645 5646 5647 5648 5649 5650 5651 5652 5653 5654 5655 5656 5657 5658 5659 5660 5661 5662 5663 5664 5665 5666 5667 5668 5669 5670 5671 5672 5673
  asm volatile(
      "pxor      %%xmm0,%%xmm0                   \n"
      "pxor      %%xmm1,%%xmm1                   \n"
      "sub       $0x4,%3                         \n"
      "jl        49f                             \n"
      "test      $0xf,%1                         \n"
      "jne       49f                             \n"

      // 4 pixel loop.
      LABELALIGN
      "40:                                       \n"
      "movdqu    (%0),%%xmm2                     \n"
      "lea       0x10(%0),%0                     \n"
      "movdqa    %%xmm2,%%xmm4                   \n"
      "punpcklbw %%xmm1,%%xmm2                   \n"
      "movdqa    %%xmm2,%%xmm3                   \n"
      "punpcklwd %%xmm1,%%xmm2                   \n"
      "punpckhwd %%xmm1,%%xmm3                   \n"
      "punpckhbw %%xmm1,%%xmm4                   \n"
      "movdqa    %%xmm4,%%xmm5                   \n"
      "punpcklwd %%xmm1,%%xmm4                   \n"
      "punpckhwd %%xmm1,%%xmm5                   \n"
      "paddd     %%xmm2,%%xmm0                   \n"
      "movdqu    (%2),%%xmm2                     \n"
      "paddd     %%xmm0,%%xmm2                   \n"
      "paddd     %%xmm3,%%xmm0                   \n"
      "movdqu    0x10(%2),%%xmm3                 \n"
      "paddd     %%xmm0,%%xmm3                   \n"
      "paddd     %%xmm4,%%xmm0                   \n"
      "movdqu    0x20(%2),%%xmm4                 \n"
      "paddd     %%xmm0,%%xmm4                   \n"
      "paddd     %%xmm5,%%xmm0                   \n"
      "movdqu    0x30(%2),%%xmm5                 \n"
      "lea       0x40(%2),%2                     \n"
      "paddd     %%xmm0,%%xmm5                   \n"
      "movdqu    %%xmm2,(%1)                     \n"
      "movdqu    %%xmm3,0x10(%1)                 \n"
      "movdqu    %%xmm4,0x20(%1)                 \n"
      "movdqu    %%xmm5,0x30(%1)                 \n"
      "lea       0x40(%1),%1                     \n"
      "sub       $0x4,%3                         \n"
      "jge       40b                             \n"

      "49:                                       \n"
      "add       $0x3,%3                         \n"
      "jl        19f                             \n"

      // 1 pixel loop.
      LABELALIGN
      "10:                                       \n"
      "movd      (%0),%%xmm2                     \n"
      "lea       0x4(%0),%0                      \n"
      "punpcklbw %%xmm1,%%xmm2                   \n"
      "punpcklwd %%xmm1,%%xmm2                   \n"
      "paddd     %%xmm2,%%xmm0                   \n"
      "movdqu    (%2),%%xmm2                     \n"
      "lea       0x10(%2),%2                     \n"
      "paddd     %%xmm0,%%xmm2                   \n"
      "movdqu    %%xmm2,(%1)                     \n"
      "lea       0x10(%1),%1                     \n"
      "sub       $0x1,%3                         \n"
      "jge       10b                             \n"

      "19:                                       \n"
      : "+r"(row),              // %0
        "+r"(cumsum),           // %1
        "+r"(previous_cumsum),  // %2
        "+r"(width)             // %3
      :
      : "memory", "cc", "xmm0", "xmm1", "xmm2", "xmm3", "xmm4", "xmm5");
fbarchard@google.com's avatar
fbarchard@google.com committed
5674 5675 5676
}
#endif  // HAS_COMPUTECUMULATIVESUMROW_SSE2

5677
#ifdef HAS_CUMULATIVESUMTOAVERAGEROW_SSE2
Frank Barchard's avatar
Frank Barchard committed
5678 5679
void CumulativeSumToAverageRow_SSE2(const int32_t* topleft,
                                    const int32_t* botleft,
5680 5681
                                    int width,
                                    int area,
Frank Barchard's avatar
Frank Barchard committed
5682
                                    uint8_t* dst,
5683
                                    int count) {
5684 5685 5686 5687 5688 5689 5690 5691 5692 5693 5694 5695 5696 5697 5698 5699 5700 5701 5702 5703 5704 5705 5706 5707 5708 5709 5710 5711 5712 5713 5714 5715 5716 5717 5718 5719 5720 5721 5722 5723 5724 5725 5726 5727 5728 5729 5730 5731 5732 5733 5734 5735 5736 5737 5738 5739 5740 5741 5742 5743 5744 5745 5746 5747 5748 5749 5750 5751 5752 5753 5754 5755 5756 5757 5758 5759 5760 5761 5762 5763 5764 5765 5766 5767 5768 5769 5770 5771 5772 5773 5774 5775 5776 5777 5778 5779 5780 5781 5782 5783 5784 5785 5786 5787 5788 5789 5790 5791 5792 5793 5794 5795 5796 5797 5798 5799 5800 5801 5802 5803 5804 5805
  asm volatile(
      "movd      %5,%%xmm5                       \n"
      "cvtdq2ps  %%xmm5,%%xmm5                   \n"
      "rcpss     %%xmm5,%%xmm4                   \n"
      "pshufd    $0x0,%%xmm4,%%xmm4              \n"
      "sub       $0x4,%3                         \n"
      "jl        49f                             \n"
      "cmpl      $0x80,%5                        \n"
      "ja        40f                             \n"

      "pshufd    $0x0,%%xmm5,%%xmm5              \n"
      "pcmpeqb   %%xmm6,%%xmm6                   \n"
      "psrld     $0x10,%%xmm6                    \n"
      "cvtdq2ps  %%xmm6,%%xmm6                   \n"
      "addps     %%xmm6,%%xmm5                   \n"
      "mulps     %%xmm4,%%xmm5                   \n"
      "cvtps2dq  %%xmm5,%%xmm5                   \n"
      "packssdw  %%xmm5,%%xmm5                   \n"

      // 4 pixel small loop.
      LABELALIGN
      "4:                                        \n"
      "movdqu    (%0),%%xmm0                     \n"
      "movdqu    0x10(%0),%%xmm1                 \n"
      "movdqu    0x20(%0),%%xmm2                 \n"
      "movdqu    0x30(%0),%%xmm3                 \n"
      "psubd     0x00(%0,%4,4),%%xmm0            \n"
      "psubd     0x10(%0,%4,4),%%xmm1            \n"
      "psubd     0x20(%0,%4,4),%%xmm2            \n"
      "psubd     0x30(%0,%4,4),%%xmm3            \n"
      "lea       0x40(%0),%0                     \n"
      "psubd     (%1),%%xmm0                     \n"
      "psubd     0x10(%1),%%xmm1                 \n"
      "psubd     0x20(%1),%%xmm2                 \n"
      "psubd     0x30(%1),%%xmm3                 \n"
      "paddd     0x00(%1,%4,4),%%xmm0            \n"
      "paddd     0x10(%1,%4,4),%%xmm1            \n"
      "paddd     0x20(%1,%4,4),%%xmm2            \n"
      "paddd     0x30(%1,%4,4),%%xmm3            \n"
      "lea       0x40(%1),%1                     \n"
      "packssdw  %%xmm1,%%xmm0                   \n"
      "packssdw  %%xmm3,%%xmm2                   \n"
      "pmulhuw   %%xmm5,%%xmm0                   \n"
      "pmulhuw   %%xmm5,%%xmm2                   \n"
      "packuswb  %%xmm2,%%xmm0                   \n"
      "movdqu    %%xmm0,(%2)                     \n"
      "lea       0x10(%2),%2                     \n"
      "sub       $0x4,%3                         \n"
      "jge       4b                              \n"
      "jmp       49f                             \n"

      // 4 pixel loop
      LABELALIGN
      "40:                                       \n"
      "movdqu    (%0),%%xmm0                     \n"
      "movdqu    0x10(%0),%%xmm1                 \n"
      "movdqu    0x20(%0),%%xmm2                 \n"
      "movdqu    0x30(%0),%%xmm3                 \n"
      "psubd     0x00(%0,%4,4),%%xmm0            \n"
      "psubd     0x10(%0,%4,4),%%xmm1            \n"
      "psubd     0x20(%0,%4,4),%%xmm2            \n"
      "psubd     0x30(%0,%4,4),%%xmm3            \n"
      "lea       0x40(%0),%0                     \n"
      "psubd     (%1),%%xmm0                     \n"
      "psubd     0x10(%1),%%xmm1                 \n"
      "psubd     0x20(%1),%%xmm2                 \n"
      "psubd     0x30(%1),%%xmm3                 \n"
      "paddd     0x00(%1,%4,4),%%xmm0            \n"
      "paddd     0x10(%1,%4,4),%%xmm1            \n"
      "paddd     0x20(%1,%4,4),%%xmm2            \n"
      "paddd     0x30(%1,%4,4),%%xmm3            \n"
      "lea       0x40(%1),%1                     \n"
      "cvtdq2ps  %%xmm0,%%xmm0                   \n"
      "cvtdq2ps  %%xmm1,%%xmm1                   \n"
      "mulps     %%xmm4,%%xmm0                   \n"
      "mulps     %%xmm4,%%xmm1                   \n"
      "cvtdq2ps  %%xmm2,%%xmm2                   \n"
      "cvtdq2ps  %%xmm3,%%xmm3                   \n"
      "mulps     %%xmm4,%%xmm2                   \n"
      "mulps     %%xmm4,%%xmm3                   \n"
      "cvtps2dq  %%xmm0,%%xmm0                   \n"
      "cvtps2dq  %%xmm1,%%xmm1                   \n"
      "cvtps2dq  %%xmm2,%%xmm2                   \n"
      "cvtps2dq  %%xmm3,%%xmm3                   \n"
      "packssdw  %%xmm1,%%xmm0                   \n"
      "packssdw  %%xmm3,%%xmm2                   \n"
      "packuswb  %%xmm2,%%xmm0                   \n"
      "movdqu    %%xmm0,(%2)                     \n"
      "lea       0x10(%2),%2                     \n"
      "sub       $0x4,%3                         \n"
      "jge       40b                             \n"

      "49:                                       \n"
      "add       $0x3,%3                         \n"
      "jl        19f                             \n"

      // 1 pixel loop
      LABELALIGN
      "10:                                       \n"
      "movdqu    (%0),%%xmm0                     \n"
      "psubd     0x00(%0,%4,4),%%xmm0            \n"
      "lea       0x10(%0),%0                     \n"
      "psubd     (%1),%%xmm0                     \n"
      "paddd     0x00(%1,%4,4),%%xmm0            \n"
      "lea       0x10(%1),%1                     \n"
      "cvtdq2ps  %%xmm0,%%xmm0                   \n"
      "mulps     %%xmm4,%%xmm0                   \n"
      "cvtps2dq  %%xmm0,%%xmm0                   \n"
      "packssdw  %%xmm0,%%xmm0                   \n"
      "packuswb  %%xmm0,%%xmm0                   \n"
      "movd      %%xmm0,(%2)                     \n"
      "lea       0x4(%2),%2                      \n"
      "sub       $0x1,%3                         \n"
      "jge       10b                             \n"
      "19:                                       \n"
      : "+r"(topleft),           // %0
        "+r"(botleft),           // %1
        "+r"(dst),               // %2
        "+rm"(count)             // %3
      : "r"((intptr_t)(width)),  // %4
        "rm"(area)               // %5
      : "memory", "cc", "xmm0", "xmm1", "xmm2", "xmm3", "xmm4", "xmm5", "xmm6");
fbarchard@google.com's avatar
fbarchard@google.com committed
5806
}
5807
#endif  // HAS_CUMULATIVESUMTOAVERAGEROW_SSE2
fbarchard@google.com's avatar
fbarchard@google.com committed
5808

5809 5810
#ifdef HAS_ARGBAFFINEROW_SSE2
// Copy ARGB pixels from source image with slope to a row of destination.
5811
LIBYUV_API
Frank Barchard's avatar
Frank Barchard committed
5812
void ARGBAffineRow_SSE2(const uint8_t* src_argb,
5813
                        int src_argb_stride,
Frank Barchard's avatar
Frank Barchard committed
5814
                        uint8_t* dst_argb,
5815 5816
                        const float* src_dudv,
                        int width) {
5817
  intptr_t src_argb_stride_temp = src_argb_stride;
5818
  intptr_t temp;
5819 5820 5821 5822 5823 5824 5825 5826 5827 5828 5829 5830 5831 5832 5833 5834 5835 5836 5837 5838 5839 5840 5841 5842 5843 5844 5845 5846 5847 5848 5849 5850 5851 5852 5853 5854 5855 5856 5857 5858 5859 5860 5861 5862 5863 5864 5865 5866 5867 5868 5869 5870 5871 5872 5873 5874 5875 5876 5877 5878 5879 5880 5881 5882 5883 5884 5885 5886 5887 5888 5889 5890 5891 5892 5893
  asm volatile(
      "movq      (%3),%%xmm2                     \n"
      "movq      0x08(%3),%%xmm7                 \n"
      "shl       $0x10,%1                        \n"
      "add       $0x4,%1                         \n"
      "movd      %1,%%xmm5                       \n"
      "sub       $0x4,%4                         \n"
      "jl        49f                             \n"

      "pshufd    $0x44,%%xmm7,%%xmm7             \n"
      "pshufd    $0x0,%%xmm5,%%xmm5              \n"
      "movdqa    %%xmm2,%%xmm0                   \n"
      "addps     %%xmm7,%%xmm0                   \n"
      "movlhps   %%xmm0,%%xmm2                   \n"
      "movdqa    %%xmm7,%%xmm4                   \n"
      "addps     %%xmm4,%%xmm4                   \n"
      "movdqa    %%xmm2,%%xmm3                   \n"
      "addps     %%xmm4,%%xmm3                   \n"
      "addps     %%xmm4,%%xmm4                   \n"

      // 4 pixel loop
      LABELALIGN
      "40:                                       \n"
      "cvttps2dq %%xmm2,%%xmm0                   \n"  // x,y float->int first 2
      "cvttps2dq %%xmm3,%%xmm1                   \n"  // x,y float->int next 2
      "packssdw  %%xmm1,%%xmm0                   \n"  // x, y as 8 shorts
      "pmaddwd   %%xmm5,%%xmm0                   \n"  // off = x*4 + y*stride
      "movd      %%xmm0,%k1                      \n"
      "pshufd    $0x39,%%xmm0,%%xmm0             \n"
      "movd      %%xmm0,%k5                      \n"
      "pshufd    $0x39,%%xmm0,%%xmm0             \n"
      "movd      0x00(%0,%1,1),%%xmm1            \n"
      "movd      0x00(%0,%5,1),%%xmm6            \n"
      "punpckldq %%xmm6,%%xmm1                   \n"
      "addps     %%xmm4,%%xmm2                   \n"
      "movq      %%xmm1,(%2)                     \n"
      "movd      %%xmm0,%k1                      \n"
      "pshufd    $0x39,%%xmm0,%%xmm0             \n"
      "movd      %%xmm0,%k5                      \n"
      "movd      0x00(%0,%1,1),%%xmm0            \n"
      "movd      0x00(%0,%5,1),%%xmm6            \n"
      "punpckldq %%xmm6,%%xmm0                   \n"
      "addps     %%xmm4,%%xmm3                   \n"
      "movq      %%xmm0,0x08(%2)                 \n"
      "lea       0x10(%2),%2                     \n"
      "sub       $0x4,%4                         \n"
      "jge       40b                             \n"

      "49:                                       \n"
      "add       $0x3,%4                         \n"
      "jl        19f                             \n"

      // 1 pixel loop
      LABELALIGN
      "10:                                       \n"
      "cvttps2dq %%xmm2,%%xmm0                   \n"
      "packssdw  %%xmm0,%%xmm0                   \n"
      "pmaddwd   %%xmm5,%%xmm0                   \n"
      "addps     %%xmm7,%%xmm2                   \n"
      "movd      %%xmm0,%k1                      \n"
      "movd      0x00(%0,%1,1),%%xmm0            \n"
      "movd      %%xmm0,(%2)                     \n"
      "lea       0x04(%2),%2                     \n"
      "sub       $0x1,%4                         \n"
      "jge       10b                             \n"
      "19:                                       \n"
      : "+r"(src_argb),              // %0
        "+r"(src_argb_stride_temp),  // %1
        "+r"(dst_argb),              // %2
        "+r"(src_dudv),              // %3
        "+rm"(width),                // %4
        "=&r"(temp)                  // %5
      :
      : "memory", "cc", "xmm0", "xmm1", "xmm2", "xmm3", "xmm4", "xmm5", "xmm6",
        "xmm7");
5894 5895 5896
}
#endif  // HAS_ARGBAFFINEROW_SSE2

5897
#ifdef HAS_INTERPOLATEROW_SSSE3
5898
// Bilinear filter 16x2 -> 16x1
Frank Barchard's avatar
Frank Barchard committed
5899 5900
void InterpolateRow_SSSE3(uint8_t* dst_ptr,
                          const uint8_t* src_ptr,
5901 5902
                          ptrdiff_t src_stride,
                          int dst_width,
5903
                          int source_y_fraction) {
5904 5905 5906 5907 5908 5909 5910 5911 5912 5913 5914 5915 5916 5917 5918 5919 5920 5921 5922 5923 5924 5925 5926 5927 5928 5929 5930 5931 5932 5933 5934 5935 5936 5937 5938 5939 5940 5941 5942 5943 5944 5945
  asm volatile(
      "sub       %1,%0                           \n"
      "cmp       $0x0,%3                         \n"
      "je        100f                            \n"
      "cmp       $0x80,%3                        \n"
      "je        50f                             \n"

      "movd      %3,%%xmm0                       \n"
      "neg       %3                              \n"
      "add       $0x100,%3                       \n"
      "movd      %3,%%xmm5                       \n"
      "punpcklbw %%xmm0,%%xmm5                   \n"
      "punpcklwd %%xmm5,%%xmm5                   \n"
      "pshufd    $0x0,%%xmm5,%%xmm5              \n"
      "mov       $0x80808080,%%eax               \n"
      "movd      %%eax,%%xmm4                    \n"
      "pshufd    $0x0,%%xmm4,%%xmm4              \n"

      // General purpose row blend.
      LABELALIGN
      "1:                                        \n"
      "movdqu    (%1),%%xmm0                     \n"
      "movdqu    0x00(%1,%4,1),%%xmm2            \n"
      "movdqa     %%xmm0,%%xmm1                  \n"
      "punpcklbw  %%xmm2,%%xmm0                  \n"
      "punpckhbw  %%xmm2,%%xmm1                  \n"
      "psubb      %%xmm4,%%xmm0                  \n"
      "psubb      %%xmm4,%%xmm1                  \n"
      "movdqa     %%xmm5,%%xmm2                  \n"
      "movdqa     %%xmm5,%%xmm3                  \n"
      "pmaddubsw  %%xmm0,%%xmm2                  \n"
      "pmaddubsw  %%xmm1,%%xmm3                  \n"
      "paddw      %%xmm4,%%xmm2                  \n"
      "paddw      %%xmm4,%%xmm3                  \n"
      "psrlw      $0x8,%%xmm2                    \n"
      "psrlw      $0x8,%%xmm3                    \n"
      "packuswb   %%xmm3,%%xmm2                  \n"
      "movdqu    %%xmm2,0x00(%1,%0,1)            \n"
      "lea       0x10(%1),%1                     \n"
      "sub       $0x10,%2                        \n"
      "jg        1b                              \n"
      "jmp       99f                             \n"
5946

5947 5948 5949 5950 5951 5952 5953 5954 5955 5956 5957 5958 5959 5960 5961 5962 5963 5964 5965 5966 5967 5968 5969 5970 5971 5972 5973 5974
      // Blend 50 / 50.
      LABELALIGN
      "50:                                       \n"
      "movdqu    (%1),%%xmm0                     \n"
      "movdqu    0x00(%1,%4,1),%%xmm1            \n"
      "pavgb     %%xmm1,%%xmm0                   \n"
      "movdqu    %%xmm0,0x00(%1,%0,1)            \n"
      "lea       0x10(%1),%1                     \n"
      "sub       $0x10,%2                        \n"
      "jg        50b                             \n"
      "jmp       99f                             \n"

      // Blend 100 / 0 - Copy row unchanged.
      LABELALIGN
      "100:                                      \n"
      "movdqu    (%1),%%xmm0                     \n"
      "movdqu    %%xmm0,0x00(%1,%0,1)            \n"
      "lea       0x10(%1),%1                     \n"
      "sub       $0x10,%2                        \n"
      "jg        100b                            \n"

      "99:                                       \n"
      : "+r"(dst_ptr),               // %0
        "+r"(src_ptr),               // %1
        "+rm"(dst_width),            // %2
        "+r"(source_y_fraction)      // %3
      : "r"((intptr_t)(src_stride))  // %4
      : "memory", "cc", "eax", "xmm0", "xmm1", "xmm2", "xmm3", "xmm4", "xmm5");
5975
}
5976
#endif  // HAS_INTERPOLATEROW_SSSE3
5977

5978 5979
#ifdef HAS_INTERPOLATEROW_AVX2
// Bilinear filter 32x2 -> 32x1
Frank Barchard's avatar
Frank Barchard committed
5980 5981
void InterpolateRow_AVX2(uint8_t* dst_ptr,
                         const uint8_t* src_ptr,
5982 5983
                         ptrdiff_t src_stride,
                         int dst_width,
5984
                         int source_y_fraction) {
5985 5986 5987 5988 5989 5990 5991 5992 5993 5994 5995 5996 5997 5998 5999 6000 6001
  asm volatile(
      "cmp       $0x0,%3                         \n"
      "je        100f                            \n"
      "sub       %1,%0                           \n"
      "cmp       $0x80,%3                        \n"
      "je        50f                             \n"

      "vmovd      %3,%%xmm0                      \n"
      "neg        %3                             \n"
      "add        $0x100,%3                      \n"
      "vmovd      %3,%%xmm5                      \n"
      "vpunpcklbw %%xmm0,%%xmm5,%%xmm5           \n"
      "vpunpcklwd %%xmm5,%%xmm5,%%xmm5           \n"
      "vbroadcastss %%xmm5,%%ymm5                \n"
      "mov        $0x80808080,%%eax              \n"
      "vmovd      %%eax,%%xmm4                   \n"
      "vbroadcastss %%xmm4,%%ymm4                \n"
6002

6003 6004 6005 6006 6007 6008 6009 6010 6011 6012 6013 6014 6015 6016 6017 6018 6019 6020 6021 6022 6023
      // General purpose row blend.
      LABELALIGN
      "1:                                        \n"
      "vmovdqu    (%1),%%ymm0                    \n"
      "vmovdqu    0x00(%1,%4,1),%%ymm2           \n"
      "vpunpckhbw %%ymm2,%%ymm0,%%ymm1           \n"
      "vpunpcklbw %%ymm2,%%ymm0,%%ymm0           \n"
      "vpsubb     %%ymm4,%%ymm1,%%ymm1           \n"
      "vpsubb     %%ymm4,%%ymm0,%%ymm0           \n"
      "vpmaddubsw %%ymm1,%%ymm5,%%ymm1           \n"
      "vpmaddubsw %%ymm0,%%ymm5,%%ymm0           \n"
      "vpaddw     %%ymm4,%%ymm1,%%ymm1           \n"
      "vpaddw     %%ymm4,%%ymm0,%%ymm0           \n"
      "vpsrlw     $0x8,%%ymm1,%%ymm1             \n"
      "vpsrlw     $0x8,%%ymm0,%%ymm0             \n"
      "vpackuswb  %%ymm1,%%ymm0,%%ymm0           \n"
      "vmovdqu    %%ymm0,0x00(%1,%0,1)           \n"
      "lea        0x20(%1),%1                    \n"
      "sub        $0x20,%2                       \n"
      "jg         1b                             \n"
      "jmp        99f                            \n"
6024

6025 6026 6027 6028 6029 6030 6031 6032 6033 6034 6035 6036 6037 6038 6039 6040
      // Blend 50 / 50.
      LABELALIGN
      "50:                                       \n"
      "vmovdqu   (%1),%%ymm0                     \n"
      "vpavgb    0x00(%1,%4,1),%%ymm0,%%ymm0     \n"
      "vmovdqu   %%ymm0,0x00(%1,%0,1)            \n"
      "lea       0x20(%1),%1                     \n"
      "sub       $0x20,%2                        \n"
      "jg        50b                             \n"
      "jmp       99f                             \n"

      // Blend 100 / 0 - Copy row unchanged.
      LABELALIGN
      "100:                                      \n"
      "rep movsb                                 \n"
      "jmp       999f                            \n"
6041

6042 6043 6044 6045 6046 6047 6048 6049 6050
      "99:                                       \n"
      "vzeroupper                                \n"
      "999:                                      \n"
      : "+D"(dst_ptr),               // %0
        "+S"(src_ptr),               // %1
        "+cm"(dst_width),            // %2
        "+r"(source_y_fraction)      // %3
      : "r"((intptr_t)(src_stride))  // %4
      : "memory", "cc", "eax", "xmm0", "xmm1", "xmm2", "xmm4", "xmm5");
6051 6052 6053
}
#endif  // HAS_INTERPOLATEROW_AVX2

6054
#ifdef HAS_ARGBSHUFFLEROW_SSSE3
fbarchard@google.com's avatar
fbarchard@google.com committed
6055
// For BGRAToARGB, ABGRToARGB, RGBAToARGB, and ARGBToRGBA.
Frank Barchard's avatar
Frank Barchard committed
6056 6057 6058
void ARGBShuffleRow_SSSE3(const uint8_t* src_argb,
                          uint8_t* dst_argb,
                          const uint8_t* shuffler,
6059
                          int width) {
6060 6061 6062 6063 6064 6065 6066 6067 6068 6069 6070 6071 6072 6073 6074 6075 6076 6077 6078 6079 6080
  asm volatile(

      "movdqu    (%3),%%xmm5                     \n"

      LABELALIGN
      "1:                                        \n"
      "movdqu    (%0),%%xmm0                     \n"
      "movdqu    0x10(%0),%%xmm1                 \n"
      "lea       0x20(%0),%0                     \n"
      "pshufb    %%xmm5,%%xmm0                   \n"
      "pshufb    %%xmm5,%%xmm1                   \n"
      "movdqu    %%xmm0,(%1)                     \n"
      "movdqu    %%xmm1,0x10(%1)                 \n"
      "lea       0x20(%1),%1                     \n"
      "sub       $0x8,%2                         \n"
      "jg        1b                              \n"
      : "+r"(src_argb),  // %0
        "+r"(dst_argb),  // %1
        "+r"(width)      // %2
      : "r"(shuffler)    // %3
      : "memory", "cc", "xmm0", "xmm1", "xmm5");
fbarchard@google.com's avatar
fbarchard@google.com committed
6081
}
6082
#endif  // HAS_ARGBSHUFFLEROW_SSSE3
fbarchard@google.com's avatar
fbarchard@google.com committed
6083

6084 6085
#ifdef HAS_ARGBSHUFFLEROW_AVX2
// For BGRAToARGB, ABGRToARGB, RGBAToARGB, and ARGBToRGBA.
Frank Barchard's avatar
Frank Barchard committed
6086 6087 6088
void ARGBShuffleRow_AVX2(const uint8_t* src_argb,
                         uint8_t* dst_argb,
                         const uint8_t* shuffler,
6089
                         int width) {
6090 6091 6092 6093 6094 6095 6096 6097 6098 6099 6100 6101 6102 6103 6104 6105 6106 6107 6108 6109 6110 6111
  asm volatile(

      "vbroadcastf128 (%3),%%ymm5                \n"

      LABELALIGN
      "1:                                        \n"
      "vmovdqu   (%0),%%ymm0                     \n"
      "vmovdqu   0x20(%0),%%ymm1                 \n"
      "lea       0x40(%0),%0                     \n"
      "vpshufb   %%ymm5,%%ymm0,%%ymm0            \n"
      "vpshufb   %%ymm5,%%ymm1,%%ymm1            \n"
      "vmovdqu   %%ymm0,(%1)                     \n"
      "vmovdqu   %%ymm1,0x20(%1)                 \n"
      "lea       0x40(%1),%1                     \n"
      "sub       $0x10,%2                        \n"
      "jg        1b                              \n"
      "vzeroupper                                \n"
      : "+r"(src_argb),  // %0
        "+r"(dst_argb),  // %1
        "+r"(width)      // %2
      : "r"(shuffler)    // %3
      : "memory", "cc", "xmm0", "xmm1", "xmm5");
6112 6113 6114
}
#endif  // HAS_ARGBSHUFFLEROW_AVX2

6115
#ifdef HAS_I422TOYUY2ROW_SSE2
Frank Barchard's avatar
Frank Barchard committed
6116 6117 6118
void I422ToYUY2Row_SSE2(const uint8_t* src_y,
                        const uint8_t* src_u,
                        const uint8_t* src_v,
6119
                        uint8_t* dst_yuy2,
6120
                        int width) {
6121 6122 6123
  asm volatile(

      "sub       %1,%2                             \n"
6124

6125 6126 6127
      LABELALIGN
      "1:                                          \n"
      "movq      (%1),%%xmm2                       \n"
6128 6129 6130
      "movq      0x00(%1,%2,1),%%xmm1              \n"
      "add       $0x8,%1                           \n"
      "punpcklbw %%xmm1,%%xmm2                     \n"
6131
      "movdqu    (%0),%%xmm0                       \n"
6132
      "add       $0x10,%0                          \n"
6133 6134 6135 6136 6137 6138 6139 6140
      "movdqa    %%xmm0,%%xmm1                     \n"
      "punpcklbw %%xmm2,%%xmm0                     \n"
      "punpckhbw %%xmm2,%%xmm1                     \n"
      "movdqu    %%xmm0,(%3)                       \n"
      "movdqu    %%xmm1,0x10(%3)                   \n"
      "lea       0x20(%3),%3                       \n"
      "sub       $0x10,%4                          \n"
      "jg         1b                               \n"
6141 6142 6143 6144 6145
      : "+r"(src_y),     // %0
        "+r"(src_u),     // %1
        "+r"(src_v),     // %2
        "+r"(dst_yuy2),  // %3
        "+rm"(width)     // %4
6146
      :
6147
      : "memory", "cc", "xmm0", "xmm1", "xmm2");
fbarchard@google.com's avatar
fbarchard@google.com committed
6148
}
6149
#endif  // HAS_I422TOYUY2ROW_SSE2
fbarchard@google.com's avatar
fbarchard@google.com committed
6150

6151
#ifdef HAS_I422TOUYVYROW_SSE2
Frank Barchard's avatar
Frank Barchard committed
6152 6153 6154
void I422ToUYVYRow_SSE2(const uint8_t* src_y,
                        const uint8_t* src_u,
                        const uint8_t* src_v,
6155
                        uint8_t* dst_uyvy,
6156
                        int width) {
6157 6158 6159 6160 6161 6162 6163
  asm volatile(

      "sub        %1,%2                            \n"

      LABELALIGN
      "1:                                          \n"
      "movq      (%1),%%xmm2                       \n"
6164 6165 6166
      "movq      0x00(%1,%2,1),%%xmm1              \n"
      "add       $0x8,%1                           \n"
      "punpcklbw %%xmm1,%%xmm2                     \n"
6167 6168
      "movdqu    (%0),%%xmm0                       \n"
      "movdqa    %%xmm2,%%xmm1                     \n"
6169
      "add       $0x10,%0                          \n"
6170 6171 6172 6173 6174 6175 6176
      "punpcklbw %%xmm0,%%xmm1                     \n"
      "punpckhbw %%xmm0,%%xmm2                     \n"
      "movdqu    %%xmm1,(%3)                       \n"
      "movdqu    %%xmm2,0x10(%3)                   \n"
      "lea       0x20(%3),%3                       \n"
      "sub       $0x10,%4                          \n"
      "jg         1b                               \n"
6177 6178 6179 6180 6181
      : "+r"(src_y),     // %0
        "+r"(src_u),     // %1
        "+r"(src_v),     // %2
        "+r"(dst_uyvy),  // %3
        "+rm"(width)     // %4
6182
      :
6183
      : "memory", "cc", "xmm0", "xmm1", "xmm2");
fbarchard@google.com's avatar
fbarchard@google.com committed
6184
}
6185
#endif  // HAS_I422TOUYVYROW_SSE2
fbarchard@google.com's avatar
fbarchard@google.com committed
6186

Frank Barchard's avatar
Frank Barchard committed
6187 6188 6189 6190
#ifdef HAS_I422TOYUY2ROW_AVX2
void I422ToYUY2Row_AVX2(const uint8_t* src_y,
                        const uint8_t* src_u,
                        const uint8_t* src_v,
6191
                        uint8_t* dst_yuy2,
Frank Barchard's avatar
Frank Barchard committed
6192 6193 6194 6195 6196 6197 6198
                        int width) {
  asm volatile(

      "sub       %1,%2                             \n"

      LABELALIGN
      "1:                                          \n"
6199 6200
      "vpmovzxbw  (%1),%%ymm1                      \n"
      "vpmovzxbw  0x00(%1,%2,1),%%ymm2             \n"
6201
      "add        $0x10,%1                         \n"
6202 6203
      "vpsllw     $0x8,%%ymm2,%%ymm2               \n"
      "vpor       %%ymm1,%%ymm2,%%ymm2             \n"
Frank Barchard's avatar
Frank Barchard committed
6204
      "vmovdqu    (%0),%%ymm0                      \n"
6205
      "add        $0x20,%0                         \n"
6206 6207 6208 6209 6210 6211
      "vpunpcklbw %%ymm2,%%ymm0,%%ymm1             \n"
      "vpunpckhbw %%ymm2,%%ymm0,%%ymm2             \n"
      "vextractf128 $0x0,%%ymm1,(%3)               \n"
      "vextractf128 $0x0,%%ymm2,0x10(%3)           \n"
      "vextractf128 $0x1,%%ymm1,0x20(%3)           \n"
      "vextractf128 $0x1,%%ymm2,0x30(%3)           \n"
Frank Barchard's avatar
Frank Barchard committed
6212 6213 6214 6215
      "lea        0x40(%3),%3                      \n"
      "sub        $0x20,%4                         \n"
      "jg         1b                               \n"
      "vzeroupper                                  \n"
6216 6217 6218 6219 6220
      : "+r"(src_y),     // %0
        "+r"(src_u),     // %1
        "+r"(src_v),     // %2
        "+r"(dst_yuy2),  // %3
        "+rm"(width)     // %4
Frank Barchard's avatar
Frank Barchard committed
6221
      :
6222
      : "memory", "cc", "xmm0", "xmm1", "xmm2");
Frank Barchard's avatar
Frank Barchard committed
6223 6224 6225 6226 6227 6228 6229
}
#endif  // HAS_I422TOYUY2ROW_AVX2

#ifdef HAS_I422TOUYVYROW_AVX2
void I422ToUYVYRow_AVX2(const uint8_t* src_y,
                        const uint8_t* src_u,
                        const uint8_t* src_v,
6230
                        uint8_t* dst_uyvy,
Frank Barchard's avatar
Frank Barchard committed
6231 6232 6233 6234 6235 6236 6237
                        int width) {
  asm volatile(

      "sub        %1,%2                            \n"

      LABELALIGN
      "1:                                          \n"
6238 6239 6240 6241 6242
      "vpmovzxbw  (%1),%%ymm1                      \n"
      "vpmovzxbw  0x00(%1,%2,1),%%ymm2             \n"
      "add        $0x10,%1                         \n"
      "vpsllw     $0x8,%%ymm2,%%ymm2               \n"
      "vpor       %%ymm1,%%ymm2,%%ymm2             \n"
Frank Barchard's avatar
Frank Barchard committed
6243
      "vmovdqu    (%0),%%ymm0                      \n"
6244
      "add        $0x20,%0                         \n"
Frank Barchard's avatar
Frank Barchard committed
6245 6246
      "vpunpcklbw %%ymm0,%%ymm2,%%ymm1             \n"
      "vpunpckhbw %%ymm0,%%ymm2,%%ymm2             \n"
6247 6248 6249 6250
      "vextractf128 $0x0,%%ymm1,(%3)               \n"
      "vextractf128 $0x0,%%ymm2,0x10(%3)           \n"
      "vextractf128 $0x1,%%ymm1,0x20(%3)           \n"
      "vextractf128 $0x1,%%ymm2,0x30(%3)           \n"
Frank Barchard's avatar
Frank Barchard committed
6251 6252 6253 6254
      "lea        0x40(%3),%3                      \n"
      "sub        $0x20,%4                         \n"
      "jg         1b                               \n"
      "vzeroupper                                  \n"
6255 6256 6257 6258 6259
      : "+r"(src_y),     // %0
        "+r"(src_u),     // %1
        "+r"(src_v),     // %2
        "+r"(dst_uyvy),  // %3
        "+rm"(width)     // %4
Frank Barchard's avatar
Frank Barchard committed
6260
      :
6261
      : "memory", "cc", "xmm0", "xmm1", "xmm2");
Frank Barchard's avatar
Frank Barchard committed
6262 6263 6264
}
#endif  // HAS_I422TOUYVYROW_AVX2

6265
#ifdef HAS_ARGBPOLYNOMIALROW_SSE2
Frank Barchard's avatar
Frank Barchard committed
6266 6267
void ARGBPolynomialRow_SSE2(const uint8_t* src_argb,
                            uint8_t* dst_argb,
6268
                            const float* poly,
6269
                            int width) {
6270
  asm volatile(
6271

6272 6273 6274 6275 6276 6277 6278 6279 6280 6281 6282 6283 6284 6285 6286 6287 6288 6289 6290 6291 6292 6293 6294 6295 6296 6297 6298 6299 6300 6301 6302 6303 6304 6305 6306 6307 6308 6309 6310 6311 6312 6313 6314 6315 6316 6317
      "pxor      %%xmm3,%%xmm3                   \n"

      // 2 pixel loop.
      LABELALIGN
      "1:                                        \n"
      "movq      (%0),%%xmm0                     \n"
      "lea       0x8(%0),%0                      \n"
      "punpcklbw %%xmm3,%%xmm0                   \n"
      "movdqa    %%xmm0,%%xmm4                   \n"
      "punpcklwd %%xmm3,%%xmm0                   \n"
      "punpckhwd %%xmm3,%%xmm4                   \n"
      "cvtdq2ps  %%xmm0,%%xmm0                   \n"
      "cvtdq2ps  %%xmm4,%%xmm4                   \n"
      "movdqa    %%xmm0,%%xmm1                   \n"
      "movdqa    %%xmm4,%%xmm5                   \n"
      "mulps     0x10(%3),%%xmm0                 \n"
      "mulps     0x10(%3),%%xmm4                 \n"
      "addps     (%3),%%xmm0                     \n"
      "addps     (%3),%%xmm4                     \n"
      "movdqa    %%xmm1,%%xmm2                   \n"
      "movdqa    %%xmm5,%%xmm6                   \n"
      "mulps     %%xmm1,%%xmm2                   \n"
      "mulps     %%xmm5,%%xmm6                   \n"
      "mulps     %%xmm2,%%xmm1                   \n"
      "mulps     %%xmm6,%%xmm5                   \n"
      "mulps     0x20(%3),%%xmm2                 \n"
      "mulps     0x20(%3),%%xmm6                 \n"
      "mulps     0x30(%3),%%xmm1                 \n"
      "mulps     0x30(%3),%%xmm5                 \n"
      "addps     %%xmm2,%%xmm0                   \n"
      "addps     %%xmm6,%%xmm4                   \n"
      "addps     %%xmm1,%%xmm0                   \n"
      "addps     %%xmm5,%%xmm4                   \n"
      "cvttps2dq %%xmm0,%%xmm0                   \n"
      "cvttps2dq %%xmm4,%%xmm4                   \n"
      "packuswb  %%xmm4,%%xmm0                   \n"
      "packuswb  %%xmm0,%%xmm0                   \n"
      "movq      %%xmm0,(%1)                     \n"
      "lea       0x8(%1),%1                      \n"
      "sub       $0x2,%2                         \n"
      "jg        1b                              \n"
      : "+r"(src_argb),  // %0
        "+r"(dst_argb),  // %1
        "+r"(width)      // %2
      : "r"(poly)        // %3
      : "memory", "cc", "xmm0", "xmm1", "xmm2", "xmm3", "xmm4", "xmm5", "xmm6");
6318 6319 6320
}
#endif  // HAS_ARGBPOLYNOMIALROW_SSE2

6321
#ifdef HAS_ARGBPOLYNOMIALROW_AVX2
Frank Barchard's avatar
Frank Barchard committed
6322 6323
void ARGBPolynomialRow_AVX2(const uint8_t* src_argb,
                            uint8_t* dst_argb,
6324
                            const float* poly,
6325
                            int width) {
6326 6327 6328 6329 6330
  asm volatile(
      "vbroadcastf128 (%3),%%ymm4                \n"
      "vbroadcastf128 0x10(%3),%%ymm5            \n"
      "vbroadcastf128 0x20(%3),%%ymm6            \n"
      "vbroadcastf128 0x30(%3),%%ymm7            \n"
6331

6332 6333 6334 6335 6336 6337 6338 6339 6340 6341 6342 6343 6344 6345 6346 6347 6348 6349 6350 6351 6352 6353 6354 6355 6356 6357 6358
      // 2 pixel loop.
      LABELALIGN
      "1:                                        \n"
      "vpmovzxbd   (%0),%%ymm0                   \n"  // 2 ARGB pixels
      "lea         0x8(%0),%0                    \n"
      "vcvtdq2ps   %%ymm0,%%ymm0                 \n"  // X 8 floats
      "vmulps      %%ymm0,%%ymm0,%%ymm2          \n"  // X * X
      "vmulps      %%ymm7,%%ymm0,%%ymm3          \n"  // C3 * X
      "vfmadd132ps %%ymm5,%%ymm4,%%ymm0          \n"  // result = C0 + C1 * X
      "vfmadd231ps %%ymm6,%%ymm2,%%ymm0          \n"  // result += C2 * X * X
      "vfmadd231ps %%ymm3,%%ymm2,%%ymm0          \n"  // result += C3 * X * X *
                                                      // X
      "vcvttps2dq  %%ymm0,%%ymm0                 \n"
      "vpackusdw   %%ymm0,%%ymm0,%%ymm0          \n"
      "vpermq      $0xd8,%%ymm0,%%ymm0           \n"
      "vpackuswb   %%xmm0,%%xmm0,%%xmm0          \n"
      "vmovq       %%xmm0,(%1)                   \n"
      "lea         0x8(%1),%1                    \n"
      "sub         $0x2,%2                       \n"
      "jg          1b                            \n"
      "vzeroupper                                \n"
      : "+r"(src_argb),  // %0
        "+r"(dst_argb),  // %1
        "+r"(width)      // %2
      : "r"(poly)        // %3
      : "memory", "cc", "xmm0", "xmm1", "xmm2", "xmm3", "xmm4", "xmm5", "xmm6",
        "xmm7");
6359 6360 6361
}
#endif  // HAS_ARGBPOLYNOMIALROW_AVX2

6362
#ifdef HAS_HALFFLOATROW_SSE2
6363
static float kScaleBias = 1.9259299444e-34f;
6364 6365 6366 6367
void HalfFloatRow_SSE2(const uint16_t* src,
                       uint16_t* dst,
                       float scale,
                       int width) {
6368
  scale *= kScaleBias;
6369 6370 6371 6372 6373
  asm volatile(
      "movd        %3,%%xmm4                     \n"
      "pshufd      $0x0,%%xmm4,%%xmm4            \n"
      "pxor        %%xmm5,%%xmm5                 \n"
      "sub         %0,%1                         \n"
6374

6375 6376 6377 6378 6379 6380 6381 6382 6383 6384 6385 6386 6387 6388 6389 6390 6391 6392 6393 6394 6395 6396 6397
      // 16 pixel loop.
      LABELALIGN
      "1:                                        \n"
      "movdqu      (%0),%%xmm2                   \n"  // 8 shorts
      "add         $0x10,%0                      \n"
      "movdqa      %%xmm2,%%xmm3                 \n"
      "punpcklwd   %%xmm5,%%xmm2                 \n"  // 8 ints in xmm2/1
      "cvtdq2ps    %%xmm2,%%xmm2                 \n"  // 8 floats
      "punpckhwd   %%xmm5,%%xmm3                 \n"
      "cvtdq2ps    %%xmm3,%%xmm3                 \n"
      "mulps       %%xmm4,%%xmm2                 \n"
      "mulps       %%xmm4,%%xmm3                 \n"
      "psrld       $0xd,%%xmm2                   \n"
      "psrld       $0xd,%%xmm3                   \n"
      "packssdw    %%xmm3,%%xmm2                 \n"
      "movdqu      %%xmm2,-0x10(%0,%1,1)         \n"
      "sub         $0x8,%2                       \n"
      "jg          1b                            \n"
      : "+r"(src),   // %0
        "+r"(dst),   // %1
        "+r"(width)  // %2
      : "m"(scale)   // %3
      : "memory", "cc", "xmm2", "xmm3", "xmm4", "xmm5");
6398 6399 6400
}
#endif  // HAS_HALFFLOATROW_SSE2

6401
#ifdef HAS_HALFFLOATROW_AVX2
6402 6403 6404 6405
void HalfFloatRow_AVX2(const uint16_t* src,
                       uint16_t* dst,
                       float scale,
                       int width) {
6406
  scale *= kScaleBias;
6407 6408 6409 6410
  asm volatile(
      "vbroadcastss  %3, %%ymm4                  \n"
      "vpxor      %%ymm5,%%ymm5,%%ymm5           \n"
      "sub        %0,%1                          \n"
6411

6412 6413 6414 6415 6416 6417 6418 6419 6420 6421 6422 6423 6424 6425 6426 6427 6428
      // 16 pixel loop.
      LABELALIGN
      "1:                                        \n"
      "vmovdqu    (%0),%%ymm2                    \n"  // 16 shorts
      "add        $0x20,%0                       \n"
      "vpunpckhwd %%ymm5,%%ymm2,%%ymm3           \n"  // mutates
      "vpunpcklwd %%ymm5,%%ymm2,%%ymm2           \n"
      "vcvtdq2ps  %%ymm3,%%ymm3                  \n"
      "vcvtdq2ps  %%ymm2,%%ymm2                  \n"
      "vmulps     %%ymm3,%%ymm4,%%ymm3           \n"
      "vmulps     %%ymm2,%%ymm4,%%ymm2           \n"
      "vpsrld     $0xd,%%ymm3,%%ymm3             \n"
      "vpsrld     $0xd,%%ymm2,%%ymm2             \n"
      "vpackssdw  %%ymm3, %%ymm2, %%ymm2         \n"  // unmutates
      "vmovdqu    %%ymm2,-0x20(%0,%1,1)          \n"
      "sub        $0x10,%2                       \n"
      "jg         1b                             \n"
6429

6430 6431 6432 6433
      "vzeroupper                                \n"
      : "+r"(src),   // %0
        "+r"(dst),   // %1
        "+r"(width)  // %2
6434
#if defined(__x86_64__)
6435
      : "x"(scale)  // %3
6436
#else
6437
      : "m"(scale)  // %3
6438
#endif
6439
      : "memory", "cc", "xmm2", "xmm3", "xmm4", "xmm5");
6440 6441 6442
}
#endif  // HAS_HALFFLOATROW_AVX2

6443
#ifdef HAS_HALFFLOATROW_F16C
6444 6445 6446 6447 6448 6449 6450
void HalfFloatRow_F16C(const uint16_t* src,
                       uint16_t* dst,
                       float scale,
                       int width) {
  asm volatile(
      "vbroadcastss  %3, %%ymm4                  \n"
      "sub        %0,%1                          \n"
6451

6452 6453 6454 6455 6456 6457 6458 6459 6460 6461 6462 6463 6464 6465 6466 6467 6468 6469 6470 6471
      // 16 pixel loop.
      LABELALIGN
      "1:                                        \n"
      "vpmovzxwd   (%0),%%ymm2                   \n"  // 16 shorts -> 16 ints
      "vpmovzxwd   0x10(%0),%%ymm3               \n"
      "vcvtdq2ps   %%ymm2,%%ymm2                 \n"
      "vcvtdq2ps   %%ymm3,%%ymm3                 \n"
      "vmulps      %%ymm2,%%ymm4,%%ymm2          \n"
      "vmulps      %%ymm3,%%ymm4,%%ymm3          \n"
      "vcvtps2ph   $3, %%ymm2, %%xmm2            \n"
      "vcvtps2ph   $3, %%ymm3, %%xmm3            \n"
      "vmovdqu     %%xmm2,0x00(%0,%1,1)          \n"
      "vmovdqu     %%xmm3,0x10(%0,%1,1)          \n"
      "add         $0x20,%0                      \n"
      "sub         $0x10,%2                      \n"
      "jg          1b                            \n"
      "vzeroupper                                \n"
      : "+r"(src),   // %0
        "+r"(dst),   // %1
        "+r"(width)  // %2
6472
#if defined(__x86_64__)
6473
      : "x"(scale)  // %3
6474
#else
6475
      : "m"(scale)  // %3
6476
#endif
6477
      : "memory", "cc", "xmm2", "xmm3", "xmm4");
6478
}
6479
#endif  // HAS_HALFFLOATROW_F16C
6480

6481
#ifdef HAS_HALFFLOATROW_F16C
Frank Barchard's avatar
Frank Barchard committed
6482
void HalfFloat1Row_F16C(const uint16_t* src, uint16_t* dst, float, int width) {
6483 6484 6485 6486 6487 6488 6489 6490 6491 6492 6493 6494 6495 6496 6497 6498 6499 6500 6501 6502 6503 6504
  asm volatile(
      "sub        %0,%1                          \n"
      // 16 pixel loop.
      LABELALIGN
      "1:                                        \n"
      "vpmovzxwd   (%0),%%ymm2                   \n"  // 16 shorts -> 16 ints
      "vpmovzxwd   0x10(%0),%%ymm3               \n"
      "vcvtdq2ps   %%ymm2,%%ymm2                 \n"
      "vcvtdq2ps   %%ymm3,%%ymm3                 \n"
      "vcvtps2ph   $3, %%ymm2, %%xmm2            \n"
      "vcvtps2ph   $3, %%ymm3, %%xmm3            \n"
      "vmovdqu     %%xmm2,0x00(%0,%1,1)          \n"
      "vmovdqu     %%xmm3,0x10(%0,%1,1)          \n"
      "add         $0x20,%0                      \n"
      "sub         $0x10,%2                      \n"
      "jg          1b                            \n"
      "vzeroupper                                \n"
      : "+r"(src),   // %0
        "+r"(dst),   // %1
        "+r"(width)  // %2
      :
      : "memory", "cc", "xmm2", "xmm3");
6505 6506 6507
}
#endif  // HAS_HALFFLOATROW_F16C

6508 6509
#ifdef HAS_ARGBCOLORTABLEROW_X86
// Tranform ARGB pixels with color table.
Frank Barchard's avatar
Frank Barchard committed
6510 6511
void ARGBColorTableRow_X86(uint8_t* dst_argb,
                           const uint8_t* table_argb,
6512
                           int width) {
6513
  uintptr_t pixel_temp;
6514 6515 6516 6517 6518 6519 6520 6521 6522 6523 6524 6525 6526 6527 6528 6529 6530 6531 6532 6533 6534 6535 6536 6537
  asm volatile(
      // 1 pixel loop.
      LABELALIGN
      "1:                                        \n"
      "movzb     (%0),%1                         \n"
      "lea       0x4(%0),%0                      \n"
      "movzb     0x00(%3,%1,4),%1                \n"
      "mov       %b1,-0x4(%0)                    \n"
      "movzb     -0x3(%0),%1                     \n"
      "movzb     0x01(%3,%1,4),%1                \n"
      "mov       %b1,-0x3(%0)                    \n"
      "movzb     -0x2(%0),%1                     \n"
      "movzb     0x02(%3,%1,4),%1                \n"
      "mov       %b1,-0x2(%0)                    \n"
      "movzb     -0x1(%0),%1                     \n"
      "movzb     0x03(%3,%1,4),%1                \n"
      "mov       %b1,-0x1(%0)                    \n"
      "dec       %2                              \n"
      "jg        1b                              \n"
      : "+r"(dst_argb),     // %0
        "=&d"(pixel_temp),  // %1
        "+r"(width)         // %2
      : "r"(table_argb)     // %3
      : "memory", "cc");
6538 6539 6540 6541 6542
}
#endif  // HAS_ARGBCOLORTABLEROW_X86

#ifdef HAS_RGBCOLORTABLEROW_X86
// Tranform RGB pixels with color table.
6543 6544 6545
void RGBColorTableRow_X86(uint8_t* dst_argb,
                          const uint8_t* table_argb,
                          int width) {
6546
  uintptr_t pixel_temp;
6547 6548 6549 6550 6551 6552 6553 6554 6555 6556 6557 6558 6559 6560 6561 6562 6563 6564 6565 6566 6567
  asm volatile(
      // 1 pixel loop.
      LABELALIGN
      "1:                                        \n"
      "movzb     (%0),%1                         \n"
      "lea       0x4(%0),%0                      \n"
      "movzb     0x00(%3,%1,4),%1                \n"
      "mov       %b1,-0x4(%0)                    \n"
      "movzb     -0x3(%0),%1                     \n"
      "movzb     0x01(%3,%1,4),%1                \n"
      "mov       %b1,-0x3(%0)                    \n"
      "movzb     -0x2(%0),%1                     \n"
      "movzb     0x02(%3,%1,4),%1                \n"
      "mov       %b1,-0x2(%0)                    \n"
      "dec       %2                              \n"
      "jg        1b                              \n"
      : "+r"(dst_argb),     // %0
        "=&d"(pixel_temp),  // %1
        "+r"(width)         // %2
      : "r"(table_argb)     // %3
      : "memory", "cc");
6568 6569 6570
}
#endif  // HAS_RGBCOLORTABLEROW_X86

fbarchard@google.com's avatar
fbarchard@google.com committed
6571 6572
#ifdef HAS_ARGBLUMACOLORTABLEROW_SSSE3
// Tranform RGB pixels with luma table.
Frank Barchard's avatar
Frank Barchard committed
6573 6574
void ARGBLumaColorTableRow_SSSE3(const uint8_t* src_argb,
                                 uint8_t* dst_argb,
6575
                                 int width,
Frank Barchard's avatar
Frank Barchard committed
6576 6577
                                 const uint8_t* luma,
                                 uint32_t lumacoeff) {
6578 6579
  uintptr_t pixel_temp;
  uintptr_t table_temp;
6580 6581 6582 6583 6584 6585
  asm volatile(
      "movd      %6,%%xmm3                       \n"
      "pshufd    $0x0,%%xmm3,%%xmm3              \n"
      "pcmpeqb   %%xmm4,%%xmm4                   \n"
      "psllw     $0x8,%%xmm4                     \n"
      "pxor      %%xmm5,%%xmm5                   \n"
fbarchard@google.com's avatar
fbarchard@google.com committed
6586

6587 6588 6589 6590 6591 6592 6593 6594 6595 6596 6597 6598 6599 6600 6601 6602 6603 6604 6605 6606 6607 6608 6609 6610 6611 6612 6613 6614 6615 6616 6617 6618 6619 6620 6621 6622 6623 6624 6625 6626 6627 6628 6629 6630 6631 6632 6633 6634 6635 6636 6637 6638 6639 6640 6641 6642 6643 6644 6645 6646 6647 6648 6649 6650 6651 6652 6653 6654 6655 6656 6657 6658 6659 6660 6661 6662 6663 6664 6665 6666 6667 6668
      // 4 pixel loop.
      LABELALIGN
      "1:                                        \n"
      "movdqu    (%2),%%xmm0                     \n"
      "pmaddubsw %%xmm3,%%xmm0                   \n"
      "phaddw    %%xmm0,%%xmm0                   \n"
      "pand      %%xmm4,%%xmm0                   \n"
      "punpcklwd %%xmm5,%%xmm0                   \n"
      "movd      %%xmm0,%k1                      \n"  // 32 bit offset
      "add       %5,%1                           \n"
      "pshufd    $0x39,%%xmm0,%%xmm0             \n"

      "movzb     (%2),%0                         \n"
      "movzb     0x00(%1,%0,1),%0                \n"
      "mov       %b0,(%3)                        \n"
      "movzb     0x1(%2),%0                      \n"
      "movzb     0x00(%1,%0,1),%0                \n"
      "mov       %b0,0x1(%3)                     \n"
      "movzb     0x2(%2),%0                      \n"
      "movzb     0x00(%1,%0,1),%0                \n"
      "mov       %b0,0x2(%3)                     \n"
      "movzb     0x3(%2),%0                      \n"
      "mov       %b0,0x3(%3)                     \n"

      "movd      %%xmm0,%k1                      \n"  // 32 bit offset
      "add       %5,%1                           \n"
      "pshufd    $0x39,%%xmm0,%%xmm0             \n"

      "movzb     0x4(%2),%0                      \n"
      "movzb     0x00(%1,%0,1),%0                \n"
      "mov       %b0,0x4(%3)                     \n"
      "movzb     0x5(%2),%0                      \n"
      "movzb     0x00(%1,%0,1),%0                \n"
      "mov       %b0,0x5(%3)                     \n"
      "movzb     0x6(%2),%0                      \n"
      "movzb     0x00(%1,%0,1),%0                \n"
      "mov       %b0,0x6(%3)                     \n"
      "movzb     0x7(%2),%0                      \n"
      "mov       %b0,0x7(%3)                     \n"

      "movd      %%xmm0,%k1                      \n"  // 32 bit offset
      "add       %5,%1                           \n"
      "pshufd    $0x39,%%xmm0,%%xmm0             \n"

      "movzb     0x8(%2),%0                      \n"
      "movzb     0x00(%1,%0,1),%0                \n"
      "mov       %b0,0x8(%3)                     \n"
      "movzb     0x9(%2),%0                      \n"
      "movzb     0x00(%1,%0,1),%0                \n"
      "mov       %b0,0x9(%3)                     \n"
      "movzb     0xa(%2),%0                      \n"
      "movzb     0x00(%1,%0,1),%0                \n"
      "mov       %b0,0xa(%3)                     \n"
      "movzb     0xb(%2),%0                      \n"
      "mov       %b0,0xb(%3)                     \n"

      "movd      %%xmm0,%k1                      \n"  // 32 bit offset
      "add       %5,%1                           \n"

      "movzb     0xc(%2),%0                      \n"
      "movzb     0x00(%1,%0,1),%0                \n"
      "mov       %b0,0xc(%3)                     \n"
      "movzb     0xd(%2),%0                      \n"
      "movzb     0x00(%1,%0,1),%0                \n"
      "mov       %b0,0xd(%3)                     \n"
      "movzb     0xe(%2),%0                      \n"
      "movzb     0x00(%1,%0,1),%0                \n"
      "mov       %b0,0xe(%3)                     \n"
      "movzb     0xf(%2),%0                      \n"
      "mov       %b0,0xf(%3)                     \n"
      "lea       0x10(%2),%2                     \n"
      "lea       0x10(%3),%3                     \n"
      "sub       $0x4,%4                         \n"
      "jg        1b                              \n"
      : "=&d"(pixel_temp),  // %0
        "=&a"(table_temp),  // %1
        "+r"(src_argb),     // %2
        "+r"(dst_argb),     // %3
        "+rm"(width)        // %4
      : "r"(luma),          // %5
        "rm"(lumacoeff)     // %6
      : "memory", "cc", "xmm0", "xmm3", "xmm4", "xmm5");
fbarchard@google.com's avatar
fbarchard@google.com committed
6669 6670 6671
}
#endif  // HAS_ARGBLUMACOLORTABLEROW_SSSE3

6672 6673
#endif  // defined(__x86_64__) || defined(__i386__)

6674
#ifdef __cplusplus
mikhal@webrtc.org's avatar
mikhal@webrtc.org committed
6675
}  // extern "C"
6676 6677
}  // namespace libyuv
#endif