astcenc_vecmathlib_sse_4.h 32 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316
  1. // SPDX-License-Identifier: Apache-2.0
  2. // ----------------------------------------------------------------------------
  3. // Copyright 2019-2023 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 4x32-bit vectors, implemented using SSE.
  19. *
  20. * This module implements 4-wide 32-bit float, int, and mask vectors for x86
  21. * SSE. The implementation requires at least SSE2, but higher levels of SSE can
  22. * be selected at compile time to improve performance.
  23. *
  24. * There is a baseline level of functionality provided by all vector widths and
  25. * implementations. This is implemented using identical function signatures,
  26. * modulo data type, so we can use them as substitutable implementations in VLA
  27. * code.
  28. *
  29. * The 4-wide vectors are also used as a fixed-width type, and significantly
  30. * extend the functionality above that available to VLA code.
  31. */
  32. #ifndef ASTC_VECMATHLIB_SSE_4_H_INCLUDED
  33. #define ASTC_VECMATHLIB_SSE_4_H_INCLUDED
  34. #ifndef ASTCENC_SIMD_INLINE
  35. #error "Include astcenc_vecmathlib.h, do not include directly"
  36. #endif
  37. #include <cstdio>
  38. #include <cstring>
  39. // ============================================================================
  40. // vfloat4 data type
  41. // ============================================================================
  42. /**
  43. * @brief Data type for 4-wide floats.
  44. */
  45. struct vfloat4
  46. {
  47. /**
  48. * @brief Construct from zero-initialized value.
  49. */
  50. ASTCENC_SIMD_INLINE vfloat4() = default;
  51. /**
  52. * @brief Construct from 4 values loaded from an unaligned address.
  53. *
  54. * Consider using loada() which is better with vectors if data is aligned
  55. * to vector length.
  56. */
  57. ASTCENC_SIMD_INLINE explicit vfloat4(const float *p)
  58. {
  59. m = _mm_loadu_ps(p);
  60. }
  61. /**
  62. * @brief Construct from 1 scalar value replicated across all lanes.
  63. *
  64. * Consider using zero() for constexpr zeros.
  65. */
  66. ASTCENC_SIMD_INLINE explicit vfloat4(float a)
  67. {
  68. m = _mm_set1_ps(a);
  69. }
  70. /**
  71. * @brief Construct from 4 scalar values.
  72. *
  73. * The value of @c a is stored to lane 0 (LSB) in the SIMD register.
  74. */
  75. ASTCENC_SIMD_INLINE explicit vfloat4(float a, float b, float c, float d)
  76. {
  77. m = _mm_set_ps(d, c, b, a);
  78. }
  79. /**
  80. * @brief Construct from an existing SIMD register.
  81. */
  82. ASTCENC_SIMD_INLINE explicit vfloat4(__m128 a)
  83. {
  84. m = a;
  85. }
  86. /**
  87. * @brief Get the scalar value of a single lane.
  88. */
  89. template <int l> ASTCENC_SIMD_INLINE float lane() const
  90. {
  91. return _mm_cvtss_f32(_mm_shuffle_ps(m, m, l));
  92. }
  93. /**
  94. * @brief Set the scalar value of a single lane.
  95. */
  96. template <int l> ASTCENC_SIMD_INLINE void set_lane(float a)
  97. {
  98. #if ASTCENC_SSE >= 41
  99. __m128 v = _mm_set1_ps(a);
  100. m = _mm_insert_ps(m, v, l << 6 | l << 4);
  101. #else
  102. alignas(16) float idx[4];
  103. _mm_store_ps(idx, m);
  104. idx[l] = a;
  105. m = _mm_load_ps(idx);
  106. #endif
  107. }
  108. /**
  109. * @brief Factory that returns a vector of zeros.
  110. */
  111. static ASTCENC_SIMD_INLINE vfloat4 zero()
  112. {
  113. return vfloat4(_mm_setzero_ps());
  114. }
  115. /**
  116. * @brief Factory that returns a replicated scalar loaded from memory.
  117. */
  118. static ASTCENC_SIMD_INLINE vfloat4 load1(const float* p)
  119. {
  120. return vfloat4(_mm_load_ps1(p));
  121. }
  122. /**
  123. * @brief Factory that returns a vector loaded from 16B aligned memory.
  124. */
  125. static ASTCENC_SIMD_INLINE vfloat4 loada(const float* p)
  126. {
  127. return vfloat4(_mm_load_ps(p));
  128. }
  129. /**
  130. * @brief Factory that returns a vector containing the lane IDs.
  131. */
  132. static ASTCENC_SIMD_INLINE vfloat4 lane_id()
  133. {
  134. return vfloat4(_mm_set_ps(3, 2, 1, 0));
  135. }
  136. /**
  137. * @brief Return a swizzled float 2.
  138. */
  139. template <int l0, int l1> ASTCENC_SIMD_INLINE vfloat4 swz() const
  140. {
  141. vfloat4 result(_mm_shuffle_ps(m, m, l0 | l1 << 2));
  142. result.set_lane<2>(0.0f);
  143. result.set_lane<3>(0.0f);
  144. return result;
  145. }
  146. /**
  147. * @brief Return a swizzled float 3.
  148. */
  149. template <int l0, int l1, int l2> ASTCENC_SIMD_INLINE vfloat4 swz() const
  150. {
  151. vfloat4 result(_mm_shuffle_ps(m, m, l0 | l1 << 2 | l2 << 4));
  152. result.set_lane<3>(0.0f);
  153. return result;
  154. }
  155. /**
  156. * @brief Return a swizzled float 4.
  157. */
  158. template <int l0, int l1, int l2, int l3> ASTCENC_SIMD_INLINE vfloat4 swz() const
  159. {
  160. return vfloat4(_mm_shuffle_ps(m, m, l0 | l1 << 2 | l2 << 4 | l3 << 6));
  161. }
  162. /**
  163. * @brief The vector ...
  164. */
  165. __m128 m;
  166. };
  167. // ============================================================================
  168. // vint4 data type
  169. // ============================================================================
  170. /**
  171. * @brief Data type for 4-wide ints.
  172. */
  173. struct vint4
  174. {
  175. /**
  176. * @brief Construct from zero-initialized value.
  177. */
  178. ASTCENC_SIMD_INLINE vint4() = default;
  179. /**
  180. * @brief Construct from 4 values loaded from an unaligned address.
  181. *
  182. * Consider using loada() which is better with vectors if data is aligned
  183. * to vector length.
  184. */
  185. ASTCENC_SIMD_INLINE explicit vint4(const int *p)
  186. {
  187. m = _mm_loadu_si128(reinterpret_cast<const __m128i*>(p));
  188. }
  189. /**
  190. * @brief Construct from 4 uint8_t loaded from an unaligned address.
  191. */
  192. ASTCENC_SIMD_INLINE explicit vint4(const uint8_t *p)
  193. {
  194. // _mm_loadu_si32 would be nicer syntax, but missing on older GCC
  195. __m128i t = _mm_cvtsi32_si128(*reinterpret_cast<const int*>(p));
  196. #if ASTCENC_SSE >= 41
  197. m = _mm_cvtepu8_epi32(t);
  198. #else
  199. t = _mm_unpacklo_epi8(t, _mm_setzero_si128());
  200. m = _mm_unpacklo_epi16(t, _mm_setzero_si128());
  201. #endif
  202. }
  203. /**
  204. * @brief Construct from 1 scalar value replicated across all lanes.
  205. *
  206. * Consider using vfloat4::zero() for constexpr zeros.
  207. */
  208. ASTCENC_SIMD_INLINE explicit vint4(int a)
  209. {
  210. m = _mm_set1_epi32(a);
  211. }
  212. /**
  213. * @brief Construct from 4 scalar values.
  214. *
  215. * The value of @c a is stored to lane 0 (LSB) in the SIMD register.
  216. */
  217. ASTCENC_SIMD_INLINE explicit vint4(int a, int b, int c, int d)
  218. {
  219. m = _mm_set_epi32(d, c, b, a);
  220. }
  221. /**
  222. * @brief Construct from an existing SIMD register.
  223. */
  224. ASTCENC_SIMD_INLINE explicit vint4(__m128i a)
  225. {
  226. m = a;
  227. }
  228. /**
  229. * @brief Get the scalar from a single lane.
  230. */
  231. template <int l> ASTCENC_SIMD_INLINE int lane() const
  232. {
  233. return _mm_cvtsi128_si32(_mm_shuffle_epi32(m, l));
  234. }
  235. /**
  236. * @brief Set the scalar value of a single lane.
  237. */
  238. template <int l> ASTCENC_SIMD_INLINE void set_lane(int a)
  239. {
  240. #if ASTCENC_SSE >= 41
  241. m = _mm_insert_epi32(m, a, l);
  242. #else
  243. alignas(16) int idx[4];
  244. _mm_store_si128(reinterpret_cast<__m128i*>(idx), m);
  245. idx[l] = a;
  246. m = _mm_load_si128(reinterpret_cast<const __m128i*>(idx));
  247. #endif
  248. }
  249. /**
  250. * @brief Factory that returns a vector of zeros.
  251. */
  252. static ASTCENC_SIMD_INLINE vint4 zero()
  253. {
  254. return vint4(_mm_setzero_si128());
  255. }
  256. /**
  257. * @brief Factory that returns a replicated scalar loaded from memory.
  258. */
  259. static ASTCENC_SIMD_INLINE vint4 load1(const int* p)
  260. {
  261. return vint4(*p);
  262. }
  263. /**
  264. * @brief Factory that returns a vector loaded from unaligned memory.
  265. */
  266. static ASTCENC_SIMD_INLINE vint4 load(const uint8_t* p)
  267. {
  268. #if ASTCENC_SSE >= 41
  269. return vint4(_mm_lddqu_si128(reinterpret_cast<const __m128i*>(p)));
  270. #else
  271. return vint4(_mm_loadu_si128(reinterpret_cast<const __m128i*>(p)));
  272. #endif
  273. }
  274. /**
  275. * @brief Factory that returns a vector loaded from 16B aligned memory.
  276. */
  277. static ASTCENC_SIMD_INLINE vint4 loada(const int* p)
  278. {
  279. return vint4(_mm_load_si128(reinterpret_cast<const __m128i*>(p)));
  280. }
  281. /**
  282. * @brief Factory that returns a vector containing the lane IDs.
  283. */
  284. static ASTCENC_SIMD_INLINE vint4 lane_id()
  285. {
  286. return vint4(_mm_set_epi32(3, 2, 1, 0));
  287. }
  288. /**
  289. * @brief The vector ...
  290. */
  291. __m128i m;
  292. };
  293. // ============================================================================
  294. // vmask4 data type
  295. // ============================================================================
  296. /**
  297. * @brief Data type for 4-wide control plane masks.
  298. */
  299. struct vmask4
  300. {
  301. /**
  302. * @brief Construct from an existing SIMD register.
  303. */
  304. ASTCENC_SIMD_INLINE explicit vmask4(__m128 a)
  305. {
  306. m = a;
  307. }
  308. /**
  309. * @brief Construct from an existing SIMD register.
  310. */
  311. ASTCENC_SIMD_INLINE explicit vmask4(__m128i a)
  312. {
  313. m = _mm_castsi128_ps(a);
  314. }
  315. /**
  316. * @brief Construct from 1 scalar value.
  317. */
  318. ASTCENC_SIMD_INLINE explicit vmask4(bool a)
  319. {
  320. vint4 mask(a == false ? 0 : -1);
  321. m = _mm_castsi128_ps(mask.m);
  322. }
  323. /**
  324. * @brief Construct from 4 scalar values.
  325. *
  326. * The value of @c a is stored to lane 0 (LSB) in the SIMD register.
  327. */
  328. ASTCENC_SIMD_INLINE explicit vmask4(bool a, bool b, bool c, bool d)
  329. {
  330. vint4 mask(a == false ? 0 : -1,
  331. b == false ? 0 : -1,
  332. c == false ? 0 : -1,
  333. d == false ? 0 : -1);
  334. m = _mm_castsi128_ps(mask.m);
  335. }
  336. /**
  337. * @brief Get the scalar value of a single lane.
  338. */
  339. template <int l> ASTCENC_SIMD_INLINE bool lane() const
  340. {
  341. return _mm_cvtss_f32(_mm_shuffle_ps(m, m, l)) != 0.0f;
  342. }
  343. /**
  344. * @brief The vector ...
  345. */
  346. __m128 m;
  347. };
  348. // ============================================================================
  349. // vmask4 operators and functions
  350. // ============================================================================
  351. /**
  352. * @brief Overload: mask union (or).
  353. */
  354. ASTCENC_SIMD_INLINE vmask4 operator|(vmask4 a, vmask4 b)
  355. {
  356. return vmask4(_mm_or_ps(a.m, b.m));
  357. }
  358. /**
  359. * @brief Overload: mask intersect (and).
  360. */
  361. ASTCENC_SIMD_INLINE vmask4 operator&(vmask4 a, vmask4 b)
  362. {
  363. return vmask4(_mm_and_ps(a.m, b.m));
  364. }
  365. /**
  366. * @brief Overload: mask difference (xor).
  367. */
  368. ASTCENC_SIMD_INLINE vmask4 operator^(vmask4 a, vmask4 b)
  369. {
  370. return vmask4(_mm_xor_ps(a.m, b.m));
  371. }
  372. /**
  373. * @brief Overload: mask invert (not).
  374. */
  375. ASTCENC_SIMD_INLINE vmask4 operator~(vmask4 a)
  376. {
  377. return vmask4(_mm_xor_si128(_mm_castps_si128(a.m), _mm_set1_epi32(-1)));
  378. }
  379. /**
  380. * @brief Return a 4-bit mask code indicating mask status.
  381. *
  382. * bit0 = lane 0
  383. */
  384. ASTCENC_SIMD_INLINE unsigned int mask(vmask4 a)
  385. {
  386. return static_cast<unsigned int>(_mm_movemask_ps(a.m));
  387. }
  388. // ============================================================================
  389. // vint4 operators and functions
  390. // ============================================================================
  391. /**
  392. * @brief Overload: vector by vector addition.
  393. */
  394. ASTCENC_SIMD_INLINE vint4 operator+(vint4 a, vint4 b)
  395. {
  396. return vint4(_mm_add_epi32(a.m, b.m));
  397. }
  398. /**
  399. * @brief Overload: vector by vector subtraction.
  400. */
  401. ASTCENC_SIMD_INLINE vint4 operator-(vint4 a, vint4 b)
  402. {
  403. return vint4(_mm_sub_epi32(a.m, b.m));
  404. }
  405. /**
  406. * @brief Overload: vector by vector multiplication.
  407. */
  408. ASTCENC_SIMD_INLINE vint4 operator*(vint4 a, vint4 b)
  409. {
  410. #if ASTCENC_SSE >= 41
  411. return vint4(_mm_mullo_epi32 (a.m, b.m));
  412. #else
  413. __m128i t1 = _mm_mul_epu32(a.m, b.m);
  414. __m128i t2 = _mm_mul_epu32(
  415. _mm_srli_si128(a.m, 4),
  416. _mm_srli_si128(b.m, 4));
  417. __m128i r = _mm_unpacklo_epi32(
  418. _mm_shuffle_epi32(t1, _MM_SHUFFLE (0, 0, 2, 0)),
  419. _mm_shuffle_epi32(t2, _MM_SHUFFLE (0, 0, 2, 0)));
  420. return vint4(r);
  421. #endif
  422. }
  423. /**
  424. * @brief Overload: vector bit invert.
  425. */
  426. ASTCENC_SIMD_INLINE vint4 operator~(vint4 a)
  427. {
  428. return vint4(_mm_xor_si128(a.m, _mm_set1_epi32(-1)));
  429. }
  430. /**
  431. * @brief Overload: vector by vector bitwise or.
  432. */
  433. ASTCENC_SIMD_INLINE vint4 operator|(vint4 a, vint4 b)
  434. {
  435. return vint4(_mm_or_si128(a.m, b.m));
  436. }
  437. /**
  438. * @brief Overload: vector by vector bitwise and.
  439. */
  440. ASTCENC_SIMD_INLINE vint4 operator&(vint4 a, vint4 b)
  441. {
  442. return vint4(_mm_and_si128(a.m, b.m));
  443. }
  444. /**
  445. * @brief Overload: vector by vector bitwise xor.
  446. */
  447. ASTCENC_SIMD_INLINE vint4 operator^(vint4 a, vint4 b)
  448. {
  449. return vint4(_mm_xor_si128(a.m, b.m));
  450. }
  451. /**
  452. * @brief Overload: vector by vector equality.
  453. */
  454. ASTCENC_SIMD_INLINE vmask4 operator==(vint4 a, vint4 b)
  455. {
  456. return vmask4(_mm_cmpeq_epi32(a.m, b.m));
  457. }
  458. /**
  459. * @brief Overload: vector by vector inequality.
  460. */
  461. ASTCENC_SIMD_INLINE vmask4 operator!=(vint4 a, vint4 b)
  462. {
  463. return ~vmask4(_mm_cmpeq_epi32(a.m, b.m));
  464. }
  465. /**
  466. * @brief Overload: vector by vector less than.
  467. */
  468. ASTCENC_SIMD_INLINE vmask4 operator<(vint4 a, vint4 b)
  469. {
  470. return vmask4(_mm_cmplt_epi32(a.m, b.m));
  471. }
  472. /**
  473. * @brief Overload: vector by vector greater than.
  474. */
  475. ASTCENC_SIMD_INLINE vmask4 operator>(vint4 a, vint4 b)
  476. {
  477. return vmask4(_mm_cmpgt_epi32(a.m, b.m));
  478. }
  479. /**
  480. * @brief Logical shift left.
  481. */
  482. template <int s> ASTCENC_SIMD_INLINE vint4 lsl(vint4 a)
  483. {
  484. return vint4(_mm_slli_epi32(a.m, s));
  485. }
  486. /**
  487. * @brief Logical shift right.
  488. */
  489. template <int s> ASTCENC_SIMD_INLINE vint4 lsr(vint4 a)
  490. {
  491. return vint4(_mm_srli_epi32(a.m, s));
  492. }
  493. /**
  494. * @brief Arithmetic shift right.
  495. */
  496. template <int s> ASTCENC_SIMD_INLINE vint4 asr(vint4 a)
  497. {
  498. return vint4(_mm_srai_epi32(a.m, s));
  499. }
  500. /**
  501. * @brief Return the min vector of two vectors.
  502. */
  503. ASTCENC_SIMD_INLINE vint4 min(vint4 a, vint4 b)
  504. {
  505. #if ASTCENC_SSE >= 41
  506. return vint4(_mm_min_epi32(a.m, b.m));
  507. #else
  508. vmask4 d = a < b;
  509. __m128i ap = _mm_and_si128(_mm_castps_si128(d.m), a.m);
  510. __m128i bp = _mm_andnot_si128(_mm_castps_si128(d.m), b.m);
  511. return vint4(_mm_or_si128(ap,bp));
  512. #endif
  513. }
  514. /**
  515. * @brief Return the max vector of two vectors.
  516. */
  517. ASTCENC_SIMD_INLINE vint4 max(vint4 a, vint4 b)
  518. {
  519. #if ASTCENC_SSE >= 41
  520. return vint4(_mm_max_epi32(a.m, b.m));
  521. #else
  522. vmask4 d = a > b;
  523. __m128i ap = _mm_and_si128(_mm_castps_si128(d.m), a.m);
  524. __m128i bp = _mm_andnot_si128(_mm_castps_si128(d.m), b.m);
  525. return vint4(_mm_or_si128(ap,bp));
  526. #endif
  527. }
  528. /**
  529. * @brief Return the horizontal minimum of a vector.
  530. */
  531. ASTCENC_SIMD_INLINE vint4 hmin(vint4 a)
  532. {
  533. a = min(a, vint4(_mm_shuffle_epi32(a.m, _MM_SHUFFLE(0, 0, 3, 2))));
  534. a = min(a, vint4(_mm_shuffle_epi32(a.m, _MM_SHUFFLE(0, 0, 0, 1))));
  535. return vint4(_mm_shuffle_epi32(a.m, _MM_SHUFFLE(0, 0, 0, 0)));
  536. }
  537. /*
  538. * @brief Return the horizontal maximum of a vector.
  539. */
  540. ASTCENC_SIMD_INLINE vint4 hmax(vint4 a)
  541. {
  542. a = max(a, vint4(_mm_shuffle_epi32(a.m, _MM_SHUFFLE(0, 0, 3, 2))));
  543. a = max(a, vint4(_mm_shuffle_epi32(a.m, _MM_SHUFFLE(0, 0, 0, 1))));
  544. return vint4(_mm_shuffle_epi32(a.m, _MM_SHUFFLE(0, 0, 0, 0)));
  545. }
  546. /**
  547. * @brief Return the horizontal sum of a vector as a scalar.
  548. */
  549. ASTCENC_SIMD_INLINE int hadd_s(vint4 a)
  550. {
  551. // Add top and bottom halves, lane 1/0
  552. __m128i fold = _mm_castps_si128(_mm_movehl_ps(_mm_castsi128_ps(a.m),
  553. _mm_castsi128_ps(a.m)));
  554. __m128i t = _mm_add_epi32(a.m, fold);
  555. // Add top and bottom halves, lane 0 (_mm_hadd_ps exists but slow)
  556. t = _mm_add_epi32(t, _mm_shuffle_epi32(t, 0x55));
  557. return _mm_cvtsi128_si32(t);
  558. }
  559. /**
  560. * @brief Store a vector to a 16B aligned memory address.
  561. */
  562. ASTCENC_SIMD_INLINE void storea(vint4 a, int* p)
  563. {
  564. _mm_store_si128(reinterpret_cast<__m128i*>(p), a.m);
  565. }
  566. /**
  567. * @brief Store a vector to an unaligned memory address.
  568. */
  569. ASTCENC_SIMD_INLINE void store(vint4 a, int* p)
  570. {
  571. // Cast due to missing intrinsics
  572. _mm_storeu_ps(reinterpret_cast<float*>(p), _mm_castsi128_ps(a.m));
  573. }
  574. /**
  575. * @brief Store a vector to an unaligned memory address.
  576. */
  577. ASTCENC_SIMD_INLINE void store(vint4 a, uint8_t* p)
  578. {
  579. std::memcpy(p, &a.m, sizeof(int) * 4);
  580. }
  581. /**
  582. * @brief Store lowest N (vector width) bytes into an unaligned address.
  583. */
  584. ASTCENC_SIMD_INLINE void store_nbytes(vint4 a, uint8_t* p)
  585. {
  586. // Cast due to missing intrinsics
  587. _mm_store_ss(reinterpret_cast<float*>(p), _mm_castsi128_ps(a.m));
  588. }
  589. /**
  590. * @brief Gather N (vector width) indices from the array.
  591. */
  592. ASTCENC_SIMD_INLINE vint4 gatheri(const int* base, vint4 indices)
  593. {
  594. #if ASTCENC_AVX >= 2
  595. return vint4(_mm_i32gather_epi32(base, indices.m, 4));
  596. #else
  597. alignas(16) int idx[4];
  598. storea(indices, idx);
  599. return vint4(base[idx[0]], base[idx[1]], base[idx[2]], base[idx[3]]);
  600. #endif
  601. }
  602. /**
  603. * @brief Pack low 8 bits of N (vector width) lanes into bottom of vector.
  604. */
  605. ASTCENC_SIMD_INLINE vint4 pack_low_bytes(vint4 a)
  606. {
  607. #if ASTCENC_SSE >= 41
  608. __m128i shuf = _mm_set_epi8(0,0,0,0, 0,0,0,0, 0,0,0,0, 12,8,4,0);
  609. return vint4(_mm_shuffle_epi8(a.m, shuf));
  610. #else
  611. __m128i va = _mm_unpacklo_epi8(a.m, _mm_shuffle_epi32(a.m, _MM_SHUFFLE(1,1,1,1)));
  612. __m128i vb = _mm_unpackhi_epi8(a.m, _mm_shuffle_epi32(a.m, _MM_SHUFFLE(3,3,3,3)));
  613. return vint4(_mm_unpacklo_epi16(va, vb));
  614. #endif
  615. }
  616. /**
  617. * @brief Return lanes from @c b if @c cond is set, else @c a.
  618. */
  619. ASTCENC_SIMD_INLINE vint4 select(vint4 a, vint4 b, vmask4 cond)
  620. {
  621. __m128i condi = _mm_castps_si128(cond.m);
  622. #if ASTCENC_SSE >= 41
  623. return vint4(_mm_blendv_epi8(a.m, b.m, condi));
  624. #else
  625. return vint4(_mm_or_si128(_mm_and_si128(condi, b.m), _mm_andnot_si128(condi, a.m)));
  626. #endif
  627. }
  628. // ============================================================================
  629. // vfloat4 operators and functions
  630. // ============================================================================
  631. /**
  632. * @brief Overload: vector by vector addition.
  633. */
  634. ASTCENC_SIMD_INLINE vfloat4 operator+(vfloat4 a, vfloat4 b)
  635. {
  636. return vfloat4(_mm_add_ps(a.m, b.m));
  637. }
  638. /**
  639. * @brief Overload: vector by vector subtraction.
  640. */
  641. ASTCENC_SIMD_INLINE vfloat4 operator-(vfloat4 a, vfloat4 b)
  642. {
  643. return vfloat4(_mm_sub_ps(a.m, b.m));
  644. }
  645. /**
  646. * @brief Overload: vector by vector multiplication.
  647. */
  648. ASTCENC_SIMD_INLINE vfloat4 operator*(vfloat4 a, vfloat4 b)
  649. {
  650. return vfloat4(_mm_mul_ps(a.m, b.m));
  651. }
  652. /**
  653. * @brief Overload: vector by vector division.
  654. */
  655. ASTCENC_SIMD_INLINE vfloat4 operator/(vfloat4 a, vfloat4 b)
  656. {
  657. return vfloat4(_mm_div_ps(a.m, b.m));
  658. }
  659. /**
  660. * @brief Overload: vector by vector equality.
  661. */
  662. ASTCENC_SIMD_INLINE vmask4 operator==(vfloat4 a, vfloat4 b)
  663. {
  664. return vmask4(_mm_cmpeq_ps(a.m, b.m));
  665. }
  666. /**
  667. * @brief Overload: vector by vector inequality.
  668. */
  669. ASTCENC_SIMD_INLINE vmask4 operator!=(vfloat4 a, vfloat4 b)
  670. {
  671. return vmask4(_mm_cmpneq_ps(a.m, b.m));
  672. }
  673. /**
  674. * @brief Overload: vector by vector less than.
  675. */
  676. ASTCENC_SIMD_INLINE vmask4 operator<(vfloat4 a, vfloat4 b)
  677. {
  678. return vmask4(_mm_cmplt_ps(a.m, b.m));
  679. }
  680. /**
  681. * @brief Overload: vector by vector greater than.
  682. */
  683. ASTCENC_SIMD_INLINE vmask4 operator>(vfloat4 a, vfloat4 b)
  684. {
  685. return vmask4(_mm_cmpgt_ps(a.m, b.m));
  686. }
  687. /**
  688. * @brief Overload: vector by vector less than or equal.
  689. */
  690. ASTCENC_SIMD_INLINE vmask4 operator<=(vfloat4 a, vfloat4 b)
  691. {
  692. return vmask4(_mm_cmple_ps(a.m, b.m));
  693. }
  694. /**
  695. * @brief Overload: vector by vector greater than or equal.
  696. */
  697. ASTCENC_SIMD_INLINE vmask4 operator>=(vfloat4 a, vfloat4 b)
  698. {
  699. return vmask4(_mm_cmpge_ps(a.m, b.m));
  700. }
  701. /**
  702. * @brief Return the min vector of two vectors.
  703. *
  704. * If either lane value is NaN, @c b will be returned for that lane.
  705. */
  706. ASTCENC_SIMD_INLINE vfloat4 min(vfloat4 a, vfloat4 b)
  707. {
  708. // Do not reorder - second operand will return if either is NaN
  709. return vfloat4(_mm_min_ps(a.m, b.m));
  710. }
  711. /**
  712. * @brief Return the max vector of two vectors.
  713. *
  714. * If either lane value is NaN, @c b will be returned for that lane.
  715. */
  716. ASTCENC_SIMD_INLINE vfloat4 max(vfloat4 a, vfloat4 b)
  717. {
  718. // Do not reorder - second operand will return if either is NaN
  719. return vfloat4(_mm_max_ps(a.m, b.m));
  720. }
  721. /**
  722. * @brief Return the absolute value of the float vector.
  723. */
  724. ASTCENC_SIMD_INLINE vfloat4 abs(vfloat4 a)
  725. {
  726. return vfloat4(_mm_max_ps(_mm_sub_ps(_mm_setzero_ps(), a.m), a.m));
  727. }
  728. /**
  729. * @brief Return a float rounded to the nearest integer value.
  730. */
  731. ASTCENC_SIMD_INLINE vfloat4 round(vfloat4 a)
  732. {
  733. #if ASTCENC_SSE >= 41
  734. constexpr int flags = _MM_FROUND_TO_NEAREST_INT | _MM_FROUND_NO_EXC;
  735. return vfloat4(_mm_round_ps(a.m, flags));
  736. #else
  737. __m128 v = a.m;
  738. __m128 neg_zero = _mm_castsi128_ps(_mm_set1_epi32(static_cast<int>(0x80000000)));
  739. __m128 no_fraction = _mm_set1_ps(8388608.0f);
  740. __m128 abs_mask = _mm_castsi128_ps(_mm_set1_epi32(0x7FFFFFFF));
  741. __m128 sign = _mm_and_ps(v, neg_zero);
  742. __m128 s_magic = _mm_or_ps(no_fraction, sign);
  743. __m128 r1 = _mm_add_ps(v, s_magic);
  744. r1 = _mm_sub_ps(r1, s_magic);
  745. __m128 r2 = _mm_and_ps(v, abs_mask);
  746. __m128 mask = _mm_cmple_ps(r2, no_fraction);
  747. r2 = _mm_andnot_ps(mask, v);
  748. r1 = _mm_and_ps(r1, mask);
  749. return vfloat4(_mm_xor_ps(r1, r2));
  750. #endif
  751. }
  752. /**
  753. * @brief Return the horizontal minimum of a vector.
  754. */
  755. ASTCENC_SIMD_INLINE vfloat4 hmin(vfloat4 a)
  756. {
  757. a = min(a, vfloat4(_mm_shuffle_ps(a.m, a.m, _MM_SHUFFLE(0, 0, 3, 2))));
  758. a = min(a, vfloat4(_mm_shuffle_ps(a.m, a.m, _MM_SHUFFLE(0, 0, 0, 1))));
  759. return vfloat4(_mm_shuffle_ps(a.m, a.m, _MM_SHUFFLE(0, 0, 0, 0)));
  760. }
  761. /**
  762. * @brief Return the horizontal maximum of a vector.
  763. */
  764. ASTCENC_SIMD_INLINE vfloat4 hmax(vfloat4 a)
  765. {
  766. a = max(a, vfloat4(_mm_shuffle_ps(a.m, a.m, _MM_SHUFFLE(0, 0, 3, 2))));
  767. a = max(a, vfloat4(_mm_shuffle_ps(a.m, a.m, _MM_SHUFFLE(0, 0, 0, 1))));
  768. return vfloat4(_mm_shuffle_ps(a.m, a.m, _MM_SHUFFLE(0, 0, 0, 0)));
  769. }
  770. /**
  771. * @brief Return the horizontal sum of a vector as a scalar.
  772. */
  773. ASTCENC_SIMD_INLINE float hadd_s(vfloat4 a)
  774. {
  775. // Add top and bottom halves, lane 1/0
  776. __m128 t = _mm_add_ps(a.m, _mm_movehl_ps(a.m, a.m));
  777. // Add top and bottom halves, lane 0 (_mm_hadd_ps exists but slow)
  778. t = _mm_add_ss(t, _mm_shuffle_ps(t, t, 0x55));
  779. return _mm_cvtss_f32(t);
  780. }
  781. /**
  782. * @brief Return the sqrt of the lanes in the vector.
  783. */
  784. ASTCENC_SIMD_INLINE vfloat4 sqrt(vfloat4 a)
  785. {
  786. return vfloat4(_mm_sqrt_ps(a.m));
  787. }
  788. /**
  789. * @brief Return lanes from @c b if @c cond is set, else @c a.
  790. */
  791. ASTCENC_SIMD_INLINE vfloat4 select(vfloat4 a, vfloat4 b, vmask4 cond)
  792. {
  793. #if ASTCENC_SSE >= 41
  794. return vfloat4(_mm_blendv_ps(a.m, b.m, cond.m));
  795. #else
  796. return vfloat4(_mm_or_ps(_mm_and_ps(cond.m, b.m), _mm_andnot_ps(cond.m, a.m)));
  797. #endif
  798. }
  799. /**
  800. * @brief Return lanes from @c b if MSB of @c cond is set, else @c a.
  801. */
  802. ASTCENC_SIMD_INLINE vfloat4 select_msb(vfloat4 a, vfloat4 b, vmask4 cond)
  803. {
  804. #if ASTCENC_SSE >= 41
  805. return vfloat4(_mm_blendv_ps(a.m, b.m, cond.m));
  806. #else
  807. __m128 d = _mm_castsi128_ps(_mm_srai_epi32(_mm_castps_si128(cond.m), 31));
  808. return vfloat4(_mm_or_ps(_mm_and_ps(d, b.m), _mm_andnot_ps(d, a.m)));
  809. #endif
  810. }
  811. /**
  812. * @brief Load a vector of gathered results from an array;
  813. */
  814. ASTCENC_SIMD_INLINE vfloat4 gatherf(const float* base, vint4 indices)
  815. {
  816. #if ASTCENC_AVX >= 2
  817. return vfloat4(_mm_i32gather_ps(base, indices.m, 4));
  818. #else
  819. alignas(16) int idx[4];
  820. storea(indices, idx);
  821. return vfloat4(base[idx[0]], base[idx[1]], base[idx[2]], base[idx[3]]);
  822. #endif
  823. }
  824. /**
  825. * @brief Store a vector to an unaligned memory address.
  826. */
  827. ASTCENC_SIMD_INLINE void store(vfloat4 a, float* p)
  828. {
  829. _mm_storeu_ps(p, a.m);
  830. }
  831. /**
  832. * @brief Store a vector to a 16B aligned memory address.
  833. */
  834. ASTCENC_SIMD_INLINE void storea(vfloat4 a, float* p)
  835. {
  836. _mm_store_ps(p, a.m);
  837. }
  838. /**
  839. * @brief Return a integer value for a float vector, using truncation.
  840. */
  841. ASTCENC_SIMD_INLINE vint4 float_to_int(vfloat4 a)
  842. {
  843. return vint4(_mm_cvttps_epi32(a.m));
  844. }
  845. /**
  846. * @brief Return a integer value for a float vector, using round-to-nearest.
  847. */
  848. ASTCENC_SIMD_INLINE vint4 float_to_int_rtn(vfloat4 a)
  849. {
  850. a = a + vfloat4(0.5f);
  851. return vint4(_mm_cvttps_epi32(a.m));
  852. }
  853. /**
  854. * @brief Return a float value for an integer vector.
  855. */
  856. ASTCENC_SIMD_INLINE vfloat4 int_to_float(vint4 a)
  857. {
  858. return vfloat4(_mm_cvtepi32_ps(a.m));
  859. }
  860. /**
  861. * @brief Return a float16 value for a float vector, using round-to-nearest.
  862. */
  863. ASTCENC_SIMD_INLINE vint4 float_to_float16(vfloat4 a)
  864. {
  865. #if ASTCENC_F16C >= 1
  866. __m128i packedf16 = _mm_cvtps_ph(a.m, 0);
  867. __m128i f16 = _mm_cvtepu16_epi32(packedf16);
  868. return vint4(f16);
  869. #else
  870. return vint4(
  871. float_to_sf16(a.lane<0>()),
  872. float_to_sf16(a.lane<1>()),
  873. float_to_sf16(a.lane<2>()),
  874. float_to_sf16(a.lane<3>()));
  875. #endif
  876. }
  877. /**
  878. * @brief Return a float16 value for a float scalar, using round-to-nearest.
  879. */
  880. static inline uint16_t float_to_float16(float a)
  881. {
  882. #if ASTCENC_F16C >= 1
  883. __m128i f16 = _mm_cvtps_ph(_mm_set1_ps(a), 0);
  884. return static_cast<uint16_t>(_mm_cvtsi128_si32(f16));
  885. #else
  886. return float_to_sf16(a);
  887. #endif
  888. }
  889. /**
  890. * @brief Return a float value for a float16 vector.
  891. */
  892. ASTCENC_SIMD_INLINE vfloat4 float16_to_float(vint4 a)
  893. {
  894. #if ASTCENC_F16C >= 1
  895. __m128i packed = _mm_packs_epi32(a.m, a.m);
  896. __m128 f32 = _mm_cvtph_ps(packed);
  897. return vfloat4(f32);
  898. #else
  899. return vfloat4(
  900. sf16_to_float(static_cast<uint16_t>(a.lane<0>())),
  901. sf16_to_float(static_cast<uint16_t>(a.lane<1>())),
  902. sf16_to_float(static_cast<uint16_t>(a.lane<2>())),
  903. sf16_to_float(static_cast<uint16_t>(a.lane<3>())));
  904. #endif
  905. }
  906. /**
  907. * @brief Return a float value for a float16 scalar.
  908. */
  909. ASTCENC_SIMD_INLINE float float16_to_float(uint16_t a)
  910. {
  911. #if ASTCENC_F16C >= 1
  912. __m128i packed = _mm_set1_epi16(static_cast<short>(a));
  913. __m128 f32 = _mm_cvtph_ps(packed);
  914. return _mm_cvtss_f32(f32);
  915. #else
  916. return sf16_to_float(a);
  917. #endif
  918. }
  919. /**
  920. * @brief Return a float value as an integer bit pattern (i.e. no conversion).
  921. *
  922. * It is a common trick to convert floats into integer bit patterns, perform
  923. * some bit hackery based on knowledge they are IEEE 754 layout, and then
  924. * convert them back again. This is the first half of that flip.
  925. */
  926. ASTCENC_SIMD_INLINE vint4 float_as_int(vfloat4 a)
  927. {
  928. return vint4(_mm_castps_si128(a.m));
  929. }
  930. /**
  931. * @brief Return a integer value as a float bit pattern (i.e. no conversion).
  932. *
  933. * It is a common trick to convert floats into integer bit patterns, perform
  934. * some bit hackery based on knowledge they are IEEE 754 layout, and then
  935. * convert them back again. This is the second half of that flip.
  936. */
  937. ASTCENC_SIMD_INLINE vfloat4 int_as_float(vint4 v)
  938. {
  939. return vfloat4(_mm_castsi128_ps(v.m));
  940. }
  941. /**
  942. * @brief Prepare a vtable lookup table for use with the native SIMD size.
  943. */
  944. ASTCENC_SIMD_INLINE void vtable_prepare(vint4 t0, vint4& t0p)
  945. {
  946. t0p = t0;
  947. }
  948. /**
  949. * @brief Prepare a vtable lookup table for use with the native SIMD size.
  950. */
  951. ASTCENC_SIMD_INLINE void vtable_prepare(vint4 t0, vint4 t1, vint4& t0p, vint4& t1p)
  952. {
  953. #if ASTCENC_SSE >= 41
  954. t0p = t0;
  955. t1p = t0 ^ t1;
  956. #else
  957. t0p = t0;
  958. t1p = t1;
  959. #endif
  960. }
  961. /**
  962. * @brief Prepare a vtable lookup table for use with the native SIMD size.
  963. */
  964. ASTCENC_SIMD_INLINE void vtable_prepare(
  965. vint4 t0, vint4 t1, vint4 t2, vint4 t3,
  966. vint4& t0p, vint4& t1p, vint4& t2p, vint4& t3p)
  967. {
  968. #if ASTCENC_SSE >= 41
  969. t0p = t0;
  970. t1p = t0 ^ t1;
  971. t2p = t1 ^ t2;
  972. t3p = t2 ^ t3;
  973. #else
  974. t0p = t0;
  975. t1p = t1;
  976. t2p = t2;
  977. t3p = t3;
  978. #endif
  979. }
  980. /**
  981. * @brief Perform an 8-bit 16-entry table lookup, with 32-bit indexes.
  982. */
  983. ASTCENC_SIMD_INLINE vint4 vtable_8bt_32bi(vint4 t0, vint4 idx)
  984. {
  985. #if ASTCENC_SSE >= 41
  986. // Set index byte MSB to 1 for unused bytes so shuffle returns zero
  987. __m128i idxx = _mm_or_si128(idx.m, _mm_set1_epi32(static_cast<int>(0xFFFFFF00)));
  988. __m128i result = _mm_shuffle_epi8(t0.m, idxx);
  989. return vint4(result);
  990. #else
  991. uint8_t table[16];
  992. std::memcpy(table + 0, &t0.m, 4 * sizeof(int));
  993. return vint4(table[idx.lane<0>()],
  994. table[idx.lane<1>()],
  995. table[idx.lane<2>()],
  996. table[idx.lane<3>()]);
  997. #endif
  998. }
  999. /**
  1000. * @brief Perform an 8-bit 32-entry table lookup, with 32-bit indexes.
  1001. */
  1002. ASTCENC_SIMD_INLINE vint4 vtable_8bt_32bi(vint4 t0, vint4 t1, vint4 idx)
  1003. {
  1004. #if ASTCENC_SSE >= 41
  1005. // Set index byte MSB to 1 for unused bytes so shuffle returns zero
  1006. __m128i idxx = _mm_or_si128(idx.m, _mm_set1_epi32(static_cast<int>(0xFFFFFF00)));
  1007. __m128i result = _mm_shuffle_epi8(t0.m, idxx);
  1008. idxx = _mm_sub_epi8(idxx, _mm_set1_epi8(16));
  1009. __m128i result2 = _mm_shuffle_epi8(t1.m, idxx);
  1010. result = _mm_xor_si128(result, result2);
  1011. return vint4(result);
  1012. #else
  1013. uint8_t table[32];
  1014. std::memcpy(table + 0, &t0.m, 4 * sizeof(int));
  1015. std::memcpy(table + 16, &t1.m, 4 * sizeof(int));
  1016. return vint4(table[idx.lane<0>()],
  1017. table[idx.lane<1>()],
  1018. table[idx.lane<2>()],
  1019. table[idx.lane<3>()]);
  1020. #endif
  1021. }
  1022. /**
  1023. * @brief Perform an 8-bit 64-entry table lookup, with 32-bit indexes.
  1024. */
  1025. ASTCENC_SIMD_INLINE vint4 vtable_8bt_32bi(vint4 t0, vint4 t1, vint4 t2, vint4 t3, vint4 idx)
  1026. {
  1027. #if ASTCENC_SSE >= 41
  1028. // Set index byte MSB to 1 for unused bytes so shuffle returns zero
  1029. __m128i idxx = _mm_or_si128(idx.m, _mm_set1_epi32(static_cast<int>(0xFFFFFF00)));
  1030. __m128i result = _mm_shuffle_epi8(t0.m, idxx);
  1031. idxx = _mm_sub_epi8(idxx, _mm_set1_epi8(16));
  1032. __m128i result2 = _mm_shuffle_epi8(t1.m, idxx);
  1033. result = _mm_xor_si128(result, result2);
  1034. idxx = _mm_sub_epi8(idxx, _mm_set1_epi8(16));
  1035. result2 = _mm_shuffle_epi8(t2.m, idxx);
  1036. result = _mm_xor_si128(result, result2);
  1037. idxx = _mm_sub_epi8(idxx, _mm_set1_epi8(16));
  1038. result2 = _mm_shuffle_epi8(t3.m, idxx);
  1039. result = _mm_xor_si128(result, result2);
  1040. return vint4(result);
  1041. #else
  1042. uint8_t table[64];
  1043. std::memcpy(table + 0, &t0.m, 4 * sizeof(int));
  1044. std::memcpy(table + 16, &t1.m, 4 * sizeof(int));
  1045. std::memcpy(table + 32, &t2.m, 4 * sizeof(int));
  1046. std::memcpy(table + 48, &t3.m, 4 * sizeof(int));
  1047. return vint4(table[idx.lane<0>()],
  1048. table[idx.lane<1>()],
  1049. table[idx.lane<2>()],
  1050. table[idx.lane<3>()]);
  1051. #endif
  1052. }
  1053. /**
  1054. * @brief Return a vector of interleaved RGBA data.
  1055. *
  1056. * Input vectors have the value stored in the bottom 8 bits of each lane,
  1057. * with high bits set to zero.
  1058. *
  1059. * Output vector stores a single RGBA texel packed in each lane.
  1060. */
  1061. ASTCENC_SIMD_INLINE vint4 interleave_rgba8(vint4 r, vint4 g, vint4 b, vint4 a)
  1062. {
  1063. // Workaround an XCode compiler internal fault; note is slower than slli_epi32
  1064. // so we should revert this when we get the opportunity
  1065. #if defined(__APPLE__)
  1066. __m128i value = r.m;
  1067. value = _mm_add_epi32(value, _mm_bslli_si128(g.m, 1));
  1068. value = _mm_add_epi32(value, _mm_bslli_si128(b.m, 2));
  1069. value = _mm_add_epi32(value, _mm_bslli_si128(a.m, 3));
  1070. return vint4(value);
  1071. #else
  1072. __m128i value = r.m;
  1073. value = _mm_add_epi32(value, _mm_slli_epi32(g.m, 8));
  1074. value = _mm_add_epi32(value, _mm_slli_epi32(b.m, 16));
  1075. value = _mm_add_epi32(value, _mm_slli_epi32(a.m, 24));
  1076. return vint4(value);
  1077. #endif
  1078. }
  1079. /**
  1080. * @brief Store a single vector lane to an unaligned address.
  1081. */
  1082. ASTCENC_SIMD_INLINE void store_lane(uint8_t* base, int data)
  1083. {
  1084. std::memcpy(base, &data, sizeof(int));
  1085. }
  1086. /**
  1087. * @brief Store a vector, skipping masked lanes.
  1088. *
  1089. * All masked lanes must be at the end of vector, after all non-masked lanes.
  1090. */
  1091. ASTCENC_SIMD_INLINE void store_lanes_masked(uint8_t* base, vint4 data, vmask4 mask)
  1092. {
  1093. #if ASTCENC_AVX >= 2
  1094. _mm_maskstore_epi32(reinterpret_cast<int*>(base), _mm_castps_si128(mask.m), data.m);
  1095. #else
  1096. // Note - we cannot use _mm_maskmoveu_si128 as the underlying hardware doesn't guarantee
  1097. // fault suppression on masked lanes so we can get page faults at the end of an image.
  1098. if (mask.lane<3>() != 0.0f)
  1099. {
  1100. store(data, base);
  1101. }
  1102. else if (mask.lane<2>() != 0.0f)
  1103. {
  1104. store_lane(base + 0, data.lane<0>());
  1105. store_lane(base + 4, data.lane<1>());
  1106. store_lane(base + 8, data.lane<2>());
  1107. }
  1108. else if (mask.lane<1>() != 0.0f)
  1109. {
  1110. store_lane(base + 0, data.lane<0>());
  1111. store_lane(base + 4, data.lane<1>());
  1112. }
  1113. else if (mask.lane<0>() != 0.0f)
  1114. {
  1115. store_lane(base + 0, data.lane<0>());
  1116. }
  1117. #endif
  1118. }
  1119. #if defined(ASTCENC_NO_INVARIANCE) && (ASTCENC_SSE >= 41)
  1120. #define ASTCENC_USE_NATIVE_DOT_PRODUCT 1
  1121. /**
  1122. * @brief Return the dot product for the full 4 lanes, returning scalar.
  1123. */
  1124. ASTCENC_SIMD_INLINE float dot_s(vfloat4 a, vfloat4 b)
  1125. {
  1126. return _mm_cvtss_f32(_mm_dp_ps(a.m, b.m, 0xFF));
  1127. }
  1128. /**
  1129. * @brief Return the dot product for the full 4 lanes, returning vector.
  1130. */
  1131. ASTCENC_SIMD_INLINE vfloat4 dot(vfloat4 a, vfloat4 b)
  1132. {
  1133. return vfloat4(_mm_dp_ps(a.m, b.m, 0xFF));
  1134. }
  1135. /**
  1136. * @brief Return the dot product for the bottom 3 lanes, returning scalar.
  1137. */
  1138. ASTCENC_SIMD_INLINE float dot3_s(vfloat4 a, vfloat4 b)
  1139. {
  1140. return _mm_cvtss_f32(_mm_dp_ps(a.m, b.m, 0x77));
  1141. }
  1142. /**
  1143. * @brief Return the dot product for the bottom 3 lanes, returning vector.
  1144. */
  1145. ASTCENC_SIMD_INLINE vfloat4 dot3(vfloat4 a, vfloat4 b)
  1146. {
  1147. return vfloat4(_mm_dp_ps(a.m, b.m, 0x77));
  1148. }
  1149. #endif // #if defined(ASTCENC_NO_INVARIANCE) && (ASTCENC_SSE >= 41)
  1150. #if ASTCENC_POPCNT >= 1
  1151. #define ASTCENC_USE_NATIVE_POPCOUNT 1
  1152. /**
  1153. * @brief Population bit count.
  1154. *
  1155. * @param v The value to population count.
  1156. *
  1157. * @return The number of 1 bits.
  1158. */
  1159. ASTCENC_SIMD_INLINE int popcount(uint64_t v)
  1160. {
  1161. return static_cast<int>(_mm_popcnt_u64(v));
  1162. }
  1163. #endif // ASTCENC_POPCNT >= 1
  1164. #endif // #ifndef ASTC_VECMATHLIB_SSE_4_H_INCLUDED