1 /* 2 * Copyright (c) 2016-present, 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 <string.h> /* memcpy */ 23 24 25 /*-**************************************** 26 * Compiler specifics 27 ******************************************/ 28 #if defined(_MSC_VER) /* Visual Studio */ 29 # include <stdlib.h> /* _byteswap_ulong */ 30 # include <intrin.h> /* _byteswap_* */ 31 #endif 32 #if defined(__GNUC__) 33 # define MEM_STATIC static __inline __attribute__((unused)) 34 #elif defined (__cplusplus) || (defined (__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L) /* C99 */) 35 # define MEM_STATIC static inline 36 #elif defined(_MSC_VER) 37 # define MEM_STATIC static __inline 38 #else 39 # define MEM_STATIC static /* this version may generate warnings for unused static functions; disable the relevant warning */ 40 #endif 41 42 /* code only tested on 32 and 64 bits systems */ 43 #define MEM_STATIC_ASSERT(c) { enum { MEM_static_assert = 1/(int)(!!(c)) }; } 44 MEM_STATIC void MEM_check(void) { MEM_STATIC_ASSERT((sizeof(size_t)==4) || (sizeof(size_t)==8)); } 45 46 47 /*-************************************************************** 48 * Basic Types 49 *****************************************************************/ 50 #if !defined (__VMS) && (defined (__cplusplus) || (defined (__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L) /* C99 */) ) 51 # include <stdint.h> 52 typedef uint8_t BYTE; 53 typedef uint16_t U16; 54 typedef int16_t S16; 55 typedef uint32_t U32; 56 typedef int32_t S32; 57 typedef uint64_t U64; 58 typedef int64_t S64; 59 typedef intptr_t iPtrDiff; 60 typedef uintptr_t uPtrDiff; 61 #else 62 typedef unsigned char BYTE; 63 typedef unsigned short U16; 64 typedef signed short S16; 65 typedef unsigned int U32; 66 typedef signed int S32; 67 typedef unsigned long long U64; 68 typedef signed long long S64; 69 typedef ptrdiff_t iPtrDiff; 70 typedef size_t uPtrDiff; 71 #endif 72 73 74 /*-************************************************************** 75 * Memory I/O 76 *****************************************************************/ 77 /* MEM_FORCE_MEMORY_ACCESS : 78 * By default, access to unaligned memory is controlled by `memcpy()`, which is safe and portable. 79 * Unfortunately, on some target/compiler combinations, the generated assembly is sub-optimal. 80 * The below switch allow to select different access method for improved performance. 81 * Method 0 (default) : use `memcpy()`. Safe and portable. 82 * Method 1 : `__packed` statement. It depends on compiler extension (i.e., not portable). 83 * This method is safe if your compiler supports it, and *generally* as fast or faster than `memcpy`. 84 * Method 2 : direct access. This method is portable but violate C standard. 85 * It can generate buggy code on targets depending on alignment. 86 * In some circumstances, it's the only known way to get the most performance (i.e. GCC + ARMv6) 87 * See http://fastcompression.blogspot.fr/2015/08/accessing-unaligned-memory.html for details. 88 * Prefer these methods in priority order (0 > 1 > 2) 89 */ 90 #ifndef MEM_FORCE_MEMORY_ACCESS /* can be defined externally, on command line for example */ 91 # 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__) ) 92 # define MEM_FORCE_MEMORY_ACCESS 2 93 # elif defined(__INTEL_COMPILER) || defined(__GNUC__) 94 # define MEM_FORCE_MEMORY_ACCESS 1 95 # endif 96 #endif 97 98 MEM_STATIC unsigned MEM_32bits(void) { return sizeof(size_t)==4; } 99 MEM_STATIC unsigned MEM_64bits(void) { return sizeof(size_t)==8; } 100 101 MEM_STATIC unsigned MEM_isLittleEndian(void) 102 { 103 const union { U32 u; BYTE c[4]; } one = { 1 }; /* don't use static : performance detrimental */ 104 return one.c[0]; 105 } 106 107 #if defined(MEM_FORCE_MEMORY_ACCESS) && (MEM_FORCE_MEMORY_ACCESS==2) 108 109 /* violates C standard, by lying on structure alignment. 110 Only use if no other choice to achieve best performance on target platform */ 111 MEM_STATIC U16 MEM_read16(const void* memPtr) { return *(const U16*) memPtr; } 112 MEM_STATIC U32 MEM_read32(const void* memPtr) { return *(const U32*) memPtr; } 113 MEM_STATIC U64 MEM_read64(const void* memPtr) { return *(const U64*) memPtr; } 114 MEM_STATIC size_t MEM_readST(const void* memPtr) { return *(const size_t*) memPtr; } 115 116 MEM_STATIC void MEM_write16(void* memPtr, U16 value) { *(U16*)memPtr = value; } 117 MEM_STATIC void MEM_write32(void* memPtr, U32 value) { *(U32*)memPtr = value; } 118 MEM_STATIC void MEM_write64(void* memPtr, U64 value) { *(U64*)memPtr = value; } 119 120 #elif defined(MEM_FORCE_MEMORY_ACCESS) && (MEM_FORCE_MEMORY_ACCESS==1) 121 122 /* __pack instructions are safer, but compiler specific, hence potentially problematic for some compilers */ 123 /* currently only defined for gcc and icc */ 124 #if defined(_MSC_VER) || (defined(__INTEL_COMPILER) && defined(WIN32)) 125 __pragma( pack(push, 1) ) 126 typedef union { U16 u16; U32 u32; U64 u64; size_t st; } unalign; 127 __pragma( pack(pop) ) 128 #else 129 typedef union { U16 u16; U32 u32; U64 u64; size_t st; } __attribute__((packed)) unalign; 130 #endif 131 132 MEM_STATIC U16 MEM_read16(const void* ptr) { return ((const unalign*)ptr)->u16; } 133 MEM_STATIC U32 MEM_read32(const void* ptr) { return ((const unalign*)ptr)->u32; } 134 MEM_STATIC U64 MEM_read64(const void* ptr) { return ((const unalign*)ptr)->u64; } 135 MEM_STATIC size_t MEM_readST(const void* ptr) { return ((const unalign*)ptr)->st; } 136 137 MEM_STATIC void MEM_write16(void* memPtr, U16 value) { ((unalign*)memPtr)->u16 = value; } 138 MEM_STATIC void MEM_write32(void* memPtr, U32 value) { ((unalign*)memPtr)->u32 = value; } 139 MEM_STATIC void MEM_write64(void* memPtr, U64 value) { ((unalign*)memPtr)->u64 = value; } 140 141 #else 142 143 /* default method, safe and standard. 144 can sometimes prove slower */ 145 146 MEM_STATIC U16 MEM_read16(const void* memPtr) 147 { 148 U16 val; memcpy(&val, memPtr, sizeof(val)); return val; 149 } 150 151 MEM_STATIC U32 MEM_read32(const void* memPtr) 152 { 153 U32 val; memcpy(&val, memPtr, sizeof(val)); return val; 154 } 155 156 MEM_STATIC U64 MEM_read64(const void* memPtr) 157 { 158 U64 val; memcpy(&val, memPtr, sizeof(val)); return val; 159 } 160 161 MEM_STATIC size_t MEM_readST(const void* memPtr) 162 { 163 size_t val; memcpy(&val, memPtr, sizeof(val)); return val; 164 } 165 166 MEM_STATIC void MEM_write16(void* memPtr, U16 value) 167 { 168 memcpy(memPtr, &value, sizeof(value)); 169 } 170 171 MEM_STATIC void MEM_write32(void* memPtr, U32 value) 172 { 173 memcpy(memPtr, &value, sizeof(value)); 174 } 175 176 MEM_STATIC void MEM_write64(void* memPtr, U64 value) 177 { 178 memcpy(memPtr, &value, sizeof(value)); 179 } 180 181 #endif /* MEM_FORCE_MEMORY_ACCESS */ 182 183 MEM_STATIC U32 MEM_swap32(U32 in) 184 { 185 #if defined(_MSC_VER) /* Visual Studio */ 186 return _byteswap_ulong(in); 187 #elif defined (__GNUC__) && (__GNUC__ * 100 + __GNUC_MINOR__ >= 403) 188 return __builtin_bswap32(in); 189 #else 190 return ((in << 24) & 0xff000000 ) | 191 ((in << 8) & 0x00ff0000 ) | 192 ((in >> 8) & 0x0000ff00 ) | 193 ((in >> 24) & 0x000000ff ); 194 #endif 195 } 196 197 MEM_STATIC U64 MEM_swap64(U64 in) 198 { 199 #if defined(_MSC_VER) /* Visual Studio */ 200 return _byteswap_uint64(in); 201 #elif defined (__GNUC__) && (__GNUC__ * 100 + __GNUC_MINOR__ >= 403) 202 return __builtin_bswap64(in); 203 #else 204 return ((in << 56) & 0xff00000000000000ULL) | 205 ((in << 40) & 0x00ff000000000000ULL) | 206 ((in << 24) & 0x0000ff0000000000ULL) | 207 ((in << 8) & 0x000000ff00000000ULL) | 208 ((in >> 8) & 0x00000000ff000000ULL) | 209 ((in >> 24) & 0x0000000000ff0000ULL) | 210 ((in >> 40) & 0x000000000000ff00ULL) | 211 ((in >> 56) & 0x00000000000000ffULL); 212 #endif 213 } 214 215 MEM_STATIC size_t MEM_swapST(size_t in) 216 { 217 if (MEM_32bits()) 218 return (size_t)MEM_swap32((U32)in); 219 else 220 return (size_t)MEM_swap64((U64)in); 221 } 222 223 /*=== Little endian r/w ===*/ 224 225 MEM_STATIC U16 MEM_readLE16(const void* memPtr) 226 { 227 if (MEM_isLittleEndian()) 228 return MEM_read16(memPtr); 229 else { 230 const BYTE* p = (const BYTE*)memPtr; 231 return (U16)(p[0] + (p[1]<<8)); 232 } 233 } 234 235 MEM_STATIC void MEM_writeLE16(void* memPtr, U16 val) 236 { 237 if (MEM_isLittleEndian()) { 238 MEM_write16(memPtr, val); 239 } else { 240 BYTE* p = (BYTE*)memPtr; 241 p[0] = (BYTE)val; 242 p[1] = (BYTE)(val>>8); 243 } 244 } 245 246 MEM_STATIC U32 MEM_readLE24(const void* memPtr) 247 { 248 return MEM_readLE16(memPtr) + (((const BYTE*)memPtr)[2] << 16); 249 } 250 251 MEM_STATIC void MEM_writeLE24(void* memPtr, U32 val) 252 { 253 MEM_writeLE16(memPtr, (U16)val); 254 ((BYTE*)memPtr)[2] = (BYTE)(val>>16); 255 } 256 257 MEM_STATIC U32 MEM_readLE32(const void* memPtr) 258 { 259 if (MEM_isLittleEndian()) 260 return MEM_read32(memPtr); 261 else 262 return MEM_swap32(MEM_read32(memPtr)); 263 } 264 265 MEM_STATIC void MEM_writeLE32(void* memPtr, U32 val32) 266 { 267 if (MEM_isLittleEndian()) 268 MEM_write32(memPtr, val32); 269 else 270 MEM_write32(memPtr, MEM_swap32(val32)); 271 } 272 273 MEM_STATIC U64 MEM_readLE64(const void* memPtr) 274 { 275 if (MEM_isLittleEndian()) 276 return MEM_read64(memPtr); 277 else 278 return MEM_swap64(MEM_read64(memPtr)); 279 } 280 281 MEM_STATIC void MEM_writeLE64(void* memPtr, U64 val64) 282 { 283 if (MEM_isLittleEndian()) 284 MEM_write64(memPtr, val64); 285 else 286 MEM_write64(memPtr, MEM_swap64(val64)); 287 } 288 289 MEM_STATIC size_t MEM_readLEST(const void* memPtr) 290 { 291 if (MEM_32bits()) 292 return (size_t)MEM_readLE32(memPtr); 293 else 294 return (size_t)MEM_readLE64(memPtr); 295 } 296 297 MEM_STATIC void MEM_writeLEST(void* memPtr, size_t val) 298 { 299 if (MEM_32bits()) 300 MEM_writeLE32(memPtr, (U32)val); 301 else 302 MEM_writeLE64(memPtr, (U64)val); 303 } 304 305 /*=== Big endian r/w ===*/ 306 307 MEM_STATIC U32 MEM_readBE32(const void* memPtr) 308 { 309 if (MEM_isLittleEndian()) 310 return MEM_swap32(MEM_read32(memPtr)); 311 else 312 return MEM_read32(memPtr); 313 } 314 315 MEM_STATIC void MEM_writeBE32(void* memPtr, U32 val32) 316 { 317 if (MEM_isLittleEndian()) 318 MEM_write32(memPtr, MEM_swap32(val32)); 319 else 320 MEM_write32(memPtr, val32); 321 } 322 323 MEM_STATIC U64 MEM_readBE64(const void* memPtr) 324 { 325 if (MEM_isLittleEndian()) 326 return MEM_swap64(MEM_read64(memPtr)); 327 else 328 return MEM_read64(memPtr); 329 } 330 331 MEM_STATIC void MEM_writeBE64(void* memPtr, U64 val64) 332 { 333 if (MEM_isLittleEndian()) 334 MEM_write64(memPtr, MEM_swap64(val64)); 335 else 336 MEM_write64(memPtr, val64); 337 } 338 339 MEM_STATIC size_t MEM_readBEST(const void* memPtr) 340 { 341 if (MEM_32bits()) 342 return (size_t)MEM_readBE32(memPtr); 343 else 344 return (size_t)MEM_readBE64(memPtr); 345 } 346 347 MEM_STATIC void MEM_writeBEST(void* memPtr, size_t val) 348 { 349 if (MEM_32bits()) 350 MEM_writeBE32(memPtr, (U32)val); 351 else 352 MEM_writeBE64(memPtr, (U64)val); 353 } 354 355 356 #if defined (__cplusplus) 357 } 358 #endif 359 360 #endif /* MEM_H_MODULE */ 361