exp neg sub

Percentage Accurate: 100.0% → 100.0%
Time: 4.3s
Alternatives: 4
Speedup: 1.0×

Specification

?
\[\begin{array}{l} \\ e^{-\left(1 - x \cdot x\right)} \end{array} \]
(FPCore (x) :precision binary64 (exp (- (- 1.0 (* x x)))))
double code(double x) {
	return exp(-(1.0 - (x * x)));
}
real(8) function code(x)
    real(8), intent (in) :: x
    code = exp(-(1.0d0 - (x * x)))
end function
public static double code(double x) {
	return Math.exp(-(1.0 - (x * x)));
}
def code(x):
	return math.exp(-(1.0 - (x * x)))
function code(x)
	return exp(Float64(-Float64(1.0 - Float64(x * x))))
end
function tmp = code(x)
	tmp = exp(-(1.0 - (x * x)));
end
code[x_] := N[Exp[(-N[(1.0 - N[(x * x), $MachinePrecision]), $MachinePrecision])], $MachinePrecision]
\begin{array}{l}

\\
e^{-\left(1 - x \cdot x\right)}
\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 4 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} \\ e^{-\left(1 - x \cdot x\right)} \end{array} \]
(FPCore (x) :precision binary64 (exp (- (- 1.0 (* x x)))))
double code(double x) {
	return exp(-(1.0 - (x * x)));
}
real(8) function code(x)
    real(8), intent (in) :: x
    code = exp(-(1.0d0 - (x * x)))
end function
public static double code(double x) {
	return Math.exp(-(1.0 - (x * x)));
}
def code(x):
	return math.exp(-(1.0 - (x * x)))
function code(x)
	return exp(Float64(-Float64(1.0 - Float64(x * x))))
end
function tmp = code(x)
	tmp = exp(-(1.0 - (x * x)));
end
code[x_] := N[Exp[(-N[(1.0 - N[(x * x), $MachinePrecision]), $MachinePrecision])], $MachinePrecision]
\begin{array}{l}

\\
e^{-\left(1 - x \cdot x\right)}
\end{array}

Alternative 1: 100.0% accurate, 1.0× speedup?

\[\begin{array}{l} x_m = \left|x\right| \\ e^{\mathsf{fma}\left(x\_m, x\_m - 1, x\_m - 1\right)} \end{array} \]
x_m = (fabs.f64 x)
(FPCore (x_m) :precision binary64 (exp (fma x_m (- x_m 1.0) (- x_m 1.0))))
x_m = fabs(x);
double code(double x_m) {
	return exp(fma(x_m, (x_m - 1.0), (x_m - 1.0)));
}
x_m = abs(x)
function code(x_m)
	return exp(fma(x_m, Float64(x_m - 1.0), Float64(x_m - 1.0)))
end
x_m = N[Abs[x], $MachinePrecision]
code[x$95$m_] := N[Exp[N[(x$95$m * N[(x$95$m - 1.0), $MachinePrecision] + N[(x$95$m - 1.0), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]
\begin{array}{l}
x_m = \left|x\right|

\\
e^{\mathsf{fma}\left(x\_m, x\_m - 1, x\_m - 1\right)}
\end{array}
Derivation
  1. Initial program 100.0%

    \[e^{-\left(1 - x \cdot x\right)} \]
  2. Add Preprocessing
  3. Applied rewrites75.4%

    \[\leadsto e^{\color{blue}{\frac{-1 + {x}^{4}}{\mathsf{fma}\left(x, x, 1\right)}}} \]
  4. Step-by-step derivation
    1. lift-/.f64N/A

      \[\leadsto e^{\color{blue}{\frac{-1 + {x}^{4}}{\mathsf{fma}\left(x, x, 1\right)}}} \]
    2. lift-+.f64N/A

      \[\leadsto e^{\frac{\color{blue}{-1 + {x}^{4}}}{\mathsf{fma}\left(x, x, 1\right)}} \]
    3. +-commutativeN/A

      \[\leadsto e^{\frac{\color{blue}{{x}^{4} + -1}}{\mathsf{fma}\left(x, x, 1\right)}} \]
    4. metadata-evalN/A

      \[\leadsto e^{\frac{{x}^{4} + \color{blue}{\left(\mathsf{neg}\left(1\right)\right)}}{\mathsf{fma}\left(x, x, 1\right)}} \]
    5. sub-negN/A

      \[\leadsto e^{\frac{\color{blue}{{x}^{4} - 1}}{\mathsf{fma}\left(x, x, 1\right)}} \]
    6. lift-pow.f64N/A

      \[\leadsto e^{\frac{\color{blue}{{x}^{4}} - 1}{\mathsf{fma}\left(x, x, 1\right)}} \]
    7. metadata-evalN/A

      \[\leadsto e^{\frac{{x}^{\color{blue}{\left(2 \cdot 2\right)}} - 1}{\mathsf{fma}\left(x, x, 1\right)}} \]
    8. pow-powN/A

      \[\leadsto e^{\frac{\color{blue}{{\left({x}^{2}\right)}^{2}} - 1}{\mathsf{fma}\left(x, x, 1\right)}} \]
    9. pow2N/A

      \[\leadsto e^{\frac{{\color{blue}{\left(x \cdot x\right)}}^{2} - 1}{\mathsf{fma}\left(x, x, 1\right)}} \]
    10. pow2N/A

      \[\leadsto e^{\frac{\color{blue}{\left(x \cdot x\right) \cdot \left(x \cdot x\right)} - 1}{\mathsf{fma}\left(x, x, 1\right)}} \]
    11. metadata-evalN/A

      \[\leadsto e^{\frac{\left(x \cdot x\right) \cdot \left(x \cdot x\right) - \color{blue}{-1 \cdot -1}}{\mathsf{fma}\left(x, x, 1\right)}} \]
    12. lift-fma.f64N/A

      \[\leadsto e^{\frac{\left(x \cdot x\right) \cdot \left(x \cdot x\right) - -1 \cdot -1}{\color{blue}{x \cdot x + 1}}} \]
    13. metadata-evalN/A

      \[\leadsto e^{\frac{\left(x \cdot x\right) \cdot \left(x \cdot x\right) - -1 \cdot -1}{x \cdot x + \color{blue}{\left(\mathsf{neg}\left(-1\right)\right)}}} \]
    14. sub-negN/A

      \[\leadsto e^{\frac{\left(x \cdot x\right) \cdot \left(x \cdot x\right) - -1 \cdot -1}{\color{blue}{x \cdot x - -1}}} \]
    15. flip-+N/A

      \[\leadsto e^{\color{blue}{x \cdot x + -1}} \]
    16. difference-of-sqr--1N/A

      \[\leadsto e^{\color{blue}{\left(x + 1\right) \cdot \left(x - 1\right)}} \]
    17. *-commutativeN/A

      \[\leadsto e^{\color{blue}{\left(x - 1\right) \cdot \left(x + 1\right)}} \]
    18. distribute-rgt-inN/A

      \[\leadsto e^{\color{blue}{x \cdot \left(x - 1\right) + 1 \cdot \left(x - 1\right)}} \]
  5. Applied rewrites100.0%

    \[\leadsto e^{\color{blue}{\mathsf{fma}\left(x, x - 1, x - 1\right)}} \]
  6. Add Preprocessing

Alternative 2: 99.2% accurate, 0.9× speedup?

\[\begin{array}{l} x_m = \left|x\right| \\ \begin{array}{l} \mathbf{if}\;x\_m \cdot x\_m \leq 0.95:\\ \;\;\;\;e^{-1}\\ \mathbf{else}:\\ \;\;\;\;e^{x\_m \cdot x\_m}\\ \end{array} \end{array} \]
x_m = (fabs.f64 x)
(FPCore (x_m)
 :precision binary64
 (if (<= (* x_m x_m) 0.95) (exp -1.0) (exp (* x_m x_m))))
x_m = fabs(x);
double code(double x_m) {
	double tmp;
	if ((x_m * x_m) <= 0.95) {
		tmp = exp(-1.0);
	} else {
		tmp = exp((x_m * x_m));
	}
	return tmp;
}
x_m = abs(x)
real(8) function code(x_m)
    real(8), intent (in) :: x_m
    real(8) :: tmp
    if ((x_m * x_m) <= 0.95d0) then
        tmp = exp((-1.0d0))
    else
        tmp = exp((x_m * x_m))
    end if
    code = tmp
end function
x_m = Math.abs(x);
public static double code(double x_m) {
	double tmp;
	if ((x_m * x_m) <= 0.95) {
		tmp = Math.exp(-1.0);
	} else {
		tmp = Math.exp((x_m * x_m));
	}
	return tmp;
}
x_m = math.fabs(x)
def code(x_m):
	tmp = 0
	if (x_m * x_m) <= 0.95:
		tmp = math.exp(-1.0)
	else:
		tmp = math.exp((x_m * x_m))
	return tmp
x_m = abs(x)
function code(x_m)
	tmp = 0.0
	if (Float64(x_m * x_m) <= 0.95)
		tmp = exp(-1.0);
	else
		tmp = exp(Float64(x_m * x_m));
	end
	return tmp
end
x_m = abs(x);
function tmp_2 = code(x_m)
	tmp = 0.0;
	if ((x_m * x_m) <= 0.95)
		tmp = exp(-1.0);
	else
		tmp = exp((x_m * x_m));
	end
	tmp_2 = tmp;
end
x_m = N[Abs[x], $MachinePrecision]
code[x$95$m_] := If[LessEqual[N[(x$95$m * x$95$m), $MachinePrecision], 0.95], N[Exp[-1.0], $MachinePrecision], N[Exp[N[(x$95$m * x$95$m), $MachinePrecision]], $MachinePrecision]]
\begin{array}{l}
x_m = \left|x\right|

\\
\begin{array}{l}
\mathbf{if}\;x\_m \cdot x\_m \leq 0.95:\\
\;\;\;\;e^{-1}\\

\mathbf{else}:\\
\;\;\;\;e^{x\_m \cdot x\_m}\\


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

    1. Initial program 100.0%

      \[e^{-\left(1 - x \cdot x\right)} \]
    2. Add Preprocessing
    3. Taylor expanded in x around 0

      \[\leadsto e^{\color{blue}{-1}} \]
    4. Step-by-step derivation
      1. Applied rewrites99.4%

        \[\leadsto e^{\color{blue}{-1}} \]

      if 0.94999999999999996 < (*.f64 x x)

      1. Initial program 100.0%

        \[e^{-\left(1 - x \cdot x\right)} \]
      2. Add Preprocessing
      3. Taylor expanded in x around 0

        \[\leadsto e^{\color{blue}{-1}} \]
      4. Step-by-step derivation
        1. Applied rewrites3.1%

          \[\leadsto e^{\color{blue}{-1}} \]
        2. Taylor expanded in x around inf

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

            \[\leadsto e^{\color{blue}{x \cdot x}} \]
          2. lower-*.f64100.0

            \[\leadsto e^{\color{blue}{x \cdot x}} \]
        4. Applied rewrites100.0%

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

      Alternative 3: 100.0% accurate, 1.0× speedup?

      \[\begin{array}{l} x_m = \left|x\right| \\ e^{\mathsf{fma}\left(x\_m, x\_m, -1\right)} \end{array} \]
      x_m = (fabs.f64 x)
      (FPCore (x_m) :precision binary64 (exp (fma x_m x_m -1.0)))
      x_m = fabs(x);
      double code(double x_m) {
      	return exp(fma(x_m, x_m, -1.0));
      }
      
      x_m = abs(x)
      function code(x_m)
      	return exp(fma(x_m, x_m, -1.0))
      end
      
      x_m = N[Abs[x], $MachinePrecision]
      code[x$95$m_] := N[Exp[N[(x$95$m * x$95$m + -1.0), $MachinePrecision]], $MachinePrecision]
      
      \begin{array}{l}
      x_m = \left|x\right|
      
      \\
      e^{\mathsf{fma}\left(x\_m, x\_m, -1\right)}
      \end{array}
      
      Derivation
      1. Initial program 100.0%

        \[e^{-\left(1 - x \cdot x\right)} \]
      2. Add Preprocessing
      3. Step-by-step derivation
        1. lift-neg.f64N/A

          \[\leadsto e^{\color{blue}{\mathsf{neg}\left(\left(1 - x \cdot x\right)\right)}} \]
        2. neg-sub0N/A

          \[\leadsto e^{\color{blue}{0 - \left(1 - x \cdot x\right)}} \]
        3. lift--.f64N/A

          \[\leadsto e^{0 - \color{blue}{\left(1 - x \cdot x\right)}} \]
        4. associate--r-N/A

          \[\leadsto e^{\color{blue}{\left(0 - 1\right) + x \cdot x}} \]
        5. metadata-evalN/A

          \[\leadsto e^{\color{blue}{-1} + x \cdot x} \]
        6. +-commutativeN/A

          \[\leadsto e^{\color{blue}{x \cdot x + -1}} \]
        7. lift-*.f64N/A

          \[\leadsto e^{\color{blue}{x \cdot x} + -1} \]
        8. lower-fma.f64100.0

          \[\leadsto e^{\color{blue}{\mathsf{fma}\left(x, x, -1\right)}} \]
      4. Applied rewrites100.0%

        \[\leadsto e^{\color{blue}{\mathsf{fma}\left(x, x, -1\right)}} \]
      5. Add Preprocessing

      Alternative 4: 50.3% accurate, 1.1× speedup?

      \[\begin{array}{l} x_m = \left|x\right| \\ e^{-1} \end{array} \]
      x_m = (fabs.f64 x)
      (FPCore (x_m) :precision binary64 (exp -1.0))
      x_m = fabs(x);
      double code(double x_m) {
      	return exp(-1.0);
      }
      
      x_m = abs(x)
      real(8) function code(x_m)
          real(8), intent (in) :: x_m
          code = exp((-1.0d0))
      end function
      
      x_m = Math.abs(x);
      public static double code(double x_m) {
      	return Math.exp(-1.0);
      }
      
      x_m = math.fabs(x)
      def code(x_m):
      	return math.exp(-1.0)
      
      x_m = abs(x)
      function code(x_m)
      	return exp(-1.0)
      end
      
      x_m = abs(x);
      function tmp = code(x_m)
      	tmp = exp(-1.0);
      end
      
      x_m = N[Abs[x], $MachinePrecision]
      code[x$95$m_] := N[Exp[-1.0], $MachinePrecision]
      
      \begin{array}{l}
      x_m = \left|x\right|
      
      \\
      e^{-1}
      \end{array}
      
      Derivation
      1. Initial program 100.0%

        \[e^{-\left(1 - x \cdot x\right)} \]
      2. Add Preprocessing
      3. Taylor expanded in x around 0

        \[\leadsto e^{\color{blue}{-1}} \]
      4. Step-by-step derivation
        1. Applied rewrites51.6%

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

        Reproduce

        ?
        herbie shell --seed 2024327 
        (FPCore (x)
          :name "exp neg sub"
          :precision binary64
          (exp (- (- 1.0 (* x x)))))