
(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}
Sampling outcomes in binary64 precision:
Herbie found 6 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 (fma 3.0 x (fma 2.0 y z)))
double code(double x, double y, double z) {
return fma(3.0, x, fma(2.0, y, z));
}
function code(x, y, z) return fma(3.0, x, fma(2.0, y, z)) end
code[x_, y_, z_] := N[(3.0 * x + N[(2.0 * y + z), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\mathsf{fma}\left(3, x, \mathsf{fma}\left(2, y, z\right)\right)
\end{array}
Initial program 99.9%
Taylor expanded in x around 0
associate-+r+N/A
+-commutativeN/A
lower-fma.f64N/A
+-commutativeN/A
lower-fma.f64100.0
Applied rewrites100.0%
(FPCore (x y z)
:precision binary64
(if (<= z -1.25e+232)
(fma 2.0 y z)
(if (<= z -1.75e+47)
(fma 3.0 x z)
(if (<= z 1.55e+26) (fma 3.0 x (+ y y)) (fma 2.0 y z)))))
double code(double x, double y, double z) {
double tmp;
if (z <= -1.25e+232) {
tmp = fma(2.0, y, z);
} else if (z <= -1.75e+47) {
tmp = fma(3.0, x, z);
} else if (z <= 1.55e+26) {
tmp = fma(3.0, x, (y + y));
} else {
tmp = fma(2.0, y, z);
}
return tmp;
}
function code(x, y, z) tmp = 0.0 if (z <= -1.25e+232) tmp = fma(2.0, y, z); elseif (z <= -1.75e+47) tmp = fma(3.0, x, z); elseif (z <= 1.55e+26) tmp = fma(3.0, x, Float64(y + y)); else tmp = fma(2.0, y, z); end return tmp end
code[x_, y_, z_] := If[LessEqual[z, -1.25e+232], N[(2.0 * y + z), $MachinePrecision], If[LessEqual[z, -1.75e+47], N[(3.0 * x + z), $MachinePrecision], If[LessEqual[z, 1.55e+26], N[(3.0 * x + N[(y + y), $MachinePrecision]), $MachinePrecision], N[(2.0 * y + z), $MachinePrecision]]]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;z \leq -1.25 \cdot 10^{+232}:\\
\;\;\;\;\mathsf{fma}\left(2, y, z\right)\\
\mathbf{elif}\;z \leq -1.75 \cdot 10^{+47}:\\
\;\;\;\;\mathsf{fma}\left(3, x, z\right)\\
\mathbf{elif}\;z \leq 1.55 \cdot 10^{+26}:\\
\;\;\;\;\mathsf{fma}\left(3, x, y + y\right)\\
\mathbf{else}:\\
\;\;\;\;\mathsf{fma}\left(2, y, z\right)\\
\end{array}
\end{array}
if z < -1.24999999999999997e232 or 1.55e26 < z Initial program 99.9%
Taylor expanded in x around 0
+-commutativeN/A
lower-fma.f6490.2
Applied rewrites90.2%
if -1.24999999999999997e232 < z < -1.75000000000000008e47Initial program 99.9%
Taylor expanded in y around 0
+-commutativeN/A
associate-+r+N/A
distribute-rgt1-inN/A
metadata-evalN/A
lower-fma.f6483.4
Applied rewrites83.4%
if -1.75000000000000008e47 < z < 1.55e26Initial program 99.9%
Taylor expanded in z around 0
+-commutativeN/A
distribute-lft-outN/A
*-commutativeN/A
lower-fma.f64N/A
+-commutativeN/A
lower-+.f6494.9
Applied rewrites94.9%
Taylor expanded in x around 0
Applied rewrites94.9%
Applied rewrites94.9%
(FPCore (x y z) :precision binary64 (if (or (<= x -5e+102) (not (<= x 2.85e+52))) (fma 3.0 x z) (fma 2.0 y z)))
double code(double x, double y, double z) {
double tmp;
if ((x <= -5e+102) || !(x <= 2.85e+52)) {
tmp = fma(3.0, x, z);
} else {
tmp = fma(2.0, y, z);
}
return tmp;
}
function code(x, y, z) tmp = 0.0 if ((x <= -5e+102) || !(x <= 2.85e+52)) tmp = fma(3.0, x, z); else tmp = fma(2.0, y, z); end return tmp end
code[x_, y_, z_] := If[Or[LessEqual[x, -5e+102], N[Not[LessEqual[x, 2.85e+52]], $MachinePrecision]], N[(3.0 * x + z), $MachinePrecision], N[(2.0 * y + z), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq -5 \cdot 10^{+102} \lor \neg \left(x \leq 2.85 \cdot 10^{+52}\right):\\
\;\;\;\;\mathsf{fma}\left(3, x, z\right)\\
\mathbf{else}:\\
\;\;\;\;\mathsf{fma}\left(2, y, z\right)\\
\end{array}
\end{array}
if x < -5e102 or 2.84999999999999998e52 < x Initial program 99.8%
Taylor expanded in y around 0
+-commutativeN/A
associate-+r+N/A
distribute-rgt1-inN/A
metadata-evalN/A
lower-fma.f6484.8
Applied rewrites84.8%
if -5e102 < x < 2.84999999999999998e52Initial program 100.0%
Taylor expanded in x around 0
+-commutativeN/A
lower-fma.f6488.7
Applied rewrites88.7%
Final simplification87.1%
(FPCore (x y z) :precision binary64 (if (or (<= x -5e+105) (not (<= x 5.4e+56))) (* 3.0 x) (fma 2.0 y z)))
double code(double x, double y, double z) {
double tmp;
if ((x <= -5e+105) || !(x <= 5.4e+56)) {
tmp = 3.0 * x;
} else {
tmp = fma(2.0, y, z);
}
return tmp;
}
function code(x, y, z) tmp = 0.0 if ((x <= -5e+105) || !(x <= 5.4e+56)) tmp = Float64(3.0 * x); else tmp = fma(2.0, y, z); end return tmp end
code[x_, y_, z_] := If[Or[LessEqual[x, -5e+105], N[Not[LessEqual[x, 5.4e+56]], $MachinePrecision]], N[(3.0 * x), $MachinePrecision], N[(2.0 * y + z), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq -5 \cdot 10^{+105} \lor \neg \left(x \leq 5.4 \cdot 10^{+56}\right):\\
\;\;\;\;3 \cdot x\\
\mathbf{else}:\\
\;\;\;\;\mathsf{fma}\left(2, y, z\right)\\
\end{array}
\end{array}
if x < -5.00000000000000046e105 or 5.40000000000000019e56 < x Initial program 99.8%
Taylor expanded in x around inf
lower-*.f6471.3
Applied rewrites71.3%
if -5.00000000000000046e105 < x < 5.40000000000000019e56Initial program 100.0%
Taylor expanded in x around 0
+-commutativeN/A
lower-fma.f6488.8
Applied rewrites88.8%
Final simplification81.7%
(FPCore (x y z) :precision binary64 (if (or (<= x -5e+102) (not (<= x 6200000000.0))) (* 3.0 x) (+ y y)))
double code(double x, double y, double z) {
double tmp;
if ((x <= -5e+102) || !(x <= 6200000000.0)) {
tmp = 3.0 * x;
} else {
tmp = y + 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)
use fmin_fmax_functions
real(8), intent (in) :: x
real(8), intent (in) :: y
real(8), intent (in) :: z
real(8) :: tmp
if ((x <= (-5d+102)) .or. (.not. (x <= 6200000000.0d0))) then
tmp = 3.0d0 * x
else
tmp = y + y
end if
code = tmp
end function
public static double code(double x, double y, double z) {
double tmp;
if ((x <= -5e+102) || !(x <= 6200000000.0)) {
tmp = 3.0 * x;
} else {
tmp = y + y;
}
return tmp;
}
def code(x, y, z): tmp = 0 if (x <= -5e+102) or not (x <= 6200000000.0): tmp = 3.0 * x else: tmp = y + y return tmp
function code(x, y, z) tmp = 0.0 if ((x <= -5e+102) || !(x <= 6200000000.0)) tmp = Float64(3.0 * x); else tmp = Float64(y + y); end return tmp end
function tmp_2 = code(x, y, z) tmp = 0.0; if ((x <= -5e+102) || ~((x <= 6200000000.0))) tmp = 3.0 * x; else tmp = y + y; end tmp_2 = tmp; end
code[x_, y_, z_] := If[Or[LessEqual[x, -5e+102], N[Not[LessEqual[x, 6200000000.0]], $MachinePrecision]], N[(3.0 * x), $MachinePrecision], N[(y + y), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq -5 \cdot 10^{+102} \lor \neg \left(x \leq 6200000000\right):\\
\;\;\;\;3 \cdot x\\
\mathbf{else}:\\
\;\;\;\;y + y\\
\end{array}
\end{array}
if x < -5e102 or 6.2e9 < x Initial program 99.8%
Taylor expanded in x around inf
lower-*.f6468.8
Applied rewrites68.8%
if -5e102 < x < 6.2e9Initial program 100.0%
Taylor expanded in z around 0
+-commutativeN/A
distribute-lft-outN/A
*-commutativeN/A
lower-fma.f64N/A
+-commutativeN/A
lower-+.f6460.5
Applied rewrites60.5%
Taylor expanded in x around 0
Applied rewrites51.3%
Applied rewrites51.3%
Final simplification58.8%
(FPCore (x y z) :precision binary64 (+ y y))
double code(double x, double y, double z) {
return y + 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)
use fmin_fmax_functions
real(8), intent (in) :: x
real(8), intent (in) :: y
real(8), intent (in) :: z
code = y + y
end function
public static double code(double x, double y, double z) {
return y + y;
}
def code(x, y, z): return y + y
function code(x, y, z) return Float64(y + y) end
function tmp = code(x, y, z) tmp = y + y; end
code[x_, y_, z_] := N[(y + y), $MachinePrecision]
\begin{array}{l}
\\
y + y
\end{array}
Initial program 99.9%
Taylor expanded in z around 0
+-commutativeN/A
distribute-lft-outN/A
*-commutativeN/A
lower-fma.f64N/A
+-commutativeN/A
lower-+.f6470.8
Applied rewrites70.8%
Taylor expanded in x around 0
Applied rewrites36.0%
Applied rewrites36.0%
herbie shell --seed 2024359
(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))