Loading...
1/*
2 * lz4defs.h -- architecture specific defines
3 *
4 * Copyright (C) 2013, LG Electronics, Kyungsik Lee <kyungsik.lee@lge.com>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
9 */
10
11/*
12 * Detects 64 bits mode
13 */
14#if (defined(__x86_64__) || defined(__x86_64) || defined(__amd64__) \
15 || defined(__ppc64__) || defined(__LP64__))
16#define LZ4_ARCH64 1
17#else
18#define LZ4_ARCH64 0
19#endif
20
21/*
22 * Architecture-specific macros
23 */
24#define BYTE u8
25typedef struct _U16_S { u16 v; } U16_S;
26typedef struct _U32_S { u32 v; } U32_S;
27typedef struct _U64_S { u64 v; } U64_S;
28#if defined(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS) \
29 || defined(CONFIG_ARM) && __LINUX_ARM_ARCH__ >= 6 \
30 && defined(ARM_EFFICIENT_UNALIGNED_ACCESS)
31
32#define A16(x) (((U16_S *)(x))->v)
33#define A32(x) (((U32_S *)(x))->v)
34#define A64(x) (((U64_S *)(x))->v)
35
36#define PUT4(s, d) (A32(d) = A32(s))
37#define PUT8(s, d) (A64(d) = A64(s))
38#define LZ4_WRITE_LITTLEENDIAN_16(p, v) \
39 do { \
40 A16(p) = v; \
41 p += 2; \
42 } while (0)
43#else /* CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS */
44
45#define A64(x) get_unaligned((u64 *)&(((U16_S *)(x))->v))
46#define A32(x) get_unaligned((u32 *)&(((U16_S *)(x))->v))
47#define A16(x) get_unaligned((u16 *)&(((U16_S *)(x))->v))
48
49#define PUT4(s, d) \
50 put_unaligned(get_unaligned((const u32 *) s), (u32 *) d)
51#define PUT8(s, d) \
52 put_unaligned(get_unaligned((const u64 *) s), (u64 *) d)
53
54#define LZ4_WRITE_LITTLEENDIAN_16(p, v) \
55 do { \
56 put_unaligned(v, (u16 *)(p)); \
57 p += 2; \
58 } while (0)
59#endif
60
61#define COPYLENGTH 8
62#define ML_BITS 4
63#define ML_MASK ((1U << ML_BITS) - 1)
64#define RUN_BITS (8 - ML_BITS)
65#define RUN_MASK ((1U << RUN_BITS) - 1)
66#define MEMORY_USAGE 14
67#define MINMATCH 4
68#define SKIPSTRENGTH 6
69#define LASTLITERALS 5
70#define MFLIMIT (COPYLENGTH + MINMATCH)
71#define MINLENGTH (MFLIMIT + 1)
72#define MAXD_LOG 16
73#define MAXD (1 << MAXD_LOG)
74#define MAXD_MASK (u32)(MAXD - 1)
75#define MAX_DISTANCE (MAXD - 1)
76#define HASH_LOG (MAXD_LOG - 1)
77#define HASHTABLESIZE (1 << HASH_LOG)
78#define MAX_NB_ATTEMPTS 256
79#define OPTIMAL_ML (int)((ML_MASK-1)+MINMATCH)
80#define LZ4_64KLIMIT ((1<<16) + (MFLIMIT - 1))
81#define HASHLOG64K ((MEMORY_USAGE - 2) + 1)
82#define HASH64KTABLESIZE (1U << HASHLOG64K)
83#define LZ4_HASH_VALUE(p) (((A32(p)) * 2654435761U) >> \
84 ((MINMATCH * 8) - (MEMORY_USAGE-2)))
85#define LZ4_HASH64K_VALUE(p) (((A32(p)) * 2654435761U) >> \
86 ((MINMATCH * 8) - HASHLOG64K))
87#define HASH_VALUE(p) (((A32(p)) * 2654435761U) >> \
88 ((MINMATCH * 8) - HASH_LOG))
89
90#if LZ4_ARCH64/* 64-bit */
91#define STEPSIZE 8
92
93#define LZ4_COPYSTEP(s, d) \
94 do { \
95 PUT8(s, d); \
96 d += 8; \
97 s += 8; \
98 } while (0)
99
100#define LZ4_COPYPACKET(s, d) LZ4_COPYSTEP(s, d)
101
102#define LZ4_SECURECOPY(s, d, e) \
103 do { \
104 if (d < e) { \
105 LZ4_WILDCOPY(s, d, e); \
106 } \
107 } while (0)
108#define HTYPE u32
109
110#ifdef __BIG_ENDIAN
111#define LZ4_NBCOMMONBYTES(val) (__builtin_clzll(val) >> 3)
112#else
113#define LZ4_NBCOMMONBYTES(val) (__builtin_ctzll(val) >> 3)
114#endif
115
116#else /* 32-bit */
117#define STEPSIZE 4
118
119#define LZ4_COPYSTEP(s, d) \
120 do { \
121 PUT4(s, d); \
122 d += 4; \
123 s += 4; \
124 } while (0)
125
126#define LZ4_COPYPACKET(s, d) \
127 do { \
128 LZ4_COPYSTEP(s, d); \
129 LZ4_COPYSTEP(s, d); \
130 } while (0)
131
132#define LZ4_SECURECOPY LZ4_WILDCOPY
133#define HTYPE const u8*
134
135#ifdef __BIG_ENDIAN
136#define LZ4_NBCOMMONBYTES(val) (__builtin_clz(val) >> 3)
137#else
138#define LZ4_NBCOMMONBYTES(val) (__builtin_ctz(val) >> 3)
139#endif
140
141#endif
142
143#define LZ4_READ_LITTLEENDIAN_16(d, s, p) \
144 (d = s - get_unaligned_le16(p))
145
146#define LZ4_WILDCOPY(s, d, e) \
147 do { \
148 LZ4_COPYPACKET(s, d); \
149 } while (d < e)
150
151#define LZ4_BLINDCOPY(s, d, l) \
152 do { \
153 u8 *e = (d) + l; \
154 LZ4_WILDCOPY(s, d, e); \
155 d = e; \
156 } while (0)
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
40#include <linux/bitops.h>
41#include <linux/string.h> /* memset, memcpy */
42
43#define FORCE_INLINE __always_inline
44
45/*-************************************
46 * Basic Types
47 **************************************/
48#include <linux/types.h>
49
50typedef uint8_t BYTE;
51typedef uint16_t U16;
52typedef uint32_t U32;
53typedef int32_t S32;
54typedef uint64_t U64;
55typedef uintptr_t uptrval;
56
57/*-************************************
58 * Architecture specifics
59 **************************************/
60#if defined(CONFIG_64BIT)
61#define LZ4_ARCH64 1
62#else
63#define LZ4_ARCH64 0
64#endif
65
66#if defined(__LITTLE_ENDIAN)
67#define LZ4_LITTLE_ENDIAN 1
68#else
69#define LZ4_LITTLE_ENDIAN 0
70#endif
71
72/*-************************************
73 * Constants
74 **************************************/
75#define MINMATCH 4
76
77#define WILDCOPYLENGTH 8
78#define LASTLITERALS 5
79#define MFLIMIT (WILDCOPYLENGTH + MINMATCH)
80/*
81 * ensure it's possible to write 2 x wildcopyLength
82 * without overflowing output buffer
83 */
84#define MATCH_SAFEGUARD_DISTANCE ((2 * WILDCOPYLENGTH) - MINMATCH)
85
86/* Increase this value ==> compression run slower on incompressible data */
87#define LZ4_SKIPTRIGGER 6
88
89#define HASH_UNIT sizeof(size_t)
90
91#define KB (1 << 10)
92#define MB (1 << 20)
93#define GB (1U << 30)
94
95#define MAXD_LOG 16
96#define MAX_DISTANCE ((1 << MAXD_LOG) - 1)
97#define STEPSIZE sizeof(size_t)
98
99#define ML_BITS 4
100#define ML_MASK ((1U << ML_BITS) - 1)
101#define RUN_BITS (8 - ML_BITS)
102#define RUN_MASK ((1U << RUN_BITS) - 1)
103
104/*-************************************
105 * Reading and writing into memory
106 **************************************/
107static FORCE_INLINE U16 LZ4_read16(const void *ptr)
108{
109 return get_unaligned((const U16 *)ptr);
110}
111
112static FORCE_INLINE U32 LZ4_read32(const void *ptr)
113{
114 return get_unaligned((const U32 *)ptr);
115}
116
117static FORCE_INLINE size_t LZ4_read_ARCH(const void *ptr)
118{
119 return get_unaligned((const size_t *)ptr);
120}
121
122static FORCE_INLINE void LZ4_write16(void *memPtr, U16 value)
123{
124 put_unaligned(value, (U16 *)memPtr);
125}
126
127static FORCE_INLINE void LZ4_write32(void *memPtr, U32 value)
128{
129 put_unaligned(value, (U32 *)memPtr);
130}
131
132static FORCE_INLINE U16 LZ4_readLE16(const void *memPtr)
133{
134 return get_unaligned_le16(memPtr);
135}
136
137static FORCE_INLINE void LZ4_writeLE16(void *memPtr, U16 value)
138{
139 return put_unaligned_le16(value, memPtr);
140}
141
142/*
143 * LZ4 relies on memcpy with a constant size being inlined. In freestanding
144 * environments, the compiler can't assume the implementation of memcpy() is
145 * standard compliant, so apply its specialized memcpy() inlining logic. When
146 * possible, use __builtin_memcpy() to tell the compiler to analyze memcpy()
147 * as-if it were standard compliant, so it can inline it in freestanding
148 * environments. This is needed when decompressing the Linux Kernel, for example.
149 */
150#define LZ4_memcpy(dst, src, size) __builtin_memcpy(dst, src, size)
151#define LZ4_memmove(dst, src, size) __builtin_memmove(dst, src, size)
152
153static FORCE_INLINE void LZ4_copy8(void *dst, const void *src)
154{
155#if LZ4_ARCH64
156 U64 a = get_unaligned((const U64 *)src);
157
158 put_unaligned(a, (U64 *)dst);
159#else
160 U32 a = get_unaligned((const U32 *)src);
161 U32 b = get_unaligned((const U32 *)src + 1);
162
163 put_unaligned(a, (U32 *)dst);
164 put_unaligned(b, (U32 *)dst + 1);
165#endif
166}
167
168/*
169 * customized variant of memcpy,
170 * which can overwrite up to 7 bytes beyond dstEnd
171 */
172static FORCE_INLINE void LZ4_wildCopy(void *dstPtr,
173 const void *srcPtr, void *dstEnd)
174{
175 BYTE *d = (BYTE *)dstPtr;
176 const BYTE *s = (const BYTE *)srcPtr;
177 BYTE *const e = (BYTE *)dstEnd;
178
179 do {
180 LZ4_copy8(d, s);
181 d += 8;
182 s += 8;
183 } while (d < e);
184}
185
186static FORCE_INLINE unsigned int LZ4_NbCommonBytes(register size_t val)
187{
188#if LZ4_LITTLE_ENDIAN
189 return __ffs(val) >> 3;
190#else
191 return (BITS_PER_LONG - 1 - __fls(val)) >> 3;
192#endif
193}
194
195static FORCE_INLINE unsigned int LZ4_count(
196 const BYTE *pIn,
197 const BYTE *pMatch,
198 const BYTE *pInLimit)
199{
200 const BYTE *const pStart = pIn;
201
202 while (likely(pIn < pInLimit - (STEPSIZE - 1))) {
203 size_t const diff = LZ4_read_ARCH(pMatch) ^ LZ4_read_ARCH(pIn);
204
205 if (!diff) {
206 pIn += STEPSIZE;
207 pMatch += STEPSIZE;
208 continue;
209 }
210
211 pIn += LZ4_NbCommonBytes(diff);
212
213 return (unsigned int)(pIn - pStart);
214 }
215
216#if LZ4_ARCH64
217 if ((pIn < (pInLimit - 3))
218 && (LZ4_read32(pMatch) == LZ4_read32(pIn))) {
219 pIn += 4;
220 pMatch += 4;
221 }
222#endif
223
224 if ((pIn < (pInLimit - 1))
225 && (LZ4_read16(pMatch) == LZ4_read16(pIn))) {
226 pIn += 2;
227 pMatch += 2;
228 }
229
230 if ((pIn < pInLimit) && (*pMatch == *pIn))
231 pIn++;
232
233 return (unsigned int)(pIn - pStart);
234}
235
236typedef enum { noLimit = 0, limitedOutput = 1 } limitedOutput_directive;
237typedef enum { byPtr, byU32, byU16 } tableType_t;
238
239typedef enum { noDict = 0, withPrefix64k, usingExtDict } dict_directive;
240typedef enum { noDictIssue = 0, dictSmall } dictIssue_directive;
241
242typedef enum { endOnOutputSize = 0, endOnInputSize = 1 } endCondition_directive;
243typedef enum { decode_full_block = 0, partial_decode = 1 } earlyEnd_directive;
244
245#define LZ4_STATIC_ASSERT(c) BUILD_BUG_ON(!(c))
246
247#endif