
(FPCore (a b c d) :precision binary64 (/ (+ (* a c) (* b d)) (+ (* c c) (* d d))))
double code(double a, double b, double c, double d) {
return ((a * c) + (b * d)) / ((c * c) + (d * d));
}
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(a, b, c, d)
use fmin_fmax_functions
real(8), intent (in) :: a
real(8), intent (in) :: b
real(8), intent (in) :: c
real(8), intent (in) :: d
code = ((a * c) + (b * d)) / ((c * c) + (d * d))
end function
public static double code(double a, double b, double c, double d) {
return ((a * c) + (b * d)) / ((c * c) + (d * d));
}
def code(a, b, c, d): return ((a * c) + (b * d)) / ((c * c) + (d * d))
function code(a, b, c, d) return Float64(Float64(Float64(a * c) + Float64(b * d)) / Float64(Float64(c * c) + Float64(d * d))) end
function tmp = code(a, b, c, d) tmp = ((a * c) + (b * d)) / ((c * c) + (d * d)); end
code[a_, b_, c_, d_] := N[(N[(N[(a * c), $MachinePrecision] + N[(b * d), $MachinePrecision]), $MachinePrecision] / N[(N[(c * c), $MachinePrecision] + N[(d * d), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{a \cdot c + b \cdot d}{c \cdot c + d \cdot d}
\end{array}
Sampling outcomes in binary64 precision:
Herbie found 10 alternatives:
| Alternative | Accuracy | Speedup |
|---|
(FPCore (a b c d) :precision binary64 (/ (+ (* a c) (* b d)) (+ (* c c) (* d d))))
double code(double a, double b, double c, double d) {
return ((a * c) + (b * d)) / ((c * c) + (d * d));
}
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(a, b, c, d)
use fmin_fmax_functions
real(8), intent (in) :: a
real(8), intent (in) :: b
real(8), intent (in) :: c
real(8), intent (in) :: d
code = ((a * c) + (b * d)) / ((c * c) + (d * d))
end function
public static double code(double a, double b, double c, double d) {
return ((a * c) + (b * d)) / ((c * c) + (d * d));
}
def code(a, b, c, d): return ((a * c) + (b * d)) / ((c * c) + (d * d))
function code(a, b, c, d) return Float64(Float64(Float64(a * c) + Float64(b * d)) / Float64(Float64(c * c) + Float64(d * d))) end
function tmp = code(a, b, c, d) tmp = ((a * c) + (b * d)) / ((c * c) + (d * d)); end
code[a_, b_, c_, d_] := N[(N[(N[(a * c), $MachinePrecision] + N[(b * d), $MachinePrecision]), $MachinePrecision] / N[(N[(c * c), $MachinePrecision] + N[(d * d), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{a \cdot c + b \cdot d}{c \cdot c + d \cdot d}
\end{array}
(FPCore (a b c d)
:precision binary64
(let* ((t_0 (fma d d (* c c))) (t_1 (/ (fma (/ c d) a b) d)))
(if (<= d -2.6e+165)
t_1
(if (<= d -2.3e-60)
(fma b (/ d t_0) (* c (/ a t_0)))
(if (<= d 2.9e-126)
(/ (fma (/ d c) b a) c)
(if (<= d 5.5e+77) (/ (fma d b (* c a)) t_0) t_1))))))
double code(double a, double b, double c, double d) {
double t_0 = fma(d, d, (c * c));
double t_1 = fma((c / d), a, b) / d;
double tmp;
if (d <= -2.6e+165) {
tmp = t_1;
} else if (d <= -2.3e-60) {
tmp = fma(b, (d / t_0), (c * (a / t_0)));
} else if (d <= 2.9e-126) {
tmp = fma((d / c), b, a) / c;
} else if (d <= 5.5e+77) {
tmp = fma(d, b, (c * a)) / t_0;
} else {
tmp = t_1;
}
return tmp;
}
function code(a, b, c, d) t_0 = fma(d, d, Float64(c * c)) t_1 = Float64(fma(Float64(c / d), a, b) / d) tmp = 0.0 if (d <= -2.6e+165) tmp = t_1; elseif (d <= -2.3e-60) tmp = fma(b, Float64(d / t_0), Float64(c * Float64(a / t_0))); elseif (d <= 2.9e-126) tmp = Float64(fma(Float64(d / c), b, a) / c); elseif (d <= 5.5e+77) tmp = Float64(fma(d, b, Float64(c * a)) / t_0); else tmp = t_1; end return tmp end
code[a_, b_, c_, d_] := Block[{t$95$0 = N[(d * d + N[(c * c), $MachinePrecision]), $MachinePrecision]}, Block[{t$95$1 = N[(N[(N[(c / d), $MachinePrecision] * a + b), $MachinePrecision] / d), $MachinePrecision]}, If[LessEqual[d, -2.6e+165], t$95$1, If[LessEqual[d, -2.3e-60], N[(b * N[(d / t$95$0), $MachinePrecision] + N[(c * N[(a / t$95$0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], If[LessEqual[d, 2.9e-126], N[(N[(N[(d / c), $MachinePrecision] * b + a), $MachinePrecision] / c), $MachinePrecision], If[LessEqual[d, 5.5e+77], N[(N[(d * b + N[(c * a), $MachinePrecision]), $MachinePrecision] / t$95$0), $MachinePrecision], t$95$1]]]]]]
\begin{array}{l}
\\
\begin{array}{l}
t_0 := \mathsf{fma}\left(d, d, c \cdot c\right)\\
t_1 := \frac{\mathsf{fma}\left(\frac{c}{d}, a, b\right)}{d}\\
\mathbf{if}\;d \leq -2.6 \cdot 10^{+165}:\\
\;\;\;\;t\_1\\
\mathbf{elif}\;d \leq -2.3 \cdot 10^{-60}:\\
\;\;\;\;\mathsf{fma}\left(b, \frac{d}{t\_0}, c \cdot \frac{a}{t\_0}\right)\\
\mathbf{elif}\;d \leq 2.9 \cdot 10^{-126}:\\
\;\;\;\;\frac{\mathsf{fma}\left(\frac{d}{c}, b, a\right)}{c}\\
\mathbf{elif}\;d \leq 5.5 \cdot 10^{+77}:\\
\;\;\;\;\frac{\mathsf{fma}\left(d, b, c \cdot a\right)}{t\_0}\\
\mathbf{else}:\\
\;\;\;\;t\_1\\
\end{array}
\end{array}
if d < -2.6000000000000001e165 or 5.50000000000000036e77 < d Initial program 38.9%
Applied rewrites38.9%
Taylor expanded in c around 0
unpow2N/A
associate-/r*N/A
div-addN/A
lower-/.f64N/A
+-commutativeN/A
associate-/l*N/A
*-commutativeN/A
lower-fma.f64N/A
lower-/.f6491.9
Applied rewrites91.9%
if -2.6000000000000001e165 < d < -2.3000000000000001e-60Initial program 68.8%
Applied rewrites83.2%
if -2.3000000000000001e-60 < d < 2.89999999999999988e-126Initial program 65.8%
Applied rewrites65.8%
Taylor expanded in c around inf
lower-/.f64N/A
+-commutativeN/A
associate-/l*N/A
*-commutativeN/A
lower-fma.f64N/A
lower-/.f6493.8
Applied rewrites93.8%
if 2.89999999999999988e-126 < d < 5.50000000000000036e77Initial program 87.7%
Applied rewrites87.7%
(FPCore (a b c d)
:precision binary64
(if (<= d -1.55e+81)
(/ b d)
(if (<= d -2.35e-63)
(* (/ b (fma d d (* c c))) d)
(if (<= d 7.5e-204)
(/ a c)
(if (<= d 1.1e-5) (/ (fma a c (* b d)) (* c c)) (/ b d))))))
double code(double a, double b, double c, double d) {
double tmp;
if (d <= -1.55e+81) {
tmp = b / d;
} else if (d <= -2.35e-63) {
tmp = (b / fma(d, d, (c * c))) * d;
} else if (d <= 7.5e-204) {
tmp = a / c;
} else if (d <= 1.1e-5) {
tmp = fma(a, c, (b * d)) / (c * c);
} else {
tmp = b / d;
}
return tmp;
}
function code(a, b, c, d) tmp = 0.0 if (d <= -1.55e+81) tmp = Float64(b / d); elseif (d <= -2.35e-63) tmp = Float64(Float64(b / fma(d, d, Float64(c * c))) * d); elseif (d <= 7.5e-204) tmp = Float64(a / c); elseif (d <= 1.1e-5) tmp = Float64(fma(a, c, Float64(b * d)) / Float64(c * c)); else tmp = Float64(b / d); end return tmp end
code[a_, b_, c_, d_] := If[LessEqual[d, -1.55e+81], N[(b / d), $MachinePrecision], If[LessEqual[d, -2.35e-63], N[(N[(b / N[(d * d + N[(c * c), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] * d), $MachinePrecision], If[LessEqual[d, 7.5e-204], N[(a / c), $MachinePrecision], If[LessEqual[d, 1.1e-5], N[(N[(a * c + N[(b * d), $MachinePrecision]), $MachinePrecision] / N[(c * c), $MachinePrecision]), $MachinePrecision], N[(b / d), $MachinePrecision]]]]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;d \leq -1.55 \cdot 10^{+81}:\\
\;\;\;\;\frac{b}{d}\\
\mathbf{elif}\;d \leq -2.35 \cdot 10^{-63}:\\
\;\;\;\;\frac{b}{\mathsf{fma}\left(d, d, c \cdot c\right)} \cdot d\\
\mathbf{elif}\;d \leq 7.5 \cdot 10^{-204}:\\
\;\;\;\;\frac{a}{c}\\
\mathbf{elif}\;d \leq 1.1 \cdot 10^{-5}:\\
\;\;\;\;\frac{\mathsf{fma}\left(a, c, b \cdot d\right)}{c \cdot c}\\
\mathbf{else}:\\
\;\;\;\;\frac{b}{d}\\
\end{array}
\end{array}
if d < -1.55e81 or 1.1e-5 < d Initial program 51.2%
Taylor expanded in c around 0
lower-/.f6478.7
Applied rewrites78.7%
if -1.55e81 < d < -2.35e-63Initial program 72.0%
Taylor expanded in a around 0
*-commutativeN/A
associate-/l*N/A
*-commutativeN/A
lower-*.f64N/A
lower-/.f64N/A
+-commutativeN/A
unpow2N/A
lower-fma.f64N/A
unpow2N/A
lower-*.f6458.0
Applied rewrites58.0%
if -2.35e-63 < d < 7.5000000000000003e-204Initial program 62.8%
Taylor expanded in c around inf
lower-/.f6474.4
Applied rewrites74.4%
if 7.5000000000000003e-204 < d < 1.1e-5Initial program 87.0%
Taylor expanded in c around inf
unpow2N/A
lower-*.f6470.8
Applied rewrites70.8%
lift-+.f64N/A
lift-*.f64N/A
lower-fma.f6470.8
Applied rewrites70.8%
Final simplification73.9%
(FPCore (a b c d)
:precision binary64
(let* ((t_0 (/ (fma (/ c d) a b) d)))
(if (<= d -7.5e+68)
t_0
(if (<= d 2.9e-126)
(/ (fma (/ d c) b a) c)
(if (<= d 5.5e+77) (/ (fma d b (* c a)) (fma d d (* c c))) t_0)))))
double code(double a, double b, double c, double d) {
double t_0 = fma((c / d), a, b) / d;
double tmp;
if (d <= -7.5e+68) {
tmp = t_0;
} else if (d <= 2.9e-126) {
tmp = fma((d / c), b, a) / c;
} else if (d <= 5.5e+77) {
tmp = fma(d, b, (c * a)) / fma(d, d, (c * c));
} else {
tmp = t_0;
}
return tmp;
}
function code(a, b, c, d) t_0 = Float64(fma(Float64(c / d), a, b) / d) tmp = 0.0 if (d <= -7.5e+68) tmp = t_0; elseif (d <= 2.9e-126) tmp = Float64(fma(Float64(d / c), b, a) / c); elseif (d <= 5.5e+77) tmp = Float64(fma(d, b, Float64(c * a)) / fma(d, d, Float64(c * c))); else tmp = t_0; end return tmp end
code[a_, b_, c_, d_] := Block[{t$95$0 = N[(N[(N[(c / d), $MachinePrecision] * a + b), $MachinePrecision] / d), $MachinePrecision]}, If[LessEqual[d, -7.5e+68], t$95$0, If[LessEqual[d, 2.9e-126], N[(N[(N[(d / c), $MachinePrecision] * b + a), $MachinePrecision] / c), $MachinePrecision], If[LessEqual[d, 5.5e+77], N[(N[(d * b + N[(c * a), $MachinePrecision]), $MachinePrecision] / N[(d * d + N[(c * c), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], t$95$0]]]]
\begin{array}{l}
\\
\begin{array}{l}
t_0 := \frac{\mathsf{fma}\left(\frac{c}{d}, a, b\right)}{d}\\
\mathbf{if}\;d \leq -7.5 \cdot 10^{+68}:\\
\;\;\;\;t\_0\\
\mathbf{elif}\;d \leq 2.9 \cdot 10^{-126}:\\
\;\;\;\;\frac{\mathsf{fma}\left(\frac{d}{c}, b, a\right)}{c}\\
\mathbf{elif}\;d \leq 5.5 \cdot 10^{+77}:\\
\;\;\;\;\frac{\mathsf{fma}\left(d, b, c \cdot a\right)}{\mathsf{fma}\left(d, d, c \cdot c\right)}\\
\mathbf{else}:\\
\;\;\;\;t\_0\\
\end{array}
\end{array}
if d < -7.49999999999999959e68 or 5.50000000000000036e77 < d Initial program 45.5%
Applied rewrites45.5%
Taylor expanded in c around 0
unpow2N/A
associate-/r*N/A
div-addN/A
lower-/.f64N/A
+-commutativeN/A
associate-/l*N/A
*-commutativeN/A
lower-fma.f64N/A
lower-/.f6489.3
Applied rewrites89.3%
if -7.49999999999999959e68 < d < 2.89999999999999988e-126Initial program 66.7%
Applied rewrites66.7%
Taylor expanded in c around inf
lower-/.f64N/A
+-commutativeN/A
associate-/l*N/A
*-commutativeN/A
lower-fma.f64N/A
lower-/.f6486.0
Applied rewrites86.0%
if 2.89999999999999988e-126 < d < 5.50000000000000036e77Initial program 87.7%
Applied rewrites87.7%
(FPCore (a b c d)
:precision binary64
(let* ((t_0 (/ (fma (/ a d) c b) d)))
(if (<= d -9e-54)
t_0
(if (<= d 7.5e-204)
(/ a c)
(if (<= d 9.5e-11) (/ (fma a c (* b d)) (* c c)) t_0)))))
double code(double a, double b, double c, double d) {
double t_0 = fma((a / d), c, b) / d;
double tmp;
if (d <= -9e-54) {
tmp = t_0;
} else if (d <= 7.5e-204) {
tmp = a / c;
} else if (d <= 9.5e-11) {
tmp = fma(a, c, (b * d)) / (c * c);
} else {
tmp = t_0;
}
return tmp;
}
function code(a, b, c, d) t_0 = Float64(fma(Float64(a / d), c, b) / d) tmp = 0.0 if (d <= -9e-54) tmp = t_0; elseif (d <= 7.5e-204) tmp = Float64(a / c); elseif (d <= 9.5e-11) tmp = Float64(fma(a, c, Float64(b * d)) / Float64(c * c)); else tmp = t_0; end return tmp end
code[a_, b_, c_, d_] := Block[{t$95$0 = N[(N[(N[(a / d), $MachinePrecision] * c + b), $MachinePrecision] / d), $MachinePrecision]}, If[LessEqual[d, -9e-54], t$95$0, If[LessEqual[d, 7.5e-204], N[(a / c), $MachinePrecision], If[LessEqual[d, 9.5e-11], N[(N[(a * c + N[(b * d), $MachinePrecision]), $MachinePrecision] / N[(c * c), $MachinePrecision]), $MachinePrecision], t$95$0]]]]
\begin{array}{l}
\\
\begin{array}{l}
t_0 := \frac{\mathsf{fma}\left(\frac{a}{d}, c, b\right)}{d}\\
\mathbf{if}\;d \leq -9 \cdot 10^{-54}:\\
\;\;\;\;t\_0\\
\mathbf{elif}\;d \leq 7.5 \cdot 10^{-204}:\\
\;\;\;\;\frac{a}{c}\\
\mathbf{elif}\;d \leq 9.5 \cdot 10^{-11}:\\
\;\;\;\;\frac{\mathsf{fma}\left(a, c, b \cdot d\right)}{c \cdot c}\\
\mathbf{else}:\\
\;\;\;\;t\_0\\
\end{array}
\end{array}
if d < -8.9999999999999997e-54 or 9.49999999999999951e-11 < d Initial program 55.1%
Taylor expanded in c around 0
+-commutativeN/A
associate-*l/N/A
associate-*l/N/A
unpow2N/A
associate-/r*N/A
div-addN/A
+-commutativeN/A
lower-/.f64N/A
+-commutativeN/A
*-commutativeN/A
associate-/l*N/A
*-commutativeN/A
lower-fma.f64N/A
lower-/.f6479.3
Applied rewrites79.3%
if -8.9999999999999997e-54 < d < 7.5000000000000003e-204Initial program 63.2%
Taylor expanded in c around inf
lower-/.f6473.7
Applied rewrites73.7%
if 7.5000000000000003e-204 < d < 9.49999999999999951e-11Initial program 87.0%
Taylor expanded in c around inf
unpow2N/A
lower-*.f6470.8
Applied rewrites70.8%
lift-+.f64N/A
lift-*.f64N/A
lower-fma.f6470.8
Applied rewrites70.8%
Final simplification76.2%
(FPCore (a b c d) :precision binary64 (if (or (<= d -7.5e+68) (not (<= d 3000.0))) (/ (fma (/ c d) a b) d) (/ (fma (/ d c) b a) c)))
double code(double a, double b, double c, double d) {
double tmp;
if ((d <= -7.5e+68) || !(d <= 3000.0)) {
tmp = fma((c / d), a, b) / d;
} else {
tmp = fma((d / c), b, a) / c;
}
return tmp;
}
function code(a, b, c, d) tmp = 0.0 if ((d <= -7.5e+68) || !(d <= 3000.0)) tmp = Float64(fma(Float64(c / d), a, b) / d); else tmp = Float64(fma(Float64(d / c), b, a) / c); end return tmp end
code[a_, b_, c_, d_] := If[Or[LessEqual[d, -7.5e+68], N[Not[LessEqual[d, 3000.0]], $MachinePrecision]], N[(N[(N[(c / d), $MachinePrecision] * a + b), $MachinePrecision] / d), $MachinePrecision], N[(N[(N[(d / c), $MachinePrecision] * b + a), $MachinePrecision] / c), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;d \leq -7.5 \cdot 10^{+68} \lor \neg \left(d \leq 3000\right):\\
\;\;\;\;\frac{\mathsf{fma}\left(\frac{c}{d}, a, b\right)}{d}\\
\mathbf{else}:\\
\;\;\;\;\frac{\mathsf{fma}\left(\frac{d}{c}, b, a\right)}{c}\\
\end{array}
\end{array}
if d < -7.49999999999999959e68 or 3e3 < d Initial program 51.7%
Applied rewrites51.7%
Taylor expanded in c around 0
unpow2N/A
associate-/r*N/A
div-addN/A
lower-/.f64N/A
+-commutativeN/A
associate-/l*N/A
*-commutativeN/A
lower-fma.f64N/A
lower-/.f6487.0
Applied rewrites87.0%
if -7.49999999999999959e68 < d < 3e3Initial program 71.8%
Applied rewrites71.8%
Taylor expanded in c around inf
lower-/.f64N/A
+-commutativeN/A
associate-/l*N/A
*-commutativeN/A
lower-fma.f64N/A
lower-/.f6483.9
Applied rewrites83.9%
Final simplification85.2%
(FPCore (a b c d) :precision binary64 (if (or (<= d -7.5e+68) (not (<= d 3000.0))) (/ (fma (/ c d) a b) d) (/ (fma (/ b c) d a) c)))
double code(double a, double b, double c, double d) {
double tmp;
if ((d <= -7.5e+68) || !(d <= 3000.0)) {
tmp = fma((c / d), a, b) / d;
} else {
tmp = fma((b / c), d, a) / c;
}
return tmp;
}
function code(a, b, c, d) tmp = 0.0 if ((d <= -7.5e+68) || !(d <= 3000.0)) tmp = Float64(fma(Float64(c / d), a, b) / d); else tmp = Float64(fma(Float64(b / c), d, a) / c); end return tmp end
code[a_, b_, c_, d_] := If[Or[LessEqual[d, -7.5e+68], N[Not[LessEqual[d, 3000.0]], $MachinePrecision]], N[(N[(N[(c / d), $MachinePrecision] * a + b), $MachinePrecision] / d), $MachinePrecision], N[(N[(N[(b / c), $MachinePrecision] * d + a), $MachinePrecision] / c), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;d \leq -7.5 \cdot 10^{+68} \lor \neg \left(d \leq 3000\right):\\
\;\;\;\;\frac{\mathsf{fma}\left(\frac{c}{d}, a, b\right)}{d}\\
\mathbf{else}:\\
\;\;\;\;\frac{\mathsf{fma}\left(\frac{b}{c}, d, a\right)}{c}\\
\end{array}
\end{array}
if d < -7.49999999999999959e68 or 3e3 < d Initial program 51.7%
Applied rewrites51.7%
Taylor expanded in c around 0
unpow2N/A
associate-/r*N/A
div-addN/A
lower-/.f64N/A
+-commutativeN/A
associate-/l*N/A
*-commutativeN/A
lower-fma.f64N/A
lower-/.f6487.0
Applied rewrites87.0%
if -7.49999999999999959e68 < d < 3e3Initial program 71.8%
Taylor expanded in c around inf
lower-/.f64N/A
*-lft-identityN/A
metadata-evalN/A
associate-*r*N/A
distribute-lft-outN/A
+-commutativeN/A
distribute-rgt-inN/A
*-commutativeN/A
mul-1-negN/A
mul-1-negN/A
remove-double-negN/A
*-commutativeN/A
associate-/l*N/A
*-commutativeN/A
*-commutativeN/A
mul-1-negN/A
mul-1-negN/A
remove-double-negN/A
lower-fma.f64N/A
lower-/.f6483.2
Applied rewrites83.2%
Final simplification84.8%
(FPCore (a b c d) :precision binary64 (if (or (<= d -7.5e+68) (not (<= d 3000.0))) (/ (fma (/ a d) c b) d) (/ (fma (/ b c) d a) c)))
double code(double a, double b, double c, double d) {
double tmp;
if ((d <= -7.5e+68) || !(d <= 3000.0)) {
tmp = fma((a / d), c, b) / d;
} else {
tmp = fma((b / c), d, a) / c;
}
return tmp;
}
function code(a, b, c, d) tmp = 0.0 if ((d <= -7.5e+68) || !(d <= 3000.0)) tmp = Float64(fma(Float64(a / d), c, b) / d); else tmp = Float64(fma(Float64(b / c), d, a) / c); end return tmp end
code[a_, b_, c_, d_] := If[Or[LessEqual[d, -7.5e+68], N[Not[LessEqual[d, 3000.0]], $MachinePrecision]], N[(N[(N[(a / d), $MachinePrecision] * c + b), $MachinePrecision] / d), $MachinePrecision], N[(N[(N[(b / c), $MachinePrecision] * d + a), $MachinePrecision] / c), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;d \leq -7.5 \cdot 10^{+68} \lor \neg \left(d \leq 3000\right):\\
\;\;\;\;\frac{\mathsf{fma}\left(\frac{a}{d}, c, b\right)}{d}\\
\mathbf{else}:\\
\;\;\;\;\frac{\mathsf{fma}\left(\frac{b}{c}, d, a\right)}{c}\\
\end{array}
\end{array}
if d < -7.49999999999999959e68 or 3e3 < d Initial program 51.7%
Taylor expanded in c around 0
+-commutativeN/A
associate-*l/N/A
associate-*l/N/A
unpow2N/A
associate-/r*N/A
div-addN/A
+-commutativeN/A
lower-/.f64N/A
+-commutativeN/A
*-commutativeN/A
associate-/l*N/A
*-commutativeN/A
lower-fma.f64N/A
lower-/.f6486.9
Applied rewrites86.9%
if -7.49999999999999959e68 < d < 3e3Initial program 71.8%
Taylor expanded in c around inf
lower-/.f64N/A
*-lft-identityN/A
metadata-evalN/A
associate-*r*N/A
distribute-lft-outN/A
+-commutativeN/A
distribute-rgt-inN/A
*-commutativeN/A
mul-1-negN/A
mul-1-negN/A
remove-double-negN/A
*-commutativeN/A
associate-/l*N/A
*-commutativeN/A
*-commutativeN/A
mul-1-negN/A
mul-1-negN/A
remove-double-negN/A
lower-fma.f64N/A
lower-/.f6483.2
Applied rewrites83.2%
Final simplification84.8%
(FPCore (a b c d)
:precision binary64
(if (<= d -1.55e+81)
(/ b d)
(if (<= d -2.35e-63)
(* (/ b (fma d d (* c c))) d)
(if (<= d 1.1e-5) (/ a c) (/ b d)))))
double code(double a, double b, double c, double d) {
double tmp;
if (d <= -1.55e+81) {
tmp = b / d;
} else if (d <= -2.35e-63) {
tmp = (b / fma(d, d, (c * c))) * d;
} else if (d <= 1.1e-5) {
tmp = a / c;
} else {
tmp = b / d;
}
return tmp;
}
function code(a, b, c, d) tmp = 0.0 if (d <= -1.55e+81) tmp = Float64(b / d); elseif (d <= -2.35e-63) tmp = Float64(Float64(b / fma(d, d, Float64(c * c))) * d); elseif (d <= 1.1e-5) tmp = Float64(a / c); else tmp = Float64(b / d); end return tmp end
code[a_, b_, c_, d_] := If[LessEqual[d, -1.55e+81], N[(b / d), $MachinePrecision], If[LessEqual[d, -2.35e-63], N[(N[(b / N[(d * d + N[(c * c), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] * d), $MachinePrecision], If[LessEqual[d, 1.1e-5], N[(a / c), $MachinePrecision], N[(b / d), $MachinePrecision]]]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;d \leq -1.55 \cdot 10^{+81}:\\
\;\;\;\;\frac{b}{d}\\
\mathbf{elif}\;d \leq -2.35 \cdot 10^{-63}:\\
\;\;\;\;\frac{b}{\mathsf{fma}\left(d, d, c \cdot c\right)} \cdot d\\
\mathbf{elif}\;d \leq 1.1 \cdot 10^{-5}:\\
\;\;\;\;\frac{a}{c}\\
\mathbf{else}:\\
\;\;\;\;\frac{b}{d}\\
\end{array}
\end{array}
if d < -1.55e81 or 1.1e-5 < d Initial program 51.2%
Taylor expanded in c around 0
lower-/.f6478.7
Applied rewrites78.7%
if -1.55e81 < d < -2.35e-63Initial program 72.0%
Taylor expanded in a around 0
*-commutativeN/A
associate-/l*N/A
*-commutativeN/A
lower-*.f64N/A
lower-/.f64N/A
+-commutativeN/A
unpow2N/A
lower-fma.f64N/A
unpow2N/A
lower-*.f6458.0
Applied rewrites58.0%
if -2.35e-63 < d < 1.1e-5Initial program 72.0%
Taylor expanded in c around inf
lower-/.f6469.4
Applied rewrites69.4%
Final simplification72.2%
(FPCore (a b c d) :precision binary64 (if (or (<= d -1.25e-50) (not (<= d 1.1e-5))) (/ b d) (/ a c)))
double code(double a, double b, double c, double d) {
double tmp;
if ((d <= -1.25e-50) || !(d <= 1.1e-5)) {
tmp = b / d;
} else {
tmp = a / c;
}
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(a, b, c, d)
use fmin_fmax_functions
real(8), intent (in) :: a
real(8), intent (in) :: b
real(8), intent (in) :: c
real(8), intent (in) :: d
real(8) :: tmp
if ((d <= (-1.25d-50)) .or. (.not. (d <= 1.1d-5))) then
tmp = b / d
else
tmp = a / c
end if
code = tmp
end function
public static double code(double a, double b, double c, double d) {
double tmp;
if ((d <= -1.25e-50) || !(d <= 1.1e-5)) {
tmp = b / d;
} else {
tmp = a / c;
}
return tmp;
}
def code(a, b, c, d): tmp = 0 if (d <= -1.25e-50) or not (d <= 1.1e-5): tmp = b / d else: tmp = a / c return tmp
function code(a, b, c, d) tmp = 0.0 if ((d <= -1.25e-50) || !(d <= 1.1e-5)) tmp = Float64(b / d); else tmp = Float64(a / c); end return tmp end
function tmp_2 = code(a, b, c, d) tmp = 0.0; if ((d <= -1.25e-50) || ~((d <= 1.1e-5))) tmp = b / d; else tmp = a / c; end tmp_2 = tmp; end
code[a_, b_, c_, d_] := If[Or[LessEqual[d, -1.25e-50], N[Not[LessEqual[d, 1.1e-5]], $MachinePrecision]], N[(b / d), $MachinePrecision], N[(a / c), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;d \leq -1.25 \cdot 10^{-50} \lor \neg \left(d \leq 1.1 \cdot 10^{-5}\right):\\
\;\;\;\;\frac{b}{d}\\
\mathbf{else}:\\
\;\;\;\;\frac{a}{c}\\
\end{array}
\end{array}
if d < -1.24999999999999992e-50 or 1.1e-5 < d Initial program 55.1%
Taylor expanded in c around 0
lower-/.f6471.4
Applied rewrites71.4%
if -1.24999999999999992e-50 < d < 1.1e-5Initial program 72.2%
Taylor expanded in c around inf
lower-/.f6469.1
Applied rewrites69.1%
Final simplification70.3%
(FPCore (a b c d) :precision binary64 (/ a c))
double code(double a, double b, double c, double d) {
return a / c;
}
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(a, b, c, d)
use fmin_fmax_functions
real(8), intent (in) :: a
real(8), intent (in) :: b
real(8), intent (in) :: c
real(8), intent (in) :: d
code = a / c
end function
public static double code(double a, double b, double c, double d) {
return a / c;
}
def code(a, b, c, d): return a / c
function code(a, b, c, d) return Float64(a / c) end
function tmp = code(a, b, c, d) tmp = a / c; end
code[a_, b_, c_, d_] := N[(a / c), $MachinePrecision]
\begin{array}{l}
\\
\frac{a}{c}
\end{array}
Initial program 63.2%
Taylor expanded in c around inf
lower-/.f6442.3
Applied rewrites42.3%
Final simplification42.3%
(FPCore (a b c d) :precision binary64 (if (< (fabs d) (fabs c)) (/ (+ a (* b (/ d c))) (+ c (* d (/ d c)))) (/ (+ b (* a (/ c d))) (+ d (* c (/ c d))))))
double code(double a, double b, double c, double d) {
double tmp;
if (fabs(d) < fabs(c)) {
tmp = (a + (b * (d / c))) / (c + (d * (d / c)));
} else {
tmp = (b + (a * (c / d))) / (d + (c * (c / d)));
}
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(a, b, c, d)
use fmin_fmax_functions
real(8), intent (in) :: a
real(8), intent (in) :: b
real(8), intent (in) :: c
real(8), intent (in) :: d
real(8) :: tmp
if (abs(d) < abs(c)) then
tmp = (a + (b * (d / c))) / (c + (d * (d / c)))
else
tmp = (b + (a * (c / d))) / (d + (c * (c / d)))
end if
code = tmp
end function
public static double code(double a, double b, double c, double d) {
double tmp;
if (Math.abs(d) < Math.abs(c)) {
tmp = (a + (b * (d / c))) / (c + (d * (d / c)));
} else {
tmp = (b + (a * (c / d))) / (d + (c * (c / d)));
}
return tmp;
}
def code(a, b, c, d): tmp = 0 if math.fabs(d) < math.fabs(c): tmp = (a + (b * (d / c))) / (c + (d * (d / c))) else: tmp = (b + (a * (c / d))) / (d + (c * (c / d))) return tmp
function code(a, b, c, d) tmp = 0.0 if (abs(d) < abs(c)) tmp = Float64(Float64(a + Float64(b * Float64(d / c))) / Float64(c + Float64(d * Float64(d / c)))); else tmp = Float64(Float64(b + Float64(a * Float64(c / d))) / Float64(d + Float64(c * Float64(c / d)))); end return tmp end
function tmp_2 = code(a, b, c, d) tmp = 0.0; if (abs(d) < abs(c)) tmp = (a + (b * (d / c))) / (c + (d * (d / c))); else tmp = (b + (a * (c / d))) / (d + (c * (c / d))); end tmp_2 = tmp; end
code[a_, b_, c_, d_] := If[Less[N[Abs[d], $MachinePrecision], N[Abs[c], $MachinePrecision]], N[(N[(a + N[(b * N[(d / c), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] / N[(c + N[(d * N[(d / c), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], N[(N[(b + N[(a * N[(c / d), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] / N[(d + N[(c * N[(c / d), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;\left|d\right| < \left|c\right|:\\
\;\;\;\;\frac{a + b \cdot \frac{d}{c}}{c + d \cdot \frac{d}{c}}\\
\mathbf{else}:\\
\;\;\;\;\frac{b + a \cdot \frac{c}{d}}{d + c \cdot \frac{c}{d}}\\
\end{array}
\end{array}
herbie shell --seed 2024360
(FPCore (a b c d)
:name "Complex division, real part"
:precision binary64
:alt
(! :herbie-platform default (if (< (fabs d) (fabs c)) (/ (+ a (* b (/ d c))) (+ c (* d (/ d c)))) (/ (+ b (* a (/ c d))) (+ d (* c (/ c d))))))
(/ (+ (* a c) (* b d)) (+ (* c c) (* d d))))