DISTRHO Plugin Framework
ScopedDenormalDisable.hpp
1 /*
2  * DISTRHO Plugin Framework (DPF)
3  * Copyright (C) 2012-2023 Filipe Coelho <falktx@falktx.com>
4  *
5  * Permission to use, copy, modify, and/or distribute this software for any purpose with
6  * or without fee is hereby granted, provided that the above copyright notice and this
7  * permission notice appear in all copies.
8  *
9  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD
10  * TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN
11  * NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
12  * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER
13  * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
14  * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15  */
16 
17 #ifndef DISTRHO_SCOPED_DENORMAL_DISABLE_HPP_INCLUDED
18 #define DISTRHO_SCOPED_DENORMAL_DISABLE_HPP_INCLUDED
19 
20 #include "../DistrhoUtils.hpp"
21 
22 #ifdef __SSE2_MATH__
23 # include <xmmintrin.h>
24 #endif
25 
27 
28 // --------------------------------------------------------------------------------------------------------------------
29 // ScopedDenormalDisable class definition
30 
31 /**
32  ScopedDenormalDisable is a handy class for disabling denormal numbers during a function scope.
33  Denormal numbers can happen in IIR or other types of filters, they are often very slow.
34 
35  Use this class with care! Messing up with the global state is bound to make some hosts unhappy.
36  */
38 public:
39  /*
40  * Constructor.
41  * Current cpu flags will saved, then denormals-as-zero and flush-to-zero set on top.
42  */
43  inline ScopedDenormalDisable() noexcept;
44 
45  /*
46  * Destructor.
47  * CPU flags will be restored to the value obtained in the constructor.
48  */
49  inline ~ScopedDenormalDisable() noexcept
50  {
51  setFlags(oldflags);
52  }
53 
54 private:
55  #if defined(__SSE2_MATH__)
56  typedef uint cpuflags_t;
57  #elif defined(__aarch64__)
58  typedef uint64_t cpuflags_t;
59  #elif defined(__arm__) && !defined(__SOFTFP__)
60  typedef uint32_t cpuflags_t;
61  #else
62  typedef char cpuflags_t;
63  #endif
64 
65  // retrieved on constructor, reset to it on destructor
66  cpuflags_t oldflags;
67 
68  // helper function to set cpu flags
69  inline void setFlags(cpuflags_t flags) noexcept;
70 
71  DISTRHO_DECLARE_NON_COPYABLE(ScopedDenormalDisable)
72  DISTRHO_PREVENT_HEAP_ALLOCATION
73 };
74 
75 // --------------------------------------------------------------------------------------------------------------------
76 // ScopedDenormalDisable class implementation
77 
78 inline ScopedDenormalDisable::ScopedDenormalDisable() noexcept
79  : oldflags(0)
80 {
81  #if defined(__SSE2_MATH__)
82  oldflags = _mm_getcsr();
83  setFlags(oldflags | 0x8040);
84  #elif defined(__aarch64__)
85  __asm__ __volatile__("mrs %0, fpcr" : "=r" (oldflags));
86  setFlags(oldflags | 0x1000000);
87  __asm__ __volatile__("isb");
88  #elif defined(__arm__) && !defined(__SOFTFP__)
89  __asm__ __volatile__("vmrs %0, fpscr" : "=r" (oldflags));
90  setFlags(oldflags | 0x1000000);
91  #endif
92 }
93 
94 inline void ScopedDenormalDisable::setFlags(const cpuflags_t flags) noexcept
95 {
96  #if defined(__SSE2_MATH__)
97  _mm_setcsr(flags);
98  #elif defined(__aarch64__)
99  __asm__ __volatile__("msr fpcr, %0" :: "r" (flags));
100  #elif defined(__arm__) && !defined(__SOFTFP__)
101  __asm__ __volatile__("vmsr fpscr, %0" :: "r" (flags));
102  #else
103  // unused
104  (void)flags;
105  #endif
106 }
107 
108 // --------------------------------------------------------------------------------------------------------------------
109 
111 
112 #endif // DISTRHO_SCOPED_DENORMAL_DISABLE_HPP_INCLUDED
Definition: ScopedDenormalDisable.hpp:37
#define END_NAMESPACE_DISTRHO
Definition: DistrhoInfo.hpp:949
#define START_NAMESPACE_DISTRHO
Definition: DistrhoInfo.hpp:943