content/media/webaudio/blink/DenormalDisabler.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.

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

mercurial