
(FPCore (x y) :precision binary64 (sqrt (+ (* x x) y)))
double code(double x, double y) {
return sqrt(((x * 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 = sqrt(((x * x) + y))
end function
public static double code(double x, double y) {
return Math.sqrt(((x * x) + y));
}
def code(x, y): return math.sqrt(((x * x) + y))
function code(x, y) return sqrt(Float64(Float64(x * x) + y)) end
function tmp = code(x, y) tmp = sqrt(((x * x) + y)); end
code[x_, y_] := N[Sqrt[N[(N[(x * x), $MachinePrecision] + y), $MachinePrecision]], $MachinePrecision]
\begin{array}{l}
\\
\sqrt{x \cdot x + y}
\end{array}
Herbie found 5 alternatives:
| Alternative | Accuracy | Speedup |
|---|
(FPCore (x y) :precision binary64 (sqrt (+ (* x x) y)))
double code(double x, double y) {
return sqrt(((x * 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 = sqrt(((x * x) + y))
end function
public static double code(double x, double y) {
return Math.sqrt(((x * x) + y));
}
def code(x, y): return math.sqrt(((x * x) + y))
function code(x, y) return sqrt(Float64(Float64(x * x) + y)) end
function tmp = code(x, y) tmp = sqrt(((x * x) + y)); end
code[x_, y_] := N[Sqrt[N[(N[(x * x), $MachinePrecision] + y), $MachinePrecision]], $MachinePrecision]
\begin{array}{l}
\\
\sqrt{x \cdot x + y}
\end{array}
x_m = (fabs.f64 x) (FPCore (x_m y) :precision binary64 (if (<= x_m 5e+110) (sqrt (+ (* x_m x_m) y)) (* x_m 1.0)))
x_m = fabs(x);
double code(double x_m, double y) {
double tmp;
if (x_m <= 5e+110) {
tmp = sqrt(((x_m * x_m) + y));
} else {
tmp = x_m * 1.0;
}
return tmp;
}
x_m = private
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_m, y)
use fmin_fmax_functions
real(8), intent (in) :: x_m
real(8), intent (in) :: y
real(8) :: tmp
if (x_m <= 5d+110) then
tmp = sqrt(((x_m * x_m) + y))
else
tmp = x_m * 1.0d0
end if
code = tmp
end function
x_m = Math.abs(x);
public static double code(double x_m, double y) {
double tmp;
if (x_m <= 5e+110) {
tmp = Math.sqrt(((x_m * x_m) + y));
} else {
tmp = x_m * 1.0;
}
return tmp;
}
x_m = math.fabs(x) def code(x_m, y): tmp = 0 if x_m <= 5e+110: tmp = math.sqrt(((x_m * x_m) + y)) else: tmp = x_m * 1.0 return tmp
x_m = abs(x) function code(x_m, y) tmp = 0.0 if (x_m <= 5e+110) tmp = sqrt(Float64(Float64(x_m * x_m) + y)); else tmp = Float64(x_m * 1.0); end return tmp end
x_m = abs(x); function tmp_2 = code(x_m, y) tmp = 0.0; if (x_m <= 5e+110) tmp = sqrt(((x_m * x_m) + y)); else tmp = x_m * 1.0; end tmp_2 = tmp; end
x_m = N[Abs[x], $MachinePrecision] code[x$95$m_, y_] := If[LessEqual[x$95$m, 5e+110], N[Sqrt[N[(N[(x$95$m * x$95$m), $MachinePrecision] + y), $MachinePrecision]], $MachinePrecision], N[(x$95$m * 1.0), $MachinePrecision]]
\begin{array}{l}
x_m = \left|x\right|
\\
\begin{array}{l}
\mathbf{if}\;x\_m \leq 5 \cdot 10^{+110}:\\
\;\;\;\;\sqrt{x\_m \cdot x\_m + y}\\
\mathbf{else}:\\
\;\;\;\;x\_m \cdot 1\\
\end{array}
\end{array}
if x < 4.99999999999999978e110Initial program 68.8%
if 4.99999999999999978e110 < x Initial program 68.8%
Taylor expanded in x around inf
Applied rewrites68.1%
Taylor expanded in x around inf
Applied rewrites67.8%
x_m = (fabs.f64 x) (FPCore (x_m y) :precision binary64 (if (<= x_m 5e+110) (sqrt (fma x_m x_m y)) (* x_m 1.0)))
x_m = fabs(x);
double code(double x_m, double y) {
double tmp;
if (x_m <= 5e+110) {
tmp = sqrt(fma(x_m, x_m, y));
} else {
tmp = x_m * 1.0;
}
return tmp;
}
x_m = abs(x) function code(x_m, y) tmp = 0.0 if (x_m <= 5e+110) tmp = sqrt(fma(x_m, x_m, y)); else tmp = Float64(x_m * 1.0); end return tmp end
x_m = N[Abs[x], $MachinePrecision] code[x$95$m_, y_] := If[LessEqual[x$95$m, 5e+110], N[Sqrt[N[(x$95$m * x$95$m + y), $MachinePrecision]], $MachinePrecision], N[(x$95$m * 1.0), $MachinePrecision]]
\begin{array}{l}
x_m = \left|x\right|
\\
\begin{array}{l}
\mathbf{if}\;x\_m \leq 5 \cdot 10^{+110}:\\
\;\;\;\;\sqrt{\mathsf{fma}\left(x\_m, x\_m, y\right)}\\
\mathbf{else}:\\
\;\;\;\;x\_m \cdot 1\\
\end{array}
\end{array}
if x < 4.99999999999999978e110Initial program 68.8%
Applied rewrites68.8%
if 4.99999999999999978e110 < x Initial program 68.8%
Taylor expanded in x around inf
Applied rewrites68.1%
Taylor expanded in x around inf
Applied rewrites67.8%
x_m = (fabs.f64 x) (FPCore (x_m y) :precision binary64 (if (<= x_m 1.4e-62) (sqrt y) (* x_m 1.0)))
x_m = fabs(x);
double code(double x_m, double y) {
double tmp;
if (x_m <= 1.4e-62) {
tmp = sqrt(y);
} else {
tmp = x_m * 1.0;
}
return tmp;
}
x_m = private
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_m, y)
use fmin_fmax_functions
real(8), intent (in) :: x_m
real(8), intent (in) :: y
real(8) :: tmp
if (x_m <= 1.4d-62) then
tmp = sqrt(y)
else
tmp = x_m * 1.0d0
end if
code = tmp
end function
x_m = Math.abs(x);
public static double code(double x_m, double y) {
double tmp;
if (x_m <= 1.4e-62) {
tmp = Math.sqrt(y);
} else {
tmp = x_m * 1.0;
}
return tmp;
}
x_m = math.fabs(x) def code(x_m, y): tmp = 0 if x_m <= 1.4e-62: tmp = math.sqrt(y) else: tmp = x_m * 1.0 return tmp
x_m = abs(x) function code(x_m, y) tmp = 0.0 if (x_m <= 1.4e-62) tmp = sqrt(y); else tmp = Float64(x_m * 1.0); end return tmp end
x_m = abs(x); function tmp_2 = code(x_m, y) tmp = 0.0; if (x_m <= 1.4e-62) tmp = sqrt(y); else tmp = x_m * 1.0; end tmp_2 = tmp; end
x_m = N[Abs[x], $MachinePrecision] code[x$95$m_, y_] := If[LessEqual[x$95$m, 1.4e-62], N[Sqrt[y], $MachinePrecision], N[(x$95$m * 1.0), $MachinePrecision]]
\begin{array}{l}
x_m = \left|x\right|
\\
\begin{array}{l}
\mathbf{if}\;x\_m \leq 1.4 \cdot 10^{-62}:\\
\;\;\;\;\sqrt{y}\\
\mathbf{else}:\\
\;\;\;\;x\_m \cdot 1\\
\end{array}
\end{array}
if x < 1.40000000000000001e-62Initial program 68.8%
Taylor expanded in x around 0
Applied rewrites34.6%
if 1.40000000000000001e-62 < x Initial program 68.8%
Taylor expanded in x around inf
Applied rewrites68.1%
Taylor expanded in x around inf
Applied rewrites67.8%
x_m = (fabs.f64 x) (FPCore (x_m y) :precision binary64 (sqrt y))
x_m = fabs(x);
double code(double x_m, double y) {
return sqrt(y);
}
x_m = private
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_m, y)
use fmin_fmax_functions
real(8), intent (in) :: x_m
real(8), intent (in) :: y
code = sqrt(y)
end function
x_m = Math.abs(x);
public static double code(double x_m, double y) {
return Math.sqrt(y);
}
x_m = math.fabs(x) def code(x_m, y): return math.sqrt(y)
x_m = abs(x) function code(x_m, y) return sqrt(y) end
x_m = abs(x); function tmp = code(x_m, y) tmp = sqrt(y); end
x_m = N[Abs[x], $MachinePrecision] code[x$95$m_, y_] := N[Sqrt[y], $MachinePrecision]
\begin{array}{l}
x_m = \left|x\right|
\\
\sqrt{y}
\end{array}
Initial program 68.8%
Taylor expanded in x around 0
Applied rewrites34.6%
x_m = (fabs.f64 x) (FPCore (x_m y) :precision binary64 (- x_m))
x_m = fabs(x);
double code(double x_m, double y) {
return -x_m;
}
x_m = private
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_m, y)
use fmin_fmax_functions
real(8), intent (in) :: x_m
real(8), intent (in) :: y
code = -x_m
end function
x_m = Math.abs(x);
public static double code(double x_m, double y) {
return -x_m;
}
x_m = math.fabs(x) def code(x_m, y): return -x_m
x_m = abs(x) function code(x_m, y) return Float64(-x_m) end
x_m = abs(x); function tmp = code(x_m, y) tmp = -x_m; end
x_m = N[Abs[x], $MachinePrecision] code[x$95$m_, y_] := (-x$95$m)
\begin{array}{l}
x_m = \left|x\right|
\\
-x\_m
\end{array}
Initial program 68.8%
Taylor expanded in x around -inf
Applied rewrites1.2%
Applied rewrites1.2%
herbie shell --seed 2025153
(FPCore (x y)
:name "Linear.Quaternion:$clog from linear-1.19.1.3"
:precision binary64
(sqrt (+ (* x x) y)))