
(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 7 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 97.9%
Taylor expanded in x around 0
+-commutativeN/A
*-commutativeN/A
lower-fma.f64N/A
*-commutativeN/A
lower-*.f64N/A
unpow2N/A
lower-*.f6499.8
Applied rewrites99.8%
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.8
Applied rewrites99.8%
(FPCore (x y z) :precision binary64 (if (<= z 2.4e+31) (fma z z (* y x)) (fma z z (* z (+ z z)))))
double code(double x, double y, double z) {
double tmp;
if (z <= 2.4e+31) {
tmp = fma(z, z, (y * x));
} else {
tmp = fma(z, z, (z * (z + z)));
}
return tmp;
}
function code(x, y, z) tmp = 0.0 if (z <= 2.4e+31) tmp = fma(z, z, Float64(y * x)); else tmp = fma(z, z, Float64(z * Float64(z + z))); end return tmp end
code[x_, y_, z_] := If[LessEqual[z, 2.4e+31], N[(z * z + N[(y * x), $MachinePrecision]), $MachinePrecision], N[(z * z + N[(z * N[(z + z), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;z \leq 2.4 \cdot 10^{+31}:\\
\;\;\;\;\mathsf{fma}\left(z, z, y \cdot x\right)\\
\mathbf{else}:\\
\;\;\;\;\mathsf{fma}\left(z, z, z \cdot \left(z + z\right)\right)\\
\end{array}
\end{array}
if z < 2.39999999999999982e31Initial program 98.8%
Applied rewrites79.5%
lift-fma.f64N/A
lift-*.f64N/A
+-rgt-identity79.5
Applied rewrites79.5%
if 2.39999999999999982e31 < z Initial program 94.6%
Taylor expanded in x around 0
+-commutativeN/A
*-commutativeN/A
lower-fma.f64N/A
*-commutativeN/A
lower-*.f64N/A
unpow2N/A
lower-*.f6499.8
Applied rewrites99.8%
lift-+.f64N/A
+-commutativeN/A
lift-*.f64N/A
lower-fma.f6499.9
Applied rewrites94.7%
Taylor expanded in x around 0
*-commutativeN/A
lower-*.f64N/A
unpow2N/A
lower-*.f6492.2
Applied rewrites92.2%
Applied rewrites92.2%
(FPCore (x y z) :precision binary64 (if (<= z 2.4e+31) (fma z z (* y x)) (* (* 3.0 z) z)))
double code(double x, double y, double z) {
double tmp;
if (z <= 2.4e+31) {
tmp = fma(z, z, (y * x));
} else {
tmp = (3.0 * z) * z;
}
return tmp;
}
function code(x, y, z) tmp = 0.0 if (z <= 2.4e+31) tmp = fma(z, z, Float64(y * x)); else tmp = Float64(Float64(3.0 * z) * z); end return tmp end
code[x_, y_, z_] := If[LessEqual[z, 2.4e+31], N[(z * z + N[(y * x), $MachinePrecision]), $MachinePrecision], N[(N[(3.0 * z), $MachinePrecision] * z), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;z \leq 2.4 \cdot 10^{+31}:\\
\;\;\;\;\mathsf{fma}\left(z, z, y \cdot x\right)\\
\mathbf{else}:\\
\;\;\;\;\left(3 \cdot z\right) \cdot z\\
\end{array}
\end{array}
if z < 2.39999999999999982e31Initial program 98.8%
Applied rewrites79.5%
lift-fma.f64N/A
lift-*.f64N/A
+-rgt-identity79.5
Applied rewrites79.5%
if 2.39999999999999982e31 < z Initial program 94.6%
Taylor expanded in x around 0
distribute-lft1-inN/A
metadata-evalN/A
lower-*.f64N/A
unpow2N/A
lower-*.f6492.1
Applied rewrites92.1%
Applied rewrites92.1%
(FPCore (x y z) :precision binary64 (if (<= z 3.4e-29) (* y x) (* (* 3.0 z) z)))
double code(double x, double y, double z) {
double tmp;
if (z <= 3.4e-29) {
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 <= 3.4d-29) 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 <= 3.4e-29) {
tmp = y * x;
} else {
tmp = (3.0 * z) * z;
}
return tmp;
}
def code(x, y, z): tmp = 0 if z <= 3.4e-29: tmp = y * x else: tmp = (3.0 * z) * z return tmp
function code(x, y, z) tmp = 0.0 if (z <= 3.4e-29) tmp = Float64(y * x); else tmp = Float64(Float64(3.0 * z) * z); end return tmp end
function tmp_2 = code(x, y, z) tmp = 0.0; if (z <= 3.4e-29) tmp = y * x; else tmp = (3.0 * z) * z; end tmp_2 = tmp; end
code[x_, y_, z_] := If[LessEqual[z, 3.4e-29], N[(y * x), $MachinePrecision], N[(N[(3.0 * z), $MachinePrecision] * z), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;z \leq 3.4 \cdot 10^{-29}:\\
\;\;\;\;y \cdot x\\
\mathbf{else}:\\
\;\;\;\;\left(3 \cdot z\right) \cdot z\\
\end{array}
\end{array}
if z < 3.39999999999999972e-29Initial program 98.8%
Taylor expanded in x around 0
+-commutativeN/A
associate-+r+N/A
distribute-lft1-inN/A
metadata-evalN/A
unpow2N/A
associate-*r*N/A
remove-double-negN/A
mul-1-negN/A
distribute-rgt-neg-inN/A
distribute-lft-neg-inN/A
lower-fma.f64N/A
lower-*.f64N/A
distribute-lft-neg-inN/A
distribute-rgt-neg-inN/A
mul-1-negN/A
remove-double-negN/A
*-commutativeN/A
lower-*.f6498.8
Applied rewrites98.8%
Applied rewrites40.9%
Taylor expanded in x around inf
Applied rewrites59.5%
if 3.39999999999999972e-29 < z Initial program 95.4%
Taylor expanded in x around 0
distribute-lft1-inN/A
metadata-evalN/A
lower-*.f64N/A
unpow2N/A
lower-*.f6488.2
Applied rewrites88.2%
Applied rewrites88.2%
(FPCore (x y z) :precision binary64 (if (<= z 3.4e-29) (* y x) (* 3.0 (* z z))))
double code(double x, double y, double z) {
double tmp;
if (z <= 3.4e-29) {
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 <= 3.4d-29) 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 <= 3.4e-29) {
tmp = y * x;
} else {
tmp = 3.0 * (z * z);
}
return tmp;
}
def code(x, y, z): tmp = 0 if z <= 3.4e-29: tmp = y * x else: tmp = 3.0 * (z * z) return tmp
function code(x, y, z) tmp = 0.0 if (z <= 3.4e-29) 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 <= 3.4e-29) tmp = y * x; else tmp = 3.0 * (z * z); end tmp_2 = tmp; end
code[x_, y_, z_] := If[LessEqual[z, 3.4e-29], N[(y * x), $MachinePrecision], N[(3.0 * N[(z * z), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;z \leq 3.4 \cdot 10^{-29}:\\
\;\;\;\;y \cdot x\\
\mathbf{else}:\\
\;\;\;\;3 \cdot \left(z \cdot z\right)\\
\end{array}
\end{array}
if z < 3.39999999999999972e-29Initial program 98.8%
Taylor expanded in x around 0
+-commutativeN/A
associate-+r+N/A
distribute-lft1-inN/A
metadata-evalN/A
unpow2N/A
associate-*r*N/A
remove-double-negN/A
mul-1-negN/A
distribute-rgt-neg-inN/A
distribute-lft-neg-inN/A
lower-fma.f64N/A
lower-*.f64N/A
distribute-lft-neg-inN/A
distribute-rgt-neg-inN/A
mul-1-negN/A
remove-double-negN/A
*-commutativeN/A
lower-*.f6498.8
Applied rewrites98.8%
Applied rewrites40.9%
Taylor expanded in x around inf
Applied rewrites59.5%
if 3.39999999999999972e-29 < z Initial program 95.4%
Taylor expanded in x around 0
distribute-lft1-inN/A
metadata-evalN/A
lower-*.f64N/A
unpow2N/A
lower-*.f6488.2
Applied rewrites88.2%
(FPCore (x y z) :precision binary64 (if (<= z 2.35e+123) (* y x) (* z z)))
double code(double x, double y, double z) {
double tmp;
if (z <= 2.35e+123) {
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 <= 2.35d+123) 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 <= 2.35e+123) {
tmp = y * x;
} else {
tmp = z * z;
}
return tmp;
}
def code(x, y, z): tmp = 0 if z <= 2.35e+123: tmp = y * x else: tmp = z * z return tmp
function code(x, y, z) tmp = 0.0 if (z <= 2.35e+123) 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 <= 2.35e+123) tmp = y * x; else tmp = z * z; end tmp_2 = tmp; end
code[x_, y_, z_] := If[LessEqual[z, 2.35e+123], N[(y * x), $MachinePrecision], N[(z * z), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;z \leq 2.35 \cdot 10^{+123}:\\
\;\;\;\;y \cdot x\\
\mathbf{else}:\\
\;\;\;\;z \cdot z\\
\end{array}
\end{array}
if z < 2.3499999999999999e123Initial program 98.9%
Taylor expanded in x around 0
+-commutativeN/A
associate-+r+N/A
distribute-lft1-inN/A
metadata-evalN/A
unpow2N/A
associate-*r*N/A
remove-double-negN/A
mul-1-negN/A
distribute-rgt-neg-inN/A
distribute-lft-neg-inN/A
lower-fma.f64N/A
lower-*.f64N/A
distribute-lft-neg-inN/A
distribute-rgt-neg-inN/A
mul-1-negN/A
remove-double-negN/A
*-commutativeN/A
lower-*.f6498.9
Applied rewrites98.9%
Applied rewrites43.6%
Taylor expanded in x around inf
Applied rewrites54.2%
if 2.3499999999999999e123 < z Initial program 92.6%
Applied rewrites70.6%
Taylor expanded in x around 0
unpow2N/A
lower-*.f6473.0
Applied rewrites73.0%
(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 97.9%
Applied rewrites74.6%
Taylor expanded in x around 0
unpow2N/A
lower-*.f6436.0
Applied rewrites36.0%
(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 2024338
(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)))