
(FPCore (x y) :precision binary64 (+ x (/ (fabs (- y x)) 2.0)))
double code(double x, double y) {
return x + (fabs((y - x)) / 2.0);
}
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)
use fmin_fmax_functions
real(8), intent (in) :: x
real(8), intent (in) :: y
code = x + (abs((y - x)) / 2.0d0)
end function
public static double code(double x, double y) {
return x + (Math.abs((y - x)) / 2.0);
}
def code(x, y): return x + (math.fabs((y - x)) / 2.0)
function code(x, y) return Float64(x + Float64(abs(Float64(y - x)) / 2.0)) end
function tmp = code(x, y) tmp = x + (abs((y - x)) / 2.0); end
code[x_, y_] := N[(x + N[(N[Abs[N[(y - x), $MachinePrecision]], $MachinePrecision] / 2.0), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
x + \frac{\left|y - x\right|}{2}
\end{array}
Sampling outcomes in binary64 precision:
Herbie found 8 alternatives:
| Alternative | Accuracy | Speedup |
|---|
(FPCore (x y) :precision binary64 (+ x (/ (fabs (- y x)) 2.0)))
double code(double x, double y) {
return x + (fabs((y - x)) / 2.0);
}
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)
use fmin_fmax_functions
real(8), intent (in) :: x
real(8), intent (in) :: y
code = x + (abs((y - x)) / 2.0d0)
end function
public static double code(double x, double y) {
return x + (Math.abs((y - x)) / 2.0);
}
def code(x, y): return x + (math.fabs((y - x)) / 2.0)
function code(x, y) return Float64(x + Float64(abs(Float64(y - x)) / 2.0)) end
function tmp = code(x, y) tmp = x + (abs((y - x)) / 2.0); end
code[x_, y_] := N[(x + N[(N[Abs[N[(y - x), $MachinePrecision]], $MachinePrecision] / 2.0), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
x + \frac{\left|y - x\right|}{2}
\end{array}
(FPCore (x y) :precision binary64 (fma 0.5 (fabs (- y x)) x))
double code(double x, double y) {
return fma(0.5, fabs((y - x)), x);
}
function code(x, y) return fma(0.5, abs(Float64(y - x)), x) end
code[x_, y_] := N[(0.5 * N[Abs[N[(y - x), $MachinePrecision]], $MachinePrecision] + x), $MachinePrecision]
\begin{array}{l}
\\
\mathsf{fma}\left(0.5, \left|y - x\right|, x\right)
\end{array}
Initial program 99.9%
Taylor expanded in x around 0
Applied rewrites99.9%
Final simplification99.9%
(FPCore (x y) :precision binary64 (if (<= y -8.8e-101) (fma 0.5 (fabs (- y)) x) (if (<= y 2.15e-115) (fma 0.5 (fabs x) x) (* (+ y x) 0.5))))
double code(double x, double y) {
double tmp;
if (y <= -8.8e-101) {
tmp = fma(0.5, fabs(-y), x);
} else if (y <= 2.15e-115) {
tmp = fma(0.5, fabs(x), x);
} else {
tmp = (y + x) * 0.5;
}
return tmp;
}
function code(x, y) tmp = 0.0 if (y <= -8.8e-101) tmp = fma(0.5, abs(Float64(-y)), x); elseif (y <= 2.15e-115) tmp = fma(0.5, abs(x), x); else tmp = Float64(Float64(y + x) * 0.5); end return tmp end
code[x_, y_] := If[LessEqual[y, -8.8e-101], N[(0.5 * N[Abs[(-y)], $MachinePrecision] + x), $MachinePrecision], If[LessEqual[y, 2.15e-115], N[(0.5 * N[Abs[x], $MachinePrecision] + x), $MachinePrecision], N[(N[(y + x), $MachinePrecision] * 0.5), $MachinePrecision]]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;y \leq -8.8 \cdot 10^{-101}:\\
\;\;\;\;\mathsf{fma}\left(0.5, \left|-y\right|, x\right)\\
\mathbf{elif}\;y \leq 2.15 \cdot 10^{-115}:\\
\;\;\;\;\mathsf{fma}\left(0.5, \left|x\right|, x\right)\\
\mathbf{else}:\\
\;\;\;\;\left(y + x\right) \cdot 0.5\\
\end{array}
\end{array}
if y < -8.7999999999999996e-101Initial program 100.0%
Taylor expanded in x around 0
Applied rewrites100.0%
Taylor expanded in x around 0
Applied rewrites75.5%
if -8.7999999999999996e-101 < y < 2.1500000000000002e-115Initial program 99.9%
Taylor expanded in x around 0
Applied rewrites99.9%
Taylor expanded in x around inf
Applied rewrites86.4%
if 2.1500000000000002e-115 < y Initial program 100.0%
lift-+.f64N/A
+-commutativeN/A
flip-+N/A
lower-/.f64N/A
lower--.f64N/A
lift-/.f64N/A
lift-/.f64N/A
frac-timesN/A
lift-fabs.f64N/A
lift-fabs.f64N/A
sqr-abs-revN/A
lower-/.f64N/A
pow2N/A
lower-pow.f64N/A
metadata-evalN/A
lower-*.f64N/A
lower--.f6451.4
Applied rewrites51.4%
Taylor expanded in x around 0
Applied rewrites42.0%
Applied rewrites41.5%
Taylor expanded in x around 0
Applied rewrites90.3%
(FPCore (x y) :precision binary64 (if (<= y -2.35e-16) (* 0.5 (fabs (- y x))) (if (<= y 2.15e-115) (fma 0.5 (fabs x) x) (* (+ y x) 0.5))))
double code(double x, double y) {
double tmp;
if (y <= -2.35e-16) {
tmp = 0.5 * fabs((y - x));
} else if (y <= 2.15e-115) {
tmp = fma(0.5, fabs(x), x);
} else {
tmp = (y + x) * 0.5;
}
return tmp;
}
function code(x, y) tmp = 0.0 if (y <= -2.35e-16) tmp = Float64(0.5 * abs(Float64(y - x))); elseif (y <= 2.15e-115) tmp = fma(0.5, abs(x), x); else tmp = Float64(Float64(y + x) * 0.5); end return tmp end
code[x_, y_] := If[LessEqual[y, -2.35e-16], N[(0.5 * N[Abs[N[(y - x), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], If[LessEqual[y, 2.15e-115], N[(0.5 * N[Abs[x], $MachinePrecision] + x), $MachinePrecision], N[(N[(y + x), $MachinePrecision] * 0.5), $MachinePrecision]]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;y \leq -2.35 \cdot 10^{-16}:\\
\;\;\;\;0.5 \cdot \left|y - x\right|\\
\mathbf{elif}\;y \leq 2.15 \cdot 10^{-115}:\\
\;\;\;\;\mathsf{fma}\left(0.5, \left|x\right|, x\right)\\
\mathbf{else}:\\
\;\;\;\;\left(y + x\right) \cdot 0.5\\
\end{array}
\end{array}
if y < -2.35000000000000022e-16Initial program 100.0%
Taylor expanded in x around 0
Applied rewrites80.0%
if -2.35000000000000022e-16 < y < 2.1500000000000002e-115Initial program 99.9%
Taylor expanded in x around 0
Applied rewrites99.9%
Taylor expanded in x around inf
Applied rewrites80.7%
if 2.1500000000000002e-115 < y Initial program 100.0%
lift-+.f64N/A
+-commutativeN/A
flip-+N/A
lower-/.f64N/A
lower--.f64N/A
lift-/.f64N/A
lift-/.f64N/A
frac-timesN/A
lift-fabs.f64N/A
lift-fabs.f64N/A
sqr-abs-revN/A
lower-/.f64N/A
pow2N/A
lower-pow.f64N/A
metadata-evalN/A
lower-*.f64N/A
lower--.f6451.4
Applied rewrites51.4%
Taylor expanded in x around 0
Applied rewrites42.0%
Applied rewrites41.5%
Taylor expanded in x around 0
Applied rewrites90.3%
Final simplification83.8%
(FPCore (x y) :precision binary64 (if (<= y -2.6e-16) (* 0.5 (fabs (- y))) (if (<= y 2.15e-115) (fma 0.5 (fabs x) x) (* (+ y x) 0.5))))
double code(double x, double y) {
double tmp;
if (y <= -2.6e-16) {
tmp = 0.5 * fabs(-y);
} else if (y <= 2.15e-115) {
tmp = fma(0.5, fabs(x), x);
} else {
tmp = (y + x) * 0.5;
}
return tmp;
}
function code(x, y) tmp = 0.0 if (y <= -2.6e-16) tmp = Float64(0.5 * abs(Float64(-y))); elseif (y <= 2.15e-115) tmp = fma(0.5, abs(x), x); else tmp = Float64(Float64(y + x) * 0.5); end return tmp end
code[x_, y_] := If[LessEqual[y, -2.6e-16], N[(0.5 * N[Abs[(-y)], $MachinePrecision]), $MachinePrecision], If[LessEqual[y, 2.15e-115], N[(0.5 * N[Abs[x], $MachinePrecision] + x), $MachinePrecision], N[(N[(y + x), $MachinePrecision] * 0.5), $MachinePrecision]]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;y \leq -2.6 \cdot 10^{-16}:\\
\;\;\;\;0.5 \cdot \left|-y\right|\\
\mathbf{elif}\;y \leq 2.15 \cdot 10^{-115}:\\
\;\;\;\;\mathsf{fma}\left(0.5, \left|x\right|, x\right)\\
\mathbf{else}:\\
\;\;\;\;\left(y + x\right) \cdot 0.5\\
\end{array}
\end{array}
if y < -2.5999999999999998e-16Initial program 100.0%
Taylor expanded in x around 0
Applied rewrites80.0%
Taylor expanded in x around 0
Applied rewrites78.4%
if -2.5999999999999998e-16 < y < 2.1500000000000002e-115Initial program 99.9%
Taylor expanded in x around 0
Applied rewrites99.9%
Taylor expanded in x around inf
Applied rewrites80.7%
if 2.1500000000000002e-115 < y Initial program 100.0%
lift-+.f64N/A
+-commutativeN/A
flip-+N/A
lower-/.f64N/A
lower--.f64N/A
lift-/.f64N/A
lift-/.f64N/A
frac-timesN/A
lift-fabs.f64N/A
lift-fabs.f64N/A
sqr-abs-revN/A
lower-/.f64N/A
pow2N/A
lower-pow.f64N/A
metadata-evalN/A
lower-*.f64N/A
lower--.f6451.4
Applied rewrites51.4%
Taylor expanded in x around 0
Applied rewrites42.0%
Applied rewrites41.5%
Taylor expanded in x around 0
Applied rewrites90.3%
(FPCore (x y) :precision binary64 (if (<= y 2.15e-115) (fma 0.5 (fabs x) x) (* (+ y x) 0.5)))
double code(double x, double y) {
double tmp;
if (y <= 2.15e-115) {
tmp = fma(0.5, fabs(x), x);
} else {
tmp = (y + x) * 0.5;
}
return tmp;
}
function code(x, y) tmp = 0.0 if (y <= 2.15e-115) tmp = fma(0.5, abs(x), x); else tmp = Float64(Float64(y + x) * 0.5); end return tmp end
code[x_, y_] := If[LessEqual[y, 2.15e-115], N[(0.5 * N[Abs[x], $MachinePrecision] + x), $MachinePrecision], N[(N[(y + x), $MachinePrecision] * 0.5), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;y \leq 2.15 \cdot 10^{-115}:\\
\;\;\;\;\mathsf{fma}\left(0.5, \left|x\right|, x\right)\\
\mathbf{else}:\\
\;\;\;\;\left(y + x\right) \cdot 0.5\\
\end{array}
\end{array}
if y < 2.1500000000000002e-115Initial program 99.9%
Taylor expanded in x around 0
Applied rewrites99.9%
Taylor expanded in x around inf
Applied rewrites59.9%
if 2.1500000000000002e-115 < y Initial program 100.0%
lift-+.f64N/A
+-commutativeN/A
flip-+N/A
lower-/.f64N/A
lower--.f64N/A
lift-/.f64N/A
lift-/.f64N/A
frac-timesN/A
lift-fabs.f64N/A
lift-fabs.f64N/A
sqr-abs-revN/A
lower-/.f64N/A
pow2N/A
lower-pow.f64N/A
metadata-evalN/A
lower-*.f64N/A
lower--.f6451.4
Applied rewrites51.4%
Taylor expanded in x around 0
Applied rewrites42.0%
Applied rewrites41.5%
Taylor expanded in x around 0
Applied rewrites90.3%
(FPCore (x y) :precision binary64 (if (<= y 1.7e+16) (fma 0.5 (fabs x) x) (* y 0.5)))
double code(double x, double y) {
double tmp;
if (y <= 1.7e+16) {
tmp = fma(0.5, fabs(x), x);
} else {
tmp = y * 0.5;
}
return tmp;
}
function code(x, y) tmp = 0.0 if (y <= 1.7e+16) tmp = fma(0.5, abs(x), x); else tmp = Float64(y * 0.5); end return tmp end
code[x_, y_] := If[LessEqual[y, 1.7e+16], N[(0.5 * N[Abs[x], $MachinePrecision] + x), $MachinePrecision], N[(y * 0.5), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;y \leq 1.7 \cdot 10^{+16}:\\
\;\;\;\;\mathsf{fma}\left(0.5, \left|x\right|, x\right)\\
\mathbf{else}:\\
\;\;\;\;y \cdot 0.5\\
\end{array}
\end{array}
if y < 1.7e16Initial program 99.9%
Taylor expanded in x around 0
Applied rewrites99.9%
Taylor expanded in x around inf
Applied rewrites59.3%
if 1.7e16 < y Initial program 100.0%
Taylor expanded in x around 0
Applied rewrites80.0%
Applied rewrites78.7%
Taylor expanded in x around 0
Applied rewrites79.2%
(FPCore (x y) :precision binary64 (if (<= y 6.2e-183) x (* y 0.5)))
double code(double x, double y) {
double tmp;
if (y <= 6.2e-183) {
tmp = x;
} else {
tmp = y * 0.5;
}
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)
use fmin_fmax_functions
real(8), intent (in) :: x
real(8), intent (in) :: y
real(8) :: tmp
if (y <= 6.2d-183) then
tmp = x
else
tmp = y * 0.5d0
end if
code = tmp
end function
public static double code(double x, double y) {
double tmp;
if (y <= 6.2e-183) {
tmp = x;
} else {
tmp = y * 0.5;
}
return tmp;
}
def code(x, y): tmp = 0 if y <= 6.2e-183: tmp = x else: tmp = y * 0.5 return tmp
function code(x, y) tmp = 0.0 if (y <= 6.2e-183) tmp = x; else tmp = Float64(y * 0.5); end return tmp end
function tmp_2 = code(x, y) tmp = 0.0; if (y <= 6.2e-183) tmp = x; else tmp = y * 0.5; end tmp_2 = tmp; end
code[x_, y_] := If[LessEqual[y, 6.2e-183], x, N[(y * 0.5), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;y \leq 6.2 \cdot 10^{-183}:\\
\;\;\;\;x\\
\mathbf{else}:\\
\;\;\;\;y \cdot 0.5\\
\end{array}
\end{array}
if y < 6.19999999999999999e-183Initial program 99.9%
Taylor expanded in x around inf
Applied rewrites12.7%
if 6.19999999999999999e-183 < y Initial program 100.0%
Taylor expanded in x around 0
Applied rewrites66.7%
Applied rewrites64.3%
Taylor expanded in x around 0
Applied rewrites65.2%
(FPCore (x y) :precision binary64 x)
double code(double x, double y) {
return 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)
use fmin_fmax_functions
real(8), intent (in) :: x
real(8), intent (in) :: y
code = x
end function
public static double code(double x, double y) {
return x;
}
def code(x, y): return x
function code(x, y) return x end
function tmp = code(x, y) tmp = x; end
code[x_, y_] := x
\begin{array}{l}
\\
x
\end{array}
Initial program 99.9%
Taylor expanded in x around inf
Applied rewrites11.2%
herbie shell --seed 2025018
(FPCore (x y)
:name "Graphics.Rendering.Chart.Plot.AreaSpots:renderSpotLegend from Chart-1.5.3"
:precision binary64
(+ x (/ (fabs (- y x)) 2.0)))