
(FPCore (x y z) :precision binary64 (+ (* x (sin y)) (* z (cos y))))
double code(double x, double y, double z) {
return (x * sin(y)) + (z * cos(y));
}
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 * sin(y)) + (z * cos(y))
end function
public static double code(double x, double y, double z) {
return (x * Math.sin(y)) + (z * Math.cos(y));
}
def code(x, y, z): return (x * math.sin(y)) + (z * math.cos(y))
function code(x, y, z) return Float64(Float64(x * sin(y)) + Float64(z * cos(y))) end
function tmp = code(x, y, z) tmp = (x * sin(y)) + (z * cos(y)); end
code[x_, y_, z_] := N[(N[(x * N[Sin[y], $MachinePrecision]), $MachinePrecision] + N[(z * N[Cos[y], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
x \cdot \sin y + z \cdot \cos y
\end{array}
Sampling outcomes in binary64 precision:
Herbie found 6 alternatives:
| Alternative | Accuracy | Speedup |
|---|
(FPCore (x y z) :precision binary64 (+ (* x (sin y)) (* z (cos y))))
double code(double x, double y, double z) {
return (x * sin(y)) + (z * cos(y));
}
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 * sin(y)) + (z * cos(y))
end function
public static double code(double x, double y, double z) {
return (x * Math.sin(y)) + (z * Math.cos(y));
}
def code(x, y, z): return (x * math.sin(y)) + (z * math.cos(y))
function code(x, y, z) return Float64(Float64(x * sin(y)) + Float64(z * cos(y))) end
function tmp = code(x, y, z) tmp = (x * sin(y)) + (z * cos(y)); end
code[x_, y_, z_] := N[(N[(x * N[Sin[y], $MachinePrecision]), $MachinePrecision] + N[(z * N[Cos[y], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
x \cdot \sin y + z \cdot \cos y
\end{array}
(FPCore (x y z) :precision binary64 (fma (cos y) z (* (sin y) x)))
double code(double x, double y, double z) {
return fma(cos(y), z, (sin(y) * x));
}
function code(x, y, z) return fma(cos(y), z, Float64(sin(y) * x)) end
code[x_, y_, z_] := N[(N[Cos[y], $MachinePrecision] * z + N[(N[Sin[y], $MachinePrecision] * x), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\mathsf{fma}\left(\cos y, z, \sin y \cdot x\right)
\end{array}
Initial program 99.8%
lift-+.f64N/A
+-commutativeN/A
lift-*.f64N/A
*-commutativeN/A
lower-fma.f6499.9
lift-*.f64N/A
*-commutativeN/A
lower-*.f6499.9
Applied rewrites99.9%
(FPCore (x y z) :precision binary64 (if (or (<= z -1.45e+103) (not (<= z 5e-77))) (* (cos y) z) (fma (sin y) x (* 1.0 z))))
double code(double x, double y, double z) {
double tmp;
if ((z <= -1.45e+103) || !(z <= 5e-77)) {
tmp = cos(y) * z;
} else {
tmp = fma(sin(y), x, (1.0 * z));
}
return tmp;
}
function code(x, y, z) tmp = 0.0 if ((z <= -1.45e+103) || !(z <= 5e-77)) tmp = Float64(cos(y) * z); else tmp = fma(sin(y), x, Float64(1.0 * z)); end return tmp end
code[x_, y_, z_] := If[Or[LessEqual[z, -1.45e+103], N[Not[LessEqual[z, 5e-77]], $MachinePrecision]], N[(N[Cos[y], $MachinePrecision] * z), $MachinePrecision], N[(N[Sin[y], $MachinePrecision] * x + N[(1.0 * z), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;z \leq -1.45 \cdot 10^{+103} \lor \neg \left(z \leq 5 \cdot 10^{-77}\right):\\
\;\;\;\;\cos y \cdot z\\
\mathbf{else}:\\
\;\;\;\;\mathsf{fma}\left(\sin y, x, 1 \cdot z\right)\\
\end{array}
\end{array}
if z < -1.4499999999999999e103 or 4.99999999999999963e-77 < z Initial program 99.8%
Taylor expanded in x around 0
*-commutativeN/A
lower-*.f64N/A
lower-cos.f6482.8
Applied rewrites82.8%
if -1.4499999999999999e103 < z < 4.99999999999999963e-77Initial program 99.9%
Taylor expanded in y around 0
Applied rewrites91.2%
lift-+.f64N/A
lift-*.f64N/A
*-commutativeN/A
lower-fma.f6491.3
lift-*.f64N/A
*-commutativeN/A
lower-*.f6491.3
Applied rewrites91.3%
Final simplification87.3%
(FPCore (x y z) :precision binary64 (if (or (<= y -1.75e+28) (not (<= y 0.032))) (* (cos y) z) (fma (fma (* -0.16666666666666666 (* x y)) y x) y z)))
double code(double x, double y, double z) {
double tmp;
if ((y <= -1.75e+28) || !(y <= 0.032)) {
tmp = cos(y) * z;
} else {
tmp = fma(fma((-0.16666666666666666 * (x * y)), y, x), y, z);
}
return tmp;
}
function code(x, y, z) tmp = 0.0 if ((y <= -1.75e+28) || !(y <= 0.032)) tmp = Float64(cos(y) * z); else tmp = fma(fma(Float64(-0.16666666666666666 * Float64(x * y)), y, x), y, z); end return tmp end
code[x_, y_, z_] := If[Or[LessEqual[y, -1.75e+28], N[Not[LessEqual[y, 0.032]], $MachinePrecision]], N[(N[Cos[y], $MachinePrecision] * z), $MachinePrecision], N[(N[(N[(-0.16666666666666666 * N[(x * y), $MachinePrecision]), $MachinePrecision] * y + x), $MachinePrecision] * y + z), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;y \leq -1.75 \cdot 10^{+28} \lor \neg \left(y \leq 0.032\right):\\
\;\;\;\;\cos y \cdot z\\
\mathbf{else}:\\
\;\;\;\;\mathsf{fma}\left(\mathsf{fma}\left(-0.16666666666666666 \cdot \left(x \cdot y\right), y, x\right), y, z\right)\\
\end{array}
\end{array}
if y < -1.75e28 or 0.032000000000000001 < y Initial program 99.7%
Taylor expanded in x around 0
*-commutativeN/A
lower-*.f64N/A
lower-cos.f6449.6
Applied rewrites49.6%
if -1.75e28 < y < 0.032000000000000001Initial program 100.0%
lift-+.f64N/A
+-commutativeN/A
lift-*.f64N/A
*-commutativeN/A
lower-fma.f64100.0
lift-*.f64N/A
*-commutativeN/A
lower-*.f64100.0
Applied rewrites100.0%
Taylor expanded in y around 0
+-commutativeN/A
*-commutativeN/A
lower-fma.f64N/A
+-commutativeN/A
*-commutativeN/A
lower-fma.f64N/A
+-commutativeN/A
lower-fma.f64N/A
lower-*.f64N/A
lower-*.f6497.9
Applied rewrites97.9%
Taylor expanded in x around inf
Applied rewrites97.9%
Final simplification75.5%
(FPCore (x y z)
:precision binary64
(if (<= y -1.75e+28)
(* (cos y) z)
(if (<= y 1.45e-5)
(fma (fma (* -0.16666666666666666 (* x y)) y x) y z)
(* (sin y) x))))
double code(double x, double y, double z) {
double tmp;
if (y <= -1.75e+28) {
tmp = cos(y) * z;
} else if (y <= 1.45e-5) {
tmp = fma(fma((-0.16666666666666666 * (x * y)), y, x), y, z);
} else {
tmp = sin(y) * x;
}
return tmp;
}
function code(x, y, z) tmp = 0.0 if (y <= -1.75e+28) tmp = Float64(cos(y) * z); elseif (y <= 1.45e-5) tmp = fma(fma(Float64(-0.16666666666666666 * Float64(x * y)), y, x), y, z); else tmp = Float64(sin(y) * x); end return tmp end
code[x_, y_, z_] := If[LessEqual[y, -1.75e+28], N[(N[Cos[y], $MachinePrecision] * z), $MachinePrecision], If[LessEqual[y, 1.45e-5], N[(N[(N[(-0.16666666666666666 * N[(x * y), $MachinePrecision]), $MachinePrecision] * y + x), $MachinePrecision] * y + z), $MachinePrecision], N[(N[Sin[y], $MachinePrecision] * x), $MachinePrecision]]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;y \leq -1.75 \cdot 10^{+28}:\\
\;\;\;\;\cos y \cdot z\\
\mathbf{elif}\;y \leq 1.45 \cdot 10^{-5}:\\
\;\;\;\;\mathsf{fma}\left(\mathsf{fma}\left(-0.16666666666666666 \cdot \left(x \cdot y\right), y, x\right), y, z\right)\\
\mathbf{else}:\\
\;\;\;\;\sin y \cdot x\\
\end{array}
\end{array}
if y < -1.75e28Initial program 99.8%
Taylor expanded in x around 0
*-commutativeN/A
lower-*.f64N/A
lower-cos.f6460.2
Applied rewrites60.2%
if -1.75e28 < y < 1.45e-5Initial program 100.0%
lift-+.f64N/A
+-commutativeN/A
lift-*.f64N/A
*-commutativeN/A
lower-fma.f64100.0
lift-*.f64N/A
*-commutativeN/A
lower-*.f64100.0
Applied rewrites100.0%
Taylor expanded in y around 0
+-commutativeN/A
*-commutativeN/A
lower-fma.f64N/A
+-commutativeN/A
*-commutativeN/A
lower-fma.f64N/A
+-commutativeN/A
lower-fma.f64N/A
lower-*.f64N/A
lower-*.f6497.9
Applied rewrites97.9%
Taylor expanded in x around inf
Applied rewrites97.9%
if 1.45e-5 < y Initial program 99.6%
lift-+.f64N/A
unpow1N/A
metadata-evalN/A
sqrt-pow1N/A
pow2N/A
lift-*.f64N/A
associate-*l*N/A
*-commutativeN/A
sqrt-prodN/A
lower-fma.f64N/A
lower-sqrt.f64N/A
lift-*.f64N/A
*-commutativeN/A
associate-*r*N/A
lower-*.f64N/A
pow2N/A
lower-pow.f64N/A
lower-sqrt.f6444.5
lift-*.f64N/A
*-commutativeN/A
lower-*.f6444.5
Applied rewrites44.5%
Taylor expanded in x around -inf
associate-*r*N/A
*-commutativeN/A
*-commutativeN/A
unpow2N/A
rem-square-sqrtN/A
mul-1-negN/A
distribute-lft-neg-inN/A
distribute-rgt-neg-inN/A
distribute-lft-neg-inN/A
metadata-evalN/A
*-lft-identityN/A
lower-*.f64N/A
lower-sin.f6455.9
Applied rewrites55.9%
Final simplification79.1%
(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 99.8%
Taylor expanded in y around 0
fp-cancel-sign-sub-invN/A
fp-cancel-sub-sign-invN/A
+-commutativeN/A
remove-double-negN/A
remove-double-negN/A
remove-double-negN/A
*-commutativeN/A
lower-fma.f6454.9
Applied rewrites54.9%
Final simplification54.9%
(FPCore (x y z) :precision binary64 (* x y))
double code(double x, double y, double z) {
return x * y;
}
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
end function
public static double code(double x, double y, double z) {
return x * y;
}
def code(x, y, z): return x * y
function code(x, y, z) return Float64(x * y) end
function tmp = code(x, y, z) tmp = x * y; end
code[x_, y_, z_] := N[(x * y), $MachinePrecision]
\begin{array}{l}
\\
x \cdot y
\end{array}
Initial program 99.8%
Taylor expanded in y around 0
fp-cancel-sign-sub-invN/A
fp-cancel-sub-sign-invN/A
+-commutativeN/A
remove-double-negN/A
remove-double-negN/A
remove-double-negN/A
*-commutativeN/A
lower-fma.f6454.9
Applied rewrites54.9%
Taylor expanded in x around inf
Applied rewrites21.4%
Final simplification21.4%
herbie shell --seed 2024360
(FPCore (x y z)
:name "Diagrams.ThreeD.Transform:aboutX from diagrams-lib-1.3.0.3, B"
:precision binary64
(+ (* x (sin y)) (* z (cos y))))