expax (section 3.5)

Percentage Accurate: 53.8% → 100.0%
Time: 2.5s
Alternatives: 4
Speedup: 18.2×

Specification

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

\\
e^{a \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 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: 53.8% accurate, 1.0× speedup?

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

\\
e^{a \cdot x} - 1
\end{array}

Alternative 1: 100.0% accurate, 1.0× speedup?

\[\begin{array}{l} \\ \mathsf{expm1}\left(x \cdot a\right) \end{array} \]
(FPCore (a x) :precision binary64 (expm1 (* x a)))
double code(double a, double x) {
	return expm1((x * a));
}
public static double code(double a, double x) {
	return Math.expm1((x * a));
}
def code(a, x):
	return math.expm1((x * a))
function code(a, x)
	return expm1(Float64(x * a))
end
code[a_, x_] := N[(Exp[N[(x * a), $MachinePrecision]] - 1), $MachinePrecision]
\begin{array}{l}

\\
\mathsf{expm1}\left(x \cdot a\right)
\end{array}
Derivation
  1. Initial program 49.7%

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

      \[\leadsto \color{blue}{e^{a \cdot x} - 1} \]
    2. lift-exp.f64N/A

      \[\leadsto \color{blue}{e^{a \cdot x}} - 1 \]
    3. lower-expm1.f64100.0

      \[\leadsto \color{blue}{\mathsf{expm1}\left(a \cdot x\right)} \]
    4. lift-*.f64N/A

      \[\leadsto \mathsf{expm1}\left(\color{blue}{a \cdot x}\right) \]
    5. *-commutativeN/A

      \[\leadsto \mathsf{expm1}\left(\color{blue}{x \cdot a}\right) \]
    6. lower-*.f64100.0

      \[\leadsto \mathsf{expm1}\left(\color{blue}{x \cdot a}\right) \]
  4. Applied rewrites100.0%

    \[\leadsto \color{blue}{\mathsf{expm1}\left(x \cdot a\right)} \]
  5. Add Preprocessing

Alternative 2: 67.5% accurate, 3.3× speedup?

\[\begin{array}{l} \\ \mathsf{fma}\left(\mathsf{fma}\left(0.16666666666666666 \cdot x, a, 0.5\right) \cdot a, x, 1\right) \cdot \left(x \cdot a\right) \end{array} \]
(FPCore (a x)
 :precision binary64
 (* (fma (* (fma (* 0.16666666666666666 x) a 0.5) a) x 1.0) (* x a)))
double code(double a, double x) {
	return fma((fma((0.16666666666666666 * x), a, 0.5) * a), x, 1.0) * (x * a);
}
function code(a, x)
	return Float64(fma(Float64(fma(Float64(0.16666666666666666 * x), a, 0.5) * a), x, 1.0) * Float64(x * a))
end
code[a_, x_] := N[(N[(N[(N[(N[(0.16666666666666666 * x), $MachinePrecision] * a + 0.5), $MachinePrecision] * a), $MachinePrecision] * x + 1.0), $MachinePrecision] * N[(x * a), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}

\\
\mathsf{fma}\left(\mathsf{fma}\left(0.16666666666666666 \cdot x, a, 0.5\right) \cdot a, x, 1\right) \cdot \left(x \cdot a\right)
\end{array}
Derivation
  1. Initial program 49.7%

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

    \[\leadsto \color{blue}{a \cdot \left(x + a \cdot \left(\frac{1}{6} \cdot \left(a \cdot {x}^{3}\right) + \frac{1}{2} \cdot {x}^{2}\right)\right)} \]
  4. Applied rewrites71.7%

    \[\leadsto \color{blue}{\mathsf{fma}\left(\mathsf{fma}\left(0.16666666666666666 \cdot x, a, 0.5\right) \cdot a, x, 1\right) \cdot \left(x \cdot a\right)} \]
  5. Add Preprocessing

Alternative 3: 67.0% accurate, 18.2× speedup?

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

\\
x \cdot a
\end{array}
Derivation
  1. Initial program 49.7%

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

    \[\leadsto \color{blue}{a \cdot x} \]
  4. Step-by-step derivation
    1. *-commutativeN/A

      \[\leadsto \color{blue}{x \cdot a} \]
    2. lower-*.f6470.7

      \[\leadsto \color{blue}{x \cdot a} \]
  5. Applied rewrites70.7%

    \[\leadsto \color{blue}{x \cdot a} \]
  6. Add Preprocessing

Alternative 4: 19.5% accurate, 27.3× speedup?

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

\\
1 - 1
\end{array}
Derivation
  1. Initial program 49.7%

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

    \[\leadsto \color{blue}{1} - 1 \]
  4. Step-by-step derivation
    1. Applied rewrites19.0%

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

    Developer Target 1: 100.0% accurate, 1.0× speedup?

    \[\begin{array}{l} \\ \mathsf{expm1}\left(a \cdot x\right) \end{array} \]
    (FPCore (a x) :precision binary64 (expm1 (* a x)))
    double code(double a, double x) {
    	return expm1((a * x));
    }
    
    public static double code(double a, double x) {
    	return Math.expm1((a * x));
    }
    
    def code(a, x):
    	return math.expm1((a * x))
    
    function code(a, x)
    	return expm1(Float64(a * x))
    end
    
    code[a_, x_] := N[(Exp[N[(a * x), $MachinePrecision]] - 1), $MachinePrecision]
    
    \begin{array}{l}
    
    \\
    \mathsf{expm1}\left(a \cdot x\right)
    \end{array}
    

    Reproduce

    ?
    herbie shell --seed 1 
    (FPCore (a x)
      :name "expax (section 3.5)"
      :precision binary64
      :pre (> 710.0 (* a x))
    
      :alt
      (! :herbie-platform default (expm1 (* a x)))
    
      (- (exp (* a x)) 1.0))