/generators/ir2c/static/cpuid.c

https://github.com/enzienaudio/hvcc · C · 142 lines · 85 code · 13 blank · 44 comment · 17 complexity · 5e9af5d98527918ec46506e5184b8014 MD5 · raw file

  1. /* cpuid.c
  2. *
  3. * Author : Alexander J. Yee
  4. * Date Created : 01/19/2012
  5. * Last Modified : 01/25/2012
  6. *
  7. *
  8. *
  9. * And of course... The typical copyright stuff...
  10. *
  11. * Redistribution of this program in both source or binary, regardless of
  12. * form, with or without modification is permitted as long as the following
  13. * conditions are met:
  14. * 1. This copyright notice is maintained either inline in the source
  15. * or distributed with the binary.
  16. * 2. A list of all contributing authors along with their contributions
  17. * is included either inline in the source or distributed with the
  18. * binary.
  19. * 3. The following disclaimer is maintained either inline in the
  20. * source or distributed with the binary.
  21. *
  22. * Disclaimer:
  23. * This software is provided "as is", without any guarantee made to its
  24. * suitability or fitness for any particular use. It may contain bugs so use
  25. * of this program is at your own risk. I take no responsibility for any
  26. * damage that may unintentionally be caused through its use.
  27. */
  28. // https://github.com/Mysticial/Flops/blob/e571da6e94f7b6d2d1a90e87b19398c5c4de4375/version1/source/cpuid.c
  29. // http://www.sandpile.org/x86/cpuid.htm
  30. #ifndef _cpuid_c
  31. #define _cpuid_c
  32. #include <stdio.h>
  33. //#include <omp.h>
  34. #include "cpuid.h"
  35. ////////////////////////////////////////////////////////////////////////////////
  36. ////////////////////////////////////////////////////////////////////////////////
  37. ////////////////////////////////////////////////////////////////////////////////
  38. ////////////////////////////////////////////////////////////////////////////////
  39. #ifdef WIN32
  40. #else
  41. void cpuid(int *info,int x){
  42. int ax,bx,cx,dx;
  43. __asm__ __volatile__ ("cpuid": "=a" (ax), "=b" (bx), "=c" (cx), "=d" (dx) : "a" (x));
  44. info[0] = ax;
  45. info[1] = bx;
  46. info[2] = cx;
  47. info[3] = dx;
  48. }
  49. #endif
  50. ////////////////////////////////////////////////////////////////////////////////
  51. ////////////////////////////////////////////////////////////////////////////////
  52. ////////////////////////////////////////////////////////////////////////////////
  53. ////////////////////////////////////////////////////////////////////////////////
  54. void cpuid_print_name(){
  55. int name[13];
  56. cpuid(name + 0,0x80000002);
  57. cpuid(name + 4,0x80000003);
  58. cpuid(name + 8,0x80000004);
  59. name[12] = '\0';
  60. printf("CPU Name = %s\n",(char*)name);
  61. // printf("Threads = %d\n",omp_get_max_threads());
  62. printf("\n");
  63. }
  64. void cpuid_print_exts(){
  65. int x64 = 0;
  66. int MMX = 0;
  67. int SSE = 0;
  68. int SSE2 = 0;
  69. int SSE3 = 0;
  70. int SSSE3 = 0;
  71. int SSE41 = 0;
  72. int SSE42 = 0;
  73. int SSE4a = 0;
  74. int AVX = 0;
  75. int AVX2 = 0;
  76. int XOP = 0;
  77. int FMA3 = 0;
  78. int FMA4 = 0;
  79. int info[4];
  80. cpuid(info, 0);
  81. int nIds = info[0];
  82. cpuid(info, 0x80000000);
  83. int nExIds = info[0];
  84. // Detect Instruction Set
  85. if (nIds >= 1){
  86. cpuid(info,0x00000001);
  87. MMX = (info[3] & ((int)1 << 23)) != 0;
  88. SSE = (info[3] & ((int)1 << 25)) != 0;
  89. SSE2 = (info[3] & ((int)1 << 26)) != 0;
  90. SSE3 = (info[2] & ((int)1 << 0)) != 0;
  91. SSSE3 = (info[2] & ((int)1 << 9)) != 0;
  92. SSE41 = (info[2] & ((int)1 << 19)) != 0;
  93. SSE42 = (info[2] & ((int)1 << 20)) != 0;
  94. AVX = (info[2] & ((int)1 << 28)) != 0;
  95. FMA3 = (info[2] & ((int)1 << 12)) != 0;
  96. }
  97. if (nExIds >= 0x00000007){
  98. cpuid(info,0x00000007);
  99. AVX2 = (info[2] & ((int)1 << 5)) != 0;
  100. }
  101. if (nExIds >= 0x80000001){
  102. cpuid(info,0x80000001);
  103. x64 = (info[3] & ((int)1 << 29)) != 0;
  104. SSE4a = (info[2] & ((int)1 << 6)) != 0;
  105. FMA4 = (info[2] & ((int)1 << 16)) != 0;
  106. XOP = (info[2] & ((int)1 << 11)) != 0;
  107. }
  108. printf("Hardware Features:\n");
  109. printf("x64 = %d\n",x64);
  110. printf("MMX = %d\n",MMX);
  111. printf("SSE = %d\n",SSE);
  112. printf("SSE2 = %d\n",SSE2);
  113. printf("SSE3 = %d\n",SSE3);
  114. printf("SSSE3 = %d\n",SSSE3);
  115. printf("SSE4a = %d\n",SSE4a);
  116. printf("SSE41 = %d\n",SSE41);
  117. printf("SSE42 = %d\n",SSE42);
  118. printf("AVX = %d\n",AVX);
  119. printf("AVX2 = %d\n",AVX2);
  120. printf("FMA3 = %d\n",FMA3);
  121. printf("FMA4 = %d\n",FMA4);
  122. printf("XOP = %d\n",XOP);
  123. printf("\n");
  124. }
  125. ////////////////////////////////////////////////////////////////////////////////
  126. ////////////////////////////////////////////////////////////////////////////////
  127. ////////////////////////////////////////////////////////////////////////////////
  128. ////////////////////////////////////////////////////////////////////////////////
  129. #endif