| 1 | // This file is part of meshoptimizer library; see meshoptimizer.h for version/license details |
| 2 | #include "meshoptimizer.h" |
| 3 | |
| 4 | #include <math.h> |
| 5 | #include <string.h> |
| 6 | |
| 7 | // The block below auto-detects SIMD ISA that can be used on the target platform |
| 8 | #ifndef MESHOPTIMIZER_NO_SIMD |
| 9 | |
| 10 | // The SIMD implementation requires SSE2, which can be enabled unconditionally through compiler settings |
| 11 | #if defined(__SSE2__) |
| 12 | #define SIMD_SSE |
| 13 | #endif |
| 14 | |
| 15 | // MSVC supports compiling SSE2 code regardless of compile options; we assume all 32-bit CPUs support SSE2 |
| 16 | #if !defined(SIMD_SSE) && defined(_MSC_VER) && !defined(__clang__) && (defined(_M_IX86) || defined(_M_X64)) |
| 17 | #define SIMD_SSE |
| 18 | #endif |
| 19 | |
| 20 | // GCC/clang define these when NEON support is available |
| 21 | #if defined(__ARM_NEON__) || defined(__ARM_NEON) |
| 22 | #define SIMD_NEON |
| 23 | #endif |
| 24 | |
| 25 | // On MSVC, we assume that ARM builds always target NEON-capable devices |
| 26 | #if !defined(SIMD_NEON) && defined(_MSC_VER) && (defined(_M_ARM) || defined(_M_ARM64)) |
| 27 | #define SIMD_NEON |
| 28 | #endif |
| 29 | |
| 30 | // When targeting Wasm SIMD we can't use runtime cpuid checks so we unconditionally enable SIMD |
| 31 | #if defined(__wasm_simd128__) |
| 32 | #define SIMD_WASM |
| 33 | // Prevent compiling other variant when wasm simd compilation is active |
| 34 | #undef SIMD_NEON |
| 35 | #undef SIMD_SSE |
| 36 | #endif |
| 37 | |
| 38 | #endif // !MESHOPTIMIZER_NO_SIMD |
| 39 | |
| 40 | #ifdef SIMD_SSE |
| 41 | #include <emmintrin.h> |
| 42 | #include <stdint.h> |
| 43 | #endif |
| 44 | |
| 45 | #ifdef _MSC_VER |
| 46 | #include <intrin.h> |
| 47 | #endif |
| 48 | |
| 49 | #ifdef SIMD_NEON |
| 50 | #if defined(_MSC_VER) && defined(_M_ARM64) |
| 51 | #include <arm64_neon.h> |
| 52 | #else |
| 53 | #include <arm_neon.h> |
| 54 | #endif |
| 55 | #endif |
| 56 | |
| 57 | #ifdef SIMD_WASM |
| 58 | #undef __DEPRECATED |
| 59 | #include <wasm_simd128.h> |
| 60 | #endif |
| 61 | |
| 62 | #ifdef SIMD_WASM |
| 63 | #define wasmx_unpacklo_v16x8(a, b) wasm_v16x8_shuffle(a, b, 0, 8, 1, 9, 2, 10, 3, 11) |
| 64 | #define wasmx_unpackhi_v16x8(a, b) wasm_v16x8_shuffle(a, b, 4, 12, 5, 13, 6, 14, 7, 15) |
| 65 | #define wasmx_unziplo_v32x4(a, b) wasm_v32x4_shuffle(a, b, 0, 2, 4, 6) |
| 66 | #define wasmx_unziphi_v32x4(a, b) wasm_v32x4_shuffle(a, b, 1, 3, 5, 7) |
| 67 | #endif |
| 68 | |
| 69 | #ifndef __has_builtin |
| 70 | #define __has_builtin(x) 0 |
| 71 | #endif |
| 72 | |
| 73 | namespace meshopt |
| 74 | { |
| 75 | |
| 76 | #if !defined(SIMD_SSE) && !defined(SIMD_NEON) && !defined(SIMD_WASM) |
| 77 | template <typename T> |
| 78 | static void decodeFilterOct(T* data, size_t count) |
| 79 | { |
| 80 | const float max = float((1 << (sizeof(T) * 8 - 1)) - 1); |
| 81 | |
| 82 | for (size_t i = 0; i < count; ++i) |
| 83 | { |
| 84 | // convert x and y to floats and reconstruct z; this assumes zf encodes 1.f at the same bit count |
| 85 | float x = float(data[i * 4 + 0]); |
| 86 | float y = float(data[i * 4 + 1]); |
| 87 | float z = float(data[i * 4 + 2]) - fabsf(x) - fabsf(y); |
| 88 | |
| 89 | // fixup octahedral coordinates for z<0 |
| 90 | float t = (z >= 0.f) ? 0.f : z; |
| 91 | |
| 92 | x += (x >= 0.f) ? t : -t; |
| 93 | y += (y >= 0.f) ? t : -t; |
| 94 | |
| 95 | // compute normal length & scale |
| 96 | float l = sqrtf(x * x + y * y + z * z); |
| 97 | float s = max / l; |
| 98 | |
| 99 | // rounded signed float->int |
| 100 | int xf = int(x * s + (x >= 0.f ? 0.5f : -0.5f)); |
| 101 | int yf = int(y * s + (y >= 0.f ? 0.5f : -0.5f)); |
| 102 | int zf = int(z * s + (z >= 0.f ? 0.5f : -0.5f)); |
| 103 | |
| 104 | data[i * 4 + 0] = T(xf); |
| 105 | data[i * 4 + 1] = T(yf); |
| 106 | data[i * 4 + 2] = T(zf); |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | static void decodeFilterQuat(short* data, size_t count) |
| 111 | { |
| 112 | const float scale = 32767.f / sqrtf(2.f); |
| 113 | |
| 114 | for (size_t i = 0; i < count; ++i) |
| 115 | { |
| 116 | // recover scale from the high byte of the component |
| 117 | int sf = data[i * 4 + 3] | 3; |
| 118 | float s = float(sf); |
| 119 | |
| 120 | // convert x/y/z to floating point (unscaled! implied scale of 1/sqrt(2.f) * 1/sf) |
| 121 | float x = float(data[i * 4 + 0]); |
| 122 | float y = float(data[i * 4 + 1]); |
| 123 | float z = float(data[i * 4 + 2]); |
| 124 | |
| 125 | // reconstruct w as a square root (unscaled); we clamp to 0.f to avoid NaN due to precision errors |
| 126 | float ws = s * s; |
| 127 | float ww = ws * 2.f - x * x - y * y - z * z; |
| 128 | float w = sqrtf(ww >= 0.f ? ww : 0.f); |
| 129 | |
| 130 | // compute final scale; note that all computations above are unscaled |
| 131 | // we need to divide by sf to get out of fixed point, divide by sqrt(2) to renormalize and multiply by 32767 to get to int16 range |
| 132 | float ss = scale / s; |
| 133 | |
| 134 | // rounded signed float->int |
| 135 | int xf = int(x * ss + (x >= 0.f ? 0.5f : -0.5f)); |
| 136 | int yf = int(y * ss + (y >= 0.f ? 0.5f : -0.5f)); |
| 137 | int zf = int(z * ss + (z >= 0.f ? 0.5f : -0.5f)); |
| 138 | int wf = int(w * ss + 0.5f); |
| 139 | |
| 140 | int qc = data[i * 4 + 3] & 3; |
| 141 | |
| 142 | // output order is dictated by input index |
| 143 | data[i * 4 + ((qc + 1) & 3)] = short(xf); |
| 144 | data[i * 4 + ((qc + 2) & 3)] = short(yf); |
| 145 | data[i * 4 + ((qc + 3) & 3)] = short(zf); |
| 146 | data[i * 4 + ((qc + 0) & 3)] = short(wf); |
| 147 | } |
| 148 | } |
| 149 | |
| 150 | static void decodeFilterExp(unsigned int* data, size_t count) |
| 151 | { |
| 152 | for (size_t i = 0; i < count; ++i) |
| 153 | { |
| 154 | unsigned int v = data[i]; |
| 155 | |
| 156 | // decode mantissa and exponent |
| 157 | int m = int(v << 8) >> 8; |
| 158 | int e = int(v) >> 24; |
| 159 | |
| 160 | union |
| 161 | { |
| 162 | float f; |
| 163 | unsigned int ui; |
| 164 | } u; |
| 165 | |
| 166 | // optimized version of ldexp(float(m), e) |
| 167 | u.ui = unsigned(e + 127) << 23; |
| 168 | u.f = u.f * float(m); |
| 169 | |
| 170 | data[i] = u.ui; |
| 171 | } |
| 172 | } |
| 173 | |
| 174 | template <typename ST, typename T> |
| 175 | static void decodeFilterColor(T* data, size_t count) |
| 176 | { |
| 177 | const float max = float((1 << (sizeof(T) * 8)) - 1); |
| 178 | |
| 179 | for (size_t i = 0; i < count; ++i) |
| 180 | { |
| 181 | // recover scale from alpha high bit |
| 182 | int as = data[i * 4 + 3]; |
| 183 | as |= as >> 1; |
| 184 | as |= as >> 2; |
| 185 | as |= as >> 4; |
| 186 | as |= as >> 8; // noop for 8-bit |
| 187 | |
| 188 | // convert to RGB in fixed point (co/cg are sign extended) |
| 189 | int y = data[i * 4 + 0], co = ST(data[i * 4 + 1]), cg = ST(data[i * 4 + 2]); |
| 190 | |
| 191 | int r = y + co - cg; |
| 192 | int g = y + cg; |
| 193 | int b = y - co - cg; |
| 194 | |
| 195 | // expand alpha by one bit to match other components |
| 196 | int a = data[i * 4 + 3]; |
| 197 | a = ((a << 1) & as) | (a & 1); |
| 198 | |
| 199 | // compute scaling factor |
| 200 | float ss = max / float(as); |
| 201 | |
| 202 | // rounded float->int |
| 203 | int rf = int(float(r) * ss + 0.5f); |
| 204 | int gf = int(float(g) * ss + 0.5f); |
| 205 | int bf = int(float(b) * ss + 0.5f); |
| 206 | int af = int(float(a) * ss + 0.5f); |
| 207 | |
| 208 | data[i * 4 + 0] = T(rf); |
| 209 | data[i * 4 + 1] = T(gf); |
| 210 | data[i * 4 + 2] = T(bf); |
| 211 | data[i * 4 + 3] = T(af); |
| 212 | } |
| 213 | } |
| 214 | #endif |
| 215 | |
| 216 | #if defined(SIMD_SSE) || defined(SIMD_NEON) || defined(SIMD_WASM) |
| 217 | template <typename T> |
| 218 | static void dispatchSimd(void (*process)(T*, size_t), T* data, size_t count, size_t stride) |
| 219 | { |
| 220 | assert(stride <= 4); |
| 221 | |
| 222 | size_t count4 = count & ~size_t(3); |
| 223 | process(data, count4); |
| 224 | |
| 225 | if (count4 < count) |
| 226 | { |
| 227 | T tail[4 * 4] = {}; // max stride 4, max count 4 |
| 228 | size_t tail_size = (count - count4) * stride * sizeof(T); |
| 229 | assert(tail_size <= sizeof(tail)); |
| 230 | |
| 231 | memcpy(tail, data + count4 * stride, tail_size); |
| 232 | process(tail, count - count4); |
| 233 | memcpy(data + count4 * stride, tail, tail_size); |
| 234 | } |
| 235 | } |
| 236 | |
| 237 | inline uint64_t rotateleft64(uint64_t v, int x) |
| 238 | { |
| 239 | #if defined(_MSC_VER) && !defined(__clang__) |
| 240 | return _rotl64(v, x); |
| 241 | #elif defined(__clang__) && __has_builtin(__builtin_rotateleft64) |
| 242 | return __builtin_rotateleft64(v, x); |
| 243 | #else |
| 244 | return (v << (x & 63)) | (v >> ((64 - x) & 63)); |
| 245 | #endif |
| 246 | } |
| 247 | #endif |
| 248 | |
| 249 | #ifdef SIMD_SSE |
| 250 | static void decodeFilterOctSimd8(signed char* data, size_t count) |
| 251 | { |
| 252 | const __m128 sign = _mm_set1_ps(-0.f); |
| 253 | |
| 254 | for (size_t i = 0; i < count; i += 4) |
| 255 | { |
| 256 | __m128i n4 = _mm_loadu_si128(reinterpret_cast<__m128i*>(&data[i * 4])); |
| 257 | |
| 258 | // sign-extends each of x,y in [x y ? ?] with arithmetic shifts |
| 259 | __m128i xf = _mm_srai_epi32(_mm_slli_epi32(n4, 24), 24); |
| 260 | __m128i yf = _mm_srai_epi32(_mm_slli_epi32(n4, 16), 24); |
| 261 | |
| 262 | // unpack z; note that z is unsigned so we technically don't need to sign extend it |
| 263 | __m128i zf = _mm_srai_epi32(_mm_slli_epi32(n4, 8), 24); |
| 264 | |
| 265 | // convert x and y to floats and reconstruct z; this assumes zf encodes 1.f at the same bit count |
| 266 | __m128 x = _mm_cvtepi32_ps(xf); |
| 267 | __m128 y = _mm_cvtepi32_ps(yf); |
| 268 | __m128 z = _mm_sub_ps(_mm_cvtepi32_ps(zf), _mm_add_ps(_mm_andnot_ps(sign, x), _mm_andnot_ps(sign, y))); |
| 269 | |
| 270 | // fixup octahedral coordinates for z<0 |
| 271 | __m128 t = _mm_min_ps(z, _mm_setzero_ps()); |
| 272 | |
| 273 | x = _mm_add_ps(x, _mm_xor_ps(t, _mm_and_ps(x, sign))); |
| 274 | y = _mm_add_ps(y, _mm_xor_ps(t, _mm_and_ps(y, sign))); |
| 275 | |
| 276 | // compute normal length & scale |
| 277 | __m128 ll = _mm_add_ps(_mm_mul_ps(x, x), _mm_add_ps(_mm_mul_ps(y, y), _mm_mul_ps(z, z))); |
| 278 | __m128 s = _mm_mul_ps(_mm_set1_ps(127.f), _mm_rsqrt_ps(ll)); |
| 279 | |
| 280 | // rounded signed float->int |
| 281 | __m128i xr = _mm_cvtps_epi32(_mm_mul_ps(x, s)); |
| 282 | __m128i yr = _mm_cvtps_epi32(_mm_mul_ps(y, s)); |
| 283 | __m128i zr = _mm_cvtps_epi32(_mm_mul_ps(z, s)); |
| 284 | |
| 285 | // combine xr/yr/zr into final value |
| 286 | __m128i res = _mm_and_si128(n4, _mm_set1_epi32(0xff000000)); |
| 287 | res = _mm_or_si128(res, _mm_and_si128(xr, _mm_set1_epi32(0xff))); |
| 288 | res = _mm_or_si128(res, _mm_slli_epi32(_mm_and_si128(yr, _mm_set1_epi32(0xff)), 8)); |
| 289 | res = _mm_or_si128(res, _mm_slli_epi32(_mm_and_si128(zr, _mm_set1_epi32(0xff)), 16)); |
| 290 | |
| 291 | _mm_storeu_si128(reinterpret_cast<__m128i*>(&data[i * 4]), res); |
| 292 | } |
| 293 | } |
| 294 | |
| 295 | static void decodeFilterOctSimd16(short* data, size_t count) |
| 296 | { |
| 297 | const __m128 sign = _mm_set1_ps(-0.f); |
| 298 | |
| 299 | for (size_t i = 0; i < count; i += 4) |
| 300 | { |
| 301 | __m128 n4_0 = _mm_loadu_ps(reinterpret_cast<float*>(&data[(i + 0) * 4])); |
| 302 | __m128 n4_1 = _mm_loadu_ps(reinterpret_cast<float*>(&data[(i + 2) * 4])); |
| 303 | |
| 304 | // gather both x/y 16-bit pairs in each 32-bit lane |
| 305 | __m128i n4 = _mm_castps_si128(_mm_shuffle_ps(n4_0, n4_1, _MM_SHUFFLE(2, 0, 2, 0))); |
| 306 | |
| 307 | // sign-extends each of x,y in [x y] with arithmetic shifts |
| 308 | __m128i xf = _mm_srai_epi32(_mm_slli_epi32(n4, 16), 16); |
| 309 | __m128i yf = _mm_srai_epi32(n4, 16); |
| 310 | |
| 311 | // unpack z; note that z is unsigned so we don't need to sign extend it |
| 312 | __m128i z4 = _mm_castps_si128(_mm_shuffle_ps(n4_0, n4_1, _MM_SHUFFLE(3, 1, 3, 1))); |
| 313 | __m128i zf = _mm_and_si128(z4, _mm_set1_epi32(0x7fff)); |
| 314 | |
| 315 | // convert x and y to floats and reconstruct z; this assumes zf encodes 1.f at the same bit count |
| 316 | __m128 x = _mm_cvtepi32_ps(xf); |
| 317 | __m128 y = _mm_cvtepi32_ps(yf); |
| 318 | __m128 z = _mm_sub_ps(_mm_cvtepi32_ps(zf), _mm_add_ps(_mm_andnot_ps(sign, x), _mm_andnot_ps(sign, y))); |
| 319 | |
| 320 | // fixup octahedral coordinates for z<0 |
| 321 | __m128 t = _mm_min_ps(z, _mm_setzero_ps()); |
| 322 | |
| 323 | x = _mm_add_ps(x, _mm_xor_ps(t, _mm_and_ps(x, sign))); |
| 324 | y = _mm_add_ps(y, _mm_xor_ps(t, _mm_and_ps(y, sign))); |
| 325 | |
| 326 | // compute normal length & scale |
| 327 | __m128 ll = _mm_add_ps(_mm_mul_ps(x, x), _mm_add_ps(_mm_mul_ps(y, y), _mm_mul_ps(z, z))); |
| 328 | __m128 s = _mm_div_ps(_mm_set1_ps(32767.f), _mm_sqrt_ps(ll)); |
| 329 | |
| 330 | // rounded signed float->int |
| 331 | __m128i xr = _mm_cvtps_epi32(_mm_mul_ps(x, s)); |
| 332 | __m128i yr = _mm_cvtps_epi32(_mm_mul_ps(y, s)); |
| 333 | __m128i zr = _mm_cvtps_epi32(_mm_mul_ps(z, s)); |
| 334 | |
| 335 | // mix x/z and y/0 to make 16-bit unpack easier |
| 336 | __m128i xzr = _mm_or_si128(_mm_and_si128(xr, _mm_set1_epi32(0xffff)), _mm_slli_epi32(zr, 16)); |
| 337 | __m128i y0r = _mm_and_si128(yr, _mm_set1_epi32(0xffff)); |
| 338 | |
| 339 | // pack x/y/z using 16-bit unpacks; note that this has 0 where we should have .w |
| 340 | __m128i res_0 = _mm_unpacklo_epi16(xzr, y0r); |
| 341 | __m128i res_1 = _mm_unpackhi_epi16(xzr, y0r); |
| 342 | |
| 343 | // patch in .w |
| 344 | __m128i maskw = _mm_set_epi32(0xffff0000, 0, 0xffff0000, 0); |
| 345 | res_0 = _mm_or_si128(res_0, _mm_and_si128(_mm_castps_si128(n4_0), maskw)); |
| 346 | res_1 = _mm_or_si128(res_1, _mm_and_si128(_mm_castps_si128(n4_1), maskw)); |
| 347 | |
| 348 | _mm_storeu_si128(reinterpret_cast<__m128i*>(&data[(i + 0) * 4]), res_0); |
| 349 | _mm_storeu_si128(reinterpret_cast<__m128i*>(&data[(i + 2) * 4]), res_1); |
| 350 | } |
| 351 | } |
| 352 | |
| 353 | static void decodeFilterQuatSimd(short* data, size_t count) |
| 354 | { |
| 355 | const float scale = 32767.f / sqrtf(2.f); |
| 356 | |
| 357 | for (size_t i = 0; i < count; i += 4) |
| 358 | { |
| 359 | __m128 q4_0 = _mm_loadu_ps(reinterpret_cast<float*>(&data[(i + 0) * 4])); |
| 360 | __m128 q4_1 = _mm_loadu_ps(reinterpret_cast<float*>(&data[(i + 2) * 4])); |
| 361 | |
| 362 | // gather both x/y 16-bit pairs in each 32-bit lane |
| 363 | __m128i q4_xy = _mm_castps_si128(_mm_shuffle_ps(q4_0, q4_1, _MM_SHUFFLE(2, 0, 2, 0))); |
| 364 | __m128i q4_zc = _mm_castps_si128(_mm_shuffle_ps(q4_0, q4_1, _MM_SHUFFLE(3, 1, 3, 1))); |
| 365 | |
| 366 | // sign-extends each of x,y in [x y] with arithmetic shifts |
| 367 | __m128i xf = _mm_srai_epi32(_mm_slli_epi32(q4_xy, 16), 16); |
| 368 | __m128i yf = _mm_srai_epi32(q4_xy, 16); |
| 369 | __m128i zf = _mm_srai_epi32(_mm_slli_epi32(q4_zc, 16), 16); |
| 370 | __m128i cf = _mm_srai_epi32(q4_zc, 16); |
| 371 | |
| 372 | // get a floating-point scaler using zc with bottom 2 bits set to 1 (which represents 1.f) |
| 373 | __m128i sf = _mm_or_si128(cf, _mm_set1_epi32(3)); |
| 374 | __m128 s = _mm_cvtepi32_ps(sf); |
| 375 | |
| 376 | // convert x/y/z to floating point (unscaled! implied scale of 1/sqrt(2.f) * 1/sf) |
| 377 | __m128 x = _mm_cvtepi32_ps(xf); |
| 378 | __m128 y = _mm_cvtepi32_ps(yf); |
| 379 | __m128 z = _mm_cvtepi32_ps(zf); |
| 380 | |
| 381 | // reconstruct w as a square root (unscaled); we clamp to 0.f to avoid NaN due to precision errors |
| 382 | __m128 ws = _mm_mul_ps(s, _mm_add_ps(s, s)); // s*2s instead of 2*(s*s) to work around clang bug with integer multiplication |
| 383 | __m128 ww = _mm_sub_ps(ws, _mm_add_ps(_mm_mul_ps(x, x), _mm_add_ps(_mm_mul_ps(y, y), _mm_mul_ps(z, z)))); |
| 384 | __m128 w = _mm_sqrt_ps(_mm_max_ps(ww, _mm_setzero_ps())); |
| 385 | |
| 386 | // compute final scale; note that all computations above are unscaled |
| 387 | // we need to divide by sf to get out of fixed point, divide by sqrt(2) to renormalize and multiply by 32767 to get to int16 range |
| 388 | __m128 ss = _mm_div_ps(_mm_set1_ps(scale), s); |
| 389 | |
| 390 | // rounded signed float->int |
| 391 | __m128i xr = _mm_cvtps_epi32(_mm_mul_ps(x, ss)); |
| 392 | __m128i yr = _mm_cvtps_epi32(_mm_mul_ps(y, ss)); |
| 393 | __m128i zr = _mm_cvtps_epi32(_mm_mul_ps(z, ss)); |
| 394 | __m128i wr = _mm_cvtps_epi32(_mm_mul_ps(w, ss)); |
| 395 | |
| 396 | // mix x/z and w/y to make 16-bit unpack easier |
| 397 | __m128i xzr = _mm_or_si128(_mm_and_si128(xr, _mm_set1_epi32(0xffff)), _mm_slli_epi32(zr, 16)); |
| 398 | __m128i wyr = _mm_or_si128(_mm_and_si128(wr, _mm_set1_epi32(0xffff)), _mm_slli_epi32(yr, 16)); |
| 399 | |
| 400 | // pack x/y/z/w using 16-bit unpacks; we pack wxyz by default (for qc=0) |
| 401 | __m128i res_0 = _mm_unpacklo_epi16(wyr, xzr); |
| 402 | __m128i res_1 = _mm_unpackhi_epi16(wyr, xzr); |
| 403 | |
| 404 | // store results to stack so that we can rotate using scalar instructions |
| 405 | uint64_t res[4]; |
| 406 | _mm_storeu_si128(reinterpret_cast<__m128i*>(&res[0]), res_0); |
| 407 | _mm_storeu_si128(reinterpret_cast<__m128i*>(&res[2]), res_1); |
| 408 | |
| 409 | // rotate and store |
| 410 | uint64_t* out = reinterpret_cast<uint64_t*>(&data[i * 4]); |
| 411 | |
| 412 | out[0] = rotateleft64(res[0], data[(i + 0) * 4 + 3] << 4); |
| 413 | out[1] = rotateleft64(res[1], data[(i + 1) * 4 + 3] << 4); |
| 414 | out[2] = rotateleft64(res[2], data[(i + 2) * 4 + 3] << 4); |
| 415 | out[3] = rotateleft64(res[3], data[(i + 3) * 4 + 3] << 4); |
| 416 | } |
| 417 | } |
| 418 | |
| 419 | static void decodeFilterExpSimd(unsigned int* data, size_t count) |
| 420 | { |
| 421 | for (size_t i = 0; i < count; i += 4) |
| 422 | { |
| 423 | __m128i v = _mm_loadu_si128(reinterpret_cast<__m128i*>(&data[i])); |
| 424 | |
| 425 | // decode exponent into 2^x directly |
| 426 | __m128i ef = _mm_srai_epi32(v, 24); |
| 427 | __m128i es = _mm_slli_epi32(_mm_add_epi32(ef, _mm_set1_epi32(127)), 23); |
| 428 | |
| 429 | // decode 24-bit mantissa into floating-point value |
| 430 | __m128i mf = _mm_srai_epi32(_mm_slli_epi32(v, 8), 8); |
| 431 | __m128 m = _mm_cvtepi32_ps(mf); |
| 432 | |
| 433 | __m128 r = _mm_mul_ps(_mm_castsi128_ps(es), m); |
| 434 | |
| 435 | _mm_storeu_ps(reinterpret_cast<float*>(&data[i]), r); |
| 436 | } |
| 437 | } |
| 438 | |
| 439 | static void decodeFilterColorSimd8(unsigned char* data, size_t count) |
| 440 | { |
| 441 | for (size_t i = 0; i < count; i += 4) |
| 442 | { |
| 443 | __m128i c4 = _mm_loadu_si128(reinterpret_cast<__m128i*>(&data[i * 4])); |
| 444 | |
| 445 | // unpack y/co/cg/a (co/cg are sign extended with arithmetic shifts) |
| 446 | __m128i yf = _mm_and_si128(c4, _mm_set1_epi32(0xff)); |
| 447 | __m128i cof = _mm_srai_epi32(_mm_slli_epi32(c4, 16), 24); |
| 448 | __m128i cgf = _mm_srai_epi32(_mm_slli_epi32(c4, 8), 24); |
| 449 | __m128i af = _mm_srli_epi32(c4, 24); |
| 450 | |
| 451 | // recover scale from alpha high bit |
| 452 | __m128i as = af; |
| 453 | as = _mm_or_si128(as, _mm_srli_epi32(as, 1)); |
| 454 | as = _mm_or_si128(as, _mm_srli_epi32(as, 2)); |
| 455 | as = _mm_or_si128(as, _mm_srli_epi32(as, 4)); |
| 456 | |
| 457 | // expand alpha by one bit to match other components |
| 458 | af = _mm_or_si128(_mm_and_si128(_mm_slli_epi32(af, 1), as), _mm_and_si128(af, _mm_set1_epi32(1))); |
| 459 | |
| 460 | // compute scaling factor |
| 461 | __m128 ss = _mm_mul_ps(_mm_set1_ps(255.f), _mm_rcp_ps(_mm_cvtepi32_ps(as))); |
| 462 | |
| 463 | // convert to RGB in fixed point |
| 464 | __m128i rf = _mm_add_epi32(yf, _mm_sub_epi32(cof, cgf)); |
| 465 | __m128i gf = _mm_add_epi32(yf, cgf); |
| 466 | __m128i bf = _mm_sub_epi32(yf, _mm_add_epi32(cof, cgf)); |
| 467 | |
| 468 | // rounded signed float->int |
| 469 | __m128i rr = _mm_cvtps_epi32(_mm_mul_ps(_mm_cvtepi32_ps(rf), ss)); |
| 470 | __m128i gr = _mm_cvtps_epi32(_mm_mul_ps(_mm_cvtepi32_ps(gf), ss)); |
| 471 | __m128i br = _mm_cvtps_epi32(_mm_mul_ps(_mm_cvtepi32_ps(bf), ss)); |
| 472 | __m128i ar = _mm_cvtps_epi32(_mm_mul_ps(_mm_cvtepi32_ps(af), ss)); |
| 473 | |
| 474 | // repack rgba into final value |
| 475 | __m128i res = rr; |
| 476 | res = _mm_or_si128(res, _mm_slli_epi32(gr, 8)); |
| 477 | res = _mm_or_si128(res, _mm_slli_epi32(br, 16)); |
| 478 | res = _mm_or_si128(res, _mm_slli_epi32(ar, 24)); |
| 479 | |
| 480 | _mm_storeu_si128(reinterpret_cast<__m128i*>(&data[i * 4]), res); |
| 481 | } |
| 482 | } |
| 483 | |
| 484 | static void decodeFilterColorSimd16(unsigned short* data, size_t count) |
| 485 | { |
| 486 | for (size_t i = 0; i < count; i += 4) |
| 487 | { |
| 488 | __m128i c4_0 = _mm_loadu_si128(reinterpret_cast<__m128i*>(&data[(i + 0) * 4])); |
| 489 | __m128i c4_1 = _mm_loadu_si128(reinterpret_cast<__m128i*>(&data[(i + 2) * 4])); |
| 490 | |
| 491 | // gather both y/co 16-bit pairs in each 32-bit lane |
| 492 | __m128i c4_yco = _mm_castps_si128(_mm_shuffle_ps(_mm_castsi128_ps(c4_0), _mm_castsi128_ps(c4_1), _MM_SHUFFLE(2, 0, 2, 0))); |
| 493 | __m128i c4_cga = _mm_castps_si128(_mm_shuffle_ps(_mm_castsi128_ps(c4_0), _mm_castsi128_ps(c4_1), _MM_SHUFFLE(3, 1, 3, 1))); |
| 494 | |
| 495 | // unpack y/co/cg/a components (co/cg are sign extended with arithmetic shifts) |
| 496 | __m128i yf = _mm_and_si128(c4_yco, _mm_set1_epi32(0xffff)); |
| 497 | __m128i cof = _mm_srai_epi32(c4_yco, 16); |
| 498 | __m128i cgf = _mm_srai_epi32(_mm_slli_epi32(c4_cga, 16), 16); |
| 499 | __m128i af = _mm_srli_epi32(c4_cga, 16); |
| 500 | |
| 501 | // recover scale from alpha high bit |
| 502 | __m128i as = af; |
| 503 | as = _mm_or_si128(as, _mm_srli_epi32(as, 1)); |
| 504 | as = _mm_or_si128(as, _mm_srli_epi32(as, 2)); |
| 505 | as = _mm_or_si128(as, _mm_srli_epi32(as, 4)); |
| 506 | as = _mm_or_si128(as, _mm_srli_epi32(as, 8)); |
| 507 | |
| 508 | // expand alpha by one bit to match other components |
| 509 | af = _mm_or_si128(_mm_and_si128(_mm_slli_epi32(af, 1), as), _mm_and_si128(af, _mm_set1_epi32(1))); |
| 510 | |
| 511 | // compute scaling factor |
| 512 | __m128 ss = _mm_div_ps(_mm_set1_ps(65535.f), _mm_cvtepi32_ps(as)); |
| 513 | |
| 514 | // convert to RGB in fixed point |
| 515 | __m128i rf = _mm_add_epi32(yf, _mm_sub_epi32(cof, cgf)); |
| 516 | __m128i gf = _mm_add_epi32(yf, cgf); |
| 517 | __m128i bf = _mm_sub_epi32(yf, _mm_add_epi32(cof, cgf)); |
| 518 | |
| 519 | // rounded signed float->int |
| 520 | __m128i rr = _mm_cvtps_epi32(_mm_mul_ps(_mm_cvtepi32_ps(rf), ss)); |
| 521 | __m128i gr = _mm_cvtps_epi32(_mm_mul_ps(_mm_cvtepi32_ps(gf), ss)); |
| 522 | __m128i br = _mm_cvtps_epi32(_mm_mul_ps(_mm_cvtepi32_ps(bf), ss)); |
| 523 | __m128i ar = _mm_cvtps_epi32(_mm_mul_ps(_mm_cvtepi32_ps(af), ss)); |
| 524 | |
| 525 | // mix r/b and g/a to make 16-bit unpack easier |
| 526 | __m128i rbr = _mm_or_si128(_mm_and_si128(rr, _mm_set1_epi32(0xffff)), _mm_slli_epi32(br, 16)); |
| 527 | __m128i gar = _mm_or_si128(_mm_and_si128(gr, _mm_set1_epi32(0xffff)), _mm_slli_epi32(ar, 16)); |
| 528 | |
| 529 | // pack r/g/b/a using 16-bit unpacks |
| 530 | __m128i res_0 = _mm_unpacklo_epi16(rbr, gar); |
| 531 | __m128i res_1 = _mm_unpackhi_epi16(rbr, gar); |
| 532 | |
| 533 | _mm_storeu_si128(reinterpret_cast<__m128i*>(&data[(i + 0) * 4]), res_0); |
| 534 | _mm_storeu_si128(reinterpret_cast<__m128i*>(&data[(i + 2) * 4]), res_1); |
| 535 | } |
| 536 | } |
| 537 | #endif |
| 538 | |
| 539 | #if defined(SIMD_NEON) && !defined(__aarch64__) && !defined(_M_ARM64) |
| 540 | inline float32x4_t vsqrtq_f32(float32x4_t x) |
| 541 | { |
| 542 | float32x4_t r = vrsqrteq_f32(x); |
| 543 | r = vmulq_f32(r, vrsqrtsq_f32(vmulq_f32(r, x), r)); // refine rsqrt estimate |
| 544 | return vmulq_f32(r, x); |
| 545 | } |
| 546 | |
| 547 | inline float32x4_t vdivq_f32(float32x4_t x, float32x4_t y) |
| 548 | { |
| 549 | float32x4_t r = vrecpeq_f32(y); |
| 550 | r = vmulq_f32(r, vrecpsq_f32(y, r)); // refine rcp estimate |
| 551 | return vmulq_f32(x, r); |
| 552 | } |
| 553 | |
| 554 | #ifndef __ARM_FEATURE_FMA |
| 555 | inline float32x4_t vfmaq_f32(float32x4_t x, float32x4_t y, float32x4_t z) |
| 556 | { |
| 557 | return vaddq_f32(x, vmulq_f32(y, z)); |
| 558 | } |
| 559 | #endif |
| 560 | #endif |
| 561 | |
| 562 | #ifdef SIMD_NEON |
| 563 | static void decodeFilterOctSimd8(signed char* data, size_t count) |
| 564 | { |
| 565 | const int32x4_t sign = vdupq_n_s32(0x80000000); |
| 566 | |
| 567 | for (size_t i = 0; i < count; i += 4) |
| 568 | { |
| 569 | int32x4_t n4 = vld1q_s32(reinterpret_cast<int32_t*>(&data[i * 4])); |
| 570 | |
| 571 | // sign-extends each of x,y in [x y ? ?] with arithmetic shifts |
| 572 | int32x4_t xf = vshrq_n_s32(vshlq_n_s32(n4, 24), 24); |
| 573 | int32x4_t yf = vshrq_n_s32(vshlq_n_s32(n4, 16), 24); |
| 574 | |
| 575 | // unpack z; note that z is unsigned so we technically don't need to sign extend it |
| 576 | int32x4_t zf = vshrq_n_s32(vshlq_n_s32(n4, 8), 24); |
| 577 | |
| 578 | // convert x and y to floats and reconstruct z; this assumes zf encodes 1.f at the same bit count |
| 579 | float32x4_t x = vcvtq_f32_s32(xf); |
| 580 | float32x4_t y = vcvtq_f32_s32(yf); |
| 581 | float32x4_t z = vsubq_f32(vcvtq_f32_s32(zf), vaddq_f32(vabsq_f32(x), vabsq_f32(y))); |
| 582 | |
| 583 | // fixup octahedral coordinates for z<0 |
| 584 | float32x4_t t = vminq_f32(z, vdupq_n_f32(0.f)); |
| 585 | |
| 586 | x = vaddq_f32(x, vreinterpretq_f32_s32(veorq_s32(vreinterpretq_s32_f32(t), vandq_s32(vreinterpretq_s32_f32(x), sign)))); |
| 587 | y = vaddq_f32(y, vreinterpretq_f32_s32(veorq_s32(vreinterpretq_s32_f32(t), vandq_s32(vreinterpretq_s32_f32(y), sign)))); |
| 588 | |
| 589 | // compute normal length & scale |
| 590 | float32x4_t ll = vfmaq_f32(vfmaq_f32(vmulq_f32(x, x), y, y), z, z); |
| 591 | float32x4_t rl = vrsqrteq_f32(ll); |
| 592 | float32x4_t s = vmulq_f32(vdupq_n_f32(127.f), rl); |
| 593 | |
| 594 | // fast rounded signed float->int: addition triggers renormalization after which mantissa stores the integer value |
| 595 | // note: the result is offset by 0x4B40_0000, but we only need the low 8 bits so we can omit the subtraction |
| 596 | const float32x4_t fsnap = vdupq_n_f32(3 << 22); |
| 597 | |
| 598 | int32x4_t xr = vreinterpretq_s32_f32(vfmaq_f32(fsnap, x, s)); |
| 599 | int32x4_t yr = vreinterpretq_s32_f32(vfmaq_f32(fsnap, y, s)); |
| 600 | int32x4_t zr = vreinterpretq_s32_f32(vfmaq_f32(fsnap, z, s)); |
| 601 | |
| 602 | // combine xr/yr/zr into final value |
| 603 | int32x4_t res = vsliq_n_s32(xr, vsliq_n_s32(yr, zr, 8), 8); |
| 604 | res = vbslq_s32(vdupq_n_u32(0xff000000), n4, res); |
| 605 | |
| 606 | vst1q_s32(reinterpret_cast<int32_t*>(&data[i * 4]), res); |
| 607 | } |
| 608 | } |
| 609 | |
| 610 | static void decodeFilterOctSimd16(short* data, size_t count) |
| 611 | { |
| 612 | const int32x4_t sign = vdupq_n_s32(0x80000000); |
| 613 | |
| 614 | for (size_t i = 0; i < count; i += 4) |
| 615 | { |
| 616 | int32x4_t n4_0 = vld1q_s32(reinterpret_cast<int32_t*>(&data[(i + 0) * 4])); |
| 617 | int32x4_t n4_1 = vld1q_s32(reinterpret_cast<int32_t*>(&data[(i + 2) * 4])); |
| 618 | |
| 619 | // gather both x/y 16-bit pairs in each 32-bit lane |
| 620 | int32x4_t n4 = vuzpq_s32(n4_0, n4_1).val[0]; |
| 621 | |
| 622 | // sign-extends each of x,y in [x y] with arithmetic shifts |
| 623 | int32x4_t xf = vshrq_n_s32(vshlq_n_s32(n4, 16), 16); |
| 624 | int32x4_t yf = vshrq_n_s32(n4, 16); |
| 625 | |
| 626 | // unpack z; note that z is unsigned so we don't need to sign extend it |
| 627 | int32x4_t z4 = vuzpq_s32(n4_0, n4_1).val[1]; |
| 628 | int32x4_t zf = vandq_s32(z4, vdupq_n_s32(0x7fff)); |
| 629 | |
| 630 | // convert x and y to floats and reconstruct z; this assumes zf encodes 1.f at the same bit count |
| 631 | float32x4_t x = vcvtq_f32_s32(xf); |
| 632 | float32x4_t y = vcvtq_f32_s32(yf); |
| 633 | float32x4_t z = vsubq_f32(vcvtq_f32_s32(zf), vaddq_f32(vabsq_f32(x), vabsq_f32(y))); |
| 634 | |
| 635 | // fixup octahedral coordinates for z<0 |
| 636 | float32x4_t t = vminq_f32(z, vdupq_n_f32(0.f)); |
| 637 | |
| 638 | x = vaddq_f32(x, vreinterpretq_f32_s32(veorq_s32(vreinterpretq_s32_f32(t), vandq_s32(vreinterpretq_s32_f32(x), sign)))); |
| 639 | y = vaddq_f32(y, vreinterpretq_f32_s32(veorq_s32(vreinterpretq_s32_f32(t), vandq_s32(vreinterpretq_s32_f32(y), sign)))); |
| 640 | |
| 641 | // compute normal length & scale |
| 642 | float32x4_t ll = vfmaq_f32(vfmaq_f32(vmulq_f32(x, x), y, y), z, z); |
| 643 | #if !defined(__aarch64__) && !defined(_M_ARM64) |
| 644 | float32x4_t rl = vrsqrteq_f32(ll); |
| 645 | rl = vmulq_f32(rl, vrsqrtsq_f32(vmulq_f32(rl, ll), rl)); // refine rsqrt estimate |
| 646 | float32x4_t s = vmulq_f32(vdupq_n_f32(32767.f), rl); |
| 647 | #else |
| 648 | float32x4_t s = vdivq_f32(vdupq_n_f32(32767.f), vsqrtq_f32(ll)); |
| 649 | #endif |
| 650 | |
| 651 | // fast rounded signed float->int: addition triggers renormalization after which mantissa stores the integer value |
| 652 | // note: the result is offset by 0x4B40_0000, but we only need the low 16 bits so we can omit the subtraction |
| 653 | const float32x4_t fsnap = vdupq_n_f32(3 << 22); |
| 654 | |
| 655 | int32x4_t xr = vreinterpretq_s32_f32(vfmaq_f32(fsnap, x, s)); |
| 656 | int32x4_t yr = vreinterpretq_s32_f32(vfmaq_f32(fsnap, y, s)); |
| 657 | int32x4_t zr = vreinterpretq_s32_f32(vfmaq_f32(fsnap, z, s)); |
| 658 | |
| 659 | // mix x/z and y/0 to make 16-bit unpack easier |
| 660 | int32x4_t xzr = vsliq_n_s32(xr, zr, 16); |
| 661 | int32x4_t y0r = vandq_s32(yr, vdupq_n_s32(0xffff)); |
| 662 | |
| 663 | // pack x/y/z using 16-bit unpacks; note that this has 0 where we should have .w |
| 664 | int32x4_t res_0 = vreinterpretq_s32_s16(vzipq_s16(vreinterpretq_s16_s32(xzr), vreinterpretq_s16_s32(y0r)).val[0]); |
| 665 | int32x4_t res_1 = vreinterpretq_s32_s16(vzipq_s16(vreinterpretq_s16_s32(xzr), vreinterpretq_s16_s32(y0r)).val[1]); |
| 666 | |
| 667 | // patch in .w |
| 668 | res_0 = vbslq_s32(vreinterpretq_u32_u64(vdupq_n_u64(0xffff000000000000)), n4_0, res_0); |
| 669 | res_1 = vbslq_s32(vreinterpretq_u32_u64(vdupq_n_u64(0xffff000000000000)), n4_1, res_1); |
| 670 | |
| 671 | vst1q_s32(reinterpret_cast<int32_t*>(&data[(i + 0) * 4]), res_0); |
| 672 | vst1q_s32(reinterpret_cast<int32_t*>(&data[(i + 2) * 4]), res_1); |
| 673 | } |
| 674 | } |
| 675 | |
| 676 | static void decodeFilterQuatSimd(short* data, size_t count) |
| 677 | { |
| 678 | const float scale = 32767.f / sqrtf(2.f); |
| 679 | |
| 680 | for (size_t i = 0; i < count; i += 4) |
| 681 | { |
| 682 | int32x4_t q4_0 = vld1q_s32(reinterpret_cast<int32_t*>(&data[(i + 0) * 4])); |
| 683 | int32x4_t q4_1 = vld1q_s32(reinterpret_cast<int32_t*>(&data[(i + 2) * 4])); |
| 684 | |
| 685 | // gather both x/y 16-bit pairs in each 32-bit lane |
| 686 | int32x4_t q4_xy = vuzpq_s32(q4_0, q4_1).val[0]; |
| 687 | int32x4_t q4_zc = vuzpq_s32(q4_0, q4_1).val[1]; |
| 688 | |
| 689 | // sign-extends each of x,y in [x y] with arithmetic shifts |
| 690 | int32x4_t xf = vshrq_n_s32(vshlq_n_s32(q4_xy, 16), 16); |
| 691 | int32x4_t yf = vshrq_n_s32(q4_xy, 16); |
| 692 | int32x4_t zf = vshrq_n_s32(vshlq_n_s32(q4_zc, 16), 16); |
| 693 | int32x4_t cf = vshrq_n_s32(q4_zc, 16); |
| 694 | |
| 695 | // get a floating-point scaler using zc with bottom 2 bits set to 1 (which represents 1.f) |
| 696 | int32x4_t sf = vorrq_s32(cf, vdupq_n_s32(3)); |
| 697 | float32x4_t s = vcvtq_f32_s32(sf); |
| 698 | |
| 699 | // convert x/y/z to floating point (unscaled! implied scale of 1/sqrt(2.f) * 1/sf) |
| 700 | float32x4_t x = vcvtq_f32_s32(xf); |
| 701 | float32x4_t y = vcvtq_f32_s32(yf); |
| 702 | float32x4_t z = vcvtq_f32_s32(zf); |
| 703 | |
| 704 | // reconstruct w as a square root (unscaled); we clamp to 0.f to avoid NaN due to precision errors |
| 705 | float32x4_t ws = vmulq_f32(s, s); |
| 706 | float32x4_t ww = vsubq_f32(vaddq_f32(ws, ws), vfmaq_f32(vfmaq_f32(vmulq_f32(x, x), y, y), z, z)); |
| 707 | float32x4_t w = vsqrtq_f32(vmaxq_f32(ww, vdupq_n_f32(0.f))); |
| 708 | |
| 709 | // compute final scale; note that all computations above are unscaled |
| 710 | // we need to divide by sf to get out of fixed point, divide by sqrt(2) to renormalize and multiply by 32767 to get to int16 range |
| 711 | float32x4_t ss = vdivq_f32(vdupq_n_f32(scale), s); |
| 712 | |
| 713 | // fast rounded signed float->int: addition triggers renormalization after which mantissa stores the integer value |
| 714 | // note: the result is offset by 0x4B40_0000, but we only need the low 16 bits so we can omit the subtraction |
| 715 | const float32x4_t fsnap = vdupq_n_f32(3 << 22); |
| 716 | |
| 717 | int32x4_t xr = vreinterpretq_s32_f32(vfmaq_f32(fsnap, x, ss)); |
| 718 | int32x4_t yr = vreinterpretq_s32_f32(vfmaq_f32(fsnap, y, ss)); |
| 719 | int32x4_t zr = vreinterpretq_s32_f32(vfmaq_f32(fsnap, z, ss)); |
| 720 | int32x4_t wr = vreinterpretq_s32_f32(vfmaq_f32(fsnap, w, ss)); |
| 721 | |
| 722 | // mix x/z and w/y to make 16-bit unpack easier |
| 723 | int32x4_t xzr = vsliq_n_s32(xr, zr, 16); |
| 724 | int32x4_t wyr = vsliq_n_s32(wr, yr, 16); |
| 725 | |
| 726 | // pack x/y/z/w using 16-bit unpacks; we pack wxyz by default (for qc=0) |
| 727 | uint64x2_t res_0 = vreinterpretq_u64_s16(vzipq_s16(vreinterpretq_s16_s32(wyr), vreinterpretq_s16_s32(xzr)).val[0]); |
| 728 | uint64x2_t res_1 = vreinterpretq_u64_s16(vzipq_s16(vreinterpretq_s16_s32(wyr), vreinterpretq_s16_s32(xzr)).val[1]); |
| 729 | |
| 730 | // store results to stack so that we can rotate using scalar instructions |
| 731 | // TODO: volatile works around LLVM mis-optimizing code; https://github.com/llvm/llvm-project/issues/166808 |
| 732 | volatile uint64_t res[4]; |
| 733 | vst1q_u64(const_cast<uint64_t*>(&res[0]), res_0); |
| 734 | vst1q_u64(const_cast<uint64_t*>(&res[2]), res_1); |
| 735 | |
| 736 | // rotate and store |
| 737 | uint64_t* out = reinterpret_cast<uint64_t*>(&data[i * 4]); |
| 738 | |
| 739 | out[0] = rotateleft64(res[0], data[(i + 0) * 4 + 3] << 4); |
| 740 | out[1] = rotateleft64(res[1], data[(i + 1) * 4 + 3] << 4); |
| 741 | out[2] = rotateleft64(res[2], data[(i + 2) * 4 + 3] << 4); |
| 742 | out[3] = rotateleft64(res[3], data[(i + 3) * 4 + 3] << 4); |
| 743 | } |
| 744 | } |
| 745 | |
| 746 | static void decodeFilterExpSimd(unsigned int* data, size_t count) |
| 747 | { |
| 748 | for (size_t i = 0; i < count; i += 4) |
| 749 | { |
| 750 | int32x4_t v = vld1q_s32(reinterpret_cast<int32_t*>(&data[i])); |
| 751 | |
| 752 | // decode exponent into 2^x directly |
| 753 | int32x4_t ef = vshrq_n_s32(v, 24); |
| 754 | int32x4_t es = vshlq_n_s32(vaddq_s32(ef, vdupq_n_s32(127)), 23); |
| 755 | |
| 756 | // decode 24-bit mantissa into floating-point value |
| 757 | int32x4_t mf = vshrq_n_s32(vshlq_n_s32(v, 8), 8); |
| 758 | float32x4_t m = vcvtq_f32_s32(mf); |
| 759 | |
| 760 | float32x4_t r = vmulq_f32(vreinterpretq_f32_s32(es), m); |
| 761 | |
| 762 | vst1q_f32(reinterpret_cast<float*>(&data[i]), r); |
| 763 | } |
| 764 | } |
| 765 | |
| 766 | static void decodeFilterColorSimd8(unsigned char* data, size_t count) |
| 767 | { |
| 768 | for (size_t i = 0; i < count; i += 4) |
| 769 | { |
| 770 | int32x4_t c4 = vld1q_s32(reinterpret_cast<int32_t*>(&data[i * 4])); |
| 771 | |
| 772 | // unpack y/co/cg/a (co/cg are sign extended with arithmetic shifts) |
| 773 | int32x4_t yf = vandq_s32(c4, vdupq_n_s32(0xff)); |
| 774 | int32x4_t cof = vshrq_n_s32(vshlq_n_s32(c4, 16), 24); |
| 775 | int32x4_t cgf = vshrq_n_s32(vshlq_n_s32(c4, 8), 24); |
| 776 | int32x4_t af = vreinterpretq_s32_u32(vshrq_n_u32(vreinterpretq_u32_s32(c4), 24)); |
| 777 | |
| 778 | // recover scale from alpha high bit |
| 779 | int32x4_t as = af; |
| 780 | as = vorrq_s32(as, vshrq_n_s32(as, 1)); |
| 781 | as = vorrq_s32(as, vshrq_n_s32(as, 2)); |
| 782 | as = vorrq_s32(as, vshrq_n_s32(as, 4)); |
| 783 | |
| 784 | // expand alpha by one bit to match other components |
| 785 | af = vorrq_s32(vandq_s32(vshlq_n_s32(af, 1), as), vandq_s32(af, vdupq_n_s32(1))); |
| 786 | |
| 787 | // compute scaling factor |
| 788 | float32x4_t ss = vmulq_f32(vdupq_n_f32(255.f), vrecpeq_f32(vcvtq_f32_s32(as))); |
| 789 | |
| 790 | // convert to RGB in fixed point |
| 791 | int32x4_t rf = vaddq_s32(yf, vsubq_s32(cof, cgf)); |
| 792 | int32x4_t gf = vaddq_s32(yf, cgf); |
| 793 | int32x4_t bf = vsubq_s32(yf, vaddq_s32(cof, cgf)); |
| 794 | |
| 795 | // fast rounded signed float->int: addition triggers renormalization after which mantissa stores the integer value |
| 796 | // note: the result is offset by 0x4B40_0000, but we only need the low 8 bits so we can omit the subtraction |
| 797 | const float32x4_t fsnap = vdupq_n_f32(3 << 22); |
| 798 | |
| 799 | int32x4_t rr = vreinterpretq_s32_f32(vfmaq_f32(fsnap, vcvtq_f32_s32(rf), ss)); |
| 800 | int32x4_t gr = vreinterpretq_s32_f32(vfmaq_f32(fsnap, vcvtq_f32_s32(gf), ss)); |
| 801 | int32x4_t br = vreinterpretq_s32_f32(vfmaq_f32(fsnap, vcvtq_f32_s32(bf), ss)); |
| 802 | int32x4_t ar = vreinterpretq_s32_f32(vfmaq_f32(fsnap, vcvtq_f32_s32(af), ss)); |
| 803 | |
| 804 | // repack rgba into final value |
| 805 | int32x4_t res = vsliq_n_s32(rr, vsliq_n_s32(gr, vsliq_n_s32(br, ar, 8), 8), 8); |
| 806 | |
| 807 | vst1q_s32(reinterpret_cast<int32_t*>(&data[i * 4]), res); |
| 808 | } |
| 809 | } |
| 810 | |
| 811 | static void decodeFilterColorSimd16(unsigned short* data, size_t count) |
| 812 | { |
| 813 | for (size_t i = 0; i < count; i += 4) |
| 814 | { |
| 815 | int32x4_t c4_0 = vld1q_s32(reinterpret_cast<int32_t*>(&data[(i + 0) * 4])); |
| 816 | int32x4_t c4_1 = vld1q_s32(reinterpret_cast<int32_t*>(&data[(i + 2) * 4])); |
| 817 | |
| 818 | // gather both y/co 16-bit pairs in each 32-bit lane |
| 819 | int32x4_t c4_yco = vuzpq_s32(c4_0, c4_1).val[0]; |
| 820 | int32x4_t c4_cga = vuzpq_s32(c4_0, c4_1).val[1]; |
| 821 | |
| 822 | // unpack y/co/cg/a components (co/cg are sign extended with arithmetic shifts) |
| 823 | int32x4_t yf = vandq_s32(c4_yco, vdupq_n_s32(0xffff)); |
| 824 | int32x4_t cof = vshrq_n_s32(c4_yco, 16); |
| 825 | int32x4_t cgf = vshrq_n_s32(vshlq_n_s32(c4_cga, 16), 16); |
| 826 | int32x4_t af = vreinterpretq_s32_u32(vshrq_n_u32(vreinterpretq_u32_s32(c4_cga), 16)); |
| 827 | |
| 828 | // recover scale from alpha high bit |
| 829 | int32x4_t as = af; |
| 830 | as = vorrq_s32(as, vshrq_n_s32(as, 1)); |
| 831 | as = vorrq_s32(as, vshrq_n_s32(as, 2)); |
| 832 | as = vorrq_s32(as, vshrq_n_s32(as, 4)); |
| 833 | as = vorrq_s32(as, vshrq_n_s32(as, 8)); |
| 834 | |
| 835 | // expand alpha by one bit to match other components |
| 836 | af = vorrq_s32(vandq_s32(vshlq_n_s32(af, 1), as), vandq_s32(af, vdupq_n_s32(1))); |
| 837 | |
| 838 | // compute scaling factor |
| 839 | float32x4_t ss = vdivq_f32(vdupq_n_f32(65535.f), vcvtq_f32_s32(as)); |
| 840 | |
| 841 | // convert to RGB in fixed point |
| 842 | int32x4_t rf = vaddq_s32(yf, vsubq_s32(cof, cgf)); |
| 843 | int32x4_t gf = vaddq_s32(yf, cgf); |
| 844 | int32x4_t bf = vsubq_s32(yf, vaddq_s32(cof, cgf)); |
| 845 | |
| 846 | // fast rounded signed float->int: addition triggers renormalization after which mantissa stores the integer value |
| 847 | // note: the result is offset by 0x4B40_0000, but we only need the low 16 bits so we can omit the subtraction |
| 848 | const float32x4_t fsnap = vdupq_n_f32(3 << 22); |
| 849 | |
| 850 | int32x4_t rr = vreinterpretq_s32_f32(vfmaq_f32(fsnap, vcvtq_f32_s32(rf), ss)); |
| 851 | int32x4_t gr = vreinterpretq_s32_f32(vfmaq_f32(fsnap, vcvtq_f32_s32(gf), ss)); |
| 852 | int32x4_t br = vreinterpretq_s32_f32(vfmaq_f32(fsnap, vcvtq_f32_s32(bf), ss)); |
| 853 | int32x4_t ar = vreinterpretq_s32_f32(vfmaq_f32(fsnap, vcvtq_f32_s32(af), ss)); |
| 854 | |
| 855 | // mix r/b and g/a to make 16-bit unpack easier |
| 856 | int32x4_t rbr = vsliq_n_s32(rr, br, 16); |
| 857 | int32x4_t gar = vsliq_n_s32(gr, ar, 16); |
| 858 | |
| 859 | // pack r/g/b/a using 16-bit unpacks |
| 860 | int32x4_t res_0 = vreinterpretq_s32_s16(vzipq_s16(vreinterpretq_s16_s32(rbr), vreinterpretq_s16_s32(gar)).val[0]); |
| 861 | int32x4_t res_1 = vreinterpretq_s32_s16(vzipq_s16(vreinterpretq_s16_s32(rbr), vreinterpretq_s16_s32(gar)).val[1]); |
| 862 | |
| 863 | vst1q_s32(reinterpret_cast<int32_t*>(&data[(i + 0) * 4]), res_0); |
| 864 | vst1q_s32(reinterpret_cast<int32_t*>(&data[(i + 2) * 4]), res_1); |
| 865 | } |
| 866 | } |
| 867 | #endif |
| 868 | |
| 869 | #ifdef SIMD_WASM |
| 870 | static void decodeFilterOctSimd8(signed char* data, size_t count) |
| 871 | { |
| 872 | const v128_t sign = wasm_f32x4_splat(-0.f); |
| 873 | |
| 874 | for (size_t i = 0; i < count; i += 4) |
| 875 | { |
| 876 | v128_t n4 = wasm_v128_load(&data[i * 4]); |
| 877 | |
| 878 | // sign-extends each of x,y in [x y ? ?] with arithmetic shifts |
| 879 | v128_t xf = wasm_i32x4_shr(wasm_i32x4_shl(n4, 24), 24); |
| 880 | v128_t yf = wasm_i32x4_shr(wasm_i32x4_shl(n4, 16), 24); |
| 881 | |
| 882 | // unpack z; note that z is unsigned so we technically don't need to sign extend it |
| 883 | v128_t zf = wasm_i32x4_shr(wasm_i32x4_shl(n4, 8), 24); |
| 884 | |
| 885 | // convert x and y to floats and reconstruct z; this assumes zf encodes 1.f at the same bit count |
| 886 | v128_t x = wasm_f32x4_convert_i32x4(xf); |
| 887 | v128_t y = wasm_f32x4_convert_i32x4(yf); |
| 888 | v128_t z = wasm_f32x4_sub(wasm_f32x4_convert_i32x4(zf), wasm_f32x4_add(wasm_f32x4_abs(x), wasm_f32x4_abs(y))); |
| 889 | |
| 890 | // fixup octahedral coordinates for z<0 |
| 891 | // note: i32x4_min with 0 is equvalent to f32x4_min |
| 892 | v128_t t = wasm_i32x4_min(z, wasm_i32x4_splat(0)); |
| 893 | |
| 894 | x = wasm_f32x4_add(x, wasm_v128_xor(t, wasm_v128_and(x, sign))); |
| 895 | y = wasm_f32x4_add(y, wasm_v128_xor(t, wasm_v128_and(y, sign))); |
| 896 | |
| 897 | // compute normal length & scale |
| 898 | v128_t ll = wasm_f32x4_add(wasm_f32x4_mul(x, x), wasm_f32x4_add(wasm_f32x4_mul(y, y), wasm_f32x4_mul(z, z))); |
| 899 | v128_t s = wasm_f32x4_div(wasm_f32x4_splat(127.f), wasm_f32x4_sqrt(ll)); |
| 900 | |
| 901 | // fast rounded signed float->int: addition triggers renormalization after which mantissa stores the integer value |
| 902 | // note: the result is offset by 0x4B40_0000, but we only need the low 8 bits so we can omit the subtraction |
| 903 | const v128_t fsnap = wasm_f32x4_splat(3 << 22); |
| 904 | |
| 905 | v128_t xr = wasm_f32x4_add(wasm_f32x4_mul(x, s), fsnap); |
| 906 | v128_t yr = wasm_f32x4_add(wasm_f32x4_mul(y, s), fsnap); |
| 907 | v128_t zr = wasm_f32x4_add(wasm_f32x4_mul(z, s), fsnap); |
| 908 | |
| 909 | // combine xr/yr/zr into final value |
| 910 | v128_t res = wasm_v128_and(n4, wasm_i32x4_splat(0xff000000)); |
| 911 | res = wasm_v128_or(res, wasm_v128_and(xr, wasm_i32x4_splat(0xff))); |
| 912 | res = wasm_v128_or(res, wasm_i32x4_shl(wasm_v128_and(yr, wasm_i32x4_splat(0xff)), 8)); |
| 913 | res = wasm_v128_or(res, wasm_i32x4_shl(wasm_v128_and(zr, wasm_i32x4_splat(0xff)), 16)); |
| 914 | |
| 915 | wasm_v128_store(&data[i * 4], res); |
| 916 | } |
| 917 | } |
| 918 | |
| 919 | static void decodeFilterOctSimd16(short* data, size_t count) |
| 920 | { |
| 921 | const v128_t sign = wasm_f32x4_splat(-0.f); |
| 922 | // TODO: volatile here works around LLVM mis-optimizing code; https://github.com/llvm/llvm-project/issues/149457 |
| 923 | volatile v128_t zmask = wasm_i32x4_splat(0x7fff); |
| 924 | |
| 925 | for (size_t i = 0; i < count; i += 4) |
| 926 | { |
| 927 | v128_t n4_0 = wasm_v128_load(&data[(i + 0) * 4]); |
| 928 | v128_t n4_1 = wasm_v128_load(&data[(i + 2) * 4]); |
| 929 | |
| 930 | // gather both x/y 16-bit pairs in each 32-bit lane |
| 931 | v128_t n4 = wasmx_unziplo_v32x4(n4_0, n4_1); |
| 932 | |
| 933 | // sign-extends each of x,y in [x y] with arithmetic shifts |
| 934 | v128_t xf = wasm_i32x4_shr(wasm_i32x4_shl(n4, 16), 16); |
| 935 | v128_t yf = wasm_i32x4_shr(n4, 16); |
| 936 | |
| 937 | // unpack z; note that z is unsigned so we don't need to sign extend it |
| 938 | v128_t z4 = wasmx_unziphi_v32x4(n4_0, n4_1); |
| 939 | v128_t zf = wasm_v128_and(z4, zmask); |
| 940 | |
| 941 | // convert x and y to floats and reconstruct z; this assumes zf encodes 1.f at the same bit count |
| 942 | v128_t x = wasm_f32x4_convert_i32x4(xf); |
| 943 | v128_t y = wasm_f32x4_convert_i32x4(yf); |
| 944 | v128_t z = wasm_f32x4_sub(wasm_f32x4_convert_i32x4(zf), wasm_f32x4_add(wasm_f32x4_abs(x), wasm_f32x4_abs(y))); |
| 945 | |
| 946 | // fixup octahedral coordinates for z<0 |
| 947 | // note: i32x4_min with 0 is equvalent to f32x4_min |
| 948 | v128_t t = wasm_i32x4_min(z, wasm_i32x4_splat(0)); |
| 949 | |
| 950 | x = wasm_f32x4_add(x, wasm_v128_xor(t, wasm_v128_and(x, sign))); |
| 951 | y = wasm_f32x4_add(y, wasm_v128_xor(t, wasm_v128_and(y, sign))); |
| 952 | |
| 953 | // compute normal length & scale |
| 954 | v128_t ll = wasm_f32x4_add(wasm_f32x4_mul(x, x), wasm_f32x4_add(wasm_f32x4_mul(y, y), wasm_f32x4_mul(z, z))); |
| 955 | v128_t s = wasm_f32x4_div(wasm_f32x4_splat(32767.f), wasm_f32x4_sqrt(ll)); |
| 956 | |
| 957 | // fast rounded signed float->int: addition triggers renormalization after which mantissa stores the integer value |
| 958 | // note: the result is offset by 0x4B40_0000, but we only need the low 16 bits so we can omit the subtraction |
| 959 | const v128_t fsnap = wasm_f32x4_splat(3 << 22); |
| 960 | |
| 961 | v128_t xr = wasm_f32x4_add(wasm_f32x4_mul(x, s), fsnap); |
| 962 | v128_t yr = wasm_f32x4_add(wasm_f32x4_mul(y, s), fsnap); |
| 963 | v128_t zr = wasm_f32x4_add(wasm_f32x4_mul(z, s), fsnap); |
| 964 | |
| 965 | // mix x/z and y/0 to make 16-bit unpack easier |
| 966 | v128_t xzr = wasm_v128_or(wasm_v128_and(xr, wasm_i32x4_splat(0xffff)), wasm_i32x4_shl(zr, 16)); |
| 967 | v128_t y0r = wasm_v128_and(yr, wasm_i32x4_splat(0xffff)); |
| 968 | |
| 969 | // pack x/y/z using 16-bit unpacks; note that this has 0 where we should have .w |
| 970 | v128_t res_0 = wasmx_unpacklo_v16x8(xzr, y0r); |
| 971 | v128_t res_1 = wasmx_unpackhi_v16x8(xzr, y0r); |
| 972 | |
| 973 | // patch in .w |
| 974 | res_0 = wasm_v128_or(res_0, wasm_v128_and(n4_0, wasm_i64x2_splat(0xffff000000000000))); |
| 975 | res_1 = wasm_v128_or(res_1, wasm_v128_and(n4_1, wasm_i64x2_splat(0xffff000000000000))); |
| 976 | |
| 977 | wasm_v128_store(&data[(i + 0) * 4], res_0); |
| 978 | wasm_v128_store(&data[(i + 2) * 4], res_1); |
| 979 | } |
| 980 | } |
| 981 | |
| 982 | static void decodeFilterQuatSimd(short* data, size_t count) |
| 983 | { |
| 984 | const float scale = 32767.f / sqrtf(2.f); |
| 985 | |
| 986 | for (size_t i = 0; i < count; i += 4) |
| 987 | { |
| 988 | v128_t q4_0 = wasm_v128_load(&data[(i + 0) * 4]); |
| 989 | v128_t q4_1 = wasm_v128_load(&data[(i + 2) * 4]); |
| 990 | |
| 991 | // gather both x/y 16-bit pairs in each 32-bit lane |
| 992 | v128_t q4_xy = wasmx_unziplo_v32x4(q4_0, q4_1); |
| 993 | v128_t q4_zc = wasmx_unziphi_v32x4(q4_0, q4_1); |
| 994 | |
| 995 | // sign-extends each of x,y in [x y] with arithmetic shifts |
| 996 | v128_t xf = wasm_i32x4_shr(wasm_i32x4_shl(q4_xy, 16), 16); |
| 997 | v128_t yf = wasm_i32x4_shr(q4_xy, 16); |
| 998 | v128_t zf = wasm_i32x4_shr(wasm_i32x4_shl(q4_zc, 16), 16); |
| 999 | v128_t cf = wasm_i32x4_shr(q4_zc, 16); |
| 1000 | |
| 1001 | // get a floating-point scaler using zc with bottom 2 bits set to 1 (which represents 1.f) |
| 1002 | v128_t sf = wasm_v128_or(cf, wasm_i32x4_splat(3)); |
| 1003 | v128_t s = wasm_f32x4_convert_i32x4(sf); |
| 1004 | |
| 1005 | // convert x/y/z to floating point (unscaled! implied scale of 1/sqrt(2.f) * 1/sf) |
| 1006 | v128_t x = wasm_f32x4_convert_i32x4(xf); |
| 1007 | v128_t y = wasm_f32x4_convert_i32x4(yf); |
| 1008 | v128_t z = wasm_f32x4_convert_i32x4(zf); |
| 1009 | |
| 1010 | // reconstruct w as a square root (unscaled); we clamp to 0.f to avoid NaN due to precision errors |
| 1011 | // note: i32x4_max with 0 is equivalent to f32x4_max |
| 1012 | v128_t ws = wasm_f32x4_mul(s, s); |
| 1013 | v128_t ww = wasm_f32x4_sub(wasm_f32x4_add(ws, ws), wasm_f32x4_add(wasm_f32x4_mul(x, x), wasm_f32x4_add(wasm_f32x4_mul(y, y), wasm_f32x4_mul(z, z)))); |
| 1014 | v128_t w = wasm_f32x4_sqrt(wasm_i32x4_max(ww, wasm_i32x4_splat(0))); |
| 1015 | |
| 1016 | // compute final scale; note that all computations above are unscaled |
| 1017 | // we need to divide by sf to get out of fixed point, divide by sqrt(2) to renormalize and multiply by 32767 to get to int16 range |
| 1018 | v128_t ss = wasm_f32x4_div(wasm_f32x4_splat(scale), s); |
| 1019 | |
| 1020 | // fast rounded signed float->int: addition triggers renormalization after which mantissa stores the integer value |
| 1021 | // note: the result is offset by 0x4B40_0000, but we only need the low 16 bits so we can omit the subtraction |
| 1022 | const v128_t fsnap = wasm_f32x4_splat(3 << 22); |
| 1023 | |
| 1024 | v128_t xr = wasm_f32x4_add(wasm_f32x4_mul(x, ss), fsnap); |
| 1025 | v128_t yr = wasm_f32x4_add(wasm_f32x4_mul(y, ss), fsnap); |
| 1026 | v128_t zr = wasm_f32x4_add(wasm_f32x4_mul(z, ss), fsnap); |
| 1027 | v128_t wr = wasm_f32x4_add(wasm_f32x4_mul(w, ss), fsnap); |
| 1028 | |
| 1029 | // mix x/z and w/y to make 16-bit unpack easier |
| 1030 | v128_t xzr = wasm_v128_or(wasm_v128_and(xr, wasm_i32x4_splat(0xffff)), wasm_i32x4_shl(zr, 16)); |
| 1031 | v128_t wyr = wasm_v128_or(wasm_v128_and(wr, wasm_i32x4_splat(0xffff)), wasm_i32x4_shl(yr, 16)); |
| 1032 | |
| 1033 | // pack x/y/z/w using 16-bit unpacks; we pack wxyz by default (for qc=0) |
| 1034 | v128_t res_0 = wasmx_unpacklo_v16x8(wyr, xzr); |
| 1035 | v128_t res_1 = wasmx_unpackhi_v16x8(wyr, xzr); |
| 1036 | |
| 1037 | // compute component index shifted left by 4 (and moved into i32x4 slot) |
| 1038 | v128_t cm = wasm_i32x4_shl(cf, 4); |
| 1039 | |
| 1040 | // rotate and store |
| 1041 | uint64_t* out = reinterpret_cast<uint64_t*>(&data[i * 4]); |
| 1042 | |
| 1043 | out[0] = rotateleft64(wasm_i64x2_extract_lane(res_0, 0), wasm_i32x4_extract_lane(cm, 0)); |
| 1044 | out[1] = rotateleft64(wasm_i64x2_extract_lane(res_0, 1), wasm_i32x4_extract_lane(cm, 1)); |
| 1045 | out[2] = rotateleft64(wasm_i64x2_extract_lane(res_1, 0), wasm_i32x4_extract_lane(cm, 2)); |
| 1046 | out[3] = rotateleft64(wasm_i64x2_extract_lane(res_1, 1), wasm_i32x4_extract_lane(cm, 3)); |
| 1047 | } |
| 1048 | } |
| 1049 | |
| 1050 | static void decodeFilterExpSimd(unsigned int* data, size_t count) |
| 1051 | { |
| 1052 | for (size_t i = 0; i < count; i += 4) |
| 1053 | { |
| 1054 | v128_t v = wasm_v128_load(&data[i]); |
| 1055 | |
| 1056 | // decode exponent into 2^x directly |
| 1057 | v128_t ef = wasm_i32x4_shr(v, 24); |
| 1058 | v128_t es = wasm_i32x4_shl(wasm_i32x4_add(ef, wasm_i32x4_splat(127)), 23); |
| 1059 | |
| 1060 | // decode 24-bit mantissa into floating-point value |
| 1061 | v128_t mf = wasm_i32x4_shr(wasm_i32x4_shl(v, 8), 8); |
| 1062 | v128_t m = wasm_f32x4_convert_i32x4(mf); |
| 1063 | |
| 1064 | v128_t r = wasm_f32x4_mul(es, m); |
| 1065 | |
| 1066 | wasm_v128_store(&data[i], r); |
| 1067 | } |
| 1068 | } |
| 1069 | |
| 1070 | static void decodeFilterColorSimd8(unsigned char* data, size_t count) |
| 1071 | { |
| 1072 | // TODO: volatile here works around LLVM mis-optimizing code; https://github.com/llvm/llvm-project/issues/149457 |
| 1073 | volatile v128_t zero = wasm_i32x4_splat(0); |
| 1074 | |
| 1075 | for (size_t i = 0; i < count; i += 4) |
| 1076 | { |
| 1077 | v128_t c4 = wasm_v128_load(&data[i * 4]); |
| 1078 | |
| 1079 | // unpack y/co/cg/a (co/cg are sign extended with arithmetic shifts) |
| 1080 | v128_t yf = wasm_v128_and(c4, wasm_i32x4_splat(0xff)); |
| 1081 | v128_t cof = wasm_i32x4_shr(wasm_i32x4_shl(c4, 16), 24); |
| 1082 | v128_t cgf = wasm_i32x4_shr(wasm_i32x4_shl(c4, 8), 24); |
| 1083 | v128_t af = wasm_v128_or(zero, wasm_u32x4_shr(c4, 24)); |
| 1084 | |
| 1085 | // recover scale from alpha high bit |
| 1086 | v128_t as = af; |
| 1087 | as = wasm_v128_or(as, wasm_i32x4_shr(as, 1)); |
| 1088 | as = wasm_v128_or(as, wasm_i32x4_shr(as, 2)); |
| 1089 | as = wasm_v128_or(as, wasm_i32x4_shr(as, 4)); |
| 1090 | |
| 1091 | // expand alpha by one bit to match other components |
| 1092 | af = wasm_v128_or(wasm_v128_and(wasm_i32x4_shl(af, 1), as), wasm_v128_and(af, wasm_i32x4_splat(1))); |
| 1093 | |
| 1094 | // compute scaling factor |
| 1095 | v128_t ss = wasm_f32x4_div(wasm_f32x4_splat(255.f), wasm_f32x4_convert_i32x4(as)); |
| 1096 | |
| 1097 | // convert to RGB in fixed point |
| 1098 | v128_t rf = wasm_i32x4_add(yf, wasm_i32x4_sub(cof, cgf)); |
| 1099 | v128_t gf = wasm_i32x4_add(yf, cgf); |
| 1100 | v128_t bf = wasm_i32x4_sub(yf, wasm_i32x4_add(cof, cgf)); |
| 1101 | |
| 1102 | // fast rounded signed float->int: addition triggers renormalization after which mantissa stores the integer value |
| 1103 | // note: the result is offset by 0x4B40_0000, but we only need the low 8 bits so we can omit the subtraction |
| 1104 | const v128_t fsnap = wasm_f32x4_splat(3 << 22); |
| 1105 | |
| 1106 | v128_t rr = wasm_f32x4_add(wasm_f32x4_mul(wasm_f32x4_convert_i32x4(rf), ss), fsnap); |
| 1107 | v128_t gr = wasm_f32x4_add(wasm_f32x4_mul(wasm_f32x4_convert_i32x4(gf), ss), fsnap); |
| 1108 | v128_t br = wasm_f32x4_add(wasm_f32x4_mul(wasm_f32x4_convert_i32x4(bf), ss), fsnap); |
| 1109 | v128_t ar = wasm_f32x4_add(wasm_f32x4_mul(wasm_f32x4_convert_i32x4(af), ss), fsnap); |
| 1110 | |
| 1111 | // repack rgba into final value |
| 1112 | v128_t res = wasm_v128_and(rr, wasm_i32x4_splat(0xff)); |
| 1113 | res = wasm_v128_or(res, wasm_i32x4_shl(wasm_v128_and(gr, wasm_i32x4_splat(0xff)), 8)); |
| 1114 | res = wasm_v128_or(res, wasm_i32x4_shl(wasm_v128_and(br, wasm_i32x4_splat(0xff)), 16)); |
| 1115 | res = wasm_v128_or(res, wasm_i32x4_shl(ar, 24)); |
| 1116 | |
| 1117 | wasm_v128_store(&data[i * 4], res); |
| 1118 | } |
| 1119 | } |
| 1120 | |
| 1121 | static void decodeFilterColorSimd16(unsigned short* data, size_t count) |
| 1122 | { |
| 1123 | // TODO: volatile here works around LLVM mis-optimizing code; https://github.com/llvm/llvm-project/issues/149457 |
| 1124 | volatile v128_t zero = wasm_i32x4_splat(0); |
| 1125 | |
| 1126 | for (size_t i = 0; i < count; i += 4) |
| 1127 | { |
| 1128 | v128_t c4_0 = wasm_v128_load(&data[(i + 0) * 4]); |
| 1129 | v128_t c4_1 = wasm_v128_load(&data[(i + 2) * 4]); |
| 1130 | |
| 1131 | // gather both y/co 16-bit pairs in each 32-bit lane |
| 1132 | v128_t c4_yco = wasmx_unziplo_v32x4(c4_0, c4_1); |
| 1133 | v128_t c4_cga = wasmx_unziphi_v32x4(c4_0, c4_1); |
| 1134 | |
| 1135 | // unpack y/co/cg/a components (co/cg are sign extended with arithmetic shifts) |
| 1136 | v128_t yf = wasm_v128_and(c4_yco, wasm_i32x4_splat(0xffff)); |
| 1137 | v128_t cof = wasm_i32x4_shr(c4_yco, 16); |
| 1138 | v128_t cgf = wasm_i32x4_shr(wasm_i32x4_shl(c4_cga, 16), 16); |
| 1139 | v128_t af = wasm_v128_or(zero, wasm_u32x4_shr(c4_cga, 16)); |
| 1140 | |
| 1141 | // recover scale from alpha high bit |
| 1142 | v128_t as = af; |
| 1143 | as = wasm_v128_or(as, wasm_i32x4_shr(as, 1)); |
| 1144 | as = wasm_v128_or(as, wasm_i32x4_shr(as, 2)); |
| 1145 | as = wasm_v128_or(as, wasm_i32x4_shr(as, 4)); |
| 1146 | as = wasm_v128_or(as, wasm_i32x4_shr(as, 8)); |
| 1147 | |
| 1148 | // expand alpha by one bit to match other components |
| 1149 | af = wasm_v128_or(wasm_v128_and(wasm_i32x4_shl(af, 1), as), wasm_v128_and(af, wasm_i32x4_splat(1))); |
| 1150 | |
| 1151 | // compute scaling factor |
| 1152 | v128_t ss = wasm_f32x4_div(wasm_f32x4_splat(65535.f), wasm_f32x4_convert_i32x4(as)); |
| 1153 | |
| 1154 | // convert to RGB in fixed point |
| 1155 | v128_t rf = wasm_i32x4_add(yf, wasm_i32x4_sub(cof, cgf)); |
| 1156 | v128_t gf = wasm_i32x4_add(yf, cgf); |
| 1157 | v128_t bf = wasm_i32x4_sub(yf, wasm_i32x4_add(cof, cgf)); |
| 1158 | |
| 1159 | // fast rounded signed float->int: addition triggers renormalization after which mantissa stores the integer value |
| 1160 | // note: the result is offset by 0x4B40_0000, but we only need the low 16 bits so we can omit the subtraction |
| 1161 | const v128_t fsnap = wasm_f32x4_splat(3 << 22); |
| 1162 | |
| 1163 | v128_t rr = wasm_f32x4_add(wasm_f32x4_mul(wasm_f32x4_convert_i32x4(rf), ss), fsnap); |
| 1164 | v128_t gr = wasm_f32x4_add(wasm_f32x4_mul(wasm_f32x4_convert_i32x4(gf), ss), fsnap); |
| 1165 | v128_t br = wasm_f32x4_add(wasm_f32x4_mul(wasm_f32x4_convert_i32x4(bf), ss), fsnap); |
| 1166 | v128_t ar = wasm_f32x4_add(wasm_f32x4_mul(wasm_f32x4_convert_i32x4(af), ss), fsnap); |
| 1167 | |
| 1168 | // mix r/b and g/a to make 16-bit unpack easier |
| 1169 | v128_t rbr = wasm_v128_or(wasm_v128_and(rr, wasm_i32x4_splat(0xffff)), wasm_i32x4_shl(br, 16)); |
| 1170 | v128_t gar = wasm_v128_or(wasm_v128_and(gr, wasm_i32x4_splat(0xffff)), wasm_i32x4_shl(ar, 16)); |
| 1171 | |
| 1172 | // pack r/g/b/a using 16-bit unpacks |
| 1173 | v128_t res_0 = wasmx_unpacklo_v16x8(rbr, gar); |
| 1174 | v128_t res_1 = wasmx_unpackhi_v16x8(rbr, gar); |
| 1175 | |
| 1176 | wasm_v128_store(&data[(i + 0) * 4], res_0); |
| 1177 | wasm_v128_store(&data[(i + 2) * 4], res_1); |
| 1178 | } |
| 1179 | } |
| 1180 | #endif |
| 1181 | |
| 1182 | // optimized variant of frexp |
| 1183 | inline int optlog2(float v) |
| 1184 | { |
| 1185 | union |
| 1186 | { |
| 1187 | float f; |
| 1188 | unsigned int ui; |
| 1189 | } u; |
| 1190 | |
| 1191 | u.f = v; |
| 1192 | // +1 accounts for implicit 1. in mantissa; denormalized numbers will end up clamped to min_exp by calling code |
| 1193 | return v == 0 ? 0 : int((u.ui >> 23) & 0xff) - 127 + 1; |
| 1194 | } |
| 1195 | |
| 1196 | // optimized variant of ldexp |
| 1197 | inline float optexp2(int e) |
| 1198 | { |
| 1199 | union |
| 1200 | { |
| 1201 | float f; |
| 1202 | unsigned int ui; |
| 1203 | } u; |
| 1204 | |
| 1205 | u.ui = unsigned(e + 127) << 23; |
| 1206 | return u.f; |
| 1207 | } |
| 1208 | |
| 1209 | } // namespace meshopt |
| 1210 | |
| 1211 | void meshopt_decodeFilterOct(void* buffer, size_t count, size_t stride) |
| 1212 | { |
| 1213 | using namespace meshopt; |
| 1214 | |
| 1215 | assert(stride == 4 || stride == 8); |
| 1216 | |
| 1217 | #if defined(SIMD_SSE) || defined(SIMD_NEON) || defined(SIMD_WASM) |
| 1218 | if (stride == 4) |
| 1219 | dispatchSimd(decodeFilterOctSimd8, static_cast<signed char*>(buffer), count, 4); |
| 1220 | else |
| 1221 | dispatchSimd(decodeFilterOctSimd16, static_cast<short*>(buffer), count, 4); |
| 1222 | #else |
| 1223 | if (stride == 4) |
| 1224 | decodeFilterOct(static_cast<signed char*>(buffer), count); |
| 1225 | else |
| 1226 | decodeFilterOct(static_cast<short*>(buffer), count); |
| 1227 | #endif |
| 1228 | } |
| 1229 | |
| 1230 | void meshopt_decodeFilterQuat(void* buffer, size_t count, size_t stride) |
| 1231 | { |
| 1232 | using namespace meshopt; |
| 1233 | |
| 1234 | assert(stride == 8); |
| 1235 | (void)stride; |
| 1236 | |
| 1237 | #if defined(SIMD_SSE) || defined(SIMD_NEON) || defined(SIMD_WASM) |
| 1238 | dispatchSimd(decodeFilterQuatSimd, static_cast<short*>(buffer), count, 4); |
| 1239 | #else |
| 1240 | decodeFilterQuat(static_cast<short*>(buffer), count); |
| 1241 | #endif |
| 1242 | } |
| 1243 | |
| 1244 | void meshopt_decodeFilterExp(void* buffer, size_t count, size_t stride) |
| 1245 | { |
| 1246 | using namespace meshopt; |
| 1247 | |
| 1248 | assert(stride > 0 && stride % 4 == 0); |
| 1249 | |
| 1250 | #if defined(SIMD_SSE) || defined(SIMD_NEON) || defined(SIMD_WASM) |
| 1251 | dispatchSimd(decodeFilterExpSimd, static_cast<unsigned int*>(buffer), count * (stride / 4), 1); |
| 1252 | #else |
| 1253 | decodeFilterExp(static_cast<unsigned int*>(buffer), count * (stride / 4)); |
| 1254 | #endif |
| 1255 | } |
| 1256 | |
| 1257 | void meshopt_decodeFilterColor(void* buffer, size_t count, size_t stride) |
| 1258 | { |
| 1259 | using namespace meshopt; |
| 1260 | |
| 1261 | assert(stride == 4 || stride == 8); |
| 1262 | |
| 1263 | #if defined(SIMD_SSE) || defined(SIMD_NEON) || defined(SIMD_WASM) |
| 1264 | if (stride == 4) |
| 1265 | dispatchSimd(decodeFilterColorSimd8, static_cast<unsigned char*>(buffer), count, 4); |
| 1266 | else |
| 1267 | dispatchSimd(decodeFilterColorSimd16, static_cast<unsigned short*>(buffer), count, 4); |
| 1268 | #else |
| 1269 | if (stride == 4) |
| 1270 | decodeFilterColor<signed char>(static_cast<unsigned char*>(buffer), count); |
| 1271 | else |
| 1272 | decodeFilterColor<short>(static_cast<unsigned short*>(buffer), count); |
| 1273 | #endif |
| 1274 | } |
| 1275 | |
| 1276 | void meshopt_encodeFilterOct(void* destination, size_t count, size_t stride, int bits, const float* data) |
| 1277 | { |
| 1278 | assert(stride == 4 || stride == 8); |
| 1279 | assert(bits >= 2 && bits <= 16); |
| 1280 | |
| 1281 | signed char* d8 = static_cast<signed char*>(destination); |
| 1282 | short* d16 = static_cast<short*>(destination); |
| 1283 | |
| 1284 | int bytebits = int(stride * 2); |
| 1285 | |
| 1286 | for (size_t i = 0; i < count; ++i) |
| 1287 | { |
| 1288 | const float* n = &data[i * 4]; |
| 1289 | |
| 1290 | // octahedral encoding of a unit vector |
| 1291 | float nx = n[0], ny = n[1], nz = n[2], nw = n[3]; |
| 1292 | float nl = fabsf(nx) + fabsf(ny) + fabsf(nz); |
| 1293 | float ns = nl == 0.f ? 0.f : 1.f / nl; |
| 1294 | |
| 1295 | nx *= ns; |
| 1296 | ny *= ns; |
| 1297 | |
| 1298 | float u = (nz >= 0.f) ? nx : (1 - fabsf(ny)) * (nx >= 0.f ? 1.f : -1.f); |
| 1299 | float v = (nz >= 0.f) ? ny : (1 - fabsf(nx)) * (ny >= 0.f ? 1.f : -1.f); |
| 1300 | |
| 1301 | int fu = meshopt_quantizeSnorm(u, bits); |
| 1302 | int fv = meshopt_quantizeSnorm(v, bits); |
| 1303 | int fo = meshopt_quantizeSnorm(1.f, bits); |
| 1304 | int fw = meshopt_quantizeSnorm(nw, bytebits); |
| 1305 | |
| 1306 | if (stride == 4) |
| 1307 | { |
| 1308 | d8[i * 4 + 0] = (signed char)(fu); |
| 1309 | d8[i * 4 + 1] = (signed char)(fv); |
| 1310 | d8[i * 4 + 2] = (signed char)(fo); |
| 1311 | d8[i * 4 + 3] = (signed char)(fw); |
| 1312 | } |
| 1313 | else |
| 1314 | { |
| 1315 | d16[i * 4 + 0] = short(fu); |
| 1316 | d16[i * 4 + 1] = short(fv); |
| 1317 | d16[i * 4 + 2] = short(fo); |
| 1318 | d16[i * 4 + 3] = short(fw); |
| 1319 | } |
| 1320 | } |
| 1321 | } |
| 1322 | |
| 1323 | void meshopt_encodeFilterQuat(void* destination_, size_t count, size_t stride, int bits, const float* data) |
| 1324 | { |
| 1325 | assert(stride == 8); |
| 1326 | assert(bits >= 4 && bits <= 16); |
| 1327 | (void)stride; |
| 1328 | |
| 1329 | short* destination = static_cast<short*>(destination_); |
| 1330 | |
| 1331 | const float scaler = sqrtf(2.f); |
| 1332 | |
| 1333 | for (size_t i = 0; i < count; ++i) |
| 1334 | { |
| 1335 | const float* q = &data[i * 4]; |
| 1336 | short* d = &destination[i * 4]; |
| 1337 | |
| 1338 | // establish maximum quaternion component |
| 1339 | int qc = 0; |
| 1340 | qc = fabsf(q[1]) > fabsf(q[qc]) ? 1 : qc; |
| 1341 | qc = fabsf(q[2]) > fabsf(q[qc]) ? 2 : qc; |
| 1342 | qc = fabsf(q[3]) > fabsf(q[qc]) ? 3 : qc; |
| 1343 | |
| 1344 | // we use double-cover properties to discard the sign |
| 1345 | float sign = q[qc] < 0.f ? -1.f : 1.f; |
| 1346 | |
| 1347 | // note: we always encode a cyclical swizzle to be able to recover the order via rotation |
| 1348 | d[0] = short(meshopt_quantizeSnorm(q[(qc + 1) & 3] * scaler * sign, bits)); |
| 1349 | d[1] = short(meshopt_quantizeSnorm(q[(qc + 2) & 3] * scaler * sign, bits)); |
| 1350 | d[2] = short(meshopt_quantizeSnorm(q[(qc + 3) & 3] * scaler * sign, bits)); |
| 1351 | d[3] = short((meshopt_quantizeSnorm(1.f, bits) & ~3) | qc); |
| 1352 | } |
| 1353 | } |
| 1354 | |
| 1355 | void meshopt_encodeFilterExp(void* destination_, size_t count, size_t stride, int bits, const float* data, enum meshopt_EncodeExpMode mode) |
| 1356 | { |
| 1357 | using namespace meshopt; |
| 1358 | |
| 1359 | assert(stride > 0 && stride % 4 == 0 && stride <= 256); |
| 1360 | assert(bits >= 1 && bits <= 24); |
| 1361 | |
| 1362 | unsigned int* destination = static_cast<unsigned int*>(destination_); |
| 1363 | size_t stride_float = stride / sizeof(float); |
| 1364 | |
| 1365 | int component_exp[64]; |
| 1366 | assert(stride_float <= sizeof(component_exp) / sizeof(int)); |
| 1367 | |
| 1368 | const int min_exp = -100; |
| 1369 | |
| 1370 | if (mode == meshopt_EncodeExpSharedComponent) |
| 1371 | { |
| 1372 | for (size_t j = 0; j < stride_float; ++j) |
| 1373 | component_exp[j] = min_exp; |
| 1374 | |
| 1375 | for (size_t i = 0; i < count; ++i) |
| 1376 | { |
| 1377 | const float* v = &data[i * stride_float]; |
| 1378 | |
| 1379 | // use maximum exponent to encode values; this guarantees that mantissa is [-1, 1] |
| 1380 | for (size_t j = 0; j < stride_float; ++j) |
| 1381 | { |
| 1382 | int e = optlog2(v[j]); |
| 1383 | |
| 1384 | component_exp[j] = (component_exp[j] < e) ? e : component_exp[j]; |
| 1385 | } |
| 1386 | } |
| 1387 | } |
| 1388 | |
| 1389 | for (size_t i = 0; i < count; ++i) |
| 1390 | { |
| 1391 | const float* v = &data[i * stride_float]; |
| 1392 | unsigned int* d = &destination[i * stride_float]; |
| 1393 | |
| 1394 | int vector_exp = min_exp; |
| 1395 | |
| 1396 | if (mode == meshopt_EncodeExpSharedVector) |
| 1397 | { |
| 1398 | // use maximum exponent to encode values; this guarantees that mantissa is [-1, 1] |
| 1399 | for (size_t j = 0; j < stride_float; ++j) |
| 1400 | { |
| 1401 | int e = optlog2(v[j]); |
| 1402 | |
| 1403 | vector_exp = (vector_exp < e) ? e : vector_exp; |
| 1404 | } |
| 1405 | } |
| 1406 | else if (mode == meshopt_EncodeExpSeparate) |
| 1407 | { |
| 1408 | for (size_t j = 0; j < stride_float; ++j) |
| 1409 | { |
| 1410 | int e = optlog2(v[j]); |
| 1411 | |
| 1412 | component_exp[j] = (min_exp < e) ? e : min_exp; |
| 1413 | } |
| 1414 | } |
| 1415 | else if (mode == meshopt_EncodeExpClamped) |
| 1416 | { |
| 1417 | for (size_t j = 0; j < stride_float; ++j) |
| 1418 | { |
| 1419 | int e = optlog2(v[j]); |
| 1420 | |
| 1421 | component_exp[j] = (0 < e) ? e : 0; |
| 1422 | } |
| 1423 | } |
| 1424 | else |
| 1425 | { |
| 1426 | // the code below assumes component_exp is initialized outside of the loop |
| 1427 | assert(mode == meshopt_EncodeExpSharedComponent); |
| 1428 | } |
| 1429 | |
| 1430 | for (size_t j = 0; j < stride_float; ++j) |
| 1431 | { |
| 1432 | int exp = (mode == meshopt_EncodeExpSharedVector) ? vector_exp : component_exp[j]; |
| 1433 | |
| 1434 | // note that we additionally scale the mantissa to make it a K-bit signed integer (K-1 bits for magnitude) |
| 1435 | exp -= (bits - 1); |
| 1436 | |
| 1437 | // compute renormalized rounded mantissa for each component |
| 1438 | int mmask = (1 << 24) - 1; |
| 1439 | int m = int(v[j] * optexp2(-exp) + (v[j] >= 0 ? 0.5f : -0.5f)); |
| 1440 | |
| 1441 | d[j] = (m & mmask) | (unsigned(exp) << 24); |
| 1442 | } |
| 1443 | } |
| 1444 | } |
| 1445 | |
| 1446 | void meshopt_encodeFilterColor(void* destination, size_t count, size_t stride, int bits, const float* data) |
| 1447 | { |
| 1448 | assert(stride == 4 || stride == 8); |
| 1449 | assert(bits >= 2 && bits <= 16); |
| 1450 | |
| 1451 | unsigned char* d8 = static_cast<unsigned char*>(destination); |
| 1452 | unsigned short* d16 = static_cast<unsigned short*>(destination); |
| 1453 | |
| 1454 | for (size_t i = 0; i < count; ++i) |
| 1455 | { |
| 1456 | const float* c = &data[i * 4]; |
| 1457 | |
| 1458 | int fr = meshopt_quantizeUnorm(c[0], bits); |
| 1459 | int fg = meshopt_quantizeUnorm(c[1], bits); |
| 1460 | int fb = meshopt_quantizeUnorm(c[2], bits); |
| 1461 | |
| 1462 | // YCoCg-R encoding with truncated Co/Cg ensures that decoding can be done using integers |
| 1463 | int fco = (fr - fb) / 2; |
| 1464 | int tmp = fb + fco; |
| 1465 | int fcg = (fg - tmp) / 2; |
| 1466 | int fy = tmp + fcg; |
| 1467 | |
| 1468 | // validate that R/G/B can be reconstructed with K bit integers |
| 1469 | assert(unsigned((fy + fco - fcg) | (fy + fcg) | (fy - fco - fcg)) < (1u << bits)); |
| 1470 | |
| 1471 | // alpha: K-1-bit encoding with high bit set to 1 |
| 1472 | int fa = meshopt_quantizeUnorm(c[3], bits - 1) | (1 << (bits - 1)); |
| 1473 | |
| 1474 | if (stride == 4) |
| 1475 | { |
| 1476 | d8[i * 4 + 0] = (unsigned char)(fy); |
| 1477 | d8[i * 4 + 1] = (unsigned char)(fco); |
| 1478 | d8[i * 4 + 2] = (unsigned char)(fcg); |
| 1479 | d8[i * 4 + 3] = (unsigned char)(fa); |
| 1480 | } |
| 1481 | else |
| 1482 | { |
| 1483 | d16[i * 4 + 0] = (unsigned short)(fy); |
| 1484 | d16[i * 4 + 1] = (unsigned short)(fco); |
| 1485 | d16[i * 4 + 2] = (unsigned short)(fcg); |
| 1486 | d16[i * 4 + 3] = (unsigned short)(fa); |
| 1487 | } |
| 1488 | } |
| 1489 | } |
| 1490 | |
| 1491 | #undef SIMD_SSE |
| 1492 | #undef SIMD_NEON |
| 1493 | #undef SIMD_WASM |