Browse Source

Add ScopedDenormalDisable class, use it in standalone native audio

Signed-off-by: falkTX <falktx@falktx.com>
pull/417/head
falkTX 2 years ago
parent
commit
482d0af8f6
Signed by: falkTX <falktx@falktx.com> GPG Key ID: CDBAA37ABC74FBA0
3 changed files with 117 additions and 1 deletions
  1. +112
    -0
      distrho/extra/ScopedDenormalDisable.hpp
  2. +2
    -0
      distrho/src/jackbridge/RtAudioBridge.hpp
  3. +3
    -1
      distrho/src/jackbridge/SDL2Bridge.hpp

+ 112
- 0
distrho/extra/ScopedDenormalDisable.hpp View File

@@ -0,0 +1,112 @@
/*
* DISTRHO Plugin Framework (DPF)
* Copyright (C) 2012-2023 Filipe Coelho <falktx@falktx.com>
*
* Permission to use, copy, modify, and/or distribute this software for any purpose with
* or without fee is hereby granted, provided that the above copyright notice and this
* permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD
* TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN
* NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
* DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER
* IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
* CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/

#ifndef DISTRHO_SCOPED_DENORMAL_DISABLE_HPP_INCLUDED
#define DISTRHO_SCOPED_DENORMAL_DISABLE_HPP_INCLUDED

#include "../DistrhoUtils.hpp"

#ifdef __SSE2_MATH__
# include <xmmintrin.h>
#endif

START_NAMESPACE_DISTRHO

// --------------------------------------------------------------------------------------------------------------------
// ScopedDenormalDisable class definition

/**
ScopedDenormalDisable is a handy class for disabling denormal numbers during a function scope.
Denormal numbers can happen in IIR or other types of filters, they are often very slow.

Use this class with care! Messing up with the global state is bound to make some hosts unhappy.
*/
class ScopedDenormalDisable {
public:
/*
* Constructor.
* Current cpu flags will saved, then denormals-as-zero and flush-to-zero set on top.
*/
inline ScopedDenormalDisable() noexcept;

/*
* Destructor.
* CPU flags will be restored to the value obtained in the constructor.
*/
inline ~ScopedDenormalDisable() noexcept
{
setFlags(oldflags);
}

private:
#if defined(__SSE2_MATH__)
typedef uint cpuflags_t;
#elif defined(__aarch64__)
typedef uint64_t cpuflags_t;
#elif defined(__arm__) && !defined(__SOFTFP__)
typedef uint32_t cpuflags_t;
#else
typedef char cpuflags_t;
#endif

// retrieved on constructor, reset to it on destructor
cpuflags_t oldflags;

// helper function to set cpu flags
inline void setFlags(cpuflags_t flags) noexcept;

DISTRHO_DECLARE_NON_COPYABLE(ScopedDenormalDisable)
DISTRHO_PREVENT_HEAP_ALLOCATION
};

// --------------------------------------------------------------------------------------------------------------------
// ScopedDenormalDisable class implementation

inline ScopedDenormalDisable::ScopedDenormalDisable() noexcept
: oldflags(0)
{
#if defined(__SSE2_MATH__)
oldflags = _mm_getcsr();
setFlags(oldflags | 0x8040);
#elif defined(__aarch64__)
__asm__ __volatile__("mrs %0, fpcr" : "=r" (oldflags));
setFlags(oldflags | 0x1000000);
__asm__ __volatile__("isb");
#elif defined(__arm__) && !defined(__SOFTFP__)
__asm__ __volatile__("vmrs %0, fpscr" : "=r" (oldflags));
setFlags(oldflags | 0x1000000);
#endif
}

inline void ScopedDenormalDisable::setFlags(const cpuflags_t flags) noexcept
{
#if defined(__SSE2_MATH__)
_mm_setcsr(flags);
#elif defined(__aarch64__)
__asm__ __volatile__("msr fpcr, %0" :: "r" (flags));
#elif defined(__arm__) && !defined(__SOFTFP__)
__asm__ __volatile__("vmsr fpscr, %0" :: "r" (flags));
#else
// unused
(void)flags;
#endif
}

// --------------------------------------------------------------------------------------------------------------------

END_NAMESPACE_DISTRHO

#endif // DISTRHO_SCOPED_DENORMAL_DISABLE_HPP_INCLUDED

+ 2
- 0
distrho/src/jackbridge/RtAudioBridge.hpp View File

@@ -50,6 +50,7 @@
# include "rtmidi/RtMidi.h"
# include "../../extra/ScopedPointer.hpp"
# include "../../extra/String.hpp"
# include "../../extra/ScopedDenormalDisable.hpp"

using DISTRHO_NAMESPACE::ScopedPointer;
using DISTRHO_NAMESPACE::String;
@@ -377,6 +378,7 @@ struct RtAudioBridge : NativeBridge {
}
#endif

const ScopedDenormalDisable sdd;
self->jackProcessCallback(numFrames, self->jackProcessArg);

return 0;


+ 3
- 1
distrho/src/jackbridge/SDL2Bridge.hpp View File

@@ -1,6 +1,6 @@
/*
* SDL Bridge for DPF
* Copyright (C) 2021-2022 Filipe Coelho <falktx@falktx.com>
* Copyright (C) 2021-2023 Filipe Coelho <falktx@falktx.com>
*
* Permission to use, copy, modify, and/or distribute this software for any purpose with
* or without fee is hereby granted, provided that the above copyright notice and this
@@ -18,6 +18,7 @@
#define SDL_BRIDGE_HPP_INCLUDED

#include "NativeBridge.hpp"
#include "../../extra/ScopedDenormalDisable.hpp"

#include <SDL.h>

@@ -246,6 +247,7 @@ struct SDL2Bridge : NativeBridge {
const uint numFrames = static_cast<uint>(len / sizeof(float) / DISTRHO_PLUGIN_NUM_OUTPUTS);
DISTRHO_SAFE_ASSERT_UINT2_RETURN(numFrames == self->bufferSize, numFrames, self->bufferSize,);

const ScopedDenormalDisable sdd;
self->jackProcessCallback(numFrames, self->jackProcessArg);

float* const fstream = (float*)stream;


Loading…
Cancel
Save