home *** CD-ROM | disk | FTP | other *** search
/ PC Welt 2004 March / PCWELT_3_2004.ISO / pcwsoft / flaskmpeg_078_39_src.z.exe / flaskmpeg / cpusupport.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2002-10-28  |  3.3 KB  |  147 lines

  1. /* 
  2.  *  cpusupport.cpp
  3.  *
  4.  *    Copyright (C) Alberto Vigata - January 2000
  5.  *
  6.  *  This file is part of FlasKMPEG, a free MPEG to MPEG/AVI converter
  7.  *    
  8.  *  FlasKMPEG is free software; you can redistribute it and/or modify
  9.  *  it under the terms of the GNU General Public License as published by
  10.  *  the Free Software Foundation; either version 2, or (at your option)
  11.  *  any later version.
  12.  *   
  13.  *  FlasKMPEG is distributed in the hope that it will be useful,
  14.  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
  15.  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  16.  *  GNU General Public License for more details.
  17.  *   
  18.  *  You should have received a copy of the GNU General Public License
  19.  *  along with GNU Make; see the file COPYING.  If not, write to
  20.  *  the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. 
  21.  *
  22.  */
  23.  
  24. #include <wtypes.h>
  25. #include <winnt.h>
  26. #include "cpusupport.h"
  27.  
  28. static long g_lCPUExtensionsAvailable;
  29.  
  30. // This is ridiculous ???
  31.  
  32. static long CPUCheckForSSESupport() {
  33.     __try 
  34.   {
  35. //        __asm andps xmm0,xmm0
  36.  
  37.         __asm _emit 0x0f
  38.         __asm _emit 0x54
  39.         __asm _emit 0xc0
  40.  
  41.     } 
  42.   __except(EXCEPTION_EXECUTE_HANDLER) 
  43.   { 
  44.     // The operating system does not recognize andps. Reset them.
  45.         if (_exception_code() == STATUS_ILLEGAL_INSTRUCTION)
  46.             g_lCPUExtensionsAvailable &= ~(CPU_SUPPORTS_SSE|CPU_SUPPORTS_SSE2);
  47.     }
  48.  
  49.     return g_lCPUExtensionsAvailable;
  50. }
  51.  
  52. long __declspec(naked) CPUGetSupportedExtensions() {
  53.     __asm {
  54.         push    ebp
  55.         push    edi
  56.         push    esi
  57.         push    ebx
  58.  
  59.         xor        ebp,ebp            ;cpu flags - if we don't have CPUID, we probably
  60.                                       ;won't want to try FPU optimizations.
  61.  
  62.         ;check for CPUID.
  63.  
  64.         pushfd                    ;flags -> EAX
  65.         pop        eax
  66.         or        eax,00200000h    ;set the ID bit
  67.         push    eax                ;EAX -> flags
  68.         popfd
  69.         pushfd                    ;flags -> EAX
  70.         pop        eax
  71.         and        eax,00200000h    ;ID bit set?
  72.         jz        done            ;nope...
  73.  
  74.         ;CPUID exists, check for features register.
  75.  
  76.         mov        ebp,00000003h
  77.         xor        eax,eax
  78.         cpuid
  79.         or        eax,eax
  80.         jz        done            ;no features register?!?
  81.  
  82.         ;features register exists, look for MMX, SSE, SSE2.
  83.  
  84.         mov        eax,1
  85.         cpuid
  86.         mov        ebx,edx
  87.         and        ebx,00800000h    ;MMX is bit 23
  88.         shr        ebx,21
  89.         or        ebp,ebx            ;set bit 2 if MMX exists
  90.  
  91.         mov        ebx,edx
  92.         and        edx,02000000h    ;SSE is bit 25
  93.         shr        edx,25
  94.         neg        edx
  95.         and        edx,00000018h    ;set bits 3 and 4 if SSE exists
  96.         or        ebp,edx
  97.  
  98.         and        ebx,04000000h    ;SSE2 is bit 26
  99.         shr        ebx,21
  100.         and        ebx,00000020h    ;set bit 5
  101.         or        ebp,ebx
  102.  
  103.         ;check for vendor feature register (K6/Athlon).
  104.  
  105.         mov        eax,80000000h
  106.         cpuid
  107.         mov        ecx,80000001h
  108.         cmp        eax,ecx
  109.         jb        done
  110.  
  111.         ;vendor feature register exists, look for 3DNow! and Athlon extensions
  112.  
  113.         mov        eax,ecx
  114.         cpuid
  115.  
  116.         mov        eax,edx
  117.         and        edx,80000000h    ;3DNow! is bit 31
  118.         shr        edx,25
  119.         or        ebp,edx            ;set bit 6
  120.  
  121.         mov        edx,eax
  122.         and        eax,40000000h    ;3DNow!2 is bit 30
  123.         shr        eax,23
  124.         or        ebp,eax            ;set bit 7
  125.  
  126.         and        edx,00400000h    ;AMD MMX extensions (integer SSE) is bit 22
  127.         shr        edx,19
  128.         or        ebp,edx
  129.  
  130. done:
  131.         mov        eax,ebp
  132.         mov        g_lCPUExtensionsAvailable, ebp
  133.  
  134.         ;Full SSE and SSE-2 require OS support for the xmm* registers.
  135.  
  136.         test    eax,00000030h
  137.         jz        nocheck
  138.         call    CPUCheckForSSESupport
  139. nocheck:
  140.         pop        ebx
  141.         pop        esi
  142.         pop        edi
  143.         pop        ebp
  144.         ret
  145.     }
  146. }
  147.