]> git.sesse.net Git - bcachefs-tools-debian/blob - linux/lz4defs.h
80018ea65855fb455f2fe4a8d9eaef15da1a742c
[bcachefs-tools-debian] / linux / lz4defs.h
1 #ifndef __LZ4DEFS_H__
2 #define __LZ4DEFS_H__
3
4 /*
5  * lz4defs.h -- common and architecture specific defines for the kernel usage
6
7  * LZ4 - Fast LZ compression algorithm
8  * Copyright (C) 2011-2016, Yann Collet.
9  * BSD 2-Clause License (http://www.opensource.org/licenses/bsd-license.php)
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions are
12  * met:
13  *      * Redistributions of source code must retain the above copyright
14  *        notice, this list of conditions and the following disclaimer.
15  *      * Redistributions in binary form must reproduce the above
16  * copyright notice, this list of conditions and the following disclaimer
17  * in the documentation and/or other materials provided with the
18  * distribution.
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30  * You can contact the author at :
31  *      - LZ4 homepage : http://www.lz4.org
32  *      - LZ4 source repository : https://github.com/lz4/lz4
33  *
34  *      Changed for kernel usage by:
35  *      Sven Schmidt <4sschmid@informatik.uni-hamburg.de>
36  */
37
38 #include <asm/unaligned.h>
39 #include <linux/bitops.h>
40 #include <linux/string.h>        /* memset, memcpy */
41
42 #define FORCE_INLINE __always_inline
43
44 /*-************************************
45  *      Basic Types
46  **************************************/
47 #include <linux/types.h>
48
49 typedef uint8_t BYTE;
50 typedef uint16_t U16;
51 typedef uint32_t U32;
52 typedef int32_t S32;
53 typedef uint64_t U64;
54 typedef uintptr_t uptrval;
55
56 /*-************************************
57  *      Architecture specifics
58  **************************************/
59 #if defined(CONFIG_64BIT)
60 #define LZ4_ARCH64 1
61 #else
62 #define LZ4_ARCH64 0
63 #endif
64
65 #if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
66 #define LZ4_LITTLE_ENDIAN 1
67 #elif __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
68 #define LZ4_LITTLE_ENDIAN 0
69 #endif
70
71 /*-************************************
72  *      Constants
73  **************************************/
74 #define MINMATCH 4
75
76 #define WILDCOPYLENGTH 8
77 #define LASTLITERALS 5
78 #define MFLIMIT (WILDCOPYLENGTH + MINMATCH)
79
80 /* Increase this value ==> compression run slower on incompressible data */
81 #define LZ4_SKIPTRIGGER 6
82
83 #define HASH_UNIT sizeof(size_t)
84
85 #define KB (1 << 10)
86 #define MB (1 << 20)
87 #define GB (1U << 30)
88
89 #define MAXD_LOG 16
90 #define MAX_DISTANCE ((1 << MAXD_LOG) - 1)
91 #define STEPSIZE sizeof(size_t)
92
93 #define ML_BITS 4
94 #define ML_MASK ((1U << ML_BITS) - 1)
95 #define RUN_BITS (8 - ML_BITS)
96 #define RUN_MASK ((1U << RUN_BITS) - 1)
97
98 /*-************************************
99  *      Reading and writing into memory
100  **************************************/
101 static FORCE_INLINE U16 LZ4_read16(const void *ptr)
102 {
103         return get_unaligned((const U16 *)ptr);
104 }
105
106 static FORCE_INLINE U32 LZ4_read32(const void *ptr)
107 {
108         return get_unaligned((const U32 *)ptr);
109 }
110
111 static FORCE_INLINE size_t LZ4_read_ARCH(const void *ptr)
112 {
113         return get_unaligned((const size_t *)ptr);
114 }
115
116 static FORCE_INLINE void LZ4_write16(void *memPtr, U16 value)
117 {
118         put_unaligned(value, (U16 *)memPtr);
119 }
120
121 static FORCE_INLINE void LZ4_write32(void *memPtr, U32 value)
122 {
123         put_unaligned(value, (U32 *)memPtr);
124 }
125
126 static FORCE_INLINE U16 LZ4_readLE16(const void *memPtr)
127 {
128         return get_unaligned_le16(memPtr);
129 }
130
131 static FORCE_INLINE void LZ4_writeLE16(void *memPtr, U16 value)
132 {
133         return put_unaligned_le16(value, memPtr);
134 }
135
136 static FORCE_INLINE void LZ4_copy8(void *dst, const void *src)
137 {
138 #if LZ4_ARCH64
139         U64 a = get_unaligned((const U64 *)src);
140
141         put_unaligned(a, (U64 *)dst);
142 #else
143         U32 a = get_unaligned((const U32 *)src);
144         U32 b = get_unaligned((const U32 *)src + 1);
145
146         put_unaligned(a, (U32 *)dst);
147         put_unaligned(b, (U32 *)dst + 1);
148 #endif
149 }
150
151 /*
152  * customized variant of memcpy,
153  * which can overwrite up to 7 bytes beyond dstEnd
154  */
155 static FORCE_INLINE void LZ4_wildCopy(void *dstPtr,
156         const void *srcPtr, void *dstEnd)
157 {
158         BYTE *d = (BYTE *)dstPtr;
159         const BYTE *s = (const BYTE *)srcPtr;
160         BYTE *const e = (BYTE *)dstEnd;
161
162         do {
163                 LZ4_copy8(d, s);
164                 d += 8;
165                 s += 8;
166         } while (d < e);
167 }
168
169 static FORCE_INLINE unsigned int LZ4_NbCommonBytes(register size_t val)
170 {
171 #if LZ4_LITTLE_ENDIAN
172         return __ffs(val) >> 3;
173 #else
174         return (BITS_PER_LONG - 1 - __fls(val)) >> 3;
175 #endif
176 }
177
178 static FORCE_INLINE unsigned int LZ4_count(
179         const BYTE *pIn,
180         const BYTE *pMatch,
181         const BYTE *pInLimit)
182 {
183         const BYTE *const pStart = pIn;
184
185         while (likely(pIn < pInLimit - (STEPSIZE - 1))) {
186                 size_t const diff = LZ4_read_ARCH(pMatch) ^ LZ4_read_ARCH(pIn);
187
188                 if (!diff) {
189                         pIn += STEPSIZE;
190                         pMatch += STEPSIZE;
191                         continue;
192                 }
193
194                 pIn += LZ4_NbCommonBytes(diff);
195
196                 return (unsigned int)(pIn - pStart);
197         }
198
199 #if LZ4_ARCH64
200         if ((pIn < (pInLimit - 3))
201                 && (LZ4_read32(pMatch) == LZ4_read32(pIn))) {
202                 pIn += 4;
203                 pMatch += 4;
204         }
205 #endif
206
207         if ((pIn < (pInLimit - 1))
208                 && (LZ4_read16(pMatch) == LZ4_read16(pIn))) {
209                 pIn += 2;
210                 pMatch += 2;
211         }
212
213         if ((pIn < pInLimit) && (*pMatch == *pIn))
214                 pIn++;
215
216         return (unsigned int)(pIn - pStart);
217 }
218
219 typedef enum { noLimit = 0, limitedOutput = 1 } limitedOutput_directive;
220 typedef enum { byPtr, byU32, byU16 } tableType_t;
221
222 typedef enum { noDict = 0, withPrefix64k, usingExtDict } dict_directive;
223 typedef enum { noDictIssue = 0, dictSmall } dictIssue_directive;
224
225 typedef enum { endOnOutputSize = 0, endOnInputSize = 1 } endCondition_directive;
226 typedef enum { full = 0, partial = 1 } earlyEnd_directive;
227
228 #endif