macros_msa.h 100 KB
Newer Older
1
/*
2
 * Copyright (c) 2016, Alliance for Open Media. All rights reserved
3
 *
4 5 6 7 8 9
 * This source code is subject to the terms of the BSD 2 Clause License and
 * the Alliance for Open Media Patent License 1.0. If the BSD 2 Clause License
 * was not distributed with this source code in the LICENSE file, you can
 * obtain it at www.aomedia.org/license/software. If the Alliance for Open
 * Media Patent License 1.0 was not distributed with this source code in the
 * PATENTS file, you can obtain it at www.aomedia.org/license/patent.
10 11 12 13 14 15 16 17
 */

#ifndef VPX_DSP_MIPS_MACROS_MSA_H_
#define VPX_DSP_MIPS_MACROS_MSA_H_

#include <msa.h>

#include "./vpx_config.h"
Yaowu Xu's avatar
Yaowu Xu committed
18
#include "aom/vpx_integer.h"
19 20 21 22 23 24 25 26 27

#define LD_B(RTYPE, psrc) *((const RTYPE *)(psrc))
#define LD_UB(...) LD_B(v16u8, __VA_ARGS__)
#define LD_SB(...) LD_B(v16i8, __VA_ARGS__)

#define LD_H(RTYPE, psrc) *((const RTYPE *)(psrc))
#define LD_UH(...) LD_H(v8u16, __VA_ARGS__)
#define LD_SH(...) LD_H(v8i16, __VA_ARGS__)

Johann's avatar
Johann committed
28 29 30 31 32 33 34
#define LD_W(RTYPE, psrc) *((const RTYPE *)(psrc))
#define LD_SW(...) LD_W(v4i32, __VA_ARGS__)

#define ST_B(RTYPE, in, pdst) *((RTYPE *)(pdst)) = (in)
#define ST_UB(...) ST_B(v16u8, __VA_ARGS__)
#define ST_SB(...) ST_B(v16i8, __VA_ARGS__)

35 36 37
#define ST_H(RTYPE, in, pdst) *((RTYPE *)(pdst)) = (in)
#define ST_SH(...) ST_H(v8i16, __VA_ARGS__)

Johann's avatar
Johann committed
38 39 40
#define ST_W(RTYPE, in, pdst) *((RTYPE *)(pdst)) = (in)
#define ST_SW(...) ST_W(v4i32, __VA_ARGS__)

41
#if (__mips_isa_rev >= 6)
clang-format's avatar
clang-format committed
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
#define LH(psrc)                                          \
  ({                                                      \
    const uint8_t *psrc_m = (const uint8_t *)(psrc);      \
    uint16_t val_m;                                       \
                                                          \
    __asm__ __volatile__("lh  %[val_m],  %[psrc_m]  \n\t" \
                                                          \
                         : [val_m] "=r"(val_m)            \
                         : [psrc_m] "m"(*psrc_m));        \
                                                          \
    val_m;                                                \
  })

#define LW(psrc)                                          \
  ({                                                      \
    const uint8_t *psrc_m = (const uint8_t *)(psrc);      \
    uint32_t val_m;                                       \
                                                          \
    __asm__ __volatile__("lw  %[val_m],  %[psrc_m]  \n\t" \
                                                          \
                         : [val_m] "=r"(val_m)            \
                         : [psrc_m] "m"(*psrc_m));        \
                                                          \
    val_m;                                                \
  })
67 68

#if (__mips == 64)
clang-format's avatar
clang-format committed
69 70 71 72 73 74 75 76 77 78 79 80
#define LD(psrc)                                          \
  ({                                                      \
    const uint8_t *psrc_m = (const uint8_t *)(psrc);      \
    uint64_t val_m = 0;                                   \
                                                          \
    __asm__ __volatile__("ld  %[val_m],  %[psrc_m]  \n\t" \
                                                          \
                         : [val_m] "=r"(val_m)            \
                         : [psrc_m] "m"(*psrc_m));        \
                                                          \
    val_m;                                                \
  })
81
#else  // !(__mips == 64)
clang-format's avatar
clang-format committed
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135
#define LD(psrc)                                            \
  ({                                                        \
    const uint8_t *psrc_m = (const uint8_t *)(psrc);        \
    uint32_t val0_m, val1_m;                                \
    uint64_t val_m = 0;                                     \
                                                            \
    val0_m = LW(psrc_m);                                    \
    val1_m = LW(psrc_m + 4);                                \
                                                            \
    val_m = (uint64_t)(val1_m);                             \
    val_m = (uint64_t)((val_m << 32) & 0xFFFFFFFF00000000); \
    val_m = (uint64_t)(val_m | (uint64_t)val0_m);           \
                                                            \
    val_m;                                                  \
  })
#endif  // (__mips == 64)

#define SH(val, pdst)                                     \
  {                                                       \
    uint8_t *pdst_m = (uint8_t *)(pdst);                  \
    const uint16_t val_m = (val);                         \
                                                          \
    __asm__ __volatile__("sh  %[val_m],  %[pdst_m]  \n\t" \
                                                          \
                         : [pdst_m] "=m"(*pdst_m)         \
                         : [val_m] "r"(val_m));           \
  }

#define SW(val, pdst)                                     \
  {                                                       \
    uint8_t *pdst_m = (uint8_t *)(pdst);                  \
    const uint32_t val_m = (val);                         \
                                                          \
    __asm__ __volatile__("sw  %[val_m],  %[pdst_m]  \n\t" \
                                                          \
                         : [pdst_m] "=m"(*pdst_m)         \
                         : [val_m] "r"(val_m));           \
  }

#define SD(val, pdst)                                     \
  {                                                       \
    uint8_t *pdst_m = (uint8_t *)(pdst);                  \
    const uint64_t val_m = (val);                         \
                                                          \
    __asm__ __volatile__("sd  %[val_m],  %[pdst_m]  \n\t" \
                                                          \
                         : [pdst_m] "=m"(*pdst_m)         \
                         : [val_m] "r"(val_m));           \
  }
#else  // !(__mips_isa_rev >= 6)
#define LH(psrc)                                           \
  ({                                                       \
    const uint8_t *psrc_m = (const uint8_t *)(psrc);       \
    uint16_t val_m;                                        \
136
                                                           \
clang-format's avatar
clang-format committed
137
    __asm__ __volatile__("ulh  %[val_m],  %[psrc_m]  \n\t" \
138
                                                           \
clang-format's avatar
clang-format committed
139 140
                         : [val_m] "=r"(val_m)             \
                         : [psrc_m] "m"(*psrc_m));         \
141
                                                           \
clang-format's avatar
clang-format committed
142 143
    val_m;                                                 \
  })
144

clang-format's avatar
clang-format committed
145 146 147 148 149 150 151 152 153 154 155 156
#define LW(psrc)                                           \
  ({                                                       \
    const uint8_t *psrc_m = (const uint8_t *)(psrc);       \
    uint32_t val_m;                                        \
                                                           \
    __asm__ __volatile__("ulw  %[val_m],  %[psrc_m]  \n\t" \
                                                           \
                         : [val_m] "=r"(val_m)             \
                         : [psrc_m] "m"(*psrc_m));         \
                                                           \
    val_m;                                                 \
  })
157 158

#if (__mips == 64)
clang-format's avatar
clang-format committed
159 160 161 162
#define LD(psrc)                                           \
  ({                                                       \
    const uint8_t *psrc_m = (const uint8_t *)(psrc);       \
    uint64_t val_m = 0;                                    \
163
                                                           \
clang-format's avatar
clang-format committed
164
    __asm__ __volatile__("uld  %[val_m],  %[psrc_m]  \n\t" \
165
                                                           \
clang-format's avatar
clang-format committed
166 167
                         : [val_m] "=r"(val_m)             \
                         : [psrc_m] "m"(*psrc_m));         \
168
                                                           \
clang-format's avatar
clang-format committed
169 170 171 172 173 174 175 176
    val_m;                                                 \
  })
#else  // !(__mips == 64)
#define LD(psrc)                                            \
  ({                                                        \
    const uint8_t *psrc_m1 = (const uint8_t *)(psrc);       \
    uint32_t val0_m, val1_m;                                \
    uint64_t val_m = 0;                                     \
177
                                                            \
clang-format's avatar
clang-format committed
178 179
    val0_m = LW(psrc_m1);                                   \
    val1_m = LW(psrc_m1 + 4);                               \
180
                                                            \
clang-format's avatar
clang-format committed
181 182 183 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
    val_m = (uint64_t)(val1_m);                             \
    val_m = (uint64_t)((val_m << 32) & 0xFFFFFFFF00000000); \
    val_m = (uint64_t)(val_m | (uint64_t)val0_m);           \
                                                            \
    val_m;                                                  \
  })
#endif  // (__mips == 64)

#define SH(val, pdst)                                      \
  {                                                        \
    uint8_t *pdst_m = (uint8_t *)(pdst);                   \
    const uint16_t val_m = (val);                          \
                                                           \
    __asm__ __volatile__("ush  %[val_m],  %[pdst_m]  \n\t" \
                                                           \
                         : [pdst_m] "=m"(*pdst_m)          \
                         : [val_m] "r"(val_m));            \
  }

#define SW(val, pdst)                                      \
  {                                                        \
    uint8_t *pdst_m = (uint8_t *)(pdst);                   \
    const uint32_t val_m = (val);                          \
                                                           \
    __asm__ __volatile__("usw  %[val_m],  %[pdst_m]  \n\t" \
                                                           \
                         : [pdst_m] "=m"(*pdst_m)          \
                         : [val_m] "r"(val_m));            \
  }

#define SD(val, pdst)                                        \
  {                                                          \
    uint8_t *pdst_m1 = (uint8_t *)(pdst);                    \
    uint32_t val0_m, val1_m;                                 \
                                                             \
    val0_m = (uint32_t)((val)&0x00000000FFFFFFFF);           \
    val1_m = (uint32_t)(((val) >> 32) & 0x00000000FFFFFFFF); \
                                                             \
    SW(val0_m, pdst_m1);                                     \
    SW(val1_m, pdst_m1 + 4);                                 \
  }
222 223 224 225 226 227 228 229 230 231
#endif  // (__mips_isa_rev >= 6)

/* Description : Load 4 words with stride
   Arguments   : Inputs  - psrc, stride
                 Outputs - out0, out1, out2, out3
   Details     : Load word in 'out0' from (psrc)
                 Load word in 'out1' from (psrc + stride)
                 Load word in 'out2' from (psrc + 2 * stride)
                 Load word in 'out3' from (psrc + 3 * stride)
*/
clang-format's avatar
clang-format committed
232 233 234 235 236 237 238
#define LW4(psrc, stride, out0, out1, out2, out3) \
  {                                               \
    out0 = LW((psrc));                            \
    out1 = LW((psrc) + stride);                   \
    out2 = LW((psrc) + 2 * stride);               \
    out3 = LW((psrc) + 3 * stride);               \
  }
239

240 241 242 243 244 245
/* Description : Load double words with stride
   Arguments   : Inputs  - psrc, stride
                 Outputs - out0, out1
   Details     : Load double word in 'out0' from (psrc)
                 Load double word in 'out1' from (psrc + stride)
*/
clang-format's avatar
clang-format committed
246 247 248 249 250 251 252 253 254 255
#define LD2(psrc, stride, out0, out1) \
  {                                   \
    out0 = LD((psrc));                \
    out1 = LD((psrc) + stride);       \
  }
#define LD4(psrc, stride, out0, out1, out2, out3) \
  {                                               \
    LD2((psrc), stride, out0, out1);              \
    LD2((psrc) + 2 * stride, stride, out2, out3); \
  }
256

Johann's avatar
Johann committed
257 258 259 260 261 262 263
/* Description : Store 4 words with stride
   Arguments   : Inputs - in0, in1, in2, in3, pdst, stride
   Details     : Store word from 'in0' to (pdst)
                 Store word from 'in1' to (pdst + stride)
                 Store word from 'in2' to (pdst + 2 * stride)
                 Store word from 'in3' to (pdst + 3 * stride)
*/
clang-format's avatar
clang-format committed
264 265 266 267 268 269 270
#define SW4(in0, in1, in2, in3, pdst, stride) \
  {                                           \
    SW(in0, (pdst))                           \
    SW(in1, (pdst) + stride);                 \
    SW(in2, (pdst) + 2 * stride);             \
    SW(in3, (pdst) + 3 * stride);             \
  }
Johann's avatar
Johann committed
271 272 273 274 275 276 277 278

/* Description : Store 4 double words with stride
   Arguments   : Inputs - in0, in1, in2, in3, pdst, stride
   Details     : Store double word from 'in0' to (pdst)
                 Store double word from 'in1' to (pdst + stride)
                 Store double word from 'in2' to (pdst + 2 * stride)
                 Store double word from 'in3' to (pdst + 3 * stride)
*/
clang-format's avatar
clang-format committed
279 280 281 282 283 284 285
#define SD4(in0, in1, in2, in3, pdst, stride) \
  {                                           \
    SD(in0, (pdst))                           \
    SD(in1, (pdst) + stride);                 \
    SD(in2, (pdst) + 2 * stride);             \
    SD(in3, (pdst) + 3 * stride);             \
  }
Johann's avatar
Johann committed
286

287 288 289 290 291 292 293
/* Description : Load vectors with 16 byte elements with stride
   Arguments   : Inputs  - psrc, stride
                 Outputs - out0, out1
                 Return Type - as per RTYPE
   Details     : Load 16 byte elements in 'out0' from (psrc)
                 Load 16 byte elements in 'out1' from (psrc + stride)
*/
clang-format's avatar
clang-format committed
294 295 296 297 298
#define LD_B2(RTYPE, psrc, stride, out0, out1) \
  {                                            \
    out0 = LD_B(RTYPE, (psrc));                \
    out1 = LD_B(RTYPE, (psrc) + stride);       \
  }
299
#define LD_UB2(...) LD_B2(v16u8, __VA_ARGS__)
300
#define LD_SB2(...) LD_B2(v16i8, __VA_ARGS__)
301

clang-format's avatar
clang-format committed
302 303 304 305 306
#define LD_B3(RTYPE, psrc, stride, out0, out1, out2) \
  {                                                  \
    LD_B2(RTYPE, (psrc), stride, out0, out1);        \
    out2 = LD_B(RTYPE, (psrc) + 2 * stride);         \
  }
307 308
#define LD_UB3(...) LD_B3(v16u8, __VA_ARGS__)

clang-format's avatar
clang-format committed
309 310 311 312 313
#define LD_B4(RTYPE, psrc, stride, out0, out1, out2, out3) \
  {                                                        \
    LD_B2(RTYPE, (psrc), stride, out0, out1);              \
    LD_B2(RTYPE, (psrc) + 2 * stride, stride, out2, out3); \
  }
314
#define LD_UB4(...) LD_B4(v16u8, __VA_ARGS__)
315
#define LD_SB4(...) LD_B4(v16i8, __VA_ARGS__)
316

clang-format's avatar
clang-format committed
317 318 319 320 321
#define LD_B5(RTYPE, psrc, stride, out0, out1, out2, out3, out4) \
  {                                                              \
    LD_B4(RTYPE, (psrc), stride, out0, out1, out2, out3);        \
    out4 = LD_B(RTYPE, (psrc) + 4 * stride);                     \
  }
322
#define LD_UB5(...) LD_B5(v16u8, __VA_ARGS__)
Johann's avatar
Johann committed
323 324
#define LD_SB5(...) LD_B5(v16i8, __VA_ARGS__)

clang-format's avatar
clang-format committed
325 326 327 328 329
#define LD_B7(RTYPE, psrc, stride, out0, out1, out2, out3, out4, out5, out6) \
  {                                                                          \
    LD_B5(RTYPE, (psrc), stride, out0, out1, out2, out3, out4);              \
    LD_B2(RTYPE, (psrc) + 5 * stride, stride, out5, out6);                   \
  }
Johann's avatar
Johann committed
330
#define LD_SB7(...) LD_B7(v16i8, __VA_ARGS__)
331

clang-format's avatar
clang-format committed
332 333 334 335 336 337
#define LD_B8(RTYPE, psrc, stride, out0, out1, out2, out3, out4, out5, out6, \
              out7)                                                          \
  {                                                                          \
    LD_B4(RTYPE, (psrc), stride, out0, out1, out2, out3);                    \
    LD_B4(RTYPE, (psrc) + 4 * stride, stride, out4, out5, out6, out7);       \
  }
338 339 340
#define LD_UB8(...) LD_B8(v16u8, __VA_ARGS__)
#define LD_SB8(...) LD_B8(v16i8, __VA_ARGS__)

341 342 343 344 345 346
/* Description : Load vectors with 8 halfword elements with stride
   Arguments   : Inputs  - psrc, stride
                 Outputs - out0, out1
   Details     : Load 8 halfword elements in 'out0' from (psrc)
                 Load 8 halfword elements in 'out1' from (psrc + stride)
*/
clang-format's avatar
clang-format committed
347 348 349 350 351
#define LD_H2(RTYPE, psrc, stride, out0, out1) \
  {                                            \
    out0 = LD_H(RTYPE, (psrc));                \
    out1 = LD_H(RTYPE, (psrc) + (stride));     \
  }
Johann's avatar
Johann committed
352
#define LD_SH2(...) LD_H2(v8i16, __VA_ARGS__)
353

clang-format's avatar
clang-format committed
354 355 356 357 358
#define LD_H4(RTYPE, psrc, stride, out0, out1, out2, out3) \
  {                                                        \
    LD_H2(RTYPE, (psrc), stride, out0, out1);              \
    LD_H2(RTYPE, (psrc) + 2 * stride, stride, out2, out3); \
  }
359 360
#define LD_SH4(...) LD_H4(v8i16, __VA_ARGS__)

clang-format's avatar
clang-format committed
361 362 363 364 365 366
#define LD_H8(RTYPE, psrc, stride, out0, out1, out2, out3, out4, out5, out6, \
              out7)                                                          \
  {                                                                          \
    LD_H4(RTYPE, (psrc), stride, out0, out1, out2, out3);                    \
    LD_H4(RTYPE, (psrc) + 4 * stride, stride, out4, out5, out6, out7);       \
  }
Johann's avatar
Johann committed
367 368
#define LD_SH8(...) LD_H8(v8i16, __VA_ARGS__)

clang-format's avatar
clang-format committed
369 370 371 372 373 374 375 376
#define LD_H16(RTYPE, psrc, stride, out0, out1, out2, out3, out4, out5, out6,  \
               out7, out8, out9, out10, out11, out12, out13, out14, out15)     \
  {                                                                            \
    LD_H8(RTYPE, (psrc), stride, out0, out1, out2, out3, out4, out5, out6,     \
          out7);                                                               \
    LD_H8(RTYPE, (psrc) + 8 * stride, stride, out8, out9, out10, out11, out12, \
          out13, out14, out15);                                                \
  }
Johann's avatar
Johann committed
377 378 379 380 381 382 383
#define LD_SH16(...) LD_H16(v8i16, __VA_ARGS__)

/* Description : Load 4x4 block of signed halfword elements from 1D source
                 data into 4 vectors (Each vector with 4 signed halfwords)
   Arguments   : Input   - psrc
                 Outputs - out0, out1, out2, out3
*/
clang-format's avatar
clang-format committed
384 385 386 387 388 389 390
#define LD4x4_SH(psrc, out0, out1, out2, out3)            \
  {                                                       \
    out0 = LD_SH(psrc);                                   \
    out2 = LD_SH(psrc + 8);                               \
    out1 = (v8i16)__msa_ilvl_d((v2i64)out0, (v2i64)out0); \
    out3 = (v8i16)__msa_ilvl_d((v2i64)out2, (v2i64)out2); \
  }
Johann's avatar
Johann committed
391 392 393 394 395 396

/* Description : Load 2 vectors of signed word elements with stride
   Arguments   : Inputs  - psrc, stride
                 Outputs - out0, out1
                 Return Type - signed word
*/
clang-format's avatar
clang-format committed
397 398 399 400 401
#define LD_SW2(psrc, stride, out0, out1) \
  {                                      \
    out0 = LD_SW((psrc));                \
    out1 = LD_SW((psrc) + stride);       \
  }
Johann's avatar
Johann committed
402 403 404 405 406 407

/* Description : Store vectors of 16 byte elements with stride
   Arguments   : Inputs - in0, in1, pdst, stride
   Details     : Store 16 byte elements from 'in0' to (pdst)
                 Store 16 byte elements from 'in1' to (pdst + stride)
*/
clang-format's avatar
clang-format committed
408 409 410 411 412
#define ST_B2(RTYPE, in0, in1, pdst, stride) \
  {                                          \
    ST_B(RTYPE, in0, (pdst));                \
    ST_B(RTYPE, in1, (pdst) + stride);       \
  }
Johann's avatar
Johann committed
413 414
#define ST_UB2(...) ST_B2(v16u8, __VA_ARGS__)

clang-format's avatar
clang-format committed
415 416 417 418 419
#define ST_B4(RTYPE, in0, in1, in2, in3, pdst, stride)   \
  {                                                      \
    ST_B2(RTYPE, in0, in1, (pdst), stride);              \
    ST_B2(RTYPE, in2, in3, (pdst) + 2 * stride, stride); \
  }
Johann's avatar
Johann committed
420 421
#define ST_UB4(...) ST_B4(v16u8, __VA_ARGS__)

clang-format's avatar
clang-format committed
422 423 424 425 426
#define ST_B8(RTYPE, in0, in1, in2, in3, in4, in5, in6, in7, pdst, stride) \
  {                                                                        \
    ST_B4(RTYPE, in0, in1, in2, in3, pdst, stride);                        \
    ST_B4(RTYPE, in4, in5, in6, in7, (pdst) + 4 * stride, stride);         \
  }
Johann's avatar
Johann committed
427 428 429 430 431 432 433
#define ST_UB8(...) ST_B8(v16u8, __VA_ARGS__)

/* Description : Store vectors of 8 halfword elements with stride
   Arguments   : Inputs - in0, in1, pdst, stride
   Details     : Store 8 halfword elements from 'in0' to (pdst)
                 Store 8 halfword elements from 'in1' to (pdst + stride)
*/
clang-format's avatar
clang-format committed
434 435 436 437 438
#define ST_H2(RTYPE, in0, in1, pdst, stride) \
  {                                          \
    ST_H(RTYPE, in0, (pdst));                \
    ST_H(RTYPE, in1, (pdst) + stride);       \
  }
Johann's avatar
Johann committed
439 440
#define ST_SH2(...) ST_H2(v8i16, __VA_ARGS__)

clang-format's avatar
clang-format committed
441 442 443 444 445
#define ST_H4(RTYPE, in0, in1, in2, in3, pdst, stride)   \
  {                                                      \
    ST_H2(RTYPE, in0, in1, (pdst), stride);              \
    ST_H2(RTYPE, in2, in3, (pdst) + 2 * stride, stride); \
  }
Johann's avatar
Johann committed
446 447
#define ST_SH4(...) ST_H4(v8i16, __VA_ARGS__)

clang-format's avatar
clang-format committed
448 449 450 451 452
#define ST_H8(RTYPE, in0, in1, in2, in3, in4, in5, in6, in7, pdst, stride) \
  {                                                                        \
    ST_H4(RTYPE, in0, in1, in2, in3, (pdst), stride);                      \
    ST_H4(RTYPE, in4, in5, in6, in7, (pdst) + 4 * stride, stride);         \
  }
Johann's avatar
Johann committed
453 454 455 456 457 458 459
#define ST_SH8(...) ST_H8(v8i16, __VA_ARGS__)

/* Description : Store vectors of word elements with stride
   Arguments   : Inputs - in0, in1, pdst, stride
   Details     : Store 4 word elements from 'in0' to (pdst)
                 Store 4 word elements from 'in1' to (pdst + stride)
*/
clang-format's avatar
clang-format committed
460 461 462 463 464
#define ST_SW2(in0, in1, pdst, stride) \
  {                                    \
    ST_SW(in0, (pdst));                \
    ST_SW(in1, (pdst) + stride);       \
  }
Johann's avatar
Johann committed
465 466 467 468 469 470 471 472 473 474 475 476

/* Description : Store 2x4 byte block to destination memory from input vector
   Arguments   : Inputs - in, stidx, pdst, stride
   Details     : Index 'stidx' halfword element from 'in' vector is copied to
                 the GP register and stored to (pdst)
                 Index 'stidx+1' halfword element from 'in' vector is copied to
                 the GP register and stored to (pdst + stride)
                 Index 'stidx+2' halfword element from 'in' vector is copied to
                 the GP register and stored to (pdst + 2 * stride)
                 Index 'stidx+3' halfword element from 'in' vector is copied to
                 the GP register and stored to (pdst + 3 * stride)
*/
clang-format's avatar
clang-format committed
477 478 479 480 481 482 483 484 485 486 487 488 489 490 491
#define ST2x4_UB(in, stidx, pdst, stride)            \
  {                                                  \
    uint16_t out0_m, out1_m, out2_m, out3_m;         \
    uint8_t *pblk_2x4_m = (uint8_t *)(pdst);         \
                                                     \
    out0_m = __msa_copy_u_h((v8i16)in, (stidx));     \
    out1_m = __msa_copy_u_h((v8i16)in, (stidx + 1)); \
    out2_m = __msa_copy_u_h((v8i16)in, (stidx + 2)); \
    out3_m = __msa_copy_u_h((v8i16)in, (stidx + 3)); \
                                                     \
    SH(out0_m, pblk_2x4_m);                          \
    SH(out1_m, pblk_2x4_m + stride);                 \
    SH(out2_m, pblk_2x4_m + 2 * stride);             \
    SH(out3_m, pblk_2x4_m + 3 * stride);             \
  }
Johann's avatar
Johann committed
492 493 494 495 496 497 498 499

/* Description : Store 4x2 byte block to destination memory from input vector
   Arguments   : Inputs - in, pdst, stride
   Details     : Index 0 word element from 'in' vector is copied to the GP
                 register and stored to (pdst)
                 Index 1 word element from 'in' vector is copied to the GP
                 register and stored to (pdst + stride)
*/
clang-format's avatar
clang-format committed
500 501 502 503 504 505 506 507 508 509 510
#define ST4x2_UB(in, pdst, stride)           \
  {                                          \
    uint32_t out0_m, out1_m;                 \
    uint8_t *pblk_4x2_m = (uint8_t *)(pdst); \
                                             \
    out0_m = __msa_copy_u_w((v4i32)in, 0);   \
    out1_m = __msa_copy_u_w((v4i32)in, 1);   \
                                             \
    SW(out0_m, pblk_4x2_m);                  \
    SW(out1_m, pblk_4x2_m + stride);         \
  }
Johann's avatar
Johann committed
511 512 513 514 515 516 517 518 519 520 521 522

/* Description : Store 4x4 byte block to destination memory from input vector
   Arguments   : Inputs - in0, in1, pdst, stride
   Details     : 'Idx0' word element from input vector 'in0' is copied to the
                 GP register and stored to (pdst)
                 'Idx1' word element from input vector 'in0' is copied to the
                 GP register and stored to (pdst + stride)
                 'Idx2' word element from input vector 'in0' is copied to the
                 GP register and stored to (pdst + 2 * stride)
                 'Idx3' word element from input vector 'in0' is copied to the
                 GP register and stored to (pdst + 3 * stride)
*/
clang-format's avatar
clang-format committed
523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541
#define ST4x4_UB(in0, in1, idx0, idx1, idx2, idx3, pdst, stride) \
  {                                                              \
    uint32_t out0_m, out1_m, out2_m, out3_m;                     \
    uint8_t *pblk_4x4_m = (uint8_t *)(pdst);                     \
                                                                 \
    out0_m = __msa_copy_u_w((v4i32)in0, idx0);                   \
    out1_m = __msa_copy_u_w((v4i32)in0, idx1);                   \
    out2_m = __msa_copy_u_w((v4i32)in1, idx2);                   \
    out3_m = __msa_copy_u_w((v4i32)in1, idx3);                   \
                                                                 \
    SW4(out0_m, out1_m, out2_m, out3_m, pblk_4x4_m, stride);     \
  }
#define ST4x8_UB(in0, in1, pdst, stride)                           \
  {                                                                \
    uint8_t *pblk_4x8 = (uint8_t *)(pdst);                         \
                                                                   \
    ST4x4_UB(in0, in0, 0, 1, 2, 3, pblk_4x8, stride);              \
    ST4x4_UB(in1, in1, 0, 1, 2, 3, pblk_4x8 + 4 * stride, stride); \
  }
Johann's avatar
Johann committed
542 543 544 545 546 547

/* Description : Store 8x1 byte block to destination memory from input vector
   Arguments   : Inputs - in, pdst
   Details     : Index 0 double word element from 'in' vector is copied to the
                 GP register and stored to (pdst)
*/
clang-format's avatar
clang-format committed
548 549 550 551 552 553 554
#define ST8x1_UB(in, pdst)                 \
  {                                        \
    uint64_t out0_m;                       \
                                           \
    out0_m = __msa_copy_u_d((v2i64)in, 0); \
    SD(out0_m, pdst);                      \
  }
Johann's avatar
Johann committed
555 556 557 558 559 560 561 562

/* Description : Store 8x2 byte block to destination memory from input vector
   Arguments   : Inputs - in, pdst, stride
   Details     : Index 0 double word element from 'in' vector is copied to the
                 GP register and stored to (pdst)
                 Index 1 double word element from 'in' vector is copied to the
                 GP register and stored to (pdst + stride)
*/
clang-format's avatar
clang-format committed
563 564 565 566 567 568 569 570 571 572 573
#define ST8x2_UB(in, pdst, stride)           \
  {                                          \
    uint64_t out0_m, out1_m;                 \
    uint8_t *pblk_8x2_m = (uint8_t *)(pdst); \
                                             \
    out0_m = __msa_copy_u_d((v2i64)in, 0);   \
    out1_m = __msa_copy_u_d((v2i64)in, 1);   \
                                             \
    SD(out0_m, pblk_8x2_m);                  \
    SD(out1_m, pblk_8x2_m + stride);         \
  }
Johann's avatar
Johann committed
574 575 576 577 578 579 580 581 582 583 584 585 586

/* Description : Store 8x4 byte block to destination memory from input
                 vectors
   Arguments   : Inputs - in0, in1, pdst, stride
   Details     : Index 0 double word element from 'in0' vector is copied to the
                 GP register and stored to (pdst)
                 Index 1 double word element from 'in0' vector is copied to the
                 GP register and stored to (pdst + stride)
                 Index 0 double word element from 'in1' vector is copied to the
                 GP register and stored to (pdst + 2 * stride)
                 Index 1 double word element from 'in1' vector is copied to the
                 GP register and stored to (pdst + 3 * stride)
*/
clang-format's avatar
clang-format committed
587 588 589 590 591 592 593 594 595 596 597 598
#define ST8x4_UB(in0, in1, pdst, stride)                     \
  {                                                          \
    uint64_t out0_m, out1_m, out2_m, out3_m;                 \
    uint8_t *pblk_8x4_m = (uint8_t *)(pdst);                 \
                                                             \
    out0_m = __msa_copy_u_d((v2i64)in0, 0);                  \
    out1_m = __msa_copy_u_d((v2i64)in0, 1);                  \
    out2_m = __msa_copy_u_d((v2i64)in1, 0);                  \
    out3_m = __msa_copy_u_d((v2i64)in1, 1);                  \
                                                             \
    SD4(out0_m, out1_m, out2_m, out3_m, pblk_8x4_m, stride); \
  }
Johann's avatar
Johann committed
599

600 601 602 603 604 605 606 607
/* Description : average with rounding (in0 + in1 + 1) / 2.
   Arguments   : Inputs  - in0, in1, in2, in3,
                 Outputs - out0, out1
                 Return Type - as per RTYPE
   Details     : Each unsigned byte element from 'in0' vector is added with
                 each unsigned byte element from 'in1' vector. Then the average
                 with rounding is calculated and written to 'out0'
*/
clang-format's avatar
clang-format committed
608 609 610 611 612
#define AVER_UB2(RTYPE, in0, in1, in2, in3, out0, out1)   \
  {                                                       \
    out0 = (RTYPE)__msa_aver_u_b((v16u8)in0, (v16u8)in1); \
    out1 = (RTYPE)__msa_aver_u_b((v16u8)in2, (v16u8)in3); \
  }
613 614
#define AVER_UB2_UB(...) AVER_UB2(v16u8, __VA_ARGS__)

clang-format's avatar
clang-format committed
615 616 617 618 619 620
#define AVER_UB4(RTYPE, in0, in1, in2, in3, in4, in5, in6, in7, out0, out1, \
                 out2, out3)                                                \
  {                                                                         \
    AVER_UB2(RTYPE, in0, in1, in2, in3, out0, out1)                         \
    AVER_UB2(RTYPE, in4, in5, in6, in7, out2, out3)                         \
  }
621 622
#define AVER_UB4_UB(...) AVER_UB4(v16u8, __VA_ARGS__)

Johann's avatar
Johann committed
623 624 625 626 627 628 629
/* Description : Immediate number of elements to slide with zero
   Arguments   : Inputs  - in0, in1, slide_val
                 Outputs - out0, out1
                 Return Type - as per RTYPE
   Details     : Byte elements from 'zero_m' vector are slid into 'in0' by
                 value specified in the 'slide_val'
*/
clang-format's avatar
clang-format committed
630 631 632 633 634 635
#define SLDI_B2_0(RTYPE, in0, in1, out0, out1, slide_val)             \
  {                                                                   \
    v16i8 zero_m = { 0 };                                             \
    out0 = (RTYPE)__msa_sldi_b((v16i8)zero_m, (v16i8)in0, slide_val); \
    out1 = (RTYPE)__msa_sldi_b((v16i8)zero_m, (v16i8)in1, slide_val); \
  }
Johann's avatar
Johann committed
636 637
#define SLDI_B2_0_SW(...) SLDI_B2_0(v4i32, __VA_ARGS__)

clang-format's avatar
clang-format committed
638 639 640 641 642 643
#define SLDI_B4_0(RTYPE, in0, in1, in2, in3, out0, out1, out2, out3, \
                  slide_val)                                         \
  {                                                                  \
    SLDI_B2_0(RTYPE, in0, in1, out0, out1, slide_val);               \
    SLDI_B2_0(RTYPE, in2, in3, out2, out3, slide_val);               \
  }
Johann's avatar
Johann committed
644 645
#define SLDI_B4_0_UB(...) SLDI_B4_0(v16u8, __VA_ARGS__)

646 647 648 649 650 651 652
/* Description : Immediate number of elements to slide
   Arguments   : Inputs  - in0_0, in0_1, in1_0, in1_1, slide_val
                 Outputs - out0, out1
                 Return Type - as per RTYPE
   Details     : Byte elements from 'in0_0' vector are slid into 'in1_0' by
                 value specified in the 'slide_val'
*/
clang-format's avatar
clang-format committed
653 654 655 656 657
#define SLDI_B2(RTYPE, in0_0, in0_1, in1_0, in1_1, out0, out1, slide_val) \
  {                                                                       \
    out0 = (RTYPE)__msa_sldi_b((v16i8)in0_0, (v16i8)in1_0, slide_val);    \
    out1 = (RTYPE)__msa_sldi_b((v16i8)in0_1, (v16i8)in1_1, slide_val);    \
  }
658
#define SLDI_B2_UB(...) SLDI_B2(v16u8, __VA_ARGS__)
Johann's avatar
Johann committed
659 660
#define SLDI_B2_SH(...) SLDI_B2(v8i16, __VA_ARGS__)

clang-format's avatar
clang-format committed
661 662 663 664 665 666
#define SLDI_B3(RTYPE, in0_0, in0_1, in0_2, in1_0, in1_1, in1_2, out0, out1, \
                out2, slide_val)                                             \
  {                                                                          \
    SLDI_B2(RTYPE, in0_0, in0_1, in1_0, in1_1, out0, out1, slide_val)        \
    out2 = (RTYPE)__msa_sldi_b((v16i8)in0_2, (v16i8)in1_2, slide_val);       \
  }
Johann's avatar
Johann committed
667 668 669 670 671 672 673 674 675 676
#define SLDI_B3_SB(...) SLDI_B3(v16i8, __VA_ARGS__)
#define SLDI_B3_UH(...) SLDI_B3(v8u16, __VA_ARGS__)

/* Description : Shuffle byte vector elements as per mask vector
   Arguments   : Inputs  - in0, in1, in2, in3, mask0, mask1
                 Outputs - out0, out1
                 Return Type - as per RTYPE
   Details     : Byte elements from 'in0' & 'in1' are copied selectively to
                 'out0' as per control vector 'mask0'
*/
clang-format's avatar
clang-format committed
677 678 679 680 681
#define VSHF_B2(RTYPE, in0, in1, in2, in3, mask0, mask1, out0, out1)  \
  {                                                                   \
    out0 = (RTYPE)__msa_vshf_b((v16i8)mask0, (v16i8)in1, (v16i8)in0); \
    out1 = (RTYPE)__msa_vshf_b((v16i8)mask1, (v16i8)in3, (v16i8)in2); \
  }
Johann's avatar
Johann committed
682 683 684 685
#define VSHF_B2_UB(...) VSHF_B2(v16u8, __VA_ARGS__)
#define VSHF_B2_SB(...) VSHF_B2(v16i8, __VA_ARGS__)
#define VSHF_B2_UH(...) VSHF_B2(v8u16, __VA_ARGS__)

clang-format's avatar
clang-format committed
686 687 688 689 690 691
#define VSHF_B4(RTYPE, in0, in1, mask0, mask1, mask2, mask3, out0, out1, out2, \
                out3)                                                          \
  {                                                                            \
    VSHF_B2(RTYPE, in0, in1, in0, in1, mask0, mask1, out0, out1);              \
    VSHF_B2(RTYPE, in0, in1, in0, in1, mask2, mask3, out2, out3);              \
  }
Johann's avatar
Johann committed
692 693 694 695 696 697 698 699 700 701 702 703 704
#define VSHF_B4_SB(...) VSHF_B4(v16i8, __VA_ARGS__)
#define VSHF_B4_SH(...) VSHF_B4(v8i16, __VA_ARGS__)

/* Description : Dot product of byte vector elements
   Arguments   : Inputs  - mult0, mult1, cnst0, cnst1
                 Outputs - out0, out1
                 Return Type - as per RTYPE
   Details     : Unsigned byte elements from 'mult0' are multiplied with
                 unsigned byte elements from 'cnst0' producing a result
                 twice the size of input i.e. unsigned halfword.
                 The multiplication result of adjacent odd-even elements
                 are added together and written to the 'out0' vector
*/
clang-format's avatar
clang-format committed
705 706 707 708 709
#define DOTP_UB2(RTYPE, mult0, mult1, cnst0, cnst1, out0, out1) \
  {                                                             \
    out0 = (RTYPE)__msa_dotp_u_h((v16u8)mult0, (v16u8)cnst0);   \
    out1 = (RTYPE)__msa_dotp_u_h((v16u8)mult1, (v16u8)cnst1);   \
  }
Johann's avatar
Johann committed
710 711
#define DOTP_UB2_UH(...) DOTP_UB2(v8u16, __VA_ARGS__)

clang-format's avatar
clang-format committed
712 713 714 715 716 717
#define DOTP_UB4(RTYPE, mult0, mult1, mult2, mult3, cnst0, cnst1, cnst2, \
                 cnst3, out0, out1, out2, out3)                          \
  {                                                                      \
    DOTP_UB2(RTYPE, mult0, mult1, cnst0, cnst1, out0, out1);             \
    DOTP_UB2(RTYPE, mult2, mult3, cnst2, cnst3, out2, out3);             \
  }
Johann's avatar
Johann committed
718 719 720 721 722 723 724 725 726 727 728 729
#define DOTP_UB4_UH(...) DOTP_UB4(v8u16, __VA_ARGS__)

/* Description : Dot product of byte vector elements
   Arguments   : Inputs  - mult0, mult1, cnst0, cnst1
                 Outputs - out0, out1
                 Return Type - as per RTYPE
   Details     : Signed byte elements from 'mult0' are multiplied with
                 signed byte elements from 'cnst0' producing a result
                 twice the size of input i.e. signed halfword.
                 The multiplication result of adjacent odd-even elements
                 are added together and written to the 'out0' vector
*/
clang-format's avatar
clang-format committed
730 731 732 733 734
#define DOTP_SB2(RTYPE, mult0, mult1, cnst0, cnst1, out0, out1) \
  {                                                             \
    out0 = (RTYPE)__msa_dotp_s_h((v16i8)mult0, (v16i8)cnst0);   \
    out1 = (RTYPE)__msa_dotp_s_h((v16i8)mult1, (v16i8)cnst1);   \
  }
Johann's avatar
Johann committed
735 736
#define DOTP_SB2_SH(...) DOTP_SB2(v8i16, __VA_ARGS__)

clang-format's avatar
clang-format committed
737 738 739 740 741 742
#define DOTP_SB4(RTYPE, mult0, mult1, mult2, mult3, cnst0, cnst1, cnst2, \
                 cnst3, out0, out1, out2, out3)                          \
  {                                                                      \
    DOTP_SB2(RTYPE, mult0, mult1, cnst0, cnst1, out0, out1);             \
    DOTP_SB2(RTYPE, mult2, mult3, cnst2, cnst3, out2, out3);             \
  }
Johann's avatar
Johann committed
743 744 745 746 747 748 749 750 751 752 753 754
#define DOTP_SB4_SH(...) DOTP_SB4(v8i16, __VA_ARGS__)

/* Description : Dot product of halfword vector elements
   Arguments   : Inputs  - mult0, mult1, cnst0, cnst1
                 Outputs - out0, out1
                 Return Type - as per RTYPE
   Details     : Signed halfword elements from 'mult0' are multiplied with
                 signed halfword elements from 'cnst0' producing a result
                 twice the size of input i.e. signed word.
                 The multiplication result of adjacent odd-even elements
                 are added together and written to the 'out0' vector
*/
clang-format's avatar
clang-format committed
755 756 757 758 759
#define DOTP_SH2(RTYPE, mult0, mult1, cnst0, cnst1, out0, out1) \
  {                                                             \
    out0 = (RTYPE)__msa_dotp_s_w((v8i16)mult0, (v8i16)cnst0);   \
    out1 = (RTYPE)__msa_dotp_s_w((v8i16)mult1, (v8i16)cnst1);   \
  }
Johann's avatar
Johann committed
760 761
#define DOTP_SH2_SW(...) DOTP_SH2(v4i32, __VA_ARGS__)

clang-format's avatar
clang-format committed
762 763 764 765 766 767
#define DOTP_SH4(RTYPE, mult0, mult1, mult2, mult3, cnst0, cnst1, cnst2, \
                 cnst3, out0, out1, out2, out3)                          \
  {                                                                      \
    DOTP_SH2(RTYPE, mult0, mult1, cnst0, cnst1, out0, out1);             \
    DOTP_SH2(RTYPE, mult2, mult3, cnst2, cnst3, out2, out3);             \
  }
Johann's avatar
Johann committed
768 769 770 771 772 773 774 775 776 777 778 779
#define DOTP_SH4_SW(...) DOTP_SH4(v4i32, __VA_ARGS__)

/* Description : Dot product of word vector elements
   Arguments   : Inputs  - mult0, mult1, cnst0, cnst1
                 Outputs - out0, out1
                 Return Type - as per RTYPE
   Details     : Signed word elements from 'mult0' are multiplied with
                 signed word elements from 'cnst0' producing a result
                 twice the size of input i.e. signed double word.
                 The multiplication result of adjacent odd-even elements
                 are added together and written to the 'out0' vector
*/
clang-format's avatar
clang-format committed
780 781 782 783 784
#define DOTP_SW2(RTYPE, mult0, mult1, cnst0, cnst1, out0, out1) \
  {                                                             \
    out0 = (RTYPE)__msa_dotp_s_d((v4i32)mult0, (v4i32)cnst0);   \
    out1 = (RTYPE)__msa_dotp_s_d((v4i32)mult1, (v4i32)cnst1);   \
  }
Johann's avatar
Johann committed
785 786 787 788 789 790 791 792 793 794 795 796
#define DOTP_SW2_SD(...) DOTP_SW2(v2i64, __VA_ARGS__)

/* Description : Dot product & addition of byte vector elements
   Arguments   : Inputs  - mult0, mult1, cnst0, cnst1
                 Outputs - out0, out1
                 Return Type - as per RTYPE
   Details     : Signed byte elements from 'mult0' are multiplied with
                 signed byte elements from 'cnst0' producing a result
                 twice the size of input i.e. signed halfword.
                 The multiplication result of adjacent odd-even elements
                 are added to the 'out0' vector
*/
clang-format's avatar
clang-format committed
797 798 799 800 801
#define DPADD_SB2(RTYPE, mult0, mult1, cnst0, cnst1, out0, out1)            \
  {                                                                         \
    out0 = (RTYPE)__msa_dpadd_s_h((v8i16)out0, (v16i8)mult0, (v16i8)cnst0); \
    out1 = (RTYPE)__msa_dpadd_s_h((v8i16)out1, (v16i8)mult1, (v16i8)cnst1); \
  }
Johann's avatar
Johann committed
802 803
#define DPADD_SB2_SH(...) DPADD_SB2(v8i16, __VA_ARGS__)

clang-format's avatar
clang-format committed
804 805 806 807 808 809
#define DPADD_SB4(RTYPE, mult0, mult1, mult2, mult3, cnst0, cnst1, cnst2, \
                  cnst3, out0, out1, out2, out3)                          \
  {                                                                       \
    DPADD_SB2(RTYPE, mult0, mult1, cnst0, cnst1, out0, out1);             \
    DPADD_SB2(RTYPE, mult2, mult3, cnst2, cnst3, out2, out3);             \
  }
Johann's avatar
Johann committed
810
#define DPADD_SB4_SH(...) DPADD_SB4(v8i16, __VA_ARGS__)
811

812 813 814 815 816 817 818 819 820 821
/* Description : Dot product & addition of halfword vector elements
   Arguments   : Inputs  - mult0, mult1, cnst0, cnst1
                 Outputs - out0, out1
                 Return Type - as per RTYPE
   Details     : Signed halfword elements from 'mult0' are multiplied with
                 signed halfword elements from 'cnst0' producing a result
                 twice the size of input i.e. signed word.
                 The multiplication result of adjacent odd-even elements
                 are added to the 'out0' vector
*/
clang-format's avatar
clang-format committed
822 823 824 825 826
#define DPADD_SH2(RTYPE, mult0, mult1, cnst0, cnst1, out0, out1)            \
  {                                                                         \
    out0 = (RTYPE)__msa_dpadd_s_w((v4i32)out0, (v8i16)mult0, (v8i16)cnst0); \
    out1 = (RTYPE)__msa_dpadd_s_w((v4i32)out1, (v8i16)mult1, (v8i16)cnst1); \
  }
827 828 829 830 831 832 833
#define DPADD_SH2_SW(...) DPADD_SH2(v4i32, __VA_ARGS__)

/* Description : Dot product & addition of double word vector elements
   Arguments   : Inputs  - mult0, mult1
                 Outputs - out0, out1
                 Return Type - as per RTYPE
   Details     : Each signed word element from 'mult0' is multiplied with itself
Johann's avatar
Johann committed
834
                 producing an intermediate result twice the size of input
835 836 837 838
                 i.e. signed double word
                 The multiplication result of adjacent odd-even elements
                 are added to the 'out0' vector
*/
clang-format's avatar
clang-format committed
839 840 841 842 843
#define DPADD_SD2(RTYPE, mult0, mult1, out0, out1)                          \
  {                                                                         \
    out0 = (RTYPE)__msa_dpadd_s_d((v2i64)out0, (v4i32)mult0, (v4i32)mult0); \
    out1 = (RTYPE)__msa_dpadd_s_d((v2i64)out1, (v4i32)mult1, (v4i32)mult1); \
  }
844 845
#define DPADD_SD2_SD(...) DPADD_SD2(v2i64, __VA_ARGS__)

Johann's avatar
Johann committed
846 847 848 849 850 851 852 853
/* Description : Minimum values between unsigned elements of
                 either vector are copied to the output vector
   Arguments   : Inputs  - in0, in1, min_vec
                 Outputs - in place operation
                 Return Type - as per RTYPE
   Details     : Minimum of unsigned halfword element values from 'in0' and
                 'min_vec' are written to output vector 'in0'
*/
clang-format's avatar
clang-format committed
854 855 856 857 858
#define MIN_UH2(RTYPE, in0, in1, min_vec)            \
  {                                                  \
    in0 = (RTYPE)__msa_min_u_h((v8u16)in0, min_vec); \
    in1 = (RTYPE)__msa_min_u_h((v8u16)in1, min_vec); \
  }
Johann's avatar
Johann committed
859 860
#define MIN_UH2_UH(...) MIN_UH2(v8u16, __VA_ARGS__)

clang-format's avatar
clang-format committed
861 862 863 864 865
#define MIN_UH4(RTYPE, in0, in1, in2, in3, min_vec) \
  {                                                 \
    MIN_UH2(RTYPE, in0, in1, min_vec);              \
    MIN_UH2(RTYPE, in2, in3, min_vec);              \
  }
Johann's avatar
Johann committed
866 867 868 869 870 871 872 873
#define MIN_UH4_UH(...) MIN_UH4(v8u16, __VA_ARGS__)

/* Description : Clips all signed halfword elements of input vector
                 between 0 & 255
   Arguments   : Input  - in
                 Output - out_m
                 Return Type - signed halfword
*/
clang-format's avatar
clang-format committed
874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892
#define CLIP_SH_0_255(in)                              \
  ({                                                   \
    v8i16 max_m = __msa_ldi_h(255);                    \
    v8i16 out_m;                                       \
                                                       \
    out_m = __msa_maxi_s_h((v8i16)in, 0);              \
    out_m = __msa_min_s_h((v8i16)max_m, (v8i16)out_m); \
    out_m;                                             \
  })
#define CLIP_SH2_0_255(in0, in1) \
  {                              \
    in0 = CLIP_SH_0_255(in0);    \
    in1 = CLIP_SH_0_255(in1);    \
  }
#define CLIP_SH4_0_255(in0, in1, in2, in3) \
  {                                        \
    CLIP_SH2_0_255(in0, in1);              \
    CLIP_SH2_0_255(in2, in3);              \
  }
Johann's avatar
Johann committed
893

894 895 896 897 898 899 900
/* Description : Horizontal addition of 4 signed word elements of input vector
   Arguments   : Input  - in       (signed word vector)
                 Output - sum_m    (i32 sum)
                 Return Type - signed word (GP)
   Details     : 4 signed word elements of 'in' vector are added together and
                 the resulting integer sum is returned
*/
clang-format's avatar
clang-format committed
901 902 903 904 905 906 907 908 909 910 911
#define HADD_SW_S32(in)                            \
  ({                                               \
    v2i64 res0_m, res1_m;                          \
    int32_t sum_m;                                 \
                                                   \
    res0_m = __msa_hadd_s_d((v4i32)in, (v4i32)in); \
    res1_m = __msa_splati_d(res0_m, 1);            \
    res0_m = res0_m + res1_m;                      \
    sum_m = __msa_copy_s_w((v4i32)res0_m, 0);      \
    sum_m;                                         \
  })
912

913 914 915 916 917 918 919
/* Description : Horizontal addition of 8 unsigned halfword elements
   Arguments   : Inputs  - in       (unsigned halfword vector)
                 Outputs - sum_m    (u32 sum)
                 Return Type - unsigned word
   Details     : 8 unsigned halfword elements of input vector are added
                 together and the resulting integer sum is returned
*/
clang-format's avatar
clang-format committed
920 921 922 923 924 925 926 927 928 929 930 931 932
#define HADD_UH_U32(in)                               \
  ({                                                  \
    v4u32 res_m;                                      \
    v2u64 res0_m, res1_m;                             \
    uint32_t sum_m;                                   \
                                                      \
    res_m = __msa_hadd_u_w((v8u16)in, (v8u16)in);     \
    res0_m = __msa_hadd_u_d(res_m, res_m);            \
    res1_m = (v2u64)__msa_splati_d((v2i64)res0_m, 1); \
    res0_m = res0_m + res1_m;                         \
    sum_m = __msa_copy_u_w((v4i32)res0_m, 0);         \
    sum_m;                                            \
  })
933

Johann's avatar
Johann committed
934
/* Description : Horizontal addition of unsigned byte vector elements
935 936 937
   Arguments   : Inputs  - in0, in1
                 Outputs - out0, out1
                 Return Type - as per RTYPE
Johann's avatar
Johann committed
938 939 940 941
   Details     : Each unsigned odd byte element from 'in0' is added to
                 even unsigned byte element from 'in0' (pairwise) and the
                 halfword result is written to 'out0'
*/
clang-format's avatar
clang-format committed
942 943 944 945 946
#define HADD_UB2(RTYPE, in0, in1, out0, out1)             \
  {                                                       \
    out0 = (RTYPE)__msa_hadd_u_h((v16u8)in0, (v16u8)in0); \
    out1 = (RTYPE)__msa_hadd_u_h((v16u8)in1, (v16u8)in1); \
  }
Johann's avatar
Johann committed
947 948
#define HADD_UB2_UH(...) HADD_UB2(v8u16, __VA_ARGS__)

clang-format's avatar
clang-format committed
949 950 951 952 953
#define HADD_UB4(RTYPE, in0, in1, in2, in3, out0, out1, out2, out3) \
  {                                                                 \
    HADD_UB2(RTYPE, in0, in1, out0, out1);                          \
    HADD_UB2(RTYPE, in2, in3, out2, out3);                          \
  }
Johann's avatar
Johann committed
954 955 956 957 958 959 960
#define HADD_UB4_UH(...) HADD_UB4(v8u16, __VA_ARGS__)

/* Description : Horizontal subtraction of unsigned byte vector elements
   Arguments   : Inputs  - in0, in1
                 Outputs - out0, out1
                 Return Type - as per RTYPE
   Details     : Each unsigned odd byte element from 'in0' is subtracted from
961 962 963
                 even unsigned byte element from 'in0' (pairwise) and the
                 halfword result is written to 'out0'
*/
clang-format's avatar
clang-format committed
964 965 966 967 968
#define HSUB_UB2(RTYPE, in0, in1, out0, out1)             \
  {                                                       \
    out0 = (RTYPE)__msa_hsub_u_h((v16u8)in0, (v16u8)in0); \
    out1 = (RTYPE)__msa_hsub_u_h((v16u8)in1, (v16u8)in1); \
  }
969 970
#define HSUB_UB2_SH(...) HSUB_UB2(v8i16, __VA_ARGS__)

971 972 973 974 975 976 977 978
/* Description : SAD (Sum of Absolute Difference)
   Arguments   : Inputs  - in0, in1, ref0, ref1
                 Outputs - sad_m                 (halfword vector)
                 Return Type - unsigned halfword
   Details     : Absolute difference of all the byte elements from 'in0' with
                 'ref0' is calculated and preserved in 'diff0'. Then even-odd
                 pairs are added together to generate 8 halfword results.
*/
clang-format's avatar
clang-format committed
979 980 981 982 983 984 985 986 987 988 989 990 991
#define SAD_UB2_UH(in0, in1, ref0, ref1)                     \
  ({                                                         \
    v16u8 diff0_m, diff1_m;                                  \
    v8u16 sad_m = { 0 };                                     \
                                                             \
    diff0_m = __msa_asub_u_b((v16u8)in0, (v16u8)ref0);       \
    diff1_m = __msa_asub_u_b((v16u8)in1, (v16u8)ref1);       \
                                                             \
    sad_m += __msa_hadd_u_h((v16u8)diff0_m, (v16u8)diff0_m); \
    sad_m += __msa_hadd_u_h((v16u8)diff1_m, (v16u8)diff1_m); \
                                                             \
    sad_m;                                                   \
  })
992

Johann's avatar
Johann committed
993 994 995 996 997 998 999 1000
/* Description : Horizontal subtraction of signed halfword vector elements
   Arguments   : Inputs  - in0, in1
                 Outputs - out0, out1
                 Return Type - as per RTYPE
   Details     : Each signed odd halfword element from 'in0' is subtracted from
                 even signed halfword element from 'in0' (pairwise) and the
                 word result is written to 'out0'
*/
clang-format's avatar
clang-format committed
1001 1002 1003 1004 1005
#define HSUB_UH2(RTYPE, in0, in1, out0, out1)             \
  {                                                       \
    out0 = (RTYPE)__msa_hsub_s_w((v8i16)in0, (v8i16)in0); \
    out1 = (RTYPE)__msa_hsub_s_w((v8i16)in1, (v8i16)in1); \
  }
Johann's avatar
Johann committed
1006 1007
#define HSUB_UH2_SW(...) HSUB_UH2(v4i32, __VA_ARGS__)

1008 1009 1010 1011 1012 1013
/* Description : Set element n input vector to GPR value
   Arguments   : Inputs - in0, in1, in2, in3
                 Output - out
                 Return Type - as per RTYPE
   Details     : Set element 0 in vector 'out' to value specified in 'in0'
*/
clang-format's avatar
clang-format committed
1014 1015 1016 1017 1018
#define INSERT_W2(RTYPE, in0, in1, out)              \
  {                                                  \
    out = (RTYPE)__msa_insert_w((v4i32)out, 0, in0); \
    out = (RTYPE)__msa_insert_w((v4i32)out, 1, in1); \
  }
Johann's avatar
Johann committed
1019 1020
#define INSERT_W2_SB(...) INSERT_W2(v16i8, __VA_ARGS__)

clang-format's avatar
clang-format committed
1021 1022 1023 1024 1025 1026 1027
#define INSERT_W4(RTYPE, in0, in1, in2, in3, out)    \
  {                                                  \
    out = (RTYPE)__msa_insert_w((v4i32)out, 0, in0); \
    out = (RTYPE)__msa_insert_w((v4i32)out, 1, in1); \
    out = (RTYPE)__msa_insert_w((v4i32)out, 2, in2); \
    out = (RTYPE)__msa_insert_w((v4i32)out, 3, in3); \
  }
1028 1029 1030
#define INSERT_W4_UB(...) INSERT_W4(v16u8, __VA_ARGS__)
#define INSERT_W4_SB(...) INSERT_W4(v16i8, __VA_ARGS__)

clang-format's avatar
clang-format committed
1031 1032 1033 1034 1035
#define INSERT_D2(RTYPE, in0, in1, out)              \
  {                                                  \
    out = (RTYPE)__msa_insert_d((v2i64)out, 0, in0); \
    out = (RTYPE)__msa_insert_d((v2i64)out, 1, in1); \
  }
1036 1037 1038
#define INSERT_D2_UB(...) INSERT_D2(v16u8, __VA_ARGS__)
#define INSERT_D2_SB(...) INSERT_D2(v16i8, __VA_ARGS__)

Johann's avatar
Johann committed
1039 1040 1041 1042 1043 1044 1045
/* Description : Interleave even byte elements from vectors
   Arguments   : Inputs  - in0, in1, in2, in3
                 Outputs - out0, out1
                 Return Type - as per RTYPE
   Details     : Even byte elements of 'in0' and 'in1' are interleaved
                 and written to 'out0'
*/
clang-format's avatar
clang-format committed
1046 1047 1048 1049 1050
#define ILVEV_B2(RTYPE, in0, in1, in2, in3, out0, out1)  \
  {                                                      \
    out0 = (RTYPE)__msa_ilvev_b((v16i8)in1, (v16i8)in0); \
    out1 = (RTYPE)__msa_ilvev_b((v16i8)in3, (v16i8)in2); \
  }
Johann's avatar
Johann committed
1051 1052 1053 1054 1055 1056 1057 1058 1059 1060
#define ILVEV_B2_UB(...) ILVEV_B2(v16u8, __VA_ARGS__)
#define ILVEV_B2_SH(...) ILVEV_B2(v8i16, __VA_ARGS__)

/* Description : Interleave even halfword elements from vectors
   Arguments   : Inputs  - in0, in1, in2, in3
                 Outputs - out0, out1
                 Return Type - as per RTYPE
   Details     : Even halfword elements of 'in0' and 'in1' are interleaved
                 and written to 'out0'
*/
clang-format's avatar
clang-format committed
1061 1062 1063 1064 1065
#define ILVEV_H2(RTYPE, in0, in1, in2, in3, out0, out1)  \
  {                                                      \
    out0 = (RTYPE)__msa_ilvev_h((v8i16)in1, (v8i16)in0); \
    out1 = (RTYPE)__msa_ilvev_h((v8i16)in3, (v8i16)in2); \
  }
Johann's avatar
Johann committed
1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076
#define ILVEV_H2_UB(...) ILVEV_H2(v16u8, __VA_ARGS__)
#define ILVEV_H2_SH(...) ILVEV_H2(v8i16, __VA_ARGS__)
#define ILVEV_H2_SW(...) ILVEV_H2(v4i32, __VA_ARGS__)

/* Description : Interleave even word elements from vectors
   Arguments   : Inputs  - in0, in1, in2, in3
                 Outputs - out0, out1
                 Return Type - as per RTYPE
   Details     : Even word elements of 'in0' and 'in1' are interleaved
                 and written to 'out0'
*/
clang-format's avatar
clang-format committed
1077 1078 1079 1080 1081
#define ILVEV_W2(RTYPE, in0, in1, in2, in3, out0, out1)  \
  {                                                      \
    out0 = (RTYPE)__msa_ilvev_w((v4i32)in1, (v4i32)in0); \
    out1 = (RTYPE)__msa_ilvev_w((v4i32)in3, (v4i32)in2); \
  }
Johann's avatar
Johann committed
1082 1083 1084 1085 1086 1087 1088 1089 1090
#define ILVEV_W2_SB(...) ILVEV_W2(v16i8, __VA_ARGS__)

/* Description : Interleave even double word elements from vectors
   Arguments   : Inputs  - in0, in1, in2, in3
                 Outputs - out0, out1
                 Return Type - as per RTYPE
   Details     : Even double word elements of 'in0' and 'in1' are interleaved
                 and written to 'out0'
*/
clang-format's avatar
clang-format committed
1091 1092 1093 1094 1095
#define ILVEV_D2(RTYPE, in0, in1, in2, in3, out0, out1)  \
  {                                                      \
    out0 = (RTYPE)__msa_ilvev_d((v2i64)in1, (v2i64)in0); \
    out1 = (RTYPE)__msa_ilvev_d((v2i64)in3, (v2i64)in2); \
  }
Johann's avatar
Johann committed
1096 1097 1098 1099 1100 1101 1102 1103 1104
#define ILVEV_D2_UB(...) ILVEV_D2(v16u8, __VA_ARGS__)

/* Description : Interleave left half of byte elements from vectors
   Arguments   : Inputs  - in0, in1, in2, in3
                 Outputs - out0, out1
                 Return Type - as per RTYPE
   Details     : Left half of byte elements of 'in0' and 'in1' are interleaved
                 and written to 'out0'.
*/
clang-format's avatar
clang-format committed
1105 1106 1107 1108 1109
#define ILVL_B2(RTYPE, in0, in1, in2, in3, out0, out1)  \
  {                                                     \
    out0 = (RTYPE)__msa_ilvl_b((v16i8)in0, (v16i8)in1); \
    out1 = (RTYPE)__msa_ilvl_b((v16i8)in2, (v16i8)in3); \
  }
Johann's avatar
Johann committed
1110 1111 1112 1113 1114
#define ILVL_B2_UB(...) ILVL_B2(v16u8, __VA_ARGS__)
#define ILVL_B2_SB(...) ILVL_B2(v16i8, __VA_ARGS__)
#define ILVL_B2_UH(...) ILVL_B2(v8u16, __VA_ARGS__)
#define ILVL_B2_SH(...) ILVL_B2(v8i16, __VA_ARGS__)

clang-format's avatar
clang-format committed
1115 1116 1117 1118 1119 1120
#define ILVL_B4(RTYPE, in0, in1, in2, in3, in4, in5, in6, in7, out0, out1, \
                out2, out3)                                                \
  {                                                                        \
    ILVL_B2(RTYPE, in0, in1, in2, in3, out0, out1);                        \
    ILVL_B2(RTYPE, in4, in5, in6, in7, out2, out3);                        \
  }
Johann's avatar
Johann committed
1121 1122 1123 1124 1125 1126 1127 1128 1129 1130
#define ILVL_B4_SB(...) ILVL_B4(v16i8, __VA_ARGS__)
#define ILVL_B4_UH(...) ILVL_B4(v8u16, __VA_ARGS__)

/* Description : Interleave left half of halfword elements from vectors
   Arguments   : Inputs  - in0, in1, in2, in3
                 Outputs - out0, out1
                 Return Type - as per RTYPE
   Details     : Left half of halfword elements of 'in0' and 'in1' are
                 interleaved and written to 'out0'.
*/
clang-format's avatar
clang-format committed
1131 1132 1133 1134 1135
#define ILVL_H2(RTYPE, in0, in1, in2, in3, out0, out1)  \
  {                                                     \
    out0 = (RTYPE)__msa_ilvl_h((v8i16)in0, (v8i16)in1); \
    out1 = (RTYPE)__msa_ilvl_h((v8i16)in2, (v8i16)in3); \
  }
Johann's avatar
Johann committed
1136 1137 1138 1139 1140 1141 1142 1143 1144
#define ILVL_H2_SH(...) ILVL_H2(v8i16, __VA_ARGS__)

/* Description : Interleave left half of word elements from vectors
   Arguments   : Inputs  - in0, in1, in2, in3
                 Outputs - out0, out1
                 Return Type - as per RTYPE
   Details     : Left half of word elements of 'in0' and 'in1' are interleaved
                 and written to 'out0'.
*/
clang-format's avatar
clang-format committed
1145 1146 1147 1148 1149
#define ILVL_W2(RTYPE, in0, in1, in2, in3, out0, out1)  \
  {                                                     \
    out0 = (RTYPE)__msa_ilvl_w((v4i32)in0, (v4i32)in1); \
    out1 = (RTYPE)__msa_ilvl_w((v4i32)in2, (v4i32)in3); \
  }
Johann's avatar
Johann committed
1150 1151 1152 1153 1154 1155 1156 1157 1158 1159
#define ILVL_W2_UB(...) ILVL_W2(v16u8, __VA_ARGS__)
#define ILVL_W2_SH(...) ILVL_W2(v8i16, __VA_ARGS__)

/* Description : Interleave right half of byte elements from vectors
   Arguments   : Inputs  - in0, in1, in2, in3
                 Outputs - out0, out1
                 Return Type - as per RTYPE
   Details     : Right half of byte elements of 'in0' and 'in1' are interleaved
                 and written to out0.
*/
clang-format's avatar
clang-format committed
1160 1161 1162 1163 1164
#define ILVR_B2(RTYPE, in0, in1, in2, in3, out0, out1)  \
  {                                                     \
    out0 = (RTYPE)__msa_ilvr_b((v16i8)in0, (v16i8)in1); \
    out1 = (RTYPE)__msa_ilvr_b((v16i8)in2, (v16i8)in3); \
  }
Johann's avatar
Johann committed
1165 1166 1167 1168 1169
#define ILVR_B2_UB(...) ILVR_B2(v16u8, __VA_ARGS__)
#define ILVR_B2_SB(...) ILVR_B2(v16i8, __VA_ARGS__)
#define ILVR_B2_UH(...) ILVR_B2(v8u16, __VA_ARGS__)
#define ILVR_B2_SH(...) ILVR_B2(v8i16, __VA_ARGS__)

clang-format's avatar
clang-format committed
1170 1171 1172 1173 1174 1175
#define ILVR_B4(RTYPE, in0, in1, in2, in3, in4, in5, in6, in7, out0, out1, \
                out2, out3)                                                \
  {                                                                        \
    ILVR_B2(RTYPE, in0, in1, in2, in3, out0, out1);                        \
    ILVR_B2(RTYPE, in4, in5, in6, in7, out2, out3);                        \
  }
Johann's avatar
Johann committed
1176 1177 1178 1179 1180
#define ILVR_B4_UB(...) ILVR_B4(v16u8, __VA_ARGS__)
#define ILVR_B4_SB(...) ILVR_B4(v16i8, __VA_ARGS__)
#define ILVR_B4_UH(...) ILVR_B4(v8u16, __VA_ARGS__)
#define ILVR_B4_SH(...) ILVR_B4(v8i16, __VA_ARGS__)

clang-format's avatar
clang-format committed
1181 1182 1183 1184 1185 1186 1187 1188 1189
#define ILVR_B8(RTYPE, in0, in1, in2, in3, in4, in5, in6, in7, in8, in9, in10, \
                in11, in12, in13, in14, in15, out0, out1, out2, out3, out4,    \
                out5, out6, out7)                                              \
  {                                                                            \
    ILVR_B4(RTYPE, in0, in1, in2, in3, in4, in5, in6, in7, out0, out1, out2,   \
            out3);                                                             \
    ILVR_B4(RTYPE, in8, in9, in10, in11, in12, in13, in14, in15, out4, out5,   \
            out6, out7);                                                       \
  }
Johann's avatar
Johann committed
1190 1191 1192 1193 1194 1195 1196 1197 1198
#define ILVR_B8_UH(...) ILVR_B8(v8u16, __VA_ARGS__)

/* Description : Interleave right half of halfword elements from vectors
   Arguments   : Inputs  - in0, in1, in2, in3
                 Outputs - out0, out1
                 Return Type - as per RTYPE
   Details     : Right half of halfword elements of 'in0' and 'in1' are
                 interleaved and written to 'out0'.
*/
clang-format's avatar
clang-format committed
1199 1200 1201 1202 1203
#define ILVR_H2(RTYPE, in0, in1, in2, in3, out0, out1)  \
  {                                                     \
    out0 = (RTYPE)__msa_ilvr_h((v8i16)in0, (v8i16)in1); \
    out1 = (RTYPE)__msa_ilvr_h((v8i16)in2, (v8i16)in3); \
  }
Johann's avatar
Johann committed
1204 1205
#define ILVR_H2_SH(...) ILVR_H2(v8i16, __VA_ARGS__)

clang-format's avatar
clang-format committed
1206 1207 1208 1209 1210 1211
#define ILVR_H4(RTYPE, in0, in1, in2, in3, in4, in5, in6, in7, out0, out1, \
                out2, out3)                                                \
  {                                                                        \
    ILVR_H2(RTYPE, in0, in1, in2, in3, out0, out1);                        \
    ILVR_H2(RTYPE, in4, in5, in6, in7, out2, out3);                        \
  }
Johann's avatar
Johann committed
1212 1213
#define ILVR_H4_SH(...) ILVR_H4(v8i16, __VA_ARGS__)

clang-format's avatar
clang-format committed
1214 1215 1216 1217 1218
#define ILVR_W2(RTYPE, in0, in1, in2, in3, out0, out1)  \
  {                                                     \
    out0 = (RTYPE)__msa_ilvr_w((v4i32)in0, (v4i32)in1); \
    out1 = (RTYPE)__msa_ilvr_w((v4i32)in2, (v4i32)in3); \
  }
Johann's avatar
Johann committed
1219 1220 1221
#define ILVR_W2_UB(...) ILVR_W2(v16u8, __VA_ARGS__)
#define ILVR_W2_SH(...) ILVR_W2(v8i16, __VA_ARGS__)

clang-format's avatar
clang-format committed
1222 1223 1224 1225 1226 1227
#define ILVR_W4(RTYPE, in0, in1, in2, in3, in4, in5, in6, in7, out0, out1, \
                out2, out3)                                                \
  {                                                                        \
    ILVR_W2(RTYPE, in0, in1, in2, in3, out0, out1);                        \
    ILVR_W2(RTYPE, in4, in5, in6, in7, out2, out3);                        \
  }
Johann's avatar
Johann committed
1228 1229 1230 1231 1232 1233 1234 1235 1236
#define ILVR_W4_UB(...) ILVR_W4(v16u8, __VA_ARGS__)

/* Description : Interleave right half of double word elements from vectors
   Arguments   : Inputs  - in0, in1, in2, in3
                 Outputs - out0, out1
                 Return Type - as per RTYPE
   Details     : Right half of double word elements of 'in0' and 'in1' are
                 interleaved and written to 'out0'.
*/
clang-format's avatar
clang-format committed
1237 1238 1239 1240 1241
#define ILVR_D2(RTYPE, in0, in1, in2, in3, out0, out1)      \
  {                                                         \
    out0 = (RTYPE)__msa_ilvr_d((v2i64)(in0), (v2i64)(in1)); \
    out1 = (RTYPE)__msa_ilvr_d((v2i64)(in2), (v2i64)(in3)); \
  }
Johann's avatar
Johann committed
1242 1243 1244 1245
#define ILVR_D2_UB(...) ILVR_D2(v16u8, __VA_ARGS__)
#define ILVR_D2_SB(...) ILVR_D2(v16i8, __VA_ARGS__)
#define ILVR_D2_SH(...) ILVR_D2(v8i16, __VA_ARGS__)

clang-format's avatar
clang-format committed
1246 1247 1248 1249 1250
#define ILVR_D3(RTYPE, in0, in1, in2, in3, in4, in5, out0, out1, out2) \
  {                                                                    \
    ILVR_D2(RTYPE, in0, in1, in2, in3, out0, out1);                    \
    out2 = (RTYPE)__msa_ilvr_d((v2i64)(in4), (v2i64)(in5));            \
  }
Johann's avatar
Johann committed
1251 1252
#define ILVR_D3_SB(...) ILVR_D3(v16i8, __VA_ARGS__)

clang-format's avatar
clang-format committed
1253 1254 1255 1256 1257 1258
#define ILVR_D4(RTYPE, in0, in1, in2, in3, in4, in5, in6, in7, out0, out1, \
                out2, out3)                                                \
  {                                                                        \
    ILVR_D2(RTYPE, in0, in1, in2, in3, out0, out1);                        \
    ILVR_D2(RTYPE, in4, in5, in6, in7, out2, out3);                        \
  }
Johann's avatar
Johann committed
1259 1260 1261
#define ILVR_D4_SB(...) ILVR_D4(v16i8, __VA_ARGS__)
#define ILVR_D4_UB(...) ILVR_D4(v16u8, __VA_ARGS__)

1262 1263 1264 1265 1266 1267 1268
/* Description : Interleave both left and right half of input vectors
   Arguments   : Inputs  - in0, in1
                 Outputs - out0, out1
                 Return Type - as per RTYPE
   Details     : Right half of byte elements from 'in0' and 'in1' are
                 interleaved and written to 'out0'
*/
clang-format's avatar
clang-format committed
1269 1270 1271 1272 1273
#define ILVRL_B2(RTYPE, in0, in1, out0, out1)           \
  {                                                     \
    out0 = (RTYPE)__msa_ilvr_b((v16i8)in0, (v16i8)in1); \
    out1 = (RTYPE)__msa_ilvl_b((v16i8)in0, (v16i8)in1); \
  }
1274
#define ILVRL_B2_UB(...) ILVRL_B2(v16u8, __VA_ARGS__)
Johann's avatar
Johann committed
1275 1276 1277
#define ILVRL_B2_SB(...) ILVRL_B2(v16i8, __VA_ARGS__)
#define ILVRL_B2_UH(...) ILVRL_B2(v8u16, __VA_ARGS__)
#define ILVRL_B2_SH(...) ILVRL_B2(v8i16, __VA_ARGS__)
1278

clang-format's avatar
clang-format committed
1279 1280 1281 1282 1283
#define ILVRL_H2(RTYPE, in0, in1, out0, out1)           \
  {                                                     \
    out0 = (RTYPE)__msa_ilvr_h((v8i16)in0, (v8i16)in1); \
    out1 = (RTYPE)__msa_ilvl_h((v8i16)in0, (v8i16)in1); \
  }
Johann's avatar
Johann committed
1284
#define ILVRL_H2_SH(...) ILVRL_H2(v8i16, __VA_ARGS__)
1285 1286
#define ILVRL_H2_SW(...) ILVRL_H2(v4i32, __VA_ARGS__)

clang-format's avatar
clang-format committed
1287 1288 1289 1290 1291
#define ILVRL_W2(RTYPE, in0, in1, out0, out1)           \
  {                                                     \
    out0 = (RTYPE)__msa_ilvr_w((v4i32)in0, (v4i32)in1); \
    out1 = (RTYPE)__msa_ilvl_w((v4i32)in0, (v4i32)in1); \
  }
Johann's avatar
Johann committed
1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304
#define ILVRL_W2_SH(...) ILVRL_W2(v8i16, __VA_ARGS__)
#define ILVRL_W2_SW(...) ILVRL_W2(v4i32, __VA_ARGS__)

/* Description : Saturate the halfword element values to the max
                 unsigned value of (sat_val + 1) bits
                 The element data width remains unchanged
   Arguments   : Inputs  - in0, in1, sat_val
                 Outputs - in place operation
                 Return Type - as per RTYPE
   Details     : Each unsigned halfword element from 'in0' is saturated to the
                 value generated with (sat_val + 1) bit range.
                 The results are written in place
*/
clang-format's avatar
clang-format committed
1305 1306 1307 1308 1309
#define SAT_UH2(RTYPE, in0, in1, sat_val)            \
  {                                                  \
    in0 = (RTYPE)__msa_sat_u_h((v8u16)in0, sat_val); \
    in1 = (RTYPE)__msa_sat_u_h((v8u16)in1, sat_val); \
  }
Johann's avatar
Johann committed
1310 1311
#define SAT_UH2_UH(...) SAT_UH2(v8u16, __VA_ARGS__)

clang-format's avatar
clang-format committed
1312 1313 1314 1315 1316
#define SAT_UH4(RTYPE, in0, in1, in2, in3, sat_val) \
  {                                                 \
    SAT_UH2(RTYPE, in0, in1, sat_val);              \
    SAT_UH2(RTYPE, in2, in3, sat_val)               \
  }
Johann's avatar
Johann committed
1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328
#define SAT_UH4_UH(...) SAT_UH4(v8u16, __VA_ARGS__)

/* Description : Saturate the halfword element values to the max
                 unsigned value of (sat_val + 1) bits
                 The element data width remains unchanged
   Arguments   : Inputs  - in0, in1, sat_val
                 Outputs - in place operation
                 Return Type - as per RTYPE
   Details     : Each unsigned halfword element from 'in0' is saturated to the
                 value generated with (sat_val + 1) bit range
                 The results are written in place
*/
clang-format's avatar
clang-format committed
1329 1330 1331 1332 1333
#define SAT_SH2(RTYPE, in0, in1, sat_val)            \
  {                                                  \
    in0 = (RTYPE)__msa_sat_s_h((v8i16)in0, sat_val); \
    in1 = (RTYPE)__msa_sat_s_h((v8i16)in1, sat_val); \
  }
Johann's avatar
Johann committed
1334 1335
#define SAT_SH2_SH(...) SAT_SH2(v8i16, __VA_ARGS__)

clang-format's avatar
clang-format committed
1336 1337 1338 1339 1340
#define SAT_SH4(RTYPE, in0, in1, in2, in3, sat_val) \
  {                                                 \
    SAT_SH2(RTYPE, in0, in1, sat_val);              \
    SAT_SH2(RTYPE, in2, in3, sat_val);              \
  }
Johann's avatar
Johann committed
1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351
#define SAT_SH4_SH(...) SAT_SH4(v8i16, __VA_ARGS__)

/* Description : Indexed halfword element values are replicated to all
                 elements in output vector
   Arguments   : Inputs  - in, idx0, idx1
                 Outputs - out0, out1
                 Return Type - as per RTYPE
   Details     : 'idx0' element value from 'in' vector is replicated to all
                  elements in 'out0' vector
                  Valid index range for halfword operation is 0-7
*/
clang-format's avatar
clang-format committed
1352 1353 1354 1355 1356
#define SPLATI_H2(RTYPE, in, idx0, idx1, out0, out1) \
  {                                                  \
    out0 = (RTYPE)__msa_splati_h((v8i16)in, idx0);   \
    out1 = (RTYPE)__msa_splati_h((v8i16)in, idx1);   \
  }
Johann's avatar
Johann committed
1357 1358
#define SPLATI_H2_SH(...) SPLATI_H2(v8i16, __VA_ARGS__)

clang-format's avatar
clang-format committed
1359 1360 1361 1362 1363
#define SPLATI_H4(RTYPE, in, idx0, idx1, idx2, idx3, out0, out1, out2, out3) \
  {                                                                          \
    SPLATI_H2(RTYPE, in, idx0, idx1, out0, out1);                            \
    SPLATI_H2(RTYPE, in, idx2, idx3, out2, out3);                            \
  }
Johann's avatar
Johann committed
1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374
#define SPLATI_H4_SB(...) SPLATI_H4(v16i8, __VA_ARGS__)
#define SPLATI_H4_SH(...) SPLATI_H4(v8i16, __VA_ARGS__)

/* Description : Pack even byte elements of vector pairs
   Arguments   : Inputs  - in0, in1, in2, in3
                 Outputs - out0, out1
                 Return Type - as per RTYPE
   Details     : Even byte elements of 'in0' are copied to the left half of
                 'out0' & even byte elements of 'in1' are copied to the right
                 half of 'out0'.
*/
clang-format's avatar
clang-format committed
1375 1376 1377 1378 1379
#define PCKEV_B2(RTYPE, in0, in1, in2, in3, out0, out1)  \
  {                                                      \
    out0 = (RTYPE)__msa_pckev_b((v16i8)in0, (v16i8)in1); \
    out1 = (RTYPE)__msa_pckev_b((v16i8)in2, (v16i8)in3); \
  }
Johann's avatar
Johann committed
1380 1381 1382 1383
#define PCKEV_B2_SB(...) PCKEV_B2(v16i8, __VA_ARGS__)
#define PCKEV_B2_UB(...) PCKEV_B2(v16u8, __VA_ARGS__)
#define PCKEV_B2_SH(...) PCKEV_B2(v8i16, __VA_ARGS__)

clang-format's avatar
clang-format committed
1384 1385 1386 1387 1388 1389
#define PCKEV_B4(RTYPE, in0, in1, in2, in3, in4, in5, in6, in7, out0, out1, \
                 out2, out3)                                                \
  {                                                                         \
    PCKEV_B2(RTYPE, in0, in1, in2, in3, out0, out1);                        \
    PCKEV_B2(RTYPE, in4, in5, in6, in7, out2, out3);                        \
  }
Johann's avatar
Johann committed
1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401
#define PCKEV_B4_SB(...) PCKEV_B4(v16i8, __VA_ARGS__)
#define PCKEV_B4_UB(...) PCKEV_B4(v16u8, __VA_ARGS__)
#define PCKEV_B4_SH(...) PCKEV_B4(v8i16, __VA_ARGS__)

/* Description : Pack even halfword elements of vector pairs
   Arguments   : Inputs  - in0, in1, in2, in3
                 Outputs - out0, out1
                 Return Type - as per RTYPE
   Details     : Even halfword elements of 'in0' are copied to the left half of
                 'out0' & even halfword elements of 'in1' are copied to the
                 right half of 'out0'.
*/
clang-format's avatar
clang-format committed
1402 1403 1404 1405 1406
#define PCKEV_H2(RTYPE, in0, in1, in2, in3, out0, out1)  \
  {                                                      \
    out0 = (RTYPE)__msa_pckev_h((v8i16)in0, (v8i16)in1); \
    out1 = (RTYPE)__msa_pckev_h((v8i16)in2, (v8i16)in3); \
  }
Johann's avatar
Johann committed
1407 1408 1409
#define PCKEV_H2_SH(...) PCKEV_H2(v8i16, __VA_ARGS__)
#define PCKEV_H2_SW(...) PCKEV_H2(v4i32, __VA_ARGS__)

clang-format's avatar
clang-format committed
1410 1411 1412 1413 1414 1415
#define PCKEV_H4(RTYPE, in0, in1, in2, in3, in4, in5, in6, in7, out0, out1, \
                 out2, out3)                                                \
  {                                                                         \
    PCKEV_H2(RTYPE, in0, in1, in2, in3, out0, out1);                        \
    PCKEV_H2(RTYPE, in4, in5, in6, in7, out2, out3);                        \
  }
Johann's avatar
Johann committed
1416 1417
#define PCKEV_H4_SH(...) PCKEV_H4(v8i16, __VA_ARGS__)

1418 1419 1420 1421 1422 1423 1424 1425
/* Description : Pack even double word elements of vector pairs
   Arguments   : Inputs  - in0, in1, in2, in3
                 Outputs - out0, out1
                 Return Type - as per RTYPE
   Details     : Even double elements of 'in0' are copied to the left half of
                 'out0' & even double elements of 'in1' are copied to the right
                 half of 'out0'.
*/
clang-format's avatar
clang-format committed
1426 1427 1428 1429 1430
#define PCKEV_D2(RTYPE, in0, in1, in2, in3, out0, out1)  \
  {                                                      \
    out0 = (RTYPE)__msa_pckev_d((v2i64)in0, (v2i64)in1); \
    out1 = (RTYPE)__msa_pckev_d((v2i64)in2, (v2i64)in3); \
  }
1431
#define PCKEV_D2_UB(...) PCKEV_D2(v16u8, __VA_ARGS__)
Johann's avatar
Johann committed
1432
#define PCKEV_D2_SH(...) PCKEV_D2(v8i16, __VA_ARGS__)
1433

clang-format's avatar
clang-format committed
1434 1435 1436 1437 1438 1439
#define PCKEV_D4(RTYPE, in0, in1, in2, in3, in4, in5, in6, in7, out0, out1, \
                 out2, out3)                                                \
  {                                                                         \
    PCKEV_D2(RTYPE, in0, in1, in2, in3, out0, out1);                        \
    PCKEV_D2(RTYPE, in4, in5, in6, in7, out2, out3);                        \
  }
1440 1441
#define PCKEV_D4_UB(...) PCKEV_D4(v16u8, __VA_ARGS__)

Johann's avatar
Johann committed
1442 1443 1444 1445 1446 1447 1448
/* Description : Each byte element is logically xor'ed with immediate 128
   Arguments   : Inputs  - in0, in1
                 Outputs - in place operation
                 Return Type - as per RTYPE
   Details     : Each unsigned byte element from input vector 'in0' is
                 logically xor'ed with 128 and the result is stored in-place.
*/
clang-format's avatar
clang-format committed
1449 1450 1451 1452 1453
#define XORI_B2_128(RTYPE, in0, in1)            \
  {                                             \
    in0 = (RTYPE)__msa_xori_b((v16u8)in0, 128); \
    in1 = (RTYPE)__msa_xori_b((v16u8)in1, 128); \
  }
Johann's avatar
Johann committed
1454 1455 1456
#define XORI_B2_128_UB(...) XORI_B2_128(v16u8, __VA_ARGS__)
#define XORI_B2_128_SB(...) XORI_B2_128(v16i8, __VA_ARGS__)

clang-format's avatar
clang-format committed
1457 1458 1459 1460 1461
#define XORI_B3_128(RTYPE, in0, in1, in2)       \
  {                                             \
    XORI_B2_128(RTYPE, in0, in1);               \
    in2 = (RTYPE)__msa_xori_b((v16u8)in2, 128); \
  }
Johann's avatar
Johann committed
1462 1463
#define XORI_B3_128_SB(...) XORI_B3_128(v16i8, __VA_ARGS__)