astcenc_vecmathlib_avx2_8.h 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213
  1. // SPDX-License-Identifier: Apache-2.0
  2. // ----------------------------------------------------------------------------
  3. // Copyright 2019-2024 Arm Limited
  4. //
  5. // Licensed under the Apache License, Version 2.0 (the "License"); you may not
  6. // use this file except in compliance with the License. You may obtain a copy
  7. // of the License at:
  8. //
  9. // http://www.apache.org/licenses/LICENSE-2.0
  10. //
  11. // Unless required by applicable law or agreed to in writing, software
  12. // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  13. // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  14. // License for the specific language governing permissions and limitations
  15. // under the License.
  16. // ----------------------------------------------------------------------------
  17. /**
  18. * @brief 8x32-bit vectors, implemented using AVX2.
  19. *
  20. * This module implements 8-wide 32-bit float, int, and mask vectors for x86
  21. * AVX2.
  22. *
  23. * There is a baseline level of functionality provided by all vector widths and
  24. * implementations. This is implemented using identical function signatures,
  25. * modulo data type, so we can use them as substitutable implementations in VLA
  26. * code.
  27. */
  28. #ifndef ASTC_VECMATHLIB_AVX2_8_H_INCLUDED
  29. #define ASTC_VECMATHLIB_AVX2_8_H_INCLUDED
  30. #ifndef ASTCENC_SIMD_INLINE
  31. #error "Include astcenc_vecmathlib.h, do not include directly"
  32. #endif
  33. #include <cstdio>
  34. // Define convenience intrinsics that are missing on older compilers
  35. #define astcenc_mm256_set_m128i(m, n) _mm256_insertf128_si256(_mm256_castsi128_si256((n)), (m), 1)
  36. // ============================================================================
  37. // vfloat8 data type
  38. // ============================================================================
  39. /**
  40. * @brief Data type for 8-wide floats.
  41. */
  42. struct vfloat8
  43. {
  44. /**
  45. * @brief Construct from zero-initialized value.
  46. */
  47. ASTCENC_SIMD_INLINE vfloat8() = default;
  48. /**
  49. * @brief Construct from 4 values loaded from an unaligned address.
  50. *
  51. * Consider using loada() which is better with vectors if data is aligned
  52. * to vector length.
  53. */
  54. ASTCENC_SIMD_INLINE explicit vfloat8(const float *p)
  55. {
  56. m = _mm256_loadu_ps(p);
  57. }
  58. /**
  59. * @brief Construct from 1 scalar value replicated across all lanes.
  60. *
  61. * Consider using zero() for constexpr zeros.
  62. */
  63. ASTCENC_SIMD_INLINE explicit vfloat8(float a)
  64. {
  65. m = _mm256_set1_ps(a);
  66. }
  67. /**
  68. * @brief Construct from 8 scalar values.
  69. *
  70. * The value of @c a is stored to lane 0 (LSB) in the SIMD register.
  71. */
  72. ASTCENC_SIMD_INLINE explicit vfloat8(
  73. float a, float b, float c, float d,
  74. float e, float f, float g, float h)
  75. {
  76. m = _mm256_set_ps(h, g, f, e, d, c, b, a);
  77. }
  78. /**
  79. * @brief Construct from an existing SIMD register.
  80. */
  81. ASTCENC_SIMD_INLINE explicit vfloat8(__m256 a)
  82. {
  83. m = a;
  84. }
  85. /**
  86. * @brief Get the scalar value of a single lane.
  87. */
  88. template <int l> ASTCENC_SIMD_INLINE float lane() const
  89. {
  90. #if !defined(__clang__) && defined(_MSC_VER)
  91. return m.m256_f32[l];
  92. #else
  93. union { __m256 m; float f[8]; } cvt;
  94. cvt.m = m;
  95. return cvt.f[l];
  96. #endif
  97. }
  98. /**
  99. * @brief Factory that returns a vector of zeros.
  100. */
  101. static ASTCENC_SIMD_INLINE vfloat8 zero()
  102. {
  103. return vfloat8(_mm256_setzero_ps());
  104. }
  105. /**
  106. * @brief Factory that returns a replicated scalar loaded from memory.
  107. */
  108. static ASTCENC_SIMD_INLINE vfloat8 load1(const float* p)
  109. {
  110. return vfloat8(_mm256_broadcast_ss(p));
  111. }
  112. /**
  113. * @brief Factory that returns a vector loaded from 32B aligned memory.
  114. */
  115. static ASTCENC_SIMD_INLINE vfloat8 loada(const float* p)
  116. {
  117. return vfloat8(_mm256_load_ps(p));
  118. }
  119. /**
  120. * @brief Factory that returns a vector containing the lane IDs.
  121. */
  122. static ASTCENC_SIMD_INLINE vfloat8 lane_id()
  123. {
  124. return vfloat8(_mm256_set_ps(7, 6, 5, 4, 3, 2, 1, 0));
  125. }
  126. /**
  127. * @brief The vector ...
  128. */
  129. __m256 m;
  130. };
  131. // ============================================================================
  132. // vint8 data type
  133. // ============================================================================
  134. /**
  135. * @brief Data type for 8-wide ints.
  136. */
  137. struct vint8
  138. {
  139. /**
  140. * @brief Construct from zero-initialized value.
  141. */
  142. ASTCENC_SIMD_INLINE vint8() = default;
  143. /**
  144. * @brief Construct from 8 values loaded from an unaligned address.
  145. *
  146. * Consider using loada() which is better with vectors if data is aligned
  147. * to vector length.
  148. */
  149. ASTCENC_SIMD_INLINE explicit vint8(const int *p)
  150. {
  151. m = _mm256_loadu_si256(reinterpret_cast<const __m256i*>(p));
  152. }
  153. /**
  154. * @brief Construct from 8 uint8_t loaded from an unaligned address.
  155. */
  156. ASTCENC_SIMD_INLINE explicit vint8(const uint8_t *p)
  157. {
  158. // _mm_loadu_si64 would be nicer syntax, but missing on older GCC
  159. m = _mm256_cvtepu8_epi32(_mm_cvtsi64_si128(*reinterpret_cast<const long long*>(p)));
  160. }
  161. /**
  162. * @brief Construct from 1 scalar value replicated across all lanes.
  163. *
  164. * Consider using vfloat4::zero() for constexpr zeros.
  165. */
  166. ASTCENC_SIMD_INLINE explicit vint8(int a)
  167. {
  168. m = _mm256_set1_epi32(a);
  169. }
  170. /**
  171. * @brief Construct from 8 scalar values.
  172. *
  173. * The value of @c a is stored to lane 0 (LSB) in the SIMD register.
  174. */
  175. ASTCENC_SIMD_INLINE explicit vint8(
  176. int a, int b, int c, int d,
  177. int e, int f, int g, int h)
  178. {
  179. m = _mm256_set_epi32(h, g, f, e, d, c, b, a);
  180. }
  181. /**
  182. * @brief Construct from an existing SIMD register.
  183. */
  184. ASTCENC_SIMD_INLINE explicit vint8(__m256i a)
  185. {
  186. m = a;
  187. }
  188. /**
  189. * @brief Get the scalar from a single lane.
  190. */
  191. template <int l> ASTCENC_SIMD_INLINE int lane() const
  192. {
  193. #if !defined(__clang__) && defined(_MSC_VER)
  194. return m.m256i_i32[l];
  195. #else
  196. union { __m256i m; int f[8]; } cvt;
  197. cvt.m = m;
  198. return cvt.f[l];
  199. #endif
  200. }
  201. /**
  202. * @brief Factory that returns a vector of zeros.
  203. */
  204. static ASTCENC_SIMD_INLINE vint8 zero()
  205. {
  206. return vint8(_mm256_setzero_si256());
  207. }
  208. /**
  209. * @brief Factory that returns a replicated scalar loaded from memory.
  210. */
  211. static ASTCENC_SIMD_INLINE vint8 load1(const int* p)
  212. {
  213. __m128i a = _mm_set1_epi32(*p);
  214. return vint8(_mm256_broadcastd_epi32(a));
  215. }
  216. /**
  217. * @brief Factory that returns a vector loaded from unaligned memory.
  218. */
  219. static ASTCENC_SIMD_INLINE vint8 load(const uint8_t* p)
  220. {
  221. return vint8(_mm256_lddqu_si256(reinterpret_cast<const __m256i*>(p)));
  222. }
  223. /**
  224. * @brief Factory that returns a vector loaded from 32B aligned memory.
  225. */
  226. static ASTCENC_SIMD_INLINE vint8 loada(const int* p)
  227. {
  228. return vint8(_mm256_load_si256(reinterpret_cast<const __m256i*>(p)));
  229. }
  230. /**
  231. * @brief Factory that returns a vector containing the lane IDs.
  232. */
  233. static ASTCENC_SIMD_INLINE vint8 lane_id()
  234. {
  235. return vint8(_mm256_set_epi32(7, 6, 5, 4, 3, 2, 1, 0));
  236. }
  237. /**
  238. * @brief The vector ...
  239. */
  240. __m256i m;
  241. };
  242. // ============================================================================
  243. // vmask8 data type
  244. // ============================================================================
  245. /**
  246. * @brief Data type for 8-wide control plane masks.
  247. */
  248. struct vmask8
  249. {
  250. /**
  251. * @brief Construct from an existing SIMD register.
  252. */
  253. ASTCENC_SIMD_INLINE explicit vmask8(__m256 a)
  254. {
  255. m = a;
  256. }
  257. /**
  258. * @brief Construct from an existing SIMD register.
  259. */
  260. ASTCENC_SIMD_INLINE explicit vmask8(__m256i a)
  261. {
  262. m = _mm256_castsi256_ps(a);
  263. }
  264. /**
  265. * @brief Construct from 1 scalar value.
  266. */
  267. ASTCENC_SIMD_INLINE explicit vmask8(bool a)
  268. {
  269. vint8 mask(a == false ? 0 : -1);
  270. m = _mm256_castsi256_ps(mask.m);
  271. }
  272. /**
  273. * @brief The vector ...
  274. */
  275. __m256 m;
  276. };
  277. // ============================================================================
  278. // vmask8 operators and functions
  279. // ============================================================================
  280. /**
  281. * @brief Overload: mask union (or).
  282. */
  283. ASTCENC_SIMD_INLINE vmask8 operator|(vmask8 a, vmask8 b)
  284. {
  285. return vmask8(_mm256_or_ps(a.m, b.m));
  286. }
  287. /**
  288. * @brief Overload: mask intersect (and).
  289. */
  290. ASTCENC_SIMD_INLINE vmask8 operator&(vmask8 a, vmask8 b)
  291. {
  292. return vmask8(_mm256_and_ps(a.m, b.m));
  293. }
  294. /**
  295. * @brief Overload: mask difference (xor).
  296. */
  297. ASTCENC_SIMD_INLINE vmask8 operator^(vmask8 a, vmask8 b)
  298. {
  299. return vmask8(_mm256_xor_ps(a.m, b.m));
  300. }
  301. /**
  302. * @brief Overload: mask invert (not).
  303. */
  304. ASTCENC_SIMD_INLINE vmask8 operator~(vmask8 a)
  305. {
  306. return vmask8(_mm256_xor_si256(_mm256_castps_si256(a.m), _mm256_set1_epi32(-1)));
  307. }
  308. /**
  309. * @brief Return a 8-bit mask code indicating mask status.
  310. *
  311. * bit0 = lane 0
  312. */
  313. ASTCENC_SIMD_INLINE unsigned int mask(vmask8 a)
  314. {
  315. return static_cast<unsigned int>(_mm256_movemask_ps(a.m));
  316. }
  317. /**
  318. * @brief True if any lanes are enabled, false otherwise.
  319. */
  320. ASTCENC_SIMD_INLINE bool any(vmask8 a)
  321. {
  322. return mask(a) != 0;
  323. }
  324. /**
  325. * @brief True if all lanes are enabled, false otherwise.
  326. */
  327. ASTCENC_SIMD_INLINE bool all(vmask8 a)
  328. {
  329. return mask(a) == 0xFF;
  330. }
  331. // ============================================================================
  332. // vint8 operators and functions
  333. // ============================================================================
  334. /**
  335. * @brief Overload: vector by vector addition.
  336. */
  337. ASTCENC_SIMD_INLINE vint8 operator+(vint8 a, vint8 b)
  338. {
  339. return vint8(_mm256_add_epi32(a.m, b.m));
  340. }
  341. /**
  342. * @brief Overload: vector by vector incremental addition.
  343. */
  344. ASTCENC_SIMD_INLINE vint8& operator+=(vint8& a, const vint8& b)
  345. {
  346. a = a + b;
  347. return a;
  348. }
  349. /**
  350. * @brief Overload: vector by vector subtraction.
  351. */
  352. ASTCENC_SIMD_INLINE vint8 operator-(vint8 a, vint8 b)
  353. {
  354. return vint8(_mm256_sub_epi32(a.m, b.m));
  355. }
  356. /**
  357. * @brief Overload: vector by vector multiplication.
  358. */
  359. ASTCENC_SIMD_INLINE vint8 operator*(vint8 a, vint8 b)
  360. {
  361. return vint8(_mm256_mullo_epi32(a.m, b.m));
  362. }
  363. /**
  364. * @brief Overload: vector bit invert.
  365. */
  366. ASTCENC_SIMD_INLINE vint8 operator~(vint8 a)
  367. {
  368. return vint8(_mm256_xor_si256(a.m, _mm256_set1_epi32(-1)));
  369. }
  370. /**
  371. * @brief Overload: vector by vector bitwise or.
  372. */
  373. ASTCENC_SIMD_INLINE vint8 operator|(vint8 a, vint8 b)
  374. {
  375. return vint8(_mm256_or_si256(a.m, b.m));
  376. }
  377. /**
  378. * @brief Overload: vector by vector bitwise and.
  379. */
  380. ASTCENC_SIMD_INLINE vint8 operator&(vint8 a, vint8 b)
  381. {
  382. return vint8(_mm256_and_si256(a.m, b.m));
  383. }
  384. /**
  385. * @brief Overload: vector by vector bitwise xor.
  386. */
  387. ASTCENC_SIMD_INLINE vint8 operator^(vint8 a, vint8 b)
  388. {
  389. return vint8(_mm256_xor_si256(a.m, b.m));
  390. }
  391. /**
  392. * @brief Overload: vector by vector equality.
  393. */
  394. ASTCENC_SIMD_INLINE vmask8 operator==(vint8 a, vint8 b)
  395. {
  396. return vmask8(_mm256_cmpeq_epi32(a.m, b.m));
  397. }
  398. /**
  399. * @brief Overload: vector by vector inequality.
  400. */
  401. ASTCENC_SIMD_INLINE vmask8 operator!=(vint8 a, vint8 b)
  402. {
  403. return ~vmask8(_mm256_cmpeq_epi32(a.m, b.m));
  404. }
  405. /**
  406. * @brief Overload: vector by vector less than.
  407. */
  408. ASTCENC_SIMD_INLINE vmask8 operator<(vint8 a, vint8 b)
  409. {
  410. return vmask8(_mm256_cmpgt_epi32(b.m, a.m));
  411. }
  412. /**
  413. * @brief Overload: vector by vector greater than.
  414. */
  415. ASTCENC_SIMD_INLINE vmask8 operator>(vint8 a, vint8 b)
  416. {
  417. return vmask8(_mm256_cmpgt_epi32(a.m, b.m));
  418. }
  419. /**
  420. * @brief Logical shift left.
  421. */
  422. template <int s> ASTCENC_SIMD_INLINE vint8 lsl(vint8 a)
  423. {
  424. return vint8(_mm256_slli_epi32(a.m, s));
  425. }
  426. /**
  427. * @brief Arithmetic shift right.
  428. */
  429. template <int s> ASTCENC_SIMD_INLINE vint8 asr(vint8 a)
  430. {
  431. return vint8(_mm256_srai_epi32(a.m, s));
  432. }
  433. /**
  434. * @brief Logical shift right.
  435. */
  436. template <int s> ASTCENC_SIMD_INLINE vint8 lsr(vint8 a)
  437. {
  438. return vint8(_mm256_srli_epi32(a.m, s));
  439. }
  440. /**
  441. * @brief Return the min vector of two vectors.
  442. */
  443. ASTCENC_SIMD_INLINE vint8 min(vint8 a, vint8 b)
  444. {
  445. return vint8(_mm256_min_epi32(a.m, b.m));
  446. }
  447. /**
  448. * @brief Return the max vector of two vectors.
  449. */
  450. ASTCENC_SIMD_INLINE vint8 max(vint8 a, vint8 b)
  451. {
  452. return vint8(_mm256_max_epi32(a.m, b.m));
  453. }
  454. /**
  455. * @brief Return the horizontal minimum of a vector.
  456. */
  457. ASTCENC_SIMD_INLINE vint8 hmin(vint8 a)
  458. {
  459. __m128i m = _mm_min_epi32(_mm256_extracti128_si256(a.m, 0), _mm256_extracti128_si256(a.m, 1));
  460. m = _mm_min_epi32(m, _mm_shuffle_epi32(m, _MM_SHUFFLE(0,0,3,2)));
  461. m = _mm_min_epi32(m, _mm_shuffle_epi32(m, _MM_SHUFFLE(0,0,0,1)));
  462. m = _mm_shuffle_epi32(m, _MM_SHUFFLE(0,0,0,0));
  463. __m256i r = astcenc_mm256_set_m128i(m, m);
  464. vint8 vmin(r);
  465. return vmin;
  466. }
  467. /**
  468. * @brief Return the horizontal maximum of a vector.
  469. */
  470. ASTCENC_SIMD_INLINE vint8 hmax(vint8 a)
  471. {
  472. __m128i m = _mm_max_epi32(_mm256_extracti128_si256(a.m, 0), _mm256_extracti128_si256(a.m, 1));
  473. m = _mm_max_epi32(m, _mm_shuffle_epi32(m, _MM_SHUFFLE(0,0,3,2)));
  474. m = _mm_max_epi32(m, _mm_shuffle_epi32(m, _MM_SHUFFLE(0,0,0,1)));
  475. m = _mm_shuffle_epi32(m, _MM_SHUFFLE(0,0,0,0));
  476. __m256i r = astcenc_mm256_set_m128i(m, m);
  477. vint8 vmax(r);
  478. return vmax;
  479. }
  480. /**
  481. * @brief Store a vector to a 16B aligned memory address.
  482. */
  483. ASTCENC_SIMD_INLINE void storea(vint8 a, int* p)
  484. {
  485. _mm256_store_si256(reinterpret_cast<__m256i*>(p), a.m);
  486. }
  487. /**
  488. * @brief Store a vector to an unaligned memory address.
  489. */
  490. ASTCENC_SIMD_INLINE void store(vint8 a, int* p)
  491. {
  492. _mm256_storeu_si256(reinterpret_cast<__m256i*>(p), a.m);
  493. }
  494. /**
  495. * @brief Store lowest N (vector width) bytes into an unaligned address.
  496. */
  497. ASTCENC_SIMD_INLINE void store_nbytes(vint8 a, uint8_t* p)
  498. {
  499. // This is the most logical implementation, but the convenience intrinsic
  500. // is missing on older compilers (supported in g++ 9 and clang++ 9).
  501. // _mm_storeu_si64(ptr, _mm256_extracti128_si256(v.m, 0))
  502. _mm_storel_epi64(reinterpret_cast<__m128i*>(p), _mm256_extracti128_si256(a.m, 0));
  503. }
  504. /**
  505. * @brief Gather N (vector width) indices from the array.
  506. */
  507. ASTCENC_SIMD_INLINE vint8 gatheri(const int* base, vint8 indices)
  508. {
  509. return vint8(_mm256_i32gather_epi32(base, indices.m, 4));
  510. }
  511. /**
  512. * @brief Pack low 8 bits of N (vector width) lanes into bottom of vector.
  513. */
  514. ASTCENC_SIMD_INLINE vint8 pack_low_bytes(vint8 v)
  515. {
  516. __m256i shuf = _mm256_set_epi8(0, 0, 0, 0, 0, 0, 0, 0,
  517. 0, 0, 0, 0, 28, 24, 20, 16,
  518. 0, 0, 0, 0, 0, 0, 0, 0,
  519. 0, 0, 0, 0, 12, 8, 4, 0);
  520. __m256i a = _mm256_shuffle_epi8(v.m, shuf);
  521. __m128i a0 = _mm256_extracti128_si256(a, 0);
  522. __m128i a1 = _mm256_extracti128_si256(a, 1);
  523. __m128i b = _mm_unpacklo_epi32(a0, a1);
  524. __m256i r = astcenc_mm256_set_m128i(b, b);
  525. return vint8(r);
  526. }
  527. /**
  528. * @brief Return lanes from @c b if @c cond is set, else @c a.
  529. */
  530. ASTCENC_SIMD_INLINE vint8 select(vint8 a, vint8 b, vmask8 cond)
  531. {
  532. __m256i condi = _mm256_castps_si256(cond.m);
  533. return vint8(_mm256_blendv_epi8(a.m, b.m, condi));
  534. }
  535. // ============================================================================
  536. // vfloat4 operators and functions
  537. // ============================================================================
  538. /**
  539. * @brief Overload: vector by vector addition.
  540. */
  541. ASTCENC_SIMD_INLINE vfloat8 operator+(vfloat8 a, vfloat8 b)
  542. {
  543. return vfloat8(_mm256_add_ps(a.m, b.m));
  544. }
  545. /**
  546. * @brief Overload: vector by vector incremental addition.
  547. */
  548. ASTCENC_SIMD_INLINE vfloat8& operator+=(vfloat8& a, const vfloat8& b)
  549. {
  550. a = a + b;
  551. return a;
  552. }
  553. /**
  554. * @brief Overload: vector by vector subtraction.
  555. */
  556. ASTCENC_SIMD_INLINE vfloat8 operator-(vfloat8 a, vfloat8 b)
  557. {
  558. return vfloat8(_mm256_sub_ps(a.m, b.m));
  559. }
  560. /**
  561. * @brief Overload: vector by vector multiplication.
  562. */
  563. ASTCENC_SIMD_INLINE vfloat8 operator*(vfloat8 a, vfloat8 b)
  564. {
  565. return vfloat8(_mm256_mul_ps(a.m, b.m));
  566. }
  567. /**
  568. * @brief Overload: vector by scalar multiplication.
  569. */
  570. ASTCENC_SIMD_INLINE vfloat8 operator*(vfloat8 a, float b)
  571. {
  572. return vfloat8(_mm256_mul_ps(a.m, _mm256_set1_ps(b)));
  573. }
  574. /**
  575. * @brief Overload: scalar by vector multiplication.
  576. */
  577. ASTCENC_SIMD_INLINE vfloat8 operator*(float a, vfloat8 b)
  578. {
  579. return vfloat8(_mm256_mul_ps(_mm256_set1_ps(a), b.m));
  580. }
  581. /**
  582. * @brief Overload: vector by vector division.
  583. */
  584. ASTCENC_SIMD_INLINE vfloat8 operator/(vfloat8 a, vfloat8 b)
  585. {
  586. return vfloat8(_mm256_div_ps(a.m, b.m));
  587. }
  588. /**
  589. * @brief Overload: vector by scalar division.
  590. */
  591. ASTCENC_SIMD_INLINE vfloat8 operator/(vfloat8 a, float b)
  592. {
  593. return vfloat8(_mm256_div_ps(a.m, _mm256_set1_ps(b)));
  594. }
  595. /**
  596. * @brief Overload: scalar by vector division.
  597. */
  598. ASTCENC_SIMD_INLINE vfloat8 operator/(float a, vfloat8 b)
  599. {
  600. return vfloat8(_mm256_div_ps(_mm256_set1_ps(a), b.m));
  601. }
  602. /**
  603. * @brief Overload: vector by vector equality.
  604. */
  605. ASTCENC_SIMD_INLINE vmask8 operator==(vfloat8 a, vfloat8 b)
  606. {
  607. return vmask8(_mm256_cmp_ps(a.m, b.m, _CMP_EQ_OQ));
  608. }
  609. /**
  610. * @brief Overload: vector by vector inequality.
  611. */
  612. ASTCENC_SIMD_INLINE vmask8 operator!=(vfloat8 a, vfloat8 b)
  613. {
  614. return vmask8(_mm256_cmp_ps(a.m, b.m, _CMP_NEQ_OQ));
  615. }
  616. /**
  617. * @brief Overload: vector by vector less than.
  618. */
  619. ASTCENC_SIMD_INLINE vmask8 operator<(vfloat8 a, vfloat8 b)
  620. {
  621. return vmask8(_mm256_cmp_ps(a.m, b.m, _CMP_LT_OQ));
  622. }
  623. /**
  624. * @brief Overload: vector by vector greater than.
  625. */
  626. ASTCENC_SIMD_INLINE vmask8 operator>(vfloat8 a, vfloat8 b)
  627. {
  628. return vmask8(_mm256_cmp_ps(a.m, b.m, _CMP_GT_OQ));
  629. }
  630. /**
  631. * @brief Overload: vector by vector less than or equal.
  632. */
  633. ASTCENC_SIMD_INLINE vmask8 operator<=(vfloat8 a, vfloat8 b)
  634. {
  635. return vmask8(_mm256_cmp_ps(a.m, b.m, _CMP_LE_OQ));
  636. }
  637. /**
  638. * @brief Overload: vector by vector greater than or equal.
  639. */
  640. ASTCENC_SIMD_INLINE vmask8 operator>=(vfloat8 a, vfloat8 b)
  641. {
  642. return vmask8(_mm256_cmp_ps(a.m, b.m, _CMP_GE_OQ));
  643. }
  644. /**
  645. * @brief Return the min vector of two vectors.
  646. *
  647. * If either lane value is NaN, @c b will be returned for that lane.
  648. */
  649. ASTCENC_SIMD_INLINE vfloat8 min(vfloat8 a, vfloat8 b)
  650. {
  651. return vfloat8(_mm256_min_ps(a.m, b.m));
  652. }
  653. /**
  654. * @brief Return the min vector of a vector and a scalar.
  655. *
  656. * If either lane value is NaN, @c b will be returned for that lane.
  657. */
  658. ASTCENC_SIMD_INLINE vfloat8 min(vfloat8 a, float b)
  659. {
  660. return min(a, vfloat8(b));
  661. }
  662. /**
  663. * @brief Return the max vector of two vectors.
  664. *
  665. * If either lane value is NaN, @c b will be returned for that lane.
  666. */
  667. ASTCENC_SIMD_INLINE vfloat8 max(vfloat8 a, vfloat8 b)
  668. {
  669. return vfloat8(_mm256_max_ps(a.m, b.m));
  670. }
  671. /**
  672. * @brief Return the max vector of a vector and a scalar.
  673. *
  674. * If either lane value is NaN, @c b will be returned for that lane.
  675. */
  676. ASTCENC_SIMD_INLINE vfloat8 max(vfloat8 a, float b)
  677. {
  678. return max(a, vfloat8(b));
  679. }
  680. /**
  681. * @brief Return the clamped value between min and max.
  682. *
  683. * It is assumed that neither @c min nor @c max are NaN values. If @c a is NaN
  684. * then @c min will be returned for that lane.
  685. */
  686. ASTCENC_SIMD_INLINE vfloat8 clamp(float min, float max, vfloat8 a)
  687. {
  688. // Do not reorder - second operand will return if either is NaN
  689. a.m = _mm256_max_ps(a.m, _mm256_set1_ps(min));
  690. a.m = _mm256_min_ps(a.m, _mm256_set1_ps(max));
  691. return a;
  692. }
  693. /**
  694. * @brief Return a clamped value between 0.0f and max.
  695. *
  696. * It is assumed that @c max is not a NaN value. If @c a is NaN then zero will
  697. * be returned for that lane.
  698. */
  699. ASTCENC_SIMD_INLINE vfloat8 clampz(float max, vfloat8 a)
  700. {
  701. a.m = _mm256_max_ps(a.m, _mm256_setzero_ps());
  702. a.m = _mm256_min_ps(a.m, _mm256_set1_ps(max));
  703. return a;
  704. }
  705. /**
  706. * @brief Return a clamped value between 0.0f and 1.0f.
  707. *
  708. * If @c a is NaN then zero will be returned for that lane.
  709. */
  710. ASTCENC_SIMD_INLINE vfloat8 clampzo(vfloat8 a)
  711. {
  712. a.m = _mm256_max_ps(a.m, _mm256_setzero_ps());
  713. a.m = _mm256_min_ps(a.m, _mm256_set1_ps(1.0f));
  714. return a;
  715. }
  716. /**
  717. * @brief Return the absolute value of the float vector.
  718. */
  719. ASTCENC_SIMD_INLINE vfloat8 abs(vfloat8 a)
  720. {
  721. __m256 msk = _mm256_castsi256_ps(_mm256_set1_epi32(0x7fffffff));
  722. return vfloat8(_mm256_and_ps(a.m, msk));
  723. }
  724. /**
  725. * @brief Return a float rounded to the nearest integer value.
  726. */
  727. ASTCENC_SIMD_INLINE vfloat8 round(vfloat8 a)
  728. {
  729. constexpr int flags = _MM_FROUND_TO_NEAREST_INT | _MM_FROUND_NO_EXC;
  730. return vfloat8(_mm256_round_ps(a.m, flags));
  731. }
  732. /**
  733. * @brief Return the horizontal minimum of a vector.
  734. */
  735. ASTCENC_SIMD_INLINE vfloat8 hmin(vfloat8 a)
  736. {
  737. __m128 vlow = _mm256_castps256_ps128(a.m);
  738. __m128 vhigh = _mm256_extractf128_ps(a.m, 1);
  739. vlow = _mm_min_ps(vlow, vhigh);
  740. // First do an horizontal reduction.
  741. __m128 shuf = _mm_shuffle_ps(vlow, vlow, _MM_SHUFFLE(2, 3, 0, 1));
  742. __m128 mins = _mm_min_ps(vlow, shuf);
  743. shuf = _mm_movehl_ps(shuf, mins);
  744. mins = _mm_min_ss(mins, shuf);
  745. // This is the most logical implementation, but the convenience intrinsic
  746. // is missing on older compilers (supported in g++ 9 and clang++ 9).
  747. //__m256i r = _mm256_set_m128(m, m)
  748. __m256 r = _mm256_insertf128_ps(_mm256_castps128_ps256(mins), mins, 1);
  749. return vfloat8(_mm256_permute_ps(r, 0));
  750. }
  751. /**
  752. * @brief Return the horizontal minimum of a vector.
  753. */
  754. ASTCENC_SIMD_INLINE float hmin_s(vfloat8 a)
  755. {
  756. return hmin(a).lane<0>();
  757. }
  758. /**
  759. * @brief Return the horizontal maximum of a vector.
  760. */
  761. ASTCENC_SIMD_INLINE vfloat8 hmax(vfloat8 a)
  762. {
  763. __m128 vlow = _mm256_castps256_ps128(a.m);
  764. __m128 vhigh = _mm256_extractf128_ps(a.m, 1);
  765. vhigh = _mm_max_ps(vlow, vhigh);
  766. // First do an horizontal reduction.
  767. __m128 shuf = _mm_shuffle_ps(vhigh, vhigh, _MM_SHUFFLE(2, 3, 0, 1));
  768. __m128 maxs = _mm_max_ps(vhigh, shuf);
  769. shuf = _mm_movehl_ps(shuf,maxs);
  770. maxs = _mm_max_ss(maxs, shuf);
  771. // This is the most logical implementation, but the convenience intrinsic
  772. // is missing on older compilers (supported in g++ 9 and clang++ 9).
  773. //__m256i r = _mm256_set_m128(m, m)
  774. __m256 r = _mm256_insertf128_ps(_mm256_castps128_ps256(maxs), maxs, 1);
  775. return vfloat8(_mm256_permute_ps(r, 0));
  776. }
  777. /**
  778. * @brief Return the horizontal maximum of a vector.
  779. */
  780. ASTCENC_SIMD_INLINE float hmax_s(vfloat8 a)
  781. {
  782. return hmax(a).lane<0>();
  783. }
  784. /**
  785. * @brief Return the horizontal sum of a vector.
  786. */
  787. ASTCENC_SIMD_INLINE float hadd_s(vfloat8 a)
  788. {
  789. // Two sequential 4-wide adds gives invariance with 4-wide code
  790. vfloat4 lo(_mm256_extractf128_ps(a.m, 0));
  791. vfloat4 hi(_mm256_extractf128_ps(a.m, 1));
  792. return hadd_s(lo) + hadd_s(hi);
  793. }
  794. /**
  795. * @brief Return lanes from @c b if @c cond is set, else @c a.
  796. */
  797. ASTCENC_SIMD_INLINE vfloat8 select(vfloat8 a, vfloat8 b, vmask8 cond)
  798. {
  799. return vfloat8(_mm256_blendv_ps(a.m, b.m, cond.m));
  800. }
  801. /**
  802. * @brief Return lanes from @c b if MSB of @c cond is set, else @c a.
  803. */
  804. ASTCENC_SIMD_INLINE vfloat8 select_msb(vfloat8 a, vfloat8 b, vmask8 cond)
  805. {
  806. return vfloat8(_mm256_blendv_ps(a.m, b.m, cond.m));
  807. }
  808. /**
  809. * @brief Accumulate lane-wise sums for a vector, folded 4-wide.
  810. *
  811. * This is invariant with 4-wide implementations.
  812. */
  813. ASTCENC_SIMD_INLINE void haccumulate(vfloat4& accum, vfloat8 a)
  814. {
  815. vfloat4 lo(_mm256_extractf128_ps(a.m, 0));
  816. haccumulate(accum, lo);
  817. vfloat4 hi(_mm256_extractf128_ps(a.m, 1));
  818. haccumulate(accum, hi);
  819. }
  820. /**
  821. * @brief Accumulate lane-wise sums for a vector.
  822. *
  823. * This is NOT invariant with 4-wide implementations.
  824. */
  825. ASTCENC_SIMD_INLINE void haccumulate(vfloat8& accum, vfloat8 a)
  826. {
  827. accum += a;
  828. }
  829. /**
  830. * @brief Accumulate masked lane-wise sums for a vector, folded 4-wide.
  831. *
  832. * This is invariant with 4-wide implementations.
  833. */
  834. ASTCENC_SIMD_INLINE void haccumulate(vfloat4& accum, vfloat8 a, vmask8 m)
  835. {
  836. a = select(vfloat8::zero(), a, m);
  837. haccumulate(accum, a);
  838. }
  839. /**
  840. * @brief Accumulate masked lane-wise sums for a vector.
  841. *
  842. * This is NOT invariant with 4-wide implementations.
  843. */
  844. ASTCENC_SIMD_INLINE void haccumulate(vfloat8& accum, vfloat8 a, vmask8 m)
  845. {
  846. a = select(vfloat8::zero(), a, m);
  847. haccumulate(accum, a);
  848. }
  849. /**
  850. * @brief Return the sqrt of the lanes in the vector.
  851. */
  852. ASTCENC_SIMD_INLINE vfloat8 sqrt(vfloat8 a)
  853. {
  854. return vfloat8(_mm256_sqrt_ps(a.m));
  855. }
  856. /**
  857. * @brief Load a vector of gathered results from an array;
  858. */
  859. ASTCENC_SIMD_INLINE vfloat8 gatherf(const float* base, vint8 indices)
  860. {
  861. return vfloat8(_mm256_i32gather_ps(base, indices.m, 4));
  862. }
  863. /**
  864. * @brief Store a vector to an unaligned memory address.
  865. */
  866. ASTCENC_SIMD_INLINE void store(vfloat8 a, float* p)
  867. {
  868. _mm256_storeu_ps(p, a.m);
  869. }
  870. /**
  871. * @brief Store a vector to a 32B aligned memory address.
  872. */
  873. ASTCENC_SIMD_INLINE void storea(vfloat8 a, float* p)
  874. {
  875. _mm256_store_ps(p, a.m);
  876. }
  877. /**
  878. * @brief Return a integer value for a float vector, using truncation.
  879. */
  880. ASTCENC_SIMD_INLINE vint8 float_to_int(vfloat8 a)
  881. {
  882. return vint8(_mm256_cvttps_epi32(a.m));
  883. }
  884. /**
  885. * @brief Return a integer value for a float vector, using round-to-nearest.
  886. */
  887. ASTCENC_SIMD_INLINE vint8 float_to_int_rtn(vfloat8 a)
  888. {
  889. a = a + vfloat8(0.5f);
  890. return vint8(_mm256_cvttps_epi32(a.m));
  891. }
  892. /**
  893. * @brief Return a float value for an integer vector.
  894. */
  895. ASTCENC_SIMD_INLINE vfloat8 int_to_float(vint8 a)
  896. {
  897. return vfloat8(_mm256_cvtepi32_ps(a.m));
  898. }
  899. /**
  900. * @brief Return a float value as an integer bit pattern (i.e. no conversion).
  901. *
  902. * It is a common trick to convert floats into integer bit patterns, perform
  903. * some bit hackery based on knowledge they are IEEE 754 layout, and then
  904. * convert them back again. This is the first half of that flip.
  905. */
  906. ASTCENC_SIMD_INLINE vint8 float_as_int(vfloat8 a)
  907. {
  908. return vint8(_mm256_castps_si256(a.m));
  909. }
  910. /**
  911. * @brief Return a integer value as a float bit pattern (i.e. no conversion).
  912. *
  913. * It is a common trick to convert floats into integer bit patterns, perform
  914. * some bit hackery based on knowledge they are IEEE 754 layout, and then
  915. * convert them back again. This is the second half of that flip.
  916. */
  917. ASTCENC_SIMD_INLINE vfloat8 int_as_float(vint8 a)
  918. {
  919. return vfloat8(_mm256_castsi256_ps(a.m));
  920. }
  921. /**
  922. * @brief Prepare a vtable lookup table for use with the native SIMD size.
  923. */
  924. ASTCENC_SIMD_INLINE void vtable_prepare(vint4 t0, vint8& t0p)
  925. {
  926. // AVX2 duplicates the table within each 128-bit lane
  927. __m128i t0n = t0.m;
  928. t0p = vint8(astcenc_mm256_set_m128i(t0n, t0n));
  929. }
  930. /**
  931. * @brief Prepare a vtable lookup table for use with the native SIMD size.
  932. */
  933. ASTCENC_SIMD_INLINE void vtable_prepare(vint4 t0, vint4 t1, vint8& t0p, vint8& t1p)
  934. {
  935. // AVX2 duplicates the table within each 128-bit lane
  936. __m128i t0n = t0.m;
  937. t0p = vint8(astcenc_mm256_set_m128i(t0n, t0n));
  938. __m128i t1n = _mm_xor_si128(t0.m, t1.m);
  939. t1p = vint8(astcenc_mm256_set_m128i(t1n, t1n));
  940. }
  941. /**
  942. * @brief Prepare a vtable lookup table for use with the native SIMD size.
  943. */
  944. ASTCENC_SIMD_INLINE void vtable_prepare(
  945. vint4 t0, vint4 t1, vint4 t2, vint4 t3,
  946. vint8& t0p, vint8& t1p, vint8& t2p, vint8& t3p)
  947. {
  948. // AVX2 duplicates the table within each 128-bit lane
  949. __m128i t0n = t0.m;
  950. t0p = vint8(astcenc_mm256_set_m128i(t0n, t0n));
  951. __m128i t1n = _mm_xor_si128(t0.m, t1.m);
  952. t1p = vint8(astcenc_mm256_set_m128i(t1n, t1n));
  953. __m128i t2n = _mm_xor_si128(t1.m, t2.m);
  954. t2p = vint8(astcenc_mm256_set_m128i(t2n, t2n));
  955. __m128i t3n = _mm_xor_si128(t2.m, t3.m);
  956. t3p = vint8(astcenc_mm256_set_m128i(t3n, t3n));
  957. }
  958. /**
  959. * @brief Perform an 8-bit 16-entry table lookup, with 32-bit indexes.
  960. */
  961. ASTCENC_SIMD_INLINE vint8 vtable_8bt_32bi(vint8 t0, vint8 idx)
  962. {
  963. // Set index byte MSB to 1 for unused bytes so shuffle returns zero
  964. __m256i idxx = _mm256_or_si256(idx.m, _mm256_set1_epi32(static_cast<int>(0xFFFFFF00)));
  965. __m256i result = _mm256_shuffle_epi8(t0.m, idxx);
  966. return vint8(result);
  967. }
  968. /**
  969. * @brief Perform an 8-bit 32-entry table lookup, with 32-bit indexes.
  970. */
  971. ASTCENC_SIMD_INLINE vint8 vtable_8bt_32bi(vint8 t0, vint8 t1, vint8 idx)
  972. {
  973. // Set index byte MSB to 1 for unused bytes so shuffle returns zero
  974. __m256i idxx = _mm256_or_si256(idx.m, _mm256_set1_epi32(static_cast<int>(0xFFFFFF00)));
  975. __m256i result = _mm256_shuffle_epi8(t0.m, idxx);
  976. idxx = _mm256_sub_epi8(idxx, _mm256_set1_epi8(16));
  977. __m256i result2 = _mm256_shuffle_epi8(t1.m, idxx);
  978. result = _mm256_xor_si256(result, result2);
  979. return vint8(result);
  980. }
  981. /**
  982. * @brief Perform an 8-bit 64-entry table lookup, with 32-bit indexes.
  983. */
  984. ASTCENC_SIMD_INLINE vint8 vtable_8bt_32bi(vint8 t0, vint8 t1, vint8 t2, vint8 t3, vint8 idx)
  985. {
  986. // Set index byte MSB to 1 for unused bytes so shuffle returns zero
  987. __m256i idxx = _mm256_or_si256(idx.m, _mm256_set1_epi32(static_cast<int>(0xFFFFFF00)));
  988. __m256i result = _mm256_shuffle_epi8(t0.m, idxx);
  989. idxx = _mm256_sub_epi8(idxx, _mm256_set1_epi8(16));
  990. __m256i result2 = _mm256_shuffle_epi8(t1.m, idxx);
  991. result = _mm256_xor_si256(result, result2);
  992. idxx = _mm256_sub_epi8(idxx, _mm256_set1_epi8(16));
  993. result2 = _mm256_shuffle_epi8(t2.m, idxx);
  994. result = _mm256_xor_si256(result, result2);
  995. idxx = _mm256_sub_epi8(idxx, _mm256_set1_epi8(16));
  996. result2 = _mm256_shuffle_epi8(t3.m, idxx);
  997. result = _mm256_xor_si256(result, result2);
  998. return vint8(result);
  999. }
  1000. /**
  1001. * @brief Return a vector of interleaved RGBA data.
  1002. *
  1003. * Input vectors have the value stored in the bottom 8 bits of each lane,
  1004. * with high bits set to zero.
  1005. *
  1006. * Output vector stores a single RGBA texel packed in each lane.
  1007. */
  1008. ASTCENC_SIMD_INLINE vint8 interleave_rgba8(vint8 r, vint8 g, vint8 b, vint8 a)
  1009. {
  1010. return r + lsl<8>(g) + lsl<16>(b) + lsl<24>(a);
  1011. }
  1012. /**
  1013. * @brief Store a vector, skipping masked lanes.
  1014. *
  1015. * All masked lanes must be at the end of vector, after all non-masked lanes.
  1016. */
  1017. ASTCENC_SIMD_INLINE void store_lanes_masked(uint8_t* base, vint8 data, vmask8 mask)
  1018. {
  1019. _mm256_maskstore_epi32(reinterpret_cast<int*>(base), _mm256_castps_si256(mask.m), data.m);
  1020. }
  1021. /**
  1022. * @brief Debug function to print a vector of ints.
  1023. */
  1024. ASTCENC_SIMD_INLINE void print(vint8 a)
  1025. {
  1026. alignas(32) int v[8];
  1027. storea(a, v);
  1028. printf("v8_i32:\n %8d %8d %8d %8d %8d %8d %8d %8d\n",
  1029. v[0], v[1], v[2], v[3], v[4], v[5], v[6], v[7]);
  1030. }
  1031. /**
  1032. * @brief Debug function to print a vector of ints.
  1033. */
  1034. ASTCENC_SIMD_INLINE void printx(vint8 a)
  1035. {
  1036. alignas(32) int v[8];
  1037. storea(a, v);
  1038. printf("v8_i32:\n %08x %08x %08x %08x %08x %08x %08x %08x\n",
  1039. v[0], v[1], v[2], v[3], v[4], v[5], v[6], v[7]);
  1040. }
  1041. /**
  1042. * @brief Debug function to print a vector of floats.
  1043. */
  1044. ASTCENC_SIMD_INLINE void print(vfloat8 a)
  1045. {
  1046. alignas(32) float v[8];
  1047. storea(a, v);
  1048. printf("v8_f32:\n %0.4f %0.4f %0.4f %0.4f %0.4f %0.4f %0.4f %0.4f\n",
  1049. static_cast<double>(v[0]), static_cast<double>(v[1]),
  1050. static_cast<double>(v[2]), static_cast<double>(v[3]),
  1051. static_cast<double>(v[4]), static_cast<double>(v[5]),
  1052. static_cast<double>(v[6]), static_cast<double>(v[7]));
  1053. }
  1054. /**
  1055. * @brief Debug function to print a vector of masks.
  1056. */
  1057. ASTCENC_SIMD_INLINE void print(vmask8 a)
  1058. {
  1059. print(select(vint8(0), vint8(1), a));
  1060. }
  1061. #endif // #ifndef ASTC_VECMATHLIB_AVX2_8_H_INCLUDED