
(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 12 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)) 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
(let* ((t_0 (/ (PI) s)) (t_1 (/ 1.0 (+ (exp t_0) 1.0))))
(if (<=
(*
(log
(-
(/ 1.0 (+ (* (- (/ 1.0 (+ (exp (/ (- (PI)) s)) 1.0)) t_1) u) t_1))
1.0))
(- s))
-1.999999936531045e-20)
(* (log (- 1.0 (/ (* (fma 2.0 u -1.0) (PI)) s))) (- s))
(*
(log
(-
(/ 1.0 (* (fma (/ (- 0.5 (* 0.25 t_0)) u) -1.0 (* -0.5 t_0)) (- u)))
1.0))
(- s)))))\begin{array}{l}
\\
\begin{array}{l}
t_0 := \frac{\mathsf{PI}\left(\right)}{s}\\
t_1 := \frac{1}{e^{t\_0} + 1}\\
\mathbf{if}\;\log \left(\frac{1}{\left(\frac{1}{e^{\frac{-\mathsf{PI}\left(\right)}{s}} + 1} - t\_1\right) \cdot u + t\_1} - 1\right) \cdot \left(-s\right) \leq -1.999999936531045 \cdot 10^{-20}:\\
\;\;\;\;\log \left(1 - \frac{\mathsf{fma}\left(2, u, -1\right) \cdot \mathsf{PI}\left(\right)}{s}\right) \cdot \left(-s\right)\\
\mathbf{else}:\\
\;\;\;\;\log \left(\frac{1}{\mathsf{fma}\left(\frac{0.5 - 0.25 \cdot t\_0}{u}, -1, -0.5 \cdot t\_0\right) \cdot \left(-u\right)} - 1\right) \cdot \left(-s\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-20Initial program 99.1%
Taylor expanded in s around -inf
mul-1-negN/A
unsub-negN/A
lower--.f32N/A
Applied rewrites11.9%
Taylor expanded in s around inf
Applied rewrites13.5%
if -1.99999994e-20 < (*.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 98.9%
Taylor expanded in s around -inf
mul-1-negN/A
unsub-negN/A
lower--.f32N/A
lower-/.f32N/A
Applied rewrites-0.0%
Applied rewrites-0.0%
Taylor expanded in u around -inf
Applied rewrites-0.0%
Final simplification8.7%
(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 rewrites98.0%
Final simplification98.0%
(FPCore (u s) :precision binary32 (* (log (- 1.0 (/ (* (* (fma -0.5 u 0.25) (PI)) -4.0) s))) (- s)))
\begin{array}{l}
\\
\log \left(1 - \frac{\left(\mathsf{fma}\left(-0.5, u, 0.25\right) \cdot \mathsf{PI}\left(\right)\right) \cdot -4}{s}\right) \cdot \left(-s\right)
\end{array}
Initial program 99.0%
Taylor expanded in s around -inf
mul-1-negN/A
unsub-negN/A
lower--.f32N/A
Applied rewrites14.7%
Applied rewrites12.1%
Taylor expanded in s around inf
Applied rewrites12.5%
Final simplification11.7%
(FPCore (u s) :precision binary32 (* (log (- 1.0 (/ (* (fma 2.0 u -1.0) (PI)) s))) (- s)))
\begin{array}{l}
\\
\log \left(1 - \frac{\mathsf{fma}\left(2, u, -1\right) \cdot \mathsf{PI}\left(\right)}{s}\right) \cdot \left(-s\right)
\end{array}
Initial program 99.0%
Taylor expanded in s around -inf
mul-1-negN/A
unsub-negN/A
lower--.f32N/A
Applied rewrites14.6%
Taylor expanded in s around inf
Applied rewrites11.0%
Final simplification13.1%
(FPCore (u s) :precision binary32 (* (* u u) (/ (+ (/ (PI) u) (* -2.0 (PI))) (- u))))
\begin{array}{l}
\\
\left(u \cdot u\right) \cdot \frac{\frac{\mathsf{PI}\left(\right)}{u} + -2 \cdot \mathsf{PI}\left(\right)}{-u}
\end{array}
Initial program 99.0%
Taylor expanded in s around -inf
Applied rewrites8.2%
Taylor expanded in u around -inf
Applied rewrites11.8%
Final simplification11.8%
(FPCore (u s) :precision binary32 (+ (* (* 0.25 (PI)) -4.0) (* (* (* (PI) u) -0.5) -4.0)))
\begin{array}{l}
\\
\left(0.25 \cdot \mathsf{PI}\left(\right)\right) \cdot -4 + \left(\left(\mathsf{PI}\left(\right) \cdot u\right) \cdot -0.5\right) \cdot -4
\end{array}
Initial program 99.0%
Taylor expanded in s around -inf
Applied rewrites7.8%
Applied rewrites7.4%
Applied rewrites11.8%
Final simplification11.8%
(FPCore (u s) :precision binary32 (fma -4.0 (* (* (PI) u) -0.5) (* (* 0.25 (PI)) -4.0)))
\begin{array}{l}
\\
\mathsf{fma}\left(-4, \left(\mathsf{PI}\left(\right) \cdot u\right) \cdot -0.5, \left(0.25 \cdot \mathsf{PI}\left(\right)\right) \cdot -4\right)
\end{array}
Initial program 99.0%
Taylor expanded in s around -inf
Applied rewrites8.6%
Applied rewrites7.2%
Applied rewrites11.6%
Final simplification11.9%
(FPCore (u s) :precision binary32 (* (fma (* -0.5 (PI)) u (* 0.25 (PI))) -4.0))
\begin{array}{l}
\\
\mathsf{fma}\left(-0.5 \cdot \mathsf{PI}\left(\right), u, 0.25 \cdot \mathsf{PI}\left(\right)\right) \cdot -4
\end{array}
Initial program 99.0%
Taylor expanded in s around -inf
Applied rewrites7.8%
Taylor expanded in s around 0
Applied rewrites10.2%
Taylor expanded in s around 0
Applied rewrites10.2%
Taylor expanded in s around -inf
*-commutativeN/A
lower-*.f32N/A
cancel-sign-sub-invN/A
*-commutativeN/A
distribute-rgt-out--N/A
metadata-evalN/A
*-commutativeN/A
metadata-evalN/A
lower-fma.f32N/A
lower-*.f32N/A
lower-PI.f32N/A
lower-*.f32N/A
lower-PI.f3211.6
Applied rewrites11.7%
(FPCore (u s) :precision binary32 (* (* -4.0 (PI)) (fma -0.5 u 0.25)))
\begin{array}{l}
\\
\left(-4 \cdot \mathsf{PI}\left(\right)\right) \cdot \mathsf{fma}\left(-0.5, u, 0.25\right)
\end{array}
Initial program 99.0%
Taylor expanded in s around -inf
Applied rewrites8.2%
Applied rewrites7.7%
Applied rewrites12.0%
Final simplification11.6%
(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.6
Applied rewrites11.6%
(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.1%
Taylor expanded in s around 0
Applied rewrites10.2%
Taylor expanded in s around 0
Applied rewrites10.2%
herbie shell --seed 2024271
(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))))