astcenc_mathlib.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489
  1. // SPDX-License-Identifier: Apache-2.0
  2. // ----------------------------------------------------------------------------
  3. // Copyright 2011-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. * This module implements a variety of mathematical data types and library
  19. * functions used by the codec.
  20. */
  21. #ifndef ASTC_MATHLIB_H_INCLUDED
  22. #define ASTC_MATHLIB_H_INCLUDED
  23. #include <cassert>
  24. #include <cstdint>
  25. #include <cmath>
  26. #ifndef ASTCENC_POPCNT
  27. #if defined(__POPCNT__)
  28. #define ASTCENC_POPCNT 1
  29. #else
  30. #define ASTCENC_POPCNT 0
  31. #endif
  32. #endif
  33. #ifndef ASTCENC_F16C
  34. #if defined(__F16C__)
  35. #define ASTCENC_F16C 1
  36. #else
  37. #define ASTCENC_F16C 0
  38. #endif
  39. #endif
  40. #ifndef ASTCENC_SSE
  41. #if defined(__SSE4_2__)
  42. #define ASTCENC_SSE 42
  43. #elif defined(__SSE4_1__)
  44. #define ASTCENC_SSE 41
  45. #elif defined(__SSE2__)
  46. #define ASTCENC_SSE 20
  47. #else
  48. #define ASTCENC_SSE 0
  49. #endif
  50. #endif
  51. #ifndef ASTCENC_AVX
  52. #if defined(__AVX2__)
  53. #define ASTCENC_AVX 2
  54. #elif defined(__AVX__)
  55. #define ASTCENC_AVX 1
  56. #else
  57. #define ASTCENC_AVX 0
  58. #endif
  59. #endif
  60. #ifndef ASTCENC_NEON
  61. #if defined(__aarch64__)
  62. #define ASTCENC_NEON 1
  63. #else
  64. #define ASTCENC_NEON 0
  65. #endif
  66. #endif
  67. // Force vector-sized SIMD alignment
  68. #if ASTCENC_AVX
  69. #define ASTCENC_VECALIGN 32
  70. #elif ASTCENC_SSE || ASTCENC_NEON
  71. #define ASTCENC_VECALIGN 16
  72. // Use default alignment for non-SIMD builds
  73. #else
  74. #define ASTCENC_VECALIGN 0
  75. #endif
  76. // C++11 states that alignas(0) should be ignored but GCC doesn't do
  77. // this on some versions, so workaround and avoid emitting alignas(0)
  78. #if ASTCENC_VECALIGN > 0
  79. #define ASTCENC_ALIGNAS alignas(ASTCENC_VECALIGN)
  80. #else
  81. #define ASTCENC_ALIGNAS
  82. #endif
  83. #if ASTCENC_SSE != 0 || ASTCENC_AVX != 0 || ASTCENC_POPCNT != 0
  84. #include <immintrin.h>
  85. #endif
  86. /* ============================================================================
  87. Fast math library; note that many of the higher-order functions in this set
  88. use approximations which are less accurate, but faster, than <cmath> standard
  89. library equivalents.
  90. Note: Many of these are not necessarily faster than simple C versions when
  91. used on a single scalar value, but are included for testing purposes as most
  92. have an option based on SSE intrinsics and therefore provide an obvious route
  93. to future vectorization.
  94. ============================================================================ */
  95. // Union for manipulation of float bit patterns
  96. typedef union
  97. {
  98. uint32_t u;
  99. int32_t s;
  100. float f;
  101. } if32;
  102. // These are namespaced to avoid colliding with C standard library functions.
  103. namespace astc
  104. {
  105. static const float PI = 3.14159265358979323846f;
  106. static const float PI_OVER_TWO = 1.57079632679489661923f;
  107. /**
  108. * @brief SP float absolute value.
  109. *
  110. * @param v The value to make absolute.
  111. *
  112. * @return The absolute value.
  113. */
  114. static inline float fabs(float v)
  115. {
  116. return std::fabs(v);
  117. }
  118. /**
  119. * @brief Test if a float value is a nan.
  120. *
  121. * @param v The value test.
  122. *
  123. * @return Zero is not a NaN, non-zero otherwise.
  124. */
  125. static inline bool isnan(float v)
  126. {
  127. return v != v;
  128. }
  129. /**
  130. * @brief Return the minimum of two values.
  131. *
  132. * For floats, NaNs are turned into @c q.
  133. *
  134. * @param p The first value to compare.
  135. * @param q The second value to compare.
  136. *
  137. * @return The smallest value.
  138. */
  139. template<typename T>
  140. static inline T min(T p, T q)
  141. {
  142. return p < q ? p : q;
  143. }
  144. /**
  145. * @brief Return the minimum of three values.
  146. *
  147. * For floats, NaNs are turned into @c r.
  148. *
  149. * @param p The first value to compare.
  150. * @param q The second value to compare.
  151. * @param r The third value to compare.
  152. *
  153. * @return The smallest value.
  154. */
  155. template<typename T>
  156. static inline T min(T p, T q, T r)
  157. {
  158. return min(min(p, q), r);
  159. }
  160. /**
  161. * @brief Return the minimum of four values.
  162. *
  163. * For floats, NaNs are turned into @c s.
  164. *
  165. * @param p The first value to compare.
  166. * @param q The second value to compare.
  167. * @param r The third value to compare.
  168. * @param s The fourth value to compare.
  169. *
  170. * @return The smallest value.
  171. */
  172. template<typename T>
  173. static inline T min(T p, T q, T r, T s)
  174. {
  175. return min(min(p, q), min(r, s));
  176. }
  177. /**
  178. * @brief Return the maximum of two values.
  179. *
  180. * For floats, NaNs are turned into @c q.
  181. *
  182. * @param p The first value to compare.
  183. * @param q The second value to compare.
  184. *
  185. * @return The largest value.
  186. */
  187. template<typename T>
  188. static inline T max(T p, T q)
  189. {
  190. return p > q ? p : q;
  191. }
  192. /**
  193. * @brief Return the maximum of three values.
  194. *
  195. * For floats, NaNs are turned into @c r.
  196. *
  197. * @param p The first value to compare.
  198. * @param q The second value to compare.
  199. * @param r The third value to compare.
  200. *
  201. * @return The largest value.
  202. */
  203. template<typename T>
  204. static inline T max(T p, T q, T r)
  205. {
  206. return max(max(p, q), r);
  207. }
  208. /**
  209. * @brief Return the maximum of four values.
  210. *
  211. * For floats, NaNs are turned into @c s.
  212. *
  213. * @param p The first value to compare.
  214. * @param q The second value to compare.
  215. * @param r The third value to compare.
  216. * @param s The fourth value to compare.
  217. *
  218. * @return The largest value.
  219. */
  220. template<typename T>
  221. static inline T max(T p, T q, T r, T s)
  222. {
  223. return max(max(p, q), max(r, s));
  224. }
  225. /**
  226. * @brief Clamp a value value between @c mn and @c mx.
  227. *
  228. * For floats, NaNs are turned into @c mn.
  229. *
  230. * @param v The value to clamp.
  231. * @param mn The min value (inclusive).
  232. * @param mx The max value (inclusive).
  233. *
  234. * @return The clamped value.
  235. */
  236. template<typename T>
  237. inline T clamp(T v, T mn, T mx)
  238. {
  239. // Do not reorder; correct NaN handling relies on the fact that comparison
  240. // with NaN returns false and will fall-though to the "min" value.
  241. if (v > mx) return mx;
  242. if (v > mn) return v;
  243. return mn;
  244. }
  245. /**
  246. * @brief Clamp a float value between 0.0f and 1.0f.
  247. *
  248. * NaNs are turned into 0.0f.
  249. *
  250. * @param v The value to clamp.
  251. *
  252. * @return The clamped value.
  253. */
  254. static inline float clamp1f(float v)
  255. {
  256. return astc::clamp(v, 0.0f, 1.0f);
  257. }
  258. /**
  259. * @brief Clamp a float value between 0.0f and 255.0f.
  260. *
  261. * NaNs are turned into 0.0f.
  262. *
  263. * @param v The value to clamp.
  264. *
  265. * @return The clamped value.
  266. */
  267. static inline float clamp255f(float v)
  268. {
  269. return astc::clamp(v, 0.0f, 255.0f);
  270. }
  271. /**
  272. * @brief SP float round-down.
  273. *
  274. * @param v The value to round.
  275. *
  276. * @return The rounded value.
  277. */
  278. static inline float flt_rd(float v)
  279. {
  280. return std::floor(v);
  281. }
  282. /**
  283. * @brief SP float round-to-nearest and convert to integer.
  284. *
  285. * @param v The value to round.
  286. *
  287. * @return The rounded value.
  288. */
  289. static inline int flt2int_rtn(float v)
  290. {
  291. return static_cast<int>(v + 0.5f);
  292. }
  293. /**
  294. * @brief SP float round down and convert to integer.
  295. *
  296. * @param v The value to round.
  297. *
  298. * @return The rounded value.
  299. */
  300. static inline int flt2int_rd(float v)
  301. {
  302. return static_cast<int>(v);
  303. }
  304. /**
  305. * @brief SP float bit-interpreted as an integer.
  306. *
  307. * @param v The value to bitcast.
  308. *
  309. * @return The converted value.
  310. */
  311. static inline int float_as_int(float v)
  312. {
  313. union { int a; float b; } u;
  314. u.b = v;
  315. return u.a;
  316. }
  317. /**
  318. * @brief Integer bit-interpreted as an SP float.
  319. *
  320. * @param v The value to bitcast.
  321. *
  322. * @return The converted value.
  323. */
  324. static inline float int_as_float(int v)
  325. {
  326. union { int a; float b; } u;
  327. u.a = v;
  328. return u.b;
  329. }
  330. /**
  331. * @brief Fast approximation of 1.0 / sqrt(val).
  332. *
  333. * @param v The input value.
  334. *
  335. * @return The approximated result.
  336. */
  337. static inline float rsqrt(float v)
  338. {
  339. return 1.0f / std::sqrt(v);
  340. }
  341. /**
  342. * @brief Fast approximation of sqrt(val).
  343. *
  344. * @param v The input value.
  345. *
  346. * @return The approximated result.
  347. */
  348. static inline float sqrt(float v)
  349. {
  350. return std::sqrt(v);
  351. }
  352. /**
  353. * @brief Extract mantissa and exponent of a float value.
  354. *
  355. * @param v The input value.
  356. * @param[out] expo The output exponent.
  357. *
  358. * @return The mantissa.
  359. */
  360. static inline float frexp(float v, int* expo)
  361. {
  362. if32 p;
  363. p.f = v;
  364. *expo = ((p.u >> 23) & 0xFF) - 126;
  365. p.u = (p.u & 0x807fffff) | 0x3f000000;
  366. return p.f;
  367. }
  368. /**
  369. * @brief Initialize the seed structure for a random number generator.
  370. *
  371. * Important note: For the purposes of ASTC we want sets of random numbers to
  372. * use the codec, but we want the same seed value across instances and threads
  373. * to ensure that image output is stable across compressor runs and across
  374. * platforms. Every PRNG created by this call will therefore return the same
  375. * sequence of values ...
  376. *
  377. * @param state The state structure to initialize.
  378. */
  379. void rand_init(uint64_t state[2]);
  380. /**
  381. * @brief Return the next random number from the generator.
  382. *
  383. * This RNG is an implementation of the "xoroshoro-128+ 1.0" PRNG, based on the
  384. * public-domain implementation given by David Blackman & Sebastiano Vigna at
  385. * http://vigna.di.unimi.it/xorshift/xoroshiro128plus.c
  386. *
  387. * @param state The state structure to use/update.
  388. */
  389. uint64_t rand(uint64_t state[2]);
  390. }
  391. /* ============================================================================
  392. Softfloat library with fp32 and fp16 conversion functionality.
  393. ============================================================================ */
  394. #if (ASTCENC_F16C == 0) && (ASTCENC_NEON == 0)
  395. /* narrowing float->float conversions */
  396. uint16_t float_to_sf16(float val);
  397. float sf16_to_float(uint16_t val);
  398. #endif
  399. /*********************************
  400. Vector library
  401. *********************************/
  402. #include "astcenc_vecmathlib.h"
  403. /*********************************
  404. Declaration of line types
  405. *********************************/
  406. // parametric line, 2D: The line is given by line = a + b * t.
  407. struct line2
  408. {
  409. vfloat4 a;
  410. vfloat4 b;
  411. };
  412. // parametric line, 3D
  413. struct line3
  414. {
  415. vfloat4 a;
  416. vfloat4 b;
  417. };
  418. struct line4
  419. {
  420. vfloat4 a;
  421. vfloat4 b;
  422. };
  423. struct processed_line2
  424. {
  425. vfloat4 amod;
  426. vfloat4 bs;
  427. };
  428. struct processed_line3
  429. {
  430. vfloat4 amod;
  431. vfloat4 bs;
  432. };
  433. struct processed_line4
  434. {
  435. vfloat4 amod;
  436. vfloat4 bs;
  437. };
  438. #endif