
(FPCore (x y z) :precision binary64 (+ x (* y (+ z x))))
double code(double x, double y, double z) {
return x + (y * (z + x));
}
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 + x))
end function
public static double code(double x, double y, double z) {
return x + (y * (z + x));
}
def code(x, y, z): return x + (y * (z + x))
function code(x, y, z) return Float64(x + Float64(y * Float64(z + x))) end
function tmp = code(x, y, z) tmp = x + (y * (z + x)); end
code[x_, y_, z_] := N[(x + N[(y * N[(z + x), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
x + y \cdot \left(z + x\right)
\end{array}
Herbie found 5 alternatives:
| Alternative | Accuracy | Speedup |
|---|
(FPCore (x y z) :precision binary64 (+ x (* y (+ z x))))
double code(double x, double y, double z) {
return x + (y * (z + x));
}
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 + x))
end function
public static double code(double x, double y, double z) {
return x + (y * (z + x));
}
def code(x, y, z): return x + (y * (z + x))
function code(x, y, z) return Float64(x + Float64(y * Float64(z + x))) end
function tmp = code(x, y, z) tmp = x + (y * (z + x)); end
code[x_, y_, z_] := N[(x + N[(y * N[(z + x), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
x + y \cdot \left(z + x\right)
\end{array}
(FPCore (x y z) :precision binary64 (fma (+ z x) y x))
double code(double x, double y, double z) {
return fma((z + x), y, x);
}
function code(x, y, z) return fma(Float64(z + x), y, x) end
code[x_, y_, z_] := N[(N[(z + x), $MachinePrecision] * y + x), $MachinePrecision]
\begin{array}{l}
\\
\mathsf{fma}\left(z + x, y, x\right)
\end{array}
Initial program 100.0%
Applied rewrites100.0%
(FPCore (x y z) :precision binary64 (let* ((t_0 (* (+ x z) y))) (if (<= y -4.4) t_0 (if (<= y 0.00125) (fma z y x) t_0))))
double code(double x, double y, double z) {
double t_0 = (x + z) * y;
double tmp;
if (y <= -4.4) {
tmp = t_0;
} else if (y <= 0.00125) {
tmp = fma(z, y, x);
} else {
tmp = t_0;
}
return tmp;
}
function code(x, y, z) t_0 = Float64(Float64(x + z) * y) tmp = 0.0 if (y <= -4.4) tmp = t_0; elseif (y <= 0.00125) tmp = fma(z, y, x); else tmp = t_0; end return tmp end
code[x_, y_, z_] := Block[{t$95$0 = N[(N[(x + z), $MachinePrecision] * y), $MachinePrecision]}, If[LessEqual[y, -4.4], t$95$0, If[LessEqual[y, 0.00125], N[(z * y + x), $MachinePrecision], t$95$0]]]
\begin{array}{l}
\\
\begin{array}{l}
t_0 := \left(x + z\right) \cdot y\\
\mathbf{if}\;y \leq -4.4:\\
\;\;\;\;t\_0\\
\mathbf{elif}\;y \leq 0.00125:\\
\;\;\;\;\mathsf{fma}\left(z, y, x\right)\\
\mathbf{else}:\\
\;\;\;\;t\_0\\
\end{array}
\end{array}
if y < -4.4000000000000004 or 0.00125000000000000003 < y Initial program 100.0%
lift-*.f64N/A
lift-+.f64N/A
lower-+.f64N/A
+-commutativeN/A
+-commutativeN/A
distribute-lft-inN/A
*-commutativeN/A
+-commutativeN/A
associate-+l+N/A
*-rgt-identityN/A
distribute-lft-inN/A
+-commutativeN/A
*-commutativeN/A
lower-fma.f64N/A
*-commutativeN/A
+-commutativeN/A
distribute-lft1-inN/A
lower-fma.f6498.9
Applied rewrites98.9%
Taylor expanded in y around inf
associate-+r+N/A
*-commutativeN/A
*-commutativeN/A
+-commutativeN/A
*-commutativeN/A
distribute-rgt-inN/A
+-commutativeN/A
*-commutativeN/A
*-commutativeN/A
lower-*.f64N/A
lower-+.f6465.0
Applied rewrites65.0%
if -4.4000000000000004 < y < 0.00125000000000000003Initial program 100.0%
Taylor expanded in x around 0
Applied rewrites75.8%
lift-+.f64N/A
+-commutativeN/A
lift-*.f64N/A
*-commutativeN/A
lower-fma.f6475.8
Applied rewrites75.8%
(FPCore (x y z) :precision binary64 (if (<= z -1.25e-62) (fma z y x) (if (<= z 5.5e-24) (fma y x x) (fma z y x))))
double code(double x, double y, double z) {
double tmp;
if (z <= -1.25e-62) {
tmp = fma(z, y, x);
} else if (z <= 5.5e-24) {
tmp = fma(y, x, x);
} else {
tmp = fma(z, y, x);
}
return tmp;
}
function code(x, y, z) tmp = 0.0 if (z <= -1.25e-62) tmp = fma(z, y, x); elseif (z <= 5.5e-24) tmp = fma(y, x, x); else tmp = fma(z, y, x); end return tmp end
code[x_, y_, z_] := If[LessEqual[z, -1.25e-62], N[(z * y + x), $MachinePrecision], If[LessEqual[z, 5.5e-24], N[(y * x + x), $MachinePrecision], N[(z * y + x), $MachinePrecision]]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;z \leq -1.25 \cdot 10^{-62}:\\
\;\;\;\;\mathsf{fma}\left(z, y, x\right)\\
\mathbf{elif}\;z \leq 5.5 \cdot 10^{-24}:\\
\;\;\;\;\mathsf{fma}\left(y, x, x\right)\\
\mathbf{else}:\\
\;\;\;\;\mathsf{fma}\left(z, y, x\right)\\
\end{array}
\end{array}
if z < -1.25e-62 or 5.4999999999999999e-24 < z Initial program 100.0%
Taylor expanded in x around 0
Applied rewrites75.8%
lift-+.f64N/A
+-commutativeN/A
lift-*.f64N/A
*-commutativeN/A
lower-fma.f6475.8
Applied rewrites75.8%
if -1.25e-62 < z < 5.4999999999999999e-24Initial program 100.0%
Taylor expanded in x around inf
*-commutativeN/A
+-commutativeN/A
distribute-lft1-inN/A
lower-fma.f6462.2
Applied rewrites62.2%
(FPCore (x y z) :precision binary64 (if (<= x -1.4e-87) (fma y x x) (if (<= x 2.5e-74) (* z y) (fma y x x))))
double code(double x, double y, double z) {
double tmp;
if (x <= -1.4e-87) {
tmp = fma(y, x, x);
} else if (x <= 2.5e-74) {
tmp = z * y;
} else {
tmp = fma(y, x, x);
}
return tmp;
}
function code(x, y, z) tmp = 0.0 if (x <= -1.4e-87) tmp = fma(y, x, x); elseif (x <= 2.5e-74) tmp = Float64(z * y); else tmp = fma(y, x, x); end return tmp end
code[x_, y_, z_] := If[LessEqual[x, -1.4e-87], N[(y * x + x), $MachinePrecision], If[LessEqual[x, 2.5e-74], N[(z * y), $MachinePrecision], N[(y * x + x), $MachinePrecision]]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq -1.4 \cdot 10^{-87}:\\
\;\;\;\;\mathsf{fma}\left(y, x, x\right)\\
\mathbf{elif}\;x \leq 2.5 \cdot 10^{-74}:\\
\;\;\;\;z \cdot y\\
\mathbf{else}:\\
\;\;\;\;\mathsf{fma}\left(y, x, x\right)\\
\end{array}
\end{array}
if x < -1.4e-87 or 2.49999999999999999e-74 < x Initial program 100.0%
Taylor expanded in x around inf
*-commutativeN/A
+-commutativeN/A
distribute-lft1-inN/A
lower-fma.f6462.2
Applied rewrites62.2%
if -1.4e-87 < x < 2.49999999999999999e-74Initial program 100.0%
Taylor expanded in x around 0
*-commutativeN/A
lower-*.f6441.5
Applied rewrites41.5%
(FPCore (x y z) :precision binary64 (* z y))
double code(double x, double y, double z) {
return z * y;
}
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 = z * y
end function
public static double code(double x, double y, double z) {
return z * y;
}
def code(x, y, z): return z * y
function code(x, y, z) return Float64(z * y) end
function tmp = code(x, y, z) tmp = z * y; end
code[x_, y_, z_] := N[(z * y), $MachinePrecision]
\begin{array}{l}
\\
z \cdot y
\end{array}
Initial program 100.0%
Taylor expanded in x around 0
*-commutativeN/A
lower-*.f6441.5
Applied rewrites41.5%
herbie shell --seed 2025132
(FPCore (x y z)
:name "Main:bigenough2 from A"
:precision binary64
(+ x (* y (+ z x))))