
(FPCore (x y z t) :precision binary64 (+ (* (+ (* x y) z) y) t))
double code(double x, double y, double z, double t) {
return (((x * y) + z) * y) + t;
}
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)
use fmin_fmax_functions
real(8), intent (in) :: x
real(8), intent (in) :: y
real(8), intent (in) :: z
real(8), intent (in) :: t
code = (((x * y) + z) * y) + t
end function
public static double code(double x, double y, double z, double t) {
return (((x * y) + z) * y) + t;
}
def code(x, y, z, t): return (((x * y) + z) * y) + t
function code(x, y, z, t) return Float64(Float64(Float64(Float64(x * y) + z) * y) + t) end
function tmp = code(x, y, z, t) tmp = (((x * y) + z) * y) + t; end
code[x_, y_, z_, t_] := N[(N[(N[(N[(x * y), $MachinePrecision] + z), $MachinePrecision] * y), $MachinePrecision] + t), $MachinePrecision]
\begin{array}{l}
\\
\left(x \cdot y + z\right) \cdot y + t
\end{array}
Sampling outcomes in binary64 precision:
Herbie found 7 alternatives:
| Alternative | Accuracy | Speedup |
|---|
(FPCore (x y z t) :precision binary64 (+ (* (+ (* x y) z) y) t))
double code(double x, double y, double z, double t) {
return (((x * y) + z) * y) + t;
}
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)
use fmin_fmax_functions
real(8), intent (in) :: x
real(8), intent (in) :: y
real(8), intent (in) :: z
real(8), intent (in) :: t
code = (((x * y) + z) * y) + t
end function
public static double code(double x, double y, double z, double t) {
return (((x * y) + z) * y) + t;
}
def code(x, y, z, t): return (((x * y) + z) * y) + t
function code(x, y, z, t) return Float64(Float64(Float64(Float64(x * y) + z) * y) + t) end
function tmp = code(x, y, z, t) tmp = (((x * y) + z) * y) + t; end
code[x_, y_, z_, t_] := N[(N[(N[(N[(x * y), $MachinePrecision] + z), $MachinePrecision] * y), $MachinePrecision] + t), $MachinePrecision]
\begin{array}{l}
\\
\left(x \cdot y + z\right) \cdot y + t
\end{array}
(FPCore (x y z t) :precision binary64 (fma (fma y x z) y t))
double code(double x, double y, double z, double t) {
return fma(fma(y, x, z), y, t);
}
function code(x, y, z, t) return fma(fma(y, x, z), y, t) end
code[x_, y_, z_, t_] := N[(N[(y * x + z), $MachinePrecision] * y + t), $MachinePrecision]
\begin{array}{l}
\\
\mathsf{fma}\left(\mathsf{fma}\left(y, x, z\right), y, t\right)
\end{array}
Initial program 99.9%
lift-+.f64N/A
lift-*.f64N/A
lower-fma.f6499.9
lift-+.f64N/A
lift-*.f64N/A
*-commutativeN/A
lower-fma.f6499.9
Applied rewrites99.9%
(FPCore (x y z t)
:precision binary64
(let* ((t_1 (* (+ (* x y) z) y)))
(if (or (<= t_1 -5e+36) (not (<= t_1 1e+219)))
(* (fma y x z) y)
(fma z y t))))
double code(double x, double y, double z, double t) {
double t_1 = ((x * y) + z) * y;
double tmp;
if ((t_1 <= -5e+36) || !(t_1 <= 1e+219)) {
tmp = fma(y, x, z) * y;
} else {
tmp = fma(z, y, t);
}
return tmp;
}
function code(x, y, z, t) t_1 = Float64(Float64(Float64(x * y) + z) * y) tmp = 0.0 if ((t_1 <= -5e+36) || !(t_1 <= 1e+219)) tmp = Float64(fma(y, x, z) * y); else tmp = fma(z, y, t); end return tmp end
code[x_, y_, z_, t_] := Block[{t$95$1 = N[(N[(N[(x * y), $MachinePrecision] + z), $MachinePrecision] * y), $MachinePrecision]}, If[Or[LessEqual[t$95$1, -5e+36], N[Not[LessEqual[t$95$1, 1e+219]], $MachinePrecision]], N[(N[(y * x + z), $MachinePrecision] * y), $MachinePrecision], N[(z * y + t), $MachinePrecision]]]
\begin{array}{l}
\\
\begin{array}{l}
t_1 := \left(x \cdot y + z\right) \cdot y\\
\mathbf{if}\;t\_1 \leq -5 \cdot 10^{+36} \lor \neg \left(t\_1 \leq 10^{+219}\right):\\
\;\;\;\;\mathsf{fma}\left(y, x, z\right) \cdot y\\
\mathbf{else}:\\
\;\;\;\;\mathsf{fma}\left(z, y, t\right)\\
\end{array}
\end{array}
if (*.f64 (+.f64 (*.f64 x y) z) y) < -4.99999999999999977e36 or 9.99999999999999965e218 < (*.f64 (+.f64 (*.f64 x y) z) y) Initial program 99.9%
Taylor expanded in y around inf
Applied rewrites94.4%
if -4.99999999999999977e36 < (*.f64 (+.f64 (*.f64 x y) z) y) < 9.99999999999999965e218Initial program 99.9%
Taylor expanded in x around 0
Applied rewrites87.9%
Final simplification91.0%
(FPCore (x y z t) :precision binary64 (if (<= z -2e+110) (* (fma y x z) y) (if (<= z 7.2e+51) (fma (* x y) y t) (fma z y t))))
double code(double x, double y, double z, double t) {
double tmp;
if (z <= -2e+110) {
tmp = fma(y, x, z) * y;
} else if (z <= 7.2e+51) {
tmp = fma((x * y), y, t);
} else {
tmp = fma(z, y, t);
}
return tmp;
}
function code(x, y, z, t) tmp = 0.0 if (z <= -2e+110) tmp = Float64(fma(y, x, z) * y); elseif (z <= 7.2e+51) tmp = fma(Float64(x * y), y, t); else tmp = fma(z, y, t); end return tmp end
code[x_, y_, z_, t_] := If[LessEqual[z, -2e+110], N[(N[(y * x + z), $MachinePrecision] * y), $MachinePrecision], If[LessEqual[z, 7.2e+51], N[(N[(x * y), $MachinePrecision] * y + t), $MachinePrecision], N[(z * y + t), $MachinePrecision]]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;z \leq -2 \cdot 10^{+110}:\\
\;\;\;\;\mathsf{fma}\left(y, x, z\right) \cdot y\\
\mathbf{elif}\;z \leq 7.2 \cdot 10^{+51}:\\
\;\;\;\;\mathsf{fma}\left(x \cdot y, y, t\right)\\
\mathbf{else}:\\
\;\;\;\;\mathsf{fma}\left(z, y, t\right)\\
\end{array}
\end{array}
if z < -2e110Initial program 100.0%
Taylor expanded in y around inf
Applied rewrites90.5%
if -2e110 < z < 7.20000000000000022e51Initial program 99.9%
lift-+.f64N/A
lift-*.f64N/A
lower-fma.f6499.9
lift-+.f64N/A
lift-*.f64N/A
*-commutativeN/A
lower-fma.f6499.9
Applied rewrites99.9%
Taylor expanded in x around inf
lower-*.f6495.0
Applied rewrites95.0%
if 7.20000000000000022e51 < z Initial program 100.0%
Taylor expanded in x around 0
Applied rewrites89.5%
(FPCore (x y z t) :precision binary64 (if (or (<= y -2.8e+51) (not (<= y 1.48e+138))) (* (* x y) y) (fma z y t)))
double code(double x, double y, double z, double t) {
double tmp;
if ((y <= -2.8e+51) || !(y <= 1.48e+138)) {
tmp = (x * y) * y;
} else {
tmp = fma(z, y, t);
}
return tmp;
}
function code(x, y, z, t) tmp = 0.0 if ((y <= -2.8e+51) || !(y <= 1.48e+138)) tmp = Float64(Float64(x * y) * y); else tmp = fma(z, y, t); end return tmp end
code[x_, y_, z_, t_] := If[Or[LessEqual[y, -2.8e+51], N[Not[LessEqual[y, 1.48e+138]], $MachinePrecision]], N[(N[(x * y), $MachinePrecision] * y), $MachinePrecision], N[(z * y + t), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;y \leq -2.8 \cdot 10^{+51} \lor \neg \left(y \leq 1.48 \cdot 10^{+138}\right):\\
\;\;\;\;\left(x \cdot y\right) \cdot y\\
\mathbf{else}:\\
\;\;\;\;\mathsf{fma}\left(z, y, t\right)\\
\end{array}
\end{array}
if y < -2.80000000000000005e51 or 1.48000000000000008e138 < y Initial program 99.9%
Taylor expanded in y around inf
Applied rewrites93.9%
Taylor expanded in x around inf
Applied rewrites70.4%
if -2.80000000000000005e51 < y < 1.48000000000000008e138Initial program 99.9%
Taylor expanded in x around 0
Applied rewrites82.8%
Final simplification78.2%
(FPCore (x y z t) :precision binary64 (if (<= y -2.8e+51) (* (* y y) x) (if (<= y 1.48e+138) (fma z y t) (* (* x y) y))))
double code(double x, double y, double z, double t) {
double tmp;
if (y <= -2.8e+51) {
tmp = (y * y) * x;
} else if (y <= 1.48e+138) {
tmp = fma(z, y, t);
} else {
tmp = (x * y) * y;
}
return tmp;
}
function code(x, y, z, t) tmp = 0.0 if (y <= -2.8e+51) tmp = Float64(Float64(y * y) * x); elseif (y <= 1.48e+138) tmp = fma(z, y, t); else tmp = Float64(Float64(x * y) * y); end return tmp end
code[x_, y_, z_, t_] := If[LessEqual[y, -2.8e+51], N[(N[(y * y), $MachinePrecision] * x), $MachinePrecision], If[LessEqual[y, 1.48e+138], N[(z * y + t), $MachinePrecision], N[(N[(x * y), $MachinePrecision] * y), $MachinePrecision]]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;y \leq -2.8 \cdot 10^{+51}:\\
\;\;\;\;\left(y \cdot y\right) \cdot x\\
\mathbf{elif}\;y \leq 1.48 \cdot 10^{+138}:\\
\;\;\;\;\mathsf{fma}\left(z, y, t\right)\\
\mathbf{else}:\\
\;\;\;\;\left(x \cdot y\right) \cdot y\\
\end{array}
\end{array}
if y < -2.80000000000000005e51Initial program 99.9%
Taylor expanded in x around inf
*-commutativeN/A
lower-*.f64N/A
unpow2N/A
lower-*.f6468.7
Applied rewrites68.7%
if -2.80000000000000005e51 < y < 1.48000000000000008e138Initial program 99.9%
Taylor expanded in x around 0
Applied rewrites82.8%
if 1.48000000000000008e138 < y Initial program 99.9%
Taylor expanded in y around inf
Applied rewrites96.7%
Taylor expanded in x around inf
Applied rewrites80.8%
(FPCore (x y z t) :precision binary64 (fma z y t))
double code(double x, double y, double z, double t) {
return fma(z, y, t);
}
function code(x, y, z, t) return fma(z, y, t) end
code[x_, y_, z_, t_] := N[(z * y + t), $MachinePrecision]
\begin{array}{l}
\\
\mathsf{fma}\left(z, y, t\right)
\end{array}
Initial program 99.9%
Taylor expanded in x around 0
Applied rewrites66.9%
(FPCore (x y z t) :precision binary64 (* z y))
double code(double x, double y, double z, double t) {
return z * y;
}
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)
use fmin_fmax_functions
real(8), intent (in) :: x
real(8), intent (in) :: y
real(8), intent (in) :: z
real(8), intent (in) :: t
code = z * y
end function
public static double code(double x, double y, double z, double t) {
return z * y;
}
def code(x, y, z, t): return z * y
function code(x, y, z, t) return Float64(z * y) end
function tmp = code(x, y, z, t) tmp = z * y; end
code[x_, y_, z_, t_] := N[(z * y), $MachinePrecision]
\begin{array}{l}
\\
z \cdot y
\end{array}
Initial program 99.9%
Taylor expanded in y around inf
Applied rewrites62.8%
Taylor expanded in x around 0
Applied rewrites30.2%
herbie shell --seed 2024363
(FPCore (x y z t)
:name "Language.Haskell.HsColour.ColourHighlight:unbase from hscolour-1.23"
:precision binary64
(+ (* (+ (* x y) z) y) t))