
(FPCore (x y z) :precision binary64 (+ x (* y (+ z x))))
double code(double x, double y, double z) {
return x + (y * (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 * (z + x))
end function
public static double code(double x, double y, double z) {
return x + (y * (z + x));
}
def code(x, y, z): return x + (y * (z + x))
function code(x, y, z) return Float64(x + Float64(y * Float64(z + x))) end
function tmp = code(x, y, z) tmp = x + (y * (z + x)); end
code[x_, y_, z_] := N[(x + N[(y * N[(z + x), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
x + y \cdot \left(z + x\right)
\end{array}
Sampling outcomes in binary64 precision:
Herbie found 5 alternatives:
| Alternative | Accuracy | Speedup |
|---|
(FPCore (x y z) :precision binary64 (+ x (* y (+ z x))))
double code(double x, double y, double z) {
return x + (y * (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 * (z + x))
end function
public static double code(double x, double y, double z) {
return x + (y * (z + x));
}
def code(x, y, z): return x + (y * (z + x))
function code(x, y, z) return Float64(x + Float64(y * Float64(z + x))) end
function tmp = code(x, y, z) tmp = x + (y * (z + x)); end
code[x_, y_, z_] := N[(x + N[(y * N[(z + x), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
x + y \cdot \left(z + x\right)
\end{array}
(FPCore (x y z) :precision binary64 (fma (+ z x) y x))
double code(double x, double y, double z) {
return fma((z + x), y, x);
}
function code(x, y, z) return fma(Float64(z + x), y, x) end
code[x_, y_, z_] := N[(N[(z + x), $MachinePrecision] * y + x), $MachinePrecision]
\begin{array}{l}
\\
\mathsf{fma}\left(z + x, y, x\right)
\end{array}
Initial program 100.0%
lift-+.f64N/A
+-commutativeN/A
lift-*.f64N/A
*-commutativeN/A
lower-fma.f64100.0
Applied rewrites100.0%
(FPCore (x y z)
:precision binary64
(let* ((t_0 (* (+ z x) y)))
(if (<= y -1.4)
t_0
(if (<= y -3.6e-40)
(fma y x x)
(if (<= y -1.85e-51)
(* z y)
(if (<= y 9e-115)
(fma y x x)
(if (<= y 1.02e-81)
(* z y)
(if (<= y 7.4e-56)
(fma y x x)
(if (<= y 1.9e+264) t_0 (* y x))))))))))
double code(double x, double y, double z) {
double t_0 = (z + x) * y;
double tmp;
if (y <= -1.4) {
tmp = t_0;
} else if (y <= -3.6e-40) {
tmp = fma(y, x, x);
} else if (y <= -1.85e-51) {
tmp = z * y;
} else if (y <= 9e-115) {
tmp = fma(y, x, x);
} else if (y <= 1.02e-81) {
tmp = z * y;
} else if (y <= 7.4e-56) {
tmp = fma(y, x, x);
} else if (y <= 1.9e+264) {
tmp = t_0;
} else {
tmp = y * x;
}
return tmp;
}
function code(x, y, z) t_0 = Float64(Float64(z + x) * y) tmp = 0.0 if (y <= -1.4) tmp = t_0; elseif (y <= -3.6e-40) tmp = fma(y, x, x); elseif (y <= -1.85e-51) tmp = Float64(z * y); elseif (y <= 9e-115) tmp = fma(y, x, x); elseif (y <= 1.02e-81) tmp = Float64(z * y); elseif (y <= 7.4e-56) tmp = fma(y, x, x); elseif (y <= 1.9e+264) tmp = t_0; else tmp = Float64(y * x); end return tmp end
code[x_, y_, z_] := Block[{t$95$0 = N[(N[(z + x), $MachinePrecision] * y), $MachinePrecision]}, If[LessEqual[y, -1.4], t$95$0, If[LessEqual[y, -3.6e-40], N[(y * x + x), $MachinePrecision], If[LessEqual[y, -1.85e-51], N[(z * y), $MachinePrecision], If[LessEqual[y, 9e-115], N[(y * x + x), $MachinePrecision], If[LessEqual[y, 1.02e-81], N[(z * y), $MachinePrecision], If[LessEqual[y, 7.4e-56], N[(y * x + x), $MachinePrecision], If[LessEqual[y, 1.9e+264], t$95$0, N[(y * x), $MachinePrecision]]]]]]]]]
\begin{array}{l}
\\
\begin{array}{l}
t_0 := \left(z + x\right) \cdot y\\
\mathbf{if}\;y \leq -1.4:\\
\;\;\;\;t\_0\\
\mathbf{elif}\;y \leq -3.6 \cdot 10^{-40}:\\
\;\;\;\;\mathsf{fma}\left(y, x, x\right)\\
\mathbf{elif}\;y \leq -1.85 \cdot 10^{-51}:\\
\;\;\;\;z \cdot y\\
\mathbf{elif}\;y \leq 9 \cdot 10^{-115}:\\
\;\;\;\;\mathsf{fma}\left(y, x, x\right)\\
\mathbf{elif}\;y \leq 1.02 \cdot 10^{-81}:\\
\;\;\;\;z \cdot y\\
\mathbf{elif}\;y \leq 7.4 \cdot 10^{-56}:\\
\;\;\;\;\mathsf{fma}\left(y, x, x\right)\\
\mathbf{elif}\;y \leq 1.9 \cdot 10^{+264}:\\
\;\;\;\;t\_0\\
\mathbf{else}:\\
\;\;\;\;y \cdot x\\
\end{array}
\end{array}
if y < -1.3999999999999999 or 7.4000000000000004e-56 < y < 1.9000000000000001e264Initial program 100.0%
lift-+.f64N/A
lift-*.f64N/A
lift-+.f64N/A
distribute-rgt-inN/A
associate-+r+N/A
+-commutativeN/A
*-commutativeN/A
lower-fma.f64N/A
+-commutativeN/A
lower-fma.f6498.4
Applied rewrites98.4%
Taylor expanded in y around -inf
distribute-lft-inN/A
associate-*r*N/A
mul-1-negN/A
distribute-rgt-neg-outN/A
distribute-lft-neg-inN/A
mul-1-negN/A
remove-double-negN/A
*-commutativeN/A
lower-*.f64N/A
+-commutativeN/A
lower-+.f6497.9
Applied rewrites97.9%
if -1.3999999999999999 < y < -3.6e-40 or -1.84999999999999987e-51 < y < 9.00000000000000046e-115 or 1.01999999999999998e-81 < y < 7.4000000000000004e-56Initial program 100.0%
Taylor expanded in x around inf
*-commutativeN/A
+-commutativeN/A
distribute-lft1-inN/A
lower-fma.f6481.1
Applied rewrites81.1%
if -3.6e-40 < y < -1.84999999999999987e-51 or 9.00000000000000046e-115 < y < 1.01999999999999998e-81Initial program 100.0%
Taylor expanded in x around 0
*-commutativeN/A
lower-*.f6475.4
Applied rewrites75.4%
if 1.9000000000000001e264 < y Initial program 100.0%
Taylor expanded in x around inf
*-commutativeN/A
+-commutativeN/A
distribute-lft1-inN/A
lower-fma.f6450.8
Applied rewrites50.8%
Taylor expanded in y around inf
Applied rewrites50.8%
Final simplification88.0%
(FPCore (x y z)
:precision binary64
(if (or (<= z -9.8e+72)
(not
(or (<= z -8.6e-223)
(not
(or (<= z -1.3e-236)
(not (or (<= z 4.6e-186) (not (<= z 6.4e+307)))))))))
(* z y)
(* y x)))
double code(double x, double y, double z) {
double tmp;
if ((z <= -9.8e+72) || !((z <= -8.6e-223) || !((z <= -1.3e-236) || !((z <= 4.6e-186) || !(z <= 6.4e+307))))) {
tmp = z * y;
} else {
tmp = y * x;
}
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 ((z <= (-9.8d+72)) .or. (.not. (z <= (-8.6d-223)) .or. (.not. (z <= (-1.3d-236)) .or. (.not. (z <= 4.6d-186) .or. (.not. (z <= 6.4d+307)))))) then
tmp = z * y
else
tmp = y * x
end if
code = tmp
end function
public static double code(double x, double y, double z) {
double tmp;
if ((z <= -9.8e+72) || !((z <= -8.6e-223) || !((z <= -1.3e-236) || !((z <= 4.6e-186) || !(z <= 6.4e+307))))) {
tmp = z * y;
} else {
tmp = y * x;
}
return tmp;
}
def code(x, y, z): tmp = 0 if (z <= -9.8e+72) or not ((z <= -8.6e-223) or not ((z <= -1.3e-236) or not ((z <= 4.6e-186) or not (z <= 6.4e+307)))): tmp = z * y else: tmp = y * x return tmp
function code(x, y, z) tmp = 0.0 if ((z <= -9.8e+72) || !((z <= -8.6e-223) || !((z <= -1.3e-236) || !((z <= 4.6e-186) || !(z <= 6.4e+307))))) tmp = Float64(z * y); else tmp = Float64(y * x); end return tmp end
function tmp_2 = code(x, y, z) tmp = 0.0; if ((z <= -9.8e+72) || ~(((z <= -8.6e-223) || ~(((z <= -1.3e-236) || ~(((z <= 4.6e-186) || ~((z <= 6.4e+307))))))))) tmp = z * y; else tmp = y * x; end tmp_2 = tmp; end
code[x_, y_, z_] := If[Or[LessEqual[z, -9.8e+72], N[Not[Or[LessEqual[z, -8.6e-223], N[Not[Or[LessEqual[z, -1.3e-236], N[Not[Or[LessEqual[z, 4.6e-186], N[Not[LessEqual[z, 6.4e+307]], $MachinePrecision]]], $MachinePrecision]]], $MachinePrecision]]], $MachinePrecision]], N[(z * y), $MachinePrecision], N[(y * x), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;z \leq -9.8 \cdot 10^{+72} \lor \neg \left(z \leq -8.6 \cdot 10^{-223} \lor \neg \left(z \leq -1.3 \cdot 10^{-236} \lor \neg \left(z \leq 4.6 \cdot 10^{-186} \lor \neg \left(z \leq 6.4 \cdot 10^{+307}\right)\right)\right)\right):\\
\;\;\;\;z \cdot y\\
\mathbf{else}:\\
\;\;\;\;y \cdot x\\
\end{array}
\end{array}
if z < -9.80000000000000012e72 or -8.5999999999999998e-223 < z < -1.3e-236 or 4.6000000000000002e-186 < z < 6.40000000000000011e307Initial program 100.0%
Taylor expanded in x around 0
*-commutativeN/A
lower-*.f6460.3
Applied rewrites60.3%
if -9.80000000000000012e72 < z < -8.5999999999999998e-223 or -1.3e-236 < z < 4.6000000000000002e-186 or 6.40000000000000011e307 < z Initial program 100.0%
Taylor expanded in x around inf
*-commutativeN/A
+-commutativeN/A
distribute-lft1-inN/A
lower-fma.f6491.6
Applied rewrites91.6%
Taylor expanded in y around inf
Applied rewrites42.4%
Final simplification53.3%
(FPCore (x y z)
:precision binary64
(if (<= z -5.1e+82)
(* z y)
(if (<= z 3800000.0)
(fma y x x)
(if (<= z 1.1e+62)
(* z y)
(if (<= z 3.5e+103) (fma y x x) (if (<= z 7e+290) (* z y) (* y x)))))))
double code(double x, double y, double z) {
double tmp;
if (z <= -5.1e+82) {
tmp = z * y;
} else if (z <= 3800000.0) {
tmp = fma(y, x, x);
} else if (z <= 1.1e+62) {
tmp = z * y;
} else if (z <= 3.5e+103) {
tmp = fma(y, x, x);
} else if (z <= 7e+290) {
tmp = z * y;
} else {
tmp = y * x;
}
return tmp;
}
function code(x, y, z) tmp = 0.0 if (z <= -5.1e+82) tmp = Float64(z * y); elseif (z <= 3800000.0) tmp = fma(y, x, x); elseif (z <= 1.1e+62) tmp = Float64(z * y); elseif (z <= 3.5e+103) tmp = fma(y, x, x); elseif (z <= 7e+290) tmp = Float64(z * y); else tmp = Float64(y * x); end return tmp end
code[x_, y_, z_] := If[LessEqual[z, -5.1e+82], N[(z * y), $MachinePrecision], If[LessEqual[z, 3800000.0], N[(y * x + x), $MachinePrecision], If[LessEqual[z, 1.1e+62], N[(z * y), $MachinePrecision], If[LessEqual[z, 3.5e+103], N[(y * x + x), $MachinePrecision], If[LessEqual[z, 7e+290], N[(z * y), $MachinePrecision], N[(y * x), $MachinePrecision]]]]]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;z \leq -5.1 \cdot 10^{+82}:\\
\;\;\;\;z \cdot y\\
\mathbf{elif}\;z \leq 3800000:\\
\;\;\;\;\mathsf{fma}\left(y, x, x\right)\\
\mathbf{elif}\;z \leq 1.1 \cdot 10^{+62}:\\
\;\;\;\;z \cdot y\\
\mathbf{elif}\;z \leq 3.5 \cdot 10^{+103}:\\
\;\;\;\;\mathsf{fma}\left(y, x, x\right)\\
\mathbf{elif}\;z \leq 7 \cdot 10^{+290}:\\
\;\;\;\;z \cdot y\\
\mathbf{else}:\\
\;\;\;\;y \cdot x\\
\end{array}
\end{array}
if z < -5.1000000000000003e82 or 3.8e6 < z < 1.10000000000000007e62 or 3.5e103 < z < 7.00000000000000026e290Initial program 100.0%
Taylor expanded in x around 0
*-commutativeN/A
lower-*.f6474.2
Applied rewrites74.2%
if -5.1000000000000003e82 < z < 3.8e6 or 1.10000000000000007e62 < z < 3.5e103Initial program 100.0%
Taylor expanded in x around inf
*-commutativeN/A
+-commutativeN/A
distribute-lft1-inN/A
lower-fma.f6485.9
Applied rewrites85.9%
if 7.00000000000000026e290 < z Initial program 100.0%
Taylor expanded in x around inf
*-commutativeN/A
+-commutativeN/A
distribute-lft1-inN/A
lower-fma.f6425.8
Applied rewrites25.8%
Taylor expanded in y around inf
Applied rewrites1.2%
Final simplification79.8%
(FPCore (x y z) :precision binary64 (* y x))
double code(double x, double y, double z) {
return y * x;
}
real(8) function code(x, y, z)
real(8), intent (in) :: x
real(8), intent (in) :: y
real(8), intent (in) :: z
code = y * x
end function
public static double code(double x, double y, double z) {
return y * x;
}
def code(x, y, z): return y * x
function code(x, y, z) return Float64(y * x) end
function tmp = code(x, y, z) tmp = y * x; end
code[x_, y_, z_] := N[(y * x), $MachinePrecision]
\begin{array}{l}
\\
y \cdot x
\end{array}
Initial program 100.0%
Taylor expanded in x around inf
*-commutativeN/A
+-commutativeN/A
distribute-lft1-inN/A
lower-fma.f6462.2
Applied rewrites62.2%
Taylor expanded in y around inf
Applied rewrites25.0%
Final simplification25.0%
herbie shell --seed 2024338
(FPCore (x y z)
:name "Main:bigenough2 from A"
:precision binary64
(+ x (* y (+ z x))))