
(FPCore (c x y) :precision binary64 (* c (log (+ 1.0 (* (- (pow (E) x) 1.0) y)))))
\begin{array}{l}
\\
c \cdot \log \left(1 + \left({\mathsf{E}\left(\right)}^{x} - 1\right) \cdot y\right)
\end{array}
Sampling outcomes in binary64 precision:
Herbie found 7 alternatives:
| Alternative | Accuracy | Speedup |
|---|
(FPCore (c x y) :precision binary64 (* c (log (+ 1.0 (* (- (pow (E) x) 1.0) y)))))
\begin{array}{l}
\\
c \cdot \log \left(1 + \left({\mathsf{E}\left(\right)}^{x} - 1\right) \cdot y\right)
\end{array}
(FPCore (c x y) :precision binary64 (* (log1p (* y (expm1 x))) c))
double code(double c, double x, double y) {
return log1p((y * expm1(x))) * c;
}
public static double code(double c, double x, double y) {
return Math.log1p((y * Math.expm1(x))) * c;
}
def code(c, x, y): return math.log1p((y * math.expm1(x))) * c
function code(c, x, y) return Float64(log1p(Float64(y * expm1(x))) * c) end
code[c_, x_, y_] := N[(N[Log[1 + N[(y * N[(Exp[x] - 1), $MachinePrecision]), $MachinePrecision]], $MachinePrecision] * c), $MachinePrecision]
\begin{array}{l}
\\
\mathsf{log1p}\left(y \cdot \mathsf{expm1}\left(x\right)\right) \cdot c
\end{array}
Initial program 37.4%
lift-*.f64N/A
*-commutativeN/A
lower-*.f6437.4
Applied rewrites92.2%
(FPCore (c x y)
:precision binary64
(if (<= (- (pow (E) x) 1.0) -0.5)
(* (* (expm1 x) y) c)
(*
(log1p
(*
(*
(fma (fma (fma 0.041666666666666664 x 0.16666666666666666) x 0.5) x 1.0)
x)
y))
c)))\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;{\mathsf{E}\left(\right)}^{x} - 1 \leq -0.5:\\
\;\;\;\;\left(\mathsf{expm1}\left(x\right) \cdot y\right) \cdot c\\
\mathbf{else}:\\
\;\;\;\;\mathsf{log1p}\left(\left(\mathsf{fma}\left(\mathsf{fma}\left(\mathsf{fma}\left(0.041666666666666664, x, 0.16666666666666666\right), x, 0.5\right), x, 1\right) \cdot x\right) \cdot y\right) \cdot c\\
\end{array}
\end{array}
if (-.f64 (pow.f64 (E.f64) x) #s(literal 1 binary64)) < -0.5Initial program 54.3%
lift-*.f64N/A
*-commutativeN/A
lower-*.f6454.3
Applied rewrites99.9%
Taylor expanded in y around 0
*-commutativeN/A
lower-*.f64N/A
*-commutativeN/A
lower-*.f64N/A
lower-expm1.f6469.8
Applied rewrites69.8%
if -0.5 < (-.f64 (pow.f64 (E.f64) x) #s(literal 1 binary64)) Initial program 29.4%
Taylor expanded in x around 0
*-commutativeN/A
lower-*.f64N/A
Applied rewrites44.8%
lift-*.f64N/A
*-commutativeN/A
lower-*.f6444.8
lift-log.f64N/A
lift-+.f64N/A
lower-log1p.f6487.9
Applied rewrites87.9%
(FPCore (c x y) :precision binary64 (if (<= (- (pow (E) x) 1.0) -0.5) (* (* (expm1 x) y) c) (* (log1p (* y (* (fma (fma 0.16666666666666666 x 0.5) x 1.0) x))) c)))
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;{\mathsf{E}\left(\right)}^{x} - 1 \leq -0.5:\\
\;\;\;\;\left(\mathsf{expm1}\left(x\right) \cdot y\right) \cdot c\\
\mathbf{else}:\\
\;\;\;\;\mathsf{log1p}\left(y \cdot \left(\mathsf{fma}\left(\mathsf{fma}\left(0.16666666666666666, x, 0.5\right), x, 1\right) \cdot x\right)\right) \cdot c\\
\end{array}
\end{array}
if (-.f64 (pow.f64 (E.f64) x) #s(literal 1 binary64)) < -0.5Initial program 54.3%
lift-*.f64N/A
*-commutativeN/A
lower-*.f6454.3
Applied rewrites99.9%
Taylor expanded in y around 0
*-commutativeN/A
lower-*.f64N/A
*-commutativeN/A
lower-*.f64N/A
lower-expm1.f6469.8
Applied rewrites69.8%
if -0.5 < (-.f64 (pow.f64 (E.f64) x) #s(literal 1 binary64)) Initial program 29.4%
lift-*.f64N/A
*-commutativeN/A
lower-*.f6429.4
Applied rewrites88.6%
Taylor expanded in x around 0
*-commutativeN/A
lower-*.f64N/A
+-commutativeN/A
*-commutativeN/A
lower-fma.f64N/A
+-commutativeN/A
lower-fma.f6487.8
Applied rewrites87.8%
(FPCore (c x y) :precision binary64 (if (<= (- (pow (E) x) 1.0) -0.5) (* (* (expm1 x) y) c) (* (log1p (* y (* (fma 0.5 x 1.0) x))) c)))
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;{\mathsf{E}\left(\right)}^{x} - 1 \leq -0.5:\\
\;\;\;\;\left(\mathsf{expm1}\left(x\right) \cdot y\right) \cdot c\\
\mathbf{else}:\\
\;\;\;\;\mathsf{log1p}\left(y \cdot \left(\mathsf{fma}\left(0.5, x, 1\right) \cdot x\right)\right) \cdot c\\
\end{array}
\end{array}
if (-.f64 (pow.f64 (E.f64) x) #s(literal 1 binary64)) < -0.5Initial program 54.3%
lift-*.f64N/A
*-commutativeN/A
lower-*.f6454.3
Applied rewrites99.9%
Taylor expanded in y around 0
*-commutativeN/A
lower-*.f64N/A
*-commutativeN/A
lower-*.f64N/A
lower-expm1.f6469.8
Applied rewrites69.8%
if -0.5 < (-.f64 (pow.f64 (E.f64) x) #s(literal 1 binary64)) Initial program 29.4%
lift-*.f64N/A
*-commutativeN/A
lower-*.f6429.4
Applied rewrites88.6%
Taylor expanded in x around 0
*-commutativeN/A
lower-*.f64N/A
+-commutativeN/A
lower-fma.f6487.6
Applied rewrites87.6%
(FPCore (c x y) :precision binary64 (if (<= (- (pow (E) x) 1.0) -5e-8) (* (* (expm1 x) y) c) (* (fma 0.5 (* (* x y) c) (* c y)) x)))
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;{\mathsf{E}\left(\right)}^{x} - 1 \leq -5 \cdot 10^{-8}:\\
\;\;\;\;\left(\mathsf{expm1}\left(x\right) \cdot y\right) \cdot c\\
\mathbf{else}:\\
\;\;\;\;\mathsf{fma}\left(0.5, \left(x \cdot y\right) \cdot c, c \cdot y\right) \cdot x\\
\end{array}
\end{array}
if (-.f64 (pow.f64 (E.f64) x) #s(literal 1 binary64)) < -4.9999999999999998e-8Initial program 54.0%
lift-*.f64N/A
*-commutativeN/A
lower-*.f6454.0
Applied rewrites99.9%
Taylor expanded in y around 0
*-commutativeN/A
lower-*.f64N/A
*-commutativeN/A
lower-*.f64N/A
lower-expm1.f6469.4
Applied rewrites69.4%
if -4.9999999999999998e-8 < (-.f64 (pow.f64 (E.f64) x) #s(literal 1 binary64)) Initial program 29.2%
lift-*.f64N/A
*-commutativeN/A
lower-*.f6429.2
Applied rewrites88.5%
Taylor expanded in y around 0
*-commutativeN/A
lower-*.f64N/A
*-commutativeN/A
lower-*.f64N/A
lower-expm1.f6473.6
Applied rewrites73.6%
Taylor expanded in x around 0
Applied rewrites78.2%
(FPCore (c x y) :precision binary64 (if (<= c 1.5e+47) (* (* c y) x) (* (* x c) y)))
double code(double c, double x, double y) {
double tmp;
if (c <= 1.5e+47) {
tmp = (c * y) * x;
} else {
tmp = (x * c) * y;
}
return tmp;
}
real(8) function code(c, x, y)
real(8), intent (in) :: c
real(8), intent (in) :: x
real(8), intent (in) :: y
real(8) :: tmp
if (c <= 1.5d+47) then
tmp = (c * y) * x
else
tmp = (x * c) * y
end if
code = tmp
end function
public static double code(double c, double x, double y) {
double tmp;
if (c <= 1.5e+47) {
tmp = (c * y) * x;
} else {
tmp = (x * c) * y;
}
return tmp;
}
def code(c, x, y): tmp = 0 if c <= 1.5e+47: tmp = (c * y) * x else: tmp = (x * c) * y return tmp
function code(c, x, y) tmp = 0.0 if (c <= 1.5e+47) tmp = Float64(Float64(c * y) * x); else tmp = Float64(Float64(x * c) * y); end return tmp end
function tmp_2 = code(c, x, y) tmp = 0.0; if (c <= 1.5e+47) tmp = (c * y) * x; else tmp = (x * c) * y; end tmp_2 = tmp; end
code[c_, x_, y_] := If[LessEqual[c, 1.5e+47], N[(N[(c * y), $MachinePrecision] * x), $MachinePrecision], N[(N[(x * c), $MachinePrecision] * y), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;c \leq 1.5 \cdot 10^{+47}:\\
\;\;\;\;\left(c \cdot y\right) \cdot x\\
\mathbf{else}:\\
\;\;\;\;\left(x \cdot c\right) \cdot y\\
\end{array}
\end{array}
if c < 1.5000000000000001e47Initial program 43.1%
Taylor expanded in x around 0
associate-*r*N/A
log-EN/A
*-rgt-identityN/A
*-commutativeN/A
associate-*l*N/A
*-commutativeN/A
*-rgt-identityN/A
metadata-evalN/A
log-EN/A
log-EN/A
metadata-evalN/A
log-EN/A
lower-*.f64N/A
log-EN/A
*-rgt-identityN/A
lower-*.f6461.9
Applied rewrites61.9%
if 1.5000000000000001e47 < c Initial program 15.8%
Taylor expanded in x around 0
associate-*r*N/A
log-EN/A
*-rgt-identityN/A
*-commutativeN/A
associate-*l*N/A
*-commutativeN/A
*-rgt-identityN/A
metadata-evalN/A
log-EN/A
log-EN/A
metadata-evalN/A
log-EN/A
lower-*.f64N/A
log-EN/A
*-rgt-identityN/A
lower-*.f6448.5
Applied rewrites48.5%
Applied rewrites62.5%
(FPCore (c x y) :precision binary64 (* (* c y) x))
double code(double c, double x, double y) {
return (c * y) * x;
}
real(8) function code(c, x, y)
real(8), intent (in) :: c
real(8), intent (in) :: x
real(8), intent (in) :: y
code = (c * y) * x
end function
public static double code(double c, double x, double y) {
return (c * y) * x;
}
def code(c, x, y): return (c * y) * x
function code(c, x, y) return Float64(Float64(c * y) * x) end
function tmp = code(c, x, y) tmp = (c * y) * x; end
code[c_, x_, y_] := N[(N[(c * y), $MachinePrecision] * x), $MachinePrecision]
\begin{array}{l}
\\
\left(c \cdot y\right) \cdot x
\end{array}
Initial program 37.4%
Taylor expanded in x around 0
associate-*r*N/A
log-EN/A
*-rgt-identityN/A
*-commutativeN/A
associate-*l*N/A
*-commutativeN/A
*-rgt-identityN/A
metadata-evalN/A
log-EN/A
log-EN/A
metadata-evalN/A
log-EN/A
lower-*.f64N/A
log-EN/A
*-rgt-identityN/A
lower-*.f6459.1
Applied rewrites59.1%
(FPCore (c x y) :precision binary64 (* c (log1p (* (expm1 x) y))))
double code(double c, double x, double y) {
return c * log1p((expm1(x) * y));
}
public static double code(double c, double x, double y) {
return c * Math.log1p((Math.expm1(x) * y));
}
def code(c, x, y): return c * math.log1p((math.expm1(x) * y))
function code(c, x, y) return Float64(c * log1p(Float64(expm1(x) * y))) end
code[c_, x_, y_] := N[(c * N[Log[1 + N[(N[(Exp[x] - 1), $MachinePrecision] * y), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
c \cdot \mathsf{log1p}\left(\mathsf{expm1}\left(x\right) \cdot y\right)
\end{array}
herbie shell --seed 2024343
(FPCore (c x y)
:name "Logarithmic Transform"
:precision binary64
:alt
(* c (log1p (* (expm1 x) y)))
(* c (log (+ 1.0 (* (- (pow (E) x) 1.0) y)))))