astcenc_vecmathlib_sve_8.h 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101
  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 SVE.
  19. *
  20. * This module implements 8-wide 32-bit float, int, and mask vectors for Arm
  21. * SVE.
  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_SVE_8_H_INCLUDED
  29. #define ASTC_VECMATHLIB_SVE_8_H_INCLUDED
  30. #ifndef ASTCENC_SIMD_INLINE
  31. #error "Include astcenc_vecmathlib.h, do not include directly"
  32. #endif
  33. #include <cstdio>
  34. typedef svbool_t svbool_8_t __attribute__((arm_sve_vector_bits(256)));
  35. typedef svuint8_t svuint8_8_t __attribute__((arm_sve_vector_bits(256)));
  36. typedef svuint16_t svuint16_8_t __attribute__((arm_sve_vector_bits(256)));
  37. typedef svuint32_t svuint32_8_t __attribute__((arm_sve_vector_bits(256)));
  38. typedef svint32_t svint32_8_t __attribute__((arm_sve_vector_bits(256)));
  39. typedef svfloat32_t svfloat32_8_t __attribute__((arm_sve_vector_bits(256)));
  40. // ============================================================================
  41. // vfloat8 data type
  42. // ============================================================================
  43. /**
  44. * @brief Data type for 8-wide floats.
  45. */
  46. struct vfloat8
  47. {
  48. /**
  49. * @brief Construct from zero-initialized value.
  50. */
  51. ASTCENC_SIMD_INLINE vfloat8() = default;
  52. /**
  53. * @brief Construct from 8 values loaded from an unaligned address.
  54. *
  55. * Consider using loada() which is better with vectors if data is aligned
  56. * to vector length.
  57. */
  58. ASTCENC_SIMD_INLINE explicit vfloat8(const float *p)
  59. {
  60. m = svld1_f32(svptrue_b32(), p);
  61. }
  62. /**
  63. * @brief Construct from 1 scalar value replicated across all lanes.
  64. *
  65. * Consider using zero() for constexpr zeros.
  66. */
  67. ASTCENC_SIMD_INLINE explicit vfloat8(float a)
  68. {
  69. m = svdup_f32(a);
  70. }
  71. /**
  72. * @brief Construct from an existing SIMD register.
  73. */
  74. ASTCENC_SIMD_INLINE explicit vfloat8(svfloat32_8_t a)
  75. {
  76. m = a;
  77. }
  78. /**
  79. * @brief Factory that returns a vector of zeros.
  80. */
  81. static ASTCENC_SIMD_INLINE vfloat8 zero()
  82. {
  83. return vfloat8(0.0f);
  84. }
  85. /**
  86. * @brief Factory that returns a replicated scalar loaded from memory.
  87. */
  88. static ASTCENC_SIMD_INLINE vfloat8 load1(const float* p)
  89. {
  90. return vfloat8(*p);
  91. }
  92. /**
  93. * @brief Factory that returns a vector loaded from 32B aligned memory.
  94. */
  95. static ASTCENC_SIMD_INLINE vfloat8 loada(const float* p)
  96. {
  97. return vfloat8(p);
  98. }
  99. /**
  100. * @brief The vector ...
  101. */
  102. svfloat32_8_t m;
  103. };
  104. // ============================================================================
  105. // vint8 data type
  106. // ============================================================================
  107. /**
  108. * @brief Data type for 8-wide ints.
  109. */
  110. struct vint8
  111. {
  112. /**
  113. * @brief Construct from zero-initialized value.
  114. */
  115. ASTCENC_SIMD_INLINE vint8() = default;
  116. /**
  117. * @brief Construct from 8 values loaded from an unaligned address.
  118. *
  119. * Consider using loada() which is better with vectors if data is aligned
  120. * to vector length.
  121. */
  122. ASTCENC_SIMD_INLINE explicit vint8(const int *p)
  123. {
  124. m = svld1_s32(svptrue_b32(), p);
  125. }
  126. /**
  127. * @brief Construct from 8 uint8_t loaded from an unaligned address.
  128. */
  129. ASTCENC_SIMD_INLINE explicit vint8(const uint8_t *p)
  130. {
  131. // Load 8-bit values and expand to 32-bits
  132. m = svld1ub_s32(svptrue_b32(), p);
  133. }
  134. /**
  135. * @brief Construct from 1 scalar value replicated across all lanes.
  136. *
  137. * Consider using zero() for constexpr zeros.
  138. */
  139. ASTCENC_SIMD_INLINE explicit vint8(int a)
  140. {
  141. m = svdup_s32(a);
  142. }
  143. /**
  144. * @brief Construct from an existing SIMD register.
  145. */
  146. ASTCENC_SIMD_INLINE explicit vint8(svint32_8_t a)
  147. {
  148. m = a;
  149. }
  150. /**
  151. * @brief Factory that returns a vector of zeros.
  152. */
  153. static ASTCENC_SIMD_INLINE vint8 zero()
  154. {
  155. return vint8(0.0f);
  156. }
  157. /**
  158. * @brief Factory that returns a replicated scalar loaded from memory.
  159. */
  160. static ASTCENC_SIMD_INLINE vint8 load1(const int* p)
  161. {
  162. return vint8(*p);
  163. }
  164. /**
  165. * @brief Factory that returns a vector loaded from unaligned memory.
  166. */
  167. static ASTCENC_SIMD_INLINE vint8 load(const uint8_t* p)
  168. {
  169. svuint8_8_t data = svld1_u8(svptrue_b8(), p);
  170. return vint8(svreinterpret_s32_u8(data));
  171. }
  172. /**
  173. * @brief Factory that returns a vector loaded from 32B aligned memory.
  174. */
  175. static ASTCENC_SIMD_INLINE vint8 loada(const int* p)
  176. {
  177. return vint8(p);
  178. }
  179. /**
  180. * @brief Factory that returns a vector containing the lane IDs.
  181. */
  182. static ASTCENC_SIMD_INLINE vint8 lane_id()
  183. {
  184. return vint8(svindex_s32(0, 1));
  185. }
  186. /**
  187. * @brief The vector ...
  188. */
  189. svint32_8_t m;
  190. };
  191. // ============================================================================
  192. // vmask8 data type
  193. // ============================================================================
  194. /**
  195. * @brief Data type for 8-wide control plane masks.
  196. */
  197. struct vmask8
  198. {
  199. /**
  200. * @brief Construct from an existing SIMD register.
  201. */
  202. ASTCENC_SIMD_INLINE explicit vmask8(svbool_8_t a)
  203. {
  204. m = a;
  205. }
  206. /**
  207. * @brief Construct from 1 scalar value.
  208. */
  209. ASTCENC_SIMD_INLINE explicit vmask8(bool a)
  210. {
  211. m = svdup_b32(a);
  212. }
  213. /**
  214. * @brief The vector ...
  215. */
  216. svbool_8_t m;
  217. };
  218. // ============================================================================
  219. // vmask8 operators and functions
  220. // ============================================================================
  221. /**
  222. * @brief Overload: mask union (or).
  223. */
  224. ASTCENC_SIMD_INLINE vmask8 operator|(vmask8 a, vmask8 b)
  225. {
  226. return vmask8(svorr_z(svptrue_b32(), a.m, b.m));
  227. }
  228. /**
  229. * @brief Overload: mask intersect (and).
  230. */
  231. ASTCENC_SIMD_INLINE vmask8 operator&(vmask8 a, vmask8 b)
  232. {
  233. return vmask8(svand_z(svptrue_b32(), a.m, b.m));
  234. }
  235. /**
  236. * @brief Overload: mask difference (xor).
  237. */
  238. ASTCENC_SIMD_INLINE vmask8 operator^(vmask8 a, vmask8 b)
  239. {
  240. return vmask8(sveor_z(svptrue_b32(), a.m, b.m));
  241. }
  242. /**
  243. * @brief Overload: mask invert (not).
  244. */
  245. ASTCENC_SIMD_INLINE vmask8 operator~(vmask8 a)
  246. {
  247. return vmask8(svnot_z(svptrue_b32(), a.m));
  248. }
  249. /**
  250. * @brief Return a 8-bit mask code indicating mask status.
  251. *
  252. * bit0 = lane 0
  253. */
  254. ASTCENC_SIMD_INLINE unsigned int mask(vmask8 a)
  255. {
  256. alignas(32) const int shifta[8] { 0x1, 0x2, 0x4, 0x8, 0x10, 0x20, 0x40, 0x80 };
  257. svint32_8_t template_vals = svld1_s32(svptrue_b32(), shifta);
  258. svint32_8_t active_vals = svsel_s32(a.m, template_vals, svdup_s32(0));
  259. return static_cast<unsigned int>(svaddv_s32(svptrue_b32(), active_vals));
  260. }
  261. /**
  262. * @brief True if any lanes are enabled, false otherwise.
  263. */
  264. ASTCENC_SIMD_INLINE bool any(vmask8 a)
  265. {
  266. return svptest_any(svptrue_b32(), a.m);
  267. }
  268. /**
  269. * @brief True if all lanes are enabled, false otherwise.
  270. */
  271. ASTCENC_SIMD_INLINE bool all(vmask8 a)
  272. {
  273. return !svptest_any(svptrue_b32(), (~a).m);
  274. }
  275. // ============================================================================
  276. // vint8 operators and functions
  277. // ============================================================================
  278. /**
  279. * @brief Overload: vector by vector addition.
  280. */
  281. ASTCENC_SIMD_INLINE vint8 operator+(vint8 a, vint8 b)
  282. {
  283. return vint8(svadd_s32_x(svptrue_b32(), a.m, b.m));
  284. }
  285. /**
  286. * @brief Overload: vector by vector incremental addition.
  287. */
  288. ASTCENC_SIMD_INLINE vint8& operator+=(vint8& a, const vint8& b)
  289. {
  290. a = a + b;
  291. return a;
  292. }
  293. /**
  294. * @brief Overload: vector by vector subtraction.
  295. */
  296. ASTCENC_SIMD_INLINE vint8 operator-(vint8 a, vint8 b)
  297. {
  298. return vint8(svsub_s32_x(svptrue_b32(), a.m, b.m));
  299. }
  300. /**
  301. * @brief Overload: vector by vector multiplication.
  302. */
  303. ASTCENC_SIMD_INLINE vint8 operator*(vint8 a, vint8 b)
  304. {
  305. return vint8(svmul_s32_x(svptrue_b32(), a.m, b.m));
  306. }
  307. /**
  308. * @brief Overload: vector bit invert.
  309. */
  310. ASTCENC_SIMD_INLINE vint8 operator~(vint8 a)
  311. {
  312. return vint8(svnot_s32_x(svptrue_b32(), a.m));
  313. }
  314. /**
  315. * @brief Overload: vector by vector bitwise or.
  316. */
  317. ASTCENC_SIMD_INLINE vint8 operator|(vint8 a, vint8 b)
  318. {
  319. return vint8(svorr_s32_x(svptrue_b32(), a.m, b.m));
  320. }
  321. /**
  322. * @brief Overload: vector by vector bitwise and.
  323. */
  324. ASTCENC_SIMD_INLINE vint8 operator&(vint8 a, vint8 b)
  325. {
  326. return vint8(svand_s32_x(svptrue_b32(), a.m, b.m));
  327. }
  328. /**
  329. * @brief Overload: vector by vector bitwise xor.
  330. */
  331. ASTCENC_SIMD_INLINE vint8 operator^(vint8 a, vint8 b)
  332. {
  333. return vint8(sveor_s32_x(svptrue_b32(), a.m, b.m));
  334. }
  335. /**
  336. * @brief Overload: vector by vector equality.
  337. */
  338. ASTCENC_SIMD_INLINE vmask8 operator==(vint8 a, vint8 b)
  339. {
  340. return vmask8(svcmpeq_s32(svptrue_b32(), a.m, b.m));
  341. }
  342. /**
  343. * @brief Overload: vector by vector inequality.
  344. */
  345. ASTCENC_SIMD_INLINE vmask8 operator!=(vint8 a, vint8 b)
  346. {
  347. return vmask8(svcmpne_s32(svptrue_b32(), a.m, b.m));
  348. }
  349. /**
  350. * @brief Overload: vector by vector less than.
  351. */
  352. ASTCENC_SIMD_INLINE vmask8 operator<(vint8 a, vint8 b)
  353. {
  354. return vmask8(svcmplt_s32(svptrue_b32(), a.m, b.m));
  355. }
  356. /**
  357. * @brief Overload: vector by vector greater than.
  358. */
  359. ASTCENC_SIMD_INLINE vmask8 operator>(vint8 a, vint8 b)
  360. {
  361. return vmask8(svcmpgt_s32(svptrue_b32(), a.m, b.m));
  362. }
  363. /**
  364. * @brief Logical shift left.
  365. */
  366. template <int s> ASTCENC_SIMD_INLINE vint8 lsl(vint8 a)
  367. {
  368. return vint8(svlsl_n_s32_x(svptrue_b32(), a.m, s));
  369. }
  370. /**
  371. * @brief Arithmetic shift right.
  372. */
  373. template <int s> ASTCENC_SIMD_INLINE vint8 asr(vint8 a)
  374. {
  375. return vint8(svasr_n_s32_x(svptrue_b32(), a.m, s));
  376. }
  377. /**
  378. * @brief Logical shift right.
  379. */
  380. template <int s> ASTCENC_SIMD_INLINE vint8 lsr(vint8 a)
  381. {
  382. svuint32_8_t r = svreinterpret_u32_s32(a.m);
  383. r = svlsr_n_u32_x(svptrue_b32(), r, s);
  384. return vint8(svreinterpret_s32_u32(r));
  385. }
  386. /**
  387. * @brief Return the min vector of two vectors.
  388. */
  389. ASTCENC_SIMD_INLINE vint8 min(vint8 a, vint8 b)
  390. {
  391. return vint8(svmin_s32_x(svptrue_b32(), a.m, b.m));
  392. }
  393. /**
  394. * @brief Return the max vector of two vectors.
  395. */
  396. ASTCENC_SIMD_INLINE vint8 max(vint8 a, vint8 b)
  397. {
  398. return vint8(svmax_s32_x(svptrue_b32(), a.m, b.m));
  399. }
  400. /**
  401. * @brief Return the horizontal minimum of a vector.
  402. */
  403. ASTCENC_SIMD_INLINE vint8 hmin(vint8 a)
  404. {
  405. return vint8(svminv_s32(svptrue_b32(), a.m));
  406. }
  407. /**
  408. * @brief Return the horizontal minimum of a vector.
  409. */
  410. ASTCENC_SIMD_INLINE int hmin_s(vint8 a)
  411. {
  412. return svminv_s32(svptrue_b32(), a.m);
  413. }
  414. /**
  415. * @brief Return the horizontal maximum of a vector.
  416. */
  417. ASTCENC_SIMD_INLINE vint8 hmax(vint8 a)
  418. {
  419. return vint8(svmaxv_s32(svptrue_b32(), a.m));
  420. }
  421. /**
  422. * @brief Return the horizontal maximum of a vector.
  423. */
  424. ASTCENC_SIMD_INLINE int hmax_s(vint8 a)
  425. {
  426. return svmaxv_s32(svptrue_b32(), a.m);
  427. }
  428. /**
  429. * @brief Generate a vint8 from a size_t.
  430. */
  431. ASTCENC_SIMD_INLINE vint8 vint8_from_size(size_t a)
  432. {
  433. assert(a <= std::numeric_limits<int>::max());
  434. return vint8(static_cast<int>(a));
  435. }
  436. /**
  437. * @brief Store a vector to a 16B aligned memory address.
  438. */
  439. ASTCENC_SIMD_INLINE void storea(vint8 a, int* p)
  440. {
  441. svst1_s32(svptrue_b32(), p, a.m);
  442. }
  443. /**
  444. * @brief Store a vector to an unaligned memory address.
  445. */
  446. ASTCENC_SIMD_INLINE void store(vint8 a, int* p)
  447. {
  448. svst1_s32(svptrue_b32(), p, a.m);
  449. }
  450. /**
  451. * @brief Store lowest N (vector width) bytes into an unaligned address.
  452. */
  453. ASTCENC_SIMD_INLINE void store_nbytes(vint8 a, uint8_t* p)
  454. {
  455. svuint8_8_t r = svreinterpret_u8_s32(a.m);
  456. svst1_u8(svptrue_pat_b8(SV_VL8), p, r);
  457. }
  458. /**
  459. * @brief Pack low 8 bits of N (vector width) lanes into bottom of vector.
  460. */
  461. ASTCENC_SIMD_INLINE void pack_and_store_low_bytes(vint8 v, uint8_t* p)
  462. {
  463. svuint32_8_t data = svreinterpret_u32_s32(v.m);
  464. svst1b_u32(svptrue_b32(), p, data);
  465. }
  466. /**
  467. * @brief Return lanes from @c b if @c cond is set, else @c a.
  468. */
  469. ASTCENC_SIMD_INLINE vint8 select(vint8 a, vint8 b, vmask8 cond)
  470. {
  471. return vint8(svsel_s32(cond.m, b.m, a.m));
  472. }
  473. // ============================================================================
  474. // vfloat8 operators and functions
  475. // ============================================================================
  476. /**
  477. * @brief Overload: vector by vector addition.
  478. */
  479. ASTCENC_SIMD_INLINE vfloat8 operator+(vfloat8 a, vfloat8 b)
  480. {
  481. return vfloat8(svadd_f32_x(svptrue_b32(), a.m, b.m));
  482. }
  483. /**
  484. * @brief Overload: vector by vector incremental addition.
  485. */
  486. ASTCENC_SIMD_INLINE vfloat8& operator+=(vfloat8& a, const vfloat8& b)
  487. {
  488. a = a + b;
  489. return a;
  490. }
  491. /**
  492. * @brief Overload: vector by vector subtraction.
  493. */
  494. ASTCENC_SIMD_INLINE vfloat8 operator-(vfloat8 a, vfloat8 b)
  495. {
  496. return vfloat8(svsub_f32_x(svptrue_b32(), a.m, b.m));
  497. }
  498. /**
  499. * @brief Overload: vector by vector multiplication.
  500. */
  501. ASTCENC_SIMD_INLINE vfloat8 operator*(vfloat8 a, vfloat8 b)
  502. {
  503. return vfloat8(svmul_f32_x(svptrue_b32(), a.m, b.m));
  504. }
  505. /**
  506. * @brief Overload: vector by scalar multiplication.
  507. */
  508. ASTCENC_SIMD_INLINE vfloat8 operator*(vfloat8 a, float b)
  509. {
  510. return vfloat8(svmul_f32_x(svptrue_b32(), a.m, svdup_f32(b)));
  511. }
  512. /**
  513. * @brief Overload: scalar by vector multiplication.
  514. */
  515. ASTCENC_SIMD_INLINE vfloat8 operator*(float a, vfloat8 b)
  516. {
  517. return vfloat8(svmul_f32_x(svptrue_b32(), svdup_f32(a), b.m));
  518. }
  519. /**
  520. * @brief Overload: vector by vector division.
  521. */
  522. ASTCENC_SIMD_INLINE vfloat8 operator/(vfloat8 a, vfloat8 b)
  523. {
  524. return vfloat8(svdiv_f32_x(svptrue_b32(), a.m, b.m));
  525. }
  526. /**
  527. * @brief Overload: vector by scalar division.
  528. */
  529. ASTCENC_SIMD_INLINE vfloat8 operator/(vfloat8 a, float b)
  530. {
  531. return vfloat8(svdiv_f32_x(svptrue_b32(), a.m, svdup_f32(b)));
  532. }
  533. /**
  534. * @brief Overload: scalar by vector division.
  535. */
  536. ASTCENC_SIMD_INLINE vfloat8 operator/(float a, vfloat8 b)
  537. {
  538. return vfloat8(svdiv_f32_x(svptrue_b32(), svdup_f32(a), b.m));
  539. }
  540. /**
  541. * @brief Overload: vector by vector equality.
  542. */
  543. ASTCENC_SIMD_INLINE vmask8 operator==(vfloat8 a, vfloat8 b)
  544. {
  545. return vmask8(svcmpeq_f32(svptrue_b32(), a.m, b.m));
  546. }
  547. /**
  548. * @brief Overload: vector by vector inequality.
  549. */
  550. ASTCENC_SIMD_INLINE vmask8 operator!=(vfloat8 a, vfloat8 b)
  551. {
  552. return vmask8(svcmpne_f32(svptrue_b32(), a.m, b.m));
  553. }
  554. /**
  555. * @brief Overload: vector by vector less than.
  556. */
  557. ASTCENC_SIMD_INLINE vmask8 operator<(vfloat8 a, vfloat8 b)
  558. {
  559. return vmask8(svcmplt_f32(svptrue_b32(), a.m, b.m));;
  560. }
  561. /**
  562. * @brief Overload: vector by vector greater than.
  563. */
  564. ASTCENC_SIMD_INLINE vmask8 operator>(vfloat8 a, vfloat8 b)
  565. {
  566. return vmask8(svcmpgt_f32(svptrue_b32(), a.m, b.m));
  567. }
  568. /**
  569. * @brief Overload: vector by vector less than or equal.
  570. */
  571. ASTCENC_SIMD_INLINE vmask8 operator<=(vfloat8 a, vfloat8 b)
  572. {
  573. return vmask8(svcmple_f32(svptrue_b32(), a.m, b.m));
  574. }
  575. /**
  576. * @brief Overload: vector by vector greater than or equal.
  577. */
  578. ASTCENC_SIMD_INLINE vmask8 operator>=(vfloat8 a, vfloat8 b)
  579. {
  580. return vmask8(svcmpge_f32(svptrue_b32(), a.m, b.m));
  581. }
  582. /**
  583. * @brief Return the min vector of two vectors.
  584. *
  585. * If either lane value is NaN, the other lane will be returned.
  586. */
  587. ASTCENC_SIMD_INLINE vfloat8 min(vfloat8 a, vfloat8 b)
  588. {
  589. return vfloat8(svminnm_f32_x(svptrue_b32(), a.m, b.m));
  590. }
  591. /**
  592. * @brief Return the min vector of a vector and a scalar.
  593. *
  594. * If either lane value is NaN, the other lane will be returned.
  595. */
  596. ASTCENC_SIMD_INLINE vfloat8 min(vfloat8 a, float b)
  597. {
  598. return min(a, vfloat8(b));
  599. }
  600. /**
  601. * @brief Return the max vector of two vectors.
  602. *
  603. * If either lane value is NaN, the other lane will be returned.
  604. */
  605. ASTCENC_SIMD_INLINE vfloat8 max(vfloat8 a, vfloat8 b)
  606. {
  607. return vfloat8(svmaxnm_f32_x(svptrue_b32(), a.m, b.m));
  608. }
  609. /**
  610. * @brief Return the max vector of a vector and a scalar.
  611. *
  612. * If either lane value is NaN, the other lane will be returned.
  613. */
  614. ASTCENC_SIMD_INLINE vfloat8 max(vfloat8 a, float b)
  615. {
  616. return max(a, vfloat8(b));
  617. }
  618. /**
  619. * @brief Return the clamped value between min and max.
  620. *
  621. * It is assumed that neither @c min nor @c max are NaN values. If @c a is NaN
  622. * then @c min will be returned for that lane.
  623. */
  624. ASTCENC_SIMD_INLINE vfloat8 clamp(float minv, float maxv, vfloat8 a)
  625. {
  626. return min(max(a, minv), maxv);
  627. }
  628. /**
  629. * @brief Return a clamped value between 0.0f and 1.0f.
  630. *
  631. * If @c a is NaN then zero will be returned for that lane.
  632. */
  633. ASTCENC_SIMD_INLINE vfloat8 clampzo(vfloat8 a)
  634. {
  635. return clamp(0.0f, 1.0f, a);
  636. }
  637. /**
  638. * @brief Return the absolute value of the float vector.
  639. */
  640. ASTCENC_SIMD_INLINE vfloat8 abs(vfloat8 a)
  641. {
  642. return vfloat8(svabs_f32_x(svptrue_b32(), a.m));
  643. }
  644. /**
  645. * @brief Return a float rounded to the nearest integer value.
  646. */
  647. ASTCENC_SIMD_INLINE vfloat8 round(vfloat8 a)
  648. {
  649. return vfloat8(svrintn_f32_x(svptrue_b32(), a.m));
  650. }
  651. /**
  652. * @brief Return the horizontal minimum of a vector.
  653. */
  654. ASTCENC_SIMD_INLINE vfloat8 hmin(vfloat8 a)
  655. {
  656. return vfloat8(svminnmv_f32(svptrue_b32(), a.m));
  657. }
  658. /**
  659. * @brief Return the horizontal minimum of a vector.
  660. */
  661. ASTCENC_SIMD_INLINE float hmin_s(vfloat8 a)
  662. {
  663. return svminnmv_f32(svptrue_b32(), a.m);
  664. }
  665. /**
  666. * @brief Return the horizontal maximum of a vector.
  667. */
  668. ASTCENC_SIMD_INLINE vfloat8 hmax(vfloat8 a)
  669. {
  670. return vfloat8(svmaxnmv_f32(svptrue_b32(), a.m));
  671. }
  672. /**
  673. * @brief Return the horizontal maximum of a vector.
  674. */
  675. ASTCENC_SIMD_INLINE float hmax_s(vfloat8 a)
  676. {
  677. return svmaxnmv_f32(svptrue_b32(), a.m);
  678. }
  679. /**
  680. * @brief Return the horizontal sum of a vector.
  681. */
  682. ASTCENC_SIMD_INLINE float hadd_s(vfloat8 a)
  683. {
  684. // Can't use svaddv - it's not invariant
  685. vfloat4 lo(svget_neonq_f32(a.m));
  686. vfloat4 hi(svget_neonq_f32(svext_f32(a.m, a.m, 4)));
  687. return hadd_s(lo) + hadd_s(hi);
  688. }
  689. /**
  690. * @brief Return lanes from @c b if @c cond is set, else @c a.
  691. */
  692. ASTCENC_SIMD_INLINE vfloat8 select(vfloat8 a, vfloat8 b, vmask8 cond)
  693. {
  694. return vfloat8(svsel_f32(cond.m, b.m, a.m));
  695. }
  696. /**
  697. * @brief Accumulate lane-wise sums for a vector, folded 4-wide.
  698. *
  699. * This is invariant with 4-wide implementations.
  700. */
  701. ASTCENC_SIMD_INLINE void haccumulate(vfloat4& accum, vfloat8 a)
  702. {
  703. vfloat4 lo(svget_neonq_f32(a.m));
  704. haccumulate(accum, lo);
  705. vfloat4 hi(svget_neonq_f32(svext_f32(a.m, a.m, 4)));
  706. haccumulate(accum, hi);
  707. }
  708. /**
  709. * @brief Accumulate lane-wise sums for a vector.
  710. *
  711. * This is NOT invariant with 4-wide implementations.
  712. */
  713. ASTCENC_SIMD_INLINE void haccumulate(vfloat8& accum, vfloat8 a)
  714. {
  715. accum += a;
  716. }
  717. /**
  718. * @brief Accumulate masked lane-wise sums for a vector, folded 4-wide.
  719. *
  720. * This is invariant with 4-wide implementations.
  721. */
  722. ASTCENC_SIMD_INLINE void haccumulate(vfloat4& accum, vfloat8 a, vmask8 m)
  723. {
  724. a = select(vfloat8::zero(), a, m);
  725. haccumulate(accum, a);
  726. }
  727. /**
  728. * @brief Accumulate masked lane-wise sums for a vector.
  729. *
  730. * This is NOT invariant with 4-wide implementations.
  731. */
  732. ASTCENC_SIMD_INLINE void haccumulate(vfloat8& accum, vfloat8 a, vmask8 m)
  733. {
  734. accum.m = svadd_f32_m(m.m, accum.m, a.m);
  735. }
  736. /**
  737. * @brief Return the sqrt of the lanes in the vector.
  738. */
  739. ASTCENC_SIMD_INLINE vfloat8 sqrt(vfloat8 a)
  740. {
  741. return vfloat8(svsqrt_f32_x(svptrue_b32(), a.m));
  742. }
  743. /**
  744. * @brief Load a vector of gathered results from an array;
  745. */
  746. ASTCENC_SIMD_INLINE vfloat8 gatherf(const float* base, vint8 indices)
  747. {
  748. return vfloat8(svld1_gather_s32index_f32(svptrue_b32(), base, indices.m));
  749. }
  750. /**
  751. * @brief Load a vector of gathered results from an array using byte indices from memory
  752. */
  753. template<>
  754. ASTCENC_SIMD_INLINE vfloat8 gatherf_byte_inds<vfloat8>(const float* base, const uint8_t* indices)
  755. {
  756. svint32_t offsets = svld1ub_s32(svptrue_b32(), indices);
  757. return vfloat8(svld1_gather_s32index_f32(svptrue_b32(), base, offsets));
  758. }
  759. /**
  760. * @brief Store a vector to an unaligned memory address.
  761. */
  762. ASTCENC_SIMD_INLINE void store(vfloat8 a, float* p)
  763. {
  764. svst1_f32(svptrue_b32(), p, a.m);
  765. }
  766. /**
  767. * @brief Store a vector to a 32B aligned memory address.
  768. */
  769. ASTCENC_SIMD_INLINE void storea(vfloat8 a, float* p)
  770. {
  771. svst1_f32(svptrue_b32(), p, a.m);
  772. }
  773. /**
  774. * @brief Return a integer value for a float vector, using truncation.
  775. */
  776. ASTCENC_SIMD_INLINE vint8 float_to_int(vfloat8 a)
  777. {
  778. return vint8(svcvt_s32_f32_x(svptrue_b32(), a.m));
  779. }
  780. /**
  781. * @brief Return a integer value for a float vector, using round-to-nearest.
  782. */
  783. ASTCENC_SIMD_INLINE vint8 float_to_int_rtn(vfloat8 a)
  784. {
  785. a = a + vfloat8(0.5f);
  786. return vint8(svcvt_s32_f32_x(svptrue_b32(), a.m));
  787. }
  788. /**
  789. * @brief Return a float value for an integer vector.
  790. */
  791. ASTCENC_SIMD_INLINE vfloat8 int_to_float(vint8 a)
  792. {
  793. return vfloat8(svcvt_f32_s32_x(svptrue_b32(), a.m));
  794. }
  795. /**
  796. * @brief Return a float value as an integer bit pattern (i.e. no conversion).
  797. *
  798. * It is a common trick to convert floats into integer bit patterns, perform
  799. * some bit hackery based on knowledge they are IEEE 754 layout, and then
  800. * convert them back again. This is the first half of that flip.
  801. */
  802. ASTCENC_SIMD_INLINE vint8 float_as_int(vfloat8 a)
  803. {
  804. return vint8(svreinterpret_s32_f32(a.m));
  805. }
  806. /**
  807. * @brief Return a integer value as a float bit pattern (i.e. no conversion).
  808. *
  809. * It is a common trick to convert floats into integer bit patterns, perform
  810. * some bit hackery based on knowledge they are IEEE 754 layout, and then
  811. * convert them back again. This is the second half of that flip.
  812. */
  813. ASTCENC_SIMD_INLINE vfloat8 int_as_float(vint8 a)
  814. {
  815. return vfloat8(svreinterpret_f32_s32(a.m));
  816. }
  817. /*
  818. * Table structure for a 16x 8-bit entry table.
  819. */
  820. struct vtable8_16x8 {
  821. svuint8_8_t t0;
  822. };
  823. /*
  824. * Table structure for a 32x 8-bit entry table.
  825. */
  826. struct vtable8_32x8 {
  827. svuint8_8_t t0;
  828. };
  829. /*
  830. * Table structure for a 64x 8-bit entry table.
  831. */
  832. struct vtable8_64x8 {
  833. svuint8_8_t t0;
  834. svuint8_8_t t1;
  835. };
  836. /**
  837. * @brief Prepare a vtable lookup table for 16x 8-bit entry table.
  838. */
  839. ASTCENC_SIMD_INLINE void vtable_prepare(
  840. vtable8_16x8& table,
  841. const uint8_t* data
  842. ) {
  843. // Top half of register will be zeros
  844. table.t0 = svld1_u8(svptrue_pat_b8(SV_VL16), data);
  845. }
  846. /**
  847. * @brief Prepare a vtable lookup table for 32x 8-bit entry table.
  848. */
  849. ASTCENC_SIMD_INLINE void vtable_prepare(
  850. vtable8_32x8& table,
  851. const uint8_t* data
  852. ) {
  853. table.t0 = svld1_u8(svptrue_b8(), data);
  854. }
  855. /**
  856. * @brief Prepare a vtable lookup table 64x 8-bit entry table.
  857. */
  858. ASTCENC_SIMD_INLINE void vtable_prepare(
  859. vtable8_64x8& table,
  860. const uint8_t* data
  861. ) {
  862. table.t0 = svld1_u8(svptrue_b8(), data);
  863. table.t1 = svld1_u8(svptrue_b8(), data + 32);
  864. }
  865. /**
  866. * @brief Perform a vtable lookup in a 16x 8-bit table with 32-bit indices.
  867. */
  868. ASTCENC_SIMD_INLINE vint8 vtable_lookup_32bit(
  869. const vtable8_16x8& tbl,
  870. vint8 idx
  871. ) {
  872. // Set index byte above max index for unused bytes so table lookup returns zero
  873. svint32_8_t idx_masked = svorr_s32_x(svptrue_b32(), idx.m, svdup_s32(0xFFFFFF00));
  874. svuint8_8_t idx_bytes = svreinterpret_u8_s32(idx_masked);
  875. svuint8_8_t result = svtbl_u8(tbl.t0, idx_bytes);
  876. return vint8(svreinterpret_s32_u8(result));
  877. }
  878. /**
  879. * @brief Perform a vtable lookup in a 32x 8-bit table with 32-bit indices.
  880. */
  881. ASTCENC_SIMD_INLINE vint8 vtable_lookup_32bit(
  882. const vtable8_32x8& tbl,
  883. vint8 idx
  884. ) {
  885. // Set index byte above max index for unused bytes so table lookup returns zero
  886. svint32_8_t idx_masked = svorr_s32_x(svptrue_b32(), idx.m, svdup_s32(0xFFFFFF00));
  887. svuint8_8_t idx_bytes = svreinterpret_u8_s32(idx_masked);
  888. svuint8_8_t result = svtbl_u8(tbl.t0, idx_bytes);
  889. return vint8(svreinterpret_s32_u8(result));
  890. }
  891. /**
  892. * @brief Perform a vtable lookup in a 64x 8-bit table with 32-bit indices.
  893. *
  894. * Future: SVE2 can directly do svtbl2_u8() for a two register table.
  895. */
  896. ASTCENC_SIMD_INLINE vint8 vtable_lookup_32bit(
  897. const vtable8_64x8& tbl,
  898. vint8 idx
  899. ) {
  900. // Set index byte above max index for unused bytes so table lookup returns zero
  901. svint32_8_t idxm = svorr_s32_x(svptrue_b32(), idx.m, svdup_s32(0xFFFFFF00));
  902. svuint8_8_t idxm8 = svreinterpret_u8_s32(idxm);
  903. svuint8_8_t t0_lookup = svtbl_u8(tbl.t0, idxm8);
  904. idxm8 = svsub_u8_x(svptrue_b8(), idxm8, svdup_u8(32));
  905. svuint8_8_t t1_lookup = svtbl_u8(tbl.t1, idxm8);
  906. svuint8_8_t result = svorr_u8_x(svptrue_b32(), t0_lookup, t1_lookup);
  907. return vint8(svreinterpret_s32_u8(result));
  908. }
  909. /**
  910. * @brief Return a vector of interleaved RGBA data.
  911. *
  912. * Input vectors have the value stored in the bottom 8 bits of each lane,
  913. * with high bits set to zero.
  914. *
  915. * Output vector stores a single RGBA texel packed in each lane.
  916. */
  917. ASTCENC_SIMD_INLINE vint8 interleave_rgba8(vint8 r, vint8 g, vint8 b, vint8 a)
  918. {
  919. return r + lsl<8>(g) + lsl<16>(b) + lsl<24>(a);
  920. }
  921. /**
  922. * @brief Store a vector, skipping masked lanes.
  923. *
  924. * All masked lanes must be at the end of vector, after all non-masked lanes.
  925. */
  926. ASTCENC_SIMD_INLINE void store_lanes_masked(uint8_t* base, vint8 data, vmask8 mask)
  927. {
  928. svst1_s32(mask.m, reinterpret_cast<int32_t*>(base), data.m);
  929. }
  930. /**
  931. * @brief Debug function to print a vector of ints.
  932. */
  933. ASTCENC_SIMD_INLINE void print(vint8 a)
  934. {
  935. alignas(32) int v[8];
  936. storea(a, v);
  937. printf("v8_i32:\n %8d %8d %8d %8d %8d %8d %8d %8d\n",
  938. v[0], v[1], v[2], v[3], v[4], v[5], v[6], v[7]);
  939. }
  940. /**
  941. * @brief Debug function to print a vector of ints.
  942. */
  943. ASTCENC_SIMD_INLINE void printx(vint8 a)
  944. {
  945. alignas(32) int v[8];
  946. storea(a, v);
  947. printf("v8_i32:\n %08x %08x %08x %08x %08x %08x %08x %08x\n",
  948. v[0], v[1], v[2], v[3], v[4], v[5], v[6], v[7]);
  949. }
  950. /**
  951. * @brief Debug function to print a vector of floats.
  952. */
  953. ASTCENC_SIMD_INLINE void print(vfloat8 a)
  954. {
  955. alignas(32) float v[8];
  956. storea(a, v);
  957. printf("v8_f32:\n %0.4f %0.4f %0.4f %0.4f %0.4f %0.4f %0.4f %0.4f\n",
  958. static_cast<double>(v[0]), static_cast<double>(v[1]),
  959. static_cast<double>(v[2]), static_cast<double>(v[3]),
  960. static_cast<double>(v[4]), static_cast<double>(v[5]),
  961. static_cast<double>(v[6]), static_cast<double>(v[7]));
  962. }
  963. /**
  964. * @brief Debug function to print a vector of masks.
  965. */
  966. ASTCENC_SIMD_INLINE void print(vmask8 a)
  967. {
  968. print(select(vint8(0), vint8(1), a));
  969. }
  970. #endif // #ifndef ASTC_VECMATHLIB_SVE_8_H_INCLUDED