
(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]
\sqrt{x \cdot x + y}
Herbie found 6 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]
\sqrt{x \cdot x + y}
(FPCore (x y) :precision binary64 (if (<= (fabs x) 3.8e+129) (sqrt (+ (* (fabs x) (fabs x)) y)) (fma (/ y (fabs x)) 0.5 (fabs x))))
double code(double x, double y) {
double tmp;
if (fabs(x) <= 3.8e+129) {
tmp = sqrt(((fabs(x) * fabs(x)) + y));
} else {
tmp = fma((y / fabs(x)), 0.5, fabs(x));
}
return tmp;
}
function code(x, y) tmp = 0.0 if (abs(x) <= 3.8e+129) tmp = sqrt(Float64(Float64(abs(x) * abs(x)) + y)); else tmp = fma(Float64(y / abs(x)), 0.5, abs(x)); end return tmp end
code[x_, y_] := If[LessEqual[N[Abs[x], $MachinePrecision], 3.8e+129], N[Sqrt[N[(N[(N[Abs[x], $MachinePrecision] * N[Abs[x], $MachinePrecision]), $MachinePrecision] + y), $MachinePrecision]], $MachinePrecision], N[(N[(y / N[Abs[x], $MachinePrecision]), $MachinePrecision] * 0.5 + N[Abs[x], $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\mathbf{if}\;\left|x\right| \leq 3.8 \cdot 10^{+129}:\\
\;\;\;\;\sqrt{\left|x\right| \cdot \left|x\right| + y}\\
\mathbf{else}:\\
\;\;\;\;\mathsf{fma}\left(\frac{y}{\left|x\right|}, 0.5, \left|x\right|\right)\\
\end{array}
if x < 3.80000000000000005e129Initial program 69.8%
if 3.80000000000000005e129 < x Initial program 69.8%
Taylor expanded in x around inf
lower-*.f64N/A
lower-+.f64N/A
lower-*.f64N/A
lower-/.f64N/A
lower-pow.f6433.7%
Applied rewrites33.7%
lift-*.f64N/A
lift-+.f64N/A
+-commutativeN/A
distribute-rgt-inN/A
Applied rewrites33.9%
(FPCore (x y) :precision binary64 (if (<= (fabs x) 4e+129) (sqrt (+ (* (fabs x) (fabs x)) y)) (* (fabs x) 1.0)))
double code(double x, double y) {
double tmp;
if (fabs(x) <= 4e+129) {
tmp = sqrt(((fabs(x) * fabs(x)) + y));
} else {
tmp = fabs(x) * 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 (abs(x) <= 4d+129) then
tmp = sqrt(((abs(x) * abs(x)) + y))
else
tmp = abs(x) * 1.0d0
end if
code = tmp
end function
public static double code(double x, double y) {
double tmp;
if (Math.abs(x) <= 4e+129) {
tmp = Math.sqrt(((Math.abs(x) * Math.abs(x)) + y));
} else {
tmp = Math.abs(x) * 1.0;
}
return tmp;
}
def code(x, y): tmp = 0 if math.fabs(x) <= 4e+129: tmp = math.sqrt(((math.fabs(x) * math.fabs(x)) + y)) else: tmp = math.fabs(x) * 1.0 return tmp
function code(x, y) tmp = 0.0 if (abs(x) <= 4e+129) tmp = sqrt(Float64(Float64(abs(x) * abs(x)) + y)); else tmp = Float64(abs(x) * 1.0); end return tmp end
function tmp_2 = code(x, y) tmp = 0.0; if (abs(x) <= 4e+129) tmp = sqrt(((abs(x) * abs(x)) + y)); else tmp = abs(x) * 1.0; end tmp_2 = tmp; end
code[x_, y_] := If[LessEqual[N[Abs[x], $MachinePrecision], 4e+129], N[Sqrt[N[(N[(N[Abs[x], $MachinePrecision] * N[Abs[x], $MachinePrecision]), $MachinePrecision] + y), $MachinePrecision]], $MachinePrecision], N[(N[Abs[x], $MachinePrecision] * 1.0), $MachinePrecision]]
\begin{array}{l}
\mathbf{if}\;\left|x\right| \leq 4 \cdot 10^{+129}:\\
\;\;\;\;\sqrt{\left|x\right| \cdot \left|x\right| + y}\\
\mathbf{else}:\\
\;\;\;\;\left|x\right| \cdot 1\\
\end{array}
if x < 4e129Initial program 69.8%
if 4e129 < x Initial program 69.8%
Taylor expanded in x around inf
lower-*.f64N/A
lower-+.f64N/A
lower-*.f64N/A
lower-/.f64N/A
lower-pow.f6433.7%
Applied rewrites33.7%
Taylor expanded in x around inf
Applied rewrites33.8%
(FPCore (x y) :precision binary64 (if (<= (fabs x) 4e+129) (sqrt (fma (fabs x) (fabs x) y)) (* (fabs x) 1.0)))
double code(double x, double y) {
double tmp;
if (fabs(x) <= 4e+129) {
tmp = sqrt(fma(fabs(x), fabs(x), y));
} else {
tmp = fabs(x) * 1.0;
}
return tmp;
}
function code(x, y) tmp = 0.0 if (abs(x) <= 4e+129) tmp = sqrt(fma(abs(x), abs(x), y)); else tmp = Float64(abs(x) * 1.0); end return tmp end
code[x_, y_] := If[LessEqual[N[Abs[x], $MachinePrecision], 4e+129], N[Sqrt[N[(N[Abs[x], $MachinePrecision] * N[Abs[x], $MachinePrecision] + y), $MachinePrecision]], $MachinePrecision], N[(N[Abs[x], $MachinePrecision] * 1.0), $MachinePrecision]]
\begin{array}{l}
\mathbf{if}\;\left|x\right| \leq 4 \cdot 10^{+129}:\\
\;\;\;\;\sqrt{\mathsf{fma}\left(\left|x\right|, \left|x\right|, y\right)}\\
\mathbf{else}:\\
\;\;\;\;\left|x\right| \cdot 1\\
\end{array}
if x < 4e129Initial program 69.8%
lift-+.f64N/A
add-flipN/A
sub-flipN/A
lift-*.f64N/A
remove-double-negN/A
lower-fma.f6469.9%
Applied rewrites69.9%
if 4e129 < x Initial program 69.8%
Taylor expanded in x around inf
lower-*.f64N/A
lower-+.f64N/A
lower-*.f64N/A
lower-/.f64N/A
lower-pow.f6433.7%
Applied rewrites33.7%
Taylor expanded in x around inf
Applied rewrites33.8%
(FPCore (x y) :precision binary64 (if (<= (fabs x) 2.8e-44) (sqrt y) (* (fabs x) 1.0)))
double code(double x, double y) {
double tmp;
if (fabs(x) <= 2.8e-44) {
tmp = sqrt(y);
} else {
tmp = fabs(x) * 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 (abs(x) <= 2.8d-44) then
tmp = sqrt(y)
else
tmp = abs(x) * 1.0d0
end if
code = tmp
end function
public static double code(double x, double y) {
double tmp;
if (Math.abs(x) <= 2.8e-44) {
tmp = Math.sqrt(y);
} else {
tmp = Math.abs(x) * 1.0;
}
return tmp;
}
def code(x, y): tmp = 0 if math.fabs(x) <= 2.8e-44: tmp = math.sqrt(y) else: tmp = math.fabs(x) * 1.0 return tmp
function code(x, y) tmp = 0.0 if (abs(x) <= 2.8e-44) tmp = sqrt(y); else tmp = Float64(abs(x) * 1.0); end return tmp end
function tmp_2 = code(x, y) tmp = 0.0; if (abs(x) <= 2.8e-44) tmp = sqrt(y); else tmp = abs(x) * 1.0; end tmp_2 = tmp; end
code[x_, y_] := If[LessEqual[N[Abs[x], $MachinePrecision], 2.8e-44], N[Sqrt[y], $MachinePrecision], N[(N[Abs[x], $MachinePrecision] * 1.0), $MachinePrecision]]
\begin{array}{l}
\mathbf{if}\;\left|x\right| \leq 2.8 \cdot 10^{-44}:\\
\;\;\;\;\sqrt{y}\\
\mathbf{else}:\\
\;\;\;\;\left|x\right| \cdot 1\\
\end{array}
if x < 2.8e-44Initial program 69.8%
Taylor expanded in x around 0
Applied rewrites36.1%
if 2.8e-44 < x Initial program 69.8%
Taylor expanded in x around inf
lower-*.f64N/A
lower-+.f64N/A
lower-*.f64N/A
lower-/.f64N/A
lower-pow.f6433.7%
Applied rewrites33.7%
Taylor expanded in x around inf
Applied rewrites33.8%
(FPCore (x y) :precision binary64 (sqrt y))
double code(double x, double y) {
return sqrt(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(y)
end function
public static double code(double x, double y) {
return Math.sqrt(y);
}
def code(x, y): return math.sqrt(y)
function code(x, y) return sqrt(y) end
function tmp = code(x, y) tmp = sqrt(y); end
code[x_, y_] := N[Sqrt[y], $MachinePrecision]
\sqrt{y}
Initial program 69.8%
Taylor expanded in x around 0
Applied rewrites36.1%
(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 Float64(-x) end
function tmp = code(x, y) tmp = -x; end
code[x_, y_] := (-x)
-x
Initial program 69.8%
Taylor expanded in x around -inf
lower-*.f6433.8%
Applied rewrites33.8%
lift-*.f64N/A
mul-1-negN/A
lower-neg.f6433.8%
Applied rewrites33.8%
herbie shell --seed 2025183
(FPCore (x y)
:name "Linear.Quaternion:$clog from linear-1.19.1.3"
:precision binary64
(sqrt (+ (* x x) y)))