1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17#ifndef MEM_H_MODULE
18#define MEM_H_MODULE
19
20
21
22
23#include <asm/unaligned.h>
24#include <linux/string.h>
25#include <linux/types.h>
26
27
28
29
30#define ZSTD_STATIC static __inline __attribute__((unused))
31
32
33
34
35typedef uint8_t BYTE;
36typedef uint16_t U16;
37typedef int16_t S16;
38typedef uint32_t U32;
39typedef int32_t S32;
40typedef uint64_t U64;
41typedef int64_t S64;
42typedef ptrdiff_t iPtrDiff;
43typedef uintptr_t uPtrDiff;
44
45
46
47
48ZSTD_STATIC unsigned ZSTD_32bits(void) { return sizeof(size_t) == 4; }
49ZSTD_STATIC unsigned ZSTD_64bits(void) { return sizeof(size_t) == 8; }
50
51#if defined(__LITTLE_ENDIAN)
52#define ZSTD_LITTLE_ENDIAN 1
53#else
54#define ZSTD_LITTLE_ENDIAN 0
55#endif
56
57ZSTD_STATIC unsigned ZSTD_isLittleEndian(void) { return ZSTD_LITTLE_ENDIAN; }
58
59ZSTD_STATIC U16 ZSTD_read16(const void *memPtr) { return get_unaligned((const U16 *)memPtr); }
60
61ZSTD_STATIC U32 ZSTD_read32(const void *memPtr) { return get_unaligned((const U32 *)memPtr); }
62
63ZSTD_STATIC U64 ZSTD_read64(const void *memPtr) { return get_unaligned((const U64 *)memPtr); }
64
65ZSTD_STATIC size_t ZSTD_readST(const void *memPtr) { return get_unaligned((const size_t *)memPtr); }
66
67ZSTD_STATIC void ZSTD_write16(void *memPtr, U16 value) { put_unaligned(value, (U16 *)memPtr); }
68
69ZSTD_STATIC void ZSTD_write32(void *memPtr, U32 value) { put_unaligned(value, (U32 *)memPtr); }
70
71ZSTD_STATIC void ZSTD_write64(void *memPtr, U64 value) { put_unaligned(value, (U64 *)memPtr); }
72
73
74
75ZSTD_STATIC U16 ZSTD_readLE16(const void *memPtr) { return get_unaligned_le16(memPtr); }
76
77ZSTD_STATIC void ZSTD_writeLE16(void *memPtr, U16 val) { put_unaligned_le16(val, memPtr); }
78
79ZSTD_STATIC U32 ZSTD_readLE24(const void *memPtr) { return ZSTD_readLE16(memPtr) + (((const BYTE *)memPtr)[2] << 16); }
80
81ZSTD_STATIC void ZSTD_writeLE24(void *memPtr, U32 val)
82{
83 ZSTD_writeLE16(memPtr, (U16)val);
84 ((BYTE *)memPtr)[2] = (BYTE)(val >> 16);
85}
86
87ZSTD_STATIC U32 ZSTD_readLE32(const void *memPtr) { return get_unaligned_le32(memPtr); }
88
89ZSTD_STATIC void ZSTD_writeLE32(void *memPtr, U32 val32) { put_unaligned_le32(val32, memPtr); }
90
91ZSTD_STATIC U64 ZSTD_readLE64(const void *memPtr) { return get_unaligned_le64(memPtr); }
92
93ZSTD_STATIC void ZSTD_writeLE64(void *memPtr, U64 val64) { put_unaligned_le64(val64, memPtr); }
94
95ZSTD_STATIC size_t ZSTD_readLEST(const void *memPtr)
96{
97 if (ZSTD_32bits())
98 return (size_t)ZSTD_readLE32(memPtr);
99 else
100 return (size_t)ZSTD_readLE64(memPtr);
101}
102
103ZSTD_STATIC void ZSTD_writeLEST(void *memPtr, size_t val)
104{
105 if (ZSTD_32bits())
106 ZSTD_writeLE32(memPtr, (U32)val);
107 else
108 ZSTD_writeLE64(memPtr, (U64)val);
109}
110
111
112
113ZSTD_STATIC U32 ZSTD_readBE32(const void *memPtr) { return get_unaligned_be32(memPtr); }
114
115ZSTD_STATIC void ZSTD_writeBE32(void *memPtr, U32 val32) { put_unaligned_be32(val32, memPtr); }
116
117ZSTD_STATIC U64 ZSTD_readBE64(const void *memPtr) { return get_unaligned_be64(memPtr); }
118
119ZSTD_STATIC void ZSTD_writeBE64(void *memPtr, U64 val64) { put_unaligned_be64(val64, memPtr); }
120
121ZSTD_STATIC size_t ZSTD_readBEST(const void *memPtr)
122{
123 if (ZSTD_32bits())
124 return (size_t)ZSTD_readBE32(memPtr);
125 else
126 return (size_t)ZSTD_readBE64(memPtr);
127}
128
129ZSTD_STATIC void ZSTD_writeBEST(void *memPtr, size_t val)
130{
131 if (ZSTD_32bits())
132 ZSTD_writeBE32(memPtr, (U32)val);
133 else
134 ZSTD_writeBE64(memPtr, (U64)val);
135}
136
137
138ZSTD_STATIC U32 ZSTD_readMINMATCH(const void *memPtr, U32 length)
139{
140 switch (length) {
141 default:
142 case 4: return ZSTD_read32(memPtr);
143 case 3:
144 if (ZSTD_isLittleEndian())
145 return ZSTD_read32(memPtr) << 8;
146 else
147 return ZSTD_read32(memPtr) >> 8;
148 }
149}
150
151#endif
152