1 /**
2 * Copyright (c) 2016-present, Yann Collet, Facebook, Inc.
3 * All rights reserved.
4 *
5 * This source code is licensed under the BSD-style license found in the
6 * LICENSE file in the root directory of https://github.com/facebook/zstd.
7 * An additional grant of patent rights can be found in the PATENTS file in the
8 * same directory.
9 *
10 * This program is free software; you can redistribute it and/or modify it under
11 * the terms of the GNU General Public License version 2 as published by the
12 * Free Software Foundation. This program is dual-licensed; you may select
13 * either version 2 of the GNU General Public License ("GPL") or BSD license
14 * ("BSD").
15 */
16
17 #ifndef MEM_H_MODULE
18 #define MEM_H_MODULE
19
20 /*-****************************************
21 * Dependencies
22 ******************************************/
23 #include <asm/unaligned.h>
24 #include <linux/string.h> /* memcpy */
25 #include <linux/types.h> /* size_t, ptrdiff_t */
26
27 /*-****************************************
28 * Compiler specifics
29 ******************************************/
30 #define ZSTD_STATIC static inline
31
32 /*-**************************************************************
33 * Basic Types
34 *****************************************************************/
35 typedef uint8_t BYTE;
36 typedef uint16_t U16;
37 typedef int16_t S16;
38 typedef uint32_t U32;
39 typedef int32_t S32;
40 typedef uint64_t U64;
41 typedef int64_t S64;
42 typedef ptrdiff_t iPtrDiff;
43 typedef uintptr_t uPtrDiff;
44
45 /*-**************************************************************
46 * Memory I/O
47 *****************************************************************/
ZSTD_32bits(void)48 ZSTD_STATIC unsigned ZSTD_32bits(void) { return sizeof(size_t) == 4; }
ZSTD_64bits(void)49 ZSTD_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
ZSTD_isLittleEndian(void)57 ZSTD_STATIC unsigned ZSTD_isLittleEndian(void) { return ZSTD_LITTLE_ENDIAN; }
58
ZSTD_read16(const void * memPtr)59 ZSTD_STATIC U16 ZSTD_read16(const void *memPtr) { return get_unaligned((const U16 *)memPtr); }
60
ZSTD_read32(const void * memPtr)61 ZSTD_STATIC U32 ZSTD_read32(const void *memPtr) { return get_unaligned((const U32 *)memPtr); }
62
ZSTD_read64(const void * memPtr)63 ZSTD_STATIC U64 ZSTD_read64(const void *memPtr) { return get_unaligned((const U64 *)memPtr); }
64
ZSTD_readST(const void * memPtr)65 ZSTD_STATIC size_t ZSTD_readST(const void *memPtr) { return get_unaligned((const size_t *)memPtr); }
66
ZSTD_write16(void * memPtr,U16 value)67 ZSTD_STATIC void ZSTD_write16(void *memPtr, U16 value) { put_unaligned(value, (U16 *)memPtr); }
68
ZSTD_write32(void * memPtr,U32 value)69 ZSTD_STATIC void ZSTD_write32(void *memPtr, U32 value) { put_unaligned(value, (U32 *)memPtr); }
70
ZSTD_write64(void * memPtr,U64 value)71 ZSTD_STATIC void ZSTD_write64(void *memPtr, U64 value) { put_unaligned(value, (U64 *)memPtr); }
72
73 /*=== Little endian r/w ===*/
74
ZSTD_readLE16(const void * memPtr)75 ZSTD_STATIC U16 ZSTD_readLE16(const void *memPtr) { return get_unaligned_le16(memPtr); }
76
ZSTD_writeLE16(void * memPtr,U16 val)77 ZSTD_STATIC void ZSTD_writeLE16(void *memPtr, U16 val) { put_unaligned_le16(val, memPtr); }
78
ZSTD_readLE24(const void * memPtr)79 ZSTD_STATIC U32 ZSTD_readLE24(const void *memPtr) { return ZSTD_readLE16(memPtr) + (((const BYTE *)memPtr)[2] << 16); }
80
ZSTD_writeLE24(void * memPtr,U32 val)81 ZSTD_STATIC void ZSTD_writeLE24(void *memPtr, U32 val)
82 {
83 ZSTD_writeLE16(memPtr, (U16)val);
84 ((BYTE *)memPtr)[2] = (BYTE)(val >> 16);
85 }
86
ZSTD_readLE32(const void * memPtr)87 ZSTD_STATIC U32 ZSTD_readLE32(const void *memPtr) { return get_unaligned_le32(memPtr); }
88
ZSTD_writeLE32(void * memPtr,U32 val32)89 ZSTD_STATIC void ZSTD_writeLE32(void *memPtr, U32 val32) { put_unaligned_le32(val32, memPtr); }
90
ZSTD_readLE64(const void * memPtr)91 ZSTD_STATIC U64 ZSTD_readLE64(const void *memPtr) { return get_unaligned_le64(memPtr); }
92
ZSTD_writeLE64(void * memPtr,U64 val64)93 ZSTD_STATIC void ZSTD_writeLE64(void *memPtr, U64 val64) { put_unaligned_le64(val64, memPtr); }
94
ZSTD_readLEST(const void * memPtr)95 ZSTD_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
ZSTD_writeLEST(void * memPtr,size_t val)103 ZSTD_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 /*=== Big endian r/w ===*/
112
ZSTD_readBE32(const void * memPtr)113 ZSTD_STATIC U32 ZSTD_readBE32(const void *memPtr) { return get_unaligned_be32(memPtr); }
114
ZSTD_writeBE32(void * memPtr,U32 val32)115 ZSTD_STATIC void ZSTD_writeBE32(void *memPtr, U32 val32) { put_unaligned_be32(val32, memPtr); }
116
ZSTD_readBE64(const void * memPtr)117 ZSTD_STATIC U64 ZSTD_readBE64(const void *memPtr) { return get_unaligned_be64(memPtr); }
118
ZSTD_writeBE64(void * memPtr,U64 val64)119 ZSTD_STATIC void ZSTD_writeBE64(void *memPtr, U64 val64) { put_unaligned_be64(val64, memPtr); }
120
ZSTD_readBEST(const void * memPtr)121 ZSTD_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
ZSTD_writeBEST(void * memPtr,size_t val)129 ZSTD_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 /* function safe only for comparisons */
ZSTD_readMINMATCH(const void * memPtr,U32 length)138 ZSTD_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 /* MEM_H_MODULE */
152