
(FPCore (x y z) :precision binary64 (+ (+ (+ (* x y) (* z z)) (* z z)) (* z z)))
double code(double x, double y, double z) {
return (((x * y) + (z * z)) + (z * z)) + (z * z);
}
module fmin_fmax_functions
implicit none
private
public fmax
public fmin
interface fmax
module procedure fmax88
module procedure fmax44
module procedure fmax84
module procedure fmax48
end interface
interface fmin
module procedure fmin88
module procedure fmin44
module procedure fmin84
module procedure fmin48
end interface
contains
real(8) function fmax88(x, y) result (res)
real(8), intent (in) :: x
real(8), intent (in) :: y
res = merge(y, merge(x, max(x, y), y /= y), x /= x)
end function
real(4) function fmax44(x, y) result (res)
real(4), intent (in) :: x
real(4), intent (in) :: y
res = merge(y, merge(x, max(x, y), y /= y), x /= x)
end function
real(8) function fmax84(x, y) result(res)
real(8), intent (in) :: x
real(4), intent (in) :: y
res = merge(dble(y), merge(x, max(x, dble(y)), y /= y), x /= x)
end function
real(8) function fmax48(x, y) result(res)
real(4), intent (in) :: x
real(8), intent (in) :: y
res = merge(y, merge(dble(x), max(dble(x), y), y /= y), x /= x)
end function
real(8) function fmin88(x, y) result (res)
real(8), intent (in) :: x
real(8), intent (in) :: y
res = merge(y, merge(x, min(x, y), y /= y), x /= x)
end function
real(4) function fmin44(x, y) result (res)
real(4), intent (in) :: x
real(4), intent (in) :: y
res = merge(y, merge(x, min(x, y), y /= y), x /= x)
end function
real(8) function fmin84(x, y) result(res)
real(8), intent (in) :: x
real(4), intent (in) :: y
res = merge(dble(y), merge(x, min(x, dble(y)), y /= y), x /= x)
end function
real(8) function fmin48(x, y) result(res)
real(4), intent (in) :: x
real(8), intent (in) :: y
res = merge(y, merge(dble(x), min(dble(x), y), y /= y), x /= x)
end function
end module
real(8) function code(x, y, z)
use fmin_fmax_functions
real(8), intent (in) :: x
real(8), intent (in) :: y
real(8), intent (in) :: z
code = (((x * y) + (z * z)) + (z * z)) + (z * z)
end function
public static double code(double x, double y, double z) {
return (((x * y) + (z * z)) + (z * z)) + (z * z);
}
def code(x, y, z): return (((x * y) + (z * z)) + (z * z)) + (z * z)
function code(x, y, z) return Float64(Float64(Float64(Float64(x * y) + Float64(z * z)) + Float64(z * z)) + Float64(z * z)) end
function tmp = code(x, y, z) tmp = (((x * y) + (z * z)) + (z * z)) + (z * z); end
code[x_, y_, z_] := N[(N[(N[(N[(x * y), $MachinePrecision] + N[(z * z), $MachinePrecision]), $MachinePrecision] + N[(z * z), $MachinePrecision]), $MachinePrecision] + N[(z * z), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\left(\left(x \cdot y + z \cdot z\right) + z \cdot z\right) + z \cdot z
\end{array}
Herbie found 6 alternatives:
| Alternative | Accuracy | Speedup |
|---|
(FPCore (x y z) :precision binary64 (+ (+ (+ (* x y) (* z z)) (* z z)) (* z z)))
double code(double x, double y, double z) {
return (((x * y) + (z * z)) + (z * z)) + (z * z);
}
module fmin_fmax_functions
implicit none
private
public fmax
public fmin
interface fmax
module procedure fmax88
module procedure fmax44
module procedure fmax84
module procedure fmax48
end interface
interface fmin
module procedure fmin88
module procedure fmin44
module procedure fmin84
module procedure fmin48
end interface
contains
real(8) function fmax88(x, y) result (res)
real(8), intent (in) :: x
real(8), intent (in) :: y
res = merge(y, merge(x, max(x, y), y /= y), x /= x)
end function
real(4) function fmax44(x, y) result (res)
real(4), intent (in) :: x
real(4), intent (in) :: y
res = merge(y, merge(x, max(x, y), y /= y), x /= x)
end function
real(8) function fmax84(x, y) result(res)
real(8), intent (in) :: x
real(4), intent (in) :: y
res = merge(dble(y), merge(x, max(x, dble(y)), y /= y), x /= x)
end function
real(8) function fmax48(x, y) result(res)
real(4), intent (in) :: x
real(8), intent (in) :: y
res = merge(y, merge(dble(x), max(dble(x), y), y /= y), x /= x)
end function
real(8) function fmin88(x, y) result (res)
real(8), intent (in) :: x
real(8), intent (in) :: y
res = merge(y, merge(x, min(x, y), y /= y), x /= x)
end function
real(4) function fmin44(x, y) result (res)
real(4), intent (in) :: x
real(4), intent (in) :: y
res = merge(y, merge(x, min(x, y), y /= y), x /= x)
end function
real(8) function fmin84(x, y) result(res)
real(8), intent (in) :: x
real(4), intent (in) :: y
res = merge(dble(y), merge(x, min(x, dble(y)), y /= y), x /= x)
end function
real(8) function fmin48(x, y) result(res)
real(4), intent (in) :: x
real(8), intent (in) :: y
res = merge(y, merge(dble(x), min(dble(x), y), y /= y), x /= x)
end function
end module
real(8) function code(x, y, z)
use fmin_fmax_functions
real(8), intent (in) :: x
real(8), intent (in) :: y
real(8), intent (in) :: z
code = (((x * y) + (z * z)) + (z * z)) + (z * z)
end function
public static double code(double x, double y, double z) {
return (((x * y) + (z * z)) + (z * z)) + (z * z);
}
def code(x, y, z): return (((x * y) + (z * z)) + (z * z)) + (z * z)
function code(x, y, z) return Float64(Float64(Float64(Float64(x * y) + Float64(z * z)) + Float64(z * z)) + Float64(z * z)) end
function tmp = code(x, y, z) tmp = (((x * y) + (z * z)) + (z * z)) + (z * z); end
code[x_, y_, z_] := N[(N[(N[(N[(x * y), $MachinePrecision] + N[(z * z), $MachinePrecision]), $MachinePrecision] + N[(z * z), $MachinePrecision]), $MachinePrecision] + N[(z * z), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\left(\left(x \cdot y + z \cdot z\right) + z \cdot z\right) + z \cdot z
\end{array}
z_m = (fabs.f64 z) (FPCore (x y z_m) :precision binary64 (if (<= z_m 2.05e+141) (fma (* z_m z_m) 3.0 (* y x)) (* (fma 3.0 (* z_m (/ z_m y)) x) y)))
z_m = fabs(z);
double code(double x, double y, double z_m) {
double tmp;
if (z_m <= 2.05e+141) {
tmp = fma((z_m * z_m), 3.0, (y * x));
} else {
tmp = fma(3.0, (z_m * (z_m / y)), x) * y;
}
return tmp;
}
z_m = abs(z) function code(x, y, z_m) tmp = 0.0 if (z_m <= 2.05e+141) tmp = fma(Float64(z_m * z_m), 3.0, Float64(y * x)); else tmp = Float64(fma(3.0, Float64(z_m * Float64(z_m / y)), x) * y); end return tmp end
z_m = N[Abs[z], $MachinePrecision] code[x_, y_, z$95$m_] := If[LessEqual[z$95$m, 2.05e+141], N[(N[(z$95$m * z$95$m), $MachinePrecision] * 3.0 + N[(y * x), $MachinePrecision]), $MachinePrecision], N[(N[(3.0 * N[(z$95$m * N[(z$95$m / y), $MachinePrecision]), $MachinePrecision] + x), $MachinePrecision] * y), $MachinePrecision]]
\begin{array}{l}
z_m = \left|z\right|
\\
\begin{array}{l}
\mathbf{if}\;z\_m \leq 2.05 \cdot 10^{+141}:\\
\;\;\;\;\mathsf{fma}\left(z\_m \cdot z\_m, 3, y \cdot x\right)\\
\mathbf{else}:\\
\;\;\;\;\mathsf{fma}\left(3, z\_m \cdot \frac{z\_m}{y}, x\right) \cdot y\\
\end{array}
\end{array}
if z < 2.05000000000000011e141Initial program 99.8%
lift-*.f64N/A
lift-+.f64N/A
lift-*.f64N/A
lift-+.f64N/A
lift-*.f64N/A
lift-*.f64N/A
lift-+.f64N/A
associate-+l+N/A
count-2-revN/A
pow2N/A
+-commutativeN/A
+-commutativeN/A
associate-+r+N/A
pow2N/A
distribute-lft1-inN/A
metadata-evalN/A
*-commutativeN/A
lower-fma.f64N/A
Applied rewrites99.8%
if 2.05000000000000011e141 < z Initial program 94.9%
Taylor expanded in y around inf
*-commutativeN/A
lower-*.f64N/A
+-commutativeN/A
distribute-lft1-inN/A
metadata-evalN/A
lower-fma.f64N/A
pow2N/A
lower-/.f64N/A
lift-*.f6495.4
Applied rewrites95.4%
lift-*.f64N/A
lift-/.f64N/A
associate-/l*N/A
lower-*.f64N/A
lower-/.f6497.0
Applied rewrites97.0%
z_m = (fabs.f64 z) (FPCore (x y z_m) :precision binary64 (if (<= z_m 1e+195) (fma (* 3.0 z_m) z_m (* y x)) (* 3.0 (* z_m z_m))))
z_m = fabs(z);
double code(double x, double y, double z_m) {
double tmp;
if (z_m <= 1e+195) {
tmp = fma((3.0 * z_m), z_m, (y * x));
} else {
tmp = 3.0 * (z_m * z_m);
}
return tmp;
}
z_m = abs(z) function code(x, y, z_m) tmp = 0.0 if (z_m <= 1e+195) tmp = fma(Float64(3.0 * z_m), z_m, Float64(y * x)); else tmp = Float64(3.0 * Float64(z_m * z_m)); end return tmp end
z_m = N[Abs[z], $MachinePrecision] code[x_, y_, z$95$m_] := If[LessEqual[z$95$m, 1e+195], N[(N[(3.0 * z$95$m), $MachinePrecision] * z$95$m + N[(y * x), $MachinePrecision]), $MachinePrecision], N[(3.0 * N[(z$95$m * z$95$m), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
z_m = \left|z\right|
\\
\begin{array}{l}
\mathbf{if}\;z\_m \leq 10^{+195}:\\
\;\;\;\;\mathsf{fma}\left(3 \cdot z\_m, z\_m, y \cdot x\right)\\
\mathbf{else}:\\
\;\;\;\;3 \cdot \left(z\_m \cdot z\_m\right)\\
\end{array}
\end{array}
if z < 9.99999999999999977e194Initial program 99.4%
lift-*.f64N/A
lift-+.f64N/A
lift-*.f64N/A
lift-+.f64N/A
lift-*.f64N/A
lift-*.f64N/A
lift-+.f64N/A
associate-+l+N/A
count-2-revN/A
pow2N/A
+-commutativeN/A
+-commutativeN/A
associate-+r+N/A
pow2N/A
distribute-lft1-inN/A
metadata-evalN/A
associate-*r*N/A
lower-fma.f64N/A
Applied rewrites99.7%
if 9.99999999999999977e194 < z Initial program 94.5%
Taylor expanded in x around 0
pow2N/A
pow2N/A
distribute-lft1-inN/A
metadata-evalN/A
lower-*.f64N/A
lift-*.f6499.2
Applied rewrites99.2%
z_m = (fabs.f64 z) (FPCore (x y z_m) :precision binary64 (if (<= z_m 1.8e-11) (fma z_m z_m (* x y)) (fma z_m z_m (* (+ z_m z_m) z_m))))
z_m = fabs(z);
double code(double x, double y, double z_m) {
double tmp;
if (z_m <= 1.8e-11) {
tmp = fma(z_m, z_m, (x * y));
} else {
tmp = fma(z_m, z_m, ((z_m + z_m) * z_m));
}
return tmp;
}
z_m = abs(z) function code(x, y, z_m) tmp = 0.0 if (z_m <= 1.8e-11) tmp = fma(z_m, z_m, Float64(x * y)); else tmp = fma(z_m, z_m, Float64(Float64(z_m + z_m) * z_m)); end return tmp end
z_m = N[Abs[z], $MachinePrecision] code[x_, y_, z$95$m_] := If[LessEqual[z$95$m, 1.8e-11], N[(z$95$m * z$95$m + N[(x * y), $MachinePrecision]), $MachinePrecision], N[(z$95$m * z$95$m + N[(N[(z$95$m + z$95$m), $MachinePrecision] * z$95$m), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
z_m = \left|z\right|
\\
\begin{array}{l}
\mathbf{if}\;z\_m \leq 1.8 \cdot 10^{-11}:\\
\;\;\;\;\mathsf{fma}\left(z\_m, z\_m, x \cdot y\right)\\
\mathbf{else}:\\
\;\;\;\;\mathsf{fma}\left(z\_m, z\_m, \left(z\_m + z\_m\right) \cdot z\_m\right)\\
\end{array}
\end{array}
if z < 1.79999999999999992e-11Initial program 99.9%
Taylor expanded in x around inf
*-commutativeN/A
lower-*.f6488.7
Applied rewrites88.7%
lift-*.f64N/A
lift-+.f64N/A
pow2N/A
+-commutativeN/A
pow2N/A
lower-fma.f6488.7
lift-*.f64N/A
*-commutativeN/A
lower-*.f6488.7
Applied rewrites88.7%
if 1.79999999999999992e-11 < z Initial program 97.1%
Taylor expanded in x around inf
*-commutativeN/A
lower-*.f6467.3
Applied rewrites67.3%
lift-*.f64N/A
lift-+.f64N/A
pow2N/A
+-commutativeN/A
pow2N/A
lower-fma.f6468.2
lift-*.f64N/A
*-commutativeN/A
lower-*.f6468.2
Applied rewrites68.2%
Taylor expanded in x around 0
count-2-revN/A
pow2N/A
pow2N/A
distribute-lft-inN/A
*-commutativeN/A
lower-*.f64N/A
lower-+.f6483.8
Applied rewrites83.8%
z_m = (fabs.f64 z) (FPCore (x y z_m) :precision binary64 (if (<= z_m 1.8e-11) (fma z_m z_m (* x y)) (* 3.0 (* z_m z_m))))
z_m = fabs(z);
double code(double x, double y, double z_m) {
double tmp;
if (z_m <= 1.8e-11) {
tmp = fma(z_m, z_m, (x * y));
} else {
tmp = 3.0 * (z_m * z_m);
}
return tmp;
}
z_m = abs(z) function code(x, y, z_m) tmp = 0.0 if (z_m <= 1.8e-11) tmp = fma(z_m, z_m, Float64(x * y)); else tmp = Float64(3.0 * Float64(z_m * z_m)); end return tmp end
z_m = N[Abs[z], $MachinePrecision] code[x_, y_, z$95$m_] := If[LessEqual[z$95$m, 1.8e-11], N[(z$95$m * z$95$m + N[(x * y), $MachinePrecision]), $MachinePrecision], N[(3.0 * N[(z$95$m * z$95$m), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
z_m = \left|z\right|
\\
\begin{array}{l}
\mathbf{if}\;z\_m \leq 1.8 \cdot 10^{-11}:\\
\;\;\;\;\mathsf{fma}\left(z\_m, z\_m, x \cdot y\right)\\
\mathbf{else}:\\
\;\;\;\;3 \cdot \left(z\_m \cdot z\_m\right)\\
\end{array}
\end{array}
if z < 1.79999999999999992e-11Initial program 99.9%
Taylor expanded in x around inf
*-commutativeN/A
lower-*.f6488.7
Applied rewrites88.7%
lift-*.f64N/A
lift-+.f64N/A
pow2N/A
+-commutativeN/A
pow2N/A
lower-fma.f6488.7
lift-*.f64N/A
*-commutativeN/A
lower-*.f6488.7
Applied rewrites88.7%
if 1.79999999999999992e-11 < z Initial program 97.1%
Taylor expanded in x around 0
pow2N/A
pow2N/A
distribute-lft1-inN/A
metadata-evalN/A
lower-*.f64N/A
lift-*.f6483.7
Applied rewrites83.7%
z_m = (fabs.f64 z) (FPCore (x y z_m) :precision binary64 (if (<= z_m 6.2e-12) (* y x) (* 3.0 (* z_m z_m))))
z_m = fabs(z);
double code(double x, double y, double z_m) {
double tmp;
if (z_m <= 6.2e-12) {
tmp = y * x;
} else {
tmp = 3.0 * (z_m * z_m);
}
return tmp;
}
z_m = private
module fmin_fmax_functions
implicit none
private
public fmax
public fmin
interface fmax
module procedure fmax88
module procedure fmax44
module procedure fmax84
module procedure fmax48
end interface
interface fmin
module procedure fmin88
module procedure fmin44
module procedure fmin84
module procedure fmin48
end interface
contains
real(8) function fmax88(x, y) result (res)
real(8), intent (in) :: x
real(8), intent (in) :: y
res = merge(y, merge(x, max(x, y), y /= y), x /= x)
end function
real(4) function fmax44(x, y) result (res)
real(4), intent (in) :: x
real(4), intent (in) :: y
res = merge(y, merge(x, max(x, y), y /= y), x /= x)
end function
real(8) function fmax84(x, y) result(res)
real(8), intent (in) :: x
real(4), intent (in) :: y
res = merge(dble(y), merge(x, max(x, dble(y)), y /= y), x /= x)
end function
real(8) function fmax48(x, y) result(res)
real(4), intent (in) :: x
real(8), intent (in) :: y
res = merge(y, merge(dble(x), max(dble(x), y), y /= y), x /= x)
end function
real(8) function fmin88(x, y) result (res)
real(8), intent (in) :: x
real(8), intent (in) :: y
res = merge(y, merge(x, min(x, y), y /= y), x /= x)
end function
real(4) function fmin44(x, y) result (res)
real(4), intent (in) :: x
real(4), intent (in) :: y
res = merge(y, merge(x, min(x, y), y /= y), x /= x)
end function
real(8) function fmin84(x, y) result(res)
real(8), intent (in) :: x
real(4), intent (in) :: y
res = merge(dble(y), merge(x, min(x, dble(y)), y /= y), x /= x)
end function
real(8) function fmin48(x, y) result(res)
real(4), intent (in) :: x
real(8), intent (in) :: y
res = merge(y, merge(dble(x), min(dble(x), y), y /= y), x /= x)
end function
end module
real(8) function code(x, y, z_m)
use fmin_fmax_functions
real(8), intent (in) :: x
real(8), intent (in) :: y
real(8), intent (in) :: z_m
real(8) :: tmp
if (z_m <= 6.2d-12) then
tmp = y * x
else
tmp = 3.0d0 * (z_m * z_m)
end if
code = tmp
end function
z_m = Math.abs(z);
public static double code(double x, double y, double z_m) {
double tmp;
if (z_m <= 6.2e-12) {
tmp = y * x;
} else {
tmp = 3.0 * (z_m * z_m);
}
return tmp;
}
z_m = math.fabs(z) def code(x, y, z_m): tmp = 0 if z_m <= 6.2e-12: tmp = y * x else: tmp = 3.0 * (z_m * z_m) return tmp
z_m = abs(z) function code(x, y, z_m) tmp = 0.0 if (z_m <= 6.2e-12) tmp = Float64(y * x); else tmp = Float64(3.0 * Float64(z_m * z_m)); end return tmp end
z_m = abs(z); function tmp_2 = code(x, y, z_m) tmp = 0.0; if (z_m <= 6.2e-12) tmp = y * x; else tmp = 3.0 * (z_m * z_m); end tmp_2 = tmp; end
z_m = N[Abs[z], $MachinePrecision] code[x_, y_, z$95$m_] := If[LessEqual[z$95$m, 6.2e-12], N[(y * x), $MachinePrecision], N[(3.0 * N[(z$95$m * z$95$m), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
z_m = \left|z\right|
\\
\begin{array}{l}
\mathbf{if}\;z\_m \leq 6.2 \cdot 10^{-12}:\\
\;\;\;\;y \cdot x\\
\mathbf{else}:\\
\;\;\;\;3 \cdot \left(z\_m \cdot z\_m\right)\\
\end{array}
\end{array}
if z < 6.2000000000000002e-12Initial program 99.9%
Taylor expanded in x around inf
*-commutativeN/A
lower-*.f6487.1
Applied rewrites87.1%
if 6.2000000000000002e-12 < z Initial program 97.1%
Taylor expanded in x around 0
pow2N/A
pow2N/A
distribute-lft1-inN/A
metadata-evalN/A
lower-*.f64N/A
lift-*.f6483.7
Applied rewrites83.7%
z_m = (fabs.f64 z) (FPCore (x y z_m) :precision binary64 (* y x))
z_m = fabs(z);
double code(double x, double y, double z_m) {
return y * x;
}
z_m = private
module fmin_fmax_functions
implicit none
private
public fmax
public fmin
interface fmax
module procedure fmax88
module procedure fmax44
module procedure fmax84
module procedure fmax48
end interface
interface fmin
module procedure fmin88
module procedure fmin44
module procedure fmin84
module procedure fmin48
end interface
contains
real(8) function fmax88(x, y) result (res)
real(8), intent (in) :: x
real(8), intent (in) :: y
res = merge(y, merge(x, max(x, y), y /= y), x /= x)
end function
real(4) function fmax44(x, y) result (res)
real(4), intent (in) :: x
real(4), intent (in) :: y
res = merge(y, merge(x, max(x, y), y /= y), x /= x)
end function
real(8) function fmax84(x, y) result(res)
real(8), intent (in) :: x
real(4), intent (in) :: y
res = merge(dble(y), merge(x, max(x, dble(y)), y /= y), x /= x)
end function
real(8) function fmax48(x, y) result(res)
real(4), intent (in) :: x
real(8), intent (in) :: y
res = merge(y, merge(dble(x), max(dble(x), y), y /= y), x /= x)
end function
real(8) function fmin88(x, y) result (res)
real(8), intent (in) :: x
real(8), intent (in) :: y
res = merge(y, merge(x, min(x, y), y /= y), x /= x)
end function
real(4) function fmin44(x, y) result (res)
real(4), intent (in) :: x
real(4), intent (in) :: y
res = merge(y, merge(x, min(x, y), y /= y), x /= x)
end function
real(8) function fmin84(x, y) result(res)
real(8), intent (in) :: x
real(4), intent (in) :: y
res = merge(dble(y), merge(x, min(x, dble(y)), y /= y), x /= x)
end function
real(8) function fmin48(x, y) result(res)
real(4), intent (in) :: x
real(8), intent (in) :: y
res = merge(y, merge(dble(x), min(dble(x), y), y /= y), x /= x)
end function
end module
real(8) function code(x, y, z_m)
use fmin_fmax_functions
real(8), intent (in) :: x
real(8), intent (in) :: y
real(8), intent (in) :: z_m
code = y * x
end function
z_m = Math.abs(z);
public static double code(double x, double y, double z_m) {
return y * x;
}
z_m = math.fabs(z) def code(x, y, z_m): return y * x
z_m = abs(z) function code(x, y, z_m) return Float64(y * x) end
z_m = abs(z); function tmp = code(x, y, z_m) tmp = y * x; end
z_m = N[Abs[z], $MachinePrecision] code[x_, y_, z$95$m_] := N[(y * x), $MachinePrecision]
\begin{array}{l}
z_m = \left|z\right|
\\
y \cdot x
\end{array}
Initial program 98.5%
Taylor expanded in x around inf
*-commutativeN/A
lower-*.f6453.2
Applied rewrites53.2%
herbie shell --seed 2025113
(FPCore (x y z)
:name "Linear.Quaternion:$c/ from linear-1.19.1.3, A"
:precision binary64
(+ (+ (+ (* x y) (* z z)) (* z z)) (* z z)))