lzham_core.h 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. // File: lzham_core.h
  2. // See Copyright Notice and license at the end of include/lzham.h
  3. #pragma once
  4. #if defined(_MSC_VER)
  5. #pragma warning (disable: 4127) // conditional expression is constant
  6. #endif
  7. // Enable this when first porting to new platforms - disables all threading and atomic ops in compressor:
  8. //#define LZHAM_ANSI_CPLUSPLUS 1
  9. #if defined(__FreeBSD__) || defined(__NetBSD__)
  10. // TODO: I haven't compiled/tested on these platforms yet, let's play it safe for now.
  11. #define LZHAM_ANSI_CPLUSPLUS 1
  12. #endif
  13. #if defined(__APPLE__) && defined(__MACH__)
  14. // Apple OSX and iOS
  15. #include <TargetConditionals.h>
  16. #endif
  17. #if defined(_XBOX) && !defined(LZHAM_ANSI_CPLUSPLUS)
  18. // --- X360 - This hasn't been tested since early an alpha.
  19. #include <xtl.h>
  20. #define _HAS_EXCEPTIONS 0
  21. #define NOMINMAX
  22. #define LZHAM_PLATFORM_X360 1
  23. #define LZHAM_USE_WIN32_API 1
  24. #define LZHAM_USE_WIN32_ATOMIC_FUNCTIONS 1
  25. #define LZHAM_64BIT_POINTERS 0
  26. #define LZHAM_CPU_HAS_64BIT_REGISTERS 1
  27. #define LZHAM_BIG_ENDIAN_CPU 1
  28. #define LZHAM_USE_UNALIGNED_INT_LOADS 1
  29. #define LZHAM_RESTRICT __restrict
  30. #define LZHAM_FORCE_INLINE __forceinline
  31. #define LZHAM_NOTE_UNUSED(x) (void)x
  32. #elif defined(WIN32) && !defined(LZHAM_ANSI_CPLUSPLUS)
  33. // --- Windows: MSVC or MinGW, x86 or x64, Win32 API's for threading and Win32 Interlocked API's or GCC built-ins for atomic ops.
  34. #ifdef NDEBUG
  35. // Ensure checked iterators are disabled.
  36. #define _SECURE_SCL 0
  37. #define _HAS_ITERATOR_DEBUGGING 0
  38. #endif
  39. #ifndef _DLL
  40. // If we're using the DLL form of the run-time libs, we're also going to be enabling exceptions because we'll be building CLR apps.
  41. // Otherwise, we disable exceptions for a small speed boost.
  42. #define _HAS_EXCEPTIONS 0
  43. #endif
  44. #define NOMINMAX
  45. #ifndef _WIN32_WINNT
  46. #define _WIN32_WINNT 0x500
  47. #endif
  48. #ifndef WIN32_LEAN_AND_MEAN
  49. #define WIN32_LEAN_AND_MEAN
  50. #endif
  51. #include <windows.h>
  52. #define LZHAM_USE_WIN32_API 1
  53. #if defined(__MINGW32__) || defined(__MINGW64__)
  54. #define LZHAM_USE_GCC_ATOMIC_BUILTINS 1
  55. #else
  56. #define LZHAM_USE_WIN32_ATOMIC_FUNCTIONS 1
  57. #endif
  58. #define LZHAM_PLATFORM_PC 1
  59. #ifdef _WIN64
  60. #define LZHAM_PLATFORM_PC_X64 1
  61. #define LZHAM_64BIT_POINTERS 1
  62. #define LZHAM_CPU_HAS_64BIT_REGISTERS 1
  63. #define LZHAM_LITTLE_ENDIAN_CPU 1
  64. #else
  65. #define LZHAM_PLATFORM_PC_X86 1
  66. #define LZHAM_64BIT_POINTERS 0
  67. #define LZHAM_CPU_HAS_64BIT_REGISTERS 0
  68. #define LZHAM_LITTLE_ENDIAN_CPU 1
  69. #endif
  70. #define LZHAM_USE_UNALIGNED_INT_LOADS 1
  71. #define LZHAM_RESTRICT __restrict
  72. #define LZHAM_FORCE_INLINE __forceinline
  73. #if defined(_MSC_VER) || defined(__MINGW32__) || defined(__MINGW64__)
  74. #define LZHAM_USE_MSVC_INTRINSICS 1
  75. #endif
  76. #define LZHAM_NOTE_UNUSED(x) (void)x
  77. #elif defined ANDROID // ESENTHEL CHANGED
  78. #define LZHAM_PLATFORM_PC 0
  79. #if defined(_WIN64) || defined(__MINGW64__) || defined(_LP64) || defined(__LP64__)
  80. #define LZHAM_PLATFORM_PC_X64 0
  81. #define LZHAM_64BIT_POINTERS 1
  82. #define LZHAM_CPU_HAS_64BIT_REGISTERS 1
  83. #else
  84. #define LZHAM_PLATFORM_PC_X86 0
  85. #define LZHAM_64BIT_POINTERS 0
  86. #define LZHAM_CPU_HAS_64BIT_REGISTERS 0
  87. #endif
  88. #define LZHAM_USE_UNALIGNED_INT_LOADS 0
  89. #if __BIG_ENDIAN__
  90. #define LZHAM_BIG_ENDIAN_CPU 1
  91. #else
  92. #define LZHAM_LITTLE_ENDIAN_CPU 1
  93. #endif
  94. #define LZHAM_USE_PTHREADS_API 0
  95. #define LZHAM_USE_GCC_ATOMIC_BUILTINS 1
  96. #define LZHAM_RESTRICT
  97. #if defined(__clang__)
  98. #define LZHAM_FORCE_INLINE inline
  99. #else
  100. #define LZHAM_FORCE_INLINE inline __attribute__((__always_inline__,__gnu_inline__))
  101. #endif
  102. #define LZHAM_NOTE_UNUSED(x) (void)x
  103. #elif defined(__APPLE__) && !defined(LZHAM_ANSI_CPLUSPLUS)
  104. // --- Apple: iOS or OSX
  105. #if (TARGET_IPHONE_SIMULATOR == 1) || (TARGET_OS_IPHONE == 1)
  106. #define LZHAM_PLATFORM_PC 0
  107. #if defined(_WIN64) || defined(__MINGW64__) || defined(_LP64) || defined(__LP64__)
  108. #define LZHAM_PLATFORM_PC_X64 0
  109. #define LZHAM_64BIT_POINTERS 1
  110. #define LZHAM_CPU_HAS_64BIT_REGISTERS 1
  111. #else
  112. #define LZHAM_PLATFORM_PC_X86 0
  113. #define LZHAM_64BIT_POINTERS 0
  114. #define LZHAM_CPU_HAS_64BIT_REGISTERS 0
  115. #endif
  116. #define LZHAM_USE_UNALIGNED_INT_LOADS 0
  117. #if __BIG_ENDIAN__
  118. #define LZHAM_BIG_ENDIAN_CPU 1
  119. #else
  120. #define LZHAM_LITTLE_ENDIAN_CPU 1
  121. #endif
  122. #define LZHAM_USE_PTHREADS_API 1
  123. #define LZHAM_USE_GCC_ATOMIC_BUILTINS 1
  124. #define LZHAM_RESTRICT
  125. #if defined(__clang__)
  126. #define LZHAM_FORCE_INLINE inline
  127. #else
  128. #define LZHAM_FORCE_INLINE inline __attribute__((__always_inline__,__gnu_inline__))
  129. #endif
  130. #define LZHAM_NOTE_UNUSED(x) (void)x
  131. #elif (TARGET_OS_MAC == 1)
  132. #define LZHAM_PLATFORM_PC 1
  133. #if defined(_WIN64) || defined(__MINGW64__) || defined(_LP64) || defined(__LP64__)
  134. #define LZHAM_PLATFORM_PC_X64 1
  135. #define LZHAM_64BIT_POINTERS 1
  136. #define LZHAM_CPU_HAS_64BIT_REGISTERS 1
  137. #else
  138. #define LZHAM_PLATFORM_PC_X86 1
  139. #define LZHAM_64BIT_POINTERS 0
  140. #define LZHAM_CPU_HAS_64BIT_REGISTERS 0
  141. #endif
  142. #define LZHAM_USE_UNALIGNED_INT_LOADS 1
  143. #if __BIG_ENDIAN__
  144. #define LZHAM_BIG_ENDIAN_CPU 1
  145. #else
  146. #define LZHAM_LITTLE_ENDIAN_CPU 1
  147. #endif
  148. #define LZHAM_USE_PTHREADS_API 1
  149. #define LZHAM_USE_GCC_ATOMIC_BUILTINS 1
  150. #define LZHAM_RESTRICT
  151. #if defined(__clang__)
  152. #define LZHAM_FORCE_INLINE inline
  153. #else
  154. #define LZHAM_FORCE_INLINE inline __attribute__((__always_inline__,__gnu_inline__))
  155. #endif
  156. #define LZHAM_NOTE_UNUSED(x) (void)x
  157. #elif
  158. #error TODO: Unknown Apple target
  159. #endif
  160. #elif defined(__linux__) && (defined(__i386__) || defined(__x86_64__)) && !defined(LZHAM_ANSI_CPLUSPLUS)
  161. // --- Generic GCC/clang path for x86/x64, clang or GCC, Linux, OSX, FreeBSD or NetBSD, pthreads for threading, GCC built-ins for atomic ops.
  162. #define LZHAM_PLATFORM_PC 1
  163. #if defined(_LP64) || defined(__LP64__) || defined(__x86_64__)
  164. // 64-bit build assumes pointers are always 64-bit
  165. #define LZHAM_PLATFORM_PC_X64 1
  166. #define LZHAM_64BIT_POINTERS 1
  167. #define LZHAM_CPU_HAS_64BIT_REGISTERS 1
  168. #else
  169. #define LZHAM_PLATFORM_PC_X86 1
  170. #define LZHAM_64BIT_POINTERS 0
  171. #define LZHAM_CPU_HAS_64BIT_REGISTERS 0
  172. #endif
  173. #define LZHAM_USE_UNALIGNED_INT_LOADS 1
  174. #if __BIG_ENDIAN__
  175. #define LZHAM_BIG_ENDIAN_CPU 1
  176. #else
  177. #define LZHAM_LITTLE_ENDIAN_CPU 1
  178. #endif
  179. #define LZHAM_USE_PTHREADS_API 1
  180. #define LZHAM_USE_GCC_ATOMIC_BUILTINS 1
  181. #define LZHAM_RESTRICT
  182. #if defined(__clang__)
  183. #define LZHAM_FORCE_INLINE inline
  184. #else
  185. #define LZHAM_FORCE_INLINE inline __attribute__((__always_inline__,__gnu_inline__))
  186. #endif
  187. #define LZHAM_NOTE_UNUSED(x) (void)x
  188. #else
  189. #warning Building as vanilla ANSI-C/C++, multi-threaded compression is disabled! Please configure lzhamdecomp/lzham_core.h.
  190. // --- Vanilla ANSI-C/C++
  191. // No threading support, unaligned loads are NOT okay, no atomic ops.
  192. #if defined(_WIN64) || defined(__MINGW64__) || defined(_LP64) || defined(__LP64__)
  193. #define LZHAM_64BIT_POINTERS 1
  194. #define LZHAM_CPU_HAS_64BIT_REGISTERS 1
  195. #else
  196. #define LZHAM_64BIT_POINTERS 0
  197. #define LZHAM_CPU_HAS_64BIT_REGISTERS 0
  198. #endif
  199. #define LZHAM_USE_UNALIGNED_INT_LOADS 0
  200. #if __BIG_ENDIAN__
  201. #define LZHAM_BIG_ENDIAN_CPU 1
  202. #else
  203. #define LZHAM_LITTLE_ENDIAN_CPU 1
  204. #endif
  205. #define LZHAM_USE_GCC_ATOMIC_BUILTINS 0
  206. #define LZHAM_USE_WIN32_ATOMIC_FUNCTIONS 0
  207. #define LZHAM_RESTRICT
  208. #define LZHAM_FORCE_INLINE inline
  209. #define LZHAM_NOTE_UNUSED(x) (void)x
  210. #endif
  211. #if LZHAM_LITTLE_ENDIAN_CPU
  212. const bool c_lzham_little_endian_platform = true;
  213. #else
  214. const bool c_lzham_little_endian_platform = false;
  215. #endif
  216. const bool c_lzham_big_endian_platform = !c_lzham_little_endian_platform;
  217. #include <stdlib.h>
  218. #include <stdio.h>
  219. #include <math.h>
  220. #if !defined(__APPLE__) && !defined(__FreeBSD__)
  221. #include <malloc.h>
  222. #endif
  223. #include <stdarg.h>
  224. #include <memory.h>
  225. #include <limits.h>
  226. #include <algorithm>
  227. #include <errno.h>
  228. #include "lzham.h"
  229. #include "lzham_config.h"
  230. #include "lzham_types.h"
  231. #include "lzham_assert.h"
  232. #include "lzham_platform.h"
  233. #include "lzham_helpers.h"
  234. #include "lzham_traits.h"
  235. #include "lzham_mem.h"
  236. #include "lzham_math.h"
  237. #include "lzham_utils.h"
  238. #include "lzham_vector.h"