"Fossies" - the Fresh Open Source Software Archive

Member "src/Crypto/cpu.h" (10 Oct 2018, 13273 Bytes) of package /windows/misc/VeraCrypt_1.23-Hotfix-2_Source.zip:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "cpu.h" see the Fossies "Dox" file reference documentation and the last Fossies "Diffs" side-by-side code changes report: 1.20_Source_vs_1.21_Source.

    1 #ifndef CRYPTOPP_CPU_H
    2 #define CRYPTOPP_CPU_H
    3 
    4 #include "Common/Tcdefs.h"
    5 #include "config.h"
    6 
    7 // Applies to both X86/X32/X64 and ARM32/ARM64
    8 #if defined(CRYPTOPP_LLVM_CLANG_VERSION) || defined(CRYPTOPP_APPLE_CLANG_VERSION) || defined(CRYPTOPP_CLANG_INTEGRATED_ASSEMBLER)
    9     #define NEW_LINE "\n"
   10     #define INTEL_PREFIX ".intel_syntax;"
   11     #define INTEL_NOPREFIX ".intel_syntax;"
   12     #define ATT_PREFIX ".att_syntax;"
   13     #define ATT_NOPREFIX ".att_syntax;"
   14 #elif defined(__GNUC__)
   15     #define NEW_LINE
   16     #define INTEL_PREFIX ".intel_syntax prefix;"
   17     #define INTEL_NOPREFIX ".intel_syntax noprefix;"
   18     #define ATT_PREFIX ".att_syntax prefix;"
   19     #define ATT_NOPREFIX ".att_syntax noprefix;"
   20 #else
   21     #define NEW_LINE
   22     #define INTEL_PREFIX
   23     #define INTEL_NOPREFIX
   24     #define ATT_PREFIX
   25     #define ATT_NOPREFIX
   26 #endif
   27 
   28 #ifdef CRYPTOPP_GENERATE_X64_MASM
   29 
   30 #define CRYPTOPP_X86_ASM_AVAILABLE
   31 #define CRYPTOPP_BOOL_X64 1
   32 #define CRYPTOPP_BOOL_SSE2_ASM_AVAILABLE 1
   33 
   34 #else
   35 
   36 #if CRYPTOPP_BOOL_SSE2_INTRINSICS_AVAILABLE
   37 #if defined(TC_WINDOWS_DRIVER) || defined (_UEFI)
   38 #if defined(__cplusplus)
   39 extern "C" {
   40 #endif
   41 typedef union __declspec(intrin_type) CRYPTOPP_ALIGN_DATA(8) __m64
   42 {
   43     unsigned __int64    m64_u64;
   44     float               m64_f32[2];
   45     __int8              m64_i8[8];
   46     __int16             m64_i16[4];
   47     __int32             m64_i32[2];    
   48     __int64             m64_i64;
   49     unsigned __int8     m64_u8[8];
   50     unsigned __int16    m64_u16[4];
   51     unsigned __int32    m64_u32[2];
   52 } __m64;
   53 
   54 typedef union __declspec(intrin_type) CRYPTOPP_ALIGN_DATA(16) __m128 {
   55      float               m128_f32[4];
   56      unsigned __int64    m128_u64[2];
   57      __int8              m128_i8[16];
   58      __int16             m128_i16[8];
   59      __int32             m128_i32[4];
   60      __int64             m128_i64[2];
   61      unsigned __int8     m128_u8[16];
   62      unsigned __int16    m128_u16[8];
   63      unsigned __int32    m128_u32[4];
   64  } __m128;
   65  
   66 typedef union __declspec(intrin_type) CRYPTOPP_ALIGN_DATA(16) __m128i {
   67     __int8              m128i_i8[16];
   68     __int16             m128i_i16[8];
   69     __int32             m128i_i32[4];    
   70     __int64             m128i_i64[2];
   71     unsigned __int8     m128i_u8[16];
   72     unsigned __int16    m128i_u16[8];
   73     unsigned __int32    m128i_u32[4];
   74     unsigned __int64    m128i_u64[2];
   75 } __m128i;
   76 
   77 typedef struct __declspec(intrin_type) CRYPTOPP_ALIGN_DATA(16) __m128d {
   78     double              m128d_f64[2];
   79 } __m128d;
   80 
   81 #define _MM_SHUFFLE2(x,y) (((x)<<1) | (y))
   82 
   83 extern void  _m_empty(void);
   84 extern int _mm_extract_epi16(__m128i _A, int _Imm);
   85 extern __m128i _mm_load_si128(__m128i const*_P);
   86 extern __m128i _mm_xor_si128(__m128i _A, __m128i _B);
   87 extern __m128i _mm_cvtsi64_si128(__int64);
   88 extern __m128i _mm_unpacklo_epi64(__m128i _A, __m128i _B);
   89 extern void _mm_store_si128(__m128i *_P, __m128i _B);
   90 extern __m64 _m_pxor(__m64 _MM1, __m64 _MM2);
   91 extern __m128i _mm_set_epi64(__m64 _Q1, __m64 _Q0);
   92 extern __m128i _mm_setr_epi32(int _I0, int _I1, int _I2, int _I3);
   93 extern __m128i _mm_loadu_si128(__m128i const*_P);
   94 extern __m128i _mm_set_epi32(int _I3, int _I2, int _I1, int _I0);
   95 extern __m128i _mm_set1_epi32(int _I);
   96 extern void _mm_storeu_si128(__m128i *_P, __m128i _B);
   97 extern __m128i _mm_or_si128(__m128i _A, __m128i _B);
   98 extern __m128i _mm_slli_epi32(__m128i _A, int _Count);
   99 extern __m128i _mm_srli_epi32(__m128i _A, int _Count);
  100 extern __m128i _mm_add_epi32(__m128i _A, __m128i _B);
  101 extern __m128i _mm_sub_epi32(__m128i _A, __m128i _B);
  102 extern __m128i _mm_or_si128(__m128i _A, __m128i _B);
  103 extern __m128i _mm_and_si128(__m128i _A, __m128i _B);
  104 extern __m128i _mm_andnot_si128(__m128i _A, __m128i _B);
  105 extern __m128i _mm_shufflehi_epi16(__m128i _A, int _Imm);
  106 extern __m128i _mm_shufflelo_epi16(__m128i _A, int _Imm);
  107 extern __m128i _mm_unpacklo_epi32(__m128i _A, __m128i _B);
  108 extern __m128i _mm_unpackhi_epi32(__m128i _A, __m128i _B);
  109 extern __m128i _mm_unpackhi_epi64(__m128i _A, __m128i _B);
  110 extern __m128i _mm_srli_epi16(__m128i _A, int _Count);
  111 extern __m128i _mm_slli_epi16(__m128i _A, int _Count);
  112 #define _mm_xor_si64      _m_pxor
  113 #define _mm_empty         _m_empty
  114 #define _MM_SHUFFLE(fp3,fp2,fp1,fp0) (((fp3) << 6) | ((fp2) << 4) | \
  115                                      ((fp1) << 2) | ((fp0)))
  116 #if defined(__cplusplus)
  117 }
  118 #endif
  119 #else
  120 #include <mmintrin.h>
  121 #include <emmintrin.h>
  122 #endif
  123 #endif
  124 
  125 #if CRYPTOPP_BOOL_AESNI_INTRINSICS_AVAILABLE
  126 #if defined(__SSSE3__) || defined(__INTEL_COMPILER)
  127 #if defined(TC_WINDOWS_DRIVER) || defined (_UEFI)
  128 #if defined(__cplusplus)
  129 extern "C" {
  130 #endif
  131 extern __m128i _mm_shuffle_epi8 (__m128i a, __m128i b);
  132 #if defined(__cplusplus)
  133 }
  134 #endif
  135 #else
  136 #include <tmmintrin.h>
  137 #endif
  138 #endif
  139 
  140 #if defined(__SSE4_1__) || defined(__INTEL_COMPILER) || defined(_MSC_VER)
  141 #if defined(TC_WINDOWS_DRIVER) || defined (_UEFI)
  142 #if defined(__cplusplus)
  143 extern "C" {
  144 #endif
  145 extern int   _mm_extract_epi32(__m128i src, const int ndx);
  146 extern __m128i _mm_insert_epi32(__m128i dst, int s, const int ndx);
  147 #if defined(_M_X64)
  148 extern __m128i _mm_insert_epi64(__m128i dst, __int64 s, const int ndx);
  149 #endif
  150 #if defined(__cplusplus)
  151 }
  152 #endif
  153 #else
  154 #include <smmintrin.h>
  155 #endif
  156 #endif
  157 
  158 #if (defined(__AES__) && defined(__PCLMUL__)) || defined(__INTEL_COMPILER) || CRYPTOPP_BOOL_AESNI_INTRINSICS_AVAILABLE
  159 #if defined(TC_WINDOWS_DRIVER) || defined (_UEFI)
  160 #if defined(__cplusplus)
  161 extern "C" {
  162 #endif
  163 extern __m128i _mm_clmulepi64_si128(__m128i v1, __m128i v2, 
  164                         const int imm8);
  165 extern __m128i _mm_aeskeygenassist_si128(__m128i ckey, const int rcon);
  166 extern __m128i _mm_aesimc_si128(__m128i v);
  167 extern __m128i _mm_aesenc_si128(__m128i v, __m128i rkey);
  168 extern __m128i _mm_aesenclast_si128(__m128i v, __m128i rkey);
  169 extern __m128i _mm_aesdec_si128(__m128i v, __m128i rkey);
  170 extern __m128i _mm_aesdeclast_si128(__m128i v, __m128i rkey);
  171 #if defined(__cplusplus)
  172 }
  173 #endif
  174 #else
  175 #include <wmmintrin.h>
  176 #endif
  177 #endif
  178 #endif
  179 
  180 #if CRYPTOPP_BOOL_X86 || CRYPTOPP_BOOL_X32 || CRYPTOPP_BOOL_X64
  181 
  182 #if defined(__cplusplus)
  183 extern "C" {
  184 #endif
  185 
  186 #define CRYPTOPP_CPUID_AVAILABLE
  187 
  188 // these should not be used directly
  189 extern volatile int g_x86DetectionDone;
  190 extern volatile int g_hasSSE2;
  191 extern volatile int g_hasISSE;
  192 extern volatile int g_hasMMX;
  193 extern volatile int g_hasAVX;
  194 extern volatile int g_hasAVX2;
  195 extern volatile int g_hasBMI2;
  196 extern volatile int g_hasSSE42;
  197 extern volatile int g_hasSSE41;
  198 extern volatile int g_hasSSSE3;
  199 extern volatile int g_hasAESNI;
  200 extern volatile int g_hasCLMUL;
  201 extern volatile int g_isP4;
  202 extern volatile int g_isIntel;
  203 extern volatile int g_isAMD;
  204 extern volatile uint32 g_cacheLineSize;
  205 void DetectX86Features(); // must be called at the start of the program/driver
  206 int CpuId(uint32 input, uint32 *output);
  207 // disable all CPU extended features (e.g. SSE, AVX, AES) that may have
  208 // been enabled by DetectX86Features.
  209 void DisableCPUExtendedFeatures (); 
  210 
  211 #define HasSSE2()   g_hasSSE2
  212 #define HasISSE()   g_hasISSE
  213 #define HasMMX()    g_hasMMX
  214 #define HasSSE42() g_hasSSE42
  215 #define HasSSE41() g_hasSSE41
  216 #define HasSAVX() g_hasAVX
  217 #define HasSAVX2() g_hasAVX2
  218 #define HasSBMI2() g_hasBMI2
  219 #define HasSSSE3() g_hasSSSE3
  220 #define HasAESNI() g_hasAESNI
  221 #define HasCLMUL() g_hasCLMUL
  222 #define IsP4() g_isP4
  223 #define IsCpuIntel() g_isIntel
  224 #define IsCpuAMD() g_isAMD
  225 #define GetCacheLineSize() g_cacheLineSize
  226 
  227 #if defined(__cplusplus)
  228 }
  229 #endif
  230 
  231 #else
  232 
  233 #define GetCacheLineSize()  CRYPTOPP_L1_CACHE_LINE_SIZE
  234 
  235 #endif
  236 
  237 #endif
  238 
  239 #if CRYPTOPP_BOOL_X86 || CRYPTOPP_BOOL_X32 || CRYPTOPP_BOOL_X64
  240 
  241 #ifdef CRYPTOPP_GENERATE_X64_MASM
  242     #define AS1(x) x*newline*
  243     #define AS2(x, y) x, y*newline*
  244     #define AS3(x, y, z) x, y, z*newline*
  245     #define ASS(x, y, a, b, c, d) x, y, a*64+b*16+c*4+d*newline*
  246     #define ASL(x) label##x:*newline*
  247     #define ASJ(x, y, z) x label##y*newline*
  248     #define ASC(x, y) x label##y*newline*
  249     #define AS_HEX(y) 0##y##h
  250 #elif defined(_MSC_VER) || defined(__BORLANDC__)
  251     #define CRYPTOPP_MS_STYLE_INLINE_ASSEMBLY
  252     #define AS1(x) __asm {x}
  253     #define AS2(x, y) __asm {x, y}
  254     #define AS3(x, y, z) __asm {x, y, z}
  255     #define ASS(x, y, a, b, c, d) __asm {x, y, (a)*64+(b)*16+(c)*4+(d)}
  256     #define ASL(x) __asm {label##x:}
  257     #define ASJ(x, y, z) __asm {x label##y}
  258     #define ASC(x, y) __asm {x label##y}
  259     #define CRYPTOPP_NAKED __declspec(naked)
  260     #define AS_HEX(y) 0x##y
  261 #else
  262     #define CRYPTOPP_GNU_STYLE_INLINE_ASSEMBLY
  263 
  264     // define these in two steps to allow arguments to be expanded
  265     #define GNU_AS1(x) #x ";" NEW_LINE
  266     #define GNU_AS2(x, y) #x ", " #y ";" NEW_LINE
  267     #define GNU_AS3(x, y, z) #x ", " #y ", " #z ";" NEW_LINE
  268     #define GNU_ASL(x) "\n" #x ":" NEW_LINE
  269     #define GNU_ASJ(x, y, z) #x " " #y #z ";" NEW_LINE
  270     #define AS1(x) GNU_AS1(x)
  271     #define AS2(x, y) GNU_AS2(x, y)
  272     #define AS3(x, y, z) GNU_AS3(x, y, z)
  273     #define ASS(x, y, a, b, c, d) #x ", " #y ", " #a "*64+" #b "*16+" #c "*4+" #d ";"
  274     #define ASL(x) GNU_ASL(x)
  275     #define ASJ(x, y, z) GNU_ASJ(x, y, z)
  276     #define ASC(x, y) #x " " #y ";"
  277     #define CRYPTOPP_NAKED
  278     #define AS_HEX(y) 0x##y
  279 #endif
  280 
  281 #define IF0(y)
  282 #define IF1(y) y
  283 
  284 #ifdef CRYPTOPP_GENERATE_X64_MASM
  285 #define ASM_MOD(x, y) ((x) MOD (y))
  286 #define XMMWORD_PTR XMMWORD PTR
  287 #else
  288 // GNU assembler doesn't seem to have mod operator
  289 #define ASM_MOD(x, y) ((x)-((x)/(y))*(y))
  290 // GAS 2.15 doesn't support XMMWORD PTR. it seems necessary only for MASM
  291 #define XMMWORD_PTR
  292 #endif
  293 
  294 #if CRYPTOPP_BOOL_X86
  295     #define AS_REG_1 ecx
  296     #define AS_REG_2 edx
  297     #define AS_REG_3 esi
  298     #define AS_REG_4 edi
  299     #define AS_REG_5 eax
  300     #define AS_REG_6 ebx
  301     #define AS_REG_7 ebp
  302     #define AS_REG_1d ecx
  303     #define AS_REG_2d edx
  304     #define AS_REG_3d esi
  305     #define AS_REG_4d edi
  306     #define AS_REG_5d eax
  307     #define AS_REG_6d ebx
  308     #define AS_REG_7d ebp
  309     #define WORD_SZ 4
  310     #define WORD_REG(x) e##x
  311     #define WORD_PTR DWORD PTR
  312     #define AS_PUSH_IF86(x) AS1(push e##x)
  313     #define AS_POP_IF86(x) AS1(pop e##x)
  314     #define AS_JCXZ jecxz
  315 #elif CRYPTOPP_BOOL_X32
  316     #define AS_REG_1 ecx
  317     #define AS_REG_2 edx
  318     #define AS_REG_3 r8d
  319     #define AS_REG_4 r9d
  320     #define AS_REG_5 eax
  321     #define AS_REG_6 r10d
  322     #define AS_REG_7 r11d
  323     #define AS_REG_1d ecx
  324     #define AS_REG_2d edx
  325     #define AS_REG_3d r8d
  326     #define AS_REG_4d r9d
  327     #define AS_REG_5d eax
  328     #define AS_REG_6d r10d
  329     #define AS_REG_7d r11d
  330     #define WORD_SZ 4
  331     #define WORD_REG(x) e##x
  332     #define WORD_PTR DWORD PTR
  333     #define AS_PUSH_IF86(x) AS1(push r##x)
  334     #define AS_POP_IF86(x) AS1(pop r##x)
  335     #define AS_JCXZ jecxz
  336 #elif CRYPTOPP_BOOL_X64
  337     #ifdef CRYPTOPP_GENERATE_X64_MASM
  338         #define AS_REG_1 rcx
  339         #define AS_REG_2 rdx
  340         #define AS_REG_3 r8
  341         #define AS_REG_4 r9
  342         #define AS_REG_5 rax
  343         #define AS_REG_6 r10
  344         #define AS_REG_7 r11
  345         #define AS_REG_1d ecx
  346         #define AS_REG_2d edx
  347         #define AS_REG_3d r8d
  348         #define AS_REG_4d r9d
  349         #define AS_REG_5d eax
  350         #define AS_REG_6d r10d
  351         #define AS_REG_7d r11d
  352     #else
  353         #define AS_REG_1 rdi
  354         #define AS_REG_2 rsi
  355         #define AS_REG_3 rdx
  356         #define AS_REG_4 rcx
  357         #define AS_REG_5 r8
  358         #define AS_REG_6 r9
  359         #define AS_REG_7 r10
  360         #define AS_REG_1d edi
  361         #define AS_REG_2d esi
  362         #define AS_REG_3d edx
  363         #define AS_REG_4d ecx
  364         #define AS_REG_5d r8d
  365         #define AS_REG_6d r9d
  366         #define AS_REG_7d r10d
  367     #endif
  368     #define WORD_SZ 8
  369     #define WORD_REG(x) r##x
  370     #define WORD_PTR QWORD PTR
  371     #define AS_PUSH_IF86(x)
  372     #define AS_POP_IF86(x)
  373     #define AS_JCXZ jrcxz
  374 #endif
  375 
  376 // helper macro for stream cipher output
  377 #define AS_XMM_OUTPUT4(labelPrefix, inputPtr, outputPtr, x0, x1, x2, x3, t, p0, p1, p2, p3, increment)\
  378     AS2(    test    inputPtr, inputPtr)\
  379     ASC(    jz,     labelPrefix##3)\
  380     AS2(    test    inputPtr, 15)\
  381     ASC(    jnz,    labelPrefix##7)\
  382     AS2(    pxor    xmm##x0, [inputPtr+p0*16])\
  383     AS2(    pxor    xmm##x1, [inputPtr+p1*16])\
  384     AS2(    pxor    xmm##x2, [inputPtr+p2*16])\
  385     AS2(    pxor    xmm##x3, [inputPtr+p3*16])\
  386     AS2(    add     inputPtr, increment*16)\
  387     ASC(    jmp,    labelPrefix##3)\
  388     ASL(labelPrefix##7)\
  389     AS2(    movdqu  xmm##t, [inputPtr+p0*16])\
  390     AS2(    pxor    xmm##x0, xmm##t)\
  391     AS2(    movdqu  xmm##t, [inputPtr+p1*16])\
  392     AS2(    pxor    xmm##x1, xmm##t)\
  393     AS2(    movdqu  xmm##t, [inputPtr+p2*16])\
  394     AS2(    pxor    xmm##x2, xmm##t)\
  395     AS2(    movdqu  xmm##t, [inputPtr+p3*16])\
  396     AS2(    pxor    xmm##x3, xmm##t)\
  397     AS2(    add     inputPtr, increment*16)\
  398     ASL(labelPrefix##3)\
  399     AS2(    test    outputPtr, 15)\
  400     ASC(    jnz,    labelPrefix##8)\
  401     AS2(    movdqa  [outputPtr+p0*16], xmm##x0)\
  402     AS2(    movdqa  [outputPtr+p1*16], xmm##x1)\
  403     AS2(    movdqa  [outputPtr+p2*16], xmm##x2)\
  404     AS2(    movdqa  [outputPtr+p3*16], xmm##x3)\
  405     ASC(    jmp,    labelPrefix##9)\
  406     ASL(labelPrefix##8)\
  407     AS2(    movdqu  [outputPtr+p0*16], xmm##x0)\
  408     AS2(    movdqu  [outputPtr+p1*16], xmm##x1)\
  409     AS2(    movdqu  [outputPtr+p2*16], xmm##x2)\
  410     AS2(    movdqu  [outputPtr+p3*16], xmm##x3)\
  411     ASL(labelPrefix##9)\
  412     AS2(    add     outputPtr, increment*16)
  413 
  414 #endif  //  X86/X32/X64
  415 
  416 #if defined(TC_WINDOWS_DRIVER) || defined (_UEFI)
  417 #ifdef  __cplusplus
  418 extern "C" {
  419 #endif
  420 extern unsigned __int64 __cdecl _rotl64(unsigned __int64,int);
  421 extern unsigned __int64 __cdecl _rotr64(unsigned __int64,int);
  422 extern unsigned int __cdecl _rotl(unsigned int,int);
  423 extern unsigned int __cdecl _rotr(unsigned int,int);
  424 extern unsigned char _rotr8(unsigned char value, unsigned char shift);
  425 extern unsigned short _rotr16(unsigned short value, unsigned char shift);
  426 extern unsigned char _rotl8(unsigned char value, unsigned char shift);
  427 extern unsigned short _rotl16(unsigned short value, unsigned char shift);
  428 #ifdef  __cplusplus
  429 }
  430 #endif
  431 #endif
  432 
  433 #endif