• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2016-2020, Yann Collet, Facebook, Inc.
3  * All rights reserved.
4  *
5  * This source code is licensed under both the BSD-style license (found in the
6  * LICENSE file in the root directory of this source tree) and the GPLv2 (found
7  * in the COPYING file in the root directory of this source tree).
8  * You may select, at your option, one of the above-listed licenses.
9  */
10 
11 #ifndef MEM_H_MODULE
12 #define MEM_H_MODULE
13 
14 #if defined (__cplusplus)
15 extern "C" {
16 #endif
17 
18 /*-****************************************
19 *  Dependencies
20 ******************************************/
21 #include <stddef.h>  /* size_t, ptrdiff_t */
22 #include "compiler.h"  /* __has_builtin */
23 #include "debug.h"  /* DEBUG_STATIC_ASSERT */
24 #include "zstd_deps.h"  /* ZSTD_memcpy */
25 
26 
27 /*-****************************************
28 *  Compiler specifics
29 ******************************************/
30 #if defined(_MSC_VER)   /* Visual Studio */
31 #   include <stdlib.h>  /* _byteswap_ulong */
32 #   include <intrin.h>  /* _byteswap_* */
33 #endif
34 #if defined(__GNUC__)
35 #  define MEM_STATIC static __inline __attribute__((unused))
36 #elif defined (__cplusplus) || (defined (__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L) /* C99 */)
37 #  define MEM_STATIC static inline
38 #elif defined(_MSC_VER)
39 #  define MEM_STATIC static __inline
40 #else
41 #  define MEM_STATIC static  /* this version may generate warnings for unused static functions; disable the relevant warning */
42 #endif
43 
44 /*-**************************************************************
45 *  Basic Types
46 *****************************************************************/
47 #if  !defined (__VMS) && (defined (__cplusplus) || (defined (__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L) /* C99 */) )
48 #  if defined(_AIX)
49 #    include <inttypes.h>
50 #  else
51 #    include <stdint.h> /* intptr_t */
52 #  endif
53   typedef   uint8_t BYTE;
54   typedef  uint16_t U16;
55   typedef   int16_t S16;
56   typedef  uint32_t U32;
57   typedef   int32_t S32;
58   typedef  uint64_t U64;
59   typedef   int64_t S64;
60 #else
61 # include <limits.h>
62 #if CHAR_BIT != 8
63 #  error "this implementation requires char to be exactly 8-bit type"
64 #endif
65   typedef unsigned char      BYTE;
66 #if USHRT_MAX != 65535
67 #  error "this implementation requires short to be exactly 16-bit type"
68 #endif
69   typedef unsigned short      U16;
70   typedef   signed short      S16;
71 #if UINT_MAX != 4294967295
72 #  error "this implementation requires int to be exactly 32-bit type"
73 #endif
74   typedef unsigned int        U32;
75   typedef   signed int        S32;
76 /* note : there are no limits defined for long long type in C90.
77  * limits exist in C99, however, in such case, <stdint.h> is preferred */
78   typedef unsigned long long  U64;
79   typedef   signed long long  S64;
80 #endif
81 
82 
83 /*-**************************************************************
84 *  Memory I/O API
85 *****************************************************************/
86 /*=== Static platform detection ===*/
87 MEM_STATIC unsigned MEM_32bits(void);
88 MEM_STATIC unsigned MEM_64bits(void);
89 MEM_STATIC unsigned MEM_isLittleEndian(void);
90 
91 /*=== Native unaligned read/write ===*/
92 MEM_STATIC U16 MEM_read16(const void* memPtr);
93 MEM_STATIC U32 MEM_read32(const void* memPtr);
94 MEM_STATIC U64 MEM_read64(const void* memPtr);
95 MEM_STATIC size_t MEM_readST(const void* memPtr);
96 
97 MEM_STATIC void MEM_write16(void* memPtr, U16 value);
98 MEM_STATIC void MEM_write32(void* memPtr, U32 value);
99 MEM_STATIC void MEM_write64(void* memPtr, U64 value);
100 
101 /*=== Little endian unaligned read/write ===*/
102 MEM_STATIC U16 MEM_readLE16(const void* memPtr);
103 MEM_STATIC U32 MEM_readLE24(const void* memPtr);
104 MEM_STATIC U32 MEM_readLE32(const void* memPtr);
105 MEM_STATIC U64 MEM_readLE64(const void* memPtr);
106 MEM_STATIC size_t MEM_readLEST(const void* memPtr);
107 
108 MEM_STATIC void MEM_writeLE16(void* memPtr, U16 val);
109 MEM_STATIC void MEM_writeLE24(void* memPtr, U32 val);
110 MEM_STATIC void MEM_writeLE32(void* memPtr, U32 val32);
111 MEM_STATIC void MEM_writeLE64(void* memPtr, U64 val64);
112 MEM_STATIC void MEM_writeLEST(void* memPtr, size_t val);
113 
114 /*=== Big endian unaligned read/write ===*/
115 MEM_STATIC U32 MEM_readBE32(const void* memPtr);
116 MEM_STATIC U64 MEM_readBE64(const void* memPtr);
117 MEM_STATIC size_t MEM_readBEST(const void* memPtr);
118 
119 MEM_STATIC void MEM_writeBE32(void* memPtr, U32 val32);
120 MEM_STATIC void MEM_writeBE64(void* memPtr, U64 val64);
121 MEM_STATIC void MEM_writeBEST(void* memPtr, size_t val);
122 
123 /*=== Byteswap ===*/
124 MEM_STATIC U32 MEM_swap32(U32 in);
125 MEM_STATIC U64 MEM_swap64(U64 in);
126 MEM_STATIC size_t MEM_swapST(size_t in);
127 
128 
129 /*-**************************************************************
130 *  Memory I/O Implementation
131 *****************************************************************/
132 /* MEM_FORCE_MEMORY_ACCESS :
133  * By default, access to unaligned memory is controlled by `memcpy()`, which is safe and portable.
134  * Unfortunately, on some target/compiler combinations, the generated assembly is sub-optimal.
135  * The below switch allow to select different access method for improved performance.
136  * Method 0 (default) : use `memcpy()`. Safe and portable.
137  * Method 1 : `__packed` statement. It depends on compiler extension (i.e., not portable).
138  *            This method is safe if your compiler supports it, and *generally* as fast or faster than `memcpy`.
139  * Method 2 : direct access. This method is portable but violate C standard.
140  *            It can generate buggy code on targets depending on alignment.
141  *            In some circumstances, it's the only known way to get the most performance (i.e. GCC + ARMv6)
142  * See http://fastcompression.blogspot.fr/2015/08/accessing-unaligned-memory.html for details.
143  * Prefer these methods in priority order (0 > 1 > 2)
144  */
145 #ifndef MEM_FORCE_MEMORY_ACCESS   /* can be defined externally, on command line for example */
146 #  if defined(__GNUC__) && ( defined(__ARM_ARCH_6__) || defined(__ARM_ARCH_6J__) || defined(__ARM_ARCH_6K__) || defined(__ARM_ARCH_6Z__) || defined(__ARM_ARCH_6ZK__) || defined(__ARM_ARCH_6T2__) )
147 #    define MEM_FORCE_MEMORY_ACCESS 2
148 #  elif defined(__INTEL_COMPILER) || defined(__GNUC__) || defined(__ICCARM__)
149 #    define MEM_FORCE_MEMORY_ACCESS 1
150 #  endif
151 #endif
152 
MEM_32bits(void)153 MEM_STATIC unsigned MEM_32bits(void) { return sizeof(size_t)==4; }
MEM_64bits(void)154 MEM_STATIC unsigned MEM_64bits(void) { return sizeof(size_t)==8; }
155 
MEM_isLittleEndian(void)156 MEM_STATIC unsigned MEM_isLittleEndian(void)
157 {
158     const union { U32 u; BYTE c[4]; } one = { 1 };   /* don't use static : performance detrimental  */
159     return one.c[0];
160 }
161 
162 #if defined(MEM_FORCE_MEMORY_ACCESS) && (MEM_FORCE_MEMORY_ACCESS==2)
163 
164 /* violates C standard, by lying on structure alignment.
165 Only use if no other choice to achieve best performance on target platform */
MEM_read16(const void * memPtr)166 MEM_STATIC U16 MEM_read16(const void* memPtr) { return *(const U16*) memPtr; }
MEM_read32(const void * memPtr)167 MEM_STATIC U32 MEM_read32(const void* memPtr) { return *(const U32*) memPtr; }
MEM_read64(const void * memPtr)168 MEM_STATIC U64 MEM_read64(const void* memPtr) { return *(const U64*) memPtr; }
MEM_readST(const void * memPtr)169 MEM_STATIC size_t MEM_readST(const void* memPtr) { return *(const size_t*) memPtr; }
170 
MEM_write16(void * memPtr,U16 value)171 MEM_STATIC void MEM_write16(void* memPtr, U16 value) { *(U16*)memPtr = value; }
MEM_write32(void * memPtr,U32 value)172 MEM_STATIC void MEM_write32(void* memPtr, U32 value) { *(U32*)memPtr = value; }
MEM_write64(void * memPtr,U64 value)173 MEM_STATIC void MEM_write64(void* memPtr, U64 value) { *(U64*)memPtr = value; }
174 
175 #elif defined(MEM_FORCE_MEMORY_ACCESS) && (MEM_FORCE_MEMORY_ACCESS==1)
176 
177 /* __pack instructions are safer, but compiler specific, hence potentially problematic for some compilers */
178 /* currently only defined for gcc and icc */
179 #if defined(_MSC_VER) || (defined(__INTEL_COMPILER) && defined(WIN32))
180     __pragma( pack(push, 1) )
181     typedef struct { U16 v; } unalign16;
182     typedef struct { U32 v; } unalign32;
183     typedef struct { U64 v; } unalign64;
184     typedef struct { size_t v; } unalignArch;
__pragma(pack (pop))185     __pragma( pack(pop) )
186 #else
187     typedef struct { U16 v; } __attribute__((packed)) unalign16;
188     typedef struct { U32 v; } __attribute__((packed)) unalign32;
189     typedef struct { U64 v; } __attribute__((packed)) unalign64;
190     typedef struct { size_t v; } __attribute__((packed)) unalignArch;
191 #endif
192 
193 MEM_STATIC U16 MEM_read16(const void* ptr) { return ((const unalign16*)ptr)->v; }
MEM_read32(const void * ptr)194 MEM_STATIC U32 MEM_read32(const void* ptr) { return ((const unalign32*)ptr)->v; }
MEM_read64(const void * ptr)195 MEM_STATIC U64 MEM_read64(const void* ptr) { return ((const unalign64*)ptr)->v; }
MEM_readST(const void * ptr)196 MEM_STATIC size_t MEM_readST(const void* ptr) { return ((const unalignArch*)ptr)->v; }
197 
MEM_write16(void * memPtr,U16 value)198 MEM_STATIC void MEM_write16(void* memPtr, U16 value) { ((unalign16*)memPtr)->v = value; }
MEM_write32(void * memPtr,U32 value)199 MEM_STATIC void MEM_write32(void* memPtr, U32 value) { ((unalign32*)memPtr)->v = value; }
MEM_write64(void * memPtr,U64 value)200 MEM_STATIC void MEM_write64(void* memPtr, U64 value) { ((unalign64*)memPtr)->v = value; }
201 
202 #else
203 
204 /* default method, safe and standard.
205    can sometimes prove slower */
206 
MEM_read16(const void * memPtr)207 MEM_STATIC U16 MEM_read16(const void* memPtr)
208 {
209     U16 val; ZSTD_memcpy(&val, memPtr, sizeof(val)); return val;
210 }
211 
MEM_read32(const void * memPtr)212 MEM_STATIC U32 MEM_read32(const void* memPtr)
213 {
214     U32 val; ZSTD_memcpy(&val, memPtr, sizeof(val)); return val;
215 }
216 
MEM_read64(const void * memPtr)217 MEM_STATIC U64 MEM_read64(const void* memPtr)
218 {
219     U64 val; ZSTD_memcpy(&val, memPtr, sizeof(val)); return val;
220 }
221 
MEM_readST(const void * memPtr)222 MEM_STATIC size_t MEM_readST(const void* memPtr)
223 {
224     size_t val; ZSTD_memcpy(&val, memPtr, sizeof(val)); return val;
225 }
226 
MEM_write16(void * memPtr,U16 value)227 MEM_STATIC void MEM_write16(void* memPtr, U16 value)
228 {
229     ZSTD_memcpy(memPtr, &value, sizeof(value));
230 }
231 
MEM_write32(void * memPtr,U32 value)232 MEM_STATIC void MEM_write32(void* memPtr, U32 value)
233 {
234     ZSTD_memcpy(memPtr, &value, sizeof(value));
235 }
236 
MEM_write64(void * memPtr,U64 value)237 MEM_STATIC void MEM_write64(void* memPtr, U64 value)
238 {
239     ZSTD_memcpy(memPtr, &value, sizeof(value));
240 }
241 
242 #endif /* MEM_FORCE_MEMORY_ACCESS */
243 
MEM_swap32(U32 in)244 MEM_STATIC U32 MEM_swap32(U32 in)
245 {
246 #if defined(_MSC_VER)     /* Visual Studio */
247     return _byteswap_ulong(in);
248 #elif (defined (__GNUC__) && (__GNUC__ * 100 + __GNUC_MINOR__ >= 403)) \
249   || (defined(__clang__) && __has_builtin(__builtin_bswap32))
250     return __builtin_bswap32(in);
251 #else
252     return  ((in << 24) & 0xff000000 ) |
253             ((in <<  8) & 0x00ff0000 ) |
254             ((in >>  8) & 0x0000ff00 ) |
255             ((in >> 24) & 0x000000ff );
256 #endif
257 }
258 
MEM_swap64(U64 in)259 MEM_STATIC U64 MEM_swap64(U64 in)
260 {
261 #if defined(_MSC_VER)     /* Visual Studio */
262     return _byteswap_uint64(in);
263 #elif (defined (__GNUC__) && (__GNUC__ * 100 + __GNUC_MINOR__ >= 403)) \
264   || (defined(__clang__) && __has_builtin(__builtin_bswap64))
265     return __builtin_bswap64(in);
266 #else
267     return  ((in << 56) & 0xff00000000000000ULL) |
268             ((in << 40) & 0x00ff000000000000ULL) |
269             ((in << 24) & 0x0000ff0000000000ULL) |
270             ((in << 8)  & 0x000000ff00000000ULL) |
271             ((in >> 8)  & 0x00000000ff000000ULL) |
272             ((in >> 24) & 0x0000000000ff0000ULL) |
273             ((in >> 40) & 0x000000000000ff00ULL) |
274             ((in >> 56) & 0x00000000000000ffULL);
275 #endif
276 }
277 
MEM_swapST(size_t in)278 MEM_STATIC size_t MEM_swapST(size_t in)
279 {
280     if (MEM_32bits())
281         return (size_t)MEM_swap32((U32)in);
282     else
283         return (size_t)MEM_swap64((U64)in);
284 }
285 
286 /*=== Little endian r/w ===*/
287 
MEM_readLE16(const void * memPtr)288 MEM_STATIC U16 MEM_readLE16(const void* memPtr)
289 {
290     if (MEM_isLittleEndian())
291         return MEM_read16(memPtr);
292     else {
293         const BYTE* p = (const BYTE*)memPtr;
294         return (U16)(p[0] + (p[1]<<8));
295     }
296 }
297 
MEM_writeLE16(void * memPtr,U16 val)298 MEM_STATIC void MEM_writeLE16(void* memPtr, U16 val)
299 {
300     if (MEM_isLittleEndian()) {
301         MEM_write16(memPtr, val);
302     } else {
303         BYTE* p = (BYTE*)memPtr;
304         p[0] = (BYTE)val;
305         p[1] = (BYTE)(val>>8);
306     }
307 }
308 
MEM_readLE24(const void * memPtr)309 MEM_STATIC U32 MEM_readLE24(const void* memPtr)
310 {
311     return MEM_readLE16(memPtr) + (((const BYTE*)memPtr)[2] << 16);
312 }
313 
MEM_writeLE24(void * memPtr,U32 val)314 MEM_STATIC void MEM_writeLE24(void* memPtr, U32 val)
315 {
316     MEM_writeLE16(memPtr, (U16)val);
317     ((BYTE*)memPtr)[2] = (BYTE)(val>>16);
318 }
319 
MEM_readLE32(const void * memPtr)320 MEM_STATIC U32 MEM_readLE32(const void* memPtr)
321 {
322     if (MEM_isLittleEndian())
323         return MEM_read32(memPtr);
324     else
325         return MEM_swap32(MEM_read32(memPtr));
326 }
327 
MEM_writeLE32(void * memPtr,U32 val32)328 MEM_STATIC void MEM_writeLE32(void* memPtr, U32 val32)
329 {
330     if (MEM_isLittleEndian())
331         MEM_write32(memPtr, val32);
332     else
333         MEM_write32(memPtr, MEM_swap32(val32));
334 }
335 
MEM_readLE64(const void * memPtr)336 MEM_STATIC U64 MEM_readLE64(const void* memPtr)
337 {
338     if (MEM_isLittleEndian())
339         return MEM_read64(memPtr);
340     else
341         return MEM_swap64(MEM_read64(memPtr));
342 }
343 
MEM_writeLE64(void * memPtr,U64 val64)344 MEM_STATIC void MEM_writeLE64(void* memPtr, U64 val64)
345 {
346     if (MEM_isLittleEndian())
347         MEM_write64(memPtr, val64);
348     else
349         MEM_write64(memPtr, MEM_swap64(val64));
350 }
351 
MEM_readLEST(const void * memPtr)352 MEM_STATIC size_t MEM_readLEST(const void* memPtr)
353 {
354     if (MEM_32bits())
355         return (size_t)MEM_readLE32(memPtr);
356     else
357         return (size_t)MEM_readLE64(memPtr);
358 }
359 
MEM_writeLEST(void * memPtr,size_t val)360 MEM_STATIC void MEM_writeLEST(void* memPtr, size_t val)
361 {
362     if (MEM_32bits())
363         MEM_writeLE32(memPtr, (U32)val);
364     else
365         MEM_writeLE64(memPtr, (U64)val);
366 }
367 
368 /*=== Big endian r/w ===*/
369 
MEM_readBE32(const void * memPtr)370 MEM_STATIC U32 MEM_readBE32(const void* memPtr)
371 {
372     if (MEM_isLittleEndian())
373         return MEM_swap32(MEM_read32(memPtr));
374     else
375         return MEM_read32(memPtr);
376 }
377 
MEM_writeBE32(void * memPtr,U32 val32)378 MEM_STATIC void MEM_writeBE32(void* memPtr, U32 val32)
379 {
380     if (MEM_isLittleEndian())
381         MEM_write32(memPtr, MEM_swap32(val32));
382     else
383         MEM_write32(memPtr, val32);
384 }
385 
MEM_readBE64(const void * memPtr)386 MEM_STATIC U64 MEM_readBE64(const void* memPtr)
387 {
388     if (MEM_isLittleEndian())
389         return MEM_swap64(MEM_read64(memPtr));
390     else
391         return MEM_read64(memPtr);
392 }
393 
MEM_writeBE64(void * memPtr,U64 val64)394 MEM_STATIC void MEM_writeBE64(void* memPtr, U64 val64)
395 {
396     if (MEM_isLittleEndian())
397         MEM_write64(memPtr, MEM_swap64(val64));
398     else
399         MEM_write64(memPtr, val64);
400 }
401 
MEM_readBEST(const void * memPtr)402 MEM_STATIC size_t MEM_readBEST(const void* memPtr)
403 {
404     if (MEM_32bits())
405         return (size_t)MEM_readBE32(memPtr);
406     else
407         return (size_t)MEM_readBE64(memPtr);
408 }
409 
MEM_writeBEST(void * memPtr,size_t val)410 MEM_STATIC void MEM_writeBEST(void* memPtr, size_t val)
411 {
412     if (MEM_32bits())
413         MEM_writeBE32(memPtr, (U32)val);
414     else
415         MEM_writeBE64(memPtr, (U64)val);
416 }
417 
418 /* code only tested on 32 and 64 bits systems */
MEM_check(void)419 MEM_STATIC void MEM_check(void) { DEBUG_STATIC_ASSERT((sizeof(size_t)==4) || (sizeof(size_t)==8)); }
420 
421 
422 #if defined (__cplusplus)
423 }
424 #endif
425 
426 #endif /* MEM_H_MODULE */
427