You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ottery_cpuinfo.c 1.9KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. /* Libottery by Nick Mathewson.
  2. This software has been dedicated to the public domain under the CC0
  3. public domain dedication.
  4. To the extent possible under law, the person who associated CC0 with
  5. libottery has waived all copyright and related or neighboring rights
  6. to libottery.
  7. You should have received a copy of the CC0 legalcode along with this
  8. work in doc/cc0.txt. If not, see
  9. <http://creativecommons.org/publicdomain/zero/1.0/>.
  10. */
  11. #include "ottery-internal.h"
  12. #include <stdint.h>
  13. #if defined(i386) || \
  14. defined(__i386) || \
  15. defined(__M_IX86) || \
  16. defined(_M_IX86)
  17. #define X86
  18. #elif defined(__x86_64) || \
  19. defined(_M_AMD64)
  20. #define X86
  21. #define X86_64
  22. #endif
  23. #if defined(__arm__) || \
  24. defined(_M_ARM)
  25. #define ARM
  26. #endif
  27. #if defined(X86)
  28. #ifdef _MSC_VER
  29. #include <intrin.h>
  30. #define cpuid(a,b) __cpuid((b), (a))
  31. #else
  32. static void
  33. cpuid(int index, int regs[4])
  34. {
  35. unsigned int eax, ebx, ecx, edx;
  36. #ifdef X86_64
  37. __asm("cpuid" : "=a"(eax), "=b" (ebx), "=c"(ecx), "=d"(edx)
  38. : "0"(index));
  39. #else
  40. __asm volatile(
  41. "xchgl %%ebx, %1; cpuid; xchgl %%ebx, %1"
  42. : "=a" (eax), "=r" (ebx), "=c" (ecx), "=d" (edx)
  43. : "0" (index)
  44. : "cc" );
  45. #endif
  46. regs[0] = eax;
  47. regs[1] = ebx;
  48. regs[2] = ecx;
  49. regs[3] = edx;
  50. }
  51. #endif
  52. #endif
  53. static uint32_t disabled_cpu_capabilities = 0;
  54. void
  55. ottery_disable_cpu_capabilities_(uint32_t disable)
  56. {
  57. disabled_cpu_capabilities |= disable;
  58. }
  59. uint32_t
  60. ottery_get_cpu_capabilities_(void)
  61. {
  62. #ifdef X86
  63. uint32_t cap = 0;
  64. int res[4];
  65. cpuid(1, res);
  66. if (res[3] & (1<<26))
  67. cap |= OTTERY_CPUCAP_SIMD;
  68. if (res[2] & (1<<9))
  69. cap |= OTTERY_CPUCAP_SSSE3;
  70. if (res[2] & (1<<25))
  71. cap |= OTTERY_CPUCAP_AES;
  72. if (res[2] & (1<<30))
  73. cap |= OTTERY_CPUCAP_RAND;
  74. #else
  75. uint32_t cap = OTTERY_CPUCAP_SIMD;
  76. #endif
  77. return cap & ~disabled_cpu_capabilities;
  78. }