
(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.4%
lift-+.f64N/A
lift-+.f64N/A
associate-+l+N/A
lift-*.f64N/A
*-commutativeN/A
lower-fma.f64N/A
lift-*.f64N/A
lower-fma.f6499.6
Applied rewrites99.6%
(FPCore (x y z t a b) :precision binary64 (if (<= (* a b) -1e+25) (fma a b (* z t)) (if (<= (* a b) 1e+29) (fma y x (* z t)) (fma a b (* y x)))))
double code(double x, double y, double z, double t, double a, double b) {
double tmp;
if ((a * b) <= -1e+25) {
tmp = fma(a, b, (z * t));
} else if ((a * b) <= 1e+29) {
tmp = fma(y, x, (z * t));
} else {
tmp = fma(a, b, (y * x));
}
return tmp;
}
function code(x, y, z, t, a, b) tmp = 0.0 if (Float64(a * b) <= -1e+25) tmp = fma(a, b, Float64(z * t)); elseif (Float64(a * b) <= 1e+29) tmp = fma(y, x, Float64(z * t)); else tmp = fma(a, b, Float64(y * x)); end return tmp end
code[x_, y_, z_, t_, a_, b_] := If[LessEqual[N[(a * b), $MachinePrecision], -1e+25], N[(a * b + N[(z * t), $MachinePrecision]), $MachinePrecision], If[LessEqual[N[(a * b), $MachinePrecision], 1e+29], N[(y * x + N[(z * t), $MachinePrecision]), $MachinePrecision], N[(a * b + N[(y * x), $MachinePrecision]), $MachinePrecision]]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;a \cdot b \leq -1 \cdot 10^{+25}:\\
\;\;\;\;\mathsf{fma}\left(a, b, z \cdot t\right)\\
\mathbf{elif}\;a \cdot b \leq 10^{+29}:\\
\;\;\;\;\mathsf{fma}\left(y, x, z \cdot t\right)\\
\mathbf{else}:\\
\;\;\;\;\mathsf{fma}\left(a, b, y \cdot x\right)\\
\end{array}
\end{array}
if (*.f64 a b) < -1.00000000000000009e25Initial program 98.4%
Taylor expanded in x around 0
lower-fma.f64N/A
lower-*.f6481.7
Applied rewrites81.7%
if -1.00000000000000009e25 < (*.f64 a b) < 9.99999999999999914e28Initial program 99.3%
lift-+.f64N/A
lift-+.f64N/A
associate-+l+N/A
lift-*.f64N/A
*-commutativeN/A
lower-fma.f64N/A
lift-*.f64N/A
lower-fma.f64100.0
Applied rewrites100.0%
Taylor expanded in z around inf
lower-*.f6492.5
Applied rewrites92.5%
if 9.99999999999999914e28 < (*.f64 a b) Initial program 96.4%
Taylor expanded in z around 0
lower-fma.f64N/A
lower-*.f6488.2
Applied rewrites88.2%
Final simplification89.0%
(FPCore (x y z t a b) :precision binary64 (if (<= (* a b) -1e+25) (fma a b (* z t)) (if (<= (* a b) 1e+29) (fma t z (* y x)) (fma a b (* y x)))))
double code(double x, double y, double z, double t, double a, double b) {
double tmp;
if ((a * b) <= -1e+25) {
tmp = fma(a, b, (z * t));
} else if ((a * b) <= 1e+29) {
tmp = fma(t, z, (y * x));
} else {
tmp = fma(a, b, (y * x));
}
return tmp;
}
function code(x, y, z, t, a, b) tmp = 0.0 if (Float64(a * b) <= -1e+25) tmp = fma(a, b, Float64(z * t)); elseif (Float64(a * b) <= 1e+29) tmp = fma(t, z, Float64(y * x)); else tmp = fma(a, b, Float64(y * x)); end return tmp end
code[x_, y_, z_, t_, a_, b_] := If[LessEqual[N[(a * b), $MachinePrecision], -1e+25], N[(a * b + N[(z * t), $MachinePrecision]), $MachinePrecision], If[LessEqual[N[(a * b), $MachinePrecision], 1e+29], N[(t * z + N[(y * x), $MachinePrecision]), $MachinePrecision], N[(a * b + N[(y * x), $MachinePrecision]), $MachinePrecision]]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;a \cdot b \leq -1 \cdot 10^{+25}:\\
\;\;\;\;\mathsf{fma}\left(a, b, z \cdot t\right)\\
\mathbf{elif}\;a \cdot b \leq 10^{+29}:\\
\;\;\;\;\mathsf{fma}\left(t, z, y \cdot x\right)\\
\mathbf{else}:\\
\;\;\;\;\mathsf{fma}\left(a, b, y \cdot x\right)\\
\end{array}
\end{array}
if (*.f64 a b) < -1.00000000000000009e25Initial program 98.4%
Taylor expanded in x around 0
lower-fma.f64N/A
lower-*.f6481.7
Applied rewrites81.7%
if -1.00000000000000009e25 < (*.f64 a b) < 9.99999999999999914e28Initial program 99.3%
Taylor expanded in a around 0
lower-fma.f64N/A
lower-*.f6491.8
Applied rewrites91.8%
if 9.99999999999999914e28 < (*.f64 a b) Initial program 96.4%
Taylor expanded in z around 0
lower-fma.f64N/A
lower-*.f6488.2
Applied rewrites88.2%
Final simplification88.6%
(FPCore (x y z t a b)
:precision binary64
(let* ((t_1 (fma a b (* z t))))
(if (<= (* z t) -1e+34)
t_1
(if (<= (* z t) 1e+110) (fma a b (* y x)) t_1))))
double code(double x, double y, double z, double t, double a, double b) {
double t_1 = fma(a, b, (z * t));
double tmp;
if ((z * t) <= -1e+34) {
tmp = t_1;
} else if ((z * t) <= 1e+110) {
tmp = fma(a, b, (y * x));
} else {
tmp = t_1;
}
return tmp;
}
function code(x, y, z, t, a, b) t_1 = fma(a, b, Float64(z * t)) tmp = 0.0 if (Float64(z * t) <= -1e+34) tmp = t_1; elseif (Float64(z * t) <= 1e+110) tmp = fma(a, b, Float64(y * x)); else tmp = t_1; end return tmp end
code[x_, y_, z_, t_, a_, b_] := Block[{t$95$1 = N[(a * b + N[(z * t), $MachinePrecision]), $MachinePrecision]}, If[LessEqual[N[(z * t), $MachinePrecision], -1e+34], t$95$1, If[LessEqual[N[(z * t), $MachinePrecision], 1e+110], N[(a * b + N[(y * x), $MachinePrecision]), $MachinePrecision], t$95$1]]]
\begin{array}{l}
\\
\begin{array}{l}
t_1 := \mathsf{fma}\left(a, b, z \cdot t\right)\\
\mathbf{if}\;z \cdot t \leq -1 \cdot 10^{+34}:\\
\;\;\;\;t\_1\\
\mathbf{elif}\;z \cdot t \leq 10^{+110}:\\
\;\;\;\;\mathsf{fma}\left(a, b, y \cdot x\right)\\
\mathbf{else}:\\
\;\;\;\;t\_1\\
\end{array}
\end{array}
if (*.f64 z t) < -9.99999999999999946e33 or 1e110 < (*.f64 z t) Initial program 97.9%
Taylor expanded in x around 0
lower-fma.f64N/A
lower-*.f6487.1
Applied rewrites87.1%
if -9.99999999999999946e33 < (*.f64 z t) < 1e110Initial program 98.7%
Taylor expanded in z around 0
lower-fma.f64N/A
lower-*.f6486.8
Applied rewrites86.8%
Final simplification86.9%
(FPCore (x y z t a b) :precision binary64 (if (<= (* y x) -6.3e+109) (* y x) (if (<= (* y x) 5.8e+76) (fma a b (* z t)) (* y x))))
double code(double x, double y, double z, double t, double a, double b) {
double tmp;
if ((y * x) <= -6.3e+109) {
tmp = y * x;
} else if ((y * x) <= 5.8e+76) {
tmp = fma(a, b, (z * t));
} else {
tmp = y * x;
}
return tmp;
}
function code(x, y, z, t, a, b) tmp = 0.0 if (Float64(y * x) <= -6.3e+109) tmp = Float64(y * x); elseif (Float64(y * x) <= 5.8e+76) tmp = fma(a, b, Float64(z * t)); else tmp = Float64(y * x); end return tmp end
code[x_, y_, z_, t_, a_, b_] := If[LessEqual[N[(y * x), $MachinePrecision], -6.3e+109], N[(y * x), $MachinePrecision], If[LessEqual[N[(y * x), $MachinePrecision], 5.8e+76], N[(a * b + N[(z * t), $MachinePrecision]), $MachinePrecision], N[(y * x), $MachinePrecision]]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;y \cdot x \leq -6.3 \cdot 10^{+109}:\\
\;\;\;\;y \cdot x\\
\mathbf{elif}\;y \cdot x \leq 5.8 \cdot 10^{+76}:\\
\;\;\;\;\mathsf{fma}\left(a, b, z \cdot t\right)\\
\mathbf{else}:\\
\;\;\;\;y \cdot x\\
\end{array}
\end{array}
if (*.f64 x y) < -6.29999999999999961e109 or 5.8000000000000003e76 < (*.f64 x y) Initial program 96.2%
Taylor expanded in x around inf
lower-*.f6474.9
Applied rewrites74.9%
if -6.29999999999999961e109 < (*.f64 x y) < 5.8000000000000003e76Initial program 99.4%
Taylor expanded in x around 0
lower-fma.f64N/A
lower-*.f6484.5
Applied rewrites84.5%
Final simplification81.5%
(FPCore (x y z t a b) :precision binary64 (if (<= (* a b) -1.55e+59) (* a b) (if (<= (* a b) 3e+29) (* z t) (* a b))))
double code(double x, double y, double z, double t, double a, double b) {
double tmp;
if ((a * b) <= -1.55e+59) {
tmp = a * b;
} else if ((a * b) <= 3e+29) {
tmp = z * t;
} else {
tmp = a * b;
}
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) <= (-1.55d+59)) then
tmp = a * b
else if ((a * b) <= 3d+29) then
tmp = z * t
else
tmp = a * b
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) <= -1.55e+59) {
tmp = a * b;
} else if ((a * b) <= 3e+29) {
tmp = z * t;
} else {
tmp = a * b;
}
return tmp;
}
def code(x, y, z, t, a, b): tmp = 0 if (a * b) <= -1.55e+59: tmp = a * b elif (a * b) <= 3e+29: tmp = z * t else: tmp = a * b return tmp
function code(x, y, z, t, a, b) tmp = 0.0 if (Float64(a * b) <= -1.55e+59) tmp = Float64(a * b); elseif (Float64(a * b) <= 3e+29) tmp = Float64(z * t); else tmp = Float64(a * b); end return tmp end
function tmp_2 = code(x, y, z, t, a, b) tmp = 0.0; if ((a * b) <= -1.55e+59) tmp = a * b; elseif ((a * b) <= 3e+29) tmp = z * t; else tmp = a * b; end tmp_2 = tmp; end
code[x_, y_, z_, t_, a_, b_] := If[LessEqual[N[(a * b), $MachinePrecision], -1.55e+59], N[(a * b), $MachinePrecision], If[LessEqual[N[(a * b), $MachinePrecision], 3e+29], N[(z * t), $MachinePrecision], N[(a * b), $MachinePrecision]]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;a \cdot b \leq -1.55 \cdot 10^{+59}:\\
\;\;\;\;a \cdot b\\
\mathbf{elif}\;a \cdot b \leq 3 \cdot 10^{+29}:\\
\;\;\;\;z \cdot t\\
\mathbf{else}:\\
\;\;\;\;a \cdot b\\
\end{array}
\end{array}
if (*.f64 a b) < -1.55000000000000007e59 or 2.9999999999999999e29 < (*.f64 a b) Initial program 97.2%
Taylor expanded in a around inf
lower-*.f6468.2
Applied rewrites68.2%
if -1.55000000000000007e59 < (*.f64 a b) < 2.9999999999999999e29Initial program 99.3%
Taylor expanded in z around inf
lower-*.f6450.2
Applied rewrites50.2%
Final simplification57.7%
(FPCore (x y z t a b) :precision binary64 (* a b))
double code(double x, double y, double z, double t, double a, double b) {
return 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 = a * b
end function
public static double code(double x, double y, double z, double t, double a, double b) {
return a * b;
}
def code(x, y, z, t, a, b): return a * b
function code(x, y, z, t, a, b) return Float64(a * b) end
function tmp = code(x, y, z, t, a, b) tmp = a * b; end
code[x_, y_, z_, t_, a_, b_] := N[(a * b), $MachinePrecision]
\begin{array}{l}
\\
a \cdot b
\end{array}
Initial program 98.4%
Taylor expanded in a around inf
lower-*.f6434.8
Applied rewrites34.8%
herbie shell --seed 2024238
(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)))