license import { GPGPUProgram } from './gpgpu_math' ; export class ComplexAbsProgram implements GPGPUProgram { variableNames = [ 'real' , 'imag' ] ; userCode : string ; outputShape : number [ ] ; constructor ( shape ) { this . outputShape = shape ; this . userCode = `
      void main() {
        float re = abs(getRealAtOutCoords());
        float im = abs(getImagAtOutCoords());
        float mx = max(re, im);

        // sadly the length function in glsl is not underflow-safe
        // (at least not on Intel GPUs). So the safe solution is
        // to ensure underflow-safety in all cases.
        setOutput(
          mx == 0.0 ? 0.0 : mx * length(vec2(1, min(re, im)/mx))
        );
      }
    ` ; } }