lz4defs.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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. * Detects 64 bits mode
  12. */
  13. #if (defined(__x86_64__) || defined(__x86_64) || defined(__amd64__) \
  14. || defined(__ppc64__) || defined(__LP64__))
  15. #define LZ4_ARCH64 1
  16. #else
  17. #define LZ4_ARCH64 0
  18. #endif
  19. /*
  20. * Architecture-specific macros
  21. */
  22. #define BYTE u8
  23. typedef struct _U16_S { u16 v; } U16_S;
  24. typedef struct _U32_S { u32 v; } U32_S;
  25. typedef struct _U64_S { u64 v; } U64_S;
  26. #if defined(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS) \
  27. || defined(CONFIG_ARM) && __LINUX_ARM_ARCH__ >= 6 \
  28. && defined(ARM_EFFICIENT_UNALIGNED_ACCESS)
  29. #define A16(x) (((U16_S *)(x))->v)
  30. #define A32(x) (((U32_S *)(x))->v)
  31. #define A64(x) (((U64_S *)(x))->v)
  32. #define PUT4(s, d) (A32(d) = A32(s))
  33. #define PUT8(s, d) (A64(d) = A64(s))
  34. #define LZ4_WRITE_LITTLEENDIAN_16(p, v) \
  35. do { \
  36. A16(p) = v; \
  37. p += 2; \
  38. } while (0)
  39. #else /* CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS */
  40. #define A64(x) get_unaligned((u64 *)&(((U16_S *)(x))->v))
  41. #define A32(x) get_unaligned((u32 *)&(((U16_S *)(x))->v))
  42. #define A16(x) get_unaligned((u16 *)&(((U16_S *)(x))->v))
  43. #define PUT4(s, d) \
  44. put_unaligned(get_unaligned((const u32 *) s), (u32 *) d)
  45. #define PUT8(s, d) \
  46. put_unaligned(get_unaligned((const u64 *) s), (u64 *) d)
  47. #define LZ4_WRITE_LITTLEENDIAN_16(p, v) \
  48. do { \
  49. put_unaligned(v, (u16 *)(p)); \
  50. p += 2; \
  51. } while (0)
  52. #endif
  53. #define COPYLENGTH 8
  54. #define ML_BITS 4
  55. #define ML_MASK ((1U << ML_BITS) - 1)
  56. #define RUN_BITS (8 - ML_BITS)
  57. #define RUN_MASK ((1U << RUN_BITS) - 1)
  58. #define MEMORY_USAGE 14
  59. #define MINMATCH 4
  60. #define SKIPSTRENGTH 6
  61. #define LASTLITERALS 5
  62. #define MFLIMIT (COPYLENGTH + MINMATCH)
  63. #define MINLENGTH (MFLIMIT + 1)
  64. #define MAXD_LOG 16
  65. #define MAXD (1 << MAXD_LOG)
  66. #define MAXD_MASK (u32)(MAXD - 1)
  67. #define MAX_DISTANCE (MAXD - 1)
  68. #define HASH_LOG (MAXD_LOG - 1)
  69. #define HASHTABLESIZE (1 << HASH_LOG)
  70. #define MAX_NB_ATTEMPTS 256
  71. #define OPTIMAL_ML (int)((ML_MASK-1)+MINMATCH)
  72. #define LZ4_64KLIMIT ((1<<16) + (MFLIMIT - 1))
  73. #define HASHLOG64K ((MEMORY_USAGE - 2) + 1)
  74. #define HASH64KTABLESIZE (1U << HASHLOG64K)
  75. #define LZ4_HASH_VALUE(p) (((A32(p)) * 2654435761U) >> \
  76. ((MINMATCH * 8) - (MEMORY_USAGE-2)))
  77. #define LZ4_HASH64K_VALUE(p) (((A32(p)) * 2654435761U) >> \
  78. ((MINMATCH * 8) - HASHLOG64K))
  79. #define HASH_VALUE(p) (((A32(p)) * 2654435761U) >> \
  80. ((MINMATCH * 8) - HASH_LOG))
  81. #if LZ4_ARCH64/* 64-bit */
  82. #define STEPSIZE 8
  83. #define LZ4_COPYSTEP(s, d) \
  84. do { \
  85. PUT8(s, d); \
  86. d += 8; \
  87. s += 8; \
  88. } while (0)
  89. #define LZ4_COPYPACKET(s, d) LZ4_COPYSTEP(s, d)
  90. #define LZ4_SECURECOPY(s, d, e) \
  91. do { \
  92. if (d < e) { \
  93. LZ4_WILDCOPY(s, d, e); \
  94. } \
  95. } while (0)
  96. #define HTYPE u32
  97. #ifdef __BIG_ENDIAN
  98. #define LZ4_NBCOMMONBYTES(val) (__builtin_clzll(val) >> 3)
  99. #else
  100. #define LZ4_NBCOMMONBYTES(val) (__builtin_ctzll(val) >> 3)
  101. #endif
  102. #else /* 32-bit */
  103. #define STEPSIZE 4
  104. #define LZ4_COPYSTEP(s, d) \
  105. do { \
  106. PUT4(s, d); \
  107. d += 4; \
  108. s += 4; \
  109. } while (0)
  110. #define LZ4_COPYPACKET(s, d) \
  111. do { \
  112. LZ4_COPYSTEP(s, d); \
  113. LZ4_COPYSTEP(s, d); \
  114. } while (0)
  115. #define LZ4_SECURECOPY LZ4_WILDCOPY
  116. #define HTYPE const u8*
  117. #ifdef __BIG_ENDIAN
  118. #define LZ4_NBCOMMONBYTES(val) (__builtin_clz(val) >> 3)
  119. #else
  120. #define LZ4_NBCOMMONBYTES(val) (__builtin_ctz(val) >> 3)
  121. #endif
  122. #endif
  123. #define LZ4_READ_LITTLEENDIAN_16(d, s, p) \
  124. (d = s - get_unaligned_le16(p))
  125. #define LZ4_WILDCOPY(s, d, e) \
  126. do { \
  127. LZ4_COPYPACKET(s, d); \
  128. } while (d < e)
  129. #define LZ4_BLINDCOPY(s, d, l) \
  130. do { \
  131. u8 *e = (d) + l; \
  132. LZ4_WILDCOPY(s, d, e); \
  133. d = e; \
  134. } while (0)