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