astcenc_vecmathlib_none_4.h 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170
  1. // SPDX-License-Identifier: Apache-2.0
  2. // ----------------------------------------------------------------------------
  3. // Copyright 2019-2022 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 plain C++.
  19. *
  20. * This module implements 4-wide 32-bit float, int, and mask vectors. This
  21. * module provides a scalar fallback for VLA code, primarily useful for
  22. * debugging VLA algorithms without the complexity of handling SIMD. Only the
  23. * baseline level of functionality needed to support VLA is provided.
  24. *
  25. * Note that the vector conditional operators implemented by this module are
  26. * designed to behave like SIMD conditional operators that generate lane masks.
  27. * Rather than returning 0/1 booleans like normal C++ code they will return
  28. * 0/-1 to give a full lane-width bitmask.
  29. *
  30. * Note that the documentation for this module still talks about "vectors" to
  31. * help developers think about the implied VLA behavior when writing optimized
  32. * paths.
  33. */
  34. #ifndef ASTC_VECMATHLIB_NONE_4_H_INCLUDED
  35. #define ASTC_VECMATHLIB_NONE_4_H_INCLUDED
  36. #ifndef ASTCENC_SIMD_INLINE
  37. #error "Include astcenc_vecmathlib.h, do not include directly"
  38. #endif
  39. #include <algorithm>
  40. #include <cstdio>
  41. #include <cstring>
  42. #include <cfenv>
  43. // ============================================================================
  44. // vfloat4 data type
  45. // ============================================================================
  46. /**
  47. * @brief Data type for 4-wide floats.
  48. */
  49. struct vfloat4
  50. {
  51. /**
  52. * @brief Construct from zero-initialized value.
  53. */
  54. ASTCENC_SIMD_INLINE vfloat4() = default;
  55. /**
  56. * @brief Construct from 4 values loaded from an unaligned address.
  57. *
  58. * Consider using loada() which is better with wider VLA vectors if data is
  59. * aligned to vector length.
  60. */
  61. ASTCENC_SIMD_INLINE explicit vfloat4(const float* p)
  62. {
  63. m[0] = p[0];
  64. m[1] = p[1];
  65. m[2] = p[2];
  66. m[3] = p[3];
  67. }
  68. /**
  69. * @brief Construct from 4 scalar values replicated across all lanes.
  70. *
  71. * Consider using zero() for constexpr zeros.
  72. */
  73. ASTCENC_SIMD_INLINE explicit vfloat4(float a)
  74. {
  75. m[0] = a;
  76. m[1] = a;
  77. m[2] = a;
  78. m[3] = a;
  79. }
  80. /**
  81. * @brief Construct from 4 scalar values.
  82. *
  83. * The value of @c a is stored to lane 0 (LSB) in the SIMD register.
  84. */
  85. ASTCENC_SIMD_INLINE explicit vfloat4(float a, float b, float c, float d)
  86. {
  87. m[0] = a;
  88. m[1] = b;
  89. m[2] = c;
  90. m[3] = d;
  91. }
  92. /**
  93. * @brief Get the scalar value of a single lane.
  94. */
  95. template <int l> ASTCENC_SIMD_INLINE float lane() const
  96. {
  97. return m[l];
  98. }
  99. /**
  100. * @brief Set the scalar value of a single lane.
  101. */
  102. template <int l> ASTCENC_SIMD_INLINE void set_lane(float a)
  103. {
  104. m[l] = a;
  105. }
  106. /**
  107. * @brief Factory that returns a vector of zeros.
  108. */
  109. static ASTCENC_SIMD_INLINE vfloat4 zero()
  110. {
  111. return vfloat4(0.0f);
  112. }
  113. /**
  114. * @brief Factory that returns a replicated scalar loaded from memory.
  115. */
  116. static ASTCENC_SIMD_INLINE vfloat4 load1(const float* p)
  117. {
  118. return vfloat4(*p);
  119. }
  120. /**
  121. * @brief Factory that returns a vector loaded from aligned memory.
  122. */
  123. static ASTCENC_SIMD_INLINE vfloat4 loada(const float* p)
  124. {
  125. return vfloat4(p);
  126. }
  127. /**
  128. * @brief Factory that returns a vector containing the lane IDs.
  129. */
  130. static ASTCENC_SIMD_INLINE vfloat4 lane_id()
  131. {
  132. return vfloat4(0.0f, 1.0f, 2.0f, 3.0f);
  133. }
  134. /**
  135. * @brief Return a swizzled float 2.
  136. */
  137. template <int l0, int l1> ASTCENC_SIMD_INLINE vfloat4 swz() const
  138. {
  139. return vfloat4(lane<l0>(), lane<l1>(), 0.0f, 0.0f);
  140. }
  141. /**
  142. * @brief Return a swizzled float 3.
  143. */
  144. template <int l0, int l1, int l2> ASTCENC_SIMD_INLINE vfloat4 swz() const
  145. {
  146. return vfloat4(lane<l0>(), lane<l1>(), lane<l2>(), 0.0f);
  147. }
  148. /**
  149. * @brief Return a swizzled float 4.
  150. */
  151. template <int l0, int l1, int l2, int l3> ASTCENC_SIMD_INLINE vfloat4 swz() const
  152. {
  153. return vfloat4(lane<l0>(), lane<l1>(), lane<l2>(), lane<l3>());
  154. }
  155. /**
  156. * @brief The vector ...
  157. */
  158. float m[4];
  159. };
  160. // ============================================================================
  161. // vint4 data type
  162. // ============================================================================
  163. /**
  164. * @brief Data type for 4-wide ints.
  165. */
  166. struct vint4
  167. {
  168. /**
  169. * @brief Construct from zero-initialized value.
  170. */
  171. ASTCENC_SIMD_INLINE vint4() = default;
  172. /**
  173. * @brief Construct from 4 values loaded from an unaligned address.
  174. *
  175. * Consider using vint4::loada() which is better with wider VLA vectors
  176. * if data is aligned.
  177. */
  178. ASTCENC_SIMD_INLINE explicit vint4(const int* p)
  179. {
  180. m[0] = p[0];
  181. m[1] = p[1];
  182. m[2] = p[2];
  183. m[3] = p[3];
  184. }
  185. /**
  186. * @brief Construct from 4 uint8_t loaded from an unaligned address.
  187. */
  188. ASTCENC_SIMD_INLINE explicit vint4(const uint8_t *p)
  189. {
  190. m[0] = p[0];
  191. m[1] = p[1];
  192. m[2] = p[2];
  193. m[3] = p[3];
  194. }
  195. /**
  196. * @brief Construct from 4 scalar values.
  197. *
  198. * The value of @c a is stored to lane 0 (LSB) in the SIMD register.
  199. */
  200. ASTCENC_SIMD_INLINE explicit vint4(int a, int b, int c, int d)
  201. {
  202. m[0] = a;
  203. m[1] = b;
  204. m[2] = c;
  205. m[3] = d;
  206. }
  207. /**
  208. * @brief Construct from 4 scalar values replicated across all lanes.
  209. *
  210. * Consider using vint4::zero() for constexpr zeros.
  211. */
  212. ASTCENC_SIMD_INLINE explicit vint4(int a)
  213. {
  214. m[0] = a;
  215. m[1] = a;
  216. m[2] = a;
  217. m[3] = a;
  218. }
  219. /**
  220. * @brief Get the scalar value of a single lane.
  221. */
  222. template <int l> ASTCENC_SIMD_INLINE int lane() const
  223. {
  224. return m[l];
  225. }
  226. /**
  227. * @brief Set the scalar value of a single lane.
  228. */
  229. template <int l> ASTCENC_SIMD_INLINE void set_lane(int a)
  230. {
  231. m[l] = a;
  232. }
  233. /**
  234. * @brief Factory that returns a vector of zeros.
  235. */
  236. static ASTCENC_SIMD_INLINE vint4 zero()
  237. {
  238. return vint4(0);
  239. }
  240. /**
  241. * @brief Factory that returns a replicated scalar loaded from memory.
  242. */
  243. static ASTCENC_SIMD_INLINE vint4 load1(const int* p)
  244. {
  245. return vint4(*p);
  246. }
  247. /**
  248. * @brief Factory that returns a vector loaded from 16B aligned memory.
  249. */
  250. static ASTCENC_SIMD_INLINE vint4 loada(const int* p)
  251. {
  252. return vint4(p);
  253. }
  254. /**
  255. * @brief Factory that returns a vector containing the lane IDs.
  256. */
  257. static ASTCENC_SIMD_INLINE vint4 lane_id()
  258. {
  259. return vint4(0, 1, 2, 3);
  260. }
  261. /**
  262. * @brief The vector ...
  263. */
  264. int m[4];
  265. };
  266. // ============================================================================
  267. // vmask4 data type
  268. // ============================================================================
  269. /**
  270. * @brief Data type for 4-wide control plane masks.
  271. */
  272. struct vmask4
  273. {
  274. /**
  275. * @brief Construct from an existing mask value.
  276. */
  277. ASTCENC_SIMD_INLINE explicit vmask4(int* p)
  278. {
  279. m[0] = p[0];
  280. m[1] = p[1];
  281. m[2] = p[2];
  282. m[3] = p[3];
  283. }
  284. /**
  285. * @brief Construct from 1 scalar value.
  286. */
  287. ASTCENC_SIMD_INLINE explicit vmask4(bool a)
  288. {
  289. m[0] = a == false ? 0 : -1;
  290. m[1] = a == false ? 0 : -1;
  291. m[2] = a == false ? 0 : -1;
  292. m[3] = a == false ? 0 : -1;
  293. }
  294. /**
  295. * @brief Construct from 4 scalar values.
  296. *
  297. * The value of @c a is stored to lane 0 (LSB) in the SIMD register.
  298. */
  299. ASTCENC_SIMD_INLINE explicit vmask4(bool a, bool b, bool c, bool d)
  300. {
  301. m[0] = a == false ? 0 : -1;
  302. m[1] = b == false ? 0 : -1;
  303. m[2] = c == false ? 0 : -1;
  304. m[3] = d == false ? 0 : -1;
  305. }
  306. /**
  307. * @brief The vector ...
  308. */
  309. int m[4];
  310. };
  311. // ============================================================================
  312. // vmask4 operators and functions
  313. // ============================================================================
  314. /**
  315. * @brief Overload: mask union (or).
  316. */
  317. ASTCENC_SIMD_INLINE vmask4 operator|(vmask4 a, vmask4 b)
  318. {
  319. return vmask4(a.m[0] | b.m[0],
  320. a.m[1] | b.m[1],
  321. a.m[2] | b.m[2],
  322. a.m[3] | b.m[3]);
  323. }
  324. /**
  325. * @brief Overload: mask intersect (and).
  326. */
  327. ASTCENC_SIMD_INLINE vmask4 operator&(vmask4 a, vmask4 b)
  328. {
  329. return vmask4(a.m[0] & b.m[0],
  330. a.m[1] & b.m[1],
  331. a.m[2] & b.m[2],
  332. a.m[3] & b.m[3]);
  333. }
  334. /**
  335. * @brief Overload: mask difference (xor).
  336. */
  337. ASTCENC_SIMD_INLINE vmask4 operator^(vmask4 a, vmask4 b)
  338. {
  339. return vmask4(a.m[0] ^ b.m[0],
  340. a.m[1] ^ b.m[1],
  341. a.m[2] ^ b.m[2],
  342. a.m[3] ^ b.m[3]);
  343. }
  344. /**
  345. * @brief Overload: mask invert (not).
  346. */
  347. ASTCENC_SIMD_INLINE vmask4 operator~(vmask4 a)
  348. {
  349. return vmask4(~a.m[0],
  350. ~a.m[1],
  351. ~a.m[2],
  352. ~a.m[3]);
  353. }
  354. /**
  355. * @brief Return a 1-bit mask code indicating mask status.
  356. *
  357. * bit0 = lane 0
  358. */
  359. ASTCENC_SIMD_INLINE unsigned int mask(vmask4 a)
  360. {
  361. return ((a.m[0] >> 31) & 0x1) |
  362. ((a.m[1] >> 30) & 0x2) |
  363. ((a.m[2] >> 29) & 0x4) |
  364. ((a.m[3] >> 28) & 0x8);
  365. }
  366. // ============================================================================
  367. // vint4 operators and functions
  368. // ============================================================================
  369. /**
  370. * @brief Overload: vector by vector addition.
  371. */
  372. ASTCENC_SIMD_INLINE vint4 operator+(vint4 a, vint4 b)
  373. {
  374. return vint4(a.m[0] + b.m[0],
  375. a.m[1] + b.m[1],
  376. a.m[2] + b.m[2],
  377. a.m[3] + b.m[3]);
  378. }
  379. /**
  380. * @brief Overload: vector by vector subtraction.
  381. */
  382. ASTCENC_SIMD_INLINE vint4 operator-(vint4 a, vint4 b)
  383. {
  384. return vint4(a.m[0] - b.m[0],
  385. a.m[1] - b.m[1],
  386. a.m[2] - b.m[2],
  387. a.m[3] - b.m[3]);
  388. }
  389. /**
  390. * @brief Overload: vector by vector multiplication.
  391. */
  392. ASTCENC_SIMD_INLINE vint4 operator*(vint4 a, vint4 b)
  393. {
  394. return vint4(a.m[0] * b.m[0],
  395. a.m[1] * b.m[1],
  396. a.m[2] * b.m[2],
  397. a.m[3] * b.m[3]);
  398. }
  399. /**
  400. * @brief Overload: vector bit invert.
  401. */
  402. ASTCENC_SIMD_INLINE vint4 operator~(vint4 a)
  403. {
  404. return vint4(~a.m[0],
  405. ~a.m[1],
  406. ~a.m[2],
  407. ~a.m[3]);
  408. }
  409. /**
  410. * @brief Overload: vector by vector bitwise or.
  411. */
  412. ASTCENC_SIMD_INLINE vint4 operator|(vint4 a, vint4 b)
  413. {
  414. return vint4(a.m[0] | b.m[0],
  415. a.m[1] | b.m[1],
  416. a.m[2] | b.m[2],
  417. a.m[3] | b.m[3]);
  418. }
  419. /**
  420. * @brief Overload: vector by vector bitwise and.
  421. */
  422. ASTCENC_SIMD_INLINE vint4 operator&(vint4 a, vint4 b)
  423. {
  424. return vint4(a.m[0] & b.m[0],
  425. a.m[1] & b.m[1],
  426. a.m[2] & b.m[2],
  427. a.m[3] & b.m[3]);
  428. }
  429. /**
  430. * @brief Overload: vector by vector bitwise xor.
  431. */
  432. ASTCENC_SIMD_INLINE vint4 operator^(vint4 a, vint4 b)
  433. {
  434. return vint4(a.m[0] ^ b.m[0],
  435. a.m[1] ^ b.m[1],
  436. a.m[2] ^ b.m[2],
  437. a.m[3] ^ b.m[3]);
  438. }
  439. /**
  440. * @brief Overload: vector by vector equality.
  441. */
  442. ASTCENC_SIMD_INLINE vmask4 operator==(vint4 a, vint4 b)
  443. {
  444. return vmask4(a.m[0] == b.m[0],
  445. a.m[1] == b.m[1],
  446. a.m[2] == b.m[2],
  447. a.m[3] == b.m[3]);
  448. }
  449. /**
  450. * @brief Overload: vector by vector inequality.
  451. */
  452. ASTCENC_SIMD_INLINE vmask4 operator!=(vint4 a, vint4 b)
  453. {
  454. return vmask4(a.m[0] != b.m[0],
  455. a.m[1] != b.m[1],
  456. a.m[2] != b.m[2],
  457. a.m[3] != b.m[3]);
  458. }
  459. /**
  460. * @brief Overload: vector by vector less than.
  461. */
  462. ASTCENC_SIMD_INLINE vmask4 operator<(vint4 a, vint4 b)
  463. {
  464. return vmask4(a.m[0] < b.m[0],
  465. a.m[1] < b.m[1],
  466. a.m[2] < b.m[2],
  467. a.m[3] < b.m[3]);
  468. }
  469. /**
  470. * @brief Overload: vector by vector greater than.
  471. */
  472. ASTCENC_SIMD_INLINE vmask4 operator>(vint4 a, vint4 b)
  473. {
  474. return vmask4(a.m[0] > b.m[0],
  475. a.m[1] > b.m[1],
  476. a.m[2] > b.m[2],
  477. a.m[3] > b.m[3]);
  478. }
  479. /**
  480. * @brief Logical shift left.
  481. */
  482. template <int s> ASTCENC_SIMD_INLINE vint4 lsl(vint4 a)
  483. {
  484. return vint4(a.m[0] << s,
  485. a.m[1] << s,
  486. a.m[2] << s,
  487. a.m[3] << s);
  488. }
  489. /**
  490. * @brief Logical shift right.
  491. */
  492. template <int s> ASTCENC_SIMD_INLINE vint4 lsr(vint4 a)
  493. {
  494. unsigned int as0 = static_cast<unsigned int>(a.m[0]) >> s;
  495. unsigned int as1 = static_cast<unsigned int>(a.m[1]) >> s;
  496. unsigned int as2 = static_cast<unsigned int>(a.m[2]) >> s;
  497. unsigned int as3 = static_cast<unsigned int>(a.m[3]) >> s;
  498. return vint4(static_cast<int>(as0),
  499. static_cast<int>(as1),
  500. static_cast<int>(as2),
  501. static_cast<int>(as3));
  502. }
  503. /**
  504. * @brief Arithmetic shift right.
  505. */
  506. template <int s> ASTCENC_SIMD_INLINE vint4 asr(vint4 a)
  507. {
  508. return vint4(a.m[0] >> s,
  509. a.m[1] >> s,
  510. a.m[2] >> s,
  511. a.m[3] >> s);
  512. }
  513. /**
  514. * @brief Return the min vector of two vectors.
  515. */
  516. ASTCENC_SIMD_INLINE vint4 min(vint4 a, vint4 b)
  517. {
  518. return vint4(a.m[0] < b.m[0] ? a.m[0] : b.m[0],
  519. a.m[1] < b.m[1] ? a.m[1] : b.m[1],
  520. a.m[2] < b.m[2] ? a.m[2] : b.m[2],
  521. a.m[3] < b.m[3] ? a.m[3] : b.m[3]);
  522. }
  523. /**
  524. * @brief Return the min vector of two vectors.
  525. */
  526. ASTCENC_SIMD_INLINE vint4 max(vint4 a, vint4 b)
  527. {
  528. return vint4(a.m[0] > b.m[0] ? a.m[0] : b.m[0],
  529. a.m[1] > b.m[1] ? a.m[1] : b.m[1],
  530. a.m[2] > b.m[2] ? a.m[2] : b.m[2],
  531. a.m[3] > b.m[3] ? a.m[3] : b.m[3]);
  532. }
  533. /**
  534. * @brief Return the horizontal minimum of a single vector.
  535. */
  536. ASTCENC_SIMD_INLINE vint4 hmin(vint4 a)
  537. {
  538. int b = std::min(a.m[0], a.m[1]);
  539. int c = std::min(a.m[2], a.m[3]);
  540. return vint4(std::min(b, c));
  541. }
  542. /**
  543. * @brief Return the horizontal maximum of a single vector.
  544. */
  545. ASTCENC_SIMD_INLINE vint4 hmax(vint4 a)
  546. {
  547. int b = std::max(a.m[0], a.m[1]);
  548. int c = std::max(a.m[2], a.m[3]);
  549. return vint4(std::max(b, c));
  550. }
  551. /**
  552. * @brief Return the horizontal sum of vector lanes as a scalar.
  553. */
  554. ASTCENC_SIMD_INLINE int hadd_s(vint4 a)
  555. {
  556. return a.m[0] + a.m[1] + a.m[2] + a.m[3];
  557. }
  558. /**
  559. * @brief Store a vector to an aligned memory address.
  560. */
  561. ASTCENC_SIMD_INLINE void storea(vint4 a, int* p)
  562. {
  563. p[0] = a.m[0];
  564. p[1] = a.m[1];
  565. p[2] = a.m[2];
  566. p[3] = a.m[3];
  567. }
  568. /**
  569. * @brief Store a vector to an unaligned memory address.
  570. */
  571. ASTCENC_SIMD_INLINE void store(vint4 a, int* p)
  572. {
  573. p[0] = a.m[0];
  574. p[1] = a.m[1];
  575. p[2] = a.m[2];
  576. p[3] = a.m[3];
  577. }
  578. /**
  579. * @brief Store lowest N (vector width) bytes into an unaligned address.
  580. */
  581. ASTCENC_SIMD_INLINE void store_nbytes(vint4 a, uint8_t* p)
  582. {
  583. int* pi = reinterpret_cast<int*>(p);
  584. *pi = a.m[0];
  585. }
  586. /**
  587. * @brief Gather N (vector width) indices from the array.
  588. */
  589. ASTCENC_SIMD_INLINE vint4 gatheri(const int* base, vint4 indices)
  590. {
  591. return vint4(base[indices.m[0]],
  592. base[indices.m[1]],
  593. base[indices.m[2]],
  594. base[indices.m[3]]);
  595. }
  596. /**
  597. * @brief Pack low 8 bits of N (vector width) lanes into bottom of vector.
  598. */
  599. ASTCENC_SIMD_INLINE vint4 pack_low_bytes(vint4 a)
  600. {
  601. int b0 = a.m[0] & 0xFF;
  602. int b1 = a.m[1] & 0xFF;
  603. int b2 = a.m[2] & 0xFF;
  604. int b3 = a.m[3] & 0xFF;
  605. int b = b0 | (b1 << 8) | (b2 << 16) | (b3 << 24);
  606. return vint4(b, 0, 0, 0);
  607. }
  608. /**
  609. * @brief Return lanes from @c b if MSB of @c cond is set, else @c a.
  610. */
  611. ASTCENC_SIMD_INLINE vint4 select(vint4 a, vint4 b, vmask4 cond)
  612. {
  613. return vint4((cond.m[0] & static_cast<int>(0x80000000)) ? b.m[0] : a.m[0],
  614. (cond.m[1] & static_cast<int>(0x80000000)) ? b.m[1] : a.m[1],
  615. (cond.m[2] & static_cast<int>(0x80000000)) ? b.m[2] : a.m[2],
  616. (cond.m[3] & static_cast<int>(0x80000000)) ? b.m[3] : a.m[3]);
  617. }
  618. // ============================================================================
  619. // vfloat4 operators and functions
  620. // ============================================================================
  621. /**
  622. * @brief Overload: vector by vector addition.
  623. */
  624. ASTCENC_SIMD_INLINE vfloat4 operator+(vfloat4 a, vfloat4 b)
  625. {
  626. return vfloat4(a.m[0] + b.m[0],
  627. a.m[1] + b.m[1],
  628. a.m[2] + b.m[2],
  629. a.m[3] + b.m[3]);
  630. }
  631. /**
  632. * @brief Overload: vector by vector subtraction.
  633. */
  634. ASTCENC_SIMD_INLINE vfloat4 operator-(vfloat4 a, vfloat4 b)
  635. {
  636. return vfloat4(a.m[0] - b.m[0],
  637. a.m[1] - b.m[1],
  638. a.m[2] - b.m[2],
  639. a.m[3] - b.m[3]);
  640. }
  641. /**
  642. * @brief Overload: vector by vector multiplication.
  643. */
  644. ASTCENC_SIMD_INLINE vfloat4 operator*(vfloat4 a, vfloat4 b)
  645. {
  646. return vfloat4(a.m[0] * b.m[0],
  647. a.m[1] * b.m[1],
  648. a.m[2] * b.m[2],
  649. a.m[3] * b.m[3]);
  650. }
  651. /**
  652. * @brief Overload: vector by vector division.
  653. */
  654. ASTCENC_SIMD_INLINE vfloat4 operator/(vfloat4 a, vfloat4 b)
  655. {
  656. return vfloat4(a.m[0] / b.m[0],
  657. a.m[1] / b.m[1],
  658. a.m[2] / b.m[2],
  659. a.m[3] / b.m[3]);
  660. }
  661. /**
  662. * @brief Overload: vector by vector equality.
  663. */
  664. ASTCENC_SIMD_INLINE vmask4 operator==(vfloat4 a, vfloat4 b)
  665. {
  666. return vmask4(a.m[0] == b.m[0],
  667. a.m[1] == b.m[1],
  668. a.m[2] == b.m[2],
  669. a.m[3] == b.m[3]);
  670. }
  671. /**
  672. * @brief Overload: vector by vector inequality.
  673. */
  674. ASTCENC_SIMD_INLINE vmask4 operator!=(vfloat4 a, vfloat4 b)
  675. {
  676. return vmask4(a.m[0] != b.m[0],
  677. a.m[1] != b.m[1],
  678. a.m[2] != b.m[2],
  679. a.m[3] != b.m[3]);
  680. }
  681. /**
  682. * @brief Overload: vector by vector less than.
  683. */
  684. ASTCENC_SIMD_INLINE vmask4 operator<(vfloat4 a, vfloat4 b)
  685. {
  686. return vmask4(a.m[0] < b.m[0],
  687. a.m[1] < b.m[1],
  688. a.m[2] < b.m[2],
  689. a.m[3] < b.m[3]);
  690. }
  691. /**
  692. * @brief Overload: vector by vector greater than.
  693. */
  694. ASTCENC_SIMD_INLINE vmask4 operator>(vfloat4 a, vfloat4 b)
  695. {
  696. return vmask4(a.m[0] > b.m[0],
  697. a.m[1] > b.m[1],
  698. a.m[2] > b.m[2],
  699. a.m[3] > b.m[3]);
  700. }
  701. /**
  702. * @brief Overload: vector by vector less than or equal.
  703. */
  704. ASTCENC_SIMD_INLINE vmask4 operator<=(vfloat4 a, vfloat4 b)
  705. {
  706. return vmask4(a.m[0] <= b.m[0],
  707. a.m[1] <= b.m[1],
  708. a.m[2] <= b.m[2],
  709. a.m[3] <= b.m[3]);
  710. }
  711. /**
  712. * @brief Overload: vector by vector greater than or equal.
  713. */
  714. ASTCENC_SIMD_INLINE vmask4 operator>=(vfloat4 a, vfloat4 b)
  715. {
  716. return vmask4(a.m[0] >= b.m[0],
  717. a.m[1] >= b.m[1],
  718. a.m[2] >= b.m[2],
  719. a.m[3] >= b.m[3]);
  720. }
  721. /**
  722. * @brief Return the min vector of two vectors.
  723. *
  724. * If either lane value is NaN, @c b will be returned for that lane.
  725. */
  726. ASTCENC_SIMD_INLINE vfloat4 min(vfloat4 a, vfloat4 b)
  727. {
  728. return vfloat4(a.m[0] < b.m[0] ? a.m[0] : b.m[0],
  729. a.m[1] < b.m[1] ? a.m[1] : b.m[1],
  730. a.m[2] < b.m[2] ? a.m[2] : b.m[2],
  731. a.m[3] < b.m[3] ? a.m[3] : b.m[3]);
  732. }
  733. /**
  734. * @brief Return the max vector of two vectors.
  735. *
  736. * If either lane value is NaN, @c b will be returned for that lane.
  737. */
  738. ASTCENC_SIMD_INLINE vfloat4 max(vfloat4 a, vfloat4 b)
  739. {
  740. return vfloat4(a.m[0] > b.m[0] ? a.m[0] : b.m[0],
  741. a.m[1] > b.m[1] ? a.m[1] : b.m[1],
  742. a.m[2] > b.m[2] ? a.m[2] : b.m[2],
  743. a.m[3] > b.m[3] ? a.m[3] : b.m[3]);
  744. }
  745. /**
  746. * @brief Return the absolute value of the float vector.
  747. */
  748. ASTCENC_SIMD_INLINE vfloat4 abs(vfloat4 a)
  749. {
  750. return vfloat4(std::abs(a.m[0]),
  751. std::abs(a.m[1]),
  752. std::abs(a.m[2]),
  753. std::abs(a.m[3]));
  754. }
  755. /**
  756. * @brief Return a float rounded to the nearest integer value.
  757. */
  758. ASTCENC_SIMD_INLINE vfloat4 round(vfloat4 a)
  759. {
  760. assert(std::fegetround() == FE_TONEAREST);
  761. return vfloat4(std::nearbyint(a.m[0]),
  762. std::nearbyint(a.m[1]),
  763. std::nearbyint(a.m[2]),
  764. std::nearbyint(a.m[3]));
  765. }
  766. /**
  767. * @brief Return the horizontal minimum of a vector.
  768. */
  769. ASTCENC_SIMD_INLINE vfloat4 hmin(vfloat4 a)
  770. {
  771. float tmp1 = std::min(a.m[0], a.m[1]);
  772. float tmp2 = std::min(a.m[2], a.m[3]);
  773. return vfloat4(std::min(tmp1, tmp2));
  774. }
  775. /**
  776. * @brief Return the horizontal maximum of a vector.
  777. */
  778. ASTCENC_SIMD_INLINE vfloat4 hmax(vfloat4 a)
  779. {
  780. float tmp1 = std::max(a.m[0], a.m[1]);
  781. float tmp2 = std::max(a.m[2], a.m[3]);
  782. return vfloat4(std::max(tmp1, tmp2));
  783. }
  784. /**
  785. * @brief Return the horizontal sum of a vector.
  786. */
  787. ASTCENC_SIMD_INLINE float hadd_s(vfloat4 a)
  788. {
  789. // Use halving add, gives invariance with SIMD versions
  790. return (a.m[0] + a.m[2]) + (a.m[1] + a.m[3]);
  791. }
  792. /**
  793. * @brief Return the sqrt of the lanes in the vector.
  794. */
  795. ASTCENC_SIMD_INLINE vfloat4 sqrt(vfloat4 a)
  796. {
  797. return vfloat4(std::sqrt(a.m[0]),
  798. std::sqrt(a.m[1]),
  799. std::sqrt(a.m[2]),
  800. std::sqrt(a.m[3]));
  801. }
  802. /**
  803. * @brief Return lanes from @c b if @c cond is set, else @c a.
  804. */
  805. ASTCENC_SIMD_INLINE vfloat4 select(vfloat4 a, vfloat4 b, vmask4 cond)
  806. {
  807. return vfloat4((cond.m[0] & static_cast<int>(0x80000000)) ? b.m[0] : a.m[0],
  808. (cond.m[1] & static_cast<int>(0x80000000)) ? b.m[1] : a.m[1],
  809. (cond.m[2] & static_cast<int>(0x80000000)) ? b.m[2] : a.m[2],
  810. (cond.m[3] & static_cast<int>(0x80000000)) ? b.m[3] : a.m[3]);
  811. }
  812. /**
  813. * @brief Return lanes from @c b if MSB of @c cond is set, else @c a.
  814. */
  815. ASTCENC_SIMD_INLINE vfloat4 select_msb(vfloat4 a, vfloat4 b, vmask4 cond)
  816. {
  817. return vfloat4((cond.m[0] & static_cast<int>(0x80000000)) ? b.m[0] : a.m[0],
  818. (cond.m[1] & static_cast<int>(0x80000000)) ? b.m[1] : a.m[1],
  819. (cond.m[2] & static_cast<int>(0x80000000)) ? b.m[2] : a.m[2],
  820. (cond.m[3] & static_cast<int>(0x80000000)) ? b.m[3] : a.m[3]);
  821. }
  822. /**
  823. * @brief Load a vector of gathered results from an array;
  824. */
  825. ASTCENC_SIMD_INLINE vfloat4 gatherf(const float* base, vint4 indices)
  826. {
  827. return vfloat4(base[indices.m[0]],
  828. base[indices.m[1]],
  829. base[indices.m[2]],
  830. base[indices.m[3]]);
  831. }
  832. /**
  833. * @brief Store a vector to an unaligned memory address.
  834. */
  835. ASTCENC_SIMD_INLINE void store(vfloat4 a, float* ptr)
  836. {
  837. ptr[0] = a.m[0];
  838. ptr[1] = a.m[1];
  839. ptr[2] = a.m[2];
  840. ptr[3] = a.m[3];
  841. }
  842. /**
  843. * @brief Store a vector to an aligned memory address.
  844. */
  845. ASTCENC_SIMD_INLINE void storea(vfloat4 a, float* ptr)
  846. {
  847. ptr[0] = a.m[0];
  848. ptr[1] = a.m[1];
  849. ptr[2] = a.m[2];
  850. ptr[3] = a.m[3];
  851. }
  852. /**
  853. * @brief Return a integer value for a float vector, using truncation.
  854. */
  855. ASTCENC_SIMD_INLINE vint4 float_to_int(vfloat4 a)
  856. {
  857. return vint4(static_cast<int>(a.m[0]),
  858. static_cast<int>(a.m[1]),
  859. static_cast<int>(a.m[2]),
  860. static_cast<int>(a.m[3]));
  861. }
  862. /**f
  863. * @brief Return a integer value for a float vector, using round-to-nearest.
  864. */
  865. ASTCENC_SIMD_INLINE vint4 float_to_int_rtn(vfloat4 a)
  866. {
  867. return vint4(static_cast<int>(a.m[0] + 0.5f),
  868. static_cast<int>(a.m[1] + 0.5f),
  869. static_cast<int>(a.m[2] + 0.5f),
  870. static_cast<int>(a.m[3] + 0.5f));
  871. }
  872. /**
  873. * @brief Return a float value for a integer vector.
  874. */
  875. ASTCENC_SIMD_INLINE vfloat4 int_to_float(vint4 a)
  876. {
  877. return vfloat4(static_cast<float>(a.m[0]),
  878. static_cast<float>(a.m[1]),
  879. static_cast<float>(a.m[2]),
  880. static_cast<float>(a.m[3]));
  881. }
  882. /**
  883. * @brief Return a float16 value for a float vector, using round-to-nearest.
  884. */
  885. ASTCENC_SIMD_INLINE vint4 float_to_float16(vfloat4 a)
  886. {
  887. return vint4(
  888. float_to_sf16(a.lane<0>()),
  889. float_to_sf16(a.lane<1>()),
  890. float_to_sf16(a.lane<2>()),
  891. float_to_sf16(a.lane<3>()));
  892. }
  893. /**
  894. * @brief Return a float16 value for a float scalar, using round-to-nearest.
  895. */
  896. static inline uint16_t float_to_float16(float a)
  897. {
  898. return float_to_sf16(a);
  899. }
  900. /**
  901. * @brief Return a float value for a float16 vector.
  902. */
  903. ASTCENC_SIMD_INLINE vfloat4 float16_to_float(vint4 a)
  904. {
  905. return vfloat4(
  906. sf16_to_float(static_cast<uint16_t>(a.lane<0>())),
  907. sf16_to_float(static_cast<uint16_t>(a.lane<1>())),
  908. sf16_to_float(static_cast<uint16_t>(a.lane<2>())),
  909. sf16_to_float(static_cast<uint16_t>(a.lane<3>())));
  910. }
  911. /**
  912. * @brief Return a float value for a float16 scalar.
  913. */
  914. ASTCENC_SIMD_INLINE float float16_to_float(uint16_t a)
  915. {
  916. return sf16_to_float(a);
  917. }
  918. /**
  919. * @brief Return a float value as an integer bit pattern (i.e. no conversion).
  920. *
  921. * It is a common trick to convert floats into integer bit patterns, perform
  922. * some bit hackery based on knowledge they are IEEE 754 layout, and then
  923. * convert them back again. This is the first half of that flip.
  924. */
  925. ASTCENC_SIMD_INLINE vint4 float_as_int(vfloat4 a)
  926. {
  927. vint4 r;
  928. memcpy(r.m, a.m, 4 * 4);
  929. return r;
  930. }
  931. /**
  932. * @brief Return a integer value as a float bit pattern (i.e. no conversion).
  933. *
  934. * It is a common trick to convert floats into integer bit patterns, perform
  935. * some bit hackery based on knowledge they are IEEE 754 layout, and then
  936. * convert them back again. This is the second half of that flip.
  937. */
  938. ASTCENC_SIMD_INLINE vfloat4 int_as_float(vint4 a)
  939. {
  940. vfloat4 r;
  941. memcpy(r.m, a.m, 4 * 4);
  942. return r;
  943. }
  944. /**
  945. * @brief Prepare a vtable lookup table for use with the native SIMD size.
  946. */
  947. ASTCENC_SIMD_INLINE void vtable_prepare(vint4 t0, vint4& t0p)
  948. {
  949. t0p = t0;
  950. }
  951. /**
  952. * @brief Prepare a vtable lookup table for use with the native SIMD size.
  953. */
  954. ASTCENC_SIMD_INLINE void vtable_prepare(vint4 t0, vint4 t1, vint4& t0p, vint4& t1p)
  955. {
  956. t0p = t0;
  957. t1p = t1;
  958. }
  959. /**
  960. * @brief Prepare a vtable lookup table for use with the native SIMD size.
  961. */
  962. ASTCENC_SIMD_INLINE void vtable_prepare(
  963. vint4 t0, vint4 t1, vint4 t2, vint4 t3,
  964. vint4& t0p, vint4& t1p, vint4& t2p, vint4& t3p)
  965. {
  966. t0p = t0;
  967. t1p = t1;
  968. t2p = t2;
  969. t3p = t3;
  970. }
  971. /**
  972. * @brief Perform an 8-bit 32-entry table lookup, with 32-bit indexes.
  973. */
  974. ASTCENC_SIMD_INLINE vint4 vtable_8bt_32bi(vint4 t0, vint4 idx)
  975. {
  976. uint8_t table[16];
  977. storea(t0, reinterpret_cast<int*>(table + 0));
  978. return vint4(table[idx.lane<0>()],
  979. table[idx.lane<1>()],
  980. table[idx.lane<2>()],
  981. table[idx.lane<3>()]);
  982. }
  983. /**
  984. * @brief Perform an 8-bit 32-entry table lookup, with 32-bit indexes.
  985. */
  986. ASTCENC_SIMD_INLINE vint4 vtable_8bt_32bi(vint4 t0, vint4 t1, vint4 idx)
  987. {
  988. uint8_t table[32];
  989. storea(t0, reinterpret_cast<int*>(table + 0));
  990. storea(t1, reinterpret_cast<int*>(table + 16));
  991. return vint4(table[idx.lane<0>()],
  992. table[idx.lane<1>()],
  993. table[idx.lane<2>()],
  994. table[idx.lane<3>()]);
  995. }
  996. /**
  997. * @brief Perform an 8-bit 64-entry table lookup, with 32-bit indexes.
  998. */
  999. ASTCENC_SIMD_INLINE vint4 vtable_8bt_32bi(vint4 t0, vint4 t1, vint4 t2, vint4 t3, vint4 idx)
  1000. {
  1001. uint8_t table[64];
  1002. storea(t0, reinterpret_cast<int*>(table + 0));
  1003. storea(t1, reinterpret_cast<int*>(table + 16));
  1004. storea(t2, reinterpret_cast<int*>(table + 32));
  1005. storea(t3, reinterpret_cast<int*>(table + 48));
  1006. return vint4(table[idx.lane<0>()],
  1007. table[idx.lane<1>()],
  1008. table[idx.lane<2>()],
  1009. table[idx.lane<3>()]);
  1010. }
  1011. /**
  1012. * @brief Return a vector of interleaved RGBA data.
  1013. *
  1014. * Input vectors have the value stored in the bottom 8 bits of each lane,
  1015. * with high bits set to zero.
  1016. *
  1017. * Output vector stores a single RGBA texel packed in each lane.
  1018. */
  1019. ASTCENC_SIMD_INLINE vint4 interleave_rgba8(vint4 r, vint4 g, vint4 b, vint4 a)
  1020. {
  1021. return r + lsl<8>(g) + lsl<16>(b) + lsl<24>(a);
  1022. }
  1023. /**
  1024. * @brief Store a vector, skipping masked lanes.
  1025. *
  1026. * All masked lanes must be at the end of vector, after all non-masked lanes.
  1027. */
  1028. ASTCENC_SIMD_INLINE void store_lanes_masked(int* base, vint4 data, vmask4 mask)
  1029. {
  1030. if (mask.m[3])
  1031. {
  1032. store(data, base);
  1033. }
  1034. else if (mask.m[2])
  1035. {
  1036. base[0] = data.lane<0>();
  1037. base[1] = data.lane<1>();
  1038. base[2] = data.lane<2>();
  1039. }
  1040. else if (mask.m[1])
  1041. {
  1042. base[0] = data.lane<0>();
  1043. base[1] = data.lane<1>();
  1044. }
  1045. else if (mask.m[0])
  1046. {
  1047. base[0] = data.lane<0>();
  1048. }
  1049. }
  1050. #endif // #ifndef ASTC_VECMATHLIB_NONE_4_H_INCLUDED