
(FPCore (x y) :precision binary64 (/ (* (* x 2.0) y) (- x y)))
double code(double x, double y) {
return ((x * 2.0) * y) / (x - 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)
use fmin_fmax_functions
real(8), intent (in) :: x
real(8), intent (in) :: y
code = ((x * 2.0d0) * y) / (x - y)
end function
public static double code(double x, double y) {
return ((x * 2.0) * y) / (x - y);
}
def code(x, y): return ((x * 2.0) * y) / (x - y)
function code(x, y) return Float64(Float64(Float64(x * 2.0) * y) / Float64(x - y)) end
function tmp = code(x, y) tmp = ((x * 2.0) * y) / (x - y); end
code[x_, y_] := N[(N[(N[(x * 2.0), $MachinePrecision] * y), $MachinePrecision] / N[(x - y), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{\left(x \cdot 2\right) \cdot y}{x - y}
\end{array}
Herbie found 5 alternatives:
| Alternative | Accuracy | Speedup |
|---|
(FPCore (x y) :precision binary64 (/ (* (* x 2.0) y) (- x y)))
double code(double x, double y) {
return ((x * 2.0) * y) / (x - 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)
use fmin_fmax_functions
real(8), intent (in) :: x
real(8), intent (in) :: y
code = ((x * 2.0d0) * y) / (x - y)
end function
public static double code(double x, double y) {
return ((x * 2.0) * y) / (x - y);
}
def code(x, y): return ((x * 2.0) * y) / (x - y)
function code(x, y) return Float64(Float64(Float64(x * 2.0) * y) / Float64(x - y)) end
function tmp = code(x, y) tmp = ((x * 2.0) * y) / (x - y); end
code[x_, y_] := N[(N[(N[(x * 2.0), $MachinePrecision] * y), $MachinePrecision] / N[(x - y), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{\left(x \cdot 2\right) \cdot y}{x - y}
\end{array}
(FPCore (x y)
:precision binary64
(let* ((t_0 (* y (/ (+ x x) (- x y)))))
(if (<= x -3.7e-176)
t_0
(if (<= x 1e-183) (* (fma x (/ x y) x) -2.0) t_0))))
double code(double x, double y) {
double t_0 = y * ((x + x) / (x - y));
double tmp;
if (x <= -3.7e-176) {
tmp = t_0;
} else if (x <= 1e-183) {
tmp = fma(x, (x / y), x) * -2.0;
} else {
tmp = t_0;
}
return tmp;
}
function code(x, y) t_0 = Float64(y * Float64(Float64(x + x) / Float64(x - y))) tmp = 0.0 if (x <= -3.7e-176) tmp = t_0; elseif (x <= 1e-183) tmp = Float64(fma(x, Float64(x / y), x) * -2.0); else tmp = t_0; end return tmp end
code[x_, y_] := Block[{t$95$0 = N[(y * N[(N[(x + x), $MachinePrecision] / N[(x - y), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]}, If[LessEqual[x, -3.7e-176], t$95$0, If[LessEqual[x, 1e-183], N[(N[(x * N[(x / y), $MachinePrecision] + x), $MachinePrecision] * -2.0), $MachinePrecision], t$95$0]]]
\begin{array}{l}
\\
\begin{array}{l}
t_0 := y \cdot \frac{x + x}{x - y}\\
\mathbf{if}\;x \leq -3.7 \cdot 10^{-176}:\\
\;\;\;\;t\_0\\
\mathbf{elif}\;x \leq 10^{-183}:\\
\;\;\;\;\mathsf{fma}\left(x, \frac{x}{y}, x\right) \cdot -2\\
\mathbf{else}:\\
\;\;\;\;t\_0\\
\end{array}
\end{array}
if x < -3.69999999999999984e-176 or 1.00000000000000001e-183 < x Initial program 81.0%
lift--.f64N/A
lift-/.f64N/A
lift-*.f64N/A
lift-*.f64N/A
lift-*.f64N/A
*-commutativeN/A
associate-/l*N/A
lower-*.f64N/A
lower-/.f64N/A
lift-*.f64N/A
*-commutativeN/A
count-2-revN/A
lower-+.f64N/A
lift--.f6495.4
Applied rewrites95.4%
if -3.69999999999999984e-176 < x < 1.00000000000000001e-183Initial program 65.4%
Taylor expanded in y around inf
distribute-lft-outN/A
*-commutativeN/A
lower-*.f64N/A
+-commutativeN/A
unpow2N/A
associate-/l*N/A
lower-fma.f64N/A
lower-/.f6489.0
Applied rewrites89.0%
(FPCore (x y) :precision binary64 (let* ((t_0 (* (fma x (/ x y) x) -2.0))) (if (<= y -2.8e-114) t_0 (if (<= y 0.0021) (+ y y) t_0))))
double code(double x, double y) {
double t_0 = fma(x, (x / y), x) * -2.0;
double tmp;
if (y <= -2.8e-114) {
tmp = t_0;
} else if (y <= 0.0021) {
tmp = y + y;
} else {
tmp = t_0;
}
return tmp;
}
function code(x, y) t_0 = Float64(fma(x, Float64(x / y), x) * -2.0) tmp = 0.0 if (y <= -2.8e-114) tmp = t_0; elseif (y <= 0.0021) tmp = Float64(y + y); else tmp = t_0; end return tmp end
code[x_, y_] := Block[{t$95$0 = N[(N[(x * N[(x / y), $MachinePrecision] + x), $MachinePrecision] * -2.0), $MachinePrecision]}, If[LessEqual[y, -2.8e-114], t$95$0, If[LessEqual[y, 0.0021], N[(y + y), $MachinePrecision], t$95$0]]]
\begin{array}{l}
\\
\begin{array}{l}
t_0 := \mathsf{fma}\left(x, \frac{x}{y}, x\right) \cdot -2\\
\mathbf{if}\;y \leq -2.8 \cdot 10^{-114}:\\
\;\;\;\;t\_0\\
\mathbf{elif}\;y \leq 0.0021:\\
\;\;\;\;y + y\\
\mathbf{else}:\\
\;\;\;\;t\_0\\
\end{array}
\end{array}
if y < -2.8000000000000001e-114 or 0.00209999999999999987 < y Initial program 79.5%
Taylor expanded in y around inf
distribute-lft-outN/A
*-commutativeN/A
lower-*.f64N/A
+-commutativeN/A
unpow2N/A
associate-/l*N/A
lower-fma.f64N/A
lower-/.f6469.8
Applied rewrites69.8%
if -2.8000000000000001e-114 < y < 0.00209999999999999987Initial program 75.3%
Taylor expanded in x around inf
count-2-revN/A
lower-+.f6479.6
Applied rewrites79.6%
(FPCore (x y) :precision binary64 (if (<= y -2.8e-114) (* -2.0 x) (if (<= y 0.0021) (+ y y) (* -2.0 x))))
double code(double x, double y) {
double tmp;
if (y <= -2.8e-114) {
tmp = -2.0 * x;
} else if (y <= 0.0021) {
tmp = y + y;
} else {
tmp = -2.0 * x;
}
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 <= (-2.8d-114)) then
tmp = (-2.0d0) * x
else if (y <= 0.0021d0) then
tmp = y + y
else
tmp = (-2.0d0) * x
end if
code = tmp
end function
public static double code(double x, double y) {
double tmp;
if (y <= -2.8e-114) {
tmp = -2.0 * x;
} else if (y <= 0.0021) {
tmp = y + y;
} else {
tmp = -2.0 * x;
}
return tmp;
}
def code(x, y): tmp = 0 if y <= -2.8e-114: tmp = -2.0 * x elif y <= 0.0021: tmp = y + y else: tmp = -2.0 * x return tmp
function code(x, y) tmp = 0.0 if (y <= -2.8e-114) tmp = Float64(-2.0 * x); elseif (y <= 0.0021) tmp = Float64(y + y); else tmp = Float64(-2.0 * x); end return tmp end
function tmp_2 = code(x, y) tmp = 0.0; if (y <= -2.8e-114) tmp = -2.0 * x; elseif (y <= 0.0021) tmp = y + y; else tmp = -2.0 * x; end tmp_2 = tmp; end
code[x_, y_] := If[LessEqual[y, -2.8e-114], N[(-2.0 * x), $MachinePrecision], If[LessEqual[y, 0.0021], N[(y + y), $MachinePrecision], N[(-2.0 * x), $MachinePrecision]]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;y \leq -2.8 \cdot 10^{-114}:\\
\;\;\;\;-2 \cdot x\\
\mathbf{elif}\;y \leq 0.0021:\\
\;\;\;\;y + y\\
\mathbf{else}:\\
\;\;\;\;-2 \cdot x\\
\end{array}
\end{array}
if y < -2.8000000000000001e-114 or 0.00209999999999999987 < y Initial program 79.5%
Taylor expanded in x around 0
lower-*.f6470.1
Applied rewrites70.1%
if -2.8000000000000001e-114 < y < 0.00209999999999999987Initial program 75.3%
Taylor expanded in x around inf
count-2-revN/A
lower-+.f6479.6
Applied rewrites79.6%
(FPCore (x y) :precision binary64 (+ y y))
double code(double x, double y) {
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)
use fmin_fmax_functions
real(8), intent (in) :: x
real(8), intent (in) :: y
code = y + y
end function
public static double code(double x, double y) {
return y + y;
}
def code(x, y): return y + y
function code(x, y) return Float64(y + y) end
function tmp = code(x, y) tmp = y + y; end
code[x_, y_] := N[(y + y), $MachinePrecision]
\begin{array}{l}
\\
y + y
\end{array}
Initial program 77.8%
Taylor expanded in x around inf
count-2-revN/A
lower-+.f6450.6
Applied rewrites50.6%
(FPCore (x y) :precision binary64 (+ x x))
double code(double x, double y) {
return x + 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 + x
end function
public static double code(double x, double y) {
return x + x;
}
def code(x, y): return x + x
function code(x, y) return Float64(x + x) end
function tmp = code(x, y) tmp = x + x; end
code[x_, y_] := N[(x + x), $MachinePrecision]
\begin{array}{l}
\\
x + x
\end{array}
Initial program 77.8%
Taylor expanded in x around 0
lower-*.f6450.7
Applied rewrites50.7%
Applied rewrites3.4%
herbie shell --seed 2025130
(FPCore (x y)
:name "Linear.Projection:perspective from linear-1.19.1.3, B"
:precision binary64
(/ (* (* x 2.0) y) (- x y)))