
(FPCore (x y z) :precision binary64 (+ (+ (/ x 2.0) (* y x)) z))
double code(double x, double y, double z) {
return ((x / 2.0) + (y * 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 / 2.0d0) + (y * x)) + z
end function
public static double code(double x, double y, double z) {
return ((x / 2.0) + (y * x)) + z;
}
def code(x, y, z): return ((x / 2.0) + (y * x)) + z
function code(x, y, z) return Float64(Float64(Float64(x / 2.0) + Float64(y * x)) + z) end
function tmp = code(x, y, z) tmp = ((x / 2.0) + (y * x)) + z; end
code[x_, y_, z_] := N[(N[(N[(x / 2.0), $MachinePrecision] + N[(y * x), $MachinePrecision]), $MachinePrecision] + z), $MachinePrecision]
\begin{array}{l}
\\
\left(\frac{x}{2} + y \cdot x\right) + z
\end{array}
Sampling outcomes in binary64 precision:
Herbie found 6 alternatives:
| Alternative | Accuracy | Speedup |
|---|
(FPCore (x y z) :precision binary64 (+ (+ (/ x 2.0) (* y x)) z))
double code(double x, double y, double z) {
return ((x / 2.0) + (y * 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 / 2.0d0) + (y * x)) + z
end function
public static double code(double x, double y, double z) {
return ((x / 2.0) + (y * x)) + z;
}
def code(x, y, z): return ((x / 2.0) + (y * x)) + z
function code(x, y, z) return Float64(Float64(Float64(x / 2.0) + Float64(y * x)) + z) end
function tmp = code(x, y, z) tmp = ((x / 2.0) + (y * x)) + z; end
code[x_, y_, z_] := N[(N[(N[(x / 2.0), $MachinePrecision] + N[(y * x), $MachinePrecision]), $MachinePrecision] + z), $MachinePrecision]
\begin{array}{l}
\\
\left(\frac{x}{2} + y \cdot x\right) + z
\end{array}
(FPCore (x y z) :precision binary64 (fma y x (fma 0.5 x z)))
double code(double x, double y, double z) {
return fma(y, x, fma(0.5, x, z));
}
function code(x, y, z) return fma(y, x, fma(0.5, x, z)) end
code[x_, y_, z_] := N[(y * x + N[(0.5 * x + z), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\mathsf{fma}\left(y, x, \mathsf{fma}\left(0.5, x, z\right)\right)
\end{array}
Initial program 100.0%
Taylor expanded in x around 0
lower-*.f64100.0
Applied rewrites100.0%
lift-+.f64N/A
lift-+.f64N/A
+-commutativeN/A
associate-+l+N/A
lift-*.f64N/A
lower-fma.f64N/A
lower-+.f64100.0
Applied rewrites100.0%
Taylor expanded in x around 0
+-commutativeN/A
lower-fma.f64100.0
Applied rewrites100.0%
(FPCore (x y z)
:precision binary64
(let* ((t_0 (+ (/ x 2.0) (* y x))))
(if (or (<= t_0 -4e+79) (not (<= t_0 5e+128)))
(* (- y -0.5) x)
(fma 0.5 x z))))
double code(double x, double y, double z) {
double t_0 = (x / 2.0) + (y * x);
double tmp;
if ((t_0 <= -4e+79) || !(t_0 <= 5e+128)) {
tmp = (y - -0.5) * x;
} else {
tmp = fma(0.5, x, z);
}
return tmp;
}
function code(x, y, z) t_0 = Float64(Float64(x / 2.0) + Float64(y * x)) tmp = 0.0 if ((t_0 <= -4e+79) || !(t_0 <= 5e+128)) tmp = Float64(Float64(y - -0.5) * x); else tmp = fma(0.5, x, z); end return tmp end
code[x_, y_, z_] := Block[{t$95$0 = N[(N[(x / 2.0), $MachinePrecision] + N[(y * x), $MachinePrecision]), $MachinePrecision]}, If[Or[LessEqual[t$95$0, -4e+79], N[Not[LessEqual[t$95$0, 5e+128]], $MachinePrecision]], N[(N[(y - -0.5), $MachinePrecision] * x), $MachinePrecision], N[(0.5 * x + z), $MachinePrecision]]]
\begin{array}{l}
\\
\begin{array}{l}
t_0 := \frac{x}{2} + y \cdot x\\
\mathbf{if}\;t\_0 \leq -4 \cdot 10^{+79} \lor \neg \left(t\_0 \leq 5 \cdot 10^{+128}\right):\\
\;\;\;\;\left(y - -0.5\right) \cdot x\\
\mathbf{else}:\\
\;\;\;\;\mathsf{fma}\left(0.5, x, z\right)\\
\end{array}
\end{array}
if (+.f64 (/.f64 x #s(literal 2 binary64)) (*.f64 y x)) < -3.99999999999999987e79 or 5e128 < (+.f64 (/.f64 x #s(literal 2 binary64)) (*.f64 y x)) Initial program 100.0%
Taylor expanded in y around 0
+-commutativeN/A
lower-fma.f6442.2
Applied rewrites42.2%
Taylor expanded in x around -inf
Applied rewrites92.5%
if -3.99999999999999987e79 < (+.f64 (/.f64 x #s(literal 2 binary64)) (*.f64 y x)) < 5e128Initial program 100.0%
Taylor expanded in y around 0
+-commutativeN/A
lower-fma.f6487.5
Applied rewrites87.5%
Final simplification89.9%
(FPCore (x y z) :precision binary64 (if (or (<= y -16500000000.0) (not (<= y 8.5e+57))) (* y x) (fma 0.5 x z)))
double code(double x, double y, double z) {
double tmp;
if ((y <= -16500000000.0) || !(y <= 8.5e+57)) {
tmp = y * x;
} else {
tmp = fma(0.5, x, z);
}
return tmp;
}
function code(x, y, z) tmp = 0.0 if ((y <= -16500000000.0) || !(y <= 8.5e+57)) tmp = Float64(y * x); else tmp = fma(0.5, x, z); end return tmp end
code[x_, y_, z_] := If[Or[LessEqual[y, -16500000000.0], N[Not[LessEqual[y, 8.5e+57]], $MachinePrecision]], N[(y * x), $MachinePrecision], N[(0.5 * x + z), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;y \leq -16500000000 \lor \neg \left(y \leq 8.5 \cdot 10^{+57}\right):\\
\;\;\;\;y \cdot x\\
\mathbf{else}:\\
\;\;\;\;\mathsf{fma}\left(0.5, x, z\right)\\
\end{array}
\end{array}
if y < -1.65e10 or 8.5000000000000001e57 < y Initial program 100.0%
Taylor expanded in y around 0
+-commutativeN/A
lower-fma.f6428.2
Applied rewrites28.2%
Taylor expanded in x around -inf
Applied rewrites74.0%
Taylor expanded in y around inf
*-commutativeN/A
lower-*.f6473.6
Applied rewrites73.6%
if -1.65e10 < y < 8.5000000000000001e57Initial program 100.0%
Taylor expanded in y around 0
+-commutativeN/A
lower-fma.f6494.6
Applied rewrites94.6%
Final simplification85.4%
(FPCore (x y z) :precision binary64 (if (or (<= y -0.5) (not (<= y 0.5))) (* y x) (* 0.5 x)))
double code(double x, double y, double z) {
double tmp;
if ((y <= -0.5) || !(y <= 0.5)) {
tmp = y * x;
} else {
tmp = 0.5 * 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 ((y <= (-0.5d0)) .or. (.not. (y <= 0.5d0))) then
tmp = y * x
else
tmp = 0.5d0 * x
end if
code = tmp
end function
public static double code(double x, double y, double z) {
double tmp;
if ((y <= -0.5) || !(y <= 0.5)) {
tmp = y * x;
} else {
tmp = 0.5 * x;
}
return tmp;
}
def code(x, y, z): tmp = 0 if (y <= -0.5) or not (y <= 0.5): tmp = y * x else: tmp = 0.5 * x return tmp
function code(x, y, z) tmp = 0.0 if ((y <= -0.5) || !(y <= 0.5)) tmp = Float64(y * x); else tmp = Float64(0.5 * x); end return tmp end
function tmp_2 = code(x, y, z) tmp = 0.0; if ((y <= -0.5) || ~((y <= 0.5))) tmp = y * x; else tmp = 0.5 * x; end tmp_2 = tmp; end
code[x_, y_, z_] := If[Or[LessEqual[y, -0.5], N[Not[LessEqual[y, 0.5]], $MachinePrecision]], N[(y * x), $MachinePrecision], N[(0.5 * x), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;y \leq -0.5 \lor \neg \left(y \leq 0.5\right):\\
\;\;\;\;y \cdot x\\
\mathbf{else}:\\
\;\;\;\;0.5 \cdot x\\
\end{array}
\end{array}
if y < -0.5 or 0.5 < y Initial program 100.0%
Taylor expanded in y around 0
+-commutativeN/A
lower-fma.f6431.3
Applied rewrites31.3%
Taylor expanded in x around -inf
Applied rewrites71.2%
Taylor expanded in y around inf
*-commutativeN/A
lower-*.f6469.7
Applied rewrites69.7%
if -0.5 < y < 0.5Initial program 100.0%
Taylor expanded in y around 0
+-commutativeN/A
lower-fma.f6498.3
Applied rewrites98.3%
Taylor expanded in x around -inf
Applied rewrites58.5%
Taylor expanded in y around 0
Applied rewrites56.8%
Final simplification63.1%
(FPCore (x y z) :precision binary64 (fma (+ 0.5 y) x z))
double code(double x, double y, double z) {
return fma((0.5 + y), x, z);
}
function code(x, y, z) return fma(Float64(0.5 + y), x, z) end
code[x_, y_, z_] := N[(N[(0.5 + y), $MachinePrecision] * x + z), $MachinePrecision]
\begin{array}{l}
\\
\mathsf{fma}\left(0.5 + y, x, z\right)
\end{array}
Initial program 100.0%
Taylor expanded in x around 0
Applied rewrites100.0%
(FPCore (x y z) :precision binary64 (* y x))
double code(double x, double y, double z) {
return y * x;
}
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 = y * x
end function
public static double code(double x, double y, double z) {
return y * x;
}
def code(x, y, z): return y * x
function code(x, y, z) return Float64(y * x) end
function tmp = code(x, y, z) tmp = y * x; end
code[x_, y_, z_] := N[(y * x), $MachinePrecision]
\begin{array}{l}
\\
y \cdot x
\end{array}
Initial program 100.0%
Taylor expanded in y around 0
+-commutativeN/A
lower-fma.f6465.6
Applied rewrites65.6%
Taylor expanded in x around -inf
Applied rewrites64.7%
Taylor expanded in y around inf
*-commutativeN/A
lower-*.f6435.9
Applied rewrites35.9%
herbie shell --seed 2024363
(FPCore (x y z)
:name "Data.Histogram.Bin.BinF:$cfromIndex from histogram-fill-0.8.4.1"
:precision binary64
(+ (+ (/ x 2.0) (* y x)) z))