Jamoma API  0.6.0.a19
CAVectorUnit.cpp
1 /* Copyright 2007 Apple Inc. All Rights Reserved.
2 
3  Disclaimer: IMPORTANT: This Apple software is supplied to you by
4  Apple Inc. ("Apple") in consideration of your agreement to the
5  following terms, and your use, installation, modification or
6  redistribution of this Apple software constitutes acceptance of these
7  terms. If you do not agree with these terms, please do not use,
8  install, modify or redistribute this Apple software.
9 
10  In consideration of your agreement to abide by the following terms, and
11  subject to these terms, Apple grants you a personal, non-exclusive
12  license, under Apple's copyrights in this original Apple software (the
13  "Apple Software"), to use, reproduce, modify and redistribute the Apple
14  Software, with or without modifications, in source and/or binary forms;
15  provided that if you redistribute the Apple Software in its entirety and
16  without modifications, you must retain this notice and the following
17  text and disclaimers in all such redistributions of the Apple Software.
18  Neither the name, trademarks, service marks or logos of Apple Inc.
19  may be used to endorse or promote products derived from the Apple
20  Software without specific prior written permission from Apple. Except
21  as expressly stated in this notice, no other rights or licenses, express
22  or implied, are granted by Apple herein, including but not limited to
23  any patent rights that may be infringed by your derivative works or by
24  other works in which the Apple Software may be incorporated.
25 
26  The Apple Software is provided by Apple on an "AS IS" basis. APPLE
27  MAKES NO WARRANTIES, EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION
28  THE IMPLIED WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY AND FITNESS
29  FOR A PARTICULAR PURPOSE, REGARDING THE APPLE SOFTWARE OR ITS USE AND
30  OPERATION ALONE OR IN COMBINATION WITH YOUR PRODUCTS.
31 
32  IN NO EVENT SHALL APPLE BE LIABLE FOR ANY SPECIAL, INDIRECT, INCIDENTAL
33  OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
34  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
35  INTERRUPTION) ARISING IN ANY WAY OUT OF THE USE, REPRODUCTION,
36  MODIFICATION AND/OR DISTRIBUTION OF THE APPLE SOFTWARE, HOWEVER CAUSED
37  AND WHETHER UNDER THEORY OF CONTRACT, TORT (INCLUDING NEGLIGENCE),
38  STRICT LIABILITY OR OTHERWISE, EVEN IF APPLE HAS BEEN ADVISED OF THE
39  POSSIBILITY OF SUCH DAMAGE.
40 */
41 #include "CAVectorUnit.h"
42 
43 #if !TARGET_OS_WIN32
44  #include <sys/sysctl.h>
45 #elif HAS_IPP
46  #include "ippdefs.h"
47  #include "ippcore.h"
48 #endif
49 
50 int gCAVectorUnitType = kVecUninitialized;
51 
52 #if TARGET_OS_WIN32
53 // Use cpuid to check if SSE2 is available.
54 // Before calling this function make sure cpuid is available
55 static SInt32 IsSSE2Available()
56 {
57  int return_value;
58 
59  {
60  int r_edx;
61  _asm
62  {
63  mov eax, 0x01
64  cpuid
65  mov r_edx, edx
66  }
67  return_value = (r_edx >> 26) & 0x1;
68  }
69  return return_value;
70 }
71 
72 // Use cpuid to check if SSE3 is available.
73 // Before calling this function make sure cpuid is available
74 static SInt32 IsSSE3Available()
75 {
76  SInt32 return_value;
77 
78  {
79  SInt32 r_ecx;
80  _asm
81  {
82  mov eax, 0x01
83  cpuid
84  mov r_ecx, ecx
85  }
86  return_value = r_ecx & 0x1;
87  }
88  return return_value;
89 }
90 
91 // Return true if the cpuid instruction is available.
92 // The cpuid instruction is available if bit 21 in the EFLAGS register can be changed
93 // This function may not work on Intel CPUs prior to Pentium (didn't test)
94 static bool IsCpuidAvailable()
95 {
96  SInt32 return_value = 0x0;
97  _asm{
98  pushfd ; //push original EFLAGS
99  pop eax ; //get original EFLAGS
100  mov ecx, eax ; //save original EFLAGS
101  xor eax, 200000h ; //flip ID bit in EFLAGS
102  push eax ; //save new EFLAGS value on stack
103  popfd ; //replace current EFLAGS value
104  pushfd ; //get new EFLAGS
105  pop eax ; //store new EFLAGS in EAX
106  xor eax, ecx ;
107  je end_cpuid_identify ; //can't toggle ID bit
108  mov return_value, 0x1;
109 end_cpuid_identify:
110  nop;
111  }
112  return return_value;
113 }
114 
115 #endif
116 
117 SInt32 CAVectorUnit_Examine()
118 {
119  int result = kVecNone;
120 
121 #if TARGET_OS_WIN32
122 #if HAS_IPP
123  // Initialize the static IPP library! This needs to be done before
124  // any IPP function calls, otherwise we may have a performance penalty
125  int status = ippStaticInit();
126  if ( status == ippStsNonIntelCpu )
127  {
128  IppCpuType cpuType = ippGetCpuType();
129  if ( cpuType >= ippCpuSSE || cpuType <= ippCpuSSE42 )
130  ippStaticInitCpu( cpuType );
131  }
132 #endif
133  {
134  // On Windows we use cpuid to detect the vector unit because it works on Intel and AMD.
135  // The IPP library does not detect SSE on AMD processors.
136  if (IsCpuidAvailable())
137  {
138  if(IsSSE3Available())
139  {
140  result = kVecSSE3;
141  }
142  else if(IsSSE2Available())
143  {
144  result = kVecSSE2;
145  }
146  }
147  }
148 #elif TARGET_OS_MAC
149 #if DEBUG
150  if (getenv("CA_NoVector")) {
151  fprintf(stderr, "CA_NoVector set; Vector unit optimized routines will be bypassed\n");
152  return result;
153  }
154  else
155 #endif
156  {
157  #if (TARGET_CPU_PPC || TARGET_CPU_PPC64)
158  int sels[2] = { CTL_HW, HW_VECTORUNIT };
159  int vType = 0; //0 == scalar only
160  size_t length = sizeof(vType);
161  int error = sysctl(sels, 2, &vType, &length, NULL, 0);
162  if (!error && vType > 0)
163  result = kVecAltivec;
164  #elif (TARGET_CPU_X86 || TARGET_CPU_X86_64)
165  int answer = 0;
166  size_t length = sizeof(answer);
167  int error = sysctlbyname("hw.optional.sse3", &answer, &length, NULL, 0);
168  if (!error && answer)
169  result = kVecSSE3;
170  else {
171  answer = 0;
172  length = sizeof(answer);
173  error = sysctlbyname("hw.optional.sse2", &answer, &length, NULL, 0);
174  if (!error && answer)
175  result = kVecSSE2;
176  }
177  #endif
178  }
179 #endif
180  gCAVectorUnitType = result;
181  return result;
182 }
183