The JUCE cross-platform C++ framework, with DISTRHO/KXStudio specific changes
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.

268 lines
10KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2022 - Raw Material Software Limited
  5. JUCE is an open source library subject to commercial or open-source
  6. licensing.
  7. By using JUCE, you agree to the terms of both the JUCE 7 End-User License
  8. Agreement and JUCE Privacy Policy.
  9. End User License Agreement: www.juce.com/juce-7-licence
  10. Privacy Policy: www.juce.com/juce-privacy-policy
  11. Or: You may also use this code under the terms of the GPL v3 (see
  12. www.gnu.org/licenses).
  13. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  14. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  15. DISCLAIMED.
  16. ==============================================================================
  17. */
  18. namespace juce
  19. {
  20. namespace dsp
  21. {
  22. /** A template specialisation to find corresponding mask type for primitives. */
  23. namespace SIMDInternal
  24. {
  25. template <typename Primitive> struct MaskTypeFor { using type = Primitive; };
  26. template <> struct MaskTypeFor <float> { using type = uint32_t; };
  27. template <> struct MaskTypeFor <double> { using type = uint64_t; };
  28. template <> struct MaskTypeFor <char> { using type = uint8_t; };
  29. template <> struct MaskTypeFor <int8_t> { using type = uint8_t; };
  30. template <> struct MaskTypeFor <int16_t> { using type = uint16_t; };
  31. template <> struct MaskTypeFor <int32_t> { using type = uint32_t; };
  32. template <> struct MaskTypeFor <int64_t> { using type = uint64_t; };
  33. template <> struct MaskTypeFor <std::complex<float>> { using type = uint32_t; };
  34. template <> struct MaskTypeFor <std::complex<double>> { using type = uint64_t; };
  35. template <typename Primitive> using MaskType = typename MaskTypeFor<Primitive>::type;
  36. template <typename Primitive> struct PrimitiveType { using type = std::remove_cv_t<Primitive>; };
  37. template <typename Primitive> struct PrimitiveType<std::complex<Primitive>> { using type = std::remove_cv_t<Primitive>; };
  38. template <int n> struct Log2Helper { enum { value = Log2Helper<n/2>::value + 1 }; };
  39. template <> struct Log2Helper<1> { enum { value = 0 }; };
  40. }
  41. /**
  42. Useful fallback routines to use if the native SIMD op is not supported. You
  43. should never need to use this directly. Use juce_SIMDRegister instead.
  44. @tags{DSP}
  45. */
  46. template <typename ScalarType, typename vSIMDType>
  47. struct SIMDFallbackOps
  48. {
  49. static constexpr size_t n = sizeof (vSIMDType) / sizeof (ScalarType);
  50. static constexpr size_t mask = (sizeof (vSIMDType) / sizeof (ScalarType)) - 1;
  51. static constexpr size_t bits = SIMDInternal::Log2Helper<(int) n>::value;
  52. // helper types
  53. using MaskType = SIMDInternal::MaskType<ScalarType>;
  54. union UnionType { vSIMDType v; ScalarType s[n]; };
  55. union UnionMaskType { vSIMDType v; MaskType m[n]; };
  56. // fallback methods
  57. static forcedinline vSIMDType add (vSIMDType a, vSIMDType b) noexcept { return apply<ScalarAdd> (a, b); }
  58. static forcedinline vSIMDType sub (vSIMDType a, vSIMDType b) noexcept { return apply<ScalarSub> (a, b); }
  59. static forcedinline vSIMDType mul (vSIMDType a, vSIMDType b) noexcept { return apply<ScalarMul> (a, b); }
  60. static forcedinline vSIMDType bit_and (vSIMDType a, vSIMDType b) noexcept { return bitapply<ScalarAnd> (a, b); }
  61. static forcedinline vSIMDType bit_or (vSIMDType a, vSIMDType b) noexcept { return bitapply<ScalarOr > (a, b); }
  62. static forcedinline vSIMDType bit_xor (vSIMDType a, vSIMDType b) noexcept { return bitapply<ScalarXor> (a, b); }
  63. static forcedinline vSIMDType bit_notand (vSIMDType a, vSIMDType b) noexcept { return bitapply<ScalarNot> (a, b); }
  64. static forcedinline vSIMDType min (vSIMDType a, vSIMDType b) noexcept { return apply<ScalarMin> (a, b); }
  65. static forcedinline vSIMDType max (vSIMDType a, vSIMDType b) noexcept { return apply<ScalarMax> (a, b); }
  66. static forcedinline vSIMDType equal (vSIMDType a, vSIMDType b) noexcept { return cmp<ScalarEq > (a, b); }
  67. static forcedinline vSIMDType notEqual (vSIMDType a, vSIMDType b) noexcept { return cmp<ScalarNeq> (a, b); }
  68. static forcedinline vSIMDType greaterThan (vSIMDType a, vSIMDType b) noexcept { return cmp<ScalarGt > (a, b); }
  69. static forcedinline vSIMDType greaterThanOrEqual (vSIMDType a, vSIMDType b) noexcept { return cmp<ScalarGeq> (a, b); }
  70. static forcedinline ScalarType get (vSIMDType v, size_t i) noexcept
  71. {
  72. UnionType u {v};
  73. return u.s[i];
  74. }
  75. static forcedinline vSIMDType set (vSIMDType v, size_t i, ScalarType s) noexcept
  76. {
  77. UnionType u {v};
  78. u.s[i] = s;
  79. return u.v;
  80. }
  81. static forcedinline vSIMDType bit_not (vSIMDType av) noexcept
  82. {
  83. UnionMaskType a {av};
  84. for (size_t i = 0; i < n; ++i)
  85. a.m[i] = ~a.m[i];
  86. return a.v;
  87. }
  88. static forcedinline ScalarType sum (vSIMDType av) noexcept
  89. {
  90. UnionType a {av};
  91. auto retval = static_cast<ScalarType> (0);
  92. for (size_t i = 0; i < n; ++i)
  93. retval = static_cast<ScalarType> (retval + a.s[i]);
  94. return retval;
  95. }
  96. static forcedinline vSIMDType truncate (vSIMDType av) noexcept
  97. {
  98. UnionType a {av};
  99. for (size_t i = 0; i < n; ++i)
  100. a.s[i] = static_cast<ScalarType> (static_cast<int> (a.s[i]));
  101. return a.v;
  102. }
  103. static forcedinline vSIMDType multiplyAdd (vSIMDType av, vSIMDType bv, vSIMDType cv) noexcept
  104. {
  105. UnionType a {av}, b {bv}, c {cv};
  106. for (size_t i = 0; i < n; ++i)
  107. a.s[i] += b.s[i] * c.s[i];
  108. return a.v;
  109. }
  110. //==============================================================================
  111. static forcedinline bool allEqual (vSIMDType av, vSIMDType bv) noexcept
  112. {
  113. UnionType a {av}, b {bv};
  114. for (size_t i = 0; i < n; ++i)
  115. if (a.s[i] != b.s[i])
  116. return false;
  117. return true;
  118. }
  119. //==============================================================================
  120. static forcedinline vSIMDType cmplxmul (vSIMDType av, vSIMDType bv) noexcept
  121. {
  122. UnionType a {av}, b {bv}, r;
  123. const int m = n >> 1;
  124. for (int i = 0; i < m; ++i)
  125. {
  126. std::complex<ScalarType> result
  127. = std::complex<ScalarType> (a.s[i<<1], a.s[(i<<1)|1])
  128. * std::complex<ScalarType> (b.s[i<<1], b.s[(i<<1)|1]);
  129. r.s[i<<1] = result.real();
  130. r.s[(i<<1)|1] = result.imag();
  131. }
  132. return r.v;
  133. }
  134. struct ScalarAdd { static forcedinline ScalarType op (ScalarType a, ScalarType b) noexcept { return a + b; } };
  135. struct ScalarSub { static forcedinline ScalarType op (ScalarType a, ScalarType b) noexcept { return a - b; } };
  136. struct ScalarMul { static forcedinline ScalarType op (ScalarType a, ScalarType b) noexcept { return a * b; } };
  137. struct ScalarMin { static forcedinline ScalarType op (ScalarType a, ScalarType b) noexcept { return jmin (a, b); } };
  138. struct ScalarMax { static forcedinline ScalarType op (ScalarType a, ScalarType b) noexcept { return jmax (a, b); } };
  139. struct ScalarAnd { static forcedinline MaskType op (MaskType a, MaskType b) noexcept { return a & b; } };
  140. struct ScalarOr { static forcedinline MaskType op (MaskType a, MaskType b) noexcept { return a | b; } };
  141. struct ScalarXor { static forcedinline MaskType op (MaskType a, MaskType b) noexcept { return a ^ b; } };
  142. struct ScalarNot { static forcedinline MaskType op (MaskType a, MaskType b) noexcept { return (~a) & b; } };
  143. struct ScalarEq { static forcedinline bool op (ScalarType a, ScalarType b) noexcept { return (a == b); } };
  144. struct ScalarNeq { static forcedinline bool op (ScalarType a, ScalarType b) noexcept { return (a != b); } };
  145. struct ScalarGt { static forcedinline bool op (ScalarType a, ScalarType b) noexcept { return (a > b); } };
  146. struct ScalarGeq { static forcedinline bool op (ScalarType a, ScalarType b) noexcept { return (a >= b); } };
  147. // generic apply routines for operations above
  148. template <typename Op>
  149. static forcedinline vSIMDType apply (vSIMDType av, vSIMDType bv) noexcept
  150. {
  151. UnionType a {av}, b {bv};
  152. for (size_t i = 0; i < n; ++i)
  153. a.s[i] = Op::op (a.s[i], b.s[i]);
  154. return a.v;
  155. }
  156. template <typename Op>
  157. static forcedinline vSIMDType cmp (vSIMDType av, vSIMDType bv) noexcept
  158. {
  159. UnionType a {av}, b {bv};
  160. UnionMaskType r;
  161. for (size_t i = 0; i < n; ++i)
  162. r.m[i] = Op::op (a.s[i], b.s[i]) ? static_cast<MaskType> (-1) : static_cast<MaskType> (0);
  163. return r.v;
  164. }
  165. template <typename Op>
  166. static forcedinline vSIMDType bitapply (vSIMDType av, vSIMDType bv) noexcept
  167. {
  168. UnionMaskType a {av}, b {bv};
  169. for (size_t i = 0; i < n; ++i)
  170. a.m[i] = Op::op (a.m[i], b.m[i]);
  171. return a.v;
  172. }
  173. static forcedinline vSIMDType expand (ScalarType s) noexcept
  174. {
  175. UnionType r;
  176. for (size_t i = 0; i < n; ++i)
  177. r.s[i] = s;
  178. return r.v;
  179. }
  180. static forcedinline vSIMDType load (const ScalarType* a) noexcept
  181. {
  182. UnionType r;
  183. for (size_t i = 0; i < n; ++i)
  184. r.s[i] = a[i];
  185. return r.v;
  186. }
  187. static forcedinline void store (vSIMDType av, ScalarType* dest) noexcept
  188. {
  189. UnionType a {av};
  190. for (size_t i = 0; i < n; ++i)
  191. dest[i] = a.s[i];
  192. }
  193. template <unsigned int shuffle_idx>
  194. static forcedinline vSIMDType shuffle (vSIMDType av) noexcept
  195. {
  196. UnionType a {av}, r;
  197. // the compiler will unroll this loop and the index can
  198. // be computed at compile-time, so this will be super fast
  199. for (size_t i = 0; i < n; ++i)
  200. r.s[i] = a.s[(shuffle_idx >> (bits * i)) & mask];
  201. return r.v;
  202. }
  203. };
  204. } // namespace dsp
  205. } // namespace juce