
(FPCore (x y) :precision binary64 (/ (+ x y) (* (* x 2.0) y)))
double code(double x, double y) {
return (x + y) / ((x * 2.0) * 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 + y) / ((x * 2.0d0) * y)
end function
public static double code(double x, double y) {
return (x + y) / ((x * 2.0) * y);
}
def code(x, y): return (x + y) / ((x * 2.0) * y)
function code(x, y) return Float64(Float64(x + y) / Float64(Float64(x * 2.0) * y)) end
function tmp = code(x, y) tmp = (x + y) / ((x * 2.0) * y); end
code[x_, y_] := N[(N[(x + y), $MachinePrecision] / N[(N[(x * 2.0), $MachinePrecision] * y), $MachinePrecision]), $MachinePrecision]
\frac{x + y}{\left(x \cdot 2\right) \cdot y}
Herbie found 4 alternatives:
| Alternative | Accuracy | Speedup |
|---|
(FPCore (x y) :precision binary64 (/ (+ x y) (* (* x 2.0) y)))
double code(double x, double y) {
return (x + y) / ((x * 2.0) * 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 + y) / ((x * 2.0d0) * y)
end function
public static double code(double x, double y) {
return (x + y) / ((x * 2.0) * y);
}
def code(x, y): return (x + y) / ((x * 2.0) * y)
function code(x, y) return Float64(Float64(x + y) / Float64(Float64(x * 2.0) * y)) end
function tmp = code(x, y) tmp = (x + y) / ((x * 2.0) * y); end
code[x_, y_] := N[(N[(x + y), $MachinePrecision] / N[(N[(x * 2.0), $MachinePrecision] * y), $MachinePrecision]), $MachinePrecision]
\frac{x + y}{\left(x \cdot 2\right) \cdot y}
(FPCore (x y) :precision binary64 (+ (/ 0.5 y) (/ 0.5 x)))
double code(double x, double y) {
return (0.5 / y) + (0.5 / 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 = (0.5d0 / y) + (0.5d0 / x)
end function
public static double code(double x, double y) {
return (0.5 / y) + (0.5 / x);
}
def code(x, y): return (0.5 / y) + (0.5 / x)
function code(x, y) return Float64(Float64(0.5 / y) + Float64(0.5 / x)) end
function tmp = code(x, y) tmp = (0.5 / y) + (0.5 / x); end
code[x_, y_] := N[(N[(0.5 / y), $MachinePrecision] + N[(0.5 / x), $MachinePrecision]), $MachinePrecision]
\frac{0.5}{y} + \frac{0.5}{x}
Initial program 76.9%
lift-/.f64N/A
lift-+.f64N/A
div-addN/A
lower-+.f64N/A
lift-*.f64N/A
associate-/r*N/A
lower-/.f64N/A
lift-*.f64N/A
associate-/r*N/A
*-inversesN/A
metadata-evalN/A
frac-2negN/A
lift-*.f64N/A
*-commutativeN/A
distribute-lft-neg-inN/A
associate-/r*N/A
frac-2negN/A
*-inversesN/A
lift-*.f64N/A
*-commutativeN/A
associate-/r*N/A
*-inversesN/A
associate-/r*N/A
lift-*.f64N/A
lower-/.f64N/A
Applied rewrites100.0%
(FPCore (x y)
:precision binary64
(if (<= (fmax x y) 2.05e-185)
(/ 0.5 (fmax x y))
(if (<= (fmax x y) 3.1e+145)
(/
(+ (fmax x y) (fmin x y))
(* (+ (fmin x y) (fmin x y)) (fmax x y)))
(/ 0.5 (fmin x y)))))double code(double x, double y) {
double tmp;
if (fmax(x, y) <= 2.05e-185) {
tmp = 0.5 / fmax(x, y);
} else if (fmax(x, y) <= 3.1e+145) {
tmp = (fmax(x, y) + fmin(x, y)) / ((fmin(x, y) + fmin(x, y)) * fmax(x, y));
} else {
tmp = 0.5 / fmin(x, 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)
use fmin_fmax_functions
real(8), intent (in) :: x
real(8), intent (in) :: y
real(8) :: tmp
if (fmax(x, y) <= 2.05d-185) then
tmp = 0.5d0 / fmax(x, y)
else if (fmax(x, y) <= 3.1d+145) then
tmp = (fmax(x, y) + fmin(x, y)) / ((fmin(x, y) + fmin(x, y)) * fmax(x, y))
else
tmp = 0.5d0 / fmin(x, y)
end if
code = tmp
end function
public static double code(double x, double y) {
double tmp;
if (fmax(x, y) <= 2.05e-185) {
tmp = 0.5 / fmax(x, y);
} else if (fmax(x, y) <= 3.1e+145) {
tmp = (fmax(x, y) + fmin(x, y)) / ((fmin(x, y) + fmin(x, y)) * fmax(x, y));
} else {
tmp = 0.5 / fmin(x, y);
}
return tmp;
}
def code(x, y): tmp = 0 if fmax(x, y) <= 2.05e-185: tmp = 0.5 / fmax(x, y) elif fmax(x, y) <= 3.1e+145: tmp = (fmax(x, y) + fmin(x, y)) / ((fmin(x, y) + fmin(x, y)) * fmax(x, y)) else: tmp = 0.5 / fmin(x, y) return tmp
function code(x, y) tmp = 0.0 if (fmax(x, y) <= 2.05e-185) tmp = Float64(0.5 / fmax(x, y)); elseif (fmax(x, y) <= 3.1e+145) tmp = Float64(Float64(fmax(x, y) + fmin(x, y)) / Float64(Float64(fmin(x, y) + fmin(x, y)) * fmax(x, y))); else tmp = Float64(0.5 / fmin(x, y)); end return tmp end
function tmp_2 = code(x, y) tmp = 0.0; if (max(x, y) <= 2.05e-185) tmp = 0.5 / max(x, y); elseif (max(x, y) <= 3.1e+145) tmp = (max(x, y) + min(x, y)) / ((min(x, y) + min(x, y)) * max(x, y)); else tmp = 0.5 / min(x, y); end tmp_2 = tmp; end
code[x_, y_] := If[LessEqual[N[Max[x, y], $MachinePrecision], 2.05e-185], N[(0.5 / N[Max[x, y], $MachinePrecision]), $MachinePrecision], If[LessEqual[N[Max[x, y], $MachinePrecision], 3.1e+145], N[(N[(N[Max[x, y], $MachinePrecision] + N[Min[x, y], $MachinePrecision]), $MachinePrecision] / N[(N[(N[Min[x, y], $MachinePrecision] + N[Min[x, y], $MachinePrecision]), $MachinePrecision] * N[Max[x, y], $MachinePrecision]), $MachinePrecision]), $MachinePrecision], N[(0.5 / N[Min[x, y], $MachinePrecision]), $MachinePrecision]]]
\begin{array}{l}
\mathbf{if}\;\mathsf{max}\left(x, y\right) \leq 2.05 \cdot 10^{-185}:\\
\;\;\;\;\frac{0.5}{\mathsf{max}\left(x, y\right)}\\
\mathbf{elif}\;\mathsf{max}\left(x, y\right) \leq 3.1 \cdot 10^{+145}:\\
\;\;\;\;\frac{\mathsf{max}\left(x, y\right) + \mathsf{min}\left(x, y\right)}{\left(\mathsf{min}\left(x, y\right) + \mathsf{min}\left(x, y\right)\right) \cdot \mathsf{max}\left(x, y\right)}\\
\mathbf{else}:\\
\;\;\;\;\frac{0.5}{\mathsf{min}\left(x, y\right)}\\
\end{array}
if y < 2.05e-185Initial program 76.9%
Taylor expanded in x around inf
lower-/.f6451.1%
Applied rewrites51.1%
if 2.05e-185 < y < 3.0999999999999999e145Initial program 76.9%
lift-+.f64N/A
+-commutativeN/A
lower-+.f6476.9%
lift-*.f64N/A
*-commutativeN/A
count-2-revN/A
lower-+.f6476.9%
Applied rewrites76.9%
if 3.0999999999999999e145 < y Initial program 76.9%
Taylor expanded in x around 0
lower-/.f6450.4%
Applied rewrites50.4%
(FPCore (x y) :precision binary64 (if (<= (fmin x y) -7.8e-64) (/ 0.5 (fmax x y)) (/ 0.5 (fmin x y))))
double code(double x, double y) {
double tmp;
if (fmin(x, y) <= -7.8e-64) {
tmp = 0.5 / fmax(x, y);
} else {
tmp = 0.5 / fmin(x, 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)
use fmin_fmax_functions
real(8), intent (in) :: x
real(8), intent (in) :: y
real(8) :: tmp
if (fmin(x, y) <= (-7.8d-64)) then
tmp = 0.5d0 / fmax(x, y)
else
tmp = 0.5d0 / fmin(x, y)
end if
code = tmp
end function
public static double code(double x, double y) {
double tmp;
if (fmin(x, y) <= -7.8e-64) {
tmp = 0.5 / fmax(x, y);
} else {
tmp = 0.5 / fmin(x, y);
}
return tmp;
}
def code(x, y): tmp = 0 if fmin(x, y) <= -7.8e-64: tmp = 0.5 / fmax(x, y) else: tmp = 0.5 / fmin(x, y) return tmp
function code(x, y) tmp = 0.0 if (fmin(x, y) <= -7.8e-64) tmp = Float64(0.5 / fmax(x, y)); else tmp = Float64(0.5 / fmin(x, y)); end return tmp end
function tmp_2 = code(x, y) tmp = 0.0; if (min(x, y) <= -7.8e-64) tmp = 0.5 / max(x, y); else tmp = 0.5 / min(x, y); end tmp_2 = tmp; end
code[x_, y_] := If[LessEqual[N[Min[x, y], $MachinePrecision], -7.8e-64], N[(0.5 / N[Max[x, y], $MachinePrecision]), $MachinePrecision], N[(0.5 / N[Min[x, y], $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\mathbf{if}\;\mathsf{min}\left(x, y\right) \leq -7.8 \cdot 10^{-64}:\\
\;\;\;\;\frac{0.5}{\mathsf{max}\left(x, y\right)}\\
\mathbf{else}:\\
\;\;\;\;\frac{0.5}{\mathsf{min}\left(x, y\right)}\\
\end{array}
if x < -7.7999999999999994e-64Initial program 76.9%
Taylor expanded in x around inf
lower-/.f6451.1%
Applied rewrites51.1%
if -7.7999999999999994e-64 < x Initial program 76.9%
Taylor expanded in x around 0
lower-/.f6450.4%
Applied rewrites50.4%
(FPCore (x y) :precision binary64 (/ 0.5 x))
double code(double x, double y) {
return 0.5 / 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 = 0.5d0 / x
end function
public static double code(double x, double y) {
return 0.5 / x;
}
def code(x, y): return 0.5 / x
function code(x, y) return Float64(0.5 / x) end
function tmp = code(x, y) tmp = 0.5 / x; end
code[x_, y_] := N[(0.5 / x), $MachinePrecision]
\frac{0.5}{x}
Initial program 76.9%
Taylor expanded in x around 0
lower-/.f6450.4%
Applied rewrites50.4%
herbie shell --seed 2025258
(FPCore (x y)
:name "Linear.Projection:inversePerspective from linear-1.19.1.3, C"
:precision binary64
(/ (+ x y) (* (* x 2.0) y)))