
(FPCore (x y) :precision binary64 (- (log (+ 1.0 (exp x))) (* x y)))
double code(double x, double y) {
return log((1.0 + exp(x))) - (x * y);
}
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, y)
use fmin_fmax_functions
real(8), intent (in) :: x
real(8), intent (in) :: y
code = log((1.0d0 + exp(x))) - (x * y)
end function
public static double code(double x, double y) {
return Math.log((1.0 + Math.exp(x))) - (x * y);
}
def code(x, y): return math.log((1.0 + math.exp(x))) - (x * y)
function code(x, y) return Float64(log(Float64(1.0 + exp(x))) - Float64(x * y)) end
function tmp = code(x, y) tmp = log((1.0 + exp(x))) - (x * y); end
code[x_, y_] := N[(N[Log[N[(1.0 + N[Exp[x], $MachinePrecision]), $MachinePrecision]], $MachinePrecision] - N[(x * y), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\log \left(1 + e^{x}\right) - x \cdot y
\end{array}
Sampling outcomes in binary64 precision:
Herbie found 4 alternatives:
| Alternative | Accuracy | Speedup |
|---|
(FPCore (x y) :precision binary64 (- (log (+ 1.0 (exp x))) (* x y)))
double code(double x, double y) {
return log((1.0 + exp(x))) - (x * y);
}
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, y)
use fmin_fmax_functions
real(8), intent (in) :: x
real(8), intent (in) :: y
code = log((1.0d0 + exp(x))) - (x * y)
end function
public static double code(double x, double y) {
return Math.log((1.0 + Math.exp(x))) - (x * y);
}
def code(x, y): return math.log((1.0 + math.exp(x))) - (x * y)
function code(x, y) return Float64(log(Float64(1.0 + exp(x))) - Float64(x * y)) end
function tmp = code(x, y) tmp = log((1.0 + exp(x))) - (x * y); end
code[x_, y_] := N[(N[Log[N[(1.0 + N[Exp[x], $MachinePrecision]), $MachinePrecision]], $MachinePrecision] - N[(x * y), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\log \left(1 + e^{x}\right) - x \cdot y
\end{array}
(FPCore (x y) :precision binary64 (fma (* -1.0 y) x (log1p (exp x))))
double code(double x, double y) {
return fma((-1.0 * y), x, log1p(exp(x)));
}
function code(x, y) return fma(Float64(-1.0 * y), x, log1p(exp(x))) end
code[x_, y_] := N[(N[(-1.0 * y), $MachinePrecision] * x + N[Log[1 + N[Exp[x], $MachinePrecision]], $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\mathsf{fma}\left(-1 \cdot y, x, \mathsf{log1p}\left(e^{x}\right)\right)
\end{array}
Initial program 99.6%
lift--.f64N/A
lift-log.f64N/A
lift-+.f64N/A
lift-exp.f64N/A
lift-*.f64N/A
fp-cancel-sub-sign-invN/A
mul-1-negN/A
associate-*r*N/A
+-commutativeN/A
*-commutativeN/A
associate-*r*N/A
lower-fma.f64N/A
lower-*.f64N/A
lower-log1p.f64N/A
lift-exp.f6499.6
Applied rewrites99.6%
(FPCore (x y) :precision binary64 (- (log (+ 1.0 (exp x))) (* x y)))
double code(double x, double y) {
return log((1.0 + exp(x))) - (x * y);
}
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, y)
use fmin_fmax_functions
real(8), intent (in) :: x
real(8), intent (in) :: y
code = log((1.0d0 + exp(x))) - (x * y)
end function
public static double code(double x, double y) {
return Math.log((1.0 + Math.exp(x))) - (x * y);
}
def code(x, y): return math.log((1.0 + math.exp(x))) - (x * y)
function code(x, y) return Float64(log(Float64(1.0 + exp(x))) - Float64(x * y)) end
function tmp = code(x, y) tmp = log((1.0 + exp(x))) - (x * y); end
code[x_, y_] := N[(N[Log[N[(1.0 + N[Exp[x], $MachinePrecision]), $MachinePrecision]], $MachinePrecision] - N[(x * y), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\log \left(1 + e^{x}\right) - x \cdot y
\end{array}
Initial program 99.6%
(FPCore (x y) :precision binary64 (- (fma (* 0.5 x) 1.0 (log 2.0)) (* x y)))
double code(double x, double y) {
return fma((0.5 * x), 1.0, log(2.0)) - (x * y);
}
function code(x, y) return Float64(fma(Float64(0.5 * x), 1.0, log(2.0)) - Float64(x * y)) end
code[x_, y_] := N[(N[(N[(0.5 * x), $MachinePrecision] * 1.0 + N[Log[2.0], $MachinePrecision]), $MachinePrecision] - N[(x * y), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\mathsf{fma}\left(0.5 \cdot x, 1, \log 2\right) - x \cdot y
\end{array}
Initial program 99.6%
Taylor expanded in x around 0
+-commutativeN/A
*-lft-identityN/A
*-commutativeN/A
lower-fma.f64N/A
lower-*.f64N/A
lower-log.f6485.9
Applied rewrites85.9%
(FPCore (x y) :precision binary64 (- (- (fma (* 0.5 x) 1.0 (log 2.0)) (* -0.5 x)) (* x y)))
double code(double x, double y) {
return (fma((0.5 * x), 1.0, log(2.0)) - (-0.5 * x)) - (x * y);
}
function code(x, y) return Float64(Float64(fma(Float64(0.5 * x), 1.0, log(2.0)) - Float64(-0.5 * x)) - Float64(x * y)) end
code[x_, y_] := N[(N[(N[(N[(0.5 * x), $MachinePrecision] * 1.0 + N[Log[2.0], $MachinePrecision]), $MachinePrecision] - N[(-0.5 * x), $MachinePrecision]), $MachinePrecision] - N[(x * y), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\left(\mathsf{fma}\left(0.5 \cdot x, 1, \log 2\right) - -0.5 \cdot x\right) - x \cdot y
\end{array}
Initial program 99.6%
Taylor expanded in x around 0
+-commutativeN/A
*-lft-identityN/A
*-commutativeN/A
lower-fma.f64N/A
lower-*.f64N/A
lower-log.f6485.9
Applied rewrites85.9%
lift-*.f64N/A
lift-log.f64N/A
lift-fma.f64N/A
*-rgt-identityN/A
+-commutativeN/A
flip-+N/A
div-subN/A
lower--.f64N/A
Applied rewrites73.2%
Taylor expanded in x around 0
+-commutativeN/A
*-rgt-identityN/A
lift-fma.f64N/A
lift-*.f64N/A
lift-log.f6473.3
Applied rewrites73.3%
Taylor expanded in x around inf
lower-*.f6485.5
Applied rewrites85.5%
herbie shell --seed 2025066
(FPCore (x y)
:name "Logistic regression 2"
:precision binary64
:alt
(! :herbie-platform c (if (<= x 0) (- (log (+ 1 (exp x))) (* x y)) (- (log (+ 1 (exp (- x)))) (* (- x) (- 1 y)))))
(- (log (+ 1.0 (exp x))) (* x y)))