
(FPCore (x y z) :precision binary64 (+ (* x y) (* (- 1.0 x) z)))
double code(double x, double y, double z) {
return (x * y) + ((1.0 - x) * 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) + ((1.0d0 - x) * z)
end function
public static double code(double x, double y, double z) {
return (x * y) + ((1.0 - x) * z);
}
def code(x, y, z): return (x * y) + ((1.0 - x) * z)
function code(x, y, z) return Float64(Float64(x * y) + Float64(Float64(1.0 - x) * z)) end
function tmp = code(x, y, z) tmp = (x * y) + ((1.0 - x) * z); end
code[x_, y_, z_] := N[(N[(x * y), $MachinePrecision] + N[(N[(1.0 - x), $MachinePrecision] * z), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
x \cdot y + \left(1 - x\right) \cdot z
\end{array}
Sampling outcomes in binary64 precision:
Herbie found 7 alternatives:
| Alternative | Accuracy | Speedup |
|---|
(FPCore (x y z) :precision binary64 (+ (* x y) (* (- 1.0 x) z)))
double code(double x, double y, double z) {
return (x * y) + ((1.0 - x) * 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) + ((1.0d0 - x) * z)
end function
public static double code(double x, double y, double z) {
return (x * y) + ((1.0 - x) * z);
}
def code(x, y, z): return (x * y) + ((1.0 - x) * z)
function code(x, y, z) return Float64(Float64(x * y) + Float64(Float64(1.0 - x) * z)) end
function tmp = code(x, y, z) tmp = (x * y) + ((1.0 - x) * z); end
code[x_, y_, z_] := N[(N[(x * y), $MachinePrecision] + N[(N[(1.0 - x), $MachinePrecision] * z), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
x \cdot y + \left(1 - x\right) \cdot z
\end{array}
(FPCore (x y z) :precision binary64 (fma (- y z) x z))
double code(double x, double y, double z) {
return fma((y - z), x, z);
}
function code(x, y, z) return fma(Float64(y - z), x, z) end
code[x_, y_, z_] := N[(N[(y - z), $MachinePrecision] * x + z), $MachinePrecision]
\begin{array}{l}
\\
\mathsf{fma}\left(y - z, x, z\right)
\end{array}
Initial program 98.4%
Taylor expanded in x around 0
Applied rewrites72.4%
lift-+.f64N/A
lift-*.f64N/A
*-commutativeN/A
lower-fma.f6472.4
Applied rewrites72.4%
Taylor expanded in x around 0
+-commutativeN/A
*-commutativeN/A
+-commutativeN/A
+-commutativeN/A
*-commutativeN/A
lower-fma.f64N/A
fp-cancel-sign-sub-invN/A
metadata-evalN/A
*-lft-identityN/A
lower--.f64100.0
Applied rewrites100.0%
(FPCore (x y z) :precision binary64 (if (or (<= x -1.0) (not (<= x 1.0))) (* (- y z) x) (fma y x z)))
double code(double x, double y, double z) {
double tmp;
if ((x <= -1.0) || !(x <= 1.0)) {
tmp = (y - z) * x;
} else {
tmp = fma(y, x, z);
}
return tmp;
}
function code(x, y, z) tmp = 0.0 if ((x <= -1.0) || !(x <= 1.0)) tmp = Float64(Float64(y - z) * x); else tmp = fma(y, x, z); end return tmp end
code[x_, y_, z_] := If[Or[LessEqual[x, -1.0], N[Not[LessEqual[x, 1.0]], $MachinePrecision]], N[(N[(y - z), $MachinePrecision] * x), $MachinePrecision], N[(y * x + z), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq -1 \lor \neg \left(x \leq 1\right):\\
\;\;\;\;\left(y - z\right) \cdot x\\
\mathbf{else}:\\
\;\;\;\;\mathsf{fma}\left(y, x, z\right)\\
\end{array}
\end{array}
if x < -1 or 1 < x Initial program 96.9%
Taylor expanded in x around -inf
Applied rewrites98.9%
lift-neg.f64N/A
mul-1-negN/A
lift--.f64N/A
lower-*.f64N/A
distribute-lft-out--N/A
associate-*r*N/A
mul-1-negN/A
cancel-sign-subN/A
+-commutativeN/A
*-commutativeN/A
associate-*r*N/A
*-commutativeN/A
distribute-lft-inN/A
*-commutativeN/A
lower-*.f64N/A
fp-cancel-sign-sub-invN/A
metadata-evalN/A
*-lft-identityN/A
lower--.f6498.9
Applied rewrites98.9%
if -1 < x < 1Initial program 99.9%
Taylor expanded in x around 0
Applied rewrites96.6%
lift-+.f64N/A
lift-*.f64N/A
*-commutativeN/A
lower-fma.f6496.6
Applied rewrites96.6%
Final simplification97.8%
(FPCore (x y z) :precision binary64 (if (or (<= z -650000.0) (not (<= z 5.2e-60))) (* (- 1.0 x) z) (fma y x z)))
double code(double x, double y, double z) {
double tmp;
if ((z <= -650000.0) || !(z <= 5.2e-60)) {
tmp = (1.0 - x) * z;
} else {
tmp = fma(y, x, z);
}
return tmp;
}
function code(x, y, z) tmp = 0.0 if ((z <= -650000.0) || !(z <= 5.2e-60)) tmp = Float64(Float64(1.0 - x) * z); else tmp = fma(y, x, z); end return tmp end
code[x_, y_, z_] := If[Or[LessEqual[z, -650000.0], N[Not[LessEqual[z, 5.2e-60]], $MachinePrecision]], N[(N[(1.0 - x), $MachinePrecision] * z), $MachinePrecision], N[(y * x + z), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;z \leq -650000 \lor \neg \left(z \leq 5.2 \cdot 10^{-60}\right):\\
\;\;\;\;\left(1 - x\right) \cdot z\\
\mathbf{else}:\\
\;\;\;\;\mathsf{fma}\left(y, x, z\right)\\
\end{array}
\end{array}
if z < -6.5e5 or 5.1999999999999995e-60 < z Initial program 97.3%
Taylor expanded in y around 0
*-commutativeN/A
lift-*.f64N/A
lift--.f6486.7
Applied rewrites86.7%
if -6.5e5 < z < 5.1999999999999995e-60Initial program 100.0%
Taylor expanded in x around 0
Applied rewrites89.7%
lift-+.f64N/A
lift-*.f64N/A
*-commutativeN/A
lower-fma.f6489.7
Applied rewrites89.7%
Final simplification87.9%
(FPCore (x y z) :precision binary64 (if (or (<= x -2.3e+192) (not (<= x 3.9e+26))) (* (- x) z) (fma y x z)))
double code(double x, double y, double z) {
double tmp;
if ((x <= -2.3e+192) || !(x <= 3.9e+26)) {
tmp = -x * z;
} else {
tmp = fma(y, x, z);
}
return tmp;
}
function code(x, y, z) tmp = 0.0 if ((x <= -2.3e+192) || !(x <= 3.9e+26)) tmp = Float64(Float64(-x) * z); else tmp = fma(y, x, z); end return tmp end
code[x_, y_, z_] := If[Or[LessEqual[x, -2.3e+192], N[Not[LessEqual[x, 3.9e+26]], $MachinePrecision]], N[((-x) * z), $MachinePrecision], N[(y * x + z), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq -2.3 \cdot 10^{+192} \lor \neg \left(x \leq 3.9 \cdot 10^{+26}\right):\\
\;\;\;\;\left(-x\right) \cdot z\\
\mathbf{else}:\\
\;\;\;\;\mathsf{fma}\left(y, x, z\right)\\
\end{array}
\end{array}
if x < -2.2999999999999999e192 or 3.9e26 < x Initial program 95.5%
Taylor expanded in y around 0
*-commutativeN/A
lift-*.f64N/A
lift--.f6467.9
Applied rewrites67.9%
Taylor expanded in x around inf
mul-1-negN/A
lower-neg.f6467.9
Applied rewrites67.9%
if -2.2999999999999999e192 < x < 3.9e26Initial program 99.9%
Taylor expanded in x around 0
Applied rewrites89.2%
lift-+.f64N/A
lift-*.f64N/A
*-commutativeN/A
lower-fma.f6489.3
Applied rewrites89.3%
Final simplification81.9%
(FPCore (x y z) :precision binary64 (if (or (<= x -9600000000000.0) (not (<= x 1.16e-89))) (* y x) z))
double code(double x, double y, double z) {
double tmp;
if ((x <= -9600000000000.0) || !(x <= 1.16e-89)) {
tmp = y * x;
} else {
tmp = z;
}
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)
use fmin_fmax_functions
real(8), intent (in) :: x
real(8), intent (in) :: y
real(8), intent (in) :: z
real(8) :: tmp
if ((x <= (-9600000000000.0d0)) .or. (.not. (x <= 1.16d-89))) then
tmp = y * x
else
tmp = z
end if
code = tmp
end function
public static double code(double x, double y, double z) {
double tmp;
if ((x <= -9600000000000.0) || !(x <= 1.16e-89)) {
tmp = y * x;
} else {
tmp = z;
}
return tmp;
}
def code(x, y, z): tmp = 0 if (x <= -9600000000000.0) or not (x <= 1.16e-89): tmp = y * x else: tmp = z return tmp
function code(x, y, z) tmp = 0.0 if ((x <= -9600000000000.0) || !(x <= 1.16e-89)) tmp = Float64(y * x); else tmp = z; end return tmp end
function tmp_2 = code(x, y, z) tmp = 0.0; if ((x <= -9600000000000.0) || ~((x <= 1.16e-89))) tmp = y * x; else tmp = z; end tmp_2 = tmp; end
code[x_, y_, z_] := If[Or[LessEqual[x, -9600000000000.0], N[Not[LessEqual[x, 1.16e-89]], $MachinePrecision]], N[(y * x), $MachinePrecision], z]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq -9600000000000 \lor \neg \left(x \leq 1.16 \cdot 10^{-89}\right):\\
\;\;\;\;y \cdot x\\
\mathbf{else}:\\
\;\;\;\;z\\
\end{array}
\end{array}
if x < -9.6e12 or 1.15999999999999993e-89 < x Initial program 97.3%
Taylor expanded in y around inf
*-commutativeN/A
lower-*.f6449.1
Applied rewrites49.1%
if -9.6e12 < x < 1.15999999999999993e-89Initial program 99.9%
Taylor expanded in x around 0
Applied rewrites71.0%
Final simplification58.3%
(FPCore (x y z) :precision binary64 (fma y x z))
double code(double x, double y, double z) {
return fma(y, x, z);
}
function code(x, y, z) return fma(y, x, z) end
code[x_, y_, z_] := N[(y * x + z), $MachinePrecision]
\begin{array}{l}
\\
\mathsf{fma}\left(y, x, z\right)
\end{array}
Initial program 98.4%
Taylor expanded in x around 0
Applied rewrites72.4%
lift-+.f64N/A
lift-*.f64N/A
*-commutativeN/A
lower-fma.f6472.4
Applied rewrites72.4%
(FPCore (x y z) :precision binary64 z)
double code(double x, double y, double z) {
return 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 = z
end function
public static double code(double x, double y, double z) {
return z;
}
def code(x, y, z): return z
function code(x, y, z) return z end
function tmp = code(x, y, z) tmp = z; end
code[x_, y_, z_] := z
\begin{array}{l}
\\
z
\end{array}
Initial program 98.4%
Taylor expanded in x around 0
Applied rewrites34.3%
herbie shell --seed 2025057
(FPCore (x y z)
:name "Diagrams.Backend.Rasterific:$crender from diagrams-rasterific-1.3.1.3"
:precision binary64
(+ (* x y) (* (- 1.0 x) z)))