astcenc_vecmathlib.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571
  1. // SPDX-License-Identifier: Apache-2.0
  2. // ----------------------------------------------------------------------------
  3. // Copyright 2019-2022 Arm Limited
  4. // Copyright 2008 Jose Fonseca
  5. //
  6. // Licensed under the Apache License, Version 2.0 (the "License"); you may not
  7. // use this file except in compliance with the License. You may obtain a copy
  8. // of the License at:
  9. //
  10. // http://www.apache.org/licenses/LICENSE-2.0
  11. //
  12. // Unless required by applicable law or agreed to in writing, software
  13. // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  14. // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  15. // License for the specific language governing permissions and limitations
  16. // under the License.
  17. // ----------------------------------------------------------------------------
  18. /*
  19. * This module implements vector support for floats, ints, and vector lane
  20. * control masks. It provides access to both explicit vector width types, and
  21. * flexible N-wide types where N can be determined at compile time.
  22. *
  23. * The design of this module encourages use of vector length agnostic code, via
  24. * the vint, vfloat, and vmask types. These will take on the widest SIMD vector
  25. * with that is available at compile time. The current vector width is
  26. * accessible for e.g. loop strides via the ASTCENC_SIMD_WIDTH constant.
  27. *
  28. * Explicit scalar types are accessible via the vint1, vfloat1, vmask1 types.
  29. * These are provided primarily for prototyping and algorithm debug of VLA
  30. * implementations.
  31. *
  32. * Explicit 4-wide types are accessible via the vint4, vfloat4, and vmask4
  33. * types. These are provided for use by VLA code, but are also expected to be
  34. * used as a fixed-width type and will supported a reference C++ fallback for
  35. * use on platforms without SIMD intrinsics.
  36. *
  37. * Explicit 8-wide types are accessible via the vint8, vfloat8, and vmask8
  38. * types. These are provide for use by VLA code, and are not expected to be
  39. * used as a fixed-width type in normal code. No reference C implementation is
  40. * provided on platforms without underlying SIMD intrinsics.
  41. *
  42. * With the current implementation ISA support is provided for:
  43. *
  44. * * 1-wide for scalar reference.
  45. * * 4-wide for Armv8-A NEON.
  46. * * 4-wide for x86-64 SSE2.
  47. * * 4-wide for x86-64 SSE4.1.
  48. * * 8-wide for x86-64 AVX2.
  49. */
  50. #ifndef ASTC_VECMATHLIB_H_INCLUDED
  51. #define ASTC_VECMATHLIB_H_INCLUDED
  52. #if ASTCENC_SSE != 0 || ASTCENC_AVX != 0
  53. #include <immintrin.h>
  54. #elif ASTCENC_NEON != 0
  55. #include <arm_neon.h>
  56. #endif
  57. #if !defined(__clang__) && defined(_MSC_VER)
  58. #define ASTCENC_SIMD_INLINE __forceinline
  59. #define ASTCENC_NO_INLINE
  60. #elif defined(__GNUC__) && !defined(__clang__)
  61. #define ASTCENC_SIMD_INLINE __attribute__((always_inline)) inline
  62. #define ASTCENC_NO_INLINE __attribute__ ((noinline))
  63. #else
  64. #define ASTCENC_SIMD_INLINE __attribute__((always_inline, nodebug)) inline
  65. #define ASTCENC_NO_INLINE __attribute__ ((noinline))
  66. #endif
  67. #if ASTCENC_AVX >= 2
  68. /* If we have AVX2 expose 8-wide VLA. */
  69. #include "astcenc_vecmathlib_sse_4.h"
  70. #include "astcenc_vecmathlib_common_4.h"
  71. #include "astcenc_vecmathlib_avx2_8.h"
  72. #define ASTCENC_SIMD_WIDTH 8
  73. using vfloat = vfloat8;
  74. #if defined(ASTCENC_NO_INVARIANCE)
  75. using vfloatacc = vfloat8;
  76. #else
  77. using vfloatacc = vfloat4;
  78. #endif
  79. using vint = vint8;
  80. using vmask = vmask8;
  81. constexpr auto loada = vfloat8::loada;
  82. constexpr auto load1 = vfloat8::load1;
  83. #elif ASTCENC_SSE >= 20
  84. /* If we have SSE expose 4-wide VLA, and 4-wide fixed width. */
  85. #include "astcenc_vecmathlib_sse_4.h"
  86. #include "astcenc_vecmathlib_common_4.h"
  87. #define ASTCENC_SIMD_WIDTH 4
  88. using vfloat = vfloat4;
  89. using vfloatacc = vfloat4;
  90. using vint = vint4;
  91. using vmask = vmask4;
  92. constexpr auto loada = vfloat4::loada;
  93. constexpr auto load1 = vfloat4::load1;
  94. #elif ASTCENC_NEON > 0
  95. /* If we have NEON expose 4-wide VLA. */
  96. #include "astcenc_vecmathlib_neon_4.h"
  97. #include "astcenc_vecmathlib_common_4.h"
  98. #define ASTCENC_SIMD_WIDTH 4
  99. using vfloat = vfloat4;
  100. using vfloatacc = vfloat4;
  101. using vint = vint4;
  102. using vmask = vmask4;
  103. constexpr auto loada = vfloat4::loada;
  104. constexpr auto load1 = vfloat4::load1;
  105. #else
  106. // If we have nothing expose 4-wide VLA, and 4-wide fixed width.
  107. // Note: We no longer expose the 1-wide scalar fallback because it is not
  108. // invariant with the 4-wide path due to algorithms that use horizontal
  109. // operations that accumulate a local vector sum before accumulating into
  110. // a running sum.
  111. //
  112. // For 4 items adding into an accumulator using 1-wide vectors the sum is:
  113. //
  114. // result = ((((sum + l0) + l1) + l2) + l3)
  115. //
  116. // ... whereas the accumulator for a 4-wide vector sum is:
  117. //
  118. // result = sum + ((l0 + l2) + (l1 + l3))
  119. //
  120. // In "normal maths" this is the same, but the floating point reassociation
  121. // differences mean that these will not produce the same result.
  122. #include "astcenc_vecmathlib_none_4.h"
  123. #include "astcenc_vecmathlib_common_4.h"
  124. #define ASTCENC_SIMD_WIDTH 4
  125. using vfloat = vfloat4;
  126. using vfloatacc = vfloat4;
  127. using vint = vint4;
  128. using vmask = vmask4;
  129. constexpr auto loada = vfloat4::loada;
  130. constexpr auto load1 = vfloat4::load1;
  131. #endif
  132. /**
  133. * @brief Round a count down to the largest multiple of 8.
  134. *
  135. * @param count The unrounded value.
  136. *
  137. * @return The rounded value.
  138. */
  139. ASTCENC_SIMD_INLINE unsigned int round_down_to_simd_multiple_8(unsigned int count)
  140. {
  141. return count & static_cast<unsigned int>(~(8 - 1));
  142. }
  143. /**
  144. * @brief Round a count down to the largest multiple of 4.
  145. *
  146. * @param count The unrounded value.
  147. *
  148. * @return The rounded value.
  149. */
  150. ASTCENC_SIMD_INLINE unsigned int round_down_to_simd_multiple_4(unsigned int count)
  151. {
  152. return count & static_cast<unsigned int>(~(4 - 1));
  153. }
  154. /**
  155. * @brief Round a count down to the largest multiple of the SIMD width.
  156. *
  157. * Assumption that the vector width is a power of two ...
  158. *
  159. * @param count The unrounded value.
  160. *
  161. * @return The rounded value.
  162. */
  163. ASTCENC_SIMD_INLINE unsigned int round_down_to_simd_multiple_vla(unsigned int count)
  164. {
  165. return count & static_cast<unsigned int>(~(ASTCENC_SIMD_WIDTH - 1));
  166. }
  167. /**
  168. * @brief Round a count up to the largest multiple of the SIMD width.
  169. *
  170. * Assumption that the vector width is a power of two ...
  171. *
  172. * @param count The unrounded value.
  173. *
  174. * @return The rounded value.
  175. */
  176. ASTCENC_SIMD_INLINE unsigned int round_up_to_simd_multiple_vla(unsigned int count)
  177. {
  178. unsigned int multiples = (count + ASTCENC_SIMD_WIDTH - 1) / ASTCENC_SIMD_WIDTH;
  179. return multiples * ASTCENC_SIMD_WIDTH;
  180. }
  181. /**
  182. * @brief Return @c a with lanes negated if the @c b lane is negative.
  183. */
  184. ASTCENC_SIMD_INLINE vfloat change_sign(vfloat a, vfloat b)
  185. {
  186. vint ia = float_as_int(a);
  187. vint ib = float_as_int(b);
  188. vint sign_mask(static_cast<int>(0x80000000));
  189. vint r = ia ^ (ib & sign_mask);
  190. return int_as_float(r);
  191. }
  192. /**
  193. * @brief Return fast, but approximate, vector atan(x).
  194. *
  195. * Max error of this implementation is 0.004883.
  196. */
  197. ASTCENC_SIMD_INLINE vfloat atan(vfloat x)
  198. {
  199. vmask c = abs(x) > vfloat(1.0f);
  200. vfloat z = change_sign(vfloat(astc::PI_OVER_TWO), x);
  201. vfloat y = select(x, vfloat(1.0f) / x, c);
  202. y = y / (y * y * vfloat(0.28f) + vfloat(1.0f));
  203. return select(y, z - y, c);
  204. }
  205. /**
  206. * @brief Return fast, but approximate, vector atan2(x, y).
  207. */
  208. ASTCENC_SIMD_INLINE vfloat atan2(vfloat y, vfloat x)
  209. {
  210. vfloat z = atan(abs(y / x));
  211. vmask xmask = vmask(float_as_int(x).m);
  212. return change_sign(select_msb(z, vfloat(astc::PI) - z, xmask), y);
  213. }
  214. /*
  215. * @brief Factory that returns a unit length 4 component vfloat4.
  216. */
  217. static ASTCENC_SIMD_INLINE vfloat4 unit4()
  218. {
  219. return vfloat4(0.5f);
  220. }
  221. /**
  222. * @brief Factory that returns a unit length 3 component vfloat4.
  223. */
  224. static ASTCENC_SIMD_INLINE vfloat4 unit3()
  225. {
  226. float val = 0.577350258827209473f;
  227. return vfloat4(val, val, val, 0.0f);
  228. }
  229. /**
  230. * @brief Factory that returns a unit length 2 component vfloat4.
  231. */
  232. static ASTCENC_SIMD_INLINE vfloat4 unit2()
  233. {
  234. float val = 0.707106769084930420f;
  235. return vfloat4(val, val, 0.0f, 0.0f);
  236. }
  237. /**
  238. * @brief Factory that returns a 3 component vfloat4.
  239. */
  240. static ASTCENC_SIMD_INLINE vfloat4 vfloat3(float a, float b, float c)
  241. {
  242. return vfloat4(a, b, c, 0.0f);
  243. }
  244. /**
  245. * @brief Factory that returns a 2 component vfloat4.
  246. */
  247. static ASTCENC_SIMD_INLINE vfloat4 vfloat2(float a, float b)
  248. {
  249. return vfloat4(a, b, 0.0f, 0.0f);
  250. }
  251. /**
  252. * @brief Normalize a non-zero length vector to unit length.
  253. */
  254. static ASTCENC_SIMD_INLINE vfloat4 normalize(vfloat4 a)
  255. {
  256. vfloat4 length = dot(a, a);
  257. return a / sqrt(length);
  258. }
  259. /**
  260. * @brief Normalize a vector, returning @c safe if len is zero.
  261. */
  262. static ASTCENC_SIMD_INLINE vfloat4 normalize_safe(vfloat4 a, vfloat4 safe)
  263. {
  264. vfloat4 length = dot(a, a);
  265. if (length.lane<0>() != 0.0f)
  266. {
  267. return a / sqrt(length);
  268. }
  269. return safe;
  270. }
  271. #define POLY0(x, c0) ( c0)
  272. #define POLY1(x, c0, c1) ((POLY0(x, c1) * x) + c0)
  273. #define POLY2(x, c0, c1, c2) ((POLY1(x, c1, c2) * x) + c0)
  274. #define POLY3(x, c0, c1, c2, c3) ((POLY2(x, c1, c2, c3) * x) + c0)
  275. #define POLY4(x, c0, c1, c2, c3, c4) ((POLY3(x, c1, c2, c3, c4) * x) + c0)
  276. #define POLY5(x, c0, c1, c2, c3, c4, c5) ((POLY4(x, c1, c2, c3, c4, c5) * x) + c0)
  277. /**
  278. * @brief Compute an approximate exp2(x) for each lane in the vector.
  279. *
  280. * Based on 5th degree minimax polynomials, ported from this blog
  281. * https://jrfonseca.blogspot.com/2008/09/fast-sse2-pow-tables-or-polynomials.html
  282. */
  283. static ASTCENC_SIMD_INLINE vfloat4 exp2(vfloat4 x)
  284. {
  285. x = clamp(-126.99999f, 129.0f, x);
  286. vint4 ipart = float_to_int(x - 0.5f);
  287. vfloat4 fpart = x - int_to_float(ipart);
  288. // Integer contrib, using 1 << ipart
  289. vfloat4 iexp = int_as_float(lsl<23>(ipart + 127));
  290. // Fractional contrib, using polynomial fit of 2^x in range [-0.5, 0.5)
  291. vfloat4 fexp = POLY5(fpart,
  292. 9.9999994e-1f,
  293. 6.9315308e-1f,
  294. 2.4015361e-1f,
  295. 5.5826318e-2f,
  296. 8.9893397e-3f,
  297. 1.8775767e-3f);
  298. return iexp * fexp;
  299. }
  300. /**
  301. * @brief Compute an approximate log2(x) for each lane in the vector.
  302. *
  303. * Based on 5th degree minimax polynomials, ported from this blog
  304. * https://jrfonseca.blogspot.com/2008/09/fast-sse2-pow-tables-or-polynomials.html
  305. */
  306. static ASTCENC_SIMD_INLINE vfloat4 log2(vfloat4 x)
  307. {
  308. vint4 exp(0x7F800000);
  309. vint4 mant(0x007FFFFF);
  310. vint4 one(0x3F800000);
  311. vint4 i = float_as_int(x);
  312. vfloat4 e = int_to_float(lsr<23>(i & exp) - 127);
  313. vfloat4 m = int_as_float((i & mant) | one);
  314. // Polynomial fit of log2(x)/(x - 1), for x in range [1, 2)
  315. vfloat4 p = POLY4(m,
  316. 2.8882704548164776201f,
  317. -2.52074962577807006663f,
  318. 1.48116647521213171641f,
  319. -0.465725644288844778798f,
  320. 0.0596515482674574969533f);
  321. // Increases the polynomial degree, but ensures that log2(1) == 0
  322. p = p * (m - 1.0f);
  323. return p + e;
  324. }
  325. /**
  326. * @brief Compute an approximate pow(x, y) for each lane in the vector.
  327. *
  328. * Power function based on the exp2(log2(x) * y) transform.
  329. */
  330. static ASTCENC_SIMD_INLINE vfloat4 pow(vfloat4 x, vfloat4 y)
  331. {
  332. vmask4 zero_mask = y == vfloat4(0.0f);
  333. vfloat4 estimate = exp2(log2(x) * y);
  334. // Guarantee that y == 0 returns exactly 1.0f
  335. return select(estimate, vfloat4(1.0f), zero_mask);
  336. }
  337. /**
  338. * @brief Count the leading zeros for each lane in @c a.
  339. *
  340. * Valid for all data values of @c a; will return a per-lane value [0, 32].
  341. */
  342. static ASTCENC_SIMD_INLINE vint4 clz(vint4 a)
  343. {
  344. // This function is a horrible abuse of floating point exponents to convert
  345. // the original integer value into a 2^N encoding we can recover easily.
  346. // Convert to float without risk of rounding up by keeping only top 8 bits.
  347. // This trick is is guaranteed to keep top 8 bits and clear the 9th.
  348. a = (~lsr<8>(a)) & a;
  349. a = float_as_int(int_to_float(a));
  350. // Extract and unbias exponent
  351. a = vint4(127 + 31) - lsr<23>(a);
  352. // Clamp result to a valid 32-bit range
  353. return clamp(0, 32, a);
  354. }
  355. /**
  356. * @brief Return lanewise 2^a for each lane in @c a.
  357. *
  358. * Use of signed int means that this is only valid for values in range [0, 31].
  359. */
  360. static ASTCENC_SIMD_INLINE vint4 two_to_the_n(vint4 a)
  361. {
  362. // 2^30 is the largest signed number than can be represented
  363. assert(all(a < vint4(31)));
  364. // This function is a horrible abuse of floating point to use the exponent
  365. // and float conversion to generate a 2^N multiple.
  366. // Bias the exponent
  367. vint4 exp = a + 127;
  368. exp = lsl<23>(exp);
  369. // Reinterpret the bits as a float, and then convert to an int
  370. vfloat4 f = int_as_float(exp);
  371. return float_to_int(f);
  372. }
  373. /**
  374. * @brief Convert unorm16 [0, 65535] to float16 in range [0, 1].
  375. */
  376. static ASTCENC_SIMD_INLINE vint4 unorm16_to_sf16(vint4 p)
  377. {
  378. vint4 fp16_one = vint4(0x3C00);
  379. vint4 fp16_small = lsl<8>(p);
  380. vmask4 is_one = p == vint4(0xFFFF);
  381. vmask4 is_small = p < vint4(4);
  382. // Manually inline clz() on Visual Studio to avoid release build codegen bug
  383. // see https://github.com/ARM-software/astc-encoder/issues/259
  384. #if !defined(__clang__) && defined(_MSC_VER)
  385. vint4 a = (~lsr<8>(p)) & p;
  386. a = float_as_int(int_to_float(a));
  387. a = vint4(127 + 31) - lsr<23>(a);
  388. vint4 lz = clamp(0, 32, a) - 16;
  389. #else
  390. vint4 lz = clz(p) - 16;
  391. #endif
  392. p = p * two_to_the_n(lz + 1);
  393. p = p & vint4(0xFFFF);
  394. p = lsr<6>(p);
  395. p = p | lsl<10>(vint4(14) - lz);
  396. vint4 r = select(p, fp16_one, is_one);
  397. r = select(r, fp16_small, is_small);
  398. return r;
  399. }
  400. /**
  401. * @brief Convert 16-bit LNS to float16.
  402. */
  403. static ASTCENC_SIMD_INLINE vint4 lns_to_sf16(vint4 p)
  404. {
  405. vint4 mc = p & 0x7FF;
  406. vint4 ec = lsr<11>(p);
  407. vint4 mc_512 = mc * 3;
  408. vmask4 mask_512 = mc < vint4(512);
  409. vint4 mc_1536 = mc * 4 - 512;
  410. vmask4 mask_1536 = mc < vint4(1536);
  411. vint4 mc_else = mc * 5 - 2048;
  412. vint4 mt = mc_else;
  413. mt = select(mt, mc_1536, mask_1536);
  414. mt = select(mt, mc_512, mask_512);
  415. vint4 res = lsl<10>(ec) | lsr<3>(mt);
  416. return min(res, vint4(0x7BFF));
  417. }
  418. /**
  419. * @brief Extract mantissa and exponent of a float value.
  420. *
  421. * @param a The input value.
  422. * @param[out] exp The output exponent.
  423. *
  424. * @return The mantissa.
  425. */
  426. static ASTCENC_SIMD_INLINE vfloat4 frexp(vfloat4 a, vint4& exp)
  427. {
  428. // Interpret the bits as an integer
  429. vint4 ai = float_as_int(a);
  430. // Extract and unbias the exponent
  431. exp = (lsr<23>(ai) & 0xFF) - 126;
  432. // Extract and unbias the mantissa
  433. vint4 manti = (ai & static_cast<int>(0x807FFFFF)) | 0x3F000000;
  434. return int_as_float(manti);
  435. }
  436. /**
  437. * @brief Convert float to 16-bit LNS.
  438. */
  439. static ASTCENC_SIMD_INLINE vfloat4 float_to_lns(vfloat4 a)
  440. {
  441. vint4 exp;
  442. vfloat4 mant = frexp(a, exp);
  443. // Do these early before we start messing about ...
  444. vmask4 mask_underflow_nan = ~(a > vfloat4(1.0f / 67108864.0f));
  445. vmask4 mask_infinity = a >= vfloat4(65536.0f);
  446. // If input is smaller than 2^-14, multiply by 2^25 and don't bias.
  447. vmask4 exp_lt_m13 = exp < vint4(-13);
  448. vfloat4 a1a = a * 33554432.0f;
  449. vint4 expa = vint4::zero();
  450. vfloat4 a1b = (mant - 0.5f) * 4096;
  451. vint4 expb = exp + 14;
  452. a = select(a1b, a1a, exp_lt_m13);
  453. exp = select(expb, expa, exp_lt_m13);
  454. vmask4 a_lt_384 = a < vfloat4(384.0f);
  455. vmask4 a_lt_1408 = a <= vfloat4(1408.0f);
  456. vfloat4 a2a = a * (4.0f / 3.0f);
  457. vfloat4 a2b = a + 128.0f;
  458. vfloat4 a2c = (a + 512.0f) * (4.0f / 5.0f);
  459. a = a2c;
  460. a = select(a, a2b, a_lt_1408);
  461. a = select(a, a2a, a_lt_384);
  462. a = a + (int_to_float(exp) * 2048.0f) + 1.0f;
  463. a = select(a, vfloat4(65535.0f), mask_infinity);
  464. a = select(a, vfloat4::zero(), mask_underflow_nan);
  465. return a;
  466. }
  467. namespace astc
  468. {
  469. static ASTCENC_SIMD_INLINE float pow(float x, float y)
  470. {
  471. return pow(vfloat4(x), vfloat4(y)).lane<0>();
  472. }
  473. }
  474. #endif // #ifndef ASTC_VECMATHLIB_H_INCLUDED