cpu.h 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089
  1. // cpu.h - originally written and placed in the public domain by Wei Dai
  2. // updated for ARM and PowerPC by Jeffrey Walton.
  3. // updated to split CPU_Query() and CPU_Probe() by Jeffrey Walton.
  4. /// \file cpu.h
  5. /// \brief Functions for CPU features and intrinsics
  6. /// \details The CPU functions are used in IA-32, ARM and PowerPC code paths. The
  7. /// functions provide cpu specific feature testing on IA-32, ARM and PowerPC machines.
  8. /// \details Feature detection uses CPUID on IA-32, like Intel and AMD. On other platforms
  9. /// a two-part strategy is used. First, the library attempts to *Query* the OS for a feature,
  10. /// like using Linux getauxval() or android_getCpuFeatures(). If that fails, then *Probe*
  11. /// the cpu executing an instruction and an observe a SIGILL if unsupported. The general
  12. /// pattern used by the library is:
  13. /// <pre>
  14. /// g_hasCRC32 = CPU_QueryCRC32() || CPU_ProbeCRC32();
  15. /// g_hasPMULL = CPU_QueryPMULL() || CPU_ProbePMULL();
  16. /// g_hasAES = CPU_QueryAES() || CPU_ProbeAES();
  17. /// </pre>
  18. /// \details Generally speaking, CPU_Query() is in the source file <tt>cpu.cpp</tt> because it
  19. /// does not require special architectural flags. CPU_Probe() is in a source file that receives
  20. /// architectural flags, like <tt>sse_simd.cpp</tt>, <tt>neon_simd.cpp</tt> and
  21. /// <tt>ppc_simd.cpp</tt>. For example, compiling <tt>neon_simd.cpp</tt> on an ARM64 machine will
  22. /// have <tt>-march=armv8-a</tt> applied during a compile to make the instruction set architecture
  23. /// (ISA) available.
  24. /// \details The cpu probes are expensive when compared to a standard OS feature query. The library
  25. /// also avoids probes on Apple platforms because Apple's signal handling for SIGILLs appears to
  26. /// corrupt memory. CPU_Probe() will unconditionally return false for Apple platforms. OpenSSL
  27. /// experienced the same problem and moved away from SIGILL probes on Apple.
  28. #ifndef CRYPTOPP_CPU_H
  29. #define CRYPTOPP_CPU_H
  30. #include "config.h"
  31. // Issue 340
  32. #if CRYPTOPP_GCC_DIAGNOSTIC_AVAILABLE
  33. # pragma GCC diagnostic push
  34. # pragma GCC diagnostic ignored "-Wconversion"
  35. # pragma GCC diagnostic ignored "-Wsign-conversion"
  36. #endif
  37. // Applies to both X86/X32/X64 and ARM32/ARM64
  38. #if defined(CRYPTOPP_LLVM_CLANG_VERSION) || defined(CRYPTOPP_APPLE_CLANG_VERSION)
  39. #define NEW_LINE "\n"
  40. #define INTEL_PREFIX ".intel_syntax;"
  41. #define INTEL_NOPREFIX ".intel_syntax;"
  42. #define ATT_PREFIX ".att_syntax;"
  43. #define ATT_NOPREFIX ".att_syntax;"
  44. #elif defined(__GNUC__)
  45. #define NEW_LINE
  46. #define INTEL_PREFIX ".intel_syntax prefix;"
  47. #define INTEL_NOPREFIX ".intel_syntax noprefix;"
  48. #define ATT_PREFIX ".att_syntax prefix;"
  49. #define ATT_NOPREFIX ".att_syntax noprefix;"
  50. #else
  51. #define NEW_LINE
  52. #define INTEL_PREFIX
  53. #define INTEL_NOPREFIX
  54. #define ATT_PREFIX
  55. #define ATT_NOPREFIX
  56. #endif
  57. // Thanks to v1ne at https://github.com/weidai11/cryptopp/pull/1133
  58. #define PERCENT_PASTE(x) "%" #x
  59. #define PERCENT_REG(x) PERCENT_PASTE(x)
  60. #ifdef CRYPTOPP_GENERATE_X64_MASM
  61. #define CRYPTOPP_X86_ASM_AVAILABLE
  62. #define CRYPTOPP_BOOL_X64 1
  63. #define CRYPTOPP_SSE2_ASM_AVAILABLE 1
  64. #define NAMESPACE_END
  65. #else
  66. NAMESPACE_BEGIN(CryptoPP)
  67. // ***************************** IA-32 ***************************** //
  68. #if CRYPTOPP_BOOL_X86 || CRYPTOPP_BOOL_X32 || CRYPTOPP_BOOL_X64 || CRYPTOPP_DOXYGEN_PROCESSING
  69. #define CRYPTOPP_CPUID_AVAILABLE 1
  70. // Hide from Doxygen
  71. #ifndef CRYPTOPP_DOXYGEN_PROCESSING
  72. // These should not be used directly
  73. extern CRYPTOPP_DLL bool g_x86DetectionDone;
  74. extern CRYPTOPP_DLL bool g_hasSSE2;
  75. extern CRYPTOPP_DLL bool g_hasSSSE3;
  76. extern CRYPTOPP_DLL bool g_hasSSE41;
  77. extern CRYPTOPP_DLL bool g_hasSSE42;
  78. extern CRYPTOPP_DLL bool g_hasMOVBE;
  79. extern CRYPTOPP_DLL bool g_hasAESNI;
  80. extern CRYPTOPP_DLL bool g_hasCLMUL;
  81. extern CRYPTOPP_DLL bool g_hasAVX;
  82. extern CRYPTOPP_DLL bool g_hasAVX2;
  83. extern CRYPTOPP_DLL bool g_hasSHA;
  84. extern CRYPTOPP_DLL bool g_hasADX;
  85. extern CRYPTOPP_DLL bool g_isP4;
  86. extern CRYPTOPP_DLL bool g_hasRDRAND;
  87. extern CRYPTOPP_DLL bool g_hasRDSEED;
  88. extern CRYPTOPP_DLL bool g_hasPadlockRNG;
  89. extern CRYPTOPP_DLL bool g_hasPadlockACE;
  90. extern CRYPTOPP_DLL bool g_hasPadlockACE2;
  91. extern CRYPTOPP_DLL bool g_hasPadlockPHE;
  92. extern CRYPTOPP_DLL bool g_hasPadlockPMM;
  93. extern CRYPTOPP_DLL word32 g_cacheLineSize;
  94. CRYPTOPP_DLL void CRYPTOPP_API DetectX86Features();
  95. CRYPTOPP_DLL bool CRYPTOPP_API CpuId(word32 func, word32 subfunc, word32 output[4]);
  96. #endif // CRYPTOPP_DOXYGEN_PROCESSING
  97. /// \name IA-32 CPU FEATURES
  98. //@{
  99. /// \brief Determine SSE2 availability
  100. /// \return true if SSE2 is determined to be available, false otherwise
  101. /// \details MMX, SSE and SSE2 are core processor features for x86_64, and
  102. /// the function return value is based on OSXSAVE. On i386 both
  103. /// SSE2 and OSXSAVE are used for the return value.
  104. /// \note This function is only available on Intel IA-32 platforms
  105. inline bool HasSSE2()
  106. {
  107. #if (CRYPTOPP_SSE2_ASM_AVAILABLE || CRYPTOPP_SSE2_INTRIN_AVAILABLE)
  108. if (!g_x86DetectionDone)
  109. DetectX86Features();
  110. return g_hasSSE2;
  111. #else
  112. return false;
  113. #endif
  114. }
  115. /// \brief Determine SSSE3 availability
  116. /// \return true if SSSE3 is determined to be available, false otherwise
  117. /// \details HasSSSE3() is a runtime check performed using CPUID
  118. /// \note This function is only available on Intel IA-32 platforms
  119. inline bool HasSSSE3()
  120. {
  121. #if CRYPTOPP_SSSE3_AVAILABLE
  122. if (!g_x86DetectionDone)
  123. DetectX86Features();
  124. return g_hasSSSE3;
  125. #else
  126. return false;
  127. #endif
  128. }
  129. /// \brief Determine SSE4.1 availability
  130. /// \return true if SSE4.1 is determined to be available, false otherwise
  131. /// \details HasSSE41() is a runtime check performed using CPUID
  132. /// \note This function is only available on Intel IA-32 platforms
  133. inline bool HasSSE41()
  134. {
  135. #if CRYPTOPP_SSE41_AVAILABLE
  136. if (!g_x86DetectionDone)
  137. DetectX86Features();
  138. return g_hasSSE41;
  139. #else
  140. return false;
  141. #endif
  142. }
  143. /// \brief Determine SSE4.2 availability
  144. /// \return true if SSE4.2 is determined to be available, false otherwise
  145. /// \details HasSSE42() is a runtime check performed using CPUID
  146. /// \note This function is only available on Intel IA-32 platforms
  147. inline bool HasSSE42()
  148. {
  149. #if CRYPTOPP_SSE42_AVAILABLE
  150. if (!g_x86DetectionDone)
  151. DetectX86Features();
  152. return g_hasSSE42;
  153. #else
  154. return false;
  155. #endif
  156. }
  157. /// \brief Determine MOVBE availability
  158. /// \return true if MOVBE is determined to be available, false otherwise
  159. /// \details HasMOVBE() is a runtime check performed using CPUID
  160. /// \since Crypto++ 8.3
  161. /// \note This function is only available on Intel IA-32 platforms
  162. inline bool HasMOVBE()
  163. {
  164. #if CRYPTOPP_SSE42_AVAILABLE
  165. if (!g_x86DetectionDone)
  166. DetectX86Features();
  167. return g_hasMOVBE;
  168. #else
  169. return false;
  170. #endif
  171. }
  172. /// \brief Determine AES-NI availability
  173. /// \return true if AES-NI is determined to be available, false otherwise
  174. /// \details HasAESNI() is a runtime check performed using CPUID
  175. /// \since Crypto++ 5.6.1
  176. /// \note This function is only available on Intel IA-32 platforms
  177. inline bool HasAESNI()
  178. {
  179. #if CRYPTOPP_AESNI_AVAILABLE
  180. if (!g_x86DetectionDone)
  181. DetectX86Features();
  182. return g_hasAESNI;
  183. #else
  184. return false;
  185. #endif
  186. }
  187. /// \brief Determine Carryless Multiply availability
  188. /// \return true if pclmulqdq is determined to be available, false otherwise
  189. /// \details HasCLMUL() is a runtime check performed using CPUID
  190. /// \since Crypto++ 5.6.1
  191. /// \note This function is only available on Intel IA-32 platforms
  192. inline bool HasCLMUL()
  193. {
  194. #if CRYPTOPP_CLMUL_AVAILABLE
  195. if (!g_x86DetectionDone)
  196. DetectX86Features();
  197. return g_hasCLMUL;
  198. #else
  199. return false;
  200. #endif
  201. }
  202. /// \brief Determine SHA availability
  203. /// \return true if SHA is determined to be available, false otherwise
  204. /// \details HasSHA() is a runtime check performed using CPUID
  205. /// \since Crypto++ 6.0
  206. /// \note This function is only available on Intel IA-32 platforms
  207. inline bool HasSHA()
  208. {
  209. #if CRYPTOPP_SHANI_AVAILABLE
  210. if (!g_x86DetectionDone)
  211. DetectX86Features();
  212. return g_hasSHA;
  213. #else
  214. return false;
  215. #endif
  216. }
  217. /// \brief Determine ADX availability
  218. /// \return true if ADX is determined to be available, false otherwise
  219. /// \details HasADX() is a runtime check performed using CPUID
  220. /// \since Crypto++ 7.0
  221. /// \note This function is only available on Intel IA-32 platforms
  222. inline bool HasADX()
  223. {
  224. #if CRYPTOPP_ADX_AVAILABLE
  225. if (!g_x86DetectionDone)
  226. DetectX86Features();
  227. return g_hasADX;
  228. #else
  229. return false;
  230. #endif
  231. }
  232. /// \brief Determine AVX availability
  233. /// \return true if AVX is determined to be available, false otherwise
  234. /// \details HasAVX() is a runtime check performed using CPUID
  235. /// \since Crypto++ 8.0
  236. /// \note This function is only available on Intel IA-32 platforms
  237. inline bool HasAVX()
  238. {
  239. #if CRYPTOPP_AVX_AVAILABLE
  240. if (!g_x86DetectionDone)
  241. DetectX86Features();
  242. return g_hasAVX;
  243. #else
  244. return false;
  245. #endif
  246. }
  247. /// \brief Determine AVX2 availability
  248. /// \return true if AVX2 is determined to be available, false otherwise
  249. /// \details HasAVX2() is a runtime check performed using CPUID
  250. /// \since Crypto++ 8.0
  251. /// \note This function is only available on Intel IA-32 platforms
  252. inline bool HasAVX2()
  253. {
  254. #if CRYPTOPP_AVX2_AVAILABLE
  255. if (!g_x86DetectionDone)
  256. DetectX86Features();
  257. return g_hasAVX2;
  258. #else
  259. return false;
  260. #endif
  261. }
  262. /// \brief Determine RDRAND availability
  263. /// \return true if RDRAND is determined to be available, false otherwise
  264. /// \details HasRDRAND() is a runtime check performed using CPUID
  265. /// \note This function is only available on Intel IA-32 platforms
  266. inline bool HasRDRAND()
  267. {
  268. #if CRYPTOPP_RDRAND_AVAILABLE
  269. if (!g_x86DetectionDone)
  270. DetectX86Features();
  271. return g_hasRDRAND;
  272. #else
  273. return false;
  274. #endif
  275. }
  276. /// \brief Determine RDSEED availability
  277. /// \return true if RDSEED is determined to be available, false otherwise
  278. /// \details HasRDSEED() is a runtime check performed using CPUID
  279. /// \note This function is only available on Intel IA-32 platforms
  280. inline bool HasRDSEED()
  281. {
  282. #if CRYPTOPP_RDSEED_AVAILABLE
  283. if (!g_x86DetectionDone)
  284. DetectX86Features();
  285. return g_hasRDSEED;
  286. #else
  287. return false;
  288. #endif
  289. }
  290. /// \brief Determine Padlock RNG availability
  291. /// \return true if VIA Padlock RNG is determined to be available, false otherwise
  292. /// \details HasPadlockRNG() is a runtime check performed using CPUID
  293. /// \note This function is only available on Intel IA-32 platforms
  294. inline bool HasPadlockRNG()
  295. {
  296. #if CRYPTOPP_PADLOCK_RNG_AVAILABLE
  297. if (!g_x86DetectionDone)
  298. DetectX86Features();
  299. return g_hasPadlockRNG;
  300. #else
  301. return false;
  302. #endif
  303. }
  304. /// \brief Determine Padlock ACE availability
  305. /// \return true if VIA Padlock ACE is determined to be available, false otherwise
  306. /// \details HasPadlockACE() is a runtime check performed using CPUID
  307. /// \note This function is only available on Intel IA-32 platforms
  308. inline bool HasPadlockACE()
  309. {
  310. #if CRYPTOPP_PADLOCK_ACE_AVAILABLE
  311. if (!g_x86DetectionDone)
  312. DetectX86Features();
  313. return g_hasPadlockACE;
  314. #else
  315. return false;
  316. #endif
  317. }
  318. /// \brief Determine Padlock ACE2 availability
  319. /// \return true if VIA Padlock ACE2 is determined to be available, false otherwise
  320. /// \details HasPadlockACE2() is a runtime check performed using CPUID
  321. /// \note This function is only available on Intel IA-32 platforms
  322. inline bool HasPadlockACE2()
  323. {
  324. #if CRYPTOPP_PADLOCK_ACE2_AVAILABLE
  325. if (!g_x86DetectionDone)
  326. DetectX86Features();
  327. return g_hasPadlockACE2;
  328. #else
  329. return false;
  330. #endif
  331. }
  332. /// \brief Determine Padlock PHE availability
  333. /// \return true if VIA Padlock PHE is determined to be available, false otherwise
  334. /// \details HasPadlockPHE() is a runtime check performed using CPUID
  335. /// \note This function is only available on Intel IA-32 platforms
  336. inline bool HasPadlockPHE()
  337. {
  338. #if CRYPTOPP_PADLOCK_PHE_AVAILABLE
  339. if (!g_x86DetectionDone)
  340. DetectX86Features();
  341. return g_hasPadlockPHE;
  342. #else
  343. return false;
  344. #endif
  345. }
  346. /// \brief Determine Padlock PMM availability
  347. /// \return true if VIA Padlock PMM is determined to be available, false otherwise
  348. /// \details HasPadlockPMM() is a runtime check performed using CPUID
  349. /// \note This function is only available on Intel IA-32 platforms
  350. inline bool HasPadlockPMM()
  351. {
  352. #if CRYPTOPP_PADLOCK_PMM_AVAILABLE
  353. if (!g_x86DetectionDone)
  354. DetectX86Features();
  355. return g_hasPadlockPMM;
  356. #else
  357. return false;
  358. #endif
  359. }
  360. /// \brief Determine if the CPU is an Intel P4
  361. /// \return true if the CPU is a P4, false otherwise
  362. /// \details IsP4() is a runtime check performed using CPUID
  363. /// \note This function is only available on Intel IA-32 platforms
  364. inline bool IsP4()
  365. {
  366. if (!g_x86DetectionDone)
  367. DetectX86Features();
  368. return g_isP4;
  369. }
  370. /// \brief Provides the cache line size
  371. /// \return lower bound on the size of a cache line in bytes, if available
  372. /// \details GetCacheLineSize() returns the lower bound on the size of a cache line, if it
  373. /// is available. If the value is not available at runtime, then 32 is returned for a 32-bit
  374. /// processor and 64 is returned for a 64-bit processor.
  375. /// \details x86/x32/x64 uses CPUID to determine the value and it is usually accurate. PowerPC
  376. /// and AIX also makes the value available to user space and it is also usually accurate. The
  377. /// ARM processor equivalent is a privileged instruction, so a compile time value is returned.
  378. inline int GetCacheLineSize()
  379. {
  380. if (!g_x86DetectionDone)
  381. DetectX86Features();
  382. return g_cacheLineSize;
  383. }
  384. //@}
  385. #endif // CRYPTOPP_BOOL_X86 || CRYPTOPP_BOOL_X32 || CRYPTOPP_BOOL_X64
  386. // ***************************** ARM-32, Aarch32 and Aarch64 ***************************** //
  387. #if CRYPTOPP_BOOL_ARM32 || CRYPTOPP_BOOL_ARMV8 || CRYPTOPP_DOXYGEN_PROCESSING
  388. // Hide from Doxygen
  389. #ifndef CRYPTOPP_DOXYGEN_PROCESSING
  390. extern bool g_ArmDetectionDone;
  391. extern bool g_hasARMv7;
  392. extern bool g_hasNEON;
  393. extern bool g_hasPMULL;
  394. extern bool g_hasCRC32;
  395. extern bool g_hasAES;
  396. extern bool g_hasSHA1;
  397. extern bool g_hasSHA2;
  398. extern bool g_hasSHA512;
  399. extern bool g_hasSHA3;
  400. extern bool g_hasSM3;
  401. extern bool g_hasSM4;
  402. void CRYPTOPP_API DetectArmFeatures();
  403. #endif // CRYPTOPP_DOXYGEN_PROCESSING
  404. /// \name ARM A-32, Aarch32 and AArch64 CPU FEATURES
  405. //@{
  406. /// \brief Determine if an ARM processor is ARMv7 or above
  407. /// \return true if the hardware is ARMv7 or above, false otherwise.
  408. /// \details Some AES code requires ARMv7 or above
  409. /// \since Crypto++ 8.0
  410. /// \note This function is only available on ARM-32, Aarch32 and Aarch64 platforms
  411. inline bool HasARMv7()
  412. {
  413. // ASIMD is a core feature on Aarch32 and Aarch64 like SSE2 is a core feature on x86_64
  414. #if defined(__aarch32__) || defined(__aarch64__)
  415. return true;
  416. #else
  417. if (!g_ArmDetectionDone)
  418. DetectArmFeatures();
  419. return g_hasARMv7;
  420. #endif
  421. }
  422. /// \brief Determine if an ARM processor has Advanced SIMD available
  423. /// \return true if the hardware is capable of Advanced SIMD at runtime, false otherwise.
  424. /// \details Advanced SIMD instructions are available under most ARMv7, Aarch32 and Aarch64.
  425. /// \details Runtime support requires compile time support. When compiling with GCC, you may
  426. /// need to compile with <tt>-mfpu=neon</tt> (32-bit) or <tt>-march=armv8-a</tt>
  427. /// (64-bit). Also see ARM's <tt>__ARM_NEON</tt> preprocessor macro.
  428. /// \since Crypto++ 5.6.4
  429. /// \note This function is only available on ARM-32, Aarch32 and Aarch64 platforms
  430. inline bool HasNEON()
  431. {
  432. // ASIMD is a core feature on Aarch32 and Aarch64 like SSE2 is a core feature on x86_64
  433. #if defined(CRYPTOPP_ARM_ASIMD_AVAILABLE)
  434. return true;
  435. #elif defined(CRYPTOPP_ARM_NEON_AVAILABLE)
  436. if (!g_ArmDetectionDone)
  437. DetectArmFeatures();
  438. return g_hasNEON;
  439. #else
  440. return false;
  441. #endif
  442. }
  443. /// \brief Determine if an ARM processor has CRC32 available
  444. /// \return true if the hardware is capable of CRC32 at runtime, false otherwise.
  445. /// \details CRC32 instructions provide access to the processor's CRC-32 and CRC-32C
  446. /// instructions. They are provided by ARM C Language Extensions 2.0 (ACLE 2.0) and
  447. /// available under Aarch32 and Aarch64.
  448. /// \details Runtime support requires compile time support. When compiling with GCC,
  449. /// you may need to compile with <tt>-march=armv8-a+crc</tt>; while Apple requires
  450. /// <tt>-arch arm64</tt>. Also see ARM's <tt>__ARM_FEATURE_CRC32</tt> preprocessor macro.
  451. /// \since Crypto++ 5.6.4
  452. /// \note This function is only available on Aarch32 and Aarch64 platforms
  453. inline bool HasCRC32()
  454. {
  455. #if defined(CRYPTOPP_ARM_CRC32_AVAILABLE)
  456. if (!g_ArmDetectionDone)
  457. DetectArmFeatures();
  458. return g_hasCRC32;
  459. #else
  460. return false;
  461. #endif
  462. }
  463. /// \brief Determine if an ARM processor has AES available
  464. /// \return true if the hardware is capable of AES at runtime, false otherwise.
  465. /// \details AES is part of the optional Crypto extensions on Aarch32 and Aarch64. They are
  466. /// accessed using ARM C Language Extensions 2.0 (ACLE 2.0).
  467. /// \details Runtime support requires compile time support. When compiling with GCC, you may
  468. /// need to compile with <tt>-march=armv8-a+crypto</tt>; while Apple requires
  469. /// <tt>-arch arm64</tt>. Also see ARM's <tt>__ARM_FEATURE_CRYPTO</tt> preprocessor macro.
  470. /// \since Crypto++ 5.6.4
  471. /// \note This function is only available on Aarch32 and Aarch64 platforms
  472. inline bool HasAES()
  473. {
  474. #if defined(CRYPTOPP_ARM_AES_AVAILABLE)
  475. if (!g_ArmDetectionDone)
  476. DetectArmFeatures();
  477. return g_hasAES;
  478. #else
  479. return false;
  480. #endif
  481. }
  482. /// \brief Determine if an ARM processor provides Polynomial Multiplication
  483. /// \return true if the hardware is capable of polynomial multiplications at runtime,
  484. /// false otherwise.
  485. /// \details The multiplication instructions are available under Aarch32 and Aarch64.
  486. /// \details Runtime support requires compile time support. When compiling with GCC,
  487. /// you may need to compile with <tt>-march=armv8-a+crypto</tt>; while Apple requires
  488. /// <tt>-arch arm64</tt>. Also see ARM's <tt>__ARM_FEATURE_CRYPTO</tt> preprocessor macro.
  489. /// \since Crypto++ 5.6.4
  490. /// \note This function is only available on Aarch32 and Aarch64 platforms
  491. inline bool HasPMULL()
  492. {
  493. #if defined(CRYPTOPP_ARM_PMULL_AVAILABLE)
  494. if (!g_ArmDetectionDone)
  495. DetectArmFeatures();
  496. return g_hasPMULL;
  497. #else
  498. return false;
  499. #endif
  500. }
  501. /// \brief Determine if an ARM processor has SHA1 available
  502. /// \return true if the hardware is capable of SHA1 at runtime, false otherwise.
  503. /// \details SHA1 is part of the optional Crypto extensions on Aarch32 and Aarch64. They are
  504. /// accessed using ARM C Language Extensions 2.0 (ACLE 2.0).
  505. /// \details Runtime support requires compile time support. When compiling with GCC, you may
  506. /// need to compile with <tt>-march=armv8-a+crypto</tt>; while Apple requires
  507. /// <tt>-arch arm64</tt>. Also see ARM's <tt>__ARM_FEATURE_CRYPTO</tt> preprocessor macro.
  508. /// \since Crypto++ 5.6.4
  509. /// \note This function is only available on Aarch32 and Aarch64 platforms
  510. inline bool HasSHA1()
  511. {
  512. #if defined(CRYPTOPP_ARM_SHA1_AVAILABLE)
  513. if (!g_ArmDetectionDone)
  514. DetectArmFeatures();
  515. return g_hasSHA1;
  516. #else
  517. return false;
  518. #endif
  519. }
  520. /// \brief Determine if an ARM processor has SHA256 available
  521. /// \return true if the hardware is capable of SHA256 at runtime, false otherwise.
  522. /// \details SHA256 is part of the optional Crypto extensions on Aarch32 and Aarch64. They are
  523. /// accessed using ARM C Language Extensions 2.0 (ACLE 2.0).
  524. /// \details Runtime support requires compile time support. When compiling with GCC, you may
  525. /// need to compile with <tt>-march=armv8-a+crypto</tt>; while Apple requires
  526. /// <tt>-arch arm64</tt>. Also see ARM's <tt>__ARM_FEATURE_CRYPTO</tt> preprocessor macro.
  527. /// \since Crypto++ 5.6.4
  528. /// \note This function is only available on Aarch32 and Aarch64 platforms
  529. inline bool HasSHA2()
  530. {
  531. #if defined(CRYPTOPP_ARM_SHA2_AVAILABLE)
  532. if (!g_ArmDetectionDone)
  533. DetectArmFeatures();
  534. return g_hasSHA2;
  535. #else
  536. return false;
  537. #endif
  538. }
  539. /// \brief Determine if an ARM processor has SHA3 available
  540. /// \return true if the hardware is capable of SHA3 at runtime, false otherwise.
  541. /// \details SHA3 is part of the ARMv8.2 Crypto extensions on Aarch32 and Aarch64. They
  542. /// are accessed using ARM C Language Extensions 2.0 (ACLE 2.0).
  543. /// \details Runtime support requires compile time support. When compiling with GCC, you
  544. /// may need to compile with <tt>-march=armv8.2-a+crypto</tt>; while Apple requires
  545. /// <tt>-arch arm64</tt>. Also see ARM's <tt>__ARM_FEATURE_CRYPTO</tt> preprocessor macro.
  546. /// \since Crypto++ 8.0
  547. /// \note This function is only available on Aarch32 and Aarch64 platforms
  548. inline bool HasSHA3()
  549. {
  550. #if defined(CRYPTOPP_ARM_SHA3_AVAILABLE)
  551. if (!g_ArmDetectionDone)
  552. DetectArmFeatures();
  553. return g_hasSHA3;
  554. #else
  555. return false;
  556. #endif
  557. }
  558. /// \brief Determine if an ARM processor has SHA512 available
  559. /// \return true if the hardware is capable of SHA512 at runtime, false otherwise.
  560. /// \details SHA512 is part of the ARMv8.2 Crypto extensions on Aarch32 and Aarch64. They
  561. /// are accessed using ARM C Language Extensions 2.0 (ACLE 2.0).
  562. /// \details Runtime support requires compile time support. When compiling with GCC, you
  563. /// may need to compile with <tt>-march=armv8.2-a+crypto</tt>; while Apple requires
  564. /// <tt>-arch arm64</tt>. Also see ARM's <tt>__ARM_FEATURE_CRYPTO</tt> preprocessor macro.
  565. /// \since Crypto++ 8.0
  566. /// \note This function is only available on Aarch32 and Aarch64 platforms
  567. inline bool HasSHA512()
  568. {
  569. #if defined(CRYPTOPP_ARM_SHA512_AVAILABLE)
  570. if (!g_ArmDetectionDone)
  571. DetectArmFeatures();
  572. return g_hasSHA512;
  573. #else
  574. return false;
  575. #endif
  576. }
  577. /// \brief Determine if an ARM processor has SM3 available
  578. /// \return true if the hardware is capable of SM3 at runtime, false otherwise.
  579. /// \details SM3 is part of the ARMv8.2 Crypto extensions on Aarch32 and Aarch64. They
  580. /// are accessed using ARM C Language Extensions 2.0 (ACLE 2.0).
  581. /// \details Runtime support requires compile time support. When compiling with GCC, you
  582. /// may need to compile with <tt>-march=armv8.2-a+crypto</tt>; while Apple requires
  583. /// <tt>-arch arm64</tt>. Also see ARM's <tt>__ARM_FEATURE_CRYPTO</tt> preprocessor macro.
  584. /// \since Crypto++ 8.0
  585. /// \note This function is only available on Aarch32 and Aarch64 platforms
  586. inline bool HasSM3()
  587. {
  588. #if defined(CRYPTOPP_ARM_SM3_AVAILABLE)
  589. if (!g_ArmDetectionDone)
  590. DetectArmFeatures();
  591. return g_hasSM3;
  592. #else
  593. return false;
  594. #endif
  595. }
  596. /// \brief Determine if an ARM processor has SM4 available
  597. /// \return true if the hardware is capable of SM4 at runtime, false otherwise.
  598. /// \details SM4 is part of the ARMv8.2 Crypto extensions on Aarch32 and Aarch64. They
  599. /// are accessed using ARM C Language Extensions 2.0 (ACLE 2.0).
  600. /// \details Runtime support requires compile time support. When compiling with GCC, you
  601. /// may need to compile with <tt>-march=armv8.2-a+crypto</tt>; while Apple requires
  602. /// <tt>-arch arm64</tt>. Also see ARM's <tt>__ARM_FEATURE_CRYPTO</tt> preprocessor macro.
  603. /// \since Crypto++ 8.0
  604. /// \note This function is only available on Aarch32 and Aarch64 platforms
  605. inline bool HasSM4()
  606. {
  607. #if defined(CRYPTOPP_ARM_SM4_AVAILABLE)
  608. if (!g_ArmDetectionDone)
  609. DetectArmFeatures();
  610. return g_hasSM4;
  611. #else
  612. return false;
  613. #endif
  614. }
  615. //@}
  616. #endif // CRYPTOPP_BOOL_ARM32 || CRYPTOPP_BOOL_ARMV8
  617. // ***************************** PowerPC ***************************** //
  618. #if CRYPTOPP_BOOL_PPC32 || CRYPTOPP_BOOL_PPC64 || CRYPTOPP_DOXYGEN_PROCESSING
  619. // Hide from Doxygen
  620. #ifndef CRYPTOPP_DOXYGEN_PROCESSING
  621. extern bool g_PowerPcDetectionDone;
  622. extern bool g_hasAltivec;
  623. extern bool g_hasPower7;
  624. extern bool g_hasPower8;
  625. extern bool g_hasPower9;
  626. extern bool g_hasAES;
  627. extern bool g_hasPMULL;
  628. extern bool g_hasSHA256;
  629. extern bool g_hasSHA512;
  630. extern bool g_hasDARN;
  631. extern word32 g_cacheLineSize;
  632. void CRYPTOPP_API DetectPowerPcFeatures();
  633. #endif // CRYPTOPP_DOXYGEN_PROCESSING
  634. /// \name POWERPC CPU FEATURES
  635. //@{
  636. /// \brief Determine if a PowerPC processor has Altivec available
  637. /// \return true if the hardware is capable of Altivec at runtime, false otherwise.
  638. /// \details Altivec instructions are available on modern PowerPCs.
  639. /// \details Runtime support requires compile time support. When compiling with GCC, you may
  640. /// need to compile with <tt>-mcpu=power4</tt>; while IBM XL C/C++ compilers require
  641. /// <tt>-qarch=pwr6 -qaltivec</tt>. Also see PowerPC's <tt>_ALTIVEC_</tt> preprocessor macro.
  642. /// \note This function is only available on PowerPC and PowerPC-64 platforms
  643. inline bool HasAltivec()
  644. {
  645. #if CRYPTOPP_ALTIVEC_AVAILABLE
  646. if (!g_PowerPcDetectionDone)
  647. DetectPowerPcFeatures();
  648. return g_hasAltivec;
  649. #else
  650. return false;
  651. #endif
  652. }
  653. /// \brief Determine if a PowerPC processor has Power7 available
  654. /// \return true if the hardware is capable of Power7 at runtime, false otherwise.
  655. /// \details Runtime support requires compile time support. When compiling with GCC, you may
  656. /// need to compile with <tt>-mcpu=power7</tt>; while IBM XL C/C++ compilers require
  657. /// <tt>-qarch=pwr7 -qaltivec</tt>. Also see PowerPC's <tt>_ALTIVEC_</tt> preprocessor macro.
  658. /// \note This function is only available on PowerPC and PowerPC-64 platforms
  659. inline bool HasPower7()
  660. {
  661. #if CRYPTOPP_POWER7_AVAILABLE
  662. if (!g_PowerPcDetectionDone)
  663. DetectPowerPcFeatures();
  664. return g_hasPower7;
  665. #else
  666. return false;
  667. #endif
  668. }
  669. /// \brief Determine if a PowerPC processor has Power8 available
  670. /// \return true if the hardware is capable of Power8 at runtime, false otherwise.
  671. /// \details Runtime support requires compile time support. When compiling with GCC, you may
  672. /// need to compile with <tt>-mcpu=power8</tt>; while IBM XL C/C++ compilers require
  673. /// <tt>-qarch=pwr8 -qaltivec</tt>. Also see PowerPC's <tt>_ALTIVEC_</tt> preprocessor macro.
  674. /// \note This function is only available on PowerPC and PowerPC-64 platforms
  675. inline bool HasPower8()
  676. {
  677. #if CRYPTOPP_POWER8_AVAILABLE
  678. if (!g_PowerPcDetectionDone)
  679. DetectPowerPcFeatures();
  680. return g_hasPower8;
  681. #else
  682. return false;
  683. #endif
  684. }
  685. /// \brief Determine if a PowerPC processor has Power9 available
  686. /// \return true if the hardware is capable of Power9 at runtime, false otherwise.
  687. /// \details Runtime support requires compile time support. When compiling with GCC, you may
  688. /// need to compile with <tt>-mcpu=power9</tt>; while IBM XL C/C++ compilers require
  689. /// <tt>-qarch=pwr9 -qaltivec</tt>. Also see PowerPC's <tt>_ALTIVEC_</tt> preprocessor macro.
  690. /// \note This function is only available on PowerPC and PowerPC-64 platforms
  691. inline bool HasPower9()
  692. {
  693. #if CRYPTOPP_POWER9_AVAILABLE
  694. if (!g_PowerPcDetectionDone)
  695. DetectPowerPcFeatures();
  696. return g_hasPower9;
  697. #else
  698. return false;
  699. #endif
  700. }
  701. /// \brief Determine if a PowerPC processor has AES available
  702. /// \return true if the hardware is capable of AES at runtime, false otherwise.
  703. /// \details AES is part of the in-crypto extensions on Power8 and Power9.
  704. /// \details Runtime support requires compile time support. When compiling with GCC, you may
  705. /// need to compile with <tt>-mcpu=power8</tt>; while IBM XL C/C++ compilers require
  706. /// <tt>-qarch=pwr8 -qaltivec</tt>. Also see PowerPC's <tt>__CRYPTO</tt> preprocessor macro.
  707. /// \note This function is only available on PowerPC and PowerPC-64 platforms
  708. inline bool HasAES()
  709. {
  710. #if CRYPTOPP_POWER8_AES_AVAILABLE
  711. if (!g_PowerPcDetectionDone)
  712. DetectPowerPcFeatures();
  713. return g_hasAES;
  714. #else
  715. return false;
  716. #endif
  717. }
  718. /// \brief Determine if a PowerPC processor has Polynomial Multiply available
  719. /// \return true if the hardware is capable of PMULL at runtime, false otherwise.
  720. /// \details PMULL is part of the in-crypto extensions on Power8 and Power9.
  721. /// \details Runtime support requires compile time support. When compiling with GCC, you may
  722. /// need to compile with <tt>-mcpu=power8</tt>; while IBM XL C/C++ compilers require
  723. /// <tt>-qarch=pwr8 -qaltivec</tt>. Also see PowerPC's <tt>__CRYPTO</tt> preprocessor macro.
  724. /// \note This function is only available on PowerPC and PowerPC-64 platforms
  725. inline bool HasPMULL()
  726. {
  727. #if CRYPTOPP_POWER8_VMULL_AVAILABLE
  728. if (!g_PowerPcDetectionDone)
  729. DetectPowerPcFeatures();
  730. return g_hasPMULL;
  731. #else
  732. return false;
  733. #endif
  734. }
  735. /// \brief Determine if a PowerPC processor has SHA256 available
  736. /// \return true if the hardware is capable of SHA256 at runtime, false otherwise.
  737. /// \details SHA is part of the in-crypto extensions on Power8 and Power9.
  738. /// \details Runtime support requires compile time support. When compiling with GCC, you may
  739. /// need to compile with <tt>-mcpu=power8</tt>; while IBM XL C/C++ compilers require
  740. /// <tt>-qarch=pwr8 -qaltivec</tt>. Also see PowerPC's <tt>__CRYPTO</tt> preprocessor macro.
  741. /// \note This function is only available on PowerPC and PowerPC-64 platforms
  742. inline bool HasSHA256()
  743. {
  744. #if CRYPTOPP_POWER8_SHA_AVAILABLE
  745. if (!g_PowerPcDetectionDone)
  746. DetectPowerPcFeatures();
  747. return g_hasSHA256;
  748. #else
  749. return false;
  750. #endif
  751. }
  752. /// \brief Determine if a PowerPC processor has SHA512 available
  753. /// \return true if the hardware is capable of SHA512 at runtime, false otherwise.
  754. /// \details SHA is part of the in-crypto extensions on Power8 and Power9.
  755. /// \details Runtime support requires compile time support. When compiling with GCC, you may
  756. /// need to compile with <tt>-mcpu=power8</tt>; while IBM XL C/C++ compilers require
  757. /// <tt>-qarch=pwr8 -qaltivec</tt>. Also see PowerPC's <tt>__CRYPTO</tt> preprocessor macro.
  758. /// \note This function is only available on PowerPC and PowerPC-64 platforms
  759. inline bool HasSHA512()
  760. {
  761. #if CRYPTOPP_POWER8_SHA_AVAILABLE
  762. if (!g_PowerPcDetectionDone)
  763. DetectPowerPcFeatures();
  764. return g_hasSHA512;
  765. #else
  766. return false;
  767. #endif
  768. }
  769. /// \brief Determine if a PowerPC processor has DARN available
  770. /// \return true if the hardware is capable of DARN at runtime, false otherwise.
  771. /// \details Runtime support requires compile time support. When compiling with GCC, you may
  772. /// need to compile with <tt>-mcpu=power9</tt>; while IBM XL C/C++ compilers require
  773. /// <tt>-qarch=pwr9 -qaltivec</tt>. Also see PowerPC's <tt>_ALTIVEC_</tt> preprocessor macro.
  774. /// \note This function is only available on PowerPC and PowerPC-64 platforms
  775. inline bool HasDARN()
  776. {
  777. #if CRYPTOPP_POWER9_AVAILABLE
  778. if (!g_PowerPcDetectionDone)
  779. DetectPowerPcFeatures();
  780. // see comments in cpu.cpp
  781. # if defined(__ibmxl__) && defined(__linux__)
  782. return false;
  783. # else
  784. return g_hasDARN;
  785. # endif
  786. #else
  787. return false;
  788. #endif
  789. }
  790. /// \brief Provides the cache line size
  791. /// \return lower bound on the size of a cache line in bytes, if available
  792. /// \details GetCacheLineSize() returns the lower bound on the size of a cache line, if it
  793. /// is available. If the value is not available at runtime, then 32 is returned for a 32-bit
  794. /// processor and 64 is returned for a 64-bit processor.
  795. /// \details x86/x32/x64 uses CPUID to determine the value and it is usually accurate. PowerPC
  796. /// and AIX also makes the value available to user space and it is also usually accurate. The
  797. /// ARM processor equivalent is a privileged instruction, so a compile time value is returned.
  798. inline int GetCacheLineSize()
  799. {
  800. if (!g_PowerPcDetectionDone)
  801. DetectPowerPcFeatures();
  802. return g_cacheLineSize;
  803. }
  804. //@}
  805. #endif // CRYPTOPP_BOOL_PPC32 || CRYPTOPP_BOOL_PPC64
  806. // ***************************** L1 cache line ***************************** //
  807. // Non-Intel systems
  808. #if !(CRYPTOPP_BOOL_X86 || CRYPTOPP_BOOL_X32 || CRYPTOPP_BOOL_X64 || CRYPTOPP_BOOL_PPC32 || CRYPTOPP_BOOL_PPC64)
  809. /// \brief Provides the cache line size
  810. /// \return lower bound on the size of a cache line in bytes, if available
  811. /// \details GetCacheLineSize() returns the lower bound on the size of a cache line, if it
  812. /// is available. If the value is not available at runtime, then 32 is returned for a 32-bit
  813. /// processor and 64 is returned for a 64-bit processor.
  814. /// \details x86/x32/x64 uses CPUID to determine the value and it is usually accurate. PowerPC
  815. /// and AIX also makes the value available to user space and it is also usually accurate. The
  816. /// ARM processor equivalent is a privileged instruction, so a compile time value is returned.
  817. inline int GetCacheLineSize()
  818. {
  819. return CRYPTOPP_L1_CACHE_LINE_SIZE;
  820. }
  821. #endif // Non-Intel systems
  822. #endif // CRYPTOPP_GENERATE_X64_MASM
  823. // ***************************** Inline ASM Helper ***************************** //
  824. #ifndef CRYPTOPP_DOXYGEN_PROCESSING
  825. #if CRYPTOPP_BOOL_X86 || CRYPTOPP_BOOL_X32 || CRYPTOPP_BOOL_X64
  826. #ifdef CRYPTOPP_GENERATE_X64_MASM
  827. #define AS1(x) x*newline*
  828. #define AS2(x, y) x, y*newline*
  829. #define AS3(x, y, z) x, y, z*newline*
  830. #define ASS(x, y, a, b, c, d) x, y, a*64+b*16+c*4+d*newline*
  831. #define ASL(x) label##x:*newline*
  832. #define ASJ(x, y, z) x label##y*newline*
  833. #define ASC(x, y) x label##y*newline*
  834. #define AS_HEX(y) 0##y##h
  835. #elif defined(_MSC_VER) || defined(__BORLANDC__)
  836. #define AS1(x) __asm {x}
  837. #define AS2(x, y) __asm {x, y}
  838. #define AS3(x, y, z) __asm {x, y, z}
  839. #define ASS(x, y, a, b, c, d) __asm {x, y, (a)*64+(b)*16+(c)*4+(d)}
  840. #define ASL(x) __asm {label##x:}
  841. #define ASJ(x, y, z) __asm {x label##y}
  842. #define ASC(x, y) __asm {x label##y}
  843. #define CRYPTOPP_NAKED __declspec(naked)
  844. #define AS_HEX(y) 0x##y
  845. #else
  846. // define these in two steps to allow arguments to be expanded
  847. #define GNU_AS1(x) #x ";" NEW_LINE
  848. #define GNU_AS2(x, y) #x ", " #y ";" NEW_LINE
  849. #define GNU_AS3(x, y, z) #x ", " #y ", " #z ";" NEW_LINE
  850. #define GNU_ASL(x) "\n" #x ":" NEW_LINE
  851. // clang 5.0.0 and apple clang 9.0.0 don't support numerical backward jumps
  852. #if (CRYPTOPP_LLVM_CLANG_VERSION >= 50000) || (CRYPTOPP_APPLE_CLANG_VERSION >= 90000)
  853. #define GNU_ASJ(x, y, z) ATT_PREFIX ";" NEW_LINE #x " " #y #z ";" NEW_LINE INTEL_PREFIX ";" NEW_LINE
  854. #else
  855. #define GNU_ASJ(x, y, z) #x " " #y #z ";" NEW_LINE
  856. #endif
  857. #define AS1(x) GNU_AS1(x)
  858. #define AS2(x, y) GNU_AS2(x, y)
  859. #define AS3(x, y, z) GNU_AS3(x, y, z)
  860. #define ASS(x, y, a, b, c, d) #x ", " #y ", " #a "*64+" #b "*16+" #c "*4+" #d ";"
  861. #define ASL(x) GNU_ASL(x)
  862. #define ASJ(x, y, z) GNU_ASJ(x, y, z)
  863. #define ASC(x, y) #x " " #y ";"
  864. #define CRYPTOPP_NAKED
  865. #define AS_HEX(y) 0x##y
  866. #endif
  867. #define IF0(y)
  868. #define IF1(y) y
  869. #ifdef CRYPTOPP_GENERATE_X64_MASM
  870. #define ASM_MOD(x, y) ((x) MOD (y))
  871. #define XMMWORD_PTR XMMWORD PTR
  872. #else
  873. // GNU assembler doesn't seem to have mod operator
  874. #define ASM_MOD(x, y) ((x)-((x)/(y))*(y))
  875. // GAS 2.15 doesn't support XMMWORD PTR. it seems necessary only for MASM
  876. #define XMMWORD_PTR
  877. #endif
  878. #if CRYPTOPP_BOOL_X86
  879. #define AS_REG_1 ecx
  880. #define AS_REG_2 edx
  881. #define AS_REG_3 esi
  882. #define AS_REG_4 edi
  883. #define AS_REG_5 eax
  884. #define AS_REG_6 ebx
  885. #define AS_REG_7 ebp
  886. #define AS_REG_1d ecx
  887. #define AS_REG_2d edx
  888. #define AS_REG_3d esi
  889. #define AS_REG_4d edi
  890. #define AS_REG_5d eax
  891. #define AS_REG_6d ebx
  892. #define AS_REG_7d ebp
  893. #define WORD_SZ 4
  894. #define WORD_REG(x) e##x
  895. #define WORD_PTR DWORD PTR
  896. #define AS_PUSH_IF86(x) AS1(push e##x)
  897. #define AS_POP_IF86(x) AS1(pop e##x)
  898. #define AS_JCXZ jecxz
  899. #elif CRYPTOPP_BOOL_X32
  900. #define AS_REG_1 ecx
  901. #define AS_REG_2 edx
  902. #define AS_REG_3 r8d
  903. #define AS_REG_4 r9d
  904. #define AS_REG_5 eax
  905. #define AS_REG_6 r10d
  906. #define AS_REG_7 r11d
  907. #define AS_REG_1d ecx
  908. #define AS_REG_2d edx
  909. #define AS_REG_3d r8d
  910. #define AS_REG_4d r9d
  911. #define AS_REG_5d eax
  912. #define AS_REG_6d r10d
  913. #define AS_REG_7d r11d
  914. #define WORD_SZ 4
  915. #define WORD_REG(x) e##x
  916. #define WORD_PTR DWORD PTR
  917. #define AS_PUSH_IF86(x) AS1(push r##x)
  918. #define AS_POP_IF86(x) AS1(pop r##x)
  919. #define AS_JCXZ jecxz
  920. #elif CRYPTOPP_BOOL_X64
  921. #ifdef CRYPTOPP_GENERATE_X64_MASM
  922. #define AS_REG_1 rcx
  923. #define AS_REG_2 rdx
  924. #define AS_REG_3 r8
  925. #define AS_REG_4 r9
  926. #define AS_REG_5 rax
  927. #define AS_REG_6 r10
  928. #define AS_REG_7 r11
  929. #define AS_REG_1d ecx
  930. #define AS_REG_2d edx
  931. #define AS_REG_3d r8d
  932. #define AS_REG_4d r9d
  933. #define AS_REG_5d eax
  934. #define AS_REG_6d r10d
  935. #define AS_REG_7d r11d
  936. #else
  937. #define AS_REG_1 rdi
  938. #define AS_REG_2 rsi
  939. #define AS_REG_3 rdx
  940. #define AS_REG_4 rcx
  941. #define AS_REG_5 r8
  942. #define AS_REG_6 r9
  943. #define AS_REG_7 r10
  944. #define AS_REG_1d edi
  945. #define AS_REG_2d esi
  946. #define AS_REG_3d edx
  947. #define AS_REG_4d ecx
  948. #define AS_REG_5d r8d
  949. #define AS_REG_6d r9d
  950. #define AS_REG_7d r10d
  951. #endif
  952. #define WORD_SZ 8
  953. #define WORD_REG(x) r##x
  954. #define WORD_PTR QWORD PTR
  955. #define AS_PUSH_IF86(x)
  956. #define AS_POP_IF86(x)
  957. #define AS_JCXZ jrcxz
  958. #endif
  959. // helper macro for stream cipher output
  960. #define AS_XMM_OUTPUT4(labelPrefix, inputPtr, outputPtr, x0, x1, x2, x3, t, p0, p1, p2, p3, increment)\
  961. AS2( test inputPtr, inputPtr)\
  962. ASC( jz, labelPrefix##3)\
  963. AS2( test inputPtr, 15)\
  964. ASC( jnz, labelPrefix##7)\
  965. AS2( pxor xmm##x0, [inputPtr+p0*16])\
  966. AS2( pxor xmm##x1, [inputPtr+p1*16])\
  967. AS2( pxor xmm##x2, [inputPtr+p2*16])\
  968. AS2( pxor xmm##x3, [inputPtr+p3*16])\
  969. AS2( add inputPtr, increment*16)\
  970. ASC( jmp, labelPrefix##3)\
  971. ASL(labelPrefix##7)\
  972. AS2( movdqu xmm##t, [inputPtr+p0*16])\
  973. AS2( pxor xmm##x0, xmm##t)\
  974. AS2( movdqu xmm##t, [inputPtr+p1*16])\
  975. AS2( pxor xmm##x1, xmm##t)\
  976. AS2( movdqu xmm##t, [inputPtr+p2*16])\
  977. AS2( pxor xmm##x2, xmm##t)\
  978. AS2( movdqu xmm##t, [inputPtr+p3*16])\
  979. AS2( pxor xmm##x3, xmm##t)\
  980. AS2( add inputPtr, increment*16)\
  981. ASL(labelPrefix##3)\
  982. AS2( test outputPtr, 15)\
  983. ASC( jnz, labelPrefix##8)\
  984. AS2( movdqa [outputPtr+p0*16], xmm##x0)\
  985. AS2( movdqa [outputPtr+p1*16], xmm##x1)\
  986. AS2( movdqa [outputPtr+p2*16], xmm##x2)\
  987. AS2( movdqa [outputPtr+p3*16], xmm##x3)\
  988. ASC( jmp, labelPrefix##9)\
  989. ASL(labelPrefix##8)\
  990. AS2( movdqu [outputPtr+p0*16], xmm##x0)\
  991. AS2( movdqu [outputPtr+p1*16], xmm##x1)\
  992. AS2( movdqu [outputPtr+p2*16], xmm##x2)\
  993. AS2( movdqu [outputPtr+p3*16], xmm##x3)\
  994. ASL(labelPrefix##9)\
  995. AS2( add outputPtr, increment*16)
  996. #endif // CRYPTOPP_BOOL_X86 || CRYPTOPP_BOOL_X32 || CRYPTOPP_BOOL_X64
  997. #endif // Not CRYPTOPP_DOXYGEN_PROCESSING
  998. NAMESPACE_END
  999. // Issue 340
  1000. #if CRYPTOPP_GCC_DIAGNOSTIC_AVAILABLE
  1001. # pragma GCC diagnostic pop
  1002. #endif
  1003. #endif // CRYPTOPP_CPU_H