
(FPCore (x y z) :precision binary64 (/ (* x (+ y z)) z))
double code(double x, double y, double z) {
return (x * (y + 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
end function
public static double code(double x, double y, double z) {
return (x * (y + z)) / z;
}
def code(x, y, z): return (x * (y + z)) / z
function code(x, y, z) return Float64(Float64(x * Float64(y + z)) / z) end
function tmp = code(x, y, z) tmp = (x * (y + z)) / z; end
code[x_, y_, z_] := N[(N[(x * N[(y + z), $MachinePrecision]), $MachinePrecision] / z), $MachinePrecision]
\begin{array}{l}
\\
\frac{x \cdot \left(y + z\right)}{z}
\end{array}
Herbie found 4 alternatives:
| Alternative | Accuracy | Speedup |
|---|
(FPCore (x y z) :precision binary64 (/ (* x (+ y z)) z))
double code(double x, double y, double z) {
return (x * (y + 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
end function
public static double code(double x, double y, double z) {
return (x * (y + z)) / z;
}
def code(x, y, z): return (x * (y + z)) / z
function code(x, y, z) return Float64(Float64(x * Float64(y + z)) / z) end
function tmp = code(x, y, z) tmp = (x * (y + z)) / z; end
code[x_, y_, z_] := N[(N[(x * N[(y + z), $MachinePrecision]), $MachinePrecision] / z), $MachinePrecision]
\begin{array}{l}
\\
\frac{x \cdot \left(y + z\right)}{z}
\end{array}
(FPCore (x y z) :precision binary64 (if (<= z -5e-29) (* (/ (+ z y) z) x) (if (<= z 8.6e-72) (* (+ z y) (/ x z)) (* (fma (pow z -1.0) y 1.0) x))))
double code(double x, double y, double z) {
double tmp;
if (z <= -5e-29) {
tmp = ((z + y) / z) * x;
} else if (z <= 8.6e-72) {
tmp = (z + y) * (x / z);
} else {
tmp = fma(pow(z, -1.0), y, 1.0) * x;
}
return tmp;
}
function code(x, y, z) tmp = 0.0 if (z <= -5e-29) tmp = Float64(Float64(Float64(z + y) / z) * x); elseif (z <= 8.6e-72) tmp = Float64(Float64(z + y) * Float64(x / z)); else tmp = Float64(fma((z ^ -1.0), y, 1.0) * x); end return tmp end
code[x_, y_, z_] := If[LessEqual[z, -5e-29], N[(N[(N[(z + y), $MachinePrecision] / z), $MachinePrecision] * x), $MachinePrecision], If[LessEqual[z, 8.6e-72], N[(N[(z + y), $MachinePrecision] * N[(x / z), $MachinePrecision]), $MachinePrecision], N[(N[(N[Power[z, -1.0], $MachinePrecision] * y + 1.0), $MachinePrecision] * x), $MachinePrecision]]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;z \leq -5 \cdot 10^{-29}:\\
\;\;\;\;\frac{z + y}{z} \cdot x\\
\mathbf{elif}\;z \leq 8.6 \cdot 10^{-72}:\\
\;\;\;\;\left(z + y\right) \cdot \frac{x}{z}\\
\mathbf{else}:\\
\;\;\;\;\mathsf{fma}\left({z}^{-1}, y, 1\right) \cdot x\\
\end{array}
\end{array}
if z < -4.99999999999999986e-29Initial program 77.5%
lift-/.f64N/A
lift-+.f64N/A
lift-*.f64N/A
associate-/l*N/A
*-commutativeN/A
lower-*.f64N/A
lower-/.f64N/A
+-commutativeN/A
lower-+.f6499.9
Applied rewrites99.9%
if -4.99999999999999986e-29 < z < 8.5999999999999998e-72Initial program 92.6%
lift-/.f64N/A
lift-+.f64N/A
lift-*.f64N/A
*-commutativeN/A
associate-/l*N/A
lower-*.f64N/A
+-commutativeN/A
lower-+.f64N/A
lower-/.f6490.6
Applied rewrites90.6%
if 8.5999999999999998e-72 < z Initial program 80.2%
lift-/.f64N/A
lift-+.f64N/A
lift-*.f64N/A
associate-/l*N/A
*-commutativeN/A
lower-*.f64N/A
lower-/.f64N/A
+-commutativeN/A
lower-+.f6499.5
Applied rewrites99.5%
Taylor expanded in y around inf
distribute-rgt-inN/A
lower-fma.f64N/A
inv-powN/A
lower-pow.f64N/A
lower-*.f64N/A
inv-powN/A
lower-pow.f6499.3
Applied rewrites99.3%
lift-pow.f64N/A
lift-fma.f64N/A
inv-powN/A
lft-mult-inverseN/A
+-commutativeN/A
lift-*.f64N/A
lower-fma.f6499.5
Applied rewrites99.5%
(FPCore (x y z) :precision binary64 (let* ((t_0 (* (fma (pow z -1.0) y 1.0) x))) (if (<= z -5e-29) t_0 (if (<= z 8.6e-72) (* (+ z y) (/ x z)) t_0))))
double code(double x, double y, double z) {
double t_0 = fma(pow(z, -1.0), y, 1.0) * x;
double tmp;
if (z <= -5e-29) {
tmp = t_0;
} else if (z <= 8.6e-72) {
tmp = (z + y) * (x / z);
} else {
tmp = t_0;
}
return tmp;
}
function code(x, y, z) t_0 = Float64(fma((z ^ -1.0), y, 1.0) * x) tmp = 0.0 if (z <= -5e-29) tmp = t_0; elseif (z <= 8.6e-72) tmp = Float64(Float64(z + y) * Float64(x / z)); else tmp = t_0; end return tmp end
code[x_, y_, z_] := Block[{t$95$0 = N[(N[(N[Power[z, -1.0], $MachinePrecision] * y + 1.0), $MachinePrecision] * x), $MachinePrecision]}, If[LessEqual[z, -5e-29], t$95$0, If[LessEqual[z, 8.6e-72], N[(N[(z + y), $MachinePrecision] * N[(x / z), $MachinePrecision]), $MachinePrecision], t$95$0]]]
\begin{array}{l}
\\
\begin{array}{l}
t_0 := \mathsf{fma}\left({z}^{-1}, y, 1\right) \cdot x\\
\mathbf{if}\;z \leq -5 \cdot 10^{-29}:\\
\;\;\;\;t\_0\\
\mathbf{elif}\;z \leq 8.6 \cdot 10^{-72}:\\
\;\;\;\;\left(z + y\right) \cdot \frac{x}{z}\\
\mathbf{else}:\\
\;\;\;\;t\_0\\
\end{array}
\end{array}
if z < -4.99999999999999986e-29 or 8.5999999999999998e-72 < z Initial program 79.0%
lift-/.f64N/A
lift-+.f64N/A
lift-*.f64N/A
associate-/l*N/A
*-commutativeN/A
lower-*.f64N/A
lower-/.f64N/A
+-commutativeN/A
lower-+.f6499.7
Applied rewrites99.7%
Taylor expanded in y around inf
distribute-rgt-inN/A
lower-fma.f64N/A
inv-powN/A
lower-pow.f64N/A
lower-*.f64N/A
inv-powN/A
lower-pow.f6499.5
Applied rewrites99.5%
lift-pow.f64N/A
lift-fma.f64N/A
inv-powN/A
lft-mult-inverseN/A
+-commutativeN/A
lift-*.f64N/A
lower-fma.f6499.7
Applied rewrites99.7%
if -4.99999999999999986e-29 < z < 8.5999999999999998e-72Initial program 92.6%
lift-/.f64N/A
lift-+.f64N/A
lift-*.f64N/A
*-commutativeN/A
associate-/l*N/A
lower-*.f64N/A
+-commutativeN/A
lower-+.f64N/A
lower-/.f6490.6
Applied rewrites90.6%
(FPCore (x y z) :precision binary64 (* (fma (pow z -1.0) y 1.0) x))
double code(double x, double y, double z) {
return fma(pow(z, -1.0), y, 1.0) * x;
}
function code(x, y, z) return Float64(fma((z ^ -1.0), y, 1.0) * x) end
code[x_, y_, z_] := N[(N[(N[Power[z, -1.0], $MachinePrecision] * y + 1.0), $MachinePrecision] * x), $MachinePrecision]
\begin{array}{l}
\\
\mathsf{fma}\left({z}^{-1}, y, 1\right) \cdot x
\end{array}
Initial program 84.7%
lift-/.f64N/A
lift-+.f64N/A
lift-*.f64N/A
associate-/l*N/A
*-commutativeN/A
lower-*.f64N/A
lower-/.f64N/A
+-commutativeN/A
lower-+.f6496.0
Applied rewrites96.0%
Taylor expanded in y around inf
distribute-rgt-inN/A
lower-fma.f64N/A
inv-powN/A
lower-pow.f64N/A
lower-*.f64N/A
inv-powN/A
lower-pow.f6495.8
Applied rewrites95.8%
lift-pow.f64N/A
lift-fma.f64N/A
inv-powN/A
lft-mult-inverseN/A
+-commutativeN/A
lift-*.f64N/A
lower-fma.f6495.9
Applied rewrites95.9%
(FPCore (x y z) :precision binary64 (* (fma (pow y -1.0) y (* (pow z -1.0) y)) x))
double code(double x, double y, double z) {
return fma(pow(y, -1.0), y, (pow(z, -1.0) * y)) * x;
}
function code(x, y, z) return Float64(fma((y ^ -1.0), y, Float64((z ^ -1.0) * y)) * x) end
code[x_, y_, z_] := N[(N[(N[Power[y, -1.0], $MachinePrecision] * y + N[(N[Power[z, -1.0], $MachinePrecision] * y), $MachinePrecision]), $MachinePrecision] * x), $MachinePrecision]
\begin{array}{l}
\\
\mathsf{fma}\left({y}^{-1}, y, {z}^{-1} \cdot y\right) \cdot x
\end{array}
Initial program 84.7%
lift-/.f64N/A
lift-+.f64N/A
lift-*.f64N/A
associate-/l*N/A
*-commutativeN/A
lower-*.f64N/A
lower-/.f64N/A
+-commutativeN/A
lower-+.f6496.0
Applied rewrites96.0%
Taylor expanded in y around inf
distribute-rgt-inN/A
lower-fma.f64N/A
inv-powN/A
lower-pow.f64N/A
lower-*.f64N/A
inv-powN/A
lower-pow.f6495.8
Applied rewrites95.8%
herbie shell --seed 2025093
(FPCore (x y z)
:name "Numeric.SpecFunctions:choose from math-functions-0.1.5.2"
:precision binary64
(/ (* x (+ y z)) z))