
(FPCore (p x) :precision binary64 (sqrt (* 0.5 (+ 1.0 (/ x (sqrt (+ (* (* 4.0 p) p) (* x x))))))))
double code(double p, double x) {
return sqrt((0.5 * (1.0 + (x / sqrt((((4.0 * p) * p) + (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(p, x)
use fmin_fmax_functions
real(8), intent (in) :: p
real(8), intent (in) :: x
code = sqrt((0.5d0 * (1.0d0 + (x / sqrt((((4.0d0 * p) * p) + (x * x)))))))
end function
public static double code(double p, double x) {
return Math.sqrt((0.5 * (1.0 + (x / Math.sqrt((((4.0 * p) * p) + (x * x)))))));
}
def code(p, x): return math.sqrt((0.5 * (1.0 + (x / math.sqrt((((4.0 * p) * p) + (x * x)))))))
function code(p, x) return sqrt(Float64(0.5 * Float64(1.0 + Float64(x / sqrt(Float64(Float64(Float64(4.0 * p) * p) + Float64(x * x))))))) end
function tmp = code(p, x) tmp = sqrt((0.5 * (1.0 + (x / sqrt((((4.0 * p) * p) + (x * x))))))); end
code[p_, x_] := N[Sqrt[N[(0.5 * N[(1.0 + N[(x / N[Sqrt[N[(N[(N[(4.0 * p), $MachinePrecision] * p), $MachinePrecision] + N[(x * x), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]
\sqrt{0.5 \cdot \left(1 + \frac{x}{\sqrt{\left(4 \cdot p\right) \cdot p + x \cdot x}}\right)}
Herbie found 5 alternatives:
| Alternative | Accuracy | Speedup |
|---|
(FPCore (p x) :precision binary64 (sqrt (* 0.5 (+ 1.0 (/ x (sqrt (+ (* (* 4.0 p) p) (* x x))))))))
double code(double p, double x) {
return sqrt((0.5 * (1.0 + (x / sqrt((((4.0 * p) * p) + (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(p, x)
use fmin_fmax_functions
real(8), intent (in) :: p
real(8), intent (in) :: x
code = sqrt((0.5d0 * (1.0d0 + (x / sqrt((((4.0d0 * p) * p) + (x * x)))))))
end function
public static double code(double p, double x) {
return Math.sqrt((0.5 * (1.0 + (x / Math.sqrt((((4.0 * p) * p) + (x * x)))))));
}
def code(p, x): return math.sqrt((0.5 * (1.0 + (x / math.sqrt((((4.0 * p) * p) + (x * x)))))))
function code(p, x) return sqrt(Float64(0.5 * Float64(1.0 + Float64(x / sqrt(Float64(Float64(Float64(4.0 * p) * p) + Float64(x * x))))))) end
function tmp = code(p, x) tmp = sqrt((0.5 * (1.0 + (x / sqrt((((4.0 * p) * p) + (x * x))))))); end
code[p_, x_] := N[Sqrt[N[(0.5 * N[(1.0 + N[(x / N[Sqrt[N[(N[(N[(4.0 * p), $MachinePrecision] * p), $MachinePrecision] + N[(x * x), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]
\sqrt{0.5 \cdot \left(1 + \frac{x}{\sqrt{\left(4 \cdot p\right) \cdot p + x \cdot x}}\right)}
(FPCore (p x)
:precision binary64
(if (<=
(sqrt (* 0.5 (+ 1.0 (/ x (sqrt (+ (* (* 4.0 p) p) (* x x)))))))
0.0)
(/ (- (fabs p)) x)
(sqrt (fma (/ 0.5 (sqrt (fma (* p 4.0) p (* x x)))) x 0.5))))double code(double p, double x) {
double tmp;
if (sqrt((0.5 * (1.0 + (x / sqrt((((4.0 * p) * p) + (x * x))))))) <= 0.0) {
tmp = -fabs(p) / x;
} else {
tmp = sqrt(fma((0.5 / sqrt(fma((p * 4.0), p, (x * x)))), x, 0.5));
}
return tmp;
}
function code(p, x) tmp = 0.0 if (sqrt(Float64(0.5 * Float64(1.0 + Float64(x / sqrt(Float64(Float64(Float64(4.0 * p) * p) + Float64(x * x))))))) <= 0.0) tmp = Float64(Float64(-abs(p)) / x); else tmp = sqrt(fma(Float64(0.5 / sqrt(fma(Float64(p * 4.0), p, Float64(x * x)))), x, 0.5)); end return tmp end
code[p_, x_] := If[LessEqual[N[Sqrt[N[(0.5 * N[(1.0 + N[(x / N[Sqrt[N[(N[(N[(4.0 * p), $MachinePrecision] * p), $MachinePrecision] + N[(x * x), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision], 0.0], N[((-N[Abs[p], $MachinePrecision]) / x), $MachinePrecision], N[Sqrt[N[(N[(0.5 / N[Sqrt[N[(N[(p * 4.0), $MachinePrecision] * p + N[(x * x), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision] * x + 0.5), $MachinePrecision]], $MachinePrecision]]
\begin{array}{l}
\mathbf{if}\;\sqrt{0.5 \cdot \left(1 + \frac{x}{\sqrt{\left(4 \cdot p\right) \cdot p + x \cdot x}}\right)} \leq 0:\\
\;\;\;\;\frac{-\left|p\right|}{x}\\
\mathbf{else}:\\
\;\;\;\;\sqrt{\mathsf{fma}\left(\frac{0.5}{\sqrt{\mathsf{fma}\left(p \cdot 4, p, x \cdot x\right)}}, x, 0.5\right)}\\
\end{array}
if (sqrt.f64 (*.f64 #s(literal 1/2 binary64) (+.f64 #s(literal 1 binary64) (/.f64 x (sqrt.f64 (+.f64 (*.f64 (*.f64 #s(literal 4 binary64) p) p) (*.f64 x x))))))) < 0.0Initial program 79.5%
Taylor expanded in x around -inf
lower-*.f64N/A
lower-/.f64N/A
lower-sqrt.f64N/A
lower-pow.f6418.0%
Applied rewrites18.0%
lift-*.f64N/A
mul-1-negN/A
lift-/.f64N/A
distribute-neg-fracN/A
lower-/.f64N/A
lower-neg.f6418.0%
lift-sqrt.f64N/A
lift-pow.f64N/A
unpow2N/A
rem-sqrt-squareN/A
lower-fabs.f6426.7%
Applied rewrites26.7%
if 0.0 < (sqrt.f64 (*.f64 #s(literal 1/2 binary64) (+.f64 #s(literal 1 binary64) (/.f64 x (sqrt.f64 (+.f64 (*.f64 (*.f64 #s(literal 4 binary64) p) p) (*.f64 x x))))))) Initial program 79.5%
lift-*.f64N/A
lift-+.f64N/A
+-commutativeN/A
distribute-rgt-inN/A
lift-/.f64N/A
associate-*l/N/A
associate-/l*N/A
*-commutativeN/A
metadata-evalN/A
lower-fma.f64N/A
Applied rewrites76.3%
(FPCore (p x)
:precision binary64
(let* ((t_0
(sqrt
(*
0.5
(+
1.0
(/ x (sqrt (+ (* (* 4.0 (fabs p)) (fabs p)) (* x x)))))))))
(if (<= t_0 0.0)
(/ (- (fabs (fabs p))) x)
(if (<= t_0 0.7071067811865483)
(sqrt (fma (/ x (fabs p)) 0.25 0.5))
(sqrt 1.0)))))double code(double p, double x) {
double t_0 = sqrt((0.5 * (1.0 + (x / sqrt((((4.0 * fabs(p)) * fabs(p)) + (x * x)))))));
double tmp;
if (t_0 <= 0.0) {
tmp = -fabs(fabs(p)) / x;
} else if (t_0 <= 0.7071067811865483) {
tmp = sqrt(fma((x / fabs(p)), 0.25, 0.5));
} else {
tmp = sqrt(1.0);
}
return tmp;
}
function code(p, x) t_0 = sqrt(Float64(0.5 * Float64(1.0 + Float64(x / sqrt(Float64(Float64(Float64(4.0 * abs(p)) * abs(p)) + Float64(x * x))))))) tmp = 0.0 if (t_0 <= 0.0) tmp = Float64(Float64(-abs(abs(p))) / x); elseif (t_0 <= 0.7071067811865483) tmp = sqrt(fma(Float64(x / abs(p)), 0.25, 0.5)); else tmp = sqrt(1.0); end return tmp end
code[p_, x_] := Block[{t$95$0 = N[Sqrt[N[(0.5 * N[(1.0 + N[(x / N[Sqrt[N[(N[(N[(4.0 * N[Abs[p], $MachinePrecision]), $MachinePrecision] * N[Abs[p], $MachinePrecision]), $MachinePrecision] + N[(x * x), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]}, If[LessEqual[t$95$0, 0.0], N[((-N[Abs[N[Abs[p], $MachinePrecision]], $MachinePrecision]) / x), $MachinePrecision], If[LessEqual[t$95$0, 0.7071067811865483], N[Sqrt[N[(N[(x / N[Abs[p], $MachinePrecision]), $MachinePrecision] * 0.25 + 0.5), $MachinePrecision]], $MachinePrecision], N[Sqrt[1.0], $MachinePrecision]]]]
\begin{array}{l}
t_0 := \sqrt{0.5 \cdot \left(1 + \frac{x}{\sqrt{\left(4 \cdot \left|p\right|\right) \cdot \left|p\right| + x \cdot x}}\right)}\\
\mathbf{if}\;t\_0 \leq 0:\\
\;\;\;\;\frac{-\left|\left|p\right|\right|}{x}\\
\mathbf{elif}\;t\_0 \leq 0.7071067811865483:\\
\;\;\;\;\sqrt{\mathsf{fma}\left(\frac{x}{\left|p\right|}, 0.25, 0.5\right)}\\
\mathbf{else}:\\
\;\;\;\;\sqrt{1}\\
\end{array}
if (sqrt.f64 (*.f64 #s(literal 1/2 binary64) (+.f64 #s(literal 1 binary64) (/.f64 x (sqrt.f64 (+.f64 (*.f64 (*.f64 #s(literal 4 binary64) p) p) (*.f64 x x))))))) < 0.0Initial program 79.5%
Taylor expanded in x around -inf
lower-*.f64N/A
lower-/.f64N/A
lower-sqrt.f64N/A
lower-pow.f6418.0%
Applied rewrites18.0%
lift-*.f64N/A
mul-1-negN/A
lift-/.f64N/A
distribute-neg-fracN/A
lower-/.f64N/A
lower-neg.f6418.0%
lift-sqrt.f64N/A
lift-pow.f64N/A
unpow2N/A
rem-sqrt-squareN/A
lower-fabs.f6426.7%
Applied rewrites26.7%
if 0.0 < (sqrt.f64 (*.f64 #s(literal 1/2 binary64) (+.f64 #s(literal 1 binary64) (/.f64 x (sqrt.f64 (+.f64 (*.f64 (*.f64 #s(literal 4 binary64) p) p) (*.f64 x x))))))) < 0.70710678118654835Initial program 79.5%
Taylor expanded in p around inf
lower-+.f64N/A
lower-*.f64N/A
lower-/.f6451.3%
Applied rewrites51.3%
lift-+.f64N/A
+-commutativeN/A
lift-*.f64N/A
*-commutativeN/A
lower-fma.f6451.3%
Applied rewrites51.3%
if 0.70710678118654835 < (sqrt.f64 (*.f64 #s(literal 1/2 binary64) (+.f64 #s(literal 1 binary64) (/.f64 x (sqrt.f64 (+.f64 (*.f64 (*.f64 #s(literal 4 binary64) p) p) (*.f64 x x))))))) Initial program 79.5%
Taylor expanded in p around -inf
lower-+.f64N/A
lower-*.f64N/A
lower-/.f6451.2%
Applied rewrites51.2%
lift-+.f64N/A
+-commutativeN/A
lift-*.f64N/A
*-commutativeN/A
lower-fma.f6451.2%
Applied rewrites51.2%
Taylor expanded in x around inf
Applied rewrites35.7%
(FPCore (p x)
:precision binary64
(let* ((t_0
(sqrt
(* 0.5 (+ 1.0 (/ x (sqrt (+ (* (* 4.0 p) p) (* x x)))))))))
(if (<= t_0 0.0)
(/ (- (fabs p)) x)
(if (<= t_0 0.7071067811865483) (sqrt 0.5) (sqrt 1.0)))))double code(double p, double x) {
double t_0 = sqrt((0.5 * (1.0 + (x / sqrt((((4.0 * p) * p) + (x * x)))))));
double tmp;
if (t_0 <= 0.0) {
tmp = -fabs(p) / x;
} else if (t_0 <= 0.7071067811865483) {
tmp = sqrt(0.5);
} else {
tmp = sqrt(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(p, x)
use fmin_fmax_functions
real(8), intent (in) :: p
real(8), intent (in) :: x
real(8) :: t_0
real(8) :: tmp
t_0 = sqrt((0.5d0 * (1.0d0 + (x / sqrt((((4.0d0 * p) * p) + (x * x)))))))
if (t_0 <= 0.0d0) then
tmp = -abs(p) / x
else if (t_0 <= 0.7071067811865483d0) then
tmp = sqrt(0.5d0)
else
tmp = sqrt(1.0d0)
end if
code = tmp
end function
public static double code(double p, double x) {
double t_0 = Math.sqrt((0.5 * (1.0 + (x / Math.sqrt((((4.0 * p) * p) + (x * x)))))));
double tmp;
if (t_0 <= 0.0) {
tmp = -Math.abs(p) / x;
} else if (t_0 <= 0.7071067811865483) {
tmp = Math.sqrt(0.5);
} else {
tmp = Math.sqrt(1.0);
}
return tmp;
}
def code(p, x): t_0 = math.sqrt((0.5 * (1.0 + (x / math.sqrt((((4.0 * p) * p) + (x * x))))))) tmp = 0 if t_0 <= 0.0: tmp = -math.fabs(p) / x elif t_0 <= 0.7071067811865483: tmp = math.sqrt(0.5) else: tmp = math.sqrt(1.0) return tmp
function code(p, x) t_0 = sqrt(Float64(0.5 * Float64(1.0 + Float64(x / sqrt(Float64(Float64(Float64(4.0 * p) * p) + Float64(x * x))))))) tmp = 0.0 if (t_0 <= 0.0) tmp = Float64(Float64(-abs(p)) / x); elseif (t_0 <= 0.7071067811865483) tmp = sqrt(0.5); else tmp = sqrt(1.0); end return tmp end
function tmp_2 = code(p, x) t_0 = sqrt((0.5 * (1.0 + (x / sqrt((((4.0 * p) * p) + (x * x))))))); tmp = 0.0; if (t_0 <= 0.0) tmp = -abs(p) / x; elseif (t_0 <= 0.7071067811865483) tmp = sqrt(0.5); else tmp = sqrt(1.0); end tmp_2 = tmp; end
code[p_, x_] := Block[{t$95$0 = N[Sqrt[N[(0.5 * N[(1.0 + N[(x / N[Sqrt[N[(N[(N[(4.0 * p), $MachinePrecision] * p), $MachinePrecision] + N[(x * x), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]}, If[LessEqual[t$95$0, 0.0], N[((-N[Abs[p], $MachinePrecision]) / x), $MachinePrecision], If[LessEqual[t$95$0, 0.7071067811865483], N[Sqrt[0.5], $MachinePrecision], N[Sqrt[1.0], $MachinePrecision]]]]
\begin{array}{l}
t_0 := \sqrt{0.5 \cdot \left(1 + \frac{x}{\sqrt{\left(4 \cdot p\right) \cdot p + x \cdot x}}\right)}\\
\mathbf{if}\;t\_0 \leq 0:\\
\;\;\;\;\frac{-\left|p\right|}{x}\\
\mathbf{elif}\;t\_0 \leq 0.7071067811865483:\\
\;\;\;\;\sqrt{0.5}\\
\mathbf{else}:\\
\;\;\;\;\sqrt{1}\\
\end{array}
if (sqrt.f64 (*.f64 #s(literal 1/2 binary64) (+.f64 #s(literal 1 binary64) (/.f64 x (sqrt.f64 (+.f64 (*.f64 (*.f64 #s(literal 4 binary64) p) p) (*.f64 x x))))))) < 0.0Initial program 79.5%
Taylor expanded in x around -inf
lower-*.f64N/A
lower-/.f64N/A
lower-sqrt.f64N/A
lower-pow.f6418.0%
Applied rewrites18.0%
lift-*.f64N/A
mul-1-negN/A
lift-/.f64N/A
distribute-neg-fracN/A
lower-/.f64N/A
lower-neg.f6418.0%
lift-sqrt.f64N/A
lift-pow.f64N/A
unpow2N/A
rem-sqrt-squareN/A
lower-fabs.f6426.7%
Applied rewrites26.7%
if 0.0 < (sqrt.f64 (*.f64 #s(literal 1/2 binary64) (+.f64 #s(literal 1 binary64) (/.f64 x (sqrt.f64 (+.f64 (*.f64 (*.f64 #s(literal 4 binary64) p) p) (*.f64 x x))))))) < 0.70710678118654835Initial program 79.5%
Taylor expanded in p around inf
Applied rewrites55.8%
if 0.70710678118654835 < (sqrt.f64 (*.f64 #s(literal 1/2 binary64) (+.f64 #s(literal 1 binary64) (/.f64 x (sqrt.f64 (+.f64 (*.f64 (*.f64 #s(literal 4 binary64) p) p) (*.f64 x x))))))) Initial program 79.5%
Taylor expanded in p around -inf
lower-+.f64N/A
lower-*.f64N/A
lower-/.f6451.2%
Applied rewrites51.2%
lift-+.f64N/A
+-commutativeN/A
lift-*.f64N/A
*-commutativeN/A
lower-fma.f6451.2%
Applied rewrites51.2%
Taylor expanded in x around inf
Applied rewrites35.7%
(FPCore (p x)
:precision binary64
(if (<=
(sqrt (* 0.5 (+ 1.0 (/ x (sqrt (+ (* (* 4.0 p) p) (* x x)))))))
0.9816941738241592)
(sqrt 0.5)
(sqrt 1.0)))double code(double p, double x) {
double tmp;
if (sqrt((0.5 * (1.0 + (x / sqrt((((4.0 * p) * p) + (x * x))))))) <= 0.9816941738241592) {
tmp = sqrt(0.5);
} else {
tmp = sqrt(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(p, x)
use fmin_fmax_functions
real(8), intent (in) :: p
real(8), intent (in) :: x
real(8) :: tmp
if (sqrt((0.5d0 * (1.0d0 + (x / sqrt((((4.0d0 * p) * p) + (x * x))))))) <= 0.9816941738241592d0) then
tmp = sqrt(0.5d0)
else
tmp = sqrt(1.0d0)
end if
code = tmp
end function
public static double code(double p, double x) {
double tmp;
if (Math.sqrt((0.5 * (1.0 + (x / Math.sqrt((((4.0 * p) * p) + (x * x))))))) <= 0.9816941738241592) {
tmp = Math.sqrt(0.5);
} else {
tmp = Math.sqrt(1.0);
}
return tmp;
}
def code(p, x): tmp = 0 if math.sqrt((0.5 * (1.0 + (x / math.sqrt((((4.0 * p) * p) + (x * x))))))) <= 0.9816941738241592: tmp = math.sqrt(0.5) else: tmp = math.sqrt(1.0) return tmp
function code(p, x) tmp = 0.0 if (sqrt(Float64(0.5 * Float64(1.0 + Float64(x / sqrt(Float64(Float64(Float64(4.0 * p) * p) + Float64(x * x))))))) <= 0.9816941738241592) tmp = sqrt(0.5); else tmp = sqrt(1.0); end return tmp end
function tmp_2 = code(p, x) tmp = 0.0; if (sqrt((0.5 * (1.0 + (x / sqrt((((4.0 * p) * p) + (x * x))))))) <= 0.9816941738241592) tmp = sqrt(0.5); else tmp = sqrt(1.0); end tmp_2 = tmp; end
code[p_, x_] := If[LessEqual[N[Sqrt[N[(0.5 * N[(1.0 + N[(x / N[Sqrt[N[(N[(N[(4.0 * p), $MachinePrecision] * p), $MachinePrecision] + N[(x * x), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision], 0.9816941738241592], N[Sqrt[0.5], $MachinePrecision], N[Sqrt[1.0], $MachinePrecision]]
\begin{array}{l}
\mathbf{if}\;\sqrt{0.5 \cdot \left(1 + \frac{x}{\sqrt{\left(4 \cdot p\right) \cdot p + x \cdot x}}\right)} \leq 0.9816941738241592:\\
\;\;\;\;\sqrt{0.5}\\
\mathbf{else}:\\
\;\;\;\;\sqrt{1}\\
\end{array}
if (sqrt.f64 (*.f64 #s(literal 1/2 binary64) (+.f64 #s(literal 1 binary64) (/.f64 x (sqrt.f64 (+.f64 (*.f64 (*.f64 #s(literal 4 binary64) p) p) (*.f64 x x))))))) < 0.98169417382415924Initial program 79.5%
Taylor expanded in p around inf
Applied rewrites55.8%
if 0.98169417382415924 < (sqrt.f64 (*.f64 #s(literal 1/2 binary64) (+.f64 #s(literal 1 binary64) (/.f64 x (sqrt.f64 (+.f64 (*.f64 (*.f64 #s(literal 4 binary64) p) p) (*.f64 x x))))))) Initial program 79.5%
Taylor expanded in p around -inf
lower-+.f64N/A
lower-*.f64N/A
lower-/.f6451.2%
Applied rewrites51.2%
lift-+.f64N/A
+-commutativeN/A
lift-*.f64N/A
*-commutativeN/A
lower-fma.f6451.2%
Applied rewrites51.2%
Taylor expanded in x around inf
Applied rewrites35.7%
(FPCore (p x) :precision binary64 (sqrt 0.5))
double code(double p, double x) {
return sqrt(0.5);
}
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(p, x)
use fmin_fmax_functions
real(8), intent (in) :: p
real(8), intent (in) :: x
code = sqrt(0.5d0)
end function
public static double code(double p, double x) {
return Math.sqrt(0.5);
}
def code(p, x): return math.sqrt(0.5)
function code(p, x) return sqrt(0.5) end
function tmp = code(p, x) tmp = sqrt(0.5); end
code[p_, x_] := N[Sqrt[0.5], $MachinePrecision]
\sqrt{0.5}
Initial program 79.5%
Taylor expanded in p around inf
Applied rewrites55.8%
(FPCore (p x) :precision binary64 (sqrt (+ 0.5 (/ (copysign 0.5 x) (hypot 1.0 (/ (* 2.0 p) x))))))
double code(double p, double x) {
return sqrt((0.5 + (copysign(0.5, x) / hypot(1.0, ((2.0 * p) / x)))));
}
public static double code(double p, double x) {
return Math.sqrt((0.5 + (Math.copySign(0.5, x) / Math.hypot(1.0, ((2.0 * p) / x)))));
}
def code(p, x): return math.sqrt((0.5 + (math.copysign(0.5, x) / math.hypot(1.0, ((2.0 * p) / x)))))
function code(p, x) return sqrt(Float64(0.5 + Float64(copysign(0.5, x) / hypot(1.0, Float64(Float64(2.0 * p) / x))))) end
function tmp = code(p, x) tmp = sqrt((0.5 + ((sign(x) * abs(0.5)) / hypot(1.0, ((2.0 * p) / x))))); end
code[p_, x_] := N[Sqrt[N[(0.5 + N[(N[With[{TMP1 = Abs[0.5], TMP2 = Sign[x]}, TMP1 * If[TMP2 == 0, 1, TMP2]], $MachinePrecision] / N[Sqrt[1.0 ^ 2 + N[(N[(2.0 * p), $MachinePrecision] / x), $MachinePrecision] ^ 2], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]
\sqrt{0.5 + \frac{\mathsf{copysign}\left(0.5, x\right)}{\mathsf{hypot}\left(1, \frac{2 \cdot p}{x}\right)}}
herbie shell --seed 2025238
(FPCore (p x)
:name "Given's Rotation SVD example"
:precision binary64
:pre (and (< 1e-150 (fabs x)) (< (fabs x) 1e+150))
:alt
(! :herbie-platform c (sqrt (+ 1/2 (/ (copysign 1/2 x) (hypot 1 (/ (* 2 p) x))))))
(sqrt (* 0.5 (+ 1.0 (/ x (sqrt (+ (* (* 4.0 p) p) (* x x))))))))