
(FPCore (u s)
:precision binary32
(let* ((t_0 (/ 1.0 (+ 1.0 (exp (/ (PI) s))))))
(*
(- s)
(log
(-
(/ 1.0 (+ (* u (- (/ 1.0 (+ 1.0 (exp (/ (- (PI)) s)))) t_0)) t_0))
1.0)))))\begin{array}{l}
\\
\begin{array}{l}
t_0 := \frac{1}{1 + e^{\frac{\mathsf{PI}\left(\right)}{s}}}\\
\left(-s\right) \cdot \log \left(\frac{1}{u \cdot \left(\frac{1}{1 + e^{\frac{-\mathsf{PI}\left(\right)}{s}}} - t\_0\right) + t\_0} - 1\right)
\end{array}
\end{array}
Sampling outcomes in binary32 precision:
Herbie found 11 alternatives:
| Alternative | Accuracy | Speedup |
|---|
(FPCore (u s)
:precision binary32
(let* ((t_0 (/ 1.0 (+ 1.0 (exp (/ (PI) s))))))
(*
(- s)
(log
(-
(/ 1.0 (+ (* u (- (/ 1.0 (+ 1.0 (exp (/ (- (PI)) s)))) t_0)) t_0))
1.0)))))\begin{array}{l}
\\
\begin{array}{l}
t_0 := \frac{1}{1 + e^{\frac{\mathsf{PI}\left(\right)}{s}}}\\
\left(-s\right) \cdot \log \left(\frac{1}{u \cdot \left(\frac{1}{1 + e^{\frac{-\mathsf{PI}\left(\right)}{s}}} - t\_0\right) + t\_0} - 1\right)
\end{array}
\end{array}
(FPCore (u s)
:precision binary32
(let* ((t_0 (/ 1.0 (+ (exp (/ (PI) s)) 1.0))))
(*
(log
(-
(/ 1.0 (+ (* (- (/ 1.0 (+ (exp (* (PI) (/ -1.0 s))) 1.0)) t_0) u) t_0))
1.0))
(- s))))\begin{array}{l}
\\
\begin{array}{l}
t_0 := \frac{1}{e^{\frac{\mathsf{PI}\left(\right)}{s}} + 1}\\
\log \left(\frac{1}{\left(\frac{1}{e^{\mathsf{PI}\left(\right) \cdot \frac{-1}{s}} + 1} - t\_0\right) \cdot u + t\_0} - 1\right) \cdot \left(-s\right)
\end{array}
\end{array}
Initial program 99.0%
lift-/.f32N/A
clear-numN/A
associate-/r/N/A
lower-*.f32N/A
lower-/.f3299.0
Applied rewrites99.0%
lift-*.f32N/A
lift-/.f32N/A
associate-*l/N/A
*-lft-identityN/A
lift-neg.f32N/A
/-rgt-identityN/A
clear-numN/A
inv-powN/A
pow-flipN/A
metadata-evalN/A
metadata-evalN/A
pow-divN/A
sqr-powN/A
unpow-prod-downN/A
sqr-negN/A
lift-neg.f32N/A
lift-neg.f32N/A
pow-prod-downN/A
sqr-powN/A
pow2N/A
sqr-negN/A
lift-neg.f32N/A
lift-neg.f32N/A
pow2N/A
pow-divN/A
metadata-evalN/A
unpow1N/A
lift-neg.f32N/A
Applied rewrites99.0%
Final simplification99.0%
(FPCore (u s)
:precision binary32
(let* ((t_0 (- (PI)))
(t_1 (* (PI) u))
(t_2 (/ 1.0 (+ (exp (/ (PI) s)) 1.0)))
(t_3 (* (fma 0.5 u -0.25) (PI))))
(if (<=
(*
(log
(-
(/ 1.0 (+ (* (- (/ 1.0 (+ (exp (/ t_0 s)) 1.0)) t_2) u) t_2))
1.0))
(- s))
-1.999999936531045e-19)
(fma (/ (* (* u u) 0.0) s) -0.5 (fma t_1 2.0 t_0))
(fma
(/ (fma (* t_3 16.0) t_3 (* -16.0 (pow t_3 2.0))) s)
-0.5
(* (fma t_1 0.5 (* -0.25 (PI))) 4.0)))))\begin{array}{l}
\\
\begin{array}{l}
t_0 := -\mathsf{PI}\left(\right)\\
t_1 := \mathsf{PI}\left(\right) \cdot u\\
t_2 := \frac{1}{e^{\frac{\mathsf{PI}\left(\right)}{s}} + 1}\\
t_3 := \mathsf{fma}\left(0.5, u, -0.25\right) \cdot \mathsf{PI}\left(\right)\\
\mathbf{if}\;\log \left(\frac{1}{\left(\frac{1}{e^{\frac{t\_0}{s}} + 1} - t\_2\right) \cdot u + t\_2} - 1\right) \cdot \left(-s\right) \leq -1.999999936531045 \cdot 10^{-19}:\\
\;\;\;\;\mathsf{fma}\left(\frac{\left(u \cdot u\right) \cdot 0}{s}, -0.5, \mathsf{fma}\left(t\_1, 2, t\_0\right)\right)\\
\mathbf{else}:\\
\;\;\;\;\mathsf{fma}\left(\frac{\mathsf{fma}\left(t\_3 \cdot 16, t\_3, -16 \cdot {t\_3}^{2}\right)}{s}, -0.5, \mathsf{fma}\left(t\_1, 0.5, -0.25 \cdot \mathsf{PI}\left(\right)\right) \cdot 4\right)\\
\end{array}
\end{array}
if (*.f32 (neg.f32 s) (log.f32 (-.f32 (/.f32 #s(literal 1 binary32) (+.f32 (*.f32 u (-.f32 (/.f32 #s(literal 1 binary32) (+.f32 #s(literal 1 binary32) (exp.f32 (/.f32 (neg.f32 (PI.f32)) s)))) (/.f32 #s(literal 1 binary32) (+.f32 #s(literal 1 binary32) (exp.f32 (/.f32 (PI.f32) s)))))) (/.f32 #s(literal 1 binary32) (+.f32 #s(literal 1 binary32) (exp.f32 (/.f32 (PI.f32) s)))))) #s(literal 1 binary32)))) < -1.99999994e-19Initial program 99.0%
Taylor expanded in s around inf
Applied rewrites9.9%
Taylor expanded in u around inf
Applied rewrites15.6%
Applied rewrites8.8%
if -1.99999994e-19 < (*.f32 (neg.f32 s) (log.f32 (-.f32 (/.f32 #s(literal 1 binary32) (+.f32 (*.f32 u (-.f32 (/.f32 #s(literal 1 binary32) (+.f32 #s(literal 1 binary32) (exp.f32 (/.f32 (neg.f32 (PI.f32)) s)))) (/.f32 #s(literal 1 binary32) (+.f32 #s(literal 1 binary32) (exp.f32 (/.f32 (PI.f32) s)))))) (/.f32 #s(literal 1 binary32) (+.f32 #s(literal 1 binary32) (exp.f32 (/.f32 (PI.f32) s)))))) #s(literal 1 binary32)))) Initial program 99.0%
Taylor expanded in s around inf
Applied rewrites6.3%
Taylor expanded in u around 0
Applied rewrites6.0%
Applied rewrites8.1%
Final simplification7.9%
(FPCore (u s)
:precision binary32
(let* ((t_0 (/ 1.0 (+ (exp (/ (PI) s)) 1.0))))
(*
(log
(-
(/ 1.0 (+ (* (- (/ 1.0 (+ (exp (/ (- (PI)) s)) 1.0)) t_0) u) t_0))
1.0))
(- s))))\begin{array}{l}
\\
\begin{array}{l}
t_0 := \frac{1}{e^{\frac{\mathsf{PI}\left(\right)}{s}} + 1}\\
\log \left(\frac{1}{\left(\frac{1}{e^{\frac{-\mathsf{PI}\left(\right)}{s}} + 1} - t\_0\right) \cdot u + t\_0} - 1\right) \cdot \left(-s\right)
\end{array}
\end{array}
Initial program 99.0%
Final simplification99.0%
(FPCore (u s)
:precision binary32
(*
(log
(-
(/
1.0
(*
(- (/ 1.0 (+ (exp (/ (- (PI)) s)) 1.0)) (/ 1.0 (+ (exp (/ (PI) s)) 1.0)))
u))
1.0))
(- s)))\begin{array}{l}
\\
\log \left(\frac{1}{\left(\frac{1}{e^{\frac{-\mathsf{PI}\left(\right)}{s}} + 1} - \frac{1}{e^{\frac{\mathsf{PI}\left(\right)}{s}} + 1}\right) \cdot u} - 1\right) \cdot \left(-s\right)
\end{array}
Initial program 99.0%
Taylor expanded in u around inf
*-commutativeN/A
lower-*.f32N/A
Applied rewrites97.8%
Final simplification97.8%
(FPCore (u s)
:precision binary32
(let* ((t_0 (fma (* -0.5 (PI)) u (* 0.25 (PI)))))
(*
(log
(- 1.0 (/ (- (fma (/ (pow t_0 2.0) s) -8.0 (/ 0.0 s)) (* 4.0 t_0)) s)))
(- s))))\begin{array}{l}
\\
\begin{array}{l}
t_0 := \mathsf{fma}\left(-0.5 \cdot \mathsf{PI}\left(\right), u, 0.25 \cdot \mathsf{PI}\left(\right)\right)\\
\log \left(1 - \frac{\mathsf{fma}\left(\frac{{t\_0}^{2}}{s}, -8, \frac{0}{s}\right) - 4 \cdot t\_0}{s}\right) \cdot \left(-s\right)
\end{array}
\end{array}
Initial program 99.0%
Taylor expanded in s around inf
Applied rewrites10.4%
Taylor expanded in s around -inf
Applied rewrites13.7%
Final simplification12.8%
(FPCore (u s)
:precision binary32
(let* ((t_0 (* (PI) u)) (t_1 (* (fma 0.5 u -0.25) (PI))) (t_2 (pow t_1 2.0)))
(if (<= s 1.9999999996399175e-23)
(fma
(/ (fma (* t_1 16.0) t_1 (* -16.0 t_2)) s)
-0.5
(* (fma t_0 0.5 (* -0.25 (PI))) 4.0))
(*
(log
(- (- 1.0 (/ (fma -8.0 t_2 0.0) (* s s))) (/ (fma t_0 2.0 (- (PI))) s)))
(- s)))))\begin{array}{l}
\\
\begin{array}{l}
t_0 := \mathsf{PI}\left(\right) \cdot u\\
t_1 := \mathsf{fma}\left(0.5, u, -0.25\right) \cdot \mathsf{PI}\left(\right)\\
t_2 := {t\_1}^{2}\\
\mathbf{if}\;s \leq 1.9999999996399175 \cdot 10^{-23}:\\
\;\;\;\;\mathsf{fma}\left(\frac{\mathsf{fma}\left(t\_1 \cdot 16, t\_1, -16 \cdot t\_2\right)}{s}, -0.5, \mathsf{fma}\left(t\_0, 0.5, -0.25 \cdot \mathsf{PI}\left(\right)\right) \cdot 4\right)\\
\mathbf{else}:\\
\;\;\;\;\log \left(\left(1 - \frac{\mathsf{fma}\left(-8, t\_2, 0\right)}{s \cdot s}\right) - \frac{\mathsf{fma}\left(t\_0, 2, -\mathsf{PI}\left(\right)\right)}{s}\right) \cdot \left(-s\right)\\
\end{array}
\end{array}
if s < 2e-23Initial program 99.0%
Taylor expanded in s around inf
Applied rewrites6.2%
Taylor expanded in u around 0
Applied rewrites6.9%
Applied rewrites7.9%
if 2e-23 < s Initial program 99.0%
Taylor expanded in s around inf
Applied rewrites7.8%
Taylor expanded in s around inf
Applied rewrites14.6%
Final simplification9.5%
(FPCore (u s) :precision binary32 (fma (/ (* (* u u) 0.0) s) -0.5 (* (- (* 2.0 (PI)) (/ (PI) u)) u)))
\begin{array}{l}
\\
\mathsf{fma}\left(\frac{\left(u \cdot u\right) \cdot 0}{s}, -0.5, \left(2 \cdot \mathsf{PI}\left(\right) - \frac{\mathsf{PI}\left(\right)}{u}\right) \cdot u\right)
\end{array}
Initial program 99.0%
Taylor expanded in s around inf
Applied rewrites8.1%
Taylor expanded in u around inf
Applied rewrites11.5%
Taylor expanded in u around inf
Applied rewrites11.6%
Final simplification11.7%
(FPCore (u s) :precision binary32 (fma (/ (* (* u u) 0.0) s) -0.5 (fma (* (PI) u) 2.0 (- (PI)))))
\begin{array}{l}
\\
\mathsf{fma}\left(\frac{\left(u \cdot u\right) \cdot 0}{s}, -0.5, \mathsf{fma}\left(\mathsf{PI}\left(\right) \cdot u, 2, -\mathsf{PI}\left(\right)\right)\right)
\end{array}
Initial program 99.0%
Taylor expanded in s around inf
Applied rewrites8.2%
Taylor expanded in u around inf
Applied rewrites11.5%
Applied rewrites8.2%
Final simplification11.5%
(FPCore (u s) :precision binary32 (- (* (* (PI) u) 2.0) (PI)))
\begin{array}{l}
\\
\left(\mathsf{PI}\left(\right) \cdot u\right) \cdot 2 - \mathsf{PI}\left(\right)
\end{array}
Initial program 99.0%
Taylor expanded in s around inf
Applied rewrites7.8%
Applied rewrites8.3%
Applied rewrites11.7%
Final simplification11.7%
(FPCore (u s) :precision binary32 (- (PI)))
\begin{array}{l}
\\
-\mathsf{PI}\left(\right)
\end{array}
Initial program 99.0%
Taylor expanded in u around 0
mul-1-negN/A
lower-neg.f32N/A
lower-PI.f3211.5
Applied rewrites11.5%
(FPCore (u s) :precision binary32 0.0)
float code(float u, float s) {
return 0.0f;
}
real(4) function code(u, s)
real(4), intent (in) :: u
real(4), intent (in) :: s
code = 0.0e0
end function
function code(u, s) return Float32(0.0) end
function tmp = code(u, s) tmp = single(0.0); end
\begin{array}{l}
\\
0
\end{array}
Initial program 99.0%
Taylor expanded in s around inf
Applied rewrites8.3%
Taylor expanded in s around 0
Applied rewrites10.4%
Taylor expanded in s around 0
Applied rewrites10.4%
herbie shell --seed 2024283
(FPCore (u s)
:name "Sample trimmed logistic on [-pi, pi]"
:precision binary32
:pre (and (and (<= 2.328306437e-10 u) (<= u 1.0)) (and (<= 0.0 s) (<= s 1.0651631)))
(* (- s) (log (- (/ 1.0 (+ (* u (- (/ 1.0 (+ 1.0 (exp (/ (- (PI)) s)))) (/ 1.0 (+ 1.0 (exp (/ (PI) s)))))) (/ 1.0 (+ 1.0 (exp (/ (PI) s)))))) 1.0))))