forked from ClickHouse/zlib-ng
-
Notifications
You must be signed in to change notification settings - Fork 0
/
chunkset.c
81 lines (71 loc) · 2.17 KB
/
chunkset.c
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
/* chunkset.c -- inline functions to copy small data chunks.
* For conditions of distribution and use, see copyright notice in zlib.h
*/
#include "zbuild.h"
#include "zutil.h"
// We need sizeof(chunk_t) to be 8, no matter what.
#if defined(UNALIGNED64_OK)
typedef uint64_t chunk_t;
#elif defined(UNALIGNED_OK)
typedef struct chunk_t { uint32_t u32[2]; } chunk_t;
#else
typedef struct chunk_t { uint8_t u8[8]; } chunk_t;
#endif
#define HAVE_CHUNKMEMSET_1
#define HAVE_CHUNKMEMSET_4
#define HAVE_CHUNKMEMSET_8
static inline void chunkmemset_1(uint8_t *from, chunk_t *chunk) {
#if defined(UNALIGNED64_OK)
*chunk = 0x0101010101010101 * (uint8_t)*from;
#elif defined(UNALIGNED_OK)
chunk->u32[0] = 0x01010101 * (uint8_t)*from;
chunk->u32[1] = chunk->u32[0];
#else
memset(chunk, *from, sizeof(chunk_t));
#endif
}
static inline void chunkmemset_4(uint8_t *from, chunk_t *chunk) {
#if defined(UNALIGNED64_OK)
uint32_t half_chunk;
half_chunk = *(uint32_t *)from;
*chunk = 0x0000000100000001 * (uint64_t)half_chunk;
#elif defined(UNALIGNED_OK)
chunk->u32[0] = *(uint32_t *)from;
chunk->u32[1] = chunk->u32[0];
#else
uint8_t *chunkptr = (uint8_t *)chunk;
memcpy(chunkptr, from, 4);
memcpy(chunkptr+4, from, 4);
#endif
}
static inline void chunkmemset_8(uint8_t *from, chunk_t *chunk) {
#if defined(UNALIGNED64_OK)
*chunk = *(uint64_t *)from;
#elif defined(UNALIGNED_OK)
uint32_t* p = (uint32_t *)from;
chunk->u32[0] = p[0];
chunk->u32[1] = p[1];
#else
memcpy(chunk, from, sizeof(chunk_t));
#endif
}
static inline void loadchunk(uint8_t const *s, chunk_t *chunk) {
chunkmemset_8((uint8_t *)s, chunk);
}
static inline void storechunk(uint8_t *out, chunk_t *chunk) {
#if defined(UNALIGNED64_OK)
*(uint64_t *)out = *chunk;
#elif defined(UNALIGNED_OK)
((uint32_t *)out)[0] = chunk->u32[0];
((uint32_t *)out)[1] = chunk->u32[1];
#else
memcpy(out, chunk, sizeof(chunk_t));
#endif
}
#define CHUNKSIZE chunksize_c
#define CHUNKCOPY chunkcopy_c
#define CHUNKCOPY_SAFE chunkcopy_safe_c
#define CHUNKUNROLL chunkunroll_c
#define CHUNKMEMSET chunkmemset_c
#define CHUNKMEMSET_SAFE chunkmemset_safe_c
#include "chunkset_tpl.h"