
(FPCore (x y z) :precision binary64 (+ (+ (+ (* x y) (* z z)) (* z z)) (* z z)))
double code(double x, double y, double z) {
return (((x * y) + (z * z)) + (z * z)) + (z * 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) + (z * z)) + (z * z)) + (z * z)
end function
public static double code(double x, double y, double z) {
return (((x * y) + (z * z)) + (z * z)) + (z * z);
}
def code(x, y, z): return (((x * y) + (z * z)) + (z * z)) + (z * z)
function code(x, y, z) return Float64(Float64(Float64(Float64(x * y) + Float64(z * z)) + Float64(z * z)) + Float64(z * z)) end
function tmp = code(x, y, z) tmp = (((x * y) + (z * z)) + (z * z)) + (z * z); end
code[x_, y_, z_] := N[(N[(N[(N[(x * y), $MachinePrecision] + N[(z * z), $MachinePrecision]), $MachinePrecision] + N[(z * z), $MachinePrecision]), $MachinePrecision] + N[(z * z), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\left(\left(x \cdot y + z \cdot z\right) + z \cdot z\right) + z \cdot z
\end{array}
Sampling outcomes in binary64 precision:
Herbie found 6 alternatives:
| Alternative | Accuracy | Speedup |
|---|
(FPCore (x y z) :precision binary64 (+ (+ (+ (* x y) (* z z)) (* z z)) (* z z)))
double code(double x, double y, double z) {
return (((x * y) + (z * z)) + (z * z)) + (z * 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) + (z * z)) + (z * z)) + (z * z)
end function
public static double code(double x, double y, double z) {
return (((x * y) + (z * z)) + (z * z)) + (z * z);
}
def code(x, y, z): return (((x * y) + (z * z)) + (z * z)) + (z * z)
function code(x, y, z) return Float64(Float64(Float64(Float64(x * y) + Float64(z * z)) + Float64(z * z)) + Float64(z * z)) end
function tmp = code(x, y, z) tmp = (((x * y) + (z * z)) + (z * z)) + (z * z); end
code[x_, y_, z_] := N[(N[(N[(N[(x * y), $MachinePrecision] + N[(z * z), $MachinePrecision]), $MachinePrecision] + N[(z * z), $MachinePrecision]), $MachinePrecision] + N[(z * z), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\left(\left(x \cdot y + z \cdot z\right) + z \cdot z\right) + z \cdot z
\end{array}
(FPCore (x y z) :precision binary64 (fma y x (* 3.0 (* z z))))
double code(double x, double y, double z) {
return fma(y, x, (3.0 * (z * z)));
}
function code(x, y, z) return fma(y, x, Float64(3.0 * Float64(z * z))) end
code[x_, y_, z_] := N[(y * x + N[(3.0 * N[(z * z), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\mathsf{fma}\left(y, x, 3 \cdot \left(z \cdot z\right)\right)
\end{array}
Initial program 98.7%
Taylor expanded in x around 0
+-commutativeN/A
*-commutativeN/A
lower-fma.f64N/A
*-commutativeN/A
lower-*.f64N/A
unpow2N/A
lower-*.f6499.5
Applied rewrites99.5%
Taylor expanded in x around 0
associate-+r+N/A
+-commutativeN/A
associate-+r+N/A
distribute-lft1-inN/A
metadata-evalN/A
*-commutativeN/A
lower-fma.f64N/A
lower-*.f64N/A
unpow2N/A
lower-*.f6499.5
Applied rewrites99.5%
(FPCore (x y z) :precision binary64 (if (or (<= (* x y) -1e-41) (not (<= (* x y) 2e-262))) (fma z z (* y x)) (* (* 3.0 z) z)))
double code(double x, double y, double z) {
double tmp;
if (((x * y) <= -1e-41) || !((x * y) <= 2e-262)) {
tmp = fma(z, z, (y * x));
} else {
tmp = (3.0 * z) * z;
}
return tmp;
}
function code(x, y, z) tmp = 0.0 if ((Float64(x * y) <= -1e-41) || !(Float64(x * y) <= 2e-262)) tmp = fma(z, z, Float64(y * x)); else tmp = Float64(Float64(3.0 * z) * z); end return tmp end
code[x_, y_, z_] := If[Or[LessEqual[N[(x * y), $MachinePrecision], -1e-41], N[Not[LessEqual[N[(x * y), $MachinePrecision], 2e-262]], $MachinePrecision]], N[(z * z + N[(y * x), $MachinePrecision]), $MachinePrecision], N[(N[(3.0 * z), $MachinePrecision] * z), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \cdot y \leq -1 \cdot 10^{-41} \lor \neg \left(x \cdot y \leq 2 \cdot 10^{-262}\right):\\
\;\;\;\;\mathsf{fma}\left(z, z, y \cdot x\right)\\
\mathbf{else}:\\
\;\;\;\;\left(3 \cdot z\right) \cdot z\\
\end{array}
\end{array}
if (*.f64 x y) < -1.00000000000000001e-41 or 2.00000000000000002e-262 < (*.f64 x y) Initial program 98.3%
Applied rewrites86.7%
lift-fma.f64N/A
lift-*.f64N/A
+-rgt-identity86.7
Applied rewrites86.7%
if -1.00000000000000001e-41 < (*.f64 x y) < 2.00000000000000002e-262Initial program 99.8%
Taylor expanded in x around 0
distribute-lft1-inN/A
metadata-evalN/A
lower-*.f64N/A
unpow2N/A
lower-*.f6487.8
Applied rewrites87.8%
Applied rewrites87.9%
Final simplification87.0%
(FPCore (x y z) :precision binary64 (if (or (<= z 1.35e-35) (not (or (<= z 1.3e-8) (not (<= z 6.8e+25))))) (* y x) (* 3.0 (* z z))))
double code(double x, double y, double z) {
double tmp;
if ((z <= 1.35e-35) || !((z <= 1.3e-8) || !(z <= 6.8e+25))) {
tmp = y * x;
} else {
tmp = 3.0 * (z * 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 ((z <= 1.35d-35) .or. (.not. (z <= 1.3d-8) .or. (.not. (z <= 6.8d+25)))) then
tmp = y * x
else
tmp = 3.0d0 * (z * z)
end if
code = tmp
end function
public static double code(double x, double y, double z) {
double tmp;
if ((z <= 1.35e-35) || !((z <= 1.3e-8) || !(z <= 6.8e+25))) {
tmp = y * x;
} else {
tmp = 3.0 * (z * z);
}
return tmp;
}
def code(x, y, z): tmp = 0 if (z <= 1.35e-35) or not ((z <= 1.3e-8) or not (z <= 6.8e+25)): tmp = y * x else: tmp = 3.0 * (z * z) return tmp
function code(x, y, z) tmp = 0.0 if ((z <= 1.35e-35) || !((z <= 1.3e-8) || !(z <= 6.8e+25))) tmp = Float64(y * x); else tmp = Float64(3.0 * Float64(z * z)); end return tmp end
function tmp_2 = code(x, y, z) tmp = 0.0; if ((z <= 1.35e-35) || ~(((z <= 1.3e-8) || ~((z <= 6.8e+25))))) tmp = y * x; else tmp = 3.0 * (z * z); end tmp_2 = tmp; end
code[x_, y_, z_] := If[Or[LessEqual[z, 1.35e-35], N[Not[Or[LessEqual[z, 1.3e-8], N[Not[LessEqual[z, 6.8e+25]], $MachinePrecision]]], $MachinePrecision]], N[(y * x), $MachinePrecision], N[(3.0 * N[(z * z), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;z \leq 1.35 \cdot 10^{-35} \lor \neg \left(z \leq 1.3 \cdot 10^{-8} \lor \neg \left(z \leq 6.8 \cdot 10^{+25}\right)\right):\\
\;\;\;\;y \cdot x\\
\mathbf{else}:\\
\;\;\;\;3 \cdot \left(z \cdot z\right)\\
\end{array}
\end{array}
if z < 1.3499999999999999e-35 or 1.3000000000000001e-8 < z < 6.79999999999999967e25Initial program 98.9%
Taylor expanded in x around 0
+-commutativeN/A
*-commutativeN/A
lower-fma.f64N/A
*-commutativeN/A
lower-*.f64N/A
unpow2N/A
lower-*.f6499.9
Applied rewrites99.9%
Taylor expanded in x around 0
associate-+r+N/A
+-commutativeN/A
associate-+r+N/A
distribute-lft1-inN/A
metadata-evalN/A
*-commutativeN/A
lower-fma.f64N/A
lower-*.f64N/A
unpow2N/A
lower-*.f6499.9
Applied rewrites99.9%
Applied rewrites32.3%
Taylor expanded in x around inf
*-commutativeN/A
lower-*.f6462.7
Applied rewrites62.7%
if 1.3499999999999999e-35 < z < 1.3000000000000001e-8 or 6.79999999999999967e25 < z Initial program 98.1%
Taylor expanded in x around 0
distribute-lft1-inN/A
metadata-evalN/A
lower-*.f64N/A
unpow2N/A
lower-*.f6485.8
Applied rewrites85.8%
Final simplification67.7%
(FPCore (x y z)
:precision binary64
(if (<= z 1.35e-35)
(* y x)
(if (<= z 1.3e-8)
(* (* 3.0 z) z)
(if (<= z 6.8e+25) (* y x) (* 3.0 (* z z))))))
double code(double x, double y, double z) {
double tmp;
if (z <= 1.35e-35) {
tmp = y * x;
} else if (z <= 1.3e-8) {
tmp = (3.0 * z) * z;
} else if (z <= 6.8e+25) {
tmp = y * x;
} else {
tmp = 3.0 * (z * 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 (z <= 1.35d-35) then
tmp = y * x
else if (z <= 1.3d-8) then
tmp = (3.0d0 * z) * z
else if (z <= 6.8d+25) then
tmp = y * x
else
tmp = 3.0d0 * (z * z)
end if
code = tmp
end function
public static double code(double x, double y, double z) {
double tmp;
if (z <= 1.35e-35) {
tmp = y * x;
} else if (z <= 1.3e-8) {
tmp = (3.0 * z) * z;
} else if (z <= 6.8e+25) {
tmp = y * x;
} else {
tmp = 3.0 * (z * z);
}
return tmp;
}
def code(x, y, z): tmp = 0 if z <= 1.35e-35: tmp = y * x elif z <= 1.3e-8: tmp = (3.0 * z) * z elif z <= 6.8e+25: tmp = y * x else: tmp = 3.0 * (z * z) return tmp
function code(x, y, z) tmp = 0.0 if (z <= 1.35e-35) tmp = Float64(y * x); elseif (z <= 1.3e-8) tmp = Float64(Float64(3.0 * z) * z); elseif (z <= 6.8e+25) tmp = Float64(y * x); else tmp = Float64(3.0 * Float64(z * z)); end return tmp end
function tmp_2 = code(x, y, z) tmp = 0.0; if (z <= 1.35e-35) tmp = y * x; elseif (z <= 1.3e-8) tmp = (3.0 * z) * z; elseif (z <= 6.8e+25) tmp = y * x; else tmp = 3.0 * (z * z); end tmp_2 = tmp; end
code[x_, y_, z_] := If[LessEqual[z, 1.35e-35], N[(y * x), $MachinePrecision], If[LessEqual[z, 1.3e-8], N[(N[(3.0 * z), $MachinePrecision] * z), $MachinePrecision], If[LessEqual[z, 6.8e+25], N[(y * x), $MachinePrecision], N[(3.0 * N[(z * z), $MachinePrecision]), $MachinePrecision]]]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;z \leq 1.35 \cdot 10^{-35}:\\
\;\;\;\;y \cdot x\\
\mathbf{elif}\;z \leq 1.3 \cdot 10^{-8}:\\
\;\;\;\;\left(3 \cdot z\right) \cdot z\\
\mathbf{elif}\;z \leq 6.8 \cdot 10^{+25}:\\
\;\;\;\;y \cdot x\\
\mathbf{else}:\\
\;\;\;\;3 \cdot \left(z \cdot z\right)\\
\end{array}
\end{array}
if z < 1.3499999999999999e-35 or 1.3000000000000001e-8 < z < 6.79999999999999967e25Initial program 98.9%
Taylor expanded in x around 0
+-commutativeN/A
*-commutativeN/A
lower-fma.f64N/A
*-commutativeN/A
lower-*.f64N/A
unpow2N/A
lower-*.f6499.9
Applied rewrites99.9%
Taylor expanded in x around 0
associate-+r+N/A
+-commutativeN/A
associate-+r+N/A
distribute-lft1-inN/A
metadata-evalN/A
*-commutativeN/A
lower-fma.f64N/A
lower-*.f64N/A
unpow2N/A
lower-*.f6499.9
Applied rewrites99.9%
Applied rewrites32.3%
Taylor expanded in x around inf
*-commutativeN/A
lower-*.f6462.7
Applied rewrites62.7%
if 1.3499999999999999e-35 < z < 1.3000000000000001e-8Initial program 99.3%
Taylor expanded in x around 0
distribute-lft1-inN/A
metadata-evalN/A
lower-*.f64N/A
unpow2N/A
lower-*.f6495.9
Applied rewrites95.9%
Applied rewrites96.3%
if 6.79999999999999967e25 < z Initial program 97.9%
Taylor expanded in x around 0
distribute-lft1-inN/A
metadata-evalN/A
lower-*.f64N/A
unpow2N/A
lower-*.f6484.3
Applied rewrites84.3%
(FPCore (x y z) :precision binary64 (if (<= z 4e+106) (* y x) (* z z)))
double code(double x, double y, double z) {
double tmp;
if (z <= 4e+106) {
tmp = y * x;
} else {
tmp = z * 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 (z <= 4d+106) then
tmp = y * x
else
tmp = z * z
end if
code = tmp
end function
public static double code(double x, double y, double z) {
double tmp;
if (z <= 4e+106) {
tmp = y * x;
} else {
tmp = z * z;
}
return tmp;
}
def code(x, y, z): tmp = 0 if z <= 4e+106: tmp = y * x else: tmp = z * z return tmp
function code(x, y, z) tmp = 0.0 if (z <= 4e+106) tmp = Float64(y * x); else tmp = Float64(z * z); end return tmp end
function tmp_2 = code(x, y, z) tmp = 0.0; if (z <= 4e+106) tmp = y * x; else tmp = z * z; end tmp_2 = tmp; end
code[x_, y_, z_] := If[LessEqual[z, 4e+106], N[(y * x), $MachinePrecision], N[(z * z), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;z \leq 4 \cdot 10^{+106}:\\
\;\;\;\;y \cdot x\\
\mathbf{else}:\\
\;\;\;\;z \cdot z\\
\end{array}
\end{array}
if z < 4.00000000000000036e106Initial program 99.0%
Taylor expanded in x around 0
+-commutativeN/A
*-commutativeN/A
lower-fma.f64N/A
*-commutativeN/A
lower-*.f64N/A
unpow2N/A
lower-*.f6499.9
Applied rewrites99.9%
Taylor expanded in x around 0
associate-+r+N/A
+-commutativeN/A
associate-+r+N/A
distribute-lft1-inN/A
metadata-evalN/A
*-commutativeN/A
lower-fma.f64N/A
lower-*.f64N/A
unpow2N/A
lower-*.f6499.9
Applied rewrites99.9%
Applied rewrites38.2%
Taylor expanded in x around inf
*-commutativeN/A
lower-*.f6460.2
Applied rewrites60.2%
if 4.00000000000000036e106 < z Initial program 97.3%
Applied rewrites87.2%
Taylor expanded in x around 0
unpow2N/A
lower-*.f6483.8
Applied rewrites83.8%
(FPCore (x y z) :precision binary64 (* z z))
double code(double x, double y, double z) {
return z * z;
}
real(8) function code(x, y, z)
real(8), intent (in) :: x
real(8), intent (in) :: y
real(8), intent (in) :: z
code = z * z
end function
public static double code(double x, double y, double z) {
return z * z;
}
def code(x, y, z): return z * z
function code(x, y, z) return Float64(z * z) end
function tmp = code(x, y, z) tmp = z * z; end
code[x_, y_, z_] := N[(z * z), $MachinePrecision]
\begin{array}{l}
\\
z \cdot z
\end{array}
Initial program 98.7%
Applied rewrites78.6%
Taylor expanded in x around 0
unpow2N/A
lower-*.f6433.2
Applied rewrites33.2%
(FPCore (x y z) :precision binary64 (+ (* (* 3.0 z) z) (* y x)))
double code(double x, double y, double z) {
return ((3.0 * z) * z) + (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 = ((3.0d0 * z) * z) + (y * x)
end function
public static double code(double x, double y, double z) {
return ((3.0 * z) * z) + (y * x);
}
def code(x, y, z): return ((3.0 * z) * z) + (y * x)
function code(x, y, z) return Float64(Float64(Float64(3.0 * z) * z) + Float64(y * x)) end
function tmp = code(x, y, z) tmp = ((3.0 * z) * z) + (y * x); end
code[x_, y_, z_] := N[(N[(N[(3.0 * z), $MachinePrecision] * z), $MachinePrecision] + N[(y * x), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\left(3 \cdot z\right) \cdot z + y \cdot x
\end{array}
herbie shell --seed 2024337
(FPCore (x y z)
:name "Linear.Quaternion:$c/ from linear-1.19.1.3, A"
:precision binary64
:alt
(! :herbie-platform default (+ (* (* 3 z) z) (* y x)))
(+ (+ (+ (* x y) (* z z)) (* z z)) (* z z)))