
(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}
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 (* (- y z) x))) (if (<= x -620.0) t_0 (if (<= x 1.4e-24) (fma y x z) t_0))))
double code(double x, double y, double z) {
double t_0 = (y - z) * x;
double tmp;
if (x <= -620.0) {
tmp = t_0;
} else if (x <= 1.4e-24) {
tmp = fma(y, x, z);
} else {
tmp = t_0;
}
return tmp;
}
function code(x, y, z) t_0 = Float64(Float64(y - z) * x) tmp = 0.0 if (x <= -620.0) tmp = t_0; elseif (x <= 1.4e-24) tmp = fma(y, x, z); else tmp = t_0; end return tmp end
code[x_, y_, z_] := Block[{t$95$0 = N[(N[(y - z), $MachinePrecision] * x), $MachinePrecision]}, If[LessEqual[x, -620.0], t$95$0, If[LessEqual[x, 1.4e-24], N[(y * x + z), $MachinePrecision], t$95$0]]]
\begin{array}{l}
\\
\begin{array}{l}
t_0 := \left(y - z\right) \cdot x\\
\mathbf{if}\;x \leq -620:\\
\;\;\;\;t\_0\\
\mathbf{elif}\;x \leq 1.4 \cdot 10^{-24}:\\
\;\;\;\;\mathsf{fma}\left(y, x, z\right)\\
\mathbf{else}:\\
\;\;\;\;t\_0\\
\end{array}
\end{array}
if x < -620 or 1.4000000000000001e-24 < x Initial program 95.5%
lift-+.f64N/A
lift-*.f64N/A
lift--.f64N/A
lift-*.f64N/A
+-commutativeN/A
lower-fma.f64N/A
lift--.f64N/A
*-commutativeN/A
lower-*.f6497.8
Applied rewrites97.8%
Taylor expanded in x around inf
*-commutativeN/A
+-commutativeN/A
*-commutativeN/A
lower-*.f64N/A
cancel-sign-subN/A
metadata-evalN/A
*-lft-identityN/A
lower--.f6497.2
Applied rewrites97.2%
if -620 < x < 1.4000000000000001e-24Initial program 100.0%
Taylor expanded in x around 0
Applied rewrites99.2%
lift-+.f64N/A
lift-*.f64N/A
*-commutativeN/A
lower-fma.f6499.2
Applied rewrites99.2%
(FPCore (x y z) :precision binary64 (if (<= y -1e-131) (fma y x z) (if (<= y 8.5e-46) (* (- 1.0 x) z) (fma y x z))))
double code(double x, double y, double z) {
double tmp;
if (y <= -1e-131) {
tmp = fma(y, x, z);
} else if (y <= 8.5e-46) {
tmp = (1.0 - x) * z;
} else {
tmp = fma(y, x, z);
}
return tmp;
}
function code(x, y, z) tmp = 0.0 if (y <= -1e-131) tmp = fma(y, x, z); elseif (y <= 8.5e-46) tmp = Float64(Float64(1.0 - x) * z); else tmp = fma(y, x, z); end return tmp end
code[x_, y_, z_] := If[LessEqual[y, -1e-131], N[(y * x + z), $MachinePrecision], If[LessEqual[y, 8.5e-46], N[(N[(1.0 - x), $MachinePrecision] * z), $MachinePrecision], N[(y * x + z), $MachinePrecision]]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;y \leq -1 \cdot 10^{-131}:\\
\;\;\;\;\mathsf{fma}\left(y, x, z\right)\\
\mathbf{elif}\;y \leq 8.5 \cdot 10^{-46}:\\
\;\;\;\;\left(1 - x\right) \cdot z\\
\mathbf{else}:\\
\;\;\;\;\mathsf{fma}\left(y, x, z\right)\\
\end{array}
\end{array}
if y < -9.9999999999999999e-132 or 8.5000000000000001e-46 < y Initial program 96.4%
Taylor expanded in x around 0
Applied rewrites84.8%
lift-+.f64N/A
lift-*.f64N/A
*-commutativeN/A
lower-fma.f6484.9
Applied rewrites84.9%
if -9.9999999999999999e-132 < y < 8.5000000000000001e-46Initial program 100.0%
Taylor expanded in y around 0
*-commutativeN/A
lift-*.f64N/A
lift--.f6488.2
Applied rewrites88.2%
(FPCore (x y z) :precision binary64 (if (<= x -1.6e-9) (* y x) (if (<= x 1.4e-24) z (* y x))))
double code(double x, double y, double z) {
double tmp;
if (x <= -1.6e-9) {
tmp = y * x;
} else if (x <= 1.4e-24) {
tmp = 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)
use fmin_fmax_functions
real(8), intent (in) :: x
real(8), intent (in) :: y
real(8), intent (in) :: z
real(8) :: tmp
if (x <= (-1.6d-9)) then
tmp = y * x
else if (x <= 1.4d-24) then
tmp = z
else
tmp = y * x
end if
code = tmp
end function
public static double code(double x, double y, double z) {
double tmp;
if (x <= -1.6e-9) {
tmp = y * x;
} else if (x <= 1.4e-24) {
tmp = z;
} else {
tmp = y * x;
}
return tmp;
}
def code(x, y, z): tmp = 0 if x <= -1.6e-9: tmp = y * x elif x <= 1.4e-24: tmp = z else: tmp = y * x return tmp
function code(x, y, z) tmp = 0.0 if (x <= -1.6e-9) tmp = Float64(y * x); elseif (x <= 1.4e-24) tmp = z; else tmp = Float64(y * x); end return tmp end
function tmp_2 = code(x, y, z) tmp = 0.0; if (x <= -1.6e-9) tmp = y * x; elseif (x <= 1.4e-24) tmp = z; else tmp = y * x; end tmp_2 = tmp; end
code[x_, y_, z_] := If[LessEqual[x, -1.6e-9], N[(y * x), $MachinePrecision], If[LessEqual[x, 1.4e-24], z, N[(y * x), $MachinePrecision]]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq -1.6 \cdot 10^{-9}:\\
\;\;\;\;y \cdot x\\
\mathbf{elif}\;x \leq 1.4 \cdot 10^{-24}:\\
\;\;\;\;z\\
\mathbf{else}:\\
\;\;\;\;y \cdot x\\
\end{array}
\end{array}
if x < -1.60000000000000006e-9 or 1.4000000000000001e-24 < x Initial program 95.6%
Taylor expanded in y around inf
*-commutativeN/A
lower-*.f6453.2
Applied rewrites53.2%
if -1.60000000000000006e-9 < x < 1.4000000000000001e-24Initial program 100.0%
Taylor expanded in x around 0
Applied rewrites72.8%
(FPCore (x y z) :precision binary64 (fma (- 1.0 x) z (* y x)))
double code(double x, double y, double z) {
return fma((1.0 - x), z, (y * x));
}
function code(x, y, z) return fma(Float64(1.0 - x), z, Float64(y * x)) end
code[x_, y_, z_] := N[(N[(1.0 - x), $MachinePrecision] * z + N[(y * x), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\mathsf{fma}\left(1 - x, z, y \cdot x\right)
\end{array}
Initial program 97.6%
lift-+.f64N/A
lift-*.f64N/A
lift--.f64N/A
lift-*.f64N/A
+-commutativeN/A
lower-fma.f64N/A
lift--.f64N/A
*-commutativeN/A
lower-*.f6498.8
Applied rewrites98.8%
(FPCore (x y z) :precision binary64 (if (<= x 2.45e+134) (fma y x z) (* (- x) z)))
double code(double x, double y, double z) {
double tmp;
if (x <= 2.45e+134) {
tmp = fma(y, x, z);
} else {
tmp = -x * z;
}
return tmp;
}
function code(x, y, z) tmp = 0.0 if (x <= 2.45e+134) tmp = fma(y, x, z); else tmp = Float64(Float64(-x) * z); end return tmp end
code[x_, y_, z_] := If[LessEqual[x, 2.45e+134], N[(y * x + z), $MachinePrecision], N[((-x) * z), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq 2.45 \cdot 10^{+134}:\\
\;\;\;\;\mathsf{fma}\left(y, x, z\right)\\
\mathbf{else}:\\
\;\;\;\;\left(-x\right) \cdot z\\
\end{array}
\end{array}
if x < 2.44999999999999998e134Initial program 98.5%
Taylor expanded in x around 0
Applied rewrites79.8%
lift-+.f64N/A
lift-*.f64N/A
*-commutativeN/A
lower-fma.f6479.8
Applied rewrites79.8%
if 2.44999999999999998e134 < x Initial program 92.7%
Taylor expanded in y around 0
*-commutativeN/A
lift-*.f64N/A
lift--.f6453.8
Applied rewrites53.8%
Taylor expanded in x around inf
mul-1-negN/A
lower-neg.f6453.8
Applied rewrites53.8%
(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 97.6%
Taylor expanded in x around 0
Applied rewrites76.1%
lift-+.f64N/A
lift-*.f64N/A
*-commutativeN/A
lower-fma.f6476.1
Applied rewrites76.1%
(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 97.6%
Taylor expanded in x around 0
Applied rewrites36.4%
herbie shell --seed 2025091
(FPCore (x y z)
:name "Diagrams.Backend.Rasterific:$crender from diagrams-rasterific-1.3.1.3"
:precision binary64
(+ (* x y) (* (- 1.0 x) z)))