1 /* CpuArch.h -- CPU specific code 2 2010-10-26: Igor Pavlov : Public domain */ 3 4 #ifndef __CPU_ARCH_H 5 #define __CPU_ARCH_H 6 7 #include <stdlib.h> 8 #include "Types.h" 9 10 EXTERN_C_BEGIN 11 12 /* 13 MY_CPU_LE means that CPU is LITTLE ENDIAN. 14 If MY_CPU_LE is not defined, we don't know about that property of platform (it can be LITTLE ENDIAN). 15 16 MY_CPU_LE_UNALIGN means that CPU is LITTLE ENDIAN and CPU supports unaligned memory accesses. 17 If MY_CPU_LE_UNALIGN is not defined, we don't know about these properties of platform. 18 */ 19 20 #if defined(_M_X64) || defined(_M_AMD64) || defined(__x86_64__) 21 #define MY_CPU_AMD64 22 #endif 23 24 #if defined(MY_CPU_AMD64) || defined(_M_IA64) 25 #define MY_CPU_64BIT 26 #endif 27 28 #if defined(_M_IX86) || defined(__i386__) 29 #define MY_CPU_X86 30 #endif 31 32 #if defined(MY_CPU_X86) || defined(MY_CPU_AMD64) 33 #define MY_CPU_X86_OR_AMD64 34 #endif 35 36 #if defined(MY_CPU_X86) || defined(_M_ARM) 37 #define MY_CPU_32BIT 38 #endif 39 40 #if defined(_WIN32) && defined(_M_ARM) 41 #define MY_CPU_ARM_LE 42 #endif 43 44 #if defined(_WIN32) && defined(_M_IA64) 45 #define MY_CPU_IA64_LE 46 #endif 47 48 #if defined(MY_CPU_X86_OR_AMD64) 49 #define MY_CPU_LE_UNALIGN 50 #endif 51 52 #if defined(MY_CPU_X86_OR_AMD64) || defined(MY_CPU_ARM_LE) || defined(MY_CPU_IA64_LE) || defined(__ARMEL__) || defined(__MIPSEL__) || defined(__LITTLE_ENDIAN__) 53 #define MY_CPU_LE 54 #endif 55 56 #if defined(__BIG_ENDIAN__) 57 #define MY_CPU_BE 58 #endif 59 60 #if defined(MY_CPU_LE) && defined(MY_CPU_BE) 61 Stop_Compiling_Bad_Endian 62 #endif 63 64 #ifdef MY_CPU_LE_UNALIGN 65 66 #define GetUi16(p) (*(const UInt16 *)(p)) 67 #define GetUi32(p) (*(const UInt32 *)(p)) 68 #define GetUi64(p) (*(const UInt64 *)(p)) 69 #define SetUi16(p, d) *(UInt16 *)(p) = (d); 70 #define SetUi32(p, d) *(UInt32 *)(p) = (d); 71 #define SetUi64(p, d) *(UInt64 *)(p) = (d); 72 73 #else 74 75 #define GetUi16(p) (((const Byte *)(p))[0] | ((UInt16)((const Byte *)(p))[1] << 8)) 76 77 #define GetUi32(p) ( \ 78 ((const Byte *)(p))[0] | \ 79 ((UInt32)((const Byte *)(p))[1] << 8) | \ 80 ((UInt32)((const Byte *)(p))[2] << 16) | \ 81 ((UInt32)((const Byte *)(p))[3] << 24)) 82 83 #define GetUi64(p) (GetUi32(p) | ((UInt64)GetUi32(((const Byte *)(p)) + 4) << 32)) 84 85 #define SetUi16(p, d) { UInt32 _x_ = (d); \ 86 ((Byte *)(p))[0] = (Byte)_x_; \ 87 ((Byte *)(p))[1] = (Byte)(_x_ >> 8); } 88 89 #define SetUi32(p, d) { UInt32 _x_ = (d); \ 90 ((Byte *)(p))[0] = (Byte)_x_; \ 91 ((Byte *)(p))[1] = (Byte)(_x_ >> 8); \ 92 ((Byte *)(p))[2] = (Byte)(_x_ >> 16); \ 93 ((Byte *)(p))[3] = (Byte)(_x_ >> 24); } 94 95 #define SetUi64(p, d) { UInt64 _x64_ = (d); \ 96 SetUi32(p, (UInt32)_x64_); \ 97 SetUi32(((Byte *)(p)) + 4, (UInt32)(_x64_ >> 32)); } 98 99 #endif 100 101 #if defined(MY_CPU_LE_UNALIGN) && defined(_WIN64) && (_MSC_VER >= 1300) 102 103 #pragma intrinsic(_byteswap_ulong) 104 #pragma intrinsic(_byteswap_uint64) 105 #define GetBe32(p) _byteswap_ulong(*(const UInt32 *)(const Byte *)(p)) 106 #define GetBe64(p) _byteswap_uint64(*(const UInt64 *)(const Byte *)(p)) 107 108 #else 109 110 #define GetBe32(p) ( \ 111 ((UInt32)((const Byte *)(p))[0] << 24) | \ 112 ((UInt32)((const Byte *)(p))[1] << 16) | \ 113 ((UInt32)((const Byte *)(p))[2] << 8) | \ 114 ((const Byte *)(p))[3] ) 115 116 #define GetBe64(p) (((UInt64)GetBe32(p) << 32) | GetBe32(((const Byte *)(p)) + 4)) 117 118 #endif 119 120 #define GetBe16(p) (((UInt16)((const Byte *)(p))[0] << 8) | ((const Byte *)(p))[1]) 121 122 123 #ifdef MY_CPU_X86_OR_AMD64 124 125 typedef struct 126 { 127 UInt32 maxFunc; 128 UInt32 vendor[3]; 129 UInt32 ver; 130 UInt32 b; 131 UInt32 c; 132 UInt32 d; 133 } Cx86cpuid; 134 135 enum 136 { 137 CPU_FIRM_INTEL, 138 CPU_FIRM_AMD, 139 CPU_FIRM_VIA 140 }; 141 142 Bool x86cpuid_CheckAndRead(Cx86cpuid *p); 143 int x86cpuid_GetFirm(const Cx86cpuid *p); 144 145 #define x86cpuid_GetFamily(p) (((p)->ver >> 8) & 0xFF00F) 146 #define x86cpuid_GetModel(p) (((p)->ver >> 4) & 0xF00F) 147 #define x86cpuid_GetStepping(p) ((p)->ver & 0xF) 148 149 Bool CPU_Is_InOrder(); 150 Bool CPU_Is_Aes_Supported(); 151 152 #endif 153 154 EXTERN_C_END 155 156 #endif 157