
(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 (- (* (- t_0 (/ -1.0 (+ (exp (/ (- (PI)) s)) 1.0))) u) t_0))))
(- s))))\begin{array}{l}
\\
\begin{array}{l}
t_0 := \frac{-1}{e^{\frac{\mathsf{PI}\left(\right)}{s}} + 1}\\
\log \left(-1 - \frac{-1}{\left(t\_0 - \frac{-1}{e^{\frac{-\mathsf{PI}\left(\right)}{s}} + 1}\right) \cdot u - t\_0}\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
(- (* (- t_1 (/ -1.0 (+ (exp (/ (- (PI)) s)) 1.0))) u) t_1))))
(- s))
-2.9999998795923744e-13)
(* (log (* (- (/ (+ t_0 1.0) u) (* 2.0 t_0)) u)) (- s))
(*
(log
(fma
(fma (* (PI) u) -0.5 (* (pow (* (PI) (PI)) 0.5) 0.25))
(/ 4.0 s)
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(-1 - \frac{-1}{\left(t\_1 - \frac{-1}{e^{\frac{-\mathsf{PI}\left(\right)}{s}} + 1}\right) \cdot u - t\_1}\right) \cdot \left(-s\right) \leq -2.9999998795923744 \cdot 10^{-13}:\\
\;\;\;\;\log \left(\left(\frac{t\_0 + 1}{u} - 2 \cdot t\_0\right) \cdot u\right) \cdot \left(-s\right)\\
\mathbf{else}:\\
\;\;\;\;\log \left(\mathsf{fma}\left(\mathsf{fma}\left(\mathsf{PI}\left(\right) \cdot u, -0.5, {\left(\mathsf{PI}\left(\right) \cdot \mathsf{PI}\left(\right)\right)}^{0.5} \cdot 0.25\right), \frac{4}{s}, 1\right)\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)))) < -2.99999988e-13Initial program 99.2%
Taylor expanded in s around -inf
associate-*r/N/A
+-commutativeN/A
*-commutativeN/A
associate-/l*N/A
lower-fma.f32N/A
Applied rewrites7.1%
Taylor expanded in u around -inf
Applied rewrites29.2%
if -2.99999988e-13 < (*.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.8%
Taylor expanded in s around -inf
associate-*r/N/A
+-commutativeN/A
*-commutativeN/A
associate-/l*N/A
lower-fma.f32N/A
Applied rewrites12.0%
Applied rewrites29.1%
Final simplification29.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 rewrites98.4%
Final simplification98.4%
(FPCore (u s)
:precision binary32
(*
(log
(-
(/
1.0
(*
(-
(/ -1.0 (+ (- 1.0 (/ (- (* -0.5 (/ (* (PI) (PI)) s)) (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}{\left(1 - \frac{-0.5 \cdot \frac{\mathsf{PI}\left(\right) \cdot \mathsf{PI}\left(\right)}{s} - \mathsf{PI}\left(\right)}{s}\right) + 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.4%
Taylor expanded in s around -inf
Applied rewrites97.4%
Final simplification97.4%
(FPCore (u s)
:precision binary32
(*
(log
(-
(/
1.0
(*
(-
(/ -1.0 (+ (+ (/ (PI) s) 1.0) 1.0))
(/ -1.0 (+ (exp (/ (- (PI)) s)) 1.0)))
u))
1.0))
(- s)))\begin{array}{l}
\\
\log \left(\frac{1}{\left(\frac{-1}{\left(\frac{\mathsf{PI}\left(\right)}{s} + 1\right) + 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.4%
Taylor expanded in s around inf
Applied rewrites95.0%
Final simplification95.0%
(FPCore (u s)
:precision binary32
(*
(log
(-
(/
1.0
(* (- (/ -1.0 (+ 1.0 1.0)) (/ -1.0 (+ (exp (/ (- (PI)) s)) 1.0))) u))
1.0))
(- s)))\begin{array}{l}
\\
\log \left(\frac{1}{\left(\frac{-1}{1 + 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.4%
Taylor expanded in s around inf
Applied rewrites37.6%
Final simplification37.6%
(FPCore (u s) :precision binary32 (* (log (+ (/ (PI) s) 1.0)) (- s)))
\begin{array}{l}
\\
\log \left(\frac{\mathsf{PI}\left(\right)}{s} + 1\right) \cdot \left(-s\right)
\end{array}
Initial program 99.0%
Taylor expanded in s around -inf
associate-*r/N/A
+-commutativeN/A
*-commutativeN/A
associate-/l*N/A
lower-fma.f32N/A
Applied rewrites10.2%
Taylor expanded in u around 0
Applied rewrites25.5%
Final simplification25.5%
(FPCore (u s) :precision binary32 (fma 4.0 (* 0.25 (PI)) (* (* (* -0.5 u) (PI)) 4.0)))
\begin{array}{l}
\\
\mathsf{fma}\left(4, 0.25 \cdot \mathsf{PI}\left(\right), \left(\left(-0.5 \cdot u\right) \cdot \mathsf{PI}\left(\right)\right) \cdot 4\right)
\end{array}
Initial program 99.0%
Taylor expanded in s around -inf
Applied rewrites8.1%
Applied rewrites11.1%
Applied rewrites11.1%
Applied rewrites12.6%
Final simplification12.6%
(FPCore (u s) :precision binary32 (* (- (* 2.0 (PI)) (/ (PI) u)) u))
\begin{array}{l}
\\
\left(2 \cdot \mathsf{PI}\left(\right) - \frac{\mathsf{PI}\left(\right)}{u}\right) \cdot u
\end{array}
Initial program 99.0%
Taylor expanded in s around -inf
Applied rewrites8.3%
Applied rewrites11.1%
Taylor expanded in u around inf
Applied rewrites11.3%
(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.4%
Applied rewrites11.1%
Applied rewrites11.1%
Taylor expanded in u around 0
Applied rewrites11.3%
(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.1
Applied rewrites11.1%
(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 rewrites7.7%
Taylor expanded in s around 0
Applied rewrites10.2%
Taylor expanded in s around 0
Applied rewrites10.2%
herbie shell --seed 2024272
(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))))