• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  *  Copyright (c) 2021 The WebRTC project authors. All Rights Reserved.
3  *
4  *  Use of this source code is governed by a BSD-style license
5  *  that can be found in the LICENSE file in the root of the source
6  *  tree. An additional intellectual property rights grant can be found
7  *  in the file PATENTS.  All contributing project authors may
8  *  be found in the AUTHORS file in the root of the source tree.
9  */
10 
11 #ifndef SYSTEM_WRAPPERS_INCLUDE_DENORMAL_DISABLER_H_
12 #define SYSTEM_WRAPPERS_INCLUDE_DENORMAL_DISABLER_H_
13 
14 #include "rtc_base/system/arch.h"
15 
16 namespace webrtc {
17 
18 // Activates the hardware (HW) way to flush denormals (see [1]) to zero as they
19 // can very seriously impact performance. At destruction time restores the
20 // denormals handling state read by the ctor; hence, supports nested calls.
21 // Equals a no-op if the architecture is not x86 or ARM or if the compiler is
22 // not CLANG.
23 // [1] https://en.wikipedia.org/wiki/Denormal_number
24 //
25 // Example usage:
26 //
27 // void Foo() {
28 //   DenormalDisabler d;
29 //   ...
30 // }
31 class DenormalDisabler {
32  public:
33   // Ctor. If `enabled` is true and architecture and compiler are supported,
34   // stores the HW settings for denormals, disables denormals and sets
35   // `disabling_activated_` to true. Otherwise, only sets `disabling_activated_`
36   // to false.
37   explicit DenormalDisabler(bool enabled);
38   DenormalDisabler(const DenormalDisabler&) = delete;
39   DenormalDisabler& operator=(const DenormalDisabler&) = delete;
40   // Dtor. If `disabling_activated_` is true, restores the denormals HW settings
41   // read by the ctor before denormals were disabled. Otherwise it's a no-op.
42   ~DenormalDisabler();
43 
44   // Returns true if architecture and compiler are supported.
45   static bool IsSupported();
46 
47  private:
48   const int status_word_;
49   const bool disabling_activated_;
50 };
51 
52 }  // namespace webrtc
53 
54 #endif  // SYSTEM_WRAPPERS_INCLUDE_DENORMAL_DISABLER_H_
55