
(FPCore (x y z) :precision binary64 (+ (+ (+ (+ (+ x y) y) x) z) x))
double code(double x, double y, double z) {
return ((((x + y) + y) + x) + z) + x;
}
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) + y) + x) + z) + x
end function
public static double code(double x, double y, double z) {
return ((((x + y) + y) + x) + z) + x;
}
def code(x, y, z): return ((((x + y) + y) + x) + z) + x
function code(x, y, z) return Float64(Float64(Float64(Float64(Float64(x + y) + y) + x) + z) + x) end
function tmp = code(x, y, z) tmp = ((((x + y) + y) + x) + z) + x; end
code[x_, y_, z_] := N[(N[(N[(N[(N[(x + y), $MachinePrecision] + y), $MachinePrecision] + x), $MachinePrecision] + z), $MachinePrecision] + x), $MachinePrecision]
\begin{array}{l}
\\
\left(\left(\left(\left(x + y\right) + y\right) + x\right) + z\right) + x
\end{array}
Sampling outcomes in binary64 precision:
Herbie found 7 alternatives:
| Alternative | Accuracy | Speedup |
|---|
(FPCore (x y z) :precision binary64 (+ (+ (+ (+ (+ x y) y) x) z) x))
double code(double x, double y, double z) {
return ((((x + y) + y) + x) + z) + x;
}
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) + y) + x) + z) + x
end function
public static double code(double x, double y, double z) {
return ((((x + y) + y) + x) + z) + x;
}
def code(x, y, z): return ((((x + y) + y) + x) + z) + x
function code(x, y, z) return Float64(Float64(Float64(Float64(Float64(x + y) + y) + x) + z) + x) end
function tmp = code(x, y, z) tmp = ((((x + y) + y) + x) + z) + x; end
code[x_, y_, z_] := N[(N[(N[(N[(N[(x + y), $MachinePrecision] + y), $MachinePrecision] + x), $MachinePrecision] + z), $MachinePrecision] + x), $MachinePrecision]
\begin{array}{l}
\\
\left(\left(\left(\left(x + y\right) + y\right) + x\right) + z\right) + x
\end{array}
(FPCore (x y z) :precision binary64 (fma 3.0 x (fma 2.0 y z)))
double code(double x, double y, double z) {
return fma(3.0, x, fma(2.0, y, z));
}
function code(x, y, z) return fma(3.0, x, fma(2.0, y, z)) end
code[x_, y_, z_] := N[(3.0 * x + N[(2.0 * y + z), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\mathsf{fma}\left(3, x, \mathsf{fma}\left(2, y, z\right)\right)
\end{array}
Initial program 99.9%
Taylor expanded in x around 0
associate-+r+N/A
+-commutativeN/A
*-rgt-identityN/A
*-inversesN/A
associate-/l*N/A
associate-*l/N/A
div-addN/A
associate-*r/N/A
+-commutativeN/A
*-commutativeN/A
cancel-sign-sub-invN/A
sub-negN/A
mul-1-negN/A
distribute-rgt-neg-outN/A
+-commutativeN/A
associate-*r/N/A
div-addN/A
mul-1-negN/A
Applied rewrites100.0%
(FPCore (x y z) :precision binary64 (if (<= z -1.55e-35) (fma 3.0 x z) (if (<= z 1.35e-11) (fma 3.0 x (+ y y)) (+ (fma 2.0 y z) x))))
double code(double x, double y, double z) {
double tmp;
if (z <= -1.55e-35) {
tmp = fma(3.0, x, z);
} else if (z <= 1.35e-11) {
tmp = fma(3.0, x, (y + y));
} else {
tmp = fma(2.0, y, z) + x;
}
return tmp;
}
function code(x, y, z) tmp = 0.0 if (z <= -1.55e-35) tmp = fma(3.0, x, z); elseif (z <= 1.35e-11) tmp = fma(3.0, x, Float64(y + y)); else tmp = Float64(fma(2.0, y, z) + x); end return tmp end
code[x_, y_, z_] := If[LessEqual[z, -1.55e-35], N[(3.0 * x + z), $MachinePrecision], If[LessEqual[z, 1.35e-11], N[(3.0 * x + N[(y + y), $MachinePrecision]), $MachinePrecision], N[(N[(2.0 * y + z), $MachinePrecision] + x), $MachinePrecision]]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;z \leq -1.55 \cdot 10^{-35}:\\
\;\;\;\;\mathsf{fma}\left(3, x, z\right)\\
\mathbf{elif}\;z \leq 1.35 \cdot 10^{-11}:\\
\;\;\;\;\mathsf{fma}\left(3, x, y + y\right)\\
\mathbf{else}:\\
\;\;\;\;\mathsf{fma}\left(2, y, z\right) + x\\
\end{array}
\end{array}
if z < -1.55000000000000006e-35Initial program 99.9%
Taylor expanded in y around 0
+-commutativeN/A
associate-+r+N/A
distribute-rgt1-inN/A
metadata-evalN/A
lower-fma.f6482.5
Applied rewrites82.5%
if -1.55000000000000006e-35 < z < 1.35000000000000002e-11Initial program 99.9%
Taylor expanded in z around 0
+-commutativeN/A
distribute-lft-outN/A
*-commutativeN/A
lower-fma.f64N/A
+-commutativeN/A
lower-+.f6492.2
Applied rewrites92.2%
Taylor expanded in x around 0
Applied rewrites92.3%
Applied rewrites92.3%
if 1.35000000000000002e-11 < z Initial program 100.0%
Taylor expanded in x around 0
+-commutativeN/A
lower-fma.f6483.5
Applied rewrites83.5%
(FPCore (x y z) :precision binary64 (if (<= z -1.55e-35) (fma 3.0 x z) (if (<= z 1.35e-11) (fma 3.0 x (+ y y)) (fma 2.0 y z))))
double code(double x, double y, double z) {
double tmp;
if (z <= -1.55e-35) {
tmp = fma(3.0, x, z);
} else if (z <= 1.35e-11) {
tmp = fma(3.0, x, (y + y));
} else {
tmp = fma(2.0, y, z);
}
return tmp;
}
function code(x, y, z) tmp = 0.0 if (z <= -1.55e-35) tmp = fma(3.0, x, z); elseif (z <= 1.35e-11) tmp = fma(3.0, x, Float64(y + y)); else tmp = fma(2.0, y, z); end return tmp end
code[x_, y_, z_] := If[LessEqual[z, -1.55e-35], N[(3.0 * x + z), $MachinePrecision], If[LessEqual[z, 1.35e-11], N[(3.0 * x + N[(y + y), $MachinePrecision]), $MachinePrecision], N[(2.0 * y + z), $MachinePrecision]]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;z \leq -1.55 \cdot 10^{-35}:\\
\;\;\;\;\mathsf{fma}\left(3, x, z\right)\\
\mathbf{elif}\;z \leq 1.35 \cdot 10^{-11}:\\
\;\;\;\;\mathsf{fma}\left(3, x, y + y\right)\\
\mathbf{else}:\\
\;\;\;\;\mathsf{fma}\left(2, y, z\right)\\
\end{array}
\end{array}
if z < -1.55000000000000006e-35Initial program 99.9%
Taylor expanded in y around 0
+-commutativeN/A
associate-+r+N/A
distribute-rgt1-inN/A
metadata-evalN/A
lower-fma.f6482.5
Applied rewrites82.5%
if -1.55000000000000006e-35 < z < 1.35000000000000002e-11Initial program 99.9%
Taylor expanded in z around 0
+-commutativeN/A
distribute-lft-outN/A
*-commutativeN/A
lower-fma.f64N/A
+-commutativeN/A
lower-+.f6492.2
Applied rewrites92.2%
Taylor expanded in x around 0
Applied rewrites92.3%
Applied rewrites92.3%
if 1.35000000000000002e-11 < z Initial program 100.0%
Taylor expanded in x around 0
associate-+r+N/A
+-commutativeN/A
*-rgt-identityN/A
*-inversesN/A
associate-/l*N/A
associate-*l/N/A
div-addN/A
associate-*r/N/A
+-commutativeN/A
*-commutativeN/A
cancel-sign-sub-invN/A
sub-negN/A
mul-1-negN/A
distribute-rgt-neg-outN/A
+-commutativeN/A
associate-*r/N/A
div-addN/A
mul-1-negN/A
Applied rewrites100.0%
Taylor expanded in x around 0
+-commutativeN/A
lower-fma.f6481.1
Applied rewrites81.1%
(FPCore (x y z) :precision binary64 (if (<= y -3.2e+25) (fma 2.0 y z) (if (<= y 8.5e-47) (fma 3.0 x z) (fma 2.0 y z))))
double code(double x, double y, double z) {
double tmp;
if (y <= -3.2e+25) {
tmp = fma(2.0, y, z);
} else if (y <= 8.5e-47) {
tmp = fma(3.0, x, z);
} else {
tmp = fma(2.0, y, z);
}
return tmp;
}
function code(x, y, z) tmp = 0.0 if (y <= -3.2e+25) tmp = fma(2.0, y, z); elseif (y <= 8.5e-47) tmp = fma(3.0, x, z); else tmp = fma(2.0, y, z); end return tmp end
code[x_, y_, z_] := If[LessEqual[y, -3.2e+25], N[(2.0 * y + z), $MachinePrecision], If[LessEqual[y, 8.5e-47], N[(3.0 * x + z), $MachinePrecision], N[(2.0 * y + z), $MachinePrecision]]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;y \leq -3.2 \cdot 10^{+25}:\\
\;\;\;\;\mathsf{fma}\left(2, y, z\right)\\
\mathbf{elif}\;y \leq 8.5 \cdot 10^{-47}:\\
\;\;\;\;\mathsf{fma}\left(3, x, z\right)\\
\mathbf{else}:\\
\;\;\;\;\mathsf{fma}\left(2, y, z\right)\\
\end{array}
\end{array}
if y < -3.1999999999999999e25 or 8.4999999999999999e-47 < y Initial program 99.9%
Taylor expanded in x around 0
associate-+r+N/A
+-commutativeN/A
*-rgt-identityN/A
*-inversesN/A
associate-/l*N/A
associate-*l/N/A
div-addN/A
associate-*r/N/A
+-commutativeN/A
*-commutativeN/A
cancel-sign-sub-invN/A
sub-negN/A
mul-1-negN/A
distribute-rgt-neg-outN/A
+-commutativeN/A
associate-*r/N/A
div-addN/A
mul-1-negN/A
Applied rewrites100.0%
Taylor expanded in x around 0
+-commutativeN/A
lower-fma.f6480.1
Applied rewrites80.1%
if -3.1999999999999999e25 < y < 8.4999999999999999e-47Initial program 99.9%
Taylor expanded in y around 0
+-commutativeN/A
associate-+r+N/A
distribute-rgt1-inN/A
metadata-evalN/A
lower-fma.f6492.2
Applied rewrites92.2%
(FPCore (x y z) :precision binary64 (if (<= x -8.6e+201) (* 3.0 x) (if (<= x 9e+47) (fma 2.0 y z) (* 3.0 x))))
double code(double x, double y, double z) {
double tmp;
if (x <= -8.6e+201) {
tmp = 3.0 * x;
} else if (x <= 9e+47) {
tmp = fma(2.0, y, z);
} else {
tmp = 3.0 * x;
}
return tmp;
}
function code(x, y, z) tmp = 0.0 if (x <= -8.6e+201) tmp = Float64(3.0 * x); elseif (x <= 9e+47) tmp = fma(2.0, y, z); else tmp = Float64(3.0 * x); end return tmp end
code[x_, y_, z_] := If[LessEqual[x, -8.6e+201], N[(3.0 * x), $MachinePrecision], If[LessEqual[x, 9e+47], N[(2.0 * y + z), $MachinePrecision], N[(3.0 * x), $MachinePrecision]]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq -8.6 \cdot 10^{+201}:\\
\;\;\;\;3 \cdot x\\
\mathbf{elif}\;x \leq 9 \cdot 10^{+47}:\\
\;\;\;\;\mathsf{fma}\left(2, y, z\right)\\
\mathbf{else}:\\
\;\;\;\;3 \cdot x\\
\end{array}
\end{array}
if x < -8.59999999999999981e201 or 8.99999999999999958e47 < x Initial program 99.7%
Taylor expanded in x around inf
lower-*.f6476.1
Applied rewrites76.1%
if -8.59999999999999981e201 < x < 8.99999999999999958e47Initial program 99.9%
Taylor expanded in x around 0
associate-+r+N/A
+-commutativeN/A
*-rgt-identityN/A
*-inversesN/A
associate-/l*N/A
associate-*l/N/A
div-addN/A
associate-*r/N/A
+-commutativeN/A
*-commutativeN/A
cancel-sign-sub-invN/A
sub-negN/A
mul-1-negN/A
distribute-rgt-neg-outN/A
+-commutativeN/A
associate-*r/N/A
div-addN/A
mul-1-negN/A
Applied rewrites100.0%
Taylor expanded in x around 0
+-commutativeN/A
lower-fma.f6481.1
Applied rewrites81.1%
(FPCore (x y z) :precision binary64 (if (<= y -3.2e+25) (+ y y) (if (<= y 1.22e-19) (* 3.0 x) (+ y y))))
double code(double x, double y, double z) {
double tmp;
if (y <= -3.2e+25) {
tmp = y + y;
} else if (y <= 1.22e-19) {
tmp = 3.0 * x;
} else {
tmp = y + y;
}
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 <= (-3.2d+25)) then
tmp = y + y
else if (y <= 1.22d-19) then
tmp = 3.0d0 * x
else
tmp = y + y
end if
code = tmp
end function
public static double code(double x, double y, double z) {
double tmp;
if (y <= -3.2e+25) {
tmp = y + y;
} else if (y <= 1.22e-19) {
tmp = 3.0 * x;
} else {
tmp = y + y;
}
return tmp;
}
def code(x, y, z): tmp = 0 if y <= -3.2e+25: tmp = y + y elif y <= 1.22e-19: tmp = 3.0 * x else: tmp = y + y return tmp
function code(x, y, z) tmp = 0.0 if (y <= -3.2e+25) tmp = Float64(y + y); elseif (y <= 1.22e-19) tmp = Float64(3.0 * x); else tmp = Float64(y + y); end return tmp end
function tmp_2 = code(x, y, z) tmp = 0.0; if (y <= -3.2e+25) tmp = y + y; elseif (y <= 1.22e-19) tmp = 3.0 * x; else tmp = y + y; end tmp_2 = tmp; end
code[x_, y_, z_] := If[LessEqual[y, -3.2e+25], N[(y + y), $MachinePrecision], If[LessEqual[y, 1.22e-19], N[(3.0 * x), $MachinePrecision], N[(y + y), $MachinePrecision]]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;y \leq -3.2 \cdot 10^{+25}:\\
\;\;\;\;y + y\\
\mathbf{elif}\;y \leq 1.22 \cdot 10^{-19}:\\
\;\;\;\;3 \cdot x\\
\mathbf{else}:\\
\;\;\;\;y + y\\
\end{array}
\end{array}
if y < -3.1999999999999999e25 or 1.2200000000000001e-19 < y Initial program 99.9%
Taylor expanded in z around 0
+-commutativeN/A
distribute-lft-outN/A
*-commutativeN/A
lower-fma.f64N/A
+-commutativeN/A
lower-+.f6479.0
Applied rewrites79.0%
Taylor expanded in x around 0
Applied rewrites59.1%
Applied rewrites59.1%
if -3.1999999999999999e25 < y < 1.2200000000000001e-19Initial program 99.9%
Taylor expanded in x around inf
lower-*.f6449.6
Applied rewrites49.6%
(FPCore (x y z) :precision binary64 (+ y y))
double code(double x, double y, double z) {
return y + y;
}
real(8) function code(x, y, z)
real(8), intent (in) :: x
real(8), intent (in) :: y
real(8), intent (in) :: z
code = y + y
end function
public static double code(double x, double y, double z) {
return y + y;
}
def code(x, y, z): return y + y
function code(x, y, z) return Float64(y + y) end
function tmp = code(x, y, z) tmp = y + y; end
code[x_, y_, z_] := N[(y + y), $MachinePrecision]
\begin{array}{l}
\\
y + y
\end{array}
Initial program 99.9%
Taylor expanded in z around 0
+-commutativeN/A
distribute-lft-outN/A
*-commutativeN/A
lower-fma.f64N/A
+-commutativeN/A
lower-+.f6467.9
Applied rewrites67.9%
Taylor expanded in x around 0
Applied rewrites33.8%
Applied rewrites33.8%
herbie shell --seed 2024298
(FPCore (x y z)
:name "Graphics.Rendering.Plot.Render.Plot.Legend:renderLegendInside from plot-0.2.3.4"
:precision binary64
(+ (+ (+ (+ (+ x y) y) x) z) x))