lz4defs.h 5.8 KB

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