CpuArch.h 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686
  1. /* CpuArch.h -- CPU specific code
  2. Igor Pavlov : Public domain */
  3. #ifndef ZIP7_INC_CPU_ARCH_H
  4. #define ZIP7_INC_CPU_ARCH_H
  5. #include "7zTypes.h"
  6. EXTERN_C_BEGIN
  7. /*
  8. MY_CPU_LE means that CPU is LITTLE ENDIAN.
  9. MY_CPU_BE means that CPU is BIG ENDIAN.
  10. If MY_CPU_LE and MY_CPU_BE are not defined, we don't know about ENDIANNESS of platform.
  11. MY_CPU_LE_UNALIGN means that CPU is LITTLE ENDIAN and CPU supports unaligned memory accesses.
  12. MY_CPU_64BIT means that processor can work with 64-bit registers.
  13. MY_CPU_64BIT can be used to select fast code branch
  14. MY_CPU_64BIT doesn't mean that (sizeof(void *) == 8)
  15. */
  16. #if !defined(_M_ARM64EC)
  17. #if defined(_M_X64) \
  18. || defined(_M_AMD64) \
  19. || defined(__x86_64__) \
  20. || defined(__AMD64__) \
  21. || defined(__amd64__)
  22. #define MY_CPU_AMD64
  23. #ifdef __ILP32__
  24. #define MY_CPU_NAME "x32"
  25. #define MY_CPU_SIZEOF_POINTER 4
  26. #else
  27. #define MY_CPU_NAME "x64"
  28. #define MY_CPU_SIZEOF_POINTER 8
  29. #endif
  30. #define MY_CPU_64BIT
  31. #endif
  32. #endif
  33. #if defined(_M_IX86) \
  34. || defined(__i386__)
  35. #define MY_CPU_X86
  36. #define MY_CPU_NAME "x86"
  37. /* #define MY_CPU_32BIT */
  38. #define MY_CPU_SIZEOF_POINTER 4
  39. #endif
  40. #if defined(__SSE2__) \
  41. || defined(MY_CPU_AMD64) \
  42. || defined(_M_IX86_FP) && (_M_IX86_FP >= 2)
  43. #define MY_CPU_SSE2
  44. #endif
  45. #if defined(_M_ARM64) \
  46. || defined(_M_ARM64EC) \
  47. || defined(__AARCH64EL__) \
  48. || defined(__AARCH64EB__) \
  49. || defined(__aarch64__)
  50. #define MY_CPU_ARM64
  51. #if defined(__ILP32__) \
  52. || defined(__SIZEOF_POINTER__) && (__SIZEOF_POINTER__ == 4)
  53. #define MY_CPU_NAME "arm64-32"
  54. #define MY_CPU_SIZEOF_POINTER 4
  55. #elif defined(__SIZEOF_POINTER__) && (__SIZEOF_POINTER__ == 16)
  56. #define MY_CPU_NAME "arm64-128"
  57. #define MY_CPU_SIZEOF_POINTER 16
  58. #else
  59. #if defined(_M_ARM64EC)
  60. #define MY_CPU_NAME "arm64ec"
  61. #else
  62. #define MY_CPU_NAME "arm64"
  63. #endif
  64. #define MY_CPU_SIZEOF_POINTER 8
  65. #endif
  66. #define MY_CPU_64BIT
  67. #endif
  68. #if defined(_M_ARM) \
  69. || defined(_M_ARM_NT) \
  70. || defined(_M_ARMT) \
  71. || defined(__arm__) \
  72. || defined(__thumb__) \
  73. || defined(__ARMEL__) \
  74. || defined(__ARMEB__) \
  75. || defined(__THUMBEL__) \
  76. || defined(__THUMBEB__)
  77. #define MY_CPU_ARM
  78. #if defined(__thumb__) || defined(__THUMBEL__) || defined(_M_ARMT)
  79. #define MY_CPU_ARMT
  80. #define MY_CPU_NAME "armt"
  81. #else
  82. #define MY_CPU_ARM32
  83. #define MY_CPU_NAME "arm"
  84. #endif
  85. /* #define MY_CPU_32BIT */
  86. #define MY_CPU_SIZEOF_POINTER 4
  87. #endif
  88. #if defined(_M_IA64) \
  89. || defined(__ia64__)
  90. #define MY_CPU_IA64
  91. #define MY_CPU_NAME "ia64"
  92. #define MY_CPU_64BIT
  93. #endif
  94. #if defined(__mips64) \
  95. || defined(__mips64__) \
  96. || (defined(__mips) && (__mips == 64 || __mips == 4 || __mips == 3))
  97. #define MY_CPU_NAME "mips64"
  98. #define MY_CPU_64BIT
  99. #elif defined(__mips__)
  100. #define MY_CPU_NAME "mips"
  101. /* #define MY_CPU_32BIT */
  102. #endif
  103. #if defined(__ppc64__) \
  104. || defined(__powerpc64__) \
  105. || defined(__ppc__) \
  106. || defined(__powerpc__) \
  107. || defined(__PPC__) \
  108. || defined(_POWER)
  109. #define MY_CPU_PPC_OR_PPC64
  110. #if defined(__ppc64__) \
  111. || defined(__powerpc64__) \
  112. || defined(_LP64) \
  113. || defined(__64BIT__)
  114. #ifdef __ILP32__
  115. #define MY_CPU_NAME "ppc64-32"
  116. #define MY_CPU_SIZEOF_POINTER 4
  117. #else
  118. #define MY_CPU_NAME "ppc64"
  119. #define MY_CPU_SIZEOF_POINTER 8
  120. #endif
  121. #define MY_CPU_64BIT
  122. #else
  123. #define MY_CPU_NAME "ppc"
  124. #define MY_CPU_SIZEOF_POINTER 4
  125. /* #define MY_CPU_32BIT */
  126. #endif
  127. #endif
  128. #if defined(__sparc__) \
  129. || defined(__sparc)
  130. #define MY_CPU_SPARC
  131. #if defined(__LP64__) \
  132. || defined(_LP64) \
  133. || defined(__SIZEOF_POINTER__) && (__SIZEOF_POINTER__ == 8)
  134. #define MY_CPU_NAME "sparcv9"
  135. #define MY_CPU_SIZEOF_POINTER 8
  136. #define MY_CPU_64BIT
  137. #elif defined(__sparc_v9__) \
  138. || defined(__sparcv9)
  139. #define MY_CPU_64BIT
  140. #if defined(__SIZEOF_POINTER__) && (__SIZEOF_POINTER__ == 4)
  141. #define MY_CPU_NAME "sparcv9-32"
  142. #else
  143. #define MY_CPU_NAME "sparcv9m"
  144. #endif
  145. #elif defined(__sparc_v8__) \
  146. || defined(__sparcv8)
  147. #define MY_CPU_NAME "sparcv8"
  148. #define MY_CPU_SIZEOF_POINTER 4
  149. #else
  150. #define MY_CPU_NAME "sparc"
  151. #endif
  152. #endif
  153. #if defined(__riscv) \
  154. || defined(__riscv__)
  155. #define MY_CPU_RISCV
  156. #if __riscv_xlen == 32
  157. #define MY_CPU_NAME "riscv32"
  158. #elif __riscv_xlen == 64
  159. #define MY_CPU_NAME "riscv64"
  160. #else
  161. #define MY_CPU_NAME "riscv"
  162. #endif
  163. #endif
  164. #if defined(__loongarch__)
  165. #define MY_CPU_LOONGARCH
  166. #if defined(__loongarch64) || defined(__loongarch_grlen) && (__loongarch_grlen == 64)
  167. #define MY_CPU_64BIT
  168. #endif
  169. #if defined(__loongarch64)
  170. #define MY_CPU_NAME "loongarch64"
  171. #define MY_CPU_LOONGARCH64
  172. #else
  173. #define MY_CPU_NAME "loongarch"
  174. #endif
  175. #endif
  176. // #undef MY_CPU_NAME
  177. // #undef MY_CPU_SIZEOF_POINTER
  178. // #define __e2k__
  179. // #define __SIZEOF_POINTER__ 4
  180. #if defined(__e2k__)
  181. #define MY_CPU_E2K
  182. #if defined(__ILP32__) || defined(__SIZEOF_POINTER__) && (__SIZEOF_POINTER__ == 4)
  183. #define MY_CPU_NAME "e2k-32"
  184. #define MY_CPU_SIZEOF_POINTER 4
  185. #else
  186. #define MY_CPU_NAME "e2k"
  187. #if defined(__LP64__) || defined(__SIZEOF_POINTER__) && (__SIZEOF_POINTER__ == 8)
  188. #define MY_CPU_SIZEOF_POINTER 8
  189. #endif
  190. #endif
  191. #define MY_CPU_64BIT
  192. #endif
  193. #if defined(MY_CPU_X86) || defined(MY_CPU_AMD64)
  194. #define MY_CPU_X86_OR_AMD64
  195. #endif
  196. #if defined(MY_CPU_ARM) || defined(MY_CPU_ARM64)
  197. #define MY_CPU_ARM_OR_ARM64
  198. #endif
  199. #ifdef _WIN32
  200. #ifdef MY_CPU_ARM
  201. #define MY_CPU_ARM_LE
  202. #endif
  203. #ifdef MY_CPU_ARM64
  204. #define MY_CPU_ARM64_LE
  205. #endif
  206. #ifdef _M_IA64
  207. #define MY_CPU_IA64_LE
  208. #endif
  209. #endif
  210. #if defined(MY_CPU_X86_OR_AMD64) \
  211. || defined(MY_CPU_ARM_LE) \
  212. || defined(MY_CPU_ARM64_LE) \
  213. || defined(MY_CPU_IA64_LE) \
  214. || defined(_LITTLE_ENDIAN) \
  215. || defined(__LITTLE_ENDIAN__) \
  216. || defined(__ARMEL__) \
  217. || defined(__THUMBEL__) \
  218. || defined(__AARCH64EL__) \
  219. || defined(__MIPSEL__) \
  220. || defined(__MIPSEL) \
  221. || defined(_MIPSEL) \
  222. || defined(__BFIN__) \
  223. || (defined(__BYTE_ORDER__) && (__BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__))
  224. #define MY_CPU_LE
  225. #endif
  226. #if defined(__BIG_ENDIAN__) \
  227. || defined(__ARMEB__) \
  228. || defined(__THUMBEB__) \
  229. || defined(__AARCH64EB__) \
  230. || defined(__MIPSEB__) \
  231. || defined(__MIPSEB) \
  232. || defined(_MIPSEB) \
  233. || defined(__m68k__) \
  234. || defined(__s390__) \
  235. || defined(__s390x__) \
  236. || defined(__zarch__) \
  237. || (defined(__BYTE_ORDER__) && (__BYTE_ORDER__ == __ORDER_BIG_ENDIAN__))
  238. #define MY_CPU_BE
  239. #endif
  240. #if defined(MY_CPU_LE) && defined(MY_CPU_BE)
  241. #error Stop_Compiling_Bad_Endian
  242. #endif
  243. #if !defined(MY_CPU_LE) && !defined(MY_CPU_BE)
  244. #error Stop_Compiling_CPU_ENDIAN_must_be_detected_at_compile_time
  245. #endif
  246. #if defined(MY_CPU_32BIT) && defined(MY_CPU_64BIT)
  247. #error Stop_Compiling_Bad_32_64_BIT
  248. #endif
  249. #ifdef __SIZEOF_POINTER__
  250. #ifdef MY_CPU_SIZEOF_POINTER
  251. #if MY_CPU_SIZEOF_POINTER != __SIZEOF_POINTER__
  252. #error Stop_Compiling_Bad_MY_CPU_PTR_SIZE
  253. #endif
  254. #else
  255. #define MY_CPU_SIZEOF_POINTER __SIZEOF_POINTER__
  256. #endif
  257. #endif
  258. #if defined(MY_CPU_SIZEOF_POINTER) && (MY_CPU_SIZEOF_POINTER == 4)
  259. #if defined (_LP64)
  260. #error Stop_Compiling_Bad_MY_CPU_PTR_SIZE
  261. #endif
  262. #endif
  263. #ifdef _MSC_VER
  264. #if _MSC_VER >= 1300
  265. #define MY_CPU_pragma_pack_push_1 __pragma(pack(push, 1))
  266. #define MY_CPU_pragma_pop __pragma(pack(pop))
  267. #else
  268. #define MY_CPU_pragma_pack_push_1
  269. #define MY_CPU_pragma_pop
  270. #endif
  271. #else
  272. #ifdef __xlC__
  273. #define MY_CPU_pragma_pack_push_1 _Pragma("pack(1)")
  274. #define MY_CPU_pragma_pop _Pragma("pack()")
  275. #else
  276. #define MY_CPU_pragma_pack_push_1 _Pragma("pack(push, 1)")
  277. #define MY_CPU_pragma_pop _Pragma("pack(pop)")
  278. #endif
  279. #endif
  280. #ifndef MY_CPU_NAME
  281. // #define MY_CPU_IS_UNKNOWN
  282. #ifdef MY_CPU_LE
  283. #define MY_CPU_NAME "LE"
  284. #elif defined(MY_CPU_BE)
  285. #define MY_CPU_NAME "BE"
  286. #else
  287. /*
  288. #define MY_CPU_NAME ""
  289. */
  290. #endif
  291. #endif
  292. #ifdef __has_builtin
  293. #define Z7_has_builtin(x) __has_builtin(x)
  294. #else
  295. #define Z7_has_builtin(x) 0
  296. #endif
  297. #define Z7_BSWAP32_CONST(v) \
  298. ( (((UInt32)(v) << 24) ) \
  299. | (((UInt32)(v) << 8) & (UInt32)0xff0000) \
  300. | (((UInt32)(v) >> 8) & (UInt32)0xff00 ) \
  301. | (((UInt32)(v) >> 24) ))
  302. #if defined(_MSC_VER) && (_MSC_VER >= 1300)
  303. #include <stdlib.h>
  304. /* Note: these macros will use bswap instruction (486), that is unsupported in 386 cpu */
  305. #pragma intrinsic(_byteswap_ushort)
  306. #pragma intrinsic(_byteswap_ulong)
  307. #pragma intrinsic(_byteswap_uint64)
  308. #define Z7_BSWAP16(v) _byteswap_ushort(v)
  309. #define Z7_BSWAP32(v) _byteswap_ulong (v)
  310. #define Z7_BSWAP64(v) _byteswap_uint64(v)
  311. #define Z7_CPU_FAST_BSWAP_SUPPORTED
  312. /* GCC can generate slow code that calls function for __builtin_bswap32() for:
  313. - GCC for RISCV, if Zbb/XTHeadBb extension is not used.
  314. - GCC for SPARC.
  315. The code from CLANG for SPARC also is not fastest.
  316. So we don't define Z7_CPU_FAST_BSWAP_SUPPORTED in some cases.
  317. */
  318. #elif (!defined(MY_CPU_RISCV) || defined (__riscv_zbb) || defined(__riscv_xtheadbb)) \
  319. && !defined(MY_CPU_SPARC) \
  320. && ( \
  321. (defined(__GNUC__) && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 3))) \
  322. || (defined(__clang__) && Z7_has_builtin(__builtin_bswap16)) \
  323. )
  324. #define Z7_BSWAP16(v) __builtin_bswap16(v)
  325. #define Z7_BSWAP32(v) __builtin_bswap32(v)
  326. #define Z7_BSWAP64(v) __builtin_bswap64(v)
  327. #define Z7_CPU_FAST_BSWAP_SUPPORTED
  328. #else
  329. #define Z7_BSWAP16(v) ((UInt16) \
  330. ( ((UInt32)(v) << 8) \
  331. | ((UInt32)(v) >> 8) \
  332. ))
  333. #define Z7_BSWAP32(v) Z7_BSWAP32_CONST(v)
  334. #define Z7_BSWAP64(v) \
  335. ( ( ( (UInt64)(v) ) << 8 * 7 ) \
  336. | ( ( (UInt64)(v) & ((UInt32)0xff << 8 * 1) ) << 8 * 5 ) \
  337. | ( ( (UInt64)(v) & ((UInt32)0xff << 8 * 2) ) << 8 * 3 ) \
  338. | ( ( (UInt64)(v) & ((UInt32)0xff << 8 * 3) ) << 8 * 1 ) \
  339. | ( ( (UInt64)(v) >> 8 * 1 ) & ((UInt32)0xff << 8 * 3) ) \
  340. | ( ( (UInt64)(v) >> 8 * 3 ) & ((UInt32)0xff << 8 * 2) ) \
  341. | ( ( (UInt64)(v) >> 8 * 5 ) & ((UInt32)0xff << 8 * 1) ) \
  342. | ( ( (UInt64)(v) >> 8 * 7 ) ) \
  343. )
  344. #endif
  345. #ifdef MY_CPU_LE
  346. #if defined(MY_CPU_X86_OR_AMD64) \
  347. || defined(MY_CPU_ARM64) \
  348. || defined(MY_CPU_RISCV) && defined(__riscv_misaligned_fast) \
  349. || defined(MY_CPU_E2K) && defined(__iset__) && (__iset__ >= 6)
  350. #define MY_CPU_LE_UNALIGN
  351. #define MY_CPU_LE_UNALIGN_64
  352. #elif defined(__ARM_FEATURE_UNALIGNED)
  353. /* === ALIGNMENT on 32-bit arm and LDRD/STRD/LDM/STM instructions.
  354. Description of problems:
  355. problem-1 : 32-bit ARM architecture:
  356. multi-access (pair of 32-bit accesses) instructions (LDRD/STRD/LDM/STM)
  357. require 32-bit (WORD) alignment (by 32-bit ARM architecture).
  358. So there is "Alignment fault exception", if data is not aligned for 32-bit.
  359. problem-2 : 32-bit kernels and arm64 kernels:
  360. 32-bit linux kernels provide fixup for these "paired" instruction "Alignment fault exception".
  361. So unaligned paired-access instructions work via exception handler in kernel in 32-bit linux.
  362. But some arm64 kernels do not handle these faults in 32-bit programs.
  363. So we have unhandled exception for such instructions.
  364. Probably some new arm64 kernels have fixed it, and unaligned
  365. paired-access instructions work in new kernels?
  366. problem-3 : compiler for 32-bit arm:
  367. Compilers use LDRD/STRD/LDM/STM for UInt64 accesses
  368. and for another cases where two 32-bit accesses are fused
  369. to one multi-access instruction.
  370. So UInt64 variables must be aligned for 32-bit, and each
  371. 32-bit access must be aligned for 32-bit, if we want to
  372. avoid "Alignment fault" exception (handled or unhandled).
  373. problem-4 : performace:
  374. Even if unaligned access is handled by kernel, it will be slow.
  375. So if we allow unaligned access, we can get fast unaligned
  376. single-access, and slow unaligned paired-access.
  377. We don't allow unaligned access on 32-bit arm, because compiler
  378. genarates paired-access instructions that require 32-bit alignment,
  379. and some arm64 kernels have no handler for these instructions.
  380. Also unaligned paired-access instructions will be slow, if kernel handles them.
  381. */
  382. // it must be disabled:
  383. // #define MY_CPU_LE_UNALIGN
  384. #endif
  385. #endif
  386. #ifdef MY_CPU_LE_UNALIGN
  387. #define GetUi16(p) (*(const UInt16 *)(const void *)(p))
  388. #define GetUi32(p) (*(const UInt32 *)(const void *)(p))
  389. #ifdef MY_CPU_LE_UNALIGN_64
  390. #define GetUi64(p) (*(const UInt64 *)(const void *)(p))
  391. #define SetUi64(p, v) { *(UInt64 *)(void *)(p) = (v); }
  392. #endif
  393. #define SetUi16(p, v) { *(UInt16 *)(void *)(p) = (v); }
  394. #define SetUi32(p, v) { *(UInt32 *)(void *)(p) = (v); }
  395. #else
  396. #define GetUi16(p) ( (UInt16) ( \
  397. ((const Byte *)(p))[0] | \
  398. ((UInt16)((const Byte *)(p))[1] << 8) ))
  399. #define GetUi32(p) ( \
  400. ((const Byte *)(p))[0] | \
  401. ((UInt32)((const Byte *)(p))[1] << 8) | \
  402. ((UInt32)((const Byte *)(p))[2] << 16) | \
  403. ((UInt32)((const Byte *)(p))[3] << 24))
  404. #define SetUi16(p, v) { Byte *_ppp_ = (Byte *)(p); UInt32 _vvv_ = (v); \
  405. _ppp_[0] = (Byte)_vvv_; \
  406. _ppp_[1] = (Byte)(_vvv_ >> 8); }
  407. #define SetUi32(p, v) { Byte *_ppp_ = (Byte *)(p); UInt32 _vvv_ = (v); \
  408. _ppp_[0] = (Byte)_vvv_; \
  409. _ppp_[1] = (Byte)(_vvv_ >> 8); \
  410. _ppp_[2] = (Byte)(_vvv_ >> 16); \
  411. _ppp_[3] = (Byte)(_vvv_ >> 24); }
  412. #endif
  413. #ifndef GetUi64
  414. #define GetUi64(p) (GetUi32(p) | ((UInt64)GetUi32(((const Byte *)(p)) + 4) << 32))
  415. #endif
  416. #ifndef SetUi64
  417. #define SetUi64(p, v) { Byte *_ppp2_ = (Byte *)(p); UInt64 _vvv2_ = (v); \
  418. SetUi32(_ppp2_ , (UInt32)_vvv2_) \
  419. SetUi32(_ppp2_ + 4, (UInt32)(_vvv2_ >> 32)) }
  420. #endif
  421. #if defined(MY_CPU_LE_UNALIGN) && defined(Z7_CPU_FAST_BSWAP_SUPPORTED)
  422. #if 0
  423. // Z7_BSWAP16 can be slow for x86-msvc
  424. #define GetBe16_to32(p) (Z7_BSWAP16 (*(const UInt16 *)(const void *)(p)))
  425. #else
  426. #define GetBe16_to32(p) (Z7_BSWAP32 (*(const UInt16 *)(const void *)(p)) >> 16)
  427. #endif
  428. #define GetBe32(p) Z7_BSWAP32 (*(const UInt32 *)(const void *)(p))
  429. #define SetBe32(p, v) { (*(UInt32 *)(void *)(p)) = Z7_BSWAP32(v); }
  430. #if defined(MY_CPU_LE_UNALIGN_64)
  431. #define GetBe64(p) Z7_BSWAP64 (*(const UInt64 *)(const void *)(p))
  432. #define SetBe64(p, v) { (*(UInt64 *)(void *)(p)) = Z7_BSWAP64(v); }
  433. #endif
  434. #else
  435. #define GetBe32(p) ( \
  436. ((UInt32)((const Byte *)(p))[0] << 24) | \
  437. ((UInt32)((const Byte *)(p))[1] << 16) | \
  438. ((UInt32)((const Byte *)(p))[2] << 8) | \
  439. ((const Byte *)(p))[3] )
  440. #define SetBe32(p, v) { Byte *_ppp_ = (Byte *)(p); UInt32 _vvv_ = (v); \
  441. _ppp_[0] = (Byte)(_vvv_ >> 24); \
  442. _ppp_[1] = (Byte)(_vvv_ >> 16); \
  443. _ppp_[2] = (Byte)(_vvv_ >> 8); \
  444. _ppp_[3] = (Byte)_vvv_; }
  445. #endif
  446. #ifndef GetBe64
  447. #define GetBe64(p) (((UInt64)GetBe32(p) << 32) | GetBe32(((const Byte *)(p)) + 4))
  448. #endif
  449. #ifndef SetBe64
  450. #define SetBe64(p, v) { Byte *_ppp_ = (Byte *)(p); UInt64 _vvv_ = (v); \
  451. _ppp_[0] = (Byte)(_vvv_ >> 56); \
  452. _ppp_[1] = (Byte)(_vvv_ >> 48); \
  453. _ppp_[2] = (Byte)(_vvv_ >> 40); \
  454. _ppp_[3] = (Byte)(_vvv_ >> 32); \
  455. _ppp_[4] = (Byte)(_vvv_ >> 24); \
  456. _ppp_[5] = (Byte)(_vvv_ >> 16); \
  457. _ppp_[6] = (Byte)(_vvv_ >> 8); \
  458. _ppp_[7] = (Byte)_vvv_; }
  459. #endif
  460. #ifndef GetBe16
  461. #ifdef GetBe16_to32
  462. #define GetBe16(p) ( (UInt16) GetBe16_to32(p))
  463. #else
  464. #define GetBe16(p) ( (UInt16) ( \
  465. ((UInt16)((const Byte *)(p))[0] << 8) | \
  466. ((const Byte *)(p))[1] ))
  467. #endif
  468. #endif
  469. #if defined(MY_CPU_BE)
  470. #define Z7_CONV_BE_TO_NATIVE_CONST32(v) (v)
  471. #define Z7_CONV_LE_TO_NATIVE_CONST32(v) Z7_BSWAP32_CONST(v)
  472. #define Z7_CONV_NATIVE_TO_BE_32(v) (v)
  473. // #define Z7_GET_NATIVE16_FROM_2_BYTES(b0, b1) ((b1) | ((b0) << 8))
  474. #elif defined(MY_CPU_LE)
  475. #define Z7_CONV_BE_TO_NATIVE_CONST32(v) Z7_BSWAP32_CONST(v)
  476. #define Z7_CONV_LE_TO_NATIVE_CONST32(v) (v)
  477. #define Z7_CONV_NATIVE_TO_BE_32(v) Z7_BSWAP32(v)
  478. // #define Z7_GET_NATIVE16_FROM_2_BYTES(b0, b1) ((b0) | ((b1) << 8))
  479. #else
  480. #error Stop_Compiling_Unknown_Endian_CONV
  481. #endif
  482. #if defined(MY_CPU_BE)
  483. #define GetBe64a(p) (*(const UInt64 *)(const void *)(p))
  484. #define GetBe32a(p) (*(const UInt32 *)(const void *)(p))
  485. #define GetBe16a(p) (*(const UInt16 *)(const void *)(p))
  486. #define SetBe32a(p, v) { *(UInt32 *)(void *)(p) = (v); }
  487. #define SetBe16a(p, v) { *(UInt16 *)(void *)(p) = (v); }
  488. #define GetUi64a(p) GetUi64(p)
  489. #define GetUi32a(p) GetUi32(p)
  490. #define GetUi16a(p) GetUi16(p)
  491. #define SetUi32a(p, v) SetUi32(p, v)
  492. #define SetUi16a(p, v) SetUi16(p, v)
  493. #elif defined(MY_CPU_LE)
  494. #define GetUi64a(p) (*(const UInt64 *)(const void *)(p))
  495. #define GetUi32a(p) (*(const UInt32 *)(const void *)(p))
  496. #define GetUi16a(p) (*(const UInt16 *)(const void *)(p))
  497. #define SetUi32a(p, v) { *(UInt32 *)(void *)(p) = (v); }
  498. #define SetUi16a(p, v) { *(UInt16 *)(void *)(p) = (v); }
  499. #define GetBe64a(p) GetBe64(p)
  500. #define GetBe32a(p) GetBe32(p)
  501. #define GetBe16a(p) GetBe16(p)
  502. #define SetBe32a(p, v) SetBe32(p, v)
  503. #define SetBe16a(p, v) SetBe16(p, v)
  504. #else
  505. #error Stop_Compiling_Unknown_Endian_CPU_a
  506. #endif
  507. #ifndef GetBe16_to32
  508. #define GetBe16_to32(p) GetBe16(p)
  509. #endif
  510. #if defined(MY_CPU_X86_OR_AMD64) \
  511. || defined(MY_CPU_ARM_OR_ARM64) \
  512. || defined(MY_CPU_PPC_OR_PPC64)
  513. #define Z7_CPU_FAST_ROTATE_SUPPORTED
  514. #endif
  515. #ifdef MY_CPU_X86_OR_AMD64
  516. void Z7_FASTCALL z7_x86_cpuid(UInt32 a[4], UInt32 function);
  517. UInt32 Z7_FASTCALL z7_x86_cpuid_GetMaxFunc(void);
  518. #if defined(MY_CPU_AMD64)
  519. #define Z7_IF_X86_CPUID_SUPPORTED
  520. #else
  521. #define Z7_IF_X86_CPUID_SUPPORTED if (z7_x86_cpuid_GetMaxFunc())
  522. #endif
  523. BoolInt CPU_IsSupported_AES(void);
  524. BoolInt CPU_IsSupported_AVX(void);
  525. BoolInt CPU_IsSupported_AVX2(void);
  526. BoolInt CPU_IsSupported_AVX512F_AVX512VL(void);
  527. BoolInt CPU_IsSupported_VAES_AVX2(void);
  528. BoolInt CPU_IsSupported_CMOV(void);
  529. BoolInt CPU_IsSupported_SSE(void);
  530. BoolInt CPU_IsSupported_SSE2(void);
  531. BoolInt CPU_IsSupported_SSSE3(void);
  532. BoolInt CPU_IsSupported_SSE41(void);
  533. BoolInt CPU_IsSupported_SHA(void);
  534. BoolInt CPU_IsSupported_SHA512(void);
  535. BoolInt CPU_IsSupported_PageGB(void);
  536. #elif defined(MY_CPU_ARM_OR_ARM64)
  537. BoolInt CPU_IsSupported_CRC32(void);
  538. BoolInt CPU_IsSupported_NEON(void);
  539. #if defined(_WIN32)
  540. BoolInt CPU_IsSupported_CRYPTO(void);
  541. #define CPU_IsSupported_SHA1 CPU_IsSupported_CRYPTO
  542. #define CPU_IsSupported_SHA2 CPU_IsSupported_CRYPTO
  543. #define CPU_IsSupported_AES CPU_IsSupported_CRYPTO
  544. #else
  545. BoolInt CPU_IsSupported_SHA1(void);
  546. BoolInt CPU_IsSupported_SHA2(void);
  547. BoolInt CPU_IsSupported_AES(void);
  548. #endif
  549. BoolInt CPU_IsSupported_SHA512(void);
  550. #endif
  551. #if defined(__APPLE__)
  552. int z7_sysctlbyname_Get(const char *name, void *buf, size_t *bufSize);
  553. int z7_sysctlbyname_Get_UInt32(const char *name, UInt32 *val);
  554. #endif
  555. EXTERN_C_END
  556. #endif