
(FPCore (x y z t) :precision binary64 (+ (* (+ (* x y) z) y) t))
double code(double x, double y, double z, double t) {
return (((x * y) + z) * y) + t;
}
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, t)
use fmin_fmax_functions
real(8), intent (in) :: x
real(8), intent (in) :: y
real(8), intent (in) :: z
real(8), intent (in) :: t
code = (((x * y) + z) * y) + t
end function
public static double code(double x, double y, double z, double t) {
return (((x * y) + z) * y) + t;
}
def code(x, y, z, t): return (((x * y) + z) * y) + t
function code(x, y, z, t) return Float64(Float64(Float64(Float64(x * y) + z) * y) + t) end
function tmp = code(x, y, z, t) tmp = (((x * y) + z) * y) + t; end
code[x_, y_, z_, t_] := N[(N[(N[(N[(x * y), $MachinePrecision] + z), $MachinePrecision] * y), $MachinePrecision] + t), $MachinePrecision]
\begin{array}{l}
\\
\left(x \cdot y + z\right) \cdot y + t
\end{array}
Herbie found 6 alternatives:
| Alternative | Accuracy | Speedup |
|---|
(FPCore (x y z t) :precision binary64 (+ (* (+ (* x y) z) y) t))
double code(double x, double y, double z, double t) {
return (((x * y) + z) * y) + t;
}
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, t)
use fmin_fmax_functions
real(8), intent (in) :: x
real(8), intent (in) :: y
real(8), intent (in) :: z
real(8), intent (in) :: t
code = (((x * y) + z) * y) + t
end function
public static double code(double x, double y, double z, double t) {
return (((x * y) + z) * y) + t;
}
def code(x, y, z, t): return (((x * y) + z) * y) + t
function code(x, y, z, t) return Float64(Float64(Float64(Float64(x * y) + z) * y) + t) end
function tmp = code(x, y, z, t) tmp = (((x * y) + z) * y) + t; end
code[x_, y_, z_, t_] := N[(N[(N[(N[(x * y), $MachinePrecision] + z), $MachinePrecision] * y), $MachinePrecision] + t), $MachinePrecision]
\begin{array}{l}
\\
\left(x \cdot y + z\right) \cdot y + t
\end{array}
(FPCore (x y z t) :precision binary64 (fma (fma y x z) y t))
double code(double x, double y, double z, double t) {
return fma(fma(y, x, z), y, t);
}
function code(x, y, z, t) return fma(fma(y, x, z), y, t) end
code[x_, y_, z_, t_] := N[(N[(y * x + z), $MachinePrecision] * y + t), $MachinePrecision]
\begin{array}{l}
\\
\mathsf{fma}\left(\mathsf{fma}\left(y, x, z\right), y, t\right)
\end{array}
Initial program 99.9%
lift-+.f64N/A
add-flipN/A
sub-flipN/A
lift-*.f64N/A
remove-double-negN/A
lower-fma.f6499.9
remove-double-negN/A
lower-+.f64N/A
remove-double-negN/A
remove-double-negN/A
lift-*.f64N/A
*-commutativeN/A
remove-double-negN/A
lower-fma.f6499.9
Applied rewrites99.9%
(FPCore (x y z t) :precision binary64 (if (<= z -4.6e+77) (fma z y t) (if (<= z 2.3e+25) (fma (* y x) y t) (fma z y t))))
double code(double x, double y, double z, double t) {
double tmp;
if (z <= -4.6e+77) {
tmp = fma(z, y, t);
} else if (z <= 2.3e+25) {
tmp = fma((y * x), y, t);
} else {
tmp = fma(z, y, t);
}
return tmp;
}
function code(x, y, z, t) tmp = 0.0 if (z <= -4.6e+77) tmp = fma(z, y, t); elseif (z <= 2.3e+25) tmp = fma(Float64(y * x), y, t); else tmp = fma(z, y, t); end return tmp end
code[x_, y_, z_, t_] := If[LessEqual[z, -4.6e+77], N[(z * y + t), $MachinePrecision], If[LessEqual[z, 2.3e+25], N[(N[(y * x), $MachinePrecision] * y + t), $MachinePrecision], N[(z * y + t), $MachinePrecision]]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;z \leq -4.6 \cdot 10^{+77}:\\
\;\;\;\;\mathsf{fma}\left(z, y, t\right)\\
\mathbf{elif}\;z \leq 2.3 \cdot 10^{+25}:\\
\;\;\;\;\mathsf{fma}\left(y \cdot x, y, t\right)\\
\mathbf{else}:\\
\;\;\;\;\mathsf{fma}\left(z, y, t\right)\\
\end{array}
\end{array}
if z < -4.5999999999999999e77 or 2.2999999999999998e25 < z Initial program 99.9%
Taylor expanded in x around 0
Applied rewrites66.3%
lift-+.f64N/A
add-flipN/A
sub-flipN/A
lift-*.f64N/A
remove-double-negN/A
lower-fma.f6466.4
Applied rewrites66.4%
if -4.5999999999999999e77 < z < 2.2999999999999998e25Initial program 99.9%
Taylor expanded in x around 0
Applied rewrites66.3%
Taylor expanded in x around inf
lower-*.f6475.9
Applied rewrites75.9%
lift-+.f64N/A
add-flipN/A
sub-flipN/A
lift-*.f64N/A
remove-double-negN/A
lower-fma.f6475.9
lift-*.f64N/A
*-commutativeN/A
lower-*.f6475.9
Applied rewrites75.9%
(FPCore (x y z t)
:precision binary64
(let* ((t_1 (* (+ (* x y) z) y)))
(if (<= t_1 -1e+305)
(* (* x y) y)
(if (<= t_1 1e+234) (fma z y t) (* (* y y) x)))))
double code(double x, double y, double z, double t) {
double t_1 = ((x * y) + z) * y;
double tmp;
if (t_1 <= -1e+305) {
tmp = (x * y) * y;
} else if (t_1 <= 1e+234) {
tmp = fma(z, y, t);
} else {
tmp = (y * y) * x;
}
return tmp;
}
function code(x, y, z, t) t_1 = Float64(Float64(Float64(x * y) + z) * y) tmp = 0.0 if (t_1 <= -1e+305) tmp = Float64(Float64(x * y) * y); elseif (t_1 <= 1e+234) tmp = fma(z, y, t); else tmp = Float64(Float64(y * y) * x); end return tmp end
code[x_, y_, z_, t_] := Block[{t$95$1 = N[(N[(N[(x * y), $MachinePrecision] + z), $MachinePrecision] * y), $MachinePrecision]}, If[LessEqual[t$95$1, -1e+305], N[(N[(x * y), $MachinePrecision] * y), $MachinePrecision], If[LessEqual[t$95$1, 1e+234], N[(z * y + t), $MachinePrecision], N[(N[(y * y), $MachinePrecision] * x), $MachinePrecision]]]]
\begin{array}{l}
\\
\begin{array}{l}
t_1 := \left(x \cdot y + z\right) \cdot y\\
\mathbf{if}\;t\_1 \leq -1 \cdot 10^{+305}:\\
\;\;\;\;\left(x \cdot y\right) \cdot y\\
\mathbf{elif}\;t\_1 \leq 10^{+234}:\\
\;\;\;\;\mathsf{fma}\left(z, y, t\right)\\
\mathbf{else}:\\
\;\;\;\;\left(y \cdot y\right) \cdot x\\
\end{array}
\end{array}
if (*.f64 (+.f64 (*.f64 x y) z) y) < -9.9999999999999994e304Initial program 99.9%
lift-+.f64N/A
add-flipN/A
sub-negate-revN/A
lift-*.f64N/A
*-commutativeN/A
lift-+.f64N/A
+-commutativeN/A
distribute-rgt-inN/A
associate--r+N/A
*-commutativeN/A
sub-flipN/A
+-commutativeN/A
distribute-neg-inN/A
remove-double-negN/A
lift-*.f64N/A
associate-*l*N/A
*-commutativeN/A
sub-negate-revN/A
lower-fma.f64N/A
lower-*.f64N/A
add-flip-revN/A
*-commutativeN/A
Applied rewrites90.8%
lift-*.f64N/A
sqr-neg-revN/A
sqr-neg-revN/A
pow2N/A
remove-double-negN/A
metadata-evalN/A
metadata-evalN/A
metadata-evalN/A
metadata-evalN/A
metadata-evalN/A
metadata-evalN/A
metadata-evalN/A
metadata-evalN/A
metadata-evalN/A
metadata-evalN/A
pow-subN/A
lower-special-/.f64N/A
lower-special-pow.f64N/A
lower-special-pow.f64N/A
metadata-eval45.7
Applied rewrites45.7%
Taylor expanded in x around inf
lower-/.f64N/A
lower-*.f64N/A
lower-sqrt.f64N/A
lower-pow.f6419.4
Applied rewrites19.4%
lift-/.f64N/A
lift-*.f64N/A
associate-/l*N/A
lift-sqrt.f64N/A
pow1/2N/A
lift-pow.f64N/A
pow-divN/A
metadata-evalN/A
unpow2N/A
associate-*r*N/A
lower-*.f64N/A
lower-*.f6439.8
Applied rewrites39.8%
if -9.9999999999999994e304 < (*.f64 (+.f64 (*.f64 x y) z) y) < 1.00000000000000002e234Initial program 99.9%
Taylor expanded in x around 0
Applied rewrites66.3%
lift-+.f64N/A
add-flipN/A
sub-flipN/A
lift-*.f64N/A
remove-double-negN/A
lower-fma.f6466.4
Applied rewrites66.4%
if 1.00000000000000002e234 < (*.f64 (+.f64 (*.f64 x y) z) y) Initial program 99.9%
lift-+.f64N/A
add-flipN/A
sub-negate-revN/A
lift-*.f64N/A
*-commutativeN/A
lift-+.f64N/A
+-commutativeN/A
distribute-rgt-inN/A
associate--r+N/A
*-commutativeN/A
sub-flipN/A
+-commutativeN/A
distribute-neg-inN/A
remove-double-negN/A
lift-*.f64N/A
associate-*l*N/A
*-commutativeN/A
sub-negate-revN/A
lower-fma.f64N/A
lower-*.f64N/A
add-flip-revN/A
*-commutativeN/A
Applied rewrites90.8%
lift-*.f64N/A
sqr-neg-revN/A
sqr-neg-revN/A
pow2N/A
remove-double-negN/A
metadata-evalN/A
metadata-evalN/A
metadata-evalN/A
metadata-evalN/A
metadata-evalN/A
metadata-evalN/A
metadata-evalN/A
metadata-evalN/A
metadata-evalN/A
metadata-evalN/A
pow-subN/A
lower-special-/.f64N/A
lower-special-pow.f64N/A
lower-special-pow.f64N/A
metadata-eval45.7
Applied rewrites45.7%
Taylor expanded in x around inf
lower-/.f64N/A
lower-*.f64N/A
lower-sqrt.f64N/A
lower-pow.f6419.4
Applied rewrites19.4%
lift-/.f64N/A
lift-*.f64N/A
*-commutativeN/A
associate-*l/N/A
lower-*.f64N/A
lift-sqrt.f64N/A
pow1/2N/A
lift-pow.f64N/A
pow-divN/A
metadata-evalN/A
unpow2N/A
lower-*.f6438.0
Applied rewrites38.0%
(FPCore (x y z t) :precision binary64 (let* ((t_1 (* (+ (* x y) z) y)) (t_2 (* (* x y) y))) (if (<= t_1 -1e+305) t_2 (if (<= t_1 1e+234) (fma z y t) t_2))))
double code(double x, double y, double z, double t) {
double t_1 = ((x * y) + z) * y;
double t_2 = (x * y) * y;
double tmp;
if (t_1 <= -1e+305) {
tmp = t_2;
} else if (t_1 <= 1e+234) {
tmp = fma(z, y, t);
} else {
tmp = t_2;
}
return tmp;
}
function code(x, y, z, t) t_1 = Float64(Float64(Float64(x * y) + z) * y) t_2 = Float64(Float64(x * y) * y) tmp = 0.0 if (t_1 <= -1e+305) tmp = t_2; elseif (t_1 <= 1e+234) tmp = fma(z, y, t); else tmp = t_2; end return tmp end
code[x_, y_, z_, t_] := Block[{t$95$1 = N[(N[(N[(x * y), $MachinePrecision] + z), $MachinePrecision] * y), $MachinePrecision]}, Block[{t$95$2 = N[(N[(x * y), $MachinePrecision] * y), $MachinePrecision]}, If[LessEqual[t$95$1, -1e+305], t$95$2, If[LessEqual[t$95$1, 1e+234], N[(z * y + t), $MachinePrecision], t$95$2]]]]
\begin{array}{l}
\\
\begin{array}{l}
t_1 := \left(x \cdot y + z\right) \cdot y\\
t_2 := \left(x \cdot y\right) \cdot y\\
\mathbf{if}\;t\_1 \leq -1 \cdot 10^{+305}:\\
\;\;\;\;t\_2\\
\mathbf{elif}\;t\_1 \leq 10^{+234}:\\
\;\;\;\;\mathsf{fma}\left(z, y, t\right)\\
\mathbf{else}:\\
\;\;\;\;t\_2\\
\end{array}
\end{array}
if (*.f64 (+.f64 (*.f64 x y) z) y) < -9.9999999999999994e304 or 1.00000000000000002e234 < (*.f64 (+.f64 (*.f64 x y) z) y) Initial program 99.9%
lift-+.f64N/A
add-flipN/A
sub-negate-revN/A
lift-*.f64N/A
*-commutativeN/A
lift-+.f64N/A
+-commutativeN/A
distribute-rgt-inN/A
associate--r+N/A
*-commutativeN/A
sub-flipN/A
+-commutativeN/A
distribute-neg-inN/A
remove-double-negN/A
lift-*.f64N/A
associate-*l*N/A
*-commutativeN/A
sub-negate-revN/A
lower-fma.f64N/A
lower-*.f64N/A
add-flip-revN/A
*-commutativeN/A
Applied rewrites90.8%
lift-*.f64N/A
sqr-neg-revN/A
sqr-neg-revN/A
pow2N/A
remove-double-negN/A
metadata-evalN/A
metadata-evalN/A
metadata-evalN/A
metadata-evalN/A
metadata-evalN/A
metadata-evalN/A
metadata-evalN/A
metadata-evalN/A
metadata-evalN/A
metadata-evalN/A
pow-subN/A
lower-special-/.f64N/A
lower-special-pow.f64N/A
lower-special-pow.f64N/A
metadata-eval45.7
Applied rewrites45.7%
Taylor expanded in x around inf
lower-/.f64N/A
lower-*.f64N/A
lower-sqrt.f64N/A
lower-pow.f6419.4
Applied rewrites19.4%
lift-/.f64N/A
lift-*.f64N/A
associate-/l*N/A
lift-sqrt.f64N/A
pow1/2N/A
lift-pow.f64N/A
pow-divN/A
metadata-evalN/A
unpow2N/A
associate-*r*N/A
lower-*.f64N/A
lower-*.f6439.8
Applied rewrites39.8%
if -9.9999999999999994e304 < (*.f64 (+.f64 (*.f64 x y) z) y) < 1.00000000000000002e234Initial program 99.9%
Taylor expanded in x around 0
Applied rewrites66.3%
lift-+.f64N/A
add-flipN/A
sub-flipN/A
lift-*.f64N/A
remove-double-negN/A
lower-fma.f6466.4
Applied rewrites66.4%
(FPCore (x y z t) :precision binary64 (fma z y t))
double code(double x, double y, double z, double t) {
return fma(z, y, t);
}
function code(x, y, z, t) return fma(z, y, t) end
code[x_, y_, z_, t_] := N[(z * y + t), $MachinePrecision]
\begin{array}{l}
\\
\mathsf{fma}\left(z, y, t\right)
\end{array}
Initial program 99.9%
Taylor expanded in x around 0
Applied rewrites66.3%
lift-+.f64N/A
add-flipN/A
sub-flipN/A
lift-*.f64N/A
remove-double-negN/A
lower-fma.f6466.4
Applied rewrites66.4%
(FPCore (x y z t) :precision binary64 t)
double code(double x, double y, double z, double t) {
return t;
}
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, t)
use fmin_fmax_functions
real(8), intent (in) :: x
real(8), intent (in) :: y
real(8), intent (in) :: z
real(8), intent (in) :: t
code = t
end function
public static double code(double x, double y, double z, double t) {
return t;
}
def code(x, y, z, t): return t
function code(x, y, z, t) return t end
function tmp = code(x, y, z, t) tmp = t; end
code[x_, y_, z_, t_] := t
\begin{array}{l}
\\
t
\end{array}
Initial program 99.9%
Taylor expanded in y around 0
Applied rewrites38.5%
herbie shell --seed 2025154
(FPCore (x y z t)
:name "Language.Haskell.HsColour.ColourHighlight:unbase from hscolour-1.23"
:precision binary64
(+ (* (+ (* x y) z) y) t))