• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 HAVE_CHUNKMEMSET_1
18 #define HAVE_CHUNKMEMSET_4
19 #define HAVE_CHUNKMEMSET_8
20 
chunkmemset_1(uint8_t * from,chunk_t * chunk)21 static inline void chunkmemset_1(uint8_t *from, chunk_t *chunk) {
22 #if defined(UNALIGNED64_OK)
23     *chunk = 0x0101010101010101 * (uint8_t)*from;
24 #elif defined(UNALIGNED_OK)
25     chunk->u32[0] = 0x01010101 * (uint8_t)*from;
26     chunk->u32[1] = chunk->u32[0];
27 #else
28     memset(chunk, *from, sizeof(chunk_t));
29 #endif
30 }
31 
chunkmemset_4(uint8_t * from,chunk_t * chunk)32 static inline void chunkmemset_4(uint8_t *from, chunk_t *chunk) {
33 #if defined(UNALIGNED64_OK)
34     uint32_t half_chunk;
35     half_chunk = *(uint32_t *)from;
36     *chunk = 0x0000000100000001 * (uint64_t)half_chunk;
37 #elif defined(UNALIGNED_OK)
38     chunk->u32[0] = *(uint32_t *)from;
39     chunk->u32[1] = chunk->u32[0];
40 #else
41     uint8_t *chunkptr = (uint8_t *)chunk;
42     memcpy(chunkptr, from, 4);
43     memcpy(chunkptr+4, from, 4);
44 #endif
45 }
46 
chunkmemset_8(uint8_t * from,chunk_t * chunk)47 static inline void chunkmemset_8(uint8_t *from, chunk_t *chunk) {
48 #if defined(UNALIGNED64_OK)
49     *chunk = *(uint64_t *)from;
50 #elif defined(UNALIGNED_OK)
51     uint32_t* p = (uint32_t *)from;
52     chunk->u32[0] = p[0];
53     chunk->u32[1] = p[1];
54 #else
55     memcpy(chunk, from, sizeof(chunk_t));
56 #endif
57 }
58 
loadchunk(uint8_t const * s,chunk_t * chunk)59 static inline void loadchunk(uint8_t const *s, chunk_t *chunk) {
60     chunkmemset_8((uint8_t *)s, chunk);
61 }
62 
storechunk(uint8_t * out,chunk_t * chunk)63 static inline void storechunk(uint8_t *out, chunk_t *chunk) {
64 #if defined(UNALIGNED64_OK)
65     *(uint64_t *)out = *chunk;
66 #elif defined(UNALIGNED_OK)
67     ((uint32_t *)out)[0] = chunk->u32[0];
68     ((uint32_t *)out)[1] = chunk->u32[1];
69 #else
70     memcpy(out, chunk, sizeof(chunk_t));
71 #endif
72 }
73 
74 #define CHUNKSIZE        chunksize_c
75 #define CHUNKCOPY        chunkcopy_c
76 #define CHUNKCOPY_SAFE   chunkcopy_safe_c
77 #define CHUNKUNROLL      chunkunroll_c
78 #define CHUNKMEMSET      chunkmemset_c
79 #define CHUNKMEMSET_SAFE chunkmemset_safe_c
80 
81 #include "chunkset_tpl.h"
82