x86cpu.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. /********************************************************************
  2. * *
  3. * THIS FILE IS PART OF THE OggTheora SOFTWARE CODEC SOURCE CODE. *
  4. * USE, DISTRIBUTION AND REPRODUCTION OF THIS LIBRARY SOURCE IS *
  5. * GOVERNED BY A BSD-STYLE SOURCE LICENSE INCLUDED WITH THIS SOURCE *
  6. * IN 'COPYING'. PLEASE READ THESE TERMS BEFORE DISTRIBUTING. *
  7. * *
  8. * THE Theora SOURCE CODE IS COPYRIGHT (C) 2002-2009 *
  9. * by the Xiph.Org Foundation and contributors http://www.xiph.org/ *
  10. * *
  11. ********************************************************************
  12. CPU capability detection for x86 processors.
  13. Originally written by Rudolf Marek.
  14. function:
  15. last mod: $Id$
  16. ********************************************************************/
  17. #include "x86cpu.h"
  18. #if !defined(OC_X86_ASM)
  19. ogg_uint32_t oc_cpu_flags_get(void){
  20. return 0;
  21. }
  22. #else
  23. /*Why does MSVC need this complicated rigamarole?
  24. At this point I honestly do not care.*/
  25. /*Visual C cpuid helper function.
  26. For VS2005 we could as well use the _cpuid builtin, but that wouldn't work
  27. for VS2003 users, so we do it in inline assembler.*/
  28. static void oc_cpuid_helper(ogg_uint32_t _cpu_info[4],ogg_uint32_t _op){
  29. _asm{
  30. mov eax,[_op]
  31. mov esi,_cpu_info
  32. cpuid
  33. mov [esi+0],eax
  34. mov [esi+4],ebx
  35. mov [esi+8],ecx
  36. mov [esi+12],edx
  37. }
  38. }
  39. # define cpuid(_op,_eax,_ebx,_ecx,_edx) \
  40. do{ \
  41. ogg_uint32_t cpu_info[4]; \
  42. oc_cpuid_helper(cpu_info,_op); \
  43. (_eax)=cpu_info[0]; \
  44. (_ebx)=cpu_info[1]; \
  45. (_ecx)=cpu_info[2]; \
  46. (_edx)=cpu_info[3]; \
  47. }while(0)
  48. static void oc_detect_cpuid_helper(ogg_uint32_t *_eax,ogg_uint32_t *_ebx){
  49. _asm{
  50. pushfd
  51. pushfd
  52. pop eax
  53. mov ebx,eax
  54. xor eax,200000h
  55. push eax
  56. popfd
  57. pushfd
  58. pop eax
  59. popfd
  60. mov ecx,_eax
  61. mov [ecx],eax
  62. mov ecx,_ebx
  63. mov [ecx],ebx
  64. }
  65. }
  66. static ogg_uint32_t oc_parse_intel_flags(ogg_uint32_t _edx,ogg_uint32_t _ecx){
  67. ogg_uint32_t flags;
  68. /*If there isn't even MMX, give up.*/
  69. if(!(_edx&0x00800000))return 0;
  70. flags=OC_CPU_X86_MMX;
  71. if(_edx&0x02000000)flags|=OC_CPU_X86_MMXEXT|OC_CPU_X86_SSE;
  72. if(_edx&0x04000000)flags|=OC_CPU_X86_SSE2;
  73. if(_ecx&0x00000001)flags|=OC_CPU_X86_PNI;
  74. if(_ecx&0x00000100)flags|=OC_CPU_X86_SSSE3;
  75. if(_ecx&0x00080000)flags|=OC_CPU_X86_SSE4_1;
  76. if(_ecx&0x00100000)flags|=OC_CPU_X86_SSE4_2;
  77. return flags;
  78. }
  79. static ogg_uint32_t oc_parse_amd_flags(ogg_uint32_t _edx,ogg_uint32_t _ecx){
  80. ogg_uint32_t flags;
  81. /*If there isn't even MMX, give up.*/
  82. if(!(_edx&0x00800000))return 0;
  83. flags=OC_CPU_X86_MMX;
  84. if(_edx&0x00400000)flags|=OC_CPU_X86_MMXEXT;
  85. if(_edx&0x80000000)flags|=OC_CPU_X86_3DNOW;
  86. if(_edx&0x40000000)flags|=OC_CPU_X86_3DNOWEXT;
  87. if(_ecx&0x00000040)flags|=OC_CPU_X86_SSE4A;
  88. if(_ecx&0x00000800)flags|=OC_CPU_X86_SSE5;
  89. return flags;
  90. }
  91. ogg_uint32_t oc_cpu_flags_get(void){
  92. ogg_uint32_t flags;
  93. ogg_uint32_t eax;
  94. ogg_uint32_t ebx;
  95. ogg_uint32_t ecx;
  96. ogg_uint32_t edx;
  97. # if !defined(__amd64__)&&!defined(__x86_64__)
  98. /*Not all x86-32 chips support cpuid, so we have to check.*/
  99. oc_detect_cpuid_helper(&eax,&ebx);
  100. /*No cpuid.*/
  101. if(eax==ebx)return 0;
  102. # endif
  103. cpuid(0,eax,ebx,ecx,edx);
  104. /* l e t n I e n i u n e G*/
  105. if(ecx==0x6C65746E&&edx==0x49656E69&&ebx==0x756E6547||
  106. /* 6 8 x M T e n i u n e G*/
  107. ecx==0x3638784D&&edx==0x54656E69&&ebx==0x756E6547){
  108. int family;
  109. int model;
  110. /*Intel, Transmeta (tested with Crusoe TM5800):*/
  111. cpuid(1,eax,ebx,ecx,edx);
  112. flags=oc_parse_intel_flags(edx,ecx);
  113. family=(eax>>8)&0xF;
  114. model=(eax>>4)&0xF;
  115. /*The SSE unit on the Pentium M and Core Duo is much slower than the MMX
  116. unit, so don't use it.*/
  117. if(family==6&&(model==9||model==13||model==14)){
  118. flags&=~(OC_CPU_X86_SSE2|OC_CPU_X86_PNI);
  119. }
  120. }
  121. /* D M A c i t n e h t u A*/
  122. else if(ecx==0x444D4163&&edx==0x69746E65&&ebx==0x68747541||
  123. /* C S N y b e d o e G*/
  124. ecx==0x43534e20&&edx==0x79622065&&ebx==0x646f6547){
  125. /*AMD, Geode:*/
  126. cpuid(0x80000000,eax,ebx,ecx,edx);
  127. if(eax<0x80000001)flags=0;
  128. else{
  129. cpuid(0x80000001,eax,ebx,ecx,edx);
  130. flags=oc_parse_amd_flags(edx,ecx);
  131. }
  132. /*Also check for SSE.*/
  133. cpuid(1,eax,ebx,ecx,edx);
  134. flags|=oc_parse_intel_flags(edx,ecx);
  135. }
  136. /*Technically some VIA chips can be configured in the BIOS to return any
  137. string here the user wants.
  138. There is a special detection method that can be used to identify such
  139. processors, but in my opinion, if the user really wants to change it, they
  140. deserve what they get.*/
  141. /* s l u a H r u a t n e C*/
  142. else if(ecx==0x736C7561&&edx==0x48727561&&ebx==0x746E6543){
  143. /*VIA:*/
  144. /*I only have documentation for the C7 (Esther) and Isaiah (forthcoming)
  145. chips (thanks to the engineers from Centaur Technology who provided it).
  146. These chips support Intel-like cpuid info.
  147. The C3-2 (Nehemiah) cores appear to, as well.*/
  148. cpuid(1,eax,ebx,ecx,edx);
  149. flags=oc_parse_intel_flags(edx,ecx);
  150. if(eax>=0x80000001){
  151. /*The (non-Nehemiah) C3 processors support AMD-like cpuid info.
  152. We need to check this even if the Intel test succeeds to pick up 3DNow!
  153. support on these processors.
  154. Unlike actual AMD processors, we cannot _rely_ on this info, since
  155. some cores (e.g., the 693 stepping of the Nehemiah) claim to support
  156. this function, yet return edx=0, despite the Intel test indicating
  157. MMX support.
  158. Therefore the features detected here are strictly added to those
  159. detected by the Intel test.*/
  160. /*TODO: How about earlier chips?*/
  161. cpuid(0x80000001,eax,ebx,ecx,edx);
  162. /*Note: As of the C7, this function returns Intel-style extended feature
  163. flags, not AMD-style.
  164. Currently, this only defines bits 11, 20, and 29 (0x20100800), which
  165. do not conflict with any of the AMD flags we inspect.
  166. For the remaining bits, Intel tells us, "Do not count on their value",
  167. but VIA assures us that they will all be zero (at least on the C7 and
  168. Isaiah chips).
  169. In the (unlikely) event a future processor uses bits 18, 19, 30, or 31
  170. (0xC0C00000) for something else, we will have to add code to detect
  171. the model to decide when it is appropriate to inspect them.*/
  172. flags|=oc_parse_amd_flags(edx,ecx);
  173. }
  174. }
  175. else{
  176. /*Implement me.*/
  177. flags=0;
  178. }
  179. return flags;
  180. }
  181. #endif