
(FPCore (a x) :precision binary64 (- (exp (* a x)) 1.0))
double code(double a, double x) {
return exp((a * x)) - 1.0;
}
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(a, x)
use fmin_fmax_functions
real(8), intent (in) :: a
real(8), intent (in) :: x
code = exp((a * x)) - 1.0d0
end function
public static double code(double a, double x) {
return Math.exp((a * x)) - 1.0;
}
def code(a, x): return math.exp((a * x)) - 1.0
function code(a, x) return Float64(exp(Float64(a * x)) - 1.0) end
function tmp = code(a, x) tmp = exp((a * x)) - 1.0; end
code[a_, x_] := N[(N[Exp[N[(a * x), $MachinePrecision]], $MachinePrecision] - 1.0), $MachinePrecision]
\begin{array}{l}
\\
e^{a \cdot x} - 1
\end{array}
Sampling outcomes in binary64 precision:
Herbie found 5 alternatives:
| Alternative | Accuracy | Speedup |
|---|
(FPCore (a x) :precision binary64 (- (exp (* a x)) 1.0))
double code(double a, double x) {
return exp((a * x)) - 1.0;
}
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(a, x)
use fmin_fmax_functions
real(8), intent (in) :: a
real(8), intent (in) :: x
code = exp((a * x)) - 1.0d0
end function
public static double code(double a, double x) {
return Math.exp((a * x)) - 1.0;
}
def code(a, x): return math.exp((a * x)) - 1.0
function code(a, x) return Float64(exp(Float64(a * x)) - 1.0) end
function tmp = code(a, x) tmp = exp((a * x)) - 1.0; end
code[a_, x_] := N[(N[Exp[N[(a * x), $MachinePrecision]], $MachinePrecision] - 1.0), $MachinePrecision]
\begin{array}{l}
\\
e^{a \cdot x} - 1
\end{array}
(FPCore (a x) :precision binary64 (expm1 (* x a)))
double code(double a, double x) {
return expm1((x * a));
}
public static double code(double a, double x) {
return Math.expm1((x * a));
}
def code(a, x): return math.expm1((x * a))
function code(a, x) return expm1(Float64(x * a)) end
code[a_, x_] := N[(Exp[N[(x * a), $MachinePrecision]] - 1), $MachinePrecision]
\begin{array}{l}
\\
\mathsf{expm1}\left(x \cdot a\right)
\end{array}
Initial program 56.1%
lift--.f64N/A
lift-*.f64N/A
lift-exp.f64N/A
lower-expm1.f64N/A
*-commutativeN/A
lower-*.f64100.0
Applied rewrites100.0%
(FPCore (a x) :precision binary64 (if (<= (- (exp (* a x)) 1.0) -1.0) (- (* (* (/ a x) x) x) 1.0) (* (fma (* a x) (* 0.5 a) a) x)))
double code(double a, double x) {
double tmp;
if ((exp((a * x)) - 1.0) <= -1.0) {
tmp = (((a / x) * x) * x) - 1.0;
} else {
tmp = fma((a * x), (0.5 * a), a) * x;
}
return tmp;
}
function code(a, x) tmp = 0.0 if (Float64(exp(Float64(a * x)) - 1.0) <= -1.0) tmp = Float64(Float64(Float64(Float64(a / x) * x) * x) - 1.0); else tmp = Float64(fma(Float64(a * x), Float64(0.5 * a), a) * x); end return tmp end
code[a_, x_] := If[LessEqual[N[(N[Exp[N[(a * x), $MachinePrecision]], $MachinePrecision] - 1.0), $MachinePrecision], -1.0], N[(N[(N[(N[(a / x), $MachinePrecision] * x), $MachinePrecision] * x), $MachinePrecision] - 1.0), $MachinePrecision], N[(N[(N[(a * x), $MachinePrecision] * N[(0.5 * a), $MachinePrecision] + a), $MachinePrecision] * x), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;e^{a \cdot x} - 1 \leq -1:\\
\;\;\;\;\left(\frac{a}{x} \cdot x\right) \cdot x - 1\\
\mathbf{else}:\\
\;\;\;\;\mathsf{fma}\left(a \cdot x, 0.5 \cdot a, a\right) \cdot x\\
\end{array}
\end{array}
if (-.f64 (exp.f64 (*.f64 a x)) #s(literal 1 binary64)) < -1Initial program 100.0%
Taylor expanded in a around 0
Applied rewrites3.1%
Taylor expanded in a around 0
+-commutativeN/A
*-commutativeN/A
lower-fma.f64N/A
+-commutativeN/A
*-commutativeN/A
lower-fma.f64N/A
*-commutativeN/A
lower-*.f64N/A
unpow2N/A
lower-*.f641.1
Applied rewrites1.1%
Taylor expanded in x around inf
*-commutativeN/A
pow2N/A
associate-*r*N/A
*-commutativeN/A
lower-*.f64N/A
*-commutativeN/A
lower-*.f64N/A
*-commutativeN/A
lower-fma.f64N/A
unpow2N/A
lower-*.f64N/A
lower-/.f647.4
Applied rewrites7.4%
Taylor expanded in a around 0
lift-/.f6415.1
Applied rewrites15.1%
if -1 < (-.f64 (exp.f64 (*.f64 a x)) #s(literal 1 binary64)) Initial program 32.7%
Taylor expanded in a around 0
distribute-rgt-inN/A
associate-*l*N/A
unpow2N/A
associate-*r*N/A
associate-*r*N/A
*-commutativeN/A
associate-*r*N/A
*-commutativeN/A
associate-*r*N/A
unpow2N/A
*-commutativeN/A
associate-*l*N/A
*-commutativeN/A
distribute-rgt-inN/A
*-commutativeN/A
lower-*.f64N/A
Applied rewrites94.3%
lift-fma.f64N/A
lift-*.f64N/A
lift-*.f64N/A
associate-*l*N/A
associate-*r*N/A
*-commutativeN/A
*-commutativeN/A
*-commutativeN/A
associate-*l*N/A
lower-fma.f64N/A
lower-*.f64N/A
lower-*.f6498.7
Applied rewrites98.7%
(FPCore (a x) :precision binary64 (if (<= (* a x) -1e+15) (- (* (* (* (* a a) 0.5) x) x) 1.0) (* (fma (* a x) (* 0.5 a) a) x)))
double code(double a, double x) {
double tmp;
if ((a * x) <= -1e+15) {
tmp = ((((a * a) * 0.5) * x) * x) - 1.0;
} else {
tmp = fma((a * x), (0.5 * a), a) * x;
}
return tmp;
}
function code(a, x) tmp = 0.0 if (Float64(a * x) <= -1e+15) tmp = Float64(Float64(Float64(Float64(Float64(a * a) * 0.5) * x) * x) - 1.0); else tmp = Float64(fma(Float64(a * x), Float64(0.5 * a), a) * x); end return tmp end
code[a_, x_] := If[LessEqual[N[(a * x), $MachinePrecision], -1e+15], N[(N[(N[(N[(N[(a * a), $MachinePrecision] * 0.5), $MachinePrecision] * x), $MachinePrecision] * x), $MachinePrecision] - 1.0), $MachinePrecision], N[(N[(N[(a * x), $MachinePrecision] * N[(0.5 * a), $MachinePrecision] + a), $MachinePrecision] * x), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;a \cdot x \leq -1 \cdot 10^{+15}:\\
\;\;\;\;\left(\left(\left(a \cdot a\right) \cdot 0.5\right) \cdot x\right) \cdot x - 1\\
\mathbf{else}:\\
\;\;\;\;\mathsf{fma}\left(a \cdot x, 0.5 \cdot a, a\right) \cdot x\\
\end{array}
\end{array}
if (*.f64 a x) < -1e15Initial program 100.0%
Taylor expanded in a around 0
Applied rewrites3.1%
Taylor expanded in a around 0
+-commutativeN/A
*-commutativeN/A
lower-fma.f64N/A
+-commutativeN/A
*-commutativeN/A
lower-fma.f64N/A
*-commutativeN/A
lower-*.f64N/A
unpow2N/A
lower-*.f641.1
Applied rewrites1.1%
Taylor expanded in x around inf
*-commutativeN/A
pow2N/A
associate-*r*N/A
*-commutativeN/A
lower-*.f64N/A
*-commutativeN/A
lower-*.f64N/A
*-commutativeN/A
lower-fma.f64N/A
unpow2N/A
lower-*.f64N/A
lower-/.f647.6
Applied rewrites7.6%
Taylor expanded in a around inf
*-commutativeN/A
lower-*.f64N/A
pow2N/A
lift-*.f647.7
Applied rewrites7.7%
if -1e15 < (*.f64 a x) Initial program 33.9%
Taylor expanded in a around 0
distribute-rgt-inN/A
associate-*l*N/A
unpow2N/A
associate-*r*N/A
associate-*r*N/A
*-commutativeN/A
associate-*r*N/A
*-commutativeN/A
associate-*r*N/A
unpow2N/A
*-commutativeN/A
associate-*l*N/A
*-commutativeN/A
distribute-rgt-inN/A
*-commutativeN/A
lower-*.f64N/A
Applied rewrites92.6%
lift-fma.f64N/A
lift-*.f64N/A
lift-*.f64N/A
associate-*l*N/A
associate-*r*N/A
*-commutativeN/A
*-commutativeN/A
*-commutativeN/A
associate-*l*N/A
lower-fma.f64N/A
lower-*.f64N/A
lower-*.f6497.0
Applied rewrites97.0%
(FPCore (a x) :precision binary64 (* x a))
double code(double a, double x) {
return x * a;
}
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(a, x)
use fmin_fmax_functions
real(8), intent (in) :: a
real(8), intent (in) :: x
code = x * a
end function
public static double code(double a, double x) {
return x * a;
}
def code(a, x): return x * a
function code(a, x) return Float64(x * a) end
function tmp = code(a, x) tmp = x * a; end
code[a_, x_] := N[(x * a), $MachinePrecision]
\begin{array}{l}
\\
x \cdot a
\end{array}
Initial program 56.1%
Taylor expanded in a around 0
*-commutativeN/A
lower-*.f6465.4
Applied rewrites65.4%
(FPCore (a x) :precision binary64 (- 1.0 1.0))
double code(double a, double x) {
return 1.0 - 1.0;
}
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(a, x)
use fmin_fmax_functions
real(8), intent (in) :: a
real(8), intent (in) :: x
code = 1.0d0 - 1.0d0
end function
public static double code(double a, double x) {
return 1.0 - 1.0;
}
def code(a, x): return 1.0 - 1.0
function code(a, x) return Float64(1.0 - 1.0) end
function tmp = code(a, x) tmp = 1.0 - 1.0; end
code[a_, x_] := N[(1.0 - 1.0), $MachinePrecision]
\begin{array}{l}
\\
1 - 1
\end{array}
Initial program 56.1%
Taylor expanded in a around 0
Applied rewrites20.2%
(FPCore (a x) :precision binary64 (expm1 (* a x)))
double code(double a, double x) {
return expm1((a * x));
}
public static double code(double a, double x) {
return Math.expm1((a * x));
}
def code(a, x): return math.expm1((a * x))
function code(a, x) return expm1(Float64(a * x)) end
code[a_, x_] := N[(Exp[N[(a * x), $MachinePrecision]] - 1), $MachinePrecision]
\begin{array}{l}
\\
\mathsf{expm1}\left(a \cdot x\right)
\end{array}
herbie shell --seed 2025056
(FPCore (a x)
:name "expax (section 3.5)"
:precision binary64
:pre (> 710.0 (* a x))
:alt
(! :herbie-platform default (expm1 (* a x)))
(- (exp (* a x)) 1.0))