Graphics.Rasterific.Shading:$sradialGradientWithFocusShader from Rasterific-0.6.1, A

Percentage Accurate: 100.0% → 100.0%
Time: 2.5s
Alternatives: 3
Speedup: 1.0×

Specification

?
\[\begin{array}{l} \\ x \cdot x + 1 \end{array} \]
(FPCore (x) :precision binary64 (+ (* x x) 1.0))
double code(double x) {
	return (x * x) + 1.0;
}
real(8) function code(x)
    real(8), intent (in) :: x
    code = (x * x) + 1.0d0
end function
public static double code(double x) {
	return (x * x) + 1.0;
}
def code(x):
	return (x * x) + 1.0
function code(x)
	return Float64(Float64(x * x) + 1.0)
end
function tmp = code(x)
	tmp = (x * x) + 1.0;
end
code[x_] := N[(N[(x * x), $MachinePrecision] + 1.0), $MachinePrecision]
\begin{array}{l}

\\
x \cdot x + 1
\end{array}

Sampling outcomes in binary64 precision:

Local Percentage Accuracy vs ?

The average percentage accuracy by input value. Horizontal axis shows value of an input variable; the variable is choosen in the title. Vertical axis is accuracy; higher is better. Red represent the original program, while blue represents Herbie's suggestion. These can be toggled with buttons below the plot. The line is an average while dots represent individual samples.

Accuracy vs Speed?

Herbie found 3 alternatives:

AlternativeAccuracySpeedup
The accuracy (vertical axis) and speed (horizontal axis) of each alternatives. Up and to the right is better. The red square shows the initial program, and each blue circle shows an alternative.The line shows the best available speed-accuracy tradeoffs.

Initial Program: 100.0% accurate, 1.0× speedup?

\[\begin{array}{l} \\ x \cdot x + 1 \end{array} \]
(FPCore (x) :precision binary64 (+ (* x x) 1.0))
double code(double x) {
	return (x * x) + 1.0;
}
real(8) function code(x)
    real(8), intent (in) :: x
    code = (x * x) + 1.0d0
end function
public static double code(double x) {
	return (x * x) + 1.0;
}
def code(x):
	return (x * x) + 1.0
function code(x)
	return Float64(Float64(x * x) + 1.0)
end
function tmp = code(x)
	tmp = (x * x) + 1.0;
end
code[x_] := N[(N[(x * x), $MachinePrecision] + 1.0), $MachinePrecision]
\begin{array}{l}

\\
x \cdot x + 1
\end{array}

Alternative 1: 100.0% accurate, 1.0× speedup?

\[\begin{array}{l} \\ x \cdot x + 1 \end{array} \]
(FPCore (x) :precision binary64 (+ (* x x) 1.0))
double code(double x) {
	return (x * x) + 1.0;
}
real(8) function code(x)
    real(8), intent (in) :: x
    code = (x * x) + 1.0d0
end function
public static double code(double x) {
	return (x * x) + 1.0;
}
def code(x):
	return (x * x) + 1.0
function code(x)
	return Float64(Float64(x * x) + 1.0)
end
function tmp = code(x)
	tmp = (x * x) + 1.0;
end
code[x_] := N[(N[(x * x), $MachinePrecision] + 1.0), $MachinePrecision]
\begin{array}{l}

\\
x \cdot x + 1
\end{array}
Derivation
  1. Initial program 100.0%

    \[x \cdot x + 1 \]
  2. Add Preprocessing
  3. Add Preprocessing

Alternative 2: 98.9% accurate, 0.5× speedup?

\[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;x \cdot x \leq 0.5:\\ \;\;\;\;1\\ \mathbf{else}:\\ \;\;\;\;x \cdot x\\ \end{array} \end{array} \]
(FPCore (x) :precision binary64 (if (<= (* x x) 0.5) 1.0 (* x x)))
double code(double x) {
	double tmp;
	if ((x * x) <= 0.5) {
		tmp = 1.0;
	} else {
		tmp = x * x;
	}
	return tmp;
}
real(8) function code(x)
    real(8), intent (in) :: x
    real(8) :: tmp
    if ((x * x) <= 0.5d0) then
        tmp = 1.0d0
    else
        tmp = x * x
    end if
    code = tmp
end function
public static double code(double x) {
	double tmp;
	if ((x * x) <= 0.5) {
		tmp = 1.0;
	} else {
		tmp = x * x;
	}
	return tmp;
}
def code(x):
	tmp = 0
	if (x * x) <= 0.5:
		tmp = 1.0
	else:
		tmp = x * x
	return tmp
function code(x)
	tmp = 0.0
	if (Float64(x * x) <= 0.5)
		tmp = 1.0;
	else
		tmp = Float64(x * x);
	end
	return tmp
end
function tmp_2 = code(x)
	tmp = 0.0;
	if ((x * x) <= 0.5)
		tmp = 1.0;
	else
		tmp = x * x;
	end
	tmp_2 = tmp;
end
code[x_] := If[LessEqual[N[(x * x), $MachinePrecision], 0.5], 1.0, N[(x * x), $MachinePrecision]]
\begin{array}{l}

\\
\begin{array}{l}
\mathbf{if}\;x \cdot x \leq 0.5:\\
\;\;\;\;1\\

\mathbf{else}:\\
\;\;\;\;x \cdot x\\


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if (*.f64 x x) < 0.5

    1. Initial program 100.0%

      \[x \cdot x + 1 \]
    2. Add Preprocessing
    3. Taylor expanded in x around 0

      \[\leadsto \color{blue}{1} \]
    4. Step-by-step derivation
      1. Simplified98.9%

        \[\leadsto \color{blue}{1} \]

      if 0.5 < (*.f64 x x)

      1. Initial program 100.0%

        \[x \cdot x + 1 \]
      2. Add Preprocessing
      3. Taylor expanded in x around inf

        \[\leadsto \color{blue}{{x}^{2}} \]
      4. Step-by-step derivation
        1. unpow2N/A

          \[\leadsto x \cdot \color{blue}{x} \]
        2. *-lowering-*.f6499.5%

          \[\leadsto \mathsf{*.f64}\left(x, \color{blue}{x}\right) \]
      5. Simplified99.5%

        \[\leadsto \color{blue}{x \cdot x} \]
    5. Recombined 2 regimes into one program.
    6. Add Preprocessing

    Alternative 3: 51.6% accurate, 5.0× speedup?

    \[\begin{array}{l} \\ 1 \end{array} \]
    (FPCore (x) :precision binary64 1.0)
    double code(double x) {
    	return 1.0;
    }
    
    real(8) function code(x)
        real(8), intent (in) :: x
        code = 1.0d0
    end function
    
    public static double code(double x) {
    	return 1.0;
    }
    
    def code(x):
    	return 1.0
    
    function code(x)
    	return 1.0
    end
    
    function tmp = code(x)
    	tmp = 1.0;
    end
    
    code[x_] := 1.0
    
    \begin{array}{l}
    
    \\
    1
    \end{array}
    
    Derivation
    1. Initial program 100.0%

      \[x \cdot x + 1 \]
    2. Add Preprocessing
    3. Taylor expanded in x around 0

      \[\leadsto \color{blue}{1} \]
    4. Step-by-step derivation
      1. Simplified54.7%

        \[\leadsto \color{blue}{1} \]
      2. Add Preprocessing

      Reproduce

      ?
      herbie shell --seed 2024163 
      (FPCore (x)
        :name "Graphics.Rasterific.Shading:$sradialGradientWithFocusShader from Rasterific-0.6.1, A"
        :precision binary64
        (+ (* x x) 1.0))