
(FPCore (x y) :precision binary64 (+ (- 1.0 x) (* y (sqrt x))))
double code(double x, double y) {
return (1.0 - x) + (y * sqrt(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 = (1.0d0 - x) + (y * sqrt(x))
end function
public static double code(double x, double y) {
return (1.0 - x) + (y * Math.sqrt(x));
}
def code(x, y): return (1.0 - x) + (y * math.sqrt(x))
function code(x, y) return Float64(Float64(1.0 - x) + Float64(y * sqrt(x))) end
function tmp = code(x, y) tmp = (1.0 - x) + (y * sqrt(x)); end
code[x_, y_] := N[(N[(1.0 - x), $MachinePrecision] + N[(y * N[Sqrt[x], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\left(1 - x\right) + y \cdot \sqrt{x}
\end{array}
Sampling outcomes in binary64 precision:
Herbie found 7 alternatives:
| Alternative | Accuracy | Speedup |
|---|
(FPCore (x y) :precision binary64 (+ (- 1.0 x) (* y (sqrt x))))
double code(double x, double y) {
return (1.0 - x) + (y * sqrt(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 = (1.0d0 - x) + (y * sqrt(x))
end function
public static double code(double x, double y) {
return (1.0 - x) + (y * Math.sqrt(x));
}
def code(x, y): return (1.0 - x) + (y * math.sqrt(x))
function code(x, y) return Float64(Float64(1.0 - x) + Float64(y * sqrt(x))) end
function tmp = code(x, y) tmp = (1.0 - x) + (y * sqrt(x)); end
code[x_, y_] := N[(N[(1.0 - x), $MachinePrecision] + N[(y * N[Sqrt[x], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\left(1 - x\right) + y \cdot \sqrt{x}
\end{array}
(FPCore (x y) :precision binary64 (fma (sqrt x) y (- 1.0 x)))
double code(double x, double y) {
return fma(sqrt(x), y, (1.0 - x));
}
function code(x, y) return fma(sqrt(x), y, Float64(1.0 - x)) end
code[x_, y_] := N[(N[Sqrt[x], $MachinePrecision] * y + N[(1.0 - x), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\mathsf{fma}\left(\sqrt{x}, y, 1 - x\right)
\end{array}
Initial program 99.9%
lift-+.f64N/A
lift--.f64N/A
lift-*.f64N/A
lift-sqrt.f64N/A
+-commutativeN/A
*-commutativeN/A
lower-fma.f64N/A
lift-sqrt.f64N/A
lift--.f6499.9
Applied rewrites99.9%
(FPCore (x y) :precision binary64 (if (<= (+ (- 1.0 x) (* y (sqrt x))) -10.0) (- x) 1.0))
double code(double x, double y) {
double tmp;
if (((1.0 - x) + (y * sqrt(x))) <= -10.0) {
tmp = -x;
} else {
tmp = 1.0;
}
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 (((1.0d0 - x) + (y * sqrt(x))) <= (-10.0d0)) then
tmp = -x
else
tmp = 1.0d0
end if
code = tmp
end function
public static double code(double x, double y) {
double tmp;
if (((1.0 - x) + (y * Math.sqrt(x))) <= -10.0) {
tmp = -x;
} else {
tmp = 1.0;
}
return tmp;
}
def code(x, y): tmp = 0 if ((1.0 - x) + (y * math.sqrt(x))) <= -10.0: tmp = -x else: tmp = 1.0 return tmp
function code(x, y) tmp = 0.0 if (Float64(Float64(1.0 - x) + Float64(y * sqrt(x))) <= -10.0) tmp = Float64(-x); else tmp = 1.0; end return tmp end
function tmp_2 = code(x, y) tmp = 0.0; if (((1.0 - x) + (y * sqrt(x))) <= -10.0) tmp = -x; else tmp = 1.0; end tmp_2 = tmp; end
code[x_, y_] := If[LessEqual[N[(N[(1.0 - x), $MachinePrecision] + N[(y * N[Sqrt[x], $MachinePrecision]), $MachinePrecision]), $MachinePrecision], -10.0], (-x), 1.0]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;\left(1 - x\right) + y \cdot \sqrt{x} \leq -10:\\
\;\;\;\;-x\\
\mathbf{else}:\\
\;\;\;\;1\\
\end{array}
\end{array}
if (+.f64 (-.f64 #s(literal 1 binary64) x) (*.f64 y (sqrt.f64 x))) < -10Initial program 99.9%
Taylor expanded in y around 0
lift--.f6472.8
Applied rewrites72.8%
Taylor expanded in x around inf
mul-1-negN/A
lower-neg.f6471.3
Applied rewrites71.3%
if -10 < (+.f64 (-.f64 #s(literal 1 binary64) x) (*.f64 y (sqrt.f64 x))) Initial program 99.9%
Taylor expanded in y around 0
lift--.f6462.7
Applied rewrites62.7%
Taylor expanded in x around 0
Applied rewrites61.4%
(FPCore (x y) :precision binary64 (if (or (<= y -3.1e+63) (not (<= y 5.4e+112))) (fma (sqrt x) y 1.0) (- 1.0 x)))
double code(double x, double y) {
double tmp;
if ((y <= -3.1e+63) || !(y <= 5.4e+112)) {
tmp = fma(sqrt(x), y, 1.0);
} else {
tmp = 1.0 - x;
}
return tmp;
}
function code(x, y) tmp = 0.0 if ((y <= -3.1e+63) || !(y <= 5.4e+112)) tmp = fma(sqrt(x), y, 1.0); else tmp = Float64(1.0 - x); end return tmp end
code[x_, y_] := If[Or[LessEqual[y, -3.1e+63], N[Not[LessEqual[y, 5.4e+112]], $MachinePrecision]], N[(N[Sqrt[x], $MachinePrecision] * y + 1.0), $MachinePrecision], N[(1.0 - x), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;y \leq -3.1 \cdot 10^{+63} \lor \neg \left(y \leq 5.4 \cdot 10^{+112}\right):\\
\;\;\;\;\mathsf{fma}\left(\sqrt{x}, y, 1\right)\\
\mathbf{else}:\\
\;\;\;\;1 - x\\
\end{array}
\end{array}
if y < -3.1000000000000001e63 or 5.4000000000000002e112 < y Initial program 99.7%
Taylor expanded in x around 0
+-commutativeN/A
lower-fma.f64N/A
lift-sqrt.f6498.3
Applied rewrites98.3%
if -3.1000000000000001e63 < y < 5.4000000000000002e112Initial program 100.0%
Taylor expanded in y around 0
lift--.f6495.5
Applied rewrites95.5%
Final simplification96.3%
(FPCore (x y) :precision binary64 (if (or (<= y -3.1e+95) (not (<= y 5.4e+112))) (* (sqrt x) y) (- 1.0 x)))
double code(double x, double y) {
double tmp;
if ((y <= -3.1e+95) || !(y <= 5.4e+112)) {
tmp = sqrt(x) * y;
} else {
tmp = 1.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 <= (-3.1d+95)) .or. (.not. (y <= 5.4d+112))) then
tmp = sqrt(x) * y
else
tmp = 1.0d0 - x
end if
code = tmp
end function
public static double code(double x, double y) {
double tmp;
if ((y <= -3.1e+95) || !(y <= 5.4e+112)) {
tmp = Math.sqrt(x) * y;
} else {
tmp = 1.0 - x;
}
return tmp;
}
def code(x, y): tmp = 0 if (y <= -3.1e+95) or not (y <= 5.4e+112): tmp = math.sqrt(x) * y else: tmp = 1.0 - x return tmp
function code(x, y) tmp = 0.0 if ((y <= -3.1e+95) || !(y <= 5.4e+112)) tmp = Float64(sqrt(x) * y); else tmp = Float64(1.0 - x); end return tmp end
function tmp_2 = code(x, y) tmp = 0.0; if ((y <= -3.1e+95) || ~((y <= 5.4e+112))) tmp = sqrt(x) * y; else tmp = 1.0 - x; end tmp_2 = tmp; end
code[x_, y_] := If[Or[LessEqual[y, -3.1e+95], N[Not[LessEqual[y, 5.4e+112]], $MachinePrecision]], N[(N[Sqrt[x], $MachinePrecision] * y), $MachinePrecision], N[(1.0 - x), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;y \leq -3.1 \cdot 10^{+95} \lor \neg \left(y \leq 5.4 \cdot 10^{+112}\right):\\
\;\;\;\;\sqrt{x} \cdot y\\
\mathbf{else}:\\
\;\;\;\;1 - x\\
\end{array}
\end{array}
if y < -3.1000000000000003e95 or 5.4000000000000002e112 < y Initial program 99.7%
Taylor expanded in y around inf
lower-*.f64N/A
lift-sqrt.f6497.9
Applied rewrites97.9%
if -3.1000000000000003e95 < y < 5.4000000000000002e112Initial program 100.0%
Taylor expanded in y around 0
lift--.f6494.7
Applied rewrites94.7%
Final simplification95.6%
(FPCore (x y) :precision binary64 (if (<= x 1.0) (fma (sqrt x) y 1.0) (fma (sqrt x) y (- x))))
double code(double x, double y) {
double tmp;
if (x <= 1.0) {
tmp = fma(sqrt(x), y, 1.0);
} else {
tmp = fma(sqrt(x), y, -x);
}
return tmp;
}
function code(x, y) tmp = 0.0 if (x <= 1.0) tmp = fma(sqrt(x), y, 1.0); else tmp = fma(sqrt(x), y, Float64(-x)); end return tmp end
code[x_, y_] := If[LessEqual[x, 1.0], N[(N[Sqrt[x], $MachinePrecision] * y + 1.0), $MachinePrecision], N[(N[Sqrt[x], $MachinePrecision] * y + (-x)), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq 1:\\
\;\;\;\;\mathsf{fma}\left(\sqrt{x}, y, 1\right)\\
\mathbf{else}:\\
\;\;\;\;\mathsf{fma}\left(\sqrt{x}, y, -x\right)\\
\end{array}
\end{array}
if x < 1Initial program 99.9%
Taylor expanded in x around 0
+-commutativeN/A
lower-fma.f64N/A
lift-sqrt.f6497.1
Applied rewrites97.1%
if 1 < x Initial program 99.9%
lift-+.f64N/A
lift--.f64N/A
lift-*.f64N/A
lift-sqrt.f64N/A
+-commutativeN/A
*-commutativeN/A
lower-fma.f64N/A
lift-sqrt.f64N/A
lift--.f6499.9
Applied rewrites99.9%
Taylor expanded in x around inf
mul-1-negN/A
lower-neg.f6498.1
Applied rewrites98.1%
(FPCore (x y) :precision binary64 (- 1.0 x))
double code(double x, double y) {
return 1.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)
use fmin_fmax_functions
real(8), intent (in) :: x
real(8), intent (in) :: y
code = 1.0d0 - x
end function
public static double code(double x, double y) {
return 1.0 - x;
}
def code(x, y): return 1.0 - x
function code(x, y) return Float64(1.0 - x) end
function tmp = code(x, y) tmp = 1.0 - x; end
code[x_, y_] := N[(1.0 - x), $MachinePrecision]
\begin{array}{l}
\\
1 - x
\end{array}
Initial program 99.9%
Taylor expanded in y around 0
lift--.f6467.7
Applied rewrites67.7%
(FPCore (x y) :precision binary64 1.0)
double code(double x, double y) {
return 1.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 = 1.0d0
end function
public static double code(double x, double y) {
return 1.0;
}
def code(x, y): return 1.0
function code(x, y) return 1.0 end
function tmp = code(x, y) tmp = 1.0; end
code[x_, y_] := 1.0
\begin{array}{l}
\\
1
\end{array}
Initial program 99.9%
Taylor expanded in y around 0
lift--.f6467.7
Applied rewrites67.7%
Taylor expanded in x around 0
Applied rewrites31.5%
herbie shell --seed 2025051
(FPCore (x y)
:name "Numeric.SpecFunctions:invIncompleteBetaWorker from math-functions-0.1.5.2, E"
:precision binary64
(+ (- 1.0 x) (* y (sqrt x))))