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.

262 lines
10KB

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