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.

214 lines
9.9KB

  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. /** A template specialisation to find corresponding mask type for primitives. */
  20. namespace SIMDInternal
  21. {
  22. template <typename Primitive> struct MaskTypeFor { typedef Primitive type; };
  23. template <> struct MaskTypeFor <float> { typedef uint32_t type; };
  24. template <> struct MaskTypeFor <double> { typedef uint64_t type; };
  25. template <> struct MaskTypeFor <char> { typedef uint8_t type; };
  26. template <> struct MaskTypeFor <int8_t> { typedef uint8_t type; };
  27. template <> struct MaskTypeFor <int16_t> { typedef uint16_t type; };
  28. template <> struct MaskTypeFor <int32_t> { typedef uint32_t type; };
  29. template <> struct MaskTypeFor <int64_t> { typedef uint64_t type; };
  30. template <> struct MaskTypeFor <std::complex<float> > { typedef uint32_t type; };
  31. template <> struct MaskTypeFor <std::complex<double> > { typedef uint64_t type; };
  32. template <typename Primitive> struct PrimitiveType { typedef Primitive type; };
  33. template <typename Primitive> struct PrimitiveType<std::complex<Primitive> > { typedef Primitive type; };
  34. template <int n> struct Log2Helper { enum { value = Log2Helper<n/2>::value + 1 }; };
  35. template <> struct Log2Helper<1> { enum { value = 0 }; };
  36. }
  37. /**
  38. Useful fallback routines to use if the native SIMD op is not supported. You
  39. should never need to use this directly. Use juce_SIMDRegister instead.
  40. */
  41. template <typename ScalarType, typename vSIMDType>
  42. struct SIMDFallbackOps
  43. {
  44. static constexpr size_t n = sizeof (vSIMDType) / sizeof (ScalarType);
  45. static constexpr size_t mask = (sizeof (vSIMDType) / sizeof (ScalarType)) - 1;
  46. static constexpr size_t bits = SIMDInternal::Log2Helper<n>::value;
  47. // corresponding mask type
  48. typedef typename SIMDInternal::MaskTypeFor<ScalarType>::type MaskType;
  49. // fallback methods
  50. static forcedinline vSIMDType add (vSIMDType a, vSIMDType b) noexcept { return apply<ScalarAdd> (a, b); }
  51. static forcedinline vSIMDType sub (vSIMDType a, vSIMDType b) noexcept { return apply<ScalarSub> (a, b); }
  52. static forcedinline vSIMDType mul (vSIMDType a, vSIMDType b) noexcept { return apply<ScalarMul> (a, b); }
  53. static forcedinline vSIMDType bit_and (vSIMDType a, vSIMDType b) noexcept { return bitapply<ScalarAnd> (a, b); }
  54. static forcedinline vSIMDType bit_or (vSIMDType a, vSIMDType b) noexcept { return bitapply<ScalarOr > (a, b); }
  55. static forcedinline vSIMDType bit_xor (vSIMDType a, vSIMDType b) noexcept { return bitapply<ScalarXor> (a, b); }
  56. static forcedinline vSIMDType bit_notand (vSIMDType a, vSIMDType b) noexcept { return bitapply<ScalarNot> (a, b); }
  57. static forcedinline vSIMDType min (vSIMDType a, vSIMDType b) noexcept { return apply<ScalarMin> (a, b); }
  58. static forcedinline vSIMDType max (vSIMDType a, vSIMDType b) noexcept { return apply<ScalarMax> (a, b); }
  59. static forcedinline vSIMDType equal (vSIMDType a, vSIMDType b) noexcept { return cmp<ScalarEq > (a, b); }
  60. static forcedinline vSIMDType notEqual (vSIMDType a, vSIMDType b) noexcept { return cmp<ScalarNeq> (a, b); }
  61. static forcedinline vSIMDType greaterThan (vSIMDType a, vSIMDType b) noexcept { return cmp<ScalarGt > (a, b); }
  62. static forcedinline vSIMDType greaterThanOrEqual (vSIMDType a, vSIMDType b) noexcept { return cmp<ScalarGeq> (a, b); }
  63. static forcedinline vSIMDType bit_not (vSIMDType a) noexcept
  64. {
  65. vSIMDType retval;
  66. auto* dst = reinterpret_cast<MaskType*> (&retval);
  67. auto* aSrc = reinterpret_cast<const MaskType*> (&a);
  68. for (int i = 0; i < n; ++i)
  69. dst [i] = ~aSrc [i];
  70. return retval;
  71. }
  72. static forcedinline ScalarType sum (vSIMDType a) noexcept
  73. {
  74. auto retval = static_cast<ScalarType> (0);
  75. auto* aSrc = reinterpret_cast<const ScalarType*> (&a);
  76. for (int i = 0; i < n; ++i)
  77. retval += aSrc [i];
  78. return retval;
  79. }
  80. static forcedinline vSIMDType multiplyAdd (vSIMDType a, vSIMDType b, vSIMDType c) noexcept
  81. {
  82. vSIMDType retval;
  83. auto* dst = reinterpret_cast<ScalarType*> (&retval);
  84. auto* aSrc = reinterpret_cast<const ScalarType*> (&a);
  85. auto* bSrc = reinterpret_cast<const ScalarType*> (&b);
  86. auto* cSrc = reinterpret_cast<const ScalarType*> (&c);
  87. for (int i = 0; i < n; ++i)
  88. dst [i] = aSrc [i] + (bSrc [i] * cSrc [i]);
  89. return retval;
  90. }
  91. //==============================================================================
  92. static forcedinline vSIMDType cmplxmul (vSIMDType a, vSIMDType b) noexcept
  93. {
  94. vSIMDType retval;
  95. auto* dst = reinterpret_cast<std::complex<ScalarType>*> (&retval);
  96. auto* aSrc = reinterpret_cast<const std::complex<ScalarType>*> (&a);
  97. auto* bSrc = reinterpret_cast<const std::complex<ScalarType>*> (&b);
  98. const int m = n >> 1;
  99. for (int i = 0; i < m; ++i)
  100. dst [i] = aSrc [i] * bSrc [i];
  101. return retval;
  102. }
  103. struct ScalarAdd { static forcedinline ScalarType op (ScalarType a, ScalarType b) noexcept { return a + b; } };
  104. struct ScalarSub { static forcedinline ScalarType op (ScalarType a, ScalarType b) noexcept { return a - b; } };
  105. struct ScalarMul { static forcedinline ScalarType op (ScalarType a, ScalarType b) noexcept { return a * b; } };
  106. struct ScalarMin { static forcedinline ScalarType op (ScalarType a, ScalarType b) noexcept { return jmin (a, b); } };
  107. struct ScalarMax { static forcedinline ScalarType op (ScalarType a, ScalarType b) noexcept { return jmax (a, b); } };
  108. struct ScalarAnd { static forcedinline MaskType op (MaskType a, MaskType b) noexcept { return a & b; } };
  109. struct ScalarOr { static forcedinline MaskType op (MaskType a, MaskType b) noexcept { return a | b; } };
  110. struct ScalarXor { static forcedinline MaskType op (MaskType a, MaskType b) noexcept { return a ^ b; } };
  111. struct ScalarNot { static forcedinline MaskType op (MaskType a, MaskType b) noexcept { return (~a) & b; } };
  112. struct ScalarEq { static forcedinline bool op (ScalarType a, ScalarType b) noexcept { return (a == b); } };
  113. struct ScalarNeq { static forcedinline bool op (ScalarType a, ScalarType b) noexcept { return (a != b); } };
  114. struct ScalarGt { static forcedinline bool op (ScalarType a, ScalarType b) noexcept { return (a > b); } };
  115. struct ScalarGeq { static forcedinline bool op (ScalarType a, ScalarType b) noexcept { return (a >= b); } };
  116. // generic apply routines for operations above
  117. template <typename Op>
  118. static forcedinline vSIMDType apply (vSIMDType a, vSIMDType b) noexcept
  119. {
  120. vSIMDType retval;
  121. auto* dst = reinterpret_cast<ScalarType*> (&retval);
  122. auto* aSrc = reinterpret_cast<const ScalarType*> (&a);
  123. auto* bSrc = reinterpret_cast<const ScalarType*> (&b);
  124. for (int i = 0; i < n; ++i)
  125. dst [i] = Op::op (aSrc [i], bSrc [i]);
  126. return retval;
  127. }
  128. template <typename Op>
  129. static forcedinline vSIMDType cmp (vSIMDType a, vSIMDType b) noexcept
  130. {
  131. vSIMDType retval;
  132. auto* dst = reinterpret_cast<MaskType*> (&retval);
  133. auto* aSrc = reinterpret_cast<const ScalarType*> (&a);
  134. auto* bSrc = reinterpret_cast<const ScalarType*> (&b);
  135. for (int i = 0; i < n; ++i)
  136. dst [i] = Op::op (aSrc [i], bSrc [i]) ? static_cast<MaskType> (-1) : static_cast<MaskType> (0);
  137. return retval;
  138. }
  139. template <typename Op>
  140. static forcedinline vSIMDType bitapply (vSIMDType a, vSIMDType b) noexcept
  141. {
  142. vSIMDType retval;
  143. auto* dst = reinterpret_cast<MaskType*> (&retval);
  144. auto* aSrc = reinterpret_cast<const MaskType*> (&a);
  145. auto* bSrc = reinterpret_cast<const MaskType*> (&b);
  146. for (int i = 0; i < n; ++i)
  147. dst [i] = Op::op (aSrc [i], bSrc [i]);
  148. return retval;
  149. }
  150. static forcedinline vSIMDType expand (ScalarType s) noexcept
  151. {
  152. vSIMDType retval;
  153. auto* dst = reinterpret_cast<ScalarType*> (&retval);
  154. for (int i = 0; i < n; ++i)
  155. dst [i] = s;
  156. return retval;
  157. }
  158. template <unsigned int shuffle_idx>
  159. static forcedinline vSIMDType shuffle (vSIMDType a) noexcept
  160. {
  161. vSIMDType retval;
  162. auto* dst = reinterpret_cast<ScalarType*> (&retval);
  163. auto* aSrc = reinterpret_cast<const ScalarType*> (&a);
  164. // the compiler will unroll this loop and the index can
  165. // be computed at compile-time, so this will be super fast
  166. for (int i = 0; i < n; ++i)
  167. dst [i] = aSrc [(shuffle_idx >> (bits * i)) & mask];
  168. return retval;
  169. }
  170. };