| 1 |
/* |
| 2 |
* riscv/matchfinder_impl.h - RISC-V implementations of matchfinder functions |
| 3 |
* |
| 4 |
* Copyright 2024 Eric Biggers |
| 5 |
* |
| 6 |
* Permission is hereby granted, free of charge, to any person |
| 7 |
* obtaining a copy of this software and associated documentation |
| 8 |
* files (the "Software"), to deal in the Software without |
| 9 |
* restriction, including without limitation the rights to use, |
| 10 |
* copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 11 |
* copies of the Software, and to permit persons to whom the |
| 12 |
* Software is furnished to do so, subject to the following |
| 13 |
* conditions: |
| 14 |
* |
| 15 |
* The above copyright notice and this permission notice shall be |
| 16 |
* included in all copies or substantial portions of the Software. |
| 17 |
* |
| 18 |
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 19 |
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES |
| 20 |
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
| 21 |
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT |
| 22 |
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, |
| 23 |
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
| 24 |
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR |
| 25 |
* OTHER DEALINGS IN THE SOFTWARE. |
| 26 |
*/ |
| 27 |
|
| 28 |
#ifndef LIB_RISCV_MATCHFINDER_IMPL_H |
| 29 |
#define LIB_RISCV_MATCHFINDER_IMPL_H |
| 30 |
|
| 31 |
#if defined(ARCH_RISCV) && defined(__riscv_vector) |
| 32 |
#include <riscv_vector.h> |
| 33 |
|
| 34 |
/* |
| 35 |
* Return the maximum number of 16-bit (mf_pos_t) elements that fit in 8 RISC-V |
| 36 |
* vector registers and also evenly divide the sizes of the matchfinder buffers. |
| 37 |
*/ |
| 38 |
static forceinline size_t |
| 39 |
riscv_matchfinder_vl(void) |
| 40 |
{ |
| 41 |
const size_t vl = __riscv_vsetvlmax_e16m8(); |
| 42 |
|
| 43 |
STATIC_ASSERT(sizeof(mf_pos_t) == sizeof(s16)); |
| 44 |
/* |
| 45 |
* MATCHFINDER_SIZE_ALIGNMENT is a power of 2, as is 'vl' because the |
| 46 |
* RISC-V Vector Extension requires that the vector register length |
| 47 |
* (VLEN) be a power of 2. Thus, a simple MIN() gives the correct |
| 48 |
* answer here; rounding to a power of 2 is not required. |
| 49 |
*/ |
| 50 |
STATIC_ASSERT((MATCHFINDER_SIZE_ALIGNMENT & |
| 51 |
(MATCHFINDER_SIZE_ALIGNMENT - 1)) == 0); |
| 52 |
ASSERT((vl & (vl - 1)) == 0); |
| 53 |
return MIN(vl, MATCHFINDER_SIZE_ALIGNMENT / sizeof(mf_pos_t)); |
| 54 |
} |
| 55 |
|
| 56 |
/* matchfinder_init() optimized using the RISC-V Vector Extension */ |
| 57 |
static forceinline void |
| 58 |
matchfinder_init_rvv(mf_pos_t *p, size_t size) |
| 59 |
{ |
| 60 |
const size_t vl = riscv_matchfinder_vl(); |
| 61 |
const vint16m8_t v = __riscv_vmv_v_x_i16m8(MATCHFINDER_INITVAL, vl); |
| 62 |
|
| 63 |
ASSERT(size > 0 && size % (vl * sizeof(p[0])) == 0); |
| 64 |
do { |
| 65 |
__riscv_vse16_v_i16m8(p, v, vl); |
| 66 |
p += vl; |
| 67 |
size -= vl * sizeof(p[0]); |
| 68 |
} while (size != 0); |
| 69 |
} |
| 70 |
#define matchfinder_init matchfinder_init_rvv |
| 71 |
|
| 72 |
/* matchfinder_rebase() optimized using the RISC-V Vector Extension */ |
| 73 |
static forceinline void |
| 74 |
matchfinder_rebase_rvv(mf_pos_t *p, size_t size) |
| 75 |
{ |
| 76 |
const size_t vl = riscv_matchfinder_vl(); |
| 77 |
|
| 78 |
ASSERT(size > 0 && size % (vl * sizeof(p[0])) == 0); |
| 79 |
do { |
| 80 |
vint16m8_t v = __riscv_vle16_v_i16m8(p, vl); |
| 81 |
|
| 82 |
/* |
| 83 |
* This should generate the vsadd.vx instruction |
| 84 |
* (Vector Saturating Add, integer vector-scalar) |
| 85 |
*/ |
| 86 |
v = __riscv_vsadd_vx_i16m8(v, (s16)-MATCHFINDER_WINDOW_SIZE, |
| 87 |
vl); |
| 88 |
__riscv_vse16_v_i16m8(p, v, vl); |
| 89 |
p += vl; |
| 90 |
size -= vl * sizeof(p[0]); |
| 91 |
} while (size != 0); |
| 92 |
} |
| 93 |
#define matchfinder_rebase matchfinder_rebase_rvv |
| 94 |
|
| 95 |
#endif /* ARCH_RISCV && __riscv_vector */ |
| 96 |
|
| 97 |
#endif /* LIB_RISCV_MATCHFINDER_IMPL_H */ |