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 | */ |
24 | |
25 | #ifndef DenormalDisabler_h |
26 | #define DenormalDisabler_h |
27 | |
28 | #include <wtf/MathExtras.h> |
29 | |
30 | namespace WebCore { |
31 | |
32 | // Deal with denormals. They can very seriously impact performance on x86. |
33 | |
34 | // Define HAVE_DENORMAL if we support flushing denormals to zero. |
35 | #if OS(WINDOWS) && COMPILER(MSVC) |
36 | #define HAVE_DENORMAL |
37 | #endif |
38 | |
39 | #if defined(__GNUC__) && (defined(__i386__) || defined(__x86_64__)) |
40 | #define HAVE_DENORMAL |
41 | #endif |
42 | |
43 | #ifdef HAVE_DENORMAL |
44 | class DenormalDisabler { |
45 | public: |
46 | DenormalDisabler() |
47 | : m_savedCSR(0) |
48 | { |
49 | #if OS(WINDOWS) && COMPILER(MSVC) |
50 | // Save the current state, and set mode to flush denormals. |
51 | // |
52 | // http://stackoverflow.com/questions/637175/possible-bug-in-controlfp-s-may-not-restore-control-word-correctly |
53 | _controlfp_s(&m_savedCSR, 0, 0); |
54 | unsigned int unused; |
55 | _controlfp_s(&unused, _DN_FLUSH, _MCW_DN); |
56 | #else |
57 | m_savedCSR = getCSR(); |
58 | setCSR(m_savedCSR | 0x8040); |
59 | #endif |
60 | } |
61 | |
62 | ~DenormalDisabler() |
63 | { |
64 | #if OS(WINDOWS) && COMPILER(MSVC) |
65 | unsigned int unused; |
66 | _controlfp_s(&unused, m_savedCSR, _MCW_DN); |
67 | #else |
68 | setCSR(m_savedCSR); |
69 | #endif |
70 | } |
71 | |
72 | // This is a nop if we can flush denormals to zero in hardware. |
73 | static inline float flushDenormalFloatToZero(float f) |
74 | { |
75 | #if OS(WINDOWS) && COMPILER(MSVC) && (!_M_IX86_FP) |
76 | // For systems using x87 instead of sse, there's no hardware support |
77 | // to flush denormals automatically. Hence, we need to flush |
78 | // denormals to zero manually. |
79 | return (fabs(f) < FLT_MIN) ? 0.0f : f; |
80 | #else |
81 | return f; |
82 | #endif |
83 | } |
84 | private: |
85 | #if defined(__GNUC__) && (defined(__i386__) || defined(__x86_64__)) |
86 | inline int getCSR() |
87 | { |
88 | int result; |
89 | asm volatile("stmxcsr %0" : "=m" (result)); |
90 | return result; |
91 | } |
92 | |
93 | inline void setCSR(int a) |
94 | { |
95 | int temp = a; |
96 | asm volatile("ldmxcsr %0" : : "m" (temp)); |
97 | } |
98 | |
99 | #endif |
100 | |
101 | unsigned int m_savedCSR; |
102 | }; |
103 | |
104 | #else |
105 | // FIXME: add implementations for other architectures and compilers |
106 | class DenormalDisabler { |
107 | public: |
108 | DenormalDisabler() { } |
109 | |
110 | // Assume the worst case that other architectures and compilers |
111 | // need to flush denormals to zero manually. |
112 | static inline float flushDenormalFloatToZero(float f) |
113 | { |
114 | return (fabs(f) < FLT_MIN) ? 0.0f : f; |
115 | } |
116 | }; |
117 | |
118 | #endif |
119 | |
120 | } // WebCore |
121 | |
122 | #undef HAVE_DENORMAL |
123 | #endif // DenormalDisabler_h |
124 | |