
(FPCore (x s) :precision binary32 (let* ((t_0 (exp (/ (- (fabs x)) s))) (t_1 (+ 1.0 t_0))) (/ t_0 (* (* s t_1) t_1))))
float code(float x, float s) {
float t_0 = expf((-fabsf(x) / s));
float t_1 = 1.0f + t_0;
return t_0 / ((s * t_1) * t_1);
}
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(4) function code(x, s)
use fmin_fmax_functions
real(4), intent (in) :: x
real(4), intent (in) :: s
real(4) :: t_0
real(4) :: t_1
t_0 = exp((-abs(x) / s))
t_1 = 1.0e0 + t_0
code = t_0 / ((s * t_1) * t_1)
end function
function code(x, s) t_0 = exp(Float32(Float32(-abs(x)) / s)) t_1 = Float32(Float32(1.0) + t_0) return Float32(t_0 / Float32(Float32(s * t_1) * t_1)) end
function tmp = code(x, s) t_0 = exp((-abs(x) / s)); t_1 = single(1.0) + t_0; tmp = t_0 / ((s * t_1) * t_1); end
\begin{array}{l}
\\
\begin{array}{l}
t_0 := e^{\frac{-\left|x\right|}{s}}\\
t_1 := 1 + t\_0\\
\frac{t\_0}{\left(s \cdot t\_1\right) \cdot t\_1}
\end{array}
\end{array}
Sampling outcomes in binary32 precision:
Herbie found 6 alternatives:
| Alternative | Accuracy | Speedup |
|---|
(FPCore (x s) :precision binary32 (let* ((t_0 (exp (/ (- (fabs x)) s))) (t_1 (+ 1.0 t_0))) (/ t_0 (* (* s t_1) t_1))))
float code(float x, float s) {
float t_0 = expf((-fabsf(x) / s));
float t_1 = 1.0f + t_0;
return t_0 / ((s * t_1) * t_1);
}
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(4) function code(x, s)
use fmin_fmax_functions
real(4), intent (in) :: x
real(4), intent (in) :: s
real(4) :: t_0
real(4) :: t_1
t_0 = exp((-abs(x) / s))
t_1 = 1.0e0 + t_0
code = t_0 / ((s * t_1) * t_1)
end function
function code(x, s) t_0 = exp(Float32(Float32(-abs(x)) / s)) t_1 = Float32(Float32(1.0) + t_0) return Float32(t_0 / Float32(Float32(s * t_1) * t_1)) end
function tmp = code(x, s) t_0 = exp((-abs(x) / s)); t_1 = single(1.0) + t_0; tmp = t_0 / ((s * t_1) * t_1); end
\begin{array}{l}
\\
\begin{array}{l}
t_0 := e^{\frac{-\left|x\right|}{s}}\\
t_1 := 1 + t\_0\\
\frac{t\_0}{\left(s \cdot t\_1\right) \cdot t\_1}
\end{array}
\end{array}
x_m = (fabs.f32 x) (FPCore (x_m s) :precision binary32 (let* ((t_0 (/ (- x_m) s))) (/ (exp (fma (log1p (exp t_0)) -2.0 t_0)) s)))
x_m = fabs(x);
float code(float x_m, float s) {
float t_0 = -x_m / s;
return expf(fmaf(log1pf(expf(t_0)), -2.0f, t_0)) / s;
}
x_m = abs(x) function code(x_m, s) t_0 = Float32(Float32(-x_m) / s) return Float32(exp(fma(log1p(exp(t_0)), Float32(-2.0), t_0)) / s) end
\begin{array}{l}
x_m = \left|x\right|
\\
\begin{array}{l}
t_0 := \frac{-x\_m}{s}\\
\frac{e^{\mathsf{fma}\left(\mathsf{log1p}\left(e^{t\_0}\right), -2, t\_0\right)}}{s}
\end{array}
\end{array}
Initial program 99.4%
lift-/.f32N/A
lift-*.f32N/A
lift-*.f32N/A
associate-*l*N/A
*-commutativeN/A
associate-/r*N/A
lower-/.f32N/A
Applied rewrites86.2%
Taylor expanded in x around inf
lower-exp.f32N/A
distribute-neg-inN/A
*-commutativeN/A
distribute-rgt-neg-inN/A
metadata-evalN/A
mul-1-negN/A
lower-fma.f32N/A
lower-log1p.f32N/A
lower-exp.f32N/A
associate-*r/N/A
mul-1-negN/A
lower-/.f32N/A
lower-neg.f32N/A
associate-*r/N/A
mul-1-negN/A
lower-/.f32N/A
lower-neg.f3286.2
Applied rewrites86.2%
x_m = (fabs.f32 x) (FPCore (x_m s) :precision binary32 (/ (exp (fma (/ -0.25 s) (* x_m (/ x_m s)) (* (log 2.0) -2.0))) s))
x_m = fabs(x);
float code(float x_m, float s) {
return expf(fmaf((-0.25f / s), (x_m * (x_m / s)), (logf(2.0f) * -2.0f))) / s;
}
x_m = abs(x) function code(x_m, s) return Float32(exp(fma(Float32(Float32(-0.25) / s), Float32(x_m * Float32(x_m / s)), Float32(log(Float32(2.0)) * Float32(-2.0)))) / s) end
\begin{array}{l}
x_m = \left|x\right|
\\
\frac{e^{\mathsf{fma}\left(\frac{-0.25}{s}, x\_m \cdot \frac{x\_m}{s}, \log 2 \cdot -2\right)}}{s}
\end{array}
Initial program 99.4%
lift-/.f32N/A
lift-*.f32N/A
lift-*.f32N/A
associate-*l*N/A
*-commutativeN/A
associate-/r*N/A
lower-/.f32N/A
Applied rewrites86.2%
Taylor expanded in x around 0
fp-cancel-sub-sign-invN/A
metadata-evalN/A
associate-*r/N/A
unpow2N/A
times-fracN/A
metadata-evalN/A
metadata-evalN/A
exp-to-powN/A
*-commutativeN/A
metadata-evalN/A
distribute-lft-neg-inN/A
mul-1-negN/A
lower-fma.f32N/A
Applied rewrites96.4%
x_m = (fabs.f32 x) (FPCore (x_m s) :precision binary32 (/ (exp (/ (- (fabs x_m)) s)) (* (+ (/ (fma (* x_m (/ x_m s)) 3.0 (* -4.0 (fabs x_m))) s) 4.0) s)))
x_m = fabs(x);
float code(float x_m, float s) {
return expf((-fabsf(x_m) / s)) / (((fmaf((x_m * (x_m / s)), 3.0f, (-4.0f * fabsf(x_m))) / s) + 4.0f) * s);
}
x_m = abs(x) function code(x_m, s) return Float32(exp(Float32(Float32(-abs(x_m)) / s)) / Float32(Float32(Float32(fma(Float32(x_m * Float32(x_m / s)), Float32(3.0), Float32(Float32(-4.0) * abs(x_m))) / s) + Float32(4.0)) * s)) end
\begin{array}{l}
x_m = \left|x\right|
\\
\frac{e^{\frac{-\left|x\_m\right|}{s}}}{\left(\frac{\mathsf{fma}\left(x\_m \cdot \frac{x\_m}{s}, 3, -4 \cdot \left|x\_m\right|\right)}{s} + 4\right) \cdot s}
\end{array}
Initial program 99.4%
Taylor expanded in s around inf
lower-*.f3294.3
Applied rewrites94.3%
Taylor expanded in s around inf
*-commutativeN/A
lower-*.f32N/A
Applied rewrites95.7%
x_m = (fabs.f32 x) (FPCore (x_m s) :precision binary32 (/ (exp (/ (- x_m) s)) (* 4.0 s)))
x_m = fabs(x);
float code(float x_m, float s) {
return expf((-x_m / s)) / (4.0f * s);
}
x_m = private
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(4) function code(x_m, s)
use fmin_fmax_functions
real(4), intent (in) :: x_m
real(4), intent (in) :: s
code = exp((-x_m / s)) / (4.0e0 * s)
end function
x_m = abs(x) function code(x_m, s) return Float32(exp(Float32(Float32(-x_m) / s)) / Float32(Float32(4.0) * s)) end
x_m = abs(x); function tmp = code(x_m, s) tmp = exp((-x_m / s)) / (single(4.0) * s); end
\begin{array}{l}
x_m = \left|x\right|
\\
\frac{e^{\frac{-x\_m}{s}}}{4 \cdot s}
\end{array}
Initial program 99.4%
Taylor expanded in s around inf
lower-*.f3294.3
Applied rewrites94.3%
lift-fabs.f32N/A
rem-sqrt-square-revN/A
pow2N/A
sqrt-pow1N/A
metadata-evalN/A
unpow162.3
Applied rewrites62.3%
x_m = (fabs.f32 x) (FPCore (x_m s) :precision binary32 (/ (fma (/ x_m s) -0.25 (fma 0.25 (/ (fabs x_m) s) 0.25)) s))
x_m = fabs(x);
float code(float x_m, float s) {
return fmaf((x_m / s), -0.25f, fmaf(0.25f, (fabsf(x_m) / s), 0.25f)) / s;
}
x_m = abs(x) function code(x_m, s) return Float32(fma(Float32(x_m / s), Float32(-0.25), fma(Float32(0.25), Float32(abs(x_m) / s), Float32(0.25))) / s) end
\begin{array}{l}
x_m = \left|x\right|
\\
\frac{\mathsf{fma}\left(\frac{x\_m}{s}, -0.25, \mathsf{fma}\left(0.25, \frac{\left|x\_m\right|}{s}, 0.25\right)\right)}{s}
\end{array}
Initial program 99.4%
Taylor expanded in s around inf
lower-*.f3294.3
Applied rewrites94.3%
lift-fabs.f32N/A
rem-sqrt-square-revN/A
pow2N/A
sqrt-pow1N/A
metadata-evalN/A
unpow162.3
Applied rewrites62.3%
Taylor expanded in s around inf
lower-/.f32N/A
fp-cancel-sub-sign-invN/A
+-commutativeN/A
metadata-evalN/A
associate-+l+N/A
*-commutativeN/A
lower-fma.f32N/A
lower-/.f32N/A
+-commutativeN/A
lower-fma.f32N/A
lower-/.f32N/A
lower-fabs.f3248.6
Applied rewrites48.6%
x_m = (fabs.f32 x) (FPCore (x_m s) :precision binary32 (/ 0.25 s))
x_m = fabs(x);
float code(float x_m, float s) {
return 0.25f / s;
}
x_m = private
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(4) function code(x_m, s)
use fmin_fmax_functions
real(4), intent (in) :: x_m
real(4), intent (in) :: s
code = 0.25e0 / s
end function
x_m = abs(x) function code(x_m, s) return Float32(Float32(0.25) / s) end
x_m = abs(x); function tmp = code(x_m, s) tmp = single(0.25) / s; end
\begin{array}{l}
x_m = \left|x\right|
\\
\frac{0.25}{s}
\end{array}
Initial program 99.4%
Taylor expanded in s around inf
lower-/.f3222.9
Applied rewrites22.9%
herbie shell --seed 2024356
(FPCore (x s)
:name "Logistic distribution"
:precision binary32
:pre (and (<= 0.0 s) (<= s 1.0651631))
(/ (exp (/ (- (fabs x)) s)) (* (* s (+ 1.0 (exp (/ (- (fabs x)) s)))) (+ 1.0 (exp (/ (- (fabs x)) s))))))