|
| 1 | +// Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | +// Licensed under the MIT License. |
| 3 | + |
| 4 | +#include "core/platform/check_intel.h" |
| 5 | + |
| 6 | +#if (defined(_M_AMD64) || defined(__x86_64__)) |
| 7 | +#if defined(__linux__) |
| 8 | +#include <cpuid.h> |
| 9 | +#elif defined(_WIN32) |
| 10 | +#include <intrin.h> |
| 11 | +#endif |
| 12 | +#endif |
| 13 | + |
| 14 | +namespace onnxruntime { |
| 15 | + |
| 16 | +CheckIntelResult CheckIntel() { |
| 17 | + CheckIntelResult intel_check = {false, false}; |
| 18 | + bool is_intel = false; |
| 19 | + bool is_intel_specified_platform = false; |
| 20 | + |
| 21 | +#if (defined(_M_AMD64) || defined(__x86_64__)) |
| 22 | +#if defined(_WIN32) |
| 23 | + constexpr unsigned int kVendorID_Intel[] = {0x756e6547, 0x6c65746e, 0x49656e69}; // "GenuntelineI" |
| 24 | + constexpr unsigned int kVendorID_IntelSpecifiedPlatformIDs[] = { |
| 25 | + // ExtendedModel, ExtendedFamily, Family Code, and Model Number |
| 26 | + 0xa06a, // MTL |
| 27 | + 0xc065, // ARL-H |
| 28 | + 0xb065 // ARL-U |
| 29 | + }; |
| 30 | + |
| 31 | + int regs_leaf0[4]; |
| 32 | + int regs_leaf1[4]; |
| 33 | + __cpuid(regs_leaf0, 0); |
| 34 | + __cpuid(regs_leaf1, 0x1); |
| 35 | + |
| 36 | + is_intel = |
| 37 | + (kVendorID_Intel[0] == static_cast<unsigned int>(regs_leaf0[1])) && |
| 38 | + (kVendorID_Intel[1] == static_cast<unsigned int>(regs_leaf0[2])) && |
| 39 | + (kVendorID_Intel[2] == static_cast<unsigned int>(regs_leaf0[3])); |
| 40 | + |
| 41 | + if (!is_intel) { |
| 42 | + return intel_check; // if not an Intel CPU, return early |
| 43 | + } |
| 44 | + |
| 45 | + for (auto intel_specified_platform : kVendorID_IntelSpecifiedPlatformIDs) { |
| 46 | + if ((static_cast<unsigned int>(regs_leaf1[0]) >> 4) == intel_specified_platform) { |
| 47 | + is_intel_specified_platform = true; |
| 48 | + break; |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | +#elif defined(__linux__) |
| 53 | + constexpr unsigned int kVendorID_Intel[] = {0x756e6547, 0x6c65746e, 0x49656e69}; // "GenuntelineI" |
| 54 | + unsigned int regs[4] = {0}; |
| 55 | + __get_cpuid(0, ®s[0], ®s[1], ®s[2], ®s[3]); |
| 56 | + |
| 57 | + is_intel = (regs[1] == kVendorID_Intel[0] && |
| 58 | + regs[2] == kVendorID_Intel[1] && |
| 59 | + regs[3] == kVendorID_Intel[2]); |
| 60 | + if (!is_intel) { |
| 61 | + return intel_check; // if not an Intel CPU, return early |
| 62 | + } |
| 63 | + |
| 64 | + __get_cpuid(1, ®s[0], ®s[1], ®s[2], ®s[3]); |
| 65 | + |
| 66 | + unsigned int base_family = (regs[0] >> 8) & 0xF; |
| 67 | + unsigned int base_model = (regs[0] >> 4) & 0xF; |
| 68 | + unsigned int extended_model = (regs[0] >> 16) & 0xF; |
| 69 | + |
| 70 | + unsigned int model = |
| 71 | + (base_family == 0x6 || base_family == 0xF) |
| 72 | + ? (base_model + (extended_model << 4)) |
| 73 | + : base_model; |
| 74 | + |
| 75 | + constexpr unsigned int kVendorID_IntelSpecifiedPlatformIDs[] = { |
| 76 | + // ExtendedModel, ExtendedFamily, Family Code, and Model Number |
| 77 | + 170, // MTL (0xAA) |
| 78 | + 197, // ARL-H (0xC5) |
| 79 | + 198 // ARL-U (0xC6) |
| 80 | + }; |
| 81 | + |
| 82 | + for (auto id : kVendorID_IntelSpecifiedPlatformIDs) { |
| 83 | + if (model == id) { |
| 84 | + is_intel_specified_platform = true; |
| 85 | + break; |
| 86 | + } |
| 87 | + } |
| 88 | +#endif //__linux__ |
| 89 | +#endif // (_M_AMD64) || (__x86_64__) |
| 90 | + |
| 91 | + intel_check.is_intel = is_intel; |
| 92 | + intel_check.is_intel_specified_platform = is_intel_specified_platform; |
| 93 | + |
| 94 | + return intel_check; |
| 95 | +} |
| 96 | + |
| 97 | +} // namespace onnxruntime |
0 commit comments