
(FPCore (x y z t) :precision binary64 (+ x (* (- y z) (- t x))))
double code(double x, double y, double z, double t) {
return x + ((y - z) * (t - x));
}
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) * (t - x))
end function
public static double code(double x, double y, double z, double t) {
return x + ((y - z) * (t - x));
}
def code(x, y, z, t): return x + ((y - z) * (t - x))
function code(x, y, z, t) return Float64(x + Float64(Float64(y - z) * Float64(t - x))) end
function tmp = code(x, y, z, t) tmp = x + ((y - z) * (t - x)); end
code[x_, y_, z_, t_] := N[(x + N[(N[(y - z), $MachinePrecision] * N[(t - x), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
x + \left(y - z\right) \cdot \left(t - x\right)
\end{array}
Herbie found 9 alternatives:
| Alternative | Accuracy | Speedup |
|---|
(FPCore (x y z t) :precision binary64 (+ x (* (- y z) (- t x))))
double code(double x, double y, double z, double t) {
return x + ((y - z) * (t - x));
}
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) * (t - x))
end function
public static double code(double x, double y, double z, double t) {
return x + ((y - z) * (t - x));
}
def code(x, y, z, t): return x + ((y - z) * (t - x))
function code(x, y, z, t) return Float64(x + Float64(Float64(y - z) * Float64(t - x))) end
function tmp = code(x, y, z, t) tmp = x + ((y - z) * (t - x)); end
code[x_, y_, z_, t_] := N[(x + N[(N[(y - z), $MachinePrecision] * N[(t - x), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
x + \left(y - z\right) \cdot \left(t - x\right)
\end{array}
(FPCore (x y z t) :precision binary64 (fma (- z y) (- x t) x))
double code(double x, double y, double z, double t) {
return fma((z - y), (x - t), x);
}
function code(x, y, z, t) return fma(Float64(z - y), Float64(x - t), x) end
code[x_, y_, z_, t_] := N[(N[(z - y), $MachinePrecision] * N[(x - t), $MachinePrecision] + x), $MachinePrecision]
\begin{array}{l}
\\
\mathsf{fma}\left(z - y, x - t, x\right)
\end{array}
Initial program 100.0%
Applied rewrites100.0%
(FPCore (x y z t) :precision binary64 (if (<= z -3.5e+30) (* z (- x t)) (if (<= z 6.9e+41) (fma (- t x) y x) (fma (- z) t (* x z)))))
double code(double x, double y, double z, double t) {
double tmp;
if (z <= -3.5e+30) {
tmp = z * (x - t);
} else if (z <= 6.9e+41) {
tmp = fma((t - x), y, x);
} else {
tmp = fma(-z, t, (x * z));
}
return tmp;
}
function code(x, y, z, t) tmp = 0.0 if (z <= -3.5e+30) tmp = Float64(z * Float64(x - t)); elseif (z <= 6.9e+41) tmp = fma(Float64(t - x), y, x); else tmp = fma(Float64(-z), t, Float64(x * z)); end return tmp end
code[x_, y_, z_, t_] := If[LessEqual[z, -3.5e+30], N[(z * N[(x - t), $MachinePrecision]), $MachinePrecision], If[LessEqual[z, 6.9e+41], N[(N[(t - x), $MachinePrecision] * y + x), $MachinePrecision], N[((-z) * t + N[(x * z), $MachinePrecision]), $MachinePrecision]]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;z \leq -3.5 \cdot 10^{+30}:\\
\;\;\;\;z \cdot \left(x - t\right)\\
\mathbf{elif}\;z \leq 6.9 \cdot 10^{+41}:\\
\;\;\;\;\mathsf{fma}\left(t - x, y, x\right)\\
\mathbf{else}:\\
\;\;\;\;\mathsf{fma}\left(-z, t, x \cdot z\right)\\
\end{array}
\end{array}
if z < -3.50000000000000021e30Initial program 100.0%
Applied rewrites100.0%
Taylor expanded in z around inf
Applied rewrites44.9%
if -3.50000000000000021e30 < z < 6.9000000000000003e41Initial program 100.0%
Taylor expanded in z around 0
Applied rewrites60.3%
Applied rewrites60.3%
if 6.9000000000000003e41 < z Initial program 100.0%
Applied rewrites100.0%
Taylor expanded in z around inf
Applied rewrites44.9%
Applied rewrites43.7%
(FPCore (x y z t) :precision binary64 (if (<= z -3.5e+30) (* z (- x t)) (if (<= z 6.9e+41) (fma (- t x) y x) (fma z (- x t) x))))
double code(double x, double y, double z, double t) {
double tmp;
if (z <= -3.5e+30) {
tmp = z * (x - t);
} else if (z <= 6.9e+41) {
tmp = fma((t - x), y, x);
} else {
tmp = fma(z, (x - t), x);
}
return tmp;
}
function code(x, y, z, t) tmp = 0.0 if (z <= -3.5e+30) tmp = Float64(z * Float64(x - t)); elseif (z <= 6.9e+41) tmp = fma(Float64(t - x), y, x); else tmp = fma(z, Float64(x - t), x); end return tmp end
code[x_, y_, z_, t_] := If[LessEqual[z, -3.5e+30], N[(z * N[(x - t), $MachinePrecision]), $MachinePrecision], If[LessEqual[z, 6.9e+41], N[(N[(t - x), $MachinePrecision] * y + x), $MachinePrecision], N[(z * N[(x - t), $MachinePrecision] + x), $MachinePrecision]]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;z \leq -3.5 \cdot 10^{+30}:\\
\;\;\;\;z \cdot \left(x - t\right)\\
\mathbf{elif}\;z \leq 6.9 \cdot 10^{+41}:\\
\;\;\;\;\mathsf{fma}\left(t - x, y, x\right)\\
\mathbf{else}:\\
\;\;\;\;\mathsf{fma}\left(z, x - t, x\right)\\
\end{array}
\end{array}
if z < -3.50000000000000021e30Initial program 100.0%
Applied rewrites100.0%
Taylor expanded in z around inf
Applied rewrites44.9%
if -3.50000000000000021e30 < z < 6.9000000000000003e41Initial program 100.0%
Taylor expanded in z around 0
Applied rewrites60.3%
Applied rewrites60.3%
if 6.9000000000000003e41 < z Initial program 100.0%
Applied rewrites100.0%
Taylor expanded in y around 0
Applied rewrites60.6%
(FPCore (x y z t) :precision binary64 (let* ((t_1 (* z (- x t)))) (if (<= z -3.5e+30) t_1 (if (<= z 6.9e+41) (fma (- t x) y x) t_1))))
double code(double x, double y, double z, double t) {
double t_1 = z * (x - t);
double tmp;
if (z <= -3.5e+30) {
tmp = t_1;
} else if (z <= 6.9e+41) {
tmp = fma((t - x), y, x);
} else {
tmp = t_1;
}
return tmp;
}
function code(x, y, z, t) t_1 = Float64(z * Float64(x - t)) tmp = 0.0 if (z <= -3.5e+30) tmp = t_1; elseif (z <= 6.9e+41) tmp = fma(Float64(t - x), y, x); else tmp = t_1; end return tmp end
code[x_, y_, z_, t_] := Block[{t$95$1 = N[(z * N[(x - t), $MachinePrecision]), $MachinePrecision]}, If[LessEqual[z, -3.5e+30], t$95$1, If[LessEqual[z, 6.9e+41], N[(N[(t - x), $MachinePrecision] * y + x), $MachinePrecision], t$95$1]]]
\begin{array}{l}
\\
\begin{array}{l}
t_1 := z \cdot \left(x - t\right)\\
\mathbf{if}\;z \leq -3.5 \cdot 10^{+30}:\\
\;\;\;\;t\_1\\
\mathbf{elif}\;z \leq 6.9 \cdot 10^{+41}:\\
\;\;\;\;\mathsf{fma}\left(t - x, y, x\right)\\
\mathbf{else}:\\
\;\;\;\;t\_1\\
\end{array}
\end{array}
if z < -3.50000000000000021e30 or 6.9000000000000003e41 < z Initial program 100.0%
Applied rewrites100.0%
Taylor expanded in z around inf
Applied rewrites44.9%
if -3.50000000000000021e30 < z < 6.9000000000000003e41Initial program 100.0%
Taylor expanded in z around 0
Applied rewrites60.3%
Applied rewrites60.3%
(FPCore (x y z t)
:precision binary64
(let* ((t_1 (* z (- x t))))
(if (<= z -3.5e+30)
t_1
(if (<= z -1.2e-79) (* y (- t x)) (if (<= z 1e+41) (fma t y x) t_1)))))
double code(double x, double y, double z, double t) {
double t_1 = z * (x - t);
double tmp;
if (z <= -3.5e+30) {
tmp = t_1;
} else if (z <= -1.2e-79) {
tmp = y * (t - x);
} else if (z <= 1e+41) {
tmp = fma(t, y, x);
} else {
tmp = t_1;
}
return tmp;
}
function code(x, y, z, t) t_1 = Float64(z * Float64(x - t)) tmp = 0.0 if (z <= -3.5e+30) tmp = t_1; elseif (z <= -1.2e-79) tmp = Float64(y * Float64(t - x)); elseif (z <= 1e+41) tmp = fma(t, y, x); else tmp = t_1; end return tmp end
code[x_, y_, z_, t_] := Block[{t$95$1 = N[(z * N[(x - t), $MachinePrecision]), $MachinePrecision]}, If[LessEqual[z, -3.5e+30], t$95$1, If[LessEqual[z, -1.2e-79], N[(y * N[(t - x), $MachinePrecision]), $MachinePrecision], If[LessEqual[z, 1e+41], N[(t * y + x), $MachinePrecision], t$95$1]]]]
\begin{array}{l}
\\
\begin{array}{l}
t_1 := z \cdot \left(x - t\right)\\
\mathbf{if}\;z \leq -3.5 \cdot 10^{+30}:\\
\;\;\;\;t\_1\\
\mathbf{elif}\;z \leq -1.2 \cdot 10^{-79}:\\
\;\;\;\;y \cdot \left(t - x\right)\\
\mathbf{elif}\;z \leq 10^{+41}:\\
\;\;\;\;\mathsf{fma}\left(t, y, x\right)\\
\mathbf{else}:\\
\;\;\;\;t\_1\\
\end{array}
\end{array}
if z < -3.50000000000000021e30 or 1.00000000000000001e41 < z Initial program 100.0%
Applied rewrites100.0%
Taylor expanded in z around inf
Applied rewrites44.9%
if -3.50000000000000021e30 < z < -1.20000000000000003e-79Initial program 100.0%
Taylor expanded in y around inf
Applied rewrites44.5%
if -1.20000000000000003e-79 < z < 1.00000000000000001e41Initial program 100.0%
Taylor expanded in z around 0
Applied rewrites60.3%
Applied rewrites60.3%
Taylor expanded in x around 0
Applied rewrites42.1%
(FPCore (x y z t)
:precision binary64
(let* ((t_1 (* y (- t x))))
(if (<= y -1.0)
t_1
(if (<= y -4.4e-282) (fma t y x) (if (<= y 180.0) (* t (- y z)) t_1)))))
double code(double x, double y, double z, double t) {
double t_1 = y * (t - x);
double tmp;
if (y <= -1.0) {
tmp = t_1;
} else if (y <= -4.4e-282) {
tmp = fma(t, y, x);
} else if (y <= 180.0) {
tmp = t * (y - z);
} else {
tmp = t_1;
}
return tmp;
}
function code(x, y, z, t) t_1 = Float64(y * Float64(t - x)) tmp = 0.0 if (y <= -1.0) tmp = t_1; elseif (y <= -4.4e-282) tmp = fma(t, y, x); elseif (y <= 180.0) tmp = Float64(t * Float64(y - z)); else tmp = t_1; end return tmp end
code[x_, y_, z_, t_] := Block[{t$95$1 = N[(y * N[(t - x), $MachinePrecision]), $MachinePrecision]}, If[LessEqual[y, -1.0], t$95$1, If[LessEqual[y, -4.4e-282], N[(t * y + x), $MachinePrecision], If[LessEqual[y, 180.0], N[(t * N[(y - z), $MachinePrecision]), $MachinePrecision], t$95$1]]]]
\begin{array}{l}
\\
\begin{array}{l}
t_1 := y \cdot \left(t - x\right)\\
\mathbf{if}\;y \leq -1:\\
\;\;\;\;t\_1\\
\mathbf{elif}\;y \leq -4.4 \cdot 10^{-282}:\\
\;\;\;\;\mathsf{fma}\left(t, y, x\right)\\
\mathbf{elif}\;y \leq 180:\\
\;\;\;\;t \cdot \left(y - z\right)\\
\mathbf{else}:\\
\;\;\;\;t\_1\\
\end{array}
\end{array}
if y < -1 or 180 < y Initial program 100.0%
Taylor expanded in y around inf
Applied rewrites44.5%
if -1 < y < -4.39999999999999962e-282Initial program 100.0%
Taylor expanded in z around 0
Applied rewrites60.3%
Applied rewrites60.3%
Taylor expanded in x around 0
Applied rewrites42.1%
if -4.39999999999999962e-282 < y < 180Initial program 100.0%
Taylor expanded in x around 0
Applied rewrites48.7%
(FPCore (x y z t) :precision binary64 (let* ((t_1 (* t (- y z)))) (if (<= z -255000000.0) t_1 (if (<= z 5e+19) (fma t y x) t_1))))
double code(double x, double y, double z, double t) {
double t_1 = t * (y - z);
double tmp;
if (z <= -255000000.0) {
tmp = t_1;
} else if (z <= 5e+19) {
tmp = fma(t, y, x);
} else {
tmp = t_1;
}
return tmp;
}
function code(x, y, z, t) t_1 = Float64(t * Float64(y - z)) tmp = 0.0 if (z <= -255000000.0) tmp = t_1; elseif (z <= 5e+19) tmp = fma(t, y, x); else tmp = t_1; end return tmp end
code[x_, y_, z_, t_] := Block[{t$95$1 = N[(t * N[(y - z), $MachinePrecision]), $MachinePrecision]}, If[LessEqual[z, -255000000.0], t$95$1, If[LessEqual[z, 5e+19], N[(t * y + x), $MachinePrecision], t$95$1]]]
\begin{array}{l}
\\
\begin{array}{l}
t_1 := t \cdot \left(y - z\right)\\
\mathbf{if}\;z \leq -255000000:\\
\;\;\;\;t\_1\\
\mathbf{elif}\;z \leq 5 \cdot 10^{+19}:\\
\;\;\;\;\mathsf{fma}\left(t, y, x\right)\\
\mathbf{else}:\\
\;\;\;\;t\_1\\
\end{array}
\end{array}
if z < -2.55e8 or 5e19 < z Initial program 100.0%
Taylor expanded in x around 0
Applied rewrites48.7%
if -2.55e8 < z < 5e19Initial program 100.0%
Taylor expanded in z around 0
Applied rewrites60.3%
Applied rewrites60.3%
Taylor expanded in x around 0
Applied rewrites42.1%
(FPCore (x y z t) :precision binary64 (fma t y x))
double code(double x, double y, double z, double t) {
return fma(t, y, x);
}
function code(x, y, z, t) return fma(t, y, x) end
code[x_, y_, z_, t_] := N[(t * y + x), $MachinePrecision]
\begin{array}{l}
\\
\mathsf{fma}\left(t, y, x\right)
\end{array}
Initial program 100.0%
Taylor expanded in z around 0
Applied rewrites60.3%
Applied rewrites60.3%
Taylor expanded in x around 0
Applied rewrites42.1%
(FPCore (x y z t) :precision binary64 (* t y))
double code(double x, double y, double z, double t) {
return t * 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 = t * y
end function
public static double code(double x, double y, double z, double t) {
return t * y;
}
def code(x, y, z, t): return t * y
function code(x, y, z, t) return Float64(t * y) end
function tmp = code(x, y, z, t) tmp = t * y; end
code[x_, y_, z_, t_] := N[(t * y), $MachinePrecision]
\begin{array}{l}
\\
t \cdot y
\end{array}
Initial program 100.0%
Taylor expanded in y around inf
Applied rewrites44.5%
Taylor expanded in x around 0
Applied rewrites26.5%
herbie shell --seed 2025153
(FPCore (x y z t)
:name "Data.Metrics.Snapshot:quantile from metrics-0.3.0.2"
:precision binary64
(+ x (* (- y z) (- t x))))