Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | /* |
michael@0 | 2 | * Copyright (C) 2011, Google Inc. All rights reserved. |
michael@0 | 3 | * |
michael@0 | 4 | * Redistribution and use in source and binary forms, with or without |
michael@0 | 5 | * modification, are permitted provided that the following conditions |
michael@0 | 6 | * are met: |
michael@0 | 7 | * 1. Redistributions of source code must retain the above copyright |
michael@0 | 8 | * notice, this list of conditions and the following disclaimer. |
michael@0 | 9 | * 2. Redistributions in binary form must reproduce the above copyright |
michael@0 | 10 | * notice, this list of conditions and the following disclaimer in the |
michael@0 | 11 | * documentation and/or other materials provided with the distribution. |
michael@0 | 12 | * |
michael@0 | 13 | * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' AND ANY |
michael@0 | 14 | * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
michael@0 | 15 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
michael@0 | 16 | * DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY |
michael@0 | 17 | * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
michael@0 | 18 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
michael@0 | 19 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON |
michael@0 | 20 | * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
michael@0 | 21 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
michael@0 | 22 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
michael@0 | 23 | */ |
michael@0 | 24 | |
michael@0 | 25 | #ifndef DenormalDisabler_h |
michael@0 | 26 | #define DenormalDisabler_h |
michael@0 | 27 | |
michael@0 | 28 | #define _USE_MATH_DEFINES |
michael@0 | 29 | #include <cmath> |
michael@0 | 30 | #include <float.h> |
michael@0 | 31 | |
michael@0 | 32 | namespace WebCore { |
michael@0 | 33 | |
michael@0 | 34 | // Deal with denormals. They can very seriously impact performance on x86. |
michael@0 | 35 | |
michael@0 | 36 | // Define HAVE_DENORMAL if we support flushing denormals to zero. |
michael@0 | 37 | #if defined(XP_WIN) && defined(_MSC_VER) |
michael@0 | 38 | #define HAVE_DENORMAL |
michael@0 | 39 | #endif |
michael@0 | 40 | |
michael@0 | 41 | #if defined(__GNUC__) && (defined(__i386__) || defined(__x86_64__)) |
michael@0 | 42 | #define HAVE_DENORMAL |
michael@0 | 43 | #endif |
michael@0 | 44 | |
michael@0 | 45 | #ifdef HAVE_DENORMAL |
michael@0 | 46 | class DenormalDisabler { |
michael@0 | 47 | public: |
michael@0 | 48 | DenormalDisabler() |
michael@0 | 49 | : m_savedCSR(0) |
michael@0 | 50 | { |
michael@0 | 51 | #if defined(XP_WIN) && defined(_MSC_VER) |
michael@0 | 52 | // Save the current state, and set mode to flush denormals. |
michael@0 | 53 | // |
michael@0 | 54 | // http://stackoverflow.com/questions/637175/possible-bug-in-controlfp-s-may-not-restore-control-word-correctly |
michael@0 | 55 | _controlfp_s(&m_savedCSR, 0, 0); |
michael@0 | 56 | unsigned int unused; |
michael@0 | 57 | _controlfp_s(&unused, _DN_FLUSH, _MCW_DN); |
michael@0 | 58 | #else |
michael@0 | 59 | m_savedCSR = getCSR(); |
michael@0 | 60 | setCSR(m_savedCSR | 0x8040); |
michael@0 | 61 | #endif |
michael@0 | 62 | } |
michael@0 | 63 | |
michael@0 | 64 | ~DenormalDisabler() |
michael@0 | 65 | { |
michael@0 | 66 | #if defined(XP_WIN) && defined(_MSC_VER) |
michael@0 | 67 | unsigned int unused; |
michael@0 | 68 | _controlfp_s(&unused, m_savedCSR, _MCW_DN); |
michael@0 | 69 | #else |
michael@0 | 70 | setCSR(m_savedCSR); |
michael@0 | 71 | #endif |
michael@0 | 72 | } |
michael@0 | 73 | |
michael@0 | 74 | // This is a nop if we can flush denormals to zero in hardware. |
michael@0 | 75 | static inline float flushDenormalFloatToZero(float f) |
michael@0 | 76 | { |
michael@0 | 77 | #if defined(XP_WIN) && defined(_MSC_VER) && _M_IX86_FP |
michael@0 | 78 | // For systems using x87 instead of sse, there's no hardware support |
michael@0 | 79 | // to flush denormals automatically. Hence, we need to flush |
michael@0 | 80 | // denormals to zero manually. |
michael@0 | 81 | return (fabs(f) < FLT_MIN) ? 0.0f : f; |
michael@0 | 82 | #else |
michael@0 | 83 | return f; |
michael@0 | 84 | #endif |
michael@0 | 85 | } |
michael@0 | 86 | private: |
michael@0 | 87 | #if defined(__GNUC__) && (defined(__i386__) || defined(__x86_64__)) |
michael@0 | 88 | inline int getCSR() |
michael@0 | 89 | { |
michael@0 | 90 | int result; |
michael@0 | 91 | asm volatile("stmxcsr %0" : "=m" (result)); |
michael@0 | 92 | return result; |
michael@0 | 93 | } |
michael@0 | 94 | |
michael@0 | 95 | inline void setCSR(int a) |
michael@0 | 96 | { |
michael@0 | 97 | int temp = a; |
michael@0 | 98 | asm volatile("ldmxcsr %0" : : "m" (temp)); |
michael@0 | 99 | } |
michael@0 | 100 | |
michael@0 | 101 | #endif |
michael@0 | 102 | |
michael@0 | 103 | unsigned int m_savedCSR; |
michael@0 | 104 | }; |
michael@0 | 105 | |
michael@0 | 106 | #else |
michael@0 | 107 | // FIXME: add implementations for other architectures and compilers |
michael@0 | 108 | class DenormalDisabler { |
michael@0 | 109 | public: |
michael@0 | 110 | DenormalDisabler() { } |
michael@0 | 111 | |
michael@0 | 112 | // Assume the worst case that other architectures and compilers |
michael@0 | 113 | // need to flush denormals to zero manually. |
michael@0 | 114 | static inline float flushDenormalFloatToZero(float f) |
michael@0 | 115 | { |
michael@0 | 116 | return (fabs(f) < FLT_MIN) ? 0.0f : f; |
michael@0 | 117 | } |
michael@0 | 118 | }; |
michael@0 | 119 | |
michael@0 | 120 | #endif |
michael@0 | 121 | |
michael@0 | 122 | } // WebCore |
michael@0 | 123 | |
michael@0 | 124 | #undef HAVE_DENORMAL |
michael@0 | 125 | #endif // DenormalDisabler_h |