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
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.
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
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()) {
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
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
106 * #ifdef MOZILLA_MAY_SUPPORT_ABC
107 * if (mozilla::supports_abc()) {
108 * mozilla::ABC::foo(); // in a separate file
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__))
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
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
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
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
147 // It's ok to use SSSE3 instructions based on the -march option.
148 #define MOZILLA_PRESUME_SSSE3 1
151 // It's ok to use SSE4A instructions based on the -march option.
152 #define MOZILLA_PRESUME_SSE4A 1
155 // It's ok to use SSE4.1 instructions based on the -march option.
156 #define MOZILLA_PRESUME_SSE4_1 1
159 // It's ok to use SSE4.2 instructions based on the -march option.
160 #define MOZILLA_PRESUME_SSE4_2 1
163 #if __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 3)
164 #define MOZILLA_SSE_HAVE_CPUID_DETECTION
167 #elif defined(_MSC_VER) && (defined(_M_IX86) || defined(_M_AMD64))
170 // MSVC 2005 or newer on x86 or amd64
171 #define MOZILLA_SSE_HAVE_CPUID_DETECTION
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
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
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
;
206 #if !defined(MOZILLA_PRESUME_SSE)
207 extern bool NS_COM_GLUE sse_enabled
;
209 #if !defined(MOZILLA_PRESUME_SSE2)
210 extern bool NS_COM_GLUE sse2_enabled
;
212 #if !defined(MOZILLA_PRESUME_SSE3)
213 extern bool NS_COM_GLUE sse3_enabled
;
215 #if !defined(MOZILLA_PRESUME_SSSE3)
216 extern bool NS_COM_GLUE ssse3_enabled
;
218 #if !defined(MOZILLA_PRESUME_SSE4A)
219 extern bool NS_COM_GLUE sse4a_enabled
;
221 #if !defined(MOZILLA_PRESUME_SSE4_1)
222 extern bool NS_COM_GLUE sse4_1_enabled
;
224 #if !defined(MOZILLA_PRESUME_SSE4_2)
225 extern bool NS_COM_GLUE sse4_2_enabled
;
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
239 inline bool supports_mmx() { return sse_private::mmx_enabled
; }
241 inline bool supports_mmx() { return false; }
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
; }
251 inline bool supports_sse() { return false; }
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
; }
261 inline bool supports_sse2() { return false; }
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
; }
271 inline bool supports_sse3() { return false; }
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
; }
281 inline bool supports_ssse3() { return false; }
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
; }
291 inline bool supports_sse4a() { return false; }
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
; }
301 inline bool supports_sse4_1() { return false; }
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
; }
311 inline bool supports_sse4_2() { return false; }
316 #endif /* !defined(mozilla_SSE_h_) */