
(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}
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 54.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 (<= (* a x) -2.0) (- (* (* (/ a x) x) x) 1.0) (* (* (fma 0.5 (* x a) 1.0) x) a)))
double code(double a, double x) {
double tmp;
if ((a * x) <= -2.0) {
tmp = (((a / x) * x) * x) - 1.0;
} else {
tmp = (fma(0.5, (x * a), 1.0) * x) * a;
}
return tmp;
}
function code(a, x) tmp = 0.0 if (Float64(a * x) <= -2.0) tmp = Float64(Float64(Float64(Float64(a / x) * x) * x) - 1.0); else tmp = Float64(Float64(fma(0.5, Float64(x * a), 1.0) * x) * a); end return tmp end
code[a_, x_] := If[LessEqual[N[(a * x), $MachinePrecision], -2.0], N[(N[(N[(N[(a / x), $MachinePrecision] * x), $MachinePrecision] * x), $MachinePrecision] - 1.0), $MachinePrecision], N[(N[(N[(0.5 * N[(x * a), $MachinePrecision] + 1.0), $MachinePrecision] * x), $MachinePrecision] * a), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;a \cdot x \leq -2:\\
\;\;\;\;\left(\frac{a}{x} \cdot x\right) \cdot x - 1\\
\mathbf{else}:\\
\;\;\;\;\left(\mathsf{fma}\left(0.5, x \cdot a, 1\right) \cdot x\right) \cdot a\\
\end{array}
\end{array}
if (*.f64 a x) < -2Initial 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.3
Applied rewrites1.3%
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-/.f646.6
Applied rewrites6.6%
Taylor expanded in a around 0
lift-/.f6415.3
Applied rewrites15.3%
if -2 < (*.f64 a x) Initial program 30.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.3%
lift-fma.f64N/A
lift-*.f64N/A
lift-*.f64N/A
associate-*l*N/A
associate-*r*N/A
*-commutativeN/A
*-rgt-identityN/A
distribute-lft-inN/A
+-commutativeN/A
*-commutativeN/A
lower-*.f64N/A
+-commutativeN/A
lower-fma.f64N/A
lower-*.f6498.5
Applied rewrites98.5%
lift-*.f64N/A
lift-*.f64N/A
lift-*.f64N/A
lift-fma.f64N/A
*-commutativeN/A
associate-*r*N/A
distribute-lft-inN/A
*-rgt-identityN/A
*-commutativeN/A
associate-*r*N/A
*-commutativeN/A
associate-*l*N/A
lower-*.f64N/A
Applied rewrites98.5%
(FPCore (a x) :precision binary64 (if (<= x 2.75e+268) (* x a) (- (* (* (* (* a a) 0.5) x) x) 1.0)))
double code(double a, double x) {
double tmp;
if (x <= 2.75e+268) {
tmp = x * a;
} else {
tmp = ((((a * a) * 0.5) * x) * x) - 1.0;
}
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(a, x)
use fmin_fmax_functions
real(8), intent (in) :: a
real(8), intent (in) :: x
real(8) :: tmp
if (x <= 2.75d+268) then
tmp = x * a
else
tmp = ((((a * a) * 0.5d0) * x) * x) - 1.0d0
end if
code = tmp
end function
public static double code(double a, double x) {
double tmp;
if (x <= 2.75e+268) {
tmp = x * a;
} else {
tmp = ((((a * a) * 0.5) * x) * x) - 1.0;
}
return tmp;
}
def code(a, x): tmp = 0 if x <= 2.75e+268: tmp = x * a else: tmp = ((((a * a) * 0.5) * x) * x) - 1.0 return tmp
function code(a, x) tmp = 0.0 if (x <= 2.75e+268) tmp = Float64(x * a); else tmp = Float64(Float64(Float64(Float64(Float64(a * a) * 0.5) * x) * x) - 1.0); end return tmp end
function tmp_2 = code(a, x) tmp = 0.0; if (x <= 2.75e+268) tmp = x * a; else tmp = ((((a * a) * 0.5) * x) * x) - 1.0; end tmp_2 = tmp; end
code[a_, x_] := If[LessEqual[x, 2.75e+268], N[(x * a), $MachinePrecision], N[(N[(N[(N[(N[(a * a), $MachinePrecision] * 0.5), $MachinePrecision] * x), $MachinePrecision] * x), $MachinePrecision] - 1.0), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq 2.75 \cdot 10^{+268}:\\
\;\;\;\;x \cdot a\\
\mathbf{else}:\\
\;\;\;\;\left(\left(\left(a \cdot a\right) \cdot 0.5\right) \cdot x\right) \cdot x - 1\\
\end{array}
\end{array}
if x < 2.7499999999999998e268Initial program 53.2%
Taylor expanded in a around 0
*-commutativeN/A
lower-*.f6467.7
Applied rewrites67.7%
if 2.7499999999999998e268 < x Initial program 97.6%
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-*.f640.7
Applied rewrites0.7%
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-/.f6421.1
Applied rewrites21.1%
Taylor expanded in a around inf
*-commutativeN/A
lower-*.f64N/A
pow2N/A
lift-*.f6421.1
Applied rewrites21.1%
(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 54.1%
Taylor expanded in a around 0
*-commutativeN/A
lower-*.f6466.4
Applied rewrites66.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 54.1%
Taylor expanded in a around 0
Applied rewrites19.1%
(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 2025095
(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))