
(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 6 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.9%
lift--.f64N/A
lift-exp.f64N/A
lower-expm1.f6499.9
lift-*.f64N/A
*-commutativeN/A
lower-*.f6499.9
Applied rewrites99.9%
(FPCore (a x) :precision binary64 (if (<= (- (exp (* a x)) 1.0) -1.0) (- (* (* (fma (* a a) 0.5 (/ a x)) x) x) 1.0) (* (* (fma (* 0.5 a) x 1.0) a) x)))
double code(double a, double x) {
double tmp;
if ((exp((a * x)) - 1.0) <= -1.0) {
tmp = ((fma((a * a), 0.5, (a / x)) * x) * x) - 1.0;
} else {
tmp = (fma((0.5 * a), x, 1.0) * 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(fma(Float64(a * a), 0.5, Float64(a / x)) * x) * x) - 1.0); else tmp = Float64(Float64(fma(Float64(0.5 * a), x, 1.0) * 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[(N[(a * a), $MachinePrecision] * 0.5 + N[(a / x), $MachinePrecision]), $MachinePrecision] * x), $MachinePrecision] * x), $MachinePrecision] - 1.0), $MachinePrecision], N[(N[(N[(N[(0.5 * a), $MachinePrecision] * x + 1.0), $MachinePrecision] * a), $MachinePrecision] * x), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;e^{a \cdot x} - 1 \leq -1:\\
\;\;\;\;\left(\mathsf{fma}\left(a \cdot a, 0.5, \frac{a}{x}\right) \cdot x\right) \cdot x - 1\\
\mathbf{else}:\\
\;\;\;\;\left(\mathsf{fma}\left(0.5 \cdot a, x, 1\right) \cdot 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
+-commutativeN/A
Applied rewrites0.9%
Taylor expanded in x around inf
Applied rewrites10.5%
if -1 < (-.f64 (exp.f64 (*.f64 a x)) #s(literal 1 binary64)) Initial program 33.6%
lift--.f64N/A
lift-exp.f64N/A
lower-expm1.f6499.9
lift-*.f64N/A
*-commutativeN/A
lower-*.f6499.9
Applied rewrites99.9%
Taylor expanded in a around 0
distribute-rgt-inN/A
associate-*l*N/A
*-commutativeN/A
associate-*r*N/A
unpow2N/A
*-commutativeN/A
unpow2N/A
associate-*r*N/A
associate-*l*N/A
*-commutativeN/A
distribute-lft-inN/A
*-commutativeN/A
lower-*.f64N/A
+-commutativeN/A
*-commutativeN/A
lower-fma.f64N/A
lower-*.f64N/A
unpow2N/A
lower-*.f6490.0
Applied rewrites90.0%
Taylor expanded in a around 0
Applied rewrites97.0%
(FPCore (a x) :precision binary64 (if (<= (- (exp (* a x)) 1.0) -1.0) (- (* (* (* (* a a) x) 0.5) x) 1.0) (* (* (fma (* 0.5 a) x 1.0) a) x)))
double code(double a, double x) {
double tmp;
if ((exp((a * x)) - 1.0) <= -1.0) {
tmp = ((((a * a) * x) * 0.5) * x) - 1.0;
} else {
tmp = (fma((0.5 * a), x, 1.0) * 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(Float64(a * a) * x) * 0.5) * x) - 1.0); else tmp = Float64(Float64(fma(Float64(0.5 * a), x, 1.0) * 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[(N[(a * a), $MachinePrecision] * x), $MachinePrecision] * 0.5), $MachinePrecision] * x), $MachinePrecision] - 1.0), $MachinePrecision], N[(N[(N[(N[(0.5 * a), $MachinePrecision] * x + 1.0), $MachinePrecision] * a), $MachinePrecision] * x), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;e^{a \cdot x} - 1 \leq -1:\\
\;\;\;\;\left(\left(\left(a \cdot a\right) \cdot x\right) \cdot 0.5\right) \cdot x - 1\\
\mathbf{else}:\\
\;\;\;\;\left(\mathsf{fma}\left(0.5 \cdot a, x, 1\right) \cdot 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
+-commutativeN/A
Applied rewrites0.9%
Taylor expanded in a around inf
Applied rewrites9.5%
if -1 < (-.f64 (exp.f64 (*.f64 a x)) #s(literal 1 binary64)) Initial program 33.6%
lift--.f64N/A
lift-exp.f64N/A
lower-expm1.f6499.9
lift-*.f64N/A
*-commutativeN/A
lower-*.f6499.9
Applied rewrites99.9%
Taylor expanded in a around 0
distribute-rgt-inN/A
associate-*l*N/A
*-commutativeN/A
associate-*r*N/A
unpow2N/A
*-commutativeN/A
unpow2N/A
associate-*r*N/A
associate-*l*N/A
*-commutativeN/A
distribute-lft-inN/A
*-commutativeN/A
lower-*.f64N/A
+-commutativeN/A
*-commutativeN/A
lower-fma.f64N/A
lower-*.f64N/A
unpow2N/A
lower-*.f6490.0
Applied rewrites90.0%
Taylor expanded in a around 0
Applied rewrites97.0%
(FPCore (a x) :precision binary64 (if (<= a -3.8e+246) (- (* (* (fma (* x x) 0.5 (/ x a)) a) a) 1.0) (* x a)))
double code(double a, double x) {
double tmp;
if (a <= -3.8e+246) {
tmp = ((fma((x * x), 0.5, (x / a)) * a) * a) - 1.0;
} else {
tmp = x * a;
}
return tmp;
}
function code(a, x) tmp = 0.0 if (a <= -3.8e+246) tmp = Float64(Float64(Float64(fma(Float64(x * x), 0.5, Float64(x / a)) * a) * a) - 1.0); else tmp = Float64(x * a); end return tmp end
code[a_, x_] := If[LessEqual[a, -3.8e+246], N[(N[(N[(N[(N[(x * x), $MachinePrecision] * 0.5 + N[(x / a), $MachinePrecision]), $MachinePrecision] * a), $MachinePrecision] * a), $MachinePrecision] - 1.0), $MachinePrecision], N[(x * a), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;a \leq -3.8 \cdot 10^{+246}:\\
\;\;\;\;\left(\mathsf{fma}\left(x \cdot x, 0.5, \frac{x}{a}\right) \cdot a\right) \cdot a - 1\\
\mathbf{else}:\\
\;\;\;\;x \cdot a\\
\end{array}
\end{array}
if a < -3.79999999999999976e246Initial program 100.0%
Taylor expanded in a around 0
+-commutativeN/A
Applied rewrites1.0%
Taylor expanded in a around inf
Applied rewrites60.3%
if -3.79999999999999976e246 < a Initial program 55.2%
Taylor expanded in a around 0
*-commutativeN/A
lower-*.f6466.2
Applied rewrites66.2%
Final simplification66.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.9%
Taylor expanded in a around 0
*-commutativeN/A
lower-*.f6463.9
Applied rewrites63.9%
Final simplification63.9%
(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.9%
Taylor expanded in a around 0
Applied rewrites19.4%
(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 2024360
(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))