
(FPCore (p r q) :precision binary64 (* (/ 1.0 2.0) (+ (+ (fabs p) (fabs r)) (sqrt (+ (pow (- p r) 2.0) (* 4.0 (pow q 2.0)))))))
double code(double p, double r, double q) {
return (1.0 / 2.0) * ((fabs(p) + fabs(r)) + sqrt((pow((p - r), 2.0) + (4.0 * pow(q, 2.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(p, r, q)
use fmin_fmax_functions
real(8), intent (in) :: p
real(8), intent (in) :: r
real(8), intent (in) :: q
code = (1.0d0 / 2.0d0) * ((abs(p) + abs(r)) + sqrt((((p - r) ** 2.0d0) + (4.0d0 * (q ** 2.0d0)))))
end function
public static double code(double p, double r, double q) {
return (1.0 / 2.0) * ((Math.abs(p) + Math.abs(r)) + Math.sqrt((Math.pow((p - r), 2.0) + (4.0 * Math.pow(q, 2.0)))));
}
def code(p, r, q): return (1.0 / 2.0) * ((math.fabs(p) + math.fabs(r)) + math.sqrt((math.pow((p - r), 2.0) + (4.0 * math.pow(q, 2.0)))))
function code(p, r, q) return Float64(Float64(1.0 / 2.0) * Float64(Float64(abs(p) + abs(r)) + sqrt(Float64((Float64(p - r) ^ 2.0) + Float64(4.0 * (q ^ 2.0)))))) end
function tmp = code(p, r, q) tmp = (1.0 / 2.0) * ((abs(p) + abs(r)) + sqrt((((p - r) ^ 2.0) + (4.0 * (q ^ 2.0))))); end
code[p_, r_, q_] := N[(N[(1.0 / 2.0), $MachinePrecision] * N[(N[(N[Abs[p], $MachinePrecision] + N[Abs[r], $MachinePrecision]), $MachinePrecision] + N[Sqrt[N[(N[Power[N[(p - r), $MachinePrecision], 2.0], $MachinePrecision] + N[(4.0 * N[Power[q, 2.0], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{1}{2} \cdot \left(\left(\left|p\right| + \left|r\right|\right) + \sqrt{{\left(p - r\right)}^{2} + 4 \cdot {q}^{2}}\right)
\end{array}
Herbie found 8 alternatives:
| Alternative | Accuracy | Speedup |
|---|
(FPCore (p r q) :precision binary64 (* (/ 1.0 2.0) (+ (+ (fabs p) (fabs r)) (sqrt (+ (pow (- p r) 2.0) (* 4.0 (pow q 2.0)))))))
double code(double p, double r, double q) {
return (1.0 / 2.0) * ((fabs(p) + fabs(r)) + sqrt((pow((p - r), 2.0) + (4.0 * pow(q, 2.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(p, r, q)
use fmin_fmax_functions
real(8), intent (in) :: p
real(8), intent (in) :: r
real(8), intent (in) :: q
code = (1.0d0 / 2.0d0) * ((abs(p) + abs(r)) + sqrt((((p - r) ** 2.0d0) + (4.0d0 * (q ** 2.0d0)))))
end function
public static double code(double p, double r, double q) {
return (1.0 / 2.0) * ((Math.abs(p) + Math.abs(r)) + Math.sqrt((Math.pow((p - r), 2.0) + (4.0 * Math.pow(q, 2.0)))));
}
def code(p, r, q): return (1.0 / 2.0) * ((math.fabs(p) + math.fabs(r)) + math.sqrt((math.pow((p - r), 2.0) + (4.0 * math.pow(q, 2.0)))))
function code(p, r, q) return Float64(Float64(1.0 / 2.0) * Float64(Float64(abs(p) + abs(r)) + sqrt(Float64((Float64(p - r) ^ 2.0) + Float64(4.0 * (q ^ 2.0)))))) end
function tmp = code(p, r, q) tmp = (1.0 / 2.0) * ((abs(p) + abs(r)) + sqrt((((p - r) ^ 2.0) + (4.0 * (q ^ 2.0))))); end
code[p_, r_, q_] := N[(N[(1.0 / 2.0), $MachinePrecision] * N[(N[(N[Abs[p], $MachinePrecision] + N[Abs[r], $MachinePrecision]), $MachinePrecision] + N[Sqrt[N[(N[Power[N[(p - r), $MachinePrecision], 2.0], $MachinePrecision] + N[(4.0 * N[Power[q, 2.0], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{1}{2} \cdot \left(\left(\left|p\right| + \left|r\right|\right) + \sqrt{{\left(p - r\right)}^{2} + 4 \cdot {q}^{2}}\right)
\end{array}
q_m = (fabs.f64 q)
NOTE: p, r, and q_m should be sorted in increasing order before calling this function.
(FPCore (p r q_m)
:precision binary64
(let* ((t_0 (+ (fabs p) (fabs r))))
(if (<= r 4.2e-266)
(* 0.5 (+ t_0 (- p)))
(if (<= r 1.35e-89)
(* (/ 1.0 2.0) (+ t_0 (* q_m 2.0)))
(if (<= r 1.22e+104)
(*
(/ 1.0 2.0)
(+ t_0 (sqrt (+ (pow (- p r) 2.0) (* 4.0 (pow q_m 2.0))))))
(* (fma (/ (+ (+ (- p) (fabs r)) (fabs p)) r) 0.5 0.5) r))))))q_m = fabs(q);
assert(p < r && r < q_m);
double code(double p, double r, double q_m) {
double t_0 = fabs(p) + fabs(r);
double tmp;
if (r <= 4.2e-266) {
tmp = 0.5 * (t_0 + -p);
} else if (r <= 1.35e-89) {
tmp = (1.0 / 2.0) * (t_0 + (q_m * 2.0));
} else if (r <= 1.22e+104) {
tmp = (1.0 / 2.0) * (t_0 + sqrt((pow((p - r), 2.0) + (4.0 * pow(q_m, 2.0)))));
} else {
tmp = fma((((-p + fabs(r)) + fabs(p)) / r), 0.5, 0.5) * r;
}
return tmp;
}
q_m = abs(q) p, r, q_m = sort([p, r, q_m]) function code(p, r, q_m) t_0 = Float64(abs(p) + abs(r)) tmp = 0.0 if (r <= 4.2e-266) tmp = Float64(0.5 * Float64(t_0 + Float64(-p))); elseif (r <= 1.35e-89) tmp = Float64(Float64(1.0 / 2.0) * Float64(t_0 + Float64(q_m * 2.0))); elseif (r <= 1.22e+104) tmp = Float64(Float64(1.0 / 2.0) * Float64(t_0 + sqrt(Float64((Float64(p - r) ^ 2.0) + Float64(4.0 * (q_m ^ 2.0)))))); else tmp = Float64(fma(Float64(Float64(Float64(Float64(-p) + abs(r)) + abs(p)) / r), 0.5, 0.5) * r); end return tmp end
q_m = N[Abs[q], $MachinePrecision]
NOTE: p, r, and q_m should be sorted in increasing order before calling this function.
code[p_, r_, q$95$m_] := Block[{t$95$0 = N[(N[Abs[p], $MachinePrecision] + N[Abs[r], $MachinePrecision]), $MachinePrecision]}, If[LessEqual[r, 4.2e-266], N[(0.5 * N[(t$95$0 + (-p)), $MachinePrecision]), $MachinePrecision], If[LessEqual[r, 1.35e-89], N[(N[(1.0 / 2.0), $MachinePrecision] * N[(t$95$0 + N[(q$95$m * 2.0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], If[LessEqual[r, 1.22e+104], N[(N[(1.0 / 2.0), $MachinePrecision] * N[(t$95$0 + N[Sqrt[N[(N[Power[N[(p - r), $MachinePrecision], 2.0], $MachinePrecision] + N[(4.0 * N[Power[q$95$m, 2.0], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]), $MachinePrecision], N[(N[(N[(N[(N[((-p) + N[Abs[r], $MachinePrecision]), $MachinePrecision] + N[Abs[p], $MachinePrecision]), $MachinePrecision] / r), $MachinePrecision] * 0.5 + 0.5), $MachinePrecision] * r), $MachinePrecision]]]]]
\begin{array}{l}
q_m = \left|q\right|
\\
[p, r, q_m] = \mathsf{sort}([p, r, q_m])\\
\\
\begin{array}{l}
t_0 := \left|p\right| + \left|r\right|\\
\mathbf{if}\;r \leq 4.2 \cdot 10^{-266}:\\
\;\;\;\;0.5 \cdot \left(t\_0 + \left(-p\right)\right)\\
\mathbf{elif}\;r \leq 1.35 \cdot 10^{-89}:\\
\;\;\;\;\frac{1}{2} \cdot \left(t\_0 + q\_m \cdot 2\right)\\
\mathbf{elif}\;r \leq 1.22 \cdot 10^{+104}:\\
\;\;\;\;\frac{1}{2} \cdot \left(t\_0 + \sqrt{{\left(p - r\right)}^{2} + 4 \cdot {q\_m}^{2}}\right)\\
\mathbf{else}:\\
\;\;\;\;\mathsf{fma}\left(\frac{\left(\left(-p\right) + \left|r\right|\right) + \left|p\right|}{r}, 0.5, 0.5\right) \cdot r\\
\end{array}
\end{array}
if r < 4.19999999999999994e-266Initial program 48.3%
Taylor expanded in p around -inf
mul-1-negN/A
lower-neg.f6465.5
Applied rewrites65.5%
lift-/.f64N/A
metadata-eval65.5
Applied rewrites65.5%
if 4.19999999999999994e-266 < r < 1.34999999999999994e-89Initial program 57.1%
Taylor expanded in q around inf
*-commutativeN/A
lower-*.f6461.3
Applied rewrites61.3%
if 1.34999999999999994e-89 < r < 1.22e104Initial program 64.7%
if 1.22e104 < r Initial program 21.8%
Taylor expanded in p around -inf
mul-1-negN/A
lower-neg.f6423.2
Applied rewrites23.2%
lift-/.f64N/A
metadata-eval23.2
Applied rewrites23.2%
Taylor expanded in r around inf
metadata-evalN/A
*-commutativeN/A
lower-*.f64N/A
Applied rewrites86.5%
lift-fabs.f64N/A
lift-fma.f64N/A
mul-1-negN/A
lower-+.f64N/A
lower-neg.f64N/A
lift-fabs.f6486.5
Applied rewrites86.5%
q_m = (fabs.f64 q)
NOTE: p, r, and q_m should be sorted in increasing order before calling this function.
(FPCore (p r q_m)
:precision binary64
(let* ((t_0 (+ (fabs p) (fabs r))))
(if (<= r 4.2e-266)
(* 0.5 (+ t_0 (- p)))
(if (<= r 1.4e-89)
(* (/ 1.0 2.0) (+ t_0 (* q_m 2.0)))
(if (<= r 9.5e-16)
(* 0.5 (+ t_0 (sqrt (fma (* q_m q_m) 4.0 (* p p)))))
(* (fma (/ (+ (+ (- p) (fabs r)) (fabs p)) r) 0.5 0.5) r))))))q_m = fabs(q);
assert(p < r && r < q_m);
double code(double p, double r, double q_m) {
double t_0 = fabs(p) + fabs(r);
double tmp;
if (r <= 4.2e-266) {
tmp = 0.5 * (t_0 + -p);
} else if (r <= 1.4e-89) {
tmp = (1.0 / 2.0) * (t_0 + (q_m * 2.0));
} else if (r <= 9.5e-16) {
tmp = 0.5 * (t_0 + sqrt(fma((q_m * q_m), 4.0, (p * p))));
} else {
tmp = fma((((-p + fabs(r)) + fabs(p)) / r), 0.5, 0.5) * r;
}
return tmp;
}
q_m = abs(q) p, r, q_m = sort([p, r, q_m]) function code(p, r, q_m) t_0 = Float64(abs(p) + abs(r)) tmp = 0.0 if (r <= 4.2e-266) tmp = Float64(0.5 * Float64(t_0 + Float64(-p))); elseif (r <= 1.4e-89) tmp = Float64(Float64(1.0 / 2.0) * Float64(t_0 + Float64(q_m * 2.0))); elseif (r <= 9.5e-16) tmp = Float64(0.5 * Float64(t_0 + sqrt(fma(Float64(q_m * q_m), 4.0, Float64(p * p))))); else tmp = Float64(fma(Float64(Float64(Float64(Float64(-p) + abs(r)) + abs(p)) / r), 0.5, 0.5) * r); end return tmp end
q_m = N[Abs[q], $MachinePrecision]
NOTE: p, r, and q_m should be sorted in increasing order before calling this function.
code[p_, r_, q$95$m_] := Block[{t$95$0 = N[(N[Abs[p], $MachinePrecision] + N[Abs[r], $MachinePrecision]), $MachinePrecision]}, If[LessEqual[r, 4.2e-266], N[(0.5 * N[(t$95$0 + (-p)), $MachinePrecision]), $MachinePrecision], If[LessEqual[r, 1.4e-89], N[(N[(1.0 / 2.0), $MachinePrecision] * N[(t$95$0 + N[(q$95$m * 2.0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], If[LessEqual[r, 9.5e-16], N[(0.5 * N[(t$95$0 + N[Sqrt[N[(N[(q$95$m * q$95$m), $MachinePrecision] * 4.0 + N[(p * p), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]), $MachinePrecision], N[(N[(N[(N[(N[((-p) + N[Abs[r], $MachinePrecision]), $MachinePrecision] + N[Abs[p], $MachinePrecision]), $MachinePrecision] / r), $MachinePrecision] * 0.5 + 0.5), $MachinePrecision] * r), $MachinePrecision]]]]]
\begin{array}{l}
q_m = \left|q\right|
\\
[p, r, q_m] = \mathsf{sort}([p, r, q_m])\\
\\
\begin{array}{l}
t_0 := \left|p\right| + \left|r\right|\\
\mathbf{if}\;r \leq 4.2 \cdot 10^{-266}:\\
\;\;\;\;0.5 \cdot \left(t\_0 + \left(-p\right)\right)\\
\mathbf{elif}\;r \leq 1.4 \cdot 10^{-89}:\\
\;\;\;\;\frac{1}{2} \cdot \left(t\_0 + q\_m \cdot 2\right)\\
\mathbf{elif}\;r \leq 9.5 \cdot 10^{-16}:\\
\;\;\;\;0.5 \cdot \left(t\_0 + \sqrt{\mathsf{fma}\left(q\_m \cdot q\_m, 4, p \cdot p\right)}\right)\\
\mathbf{else}:\\
\;\;\;\;\mathsf{fma}\left(\frac{\left(\left(-p\right) + \left|r\right|\right) + \left|p\right|}{r}, 0.5, 0.5\right) \cdot r\\
\end{array}
\end{array}
if r < 4.19999999999999994e-266Initial program 48.3%
Taylor expanded in p around -inf
mul-1-negN/A
lower-neg.f6465.5
Applied rewrites65.5%
lift-/.f64N/A
metadata-eval65.5
Applied rewrites65.5%
if 4.19999999999999994e-266 < r < 1.3999999999999999e-89Initial program 57.1%
Taylor expanded in q around inf
*-commutativeN/A
lower-*.f6461.3
Applied rewrites61.3%
if 1.3999999999999999e-89 < r < 9.5000000000000005e-16Initial program 63.9%
Taylor expanded in p around -inf
mul-1-negN/A
lower-neg.f6438.3
Applied rewrites38.3%
lift-/.f64N/A
metadata-eval38.3
Applied rewrites38.3%
Taylor expanded in r around 0
lower-sqrt.f64N/A
*-commutativeN/A
lower-fma.f64N/A
unpow2N/A
lower-*.f64N/A
unpow2N/A
lower-*.f6445.2
Applied rewrites45.2%
if 9.5000000000000005e-16 < r Initial program 36.0%
Taylor expanded in p around -inf
mul-1-negN/A
lower-neg.f6425.4
Applied rewrites25.4%
lift-/.f64N/A
metadata-eval25.4
Applied rewrites25.4%
Taylor expanded in r around inf
metadata-evalN/A
*-commutativeN/A
lower-*.f64N/A
Applied rewrites79.1%
lift-fabs.f64N/A
lift-fma.f64N/A
mul-1-negN/A
lower-+.f64N/A
lower-neg.f64N/A
lift-fabs.f6479.1
Applied rewrites79.1%
q_m = (fabs.f64 q)
NOTE: p, r, and q_m should be sorted in increasing order before calling this function.
(FPCore (p r q_m)
:precision binary64
(let* ((t_0 (+ (fabs p) (fabs r))))
(if (<= r 4.2e-266)
(* 0.5 (+ t_0 (- p)))
(if (<= r 4.3e-36)
(* (/ 1.0 2.0) (+ t_0 (* q_m 2.0)))
(* (fma (/ (+ (+ (- p) (fabs r)) (fabs p)) r) 0.5 0.5) r)))))q_m = fabs(q);
assert(p < r && r < q_m);
double code(double p, double r, double q_m) {
double t_0 = fabs(p) + fabs(r);
double tmp;
if (r <= 4.2e-266) {
tmp = 0.5 * (t_0 + -p);
} else if (r <= 4.3e-36) {
tmp = (1.0 / 2.0) * (t_0 + (q_m * 2.0));
} else {
tmp = fma((((-p + fabs(r)) + fabs(p)) / r), 0.5, 0.5) * r;
}
return tmp;
}
q_m = abs(q) p, r, q_m = sort([p, r, q_m]) function code(p, r, q_m) t_0 = Float64(abs(p) + abs(r)) tmp = 0.0 if (r <= 4.2e-266) tmp = Float64(0.5 * Float64(t_0 + Float64(-p))); elseif (r <= 4.3e-36) tmp = Float64(Float64(1.0 / 2.0) * Float64(t_0 + Float64(q_m * 2.0))); else tmp = Float64(fma(Float64(Float64(Float64(Float64(-p) + abs(r)) + abs(p)) / r), 0.5, 0.5) * r); end return tmp end
q_m = N[Abs[q], $MachinePrecision]
NOTE: p, r, and q_m should be sorted in increasing order before calling this function.
code[p_, r_, q$95$m_] := Block[{t$95$0 = N[(N[Abs[p], $MachinePrecision] + N[Abs[r], $MachinePrecision]), $MachinePrecision]}, If[LessEqual[r, 4.2e-266], N[(0.5 * N[(t$95$0 + (-p)), $MachinePrecision]), $MachinePrecision], If[LessEqual[r, 4.3e-36], N[(N[(1.0 / 2.0), $MachinePrecision] * N[(t$95$0 + N[(q$95$m * 2.0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], N[(N[(N[(N[(N[((-p) + N[Abs[r], $MachinePrecision]), $MachinePrecision] + N[Abs[p], $MachinePrecision]), $MachinePrecision] / r), $MachinePrecision] * 0.5 + 0.5), $MachinePrecision] * r), $MachinePrecision]]]]
\begin{array}{l}
q_m = \left|q\right|
\\
[p, r, q_m] = \mathsf{sort}([p, r, q_m])\\
\\
\begin{array}{l}
t_0 := \left|p\right| + \left|r\right|\\
\mathbf{if}\;r \leq 4.2 \cdot 10^{-266}:\\
\;\;\;\;0.5 \cdot \left(t\_0 + \left(-p\right)\right)\\
\mathbf{elif}\;r \leq 4.3 \cdot 10^{-36}:\\
\;\;\;\;\frac{1}{2} \cdot \left(t\_0 + q\_m \cdot 2\right)\\
\mathbf{else}:\\
\;\;\;\;\mathsf{fma}\left(\frac{\left(\left(-p\right) + \left|r\right|\right) + \left|p\right|}{r}, 0.5, 0.5\right) \cdot r\\
\end{array}
\end{array}
if r < 4.19999999999999994e-266Initial program 48.3%
Taylor expanded in p around -inf
mul-1-negN/A
lower-neg.f6465.5
Applied rewrites65.5%
lift-/.f64N/A
metadata-eval65.5
Applied rewrites65.5%
if 4.19999999999999994e-266 < r < 4.3000000000000002e-36Initial program 59.1%
Taylor expanded in q around inf
*-commutativeN/A
lower-*.f6460.0
Applied rewrites60.0%
if 4.3000000000000002e-36 < r Initial program 37.3%
Taylor expanded in p around -inf
mul-1-negN/A
lower-neg.f6426.2
Applied rewrites26.2%
lift-/.f64N/A
metadata-eval26.2
Applied rewrites26.2%
Taylor expanded in r around inf
metadata-evalN/A
*-commutativeN/A
lower-*.f64N/A
Applied rewrites78.1%
lift-fabs.f64N/A
lift-fma.f64N/A
mul-1-negN/A
lower-+.f64N/A
lower-neg.f64N/A
lift-fabs.f6478.1
Applied rewrites78.1%
q_m = (fabs.f64 q)
NOTE: p, r, and q_m should be sorted in increasing order before calling this function.
(FPCore (p r q_m)
:precision binary64
(let* ((t_0 (+ (fabs p) (fabs r))))
(if (<= p -1.35e+30)
(* 0.5 (+ t_0 (- p)))
(if (<= p 3.3e-108) (* (/ 1.0 2.0) (+ t_0 (* q_m 2.0))) r))))q_m = fabs(q);
assert(p < r && r < q_m);
double code(double p, double r, double q_m) {
double t_0 = fabs(p) + fabs(r);
double tmp;
if (p <= -1.35e+30) {
tmp = 0.5 * (t_0 + -p);
} else if (p <= 3.3e-108) {
tmp = (1.0 / 2.0) * (t_0 + (q_m * 2.0));
} else {
tmp = r;
}
return tmp;
}
q_m = private
NOTE: p, r, and q_m should be sorted in increasing order before calling this function.
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, r, q_m)
use fmin_fmax_functions
real(8), intent (in) :: p
real(8), intent (in) :: r
real(8), intent (in) :: q_m
real(8) :: t_0
real(8) :: tmp
t_0 = abs(p) + abs(r)
if (p <= (-1.35d+30)) then
tmp = 0.5d0 * (t_0 + -p)
else if (p <= 3.3d-108) then
tmp = (1.0d0 / 2.0d0) * (t_0 + (q_m * 2.0d0))
else
tmp = r
end if
code = tmp
end function
q_m = Math.abs(q);
assert p < r && r < q_m;
public static double code(double p, double r, double q_m) {
double t_0 = Math.abs(p) + Math.abs(r);
double tmp;
if (p <= -1.35e+30) {
tmp = 0.5 * (t_0 + -p);
} else if (p <= 3.3e-108) {
tmp = (1.0 / 2.0) * (t_0 + (q_m * 2.0));
} else {
tmp = r;
}
return tmp;
}
q_m = math.fabs(q) [p, r, q_m] = sort([p, r, q_m]) def code(p, r, q_m): t_0 = math.fabs(p) + math.fabs(r) tmp = 0 if p <= -1.35e+30: tmp = 0.5 * (t_0 + -p) elif p <= 3.3e-108: tmp = (1.0 / 2.0) * (t_0 + (q_m * 2.0)) else: tmp = r return tmp
q_m = abs(q) p, r, q_m = sort([p, r, q_m]) function code(p, r, q_m) t_0 = Float64(abs(p) + abs(r)) tmp = 0.0 if (p <= -1.35e+30) tmp = Float64(0.5 * Float64(t_0 + Float64(-p))); elseif (p <= 3.3e-108) tmp = Float64(Float64(1.0 / 2.0) * Float64(t_0 + Float64(q_m * 2.0))); else tmp = r; end return tmp end
q_m = abs(q);
p, r, q_m = num2cell(sort([p, r, q_m])){:}
function tmp_2 = code(p, r, q_m)
t_0 = abs(p) + abs(r);
tmp = 0.0;
if (p <= -1.35e+30)
tmp = 0.5 * (t_0 + -p);
elseif (p <= 3.3e-108)
tmp = (1.0 / 2.0) * (t_0 + (q_m * 2.0));
else
tmp = r;
end
tmp_2 = tmp;
end
q_m = N[Abs[q], $MachinePrecision]
NOTE: p, r, and q_m should be sorted in increasing order before calling this function.
code[p_, r_, q$95$m_] := Block[{t$95$0 = N[(N[Abs[p], $MachinePrecision] + N[Abs[r], $MachinePrecision]), $MachinePrecision]}, If[LessEqual[p, -1.35e+30], N[(0.5 * N[(t$95$0 + (-p)), $MachinePrecision]), $MachinePrecision], If[LessEqual[p, 3.3e-108], N[(N[(1.0 / 2.0), $MachinePrecision] * N[(t$95$0 + N[(q$95$m * 2.0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], r]]]
\begin{array}{l}
q_m = \left|q\right|
\\
[p, r, q_m] = \mathsf{sort}([p, r, q_m])\\
\\
\begin{array}{l}
t_0 := \left|p\right| + \left|r\right|\\
\mathbf{if}\;p \leq -1.35 \cdot 10^{+30}:\\
\;\;\;\;0.5 \cdot \left(t\_0 + \left(-p\right)\right)\\
\mathbf{elif}\;p \leq 3.3 \cdot 10^{-108}:\\
\;\;\;\;\frac{1}{2} \cdot \left(t\_0 + q\_m \cdot 2\right)\\
\mathbf{else}:\\
\;\;\;\;r\\
\end{array}
\end{array}
if p < -1.3499999999999999e30Initial program 33.8%
Taylor expanded in p around -inf
mul-1-negN/A
lower-neg.f6471.3
Applied rewrites71.3%
lift-/.f64N/A
metadata-eval71.3
Applied rewrites71.3%
if -1.3499999999999999e30 < p < 3.3000000000000002e-108Initial program 58.6%
Taylor expanded in q around inf
*-commutativeN/A
lower-*.f6456.7
Applied rewrites56.7%
if 3.3000000000000002e-108 < p Initial program 34.4%
Taylor expanded in p around inf
Applied rewrites16.0%
lift-*.f64N/A
lift-/.f64N/A
*-commutativeN/A
lower-*.f64N/A
Applied rewrites16.0%
Taylor expanded in p around -inf
Applied rewrites78.7%
q_m = (fabs.f64 q) NOTE: p, r, and q_m should be sorted in increasing order before calling this function. (FPCore (p r q_m) :precision binary64 (if (<= p -8.5e+24) (* 0.5 (+ (+ (fabs p) (fabs r)) (- p))) (if (<= p 3.3e-108) (fma (+ r p) 0.5 q_m) r)))
q_m = fabs(q);
assert(p < r && r < q_m);
double code(double p, double r, double q_m) {
double tmp;
if (p <= -8.5e+24) {
tmp = 0.5 * ((fabs(p) + fabs(r)) + -p);
} else if (p <= 3.3e-108) {
tmp = fma((r + p), 0.5, q_m);
} else {
tmp = r;
}
return tmp;
}
q_m = abs(q) p, r, q_m = sort([p, r, q_m]) function code(p, r, q_m) tmp = 0.0 if (p <= -8.5e+24) tmp = Float64(0.5 * Float64(Float64(abs(p) + abs(r)) + Float64(-p))); elseif (p <= 3.3e-108) tmp = fma(Float64(r + p), 0.5, q_m); else tmp = r; end return tmp end
q_m = N[Abs[q], $MachinePrecision] NOTE: p, r, and q_m should be sorted in increasing order before calling this function. code[p_, r_, q$95$m_] := If[LessEqual[p, -8.5e+24], N[(0.5 * N[(N[(N[Abs[p], $MachinePrecision] + N[Abs[r], $MachinePrecision]), $MachinePrecision] + (-p)), $MachinePrecision]), $MachinePrecision], If[LessEqual[p, 3.3e-108], N[(N[(r + p), $MachinePrecision] * 0.5 + q$95$m), $MachinePrecision], r]]
\begin{array}{l}
q_m = \left|q\right|
\\
[p, r, q_m] = \mathsf{sort}([p, r, q_m])\\
\\
\begin{array}{l}
\mathbf{if}\;p \leq -8.5 \cdot 10^{+24}:\\
\;\;\;\;0.5 \cdot \left(\left(\left|p\right| + \left|r\right|\right) + \left(-p\right)\right)\\
\mathbf{elif}\;p \leq 3.3 \cdot 10^{-108}:\\
\;\;\;\;\mathsf{fma}\left(r + p, 0.5, q\_m\right)\\
\mathbf{else}:\\
\;\;\;\;r\\
\end{array}
\end{array}
if p < -8.49999999999999959e24Initial program 34.3%
Taylor expanded in p around -inf
mul-1-negN/A
lower-neg.f6470.9
Applied rewrites70.9%
lift-/.f64N/A
metadata-eval70.9
Applied rewrites70.9%
if -8.49999999999999959e24 < p < 3.3000000000000002e-108Initial program 58.4%
Taylor expanded in q around inf
metadata-evalN/A
*-commutativeN/A
lower-*.f64N/A
Applied rewrites53.3%
Taylor expanded in q around 0
+-commutativeN/A
+-commutativeN/A
*-commutativeN/A
lower-fma.f64N/A
lift-+.f6454.8
Applied rewrites54.8%
if 3.3000000000000002e-108 < p Initial program 34.4%
Taylor expanded in p around inf
Applied rewrites16.0%
lift-*.f64N/A
lift-/.f64N/A
*-commutativeN/A
lower-*.f64N/A
Applied rewrites16.0%
Taylor expanded in p around -inf
Applied rewrites78.7%
q_m = (fabs.f64 q) NOTE: p, r, and q_m should be sorted in increasing order before calling this function. (FPCore (p r q_m) :precision binary64 (if (<= r 4.5e-10) q_m r))
q_m = fabs(q);
assert(p < r && r < q_m);
double code(double p, double r, double q_m) {
double tmp;
if (r <= 4.5e-10) {
tmp = q_m;
} else {
tmp = r;
}
return tmp;
}
q_m = private
NOTE: p, r, and q_m should be sorted in increasing order before calling this function.
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, r, q_m)
use fmin_fmax_functions
real(8), intent (in) :: p
real(8), intent (in) :: r
real(8), intent (in) :: q_m
real(8) :: tmp
if (r <= 4.5d-10) then
tmp = q_m
else
tmp = r
end if
code = tmp
end function
q_m = Math.abs(q);
assert p < r && r < q_m;
public static double code(double p, double r, double q_m) {
double tmp;
if (r <= 4.5e-10) {
tmp = q_m;
} else {
tmp = r;
}
return tmp;
}
q_m = math.fabs(q) [p, r, q_m] = sort([p, r, q_m]) def code(p, r, q_m): tmp = 0 if r <= 4.5e-10: tmp = q_m else: tmp = r return tmp
q_m = abs(q) p, r, q_m = sort([p, r, q_m]) function code(p, r, q_m) tmp = 0.0 if (r <= 4.5e-10) tmp = q_m; else tmp = r; end return tmp end
q_m = abs(q);
p, r, q_m = num2cell(sort([p, r, q_m])){:}
function tmp_2 = code(p, r, q_m)
tmp = 0.0;
if (r <= 4.5e-10)
tmp = q_m;
else
tmp = r;
end
tmp_2 = tmp;
end
q_m = N[Abs[q], $MachinePrecision] NOTE: p, r, and q_m should be sorted in increasing order before calling this function. code[p_, r_, q$95$m_] := If[LessEqual[r, 4.5e-10], q$95$m, r]
\begin{array}{l}
q_m = \left|q\right|
\\
[p, r, q_m] = \mathsf{sort}([p, r, q_m])\\
\\
\begin{array}{l}
\mathbf{if}\;r \leq 4.5 \cdot 10^{-10}:\\
\;\;\;\;q\_m\\
\mathbf{else}:\\
\;\;\;\;r\\
\end{array}
\end{array}
if r < 4.5e-10Initial program 53.6%
Taylor expanded in q around inf
Applied rewrites44.1%
if 4.5e-10 < r Initial program 35.7%
Taylor expanded in p around inf
Applied rewrites14.2%
lift-*.f64N/A
lift-/.f64N/A
*-commutativeN/A
lower-*.f64N/A
Applied rewrites13.8%
Taylor expanded in p around -inf
Applied rewrites67.9%
q_m = (fabs.f64 q) NOTE: p, r, and q_m should be sorted in increasing order before calling this function. (FPCore (p r q_m) :precision binary64 r)
q_m = fabs(q);
assert(p < r && r < q_m);
double code(double p, double r, double q_m) {
return r;
}
q_m = private
NOTE: p, r, and q_m should be sorted in increasing order before calling this function.
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, r, q_m)
use fmin_fmax_functions
real(8), intent (in) :: p
real(8), intent (in) :: r
real(8), intent (in) :: q_m
code = r
end function
q_m = Math.abs(q);
assert p < r && r < q_m;
public static double code(double p, double r, double q_m) {
return r;
}
q_m = math.fabs(q) [p, r, q_m] = sort([p, r, q_m]) def code(p, r, q_m): return r
q_m = abs(q) p, r, q_m = sort([p, r, q_m]) function code(p, r, q_m) return r end
q_m = abs(q);
p, r, q_m = num2cell(sort([p, r, q_m])){:}
function tmp = code(p, r, q_m)
tmp = r;
end
q_m = N[Abs[q], $MachinePrecision] NOTE: p, r, and q_m should be sorted in increasing order before calling this function. code[p_, r_, q$95$m_] := r
\begin{array}{l}
q_m = \left|q\right|
\\
[p, r, q_m] = \mathsf{sort}([p, r, q_m])\\
\\
r
\end{array}
Initial program 45.7%
Taylor expanded in p around inf
Applied rewrites8.4%
lift-*.f64N/A
lift-/.f64N/A
*-commutativeN/A
lower-*.f64N/A
Applied rewrites7.5%
Taylor expanded in p around -inf
Applied rewrites34.6%
q_m = (fabs.f64 q) NOTE: p, r, and q_m should be sorted in increasing order before calling this function. (FPCore (p r q_m) :precision binary64 p)
q_m = fabs(q);
assert(p < r && r < q_m);
double code(double p, double r, double q_m) {
return p;
}
q_m = private
NOTE: p, r, and q_m should be sorted in increasing order before calling this function.
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, r, q_m)
use fmin_fmax_functions
real(8), intent (in) :: p
real(8), intent (in) :: r
real(8), intent (in) :: q_m
code = p
end function
q_m = Math.abs(q);
assert p < r && r < q_m;
public static double code(double p, double r, double q_m) {
return p;
}
q_m = math.fabs(q) [p, r, q_m] = sort([p, r, q_m]) def code(p, r, q_m): return p
q_m = abs(q) p, r, q_m = sort([p, r, q_m]) function code(p, r, q_m) return p end
q_m = abs(q);
p, r, q_m = num2cell(sort([p, r, q_m])){:}
function tmp = code(p, r, q_m)
tmp = p;
end
q_m = N[Abs[q], $MachinePrecision] NOTE: p, r, and q_m should be sorted in increasing order before calling this function. code[p_, r_, q$95$m_] := p
\begin{array}{l}
q_m = \left|q\right|
\\
[p, r, q_m] = \mathsf{sort}([p, r, q_m])\\
\\
p
\end{array}
Initial program 45.7%
Taylor expanded in p around inf
Applied rewrites8.4%
lift-*.f64N/A
lift-/.f64N/A
*-commutativeN/A
lower-*.f64N/A
Applied rewrites7.5%
Taylor expanded in p around inf
Applied rewrites1.8%
herbie shell --seed 2025105
(FPCore (p r q)
:name "1/2(abs(p)+abs(r) + sqrt((p-r)^2 + 4q^2))"
:precision binary64
(* (/ 1.0 2.0) (+ (+ (fabs p) (fabs r)) (sqrt (+ (pow (- p r) 2.0) (* 4.0 (pow q 2.0)))))))