src/corelib/tools/qsimd.cpp
changeset 30 5dc02b23752f
child 33 3e2da88830cd
equal deleted inserted replaced
29:b72c6db6890b 30:5dc02b23752f
       
     1 /****************************************************************************
       
     2 **
       
     3 ** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
       
     4 ** All rights reserved.
       
     5 ** Contact: Nokia Corporation (qt-info@nokia.com)
       
     6 **
       
     7 ** This file is part of the QtCore module of the Qt Toolkit.
       
     8 **
       
     9 ** $QT_BEGIN_LICENSE:LGPL$
       
    10 ** No Commercial Usage
       
    11 ** This file contains pre-release code and may not be distributed.
       
    12 ** You may use this file in accordance with the terms and conditions
       
    13 ** contained in the Technology Preview License Agreement accompanying
       
    14 ** this package.
       
    15 **
       
    16 ** GNU Lesser General Public License Usage
       
    17 ** Alternatively, this file may be used under the terms of the GNU Lesser
       
    18 ** General Public License version 2.1 as published by the Free Software
       
    19 ** Foundation and appearing in the file LICENSE.LGPL included in the
       
    20 ** packaging of this file.  Please review the following information to
       
    21 ** ensure the GNU Lesser General Public License version 2.1 requirements
       
    22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
       
    23 **
       
    24 ** In addition, as a special exception, Nokia gives you certain additional
       
    25 ** rights.  These rights are described in the Nokia Qt LGPL Exception
       
    26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
       
    27 **
       
    28 ** If you have questions regarding the use of this file, please contact
       
    29 ** Nokia at qt-info@nokia.com.
       
    30 **
       
    31 **
       
    32 **
       
    33 **
       
    34 **
       
    35 **
       
    36 **
       
    37 **
       
    38 ** $QT_END_LICENSE$
       
    39 **
       
    40 ****************************************************************************/
       
    41 
       
    42 #include "qsimd_p.h"
       
    43 #include <QByteArray>
       
    44 
       
    45 #if defined(Q_OS_WINCE)
       
    46 #include <windows.h>
       
    47 #endif
       
    48 
       
    49 QT_BEGIN_NAMESPACE
       
    50 
       
    51 uint qDetectCPUFeatures()
       
    52 {
       
    53     static uint features = 0xffffffff;
       
    54     if (features != 0xffffffff)
       
    55         return features;
       
    56 
       
    57 #if defined (Q_OS_WINCE)
       
    58 #if defined (ARM)
       
    59     if (IsProcessorFeaturePresent(PF_ARM_INTEL_WMMX)) {
       
    60         features = IWMMXT;
       
    61         return features;
       
    62     }
       
    63 #elif defined(_X86_)
       
    64     features = 0;
       
    65 #if defined QT_HAVE_MMX
       
    66     if (IsProcessorFeaturePresent(PF_MMX_INSTRUCTIONS_AVAILABLE))
       
    67         features |= MMX;
       
    68 #endif
       
    69 #if defined QT_HAVE_3DNOW
       
    70     if (IsProcessorFeaturePresent(PF_3DNOW_INSTRUCTIONS_AVAILABLE))
       
    71         features |= MMX3DNOW;
       
    72 #endif
       
    73     return features;
       
    74 #endif
       
    75     features = 0;
       
    76     return features;
       
    77 #elif defined(QT_HAVE_IWMMXT)
       
    78     // runtime detection only available when running as a previlegied process
       
    79     static const bool doIWMMXT = !qgetenv("QT_NO_IWMMXT").toInt();
       
    80     features = doIWMMXT ? IWMMXT : 0;
       
    81     return features;
       
    82 #elif defined(QT_HAVE_NEON)
       
    83     static const bool doNEON = !qgetenv("QT_NO_NEON").toInt();
       
    84     features = doNEON ? NEON : 0;
       
    85     return features;
       
    86 #else
       
    87     features = 0;
       
    88 #if defined(__x86_64__) || defined(Q_OS_WIN64)
       
    89     features = MMX|SSE|SSE2|CMOV;
       
    90 #elif defined(__ia64__)
       
    91     features = MMX|SSE|SSE2;
       
    92 #elif defined(__i386__) || defined(_M_IX86)
       
    93     unsigned int extended_result = 0;
       
    94     uint result = 0;
       
    95     /* see p. 118 of amd64 instruction set manual Vol3 */
       
    96 #if defined(Q_CC_GNU)
       
    97     asm ("push %%ebx\n"
       
    98          "pushf\n"
       
    99          "pop %%eax\n"
       
   100          "mov %%eax, %%ebx\n"
       
   101          "xor $0x00200000, %%eax\n"
       
   102          "push %%eax\n"
       
   103          "popf\n"
       
   104          "pushf\n"
       
   105          "pop %%eax\n"
       
   106          "xor %%edx, %%edx\n"
       
   107          "xor %%ebx, %%eax\n"
       
   108          "jz 1f\n"
       
   109 
       
   110          "mov $0x00000001, %%eax\n"
       
   111          "cpuid\n"
       
   112          "1:\n"
       
   113          "pop %%ebx\n"
       
   114          "mov %%edx, %0\n"
       
   115         : "=r" (result)
       
   116         :
       
   117         : "%eax", "%ecx", "%edx"
       
   118         );
       
   119 
       
   120     asm ("push %%ebx\n"
       
   121          "pushf\n"
       
   122          "pop %%eax\n"
       
   123          "mov %%eax, %%ebx\n"
       
   124          "xor $0x00200000, %%eax\n"
       
   125          "push %%eax\n"
       
   126          "popf\n"
       
   127          "pushf\n"
       
   128          "pop %%eax\n"
       
   129          "xor %%edx, %%edx\n"
       
   130          "xor %%ebx, %%eax\n"
       
   131          "jz 2f\n"
       
   132 
       
   133          "mov $0x80000000, %%eax\n"
       
   134          "cpuid\n"
       
   135          "cmp $0x80000000, %%eax\n"
       
   136          "jbe 2f\n"
       
   137          "mov $0x80000001, %%eax\n"
       
   138          "cpuid\n"
       
   139          "2:\n"
       
   140          "pop %%ebx\n"
       
   141          "mov %%edx, %0\n"
       
   142         : "=r" (extended_result)
       
   143         :
       
   144         : "%eax", "%ecx", "%edx"
       
   145         );
       
   146 #elif defined (Q_OS_WIN)
       
   147     _asm {
       
   148         push eax
       
   149         push ebx
       
   150         push ecx
       
   151         push edx
       
   152         pushfd
       
   153         pop eax
       
   154         mov ebx, eax
       
   155         xor eax, 00200000h
       
   156         push eax
       
   157         popfd
       
   158         pushfd
       
   159         pop eax
       
   160         mov edx, 0
       
   161         xor eax, ebx
       
   162         jz skip
       
   163 
       
   164         mov eax, 1
       
   165         cpuid
       
   166         mov result, edx
       
   167     skip:
       
   168         pop edx
       
   169         pop ecx
       
   170         pop ebx
       
   171         pop eax
       
   172     }
       
   173 
       
   174     _asm {
       
   175         push eax
       
   176         push ebx
       
   177         push ecx
       
   178         push edx
       
   179         pushfd
       
   180         pop eax
       
   181         mov ebx, eax
       
   182         xor eax, 00200000h
       
   183         push eax
       
   184         popfd
       
   185         pushfd
       
   186         pop eax
       
   187         mov edx, 0
       
   188         xor eax, ebx
       
   189         jz skip2
       
   190 
       
   191         mov eax, 80000000h
       
   192         cpuid
       
   193         cmp eax, 80000000h
       
   194         jbe skip2
       
   195         mov eax, 80000001h
       
   196         cpuid
       
   197         mov extended_result, edx
       
   198     skip2:
       
   199         pop edx
       
   200         pop ecx
       
   201         pop ebx
       
   202         pop eax
       
   203     }
       
   204 #endif
       
   205 
       
   206     // result now contains the standard feature bits
       
   207     if (result & (1u << 15))
       
   208         features |= CMOV;
       
   209     if (result & (1u << 23))
       
   210         features |= MMX;
       
   211     if (extended_result & (1u << 22))
       
   212         features |= MMXEXT;
       
   213     if (extended_result & (1u << 31))
       
   214         features |= MMX3DNOW;
       
   215     if (extended_result & (1u << 30))
       
   216         features |= MMX3DNOWEXT;
       
   217     if (result & (1u << 25))
       
   218         features |= SSE;
       
   219     if (result & (1u << 26))
       
   220         features |= SSE2;
       
   221 #endif // i386
       
   222 
       
   223 #if defined(QT_HAVE_MMX)
       
   224     if (qgetenv("QT_NO_MMX").toInt())
       
   225         features ^= MMX;
       
   226 #endif
       
   227     if (qgetenv("QT_NO_MMXEXT").toInt())
       
   228         features ^= MMXEXT;
       
   229 
       
   230 #if defined(QT_HAVE_3DNOW)
       
   231     if (qgetenv("QT_NO_3DNOW").toInt())
       
   232         features ^= MMX3DNOW;
       
   233 #endif
       
   234     if (qgetenv("QT_NO_3DNOWEXT").toInt())
       
   235         features ^= MMX3DNOWEXT;
       
   236 
       
   237 #if defined(QT_HAVE_SSE)
       
   238     if (qgetenv("QT_NO_SSE").toInt())
       
   239         features ^= SSE;
       
   240 #endif
       
   241 #if defined(QT_HAVE_SSE2)
       
   242     if (qgetenv("QT_NO_SSE2").toInt())
       
   243         features ^= SSE2;
       
   244 #endif
       
   245 
       
   246     return features;
       
   247 #endif
       
   248 }
       
   249 
       
   250 QT_END_NAMESPACE