Collection of DPF-based plugins for packaging
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

113 lines
3.6KB

  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. #ifndef DISTRHO_SCOPED_DENORMAL_DISABLE_HPP_INCLUDED
  17. #define DISTRHO_SCOPED_DENORMAL_DISABLE_HPP_INCLUDED
  18. #include "../DistrhoUtils.hpp"
  19. #ifdef __SSE2_MATH__
  20. # include <xmmintrin.h>
  21. #endif
  22. START_NAMESPACE_DISTRHO
  23. // --------------------------------------------------------------------------------------------------------------------
  24. // ScopedDenormalDisable class definition
  25. /**
  26. ScopedDenormalDisable is a handy class for disabling denormal numbers during a function scope.
  27. Denormal numbers can happen in IIR or other types of filters, they are often very slow.
  28. Use this class with care! Messing up with the global state is bound to make some hosts unhappy.
  29. */
  30. class ScopedDenormalDisable {
  31. public:
  32. /*
  33. * Constructor.
  34. * Current cpu flags will saved, then denormals-as-zero and flush-to-zero set on top.
  35. */
  36. inline ScopedDenormalDisable() noexcept;
  37. /*
  38. * Destructor.
  39. * CPU flags will be restored to the value obtained in the constructor.
  40. */
  41. inline ~ScopedDenormalDisable() noexcept
  42. {
  43. setFlags(oldflags);
  44. }
  45. private:
  46. #if defined(__SSE2_MATH__)
  47. typedef uint cpuflags_t;
  48. #elif defined(__aarch64__)
  49. typedef uint64_t cpuflags_t;
  50. #elif defined(__arm__) && !defined(__SOFTFP__)
  51. typedef uint32_t cpuflags_t;
  52. #else
  53. typedef char cpuflags_t;
  54. #endif
  55. // retrieved on constructor, reset to it on destructor
  56. cpuflags_t oldflags;
  57. // helper function to set cpu flags
  58. inline void setFlags(cpuflags_t flags) noexcept;
  59. DISTRHO_DECLARE_NON_COPYABLE(ScopedDenormalDisable)
  60. DISTRHO_PREVENT_HEAP_ALLOCATION
  61. };
  62. // --------------------------------------------------------------------------------------------------------------------
  63. // ScopedDenormalDisable class implementation
  64. inline ScopedDenormalDisable::ScopedDenormalDisable() noexcept
  65. : oldflags(0)
  66. {
  67. #if defined(__SSE2_MATH__)
  68. oldflags = _mm_getcsr();
  69. setFlags(oldflags | 0x8040);
  70. #elif defined(__aarch64__)
  71. __asm__ __volatile__("mrs %0, fpcr" : "=r" (oldflags));
  72. setFlags(oldflags | 0x1000000);
  73. __asm__ __volatile__("isb");
  74. #elif defined(__arm__) && !defined(__SOFTFP__)
  75. __asm__ __volatile__("vmrs %0, fpscr" : "=r" (oldflags));
  76. setFlags(oldflags | 0x1000000);
  77. #endif
  78. }
  79. inline void ScopedDenormalDisable::setFlags(const cpuflags_t flags) noexcept
  80. {
  81. #if defined(__SSE2_MATH__)
  82. _mm_setcsr(flags);
  83. #elif defined(__aarch64__)
  84. __asm__ __volatile__("msr fpcr, %0" :: "r" (flags));
  85. #elif defined(__arm__) && !defined(__SOFTFP__)
  86. __asm__ __volatile__("vmsr fpscr, %0" :: "r" (flags));
  87. #else
  88. // unused
  89. (void)flags;
  90. #endif
  91. }
  92. // --------------------------------------------------------------------------------------------------------------------
  93. END_NAMESPACE_DISTRHO
  94. #endif // DISTRHO_SCOPED_DENORMAL_DISABLE_HPP_INCLUDED