
(FPCore (x y z) :precision binary64 (+ (* x (+ y z)) (* z 5.0)))
double code(double x, double y, double z) {
return (x * (y + z)) + (z * 5.0);
}
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 + z)) + (z * 5.0d0)
end function
public static double code(double x, double y, double z) {
return (x * (y + z)) + (z * 5.0);
}
def code(x, y, z): return (x * (y + z)) + (z * 5.0)
function code(x, y, z) return Float64(Float64(x * Float64(y + z)) + Float64(z * 5.0)) end
function tmp = code(x, y, z) tmp = (x * (y + z)) + (z * 5.0); end
code[x_, y_, z_] := N[(N[(x * N[(y + z), $MachinePrecision]), $MachinePrecision] + N[(z * 5.0), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
x \cdot \left(y + z\right) + z \cdot 5
\end{array}
Sampling outcomes in binary64 precision:
Herbie found 8 alternatives:
| Alternative | Accuracy | Speedup |
|---|
(FPCore (x y z) :precision binary64 (+ (* x (+ y z)) (* z 5.0)))
double code(double x, double y, double z) {
return (x * (y + z)) + (z * 5.0);
}
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 + z)) + (z * 5.0d0)
end function
public static double code(double x, double y, double z) {
return (x * (y + z)) + (z * 5.0);
}
def code(x, y, z): return (x * (y + z)) + (z * 5.0)
function code(x, y, z) return Float64(Float64(x * Float64(y + z)) + Float64(z * 5.0)) end
function tmp = code(x, y, z) tmp = (x * (y + z)) + (z * 5.0); end
code[x_, y_, z_] := N[(N[(x * N[(y + z), $MachinePrecision]), $MachinePrecision] + N[(z * 5.0), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
x \cdot \left(y + z\right) + z \cdot 5
\end{array}
(FPCore (x y z) :precision binary64 (fma z 5.0 (* (+ z y) x)))
double code(double x, double y, double z) {
return fma(z, 5.0, ((z + y) * x));
}
function code(x, y, z) return fma(z, 5.0, Float64(Float64(z + y) * x)) end
code[x_, y_, z_] := N[(z * 5.0 + N[(N[(z + y), $MachinePrecision] * x), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\mathsf{fma}\left(z, 5, \left(z + y\right) \cdot x\right)
\end{array}
Initial program 99.9%
lift-+.f64N/A
+-commutativeN/A
lift-*.f64N/A
lower-fma.f64100.0
lift-*.f64N/A
*-commutativeN/A
lower-*.f64100.0
lift-+.f64N/A
+-commutativeN/A
lower-+.f64100.0
Applied rewrites100.0%
(FPCore (x y z) :precision binary64 (if (or (<= x -72000.0) (not (<= x 5.0))) (* (+ z y) x) (fma 5.0 z (* x y))))
double code(double x, double y, double z) {
double tmp;
if ((x <= -72000.0) || !(x <= 5.0)) {
tmp = (z + y) * x;
} else {
tmp = fma(5.0, z, (x * y));
}
return tmp;
}
function code(x, y, z) tmp = 0.0 if ((x <= -72000.0) || !(x <= 5.0)) tmp = Float64(Float64(z + y) * x); else tmp = fma(5.0, z, Float64(x * y)); end return tmp end
code[x_, y_, z_] := If[Or[LessEqual[x, -72000.0], N[Not[LessEqual[x, 5.0]], $MachinePrecision]], N[(N[(z + y), $MachinePrecision] * x), $MachinePrecision], N[(5.0 * z + N[(x * y), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq -72000 \lor \neg \left(x \leq 5\right):\\
\;\;\;\;\left(z + y\right) \cdot x\\
\mathbf{else}:\\
\;\;\;\;\mathsf{fma}\left(5, z, x \cdot y\right)\\
\end{array}
\end{array}
if x < -72000 or 5 < x Initial program 100.0%
lift-+.f64N/A
+-commutativeN/A
lift-*.f64N/A
lower-fma.f64100.0
lift-*.f64N/A
*-commutativeN/A
lower-*.f64100.0
lift-+.f64N/A
+-commutativeN/A
lower-+.f64100.0
Applied rewrites100.0%
lift-fma.f64N/A
lift-*.f64N/A
+-commutativeN/A
lift-*.f64N/A
*-commutativeN/A
lift-+.f64N/A
+-commutativeN/A
distribute-rgt-inN/A
lift-*.f64N/A
lift-*.f64N/A
associate-+l+N/A
lift-*.f64N/A
lift-*.f64N/A
lift-*.f64N/A
distribute-lft-inN/A
metadata-evalN/A
sub-negN/A
lift--.f64N/A
*-commutativeN/A
lift-*.f64N/A
lower-fma.f6495.5
Applied rewrites95.5%
Taylor expanded in x around -inf
*-commutativeN/A
associate-*r*N/A
neg-mul-1N/A
distribute-lft-inN/A
mul-1-negN/A
remove-double-negN/A
lower-*.f64N/A
+-commutativeN/A
lower-+.f6499.5
Applied rewrites99.5%
if -72000 < x < 5Initial program 99.9%
lift-+.f64N/A
+-commutativeN/A
lift-*.f64N/A
lower-fma.f64100.0
lift-*.f64N/A
*-commutativeN/A
lower-*.f64100.0
lift-+.f64N/A
+-commutativeN/A
lower-+.f64100.0
Applied rewrites100.0%
lift-fma.f64N/A
lift-*.f64N/A
lift-*.f64N/A
*-commutativeN/A
lift-+.f64N/A
+-commutativeN/A
+-commutativeN/A
distribute-rgt-inN/A
lift-*.f64N/A
lift-*.f64N/A
associate-+r+N/A
lift-*.f64N/A
lift-*.f64N/A
distribute-lft-inN/A
+-commutativeN/A
metadata-evalN/A
sub-negN/A
lift--.f64N/A
*-commutativeN/A
lower-fma.f6499.9
lift-*.f64N/A
*-commutativeN/A
lower-*.f6499.9
Applied rewrites99.9%
Taylor expanded in x around 0
Applied rewrites97.7%
Final simplification98.6%
(FPCore (x y z) :precision binary64 (if (or (<= x -44.0) (not (<= x 6.5e-9))) (* (+ z y) x) (fma z 5.0 (* x z))))
double code(double x, double y, double z) {
double tmp;
if ((x <= -44.0) || !(x <= 6.5e-9)) {
tmp = (z + y) * x;
} else {
tmp = fma(z, 5.0, (x * z));
}
return tmp;
}
function code(x, y, z) tmp = 0.0 if ((x <= -44.0) || !(x <= 6.5e-9)) tmp = Float64(Float64(z + y) * x); else tmp = fma(z, 5.0, Float64(x * z)); end return tmp end
code[x_, y_, z_] := If[Or[LessEqual[x, -44.0], N[Not[LessEqual[x, 6.5e-9]], $MachinePrecision]], N[(N[(z + y), $MachinePrecision] * x), $MachinePrecision], N[(z * 5.0 + N[(x * z), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq -44 \lor \neg \left(x \leq 6.5 \cdot 10^{-9}\right):\\
\;\;\;\;\left(z + y\right) \cdot x\\
\mathbf{else}:\\
\;\;\;\;\mathsf{fma}\left(z, 5, x \cdot z\right)\\
\end{array}
\end{array}
if x < -44 or 6.5000000000000003e-9 < x Initial program 100.0%
lift-+.f64N/A
+-commutativeN/A
lift-*.f64N/A
lower-fma.f64100.0
lift-*.f64N/A
*-commutativeN/A
lower-*.f64100.0
lift-+.f64N/A
+-commutativeN/A
lower-+.f64100.0
Applied rewrites100.0%
lift-fma.f64N/A
lift-*.f64N/A
+-commutativeN/A
lift-*.f64N/A
*-commutativeN/A
lift-+.f64N/A
+-commutativeN/A
distribute-rgt-inN/A
lift-*.f64N/A
lift-*.f64N/A
associate-+l+N/A
lift-*.f64N/A
lift-*.f64N/A
lift-*.f64N/A
distribute-lft-inN/A
metadata-evalN/A
sub-negN/A
lift--.f64N/A
*-commutativeN/A
lift-*.f64N/A
lower-fma.f6495.7
Applied rewrites95.7%
Taylor expanded in x around -inf
*-commutativeN/A
associate-*r*N/A
neg-mul-1N/A
distribute-lft-inN/A
mul-1-negN/A
remove-double-negN/A
lower-*.f64N/A
+-commutativeN/A
lower-+.f6498.9
Applied rewrites98.9%
if -44 < x < 6.5000000000000003e-9Initial program 99.9%
Taylor expanded in y around 0
*-commutativeN/A
lower-*.f6475.6
Applied rewrites75.6%
lift-+.f64N/A
+-commutativeN/A
lift-*.f64N/A
lower-fma.f6475.7
Applied rewrites75.7%
Final simplification88.4%
(FPCore (x y z) :precision binary64 (if (or (<= x -44.0) (not (<= x 6.5e-9))) (* (+ z y) x) (* (- x -5.0) z)))
double code(double x, double y, double z) {
double tmp;
if ((x <= -44.0) || !(x <= 6.5e-9)) {
tmp = (z + y) * x;
} else {
tmp = (x - -5.0) * z;
}
return tmp;
}
real(8) function code(x, y, z)
real(8), intent (in) :: x
real(8), intent (in) :: y
real(8), intent (in) :: z
real(8) :: tmp
if ((x <= (-44.0d0)) .or. (.not. (x <= 6.5d-9))) then
tmp = (z + y) * x
else
tmp = (x - (-5.0d0)) * z
end if
code = tmp
end function
public static double code(double x, double y, double z) {
double tmp;
if ((x <= -44.0) || !(x <= 6.5e-9)) {
tmp = (z + y) * x;
} else {
tmp = (x - -5.0) * z;
}
return tmp;
}
def code(x, y, z): tmp = 0 if (x <= -44.0) or not (x <= 6.5e-9): tmp = (z + y) * x else: tmp = (x - -5.0) * z return tmp
function code(x, y, z) tmp = 0.0 if ((x <= -44.0) || !(x <= 6.5e-9)) tmp = Float64(Float64(z + y) * x); else tmp = Float64(Float64(x - -5.0) * z); end return tmp end
function tmp_2 = code(x, y, z) tmp = 0.0; if ((x <= -44.0) || ~((x <= 6.5e-9))) tmp = (z + y) * x; else tmp = (x - -5.0) * z; end tmp_2 = tmp; end
code[x_, y_, z_] := If[Or[LessEqual[x, -44.0], N[Not[LessEqual[x, 6.5e-9]], $MachinePrecision]], N[(N[(z + y), $MachinePrecision] * x), $MachinePrecision], N[(N[(x - -5.0), $MachinePrecision] * z), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq -44 \lor \neg \left(x \leq 6.5 \cdot 10^{-9}\right):\\
\;\;\;\;\left(z + y\right) \cdot x\\
\mathbf{else}:\\
\;\;\;\;\left(x - -5\right) \cdot z\\
\end{array}
\end{array}
if x < -44 or 6.5000000000000003e-9 < x Initial program 100.0%
lift-+.f64N/A
+-commutativeN/A
lift-*.f64N/A
lower-fma.f64100.0
lift-*.f64N/A
*-commutativeN/A
lower-*.f64100.0
lift-+.f64N/A
+-commutativeN/A
lower-+.f64100.0
Applied rewrites100.0%
lift-fma.f64N/A
lift-*.f64N/A
+-commutativeN/A
lift-*.f64N/A
*-commutativeN/A
lift-+.f64N/A
+-commutativeN/A
distribute-rgt-inN/A
lift-*.f64N/A
lift-*.f64N/A
associate-+l+N/A
lift-*.f64N/A
lift-*.f64N/A
lift-*.f64N/A
distribute-lft-inN/A
metadata-evalN/A
sub-negN/A
lift--.f64N/A
*-commutativeN/A
lift-*.f64N/A
lower-fma.f6495.7
Applied rewrites95.7%
Taylor expanded in x around -inf
*-commutativeN/A
associate-*r*N/A
neg-mul-1N/A
distribute-lft-inN/A
mul-1-negN/A
remove-double-negN/A
lower-*.f64N/A
+-commutativeN/A
lower-+.f6498.9
Applied rewrites98.9%
if -44 < x < 6.5000000000000003e-9Initial program 99.9%
Taylor expanded in y around 0
distribute-rgt-inN/A
*-commutativeN/A
lower-*.f64N/A
+-commutativeN/A
metadata-evalN/A
sub-negN/A
lower--.f6475.5
Applied rewrites75.5%
Final simplification88.3%
(FPCore (x y z) :precision binary64 (if (or (<= y -1.05e+126) (not (<= y 0.38))) (* y x) (* (- x -5.0) z)))
double code(double x, double y, double z) {
double tmp;
if ((y <= -1.05e+126) || !(y <= 0.38)) {
tmp = y * x;
} else {
tmp = (x - -5.0) * z;
}
return tmp;
}
real(8) function code(x, y, z)
real(8), intent (in) :: x
real(8), intent (in) :: y
real(8), intent (in) :: z
real(8) :: tmp
if ((y <= (-1.05d+126)) .or. (.not. (y <= 0.38d0))) then
tmp = y * x
else
tmp = (x - (-5.0d0)) * z
end if
code = tmp
end function
public static double code(double x, double y, double z) {
double tmp;
if ((y <= -1.05e+126) || !(y <= 0.38)) {
tmp = y * x;
} else {
tmp = (x - -5.0) * z;
}
return tmp;
}
def code(x, y, z): tmp = 0 if (y <= -1.05e+126) or not (y <= 0.38): tmp = y * x else: tmp = (x - -5.0) * z return tmp
function code(x, y, z) tmp = 0.0 if ((y <= -1.05e+126) || !(y <= 0.38)) tmp = Float64(y * x); else tmp = Float64(Float64(x - -5.0) * z); end return tmp end
function tmp_2 = code(x, y, z) tmp = 0.0; if ((y <= -1.05e+126) || ~((y <= 0.38))) tmp = y * x; else tmp = (x - -5.0) * z; end tmp_2 = tmp; end
code[x_, y_, z_] := If[Or[LessEqual[y, -1.05e+126], N[Not[LessEqual[y, 0.38]], $MachinePrecision]], N[(y * x), $MachinePrecision], N[(N[(x - -5.0), $MachinePrecision] * z), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;y \leq -1.05 \cdot 10^{+126} \lor \neg \left(y \leq 0.38\right):\\
\;\;\;\;y \cdot x\\
\mathbf{else}:\\
\;\;\;\;\left(x - -5\right) \cdot z\\
\end{array}
\end{array}
if y < -1.05e126 or 0.38 < y Initial program 100.0%
Taylor expanded in y around inf
*-commutativeN/A
lower-*.f6473.5
Applied rewrites73.5%
if -1.05e126 < y < 0.38Initial program 99.9%
Taylor expanded in y around 0
distribute-rgt-inN/A
*-commutativeN/A
lower-*.f64N/A
+-commutativeN/A
metadata-evalN/A
sub-negN/A
lower--.f6480.0
Applied rewrites80.0%
Final simplification77.2%
(FPCore (x y z) :precision binary64 (if (<= x 1e+18) (fma y x (* z (+ x 5.0))) (* (+ z y) x)))
double code(double x, double y, double z) {
double tmp;
if (x <= 1e+18) {
tmp = fma(y, x, (z * (x + 5.0)));
} else {
tmp = (z + y) * x;
}
return tmp;
}
function code(x, y, z) tmp = 0.0 if (x <= 1e+18) tmp = fma(y, x, Float64(z * Float64(x + 5.0))); else tmp = Float64(Float64(z + y) * x); end return tmp end
code[x_, y_, z_] := If[LessEqual[x, 1e+18], N[(y * x + N[(z * N[(x + 5.0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], N[(N[(z + y), $MachinePrecision] * x), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq 10^{+18}:\\
\;\;\;\;\mathsf{fma}\left(y, x, z \cdot \left(x + 5\right)\right)\\
\mathbf{else}:\\
\;\;\;\;\left(z + y\right) \cdot x\\
\end{array}
\end{array}
if x < 1e18Initial program 99.9%
lift-+.f64N/A
lift-*.f64N/A
lift-+.f64N/A
distribute-rgt-inN/A
associate-+l+N/A
lower-fma.f64N/A
lift-*.f64N/A
distribute-lft-outN/A
lower-*.f64N/A
lower-+.f6499.9
Applied rewrites99.9%
if 1e18 < x Initial program 100.0%
lift-+.f64N/A
+-commutativeN/A
lift-*.f64N/A
lower-fma.f64100.0
lift-*.f64N/A
*-commutativeN/A
lower-*.f64100.0
lift-+.f64N/A
+-commutativeN/A
lower-+.f64100.0
Applied rewrites100.0%
lift-fma.f64N/A
lift-*.f64N/A
+-commutativeN/A
lift-*.f64N/A
*-commutativeN/A
lift-+.f64N/A
+-commutativeN/A
distribute-rgt-inN/A
lift-*.f64N/A
lift-*.f64N/A
associate-+l+N/A
lift-*.f64N/A
lift-*.f64N/A
lift-*.f64N/A
distribute-lft-inN/A
metadata-evalN/A
sub-negN/A
lift--.f64N/A
*-commutativeN/A
lift-*.f64N/A
lower-fma.f6491.7
Applied rewrites91.7%
Taylor expanded in x around -inf
*-commutativeN/A
associate-*r*N/A
neg-mul-1N/A
distribute-lft-inN/A
mul-1-negN/A
remove-double-negN/A
lower-*.f64N/A
+-commutativeN/A
lower-+.f64100.0
Applied rewrites100.0%
(FPCore (x y z) :precision binary64 (if (or (<= x -0.46) (not (<= x 6.5e-12))) (* y x) (* 5.0 z)))
double code(double x, double y, double z) {
double tmp;
if ((x <= -0.46) || !(x <= 6.5e-12)) {
tmp = y * x;
} else {
tmp = 5.0 * z;
}
return tmp;
}
real(8) function code(x, y, z)
real(8), intent (in) :: x
real(8), intent (in) :: y
real(8), intent (in) :: z
real(8) :: tmp
if ((x <= (-0.46d0)) .or. (.not. (x <= 6.5d-12))) then
tmp = y * x
else
tmp = 5.0d0 * z
end if
code = tmp
end function
public static double code(double x, double y, double z) {
double tmp;
if ((x <= -0.46) || !(x <= 6.5e-12)) {
tmp = y * x;
} else {
tmp = 5.0 * z;
}
return tmp;
}
def code(x, y, z): tmp = 0 if (x <= -0.46) or not (x <= 6.5e-12): tmp = y * x else: tmp = 5.0 * z return tmp
function code(x, y, z) tmp = 0.0 if ((x <= -0.46) || !(x <= 6.5e-12)) tmp = Float64(y * x); else tmp = Float64(5.0 * z); end return tmp end
function tmp_2 = code(x, y, z) tmp = 0.0; if ((x <= -0.46) || ~((x <= 6.5e-12))) tmp = y * x; else tmp = 5.0 * z; end tmp_2 = tmp; end
code[x_, y_, z_] := If[Or[LessEqual[x, -0.46], N[Not[LessEqual[x, 6.5e-12]], $MachinePrecision]], N[(y * x), $MachinePrecision], N[(5.0 * z), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq -0.46 \lor \neg \left(x \leq 6.5 \cdot 10^{-12}\right):\\
\;\;\;\;y \cdot x\\
\mathbf{else}:\\
\;\;\;\;5 \cdot z\\
\end{array}
\end{array}
if x < -0.46000000000000002 or 6.5000000000000002e-12 < x Initial program 100.0%
Taylor expanded in y around inf
*-commutativeN/A
lower-*.f6458.6
Applied rewrites58.6%
if -0.46000000000000002 < x < 6.5000000000000002e-12Initial program 99.9%
Taylor expanded in x around 0
lower-*.f6474.0
Applied rewrites74.0%
Final simplification65.6%
(FPCore (x y z) :precision binary64 (* 5.0 z))
double code(double x, double y, double z) {
return 5.0 * z;
}
real(8) function code(x, y, z)
real(8), intent (in) :: x
real(8), intent (in) :: y
real(8), intent (in) :: z
code = 5.0d0 * z
end function
public static double code(double x, double y, double z) {
return 5.0 * z;
}
def code(x, y, z): return 5.0 * z
function code(x, y, z) return Float64(5.0 * z) end
function tmp = code(x, y, z) tmp = 5.0 * z; end
code[x_, y_, z_] := N[(5.0 * z), $MachinePrecision]
\begin{array}{l}
\\
5 \cdot z
\end{array}
Initial program 99.9%
Taylor expanded in x around 0
lower-*.f6435.3
Applied rewrites35.3%
(FPCore (x y z) :precision binary64 (+ (* (+ x 5.0) z) (* x y)))
double code(double x, double y, double z) {
return ((x + 5.0) * z) + (x * y);
}
real(8) function code(x, y, z)
real(8), intent (in) :: x
real(8), intent (in) :: y
real(8), intent (in) :: z
code = ((x + 5.0d0) * z) + (x * y)
end function
public static double code(double x, double y, double z) {
return ((x + 5.0) * z) + (x * y);
}
def code(x, y, z): return ((x + 5.0) * z) + (x * y)
function code(x, y, z) return Float64(Float64(Float64(x + 5.0) * z) + Float64(x * y)) end
function tmp = code(x, y, z) tmp = ((x + 5.0) * z) + (x * y); end
code[x_, y_, z_] := N[(N[(N[(x + 5.0), $MachinePrecision] * z), $MachinePrecision] + N[(x * y), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\left(x + 5\right) \cdot z + x \cdot y
\end{array}
herbie shell --seed 2024313
(FPCore (x y z)
:name "Graphics.Rendering.Plot.Render.Plot.Legend:renderLegendOutside from plot-0.2.3.4, C"
:precision binary64
:alt
(! :herbie-platform default (+ (* (+ x 5) z) (* x y)))
(+ (* x (+ y z)) (* z 5.0)))