
(FPCore (x y z) :precision binary64 (+ (+ (+ (+ (+ x y) y) x) z) x))
double code(double x, double y, double z) {
return ((((x + y) + y) + x) + z) + 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)
use fmin_fmax_functions
real(8), intent (in) :: x
real(8), intent (in) :: y
real(8), intent (in) :: z
code = ((((x + y) + y) + x) + z) + x
end function
public static double code(double x, double y, double z) {
return ((((x + y) + y) + x) + z) + x;
}
def code(x, y, z): return ((((x + y) + y) + x) + z) + x
function code(x, y, z) return Float64(Float64(Float64(Float64(Float64(x + y) + y) + x) + z) + x) end
function tmp = code(x, y, z) tmp = ((((x + y) + y) + x) + z) + x; end
code[x_, y_, z_] := N[(N[(N[(N[(N[(x + y), $MachinePrecision] + y), $MachinePrecision] + x), $MachinePrecision] + z), $MachinePrecision] + x), $MachinePrecision]
\begin{array}{l}
\\
\left(\left(\left(\left(x + y\right) + y\right) + x\right) + z\right) + x
\end{array}
Herbie found 7 alternatives:
| Alternative | Accuracy | Speedup |
|---|
(FPCore (x y z) :precision binary64 (+ (+ (+ (+ (+ x y) y) x) z) x))
double code(double x, double y, double z) {
return ((((x + y) + y) + x) + z) + 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)
use fmin_fmax_functions
real(8), intent (in) :: x
real(8), intent (in) :: y
real(8), intent (in) :: z
code = ((((x + y) + y) + x) + z) + x
end function
public static double code(double x, double y, double z) {
return ((((x + y) + y) + x) + z) + x;
}
def code(x, y, z): return ((((x + y) + y) + x) + z) + x
function code(x, y, z) return Float64(Float64(Float64(Float64(Float64(x + y) + y) + x) + z) + x) end
function tmp = code(x, y, z) tmp = ((((x + y) + y) + x) + z) + x; end
code[x_, y_, z_] := N[(N[(N[(N[(N[(x + y), $MachinePrecision] + y), $MachinePrecision] + x), $MachinePrecision] + z), $MachinePrecision] + x), $MachinePrecision]
\begin{array}{l}
\\
\left(\left(\left(\left(x + y\right) + y\right) + x\right) + z\right) + x
\end{array}
(FPCore (x y z) :precision binary64 (let* ((t_0 (+ (/ x (fma 2.0 (+ y x) z)) 1.0))) (fma t_0 (* 2.0 (+ y x)) (* t_0 z))))
double code(double x, double y, double z) {
double t_0 = (x / fma(2.0, (y + x), z)) + 1.0;
return fma(t_0, (2.0 * (y + x)), (t_0 * z));
}
function code(x, y, z) t_0 = Float64(Float64(x / fma(2.0, Float64(y + x), z)) + 1.0) return fma(t_0, Float64(2.0 * Float64(y + x)), Float64(t_0 * z)) end
code[x_, y_, z_] := Block[{t$95$0 = N[(N[(x / N[(2.0 * N[(y + x), $MachinePrecision] + z), $MachinePrecision]), $MachinePrecision] + 1.0), $MachinePrecision]}, N[(t$95$0 * N[(2.0 * N[(y + x), $MachinePrecision]), $MachinePrecision] + N[(t$95$0 * z), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
t_0 := \frac{x}{\mathsf{fma}\left(2, y + x, z\right)} + 1\\
\mathsf{fma}\left(t\_0, 2 \cdot \left(y + x\right), t\_0 \cdot z\right)
\end{array}
\end{array}
Initial program 99.9%
lift-+.f64N/A
sum-to-multN/A
lift-+.f64N/A
distribute-lft-inN/A
lower-fma.f64N/A
Applied rewrites99.9%
(FPCore (x y z) :precision binary64 (+ z (fma 2.0 (+ y x) x)))
double code(double x, double y, double z) {
return z + fma(2.0, (y + x), x);
}
function code(x, y, z) return Float64(z + fma(2.0, Float64(y + x), x)) end
code[x_, y_, z_] := N[(z + N[(2.0 * N[(y + x), $MachinePrecision] + x), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
z + \mathsf{fma}\left(2, y + x, x\right)
\end{array}
Initial program 99.9%
lift-+.f64N/A
lift-+.f64N/A
+-commutativeN/A
associate-+l+N/A
lower-+.f64N/A
lift-+.f64N/A
lift-+.f64N/A
associate-+l+N/A
+-commutativeN/A
lift-+.f64N/A
count-2N/A
lower-fma.f6499.9
lift-+.f64N/A
+-commutativeN/A
lower-+.f6499.9
Applied rewrites99.9%
(FPCore (x y z) :precision binary64 (let* ((t_0 (fma (+ y x) 2.0 z))) (if (<= y -3.1e+31) t_0 (if (<= y 2.25e-14) (+ (* 3.0 x) z) t_0))))
double code(double x, double y, double z) {
double t_0 = fma((y + x), 2.0, z);
double tmp;
if (y <= -3.1e+31) {
tmp = t_0;
} else if (y <= 2.25e-14) {
tmp = (3.0 * x) + z;
} else {
tmp = t_0;
}
return tmp;
}
function code(x, y, z) t_0 = fma(Float64(y + x), 2.0, z) tmp = 0.0 if (y <= -3.1e+31) tmp = t_0; elseif (y <= 2.25e-14) tmp = Float64(Float64(3.0 * x) + z); else tmp = t_0; end return tmp end
code[x_, y_, z_] := Block[{t$95$0 = N[(N[(y + x), $MachinePrecision] * 2.0 + z), $MachinePrecision]}, If[LessEqual[y, -3.1e+31], t$95$0, If[LessEqual[y, 2.25e-14], N[(N[(3.0 * x), $MachinePrecision] + z), $MachinePrecision], t$95$0]]]
\begin{array}{l}
\\
\begin{array}{l}
t_0 := \mathsf{fma}\left(y + x, 2, z\right)\\
\mathbf{if}\;y \leq -3.1 \cdot 10^{+31}:\\
\;\;\;\;t\_0\\
\mathbf{elif}\;y \leq 2.25 \cdot 10^{-14}:\\
\;\;\;\;3 \cdot x + z\\
\mathbf{else}:\\
\;\;\;\;t\_0\\
\end{array}
\end{array}
if y < -3.1000000000000002e31 or 2.2499999999999999e-14 < y Initial program 99.9%
lift-+.f64N/A
sum-to-multN/A
lift-+.f64N/A
distribute-lft-inN/A
lower-fma.f64N/A
Applied rewrites99.9%
lift-fma.f64N/A
lift-*.f64N/A
distribute-lft-outN/A
lift-+.f64N/A
+-commutativeN/A
lift-/.f64N/A
lift-fma.f64N/A
count-2-revN/A
lift-+.f64N/A
+-commutativeN/A
lift-+.f64N/A
associate-+l+N/A
lift-*.f64N/A
count-2-revN/A
lift-+.f64N/A
+-commutativeN/A
lift-+.f64N/A
associate-+l+N/A
Applied rewrites99.9%
Taylor expanded in x around 0
Applied rewrites72.6%
if -3.1000000000000002e31 < y < 2.2499999999999999e-14Initial program 99.9%
lift-+.f64N/A
lift-+.f64N/A
+-commutativeN/A
associate-+l+N/A
lower-+.f64N/A
lift-+.f64N/A
lift-+.f64N/A
associate-+l+N/A
+-commutativeN/A
lift-+.f64N/A
count-2N/A
lower-fma.f6499.9
lift-+.f64N/A
+-commutativeN/A
lower-+.f6499.9
Applied rewrites99.9%
Taylor expanded in y around 0
lower-+.f64N/A
lower-*.f6466.5
Applied rewrites66.5%
lift-+.f64N/A
+-commutativeN/A
lower-+.f6466.5
lift-+.f64N/A
lift-*.f64N/A
distribute-rgt1-inN/A
metadata-evalN/A
lower-*.f6466.5
Applied rewrites66.5%
(FPCore (x y z) :precision binary64 (let* ((t_0 (+ z (fma 2.0 y x)))) (if (<= y -8.2e+32) t_0 (if (<= y 2.25e-14) (+ (* 3.0 x) z) t_0))))
double code(double x, double y, double z) {
double t_0 = z + fma(2.0, y, x);
double tmp;
if (y <= -8.2e+32) {
tmp = t_0;
} else if (y <= 2.25e-14) {
tmp = (3.0 * x) + z;
} else {
tmp = t_0;
}
return tmp;
}
function code(x, y, z) t_0 = Float64(z + fma(2.0, y, x)) tmp = 0.0 if (y <= -8.2e+32) tmp = t_0; elseif (y <= 2.25e-14) tmp = Float64(Float64(3.0 * x) + z); else tmp = t_0; end return tmp end
code[x_, y_, z_] := Block[{t$95$0 = N[(z + N[(2.0 * y + x), $MachinePrecision]), $MachinePrecision]}, If[LessEqual[y, -8.2e+32], t$95$0, If[LessEqual[y, 2.25e-14], N[(N[(3.0 * x), $MachinePrecision] + z), $MachinePrecision], t$95$0]]]
\begin{array}{l}
\\
\begin{array}{l}
t_0 := z + \mathsf{fma}\left(2, y, x\right)\\
\mathbf{if}\;y \leq -8.2 \cdot 10^{+32}:\\
\;\;\;\;t\_0\\
\mathbf{elif}\;y \leq 2.25 \cdot 10^{-14}:\\
\;\;\;\;3 \cdot x + z\\
\mathbf{else}:\\
\;\;\;\;t\_0\\
\end{array}
\end{array}
if y < -8.19999999999999961e32 or 2.2499999999999999e-14 < y Initial program 99.9%
lift-+.f64N/A
lift-+.f64N/A
+-commutativeN/A
associate-+l+N/A
lower-+.f64N/A
lift-+.f64N/A
lift-+.f64N/A
associate-+l+N/A
+-commutativeN/A
lift-+.f64N/A
count-2N/A
lower-fma.f6499.9
lift-+.f64N/A
+-commutativeN/A
lower-+.f6499.9
Applied rewrites99.9%
Taylor expanded in x around 0
Applied rewrites71.8%
if -8.19999999999999961e32 < y < 2.2499999999999999e-14Initial program 99.9%
lift-+.f64N/A
lift-+.f64N/A
+-commutativeN/A
associate-+l+N/A
lower-+.f64N/A
lift-+.f64N/A
lift-+.f64N/A
associate-+l+N/A
+-commutativeN/A
lift-+.f64N/A
count-2N/A
lower-fma.f6499.9
lift-+.f64N/A
+-commutativeN/A
lower-+.f6499.9
Applied rewrites99.9%
Taylor expanded in y around 0
lower-+.f64N/A
lower-*.f6466.5
Applied rewrites66.5%
lift-+.f64N/A
+-commutativeN/A
lower-+.f6466.5
lift-+.f64N/A
lift-*.f64N/A
distribute-rgt1-inN/A
metadata-evalN/A
lower-*.f6466.5
Applied rewrites66.5%
(FPCore (x y z) :precision binary64 (if (<= y -7.8e+65) (fma y 2.0 z) (if (<= y 2.75e-14) (+ (* 3.0 x) z) (fma y 2.0 z))))
double code(double x, double y, double z) {
double tmp;
if (y <= -7.8e+65) {
tmp = fma(y, 2.0, z);
} else if (y <= 2.75e-14) {
tmp = (3.0 * x) + z;
} else {
tmp = fma(y, 2.0, z);
}
return tmp;
}
function code(x, y, z) tmp = 0.0 if (y <= -7.8e+65) tmp = fma(y, 2.0, z); elseif (y <= 2.75e-14) tmp = Float64(Float64(3.0 * x) + z); else tmp = fma(y, 2.0, z); end return tmp end
code[x_, y_, z_] := If[LessEqual[y, -7.8e+65], N[(y * 2.0 + z), $MachinePrecision], If[LessEqual[y, 2.75e-14], N[(N[(3.0 * x), $MachinePrecision] + z), $MachinePrecision], N[(y * 2.0 + z), $MachinePrecision]]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;y \leq -7.8 \cdot 10^{+65}:\\
\;\;\;\;\mathsf{fma}\left(y, 2, z\right)\\
\mathbf{elif}\;y \leq 2.75 \cdot 10^{-14}:\\
\;\;\;\;3 \cdot x + z\\
\mathbf{else}:\\
\;\;\;\;\mathsf{fma}\left(y, 2, z\right)\\
\end{array}
\end{array}
if y < -7.7999999999999996e65 or 2.74999999999999996e-14 < y Initial program 99.9%
Taylor expanded in x around 0
lower-+.f64N/A
lower-*.f6467.0
Applied rewrites67.0%
lift-+.f64N/A
+-commutativeN/A
lift-*.f64N/A
*-commutativeN/A
lower-fma.f6467.0
Applied rewrites67.0%
if -7.7999999999999996e65 < y < 2.74999999999999996e-14Initial program 99.9%
lift-+.f64N/A
lift-+.f64N/A
+-commutativeN/A
associate-+l+N/A
lower-+.f64N/A
lift-+.f64N/A
lift-+.f64N/A
associate-+l+N/A
+-commutativeN/A
lift-+.f64N/A
count-2N/A
lower-fma.f6499.9
lift-+.f64N/A
+-commutativeN/A
lower-+.f6499.9
Applied rewrites99.9%
Taylor expanded in y around 0
lower-+.f64N/A
lower-*.f6466.5
Applied rewrites66.5%
lift-+.f64N/A
+-commutativeN/A
lower-+.f6466.5
lift-+.f64N/A
lift-*.f64N/A
distribute-rgt1-inN/A
metadata-evalN/A
lower-*.f6466.5
Applied rewrites66.5%
(FPCore (x y z) :precision binary64 (if (<= x -3e+122) (* 3.0 x) (if (<= x 4.5e+181) (fma y 2.0 z) (* 3.0 x))))
double code(double x, double y, double z) {
double tmp;
if (x <= -3e+122) {
tmp = 3.0 * x;
} else if (x <= 4.5e+181) {
tmp = fma(y, 2.0, z);
} else {
tmp = 3.0 * x;
}
return tmp;
}
function code(x, y, z) tmp = 0.0 if (x <= -3e+122) tmp = Float64(3.0 * x); elseif (x <= 4.5e+181) tmp = fma(y, 2.0, z); else tmp = Float64(3.0 * x); end return tmp end
code[x_, y_, z_] := If[LessEqual[x, -3e+122], N[(3.0 * x), $MachinePrecision], If[LessEqual[x, 4.5e+181], N[(y * 2.0 + z), $MachinePrecision], N[(3.0 * x), $MachinePrecision]]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq -3 \cdot 10^{+122}:\\
\;\;\;\;3 \cdot x\\
\mathbf{elif}\;x \leq 4.5 \cdot 10^{+181}:\\
\;\;\;\;\mathsf{fma}\left(y, 2, z\right)\\
\mathbf{else}:\\
\;\;\;\;3 \cdot x\\
\end{array}
\end{array}
if x < -2.99999999999999986e122 or 4.5e181 < x Initial program 99.9%
Taylor expanded in x around inf
lower-*.f6433.9
Applied rewrites33.9%
if -2.99999999999999986e122 < x < 4.5e181Initial program 99.9%
Taylor expanded in x around 0
lower-+.f64N/A
lower-*.f6467.0
Applied rewrites67.0%
lift-+.f64N/A
+-commutativeN/A
lift-*.f64N/A
*-commutativeN/A
lower-fma.f6467.0
Applied rewrites67.0%
(FPCore (x y z) :precision binary64 (* 3.0 x))
double code(double x, double y, double z) {
return 3.0 * 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)
use fmin_fmax_functions
real(8), intent (in) :: x
real(8), intent (in) :: y
real(8), intent (in) :: z
code = 3.0d0 * x
end function
public static double code(double x, double y, double z) {
return 3.0 * x;
}
def code(x, y, z): return 3.0 * x
function code(x, y, z) return Float64(3.0 * x) end
function tmp = code(x, y, z) tmp = 3.0 * x; end
code[x_, y_, z_] := N[(3.0 * x), $MachinePrecision]
\begin{array}{l}
\\
3 \cdot x
\end{array}
Initial program 99.9%
Taylor expanded in x around inf
lower-*.f6433.9
Applied rewrites33.9%
herbie shell --seed 2025142
(FPCore (x y z)
:name "Graphics.Rendering.Plot.Render.Plot.Legend:renderLegendInside from plot-0.2.3.4"
:precision binary64
(+ (+ (+ (+ (+ x y) y) x) z) x))