Merge tracemonkey and mozilla-central. (a=blockers)
[mozilla-central.git] / xpcom / glue / SSE.h
blobedf16054aa216b6664743ca4fdb4816f31a47b08
1 /* vim: set shiftwidth=2 tabstop=8 autoindent cindent expandtab: */
2 /* ***** BEGIN LICENSE BLOCK *****
3 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
5 * The contents of this file are subject to the Mozilla Public License Version
6 * 1.1 (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 * http://www.mozilla.org/MPL/
10 * Software distributed under the License is distributed on an "AS IS" basis,
11 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 * for the specific language governing rights and limitations under the
13 * License.
15 * The Original Code is SSE.h
17 * The Initial Developer of the Original Code is the Mozilla Foundation.
18 * Portions created by the Initial Developer are Copyright (C) 2009
19 * the Initial Developer. All Rights Reserved.
21 * Contributor(s):
22 * L. David Baron <dbaron@dbaron.org>, Mozilla Corporation (original author)
23 * Justin Lebar <justin.lebar@gmail.com>, Mozilla Corporation
25 * Alternatively, the contents of this file may be used under the terms of
26 * either the GNU General Public License Version 2 or later (the "GPL"), or
27 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
28 * in which case the provisions of the GPL or the LGPL are applicable instead
29 * of those above. If you wish to allow use of your version of this file only
30 * under the terms of either the GPL or the LGPL, and not to allow others to
31 * use your version of this file under the terms of the MPL, indicate your
32 * decision by deleting the provisions above and replace them with the notice
33 * and other provisions required by the GPL or the LGPL. If you do not delete
34 * the provisions above, a recipient may use your version of this file under
35 * the terms of any one of the MPL, the GPL or the LGPL.
37 * ***** END LICENSE BLOCK ***** */
39 /* compile-time and runtime tests for whether to use SSE instructions */
41 #ifndef mozilla_SSE_h_
42 #define mozilla_SSE_h_
44 // for definition of NS_COM_GLUE
45 #include "nscore.h"
47 /**
48 * The public interface of this header consists of a set of macros and
49 * functions for Intel CPU features.
51 * DETECTING ISA EXTENSIONS
52 * ========================
54 * This header provides the following functions for determining whether the
55 * current CPU supports a particular instruction set extension:
57 * mozilla::supports_mmx
58 * mozilla::supports_sse
59 * mozilla::supports_sse2
60 * mozilla::supports_sse3
61 * mozilla::supports_ssse3
62 * mozilla::supports_sse4a
63 * mozilla::supports_sse4_1
64 * mozilla::supports_sse4_2
66 * If you're writing code using inline assembly, you should guard it with a
67 * call to one of these functions. For instance:
69 * if (mozilla::supports_sse2()) {
70 * asm(" ... ");
71 * }
72 * else {
73 * ...
74 * }
76 * Note that these functions depend on cpuid intrinsics only available in gcc
77 * 4.3 or later and MSVC 8.0 (Visual C++ 2005) or later, so they return false
78 * in older compilers. (This could be fixed by replacing the code with inline
79 * assembly.)
82 * USING INTRINSICS
83 * ================
85 * This header also provides support for coding using CPU intrinsics.
87 * For each mozilla::supports_abc function, we define a MOZILLA_MAY_SUPPORT_ABC
88 * macro which indicates that the target/compiler combination we're using is
89 * compatible with the ABC extension. For instance, x86_64 with MSVC 2003 is
90 * compatible with SSE2 but not SSE3, since although there exist x86_64 CPUs
91 * with SSE3 support, MSVC 2003 only supports through SSE2.
93 * Until gcc fixes #pragma target [1] [2] or our x86 builds require SSE2,
94 * you'll need to separate code using intrinsics into a file separate from your
95 * regular code. Here's the recommended pattern:
97 * #ifdef MOZILLA_MAY_SUPPORT_ABC
98 * namespace mozilla {
99 * namespace ABC {
100 * void foo();
103 * #endif
105 * void foo() {
106 * #ifdef MOZILLA_MAY_SUPPORT_ABC
107 * if (mozilla::supports_abc()) {
108 * mozilla::ABC::foo(); // in a separate file
109 * return;
111 * #endif
113 * foo_unvectorized();
116 * You'll need to define mozilla::ABC::foo() in a separate file and add the
117 * -mabc flag when using gcc.
119 * [1] http://gcc.gnu.org/bugzilla/show_bug.cgi?id=39787 and
120 * [2] http://gcc.gnu.org/bugzilla/show_bug.cgi?id=41201 being fixed.
124 #if defined(__GNUC__) && (defined(__i386__) || defined(__x86_64__))
126 #ifdef __MMX__
127 // It's ok to use MMX instructions based on the -march option (or
128 // the default for x86_64 or for Intel Mac).
129 #define MOZILLA_PRESUME_MMX 1
130 #endif
131 #ifdef __SSE__
132 // It's ok to use SSE instructions based on the -march option (or
133 // the default for x86_64 or for Intel Mac).
134 #define MOZILLA_PRESUME_SSE 1
135 #endif
136 #ifdef __SSE2__
137 // It's ok to use SSE2 instructions based on the -march option (or
138 // the default for x86_64 or for Intel Mac).
139 #define MOZILLA_PRESUME_SSE2 1
140 #endif
141 #ifdef __SSE3__
142 // It's ok to use SSE3 instructions based on the -march option (or the
143 // default for Intel Mac).
144 #define MOZILLA_PRESUME_SSE3 1
145 #endif
146 #ifdef __SSSE3__
147 // It's ok to use SSSE3 instructions based on the -march option.
148 #define MOZILLA_PRESUME_SSSE3 1
149 #endif
150 #ifdef __SSE4A__
151 // It's ok to use SSE4A instructions based on the -march option.
152 #define MOZILLA_PRESUME_SSE4A 1
153 #endif
154 #ifdef __SSE4_1__
155 // It's ok to use SSE4.1 instructions based on the -march option.
156 #define MOZILLA_PRESUME_SSE4_1 1
157 #endif
158 #ifdef __SSE4_2__
159 // It's ok to use SSE4.2 instructions based on the -march option.
160 #define MOZILLA_PRESUME_SSE4_2 1
161 #endif
163 #if __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 3)
164 #define MOZILLA_SSE_HAVE_CPUID_DETECTION
165 #endif
167 #elif defined(_MSC_VER) && (defined(_M_IX86) || defined(_M_AMD64))
169 #if _MSC_VER >= 1400
170 // MSVC 2005 or newer on x86 or amd64
171 #define MOZILLA_SSE_HAVE_CPUID_DETECTION
172 #endif
174 #if defined(_M_AMD64)
175 // MSVC for AMD64 doesn't support MMX, so don't presume it here.
177 // SSE is always available on AMD64.
178 #define MOZILLA_PRESUME_SSE
179 // SSE2 is always available on AMD64.
180 #define MOZILLA_PRESUME_SSE2
181 #endif
183 #elif defined(__SUNPRO_CC) && (defined(__i386) || defined(__x86_64__))
184 // Sun Studio on x86 or amd64
186 #define MOZILLA_SSE_HAVE_CPUID_DETECTION
188 #if defined(__x86_64__)
189 // MMX is always available on AMD64.
190 #define MOZILLA_PRESUME_MMX
191 // SSE is always available on AMD64.
192 #define MOZILLA_PRESUME_SSE
193 // SSE2 is always available on AMD64.
194 #define MOZILLA_PRESUME_SSE2
195 #endif
197 #endif
199 namespace mozilla {
201 namespace sse_private {
202 #if defined(MOZILLA_SSE_HAVE_CPUID_DETECTION)
203 #if !defined(MOZILLA_PRESUME_MMX)
204 extern bool NS_COM_GLUE mmx_enabled;
205 #endif
206 #if !defined(MOZILLA_PRESUME_SSE)
207 extern bool NS_COM_GLUE sse_enabled;
208 #endif
209 #if !defined(MOZILLA_PRESUME_SSE2)
210 extern bool NS_COM_GLUE sse2_enabled;
211 #endif
212 #if !defined(MOZILLA_PRESUME_SSE3)
213 extern bool NS_COM_GLUE sse3_enabled;
214 #endif
215 #if !defined(MOZILLA_PRESUME_SSSE3)
216 extern bool NS_COM_GLUE ssse3_enabled;
217 #endif
218 #if !defined(MOZILLA_PRESUME_SSE4A)
219 extern bool NS_COM_GLUE sse4a_enabled;
220 #endif
221 #if !defined(MOZILLA_PRESUME_SSE4_1)
222 extern bool NS_COM_GLUE sse4_1_enabled;
223 #endif
224 #if !defined(MOZILLA_PRESUME_SSE4_2)
225 extern bool NS_COM_GLUE sse4_2_enabled;
226 #endif
227 #endif
230 #if defined(MOZILLA_PRESUME_MMX)
231 #define MOZILLA_MAY_SUPPORT_MMX 1
232 inline bool supports_mmx() { return true; }
233 #elif defined(MOZILLA_SSE_HAVE_CPUID_DETECTION)
234 #if !(defined(_MSC_VER) && defined(_M_AMD64))
235 // Define MOZILLA_MAY_SUPPORT_MMX only if we're not on MSVC for
236 // AMD64, since that compiler doesn't support MMX.
237 #define MOZILLA_MAY_SUPPORT_MMX 1
238 #endif
239 inline bool supports_mmx() { return sse_private::mmx_enabled; }
240 #else
241 inline bool supports_mmx() { return false; }
242 #endif
244 #if defined(MOZILLA_PRESUME_SSE)
245 #define MOZILLA_MAY_SUPPORT_SSE 1
246 inline bool supports_sse() { return true; }
247 #elif defined(MOZILLA_SSE_HAVE_CPUID_DETECTION)
248 #define MOZILLA_MAY_SUPPORT_SSE 1
249 inline bool supports_sse() { return sse_private::sse_enabled; }
250 #else
251 inline bool supports_sse() { return false; }
252 #endif
254 #if defined(MOZILLA_PRESUME_SSE2)
255 #define MOZILLA_MAY_SUPPORT_SSE2 1
256 inline bool supports_sse2() { return true; }
257 #elif defined(MOZILLA_SSE_HAVE_CPUID_DETECTION)
258 #define MOZILLA_MAY_SUPPORT_SSE2 1
259 inline bool supports_sse2() { return sse_private::sse2_enabled; }
260 #else
261 inline bool supports_sse2() { return false; }
262 #endif
264 #if defined(MOZILLA_PRESUME_SSE3)
265 #define MOZILLA_MAY_SUPPORT_SSE3 1
266 inline bool supports_sse3() { return true; }
267 #elif defined(MOZILLA_SSE_HAVE_CPUID_DETECTION)
268 #define MOZILLA_MAY_SUPPORT_SSE3 1
269 inline bool supports_sse3() { return sse_private::sse3_enabled; }
270 #else
271 inline bool supports_sse3() { return false; }
272 #endif
274 #if defined(MOZILLA_PRESUME_SSSE3)
275 #define MOZILLA_MAY_SUPPORT_SSSE3 1
276 inline bool supports_ssse3() { return true; }
277 #elif defined(MOZILLA_SSE_HAVE_CPUID_DETECTION)
278 #define MOZILLA_MAY_SUPPORT_SSSE3 1
279 inline bool supports_ssse3() { return sse_private::ssse3_enabled; }
280 #else
281 inline bool supports_ssse3() { return false; }
282 #endif
284 #if defined(MOZILLA_PRESUME_SSE4A)
285 #define MOZILLA_MAY_SUPPORT_SSE4A 1
286 inline bool supports_sse4a() { return true; }
287 #elif defined(MOZILLA_SSE_HAVE_CPUID_DETECTION)
288 #define MOZILLA_MAY_SUPPORT_SSE4A 1
289 inline bool supports_sse4a() { return sse_private::sse4a_enabled; }
290 #else
291 inline bool supports_sse4a() { return false; }
292 #endif
294 #if defined(MOZILLA_PRESUME_SSE4_1)
295 #define MOZILLA_MAY_SUPPORT_SSE4_1 1
296 inline bool supports_sse4_1() { return true; }
297 #elif defined(MOZILLA_SSE_HAVE_CPUID_DETECTION)
298 #define MOZILLA_MAY_SUPPORT_SSE4_1 1
299 inline bool supports_sse4_1() { return sse_private::sse4_1_enabled; }
300 #else
301 inline bool supports_sse4_1() { return false; }
302 #endif
304 #if defined(MOZILLA_PRESUME_SSE4_2)
305 #define MOZILLA_MAY_SUPPORT_SSE4_2 1
306 inline bool supports_sse4_2() { return true; }
307 #elif defined(MOZILLA_SSE_HAVE_CPUID_DETECTION)
308 #define MOZILLA_MAY_SUPPORT_SSE4_2 1
309 inline bool supports_sse4_2() { return sse_private::sse4_2_enabled; }
310 #else
311 inline bool supports_sse4_2() { return false; }
312 #endif
316 #endif /* !defined(mozilla_SSE_h_) */