michael@0: /* michael@0: * Copyright (C) 2011, Google Inc. All rights reserved. michael@0: * michael@0: * Redistribution and use in source and binary forms, with or without michael@0: * modification, are permitted provided that the following conditions michael@0: * are met: michael@0: * 1. Redistributions of source code must retain the above copyright michael@0: * notice, this list of conditions and the following disclaimer. michael@0: * 2. Redistributions in binary form must reproduce the above copyright michael@0: * notice, this list of conditions and the following disclaimer in the michael@0: * documentation and/or other materials provided with the distribution. michael@0: * michael@0: * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' AND ANY michael@0: * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED michael@0: * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE michael@0: * DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY michael@0: * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES michael@0: * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; michael@0: * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON michael@0: * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT michael@0: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS michael@0: * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. michael@0: */ michael@0: michael@0: #ifndef DenormalDisabler_h michael@0: #define DenormalDisabler_h michael@0: michael@0: #define _USE_MATH_DEFINES michael@0: #include michael@0: #include michael@0: michael@0: namespace WebCore { michael@0: michael@0: // Deal with denormals. They can very seriously impact performance on x86. michael@0: michael@0: // Define HAVE_DENORMAL if we support flushing denormals to zero. michael@0: #if defined(XP_WIN) && defined(_MSC_VER) michael@0: #define HAVE_DENORMAL michael@0: #endif michael@0: michael@0: #if defined(__GNUC__) && (defined(__i386__) || defined(__x86_64__)) michael@0: #define HAVE_DENORMAL michael@0: #endif michael@0: michael@0: #ifdef HAVE_DENORMAL michael@0: class DenormalDisabler { michael@0: public: michael@0: DenormalDisabler() michael@0: : m_savedCSR(0) michael@0: { michael@0: #if defined(XP_WIN) && defined(_MSC_VER) michael@0: // Save the current state, and set mode to flush denormals. michael@0: // michael@0: // http://stackoverflow.com/questions/637175/possible-bug-in-controlfp-s-may-not-restore-control-word-correctly michael@0: _controlfp_s(&m_savedCSR, 0, 0); michael@0: unsigned int unused; michael@0: _controlfp_s(&unused, _DN_FLUSH, _MCW_DN); michael@0: #else michael@0: m_savedCSR = getCSR(); michael@0: setCSR(m_savedCSR | 0x8040); michael@0: #endif michael@0: } michael@0: michael@0: ~DenormalDisabler() michael@0: { michael@0: #if defined(XP_WIN) && defined(_MSC_VER) michael@0: unsigned int unused; michael@0: _controlfp_s(&unused, m_savedCSR, _MCW_DN); michael@0: #else michael@0: setCSR(m_savedCSR); michael@0: #endif michael@0: } michael@0: michael@0: // This is a nop if we can flush denormals to zero in hardware. michael@0: static inline float flushDenormalFloatToZero(float f) michael@0: { michael@0: #if defined(XP_WIN) && defined(_MSC_VER) && _M_IX86_FP michael@0: // For systems using x87 instead of sse, there's no hardware support michael@0: // to flush denormals automatically. Hence, we need to flush michael@0: // denormals to zero manually. michael@0: return (fabs(f) < FLT_MIN) ? 0.0f : f; michael@0: #else michael@0: return f; michael@0: #endif michael@0: } michael@0: private: michael@0: #if defined(__GNUC__) && (defined(__i386__) || defined(__x86_64__)) michael@0: inline int getCSR() michael@0: { michael@0: int result; michael@0: asm volatile("stmxcsr %0" : "=m" (result)); michael@0: return result; michael@0: } michael@0: michael@0: inline void setCSR(int a) michael@0: { michael@0: int temp = a; michael@0: asm volatile("ldmxcsr %0" : : "m" (temp)); michael@0: } michael@0: michael@0: #endif michael@0: michael@0: unsigned int m_savedCSR; michael@0: }; michael@0: michael@0: #else michael@0: // FIXME: add implementations for other architectures and compilers michael@0: class DenormalDisabler { michael@0: public: michael@0: DenormalDisabler() { } michael@0: michael@0: // Assume the worst case that other architectures and compilers michael@0: // need to flush denormals to zero manually. michael@0: static inline float flushDenormalFloatToZero(float f) michael@0: { michael@0: return (fabs(f) < FLT_MIN) ? 0.0f : f; michael@0: } michael@0: }; michael@0: michael@0: #endif michael@0: michael@0: } // WebCore michael@0: michael@0: #undef HAVE_DENORMAL michael@0: #endif // DenormalDisabler_h