
(FPCore (x y z t) :precision binary64 (+ (* x y) (* z t)))
double code(double x, double y, double z, double t) {
return (x * y) + (z * 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 * t)
end function
public static double code(double x, double y, double z, double t) {
return (x * y) + (z * t);
}
def code(x, y, z, t): return (x * y) + (z * t)
function code(x, y, z, t) return Float64(Float64(x * y) + Float64(z * t)) end
function tmp = code(x, y, z, t) tmp = (x * y) + (z * t); end
code[x_, y_, z_, t_] := N[(N[(x * y), $MachinePrecision] + N[(z * t), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
x \cdot y + z \cdot t
\end{array}
Sampling outcomes in binary64 precision:
Herbie found 3 alternatives:
| Alternative | Accuracy | Speedup |
|---|
(FPCore (x y z t) :precision binary64 (+ (* x y) (* z t)))
double code(double x, double y, double z, double t) {
return (x * y) + (z * 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 * t)
end function
public static double code(double x, double y, double z, double t) {
return (x * y) + (z * t);
}
def code(x, y, z, t): return (x * y) + (z * t)
function code(x, y, z, t) return Float64(Float64(x * y) + Float64(z * t)) end
function tmp = code(x, y, z, t) tmp = (x * y) + (z * t); end
code[x_, y_, z_, t_] := N[(N[(x * y), $MachinePrecision] + N[(z * t), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
x \cdot y + z \cdot t
\end{array}
(FPCore (x y z t) :precision binary64 (fma z t (* y x)))
double code(double x, double y, double z, double t) {
return fma(z, t, (y * x));
}
function code(x, y, z, t) return fma(z, t, Float64(y * x)) end
code[x_, y_, z_, t_] := N[(z * t + N[(y * x), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\mathsf{fma}\left(z, t, y \cdot x\right)
\end{array}
Initial program 99.2%
lift-+.f64N/A
lift-*.f64N/A
+-commutativeN/A
lift-*.f64N/A
lower-fma.f64N/A
*-commutativeN/A
lower-*.f6499.6
Applied rewrites99.6%
(FPCore (x y z t) :precision binary64 (if (or (<= (* z t) -2e-55) (not (<= (* z t) 1000000000.0))) (* t z) (* y x)))
double code(double x, double y, double z, double t) {
double tmp;
if (((z * t) <= -2e-55) || !((z * t) <= 1000000000.0)) {
tmp = t * z;
} else {
tmp = y * x;
}
return tmp;
}
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
real(8) :: tmp
if (((z * t) <= (-2d-55)) .or. (.not. ((z * t) <= 1000000000.0d0))) then
tmp = t * z
else
tmp = y * x
end if
code = tmp
end function
public static double code(double x, double y, double z, double t) {
double tmp;
if (((z * t) <= -2e-55) || !((z * t) <= 1000000000.0)) {
tmp = t * z;
} else {
tmp = y * x;
}
return tmp;
}
def code(x, y, z, t): tmp = 0 if ((z * t) <= -2e-55) or not ((z * t) <= 1000000000.0): tmp = t * z else: tmp = y * x return tmp
function code(x, y, z, t) tmp = 0.0 if ((Float64(z * t) <= -2e-55) || !(Float64(z * t) <= 1000000000.0)) tmp = Float64(t * z); else tmp = Float64(y * x); end return tmp end
function tmp_2 = code(x, y, z, t) tmp = 0.0; if (((z * t) <= -2e-55) || ~(((z * t) <= 1000000000.0))) tmp = t * z; else tmp = y * x; end tmp_2 = tmp; end
code[x_, y_, z_, t_] := If[Or[LessEqual[N[(z * t), $MachinePrecision], -2e-55], N[Not[LessEqual[N[(z * t), $MachinePrecision], 1000000000.0]], $MachinePrecision]], N[(t * z), $MachinePrecision], N[(y * x), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;z \cdot t \leq -2 \cdot 10^{-55} \lor \neg \left(z \cdot t \leq 1000000000\right):\\
\;\;\;\;t \cdot z\\
\mathbf{else}:\\
\;\;\;\;y \cdot x\\
\end{array}
\end{array}
if (*.f64 z t) < -1.99999999999999999e-55 or 1e9 < (*.f64 z t) Initial program 98.5%
Taylor expanded in x around 0
lower-*.f6479.9
Applied rewrites79.9%
if -1.99999999999999999e-55 < (*.f64 z t) < 1e9Initial program 100.0%
Taylor expanded in x around inf
*-commutativeN/A
lower-*.f6483.8
Applied rewrites83.8%
Final simplification81.8%
(FPCore (x y z t) :precision binary64 (* t z))
double code(double x, double y, double z, double t) {
return t * 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, 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 * z
end function
public static double code(double x, double y, double z, double t) {
return t * z;
}
def code(x, y, z, t): return t * z
function code(x, y, z, t) return Float64(t * z) end
function tmp = code(x, y, z, t) tmp = t * z; end
code[x_, y_, z_, t_] := N[(t * z), $MachinePrecision]
\begin{array}{l}
\\
t \cdot z
\end{array}
Initial program 99.2%
Taylor expanded in x around 0
lower-*.f6452.8
Applied rewrites52.8%
herbie shell --seed 2025043
(FPCore (x y z t)
:name "Linear.V2:$cdot from linear-1.19.1.3, A"
:precision binary64
(+ (* x y) (* z t)))