
(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}
Sampling outcomes in binary64 precision:
Herbie found 5 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}
NOTE: x, y, and z should be sorted in increasing order before calling this function. (FPCore (x y z) :precision binary64 (if (<= x -2e+202) (* (fma 1.0 (* 3.0 (* z (/ z x))) y) x) (+ (+ (+ (* x y) (* z z)) (* z z)) (* z z))))
assert(x < y && y < z);
double code(double x, double y, double z) {
double tmp;
if (x <= -2e+202) {
tmp = fma(1.0, (3.0 * (z * (z / x))), y) * x;
} else {
tmp = (((x * y) + (z * z)) + (z * z)) + (z * z);
}
return tmp;
}
x, y, z = sort([x, y, z]) function code(x, y, z) tmp = 0.0 if (x <= -2e+202) tmp = Float64(fma(1.0, Float64(3.0 * Float64(z * Float64(z / x))), y) * x); else tmp = Float64(Float64(Float64(Float64(x * y) + Float64(z * z)) + Float64(z * z)) + Float64(z * z)); end return tmp end
NOTE: x, y, and z should be sorted in increasing order before calling this function. code[x_, y_, z_] := If[LessEqual[x, -2e+202], N[(N[(1.0 * N[(3.0 * N[(z * N[(z / x), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] + y), $MachinePrecision] * x), $MachinePrecision], 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}
[x, y, z] = \mathsf{sort}([x, y, z])\\
\\
\begin{array}{l}
\mathbf{if}\;x \leq -2 \cdot 10^{+202}:\\
\;\;\;\;\mathsf{fma}\left(1, 3 \cdot \left(z \cdot \frac{z}{x}\right), y\right) \cdot x\\
\mathbf{else}:\\
\;\;\;\;\left(\left(x \cdot y + z \cdot z\right) + z \cdot z\right) + z \cdot z\\
\end{array}
\end{array}
if x < -1.9999999999999998e202Initial program 95.7%
Taylor expanded in x around inf
*-commutativeN/A
lower-*.f64N/A
+-commutativeN/A
*-lft-identityN/A
lower-fma.f64N/A
distribute-lft1-inN/A
metadata-evalN/A
lower-*.f64N/A
lower-/.f64N/A
pow2N/A
lift-*.f6495.7
Applied rewrites95.7%
lift-*.f64N/A
lift-/.f64N/A
associate-/l*N/A
lower-*.f64N/A
lower-/.f64100.0
Applied rewrites100.0%
if -1.9999999999999998e202 < x Initial program 97.7%
NOTE: x, y, and z should be sorted in increasing order before calling this function. (FPCore (x y z) :precision binary64 (fma (* -1.0 z) (* -1.0 z) (fma z z (fma z z (* y x)))))
assert(x < y && y < z);
double code(double x, double y, double z) {
return fma((-1.0 * z), (-1.0 * z), fma(z, z, fma(z, z, (y * x))));
}
x, y, z = sort([x, y, z]) function code(x, y, z) return fma(Float64(-1.0 * z), Float64(-1.0 * z), fma(z, z, fma(z, z, Float64(y * x)))) end
NOTE: x, y, and z should be sorted in increasing order before calling this function. code[x_, y_, z_] := N[(N[(-1.0 * z), $MachinePrecision] * N[(-1.0 * z), $MachinePrecision] + N[(z * z + N[(z * z + N[(y * x), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
[x, y, z] = \mathsf{sort}([x, y, z])\\
\\
\mathsf{fma}\left(-1 \cdot z, -1 \cdot z, \mathsf{fma}\left(z, z, \mathsf{fma}\left(z, z, y \cdot x\right)\right)\right)
\end{array}
Initial program 97.5%
lift-+.f64N/A
lift-+.f64N/A
lift-+.f64N/A
lift-*.f64N/A
lift-*.f64N/A
lift-*.f64N/A
lift-*.f64N/A
pow2N/A
+-commutativeN/A
*-rgt-identityN/A
pow2N/A
sqr-neg-revN/A
metadata-evalN/A
unswap-sqrN/A
lower-fma.f64N/A
lower-*.f64N/A
lower-neg.f64N/A
lower-*.f64N/A
lower-neg.f64N/A
pow2N/A
+-commutativeN/A
Applied rewrites98.7%
Final simplification98.7%
NOTE: x, y, and z should be sorted in increasing order before calling this function. (FPCore (x y z) :precision binary64 (if (<= x -3.4e-28) (* (fma 1.0 (* 3.0 (* z (/ z x))) y) x) (+ (+ (+ (* x y) (* z z)) (* z z)) (exp (fma (log z) 2.0 0.0)))))
assert(x < y && y < z);
double code(double x, double y, double z) {
double tmp;
if (x <= -3.4e-28) {
tmp = fma(1.0, (3.0 * (z * (z / x))), y) * x;
} else {
tmp = (((x * y) + (z * z)) + (z * z)) + exp(fma(log(z), 2.0, 0.0));
}
return tmp;
}
x, y, z = sort([x, y, z]) function code(x, y, z) tmp = 0.0 if (x <= -3.4e-28) tmp = Float64(fma(1.0, Float64(3.0 * Float64(z * Float64(z / x))), y) * x); else tmp = Float64(Float64(Float64(Float64(x * y) + Float64(z * z)) + Float64(z * z)) + exp(fma(log(z), 2.0, 0.0))); end return tmp end
NOTE: x, y, and z should be sorted in increasing order before calling this function. code[x_, y_, z_] := If[LessEqual[x, -3.4e-28], N[(N[(1.0 * N[(3.0 * N[(z * N[(z / x), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] + y), $MachinePrecision] * x), $MachinePrecision], N[(N[(N[(N[(x * y), $MachinePrecision] + N[(z * z), $MachinePrecision]), $MachinePrecision] + N[(z * z), $MachinePrecision]), $MachinePrecision] + N[Exp[N[(N[Log[z], $MachinePrecision] * 2.0 + 0.0), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
[x, y, z] = \mathsf{sort}([x, y, z])\\
\\
\begin{array}{l}
\mathbf{if}\;x \leq -3.4 \cdot 10^{-28}:\\
\;\;\;\;\mathsf{fma}\left(1, 3 \cdot \left(z \cdot \frac{z}{x}\right), y\right) \cdot x\\
\mathbf{else}:\\
\;\;\;\;\left(\left(x \cdot y + z \cdot z\right) + z \cdot z\right) + e^{\mathsf{fma}\left(\log z, 2, 0\right)}\\
\end{array}
\end{array}
if x < -3.4000000000000001e-28Initial program 98.5%
Taylor expanded in x around inf
*-commutativeN/A
lower-*.f64N/A
+-commutativeN/A
*-lft-identityN/A
lower-fma.f64N/A
distribute-lft1-inN/A
metadata-evalN/A
lower-*.f64N/A
lower-/.f64N/A
pow2N/A
lift-*.f6498.4
Applied rewrites98.4%
lift-*.f64N/A
lift-/.f64N/A
associate-/l*N/A
lower-*.f64N/A
lower-/.f6499.9
Applied rewrites99.9%
if -3.4000000000000001e-28 < x Initial program 97.2%
lift-*.f64N/A
pow2N/A
*-rgt-identityN/A
pow-to-expN/A
1-expN/A
prod-expN/A
lower-exp.f64N/A
lower-fma.f64N/A
lower-log.f6448.0
Applied rewrites48.0%
NOTE: x, y, and z should be sorted in increasing order before calling this function. (FPCore (x y z) :precision binary64 (fma (* 3.0 (* -1.0 z)) (* -1.0 z) (* y x)))
assert(x < y && y < z);
double code(double x, double y, double z) {
return fma((3.0 * (-1.0 * z)), (-1.0 * z), (y * x));
}
x, y, z = sort([x, y, z]) function code(x, y, z) return fma(Float64(3.0 * Float64(-1.0 * z)), Float64(-1.0 * z), Float64(y * x)) end
NOTE: x, y, and z should be sorted in increasing order before calling this function. code[x_, y_, z_] := N[(N[(3.0 * N[(-1.0 * z), $MachinePrecision]), $MachinePrecision] * N[(-1.0 * z), $MachinePrecision] + N[(y * x), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
[x, y, z] = \mathsf{sort}([x, y, z])\\
\\
\mathsf{fma}\left(3 \cdot \left(-1 \cdot z\right), -1 \cdot z, y \cdot x\right)
\end{array}
Initial program 97.5%
lift-+.f64N/A
lift-+.f64N/A
lift-+.f64N/A
lift-*.f64N/A
lift-*.f64N/A
lift-*.f64N/A
lift-*.f64N/A
pow2N/A
pow2N/A
associate-+l+N/A
count-2-revN/A
pow2N/A
associate-+r+N/A
distribute-lft1-inN/A
metadata-evalN/A
+-commutativeN/A
pow2N/A
sqr-neg-revN/A
associate-*r*N/A
lower-fma.f64N/A
Applied rewrites98.7%
Final simplification98.7%
NOTE: x, y, and z should be sorted in increasing order before calling this function. (FPCore (x y z) :precision binary64 (* (fma 1.0 (* 3.0 (* z (/ z x))) y) x))
assert(x < y && y < z);
double code(double x, double y, double z) {
return fma(1.0, (3.0 * (z * (z / x))), y) * x;
}
x, y, z = sort([x, y, z]) function code(x, y, z) return Float64(fma(1.0, Float64(3.0 * Float64(z * Float64(z / x))), y) * x) end
NOTE: x, y, and z should be sorted in increasing order before calling this function. code[x_, y_, z_] := N[(N[(1.0 * N[(3.0 * N[(z * N[(z / x), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] + y), $MachinePrecision] * x), $MachinePrecision]
\begin{array}{l}
[x, y, z] = \mathsf{sort}([x, y, z])\\
\\
\mathsf{fma}\left(1, 3 \cdot \left(z \cdot \frac{z}{x}\right), y\right) \cdot x
\end{array}
Initial program 97.5%
Taylor expanded in x around inf
*-commutativeN/A
lower-*.f64N/A
+-commutativeN/A
*-lft-identityN/A
lower-fma.f64N/A
distribute-lft1-inN/A
metadata-evalN/A
lower-*.f64N/A
lower-/.f64N/A
pow2N/A
lift-*.f6491.1
Applied rewrites91.1%
lift-*.f64N/A
lift-/.f64N/A
associate-/l*N/A
lower-*.f64N/A
lower-/.f6492.3
Applied rewrites92.3%
herbie shell --seed 2025065
(FPCore (x y z)
:name "Linear.Quaternion:$c/ from linear-1.19.1.3, A"
:precision binary64
:alt
(! :herbie-platform default (+ (* (* 3 z) z) (* y x)))
(+ (+ (+ (* x y) (* z z)) (* z z)) (* z z)))