Linux 4.19.15
[linux/fpc-iii.git] / lib / zstd / mem.h
blob3a0f34c8706c2907be6634585fc964cf79c56f04
1 /**
2 * Copyright (c) 2016-present, Yann Collet, Facebook, Inc.
3 * All rights reserved.
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.
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").
17 #ifndef MEM_H_MODULE
18 #define MEM_H_MODULE
20 /*-****************************************
21 * Dependencies
22 ******************************************/
23 #include <asm/unaligned.h>
24 #include <linux/string.h> /* memcpy */
25 #include <linux/types.h> /* size_t, ptrdiff_t */
27 /*-****************************************
28 * Compiler specifics
29 ******************************************/
30 #define ZSTD_STATIC static __inline __attribute__((unused))
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;
45 /*-**************************************************************
46 * Memory I/O
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
53 #else
54 #define ZSTD_LITTLE_ENDIAN 0
55 #endif
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)
97 if (ZSTD_32bits())
98 return (size_t)ZSTD_readLE32(memPtr);
99 else
100 return (size_t)ZSTD_readLE64(memPtr);
103 ZSTD_STATIC void ZSTD_writeLEST(void *memPtr, size_t val)
105 if (ZSTD_32bits())
106 ZSTD_writeLE32(memPtr, (U32)val);
107 else
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)
123 if (ZSTD_32bits())
124 return (size_t)ZSTD_readBE32(memPtr);
125 else
126 return (size_t)ZSTD_readBE64(memPtr);
129 ZSTD_STATIC void ZSTD_writeBEST(void *memPtr, size_t val)
131 if (ZSTD_32bits())
132 ZSTD_writeBE32(memPtr, (U32)val);
133 else
134 ZSTD_writeBE64(memPtr, (U64)val);
137 /* function safe only for comparisons */
138 ZSTD_STATIC U32 ZSTD_readMINMATCH(const void *memPtr, U32 length)
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;
151 #endif /* MEM_H_MODULE */