1 /* Copyright 2021 Google LLC. All Rights Reserved.
2 
3 Licensed under the Apache License, Version 2.0 (the "License");
4 you may not use this file except in compliance with the License.
5 You may obtain a copy of the License at
6 
7     http://www.apache.org/licenses/LICENSE-2.0
8 
9 Unless required by applicable law or agreed to in writing, software
10 distributed under the License is distributed on an "AS IS" BASIS,
11 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 See the License for the specific language governing permissions and
13 limitations under the License.
14 ==============================================================================*/
15 #ifndef RUY_RUY_DENORMAL_H_
16 #define RUY_RUY_DENORMAL_H_
17 
18 #include <cstdint>
19 
20 namespace ruy {
21 // NOTE: the following 'fpu_state' struct is copied from
22 // pthreadpool/src/threadpool-utils.h that's not exposed by the pthreadpool
23 // library (https://github.com/Maratyszcza/pthreadpool).
24 struct fpu_state {
25 #if defined(__SSE__) || defined(__x86_64__) || defined(_M_X64) || \
26     (defined(_M_IX86_FP) && _M_IX86_FP >= 1)
27   std::uint32_t mxcsr;
28 #elif defined(__GNUC__) && defined(__arm__) && defined(__ARM_FP) && \
29         (__ARM_FP != 0) ||                                          \
30     defined(_MSC_VER) && defined(_M_ARM)
31   std::uint32_t fpscr;
32 #elif defined(__GNUC__) && defined(__aarch64__) || \
33     defined(_MSC_VER) && defined(_M_ARM64)
34   std::uint64_t fpcr;
35 #endif
36 };
37 
38 // While this class is active, denormal floating point numbers are suppressed.
39 // The destructor restores the original flags.
40 class ScopedSuppressDenormals {
41  public:
42   ScopedSuppressDenormals();
43   ~ScopedSuppressDenormals();
44 
45  private:
46   fpu_state restore_;
47 
48   ScopedSuppressDenormals(const ScopedSuppressDenormals&) = delete;
49   void operator=(const ScopedSuppressDenormals&) = delete;
50 };
51 }  // namespace ruy
52 
53 #endif  // RUY_RUY_DENORMAL_H_
54