
(FPCore (x y z t a b) :precision binary64 (+ (+ (* x y) (* z t)) (* a b)))
double code(double x, double y, double z, double t, double a, double b) {
return ((x * y) + (z * t)) + (a * b);
}
real(8) function code(x, y, z, t, a, b)
real(8), intent (in) :: x
real(8), intent (in) :: y
real(8), intent (in) :: z
real(8), intent (in) :: t
real(8), intent (in) :: a
real(8), intent (in) :: b
code = ((x * y) + (z * t)) + (a * b)
end function
public static double code(double x, double y, double z, double t, double a, double b) {
return ((x * y) + (z * t)) + (a * b);
}
def code(x, y, z, t, a, b): return ((x * y) + (z * t)) + (a * b)
function code(x, y, z, t, a, b) return Float64(Float64(Float64(x * y) + Float64(z * t)) + Float64(a * b)) end
function tmp = code(x, y, z, t, a, b) tmp = ((x * y) + (z * t)) + (a * b); end
code[x_, y_, z_, t_, a_, b_] := N[(N[(N[(x * y), $MachinePrecision] + N[(z * t), $MachinePrecision]), $MachinePrecision] + N[(a * b), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\left(x \cdot y + z \cdot t\right) + a \cdot b
\end{array}
Sampling outcomes in binary64 precision:
Herbie found 7 alternatives:
| Alternative | Accuracy | Speedup |
|---|
(FPCore (x y z t a b) :precision binary64 (+ (+ (* x y) (* z t)) (* a b)))
double code(double x, double y, double z, double t, double a, double b) {
return ((x * y) + (z * t)) + (a * b);
}
real(8) function code(x, y, z, t, a, b)
real(8), intent (in) :: x
real(8), intent (in) :: y
real(8), intent (in) :: z
real(8), intent (in) :: t
real(8), intent (in) :: a
real(8), intent (in) :: b
code = ((x * y) + (z * t)) + (a * b)
end function
public static double code(double x, double y, double z, double t, double a, double b) {
return ((x * y) + (z * t)) + (a * b);
}
def code(x, y, z, t, a, b): return ((x * y) + (z * t)) + (a * b)
function code(x, y, z, t, a, b) return Float64(Float64(Float64(x * y) + Float64(z * t)) + Float64(a * b)) end
function tmp = code(x, y, z, t, a, b) tmp = ((x * y) + (z * t)) + (a * b); end
code[x_, y_, z_, t_, a_, b_] := N[(N[(N[(x * y), $MachinePrecision] + N[(z * t), $MachinePrecision]), $MachinePrecision] + N[(a * b), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\left(x \cdot y + z \cdot t\right) + a \cdot b
\end{array}
(FPCore (x y z t a b) :precision binary64 (fma y x (fma z t (* a b))))
double code(double x, double y, double z, double t, double a, double b) {
return fma(y, x, fma(z, t, (a * b)));
}
function code(x, y, z, t, a, b) return fma(y, x, fma(z, t, Float64(a * b))) end
code[x_, y_, z_, t_, a_, b_] := N[(y * x + N[(z * t + N[(a * b), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\mathsf{fma}\left(y, x, \mathsf{fma}\left(z, t, a \cdot b\right)\right)
\end{array}
Initial program 98.8%
lift-*.f64N/A
lift-*.f64N/A
lift-*.f64N/A
associate-+l+N/A
lift-*.f64N/A
*-commutativeN/A
lower-fma.f64N/A
lift-*.f64N/A
lower-fma.f6498.8
Applied egg-rr98.8%
(FPCore (x y z t a b) :precision binary64 (if (<= (* y x) -5e-16) (fma a b (* y x)) (if (<= (* y x) 5e+133) (fma a b (* z t)) (fma t z (* y x)))))
double code(double x, double y, double z, double t, double a, double b) {
double tmp;
if ((y * x) <= -5e-16) {
tmp = fma(a, b, (y * x));
} else if ((y * x) <= 5e+133) {
tmp = fma(a, b, (z * t));
} else {
tmp = fma(t, z, (y * x));
}
return tmp;
}
function code(x, y, z, t, a, b) tmp = 0.0 if (Float64(y * x) <= -5e-16) tmp = fma(a, b, Float64(y * x)); elseif (Float64(y * x) <= 5e+133) tmp = fma(a, b, Float64(z * t)); else tmp = fma(t, z, Float64(y * x)); end return tmp end
code[x_, y_, z_, t_, a_, b_] := If[LessEqual[N[(y * x), $MachinePrecision], -5e-16], N[(a * b + N[(y * x), $MachinePrecision]), $MachinePrecision], If[LessEqual[N[(y * x), $MachinePrecision], 5e+133], N[(a * b + N[(z * t), $MachinePrecision]), $MachinePrecision], N[(t * z + N[(y * x), $MachinePrecision]), $MachinePrecision]]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;y \cdot x \leq -5 \cdot 10^{-16}:\\
\;\;\;\;\mathsf{fma}\left(a, b, y \cdot x\right)\\
\mathbf{elif}\;y \cdot x \leq 5 \cdot 10^{+133}:\\
\;\;\;\;\mathsf{fma}\left(a, b, z \cdot t\right)\\
\mathbf{else}:\\
\;\;\;\;\mathsf{fma}\left(t, z, y \cdot x\right)\\
\end{array}
\end{array}
if (*.f64 x y) < -5.0000000000000004e-16Initial program 96.4%
Taylor expanded in z around 0
lower-fma.f64N/A
lower-*.f6476.2
Simplified76.2%
if -5.0000000000000004e-16 < (*.f64 x y) < 4.99999999999999961e133Initial program 99.0%
Taylor expanded in x around 0
lower-fma.f64N/A
lower-*.f6486.8
Simplified86.8%
if 4.99999999999999961e133 < (*.f64 x y) Initial program 95.2%
Taylor expanded in a around 0
lower-fma.f64N/A
lower-*.f6486.9
Simplified86.9%
Final simplification84.1%
herbie shell --seed 2024218
(FPCore (x y z t a b)
:name "Linear.V3:$cdot from linear-1.19.1.3, B"
:precision binary64
(+ (+ (* x y) (* z t)) (* a b)))