AkSimd.h 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751
  1. /*******************************************************************************
  2. The content of this file includes portions of the AUDIOKINETIC Wwise Technology
  3. released in source code form as part of the SDK installer package.
  4. Commercial License Usage
  5. Licensees holding valid commercial licenses to the AUDIOKINETIC Wwise Technology
  6. may use this file in accordance with the end user license agreement provided
  7. with the software or, alternatively, in accordance with the terms contained in a
  8. written agreement between you and Audiokinetic Inc.
  9. Apache License Usage
  10. Alternatively, this file may be used under the Apache License, Version 2.0 (the
  11. "Apache License"); you may not use this file except in compliance with the
  12. Apache License. You may obtain a copy of the Apache License at
  13. http://www.apache.org/licenses/LICENSE-2.0.
  14. Unless required by applicable law or agreed to in writing, software distributed
  15. under the Apache License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES
  16. OR CONDITIONS OF ANY KIND, either express or implied. See the Apache License for
  17. the specific language governing permissions and limitations under the License.
  18. Copyright (c) 2023 Audiokinetic Inc.
  19. *******************************************************************************/
  20. // AkSimd.h
  21. /// \file
  22. /// AKSIMD - SSE implementation
  23. #pragma once
  24. #include <AK/SoundEngine/Common/AkSimdTypes.h>
  25. #include <AK/SoundEngine/Common/AkTypes.h>
  26. #include <xmmintrin.h>
  27. #include <smmintrin.h>
  28. #include <emmintrin.h>
  29. #if defined(__FMA__) || defined(__AVX2__)
  30. #include <immintrin.h>
  31. #endif
  32. ////////////////////////////////////////////////////////////////////////
  33. /// @name Platform specific memory size alignment for allocation purposes
  34. //@{
  35. #define AKSIMD_ALIGNSIZE( __Size__ ) (((__Size__) + 15) & ~15)
  36. //@}
  37. ////////////////////////////////////////////////////////////////////////
  38. ////////////////////////////////////////////////////////////////////////
  39. /// @name AKSIMD loading / setting
  40. //@{
  41. /// Loads four single-precision floating-point values from unaligned
  42. /// memory (see _mm_loadu_ps)
  43. #define AKSIMD_LOAD_V4F32( __addr__ ) _mm_loadu_ps( (AkReal32*)(__addr__) )
  44. /// Loads four single-precision floating-point values from unaligned
  45. /// memory (see _mm_loadu_ps)
  46. #define AKSIMD_LOADU_V4F32( __addr__ ) _mm_loadu_ps( (__addr__) )
  47. /// Loads a single single-precision, floating-point value, copying it into
  48. /// all four words (see _mm_load1_ps, _mm_load_ps1)
  49. #define AKSIMD_LOAD1_V4F32( __scalar__ ) _mm_load1_ps( &(__scalar__) )
  50. /// Sets the four single-precision, floating-point values to in_value (see
  51. /// _mm_set1_ps, _mm_set_ps1)
  52. #define AKSIMD_SET_V4F32( __scalar__ ) _mm_set_ps1( (__scalar__) )
  53. /// Sets the two double-precision, floating-point values to in_value
  54. #define AKSIMD_SETV_V2F64( _b, _a ) _mm_castpd_ps(_mm_set_pd( (_b), (_a) ))
  55. /// Populates the full vector with the 4 floating point elements provided
  56. #define AKSIMD_SETV_V4F32( _d, _c, _b, _a ) _mm_set_ps( (_d), (_c), (_b), (_a) )
  57. /// Populates the full vector with the mask[3:0], setting each to 0 or ~0
  58. static AkForceInline AKSIMD_V4COND AKSIMD_SETMASK_V4COND( AkUInt32 x )
  59. {
  60. __m128i temp = _mm_set_epi32(8, 4, 2, 1);
  61. __m128i xvec = _mm_set1_epi32(x);
  62. __m128i xand = _mm_and_si128(xvec, temp);
  63. return _mm_castsi128_ps(_mm_cmpeq_epi32(temp, xand));
  64. }
  65. /// Sets the four single-precision, floating-point values to zero (see
  66. /// _mm_setzero_ps)
  67. #define AKSIMD_SETZERO_V4F32() _mm_setzero_ps()
  68. /// Loads a single-precision, floating-point value into the low word
  69. /// and clears the upper three words.
  70. /// r0 := *p; r1 := 0.0 ; r2 := 0.0 ; r3 := 0.0 (see _mm_load_ss)
  71. #define AKSIMD_LOAD_SS_V4F32( __addr__ ) _mm_load_ss( (__addr__) )
  72. //@}
  73. ////////////////////////////////////////////////////////////////////////
  74. ////////////////////////////////////////////////////////////////////////
  75. /// @name AKSIMD storing
  76. //@{
  77. /// Stores four single-precision, floating-point values. The address
  78. /// does not need to be 16-byte aligned (see _mm_storeu_ps).
  79. #define AKSIMD_STORE_V4F32( __addr__, __vec__ ) _mm_storeu_ps( (AkReal32*)(__addr__), (__vec__) )
  80. /// Stores four single-precision, floating-point values. The address
  81. /// does not need to be 16-byte aligned (see _mm_storeu_ps).
  82. #define AKSIMD_STOREU_V4F32( __addr__, __vec__ ) _mm_storeu_ps( (AkReal32*)(__addr__), (__vec__) )
  83. /// Stores the lower single-precision, floating-point value.
  84. /// *p := a0 (see _mm_store_ss)
  85. #define AKSIMD_STORE1_V4F32( __addr__, __vec__ ) _mm_store_ss( (AkReal32*)(__addr__), (__vec__) )
  86. /// Stores the lower double-precision, floating-point value.
  87. /// *p := a0 (see _mm_store_sd)
  88. #define AKSIMD_STORE1_V2F64( __addr__, __vec__ ) _mm_store_sd( (AkReal64*)(__addr__), _mm_castps_pd(__vec__) )
  89. //@}
  90. ////////////////////////////////////////////////////////////////////////
  91. ////////////////////////////////////////////////////////////////////////
  92. /// @name AKSIMD shuffling
  93. //@{
  94. // Macro for shuffle parameter for AKSIMD_SHUFFLE_V4F32() (see _MM_SHUFFLE)
  95. #define AKSIMD_SHUFFLE( fp3, fp2, fp1, fp0 ) _MM_SHUFFLE( (fp3), (fp2), (fp1), (fp0) )
  96. /// Selects four specific single-precision, floating-point values from
  97. /// a and b, based on the mask i (see _mm_shuffle_ps)
  98. // Usage: AKSIMD_SHUFFLE_V4F32( vec1, vec2, AKSIMD_SHUFFLE( z, y, x, w ) )
  99. #define AKSIMD_SHUFFLE_V4F32( a, b, i ) _mm_shuffle_ps( a, b, i )
  100. #define AKSIMD_SHUFFLE_V4I32( a, b, i ) _mm_castps_si128(_mm_shuffle_ps( _mm_castsi128_ps(a), _mm_castsi128_ps(b), i ))
  101. /// Moves the upper two single-precision, floating-point values of b to
  102. /// the lower two single-precision, floating-point values of the result.
  103. /// The upper two single-precision, floating-point values of a are passed
  104. /// through to the result.
  105. /// r3 := a3; r2 := a2; r1 := b3; r0 := b2 (see _mm_movehl_ps)
  106. #define AKSIMD_MOVEHL_V4F32( a, b ) _mm_movehl_ps( a, b )
  107. /// Moves the lower two single-precision, floating-point values of b to
  108. /// the upper two single-precision, floating-point values of the result.
  109. /// The lower two single-precision, floating-point values of a are passed
  110. /// through to the result.
  111. /// r3 := b1 ; r2 := b0 ; r1 := a1 ; r0 := a0 (see _mm_movelh_ps)
  112. #define AKSIMD_MOVELH_V4F32( a, b ) _mm_movelh_ps( a, b )
  113. /// Swap the 2 lower floats together and the 2 higher floats together.
  114. #define AKSIMD_SHUFFLE_BADC( __a__ ) _mm_shuffle_ps( (__a__), (__a__), _MM_SHUFFLE(2,3,0,1))
  115. /// Swap the 2 lower floats with the 2 higher floats.
  116. #define AKSIMD_SHUFFLE_CDAB( __a__ ) _mm_shuffle_ps( (__a__), (__a__), _MM_SHUFFLE(1,0,3,2))
  117. /// Barrel-shift all floats by one.
  118. #define AKSIMD_SHUFFLE_BCDA( __a__ ) AKSIMD_SHUFFLE_V4F32( (__a__), (__a__), _MM_SHUFFLE(0,3,2,1))
  119. /// Duplicates the odd items into the even items (d c b a -> d d b b )
  120. #define AKSIMD_DUP_ODD(__vv) AKSIMD_SHUFFLE_V4F32(__vv, __vv, AKSIMD_SHUFFLE(3,3,1,1))
  121. /// Duplicates the even items into the odd items (d c b a -> c c a a )
  122. #define AKSIMD_DUP_EVEN(__vv) AKSIMD_SHUFFLE_V4F32(__vv, __vv, AKSIMD_SHUFFLE(2,2,0,0))
  123. //@}
  124. ////////////////////////////////////////////////////////////////////////
  125. ////////////////////////////////////////////////////////////////////////
  126. /// @name AKSIMD arithmetic
  127. //@{
  128. /// Subtracts the four single-precision, floating-point values of
  129. /// a and b (a - b) (see _mm_sub_ps)
  130. #define AKSIMD_SUB_V4F32( a, b ) _mm_sub_ps( a, b )
  131. /// Subtracts the lower single-precision, floating-point values of a and b.
  132. /// The upper three single-precision, floating-point values are passed through from a.
  133. /// r0 := a0 - b0 ; r1 := a1 ; r2 := a2 ; r3 := a3 (see _mm_sub_ss)
  134. #define AKSIMD_SUB_SS_V4F32( a, b ) _mm_sub_ss( a, b )
  135. /// Adds the four single-precision, floating-point values of
  136. /// a and b (see _mm_add_ps)
  137. #define AKSIMD_ADD_V4F32( a, b ) _mm_add_ps( a, b )
  138. /// Adds the lower single-precision, floating-point values of a and b; the
  139. /// upper three single-precision, floating-point values are passed through from a.
  140. /// r0 := a0 + b0; r1 := a1; r2 := a2; r3 := a3 (see _mm_add_ss)
  141. #define AKSIMD_ADD_SS_V4F32( a, b ) _mm_add_ss( a, b )
  142. /// Multiplies the four single-precision, floating-point values
  143. /// of a and b (see _mm_mul_ps)
  144. #define AKSIMD_MUL_V4F32( a, b ) _mm_mul_ps( a, b )
  145. #define AKSIMD_DIV_V4F32( a, b ) _mm_div_ps( a, b )
  146. /// Multiplies the lower single-precision, floating-point values of
  147. /// a and b; the upper three single-precision, floating-point values
  148. /// are passed through from a.
  149. /// r0 := a0 * b0; r1 := a1; r2 := a2; r3 := a3 (see _mm_add_ss)
  150. #define AKSIMD_MUL_SS_V4F32( a, b ) _mm_mul_ss( a, b )
  151. /// Vector multiply-add operation. (if we're targeting a platform or arch with FMA, (AVX2 implies FMA) using the fma intrinsics directly tends to be slightly more desirable)
  152. #if defined(__FMA__) || defined(__AVX2__)
  153. #define AKSIMD_MADD_V4F32( __a__, __b__, __c__ ) _mm_fmadd_ps( (__a__), (__b__) , (__c__) )
  154. #define AKSIMD_MSUB_V4F32( __a__, __b__, __c__ ) _mm_fmsub_ps( (__a__), (__b__) , (__c__) )
  155. #else
  156. #define AKSIMD_MADD_V4F32( __a__, __b__, __c__ ) _mm_add_ps( _mm_mul_ps( (__a__), (__b__) ), (__c__) )
  157. #define AKSIMD_MSUB_V4F32( __a__, __b__, __c__ ) _mm_sub_ps( _mm_mul_ps( (__a__), (__b__) ), (__c__) )
  158. #endif
  159. /// Vector multiply-add operation.
  160. #define AKSIMD_MADD_SS_V4F32( __a__, __b__, __c__ ) _mm_add_ss( _mm_mul_ss( (__a__), (__b__) ), (__c__) )
  161. /// Computes the minima of the four single-precision, floating-point
  162. /// values of a and b (see _mm_min_ps)
  163. #define AKSIMD_MIN_V4F32( a, b ) _mm_min_ps( a, b )
  164. /// Computes the maximums of the four single-precision, floating-point
  165. /// values of a and b (see _mm_max_ps)
  166. #define AKSIMD_MAX_V4F32( a, b ) _mm_max_ps( a, b )
  167. /// Computes the absolute value
  168. #define AKSIMD_ABS_V4F32( a ) _mm_andnot_ps(_mm_set1_ps(-0.f), a)
  169. /// Changes the sign
  170. #define AKSIMD_NEG_V4F32( __a__ ) _mm_xor_ps(_mm_set1_ps(-0.f), __a__)
  171. /// Vector square root aproximation (see _mm_sqrt_ps)
  172. #define AKSIMD_SQRT_V4F32( __a__ ) _mm_sqrt_ps( (__a__) )
  173. /// Vector reciprocal square root approximation 1/sqrt(a), or equivalently, sqrt(1/a)
  174. #define AKSIMD_RSQRT_V4F32( __a__ ) _mm_rsqrt_ps( (__a__) )
  175. /// Reciprocal of x (1/x)
  176. #define AKSIMD_RECIP_V4F32(__a__) _mm_rcp_ps(__a__)
  177. /// Binary xor for single-precision floating-point
  178. #define AKSIMD_XOR_V4F32( a, b ) _mm_xor_ps(a,b)
  179. /// Rounds to upper value
  180. static AkForceInline AKSIMD_V4F32 AKSIMD_CEIL_V4F32(const AKSIMD_V4F32 & x)
  181. {
  182. static const AKSIMD_V4F32 vEpsilon = { 0.49999f, 0.49999f, 0.49999f, 0.49999f };
  183. return _mm_cvtepi32_ps(_mm_cvtps_epi32(_mm_add_ps(x, vEpsilon)));
  184. }
  185. /// Faked in-place vector horizontal add - each element will represent sum of all elements
  186. /// \akwarning
  187. /// Don't expect this to be very efficient.
  188. /// \endakwarning
  189. static AkForceInline AKSIMD_V4F32 AKSIMD_HORIZONTALADD_V4F32(AKSIMD_V4F32 vVec)
  190. {
  191. __m128 vAb = _mm_shuffle_ps(vVec, vVec, 0xB1);
  192. __m128 vHaddAb = _mm_add_ps(vVec, vAb);
  193. __m128 vHaddCd = _mm_shuffle_ps(vHaddAb, vHaddAb, 0x4E);
  194. __m128 vHaddAbcd = _mm_add_ps(vHaddAb, vHaddCd);
  195. return vHaddAbcd;
  196. }
  197. static AkForceInline AKSIMD_V4F32 AKSIMD_DOTPRODUCT( AKSIMD_V4F32 & vVec, const AKSIMD_V4F32 & vfSigns )
  198. {
  199. AKSIMD_V4F32 vfDotProduct = AKSIMD_MUL_V4F32( vVec, vfSigns );
  200. return AKSIMD_HORIZONTALADD_V4F32( vfDotProduct );
  201. }
  202. /// Cross-platform SIMD multiplication of 2 complex data elements with interleaved real and imaginary parts
  203. static AkForceInline AKSIMD_V4F32 AKSIMD_COMPLEXMUL_V4F32( const AKSIMD_V4F32 vCIn1, const AKSIMD_V4F32 vCIn2 )
  204. {
  205. static const AKSIMD_V4F32 vSign = { -0.f, 0.f, -0.f, 0.f };
  206. AKSIMD_V4F32 vTmp1 = AKSIMD_SHUFFLE_V4F32( vCIn1, vCIn1, AKSIMD_SHUFFLE(2,2,0,0));
  207. vTmp1 = AKSIMD_MUL_V4F32( vTmp1, vCIn2 );
  208. AKSIMD_V4F32 vTmp2 = AKSIMD_SHUFFLE_V4F32( vCIn1, vCIn1, AKSIMD_SHUFFLE(3,3,1,1));
  209. vTmp2 = AKSIMD_XOR_V4F32( vTmp2, vSign );
  210. vTmp2 = AKSIMD_MADD_V4F32( vTmp2, AKSIMD_SHUFFLE_BADC( vCIn2 ), vTmp1 );
  211. return vTmp2;
  212. }
  213. #ifdef AK_SSE3
  214. #include <pmmintrin.h>
  215. /// Cross-platform SIMD multiplication of 2 complex data elements with interleaved real and imaginary parts
  216. static AKSIMD_V4F32 AKSIMD_COMPLEXMUL_SSE3( const AKSIMD_V4F32 vCIn1, const AKSIMD_V4F32 vCIn2 )
  217. {
  218. AKSIMD_V4F32 vXMM0 = _mm_moveldup_ps(vCIn1); // multiplier real (a1, a1, a0, a0)
  219. vXMM0 = AKSIMD_MUL_V4F32(vXMM0, vCIn2); // temp1 (a1d1, a1c1, a0d0, a0c0)
  220. AKSIMD_V4F32 xMM1 = _mm_shuffle_ps(vCIn2, vCIn2, 0xB1); // shuf multiplicand(c1, d1, c0, d0)
  221. AKSIMD_V4F32 xMM2 = _mm_movehdup_ps(vCIn1); // multiplier imag (b1, b1, b0, b0)
  222. xMM2 = AKSIMD_MUL_V4F32( xMM2, xMM1); // temp2 (b1c1, b1d1, b0c0, b0d0)
  223. AKSIMD_V4F32 vCOut = _mm_addsub_ps(vXMM0, xMM2); // b1c1+a1d1, a1c1-b1d1, a0d0+b0d0, a0c0-b0c0
  224. return vCOut;
  225. }
  226. #endif
  227. #if __SSE3__
  228. // Alternatively add and subtract packed single-precision (32-bit) floating-point elements in a
  229. // to/from packed elements in b, and store the results in dst.
  230. #define AKSIMD_ADDSUB_V4F32( a, b ) _mm_addsub_ps( a, b)
  231. #else
  232. // Alternatively add and subtract packed single-precision (32-bit) floating-point elements in a
  233. // to/from packed elements in b, and store the results in dst.
  234. #define AKSIMD_ADDSUB_V4F32( a, b ) _mm_add_ps( a, _mm_xor_ps(b, AKSIMD_SETV_V4F32(0.f, -0.f, 0.f, -0.f)))
  235. #endif
  236. #if defined _MSC_VER && ( _MSC_VER <= 1600 )
  237. #define AKSIMD_ASSERTFLUSHZEROMODE AKASSERT( _MM_GET_FLUSH_ZERO_MODE(dummy) == _MM_FLUSH_ZERO_ON )
  238. #elif defined(AK_CPU_X86) || defined(AK_CPU_X86_64)
  239. #define AKSIMD_ASSERTFLUSHZEROMODE AKASSERT( _MM_GET_FLUSH_ZERO_MODE() == _MM_FLUSH_ZERO_ON )
  240. #else
  241. #define AKSIMD_ASSERTFLUSHZEROMODE
  242. #endif
  243. //@}
  244. ////////////////////////////////////////////////////////////////////////
  245. ////////////////////////////////////////////////////////////////////////
  246. /// @name AKSIMD integer arithmetic
  247. //@{
  248. /// Adds the four integer values of a and b
  249. #define AKSIMD_ADD_V4I32( a, b ) _mm_add_epi32( a, b )
  250. #define AKSIMD_CMPLT_V4I32( a, b ) _mm_cmplt_epi32(a,b)
  251. #define AKSIMD_CMPGT_V4I32( a, b ) _mm_cmpgt_epi32(a,b)
  252. #define AKSIMD_OR_V4I32( a, b ) _mm_or_si128(a,b)
  253. #define AKSIMD_XOR_V4I32( a, b ) _mm_xor_si128(a,b)
  254. #define AKSIMD_SUB_V4I32( a, b ) _mm_sub_epi32(a,b)
  255. #define AKSIMD_NOT_V4I32( a ) _mm_xor_si128(a,_mm_set1_epi32(~0))
  256. #define AKSIMD_OR_V4F32( a, b ) _mm_or_ps(a,b)
  257. #define AKSIMD_AND_V4F32( a, b ) _mm_and_ps(a,b)
  258. #define AKSIMD_ANDNOT_V4F32( a, b ) _mm_andnot_ps(a,b)
  259. #define AKSIMD_NOT_V4F32( a ) _mm_xor_ps(a,_mm_castsi128_ps(_mm_set1_epi32(~0)))
  260. #define AKSIMD_OR_V4COND( a, b ) _mm_or_ps(a,b)
  261. #define AKSIMD_AND_V4COND( a, b ) _mm_and_ps(a,b)
  262. /// Multiplies the low 16bits of a by b and stores it in V4I32 (no overflow)
  263. #define AKSIMD_MULLO16_V4I32( a , b) _mm_mullo_epi16(a, b)
  264. /// Multiplies the low 32bits of a by b and stores it in V4I32 (no overflow)
  265. static AkForceInline AKSIMD_V4I32 AKSIMD_MULLO_V4I32(const AKSIMD_V4I32 vIn1, const AKSIMD_V4I32 vIn2)
  266. {
  267. #ifdef __SSE4_1__ // use SSE 4.1 version directly where possible
  268. return _mm_mullo_epi32(vIn1, vIn2);
  269. #else // use SSE 2 otherwise
  270. __m128i tmp1 = _mm_mul_epu32(vIn1, vIn2); // mul 2,0
  271. __m128i tmp2 = _mm_mul_epu32(_mm_srli_si128(vIn1, 4), _mm_srli_si128(vIn2, 4)); // mul 3,1
  272. return _mm_unpacklo_epi32(_mm_shuffle_epi32(tmp1, _MM_SHUFFLE(0, 0, 2, 0)), _mm_shuffle_epi32(tmp2, _MM_SHUFFLE(0, 0, 2, 0))); // shuffle results to [63..0] and pack
  273. #endif
  274. }
  275. //@}
  276. ////////////////////////////////////////////////////////////////////////
  277. ////////////////////////////////////////////////////////////////////////
  278. /// @name AKSIMD packing / unpacking
  279. //@{
  280. /// Selects and interleaves the lower two single-precision, floating-point
  281. /// values from a and b (see _mm_unpacklo_ps)
  282. #define AKSIMD_UNPACKLO_V4F32( a, b ) _mm_unpacklo_ps( a, b )
  283. /// Selects and interleaves the upper two single-precision, floating-point
  284. /// values from a and b (see _mm_unpackhi_ps)
  285. #define AKSIMD_UNPACKHI_V4F32( a, b ) _mm_unpackhi_ps( a, b )
  286. // Given four pointers, gathers 32-bits of data from each location,
  287. // deinterleaves them as 16-bits of each, and sign-extends to 32-bits
  288. // e.g. (*addr[0]) := (b a)
  289. // e.g. (*addr[1]) := (d c)
  290. // e.g. (*addr[2]) := (f e)
  291. // e.g. (*addr[3]) := (h g)
  292. // return struct has
  293. // val[0] := (g e c a)
  294. // val[1] := (h f d b)
  295. static AkForceInline AKSIMD_V4I32X2 AKSIMD_GATHER_V4I32_AND_DEINTERLEAVE_V4I32X2(AkInt16* addr3, AkInt16* addr2, AkInt16* addr1, AkInt16* addr0)
  296. {
  297. __m128i data[4] = {
  298. _mm_set1_epi32(*(AkInt32*)addr0),
  299. _mm_set1_epi32(*(AkInt32*)addr1),
  300. _mm_set1_epi32(*(AkInt32*)addr2),
  301. _mm_set1_epi32(*(AkInt32*)addr3),
  302. };
  303. __m128i group[2] = {
  304. _mm_unpacklo_epi32(data[0], data[1]),
  305. _mm_unpacklo_epi32(data[2], data[3]),
  306. };
  307. __m128i shuffle = _mm_unpacklo_epi64(group[0], group[1]);
  308. AKSIMD_V4I32X2 ret{
  309. _mm_srai_epi32(_mm_slli_epi32(shuffle, 16), 16),
  310. _mm_srai_epi32(shuffle, 16)
  311. };
  312. return ret;
  313. }
  314. // Given four pointers, gathers 64-bits of data from each location,
  315. // deinterleaves them as 16-bits of each, and sign-extends to 32-bits
  316. // e.g. (*addr[0]) := (d c b a)
  317. // e.g. (*addr[1]) := (h g f e)
  318. // e.g. (*addr[2]) := (l k j i)
  319. // e.g. (*addr[3]) := (p o n m)
  320. // return struct has
  321. // val[0] := (m i e a)
  322. // val[1] := (n j f b)
  323. // val[2] := (o k g c)
  324. // val[3] := (p l h d)
  325. static AkForceInline AKSIMD_V4I32X4 AKSIMD_GATHER_V4I64_AND_DEINTERLEAVE_V4I32X4(AkInt16* addr3, AkInt16* addr2, AkInt16* addr1, AkInt16* addr0)
  326. {
  327. __m128i data[4] = {
  328. _mm_set1_epi64x(*(AkInt64*)addr0),
  329. _mm_set1_epi64x(*(AkInt64*)addr1),
  330. _mm_set1_epi64x(*(AkInt64*)addr2),
  331. _mm_set1_epi64x(*(AkInt64*)addr3),
  332. };
  333. __m128i group[2] = {
  334. _mm_unpacklo_epi64(data[0], data[1]),
  335. _mm_unpacklo_epi64(data[2], data[3]),
  336. };
  337. __m128i shuffle[2] = {
  338. _mm_castps_si128 (_mm_shuffle_ps(_mm_castsi128_ps(group[0]), _mm_castsi128_ps(group[1]), 0x88)),
  339. _mm_castps_si128 (_mm_shuffle_ps(_mm_castsi128_ps(group[0]), _mm_castsi128_ps(group[1]), 0xDD)),
  340. };
  341. AKSIMD_V4I32X4 ret{
  342. _mm_srai_epi32(_mm_slli_epi32(shuffle[0],16),16),
  343. _mm_srai_epi32(shuffle[0],16),
  344. _mm_srai_epi32(_mm_slli_epi32(shuffle[1],16),16),
  345. _mm_srai_epi32(shuffle[1],16),
  346. };
  347. return ret;
  348. }
  349. //@}
  350. ////////////////////////////////////////////////////////////////////////
  351. ////////////////////////////////////////////////////////////////////////
  352. /// @name AKSIMD vector comparison
  353. /// Apart from AKSIMD_SEL_GTEQ_V4F32, these implementations are limited to a few platforms.
  354. //@{
  355. #define AKSIMD_CMP_CTRLMASK __m128
  356. /// Vector "<=" operation (see _mm_cmple_ps)
  357. #define AKSIMD_LTEQ_V4F32( __a__, __b__ ) _mm_cmple_ps( (__a__), (__b__) )
  358. #define AKSIMD_LT_V4F32( __a__, __b__ ) _mm_cmplt_ps( (__a__), (__b__) )
  359. /// Vector ">=" operation (see _mm_cmple_ps)
  360. #define AKSIMD_GTEQ_V4F32( __a__, __b__ ) _mm_cmpge_ps( (__a__), (__b__) )
  361. #define AKSIMD_GT_V4F32( __a__, __b__ ) _mm_cmpgt_ps( (__a__), (__b__) )
  362. /// Vector "==" operation (see _mm_cmpeq_ps)
  363. #define AKSIMD_EQ_V4F32( __a__, __b__ ) _mm_cmpeq_ps( (__a__), (__b__) )
  364. /// Return a when control mask is 0, return b when control mask is non zero, control mask is in c and usually provided by above comparison operations
  365. static AkForceInline AKSIMD_V4F32 AKSIMD_VSEL_V4F32( AKSIMD_V4F32 vA, AKSIMD_V4F32 vB, AKSIMD_V4F32 vMask )
  366. {
  367. #if defined(__SSE4_1__)
  368. return _mm_blendv_ps(vA, vB, vMask);
  369. #else
  370. vB = _mm_and_ps( vB, vMask );
  371. vA= _mm_andnot_ps( vMask, vA );
  372. return _mm_or_ps( vA, vB );
  373. #endif
  374. }
  375. // (cond1 >= cond2) ? b : a.
  376. #define AKSIMD_SEL_GTEQ_V4F32( __a__, __b__, __cond1__, __cond2__ ) AKSIMD_VSEL_V4F32( __a__, __b__, AKSIMD_GTEQ_V4F32( __cond1__, __cond2__ ) )
  377. // a >= 0 ? b : c ... Written, like, you know, the normal C++ operator syntax.
  378. #define AKSIMD_SEL_GTEZ_V4F32( __a__, __b__, __c__ ) AKSIMD_VSEL_V4F32( (__c__), (__b__), AKSIMD_GTEQ_V4F32( __a__, _mm_set1_ps(0) ) )
  379. #define AKSIMD_SPLAT_V4F32(var, idx) AKSIMD_SHUFFLE_V4F32(var,var, AKSIMD_SHUFFLE(idx,idx,idx,idx))
  380. #define AKSIMD_MASK_V4F32( __a__ ) _mm_movemask_ps( __a__ )
  381. // returns true if every element of the provided vector is zero
  382. static AkForceInline bool AKSIMD_TESTZERO_V4I32(AKSIMD_V4I32 a)
  383. {
  384. return _mm_movemask_epi8(_mm_cmpeq_epi32(a, _mm_setzero_si128())) == 0xFFFF;
  385. }
  386. #define AKSIMD_TESTZERO_V4F32( __a__ ) AKSIMD_TESTZERO_V4I32(_mm_castps_si128(__a__))
  387. #define AKSIMD_TESTZERO_V4COND( __a__ ) AKSIMD_TESTZERO_V4F32(__a__)
  388. // returns true if every element of the provided vector is ones
  389. static AkForceInline bool AKSIMD_TESTONES_V4I32(AKSIMD_V4I32 a)
  390. {
  391. return _mm_movemask_epi8(_mm_cmpeq_epi32(a, _mm_set1_epi32(~0))) == 0xFFFF;
  392. }
  393. #define AKSIMD_TESTONES_V4F32( __a__ ) AKSIMD_TESTONES_V4I32(_mm_castps_si128(__a__))
  394. #define AKSIMD_TESTONES_V4COND( __a__ ) AKSIMD_TESTONES_V4F32(__a__)
  395. //@}
  396. ////////////////////////////////////////////////////////////////////////
  397. /// Loads unaligned 128-bit value (see _mm_loadu_si128)
  398. #define AKSIMD_LOADU_V4I32( __addr__ ) _mm_loadu_si128( (__addr__) )
  399. /// Loads aligned 128-bit value (see _mm_loadu_si128)
  400. #define AKSIMD_LOAD_V4I32( __addr__ ) _mm_loadu_si128( (__addr__) )
  401. /// Sets the four 32-bit integer values to zero (see _mm_setzero_si128)
  402. #define AKSIMD_SETZERO_V4I32() _mm_setzero_si128()
  403. #define AKSIMD_SET_V4I32( __scalar__ ) _mm_set1_epi32( (__scalar__) )
  404. #define AKSIMD_SETV_V4I32( _d, _c, _b, _a ) _mm_set_epi32( (_d), (_c), (_b), (_a) )
  405. #define AKSIMD_SETV_V2I64( _b, _a ) _mm_set_epi64x( (_b), (_a) )
  406. /// Sets the 32b integer i at the location specified by index in a
  407. #define AKSIMD_INSERT_V4I32( a, i, index) _mm_insert_epi32(a, i, index)
  408. /// Sets the 64b integer i at the location specified by index in a
  409. #define AKSIMD_INSERT_V2I64( a, i, index) _mm_insert_epi64(a, i, index)
  410. /// Stores four 32-bit integer values. The address
  411. /// does not need to be 16-byte aligned (see _mm_storeu_si128).
  412. #define AKSIMD_STORE_V4I32( __addr__, __vec__ ) _mm_storeu_si128( (__m128i*)(__addr__), (__vec__) )
  413. /// Stores four 32-bit integer values. The address
  414. /// does not need to be 16-byte aligned (see _mm_storeu_si128).
  415. #define AKSIMD_STOREU_V4I32( __addr__, __vec__ ) _mm_storeu_si128( (__m128i*)(__addr__), (__vec__) )
  416. ////////////////////////////////////////////////////////////////////////
  417. /// @name AKSIMD conversion
  418. //@{
  419. /// Converts the four signed 32-bit integer values of a to single-precision,
  420. /// floating-point values (see _mm_cvtepi32_ps)
  421. #define AKSIMD_CONVERT_V4I32_TO_V4F32( __vec__ ) _mm_cvtepi32_ps( (__vec__) )
  422. /// Converts the four single-precision, floating-point values of a to signed
  423. /// 32-bit integer values by rounding (see _mm_cvtps_epi32)
  424. #define AKSIMD_ROUND_V4F32_TO_V4I32( __vec__ ) _mm_cvtps_epi32( (__vec__) )
  425. /// Converts the four single-precision, floating-point values of a to signed
  426. /// 32-bit integer values by truncating (see _mm_cvttps_epi32)
  427. #define AKSIMD_TRUNCATE_V4F32_TO_V4I32( __vec__ ) _mm_cvttps_epi32( (__vec__) )
  428. /// Computes the bitwise AND of the 128-bit value in a and the
  429. /// 128-bit value in b (see _mm_and_si128)
  430. #define AKSIMD_AND_V4I32( __a__, __b__ ) _mm_and_si128( (__a__), (__b__) )
  431. /// Compares the 8 signed 16-bit integers in a and the 8 signed
  432. /// 16-bit integers in b for greater than (see _mm_cmpgt_epi16)
  433. #define AKSIMD_CMPGT_V8I16( __a__, __b__ ) _mm_cmpgt_epi16( (__a__), (__b__) )
  434. /// Converts the 4 half-precision floats in the lower 64-bits of the provided
  435. /// vector to 4 full-precision floats
  436. #define AKSIMD_CONVERT_V4F16_TO_V4F32_LO(__vec__) AKSIMD_CONVERT_V4F16_TO_V4F32_HELPER( _mm_unpacklo_epi16(_mm_setzero_si128(), __vec__))
  437. /// Converts the 4 half-precision floats in the upper 64-bits of the provided
  438. /// vector to 4 full-precision floats
  439. #define AKSIMD_CONVERT_V4F16_TO_V4F32_HI(__vec__) AKSIMD_CONVERT_V4F16_TO_V4F32_HELPER( _mm_unpackhi_epi16(_mm_setzero_si128(), __vec__))
  440. static AkForceInline AKSIMD_V4F32 AKSIMD_CONVERT_V4F16_TO_V4F32_HELPER(AKSIMD_V4I32 vec)
  441. {
  442. __m128i expMantData = _mm_and_si128(vec, _mm_set1_epi32(0x7fff0000));
  443. __m128i expMantShifted = _mm_srli_epi32(expMantData, 3); // shift so that the float16 exp/mant is now split along float32's bounds
  444. // magic number to get scale fp16 exp range into fp32 exp range (also renormalize any denorms)
  445. __m128i expMantFloat = _mm_castps_si128(_mm_mul_ps(_mm_castsi128_ps(expMantShifted), _mm_castsi128_ps(_mm_set1_epi32(0x77800000))));
  446. // if fp16 val was inf or nan, preserve the inf/nan exponent field (we can just 'or' the new inf-bits into the attempt at scaling from inf previously)
  447. __m128i infnanCheck = _mm_cmpgt_epi32(expMantData, _mm_set1_epi32(0x7bffffff));
  448. __m128i infnanExp = _mm_and_si128(infnanCheck, _mm_set1_epi32(255 << 23));
  449. __m128i expMantWithInfNan = _mm_or_si128(expMantFloat, infnanExp);
  450. // reincorporate the sign
  451. __m128i signData = _mm_and_si128(vec, _mm_set1_epi32(0x80000000));
  452. __m128 assembledFloat = _mm_castsi128_ps(_mm_or_si128(signData, expMantWithInfNan));
  453. return assembledFloat;
  454. }
  455. /// Converts the 4 full-precision floats vector to 4 half-precision floats
  456. /// occupying the lower bits and leaving the upper bits as zero
  457. static AkForceInline AKSIMD_V4I32 AKSIMD_CONVERT_V4F32_TO_V4F16(AKSIMD_V4F32 vec)
  458. {
  459. __m128i signData = _mm_and_si128(_mm_castps_si128(vec), _mm_set1_epi32(0x80000000));
  460. __m128i unsignedVec = _mm_and_si128(_mm_castps_si128(vec), _mm_set1_epi32(0x7fffffff));
  461. // do the processing for values that will be denormed in float16
  462. // Add 0.5 to get value within range, and rounde; then move mantissa data up
  463. __m128 denormedVec = _mm_add_ps(_mm_castsi128_ps(unsignedVec), _mm_set1_ps(0.5f));
  464. __m128i denormResult = _mm_slli_epi32(_mm_castps_si128(denormedVec), 16);
  465. // processing for values that will be normal in float16
  466. __m128i subnormMagic = _mm_set1_epi32(0xC8000FFF); // -131072 + rounding bias
  467. __m128i normRoundPart1 = _mm_add_epi32(unsignedVec, subnormMagic);
  468. __m128i mantLsb = _mm_slli_epi32(unsignedVec, 31 - 13);
  469. __m128i mantSignExtendLsb = _mm_srai_epi32(mantLsb, 31); // Extend Lsb so that it's -1 when set
  470. __m128i normRoundPart2 = _mm_sub_epi32(normRoundPart1, mantSignExtendLsb); // and subtract the sign-extended bit to finish rounding up
  471. __m128i normResult = _mm_slli_epi32(normRoundPart2, 3);
  472. // Combine the norm and subnorm paths together
  473. __m128i normalMinimum = _mm_set1_epi32((127 - 14) << 23); // smallest float32 that yields a normalized float16
  474. __m128i denormMask = _mm_cmpgt_epi32(normalMinimum, unsignedVec);
  475. __m128i nonNanFloat = _mm_or_si128(_mm_and_si128(denormMask, denormResult), _mm_andnot_si128(denormMask, normResult));
  476. // apply inf/nan check
  477. __m128i isNotInfNanMask = _mm_cmplt_epi32(unsignedVec, _mm_set1_epi32(0x47800000)); // test if the value will be greater than the max representable by float16
  478. __m128i mantissaData = _mm_and_si128(unsignedVec, _mm_set1_epi32(0x007fffff));
  479. __m128i isNanMask = _mm_cmpgt_epi32(unsignedVec, _mm_set1_epi32(0x7F800000)); // mark the parts of the vector where we have a mantissa (i.e. NAN) as 0xffffffff
  480. __m128i nantissaBit = _mm_and_si128(isNanMask, _mm_set1_epi32(0x02000000)); // set the NaN mantissa bit if mantissa suggests this is NaN
  481. __m128i infData = _mm_andnot_si128(mantissaData, _mm_set1_epi32(0x7c000000)); // grab the exponent data from unsigned vec with no mantissa
  482. __m128i infNanFloat = _mm_or_si128(infData, nantissaBit); // if we have a non-zero mantissa, add the NaN mantissa bit
  483. __m128i resultWithInfNan = _mm_or_si128(_mm_and_si128(isNotInfNanMask, nonNanFloat), _mm_andnot_si128(isNotInfNanMask, infNanFloat));
  484. // reincorporate the original sign
  485. __m128i signedResult = _mm_or_si128(signData, resultWithInfNan);
  486. // store results packed in lower 64 bits, and set upper 64 to zero
  487. __m128i resultEpi16Lo = _mm_shufflelo_epi16(signedResult, 0xD); // move 16b ints (x,x,x,x,d,c,b,a) down to (x,x,x,x,x,x,d,b)
  488. __m128i resultEpi16Hi = _mm_shufflehi_epi16(signedResult, 0xD); // move 16b ints (h,g,f,e,x,x,x,x) down to (x,x,h,f,x,x,x,x)
  489. __m128 resultEpi16 = _mm_shuffle_ps(_mm_castsi128_ps(resultEpi16Lo), _mm_castsi128_ps(resultEpi16Hi), 0xE4); // combine - (x, x, h, f, x, x, d, b)
  490. __m128i result = _mm_castps_si128(_mm_shuffle_ps(resultEpi16, _mm_setzero_ps(), 0x8)); // reshuffle with zero - (0,0,0,0,h,f,d,b)
  491. return result;
  492. }
  493. //@}
  494. ////////////////////////////////////////////////////////////////////////
  495. ////////////////////////////////////////////////////////////////////////
  496. /// @name AKSIMD cast
  497. //@{
  498. /// Cast vector of type AKSIMD_V2F64 to type AKSIMD_V4F32. This intrinsic is only
  499. /// used for compilation and does not generate any instructions, thus it has zero latency.
  500. #define AKSIMD_CAST_V2F64_TO_V4F32( __vec__ ) _mm_castpd_ps(__vec__)
  501. /// Cast vector of type AKSIMD_V2F64 to type AKSIMD_V4I32. This intrinsic is only
  502. /// used for compilation and does not generate any instructions, thus it has zero latency.
  503. #define AKSIMD_CAST_V2F64_TO_V4I32( __vec__ ) _mm_castpd_si128(__vec__)
  504. /// Cast vector of type AKSIMD_V4F32 to type AKSIMD_V2F64. This intrinsic is only
  505. /// used for compilation and does not generate any instructions, thus it has zero latency.
  506. #define AKSIMD_CAST_V4F32_TO_V2F64( __vec__ ) _mm_castps_pd(__vec__)
  507. /// Cast vector of type AKSIMD_V4F32 to type AKSIMD_V4I32. This intrinsic is only
  508. /// used for compilation and does not generate any instructions, thus it has zero latency.
  509. #define AKSIMD_CAST_V4F32_TO_V4I32( __vec__ ) _mm_castps_si128(__vec__)
  510. /// Cast vector of type AKSIMD_V4I32 to type AKSIMD_V2F64. This intrinsic is only
  511. /// used for compilation and does not generate any instructions, thus it has zero latency.
  512. #define AKSIMD_CAST_V4I32_TO_V2F64( __vec__ ) _mm_castsi128_pd(__vec__)
  513. /// Cast vector of type AKSIMD_V4I32 to type AKSIMD_V4F32. This intrinsic is only
  514. /// used for compilation and does not generate any instructions, thus it has zero latency.
  515. #define AKSIMD_CAST_V4I32_TO_V4F32( __vec__ ) _mm_castsi128_ps(__vec__)
  516. /// Cast vector of type AKSIMD_V4COND to AKSIMD_V4F32.
  517. #define AKSIMD_CAST_V4COND_TO_V4F32( __vec__ ) (__vec__)
  518. /// Cast vector of type AKSIMD_V4F32 to AKSIMD_V4COND.
  519. #define AKSIMD_CAST_V4F32_TO_V4COND( __vec__ ) (__vec__)
  520. /// Cast vector of type AKSIMD_V4COND to AKSIMD_V4I32.
  521. #define AKSIMD_CAST_V4COND_TO_V4I32( __vec__ ) _mm_castps_si128(__vec__)
  522. /// Cast vector of type AKSIMD_V4I32 to AKSIMD_V4COND.
  523. #define AKSIMD_CAST_V4I32_TO_V4COND( __vec__ ) _mm_castsi128_ps(__vec__)
  524. //@}
  525. ////////////////////////////////////////////////////////////////////////
  526. /// Interleaves the lower 4 signed or unsigned 16-bit integers in a with
  527. /// the lower 4 signed or unsigned 16-bit integers in b (see _mm_unpacklo_epi16)
  528. #define AKSIMD_UNPACKLO_VECTOR8I16( a, b ) _mm_unpacklo_epi16( a, b )
  529. /// Interleaves the upper 4 signed or unsigned 16-bit integers in a with
  530. /// the upper 4 signed or unsigned 16-bit integers in b (see _mm_unpackhi_epi16)
  531. #define AKSIMD_UNPACKHI_VECTOR8I16( a, b ) _mm_unpackhi_epi16( a, b )
  532. /// Packs the 8 signed 32-bit integers from a and b into signed 16-bit
  533. /// integers and saturates (see _mm_packs_epi32)
  534. #define AKSIMD_PACKS_V4I32( a, b ) _mm_packs_epi32( a, b )
  535. ////////////////////////////////////////////////////////////////////////
  536. /// @name AKSIMD shifting
  537. //@{
  538. /// Shifts the 4 signed or unsigned 32-bit integers in a left by
  539. /// in_shiftBy bits while shifting in zeros (see _mm_slli_epi32)
  540. #define AKSIMD_SHIFTLEFT_V4I32( __vec__, __shiftBy__ ) \
  541. _mm_slli_epi32( (__vec__), (__shiftBy__) )
  542. /// Shifts the 4 signed or unsigned 32-bit integers in a right by
  543. /// in_shiftBy bits while shifting in zeros (see _mm_srli_epi32)
  544. #define AKSIMD_SHIFTRIGHT_V4I32( __vec__, __shiftBy__ ) \
  545. _mm_srli_epi32( (__vec__), (__shiftBy__) )
  546. /// Shifts the 4 signed 32-bit integers in a right by in_shiftBy
  547. /// bits while shifting in the sign bit (see _mm_srai_epi32)
  548. #define AKSIMD_SHIFTRIGHTARITH_V4I32( __vec__, __shiftBy__ ) \
  549. _mm_srai_epi32( (__vec__), (__shiftBy__) )
  550. //@}
  551. ////////////////////////////////////////////////////////////////////////
  552. #if defined( AK_CPU_X86 ) /// MMX
  553. typedef __m64 AKSIMD_V2F32; ///< Vector of 2 32-bit floats
  554. #define AKSIMD_SETZERO_V2F32() _mm_setzero_si64()
  555. #define AKSIMD_CMPGT_V2I32( a, b ) _mm_cmpgt_pi16(a,b)
  556. /// Interleaves the lower 2 signed or unsigned 16-bit integers in a with
  557. /// the lower 2 signed or unsigned 16-bit integers in b (see _mm_unpackhi_epi16)
  558. #define AKSIMD_UNPACKLO_VECTOR4I16( a, b ) _mm_unpacklo_pi16( a, b )
  559. /// Interleaves the upper 2 signed or unsigned 16-bit integers in a with
  560. /// the upper 2 signed or unsigned 16-bit integers in b (see _mm_unpackhi_epi16)
  561. #define AKSIMD_UNPACKHI_VECTOR4I16( a, b ) _mm_unpackhi_pi16( a, b )
  562. /// Shifts the 2 signed or unsigned 32-bit integers in a left by
  563. /// in_shiftBy bits while shifting in zeros (see _mm_slli_epi32)
  564. #define AKSIMD_SHIFTLEFT_V2I32( __vec__, __shiftBy__ ) \
  565. _mm_slli_pi32( (__vec__), (__shiftBy__) )
  566. /// Shifts the 2 signed 32-bit integers in a right by in_shiftBy
  567. /// bits while shifting in the sign bit (see _mm_srai_epi32)
  568. #define AKSIMD_SHIFTRIGHTARITH_V2I32( __vec__, __shiftBy__ ) \
  569. _mm_srai_pi32( (__vec__), (__shiftBy__) )
  570. /// Used when ending a block of code that utilizes any MMX construct on x86 code
  571. /// so that the x87 FPU can be used again
  572. #define AKSIMD_MMX_EMPTY _mm_empty()
  573. #endif