
(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}
Herbie found 8 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
lift-+.f64N/A
lift-*.f64N/A
lower-fma.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)) (t_2 (* (fma y x z) y))) (if (<= t_1 -5e+227) t_2 (if (<= t_1 5e+158) (fma z y t) t_2))))
double code(double x, double y, double z, double t) {
double t_1 = ((x * y) + z) * y;
double t_2 = fma(y, x, z) * y;
double tmp;
if (t_1 <= -5e+227) {
tmp = t_2;
} else if (t_1 <= 5e+158) {
tmp = fma(z, y, t);
} else {
tmp = t_2;
}
return tmp;
}
function code(x, y, z, t) t_1 = Float64(Float64(Float64(x * y) + z) * y) t_2 = Float64(fma(y, x, z) * y) tmp = 0.0 if (t_1 <= -5e+227) tmp = t_2; elseif (t_1 <= 5e+158) tmp = fma(z, y, t); else tmp = t_2; end return tmp end
code[x_, y_, z_, t_] := Block[{t$95$1 = N[(N[(N[(x * y), $MachinePrecision] + z), $MachinePrecision] * y), $MachinePrecision]}, Block[{t$95$2 = N[(N[(y * x + z), $MachinePrecision] * y), $MachinePrecision]}, If[LessEqual[t$95$1, -5e+227], t$95$2, If[LessEqual[t$95$1, 5e+158], N[(z * y + t), $MachinePrecision], t$95$2]]]]
\begin{array}{l}
\\
\begin{array}{l}
t_1 := \left(x \cdot y + z\right) \cdot y\\
t_2 := \mathsf{fma}\left(y, x, z\right) \cdot y\\
\mathbf{if}\;t\_1 \leq -5 \cdot 10^{+227}:\\
\;\;\;\;t\_2\\
\mathbf{elif}\;t\_1 \leq 5 \cdot 10^{+158}:\\
\;\;\;\;\mathsf{fma}\left(z, y, t\right)\\
\mathbf{else}:\\
\;\;\;\;t\_2\\
\end{array}
\end{array}
if (*.f64 (+.f64 (*.f64 x y) z) y) < -4.9999999999999996e227 or 4.9999999999999996e158 < (*.f64 (+.f64 (*.f64 x y) z) y) Initial program 99.9%
Taylor expanded in t around 0
+-commutativeN/A
*-commutativeN/A
lower-*.f64N/A
*-commutativeN/A
lower-fma.f6496.5
Applied rewrites96.5%
if -4.9999999999999996e227 < (*.f64 (+.f64 (*.f64 x y) z) y) < 4.9999999999999996e158Initial program 99.9%
Taylor expanded in x around 0
+-commutativeN/A
*-commutativeN/A
lower-fma.f6484.6
Applied rewrites84.6%
(FPCore (x y z t)
:precision binary64
(let* ((t_1 (* (fma y x z) y)))
(if (<= y -4.85e+58)
t_1
(if (<= y -1.4e-52)
(fma (* y y) x t)
(if (<= y 8.5e-43) (fma z y t) t_1)))))
double code(double x, double y, double z, double t) {
double t_1 = fma(y, x, z) * y;
double tmp;
if (y <= -4.85e+58) {
tmp = t_1;
} else if (y <= -1.4e-52) {
tmp = fma((y * y), x, t);
} else if (y <= 8.5e-43) {
tmp = fma(z, y, t);
} else {
tmp = t_1;
}
return tmp;
}
function code(x, y, z, t) t_1 = Float64(fma(y, x, z) * y) tmp = 0.0 if (y <= -4.85e+58) tmp = t_1; elseif (y <= -1.4e-52) tmp = fma(Float64(y * y), x, t); elseif (y <= 8.5e-43) tmp = fma(z, y, t); else tmp = t_1; end return tmp end
code[x_, y_, z_, t_] := Block[{t$95$1 = N[(N[(y * x + z), $MachinePrecision] * y), $MachinePrecision]}, If[LessEqual[y, -4.85e+58], t$95$1, If[LessEqual[y, -1.4e-52], N[(N[(y * y), $MachinePrecision] * x + t), $MachinePrecision], If[LessEqual[y, 8.5e-43], N[(z * y + t), $MachinePrecision], t$95$1]]]]
\begin{array}{l}
\\
\begin{array}{l}
t_1 := \mathsf{fma}\left(y, x, z\right) \cdot y\\
\mathbf{if}\;y \leq -4.85 \cdot 10^{+58}:\\
\;\;\;\;t\_1\\
\mathbf{elif}\;y \leq -1.4 \cdot 10^{-52}:\\
\;\;\;\;\mathsf{fma}\left(y \cdot y, x, t\right)\\
\mathbf{elif}\;y \leq 8.5 \cdot 10^{-43}:\\
\;\;\;\;\mathsf{fma}\left(z, y, t\right)\\
\mathbf{else}:\\
\;\;\;\;t\_1\\
\end{array}
\end{array}
if y < -4.8499999999999998e58 or 8.50000000000000056e-43 < y Initial program 99.9%
Taylor expanded in t around 0
+-commutativeN/A
*-commutativeN/A
lower-*.f64N/A
*-commutativeN/A
lower-fma.f6488.7
Applied rewrites88.7%
if -4.8499999999999998e58 < y < -1.39999999999999997e-52Initial program 99.9%
Taylor expanded in z around 0
+-commutativeN/A
*-commutativeN/A
lower-fma.f64N/A
unpow2N/A
lower-*.f6466.9
Applied rewrites66.9%
if -1.39999999999999997e-52 < y < 8.50000000000000056e-43Initial program 100.0%
Taylor expanded in x around 0
+-commutativeN/A
*-commutativeN/A
lower-fma.f6492.7
Applied rewrites92.7%
(FPCore (x y z t) :precision binary64 (if (<= y -6.2e+25) (* (* x y) y) (if (<= y 3.8e+137) (fma z y t) (* (* y y) x))))
double code(double x, double y, double z, double t) {
double tmp;
if (y <= -6.2e+25) {
tmp = (x * y) * y;
} else if (y <= 3.8e+137) {
tmp = fma(z, y, t);
} else {
tmp = (y * y) * x;
}
return tmp;
}
function code(x, y, z, t) tmp = 0.0 if (y <= -6.2e+25) tmp = Float64(Float64(x * y) * y); elseif (y <= 3.8e+137) tmp = fma(z, y, t); else tmp = Float64(Float64(y * y) * x); end return tmp end
code[x_, y_, z_, t_] := If[LessEqual[y, -6.2e+25], N[(N[(x * y), $MachinePrecision] * y), $MachinePrecision], If[LessEqual[y, 3.8e+137], N[(z * y + t), $MachinePrecision], N[(N[(y * y), $MachinePrecision] * x), $MachinePrecision]]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;y \leq -6.2 \cdot 10^{+25}:\\
\;\;\;\;\left(x \cdot y\right) \cdot y\\
\mathbf{elif}\;y \leq 3.8 \cdot 10^{+137}:\\
\;\;\;\;\mathsf{fma}\left(z, y, t\right)\\
\mathbf{else}:\\
\;\;\;\;\left(y \cdot y\right) \cdot x\\
\end{array}
\end{array}
if y < -6.1999999999999996e25Initial program 99.9%
Taylor expanded in t around 0
+-commutativeN/A
*-commutativeN/A
lower-*.f64N/A
*-commutativeN/A
lower-fma.f6491.3
Applied rewrites91.3%
Taylor expanded in x around inf
lower-*.f6469.7
Applied rewrites69.7%
if -6.1999999999999996e25 < y < 3.79999999999999963e137Initial program 99.9%
Taylor expanded in x around 0
+-commutativeN/A
*-commutativeN/A
lower-fma.f6482.1
Applied rewrites82.1%
if 3.79999999999999963e137 < y Initial program 100.0%
Taylor expanded in x around inf
*-commutativeN/A
lower-*.f64N/A
unpow2N/A
lower-*.f6476.1
Applied rewrites76.1%
(FPCore (x y z t) :precision binary64 (let* ((t_1 (* (* x y) y))) (if (<= y -6.2e+25) t_1 (if (<= y 3.8e+137) (fma z y t) t_1))))
double code(double x, double y, double z, double t) {
double t_1 = (x * y) * y;
double tmp;
if (y <= -6.2e+25) {
tmp = t_1;
} else if (y <= 3.8e+137) {
tmp = fma(z, y, t);
} else {
tmp = t_1;
}
return tmp;
}
function code(x, y, z, t) t_1 = Float64(Float64(x * y) * y) tmp = 0.0 if (y <= -6.2e+25) tmp = t_1; elseif (y <= 3.8e+137) tmp = fma(z, y, t); else tmp = t_1; end return tmp end
code[x_, y_, z_, t_] := Block[{t$95$1 = N[(N[(x * y), $MachinePrecision] * y), $MachinePrecision]}, If[LessEqual[y, -6.2e+25], t$95$1, If[LessEqual[y, 3.8e+137], N[(z * y + t), $MachinePrecision], t$95$1]]]
\begin{array}{l}
\\
\begin{array}{l}
t_1 := \left(x \cdot y\right) \cdot y\\
\mathbf{if}\;y \leq -6.2 \cdot 10^{+25}:\\
\;\;\;\;t\_1\\
\mathbf{elif}\;y \leq 3.8 \cdot 10^{+137}:\\
\;\;\;\;\mathsf{fma}\left(z, y, t\right)\\
\mathbf{else}:\\
\;\;\;\;t\_1\\
\end{array}
\end{array}
if y < -6.1999999999999996e25 or 3.79999999999999963e137 < y Initial program 99.9%
Taylor expanded in t around 0
+-commutativeN/A
*-commutativeN/A
lower-*.f64N/A
*-commutativeN/A
lower-fma.f6493.0
Applied rewrites93.0%
Taylor expanded in x around inf
lower-*.f6473.8
Applied rewrites73.8%
if -6.1999999999999996e25 < y < 3.79999999999999963e137Initial program 99.9%
Taylor expanded in x around 0
+-commutativeN/A
*-commutativeN/A
lower-fma.f6482.1
Applied rewrites82.1%
(FPCore (x y z t) :precision binary64 (if (<= z -5.8e+75) (* z y) (if (<= z 8.5e+69) t (* z y))))
double code(double x, double y, double z, double t) {
double tmp;
if (z <= -5.8e+75) {
tmp = z * y;
} else if (z <= 8.5e+69) {
tmp = t;
} else {
tmp = z * y;
}
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)
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) :: tmp
if (z <= (-5.8d+75)) then
tmp = z * y
else if (z <= 8.5d+69) then
tmp = t
else
tmp = z * y
end if
code = tmp
end function
public static double code(double x, double y, double z, double t) {
double tmp;
if (z <= -5.8e+75) {
tmp = z * y;
} else if (z <= 8.5e+69) {
tmp = t;
} else {
tmp = z * y;
}
return tmp;
}
def code(x, y, z, t): tmp = 0 if z <= -5.8e+75: tmp = z * y elif z <= 8.5e+69: tmp = t else: tmp = z * y return tmp
function code(x, y, z, t) tmp = 0.0 if (z <= -5.8e+75) tmp = Float64(z * y); elseif (z <= 8.5e+69) tmp = t; else tmp = Float64(z * y); end return tmp end
function tmp_2 = code(x, y, z, t) tmp = 0.0; if (z <= -5.8e+75) tmp = z * y; elseif (z <= 8.5e+69) tmp = t; else tmp = z * y; end tmp_2 = tmp; end
code[x_, y_, z_, t_] := If[LessEqual[z, -5.8e+75], N[(z * y), $MachinePrecision], If[LessEqual[z, 8.5e+69], t, N[(z * y), $MachinePrecision]]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;z \leq -5.8 \cdot 10^{+75}:\\
\;\;\;\;z \cdot y\\
\mathbf{elif}\;z \leq 8.5 \cdot 10^{+69}:\\
\;\;\;\;t\\
\mathbf{else}:\\
\;\;\;\;z \cdot y\\
\end{array}
\end{array}
if z < -5.7999999999999997e75 or 8.5000000000000002e69 < z Initial program 100.0%
Taylor expanded in z around inf
*-commutativeN/A
lower-*.f6459.3
Applied rewrites59.3%
if -5.7999999999999997e75 < z < 8.5000000000000002e69Initial program 99.9%
Taylor expanded in y around 0
Applied rewrites47.3%
(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
+-commutativeN/A
*-commutativeN/A
lower-fma.f6465.9
Applied rewrites65.9%
(FPCore (x y z t) :precision binary64 t)
double code(double x, double y, double z, double t) {
return 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 = t
end function
public static double code(double x, double y, double z, double t) {
return t;
}
def code(x, y, z, t): return t
function code(x, y, z, t) return t end
function tmp = code(x, y, z, t) tmp = t; end
code[x_, y_, z_, t_] := t
\begin{array}{l}
\\
t
\end{array}
Initial program 99.9%
Taylor expanded in y around 0
Applied rewrites38.8%
herbie shell --seed 2025089
(FPCore (x y z t)
:name "Language.Haskell.HsColour.ColourHighlight:unbase from hscolour-1.23"
:precision binary64
(+ (* (+ (* x y) z) y) t))