
(FPCore (x) :precision binary64 (exp (- (- 1.0 (* x x)))))
double code(double x) {
return exp(-(1.0 - (x * 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)
use fmin_fmax_functions
real(8), intent (in) :: x
code = exp(-(1.0d0 - (x * x)))
end function
public static double code(double x) {
return Math.exp(-(1.0 - (x * x)));
}
def code(x): return math.exp(-(1.0 - (x * x)))
function code(x) return exp(Float64(-Float64(1.0 - Float64(x * x)))) end
function tmp = code(x) tmp = exp(-(1.0 - (x * x))); end
code[x_] := N[Exp[(-N[(1.0 - N[(x * x), $MachinePrecision]), $MachinePrecision])], $MachinePrecision]
\begin{array}{l}
\\
e^{-\left(1 - x \cdot x\right)}
\end{array}
Sampling outcomes in binary64 precision:
Herbie found 6 alternatives:
| Alternative | Accuracy | Speedup |
|---|
(FPCore (x) :precision binary64 (exp (- (- 1.0 (* x x)))))
double code(double x) {
return exp(-(1.0 - (x * 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)
use fmin_fmax_functions
real(8), intent (in) :: x
code = exp(-(1.0d0 - (x * x)))
end function
public static double code(double x) {
return Math.exp(-(1.0 - (x * x)));
}
def code(x): return math.exp(-(1.0 - (x * x)))
function code(x) return exp(Float64(-Float64(1.0 - Float64(x * x)))) end
function tmp = code(x) tmp = exp(-(1.0 - (x * x))); end
code[x_] := N[Exp[(-N[(1.0 - N[(x * x), $MachinePrecision]), $MachinePrecision])], $MachinePrecision]
\begin{array}{l}
\\
e^{-\left(1 - x \cdot x\right)}
\end{array}
(FPCore (x) :precision binary64 (exp (fma x x -1.0)))
double code(double x) {
return exp(fma(x, x, -1.0));
}
function code(x) return exp(fma(x, x, -1.0)) end
code[x_] := N[Exp[N[(x * x + -1.0), $MachinePrecision]], $MachinePrecision]
\begin{array}{l}
\\
e^{\mathsf{fma}\left(x, x, -1\right)}
\end{array}
Initial program 100.0%
Applied rewrites100.0%
(FPCore (x) :precision binary64 (if (<= x 1.75) (* (/ 1.0 (E)) (fma (fma (* 0.5 x) x 1.0) (* x x) 1.0)) (exp (* x x))))
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq 1.75:\\
\;\;\;\;\frac{1}{\mathsf{E}\left(\right)} \cdot \mathsf{fma}\left(\mathsf{fma}\left(0.5 \cdot x, x, 1\right), x \cdot x, 1\right)\\
\mathbf{else}:\\
\;\;\;\;e^{x \cdot x}\\
\end{array}
\end{array}
if x < 1.75Initial program 100.0%
Taylor expanded in x around 0
distribute-lft-inN/A
associate-+r+N/A
+-commutativeN/A
associate-+l+N/A
associate-*r*N/A
associate-*r*N/A
distribute-rgt1-inN/A
distribute-rgt-outN/A
lower-*.f64N/A
lower-exp.f64N/A
unpow2N/A
lower-fma.f64N/A
Applied rewrites91.7%
Applied rewrites91.7%
Applied rewrites91.7%
Applied rewrites91.7%
if 1.75 < x Initial program 100.0%
Taylor expanded in x around 0
Applied rewrites3.1%
Taylor expanded in x around inf
unpow2N/A
lower-*.f64100.0
Applied rewrites100.0%
(FPCore (x) :precision binary64 (* (/ 1.0 (E)) (fma (fma (* 0.5 x) x 1.0) (* x x) 1.0)))
\begin{array}{l}
\\
\frac{1}{\mathsf{E}\left(\right)} \cdot \mathsf{fma}\left(\mathsf{fma}\left(0.5 \cdot x, x, 1\right), x \cdot x, 1\right)
\end{array}
Initial program 100.0%
Taylor expanded in x around 0
distribute-lft-inN/A
associate-+r+N/A
+-commutativeN/A
associate-+l+N/A
associate-*r*N/A
associate-*r*N/A
distribute-rgt1-inN/A
distribute-rgt-outN/A
lower-*.f64N/A
lower-exp.f64N/A
unpow2N/A
lower-fma.f64N/A
Applied rewrites89.9%
Applied rewrites89.9%
Applied rewrites89.9%
Applied rewrites89.9%
(FPCore (x) :precision binary64 (if (<= x 1.0) (/ 1.0 (E)) (/ (* x x) (E))))
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq 1:\\
\;\;\;\;\frac{1}{\mathsf{E}\left(\right)}\\
\mathbf{else}:\\
\;\;\;\;\frac{x \cdot x}{\mathsf{E}\left(\right)}\\
\end{array}
\end{array}
if x < 1Initial program 100.0%
Taylor expanded in x around 0
distribute-lft-inN/A
associate-+r+N/A
+-commutativeN/A
associate-+l+N/A
associate-*r*N/A
associate-*r*N/A
distribute-rgt1-inN/A
distribute-rgt-outN/A
lower-*.f64N/A
lower-exp.f64N/A
unpow2N/A
lower-fma.f64N/A
Applied rewrites92.1%
Applied rewrites92.1%
Taylor expanded in x around 0
Applied rewrites70.3%
Applied rewrites70.3%
if 1 < x Initial program 100.0%
Taylor expanded in x around 0
distribute-lft-inN/A
associate-+r+N/A
+-commutativeN/A
associate-+l+N/A
associate-*r*N/A
associate-*r*N/A
distribute-rgt1-inN/A
distribute-rgt-outN/A
lower-*.f64N/A
lower-exp.f64N/A
unpow2N/A
lower-fma.f64N/A
Applied rewrites84.3%
Applied rewrites84.3%
Taylor expanded in x around 0
distribute-rgt1-inN/A
+-commutativeN/A
metadata-evalN/A
rec-expN/A
e-exp-1N/A
associate-*r/N/A
*-rgt-identityN/A
lower-/.f64N/A
+-commutativeN/A
unpow2N/A
lower-fma.f64N/A
lower-E.f6452.5
Applied rewrites52.5%
Taylor expanded in x around inf
Applied rewrites52.5%
(FPCore (x) :precision binary64 (/ (fma x x 1.0) (E)))
\begin{array}{l}
\\
\frac{\mathsf{fma}\left(x, x, 1\right)}{\mathsf{E}\left(\right)}
\end{array}
Initial program 100.0%
Taylor expanded in x around 0
distribute-lft-inN/A
associate-+r+N/A
+-commutativeN/A
associate-+l+N/A
associate-*r*N/A
associate-*r*N/A
distribute-rgt1-inN/A
distribute-rgt-outN/A
lower-*.f64N/A
lower-exp.f64N/A
unpow2N/A
lower-fma.f64N/A
Applied rewrites89.9%
Applied rewrites89.9%
Taylor expanded in x around 0
distribute-rgt1-inN/A
+-commutativeN/A
metadata-evalN/A
rec-expN/A
e-exp-1N/A
associate-*r/N/A
*-rgt-identityN/A
lower-/.f64N/A
+-commutativeN/A
unpow2N/A
lower-fma.f64N/A
lower-E.f6478.0
Applied rewrites78.0%
(FPCore (x) :precision binary64 (/ 1.0 (E)))
\begin{array}{l}
\\
\frac{1}{\mathsf{E}\left(\right)}
\end{array}
Initial program 100.0%
Taylor expanded in x around 0
distribute-lft-inN/A
associate-+r+N/A
+-commutativeN/A
associate-+l+N/A
associate-*r*N/A
associate-*r*N/A
distribute-rgt1-inN/A
distribute-rgt-outN/A
lower-*.f64N/A
lower-exp.f64N/A
unpow2N/A
lower-fma.f64N/A
Applied rewrites89.9%
Applied rewrites89.9%
Taylor expanded in x around 0
Applied rewrites51.7%
Applied rewrites51.7%
herbie shell --seed 2025003
(FPCore (x)
:name "exp neg sub"
:precision binary64
(exp (- (- 1.0 (* x x)))))