UNPKG

playcanvas

Version:

PlayCanvas WebGL game engine

3 lines (2 loc) 4.04 kB
declare const _default: "\n\n#if defined(PLATFORM_ANDROID)\n\n // Software pack of one f32 -> f16 (low 16 bits). Ties-to-even, full subnormals.\n fn floatToHalf(a: f32) -> u32 {\n let u: u32 = bitcast<u32>(a);\n let sign: u32 = (u >> 16u) & 0x8000u;\n let absu: u32 = u & 0x7FFFFFFFu;\n let man: u32 = u & 0x007FFFFFu;\n let e32: i32 = i32((u >> 23u) & 0xFFu) - 127;\n \n // NaN / Inf\n if ((absu & 0x7F800000u) == 0x7F800000u) {\n let isnan = (man != 0u);\n return sign | select(0x7C00u, 0x7E00u, isnan);\n }\n \n // Overflow to Inf\n if (e32 > 15) { return sign | 0x7C00u; }\n \n // Normal half\n if (e32 >= -14) {\n var he: u32 = u32(e32 + 15);\n var hm: u32 = man >> 13u;\n let rem: u32 = man & 0x1FFFu;\n let add: u32 = select(0u, 1u, (rem > 0x1000u) || (rem == 0x1000u && (hm & 1u) == 1u));\n hm = (hm + add) & 0x3FFu;\n if ((hm & 0x400u) != 0u) {\n hm = 0u; he = he + 1u;\n if (he >= 31u) { return sign | 0x7C00u; }\n }\n return sign | (he << 10u) | hm;\n }\n \n // Subnormals\n if (e32 >= -24) {\n let s: u32 = u32(-(e32 + 1));\n let mnorm: u32 = 0x00800000u | man;\n var hm: u32 = mnorm >> s;\n let mask: u32 = (1u << s) - 1u;\n let rem: u32 = mnorm & mask;\n let halfBt: u32 = 1u << (s - 1u);\n let add: u32 = select(0u, 1u, (rem > halfBt) || (rem == halfBt && (hm & 1u) == 1u));\n hm = hm + add;\n if (hm >= 0x400u) { return sign | (1u << 10u); }\n return sign | hm;\n }\n \n return sign; // signed zero\n }\n\n // Hybrid pack: software for subnormals, builtin for normal range\n fn pack2x16floatSafe(v: vec2f) -> u32 {\n // Convert the input floats to their 32-bit IEEE-754 bit patterns.\n // We'll inspect the exponent bits directly to determine their numeric range.\n let u_x: u32 = bitcast<u32>(v.x);\n let u_y: u32 = bitcast<u32>(v.y);\n \n // Extract the unbiased exponent for each component (float32 uses bias = 127).\n // e32 = exponent - 127 \u21D2 actual power of two for each value.\n let e32_x: i32 = i32((u_x >> 23u) & 0xFFu) - 127;\n let e32_y: i32 = i32((u_y >> 23u) & 0xFFu) - 127;\n \n // -------------------------------------------------------------------------\n // Detect values that would become *subnormal* (or zero) in float16.\n //\n // e32 < -14 \u21D4 |value| < 2^-14 \u2248 6.1035e-5\n //\n // Many mobile GPUs (including Adreno and Mali) mishandle half-precision\n // subnormals\u2014typically flushing them to zero or rounding incorrectly.\n // To preserve correct rounding and sign, we use the software conversion\n // path (floatToHalf) for these small magnitudes.\n //\n // The software branch runs very rarely (<0.1% of typical values for\n // normalized scene data) and costs only a few ALU instructions, so the\n // performance impact is negligible while avoiding visible precision loss.\n // -------------------------------------------------------------------------\n if (e32_x < -14 || e32_y < -14) {\n // Convert both components with the reference software routine\n // and pack into a 32-bit uint: low 16 bits = x, high 16 bits = y.\n return (floatToHalf(v.y) << 16u) | floatToHalf(v.x);\n }\n \n // Normal range: use the fast hardware builtin\n return pack2x16float(v);\n }\n\n#else\n\n // On non-Android platforms, use builtin directly (no subnormal workaround needed)\n fn pack2x16floatSafe(v: vec2f) -> u32 {\n return pack2x16float(v);\n }\n\n#endif\n"; export default _default;