2 * Copyright (c) 2016-present, Yann Collet, Facebook, Inc.
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
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
20 /*-****************************************
22 ******************************************/
23 #include <asm/unaligned.h>
24 #include <linux/string.h> /* memcpy */
25 #include <linux/types.h> /* size_t, ptrdiff_t */
27 /*-****************************************
29 ******************************************/
30 #define ZSTD_STATIC static inline
32 /*-**************************************************************
34 *****************************************************************/
42 typedef ptrdiff_t iPtrDiff;
43 typedef uintptr_t uPtrDiff;
45 /*-**************************************************************
47 *****************************************************************/
48 ZSTD_STATIC unsigned ZSTD_32bits(void) { return sizeof(size_t) == 4; }
49 ZSTD_STATIC unsigned ZSTD_64bits(void) { return sizeof(size_t) == 8; }
51 #if defined(__LITTLE_ENDIAN)
52 #define ZSTD_LITTLE_ENDIAN 1
54 #define ZSTD_LITTLE_ENDIAN 0
57 ZSTD_STATIC unsigned ZSTD_isLittleEndian(void) { return ZSTD_LITTLE_ENDIAN; }
59 ZSTD_STATIC U16 ZSTD_read16(const void *memPtr) { return get_unaligned((const U16 *)memPtr); }
61 ZSTD_STATIC U32 ZSTD_read32(const void *memPtr) { return get_unaligned((const U32 *)memPtr); }
63 ZSTD_STATIC U64 ZSTD_read64(const void *memPtr) { return get_unaligned((const U64 *)memPtr); }
65 ZSTD_STATIC size_t ZSTD_readST(const void *memPtr) { return get_unaligned((const size_t *)memPtr); }
67 ZSTD_STATIC void ZSTD_write16(void *memPtr, U16 value) { put_unaligned(value, (U16 *)memPtr); }
69 ZSTD_STATIC void ZSTD_write32(void *memPtr, U32 value) { put_unaligned(value, (U32 *)memPtr); }
71 ZSTD_STATIC void ZSTD_write64(void *memPtr, U64 value) { put_unaligned(value, (U64 *)memPtr); }
73 /*=== Little endian r/w ===*/
75 ZSTD_STATIC U16 ZSTD_readLE16(const void *memPtr) { return get_unaligned_le16(memPtr); }
77 ZSTD_STATIC void ZSTD_writeLE16(void *memPtr, U16 val) { put_unaligned_le16(val, memPtr); }
79 ZSTD_STATIC U32 ZSTD_readLE24(const void *memPtr) { return ZSTD_readLE16(memPtr) + (((const BYTE *)memPtr)[2] << 16); }
81 ZSTD_STATIC void ZSTD_writeLE24(void *memPtr, U32 val)
83 ZSTD_writeLE16(memPtr, (U16)val);
84 ((BYTE *)memPtr)[2] = (BYTE)(val >> 16);
87 ZSTD_STATIC U32 ZSTD_readLE32(const void *memPtr) { return get_unaligned_le32(memPtr); }
89 ZSTD_STATIC void ZSTD_writeLE32(void *memPtr, U32 val32) { put_unaligned_le32(val32, memPtr); }
91 ZSTD_STATIC U64 ZSTD_readLE64(const void *memPtr) { return get_unaligned_le64(memPtr); }
93 ZSTD_STATIC void ZSTD_writeLE64(void *memPtr, U64 val64) { put_unaligned_le64(val64, memPtr); }
95 ZSTD_STATIC size_t ZSTD_readLEST(const void *memPtr)
98 return (size_t)ZSTD_readLE32(memPtr);
100 return (size_t)ZSTD_readLE64(memPtr);
103 ZSTD_STATIC void ZSTD_writeLEST(void *memPtr, size_t val)
106 ZSTD_writeLE32(memPtr, (U32)val);
108 ZSTD_writeLE64(memPtr, (U64)val);
111 /*=== Big endian r/w ===*/
113 ZSTD_STATIC U32 ZSTD_readBE32(const void *memPtr) { return get_unaligned_be32(memPtr); }
115 ZSTD_STATIC void ZSTD_writeBE32(void *memPtr, U32 val32) { put_unaligned_be32(val32, memPtr); }
117 ZSTD_STATIC U64 ZSTD_readBE64(const void *memPtr) { return get_unaligned_be64(memPtr); }
119 ZSTD_STATIC void ZSTD_writeBE64(void *memPtr, U64 val64) { put_unaligned_be64(val64, memPtr); }
121 ZSTD_STATIC size_t ZSTD_readBEST(const void *memPtr)
124 return (size_t)ZSTD_readBE32(memPtr);
126 return (size_t)ZSTD_readBE64(memPtr);
129 ZSTD_STATIC void ZSTD_writeBEST(void *memPtr, size_t val)
132 ZSTD_writeBE32(memPtr, (U32)val);
134 ZSTD_writeBE64(memPtr, (U64)val);
137 /* function safe only for comparisons */
138 ZSTD_STATIC U32 ZSTD_readMINMATCH(const void *memPtr, U32 length)
142 case 4: return ZSTD_read32(memPtr);
144 if (ZSTD_isLittleEndian())
145 return ZSTD_read32(memPtr) << 8;
147 return ZSTD_read32(memPtr) >> 8;
151 #endif /* MEM_H_MODULE */