vectorscan-src 5.4.13

Bundled source of [VectorCamp/vectorscan](https://github.com/VectorCamp/vectorscan) (BSD-3-Clause).
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
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
222
223
224
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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
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
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
/*
 * Copyright (c) 2015-2020, Intel Corporation
 * Copyright (c) 2020-2021, VectorCamp PC
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 *  * Redistributions of source code must retain the above copyright notice,
 *    this list of conditions and the following disclaimer.
 *  * Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *  * Neither the name of Intel Corporation nor the names of its contributors
 *    may be used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

/** \file
 * \brief SIMD types and primitive operations.
 */

#ifndef ARCH_PPC64EL_SIMD_UTILS_H
#define ARCH_PPC64EL_SIMD_UTILS_H

#include <stdio.h>

#include "ue2common.h"
#include "util/simd_types.h"
#include "util/unaligned.h"
#include "util/intrinsics.h"

#include <string.h> // for memcpy

#if defined(__clang__) && (__clang_major__ == 15)
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecate-lax-vec-conv-all"
#endif // defined(__clang__) && (__clang_major__ == 15)

typedef __vector unsigned long long int  uint64x2_t;
typedef __vector   signed long long int   int64x2_t;
typedef __vector unsigned int            uint32x4_t;
typedef __vector   signed int             int32x4_t;
typedef __vector unsigned short int      uint16x8_t;
typedef __vector   signed short int       int16x8_t;
typedef __vector unsigned char           uint8x16_t;
typedef __vector  signed char             int8x16_t;

typedef unsigned long long int ulong64_t;
typedef   signed long long int  long64_t;

static really_inline m128 ones128(void) {
    return (m128) vec_splat_u8(-1);
}

static really_inline m128 zeroes128(void) {
    return (m128) vec_splat_s32(0);
}

/** \brief Bitwise not for m128*/
static really_inline m128 not128(m128 a) {
    //return (m128)vec_xor(a, a);
    return (m128) vec_xor(a,ones128());
}

/** \brief Return 1 if a and b are different otherwise 0 */
static really_inline int diff128(m128 a, m128 b) {
    return vec_any_ne(a, b);
}

static really_inline int isnonzero128(m128 a) {
    return !!diff128(a, zeroes128());
}

/**
 * "Rich" version of diff128(). Takes two vectors a and b and returns a 4-bit
 * mask indicating which 32-bit words contain differences.
 */
static really_inline u32 diffrich128(m128 a, m128 b) {
    static const m128 movemask = { 1, 2, 4, 8 };  
    m128 mask = (m128) vec_cmpeq(a, b); // _mm_cmpeq_epi32 (a, b);
    mask = vec_and(not128(mask), movemask);
    m128 sum = vec_sums(mask, zeroes128()); 
    return sum[3];
}

/**
 * "Rich" version of diff128(), 64-bit variant. Takes two vectors a and b and
 * returns a 4-bit mask indicating which 64-bit words contain differences.
 */
static really_inline u32 diffrich64_128(m128 a, m128 b) {
    static const uint64x2_t movemask = { 1, 4 };
    uint64x2_t mask = (uint64x2_t) vec_cmpeq((uint64x2_t)a, (uint64x2_t)b);
    mask = (uint64x2_t) vec_and((uint64x2_t)not128((m128)mask), movemask);
    m128 sum = vec_sums((m128)mask, zeroes128());
    return sum[3];
}

static really_really_inline
m128 add_2x64(m128 a, m128 b) {
    return (m128) vec_add((uint64x2_t)a, (uint64x2_t)b);
}

static really_really_inline
m128 sub_2x64(m128 a, m128 b) {
    return (m128) vec_sub((uint64x2_t)a, (uint64x2_t)b);
}

static really_really_inline
m128 lshift_m128(m128 a, unsigned b) {
    if (b == 0) return a;
    m128 sl = (m128) vec_splats((uint8_t) b << 3);
    m128 result = (m128) vec_slo((uint8x16_t) a, (uint8x16_t) sl);
    return result;
}

static really_really_inline
m128 rshift_m128(m128 a, unsigned b) {
    if (b == 0) return a;
    m128 sl = (m128) vec_splats((uint8_t) b << 3);
    uint8x16_t result = vec_sro((uint8x16_t) a, (uint8x16_t) sl);
    return (m128) result;
}

static really_really_inline
m128 lshift64_m128(m128 a, unsigned b) {
  uint64x2_t shift_indices = vec_splats((ulong64_t)b); 
  return (m128) vec_sl((int64x2_t)a, shift_indices);
}

static really_really_inline
m128 rshift64_m128(m128 a, unsigned  b) {
  uint64x2_t shift_indices = vec_splats((ulong64_t)b); 
  return (m128) vec_sr((int64x2_t)a, shift_indices);
}

static really_inline m128 eq128(m128 a, m128 b) {
   return (m128) vec_cmpeq((uint8x16_t)a, (uint8x16_t)b);
}

static really_inline m128 eq64_m128(m128 a, m128 b) {
   return (m128) vec_cmpeq((uint64x2_t)a, (uint64x2_t)b);
}

static really_inline u32 movemask128(m128 a) {
   static uint8x16_t perm = { 16, 24, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
   uint8x16_t bitmask = vec_gb((uint8x16_t) a);
   bitmask = (uint8x16_t) vec_perm(vec_splat_u8(0), bitmask, perm);
   u32 ALIGN_ATTR(16) movemask;
   vec_ste((uint32x4_t) bitmask, 0, &movemask);
   return movemask;
}

static really_inline m128 set1_16x8(u8 c) {
    return (m128) vec_splats(c);
}

static really_inline m128 set1_4x32(u32 c) {
    return (m128) vec_splats(c);
}

static really_inline m128 set1_2x64(u64a c) {
    return (m128) vec_splats(c);
}

static really_inline u32 movd(const m128 in) {
   return (u32) vec_extract((uint32x4_t)in, 0);
}

static really_inline u64a movq(const m128 in) {
    u64a ALIGN_ATTR(16) a[2];
    vec_xst((uint64x2_t) in, 0, a);
    return a[0];  
}

/* another form of movq */
static really_inline
m128 load_m128_from_u64a(const u64a *p) {
    m128 vec =(m128) vec_splats(*p);
    return rshift_m128(vec,8);
}


static really_inline u32 extract32from128(const m128 in, unsigned imm) {
u32 ALIGN_ATTR(16) a[4];
vec_xst((uint32x4_t) in, 0, a);
switch (imm) {
    case 0:
        return a[0];break;
    case 1:
        return a[1];break;
    case 2:
        return a[2];break;
    case 3:
        return a[3];break;
    default:
	return 0;break;
    }
}

static really_inline u64a extract64from128(const m128 in, unsigned imm) {
u64a ALIGN_ATTR(16) a[2];
vec_xst((uint64x2_t) in, 0, a);
switch (imm) {
    case 0:
        return a[0];break;
    case 1:
        return a[1];break;
    default:
	return 0;
	break;
    }
}

static really_inline m128 low64from128(const m128 in) {
    return rshift_m128(in,8); 
}

static really_inline m128 high64from128(const m128 in) {
    return lshift_m128(in,8); 
}


static really_inline m128 add128(m128 a, m128 b) {
    return (m128) vec_add((uint64x2_t)a, (uint64x2_t)b);
}

static really_inline m128 and128(m128 a, m128 b) {
    return (m128) vec_and((int8x16_t)a, (int8x16_t)b);
}

static really_inline m128 xor128(m128 a, m128 b) {
    return (m128) vec_xor((int8x16_t)a, (int8x16_t)b);
}

static really_inline m128 or128(m128 a, m128 b) {
    return (m128) vec_or((int8x16_t)a, (int8x16_t)b);
}

static really_inline m128 andnot128(m128 a, m128 b) {
    return (m128) and128(not128(a),b);
}

// aligned load
static really_inline m128 load128(const void *ptr) {
    assert(ISALIGNED_N(ptr, alignof(m128)));
    // cppcheck-suppress cstyleCast
    return (m128) vec_xl(0, (const int32_t*)ptr);
}

// aligned store
static really_inline void store128(void *ptr, m128 a) { 	
    assert(ISALIGNED_N(ptr, alignof(m128)));
    // cppcheck-suppress cstyleCast
    vec_st(a, 0, (int32_t*)ptr);
}

// unaligned load
static really_inline m128 loadu128(const void *ptr) {
    // cppcheck-suppress cstyleCast
    return (m128) vec_xl(0, (const int32_t*)ptr);
}

// unaligned store
static really_inline void storeu128(void *ptr, m128 a) {
    // cppcheck-suppress cstyleCast
    vec_xst(a, 0, (int32_t*)ptr);
}

// packed unaligned store of first N bytes
static really_inline
void storebytes128(void *ptr, m128 a, unsigned int n) {
    assert(n <= sizeof(a));
    memcpy(ptr, &a, n);
}

// packed unaligned load of first N bytes, pad with zero
static really_inline
m128 loadbytes128(const void *ptr, unsigned int n) {
    m128 a = zeroes128();
    assert(n <= sizeof(a));
    memcpy(&a, ptr, n);
    return a;
}

#define CASE_ALIGN_VECTORS(a, b, offset)  case offset: return (m128)vec_sld((int8x16_t)(a), (int8x16_t)(b), (16 - offset)); break;

static really_really_inline
m128 palignr_imm(m128 r, m128 l, int offset) {
    switch (offset) {
    case 0: return l; break;
    CASE_ALIGN_VECTORS(r, l, 1);
    CASE_ALIGN_VECTORS(r, l, 2);
    CASE_ALIGN_VECTORS(r, l, 3);
    CASE_ALIGN_VECTORS(r, l, 4);
    CASE_ALIGN_VECTORS(r, l, 5);
    CASE_ALIGN_VECTORS(r, l, 6);
    CASE_ALIGN_VECTORS(r, l, 7);
    CASE_ALIGN_VECTORS(r, l, 8);
    CASE_ALIGN_VECTORS(r, l, 9);
    CASE_ALIGN_VECTORS(r, l, 10);
    CASE_ALIGN_VECTORS(r, l, 11);
    CASE_ALIGN_VECTORS(r, l, 12);
    CASE_ALIGN_VECTORS(r, l, 13);
    CASE_ALIGN_VECTORS(r, l, 14);
    CASE_ALIGN_VECTORS(r, l, 15);
    case 16: return r; break;
    default: return zeroes128(); break;
    } 
}

static really_really_inline
m128 palignr(m128 r, m128 l, int offset) {
    if (offset == 0) return l;
    if (offset == 16) return r;
#if defined(HAVE__BUILTIN_CONSTANT_P)
    if (__builtin_constant_p(offset)) {
        return (m128)vec_sld((int8x16_t)(r), (int8x16_t)(l), 16 - offset);
    }
#endif
    m128 sl = (m128) vec_splats((uint8_t) (offset << 3));
    m128 sr = (m128) vec_splats((uint8_t) ((16 - offset) << 3));
    m128 rhs = (m128) vec_slo((uint8x16_t) r, (uint8x16_t) sr);
    m128 lhs = (m128) vec_sro((uint8x16_t) l, (uint8x16_t) sl);
    return or128(lhs, rhs);
}

#undef CASE_ALIGN_VECTORS

static really_really_inline
m128 rshiftbyte_m128(m128 a, unsigned b) {
    return palignr_imm(zeroes128(), a, b);
}

static really_really_inline
m128 lshiftbyte_m128(m128 a, unsigned b) {
    return palignr_imm(a, zeroes128(), 16 - b);
}

static really_inline
m128 variable_byte_shift_m128(m128 in, s32 amount) {
    assert(amount >= -16 && amount <= 16);
    if (amount < 0) {
        return rshiftbyte_m128(in, -amount);
    } else {
        return lshiftbyte_m128(in, amount);
    }
}

static really_inline
m128 mask1bit128(unsigned int n) {
    assert(n < sizeof(m128) * 8);
    static uint64x2_t onebit = { 1, 0 };
    m128 octets = (m128) vec_splats((uint8_t) ((n / 8) << 3));
    m128 bits = (m128) vec_splats((uint8_t) ((n % 8)));
    m128 mask = (m128) vec_slo((uint8x16_t) onebit, (uint8x16_t) octets);
    return (m128) vec_sll((uint8x16_t) mask, (uint8x16_t) bits);
}

// switches on bit N in the given vector.
static really_inline
void setbit128(m128 *ptr, unsigned int n) {
    *ptr = or128(mask1bit128(n), *ptr);
}

// switches off bit N in the given vector.
static really_inline
void clearbit128(m128 *ptr, unsigned int n) {
    *ptr = andnot128(mask1bit128(n), *ptr);
}

// tests bit N in the given vector.
static really_inline
char testbit128(m128 val, unsigned int n) {
    const m128 mask = mask1bit128(n);
    return isnonzero128(and128(mask, val));
}

static really_inline
m128 pshufb_m128(m128 a, m128 b) {
    /* On Intel, if bit 0x80 is set, then result is zero, otherwise which the lane it is &0xf.
       In NEON or PPC, if >=16, then the result is zero, otherwise it is that lane.
       below is the version that is converted from Intel to PPC.  */
    uint8x16_t mask =(uint8x16_t)vec_cmpge((uint8x16_t)b, (uint8x16_t)vec_splats((uint8_t)0x80));
    uint8x16_t res = vec_perm ((uint8x16_t)a, (uint8x16_t)a, (uint8x16_t)b);
    return (m128) vec_sel((uint8x16_t)res, (uint8x16_t)zeroes128(), (uint8x16_t)mask);
}

static really_inline
m128 max_u8_m128(m128 a, m128 b) {
    return (m128) vec_max((uint8x16_t)a, (uint8x16_t)b);
}

static really_inline
m128 min_u8_m128(m128 a, m128 b) {
    return (m128) vec_min((uint8x16_t)a, (uint8x16_t)b);
}

static really_inline
m128 sadd_u8_m128(m128 a, m128 b) {
    return (m128) vec_adds((uint8x16_t)a, (uint8x16_t)b);
}

static really_inline
m128 sub_u8_m128(m128 a, m128 b) {
    return (m128) vec_sub((uint8x16_t)a, (uint8x16_t)b);
}

static really_inline
m128 set4x32(u32 x3, u32 x2, u32 x1, u32  x0) {
    uint32x4_t v = { x0, x1, x2, x3 };
    return (m128) v;
}

static really_inline
m128 set2x64(u64a hi, u64a lo) {
    uint64x2_t v = { lo, hi };
    return (m128) v;
}

static really_inline
m128 widenlo128(m128 x) {
    return (m128) vec_mergel((m128)x, zeroes128());
}

static really_inline
m128 widenhi128(m128 x) {
    return (m128) vec_mergeh((m128)x, zeroes128());
}

#if defined(__clang__) && (__clang_major__ == 15)
#pragma clang diagnostic pop
#endif // defined(__clang__) && (__clang_major__ == 15)

#endif // ARCH_PPC64EL_SIMD_UTILS_H