x86cpu.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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. # if defined(__amd64__)||defined(__x86_64__)
  24. /*On x86-64, gcc seems to be able to figure out how to save %rbx for us when
  25. compiling with -fPIC.*/
  26. # define cpuid(_op,_eax,_ebx,_ecx,_edx) \
  27. __asm__ __volatile__( \
  28. "cpuid\n\t" \
  29. :[eax]"=a"(_eax),[ebx]"=b"(_ebx),[ecx]"=c"(_ecx),[edx]"=d"(_edx) \
  30. :"a"(_op) \
  31. :"cc" \
  32. )
  33. # else
  34. /*On x86-32, not so much.*/
  35. # define cpuid(_op,_eax,_ebx,_ecx,_edx) \
  36. __asm__ __volatile__( \
  37. "xchgl %%ebx,%[ebx]\n\t" \
  38. "cpuid\n\t" \
  39. "xchgl %%ebx,%[ebx]\n\t" \
  40. :[eax]"=a"(_eax),[ebx]"=r"(_ebx),[ecx]"=c"(_ecx),[edx]"=d"(_edx) \
  41. :"a"(_op) \
  42. :"cc" \
  43. )
  44. # endif
  45. static ogg_uint32_t oc_parse_intel_flags(ogg_uint32_t _edx,ogg_uint32_t _ecx){
  46. ogg_uint32_t flags;
  47. /*If there isn't even MMX, give up.*/
  48. if(!(_edx&0x00800000))return 0;
  49. flags=OC_CPU_X86_MMX;
  50. if(_edx&0x02000000)flags|=OC_CPU_X86_MMXEXT|OC_CPU_X86_SSE;
  51. if(_edx&0x04000000)flags|=OC_CPU_X86_SSE2;
  52. if(_ecx&0x00000001)flags|=OC_CPU_X86_PNI;
  53. if(_ecx&0x00000100)flags|=OC_CPU_X86_SSSE3;
  54. if(_ecx&0x00080000)flags|=OC_CPU_X86_SSE4_1;
  55. if(_ecx&0x00100000)flags|=OC_CPU_X86_SSE4_2;
  56. return flags;
  57. }
  58. static ogg_uint32_t oc_parse_amd_flags(ogg_uint32_t _edx,ogg_uint32_t _ecx){
  59. ogg_uint32_t flags;
  60. /*If there isn't even MMX, give up.*/
  61. if(!(_edx&0x00800000))return 0;
  62. flags=OC_CPU_X86_MMX;
  63. if(_edx&0x00400000)flags|=OC_CPU_X86_MMXEXT;
  64. if(_edx&0x80000000)flags|=OC_CPU_X86_3DNOW;
  65. if(_edx&0x40000000)flags|=OC_CPU_X86_3DNOWEXT;
  66. if(_ecx&0x00000040)flags|=OC_CPU_X86_SSE4A;
  67. if(_ecx&0x00000800)flags|=OC_CPU_X86_SSE5;
  68. return flags;
  69. }
  70. ogg_uint32_t oc_cpu_flags_get(void){
  71. ogg_uint32_t flags;
  72. ogg_uint32_t eax;
  73. ogg_uint32_t ebx;
  74. ogg_uint32_t ecx;
  75. ogg_uint32_t edx;
  76. # if !defined(__amd64__)&&!defined(__x86_64__)
  77. /*Not all x86-32 chips support cpuid, so we have to check.*/
  78. __asm__ __volatile__(
  79. "pushfl\n\t"
  80. "pushfl\n\t"
  81. "popl %[a]\n\t"
  82. "movl %[a],%[b]\n\t"
  83. "xorl $0x200000,%[a]\n\t"
  84. "pushl %[a]\n\t"
  85. "popfl\n\t"
  86. "pushfl\n\t"
  87. "popl %[a]\n\t"
  88. "popfl\n\t"
  89. :[a]"=r"(eax),[b]"=r"(ebx)
  90. :
  91. :"cc"
  92. );
  93. /*No cpuid.*/
  94. if(eax==ebx)return 0;
  95. # endif
  96. cpuid(0,eax,ebx,ecx,edx);
  97. /* l e t n I e n i u n e G*/
  98. if(ecx==0x6C65746E&&edx==0x49656E69&&ebx==0x756E6547||
  99. /* 6 8 x M T e n i u n e G*/
  100. ecx==0x3638784D&&edx==0x54656E69&&ebx==0x756E6547){
  101. int family;
  102. int model;
  103. /*Intel, Transmeta (tested with Crusoe TM5800):*/
  104. cpuid(1,eax,ebx,ecx,edx);
  105. flags=oc_parse_intel_flags(edx,ecx);
  106. family=(eax>>8)&0xF;
  107. model=(eax>>4)&0xF;
  108. /*The SSE unit on the Pentium M and Core Duo is much slower than the MMX
  109. unit, so don't use it.*/
  110. if(family==6&&(model==9||model==13||model==14)){
  111. flags&=~(OC_CPU_X86_SSE2|OC_CPU_X86_PNI);
  112. }
  113. }
  114. /* D M A c i t n e h t u A*/
  115. else if(ecx==0x444D4163&&edx==0x69746E65&&ebx==0x68747541||
  116. /* C S N y b e d o e G*/
  117. ecx==0x43534e20&&edx==0x79622065&&ebx==0x646f6547){
  118. /*AMD, Geode:*/
  119. cpuid(0x80000000,eax,ebx,ecx,edx);
  120. if(eax<0x80000001)flags=0;
  121. else{
  122. cpuid(0x80000001,eax,ebx,ecx,edx);
  123. flags=oc_parse_amd_flags(edx,ecx);
  124. }
  125. /*Also check for SSE.*/
  126. cpuid(1,eax,ebx,ecx,edx);
  127. flags|=oc_parse_intel_flags(edx,ecx);
  128. }
  129. /*Technically some VIA chips can be configured in the BIOS to return any
  130. string here the user wants.
  131. There is a special detection method that can be used to identify such
  132. processors, but in my opinion, if the user really wants to change it, they
  133. deserve what they get.*/
  134. /* s l u a H r u a t n e C*/
  135. else if(ecx==0x736C7561&&edx==0x48727561&&ebx==0x746E6543){
  136. /*VIA:*/
  137. /*I only have documentation for the C7 (Esther) and Isaiah (forthcoming)
  138. chips (thanks to the engineers from Centaur Technology who provided it).
  139. These chips support Intel-like cpuid info.
  140. The C3-2 (Nehemiah) cores appear to, as well.*/
  141. cpuid(1,eax,ebx,ecx,edx);
  142. flags=oc_parse_intel_flags(edx,ecx);
  143. if(eax>=0x80000001){
  144. /*The (non-Nehemiah) C3 processors support AMD-like cpuid info.
  145. We need to check this even if the Intel test succeeds to pick up 3DNow!
  146. support on these processors.
  147. Unlike actual AMD processors, we cannot _rely_ on this info, since
  148. some cores (e.g., the 693 stepping of the Nehemiah) claim to support
  149. this function, yet return edx=0, despite the Intel test indicating
  150. MMX support.
  151. Therefore the features detected here are strictly added to those
  152. detected by the Intel test.*/
  153. /*TODO: How about earlier chips?*/
  154. cpuid(0x80000001,eax,ebx,ecx,edx);
  155. /*Note: As of the C7, this function returns Intel-style extended feature
  156. flags, not AMD-style.
  157. Currently, this only defines bits 11, 20, and 29 (0x20100800), which
  158. do not conflict with any of the AMD flags we inspect.
  159. For the remaining bits, Intel tells us, "Do not count on their value",
  160. but VIA assures us that they will all be zero (at least on the C7 and
  161. Isaiah chips).
  162. In the (unlikely) event a future processor uses bits 18, 19, 30, or 31
  163. (0xC0C00000) for something else, we will have to add code to detect
  164. the model to decide when it is appropriate to inspect them.*/
  165. flags|=oc_parse_amd_flags(edx,ecx);
  166. }
  167. }
  168. else{
  169. /*Implement me.*/
  170. flags=0;
  171. }
  172. return flags;
  173. }
  174. #endif