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.

1713 lines
77KB

  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. 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. namespace FloatVectorHelpers
  20. {
  21. #define JUCE_INCREMENT_SRC_DEST dest += (16 / sizeof (*dest)); src += (16 / sizeof (*dest));
  22. #define JUCE_INCREMENT_SRC1_SRC2_DEST dest += (16 / sizeof (*dest)); src1 += (16 / sizeof (*dest)); src2 += (16 / sizeof (*dest));
  23. #define JUCE_INCREMENT_DEST dest += (16 / sizeof (*dest));
  24. #if JUCE_USE_SSE_INTRINSICS
  25. static bool isAligned (const void* p) noexcept
  26. {
  27. return (((pointer_sized_int) p) & 15) == 0;
  28. }
  29. struct BasicOps32
  30. {
  31. using Type = float;
  32. using ParallelType = __m128;
  33. using IntegerType = __m128;
  34. enum { numParallel = 4 };
  35. // Integer and parallel types are the same for SSE. On neon they have different types
  36. static forcedinline IntegerType toint (ParallelType v) noexcept { return v; }
  37. static forcedinline ParallelType toflt (IntegerType v) noexcept { return v; }
  38. static forcedinline ParallelType load1 (Type v) noexcept { return _mm_load1_ps (&v); }
  39. static forcedinline ParallelType loadA (const Type* v) noexcept { return _mm_load_ps (v); }
  40. static forcedinline ParallelType loadU (const Type* v) noexcept { return _mm_loadu_ps (v); }
  41. static forcedinline void storeA (Type* dest, ParallelType a) noexcept { _mm_store_ps (dest, a); }
  42. static forcedinline void storeU (Type* dest, ParallelType a) noexcept { _mm_storeu_ps (dest, a); }
  43. static forcedinline ParallelType add (ParallelType a, ParallelType b) noexcept { return _mm_add_ps (a, b); }
  44. static forcedinline ParallelType sub (ParallelType a, ParallelType b) noexcept { return _mm_sub_ps (a, b); }
  45. static forcedinline ParallelType mul (ParallelType a, ParallelType b) noexcept { return _mm_mul_ps (a, b); }
  46. static forcedinline ParallelType max (ParallelType a, ParallelType b) noexcept { return _mm_max_ps (a, b); }
  47. static forcedinline ParallelType min (ParallelType a, ParallelType b) noexcept { return _mm_min_ps (a, b); }
  48. static forcedinline ParallelType bit_and (ParallelType a, ParallelType b) noexcept { return _mm_and_ps (a, b); }
  49. static forcedinline ParallelType bit_not (ParallelType a, ParallelType b) noexcept { return _mm_andnot_ps (a, b); }
  50. static forcedinline ParallelType bit_or (ParallelType a, ParallelType b) noexcept { return _mm_or_ps (a, b); }
  51. static forcedinline ParallelType bit_xor (ParallelType a, ParallelType b) noexcept { return _mm_xor_ps (a, b); }
  52. static forcedinline Type max (ParallelType a) noexcept { Type v[numParallel]; storeU (v, a); return jmax (v[0], v[1], v[2], v[3]); }
  53. static forcedinline Type min (ParallelType a) noexcept { Type v[numParallel]; storeU (v, a); return jmin (v[0], v[1], v[2], v[3]); }
  54. };
  55. struct BasicOps64
  56. {
  57. using Type = double;
  58. using ParallelType = __m128d;
  59. using IntegerType = __m128d;
  60. enum { numParallel = 2 };
  61. // Integer and parallel types are the same for SSE. On neon they have different types
  62. static forcedinline IntegerType toint (ParallelType v) noexcept { return v; }
  63. static forcedinline ParallelType toflt (IntegerType v) noexcept { return v; }
  64. static forcedinline ParallelType load1 (Type v) noexcept { return _mm_load1_pd (&v); }
  65. static forcedinline ParallelType loadA (const Type* v) noexcept { return _mm_load_pd (v); }
  66. static forcedinline ParallelType loadU (const Type* v) noexcept { return _mm_loadu_pd (v); }
  67. static forcedinline void storeA (Type* dest, ParallelType a) noexcept { _mm_store_pd (dest, a); }
  68. static forcedinline void storeU (Type* dest, ParallelType a) noexcept { _mm_storeu_pd (dest, a); }
  69. static forcedinline ParallelType add (ParallelType a, ParallelType b) noexcept { return _mm_add_pd (a, b); }
  70. static forcedinline ParallelType sub (ParallelType a, ParallelType b) noexcept { return _mm_sub_pd (a, b); }
  71. static forcedinline ParallelType mul (ParallelType a, ParallelType b) noexcept { return _mm_mul_pd (a, b); }
  72. static forcedinline ParallelType max (ParallelType a, ParallelType b) noexcept { return _mm_max_pd (a, b); }
  73. static forcedinline ParallelType min (ParallelType a, ParallelType b) noexcept { return _mm_min_pd (a, b); }
  74. static forcedinline ParallelType bit_and (ParallelType a, ParallelType b) noexcept { return _mm_and_pd (a, b); }
  75. static forcedinline ParallelType bit_not (ParallelType a, ParallelType b) noexcept { return _mm_andnot_pd (a, b); }
  76. static forcedinline ParallelType bit_or (ParallelType a, ParallelType b) noexcept { return _mm_or_pd (a, b); }
  77. static forcedinline ParallelType bit_xor (ParallelType a, ParallelType b) noexcept { return _mm_xor_pd (a, b); }
  78. static forcedinline Type max (ParallelType a) noexcept { Type v[numParallel]; storeU (v, a); return jmax (v[0], v[1]); }
  79. static forcedinline Type min (ParallelType a) noexcept { Type v[numParallel]; storeU (v, a); return jmin (v[0], v[1]); }
  80. };
  81. #define JUCE_BEGIN_VEC_OP \
  82. using Mode = FloatVectorHelpers::ModeType<sizeof(*dest)>::Mode; \
  83. { \
  84. const auto numLongOps = num / Mode::numParallel;
  85. #define JUCE_FINISH_VEC_OP(normalOp) \
  86. num &= (Mode::numParallel - 1); \
  87. if (num == 0) return; \
  88. } \
  89. for (auto i = (decltype (num)) 0; i < num; ++i) normalOp;
  90. #define JUCE_PERFORM_VEC_OP_DEST(normalOp, vecOp, locals, setupOp) \
  91. JUCE_BEGIN_VEC_OP \
  92. setupOp \
  93. if (FloatVectorHelpers::isAligned (dest)) JUCE_VEC_LOOP (vecOp, dummy, Mode::loadA, Mode::storeA, locals, JUCE_INCREMENT_DEST) \
  94. else JUCE_VEC_LOOP (vecOp, dummy, Mode::loadU, Mode::storeU, locals, JUCE_INCREMENT_DEST) \
  95. JUCE_FINISH_VEC_OP (normalOp)
  96. #define JUCE_PERFORM_VEC_OP_SRC_DEST(normalOp, vecOp, locals, increment, setupOp) \
  97. JUCE_BEGIN_VEC_OP \
  98. setupOp \
  99. if (FloatVectorHelpers::isAligned (dest)) \
  100. { \
  101. if (FloatVectorHelpers::isAligned (src)) JUCE_VEC_LOOP (vecOp, Mode::loadA, Mode::loadA, Mode::storeA, locals, increment) \
  102. else JUCE_VEC_LOOP (vecOp, Mode::loadU, Mode::loadA, Mode::storeA, locals, increment) \
  103. }\
  104. else \
  105. { \
  106. if (FloatVectorHelpers::isAligned (src)) JUCE_VEC_LOOP (vecOp, Mode::loadA, Mode::loadU, Mode::storeU, locals, increment) \
  107. else JUCE_VEC_LOOP (vecOp, Mode::loadU, Mode::loadU, Mode::storeU, locals, increment) \
  108. } \
  109. JUCE_FINISH_VEC_OP (normalOp)
  110. #define JUCE_PERFORM_VEC_OP_SRC1_SRC2_DEST(normalOp, vecOp, locals, increment, setupOp) \
  111. JUCE_BEGIN_VEC_OP \
  112. setupOp \
  113. if (FloatVectorHelpers::isAligned (dest)) \
  114. { \
  115. if (FloatVectorHelpers::isAligned (src1)) \
  116. { \
  117. if (FloatVectorHelpers::isAligned (src2)) JUCE_VEC_LOOP_TWO_SOURCES (vecOp, Mode::loadA, Mode::loadA, Mode::storeA, locals, increment) \
  118. else JUCE_VEC_LOOP_TWO_SOURCES (vecOp, Mode::loadA, Mode::loadU, Mode::storeA, locals, increment) \
  119. } \
  120. else \
  121. { \
  122. if (FloatVectorHelpers::isAligned (src2)) JUCE_VEC_LOOP_TWO_SOURCES (vecOp, Mode::loadU, Mode::loadA, Mode::storeA, locals, increment) \
  123. else JUCE_VEC_LOOP_TWO_SOURCES (vecOp, Mode::loadU, Mode::loadU, Mode::storeA, locals, increment) \
  124. } \
  125. } \
  126. else \
  127. { \
  128. if (FloatVectorHelpers::isAligned (src1)) \
  129. { \
  130. if (FloatVectorHelpers::isAligned (src2)) JUCE_VEC_LOOP_TWO_SOURCES (vecOp, Mode::loadA, Mode::loadA, Mode::storeU, locals, increment) \
  131. else JUCE_VEC_LOOP_TWO_SOURCES (vecOp, Mode::loadA, Mode::loadU, Mode::storeU, locals, increment) \
  132. } \
  133. else \
  134. { \
  135. if (FloatVectorHelpers::isAligned (src2)) JUCE_VEC_LOOP_TWO_SOURCES (vecOp, Mode::loadU, Mode::loadA, Mode::storeU, locals, increment) \
  136. else JUCE_VEC_LOOP_TWO_SOURCES (vecOp, Mode::loadU, Mode::loadU, Mode::storeU, locals, increment) \
  137. } \
  138. } \
  139. JUCE_FINISH_VEC_OP (normalOp)
  140. #define JUCE_PERFORM_VEC_OP_SRC1_SRC2_DEST_DEST(normalOp, vecOp, locals, increment, setupOp) \
  141. JUCE_BEGIN_VEC_OP \
  142. setupOp \
  143. if (FloatVectorHelpers::isAligned (dest)) \
  144. { \
  145. if (FloatVectorHelpers::isAligned (src1)) \
  146. { \
  147. if (FloatVectorHelpers::isAligned (src2)) JUCE_VEC_LOOP_TWO_SOURCES_WITH_DEST_LOAD (vecOp, Mode::loadA, Mode::loadA, Mode::loadA, Mode::storeA, locals, increment) \
  148. else JUCE_VEC_LOOP_TWO_SOURCES_WITH_DEST_LOAD (vecOp, Mode::loadA, Mode::loadU, Mode::loadA, Mode::storeA, locals, increment) \
  149. } \
  150. else \
  151. { \
  152. if (FloatVectorHelpers::isAligned (src2)) JUCE_VEC_LOOP_TWO_SOURCES_WITH_DEST_LOAD (vecOp, Mode::loadU, Mode::loadA, Mode::loadA, Mode::storeA, locals, increment) \
  153. else JUCE_VEC_LOOP_TWO_SOURCES_WITH_DEST_LOAD (vecOp, Mode::loadU, Mode::loadU, Mode::loadA, Mode::storeA, locals, increment) \
  154. } \
  155. } \
  156. else \
  157. { \
  158. if (FloatVectorHelpers::isAligned (src1)) \
  159. { \
  160. if (FloatVectorHelpers::isAligned (src2)) JUCE_VEC_LOOP_TWO_SOURCES_WITH_DEST_LOAD (vecOp, Mode::loadA, Mode::loadA, Mode::loadU, Mode::storeU, locals, increment) \
  161. else JUCE_VEC_LOOP_TWO_SOURCES_WITH_DEST_LOAD (vecOp, Mode::loadA, Mode::loadU, Mode::loadU, Mode::storeU, locals, increment) \
  162. } \
  163. else \
  164. { \
  165. if (FloatVectorHelpers::isAligned (src2)) JUCE_VEC_LOOP_TWO_SOURCES_WITH_DEST_LOAD (vecOp, Mode::loadU, Mode::loadA, Mode::loadU, Mode::storeU, locals, increment) \
  166. else JUCE_VEC_LOOP_TWO_SOURCES_WITH_DEST_LOAD (vecOp, Mode::loadU, Mode::loadU, Mode::loadU, Mode::storeU, locals, increment) \
  167. } \
  168. } \
  169. JUCE_FINISH_VEC_OP (normalOp)
  170. //==============================================================================
  171. #elif JUCE_USE_ARM_NEON
  172. struct BasicOps32
  173. {
  174. using Type = float;
  175. using ParallelType = float32x4_t;
  176. using IntegerType = uint32x4_t;
  177. union signMaskUnion { ParallelType f; IntegerType i; };
  178. enum { numParallel = 4 };
  179. static forcedinline IntegerType toint (ParallelType v) noexcept { signMaskUnion u; u.f = v; return u.i; }
  180. static forcedinline ParallelType toflt (IntegerType v) noexcept { signMaskUnion u; u.i = v; return u.f; }
  181. static forcedinline ParallelType load1 (Type v) noexcept { return vld1q_dup_f32 (&v); }
  182. static forcedinline ParallelType loadA (const Type* v) noexcept { return vld1q_f32 (v); }
  183. static forcedinline ParallelType loadU (const Type* v) noexcept { return vld1q_f32 (v); }
  184. static forcedinline void storeA (Type* dest, ParallelType a) noexcept { vst1q_f32 (dest, a); }
  185. static forcedinline void storeU (Type* dest, ParallelType a) noexcept { vst1q_f32 (dest, a); }
  186. static forcedinline ParallelType add (ParallelType a, ParallelType b) noexcept { return vaddq_f32 (a, b); }
  187. static forcedinline ParallelType sub (ParallelType a, ParallelType b) noexcept { return vsubq_f32 (a, b); }
  188. static forcedinline ParallelType mul (ParallelType a, ParallelType b) noexcept { return vmulq_f32 (a, b); }
  189. static forcedinline ParallelType max (ParallelType a, ParallelType b) noexcept { return vmaxq_f32 (a, b); }
  190. static forcedinline ParallelType min (ParallelType a, ParallelType b) noexcept { return vminq_f32 (a, b); }
  191. static forcedinline ParallelType bit_and (ParallelType a, ParallelType b) noexcept { return toflt (vandq_u32 (toint (a), toint (b))); }
  192. static forcedinline ParallelType bit_not (ParallelType a, ParallelType b) noexcept { return toflt (vbicq_u32 (toint (a), toint (b))); }
  193. static forcedinline ParallelType bit_or (ParallelType a, ParallelType b) noexcept { return toflt (vorrq_u32 (toint (a), toint (b))); }
  194. static forcedinline ParallelType bit_xor (ParallelType a, ParallelType b) noexcept { return toflt (veorq_u32 (toint (a), toint (b))); }
  195. static forcedinline Type max (ParallelType a) noexcept { Type v[numParallel]; storeU (v, a); return jmax (v[0], v[1], v[2], v[3]); }
  196. static forcedinline Type min (ParallelType a) noexcept { Type v[numParallel]; storeU (v, a); return jmin (v[0], v[1], v[2], v[3]); }
  197. };
  198. struct BasicOps64
  199. {
  200. using Type = double;
  201. using ParallelType = double;
  202. using IntegerType = uint64;
  203. union signMaskUnion { ParallelType f; IntegerType i; };
  204. enum { numParallel = 1 };
  205. static forcedinline IntegerType toint (ParallelType v) noexcept { signMaskUnion u; u.f = v; return u.i; }
  206. static forcedinline ParallelType toflt (IntegerType v) noexcept { signMaskUnion u; u.i = v; return u.f; }
  207. static forcedinline ParallelType load1 (Type v) noexcept { return v; }
  208. static forcedinline ParallelType loadA (const Type* v) noexcept { return *v; }
  209. static forcedinline ParallelType loadU (const Type* v) noexcept { return *v; }
  210. static forcedinline void storeA (Type* dest, ParallelType a) noexcept { *dest = a; }
  211. static forcedinline void storeU (Type* dest, ParallelType a) noexcept { *dest = a; }
  212. static forcedinline ParallelType add (ParallelType a, ParallelType b) noexcept { return a + b; }
  213. static forcedinline ParallelType sub (ParallelType a, ParallelType b) noexcept { return a - b; }
  214. static forcedinline ParallelType mul (ParallelType a, ParallelType b) noexcept { return a * b; }
  215. static forcedinline ParallelType max (ParallelType a, ParallelType b) noexcept { return jmax (a, b); }
  216. static forcedinline ParallelType min (ParallelType a, ParallelType b) noexcept { return jmin (a, b); }
  217. static forcedinline ParallelType bit_and (ParallelType a, ParallelType b) noexcept { return toflt (toint (a) & toint (b)); }
  218. static forcedinline ParallelType bit_not (ParallelType a, ParallelType b) noexcept { return toflt ((~toint (a)) & toint (b)); }
  219. static forcedinline ParallelType bit_or (ParallelType a, ParallelType b) noexcept { return toflt (toint (a) | toint (b)); }
  220. static forcedinline ParallelType bit_xor (ParallelType a, ParallelType b) noexcept { return toflt (toint (a) ^ toint (b)); }
  221. static forcedinline Type max (ParallelType a) noexcept { return a; }
  222. static forcedinline Type min (ParallelType a) noexcept { return a; }
  223. };
  224. #define JUCE_BEGIN_VEC_OP \
  225. using Mode = FloatVectorHelpers::ModeType<sizeof(*dest)>::Mode; \
  226. if (Mode::numParallel > 1) \
  227. { \
  228. const auto numLongOps = num / Mode::numParallel;
  229. #define JUCE_FINISH_VEC_OP(normalOp) \
  230. num &= (Mode::numParallel - 1); \
  231. if (num == 0) return; \
  232. } \
  233. for (auto i = (decltype (num)) 0; i < num; ++i) normalOp;
  234. #define JUCE_PERFORM_VEC_OP_DEST(normalOp, vecOp, locals, setupOp) \
  235. JUCE_BEGIN_VEC_OP \
  236. setupOp \
  237. JUCE_VEC_LOOP (vecOp, dummy, Mode::loadU, Mode::storeU, locals, JUCE_INCREMENT_DEST) \
  238. JUCE_FINISH_VEC_OP (normalOp)
  239. #define JUCE_PERFORM_VEC_OP_SRC_DEST(normalOp, vecOp, locals, increment, setupOp) \
  240. JUCE_BEGIN_VEC_OP \
  241. setupOp \
  242. JUCE_VEC_LOOP (vecOp, Mode::loadU, Mode::loadU, Mode::storeU, locals, increment) \
  243. JUCE_FINISH_VEC_OP (normalOp)
  244. #define JUCE_PERFORM_VEC_OP_SRC1_SRC2_DEST(normalOp, vecOp, locals, increment, setupOp) \
  245. JUCE_BEGIN_VEC_OP \
  246. setupOp \
  247. JUCE_VEC_LOOP_TWO_SOURCES (vecOp, Mode::loadU, Mode::loadU, Mode::storeU, locals, increment) \
  248. JUCE_FINISH_VEC_OP (normalOp)
  249. #define JUCE_PERFORM_VEC_OP_SRC1_SRC2_DEST_DEST(normalOp, vecOp, locals, increment, setupOp) \
  250. JUCE_BEGIN_VEC_OP \
  251. setupOp \
  252. JUCE_VEC_LOOP_TWO_SOURCES_WITH_DEST_LOAD (vecOp, Mode::loadU, Mode::loadU, Mode::loadU, Mode::storeU, locals, increment) \
  253. JUCE_FINISH_VEC_OP (normalOp)
  254. //==============================================================================
  255. #else
  256. #define JUCE_PERFORM_VEC_OP_DEST(normalOp, vecOp, locals, setupOp) \
  257. for (auto i = (decltype (num)) 0; i < num; ++i) normalOp;
  258. #define JUCE_PERFORM_VEC_OP_SRC_DEST(normalOp, vecOp, locals, increment, setupOp) \
  259. for (auto i = (decltype (num)) 0; i < num; ++i) normalOp;
  260. #define JUCE_PERFORM_VEC_OP_SRC1_SRC2_DEST(normalOp, vecOp, locals, increment, setupOp) \
  261. for (auto i = (decltype (num)) 0; i < num; ++i) normalOp;
  262. #define JUCE_PERFORM_VEC_OP_SRC1_SRC2_DEST_DEST(normalOp, vecOp, locals, increment, setupOp) \
  263. for (auto i = (decltype (num)) 0; i < num; ++i) normalOp;
  264. #endif
  265. //==============================================================================
  266. #define JUCE_VEC_LOOP(vecOp, srcLoad, dstLoad, dstStore, locals, increment) \
  267. for (auto i = (decltype (numLongOps)) 0; i < numLongOps; ++i) \
  268. { \
  269. locals (srcLoad, dstLoad); \
  270. dstStore (dest, vecOp); \
  271. increment; \
  272. }
  273. #define JUCE_VEC_LOOP_TWO_SOURCES(vecOp, src1Load, src2Load, dstStore, locals, increment) \
  274. for (auto i = (decltype (numLongOps)) 0; i < numLongOps; ++i) \
  275. { \
  276. locals (src1Load, src2Load); \
  277. dstStore (dest, vecOp); \
  278. increment; \
  279. }
  280. #define JUCE_VEC_LOOP_TWO_SOURCES_WITH_DEST_LOAD(vecOp, src1Load, src2Load, dstLoad, dstStore, locals, increment) \
  281. for (auto i = (decltype (numLongOps)) 0; i < numLongOps; ++i) \
  282. { \
  283. locals (src1Load, src2Load, dstLoad); \
  284. dstStore (dest, vecOp); \
  285. increment; \
  286. }
  287. #define JUCE_LOAD_NONE(srcLoad, dstLoad)
  288. #define JUCE_LOAD_DEST(srcLoad, dstLoad) const Mode::ParallelType d = dstLoad (dest);
  289. #define JUCE_LOAD_SRC(srcLoad, dstLoad) const Mode::ParallelType s = srcLoad (src);
  290. #define JUCE_LOAD_SRC1_SRC2(src1Load, src2Load) const Mode::ParallelType s1 = src1Load (src1), s2 = src2Load (src2);
  291. #define JUCE_LOAD_SRC1_SRC2_DEST(src1Load, src2Load, dstLoad) const Mode::ParallelType d = dstLoad (dest), s1 = src1Load (src1), s2 = src2Load (src2);
  292. #define JUCE_LOAD_SRC_DEST(srcLoad, dstLoad) const Mode::ParallelType d = dstLoad (dest), s = srcLoad (src);
  293. union signMask32 { float f; uint32 i; };
  294. union signMask64 { double d; uint64 i; };
  295. #if JUCE_USE_SSE_INTRINSICS || JUCE_USE_ARM_NEON
  296. template <int typeSize> struct ModeType { using Mode = BasicOps32; };
  297. template <> struct ModeType<8> { using Mode = BasicOps64; };
  298. template <typename Mode>
  299. struct MinMax
  300. {
  301. using Type = typename Mode::Type;
  302. using ParallelType = typename Mode::ParallelType;
  303. template <typename Size>
  304. static Type findMinOrMax (const Type* src, Size num, const bool isMinimum) noexcept
  305. {
  306. auto numLongOps = num / Mode::numParallel;
  307. if (numLongOps > 1)
  308. {
  309. ParallelType val;
  310. #if ! JUCE_USE_ARM_NEON
  311. if (isAligned (src))
  312. {
  313. val = Mode::loadA (src);
  314. if (isMinimum)
  315. {
  316. while (--numLongOps > 0)
  317. {
  318. src += Mode::numParallel;
  319. val = Mode::min (val, Mode::loadA (src));
  320. }
  321. }
  322. else
  323. {
  324. while (--numLongOps > 0)
  325. {
  326. src += Mode::numParallel;
  327. val = Mode::max (val, Mode::loadA (src));
  328. }
  329. }
  330. }
  331. else
  332. #endif
  333. {
  334. val = Mode::loadU (src);
  335. if (isMinimum)
  336. {
  337. while (--numLongOps > 0)
  338. {
  339. src += Mode::numParallel;
  340. val = Mode::min (val, Mode::loadU (src));
  341. }
  342. }
  343. else
  344. {
  345. while (--numLongOps > 0)
  346. {
  347. src += Mode::numParallel;
  348. val = Mode::max (val, Mode::loadU (src));
  349. }
  350. }
  351. }
  352. Type result = isMinimum ? Mode::min (val)
  353. : Mode::max (val);
  354. num &= (Mode::numParallel - 1);
  355. src += Mode::numParallel;
  356. for (auto i = (decltype (num)) 0; i < num; ++i)
  357. result = isMinimum ? jmin (result, src[i])
  358. : jmax (result, src[i]);
  359. return result;
  360. }
  361. if (num <= 0)
  362. return 0;
  363. return isMinimum ? *std::min_element (src, src + num)
  364. : *std::max_element (src, src + num);
  365. }
  366. template <typename Size>
  367. static Range<Type> findMinAndMax (const Type* src, Size num) noexcept
  368. {
  369. auto numLongOps = num / Mode::numParallel;
  370. if (numLongOps > 1)
  371. {
  372. ParallelType mn, mx;
  373. #if ! JUCE_USE_ARM_NEON
  374. if (isAligned (src))
  375. {
  376. mn = Mode::loadA (src);
  377. mx = mn;
  378. while (--numLongOps > 0)
  379. {
  380. src += Mode::numParallel;
  381. const ParallelType v = Mode::loadA (src);
  382. mn = Mode::min (mn, v);
  383. mx = Mode::max (mx, v);
  384. }
  385. }
  386. else
  387. #endif
  388. {
  389. mn = Mode::loadU (src);
  390. mx = mn;
  391. while (--numLongOps > 0)
  392. {
  393. src += Mode::numParallel;
  394. const ParallelType v = Mode::loadU (src);
  395. mn = Mode::min (mn, v);
  396. mx = Mode::max (mx, v);
  397. }
  398. }
  399. Range<Type> result (Mode::min (mn),
  400. Mode::max (mx));
  401. num &= (Mode::numParallel - 1);
  402. src += Mode::numParallel;
  403. for (auto i = (decltype (num)) 0; i < num; ++i)
  404. result = result.getUnionWith (src[i]);
  405. return result;
  406. }
  407. return Range<Type>::findMinAndMax (src, num);
  408. }
  409. };
  410. #endif
  411. //==============================================================================
  412. namespace
  413. {
  414. template <typename Size>
  415. void clear (float* dest, Size num) noexcept
  416. {
  417. #if JUCE_USE_VDSP_FRAMEWORK
  418. vDSP_vclr (dest, 1, (vDSP_Length) num);
  419. #else
  420. zeromem (dest, (size_t) num * sizeof (float));
  421. #endif
  422. }
  423. template <typename Size>
  424. void clear (double* dest, Size num) noexcept
  425. {
  426. #if JUCE_USE_VDSP_FRAMEWORK
  427. vDSP_vclrD (dest, 1, (vDSP_Length) num);
  428. #else
  429. zeromem (dest, (size_t) num * sizeof (double));
  430. #endif
  431. }
  432. template <typename Size>
  433. void fill (float* dest, float valueToFill, Size num) noexcept
  434. {
  435. #if JUCE_USE_VDSP_FRAMEWORK
  436. vDSP_vfill (&valueToFill, dest, 1, (vDSP_Length) num);
  437. #else
  438. JUCE_PERFORM_VEC_OP_DEST (dest[i] = valueToFill,
  439. val,
  440. JUCE_LOAD_NONE,
  441. const Mode::ParallelType val = Mode::load1 (valueToFill);)
  442. #endif
  443. }
  444. template <typename Size>
  445. void fill (double* dest, double valueToFill, Size num) noexcept
  446. {
  447. #if JUCE_USE_VDSP_FRAMEWORK
  448. vDSP_vfillD (&valueToFill, dest, 1, (vDSP_Length) num);
  449. #else
  450. JUCE_PERFORM_VEC_OP_DEST (dest[i] = valueToFill,
  451. val,
  452. JUCE_LOAD_NONE,
  453. const Mode::ParallelType val = Mode::load1 (valueToFill);)
  454. #endif
  455. }
  456. template <typename Size>
  457. void copyWithMultiply (float* dest, const float* src, float multiplier, Size num) noexcept
  458. {
  459. #if JUCE_USE_VDSP_FRAMEWORK
  460. vDSP_vsmul (src, 1, &multiplier, dest, 1, (vDSP_Length) num);
  461. #else
  462. JUCE_PERFORM_VEC_OP_SRC_DEST (dest[i] = src[i] * multiplier,
  463. Mode::mul (mult, s),
  464. JUCE_LOAD_SRC,
  465. JUCE_INCREMENT_SRC_DEST,
  466. const Mode::ParallelType mult = Mode::load1 (multiplier);)
  467. #endif
  468. }
  469. template <typename Size>
  470. void copyWithMultiply (double* dest, const double* src, double multiplier, Size num) noexcept
  471. {
  472. #if JUCE_USE_VDSP_FRAMEWORK
  473. vDSP_vsmulD (src, 1, &multiplier, dest, 1, (vDSP_Length) num);
  474. #else
  475. JUCE_PERFORM_VEC_OP_SRC_DEST (dest[i] = src[i] * multiplier,
  476. Mode::mul (mult, s),
  477. JUCE_LOAD_SRC,
  478. JUCE_INCREMENT_SRC_DEST,
  479. const Mode::ParallelType mult = Mode::load1 (multiplier);)
  480. #endif
  481. }
  482. template <typename Size>
  483. void add (float* dest, float amount, Size num) noexcept
  484. {
  485. #if JUCE_USE_VDSP_FRAMEWORK
  486. vDSP_vsadd (dest, 1, &amount, dest, 1, (vDSP_Length) num);
  487. #else
  488. JUCE_PERFORM_VEC_OP_DEST (dest[i] += amount,
  489. Mode::add (d, amountToAdd),
  490. JUCE_LOAD_DEST,
  491. const Mode::ParallelType amountToAdd = Mode::load1 (amount);)
  492. #endif
  493. }
  494. template <typename Size>
  495. void add (double* dest, double amount, Size num) noexcept
  496. {
  497. JUCE_PERFORM_VEC_OP_DEST (dest[i] += amount,
  498. Mode::add (d, amountToAdd),
  499. JUCE_LOAD_DEST,
  500. const Mode::ParallelType amountToAdd = Mode::load1 (amount);)
  501. }
  502. template <typename Size>
  503. void add (float* dest, const float* src, float amount, Size num) noexcept
  504. {
  505. #if JUCE_USE_VDSP_FRAMEWORK
  506. vDSP_vsadd (src, 1, &amount, dest, 1, (vDSP_Length) num);
  507. #else
  508. JUCE_PERFORM_VEC_OP_SRC_DEST (dest[i] = src[i] + amount,
  509. Mode::add (am, s),
  510. JUCE_LOAD_SRC,
  511. JUCE_INCREMENT_SRC_DEST,
  512. const Mode::ParallelType am = Mode::load1 (amount);)
  513. #endif
  514. }
  515. template <typename Size>
  516. void add (double* dest, const double* src, double amount, Size num) noexcept
  517. {
  518. #if JUCE_USE_VDSP_FRAMEWORK
  519. vDSP_vsaddD (src, 1, &amount, dest, 1, (vDSP_Length) num);
  520. #else
  521. JUCE_PERFORM_VEC_OP_SRC_DEST (dest[i] = src[i] + amount,
  522. Mode::add (am, s),
  523. JUCE_LOAD_SRC,
  524. JUCE_INCREMENT_SRC_DEST,
  525. const Mode::ParallelType am = Mode::load1 (amount);)
  526. #endif
  527. }
  528. template <typename Size>
  529. void add (float* dest, const float* src, Size num) noexcept
  530. {
  531. #if JUCE_USE_VDSP_FRAMEWORK
  532. vDSP_vadd (src, 1, dest, 1, dest, 1, (vDSP_Length) num);
  533. #else
  534. JUCE_PERFORM_VEC_OP_SRC_DEST (dest[i] += src[i],
  535. Mode::add (d, s),
  536. JUCE_LOAD_SRC_DEST,
  537. JUCE_INCREMENT_SRC_DEST, )
  538. #endif
  539. }
  540. template <typename Size>
  541. void add (double* dest, const double* src, Size num) noexcept
  542. {
  543. #if JUCE_USE_VDSP_FRAMEWORK
  544. vDSP_vaddD (src, 1, dest, 1, dest, 1, (vDSP_Length) num);
  545. #else
  546. JUCE_PERFORM_VEC_OP_SRC_DEST (dest[i] += src[i],
  547. Mode::add (d, s),
  548. JUCE_LOAD_SRC_DEST,
  549. JUCE_INCREMENT_SRC_DEST, )
  550. #endif
  551. }
  552. template <typename Size>
  553. void add (float* dest, const float* src1, const float* src2, Size num) noexcept
  554. {
  555. #if JUCE_USE_VDSP_FRAMEWORK
  556. vDSP_vadd (src1, 1, src2, 1, dest, 1, (vDSP_Length) num);
  557. #else
  558. JUCE_PERFORM_VEC_OP_SRC1_SRC2_DEST (dest[i] = src1[i] + src2[i],
  559. Mode::add (s1, s2),
  560. JUCE_LOAD_SRC1_SRC2,
  561. JUCE_INCREMENT_SRC1_SRC2_DEST, )
  562. #endif
  563. }
  564. template <typename Size>
  565. void add (double* dest, const double* src1, const double* src2, Size num) noexcept
  566. {
  567. #if JUCE_USE_VDSP_FRAMEWORK
  568. vDSP_vaddD (src1, 1, src2, 1, dest, 1, (vDSP_Length) num);
  569. #else
  570. JUCE_PERFORM_VEC_OP_SRC1_SRC2_DEST (dest[i] = src1[i] + src2[i],
  571. Mode::add (s1, s2),
  572. JUCE_LOAD_SRC1_SRC2,
  573. JUCE_INCREMENT_SRC1_SRC2_DEST, )
  574. #endif
  575. }
  576. template <typename Size>
  577. void subtract (float* dest, const float* src, Size num) noexcept
  578. {
  579. #if JUCE_USE_VDSP_FRAMEWORK
  580. vDSP_vsub (src, 1, dest, 1, dest, 1, (vDSP_Length) num);
  581. #else
  582. JUCE_PERFORM_VEC_OP_SRC_DEST (dest[i] -= src[i],
  583. Mode::sub (d, s),
  584. JUCE_LOAD_SRC_DEST,
  585. JUCE_INCREMENT_SRC_DEST, )
  586. #endif
  587. }
  588. template <typename Size>
  589. void subtract (double* dest, const double* src, Size num) noexcept
  590. {
  591. #if JUCE_USE_VDSP_FRAMEWORK
  592. vDSP_vsubD (src, 1, dest, 1, dest, 1, (vDSP_Length) num);
  593. #else
  594. JUCE_PERFORM_VEC_OP_SRC_DEST (dest[i] -= src[i],
  595. Mode::sub (d, s),
  596. JUCE_LOAD_SRC_DEST,
  597. JUCE_INCREMENT_SRC_DEST, )
  598. #endif
  599. }
  600. template <typename Size>
  601. void subtract (float* dest, const float* src1, const float* src2, Size num) noexcept
  602. {
  603. #if JUCE_USE_VDSP_FRAMEWORK
  604. vDSP_vsub (src2, 1, src1, 1, dest, 1, (vDSP_Length) num);
  605. #else
  606. JUCE_PERFORM_VEC_OP_SRC1_SRC2_DEST (dest[i] = src1[i] - src2[i],
  607. Mode::sub (s1, s2),
  608. JUCE_LOAD_SRC1_SRC2,
  609. JUCE_INCREMENT_SRC1_SRC2_DEST, )
  610. #endif
  611. }
  612. template <typename Size>
  613. void subtract (double* dest, const double* src1, const double* src2, Size num) noexcept
  614. {
  615. #if JUCE_USE_VDSP_FRAMEWORK
  616. vDSP_vsubD (src2, 1, src1, 1, dest, 1, (vDSP_Length) num);
  617. #else
  618. JUCE_PERFORM_VEC_OP_SRC1_SRC2_DEST (dest[i] = src1[i] - src2[i],
  619. Mode::sub (s1, s2),
  620. JUCE_LOAD_SRC1_SRC2,
  621. JUCE_INCREMENT_SRC1_SRC2_DEST, )
  622. #endif
  623. }
  624. template <typename Size>
  625. void addWithMultiply (float* dest, const float* src, float multiplier, Size num) noexcept
  626. {
  627. #if JUCE_USE_VDSP_FRAMEWORK
  628. vDSP_vsma (src, 1, &multiplier, dest, 1, dest, 1, (vDSP_Length) num);
  629. #else
  630. JUCE_PERFORM_VEC_OP_SRC_DEST (dest[i] += src[i] * multiplier,
  631. Mode::add (d, Mode::mul (mult, s)),
  632. JUCE_LOAD_SRC_DEST,
  633. JUCE_INCREMENT_SRC_DEST,
  634. const Mode::ParallelType mult = Mode::load1 (multiplier);)
  635. #endif
  636. }
  637. template <typename Size>
  638. void addWithMultiply (double* dest, const double* src, double multiplier, Size num) noexcept
  639. {
  640. #if JUCE_USE_VDSP_FRAMEWORK
  641. vDSP_vsmaD (src, 1, &multiplier, dest, 1, dest, 1, (vDSP_Length) num);
  642. #else
  643. JUCE_PERFORM_VEC_OP_SRC_DEST (dest[i] += src[i] * multiplier,
  644. Mode::add (d, Mode::mul (mult, s)),
  645. JUCE_LOAD_SRC_DEST,
  646. JUCE_INCREMENT_SRC_DEST,
  647. const Mode::ParallelType mult = Mode::load1 (multiplier);)
  648. #endif
  649. }
  650. template <typename Size>
  651. void addWithMultiply (float* dest, const float* src1, const float* src2, Size num) noexcept
  652. {
  653. #if JUCE_USE_VDSP_FRAMEWORK
  654. vDSP_vma ((float*) src1, 1, (float*) src2, 1, dest, 1, dest, 1, (vDSP_Length) num);
  655. #else
  656. JUCE_PERFORM_VEC_OP_SRC1_SRC2_DEST_DEST (dest[i] += src1[i] * src2[i],
  657. Mode::add (d, Mode::mul (s1, s2)),
  658. JUCE_LOAD_SRC1_SRC2_DEST,
  659. JUCE_INCREMENT_SRC1_SRC2_DEST, )
  660. #endif
  661. }
  662. template <typename Size>
  663. void addWithMultiply (double* dest, const double* src1, const double* src2, Size num) noexcept
  664. {
  665. #if JUCE_USE_VDSP_FRAMEWORK
  666. vDSP_vmaD ((double*) src1, 1, (double*) src2, 1, dest, 1, dest, 1, (vDSP_Length) num);
  667. #else
  668. JUCE_PERFORM_VEC_OP_SRC1_SRC2_DEST_DEST (dest[i] += src1[i] * src2[i],
  669. Mode::add (d, Mode::mul (s1, s2)),
  670. JUCE_LOAD_SRC1_SRC2_DEST,
  671. JUCE_INCREMENT_SRC1_SRC2_DEST, )
  672. #endif
  673. }
  674. template <typename Size>
  675. void subtractWithMultiply (float* dest, const float* src, float multiplier, Size num) noexcept
  676. {
  677. JUCE_PERFORM_VEC_OP_SRC_DEST (dest[i] -= src[i] * multiplier,
  678. Mode::sub (d, Mode::mul (mult, s)),
  679. JUCE_LOAD_SRC_DEST,
  680. JUCE_INCREMENT_SRC_DEST,
  681. const Mode::ParallelType mult = Mode::load1 (multiplier);)
  682. }
  683. template <typename Size>
  684. void subtractWithMultiply (double* dest, const double* src, double multiplier, Size num) noexcept
  685. {
  686. JUCE_PERFORM_VEC_OP_SRC_DEST (dest[i] -= src[i] * multiplier,
  687. Mode::sub (d, Mode::mul (mult, s)),
  688. JUCE_LOAD_SRC_DEST,
  689. JUCE_INCREMENT_SRC_DEST,
  690. const Mode::ParallelType mult = Mode::load1 (multiplier);)
  691. }
  692. template <typename Size>
  693. void subtractWithMultiply (float* dest, const float* src1, const float* src2, Size num) noexcept
  694. {
  695. JUCE_PERFORM_VEC_OP_SRC1_SRC2_DEST_DEST (dest[i] -= src1[i] * src2[i],
  696. Mode::sub (d, Mode::mul (s1, s2)),
  697. JUCE_LOAD_SRC1_SRC2_DEST,
  698. JUCE_INCREMENT_SRC1_SRC2_DEST, )
  699. }
  700. template <typename Size>
  701. void subtractWithMultiply (double* dest, const double* src1, const double* src2, Size num) noexcept
  702. {
  703. JUCE_PERFORM_VEC_OP_SRC1_SRC2_DEST_DEST (dest[i] -= src1[i] * src2[i],
  704. Mode::sub (d, Mode::mul (s1, s2)),
  705. JUCE_LOAD_SRC1_SRC2_DEST,
  706. JUCE_INCREMENT_SRC1_SRC2_DEST, )
  707. }
  708. template <typename Size>
  709. void multiply (float* dest, const float* src, Size num) noexcept
  710. {
  711. #if JUCE_USE_VDSP_FRAMEWORK
  712. vDSP_vmul (src, 1, dest, 1, dest, 1, (vDSP_Length) num);
  713. #else
  714. JUCE_PERFORM_VEC_OP_SRC_DEST (dest[i] *= src[i],
  715. Mode::mul (d, s),
  716. JUCE_LOAD_SRC_DEST,
  717. JUCE_INCREMENT_SRC_DEST, )
  718. #endif
  719. }
  720. template <typename Size>
  721. void multiply (double* dest, const double* src, Size num) noexcept
  722. {
  723. #if JUCE_USE_VDSP_FRAMEWORK
  724. vDSP_vmulD (src, 1, dest, 1, dest, 1, (vDSP_Length) num);
  725. #else
  726. JUCE_PERFORM_VEC_OP_SRC_DEST (dest[i] *= src[i],
  727. Mode::mul (d, s),
  728. JUCE_LOAD_SRC_DEST,
  729. JUCE_INCREMENT_SRC_DEST, )
  730. #endif
  731. }
  732. template <typename Size>
  733. void multiply (float* dest, const float* src1, const float* src2, Size num) noexcept
  734. {
  735. #if JUCE_USE_VDSP_FRAMEWORK
  736. vDSP_vmul (src1, 1, src2, 1, dest, 1, (vDSP_Length) num);
  737. #else
  738. JUCE_PERFORM_VEC_OP_SRC1_SRC2_DEST (dest[i] = src1[i] * src2[i],
  739. Mode::mul (s1, s2),
  740. JUCE_LOAD_SRC1_SRC2,
  741. JUCE_INCREMENT_SRC1_SRC2_DEST, )
  742. #endif
  743. }
  744. template <typename Size>
  745. void multiply (double* dest, const double* src1, const double* src2, Size num) noexcept
  746. {
  747. #if JUCE_USE_VDSP_FRAMEWORK
  748. vDSP_vmulD (src1, 1, src2, 1, dest, 1, (vDSP_Length) num);
  749. #else
  750. JUCE_PERFORM_VEC_OP_SRC1_SRC2_DEST (dest[i] = src1[i] * src2[i],
  751. Mode::mul (s1, s2),
  752. JUCE_LOAD_SRC1_SRC2,
  753. JUCE_INCREMENT_SRC1_SRC2_DEST, )
  754. #endif
  755. }
  756. template <typename Size>
  757. void multiply (float* dest, float multiplier, Size num) noexcept
  758. {
  759. #if JUCE_USE_VDSP_FRAMEWORK
  760. vDSP_vsmul (dest, 1, &multiplier, dest, 1, (vDSP_Length) num);
  761. #else
  762. JUCE_PERFORM_VEC_OP_DEST (dest[i] *= multiplier,
  763. Mode::mul (d, mult),
  764. JUCE_LOAD_DEST,
  765. const Mode::ParallelType mult = Mode::load1 (multiplier);)
  766. #endif
  767. }
  768. template <typename Size>
  769. void multiply (double* dest, double multiplier, Size num) noexcept
  770. {
  771. #if JUCE_USE_VDSP_FRAMEWORK
  772. vDSP_vsmulD (dest, 1, &multiplier, dest, 1, (vDSP_Length) num);
  773. #else
  774. JUCE_PERFORM_VEC_OP_DEST (dest[i] *= multiplier,
  775. Mode::mul (d, mult),
  776. JUCE_LOAD_DEST,
  777. const Mode::ParallelType mult = Mode::load1 (multiplier);)
  778. #endif
  779. }
  780. template <typename Size>
  781. void multiply (float* dest, const float* src, float multiplier, Size num) noexcept
  782. {
  783. JUCE_PERFORM_VEC_OP_SRC_DEST (dest[i] = src[i] * multiplier,
  784. Mode::mul (mult, s),
  785. JUCE_LOAD_SRC,
  786. JUCE_INCREMENT_SRC_DEST,
  787. const Mode::ParallelType mult = Mode::load1 (multiplier);)
  788. }
  789. template <typename Size>
  790. void multiply (double* dest, const double* src, double multiplier, Size num) noexcept
  791. {
  792. JUCE_PERFORM_VEC_OP_SRC_DEST (dest[i] = src[i] * multiplier,
  793. Mode::mul (mult, s),
  794. JUCE_LOAD_SRC,
  795. JUCE_INCREMENT_SRC_DEST,
  796. const Mode::ParallelType mult = Mode::load1 (multiplier);)
  797. }
  798. template <typename Size>
  799. void negate (float* dest, const float* src, Size num) noexcept
  800. {
  801. #if JUCE_USE_VDSP_FRAMEWORK
  802. vDSP_vneg ((float*) src, 1, dest, 1, (vDSP_Length) num);
  803. #else
  804. copyWithMultiply (dest, src, -1.0f, num);
  805. #endif
  806. }
  807. template <typename Size>
  808. void negate (double* dest, const double* src, Size num) noexcept
  809. {
  810. #if JUCE_USE_VDSP_FRAMEWORK
  811. vDSP_vnegD ((double*) src, 1, dest, 1, (vDSP_Length) num);
  812. #else
  813. copyWithMultiply (dest, src, -1.0f, num);
  814. #endif
  815. }
  816. template <typename Size>
  817. void abs (float* dest, const float* src, Size num) noexcept
  818. {
  819. #if JUCE_USE_VDSP_FRAMEWORK
  820. vDSP_vabs ((float*) src, 1, dest, 1, (vDSP_Length) num);
  821. #else
  822. FloatVectorHelpers::signMask32 signMask;
  823. signMask.i = 0x7fffffffUL;
  824. JUCE_PERFORM_VEC_OP_SRC_DEST (dest[i] = std::abs (src[i]),
  825. Mode::bit_and (s, mask),
  826. JUCE_LOAD_SRC,
  827. JUCE_INCREMENT_SRC_DEST,
  828. const Mode::ParallelType mask = Mode::load1 (signMask.f);)
  829. ignoreUnused (signMask);
  830. #endif
  831. }
  832. template <typename Size>
  833. void abs (double* dest, const double* src, Size num) noexcept
  834. {
  835. #if JUCE_USE_VDSP_FRAMEWORK
  836. vDSP_vabsD ((double*) src, 1, dest, 1, (vDSP_Length) num);
  837. #else
  838. FloatVectorHelpers::signMask64 signMask;
  839. signMask.i = 0x7fffffffffffffffULL;
  840. JUCE_PERFORM_VEC_OP_SRC_DEST (dest[i] = std::abs (src[i]),
  841. Mode::bit_and (s, mask),
  842. JUCE_LOAD_SRC,
  843. JUCE_INCREMENT_SRC_DEST,
  844. const Mode::ParallelType mask = Mode::load1 (signMask.d);)
  845. ignoreUnused (signMask);
  846. #endif
  847. }
  848. template <typename Size>
  849. void min (float* dest, const float* src, float comp, Size num) noexcept
  850. {
  851. JUCE_PERFORM_VEC_OP_SRC_DEST (dest[i] = jmin (src[i], comp),
  852. Mode::min (s, cmp),
  853. JUCE_LOAD_SRC,
  854. JUCE_INCREMENT_SRC_DEST,
  855. const Mode::ParallelType cmp = Mode::load1 (comp);)
  856. }
  857. template <typename Size>
  858. void min (double* dest, const double* src, double comp, Size num) noexcept
  859. {
  860. JUCE_PERFORM_VEC_OP_SRC_DEST (dest[i] = jmin (src[i], comp),
  861. Mode::min (s, cmp),
  862. JUCE_LOAD_SRC,
  863. JUCE_INCREMENT_SRC_DEST,
  864. const Mode::ParallelType cmp = Mode::load1 (comp);)
  865. }
  866. template <typename Size>
  867. void min (float* dest, const float* src1, const float* src2, Size num) noexcept
  868. {
  869. #if JUCE_USE_VDSP_FRAMEWORK
  870. vDSP_vmin ((float*) src1, 1, (float*) src2, 1, dest, 1, (vDSP_Length) num);
  871. #else
  872. JUCE_PERFORM_VEC_OP_SRC1_SRC2_DEST (dest[i] = jmin (src1[i], src2[i]),
  873. Mode::min (s1, s2),
  874. JUCE_LOAD_SRC1_SRC2,
  875. JUCE_INCREMENT_SRC1_SRC2_DEST, )
  876. #endif
  877. }
  878. template <typename Size>
  879. void min (double* dest, const double* src1, const double* src2, Size num) noexcept
  880. {
  881. #if JUCE_USE_VDSP_FRAMEWORK
  882. vDSP_vminD ((double*) src1, 1, (double*) src2, 1, dest, 1, (vDSP_Length) num);
  883. #else
  884. JUCE_PERFORM_VEC_OP_SRC1_SRC2_DEST (dest[i] = jmin (src1[i], src2[i]),
  885. Mode::min (s1, s2),
  886. JUCE_LOAD_SRC1_SRC2,
  887. JUCE_INCREMENT_SRC1_SRC2_DEST, )
  888. #endif
  889. }
  890. template <typename Size>
  891. void max (float* dest, const float* src, float comp, Size num) noexcept
  892. {
  893. JUCE_PERFORM_VEC_OP_SRC_DEST (dest[i] = jmax (src[i], comp),
  894. Mode::max (s, cmp),
  895. JUCE_LOAD_SRC,
  896. JUCE_INCREMENT_SRC_DEST,
  897. const Mode::ParallelType cmp = Mode::load1 (comp);)
  898. }
  899. template <typename Size>
  900. void max (double* dest, const double* src, double comp, Size num) noexcept
  901. {
  902. JUCE_PERFORM_VEC_OP_SRC_DEST (dest[i] = jmax (src[i], comp),
  903. Mode::max (s, cmp),
  904. JUCE_LOAD_SRC,
  905. JUCE_INCREMENT_SRC_DEST,
  906. const Mode::ParallelType cmp = Mode::load1 (comp);)
  907. }
  908. template <typename Size>
  909. void max (float* dest, const float* src1, const float* src2, Size num) noexcept
  910. {
  911. #if JUCE_USE_VDSP_FRAMEWORK
  912. vDSP_vmax ((float*) src1, 1, (float*) src2, 1, dest, 1, (vDSP_Length) num);
  913. #else
  914. JUCE_PERFORM_VEC_OP_SRC1_SRC2_DEST (dest[i] = jmax (src1[i], src2[i]),
  915. Mode::max (s1, s2),
  916. JUCE_LOAD_SRC1_SRC2,
  917. JUCE_INCREMENT_SRC1_SRC2_DEST, )
  918. #endif
  919. }
  920. template <typename Size>
  921. void max (double* dest, const double* src1, const double* src2, Size num) noexcept
  922. {
  923. #if JUCE_USE_VDSP_FRAMEWORK
  924. vDSP_vmaxD ((double*) src1, 1, (double*) src2, 1, dest, 1, (vDSP_Length) num);
  925. #else
  926. JUCE_PERFORM_VEC_OP_SRC1_SRC2_DEST (dest[i] = jmax (src1[i], src2[i]),
  927. Mode::max (s1, s2),
  928. JUCE_LOAD_SRC1_SRC2,
  929. JUCE_INCREMENT_SRC1_SRC2_DEST, )
  930. #endif
  931. }
  932. template <typename Size>
  933. void clip (float* dest, const float* src, float low, float high, Size num) noexcept
  934. {
  935. jassert (high >= low);
  936. #if JUCE_USE_VDSP_FRAMEWORK
  937. vDSP_vclip ((float*) src, 1, &low, &high, dest, 1, (vDSP_Length) num);
  938. #else
  939. JUCE_PERFORM_VEC_OP_SRC_DEST (dest[i] = jmax (jmin (src[i], high), low),
  940. Mode::max (Mode::min (s, hi), lo),
  941. JUCE_LOAD_SRC,
  942. JUCE_INCREMENT_SRC_DEST,
  943. const Mode::ParallelType lo = Mode::load1 (low);
  944. const Mode::ParallelType hi = Mode::load1 (high);)
  945. #endif
  946. }
  947. template <typename Size>
  948. void clip (double* dest, const double* src, double low, double high, Size num) noexcept
  949. {
  950. jassert (high >= low);
  951. #if JUCE_USE_VDSP_FRAMEWORK
  952. vDSP_vclipD ((double*) src, 1, &low, &high, dest, 1, (vDSP_Length) num);
  953. #else
  954. JUCE_PERFORM_VEC_OP_SRC_DEST (dest[i] = jmax (jmin (src[i], high), low),
  955. Mode::max (Mode::min (s, hi), lo),
  956. JUCE_LOAD_SRC,
  957. JUCE_INCREMENT_SRC_DEST,
  958. const Mode::ParallelType lo = Mode::load1 (low);
  959. const Mode::ParallelType hi = Mode::load1 (high);)
  960. #endif
  961. }
  962. template <typename Size>
  963. Range<float> findMinAndMax (const float* src, Size num) noexcept
  964. {
  965. #if JUCE_USE_SSE_INTRINSICS || JUCE_USE_ARM_NEON
  966. return FloatVectorHelpers::MinMax<FloatVectorHelpers::BasicOps32>::findMinAndMax (src, num);
  967. #else
  968. return Range<float>::findMinAndMax (src, num);
  969. #endif
  970. }
  971. template <typename Size>
  972. Range<double> findMinAndMax (const double* src, Size num) noexcept
  973. {
  974. #if JUCE_USE_SSE_INTRINSICS || JUCE_USE_ARM_NEON
  975. return FloatVectorHelpers::MinMax<FloatVectorHelpers::BasicOps64>::findMinAndMax (src, num);
  976. #else
  977. return Range<double>::findMinAndMax (src, num);
  978. #endif
  979. }
  980. template <typename Size>
  981. float findMinimum (const float* src, Size num) noexcept
  982. {
  983. #if JUCE_USE_SSE_INTRINSICS || JUCE_USE_ARM_NEON
  984. return FloatVectorHelpers::MinMax<FloatVectorHelpers::BasicOps32>::findMinOrMax (src, num, true);
  985. #else
  986. return juce::findMinimum (src, num);
  987. #endif
  988. }
  989. template <typename Size>
  990. double findMinimum (const double* src, Size num) noexcept
  991. {
  992. #if JUCE_USE_SSE_INTRINSICS || JUCE_USE_ARM_NEON
  993. return FloatVectorHelpers::MinMax<FloatVectorHelpers::BasicOps64>::findMinOrMax (src, num, true);
  994. #else
  995. return juce::findMinimum (src, num);
  996. #endif
  997. }
  998. template <typename Size>
  999. float findMaximum (const float* src, Size num) noexcept
  1000. {
  1001. #if JUCE_USE_SSE_INTRINSICS || JUCE_USE_ARM_NEON
  1002. return FloatVectorHelpers::MinMax<FloatVectorHelpers::BasicOps32>::findMinOrMax (src, num, false);
  1003. #else
  1004. return juce::findMaximum (src, num);
  1005. #endif
  1006. }
  1007. template <typename Size>
  1008. double findMaximum (const double* src, Size num) noexcept
  1009. {
  1010. #if JUCE_USE_SSE_INTRINSICS || JUCE_USE_ARM_NEON
  1011. return FloatVectorHelpers::MinMax<FloatVectorHelpers::BasicOps64>::findMinOrMax (src, num, false);
  1012. #else
  1013. return juce::findMaximum (src, num);
  1014. #endif
  1015. }
  1016. template <typename Size>
  1017. void convertFixedToFloat (float* dest, const int* src, float multiplier, Size num) noexcept
  1018. {
  1019. #if JUCE_USE_ARM_NEON
  1020. JUCE_PERFORM_VEC_OP_SRC_DEST (dest[i] = (float) src[i] * multiplier,
  1021. vmulq_n_f32 (vcvtq_f32_s32 (vld1q_s32 (src)), multiplier),
  1022. JUCE_LOAD_NONE,
  1023. JUCE_INCREMENT_SRC_DEST, )
  1024. #else
  1025. JUCE_PERFORM_VEC_OP_SRC_DEST (dest[i] = (float) src[i] * multiplier,
  1026. Mode::mul (mult, _mm_cvtepi32_ps (_mm_loadu_si128 (reinterpret_cast<const __m128i*> (src)))),
  1027. JUCE_LOAD_NONE,
  1028. JUCE_INCREMENT_SRC_DEST,
  1029. const Mode::ParallelType mult = Mode::load1 (multiplier);)
  1030. #endif
  1031. }
  1032. } // namespace
  1033. } // namespace FloatVectorHelpers
  1034. //==============================================================================
  1035. template <typename FloatType, typename CountType>
  1036. void JUCE_CALLTYPE FloatVectorOperationsBase<FloatType, CountType>::clear (FloatType* dest,
  1037. CountType numValues) noexcept
  1038. {
  1039. FloatVectorHelpers::clear (dest, numValues);
  1040. }
  1041. template <typename FloatType, typename CountType>
  1042. void JUCE_CALLTYPE FloatVectorOperationsBase<FloatType, CountType>::fill (FloatType* dest,
  1043. FloatType valueToFill,
  1044. CountType numValues) noexcept
  1045. {
  1046. FloatVectorHelpers::fill (dest, valueToFill, numValues);
  1047. }
  1048. template <typename FloatType, typename CountType>
  1049. void JUCE_CALLTYPE FloatVectorOperationsBase<FloatType, CountType>::copy (FloatType* dest,
  1050. const FloatType* src,
  1051. CountType numValues) noexcept
  1052. {
  1053. memcpy (dest, src, (size_t) numValues * sizeof (FloatType));
  1054. }
  1055. template <typename FloatType, typename CountType>
  1056. void JUCE_CALLTYPE FloatVectorOperationsBase<FloatType, CountType>::copyWithMultiply (FloatType* dest,
  1057. const FloatType* src,
  1058. FloatType multiplier,
  1059. CountType numValues) noexcept
  1060. {
  1061. FloatVectorHelpers::copyWithMultiply (dest, src, multiplier, numValues);
  1062. }
  1063. template <typename FloatType, typename CountType>
  1064. void JUCE_CALLTYPE FloatVectorOperationsBase<FloatType, CountType>::add (FloatType* dest,
  1065. FloatType amountToAdd,
  1066. CountType numValues) noexcept
  1067. {
  1068. FloatVectorHelpers::add (dest, amountToAdd, numValues);
  1069. }
  1070. template <typename FloatType, typename CountType>
  1071. void JUCE_CALLTYPE FloatVectorOperationsBase<FloatType, CountType>::add (FloatType* dest,
  1072. const FloatType* src,
  1073. FloatType amount,
  1074. CountType numValues) noexcept
  1075. {
  1076. FloatVectorHelpers::add (dest, src, amount, numValues);
  1077. }
  1078. template <typename FloatType, typename CountType>
  1079. void JUCE_CALLTYPE FloatVectorOperationsBase<FloatType, CountType>::add (FloatType* dest,
  1080. const FloatType* src,
  1081. CountType numValues) noexcept
  1082. {
  1083. FloatVectorHelpers::add (dest, src, numValues);
  1084. }
  1085. template <typename FloatType, typename CountType>
  1086. void JUCE_CALLTYPE FloatVectorOperationsBase<FloatType, CountType>::add (FloatType* dest,
  1087. const FloatType* src1,
  1088. const FloatType* src2,
  1089. CountType num) noexcept
  1090. {
  1091. FloatVectorHelpers::add (dest, src1, src2, num);
  1092. }
  1093. template <typename FloatType, typename CountType>
  1094. void JUCE_CALLTYPE FloatVectorOperationsBase<FloatType, CountType>::subtract (FloatType* dest,
  1095. const FloatType* src,
  1096. CountType numValues) noexcept
  1097. {
  1098. FloatVectorHelpers::subtract (dest, src, numValues);
  1099. }
  1100. template <typename FloatType, typename CountType>
  1101. void JUCE_CALLTYPE FloatVectorOperationsBase<FloatType, CountType>::subtract (FloatType* dest,
  1102. const FloatType* src1,
  1103. const FloatType* src2,
  1104. CountType num) noexcept
  1105. {
  1106. FloatVectorHelpers::subtract (dest, src1, src2, num);
  1107. }
  1108. template <typename FloatType, typename CountType>
  1109. void JUCE_CALLTYPE FloatVectorOperationsBase<FloatType, CountType>::addWithMultiply (FloatType* dest,
  1110. const FloatType* src,
  1111. FloatType multiplier,
  1112. CountType numValues) noexcept
  1113. {
  1114. FloatVectorHelpers::addWithMultiply (dest, src, multiplier, numValues);
  1115. }
  1116. template <typename FloatType, typename CountType>
  1117. void JUCE_CALLTYPE FloatVectorOperationsBase<FloatType, CountType>::addWithMultiply (FloatType* dest,
  1118. const FloatType* src1,
  1119. const FloatType* src2,
  1120. CountType num) noexcept
  1121. {
  1122. FloatVectorHelpers::addWithMultiply (dest, src1, src2, num);
  1123. }
  1124. template <typename FloatType, typename CountType>
  1125. void JUCE_CALLTYPE FloatVectorOperationsBase<FloatType, CountType>::subtractWithMultiply (FloatType* dest,
  1126. const FloatType* src,
  1127. FloatType multiplier,
  1128. CountType numValues) noexcept
  1129. {
  1130. FloatVectorHelpers::subtractWithMultiply (dest, src, multiplier, numValues);
  1131. }
  1132. template <typename FloatType, typename CountType>
  1133. void JUCE_CALLTYPE FloatVectorOperationsBase<FloatType, CountType>::subtractWithMultiply (FloatType* dest,
  1134. const FloatType* src1,
  1135. const FloatType* src2,
  1136. CountType num) noexcept
  1137. {
  1138. FloatVectorHelpers::subtractWithMultiply (dest, src1, src2, num);
  1139. }
  1140. template <typename FloatType, typename CountType>
  1141. void JUCE_CALLTYPE FloatVectorOperationsBase<FloatType, CountType>::multiply (FloatType* dest,
  1142. const FloatType* src,
  1143. CountType numValues) noexcept
  1144. {
  1145. FloatVectorHelpers::multiply (dest, src, numValues);
  1146. }
  1147. template <typename FloatType, typename CountType>
  1148. void JUCE_CALLTYPE FloatVectorOperationsBase<FloatType, CountType>::multiply (FloatType* dest,
  1149. const FloatType* src1,
  1150. const FloatType* src2,
  1151. CountType numValues) noexcept
  1152. {
  1153. FloatVectorHelpers::multiply (dest, src1, src2, numValues);
  1154. }
  1155. template <typename FloatType, typename CountType>
  1156. void JUCE_CALLTYPE FloatVectorOperationsBase<FloatType, CountType>::multiply (FloatType* dest,
  1157. FloatType multiplier,
  1158. CountType numValues) noexcept
  1159. {
  1160. FloatVectorHelpers::multiply (dest, multiplier, numValues);
  1161. }
  1162. template <typename FloatType, typename CountType>
  1163. void JUCE_CALLTYPE FloatVectorOperationsBase<FloatType, CountType>::multiply (FloatType* dest,
  1164. const FloatType* src,
  1165. FloatType multiplier,
  1166. CountType num) noexcept
  1167. {
  1168. FloatVectorHelpers::multiply (dest, src, multiplier, num);
  1169. }
  1170. template <typename FloatType, typename CountType>
  1171. void JUCE_CALLTYPE FloatVectorOperationsBase<FloatType, CountType>::negate (FloatType* dest,
  1172. const FloatType* src,
  1173. CountType numValues) noexcept
  1174. {
  1175. FloatVectorHelpers::negate (dest, src, numValues);
  1176. }
  1177. template <typename FloatType, typename CountType>
  1178. void JUCE_CALLTYPE FloatVectorOperationsBase<FloatType, CountType>::abs (FloatType* dest,
  1179. const FloatType* src,
  1180. CountType numValues) noexcept
  1181. {
  1182. FloatVectorHelpers::abs (dest, src, numValues);
  1183. }
  1184. template <typename FloatType, typename CountType>
  1185. void JUCE_CALLTYPE FloatVectorOperationsBase<FloatType, CountType>::min (FloatType* dest,
  1186. const FloatType* src,
  1187. FloatType comp,
  1188. CountType num) noexcept
  1189. {
  1190. FloatVectorHelpers::min (dest, src, comp, num);
  1191. }
  1192. template <typename FloatType, typename CountType>
  1193. void JUCE_CALLTYPE FloatVectorOperationsBase<FloatType, CountType>::min (FloatType* dest,
  1194. const FloatType* src1,
  1195. const FloatType* src2,
  1196. CountType num) noexcept
  1197. {
  1198. FloatVectorHelpers::min (dest, src1, src2, num);
  1199. }
  1200. template <typename FloatType, typename CountType>
  1201. void JUCE_CALLTYPE FloatVectorOperationsBase<FloatType, CountType>::max (FloatType* dest,
  1202. const FloatType* src,
  1203. FloatType comp,
  1204. CountType num) noexcept
  1205. {
  1206. FloatVectorHelpers::max (dest, src, comp, num);
  1207. }
  1208. template <typename FloatType, typename CountType>
  1209. void JUCE_CALLTYPE FloatVectorOperationsBase<FloatType, CountType>::max (FloatType* dest,
  1210. const FloatType* src1,
  1211. const FloatType* src2,
  1212. CountType num) noexcept
  1213. {
  1214. FloatVectorHelpers::max (dest, src1, src2, num);
  1215. }
  1216. template <typename FloatType, typename CountType>
  1217. void JUCE_CALLTYPE FloatVectorOperationsBase<FloatType, CountType>::clip (FloatType* dest,
  1218. const FloatType* src,
  1219. FloatType low,
  1220. FloatType high,
  1221. CountType num) noexcept
  1222. {
  1223. FloatVectorHelpers::clip (dest, src, low, high, num);
  1224. }
  1225. template <typename FloatType, typename CountType>
  1226. Range<FloatType> JUCE_CALLTYPE FloatVectorOperationsBase<FloatType, CountType>::findMinAndMax (const FloatType* src,
  1227. CountType numValues) noexcept
  1228. {
  1229. return FloatVectorHelpers::findMinAndMax (src, numValues);
  1230. }
  1231. template <typename FloatType, typename CountType>
  1232. FloatType JUCE_CALLTYPE FloatVectorOperationsBase<FloatType, CountType>::findMinimum (const FloatType* src,
  1233. CountType numValues) noexcept
  1234. {
  1235. return FloatVectorHelpers::findMinimum (src, numValues);
  1236. }
  1237. template <typename FloatType, typename CountType>
  1238. FloatType JUCE_CALLTYPE FloatVectorOperationsBase<FloatType, CountType>::findMaximum (const FloatType* src,
  1239. CountType numValues) noexcept
  1240. {
  1241. return FloatVectorHelpers::findMaximum (src, numValues);
  1242. }
  1243. template struct FloatVectorOperationsBase<float, int>;
  1244. template struct FloatVectorOperationsBase<float, size_t>;
  1245. template struct FloatVectorOperationsBase<double, int>;
  1246. template struct FloatVectorOperationsBase<double, size_t>;
  1247. void JUCE_CALLTYPE FloatVectorOperations::convertFixedToFloat (float* dest, const int* src, float multiplier, size_t num) noexcept
  1248. {
  1249. FloatVectorHelpers::convertFixedToFloat (dest, src, multiplier, num);
  1250. }
  1251. void JUCE_CALLTYPE FloatVectorOperations::convertFixedToFloat (float* dest, const int* src, float multiplier, int num) noexcept
  1252. {
  1253. FloatVectorHelpers::convertFixedToFloat (dest, src, multiplier, num);
  1254. }
  1255. intptr_t JUCE_CALLTYPE FloatVectorOperations::getFpStatusRegister() noexcept
  1256. {
  1257. intptr_t fpsr = 0;
  1258. #if JUCE_INTEL && JUCE_USE_SSE_INTRINSICS
  1259. fpsr = static_cast<intptr_t> (_mm_getcsr());
  1260. #elif defined(__arm64__) || defined(__aarch64__) || JUCE_USE_ARM_NEON
  1261. #if defined(__arm64__) || defined(__aarch64__)
  1262. asm volatile("mrs %0, fpcr"
  1263. : "=r"(fpsr));
  1264. #elif JUCE_USE_ARM_NEON
  1265. asm volatile("vmrs %0, fpscr"
  1266. : "=r"(fpsr));
  1267. #endif
  1268. #else
  1269. #if ! (defined(JUCE_INTEL) || defined(JUCE_ARM))
  1270. jassertfalse; // No support for getting the floating point status register for your platform
  1271. #endif
  1272. #endif
  1273. return fpsr;
  1274. }
  1275. void JUCE_CALLTYPE FloatVectorOperations::setFpStatusRegister (intptr_t fpsr) noexcept
  1276. {
  1277. #if JUCE_INTEL && JUCE_USE_SSE_INTRINSICS
  1278. // the volatile keyword here is needed to workaround a bug in AppleClang 13.0
  1279. // which aggressively optimises away the variable otherwise
  1280. volatile auto fpsr_w = static_cast<uint32_t> (fpsr);
  1281. _mm_setcsr (fpsr_w);
  1282. #elif defined(__arm64__) || defined(__aarch64__) || JUCE_USE_ARM_NEON
  1283. #if defined(__arm64__) || defined(__aarch64__)
  1284. asm volatile("msr fpcr, %0"
  1285. :
  1286. : "ri"(fpsr));
  1287. #elif JUCE_USE_ARM_NEON
  1288. asm volatile("vmsr fpscr, %0"
  1289. :
  1290. : "ri"(fpsr));
  1291. #endif
  1292. #else
  1293. #if ! (defined(JUCE_INTEL) || defined(JUCE_ARM))
  1294. jassertfalse; // No support for getting the floating point status register for your platform
  1295. #endif
  1296. ignoreUnused (fpsr);
  1297. #endif
  1298. }
  1299. void JUCE_CALLTYPE FloatVectorOperations::enableFlushToZeroMode (bool shouldEnable) noexcept
  1300. {
  1301. #if JUCE_USE_SSE_INTRINSICS || (JUCE_USE_ARM_NEON || defined(__arm64__) || defined(__aarch64__))
  1302. #if JUCE_USE_SSE_INTRINSICS
  1303. intptr_t mask = _MM_FLUSH_ZERO_MASK;
  1304. #else /*JUCE_USE_ARM_NEON*/
  1305. intptr_t mask = (1 << 24 /* FZ */);
  1306. #endif
  1307. setFpStatusRegister ((getFpStatusRegister() & (~mask)) | (shouldEnable ? mask : 0));
  1308. #else
  1309. #if ! (defined(JUCE_INTEL) || defined(JUCE_ARM))
  1310. jassertfalse; // No support for flush to zero mode on your platform
  1311. #endif
  1312. ignoreUnused (shouldEnable);
  1313. #endif
  1314. }
  1315. void JUCE_CALLTYPE FloatVectorOperations::disableDenormalisedNumberSupport (bool shouldDisable) noexcept
  1316. {
  1317. #if JUCE_USE_SSE_INTRINSICS || (JUCE_USE_ARM_NEON || defined(__arm64__) || defined(__aarch64__))
  1318. #if JUCE_USE_SSE_INTRINSICS
  1319. intptr_t mask = 0x8040;
  1320. #else /*JUCE_USE_ARM_NEON*/
  1321. intptr_t mask = (1 << 24 /* FZ */);
  1322. #endif
  1323. setFpStatusRegister ((getFpStatusRegister() & (~mask)) | (shouldDisable ? mask : 0));
  1324. #else
  1325. ignoreUnused (shouldDisable);
  1326. #if ! (defined(JUCE_INTEL) || defined(JUCE_ARM))
  1327. jassertfalse; // No support for disable denormals mode on your platform
  1328. #endif
  1329. #endif
  1330. }
  1331. bool JUCE_CALLTYPE FloatVectorOperations::areDenormalsDisabled() noexcept
  1332. {
  1333. #if JUCE_USE_SSE_INTRINSICS || (JUCE_USE_ARM_NEON || defined(__arm64__) || defined(__aarch64__))
  1334. #if JUCE_USE_SSE_INTRINSICS
  1335. intptr_t mask = 0x8040;
  1336. #else /*JUCE_USE_ARM_NEON*/
  1337. intptr_t mask = (1 << 24 /* FZ */);
  1338. #endif
  1339. return ((getFpStatusRegister() & mask) == mask);
  1340. #else
  1341. return false;
  1342. #endif
  1343. }
  1344. ScopedNoDenormals::ScopedNoDenormals() noexcept
  1345. {
  1346. #if JUCE_USE_SSE_INTRINSICS || (JUCE_USE_ARM_NEON || defined(__arm64__) || defined(__aarch64__))
  1347. #if JUCE_USE_SSE_INTRINSICS
  1348. intptr_t mask = 0x8040;
  1349. #else /*JUCE_USE_ARM_NEON*/
  1350. intptr_t mask = (1 << 24 /* FZ */);
  1351. #endif
  1352. fpsr = FloatVectorOperations::getFpStatusRegister();
  1353. FloatVectorOperations::setFpStatusRegister (fpsr | mask);
  1354. #endif
  1355. }
  1356. ScopedNoDenormals::~ScopedNoDenormals() noexcept
  1357. {
  1358. #if JUCE_USE_SSE_INTRINSICS || (JUCE_USE_ARM_NEON || defined(__arm64__) || defined(__aarch64__))
  1359. FloatVectorOperations::setFpStatusRegister (fpsr);
  1360. #endif
  1361. }
  1362. //==============================================================================
  1363. //==============================================================================
  1364. #if JUCE_UNIT_TESTS
  1365. class FloatVectorOperationsTests : public UnitTest
  1366. {
  1367. public:
  1368. FloatVectorOperationsTests()
  1369. : UnitTest ("FloatVectorOperations", UnitTestCategories::audio)
  1370. {}
  1371. template <typename ValueType>
  1372. struct TestRunner
  1373. {
  1374. static void runTest (UnitTest& u, Random random)
  1375. {
  1376. const int range = random.nextBool() ? 500 : 10;
  1377. const int num = random.nextInt (range) + 1;
  1378. HeapBlock<ValueType> buffer1 (num + 16), buffer2 (num + 16);
  1379. HeapBlock<int> buffer3 (num + 16, true);
  1380. #if JUCE_ARM
  1381. ValueType* const data1 = buffer1;
  1382. ValueType* const data2 = buffer2;
  1383. int* const int1 = buffer3;
  1384. #else
  1385. // These tests deliberately operate on misaligned memory and will be flagged up by
  1386. // checks for undefined behavior!
  1387. ValueType* const data1 = addBytesToPointer (buffer1.get(), random.nextInt (16));
  1388. ValueType* const data2 = addBytesToPointer (buffer2.get(), random.nextInt (16));
  1389. int* const int1 = addBytesToPointer (buffer3.get(), random.nextInt (16));
  1390. #endif
  1391. fillRandomly (random, data1, num);
  1392. fillRandomly (random, data2, num);
  1393. Range<ValueType> minMax1 (FloatVectorOperations::findMinAndMax (data1, num));
  1394. Range<ValueType> minMax2 (Range<ValueType>::findMinAndMax (data1, num));
  1395. u.expect (minMax1 == minMax2);
  1396. u.expect (valuesMatch (FloatVectorOperations::findMinimum (data1, num), juce::findMinimum (data1, num)));
  1397. u.expect (valuesMatch (FloatVectorOperations::findMaximum (data1, num), juce::findMaximum (data1, num)));
  1398. u.expect (valuesMatch (FloatVectorOperations::findMinimum (data2, num), juce::findMinimum (data2, num)));
  1399. u.expect (valuesMatch (FloatVectorOperations::findMaximum (data2, num), juce::findMaximum (data2, num)));
  1400. FloatVectorOperations::clear (data1, num);
  1401. u.expect (areAllValuesEqual (data1, num, 0));
  1402. FloatVectorOperations::fill (data1, (ValueType) 2, num);
  1403. u.expect (areAllValuesEqual (data1, num, (ValueType) 2));
  1404. FloatVectorOperations::add (data1, (ValueType) 2, num);
  1405. u.expect (areAllValuesEqual (data1, num, (ValueType) 4));
  1406. FloatVectorOperations::copy (data2, data1, num);
  1407. u.expect (areAllValuesEqual (data2, num, (ValueType) 4));
  1408. FloatVectorOperations::add (data2, data1, num);
  1409. u.expect (areAllValuesEqual (data2, num, (ValueType) 8));
  1410. FloatVectorOperations::copyWithMultiply (data2, data1, (ValueType) 4, num);
  1411. u.expect (areAllValuesEqual (data2, num, (ValueType) 16));
  1412. FloatVectorOperations::addWithMultiply (data2, data1, (ValueType) 4, num);
  1413. u.expect (areAllValuesEqual (data2, num, (ValueType) 32));
  1414. FloatVectorOperations::multiply (data1, (ValueType) 2, num);
  1415. u.expect (areAllValuesEqual (data1, num, (ValueType) 8));
  1416. FloatVectorOperations::multiply (data1, data2, num);
  1417. u.expect (areAllValuesEqual (data1, num, (ValueType) 256));
  1418. FloatVectorOperations::negate (data2, data1, num);
  1419. u.expect (areAllValuesEqual (data2, num, (ValueType) -256));
  1420. FloatVectorOperations::subtract (data1, data2, num);
  1421. u.expect (areAllValuesEqual (data1, num, (ValueType) 512));
  1422. FloatVectorOperations::abs (data1, data2, num);
  1423. u.expect (areAllValuesEqual (data1, num, (ValueType) 256));
  1424. FloatVectorOperations::abs (data2, data1, num);
  1425. u.expect (areAllValuesEqual (data2, num, (ValueType) 256));
  1426. fillRandomly (random, int1, num);
  1427. doConversionTest (u, data1, data2, int1, num);
  1428. FloatVectorOperations::fill (data1, (ValueType) 2, num);
  1429. FloatVectorOperations::fill (data2, (ValueType) 3, num);
  1430. FloatVectorOperations::addWithMultiply (data1, data1, data2, num);
  1431. u.expect (areAllValuesEqual (data1, num, (ValueType) 8));
  1432. }
  1433. static void doConversionTest (UnitTest& u, float* data1, float* data2, int* const int1, int num)
  1434. {
  1435. FloatVectorOperations::convertFixedToFloat (data1, int1, 2.0f, num);
  1436. convertFixed (data2, int1, 2.0f, num);
  1437. u.expect (buffersMatch (data1, data2, num));
  1438. }
  1439. static void doConversionTest (UnitTest&, double*, double*, int*, int) {}
  1440. static void fillRandomly (Random& random, ValueType* d, int num)
  1441. {
  1442. while (--num >= 0)
  1443. *d++ = (ValueType) (random.nextDouble() * 1000.0);
  1444. }
  1445. static void fillRandomly (Random& random, int* d, int num)
  1446. {
  1447. while (--num >= 0)
  1448. *d++ = random.nextInt();
  1449. }
  1450. static void convertFixed (float* d, const int* s, ValueType multiplier, int num)
  1451. {
  1452. while (--num >= 0)
  1453. *d++ = (float) *s++ * multiplier;
  1454. }
  1455. static bool areAllValuesEqual (const ValueType* d, int num, ValueType target)
  1456. {
  1457. while (--num >= 0)
  1458. if (*d++ != target)
  1459. return false;
  1460. return true;
  1461. }
  1462. static bool buffersMatch (const ValueType* d1, const ValueType* d2, int num)
  1463. {
  1464. while (--num >= 0)
  1465. if (! valuesMatch (*d1++, *d2++))
  1466. return false;
  1467. return true;
  1468. }
  1469. static bool valuesMatch (ValueType v1, ValueType v2)
  1470. {
  1471. return std::abs (v1 - v2) < std::numeric_limits<ValueType>::epsilon();
  1472. }
  1473. };
  1474. void runTest() override
  1475. {
  1476. beginTest ("FloatVectorOperations");
  1477. for (int i = 1000; --i >= 0;)
  1478. {
  1479. TestRunner<float>::runTest (*this, getRandom());
  1480. TestRunner<double>::runTest (*this, getRandom());
  1481. }
  1482. }
  1483. };
  1484. static FloatVectorOperationsTests vectorOpTests;
  1485. #endif
  1486. } // namespace juce