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.

270 lines
10KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2017 - ROLI Ltd.
  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 5 End-User License
  8. Agreement and JUCE 5 Privacy Policy (both updated and effective as of the
  9. 27th April 2017).
  10. End User License Agreement: www.juce.com/juce-5-licence
  11. Privacy Policy: www.juce.com/juce-5-privacy-policy
  12. Or: You may also use this code under the terms of the GPL v3 (see
  13. www.gnu.org/licenses).
  14. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  15. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  16. DISCLAIMED.
  17. ==============================================================================
  18. */
  19. namespace juce
  20. {
  21. namespace dsp
  22. {
  23. /** A template specialisation to find corresponding mask type for primitives. */
  24. namespace SIMDInternal
  25. {
  26. template <typename Primitive> struct MaskTypeFor { using type = Primitive; };
  27. template <> struct MaskTypeFor <float> { using type = uint32_t; };
  28. template <> struct MaskTypeFor <double> { using type = uint64_t; };
  29. template <> struct MaskTypeFor <char> { using type = uint8_t; };
  30. template <> struct MaskTypeFor <int8_t> { using type = uint8_t; };
  31. template <> struct MaskTypeFor <int16_t> { using type = uint16_t; };
  32. template <> struct MaskTypeFor <int32_t> { using type = uint32_t; };
  33. template <> struct MaskTypeFor <int64_t> { using type = uint64_t; };
  34. template <> struct MaskTypeFor <std::complex<float>> { using type = uint32_t; };
  35. template <> struct MaskTypeFor <std::complex<double>> { using type = uint64_t; };
  36. template <typename Primitive> struct PrimitiveType { using type = typename std::remove_cv<Primitive>::type; };
  37. template <typename Primitive> struct PrimitiveType<std::complex<Primitive>> { using type = typename std::remove_cv<Primitive>::type; };
  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 = typename SIMDInternal::MaskTypeFor<ScalarType>::type;
  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 += 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. {
  101. jassert (a.s[i] >= ScalarType (0));
  102. a.s[i] = static_cast <ScalarType> (static_cast<int> (a.s[i]));
  103. }
  104. return a.v;
  105. }
  106. static forcedinline vSIMDType multiplyAdd (vSIMDType av, vSIMDType bv, vSIMDType cv) noexcept
  107. {
  108. UnionType a {av}, b {bv}, c {cv};
  109. for (size_t i = 0; i < n; ++i)
  110. a.s[i] += b.s[i] * c.s[i];
  111. return a.v;
  112. }
  113. //==============================================================================
  114. static forcedinline bool allEqual (vSIMDType av, vSIMDType bv) noexcept
  115. {
  116. UnionType a {av}, b {bv};
  117. for (size_t i = 0; i < n; ++i)
  118. if (a.s[i] != b.s[i])
  119. return false;
  120. return true;
  121. }
  122. //==============================================================================
  123. static forcedinline vSIMDType cmplxmul (vSIMDType av, vSIMDType bv) noexcept
  124. {
  125. UnionType a {av}, b {bv}, r;
  126. const int m = n >> 1;
  127. for (int i = 0; i < m; ++i)
  128. {
  129. std::complex<ScalarType> result
  130. = std::complex<ScalarType> (a.s[i<<1], a.s[(i<<1)|1])
  131. * std::complex<ScalarType> (b.s[i<<1], b.s[(i<<1)|1]);
  132. r.s[i<<1] = result.real();
  133. r.s[(i<<1)|1] = result.imag();
  134. }
  135. return r.v;
  136. }
  137. struct ScalarAdd { static forcedinline ScalarType op (ScalarType a, ScalarType b) noexcept { return a + b; } };
  138. struct ScalarSub { static forcedinline ScalarType op (ScalarType a, ScalarType b) noexcept { return a - b; } };
  139. struct ScalarMul { static forcedinline ScalarType op (ScalarType a, ScalarType b) noexcept { return a * b; } };
  140. struct ScalarMin { static forcedinline ScalarType op (ScalarType a, ScalarType b) noexcept { return jmin (a, b); } };
  141. struct ScalarMax { static forcedinline ScalarType op (ScalarType a, ScalarType b) noexcept { return jmax (a, b); } };
  142. struct ScalarAnd { static forcedinline MaskType op (MaskType a, MaskType b) noexcept { return a & b; } };
  143. struct ScalarOr { static forcedinline MaskType op (MaskType a, MaskType b) noexcept { return a | b; } };
  144. struct ScalarXor { static forcedinline MaskType op (MaskType a, MaskType b) noexcept { return a ^ b; } };
  145. struct ScalarNot { static forcedinline MaskType op (MaskType a, MaskType b) noexcept { return (~a) & b; } };
  146. struct ScalarEq { static forcedinline bool op (ScalarType a, ScalarType b) noexcept { return (a == b); } };
  147. struct ScalarNeq { static forcedinline bool op (ScalarType a, ScalarType b) noexcept { return (a != b); } };
  148. struct ScalarGt { static forcedinline bool op (ScalarType a, ScalarType b) noexcept { return (a > b); } };
  149. struct ScalarGeq { static forcedinline bool op (ScalarType a, ScalarType b) noexcept { return (a >= b); } };
  150. // generic apply routines for operations above
  151. template <typename Op>
  152. static forcedinline vSIMDType apply (vSIMDType av, vSIMDType bv) noexcept
  153. {
  154. UnionType a {av}, b {bv};
  155. for (size_t i = 0; i < n; ++i)
  156. a.s[i] = Op::op (a.s[i], b.s[i]);
  157. return a.v;
  158. }
  159. template <typename Op>
  160. static forcedinline vSIMDType cmp (vSIMDType av, vSIMDType bv) noexcept
  161. {
  162. UnionType a {av}, b {bv};
  163. UnionMaskType r;
  164. for (size_t i = 0; i < n; ++i)
  165. r.m[i] = Op::op (a.s[i], b.s[i]) ? static_cast<MaskType> (-1) : static_cast<MaskType> (0);
  166. return r.v;
  167. }
  168. template <typename Op>
  169. static forcedinline vSIMDType bitapply (vSIMDType av, vSIMDType bv) noexcept
  170. {
  171. UnionMaskType a {av}, b {bv};
  172. for (size_t i = 0; i < n; ++i)
  173. a.m[i] = Op::op (a.m[i], b.m[i]);
  174. return a.v;
  175. }
  176. static forcedinline vSIMDType expand (ScalarType s) noexcept
  177. {
  178. UnionType r;
  179. for (size_t i = 0; i < n; ++i)
  180. r.s[i] = s;
  181. return r.v;
  182. }
  183. static forcedinline vSIMDType load (const ScalarType* a) noexcept
  184. {
  185. UnionType r;
  186. for (size_t i = 0; i < n; ++i)
  187. r.s[i] = a[i];
  188. return r.v;
  189. }
  190. static forcedinline void store (vSIMDType av, ScalarType* dest) noexcept
  191. {
  192. UnionType a {av};
  193. for (size_t i = 0; i < n; ++i)
  194. dest[i] = a.s[i];
  195. }
  196. template <unsigned int shuffle_idx>
  197. static forcedinline vSIMDType shuffle (vSIMDType av) noexcept
  198. {
  199. UnionType a {av}, r;
  200. // the compiler will unroll this loop and the index can
  201. // be computed at compile-time, so this will be super fast
  202. for (size_t i = 0; i < n; ++i)
  203. r.s[i] = a.s[(shuffle_idx >> (bits * i)) & mask];
  204. return r.v;
  205. }
  206. };
  207. } // namespace dsp
  208. } // namespace juce