mozglue/build/SSE.h

Tue, 06 Jan 2015 21:39:09 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Tue, 06 Jan 2015 21:39:09 +0100
branch
TOR_BUG_9701
changeset 8
97036ab72558
permissions
-rw-r--r--

Conditionally force memory storage according to privacy.thirdparty.isolate;
This solves Tor bug #9701, complying with disk avoidance documented in
https://www.torproject.org/projects/torbrowser/design/#disk-avoidance.

michael@0 1 /* vim: set shiftwidth=2 tabstop=8 autoindent cindent expandtab: */
michael@0 2 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 3 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 5
michael@0 6 /* compile-time and runtime tests for whether to use SSE instructions */
michael@0 7
michael@0 8 #ifndef mozilla_SSE_h_
michael@0 9 #define mozilla_SSE_h_
michael@0 10
michael@0 11 // for definition of MFBT_DATA
michael@0 12 #include "mozilla/Types.h"
michael@0 13
michael@0 14 /**
michael@0 15 * The public interface of this header consists of a set of macros and
michael@0 16 * functions for Intel CPU features.
michael@0 17 *
michael@0 18 * DETECTING ISA EXTENSIONS
michael@0 19 * ========================
michael@0 20 *
michael@0 21 * This header provides the following functions for determining whether the
michael@0 22 * current CPU supports a particular instruction set extension:
michael@0 23 *
michael@0 24 * mozilla::supports_mmx
michael@0 25 * mozilla::supports_sse
michael@0 26 * mozilla::supports_sse2
michael@0 27 * mozilla::supports_sse3
michael@0 28 * mozilla::supports_ssse3
michael@0 29 * mozilla::supports_sse4a
michael@0 30 * mozilla::supports_sse4_1
michael@0 31 * mozilla::supports_sse4_2
michael@0 32 *
michael@0 33 * If you're writing code using inline assembly, you should guard it with a
michael@0 34 * call to one of these functions. For instance:
michael@0 35 *
michael@0 36 * if (mozilla::supports_sse2()) {
michael@0 37 * asm(" ... ");
michael@0 38 * }
michael@0 39 * else {
michael@0 40 * ...
michael@0 41 * }
michael@0 42 *
michael@0 43 * Note that these functions depend on cpuid intrinsics only available in gcc
michael@0 44 * 4.3 or later and MSVC 8.0 (Visual C++ 2005) or later, so they return false
michael@0 45 * in older compilers. (This could be fixed by replacing the code with inline
michael@0 46 * assembly.)
michael@0 47 *
michael@0 48 *
michael@0 49 * USING INTRINSICS
michael@0 50 * ================
michael@0 51 *
michael@0 52 * This header also provides support for coding using CPU intrinsics.
michael@0 53 *
michael@0 54 * For each mozilla::supports_abc function, we define a MOZILLA_MAY_SUPPORT_ABC
michael@0 55 * macro which indicates that the target/compiler combination we're using is
michael@0 56 * compatible with the ABC extension. For instance, x86_64 with MSVC 2003 is
michael@0 57 * compatible with SSE2 but not SSE3, since although there exist x86_64 CPUs
michael@0 58 * with SSE3 support, MSVC 2003 only supports through SSE2.
michael@0 59 *
michael@0 60 * Until gcc fixes #pragma target [1] [2] or our x86 builds require SSE2,
michael@0 61 * you'll need to separate code using intrinsics into a file separate from your
michael@0 62 * regular code. Here's the recommended pattern:
michael@0 63 *
michael@0 64 * #ifdef MOZILLA_MAY_SUPPORT_ABC
michael@0 65 * namespace mozilla {
michael@0 66 * namespace ABC {
michael@0 67 * void foo();
michael@0 68 * }
michael@0 69 * }
michael@0 70 * #endif
michael@0 71 *
michael@0 72 * void foo() {
michael@0 73 * #ifdef MOZILLA_MAY_SUPPORT_ABC
michael@0 74 * if (mozilla::supports_abc()) {
michael@0 75 * mozilla::ABC::foo(); // in a separate file
michael@0 76 * return;
michael@0 77 * }
michael@0 78 * #endif
michael@0 79 *
michael@0 80 * foo_unvectorized();
michael@0 81 * }
michael@0 82 *
michael@0 83 * You'll need to define mozilla::ABC::foo() in a separate file and add the
michael@0 84 * -mabc flag when using gcc.
michael@0 85 *
michael@0 86 * [1] http://gcc.gnu.org/bugzilla/show_bug.cgi?id=39787 and
michael@0 87 * [2] http://gcc.gnu.org/bugzilla/show_bug.cgi?id=41201 being fixed.
michael@0 88 *
michael@0 89 */
michael@0 90
michael@0 91 #if defined(__GNUC__) && (defined(__i386__) || defined(__x86_64__))
michael@0 92
michael@0 93 #ifdef __MMX__
michael@0 94 // It's ok to use MMX instructions based on the -march option (or
michael@0 95 // the default for x86_64 or for Intel Mac).
michael@0 96 #define MOZILLA_PRESUME_MMX 1
michael@0 97 #endif
michael@0 98 #ifdef __SSE__
michael@0 99 // It's ok to use SSE instructions based on the -march option (or
michael@0 100 // the default for x86_64 or for Intel Mac).
michael@0 101 #define MOZILLA_PRESUME_SSE 1
michael@0 102 #endif
michael@0 103 #ifdef __SSE2__
michael@0 104 // It's ok to use SSE2 instructions based on the -march option (or
michael@0 105 // the default for x86_64 or for Intel Mac).
michael@0 106 #define MOZILLA_PRESUME_SSE2 1
michael@0 107 #endif
michael@0 108 #ifdef __SSE3__
michael@0 109 // It's ok to use SSE3 instructions based on the -march option (or the
michael@0 110 // default for Intel Mac).
michael@0 111 #define MOZILLA_PRESUME_SSE3 1
michael@0 112 #endif
michael@0 113 #ifdef __SSSE3__
michael@0 114 // It's ok to use SSSE3 instructions based on the -march option.
michael@0 115 #define MOZILLA_PRESUME_SSSE3 1
michael@0 116 #endif
michael@0 117 #ifdef __SSE4A__
michael@0 118 // It's ok to use SSE4A instructions based on the -march option.
michael@0 119 #define MOZILLA_PRESUME_SSE4A 1
michael@0 120 #endif
michael@0 121 #ifdef __SSE4_1__
michael@0 122 // It's ok to use SSE4.1 instructions based on the -march option.
michael@0 123 #define MOZILLA_PRESUME_SSE4_1 1
michael@0 124 #endif
michael@0 125 #ifdef __SSE4_2__
michael@0 126 // It's ok to use SSE4.2 instructions based on the -march option.
michael@0 127 #define MOZILLA_PRESUME_SSE4_2 1
michael@0 128 #endif
michael@0 129
michael@0 130 #ifdef HAVE_CPUID_H
michael@0 131 #define MOZILLA_SSE_HAVE_CPUID_DETECTION
michael@0 132 #endif
michael@0 133
michael@0 134 #elif defined(_MSC_VER) && (defined(_M_IX86) || defined(_M_AMD64))
michael@0 135
michael@0 136 #define MOZILLA_SSE_HAVE_CPUID_DETECTION
michael@0 137
michael@0 138 #if defined(_M_IX86_FP)
michael@0 139
michael@0 140 #if _M_IX86_FP >= 1
michael@0 141 // It's ok to use SSE instructions based on the /arch option
michael@0 142 #define MOZILLA_PRESUME_SSE
michael@0 143 #endif
michael@0 144 #if _M_IX86_FP >= 2
michael@0 145 // It's ok to use SSE2 instructions based on the /arch option
michael@0 146 #define MOZILLA_PRESUME_SSE2
michael@0 147 #endif
michael@0 148
michael@0 149 #elif defined(_M_AMD64)
michael@0 150 // MSVC for AMD64 doesn't support MMX, so don't presume it here.
michael@0 151
michael@0 152 // SSE is always available on AMD64.
michael@0 153 #define MOZILLA_PRESUME_SSE
michael@0 154 // SSE2 is always available on AMD64.
michael@0 155 #define MOZILLA_PRESUME_SSE2
michael@0 156 #endif
michael@0 157
michael@0 158 #elif defined(__SUNPRO_CC) && (defined(__i386) || defined(__x86_64__))
michael@0 159 // Sun Studio on x86 or amd64
michael@0 160
michael@0 161 #define MOZILLA_SSE_HAVE_CPUID_DETECTION
michael@0 162
michael@0 163 #if defined(__x86_64__)
michael@0 164 // MMX is always available on AMD64.
michael@0 165 #define MOZILLA_PRESUME_MMX
michael@0 166 // SSE is always available on AMD64.
michael@0 167 #define MOZILLA_PRESUME_SSE
michael@0 168 // SSE2 is always available on AMD64.
michael@0 169 #define MOZILLA_PRESUME_SSE2
michael@0 170 #endif
michael@0 171
michael@0 172 #endif
michael@0 173
michael@0 174 namespace mozilla {
michael@0 175
michael@0 176 namespace sse_private {
michael@0 177 #if defined(MOZILLA_SSE_HAVE_CPUID_DETECTION)
michael@0 178 #if !defined(MOZILLA_PRESUME_MMX)
michael@0 179 extern bool MFBT_DATA mmx_enabled;
michael@0 180 #endif
michael@0 181 #if !defined(MOZILLA_PRESUME_SSE)
michael@0 182 extern bool MFBT_DATA sse_enabled;
michael@0 183 #endif
michael@0 184 #if !defined(MOZILLA_PRESUME_SSE2)
michael@0 185 extern bool MFBT_DATA sse2_enabled;
michael@0 186 #endif
michael@0 187 #if !defined(MOZILLA_PRESUME_SSE3)
michael@0 188 extern bool MFBT_DATA sse3_enabled;
michael@0 189 #endif
michael@0 190 #if !defined(MOZILLA_PRESUME_SSSE3)
michael@0 191 extern bool MFBT_DATA ssse3_enabled;
michael@0 192 #endif
michael@0 193 #if !defined(MOZILLA_PRESUME_SSE4A)
michael@0 194 extern bool MFBT_DATA sse4a_enabled;
michael@0 195 #endif
michael@0 196 #if !defined(MOZILLA_PRESUME_SSE4_1)
michael@0 197 extern bool MFBT_DATA sse4_1_enabled;
michael@0 198 #endif
michael@0 199 #if !defined(MOZILLA_PRESUME_SSE4_2)
michael@0 200 extern bool MFBT_DATA sse4_2_enabled;
michael@0 201 #endif
michael@0 202 #endif
michael@0 203 }
michael@0 204
michael@0 205 #if defined(MOZILLA_PRESUME_MMX)
michael@0 206 #define MOZILLA_MAY_SUPPORT_MMX 1
michael@0 207 inline bool supports_mmx() { return true; }
michael@0 208 #elif defined(MOZILLA_SSE_HAVE_CPUID_DETECTION)
michael@0 209 #if !(defined(_MSC_VER) && defined(_M_AMD64))
michael@0 210 // Define MOZILLA_MAY_SUPPORT_MMX only if we're not on MSVC for
michael@0 211 // AMD64, since that compiler doesn't support MMX.
michael@0 212 #define MOZILLA_MAY_SUPPORT_MMX 1
michael@0 213 #endif
michael@0 214 inline bool supports_mmx() { return sse_private::mmx_enabled; }
michael@0 215 #else
michael@0 216 inline bool supports_mmx() { return false; }
michael@0 217 #endif
michael@0 218
michael@0 219 #if defined(MOZILLA_PRESUME_SSE)
michael@0 220 #define MOZILLA_MAY_SUPPORT_SSE 1
michael@0 221 inline bool supports_sse() { return true; }
michael@0 222 #elif defined(MOZILLA_SSE_HAVE_CPUID_DETECTION)
michael@0 223 #define MOZILLA_MAY_SUPPORT_SSE 1
michael@0 224 inline bool supports_sse() { return sse_private::sse_enabled; }
michael@0 225 #else
michael@0 226 inline bool supports_sse() { return false; }
michael@0 227 #endif
michael@0 228
michael@0 229 #if defined(MOZILLA_PRESUME_SSE2)
michael@0 230 #define MOZILLA_MAY_SUPPORT_SSE2 1
michael@0 231 inline bool supports_sse2() { return true; }
michael@0 232 #elif defined(MOZILLA_SSE_HAVE_CPUID_DETECTION)
michael@0 233 #define MOZILLA_MAY_SUPPORT_SSE2 1
michael@0 234 inline bool supports_sse2() { return sse_private::sse2_enabled; }
michael@0 235 #else
michael@0 236 inline bool supports_sse2() { return false; }
michael@0 237 #endif
michael@0 238
michael@0 239 #if defined(MOZILLA_PRESUME_SSE3)
michael@0 240 #define MOZILLA_MAY_SUPPORT_SSE3 1
michael@0 241 inline bool supports_sse3() { return true; }
michael@0 242 #elif defined(MOZILLA_SSE_HAVE_CPUID_DETECTION)
michael@0 243 #define MOZILLA_MAY_SUPPORT_SSE3 1
michael@0 244 inline bool supports_sse3() { return sse_private::sse3_enabled; }
michael@0 245 #else
michael@0 246 inline bool supports_sse3() { return false; }
michael@0 247 #endif
michael@0 248
michael@0 249 #if defined(MOZILLA_PRESUME_SSSE3)
michael@0 250 #define MOZILLA_MAY_SUPPORT_SSSE3 1
michael@0 251 inline bool supports_ssse3() { return true; }
michael@0 252 #elif defined(MOZILLA_SSE_HAVE_CPUID_DETECTION)
michael@0 253 #define MOZILLA_MAY_SUPPORT_SSSE3 1
michael@0 254 inline bool supports_ssse3() { return sse_private::ssse3_enabled; }
michael@0 255 #else
michael@0 256 inline bool supports_ssse3() { return false; }
michael@0 257 #endif
michael@0 258
michael@0 259 #if defined(MOZILLA_PRESUME_SSE4A)
michael@0 260 #define MOZILLA_MAY_SUPPORT_SSE4A 1
michael@0 261 inline bool supports_sse4a() { return true; }
michael@0 262 #elif defined(MOZILLA_SSE_HAVE_CPUID_DETECTION)
michael@0 263 #define MOZILLA_MAY_SUPPORT_SSE4A 1
michael@0 264 inline bool supports_sse4a() { return sse_private::sse4a_enabled; }
michael@0 265 #else
michael@0 266 inline bool supports_sse4a() { return false; }
michael@0 267 #endif
michael@0 268
michael@0 269 #if defined(MOZILLA_PRESUME_SSE4_1)
michael@0 270 #define MOZILLA_MAY_SUPPORT_SSE4_1 1
michael@0 271 inline bool supports_sse4_1() { return true; }
michael@0 272 #elif defined(MOZILLA_SSE_HAVE_CPUID_DETECTION)
michael@0 273 #define MOZILLA_MAY_SUPPORT_SSE4_1 1
michael@0 274 inline bool supports_sse4_1() { return sse_private::sse4_1_enabled; }
michael@0 275 #else
michael@0 276 inline bool supports_sse4_1() { return false; }
michael@0 277 #endif
michael@0 278
michael@0 279 #if defined(MOZILLA_PRESUME_SSE4_2)
michael@0 280 #define MOZILLA_MAY_SUPPORT_SSE4_2 1
michael@0 281 inline bool supports_sse4_2() { return true; }
michael@0 282 #elif defined(MOZILLA_SSE_HAVE_CPUID_DETECTION)
michael@0 283 #define MOZILLA_MAY_SUPPORT_SSE4_2 1
michael@0 284 inline bool supports_sse4_2() { return sse_private::sse4_2_enabled; }
michael@0 285 #else
michael@0 286 inline bool supports_sse4_2() { return false; }
michael@0 287 #endif
michael@0 288
michael@0 289 }
michael@0 290
michael@0 291 #endif /* !defined(mozilla_SSE_h_) */

mercurial