Audio plugin host https://kx.studio/carla
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.

258 lines
14KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2020 - Raw Material Software Limited
  5. JUCE is an open source library subject to commercial or open-source
  6. licensing.
  7. The code included in this file is provided under the terms of the ISC license
  8. http://www.isc.org/downloads/software-support-policy/isc-license. Permission
  9. To use, copy, modify, and/or distribute this software for any purpose with or
  10. without fee is hereby granted provided that the above copyright notice and
  11. this permission notice appear in all copies.
  12. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  13. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  14. DISCLAIMED.
  15. ==============================================================================
  16. */
  17. namespace juce
  18. {
  19. #ifndef JUCE_SNAP_TO_ZERO
  20. #if JUCE_INTEL
  21. #define JUCE_SNAP_TO_ZERO(n) if (! (n < -1.0e-8f || n > 1.0e-8f)) n = 0;
  22. #else
  23. #define JUCE_SNAP_TO_ZERO(n) ignoreUnused (n)
  24. #endif
  25. #endif
  26. class ScopedNoDenormals;
  27. //==============================================================================
  28. /**
  29. A collection of simple vector operations on arrays of floats, accelerated with
  30. SIMD instructions where possible.
  31. @tags{Audio}
  32. */
  33. class JUCE_API FloatVectorOperations
  34. {
  35. public:
  36. //==============================================================================
  37. /** Clears a vector of floats. */
  38. static void JUCE_CALLTYPE clear (float* dest, int numValues) noexcept;
  39. /** Clears a vector of doubles. */
  40. static void JUCE_CALLTYPE clear (double* dest, int numValues) noexcept;
  41. /** Copies a repeated value into a vector of floats. */
  42. static void JUCE_CALLTYPE fill (float* dest, float valueToFill, int numValues) noexcept;
  43. /** Copies a repeated value into a vector of doubles. */
  44. static void JUCE_CALLTYPE fill (double* dest, double valueToFill, int numValues) noexcept;
  45. /** Copies a vector of floats. */
  46. static void JUCE_CALLTYPE copy (float* dest, const float* src, int numValues) noexcept;
  47. /** Copies a vector of doubles. */
  48. static void JUCE_CALLTYPE copy (double* dest, const double* src, int numValues) noexcept;
  49. /** Copies a vector of floats, multiplying each value by a given multiplier */
  50. static void JUCE_CALLTYPE copyWithMultiply (float* dest, const float* src, float multiplier, int numValues) noexcept;
  51. /** Copies a vector of doubles, multiplying each value by a given multiplier */
  52. static void JUCE_CALLTYPE copyWithMultiply (double* dest, const double* src, double multiplier, int numValues) noexcept;
  53. /** Adds a fixed value to the destination values. */
  54. static void JUCE_CALLTYPE add (float* dest, float amountToAdd, int numValues) noexcept;
  55. /** Adds a fixed value to the destination values. */
  56. static void JUCE_CALLTYPE add (double* dest, double amountToAdd, int numValues) noexcept;
  57. /** Adds a fixed value to each source value and stores it in the destination array. */
  58. static void JUCE_CALLTYPE add (float* dest, const float* src, float amount, int numValues) noexcept;
  59. /** Adds a fixed value to each source value and stores it in the destination array. */
  60. static void JUCE_CALLTYPE add (double* dest, const double* src, double amount, int numValues) noexcept;
  61. /** Adds the source values to the destination values. */
  62. static void JUCE_CALLTYPE add (float* dest, const float* src, int numValues) noexcept;
  63. /** Adds the source values to the destination values. */
  64. static void JUCE_CALLTYPE add (double* dest, const double* src, int numValues) noexcept;
  65. /** Adds each source1 value to the corresponding source2 value and stores the result in the destination array. */
  66. static void JUCE_CALLTYPE add (float* dest, const float* src1, const float* src2, int num) noexcept;
  67. /** Adds each source1 value to the corresponding source2 value and stores the result in the destination array. */
  68. static void JUCE_CALLTYPE add (double* dest, const double* src1, const double* src2, int num) noexcept;
  69. /** Subtracts the source values from the destination values. */
  70. static void JUCE_CALLTYPE subtract (float* dest, const float* src, int numValues) noexcept;
  71. /** Subtracts the source values from the destination values. */
  72. static void JUCE_CALLTYPE subtract (double* dest, const double* src, int numValues) noexcept;
  73. /** Subtracts each source2 value from the corresponding source1 value and stores the result in the destination array. */
  74. static void JUCE_CALLTYPE subtract (float* dest, const float* src1, const float* src2, int num) noexcept;
  75. /** Subtracts each source2 value from the corresponding source1 value and stores the result in the destination array. */
  76. static void JUCE_CALLTYPE subtract (double* dest, const double* src1, const double* src2, int num) noexcept;
  77. /** Multiplies each source value by the given multiplier, then adds it to the destination value. */
  78. static void JUCE_CALLTYPE addWithMultiply (float* dest, const float* src, float multiplier, int numValues) noexcept;
  79. /** Multiplies each source value by the given multiplier, then adds it to the destination value. */
  80. static void JUCE_CALLTYPE addWithMultiply (double* dest, const double* src, double multiplier, int numValues) noexcept;
  81. /** Multiplies each source1 value by the corresponding source2 value, then adds it to the destination value. */
  82. static void JUCE_CALLTYPE addWithMultiply (float* dest, const float* src1, const float* src2, int num) noexcept;
  83. /** Multiplies each source1 value by the corresponding source2 value, then adds it to the destination value. */
  84. static void JUCE_CALLTYPE addWithMultiply (double* dest, const double* src1, const double* src2, int num) noexcept;
  85. /** Multiplies each source value by the given multiplier, then subtracts it to the destination value. */
  86. static void JUCE_CALLTYPE subtractWithMultiply (float* dest, const float* src, float multiplier, int numValues) noexcept;
  87. /** Multiplies each source value by the given multiplier, then subtracts it to the destination value. */
  88. static void JUCE_CALLTYPE subtractWithMultiply (double* dest, const double* src, double multiplier, int numValues) noexcept;
  89. /** Multiplies each source1 value by the corresponding source2 value, then subtracts it to the destination value. */
  90. static void JUCE_CALLTYPE subtractWithMultiply (float* dest, const float* src1, const float* src2, int num) noexcept;
  91. /** Multiplies each source1 value by the corresponding source2 value, then subtracts it to the destination value. */
  92. static void JUCE_CALLTYPE subtractWithMultiply (double* dest, const double* src1, const double* src2, int num) noexcept;
  93. /** Multiplies the destination values by the source values. */
  94. static void JUCE_CALLTYPE multiply (float* dest, const float* src, int numValues) noexcept;
  95. /** Multiplies the destination values by the source values. */
  96. static void JUCE_CALLTYPE multiply (double* dest, const double* src, int numValues) noexcept;
  97. /** Multiplies each source1 value by the correspinding source2 value, then stores it in the destination array. */
  98. static void JUCE_CALLTYPE multiply (float* dest, const float* src1, const float* src2, int numValues) noexcept;
  99. /** Multiplies each source1 value by the correspinding source2 value, then stores it in the destination array. */
  100. static void JUCE_CALLTYPE multiply (double* dest, const double* src1, const double* src2, int numValues) noexcept;
  101. /** Multiplies each of the destination values by a fixed multiplier. */
  102. static void JUCE_CALLTYPE multiply (float* dest, float multiplier, int numValues) noexcept;
  103. /** Multiplies each of the destination values by a fixed multiplier. */
  104. static void JUCE_CALLTYPE multiply (double* dest, double multiplier, int numValues) noexcept;
  105. /** Multiplies each of the source values by a fixed multiplier and stores the result in the destination array. */
  106. static void JUCE_CALLTYPE multiply (float* dest, const float* src, float multiplier, int num) noexcept;
  107. /** Multiplies each of the source values by a fixed multiplier and stores the result in the destination array. */
  108. static void JUCE_CALLTYPE multiply (double* dest, const double* src, double multiplier, int num) noexcept;
  109. /** Copies a source vector to a destination, negating each value. */
  110. static void JUCE_CALLTYPE negate (float* dest, const float* src, int numValues) noexcept;
  111. /** Copies a source vector to a destination, negating each value. */
  112. static void JUCE_CALLTYPE negate (double* dest, const double* src, int numValues) noexcept;
  113. /** Copies a source vector to a destination, taking the absolute of each value. */
  114. static void JUCE_CALLTYPE abs (float* dest, const float* src, int numValues) noexcept;
  115. /** Copies a source vector to a destination, taking the absolute of each value. */
  116. static void JUCE_CALLTYPE abs (double* dest, const double* src, int numValues) noexcept;
  117. /** Converts a stream of integers to floats, multiplying each one by the given multiplier. */
  118. static void JUCE_CALLTYPE convertFixedToFloat (float* dest, const int* src, float multiplier, int numValues) noexcept;
  119. /** Each element of dest will be the minimum of the corresponding element of the source array and the given comp value. */
  120. static void JUCE_CALLTYPE min (float* dest, const float* src, float comp, int num) noexcept;
  121. /** Each element of dest will be the minimum of the corresponding element of the source array and the given comp value. */
  122. static void JUCE_CALLTYPE min (double* dest, const double* src, double comp, int num) noexcept;
  123. /** Each element of dest will be the minimum of the corresponding source1 and source2 values. */
  124. static void JUCE_CALLTYPE min (float* dest, const float* src1, const float* src2, int num) noexcept;
  125. /** Each element of dest will be the minimum of the corresponding source1 and source2 values. */
  126. static void JUCE_CALLTYPE min (double* dest, const double* src1, const double* src2, int num) noexcept;
  127. /** Each element of dest will be the maximum of the corresponding element of the source array and the given comp value. */
  128. static void JUCE_CALLTYPE max (float* dest, const float* src, float comp, int num) noexcept;
  129. /** Each element of dest will be the maximum of the corresponding element of the source array and the given comp value. */
  130. static void JUCE_CALLTYPE max (double* dest, const double* src, double comp, int num) noexcept;
  131. /** Each element of dest will be the maximum of the corresponding source1 and source2 values. */
  132. static void JUCE_CALLTYPE max (float* dest, const float* src1, const float* src2, int num) noexcept;
  133. /** Each element of dest will be the maximum of the corresponding source1 and source2 values. */
  134. static void JUCE_CALLTYPE max (double* dest, const double* src1, const double* src2, int num) noexcept;
  135. /** Each element of dest is calculated by hard clipping the corresponding src element so that it is in the range specified by the arguments low and high. */
  136. static void JUCE_CALLTYPE clip (float* dest, const float* src, float low, float high, int num) noexcept;
  137. /** Each element of dest is calculated by hard clipping the corresponding src element so that it is in the range specified by the arguments low and high. */
  138. static void JUCE_CALLTYPE clip (double* dest, const double* src, double low, double high, int num) noexcept;
  139. /** Finds the minimum and maximum values in the given array. */
  140. static Range<float> JUCE_CALLTYPE findMinAndMax (const float* src, int numValues) noexcept;
  141. /** Finds the minimum and maximum values in the given array. */
  142. static Range<double> JUCE_CALLTYPE findMinAndMax (const double* src, int numValues) noexcept;
  143. /** Finds the minimum value in the given array. */
  144. static float JUCE_CALLTYPE findMinimum (const float* src, int numValues) noexcept;
  145. /** Finds the minimum value in the given array. */
  146. static double JUCE_CALLTYPE findMinimum (const double* src, int numValues) noexcept;
  147. /** Finds the maximum value in the given array. */
  148. static float JUCE_CALLTYPE findMaximum (const float* src, int numValues) noexcept;
  149. /** Finds the maximum value in the given array. */
  150. static double JUCE_CALLTYPE findMaximum (const double* src, int numValues) noexcept;
  151. /** This method enables or disables the SSE/NEON flush-to-zero mode. */
  152. static void JUCE_CALLTYPE enableFlushToZeroMode (bool shouldEnable) noexcept;
  153. /** On Intel CPUs, this method enables the SSE flush-to-zero and denormalised-are-zero modes.
  154. This effectively sets the DAZ and FZ bits of the MXCSR register. On arm CPUs this will
  155. enable flush to zero mode.
  156. It's a convenient thing to call before audio processing code where you really want to
  157. avoid denormalisation performance hits.
  158. */
  159. static void JUCE_CALLTYPE disableDenormalisedNumberSupport (bool shouldDisable = true) noexcept;
  160. /** This method returns true if denormals are currently disabled. */
  161. static bool JUCE_CALLTYPE areDenormalsDisabled() noexcept;
  162. private:
  163. friend ScopedNoDenormals;
  164. static intptr_t JUCE_CALLTYPE getFpStatusRegister() noexcept;
  165. static void JUCE_CALLTYPE setFpStatusRegister (intptr_t) noexcept;
  166. };
  167. //==============================================================================
  168. /**
  169. Helper class providing an RAII-based mechanism for temporarily disabling
  170. denormals on your CPU.
  171. @tags{Audio}
  172. */
  173. class ScopedNoDenormals
  174. {
  175. public:
  176. ScopedNoDenormals() noexcept;
  177. ~ScopedNoDenormals() noexcept;
  178. private:
  179. #if JUCE_USE_SSE_INTRINSICS || (JUCE_USE_ARM_NEON || defined (__arm64__) || defined (__aarch64__))
  180. intptr_t fpsr;
  181. #endif
  182. };
  183. } // namespace juce