blob: 7225b39742c014d5bf8c73cb88ac48cbfa5fbd61 [file] [log] [blame]
Marek BehĂșn8509f222019-04-29 22:40:44 +02001/* SPDX-License-Identifier: (GPL-2.0 or BSD-3-Clause-Clear) */
2/**
3 * Copyright (c) 2016-present, Yann Collet, Facebook, Inc.
4 * All rights reserved.
5 */
6
7#ifndef MEM_H_MODULE
8#define MEM_H_MODULE
9
10/*-****************************************
11* Dependencies
12******************************************/
13#include <asm/unaligned.h>
14#include <compiler.h>
15#include <linux/string.h> /* memcpy */
16#include <linux/types.h> /* size_t, ptrdiff_t */
17
18/*-****************************************
19* Compiler specifics
20******************************************/
21#define ZSTD_STATIC static __inline __attribute__((unused))
22
23/*-**************************************************************
24* Basic Types
25*****************************************************************/
26typedef uint8_t BYTE;
27typedef uint16_t U16;
28typedef int16_t S16;
29typedef uint32_t U32;
30typedef int32_t S32;
31typedef uint64_t U64;
32typedef int64_t S64;
33typedef ptrdiff_t iPtrDiff;
34typedef uintptr_t uPtrDiff;
35
36/*-**************************************************************
37* Memory I/O
38*****************************************************************/
39ZSTD_STATIC unsigned ZSTD_32bits(void) { return sizeof(size_t) == 4; }
40ZSTD_STATIC unsigned ZSTD_64bits(void) { return sizeof(size_t) == 8; }
41
42#if defined(__LITTLE_ENDIAN)
43#define ZSTD_LITTLE_ENDIAN 1
44#else
45#define ZSTD_LITTLE_ENDIAN 0
46#endif
47
48ZSTD_STATIC unsigned ZSTD_isLittleEndian(void) { return ZSTD_LITTLE_ENDIAN; }
49
50ZSTD_STATIC U16 ZSTD_read16(const void *memPtr) { return get_unaligned((const U16 *)memPtr); }
51
52ZSTD_STATIC U32 ZSTD_read32(const void *memPtr) { return get_unaligned((const U32 *)memPtr); }
53
54ZSTD_STATIC U64 ZSTD_read64(const void *memPtr) { return get_unaligned((const U64 *)memPtr); }
55
56ZSTD_STATIC size_t ZSTD_readST(const void *memPtr) { return get_unaligned((const size_t *)memPtr); }
57
58ZSTD_STATIC void ZSTD_write16(void *memPtr, U16 value) { put_unaligned(value, (U16 *)memPtr); }
59
60ZSTD_STATIC void ZSTD_write32(void *memPtr, U32 value) { put_unaligned(value, (U32 *)memPtr); }
61
62ZSTD_STATIC void ZSTD_write64(void *memPtr, U64 value) { put_unaligned(value, (U64 *)memPtr); }
63
64/*=== Little endian r/w ===*/
65
66ZSTD_STATIC U16 ZSTD_readLE16(const void *memPtr) { return get_unaligned_le16(memPtr); }
67
68ZSTD_STATIC void ZSTD_writeLE16(void *memPtr, U16 val) { put_unaligned_le16(val, memPtr); }
69
70ZSTD_STATIC U32 ZSTD_readLE24(const void *memPtr) { return ZSTD_readLE16(memPtr) + (((const BYTE *)memPtr)[2] << 16); }
71
72ZSTD_STATIC void ZSTD_writeLE24(void *memPtr, U32 val)
73{
74 ZSTD_writeLE16(memPtr, (U16)val);
75 ((BYTE *)memPtr)[2] = (BYTE)(val >> 16);
76}
77
78ZSTD_STATIC U32 ZSTD_readLE32(const void *memPtr) { return get_unaligned_le32(memPtr); }
79
80ZSTD_STATIC void ZSTD_writeLE32(void *memPtr, U32 val32) { put_unaligned_le32(val32, memPtr); }
81
82ZSTD_STATIC U64 ZSTD_readLE64(const void *memPtr) { return get_unaligned_le64(memPtr); }
83
84ZSTD_STATIC void ZSTD_writeLE64(void *memPtr, U64 val64) { put_unaligned_le64(val64, memPtr); }
85
86ZSTD_STATIC size_t ZSTD_readLEST(const void *memPtr)
87{
88 if (ZSTD_32bits())
89 return (size_t)ZSTD_readLE32(memPtr);
90 else
91 return (size_t)ZSTD_readLE64(memPtr);
92}
93
94ZSTD_STATIC void ZSTD_writeLEST(void *memPtr, size_t val)
95{
96 if (ZSTD_32bits())
97 ZSTD_writeLE32(memPtr, (U32)val);
98 else
99 ZSTD_writeLE64(memPtr, (U64)val);
100}
101
102/*=== Big endian r/w ===*/
103
104ZSTD_STATIC U32 ZSTD_readBE32(const void *memPtr) { return get_unaligned_be32(memPtr); }
105
106ZSTD_STATIC void ZSTD_writeBE32(void *memPtr, U32 val32) { put_unaligned_be32(val32, memPtr); }
107
108ZSTD_STATIC U64 ZSTD_readBE64(const void *memPtr) { return get_unaligned_be64(memPtr); }
109
110ZSTD_STATIC void ZSTD_writeBE64(void *memPtr, U64 val64) { put_unaligned_be64(val64, memPtr); }
111
112ZSTD_STATIC size_t ZSTD_readBEST(const void *memPtr)
113{
114 if (ZSTD_32bits())
115 return (size_t)ZSTD_readBE32(memPtr);
116 else
117 return (size_t)ZSTD_readBE64(memPtr);
118}
119
120ZSTD_STATIC void ZSTD_writeBEST(void *memPtr, size_t val)
121{
122 if (ZSTD_32bits())
123 ZSTD_writeBE32(memPtr, (U32)val);
124 else
125 ZSTD_writeBE64(memPtr, (U64)val);
126}
127
128/* function safe only for comparisons */
129ZSTD_STATIC U32 ZSTD_readMINMATCH(const void *memPtr, U32 length)
130{
131 switch (length) {
132 default:
133 case 4: return ZSTD_read32(memPtr);
134 case 3:
135 if (ZSTD_isLittleEndian())
136 return ZSTD_read32(memPtr) << 8;
137 else
138 return ZSTD_read32(memPtr) >> 8;
139 }
140}
141
142#endif /* MEM_H_MODULE */