
(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 6 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 z t (fma b a (* y x))))
double code(double x, double y, double z, double t, double a, double b) {
return fma(z, t, fma(b, a, (y * x)));
}
function code(x, y, z, t, a, b) return fma(z, t, fma(b, a, Float64(y * x))) end
code[x_, y_, z_, t_, a_, b_] := N[(z * t + N[(b * a + N[(y * x), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\mathsf{fma}\left(z, t, \mathsf{fma}\left(b, a, y \cdot x\right)\right)
\end{array}
Initial program 98.0%
lift-+.f64N/A
lift-+.f64N/A
+-commutativeN/A
associate-+l+N/A
lift-*.f64N/A
lower-fma.f64N/A
+-commutativeN/A
lift-*.f64N/A
*-commutativeN/A
lower-fma.f6499.2
lift-*.f64N/A
*-commutativeN/A
lower-*.f6499.2
Applied rewrites99.2%
(FPCore (x y z t a b) :precision binary64 (if (or (<= (* a b) -500.0) (not (<= (* a b) 5e+36))) (fma t z (* b a)) (fma t z (* y x))))
double code(double x, double y, double z, double t, double a, double b) {
double tmp;
if (((a * b) <= -500.0) || !((a * b) <= 5e+36)) {
tmp = fma(t, z, (b * a));
} else {
tmp = fma(t, z, (y * x));
}
return tmp;
}
function code(x, y, z, t, a, b) tmp = 0.0 if ((Float64(a * b) <= -500.0) || !(Float64(a * b) <= 5e+36)) tmp = fma(t, z, Float64(b * a)); else tmp = fma(t, z, Float64(y * x)); end return tmp end
code[x_, y_, z_, t_, a_, b_] := If[Or[LessEqual[N[(a * b), $MachinePrecision], -500.0], N[Not[LessEqual[N[(a * b), $MachinePrecision], 5e+36]], $MachinePrecision]], N[(t * z + N[(b * a), $MachinePrecision]), $MachinePrecision], N[(t * z + N[(y * x), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;a \cdot b \leq -500 \lor \neg \left(a \cdot b \leq 5 \cdot 10^{+36}\right):\\
\;\;\;\;\mathsf{fma}\left(t, z, b \cdot a\right)\\
\mathbf{else}:\\
\;\;\;\;\mathsf{fma}\left(t, z, y \cdot x\right)\\
\end{array}
\end{array}
if (*.f64 a b) < -500 or 4.99999999999999977e36 < (*.f64 a b) Initial program 96.5%
Taylor expanded in x around 0
+-commutativeN/A
lower-fma.f64N/A
*-commutativeN/A
lower-*.f6488.0
Applied rewrites88.0%
if -500 < (*.f64 a b) < 4.99999999999999977e36Initial program 99.3%
Taylor expanded in x around 0
+-commutativeN/A
lower-fma.f64N/A
*-commutativeN/A
lower-*.f6450.6
Applied rewrites50.6%
Taylor expanded in a around 0
fp-cancel-sign-sub-invN/A
distribute-lft-neg-outN/A
distribute-rgt-neg-outN/A
mul-1-negN/A
fp-cancel-sub-sign-invN/A
mul-1-negN/A
lower-fma.f64N/A
*-commutativeN/A
mul-1-negN/A
distribute-lft-neg-inN/A
distribute-rgt-neg-inN/A
mul-1-negN/A
remove-double-negN/A
lower-*.f6488.5
Applied rewrites88.5%
Final simplification88.3%
(FPCore (x y z t a b) :precision binary64 (if (<= (* a b) -3.44e+91) (fma y x (* b a)) (if (<= (* a b) 5e+36) (fma t z (* y x)) (fma t z (* b a)))))
double code(double x, double y, double z, double t, double a, double b) {
double tmp;
if ((a * b) <= -3.44e+91) {
tmp = fma(y, x, (b * a));
} else if ((a * b) <= 5e+36) {
tmp = fma(t, z, (y * x));
} else {
tmp = fma(t, z, (b * a));
}
return tmp;
}
function code(x, y, z, t, a, b) tmp = 0.0 if (Float64(a * b) <= -3.44e+91) tmp = fma(y, x, Float64(b * a)); elseif (Float64(a * b) <= 5e+36) tmp = fma(t, z, Float64(y * x)); else tmp = fma(t, z, Float64(b * a)); end return tmp end
code[x_, y_, z_, t_, a_, b_] := If[LessEqual[N[(a * b), $MachinePrecision], -3.44e+91], N[(y * x + N[(b * a), $MachinePrecision]), $MachinePrecision], If[LessEqual[N[(a * b), $MachinePrecision], 5e+36], N[(t * z + N[(y * x), $MachinePrecision]), $MachinePrecision], N[(t * z + N[(b * a), $MachinePrecision]), $MachinePrecision]]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;a \cdot b \leq -3.44 \cdot 10^{+91}:\\
\;\;\;\;\mathsf{fma}\left(y, x, b \cdot a\right)\\
\mathbf{elif}\;a \cdot b \leq 5 \cdot 10^{+36}:\\
\;\;\;\;\mathsf{fma}\left(t, z, y \cdot x\right)\\
\mathbf{else}:\\
\;\;\;\;\mathsf{fma}\left(t, z, b \cdot a\right)\\
\end{array}
\end{array}
if (*.f64 a b) < -3.4399999999999998e91Initial program 95.8%
Taylor expanded in z around 0
+-commutativeN/A
*-commutativeN/A
lower-fma.f64N/A
*-commutativeN/A
lower-*.f6491.8
Applied rewrites91.8%
if -3.4399999999999998e91 < (*.f64 a b) < 4.99999999999999977e36Initial program 99.4%
Taylor expanded in x around 0
+-commutativeN/A
lower-fma.f64N/A
*-commutativeN/A
lower-*.f6452.9
Applied rewrites52.9%
Taylor expanded in a around 0
fp-cancel-sign-sub-invN/A
distribute-lft-neg-outN/A
distribute-rgt-neg-outN/A
mul-1-negN/A
fp-cancel-sub-sign-invN/A
mul-1-negN/A
lower-fma.f64N/A
*-commutativeN/A
mul-1-negN/A
distribute-lft-neg-inN/A
distribute-rgt-neg-inN/A
mul-1-negN/A
remove-double-negN/A
lower-*.f6487.4
Applied rewrites87.4%
if 4.99999999999999977e36 < (*.f64 a b) Initial program 96.3%
Taylor expanded in x around 0
+-commutativeN/A
lower-fma.f64N/A
*-commutativeN/A
lower-*.f6491.2
Applied rewrites91.2%
(FPCore (x y z t a b) :precision binary64 (if (or (<= (* a b) -5e+101) (not (<= (* a b) 5e-55))) (* b a) (* t z)))
double code(double x, double y, double z, double t, double a, double b) {
double tmp;
if (((a * b) <= -5e+101) || !((a * b) <= 5e-55)) {
tmp = b * a;
} else {
tmp = t * z;
}
return tmp;
}
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
real(8) :: tmp
if (((a * b) <= (-5d+101)) .or. (.not. ((a * b) <= 5d-55))) then
tmp = b * a
else
tmp = t * z
end if
code = tmp
end function
public static double code(double x, double y, double z, double t, double a, double b) {
double tmp;
if (((a * b) <= -5e+101) || !((a * b) <= 5e-55)) {
tmp = b * a;
} else {
tmp = t * z;
}
return tmp;
}
def code(x, y, z, t, a, b): tmp = 0 if ((a * b) <= -5e+101) or not ((a * b) <= 5e-55): tmp = b * a else: tmp = t * z return tmp
function code(x, y, z, t, a, b) tmp = 0.0 if ((Float64(a * b) <= -5e+101) || !(Float64(a * b) <= 5e-55)) tmp = Float64(b * a); else tmp = Float64(t * z); end return tmp end
function tmp_2 = code(x, y, z, t, a, b) tmp = 0.0; if (((a * b) <= -5e+101) || ~(((a * b) <= 5e-55))) tmp = b * a; else tmp = t * z; end tmp_2 = tmp; end
code[x_, y_, z_, t_, a_, b_] := If[Or[LessEqual[N[(a * b), $MachinePrecision], -5e+101], N[Not[LessEqual[N[(a * b), $MachinePrecision], 5e-55]], $MachinePrecision]], N[(b * a), $MachinePrecision], N[(t * z), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;a \cdot b \leq -5 \cdot 10^{+101} \lor \neg \left(a \cdot b \leq 5 \cdot 10^{-55}\right):\\
\;\;\;\;b \cdot a\\
\mathbf{else}:\\
\;\;\;\;t \cdot z\\
\end{array}
\end{array}
if (*.f64 a b) < -4.99999999999999989e101 or 5.0000000000000002e-55 < (*.f64 a b) Initial program 96.8%
Taylor expanded in z around 0
+-commutativeN/A
*-commutativeN/A
lower-fma.f64N/A
*-commutativeN/A
lower-*.f6485.3
Applied rewrites85.3%
Taylor expanded in x around 0
Applied rewrites66.0%
if -4.99999999999999989e101 < (*.f64 a b) < 5.0000000000000002e-55Initial program 99.2%
Taylor expanded in x around 0
+-commutativeN/A
lower-fma.f64N/A
*-commutativeN/A
lower-*.f6453.8
Applied rewrites53.8%
Taylor expanded in z around inf
lower-*.f6445.7
Applied rewrites45.7%
Final simplification55.6%
(FPCore (x y z t a b) :precision binary64 (fma t z (* b a)))
double code(double x, double y, double z, double t, double a, double b) {
return fma(t, z, (b * a));
}
function code(x, y, z, t, a, b) return fma(t, z, Float64(b * a)) end
code[x_, y_, z_, t_, a_, b_] := N[(t * z + N[(b * a), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\mathsf{fma}\left(t, z, b \cdot a\right)
\end{array}
Initial program 98.0%
Taylor expanded in x around 0
+-commutativeN/A
lower-fma.f64N/A
*-commutativeN/A
lower-*.f6467.1
Applied rewrites67.1%
(FPCore (x y z t a b) :precision binary64 (* b a))
double code(double x, double y, double z, double t, double a, double b) {
return b * a;
}
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 = b * a
end function
public static double code(double x, double y, double z, double t, double a, double b) {
return b * a;
}
def code(x, y, z, t, a, b): return b * a
function code(x, y, z, t, a, b) return Float64(b * a) end
function tmp = code(x, y, z, t, a, b) tmp = b * a; end
code[x_, y_, z_, t_, a_, b_] := N[(b * a), $MachinePrecision]
\begin{array}{l}
\\
b \cdot a
\end{array}
Initial program 98.0%
Taylor expanded in z around 0
+-commutativeN/A
*-commutativeN/A
lower-fma.f64N/A
*-commutativeN/A
lower-*.f6471.3
Applied rewrites71.3%
Taylor expanded in x around 0
Applied rewrites37.8%
herbie shell --seed 2024320
(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)))