
(FPCore (x y z) :precision binary64 (+ x (* (- y x) z)))
double code(double x, double y, double z) {
return x + ((y - x) * z);
}
real(8) function code(x, y, z)
real(8), intent (in) :: x
real(8), intent (in) :: y
real(8), intent (in) :: z
code = x + ((y - x) * z)
end function
public static double code(double x, double y, double z) {
return x + ((y - x) * z);
}
def code(x, y, z): return x + ((y - x) * z)
function code(x, y, z) return Float64(x + Float64(Float64(y - x) * z)) end
function tmp = code(x, y, z) tmp = x + ((y - x) * z); end
code[x_, y_, z_] := N[(x + N[(N[(y - x), $MachinePrecision] * z), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
x + \left(y - x\right) \cdot z
\end{array}
Sampling outcomes in binary64 precision:
Herbie found 5 alternatives:
| Alternative | Accuracy | Speedup |
|---|
(FPCore (x y z) :precision binary64 (+ x (* (- y x) z)))
double code(double x, double y, double z) {
return x + ((y - x) * z);
}
real(8) function code(x, y, z)
real(8), intent (in) :: x
real(8), intent (in) :: y
real(8), intent (in) :: z
code = x + ((y - x) * z)
end function
public static double code(double x, double y, double z) {
return x + ((y - x) * z);
}
def code(x, y, z): return x + ((y - x) * z)
function code(x, y, z) return Float64(x + Float64(Float64(y - x) * z)) end
function tmp = code(x, y, z) tmp = x + ((y - x) * z); end
code[x_, y_, z_] := N[(x + N[(N[(y - x), $MachinePrecision] * z), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
x + \left(y - x\right) \cdot z
\end{array}
(FPCore (x y z) :precision binary64 (fma (- y x) z x))
double code(double x, double y, double z) {
return fma((y - x), z, x);
}
function code(x, y, z) return fma(Float64(y - x), z, x) end
code[x_, y_, z_] := N[(N[(y - x), $MachinePrecision] * z + x), $MachinePrecision]
\begin{array}{l}
\\
\mathsf{fma}\left(y - x, z, x\right)
\end{array}
Initial program 100.0%
lift--.f64N/A
lift-*.f64N/A
+-commutativeN/A
lift-*.f64N/A
lower-fma.f64100.0
Applied rewrites100.0%
(FPCore (x y z)
:precision binary64
(let* ((t_0 (- (* x z))))
(if (<= z -1.45e+36)
t_0
(if (<= z 1.22e+83) (fma z y x) (if (<= z 4.5e+292) t_0 (* y z))))))
double code(double x, double y, double z) {
double t_0 = -(x * z);
double tmp;
if (z <= -1.45e+36) {
tmp = t_0;
} else if (z <= 1.22e+83) {
tmp = fma(z, y, x);
} else if (z <= 4.5e+292) {
tmp = t_0;
} else {
tmp = y * z;
}
return tmp;
}
function code(x, y, z) t_0 = Float64(-Float64(x * z)) tmp = 0.0 if (z <= -1.45e+36) tmp = t_0; elseif (z <= 1.22e+83) tmp = fma(z, y, x); elseif (z <= 4.5e+292) tmp = t_0; else tmp = Float64(y * z); end return tmp end
code[x_, y_, z_] := Block[{t$95$0 = (-N[(x * z), $MachinePrecision])}, If[LessEqual[z, -1.45e+36], t$95$0, If[LessEqual[z, 1.22e+83], N[(z * y + x), $MachinePrecision], If[LessEqual[z, 4.5e+292], t$95$0, N[(y * z), $MachinePrecision]]]]]
\begin{array}{l}
\\
\begin{array}{l}
t_0 := -x \cdot z\\
\mathbf{if}\;z \leq -1.45 \cdot 10^{+36}:\\
\;\;\;\;t\_0\\
\mathbf{elif}\;z \leq 1.22 \cdot 10^{+83}:\\
\;\;\;\;\mathsf{fma}\left(z, y, x\right)\\
\mathbf{elif}\;z \leq 4.5 \cdot 10^{+292}:\\
\;\;\;\;t\_0\\
\mathbf{else}:\\
\;\;\;\;y \cdot z\\
\end{array}
\end{array}
if z < -1.45e36 or 1.22e83 < z < 4.49999999999999984e292Initial program 100.0%
Taylor expanded in z around inf
lower-*.f64N/A
lower--.f64100.0
Applied rewrites100.0%
Taylor expanded in y around 0
mul-1-negN/A
lower-neg.f6462.0
Applied rewrites62.0%
if -1.45e36 < z < 1.22e83Initial program 100.0%
Taylor expanded in y around inf
lower-*.f6494.8
Applied rewrites94.8%
lift-*.f64N/A
+-commutativeN/A
lift-*.f64N/A
*-commutativeN/A
lower-fma.f6494.8
Applied rewrites94.8%
if 4.49999999999999984e292 < z Initial program 100.0%
Taylor expanded in x around 0
lower-*.f64100.0
Applied rewrites100.0%
Final simplification81.7%
(FPCore (x y z) :precision binary64 (let* ((t_0 (* (- y x) z))) (if (<= z -1.0) t_0 (if (<= z 1.0) (fma z y x) t_0))))
double code(double x, double y, double z) {
double t_0 = (y - x) * z;
double tmp;
if (z <= -1.0) {
tmp = t_0;
} else if (z <= 1.0) {
tmp = fma(z, y, x);
} else {
tmp = t_0;
}
return tmp;
}
function code(x, y, z) t_0 = Float64(Float64(y - x) * z) tmp = 0.0 if (z <= -1.0) tmp = t_0; elseif (z <= 1.0) tmp = fma(z, y, x); else tmp = t_0; end return tmp end
code[x_, y_, z_] := Block[{t$95$0 = N[(N[(y - x), $MachinePrecision] * z), $MachinePrecision]}, If[LessEqual[z, -1.0], t$95$0, If[LessEqual[z, 1.0], N[(z * y + x), $MachinePrecision], t$95$0]]]
\begin{array}{l}
\\
\begin{array}{l}
t_0 := \left(y - x\right) \cdot z\\
\mathbf{if}\;z \leq -1:\\
\;\;\;\;t\_0\\
\mathbf{elif}\;z \leq 1:\\
\;\;\;\;\mathsf{fma}\left(z, y, x\right)\\
\mathbf{else}:\\
\;\;\;\;t\_0\\
\end{array}
\end{array}
if z < -1 or 1 < z Initial program 100.0%
Taylor expanded in z around inf
lower-*.f64N/A
lower--.f6499.4
Applied rewrites99.4%
if -1 < z < 1Initial program 100.0%
Taylor expanded in y around inf
lower-*.f6499.3
Applied rewrites99.3%
lift-*.f64N/A
+-commutativeN/A
lift-*.f64N/A
*-commutativeN/A
lower-fma.f6499.3
Applied rewrites99.3%
Final simplification99.3%
(FPCore (x y z) :precision binary64 (fma z y x))
double code(double x, double y, double z) {
return fma(z, y, x);
}
function code(x, y, z) return fma(z, y, x) end
code[x_, y_, z_] := N[(z * y + x), $MachinePrecision]
\begin{array}{l}
\\
\mathsf{fma}\left(z, y, x\right)
\end{array}
Initial program 100.0%
Taylor expanded in y around inf
lower-*.f6474.8
Applied rewrites74.8%
lift-*.f64N/A
+-commutativeN/A
lift-*.f64N/A
*-commutativeN/A
lower-fma.f6474.8
Applied rewrites74.8%
(FPCore (x y z) :precision binary64 (* y z))
double code(double x, double y, double z) {
return y * z;
}
real(8) function code(x, y, z)
real(8), intent (in) :: x
real(8), intent (in) :: y
real(8), intent (in) :: z
code = y * z
end function
public static double code(double x, double y, double z) {
return y * z;
}
def code(x, y, z): return y * z
function code(x, y, z) return Float64(y * z) end
function tmp = code(x, y, z) tmp = y * z; end
code[x_, y_, z_] := N[(y * z), $MachinePrecision]
\begin{array}{l}
\\
y \cdot z
\end{array}
Initial program 100.0%
Taylor expanded in x around 0
lower-*.f6443.6
Applied rewrites43.6%
herbie shell --seed 2024216
(FPCore (x y z)
:name "Diagrams.ThreeD.Shapes:frustum from diagrams-lib-1.3.0.3, B"
:precision binary64
(+ x (* (- y x) z)))