Project Alice
Loading...
Searching...
No Matches
mem.h
Go to the documentation of this file.
1/*
2 * Copyright (c) Meta Platforms, Inc. and affiliates.
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)
15extern "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
35/*-**************************************************************
36* Basic Types
37*****************************************************************/
38#if !defined (__VMS) && (defined (__cplusplus) || (defined (__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L) /* C99 */) )
39# if defined(_AIX)
40# include <inttypes.h>
41# else
42# include <stdint.h> /* intptr_t */
43# endif
44 typedef uint8_t BYTE;
45 typedef uint8_t U8;
46 typedef int8_t S8;
47 typedef uint16_t U16;
48 typedef int16_t S16;
49 typedef uint32_t U32;
50 typedef int32_t S32;
51 typedef uint64_t U64;
52 typedef int64_t S64;
53#else
54# include <limits.h>
55#if CHAR_BIT != 8
56# error "this implementation requires char to be exactly 8-bit type"
57#endif
58 typedef unsigned char BYTE;
59 typedef unsigned char U8;
60 typedef signed char S8;
61#if USHRT_MAX != 65535
62# error "this implementation requires short to be exactly 16-bit type"
63#endif
64 typedef unsigned short U16;
65 typedef signed short S16;
66#if UINT_MAX != 4294967295
67# error "this implementation requires int to be exactly 32-bit type"
68#endif
69 typedef unsigned int U32;
70 typedef signed int S32;
71/* note : there are no limits defined for long long type in C90.
72 * limits exist in C99, however, in such case, <stdint.h> is preferred */
73 typedef unsigned long long U64;
74 typedef signed long long S64;
75#endif
76
77
78/*-**************************************************************
79* Memory I/O API
80*****************************************************************/
81/*=== Static platform detection ===*/
82MEM_STATIC unsigned MEM_32bits(void);
83MEM_STATIC unsigned MEM_64bits(void);
84MEM_STATIC unsigned MEM_isLittleEndian(void);
85
86/*=== Native unaligned read/write ===*/
87MEM_STATIC U16 MEM_read16(const void* memPtr);
88MEM_STATIC U32 MEM_read32(const void* memPtr);
89MEM_STATIC U64 MEM_read64(const void* memPtr);
90MEM_STATIC size_t MEM_readST(const void* memPtr);
91
92MEM_STATIC void MEM_write16(void* memPtr, U16 value);
93MEM_STATIC void MEM_write32(void* memPtr, U32 value);
94MEM_STATIC void MEM_write64(void* memPtr, U64 value);
95
96/*=== Little endian unaligned read/write ===*/
97MEM_STATIC U16 MEM_readLE16(const void* memPtr);
98MEM_STATIC U32 MEM_readLE24(const void* memPtr);
99MEM_STATIC U32 MEM_readLE32(const void* memPtr);
100MEM_STATIC U64 MEM_readLE64(const void* memPtr);
101MEM_STATIC size_t MEM_readLEST(const void* memPtr);
102
103MEM_STATIC void MEM_writeLE16(void* memPtr, U16 val);
104MEM_STATIC void MEM_writeLE24(void* memPtr, U32 val);
105MEM_STATIC void MEM_writeLE32(void* memPtr, U32 val32);
106MEM_STATIC void MEM_writeLE64(void* memPtr, U64 val64);
107MEM_STATIC void MEM_writeLEST(void* memPtr, size_t val);
108
109/*=== Big endian unaligned read/write ===*/
110MEM_STATIC U32 MEM_readBE32(const void* memPtr);
111MEM_STATIC U64 MEM_readBE64(const void* memPtr);
112MEM_STATIC size_t MEM_readBEST(const void* memPtr);
113
114MEM_STATIC void MEM_writeBE32(void* memPtr, U32 val32);
115MEM_STATIC void MEM_writeBE64(void* memPtr, U64 val64);
116MEM_STATIC void MEM_writeBEST(void* memPtr, size_t val);
117
118/*=== Byteswap ===*/
121MEM_STATIC size_t MEM_swapST(size_t in);
122
123
124/*-**************************************************************
125* Memory I/O Implementation
126*****************************************************************/
127/* MEM_FORCE_MEMORY_ACCESS : For accessing unaligned memory:
128 * Method 0 : always use `memcpy()`. Safe and portable.
129 * Method 1 : Use compiler extension to set unaligned access.
130 * Method 2 : direct access. This method is portable but violate C standard.
131 * It can generate buggy code on targets depending on alignment.
132 * Default : method 1 if supported, else method 0
133 */
134#ifndef MEM_FORCE_MEMORY_ACCESS /* can be defined externally, on command line for example */
135# ifdef __GNUC__
136# define MEM_FORCE_MEMORY_ACCESS 1
137# endif
138#endif
139
140MEM_STATIC unsigned MEM_32bits(void) { return sizeof(size_t)==4; }
141MEM_STATIC unsigned MEM_64bits(void) { return sizeof(size_t)==8; }
142
144{
145#if defined(__BYTE_ORDER__) && defined(__ORDER_LITTLE_ENDIAN__) && (__BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__)
146 return 1;
147#elif defined(__BYTE_ORDER__) && defined(__ORDER_BIG_ENDIAN__) && (__BYTE_ORDER__ == __ORDER_BIG_ENDIAN__)
148 return 0;
149#elif defined(__clang__) && __LITTLE_ENDIAN__
150 return 1;
151#elif defined(__clang__) && __BIG_ENDIAN__
152 return 0;
153#elif defined(_MSC_VER) && (_M_AMD64 || _M_IX86)
154 return 1;
155#elif defined(__DMC__) && defined(_M_IX86)
156 return 1;
157#else
158 const union { U32 u; BYTE c[4]; } one = { 1 }; /* don't use static : performance detrimental */
159 return one.c[0];
160#endif
161}
162
163#if defined(MEM_FORCE_MEMORY_ACCESS) && (MEM_FORCE_MEMORY_ACCESS==2)
164
165/* violates C standard, by lying on structure alignment.
166Only use if no other choice to achieve best performance on target platform */
167MEM_STATIC U16 MEM_read16(const void* memPtr) { return *(const U16*) memPtr; }
168MEM_STATIC U32 MEM_read32(const void* memPtr) { return *(const U32*) memPtr; }
169MEM_STATIC U64 MEM_read64(const void* memPtr) { return *(const U64*) memPtr; }
170MEM_STATIC size_t MEM_readST(const void* memPtr) { return *(const size_t*) memPtr; }
171
172MEM_STATIC void MEM_write16(void* memPtr, U16 value) { *(U16*)memPtr = value; }
173MEM_STATIC void MEM_write32(void* memPtr, U32 value) { *(U32*)memPtr = value; }
174MEM_STATIC void MEM_write64(void* memPtr, U64 value) { *(U64*)memPtr = value; }
175
176#elif defined(MEM_FORCE_MEMORY_ACCESS) && (MEM_FORCE_MEMORY_ACCESS==1)
177
178typedef __attribute__((aligned(1))) U16 unalign16;
179typedef __attribute__((aligned(1))) U32 unalign32;
180typedef __attribute__((aligned(1))) U64 unalign64;
181typedef __attribute__((aligned(1))) size_t unalignArch;
182
183MEM_STATIC U16 MEM_read16(const void* ptr) { return *(const unalign16*)ptr; }
184MEM_STATIC U32 MEM_read32(const void* ptr) { return *(const unalign32*)ptr; }
185MEM_STATIC U64 MEM_read64(const void* ptr) { return *(const unalign64*)ptr; }
186MEM_STATIC size_t MEM_readST(const void* ptr) { return *(const unalignArch*)ptr; }
187
188MEM_STATIC void MEM_write16(void* memPtr, U16 value) { *(unalign16*)memPtr = value; }
189MEM_STATIC void MEM_write32(void* memPtr, U32 value) { *(unalign32*)memPtr = value; }
190MEM_STATIC void MEM_write64(void* memPtr, U64 value) { *(unalign64*)memPtr = value; }
191
192#else
193
194/* default method, safe and standard.
195 can sometimes prove slower */
196
197MEM_STATIC U16 MEM_read16(const void* memPtr)
198{
199 U16 val; ZSTD_memcpy(&val, memPtr, sizeof(val)); return val;
200}
201
202MEM_STATIC U32 MEM_read32(const void* memPtr)
203{
204 U32 val; ZSTD_memcpy(&val, memPtr, sizeof(val)); return val;
205}
206
207MEM_STATIC U64 MEM_read64(const void* memPtr)
208{
209 U64 val; ZSTD_memcpy(&val, memPtr, sizeof(val)); return val;
210}
211
212MEM_STATIC size_t MEM_readST(const void* memPtr)
213{
214 size_t val; ZSTD_memcpy(&val, memPtr, sizeof(val)); return val;
215}
216
217MEM_STATIC void MEM_write16(void* memPtr, U16 value)
218{
219 ZSTD_memcpy(memPtr, &value, sizeof(value));
220}
221
222MEM_STATIC void MEM_write32(void* memPtr, U32 value)
223{
224 ZSTD_memcpy(memPtr, &value, sizeof(value));
225}
226
227MEM_STATIC void MEM_write64(void* memPtr, U64 value)
228{
229 ZSTD_memcpy(memPtr, &value, sizeof(value));
230}
231
232#endif /* MEM_FORCE_MEMORY_ACCESS */
233
235{
236 return ((in << 24) & 0xff000000 ) |
237 ((in << 8) & 0x00ff0000 ) |
238 ((in >> 8) & 0x0000ff00 ) |
239 ((in >> 24) & 0x000000ff );
240}
241
243{
244#if defined(_MSC_VER) /* Visual Studio */
245 return _byteswap_ulong(in);
246#elif (defined (__GNUC__) && (__GNUC__ * 100 + __GNUC_MINOR__ >= 403)) \
247 || (defined(__clang__) && __has_builtin(__builtin_bswap32))
248 return __builtin_bswap32(in);
249#else
250 return MEM_swap32_fallback(in);
251#endif
252}
253
255{
256 return ((in << 56) & 0xff00000000000000ULL) |
257 ((in << 40) & 0x00ff000000000000ULL) |
258 ((in << 24) & 0x0000ff0000000000ULL) |
259 ((in << 8) & 0x000000ff00000000ULL) |
260 ((in >> 8) & 0x00000000ff000000ULL) |
261 ((in >> 24) & 0x0000000000ff0000ULL) |
262 ((in >> 40) & 0x000000000000ff00ULL) |
263 ((in >> 56) & 0x00000000000000ffULL);
264}
265
267{
268#if defined(_MSC_VER) /* Visual Studio */
269 return _byteswap_uint64(in);
270#elif (defined (__GNUC__) && (__GNUC__ * 100 + __GNUC_MINOR__ >= 403)) \
271 || (defined(__clang__) && __has_builtin(__builtin_bswap64))
272 return __builtin_bswap64(in);
273#else
274 return MEM_swap64_fallback(in);
275#endif
276}
277
278MEM_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
288MEM_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
298MEM_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
309MEM_STATIC U32 MEM_readLE24(const void* memPtr)
310{
311 return (U32)MEM_readLE16(memPtr) + ((U32)(((const BYTE*)memPtr)[2]) << 16);
312}
313
314MEM_STATIC void MEM_writeLE24(void* memPtr, U32 val)
315{
316 MEM_writeLE16(memPtr, (U16)val);
317 ((BYTE*)memPtr)[2] = (BYTE)(val>>16);
318}
319
320MEM_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
328MEM_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
336MEM_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
344MEM_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
352MEM_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
360MEM_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
370MEM_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
378MEM_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
386MEM_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
394MEM_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
402MEM_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
410MEM_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 */
419MEM_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 */
#define MEM_STATIC
Definition: compiler.h:103
#define DEBUG_STATIC_ASSERT(c)
Definition: debug.h:43
MEM_STATIC U32 MEM_readLE24(const void *memPtr)
Definition: mem.h:309
MEM_STATIC void MEM_write64(void *memPtr, U64 value)
Definition: mem.h:227
MEM_STATIC unsigned MEM_32bits(void)
Definition: mem.h:140
MEM_STATIC U64 MEM_swap64(U64 in)
Definition: mem.h:266
MEM_STATIC U64 MEM_readBE64(const void *memPtr)
Definition: mem.h:386
MEM_STATIC U32 MEM_read32(const void *memPtr)
Definition: mem.h:202
MEM_STATIC U32 MEM_swap32(U32 in)
Definition: mem.h:242
unsigned long long U64
Definition: mem.h:73
MEM_STATIC U16 MEM_read16(const void *memPtr)
Definition: mem.h:197
MEM_STATIC void MEM_writeBE32(void *memPtr, U32 val32)
Definition: mem.h:378
MEM_STATIC size_t MEM_readLEST(const void *memPtr)
Definition: mem.h:352
unsigned char U8
Definition: mem.h:59
MEM_STATIC size_t MEM_readST(const void *memPtr)
Definition: mem.h:212
unsigned char BYTE
Definition: mem.h:58
MEM_STATIC size_t MEM_readBEST(const void *memPtr)
Definition: mem.h:402
MEM_STATIC U32 MEM_readBE32(const void *memPtr)
Definition: mem.h:370
MEM_STATIC void MEM_writeLE16(void *memPtr, U16 val)
Definition: mem.h:298
signed long long S64
Definition: mem.h:74
signed int S32
Definition: mem.h:70
MEM_STATIC void MEM_writeBEST(void *memPtr, size_t val)
Definition: mem.h:410
MEM_STATIC size_t MEM_swapST(size_t in)
Definition: mem.h:278
MEM_STATIC unsigned MEM_isLittleEndian(void)
Definition: mem.h:143
MEM_STATIC void MEM_writeLE32(void *memPtr, U32 val32)
Definition: mem.h:328
signed char S8
Definition: mem.h:60
MEM_STATIC U16 MEM_readLE16(const void *memPtr)
Definition: mem.h:288
MEM_STATIC void MEM_check(void)
Definition: mem.h:419
MEM_STATIC unsigned MEM_64bits(void)
Definition: mem.h:141
MEM_STATIC U64 MEM_swap64_fallback(U64 in)
Definition: mem.h:254
MEM_STATIC void MEM_write32(void *memPtr, U32 value)
Definition: mem.h:222
MEM_STATIC U64 MEM_readLE64(const void *memPtr)
Definition: mem.h:336
MEM_STATIC void MEM_writeLEST(void *memPtr, size_t val)
Definition: mem.h:360
unsigned int U32
Definition: mem.h:69
MEM_STATIC void MEM_write16(void *memPtr, U16 value)
Definition: mem.h:217
MEM_STATIC void MEM_writeLE64(void *memPtr, U64 val64)
Definition: mem.h:344
MEM_STATIC void MEM_writeLE24(void *memPtr, U32 val)
Definition: mem.h:314
unsigned short U16
Definition: mem.h:64
signed short S16
Definition: mem.h:65
MEM_STATIC U32 MEM_readLE32(const void *memPtr)
Definition: mem.h:320
MEM_STATIC U64 MEM_read64(const void *memPtr)
Definition: mem.h:207
MEM_STATIC void MEM_writeBE64(void *memPtr, U64 val64)
Definition: mem.h:394
MEM_STATIC U32 MEM_swap32_fallback(U32 in)
Definition: mem.h:234
uint uint32_t
ulong uint64_t
uchar uint8_t
#define ZSTD_memcpy(d, s, l)
Definition: zstd_deps.h:36