
(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 (let* ((t_0 (* (- 1.0 x) z))) (if (<= (+ (* x y) t_0) 2e+306) (fma y x t_0) (* (- y z) x))))
double code(double x, double y, double z) {
double t_0 = (1.0 - x) * z;
double tmp;
if (((x * y) + t_0) <= 2e+306) {
tmp = fma(y, x, t_0);
} else {
tmp = (y - z) * x;
}
return tmp;
}
function code(x, y, z) t_0 = Float64(Float64(1.0 - x) * z) tmp = 0.0 if (Float64(Float64(x * y) + t_0) <= 2e+306) tmp = fma(y, x, t_0); else tmp = Float64(Float64(y - z) * x); end return tmp end
code[x_, y_, z_] := Block[{t$95$0 = N[(N[(1.0 - x), $MachinePrecision] * z), $MachinePrecision]}, If[LessEqual[N[(N[(x * y), $MachinePrecision] + t$95$0), $MachinePrecision], 2e+306], N[(y * x + t$95$0), $MachinePrecision], N[(N[(y - z), $MachinePrecision] * x), $MachinePrecision]]]
\begin{array}{l}
\\
\begin{array}{l}
t_0 := \left(1 - x\right) \cdot z\\
\mathbf{if}\;x \cdot y + t\_0 \leq 2 \cdot 10^{+306}:\\
\;\;\;\;\mathsf{fma}\left(y, x, t\_0\right)\\
\mathbf{else}:\\
\;\;\;\;\left(y - z\right) \cdot x\\
\end{array}
\end{array}
if (+.f64 (*.f64 x y) (*.f64 (-.f64 #s(literal 1 binary64) x) z)) < 2.00000000000000003e306Initial program 100.0%
lift-+.f64N/A
lift-*.f64N/A
*-commutativeN/A
lift--.f64N/A
lift-*.f64N/A
lower-fma.f64N/A
lift-*.f64N/A
lift--.f64100.0
Applied rewrites100.0%
if 2.00000000000000003e306 < (+.f64 (*.f64 x y) (*.f64 (-.f64 #s(literal 1 binary64) x) z)) Initial program 71.4%
Taylor expanded in x around -inf
Applied rewrites100.0%
*-commutative100.0
lift--.f64N/A
lift-*.f64N/A
lift-neg.f64N/A
mul-1-negN/A
distribute-lft-out--N/A
associate-*r*N/A
mul-1-negN/A
fp-cancel-sign-sub-invN/A
mul-1-negN/A
distribute-rgt-neg-inN/A
mul-1-negN/A
distribute-lft-inN/A
+-commutativeN/A
*-commutativeN/A
lower-*.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.45e-18))) (* (- y z) x) (fma y x z)))
double code(double x, double y, double z) {
double tmp;
if ((x <= -1.0) || !(x <= 1.45e-18)) {
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.45e-18)) 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.45e-18]], $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.45 \cdot 10^{-18}\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.45e-18 < x Initial program 93.5%
Taylor expanded in x around -inf
Applied rewrites99.0%
*-commutative99.0
lift--.f64N/A
lift-*.f64N/A
lift-neg.f64N/A
mul-1-negN/A
distribute-lft-out--N/A
associate-*r*N/A
mul-1-negN/A
fp-cancel-sign-sub-invN/A
mul-1-negN/A
distribute-rgt-neg-inN/A
mul-1-negN/A
distribute-lft-inN/A
+-commutativeN/A
*-commutativeN/A
lower-*.f64N/A
fp-cancel-sign-sub-invN/A
metadata-evalN/A
*-lft-identityN/A
lower--.f6499.0
Applied rewrites99.0%
if -1 < x < 1.45e-18Initial program 100.0%
Taylor expanded in x around 0
Applied rewrites99.9%
lift-+.f64N/A
lift-*.f64N/A
*-commutativeN/A
lower-fma.f6499.9
Applied rewrites99.9%
Final simplification99.5%
(FPCore (x y z) :precision binary64 (if (or (<= z -2.3e+83) (not (<= z 3.4e+15))) (* (- 1.0 x) z) (fma y x z)))
double code(double x, double y, double z) {
double tmp;
if ((z <= -2.3e+83) || !(z <= 3.4e+15)) {
tmp = (1.0 - x) * z;
} else {
tmp = fma(y, x, z);
}
return tmp;
}
function code(x, y, z) tmp = 0.0 if ((z <= -2.3e+83) || !(z <= 3.4e+15)) 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, -2.3e+83], N[Not[LessEqual[z, 3.4e+15]], $MachinePrecision]], N[(N[(1.0 - x), $MachinePrecision] * z), $MachinePrecision], N[(y * x + z), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;z \leq -2.3 \cdot 10^{+83} \lor \neg \left(z \leq 3.4 \cdot 10^{+15}\right):\\
\;\;\;\;\left(1 - x\right) \cdot z\\
\mathbf{else}:\\
\;\;\;\;\mathsf{fma}\left(y, x, z\right)\\
\end{array}
\end{array}
if z < -2.29999999999999995e83 or 3.4e15 < z Initial program 93.9%
Taylor expanded in y around 0
*-commutativeN/A
lift-*.f64N/A
lift--.f6490.3
Applied rewrites90.3%
if -2.29999999999999995e83 < z < 3.4e15Initial program 99.3%
Taylor expanded in x around 0
Applied rewrites93.1%
lift-+.f64N/A
lift-*.f64N/A
*-commutativeN/A
lower-fma.f6493.1
Applied rewrites93.1%
Final simplification91.8%
(FPCore (x y z) :precision binary64 (if (or (<= x -2.05e+234) (not (<= x 1.35e+67))) (* (- x) z) (fma y x z)))
double code(double x, double y, double z) {
double tmp;
if ((x <= -2.05e+234) || !(x <= 1.35e+67)) {
tmp = -x * z;
} else {
tmp = fma(y, x, z);
}
return tmp;
}
function code(x, y, z) tmp = 0.0 if ((x <= -2.05e+234) || !(x <= 1.35e+67)) tmp = Float64(Float64(-x) * z); else tmp = fma(y, x, z); end return tmp end
code[x_, y_, z_] := If[Or[LessEqual[x, -2.05e+234], N[Not[LessEqual[x, 1.35e+67]], $MachinePrecision]], N[((-x) * z), $MachinePrecision], N[(y * x + z), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq -2.05 \cdot 10^{+234} \lor \neg \left(x \leq 1.35 \cdot 10^{+67}\right):\\
\;\;\;\;\left(-x\right) \cdot z\\
\mathbf{else}:\\
\;\;\;\;\mathsf{fma}\left(y, x, z\right)\\
\end{array}
\end{array}
if x < -2.04999999999999987e234 or 1.35e67 < x Initial program 92.5%
Taylor expanded in y around 0
*-commutativeN/A
lift-*.f64N/A
lift--.f6466.5
Applied rewrites66.5%
Taylor expanded in x around inf
mul-1-negN/A
lower-neg.f6466.5
Applied rewrites66.5%
if -2.04999999999999987e234 < x < 1.35e67Initial program 98.0%
Taylor expanded in x around 0
Applied rewrites89.3%
lift-+.f64N/A
lift-*.f64N/A
*-commutativeN/A
lower-fma.f6489.3
Applied rewrites89.3%
Final simplification84.5%
(FPCore (x y z) :precision binary64 (if (or (<= x -4200.0) (not (<= x 1.75e-45))) (* y x) z))
double code(double x, double y, double z) {
double tmp;
if ((x <= -4200.0) || !(x <= 1.75e-45)) {
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 <= (-4200.0d0)) .or. (.not. (x <= 1.75d-45))) 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 <= -4200.0) || !(x <= 1.75e-45)) {
tmp = y * x;
} else {
tmp = z;
}
return tmp;
}
def code(x, y, z): tmp = 0 if (x <= -4200.0) or not (x <= 1.75e-45): tmp = y * x else: tmp = z return tmp
function code(x, y, z) tmp = 0.0 if ((x <= -4200.0) || !(x <= 1.75e-45)) tmp = Float64(y * x); else tmp = z; end return tmp end
function tmp_2 = code(x, y, z) tmp = 0.0; if ((x <= -4200.0) || ~((x <= 1.75e-45))) tmp = y * x; else tmp = z; end tmp_2 = tmp; end
code[x_, y_, z_] := If[Or[LessEqual[x, -4200.0], N[Not[LessEqual[x, 1.75e-45]], $MachinePrecision]], N[(y * x), $MachinePrecision], z]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq -4200 \lor \neg \left(x \leq 1.75 \cdot 10^{-45}\right):\\
\;\;\;\;y \cdot x\\
\mathbf{else}:\\
\;\;\;\;z\\
\end{array}
\end{array}
if x < -4200 or 1.75e-45 < x Initial program 93.8%
Taylor expanded in y around inf
*-commutativeN/A
lower-*.f6458.1
Applied rewrites58.1%
if -4200 < x < 1.75e-45Initial program 100.0%
Taylor expanded in x around 0
Applied rewrites71.8%
Final simplification64.9%
(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 96.9%
Taylor expanded in x around 0
Applied rewrites79.3%
lift-+.f64N/A
lift-*.f64N/A
*-commutativeN/A
lower-fma.f6479.3
Applied rewrites79.3%
(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 96.9%
Taylor expanded in x around 0
Applied rewrites37.6%
Final simplification37.6%
herbie shell --seed 2025084
(FPCore (x y z)
:name "Diagrams.Backend.Rasterific:$crender from diagrams-rasterific-1.3.1.3"
:precision binary64
(+ (* x y) (* (- 1.0 x) z)))