// luma.gl // SPDX-License-Identifier: MIT // Copyright (c) vis.gl contributors export const fp64arithmeticShader = /* glsl */ `\ layout(std140) uniform fp64arithmeticUniforms { uniform float ONE; uniform float SPLIT; } fp64; /* About LUMA_FP64_CODE_ELIMINATION_WORKAROUND The purpose of this workaround is to prevent shader compilers from optimizing away necessary arithmetic operations by swapping their sequences or transform the equation to some 'equivalent' form. These helpers implement Dekker/Veltkamp-style error tracking. If the compiler folds constants or reassociates the arithmetic, the high/low split can stop tracking the rounding error correctly. That failure mode tends to look fine in simple coordinate setup, but then breaks down inside iterative arithmetic such as fp64 Mandelbrot loops. The method is to multiply an artifical variable, ONE, which will be known to the compiler to be 1 only at runtime. The whole expression is then represented as a polynomial with respective to ONE. In the coefficients of all terms, only one a and one b should appear err = (a + b) * ONE^6 - a * ONE^5 - (a + b) * ONE^4 + a * ONE^3 - b - (a + b) * ONE^2 + a * ONE */ float prevent_fp64_optimization(float value) { #if defined(LUMA_FP64_CODE_ELIMINATION_WORKAROUND) return value + fp64.ONE * 0.0; #else return value; #endif } // Divide float number to high and low floats to extend fraction bits vec2 split(float a) { // Keep SPLIT as a runtime uniform so the compiler cannot fold the Dekker // split into a constant expression and reassociate the recovery steps. float split = prevent_fp64_optimization(fp64.SPLIT); float t = prevent_fp64_optimization(a * split); float temp = t - a; float a_hi = t - temp; float a_lo = a - a_hi; return vec2(a_hi, a_lo); } // Divide float number again when high float uses too many fraction bits vec2 split2(vec2 a) { vec2 b = split(a.x); b.y += a.y; return b; } // Special sum operation when a > b vec2 quickTwoSum(float a, float b) { #if defined(LUMA_FP64_CODE_ELIMINATION_WORKAROUND) float sum = (a + b) * fp64.ONE; float err = b - (sum - a) * fp64.ONE; #else float sum = a + b; float err = b - (sum - a); #endif return vec2(sum, err); } // General sum operation vec2 twoSum(float a, float b) { float s = (a + b); #if defined(LUMA_FP64_CODE_ELIMINATION_WORKAROUND) float v = (s * fp64.ONE - a) * fp64.ONE; float err = (a - (s - v) * fp64.ONE) * fp64.ONE * fp64.ONE * fp64.ONE + (b - v); #else float v = s - a; float err = (a - (s - v)) + (b - v); #endif return vec2(s, err); } vec2 twoSub(float a, float b) { float s = (a - b); #if defined(LUMA_FP64_CODE_ELIMINATION_WORKAROUND) float v = (s * fp64.ONE - a) * fp64.ONE; float err = (a - (s - v) * fp64.ONE) * fp64.ONE * fp64.ONE * fp64.ONE - (b + v); #else float v = s - a; float err = (a - (s - v)) - (b + v); #endif return vec2(s, err); } vec2 twoSqr(float a) { float prod = a * a; vec2 a_fp64 = split(a); #if defined(LUMA_FP64_CODE_ELIMINATION_WORKAROUND) float err = ((a_fp64.x * a_fp64.x - prod) * fp64.ONE + 2.0 * a_fp64.x * a_fp64.y * fp64.ONE * fp64.ONE) + a_fp64.y * a_fp64.y * fp64.ONE * fp64.ONE * fp64.ONE; #else float err = ((a_fp64.x * a_fp64.x - prod) + 2.0 * a_fp64.x * a_fp64.y) + a_fp64.y * a_fp64.y; #endif return vec2(prod, err); } vec2 twoProd(float a, float b) { float prod = a * b; vec2 a_fp64 = split(a); vec2 b_fp64 = split(b); // twoProd is especially sensitive because mul_fp64 and div_fp64 both depend // on the split terms and cross terms staying in the original evaluation // order. If the compiler folds or reassociates them, the low part tends to // collapse to zero or NaN on some drivers. float highProduct = prevent_fp64_optimization(a_fp64.x * b_fp64.x); float crossProduct1 = prevent_fp64_optimization(a_fp64.x * b_fp64.y); float crossProduct2 = prevent_fp64_optimization(a_fp64.y * b_fp64.x); float lowProduct = prevent_fp64_optimization(a_fp64.y * b_fp64.y); #if defined(LUMA_FP64_CODE_ELIMINATION_WORKAROUND) float err1 = (highProduct - prod) * fp64.ONE; float err2 = crossProduct1 * fp64.ONE * fp64.ONE; float err3 = crossProduct2 * fp64.ONE * fp64.ONE * fp64.ONE; float err4 = lowProduct * fp64.ONE * fp64.ONE * fp64.ONE * fp64.ONE; #else float err1 = highProduct - prod; float err2 = crossProduct1; float err3 = crossProduct2; float err4 = lowProduct; #endif float err = ((err1 + err2) + err3) + err4; return vec2(prod, err); } vec2 sum_fp64(vec2 a, vec2 b) { vec2 s, t; s = twoSum(a.x, b.x); t = twoSum(a.y, b.y); s.y += t.x; s = quickTwoSum(s.x, s.y); s.y += t.y; s = quickTwoSum(s.x, s.y); return s; } vec2 sub_fp64(vec2 a, vec2 b) { vec2 s, t; s = twoSub(a.x, b.x); t = twoSub(a.y, b.y); s.y += t.x; s = quickTwoSum(s.x, s.y); s.y += t.y; s = quickTwoSum(s.x, s.y); return s; } vec2 mul_fp64(vec2 a, vec2 b) { vec2 prod = twoProd(a.x, b.x); // y component is for the error prod.y += a.x * b.y; #if defined(LUMA_FP64_HIGH_BITS_OVERFLOW_WORKAROUND) prod = split2(prod); #endif prod = quickTwoSum(prod.x, prod.y); prod.y += a.y * b.x; #if defined(LUMA_FP64_HIGH_BITS_OVERFLOW_WORKAROUND) prod = split2(prod); #endif prod = quickTwoSum(prod.x, prod.y); return prod; } vec2 div_fp64(vec2 a, vec2 b) { float xn = 1.0 / b.x; #if defined(LUMA_FP64_HIGH_BITS_OVERFLOW_WORKAROUND) vec2 yn = mul_fp64(a, vec2(xn, 0)); #else vec2 yn = a * xn; #endif float diff = (sub_fp64(a, mul_fp64(b, yn))).x; vec2 prod = twoProd(xn, diff); return sum_fp64(yn, prod); } vec2 sqrt_fp64(vec2 a) { if (a.x == 0.0 && a.y == 0.0) return vec2(0.0, 0.0); if (a.x < 0.0) return vec2(0.0 / 0.0, 0.0 / 0.0); float x = 1.0 / sqrt(a.x); float yn = a.x * x; #if defined(LUMA_FP64_CODE_ELIMINATION_WORKAROUND) vec2 yn_sqr = twoSqr(yn) * fp64.ONE; #else vec2 yn_sqr = twoSqr(yn); #endif float diff = sub_fp64(a, yn_sqr).x; vec2 prod = twoProd(x * 0.5, diff); #if defined(LUMA_FP64_HIGH_BITS_OVERFLOW_WORKAROUND) return sum_fp64(split(yn), prod); #else return sum_fp64(vec2(yn, 0.0), prod); #endif } `;