| 1 |
/* chunkset_rvv.c - RVV version of chunkset |
| 2 |
* Copyright (C) 2023 SiFive, Inc. All rights reserved. |
| 3 |
* Contributed by Alex Chiang <[email protected]> |
| 4 |
* For conditions of distribution and use, see copyright notice in zlib.h |
| 5 |
*/ |
| 6 |
#include <riscv_vector.h> |
| 7 |
#include "zbuild.h" |
| 8 |
|
| 9 |
/* |
| 10 |
* RISC-V glibc would enable RVV optimized memcpy at runtime by IFUNC, |
| 11 |
* so we prefer using large size chunk and copy memory as much as possible. |
| 12 |
*/ |
| 13 |
#define CHUNK_SIZE 32 |
| 14 |
|
| 15 |
#define HAVE_CHUNKMEMSET_2 |
| 16 |
#define HAVE_CHUNKMEMSET_4 |
| 17 |
#define HAVE_CHUNKMEMSET_8 |
| 18 |
|
| 19 |
#define CHUNK_MEMSET_RVV_IMPL(elen) \ |
| 20 |
do { \ |
| 21 |
size_t vl, len = CHUNK_SIZE / sizeof(uint##elen##_t); \ |
| 22 |
uint##elen##_t val = *(uint##elen##_t*)from; \ |
| 23 |
uint##elen##_t* chunk_p = (uint##elen##_t*)chunk; \ |
| 24 |
do { \ |
| 25 |
vl = __riscv_vsetvl_e##elen##m4(len); \ |
| 26 |
vuint##elen##m4_t v_val = __riscv_vmv_v_x_u##elen##m4(val, vl); \ |
| 27 |
__riscv_vse##elen##_v_u##elen##m4(chunk_p, v_val, vl); \ |
| 28 |
len -= vl; chunk_p += vl; \ |
| 29 |
} while (len > 0); \ |
| 30 |
} while (0) |
| 31 |
|
| 32 |
/* We don't have a 32-byte datatype for RISC-V arch. */ |
| 33 |
typedef struct chunk_s { |
| 34 |
uint64_t data[4]; |
| 35 |
} chunk_t; |
| 36 |
|
| 37 |
static inline void chunkmemset_2(uint8_t *from, chunk_t *chunk) { |
| 38 |
CHUNK_MEMSET_RVV_IMPL(16); |
| 39 |
} |
| 40 |
|
| 41 |
static inline void chunkmemset_4(uint8_t *from, chunk_t *chunk) { |
| 42 |
CHUNK_MEMSET_RVV_IMPL(32); |
| 43 |
} |
| 44 |
|
| 45 |
static inline void chunkmemset_8(uint8_t *from, chunk_t *chunk) { |
| 46 |
CHUNK_MEMSET_RVV_IMPL(64); |
| 47 |
} |
| 48 |
|
| 49 |
static inline void loadchunk(uint8_t const *s, chunk_t *chunk) { |
| 50 |
memcpy(chunk->data, (uint8_t *)s, CHUNK_SIZE); |
| 51 |
} |
| 52 |
|
| 53 |
static inline void storechunk(uint8_t *out, chunk_t *chunk) { |
| 54 |
memcpy(out, chunk->data, CHUNK_SIZE); |
| 55 |
} |
| 56 |
|
| 57 |
#define CHUNKSIZE chunksize_rvv |
| 58 |
#define CHUNKCOPY chunkcopy_rvv |
| 59 |
#define CHUNKUNROLL chunkunroll_rvv |
| 60 |
#define CHUNKMEMSET chunkmemset_rvv |
| 61 |
#define CHUNKMEMSET_SAFE chunkmemset_safe_rvv |
| 62 |
|
| 63 |
#define HAVE_CHUNKCOPY |
| 64 |
|
| 65 |
/* |
| 66 |
* Assuming that the length is non-zero, and that `from` lags `out` by at least |
| 67 |
* sizeof chunk_t bytes, please see the comments in chunkset_tpl.h. |
| 68 |
* |
| 69 |
* We load/store a single chunk once in the `CHUNKCOPY`. |
| 70 |
* However, RISC-V glibc would enable RVV optimized memcpy at runtime by IFUNC, |
| 71 |
* such that, we prefer copy large memory size once to make good use of the the RVV advance. |
| 72 |
* |
| 73 |
* To be aligned to the other platforms, we didn't modify `CHUNKCOPY` method a lot, |
| 74 |
* but we still copy as much memory as possible for some conditions. |
| 75 |
* |
| 76 |
* case 1: out - from >= len (no overlap) |
| 77 |
* We can use memcpy to copy `len` size once |
| 78 |
* because the memory layout would be the same. |
| 79 |
* |
| 80 |
* case 2: overlap |
| 81 |
* We copy N chunks using memcpy at once, aiming to achieve our goal: |
| 82 |
* to copy as much memory as possible. |
| 83 |
* |
| 84 |
* After using a single memcpy to copy N chunks, we have to use series of |
| 85 |
* loadchunk and storechunk to ensure the result is correct. |
| 86 |
*/ |
| 87 |
static inline uint8_t* CHUNKCOPY(uint8_t *out, uint8_t const *from, unsigned len) { |
| 88 |
Assert(len > 0, "chunkcopy should never have a length 0"); |
| 89 |
int32_t align = ((len - 1) % sizeof(chunk_t)) + 1; |
| 90 |
memcpy(out, from, sizeof(chunk_t)); |
| 91 |
out += align; |
| 92 |
from += align; |
| 93 |
len -= align; |
| 94 |
ptrdiff_t dist = out - from; |
| 95 |
if (dist >= len) { |
| 96 |
memcpy(out, from, len); |
| 97 |
out += len; |
| 98 |
from += len; |
| 99 |
return out; |
| 100 |
} |
| 101 |
if (dist >= sizeof(chunk_t)) { |
| 102 |
dist = (dist / sizeof(chunk_t)) * sizeof(chunk_t); |
| 103 |
memcpy(out, from, dist); |
| 104 |
out += dist; |
| 105 |
from += dist; |
| 106 |
len -= dist; |
| 107 |
} |
| 108 |
while (len > 0) { |
| 109 |
memcpy(out, from, sizeof(chunk_t)); |
| 110 |
out += sizeof(chunk_t); |
| 111 |
from += sizeof(chunk_t); |
| 112 |
len -= sizeof(chunk_t); |
| 113 |
} |
| 114 |
return out; |
| 115 |
} |
| 116 |
|
| 117 |
#include "chunkset_tpl.h" |
| 118 |
|
| 119 |
#define INFLATE_FAST inflate_fast_rvv |
| 120 |
|
| 121 |
#include "inffast_tpl.h" |