
(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 8 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%
lift-+.f64N/A
+-commutativeN/A
lift-*.f64N/A
lift--.f64N/A
sub-negate-revN/A
distribute-lft-neg-outN/A
distribute-rgt-neg-outN/A
lift--.f64N/A
sub-negate-revN/A
lower-fma.f64N/A
lower--.f64N/A
lower--.f64100.0
Applied rewrites100.0%
(FPCore (x y z t) :precision binary64 (if (<= z -9.5e+96) (* z (- x t)) (if (<= z 9e+31) (fma (- t x) y x) (fma (- z) t (* z x)))))
double code(double x, double y, double z, double t) {
double tmp;
if (z <= -9.5e+96) {
tmp = z * (x - t);
} else if (z <= 9e+31) {
tmp = fma((t - x), y, x);
} else {
tmp = fma(-z, t, (z * x));
}
return tmp;
}
function code(x, y, z, t) tmp = 0.0 if (z <= -9.5e+96) tmp = Float64(z * Float64(x - t)); elseif (z <= 9e+31) tmp = fma(Float64(t - x), y, x); else tmp = fma(Float64(-z), t, Float64(z * x)); end return tmp end
code[x_, y_, z_, t_] := If[LessEqual[z, -9.5e+96], N[(z * N[(x - t), $MachinePrecision]), $MachinePrecision], If[LessEqual[z, 9e+31], N[(N[(t - x), $MachinePrecision] * y + x), $MachinePrecision], N[((-z) * t + N[(z * x), $MachinePrecision]), $MachinePrecision]]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;z \leq -9.5 \cdot 10^{+96}:\\
\;\;\;\;z \cdot \left(x - t\right)\\
\mathbf{elif}\;z \leq 9 \cdot 10^{+31}:\\
\;\;\;\;\mathsf{fma}\left(t - x, y, x\right)\\
\mathbf{else}:\\
\;\;\;\;\mathsf{fma}\left(-z, t, z \cdot x\right)\\
\end{array}
\end{array}
if z < -9.50000000000000089e96Initial program 100.0%
lift-+.f64N/A
+-commutativeN/A
lift-*.f64N/A
lift--.f64N/A
sub-negate-revN/A
distribute-lft-neg-outN/A
distribute-rgt-neg-outN/A
lift--.f64N/A
sub-negate-revN/A
lower-fma.f64N/A
lower--.f64N/A
lower--.f64100.0
Applied rewrites100.0%
Taylor expanded in z around inf
lower-*.f64N/A
lower--.f6444.8
Applied rewrites44.8%
if -9.50000000000000089e96 < z < 8.9999999999999992e31Initial program 100.0%
Taylor expanded in z around 0
lower-+.f64N/A
lower-*.f64N/A
lower--.f6460.8
Applied rewrites60.8%
lift-+.f64N/A
+-commutativeN/A
lift-*.f64N/A
*-commutativeN/A
lower-fma.f6460.8
Applied rewrites60.8%
if 8.9999999999999992e31 < z Initial program 100.0%
lift-+.f64N/A
+-commutativeN/A
lift-*.f64N/A
lift--.f64N/A
sub-negate-revN/A
distribute-lft-neg-outN/A
distribute-rgt-neg-outN/A
lift--.f64N/A
sub-negate-revN/A
lower-fma.f64N/A
lower--.f64N/A
lower--.f64100.0
Applied rewrites100.0%
Taylor expanded in z around inf
lower-*.f64N/A
lower--.f6444.8
Applied rewrites44.8%
lift-*.f64N/A
lift--.f64N/A
sub-flipN/A
+-commutativeN/A
distribute-lft-inN/A
distribute-rgt-neg-outN/A
distribute-lft-neg-outN/A
*-commutativeN/A
lift-*.f64N/A
lower-fma.f64N/A
lower-neg.f6444.0
lift-*.f64N/A
*-commutativeN/A
lower-*.f6444.0
Applied rewrites44.0%
(FPCore (x y z t) :precision binary64 (let* ((t_1 (* z (- x t)))) (if (<= z -9.5e+96) t_1 (if (<= z 9e+31) (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 <= -9.5e+96) {
tmp = t_1;
} else if (z <= 9e+31) {
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 <= -9.5e+96) tmp = t_1; elseif (z <= 9e+31) 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, -9.5e+96], t$95$1, If[LessEqual[z, 9e+31], 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 -9.5 \cdot 10^{+96}:\\
\;\;\;\;t\_1\\
\mathbf{elif}\;z \leq 9 \cdot 10^{+31}:\\
\;\;\;\;\mathsf{fma}\left(t - x, y, x\right)\\
\mathbf{else}:\\
\;\;\;\;t\_1\\
\end{array}
\end{array}
if z < -9.50000000000000089e96 or 8.9999999999999992e31 < z Initial program 100.0%
lift-+.f64N/A
+-commutativeN/A
lift-*.f64N/A
lift--.f64N/A
sub-negate-revN/A
distribute-lft-neg-outN/A
distribute-rgt-neg-outN/A
lift--.f64N/A
sub-negate-revN/A
lower-fma.f64N/A
lower--.f64N/A
lower--.f64100.0
Applied rewrites100.0%
Taylor expanded in z around inf
lower-*.f64N/A
lower--.f6444.8
Applied rewrites44.8%
if -9.50000000000000089e96 < z < 8.9999999999999992e31Initial program 100.0%
Taylor expanded in z around 0
lower-+.f64N/A
lower-*.f64N/A
lower--.f6460.8
Applied rewrites60.8%
lift-+.f64N/A
+-commutativeN/A
lift-*.f64N/A
*-commutativeN/A
lower-fma.f6460.8
Applied rewrites60.8%
(FPCore (x y z t)
:precision binary64
(let* ((t_1 (* y (- t x))))
(if (<= y -4e+17)
t_1
(if (<= y -2e-121) (fma t y x) (if (<= y 1e+30) (* z (- x t)) t_1)))))
double code(double x, double y, double z, double t) {
double t_1 = y * (t - x);
double tmp;
if (y <= -4e+17) {
tmp = t_1;
} else if (y <= -2e-121) {
tmp = fma(t, y, x);
} else if (y <= 1e+30) {
tmp = z * (x - t);
} else {
tmp = t_1;
}
return tmp;
}
function code(x, y, z, t) t_1 = Float64(y * Float64(t - x)) tmp = 0.0 if (y <= -4e+17) tmp = t_1; elseif (y <= -2e-121) tmp = fma(t, y, x); elseif (y <= 1e+30) tmp = Float64(z * Float64(x - t)); 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, -4e+17], t$95$1, If[LessEqual[y, -2e-121], N[(t * y + x), $MachinePrecision], If[LessEqual[y, 1e+30], N[(z * N[(x - t), $MachinePrecision]), $MachinePrecision], t$95$1]]]]
\begin{array}{l}
\\
\begin{array}{l}
t_1 := y \cdot \left(t - x\right)\\
\mathbf{if}\;y \leq -4 \cdot 10^{+17}:\\
\;\;\;\;t\_1\\
\mathbf{elif}\;y \leq -2 \cdot 10^{-121}:\\
\;\;\;\;\mathsf{fma}\left(t, y, x\right)\\
\mathbf{elif}\;y \leq 10^{+30}:\\
\;\;\;\;z \cdot \left(x - t\right)\\
\mathbf{else}:\\
\;\;\;\;t\_1\\
\end{array}
\end{array}
if y < -4e17 or 1e30 < y Initial program 100.0%
Taylor expanded in y around inf
lower-*.f64N/A
lower--.f6445.2
Applied rewrites45.2%
if -4e17 < y < -2e-121Initial program 100.0%
Taylor expanded in z around 0
lower-+.f64N/A
lower-*.f64N/A
lower--.f6460.8
Applied rewrites60.8%
lift-+.f64N/A
+-commutativeN/A
lift-*.f64N/A
*-commutativeN/A
lower-fma.f6460.8
Applied rewrites60.8%
Taylor expanded in x around 0
Applied rewrites43.0%
if -2e-121 < y < 1e30Initial program 100.0%
lift-+.f64N/A
+-commutativeN/A
lift-*.f64N/A
lift--.f64N/A
sub-negate-revN/A
distribute-lft-neg-outN/A
distribute-rgt-neg-outN/A
lift--.f64N/A
sub-negate-revN/A
lower-fma.f64N/A
lower--.f64N/A
lower--.f64100.0
Applied rewrites100.0%
Taylor expanded in z around inf
lower-*.f64N/A
lower--.f6444.8
Applied rewrites44.8%
(FPCore (x y z t)
:precision binary64
(let* ((t_1 (* y (- t x))))
(if (<= y -4e+17)
t_1
(if (<= y -5.2e-122) (fma t y x) (if (<= y 7e+59) (* 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 <= -4e+17) {
tmp = t_1;
} else if (y <= -5.2e-122) {
tmp = fma(t, y, x);
} else if (y <= 7e+59) {
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 <= -4e+17) tmp = t_1; elseif (y <= -5.2e-122) tmp = fma(t, y, x); elseif (y <= 7e+59) 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, -4e+17], t$95$1, If[LessEqual[y, -5.2e-122], N[(t * y + x), $MachinePrecision], If[LessEqual[y, 7e+59], 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 -4 \cdot 10^{+17}:\\
\;\;\;\;t\_1\\
\mathbf{elif}\;y \leq -5.2 \cdot 10^{-122}:\\
\;\;\;\;\mathsf{fma}\left(t, y, x\right)\\
\mathbf{elif}\;y \leq 7 \cdot 10^{+59}:\\
\;\;\;\;t \cdot \left(y - z\right)\\
\mathbf{else}:\\
\;\;\;\;t\_1\\
\end{array}
\end{array}
if y < -4e17 or 7e59 < y Initial program 100.0%
Taylor expanded in y around inf
lower-*.f64N/A
lower--.f6445.2
Applied rewrites45.2%
if -4e17 < y < -5.1999999999999995e-122Initial program 100.0%
Taylor expanded in z around 0
lower-+.f64N/A
lower-*.f64N/A
lower--.f6460.8
Applied rewrites60.8%
lift-+.f64N/A
+-commutativeN/A
lift-*.f64N/A
*-commutativeN/A
lower-fma.f6460.8
Applied rewrites60.8%
Taylor expanded in x around 0
Applied rewrites43.0%
if -5.1999999999999995e-122 < y < 7e59Initial program 100.0%
Taylor expanded in x around 0
lower-*.f64N/A
lower--.f6449.2
Applied rewrites49.2%
(FPCore (x y z t) :precision binary64 (let* ((t_1 (* t (- y z)))) (if (<= z -3.6e-29) t_1 (if (<= z 2.5e-12) (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 <= -3.6e-29) {
tmp = t_1;
} else if (z <= 2.5e-12) {
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 <= -3.6e-29) tmp = t_1; elseif (z <= 2.5e-12) 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, -3.6e-29], t$95$1, If[LessEqual[z, 2.5e-12], 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 -3.6 \cdot 10^{-29}:\\
\;\;\;\;t\_1\\
\mathbf{elif}\;z \leq 2.5 \cdot 10^{-12}:\\
\;\;\;\;\mathsf{fma}\left(t, y, x\right)\\
\mathbf{else}:\\
\;\;\;\;t\_1\\
\end{array}
\end{array}
if z < -3.59999999999999974e-29 or 2.49999999999999985e-12 < z Initial program 100.0%
Taylor expanded in x around 0
lower-*.f64N/A
lower--.f6449.2
Applied rewrites49.2%
if -3.59999999999999974e-29 < z < 2.49999999999999985e-12Initial program 100.0%
Taylor expanded in z around 0
lower-+.f64N/A
lower-*.f64N/A
lower--.f6460.8
Applied rewrites60.8%
lift-+.f64N/A
+-commutativeN/A
lift-*.f64N/A
*-commutativeN/A
lower-fma.f6460.8
Applied rewrites60.8%
Taylor expanded in x around 0
Applied rewrites43.0%
(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
lower-+.f64N/A
lower-*.f64N/A
lower--.f6460.8
Applied rewrites60.8%
lift-+.f64N/A
+-commutativeN/A
lift-*.f64N/A
*-commutativeN/A
lower-fma.f6460.8
Applied rewrites60.8%
Taylor expanded in x around 0
Applied rewrites43.0%
(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
lower-*.f64N/A
lower--.f6445.2
Applied rewrites45.2%
Taylor expanded in x around 0
lower-*.f6427.5
Applied rewrites27.5%
herbie shell --seed 2025162
(FPCore (x y z t)
:name "Data.Metrics.Snapshot:quantile from metrics-0.3.0.2"
:precision binary64
(+ x (* (- y z) (- t x))))