
(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 b a (* t z))))
double code(double x, double y, double z, double t, double a, double b) {
return fma(y, x, fma(b, a, (t * z)));
}
function code(x, y, z, t, a, b) return fma(y, x, fma(b, a, Float64(t * z))) end
code[x_, y_, z_, t_, a_, b_] := N[(y * x + N[(b * a + N[(t * z), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\mathsf{fma}\left(y, x, \mathsf{fma}\left(b, a, t \cdot z\right)\right)
\end{array}
Initial program 99.6%
lift-+.f64N/A
lift-+.f64N/A
associate-+l+N/A
lift-*.f64N/A
*-commutativeN/A
lower-fma.f64N/A
+-commutativeN/A
lift-*.f64N/A
*-commutativeN/A
lower-fma.f6499.6
lift-*.f64N/A
*-commutativeN/A
lower-*.f6499.6
Applied rewrites99.6%
(FPCore (x y z t a b)
:precision binary64
(let* ((t_1 (fma t z (* b a))))
(if (<= (* z t) -1e+131)
t_1
(if (<= (* z t) 2e-42)
(fma y x (* b a))
(if (<= (* z t) 4e+44) (fma y x (* t z)) t_1)))))
double code(double x, double y, double z, double t, double a, double b) {
double t_1 = fma(t, z, (b * a));
double tmp;
if ((z * t) <= -1e+131) {
tmp = t_1;
} else if ((z * t) <= 2e-42) {
tmp = fma(y, x, (b * a));
} else if ((z * t) <= 4e+44) {
tmp = fma(y, x, (t * z));
} else {
tmp = t_1;
}
return tmp;
}
function code(x, y, z, t, a, b) t_1 = fma(t, z, Float64(b * a)) tmp = 0.0 if (Float64(z * t) <= -1e+131) tmp = t_1; elseif (Float64(z * t) <= 2e-42) tmp = fma(y, x, Float64(b * a)); elseif (Float64(z * t) <= 4e+44) tmp = fma(y, x, Float64(t * z)); else tmp = t_1; end return tmp end
code[x_, y_, z_, t_, a_, b_] := Block[{t$95$1 = N[(t * z + N[(b * a), $MachinePrecision]), $MachinePrecision]}, If[LessEqual[N[(z * t), $MachinePrecision], -1e+131], t$95$1, If[LessEqual[N[(z * t), $MachinePrecision], 2e-42], N[(y * x + N[(b * a), $MachinePrecision]), $MachinePrecision], If[LessEqual[N[(z * t), $MachinePrecision], 4e+44], N[(y * x + N[(t * z), $MachinePrecision]), $MachinePrecision], t$95$1]]]]
\begin{array}{l}
\\
\begin{array}{l}
t_1 := \mathsf{fma}\left(t, z, b \cdot a\right)\\
\mathbf{if}\;z \cdot t \leq -1 \cdot 10^{+131}:\\
\;\;\;\;t\_1\\
\mathbf{elif}\;z \cdot t \leq 2 \cdot 10^{-42}:\\
\;\;\;\;\mathsf{fma}\left(y, x, b \cdot a\right)\\
\mathbf{elif}\;z \cdot t \leq 4 \cdot 10^{+44}:\\
\;\;\;\;\mathsf{fma}\left(y, x, t \cdot z\right)\\
\mathbf{else}:\\
\;\;\;\;t\_1\\
\end{array}
\end{array}
if (*.f64 z t) < -9.9999999999999991e130 or 4.0000000000000004e44 < (*.f64 z t) Initial program 98.8%
Taylor expanded in x around 0
+-commutativeN/A
lower-fma.f64N/A
*-commutativeN/A
lower-*.f6494.4
Applied rewrites94.4%
if -9.9999999999999991e130 < (*.f64 z t) < 2.00000000000000008e-42Initial program 100.0%
Taylor expanded in z around 0
+-commutativeN/A
*-commutativeN/A
lower-fma.f64N/A
*-commutativeN/A
lower-*.f6492.1
Applied rewrites92.1%
if 2.00000000000000008e-42 < (*.f64 z t) < 4.0000000000000004e44Initial program 99.9%
Taylor expanded in z around 0
+-commutativeN/A
*-commutativeN/A
lower-fma.f64N/A
*-commutativeN/A
lower-*.f6460.6
Applied rewrites60.6%
Taylor expanded in x around 0
Applied rewrites8.5%
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-*.f6492.2
Applied rewrites92.2%
Applied rewrites92.3%
(FPCore (x y z t a b)
:precision binary64
(let* ((t_1 (fma t z (* b a))))
(if (<= (* z t) -1e+131)
t_1
(if (<= (* z t) 2e-42)
(fma y x (* b a))
(if (<= (* z t) 4e+44) (fma t z (* y x)) t_1)))))
double code(double x, double y, double z, double t, double a, double b) {
double t_1 = fma(t, z, (b * a));
double tmp;
if ((z * t) <= -1e+131) {
tmp = t_1;
} else if ((z * t) <= 2e-42) {
tmp = fma(y, x, (b * a));
} else if ((z * t) <= 4e+44) {
tmp = fma(t, z, (y * x));
} else {
tmp = t_1;
}
return tmp;
}
function code(x, y, z, t, a, b) t_1 = fma(t, z, Float64(b * a)) tmp = 0.0 if (Float64(z * t) <= -1e+131) tmp = t_1; elseif (Float64(z * t) <= 2e-42) tmp = fma(y, x, Float64(b * a)); elseif (Float64(z * t) <= 4e+44) tmp = fma(t, z, Float64(y * x)); else tmp = t_1; end return tmp end
code[x_, y_, z_, t_, a_, b_] := Block[{t$95$1 = N[(t * z + N[(b * a), $MachinePrecision]), $MachinePrecision]}, If[LessEqual[N[(z * t), $MachinePrecision], -1e+131], t$95$1, If[LessEqual[N[(z * t), $MachinePrecision], 2e-42], N[(y * x + N[(b * a), $MachinePrecision]), $MachinePrecision], If[LessEqual[N[(z * t), $MachinePrecision], 4e+44], N[(t * z + N[(y * x), $MachinePrecision]), $MachinePrecision], t$95$1]]]]
\begin{array}{l}
\\
\begin{array}{l}
t_1 := \mathsf{fma}\left(t, z, b \cdot a\right)\\
\mathbf{if}\;z \cdot t \leq -1 \cdot 10^{+131}:\\
\;\;\;\;t\_1\\
\mathbf{elif}\;z \cdot t \leq 2 \cdot 10^{-42}:\\
\;\;\;\;\mathsf{fma}\left(y, x, b \cdot a\right)\\
\mathbf{elif}\;z \cdot t \leq 4 \cdot 10^{+44}:\\
\;\;\;\;\mathsf{fma}\left(t, z, y \cdot x\right)\\
\mathbf{else}:\\
\;\;\;\;t\_1\\
\end{array}
\end{array}
if (*.f64 z t) < -9.9999999999999991e130 or 4.0000000000000004e44 < (*.f64 z t) Initial program 98.8%
Taylor expanded in x around 0
+-commutativeN/A
lower-fma.f64N/A
*-commutativeN/A
lower-*.f6494.4
Applied rewrites94.4%
if -9.9999999999999991e130 < (*.f64 z t) < 2.00000000000000008e-42Initial program 100.0%
Taylor expanded in z around 0
+-commutativeN/A
*-commutativeN/A
lower-fma.f64N/A
*-commutativeN/A
lower-*.f6492.1
Applied rewrites92.1%
if 2.00000000000000008e-42 < (*.f64 z t) < 4.0000000000000004e44Initial program 99.9%
Taylor expanded in z around 0
+-commutativeN/A
*-commutativeN/A
lower-fma.f64N/A
*-commutativeN/A
lower-*.f6460.6
Applied rewrites60.6%
Taylor expanded in x around 0
Applied rewrites8.5%
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-*.f6492.2
Applied rewrites92.2%
(FPCore (x y z t a b) :precision binary64 (if (or (<= (* a b) -5e+139) (not (<= (* a b) 2e+52))) (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) <= -5e+139) || !((a * b) <= 2e+52)) {
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) <= -5e+139) || !(Float64(a * b) <= 2e+52)) 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], -5e+139], N[Not[LessEqual[N[(a * b), $MachinePrecision], 2e+52]], $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 -5 \cdot 10^{+139} \lor \neg \left(a \cdot b \leq 2 \cdot 10^{+52}\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) < -5.0000000000000003e139 or 2e52 < (*.f64 a b) Initial program 98.7%
Taylor expanded in x around 0
+-commutativeN/A
lower-fma.f64N/A
*-commutativeN/A
lower-*.f6492.6
Applied rewrites92.6%
if -5.0000000000000003e139 < (*.f64 a b) < 2e52Initial program 100.0%
Taylor expanded in z around 0
+-commutativeN/A
*-commutativeN/A
lower-fma.f64N/A
*-commutativeN/A
lower-*.f6460.3
Applied rewrites60.3%
Taylor expanded in x around 0
Applied rewrites11.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-*.f6490.1
Applied rewrites90.1%
Final simplification90.8%
(FPCore (x y z t a b) :precision binary64 (if (or (<= (* a b) -5e+139) (not (<= (* a b) 2e+94))) (* b a) (* t z)))
double code(double x, double y, double z, double t, double a, double b) {
double tmp;
if (((a * b) <= -5e+139) || !((a * b) <= 2e+94)) {
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+139)) .or. (.not. ((a * b) <= 2d+94))) 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+139) || !((a * b) <= 2e+94)) {
tmp = b * a;
} else {
tmp = t * z;
}
return tmp;
}
def code(x, y, z, t, a, b): tmp = 0 if ((a * b) <= -5e+139) or not ((a * b) <= 2e+94): 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+139) || !(Float64(a * b) <= 2e+94)) 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+139) || ~(((a * b) <= 2e+94))) 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+139], N[Not[LessEqual[N[(a * b), $MachinePrecision], 2e+94]], $MachinePrecision]], N[(b * a), $MachinePrecision], N[(t * z), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;a \cdot b \leq -5 \cdot 10^{+139} \lor \neg \left(a \cdot b \leq 2 \cdot 10^{+94}\right):\\
\;\;\;\;b \cdot a\\
\mathbf{else}:\\
\;\;\;\;t \cdot z\\
\end{array}
\end{array}
if (*.f64 a b) < -5.0000000000000003e139 or 2e94 < (*.f64 a b) Initial program 98.5%
Taylor expanded in z around 0
+-commutativeN/A
*-commutativeN/A
lower-fma.f64N/A
*-commutativeN/A
lower-*.f6490.3
Applied rewrites90.3%
Taylor expanded in x around 0
Applied rewrites83.5%
if -5.0000000000000003e139 < (*.f64 a b) < 2e94Initial program 100.0%
Taylor expanded in z around 0
+-commutativeN/A
*-commutativeN/A
lower-fma.f64N/A
*-commutativeN/A
lower-*.f6459.7
Applied rewrites59.7%
Taylor expanded in x around 0
Applied rewrites12.4%
Taylor expanded in z around inf
lower-*.f6442.5
Applied rewrites42.5%
Final simplification53.7%
(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 99.6%
Taylor expanded in x around 0
+-commutativeN/A
lower-fma.f64N/A
*-commutativeN/A
lower-*.f6463.5
Applied rewrites63.5%
(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 99.6%
Taylor expanded in z around 0
+-commutativeN/A
*-commutativeN/A
lower-fma.f64N/A
*-commutativeN/A
lower-*.f6468.1
Applied rewrites68.1%
Taylor expanded in x around 0
Applied rewrites31.8%
herbie shell --seed 2024326
(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)))