
(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);
}
module fmin_fmax_functions
implicit none
private
public fmax
public fmin
interface fmax
module procedure fmax88
module procedure fmax44
module procedure fmax84
module procedure fmax48
end interface
interface fmin
module procedure fmin88
module procedure fmin44
module procedure fmin84
module procedure fmin48
end interface
contains
real(8) function fmax88(x, y) result (res)
real(8), intent (in) :: x
real(8), intent (in) :: y
res = merge(y, merge(x, max(x, y), y /= y), x /= x)
end function
real(4) function fmax44(x, y) result (res)
real(4), intent (in) :: x
real(4), intent (in) :: y
res = merge(y, merge(x, max(x, y), y /= y), x /= x)
end function
real(8) function fmax84(x, y) result(res)
real(8), intent (in) :: x
real(4), intent (in) :: y
res = merge(dble(y), merge(x, max(x, dble(y)), y /= y), x /= x)
end function
real(8) function fmax48(x, y) result(res)
real(4), intent (in) :: x
real(8), intent (in) :: y
res = merge(y, merge(dble(x), max(dble(x), y), y /= y), x /= x)
end function
real(8) function fmin88(x, y) result (res)
real(8), intent (in) :: x
real(8), intent (in) :: y
res = merge(y, merge(x, min(x, y), y /= y), x /= x)
end function
real(4) function fmin44(x, y) result (res)
real(4), intent (in) :: x
real(4), intent (in) :: y
res = merge(y, merge(x, min(x, y), y /= y), x /= x)
end function
real(8) function fmin84(x, y) result(res)
real(8), intent (in) :: x
real(4), intent (in) :: y
res = merge(dble(y), merge(x, min(x, dble(y)), y /= y), x /= x)
end function
real(8) function fmin48(x, y) result(res)
real(4), intent (in) :: x
real(8), intent (in) :: y
res = merge(y, merge(dble(x), min(dble(x), y), y /= y), x /= x)
end function
end module
real(8) function code(x, y, z, t, a, b)
use fmin_fmax_functions
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);
}
module fmin_fmax_functions
implicit none
private
public fmax
public fmin
interface fmax
module procedure fmax88
module procedure fmax44
module procedure fmax84
module procedure fmax48
end interface
interface fmin
module procedure fmin88
module procedure fmin44
module procedure fmin84
module procedure fmin48
end interface
contains
real(8) function fmax88(x, y) result (res)
real(8), intent (in) :: x
real(8), intent (in) :: y
res = merge(y, merge(x, max(x, y), y /= y), x /= x)
end function
real(4) function fmax44(x, y) result (res)
real(4), intent (in) :: x
real(4), intent (in) :: y
res = merge(y, merge(x, max(x, y), y /= y), x /= x)
end function
real(8) function fmax84(x, y) result(res)
real(8), intent (in) :: x
real(4), intent (in) :: y
res = merge(dble(y), merge(x, max(x, dble(y)), y /= y), x /= x)
end function
real(8) function fmax48(x, y) result(res)
real(4), intent (in) :: x
real(8), intent (in) :: y
res = merge(y, merge(dble(x), max(dble(x), y), y /= y), x /= x)
end function
real(8) function fmin88(x, y) result (res)
real(8), intent (in) :: x
real(8), intent (in) :: y
res = merge(y, merge(x, min(x, y), y /= y), x /= x)
end function
real(4) function fmin44(x, y) result (res)
real(4), intent (in) :: x
real(4), intent (in) :: y
res = merge(y, merge(x, min(x, y), y /= y), x /= x)
end function
real(8) function fmin84(x, y) result(res)
real(8), intent (in) :: x
real(4), intent (in) :: y
res = merge(dble(y), merge(x, min(x, dble(y)), y /= y), x /= x)
end function
real(8) function fmin48(x, y) result(res)
real(4), intent (in) :: x
real(8), intent (in) :: y
res = merge(y, merge(dble(x), min(dble(x), y), y /= y), x /= x)
end function
end module
real(8) function code(x, y, z, t, a, b)
use fmin_fmax_functions
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) -1e-5) (not (<= (* a b) 5e+68))) (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) <= -1e-5) || !((a * b) <= 5e+68)) {
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) <= -1e-5) || !(Float64(a * b) <= 5e+68)) 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], -1e-5], N[Not[LessEqual[N[(a * b), $MachinePrecision], 5e+68]], $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 -1 \cdot 10^{-5} \lor \neg \left(a \cdot b \leq 5 \cdot 10^{+68}\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) < -1.00000000000000008e-5 or 5.0000000000000004e68 < (*.f64 a b) Initial program 97.0%
Taylor expanded in x around 0
+-commutativeN/A
lower-fma.f64N/A
*-commutativeN/A
lower-*.f6489.2
Applied rewrites89.2%
if -1.00000000000000008e-5 < (*.f64 a b) < 5.0000000000000004e68Initial program 98.7%
Taylor expanded in x around 0
+-commutativeN/A
lower-fma.f64N/A
*-commutativeN/A
lower-*.f6452.1
Applied rewrites52.1%
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
mul-1-negN/A
distribute-rgt-neg-inN/A
mul-1-negN/A
distribute-lft-neg-outN/A
remove-double-negN/A
*-commutativeN/A
lower-*.f6494.9
Applied rewrites94.9%
Final simplification92.6%
(FPCore (x y z t a b) :precision binary64 (if (or (<= (* z t) -1e-28) (not (<= (* z t) 1e-14))) (fma t z (* b a)) (fma b a (* y x))))
double code(double x, double y, double z, double t, double a, double b) {
double tmp;
if (((z * t) <= -1e-28) || !((z * t) <= 1e-14)) {
tmp = fma(t, z, (b * a));
} else {
tmp = fma(b, a, (y * x));
}
return tmp;
}
function code(x, y, z, t, a, b) tmp = 0.0 if ((Float64(z * t) <= -1e-28) || !(Float64(z * t) <= 1e-14)) tmp = fma(t, z, Float64(b * a)); else tmp = fma(b, a, Float64(y * x)); end return tmp end
code[x_, y_, z_, t_, a_, b_] := If[Or[LessEqual[N[(z * t), $MachinePrecision], -1e-28], N[Not[LessEqual[N[(z * t), $MachinePrecision], 1e-14]], $MachinePrecision]], N[(t * z + N[(b * a), $MachinePrecision]), $MachinePrecision], N[(b * a + N[(y * x), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;z \cdot t \leq -1 \cdot 10^{-28} \lor \neg \left(z \cdot t \leq 10^{-14}\right):\\
\;\;\;\;\mathsf{fma}\left(t, z, b \cdot a\right)\\
\mathbf{else}:\\
\;\;\;\;\mathsf{fma}\left(b, a, y \cdot x\right)\\
\end{array}
\end{array}
if (*.f64 z t) < -9.99999999999999971e-29 or 9.99999999999999999e-15 < (*.f64 z t) Initial program 97.2%
Taylor expanded in x around 0
+-commutativeN/A
lower-fma.f64N/A
*-commutativeN/A
lower-*.f6483.7
Applied rewrites83.7%
if -9.99999999999999971e-29 < (*.f64 z t) < 9.99999999999999999e-15Initial program 99.1%
Taylor expanded in z around 0
+-commutativeN/A
*-commutativeN/A
lower-fma.f64N/A
*-commutativeN/A
lower-*.f6496.2
Applied rewrites96.2%
Applied rewrites95.3%
Final simplification88.9%
(FPCore (x y z t a b) :precision binary64 (if (or (<= (* z t) -5e+105) (not (<= (* z t) 5e+138))) (* t z) (fma b a (* y x))))
double code(double x, double y, double z, double t, double a, double b) {
double tmp;
if (((z * t) <= -5e+105) || !((z * t) <= 5e+138)) {
tmp = t * z;
} else {
tmp = fma(b, a, (y * x));
}
return tmp;
}
function code(x, y, z, t, a, b) tmp = 0.0 if ((Float64(z * t) <= -5e+105) || !(Float64(z * t) <= 5e+138)) tmp = Float64(t * z); else tmp = fma(b, a, Float64(y * x)); end return tmp end
code[x_, y_, z_, t_, a_, b_] := If[Or[LessEqual[N[(z * t), $MachinePrecision], -5e+105], N[Not[LessEqual[N[(z * t), $MachinePrecision], 5e+138]], $MachinePrecision]], N[(t * z), $MachinePrecision], N[(b * a + N[(y * x), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;z \cdot t \leq -5 \cdot 10^{+105} \lor \neg \left(z \cdot t \leq 5 \cdot 10^{+138}\right):\\
\;\;\;\;t \cdot z\\
\mathbf{else}:\\
\;\;\;\;\mathsf{fma}\left(b, a, y \cdot x\right)\\
\end{array}
\end{array}
if (*.f64 z t) < -5.00000000000000046e105 or 5.00000000000000016e138 < (*.f64 z t) Initial program 95.4%
Taylor expanded in x around 0
+-commutativeN/A
lower-fma.f64N/A
*-commutativeN/A
lower-*.f6487.8
Applied rewrites87.8%
Taylor expanded in z around inf
lower-*.f6476.7
Applied rewrites76.7%
if -5.00000000000000046e105 < (*.f64 z t) < 5.00000000000000016e138Initial program 99.4%
Taylor expanded in z around 0
+-commutativeN/A
*-commutativeN/A
lower-fma.f64N/A
*-commutativeN/A
lower-*.f6483.9
Applied rewrites83.9%
Applied rewrites83.3%
Final simplification81.1%
(FPCore (x y z t a b) :precision binary64 (if (or (<= (* a b) -1e+70) (not (<= (* a b) 2e+118))) (* b a) (* t z)))
double code(double x, double y, double z, double t, double a, double b) {
double tmp;
if (((a * b) <= -1e+70) || !((a * b) <= 2e+118)) {
tmp = b * a;
} else {
tmp = t * z;
}
return tmp;
}
module fmin_fmax_functions
implicit none
private
public fmax
public fmin
interface fmax
module procedure fmax88
module procedure fmax44
module procedure fmax84
module procedure fmax48
end interface
interface fmin
module procedure fmin88
module procedure fmin44
module procedure fmin84
module procedure fmin48
end interface
contains
real(8) function fmax88(x, y) result (res)
real(8), intent (in) :: x
real(8), intent (in) :: y
res = merge(y, merge(x, max(x, y), y /= y), x /= x)
end function
real(4) function fmax44(x, y) result (res)
real(4), intent (in) :: x
real(4), intent (in) :: y
res = merge(y, merge(x, max(x, y), y /= y), x /= x)
end function
real(8) function fmax84(x, y) result(res)
real(8), intent (in) :: x
real(4), intent (in) :: y
res = merge(dble(y), merge(x, max(x, dble(y)), y /= y), x /= x)
end function
real(8) function fmax48(x, y) result(res)
real(4), intent (in) :: x
real(8), intent (in) :: y
res = merge(y, merge(dble(x), max(dble(x), y), y /= y), x /= x)
end function
real(8) function fmin88(x, y) result (res)
real(8), intent (in) :: x
real(8), intent (in) :: y
res = merge(y, merge(x, min(x, y), y /= y), x /= x)
end function
real(4) function fmin44(x, y) result (res)
real(4), intent (in) :: x
real(4), intent (in) :: y
res = merge(y, merge(x, min(x, y), y /= y), x /= x)
end function
real(8) function fmin84(x, y) result(res)
real(8), intent (in) :: x
real(4), intent (in) :: y
res = merge(dble(y), merge(x, min(x, dble(y)), y /= y), x /= x)
end function
real(8) function fmin48(x, y) result(res)
real(4), intent (in) :: x
real(8), intent (in) :: y
res = merge(y, merge(dble(x), min(dble(x), y), y /= y), x /= x)
end function
end module
real(8) function code(x, y, z, t, a, b)
use fmin_fmax_functions
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) <= (-1d+70)) .or. (.not. ((a * b) <= 2d+118))) 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) <= -1e+70) || !((a * b) <= 2e+118)) {
tmp = b * a;
} else {
tmp = t * z;
}
return tmp;
}
def code(x, y, z, t, a, b): tmp = 0 if ((a * b) <= -1e+70) or not ((a * b) <= 2e+118): tmp = b * a else: tmp = t * z return tmp
function code(x, y, z, t, a, b) tmp = 0.0 if ((Float64(a * b) <= -1e+70) || !(Float64(a * b) <= 2e+118)) 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) <= -1e+70) || ~(((a * b) <= 2e+118))) 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], -1e+70], N[Not[LessEqual[N[(a * b), $MachinePrecision], 2e+118]], $MachinePrecision]], N[(b * a), $MachinePrecision], N[(t * z), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;a \cdot b \leq -1 \cdot 10^{+70} \lor \neg \left(a \cdot b \leq 2 \cdot 10^{+118}\right):\\
\;\;\;\;b \cdot a\\
\mathbf{else}:\\
\;\;\;\;t \cdot z\\
\end{array}
\end{array}
if (*.f64 a b) < -1.00000000000000007e70 or 1.99999999999999993e118 < (*.f64 a b) Initial program 96.3%
Taylor expanded in z around 0
+-commutativeN/A
*-commutativeN/A
lower-fma.f64N/A
*-commutativeN/A
lower-*.f6489.6
Applied rewrites89.6%
Taylor expanded in x around 0
Applied rewrites81.9%
if -1.00000000000000007e70 < (*.f64 a b) < 1.99999999999999993e118Initial program 98.8%
Taylor expanded in x around 0
+-commutativeN/A
lower-fma.f64N/A
*-commutativeN/A
lower-*.f6455.1
Applied rewrites55.1%
Taylor expanded in z around inf
lower-*.f6447.7
Applied rewrites47.7%
Final simplification58.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;
}
module fmin_fmax_functions
implicit none
private
public fmax
public fmin
interface fmax
module procedure fmax88
module procedure fmax44
module procedure fmax84
module procedure fmax48
end interface
interface fmin
module procedure fmin88
module procedure fmin44
module procedure fmin84
module procedure fmin48
end interface
contains
real(8) function fmax88(x, y) result (res)
real(8), intent (in) :: x
real(8), intent (in) :: y
res = merge(y, merge(x, max(x, y), y /= y), x /= x)
end function
real(4) function fmax44(x, y) result (res)
real(4), intent (in) :: x
real(4), intent (in) :: y
res = merge(y, merge(x, max(x, y), y /= y), x /= x)
end function
real(8) function fmax84(x, y) result(res)
real(8), intent (in) :: x
real(4), intent (in) :: y
res = merge(dble(y), merge(x, max(x, dble(y)), y /= y), x /= x)
end function
real(8) function fmax48(x, y) result(res)
real(4), intent (in) :: x
real(8), intent (in) :: y
res = merge(y, merge(dble(x), max(dble(x), y), y /= y), x /= x)
end function
real(8) function fmin88(x, y) result (res)
real(8), intent (in) :: x
real(8), intent (in) :: y
res = merge(y, merge(x, min(x, y), y /= y), x /= x)
end function
real(4) function fmin44(x, y) result (res)
real(4), intent (in) :: x
real(4), intent (in) :: y
res = merge(y, merge(x, min(x, y), y /= y), x /= x)
end function
real(8) function fmin84(x, y) result(res)
real(8), intent (in) :: x
real(4), intent (in) :: y
res = merge(dble(y), merge(x, min(x, dble(y)), y /= y), x /= x)
end function
real(8) function fmin48(x, y) result(res)
real(4), intent (in) :: x
real(8), intent (in) :: y
res = merge(y, merge(dble(x), min(dble(x), y), y /= y), x /= x)
end function
end module
real(8) function code(x, y, z, t, a, b)
use fmin_fmax_functions
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-*.f6465.6
Applied rewrites65.6%
Taylor expanded in x around 0
Applied rewrites32.2%
herbie shell --seed 2024359
(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)))