| 1 | /* chunkset.c -- inline functions to copy small data chunks. |
| 2 | * For conditions of distribution and use, see copyright notice in zlib.h |
| 3 | */ |
| 4 | |
| 5 | #include "zbuild.h" |
| 6 | #include "zutil.h" |
| 7 | |
| 8 | // We need sizeof(chunk_t) to be 8, no matter what. |
| 9 | #if defined(UNALIGNED64_OK) |
| 10 | typedef uint64_t chunk_t; |
| 11 | #elif defined(UNALIGNED_OK) |
| 12 | typedef struct chunk_t { uint32_t u32[2]; } chunk_t; |
| 13 | #else |
| 14 | typedef struct chunk_t { uint8_t u8[8]; } chunk_t; |
| 15 | #endif |
| 16 | |
| 17 | #define CHUNK_SIZE 8 |
| 18 | |
| 19 | #define HAVE_CHUNKMEMSET_1 |
| 20 | #define HAVE_CHUNKMEMSET_4 |
| 21 | #define HAVE_CHUNKMEMSET_8 |
| 22 | |
| 23 | static inline void chunkmemset_1(uint8_t *from, chunk_t *chunk) { |
| 24 | #if defined(UNALIGNED64_OK) |
| 25 | *chunk = 0x0101010101010101 * (uint8_t)*from; |
| 26 | #elif defined(UNALIGNED_OK) |
| 27 | chunk->u32[0] = 0x01010101 * (uint8_t)*from; |
| 28 | chunk->u32[1] = chunk->u32[0]; |
| 29 | #else |
| 30 | memset(chunk, *from, sizeof(chunk_t)); |
| 31 | #endif |
| 32 | } |
| 33 | |
| 34 | static inline void chunkmemset_4(uint8_t *from, chunk_t *chunk) { |
| 35 | #if defined(UNALIGNED64_OK) |
| 36 | uint32_t half_chunk; |
| 37 | half_chunk = *(uint32_t *)from; |
| 38 | *chunk = 0x0000000100000001 * (uint64_t)half_chunk; |
| 39 | #elif defined(UNALIGNED_OK) |
| 40 | chunk->u32[0] = *(uint32_t *)from; |
| 41 | chunk->u32[1] = chunk->u32[0]; |
| 42 | #else |
| 43 | uint8_t *chunkptr = (uint8_t *)chunk; |
| 44 | memcpy(chunkptr, from, 4); |
| 45 | memcpy(chunkptr+4, from, 4); |
| 46 | #endif |
| 47 | } |
| 48 | |
| 49 | static inline void chunkmemset_8(uint8_t *from, chunk_t *chunk) { |
| 50 | #if defined(UNALIGNED64_OK) |
| 51 | *chunk = *(uint64_t *)from; |
| 52 | #elif defined(UNALIGNED_OK) |
| 53 | uint32_t* p = (uint32_t *)from; |
| 54 | chunk->u32[0] = p[0]; |
| 55 | chunk->u32[1] = p[1]; |
| 56 | #else |
| 57 | memcpy(chunk, from, sizeof(chunk_t)); |
| 58 | #endif |
| 59 | } |
| 60 | |
| 61 | static inline void loadchunk(uint8_t const *s, chunk_t *chunk) { |
| 62 | chunkmemset_8(from: (uint8_t *)s, chunk); |
| 63 | } |
| 64 | |
| 65 | static inline void storechunk(uint8_t *out, chunk_t *chunk) { |
| 66 | #if defined(UNALIGNED64_OK) |
| 67 | *(uint64_t *)out = *chunk; |
| 68 | #elif defined(UNALIGNED_OK) |
| 69 | ((uint32_t *)out)[0] = chunk->u32[0]; |
| 70 | ((uint32_t *)out)[1] = chunk->u32[1]; |
| 71 | #else |
| 72 | memcpy(out, chunk, sizeof(chunk_t)); |
| 73 | #endif |
| 74 | } |
| 75 | |
| 76 | #define CHUNKSIZE chunksize_c |
| 77 | #define CHUNKCOPY chunkcopy_c |
| 78 | #define CHUNKCOPY_SAFE chunkcopy_safe_c |
| 79 | #define CHUNKUNROLL chunkunroll_c |
| 80 | #define CHUNKMEMSET chunkmemset_c |
| 81 | #define CHUNKMEMSET_SAFE chunkmemset_safe_c |
| 82 | |
| 83 | #include "chunkset_tpl.h" |
| 84 | |