Sample trimmed logistic on [-pi, pi]

Percentage Accurate: 99.0% → 99.0%
Time: 16.5s
Alternatives: 8
Speedup: N/A×

Specification

?
\[\left(2.328306437 \cdot 10^{-10} \leq u \land u \leq 1\right) \land \left(0 \leq s \land s \leq 1.0651631\right)\]
\[\begin{array}{l} \\ \begin{array}{l} t_0 := \frac{1}{1 + e^{\frac{\pi}{s}}}\\ \left(-s\right) \cdot \log \left(\frac{1}{u \cdot \left(\frac{1}{1 + e^{\frac{-\pi}{s}}} - t\_0\right) + t\_0} - 1\right) \end{array} \end{array} \]
(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)))))
float code(float u, float s) {
	float t_0 = 1.0f / (1.0f + expf((((float) M_PI) / s)));
	return -s * logf(((1.0f / ((u * ((1.0f / (1.0f + expf((-((float) M_PI) / s)))) - t_0)) + t_0)) - 1.0f));
}
function code(u, s)
	t_0 = Float32(Float32(1.0) / Float32(Float32(1.0) + exp(Float32(Float32(pi) / s))))
	return Float32(Float32(-s) * log(Float32(Float32(Float32(1.0) / Float32(Float32(u * Float32(Float32(Float32(1.0) / Float32(Float32(1.0) + exp(Float32(Float32(-Float32(pi)) / s)))) - t_0)) + t_0)) - Float32(1.0))))
end
function tmp = code(u, s)
	t_0 = single(1.0) / (single(1.0) + exp((single(pi) / s)));
	tmp = -s * log(((single(1.0) / ((u * ((single(1.0) / (single(1.0) + exp((-single(pi) / s)))) - t_0)) + t_0)) - single(1.0)));
end
\begin{array}{l}

\\
\begin{array}{l}
t_0 := \frac{1}{1 + e^{\frac{\pi}{s}}}\\
\left(-s\right) \cdot \log \left(\frac{1}{u \cdot \left(\frac{1}{1 + e^{\frac{-\pi}{s}}} - t\_0\right) + t\_0} - 1\right)
\end{array}
\end{array}

Sampling outcomes in binary32 precision:

Local Percentage Accuracy vs ?

The average percentage accuracy by input value. Horizontal axis shows value of an input variable; the variable is choosen in the title. Vertical axis is accuracy; higher is better. Red represent the original program, while blue represents Herbie's suggestion. These can be toggled with buttons below the plot. The line is an average while dots represent individual samples.

Accuracy vs Speed?

Herbie found 8 alternatives:

AlternativeAccuracySpeedup
The accuracy (vertical axis) and speed (horizontal axis) of each alternatives. Up and to the right is better. The red square shows the initial program, and each blue circle shows an alternative.The line shows the best available speed-accuracy tradeoffs.

Initial Program: 99.0% accurate, 1.0× speedup?

\[\begin{array}{l} \\ \begin{array}{l} t_0 := \frac{1}{1 + e^{\frac{\pi}{s}}}\\ \left(-s\right) \cdot \log \left(\frac{1}{u \cdot \left(\frac{1}{1 + e^{\frac{-\pi}{s}}} - t\_0\right) + t\_0} - 1\right) \end{array} \end{array} \]
(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)))))
float code(float u, float s) {
	float t_0 = 1.0f / (1.0f + expf((((float) M_PI) / s)));
	return -s * logf(((1.0f / ((u * ((1.0f / (1.0f + expf((-((float) M_PI) / s)))) - t_0)) + t_0)) - 1.0f));
}
function code(u, s)
	t_0 = Float32(Float32(1.0) / Float32(Float32(1.0) + exp(Float32(Float32(pi) / s))))
	return Float32(Float32(-s) * log(Float32(Float32(Float32(1.0) / Float32(Float32(u * Float32(Float32(Float32(1.0) / Float32(Float32(1.0) + exp(Float32(Float32(-Float32(pi)) / s)))) - t_0)) + t_0)) - Float32(1.0))))
end
function tmp = code(u, s)
	t_0 = single(1.0) / (single(1.0) + exp((single(pi) / s)));
	tmp = -s * log(((single(1.0) / ((u * ((single(1.0) / (single(1.0) + exp((-single(pi) / s)))) - t_0)) + t_0)) - single(1.0)));
end
\begin{array}{l}

\\
\begin{array}{l}
t_0 := \frac{1}{1 + e^{\frac{\pi}{s}}}\\
\left(-s\right) \cdot \log \left(\frac{1}{u \cdot \left(\frac{1}{1 + e^{\frac{-\pi}{s}}} - t\_0\right) + t\_0} - 1\right)
\end{array}
\end{array}

Alternative 1: 99.0% accurate, 1.3× speedup?

\[\begin{array}{l} \\ s \cdot \left(-\log \left(\frac{1}{\frac{u}{1 + e^{-\frac{\pi}{s}}} + \frac{1 - u}{1 + e^{\frac{\pi}{s}}}} + -1\right)\right) \end{array} \]
(FPCore (u s)
 :precision binary32
 (*
  s
  (-
   (log
    (+
     (/
      1.0
      (+
       (/ u (+ 1.0 (exp (- (/ PI s)))))
       (/ (- 1.0 u) (+ 1.0 (exp (/ PI s))))))
     -1.0)))))
float code(float u, float s) {
	return s * -logf(((1.0f / ((u / (1.0f + expf(-(((float) M_PI) / s)))) + ((1.0f - u) / (1.0f + expf((((float) M_PI) / s)))))) + -1.0f));
}
function code(u, s)
	return Float32(s * Float32(-log(Float32(Float32(Float32(1.0) / Float32(Float32(u / Float32(Float32(1.0) + exp(Float32(-Float32(Float32(pi) / s))))) + Float32(Float32(Float32(1.0) - u) / Float32(Float32(1.0) + exp(Float32(Float32(pi) / s)))))) + Float32(-1.0)))))
end
function tmp = code(u, s)
	tmp = s * -log(((single(1.0) / ((u / (single(1.0) + exp(-(single(pi) / s)))) + ((single(1.0) - u) / (single(1.0) + exp((single(pi) / s)))))) + single(-1.0)));
end
\begin{array}{l}

\\
s \cdot \left(-\log \left(\frac{1}{\frac{u}{1 + e^{-\frac{\pi}{s}}} + \frac{1 - u}{1 + e^{\frac{\pi}{s}}}} + -1\right)\right)
\end{array}
Derivation
  1. Initial program 99.1%

    \[\left(-s\right) \cdot \log \left(\frac{1}{u \cdot \left(\frac{1}{1 + e^{\frac{-\pi}{s}}} - \frac{1}{1 + e^{\frac{\pi}{s}}}\right) + \frac{1}{1 + e^{\frac{\pi}{s}}}} - 1\right) \]
  2. Simplified99.1%

    \[\leadsto \color{blue}{\left(-s\right) \cdot \log \left(\frac{1}{\frac{u}{1 + e^{\frac{\pi}{-s}}} + \frac{1 - u}{1 + e^{\frac{\pi}{s}}}} + -1\right)} \]
  3. Add Preprocessing
  4. Final simplification99.1%

    \[\leadsto s \cdot \left(-\log \left(\frac{1}{\frac{u}{1 + e^{-\frac{\pi}{s}}} + \frac{1 - u}{1 + e^{\frac{\pi}{s}}}} + -1\right)\right) \]
  5. Add Preprocessing

Alternative 2: 25.3% accurate, 2.0× speedup?

\[\begin{array}{l} \\ s \cdot \left(\log s + \left(u \cdot \left(2 - u \cdot -2\right) - \mathsf{log1p}\left(\pi + -1\right)\right)\right) \end{array} \]
(FPCore (u s)
 :precision binary32
 (* s (+ (log s) (- (* u (- 2.0 (* u -2.0))) (log1p (+ PI -1.0))))))
float code(float u, float s) {
	return s * (logf(s) + ((u * (2.0f - (u * -2.0f))) - log1pf((((float) M_PI) + -1.0f))));
}
function code(u, s)
	return Float32(s * Float32(log(s) + Float32(Float32(u * Float32(Float32(2.0) - Float32(u * Float32(-2.0)))) - log1p(Float32(Float32(pi) + Float32(-1.0))))))
end
\begin{array}{l}

\\
s \cdot \left(\log s + \left(u \cdot \left(2 - u \cdot -2\right) - \mathsf{log1p}\left(\pi + -1\right)\right)\right)
\end{array}
Derivation
  1. Initial program 99.1%

    \[\left(-s\right) \cdot \log \left(\frac{1}{u \cdot \left(\frac{1}{1 + e^{\frac{-\pi}{s}}} - \frac{1}{1 + e^{\frac{\pi}{s}}}\right) + \frac{1}{1 + e^{\frac{\pi}{s}}}} - 1\right) \]
  2. Simplified99.1%

    \[\leadsto \color{blue}{\left(-s\right) \cdot \log \left(\frac{1}{\frac{u}{1 + e^{\frac{\pi}{-s}}} + \frac{1 - u}{1 + e^{\frac{\pi}{s}}}} + -1\right)} \]
  3. Add Preprocessing
  4. Taylor expanded in s around -inf 25.7%

    \[\leadsto \left(-s\right) \cdot \log \color{blue}{\left(1 + 4 \cdot \frac{-0.25 \cdot \left(u \cdot \pi\right) - \left(-0.25 \cdot \pi + 0.25 \cdot \left(u \cdot \pi\right)\right)}{s}\right)} \]
  5. Taylor expanded in s around 0 25.6%

    \[\leadsto \color{blue}{-1 \cdot \left(s \cdot \left(\log \left(4 \cdot \left(-0.25 \cdot \left(u \cdot \pi\right) - \left(-0.25 \cdot \pi + 0.25 \cdot \left(u \cdot \pi\right)\right)\right)\right) + -1 \cdot \log s\right)\right)} \]
  6. Simplified25.6%

    \[\leadsto \color{blue}{\left(\log \left(4 \cdot \left(\pi \cdot \left(0.25 + -0.5 \cdot u\right)\right)\right) - \log s\right) \cdot \left(-s\right)} \]
  7. Taylor expanded in u around 0 25.9%

    \[\leadsto \left(\color{blue}{\left(\log \pi + u \cdot \left(-2 \cdot u - 2\right)\right)} - \log s\right) \cdot \left(-s\right) \]
  8. Step-by-step derivation
    1. log1p-expm1-u25.9%

      \[\leadsto \left(\left(\color{blue}{\mathsf{log1p}\left(\mathsf{expm1}\left(\log \pi\right)\right)} + u \cdot \left(-2 \cdot u - 2\right)\right) - \log s\right) \cdot \left(-s\right) \]
    2. expm1-undefine25.9%

      \[\leadsto \left(\left(\mathsf{log1p}\left(\color{blue}{e^{\log \pi} - 1}\right) + u \cdot \left(-2 \cdot u - 2\right)\right) - \log s\right) \cdot \left(-s\right) \]
    3. add-exp-log25.9%

      \[\leadsto \left(\left(\mathsf{log1p}\left(\color{blue}{\pi} - 1\right) + u \cdot \left(-2 \cdot u - 2\right)\right) - \log s\right) \cdot \left(-s\right) \]
  9. Applied egg-rr25.9%

    \[\leadsto \left(\left(\color{blue}{\mathsf{log1p}\left(\pi - 1\right)} + u \cdot \left(-2 \cdot u - 2\right)\right) - \log s\right) \cdot \left(-s\right) \]
  10. Final simplification25.9%

    \[\leadsto s \cdot \left(\log s + \left(u \cdot \left(2 - u \cdot -2\right) - \mathsf{log1p}\left(\pi + -1\right)\right)\right) \]
  11. Add Preprocessing

Alternative 3: 25.3% accurate, 2.0× speedup?

\[\begin{array}{l} \\ s \cdot \left(\log s - \left(u \cdot \left(u \cdot -2 - 2\right) + \log \pi\right)\right) \end{array} \]
(FPCore (u s)
 :precision binary32
 (* s (- (log s) (+ (* u (- (* u -2.0) 2.0)) (log PI)))))
float code(float u, float s) {
	return s * (logf(s) - ((u * ((u * -2.0f) - 2.0f)) + logf(((float) M_PI))));
}
function code(u, s)
	return Float32(s * Float32(log(s) - Float32(Float32(u * Float32(Float32(u * Float32(-2.0)) - Float32(2.0))) + log(Float32(pi)))))
end
function tmp = code(u, s)
	tmp = s * (log(s) - ((u * ((u * single(-2.0)) - single(2.0))) + log(single(pi))));
end
\begin{array}{l}

\\
s \cdot \left(\log s - \left(u \cdot \left(u \cdot -2 - 2\right) + \log \pi\right)\right)
\end{array}
Derivation
  1. Initial program 99.1%

    \[\left(-s\right) \cdot \log \left(\frac{1}{u \cdot \left(\frac{1}{1 + e^{\frac{-\pi}{s}}} - \frac{1}{1 + e^{\frac{\pi}{s}}}\right) + \frac{1}{1 + e^{\frac{\pi}{s}}}} - 1\right) \]
  2. Simplified99.1%

    \[\leadsto \color{blue}{\left(-s\right) \cdot \log \left(\frac{1}{\frac{u}{1 + e^{\frac{\pi}{-s}}} + \frac{1 - u}{1 + e^{\frac{\pi}{s}}}} + -1\right)} \]
  3. Add Preprocessing
  4. Taylor expanded in s around -inf 25.7%

    \[\leadsto \left(-s\right) \cdot \log \color{blue}{\left(1 + 4 \cdot \frac{-0.25 \cdot \left(u \cdot \pi\right) - \left(-0.25 \cdot \pi + 0.25 \cdot \left(u \cdot \pi\right)\right)}{s}\right)} \]
  5. Taylor expanded in s around 0 25.6%

    \[\leadsto \color{blue}{-1 \cdot \left(s \cdot \left(\log \left(4 \cdot \left(-0.25 \cdot \left(u \cdot \pi\right) - \left(-0.25 \cdot \pi + 0.25 \cdot \left(u \cdot \pi\right)\right)\right)\right) + -1 \cdot \log s\right)\right)} \]
  6. Simplified25.6%

    \[\leadsto \color{blue}{\left(\log \left(4 \cdot \left(\pi \cdot \left(0.25 + -0.5 \cdot u\right)\right)\right) - \log s\right) \cdot \left(-s\right)} \]
  7. Taylor expanded in u around 0 25.9%

    \[\leadsto \left(\color{blue}{\left(\log \pi + u \cdot \left(-2 \cdot u - 2\right)\right)} - \log s\right) \cdot \left(-s\right) \]
  8. Final simplification25.9%

    \[\leadsto s \cdot \left(\log s - \left(u \cdot \left(u \cdot -2 - 2\right) + \log \pi\right)\right) \]
  9. Add Preprocessing

Alternative 4: 25.2% accurate, 3.9× speedup?

\[\begin{array}{l} \\ u \cdot \left(s \cdot 2\right) - s \cdot \mathsf{log1p}\left(\frac{\pi}{s}\right) \end{array} \]
(FPCore (u s) :precision binary32 (- (* u (* s 2.0)) (* s (log1p (/ PI s)))))
float code(float u, float s) {
	return (u * (s * 2.0f)) - (s * log1pf((((float) M_PI) / s)));
}
function code(u, s)
	return Float32(Float32(u * Float32(s * Float32(2.0))) - Float32(s * log1p(Float32(Float32(pi) / s))))
end
\begin{array}{l}

\\
u \cdot \left(s \cdot 2\right) - s \cdot \mathsf{log1p}\left(\frac{\pi}{s}\right)
\end{array}
Derivation
  1. Initial program 99.1%

    \[\left(-s\right) \cdot \log \left(\frac{1}{u \cdot \left(\frac{1}{1 + e^{\frac{-\pi}{s}}} - \frac{1}{1 + e^{\frac{\pi}{s}}}\right) + \frac{1}{1 + e^{\frac{\pi}{s}}}} - 1\right) \]
  2. Simplified99.1%

    \[\leadsto \color{blue}{\left(-s\right) \cdot \log \left(\frac{1}{\frac{u}{1 + e^{\frac{\pi}{-s}}} + \frac{1 - u}{1 + e^{\frac{\pi}{s}}}} + -1\right)} \]
  3. Add Preprocessing
  4. Taylor expanded in s around inf 25.7%

    \[\leadsto \left(-s\right) \cdot \log \color{blue}{\left(1 + -4 \cdot \frac{0.25 \cdot \left(u \cdot \pi\right) - \left(-0.25 \cdot \left(u \cdot \pi\right) + 0.25 \cdot \pi\right)}{s}\right)} \]
  5. Step-by-step derivation
    1. +-commutative25.7%

      \[\leadsto \left(-s\right) \cdot \log \color{blue}{\left(-4 \cdot \frac{0.25 \cdot \left(u \cdot \pi\right) - \left(-0.25 \cdot \left(u \cdot \pi\right) + 0.25 \cdot \pi\right)}{s} + 1\right)} \]
    2. fma-define25.7%

      \[\leadsto \left(-s\right) \cdot \log \color{blue}{\left(\mathsf{fma}\left(-4, \frac{0.25 \cdot \left(u \cdot \pi\right) - \left(-0.25 \cdot \left(u \cdot \pi\right) + 0.25 \cdot \pi\right)}{s}, 1\right)\right)} \]
    3. associate--r+25.7%

      \[\leadsto \left(-s\right) \cdot \log \left(\mathsf{fma}\left(-4, \frac{\color{blue}{\left(0.25 \cdot \left(u \cdot \pi\right) - -0.25 \cdot \left(u \cdot \pi\right)\right) - 0.25 \cdot \pi}}{s}, 1\right)\right) \]
    4. cancel-sign-sub-inv25.7%

      \[\leadsto \left(-s\right) \cdot \log \left(\mathsf{fma}\left(-4, \frac{\color{blue}{\left(0.25 \cdot \left(u \cdot \pi\right) - -0.25 \cdot \left(u \cdot \pi\right)\right) + \left(-0.25\right) \cdot \pi}}{s}, 1\right)\right) \]
    5. distribute-rgt-out--25.7%

      \[\leadsto \left(-s\right) \cdot \log \left(\mathsf{fma}\left(-4, \frac{\color{blue}{\left(u \cdot \pi\right) \cdot \left(0.25 - -0.25\right)} + \left(-0.25\right) \cdot \pi}{s}, 1\right)\right) \]
    6. *-commutative25.7%

      \[\leadsto \left(-s\right) \cdot \log \left(\mathsf{fma}\left(-4, \frac{\color{blue}{\left(\pi \cdot u\right)} \cdot \left(0.25 - -0.25\right) + \left(-0.25\right) \cdot \pi}{s}, 1\right)\right) \]
    7. metadata-eval25.7%

      \[\leadsto \left(-s\right) \cdot \log \left(\mathsf{fma}\left(-4, \frac{\left(\pi \cdot u\right) \cdot \color{blue}{0.5} + \left(-0.25\right) \cdot \pi}{s}, 1\right)\right) \]
    8. metadata-eval25.7%

      \[\leadsto \left(-s\right) \cdot \log \left(\mathsf{fma}\left(-4, \frac{\left(\pi \cdot u\right) \cdot 0.5 + \color{blue}{-0.25} \cdot \pi}{s}, 1\right)\right) \]
    9. *-commutative25.7%

      \[\leadsto \left(-s\right) \cdot \log \left(\mathsf{fma}\left(-4, \frac{\left(\pi \cdot u\right) \cdot 0.5 + \color{blue}{\pi \cdot -0.25}}{s}, 1\right)\right) \]
  6. Simplified25.7%

    \[\leadsto \left(-s\right) \cdot \log \color{blue}{\left(\mathsf{fma}\left(-4, \frac{\left(\pi \cdot u\right) \cdot 0.5 + \pi \cdot -0.25}{s}, 1\right)\right)} \]
  7. Taylor expanded in u around 0 25.9%

    \[\leadsto \color{blue}{-1 \cdot \left(s \cdot \log \left(1 + \frac{\pi}{s}\right)\right) + 2 \cdot \frac{u \cdot \pi}{1 + \frac{\pi}{s}}} \]
  8. Step-by-step derivation
    1. +-commutative25.9%

      \[\leadsto \color{blue}{2 \cdot \frac{u \cdot \pi}{1 + \frac{\pi}{s}} + -1 \cdot \left(s \cdot \log \left(1 + \frac{\pi}{s}\right)\right)} \]
    2. mul-1-neg25.9%

      \[\leadsto 2 \cdot \frac{u \cdot \pi}{1 + \frac{\pi}{s}} + \color{blue}{\left(-s \cdot \log \left(1 + \frac{\pi}{s}\right)\right)} \]
    3. unsub-neg25.9%

      \[\leadsto \color{blue}{2 \cdot \frac{u \cdot \pi}{1 + \frac{\pi}{s}} - s \cdot \log \left(1 + \frac{\pi}{s}\right)} \]
    4. *-commutative25.9%

      \[\leadsto \color{blue}{\frac{u \cdot \pi}{1 + \frac{\pi}{s}} \cdot 2} - s \cdot \log \left(1 + \frac{\pi}{s}\right) \]
    5. associate-/l*25.9%

      \[\leadsto \color{blue}{\left(u \cdot \frac{\pi}{1 + \frac{\pi}{s}}\right)} \cdot 2 - s \cdot \log \left(1 + \frac{\pi}{s}\right) \]
    6. associate-*r*25.9%

      \[\leadsto \color{blue}{u \cdot \left(\frac{\pi}{1 + \frac{\pi}{s}} \cdot 2\right)} - s \cdot \log \left(1 + \frac{\pi}{s}\right) \]
    7. *-commutative25.9%

      \[\leadsto u \cdot \color{blue}{\left(2 \cdot \frac{\pi}{1 + \frac{\pi}{s}}\right)} - s \cdot \log \left(1 + \frac{\pi}{s}\right) \]
    8. associate-*r/25.9%

      \[\leadsto u \cdot \color{blue}{\frac{2 \cdot \pi}{1 + \frac{\pi}{s}}} - s \cdot \log \left(1 + \frac{\pi}{s}\right) \]
    9. log1p-define25.9%

      \[\leadsto u \cdot \frac{2 \cdot \pi}{1 + \frac{\pi}{s}} - s \cdot \color{blue}{\mathsf{log1p}\left(\frac{\pi}{s}\right)} \]
  9. Simplified25.9%

    \[\leadsto \color{blue}{u \cdot \frac{2 \cdot \pi}{1 + \frac{\pi}{s}} - s \cdot \mathsf{log1p}\left(\frac{\pi}{s}\right)} \]
  10. Taylor expanded in s around 0 25.9%

    \[\leadsto u \cdot \color{blue}{\left(2 \cdot s\right)} - s \cdot \mathsf{log1p}\left(\frac{\pi}{s}\right) \]
  11. Final simplification25.9%

    \[\leadsto u \cdot \left(s \cdot 2\right) - s \cdot \mathsf{log1p}\left(\frac{\pi}{s}\right) \]
  12. Add Preprocessing

Alternative 5: 25.2% accurate, 4.0× speedup?

\[\begin{array}{l} \\ s \cdot \left(-\mathsf{log1p}\left(\frac{1}{\frac{s}{\pi}}\right)\right) \end{array} \]
(FPCore (u s) :precision binary32 (* s (- (log1p (/ 1.0 (/ s PI))))))
float code(float u, float s) {
	return s * -log1pf((1.0f / (s / ((float) M_PI))));
}
function code(u, s)
	return Float32(s * Float32(-log1p(Float32(Float32(1.0) / Float32(s / Float32(pi))))))
end
\begin{array}{l}

\\
s \cdot \left(-\mathsf{log1p}\left(\frac{1}{\frac{s}{\pi}}\right)\right)
\end{array}
Derivation
  1. Initial program 99.1%

    \[\left(-s\right) \cdot \log \left(\frac{1}{u \cdot \left(\frac{1}{1 + e^{\frac{-\pi}{s}}} - \frac{1}{1 + e^{\frac{\pi}{s}}}\right) + \frac{1}{1 + e^{\frac{\pi}{s}}}} - 1\right) \]
  2. Simplified99.1%

    \[\leadsto \color{blue}{\left(-s\right) \cdot \log \left(\frac{1}{\frac{u}{1 + e^{\frac{\pi}{-s}}} + \frac{1 - u}{1 + e^{\frac{\pi}{s}}}} + -1\right)} \]
  3. Add Preprocessing
  4. Taylor expanded in s around -inf 25.7%

    \[\leadsto \left(-s\right) \cdot \log \color{blue}{\left(1 + 4 \cdot \frac{-0.25 \cdot \left(u \cdot \pi\right) - \left(-0.25 \cdot \pi + 0.25 \cdot \left(u \cdot \pi\right)\right)}{s}\right)} \]
  5. Step-by-step derivation
    1. distribute-lft-neg-out25.7%

      \[\leadsto \color{blue}{-s \cdot \log \left(1 + 4 \cdot \frac{-0.25 \cdot \left(u \cdot \pi\right) - \left(-0.25 \cdot \pi + 0.25 \cdot \left(u \cdot \pi\right)\right)}{s}\right)} \]
    2. neg-sub025.7%

      \[\leadsto \color{blue}{0 - s \cdot \log \left(1 + 4 \cdot \frac{-0.25 \cdot \left(u \cdot \pi\right) - \left(-0.25 \cdot \pi + 0.25 \cdot \left(u \cdot \pi\right)\right)}{s}\right)} \]
    3. log1p-define25.7%

      \[\leadsto 0 - s \cdot \color{blue}{\mathsf{log1p}\left(4 \cdot \frac{-0.25 \cdot \left(u \cdot \pi\right) - \left(-0.25 \cdot \pi + 0.25 \cdot \left(u \cdot \pi\right)\right)}{s}\right)} \]
  6. Applied egg-rr25.7%

    \[\leadsto \color{blue}{0 - s \cdot \mathsf{log1p}\left(4 \cdot \frac{\mathsf{fma}\left(\pi, \mathsf{fma}\left(u, -0.25, 0.25\right), u \cdot \left(\pi \cdot -0.25\right)\right)}{s}\right)} \]
  7. Step-by-step derivation
    1. neg-sub025.7%

      \[\leadsto \color{blue}{-s \cdot \mathsf{log1p}\left(4 \cdot \frac{\mathsf{fma}\left(\pi, \mathsf{fma}\left(u, -0.25, 0.25\right), u \cdot \left(\pi \cdot -0.25\right)\right)}{s}\right)} \]
    2. *-commutative25.7%

      \[\leadsto -\color{blue}{\mathsf{log1p}\left(4 \cdot \frac{\mathsf{fma}\left(\pi, \mathsf{fma}\left(u, -0.25, 0.25\right), u \cdot \left(\pi \cdot -0.25\right)\right)}{s}\right) \cdot s} \]
    3. distribute-rgt-neg-in25.7%

      \[\leadsto \color{blue}{\mathsf{log1p}\left(4 \cdot \frac{\mathsf{fma}\left(\pi, \mathsf{fma}\left(u, -0.25, 0.25\right), u \cdot \left(\pi \cdot -0.25\right)\right)}{s}\right) \cdot \left(-s\right)} \]
  8. Simplified25.7%

    \[\leadsto \color{blue}{\mathsf{log1p}\left(\frac{4 \cdot \left(\pi \cdot \left(0.25 + -0.5 \cdot u\right)\right)}{s}\right) \cdot \left(-s\right)} \]
  9. Step-by-step derivation
    1. clear-num25.7%

      \[\leadsto \mathsf{log1p}\left(\color{blue}{\frac{1}{\frac{s}{4 \cdot \left(\pi \cdot \left(0.25 + -0.5 \cdot u\right)\right)}}}\right) \cdot \left(-s\right) \]
    2. inv-pow25.7%

      \[\leadsto \mathsf{log1p}\left(\color{blue}{{\left(\frac{s}{4 \cdot \left(\pi \cdot \left(0.25 + -0.5 \cdot u\right)\right)}\right)}^{-1}}\right) \cdot \left(-s\right) \]
    3. *-un-lft-identity25.7%

      \[\leadsto \mathsf{log1p}\left({\left(\frac{\color{blue}{1 \cdot s}}{4 \cdot \left(\pi \cdot \left(0.25 + -0.5 \cdot u\right)\right)}\right)}^{-1}\right) \cdot \left(-s\right) \]
    4. times-frac25.7%

      \[\leadsto \mathsf{log1p}\left({\color{blue}{\left(\frac{1}{4} \cdot \frac{s}{\pi \cdot \left(0.25 + -0.5 \cdot u\right)}\right)}}^{-1}\right) \cdot \left(-s\right) \]
    5. metadata-eval25.7%

      \[\leadsto \mathsf{log1p}\left({\left(\color{blue}{0.25} \cdot \frac{s}{\pi \cdot \left(0.25 + -0.5 \cdot u\right)}\right)}^{-1}\right) \cdot \left(-s\right) \]
    6. +-commutative25.7%

      \[\leadsto \mathsf{log1p}\left({\left(0.25 \cdot \frac{s}{\pi \cdot \color{blue}{\left(-0.5 \cdot u + 0.25\right)}}\right)}^{-1}\right) \cdot \left(-s\right) \]
    7. fma-define25.7%

      \[\leadsto \mathsf{log1p}\left({\left(0.25 \cdot \frac{s}{\pi \cdot \color{blue}{\mathsf{fma}\left(-0.5, u, 0.25\right)}}\right)}^{-1}\right) \cdot \left(-s\right) \]
  10. Applied egg-rr25.7%

    \[\leadsto \mathsf{log1p}\left(\color{blue}{{\left(0.25 \cdot \frac{s}{\pi \cdot \mathsf{fma}\left(-0.5, u, 0.25\right)}\right)}^{-1}}\right) \cdot \left(-s\right) \]
  11. Step-by-step derivation
    1. unpow-125.7%

      \[\leadsto \mathsf{log1p}\left(\color{blue}{\frac{1}{0.25 \cdot \frac{s}{\pi \cdot \mathsf{fma}\left(-0.5, u, 0.25\right)}}}\right) \cdot \left(-s\right) \]
    2. associate-*r/25.7%

      \[\leadsto \mathsf{log1p}\left(\frac{1}{\color{blue}{\frac{0.25 \cdot s}{\pi \cdot \mathsf{fma}\left(-0.5, u, 0.25\right)}}}\right) \cdot \left(-s\right) \]
    3. times-frac25.7%

      \[\leadsto \mathsf{log1p}\left(\frac{1}{\color{blue}{\frac{0.25}{\pi} \cdot \frac{s}{\mathsf{fma}\left(-0.5, u, 0.25\right)}}}\right) \cdot \left(-s\right) \]
  12. Simplified25.7%

    \[\leadsto \mathsf{log1p}\left(\color{blue}{\frac{1}{\frac{0.25}{\pi} \cdot \frac{s}{\mathsf{fma}\left(-0.5, u, 0.25\right)}}}\right) \cdot \left(-s\right) \]
  13. Taylor expanded in u around 0 25.8%

    \[\leadsto \mathsf{log1p}\left(\frac{1}{\color{blue}{\frac{s}{\pi}}}\right) \cdot \left(-s\right) \]
  14. Final simplification25.8%

    \[\leadsto s \cdot \left(-\mathsf{log1p}\left(\frac{1}{\frac{s}{\pi}}\right)\right) \]
  15. Add Preprocessing

Alternative 6: 25.2% accurate, 4.1× speedup?

\[\begin{array}{l} \\ s \cdot \left(-\mathsf{log1p}\left(\frac{\pi}{s}\right)\right) \end{array} \]
(FPCore (u s) :precision binary32 (* s (- (log1p (/ PI s)))))
float code(float u, float s) {
	return s * -log1pf((((float) M_PI) / s));
}
function code(u, s)
	return Float32(s * Float32(-log1p(Float32(Float32(pi) / s))))
end
\begin{array}{l}

\\
s \cdot \left(-\mathsf{log1p}\left(\frac{\pi}{s}\right)\right)
\end{array}
Derivation
  1. Initial program 99.1%

    \[\left(-s\right) \cdot \log \left(\frac{1}{u \cdot \left(\frac{1}{1 + e^{\frac{-\pi}{s}}} - \frac{1}{1 + e^{\frac{\pi}{s}}}\right) + \frac{1}{1 + e^{\frac{\pi}{s}}}} - 1\right) \]
  2. Simplified99.1%

    \[\leadsto \color{blue}{\left(-s\right) \cdot \log \left(\frac{1}{\frac{u}{1 + e^{\frac{\pi}{-s}}} + \frac{1 - u}{1 + e^{\frac{\pi}{s}}}} + -1\right)} \]
  3. Add Preprocessing
  4. Taylor expanded in s around -inf 25.7%

    \[\leadsto \left(-s\right) \cdot \log \color{blue}{\left(1 + 4 \cdot \frac{-0.25 \cdot \left(u \cdot \pi\right) - \left(-0.25 \cdot \pi + 0.25 \cdot \left(u \cdot \pi\right)\right)}{s}\right)} \]
  5. Taylor expanded in u around 0 25.8%

    \[\leadsto \color{blue}{-1 \cdot \left(s \cdot \log \left(1 + \frac{\pi}{s}\right)\right)} \]
  6. Step-by-step derivation
    1. mul-1-neg25.8%

      \[\leadsto \color{blue}{-s \cdot \log \left(1 + \frac{\pi}{s}\right)} \]
    2. *-commutative25.8%

      \[\leadsto -\color{blue}{\log \left(1 + \frac{\pi}{s}\right) \cdot s} \]
    3. distribute-rgt-neg-in25.8%

      \[\leadsto \color{blue}{\log \left(1 + \frac{\pi}{s}\right) \cdot \left(-s\right)} \]
    4. log1p-define25.8%

      \[\leadsto \color{blue}{\mathsf{log1p}\left(\frac{\pi}{s}\right)} \cdot \left(-s\right) \]
  7. Simplified25.8%

    \[\leadsto \color{blue}{\mathsf{log1p}\left(\frac{\pi}{s}\right) \cdot \left(-s\right)} \]
  8. Final simplification25.8%

    \[\leadsto s \cdot \left(-\mathsf{log1p}\left(\frac{\pi}{s}\right)\right) \]
  9. Add Preprocessing

Alternative 7: 11.6% accurate, 61.9× speedup?

\[\begin{array}{l} \\ \pi \cdot \left(-1 + u \cdot 2\right) \end{array} \]
(FPCore (u s) :precision binary32 (* PI (+ -1.0 (* u 2.0))))
float code(float u, float s) {
	return ((float) M_PI) * (-1.0f + (u * 2.0f));
}
function code(u, s)
	return Float32(Float32(pi) * Float32(Float32(-1.0) + Float32(u * Float32(2.0))))
end
function tmp = code(u, s)
	tmp = single(pi) * (single(-1.0) + (u * single(2.0)));
end
\begin{array}{l}

\\
\pi \cdot \left(-1 + u \cdot 2\right)
\end{array}
Derivation
  1. Initial program 99.1%

    \[\left(-s\right) \cdot \log \left(\frac{1}{u \cdot \left(\frac{1}{1 + e^{\frac{-\pi}{s}}} - \frac{1}{1 + e^{\frac{\pi}{s}}}\right) + \frac{1}{1 + e^{\frac{\pi}{s}}}} - 1\right) \]
  2. Simplified99.1%

    \[\leadsto \color{blue}{\left(-s\right) \cdot \log \left(\frac{1}{\frac{u}{1 + e^{\frac{\pi}{-s}}} + \frac{1 - u}{1 + e^{\frac{\pi}{s}}}} + -1\right)} \]
  3. Add Preprocessing
  4. Taylor expanded in s around inf 25.7%

    \[\leadsto \left(-s\right) \cdot \log \color{blue}{\left(1 + -4 \cdot \frac{0.25 \cdot \left(u \cdot \pi\right) - \left(-0.25 \cdot \left(u \cdot \pi\right) + 0.25 \cdot \pi\right)}{s}\right)} \]
  5. Step-by-step derivation
    1. +-commutative25.7%

      \[\leadsto \left(-s\right) \cdot \log \color{blue}{\left(-4 \cdot \frac{0.25 \cdot \left(u \cdot \pi\right) - \left(-0.25 \cdot \left(u \cdot \pi\right) + 0.25 \cdot \pi\right)}{s} + 1\right)} \]
    2. fma-define25.7%

      \[\leadsto \left(-s\right) \cdot \log \color{blue}{\left(\mathsf{fma}\left(-4, \frac{0.25 \cdot \left(u \cdot \pi\right) - \left(-0.25 \cdot \left(u \cdot \pi\right) + 0.25 \cdot \pi\right)}{s}, 1\right)\right)} \]
    3. associate--r+25.7%

      \[\leadsto \left(-s\right) \cdot \log \left(\mathsf{fma}\left(-4, \frac{\color{blue}{\left(0.25 \cdot \left(u \cdot \pi\right) - -0.25 \cdot \left(u \cdot \pi\right)\right) - 0.25 \cdot \pi}}{s}, 1\right)\right) \]
    4. cancel-sign-sub-inv25.7%

      \[\leadsto \left(-s\right) \cdot \log \left(\mathsf{fma}\left(-4, \frac{\color{blue}{\left(0.25 \cdot \left(u \cdot \pi\right) - -0.25 \cdot \left(u \cdot \pi\right)\right) + \left(-0.25\right) \cdot \pi}}{s}, 1\right)\right) \]
    5. distribute-rgt-out--25.7%

      \[\leadsto \left(-s\right) \cdot \log \left(\mathsf{fma}\left(-4, \frac{\color{blue}{\left(u \cdot \pi\right) \cdot \left(0.25 - -0.25\right)} + \left(-0.25\right) \cdot \pi}{s}, 1\right)\right) \]
    6. *-commutative25.7%

      \[\leadsto \left(-s\right) \cdot \log \left(\mathsf{fma}\left(-4, \frac{\color{blue}{\left(\pi \cdot u\right)} \cdot \left(0.25 - -0.25\right) + \left(-0.25\right) \cdot \pi}{s}, 1\right)\right) \]
    7. metadata-eval25.7%

      \[\leadsto \left(-s\right) \cdot \log \left(\mathsf{fma}\left(-4, \frac{\left(\pi \cdot u\right) \cdot \color{blue}{0.5} + \left(-0.25\right) \cdot \pi}{s}, 1\right)\right) \]
    8. metadata-eval25.7%

      \[\leadsto \left(-s\right) \cdot \log \left(\mathsf{fma}\left(-4, \frac{\left(\pi \cdot u\right) \cdot 0.5 + \color{blue}{-0.25} \cdot \pi}{s}, 1\right)\right) \]
    9. *-commutative25.7%

      \[\leadsto \left(-s\right) \cdot \log \left(\mathsf{fma}\left(-4, \frac{\left(\pi \cdot u\right) \cdot 0.5 + \color{blue}{\pi \cdot -0.25}}{s}, 1\right)\right) \]
  6. Simplified25.7%

    \[\leadsto \left(-s\right) \cdot \log \color{blue}{\left(\mathsf{fma}\left(-4, \frac{\left(\pi \cdot u\right) \cdot 0.5 + \pi \cdot -0.25}{s}, 1\right)\right)} \]
  7. Taylor expanded in s around inf 11.8%

    \[\leadsto \color{blue}{4 \cdot \left(-0.25 \cdot \pi + 0.5 \cdot \left(u \cdot \pi\right)\right)} \]
  8. Step-by-step derivation
    1. distribute-lft-in11.8%

      \[\leadsto \color{blue}{4 \cdot \left(-0.25 \cdot \pi\right) + 4 \cdot \left(0.5 \cdot \left(u \cdot \pi\right)\right)} \]
    2. associate-*r*11.8%

      \[\leadsto \color{blue}{\left(4 \cdot -0.25\right) \cdot \pi} + 4 \cdot \left(0.5 \cdot \left(u \cdot \pi\right)\right) \]
    3. metadata-eval11.8%

      \[\leadsto \color{blue}{-1} \cdot \pi + 4 \cdot \left(0.5 \cdot \left(u \cdot \pi\right)\right) \]
    4. neg-mul-111.8%

      \[\leadsto \color{blue}{\left(-\pi\right)} + 4 \cdot \left(0.5 \cdot \left(u \cdot \pi\right)\right) \]
    5. *-commutative11.8%

      \[\leadsto \left(-\pi\right) + 4 \cdot \left(0.5 \cdot \color{blue}{\left(\pi \cdot u\right)}\right) \]
    6. associate-*r*11.8%

      \[\leadsto \left(-\pi\right) + \color{blue}{\left(4 \cdot 0.5\right) \cdot \left(\pi \cdot u\right)} \]
    7. metadata-eval11.8%

      \[\leadsto \left(-\pi\right) + \color{blue}{2} \cdot \left(\pi \cdot u\right) \]
    8. *-commutative11.8%

      \[\leadsto \left(-\pi\right) + 2 \cdot \color{blue}{\left(u \cdot \pi\right)} \]
    9. associate-*r*11.8%

      \[\leadsto \left(-\pi\right) + \color{blue}{\left(2 \cdot u\right) \cdot \pi} \]
  9. Simplified11.8%

    \[\leadsto \color{blue}{\left(-\pi\right) + \left(2 \cdot u\right) \cdot \pi} \]
  10. Taylor expanded in u around 0 11.8%

    \[\leadsto \color{blue}{2 \cdot \left(u \cdot \pi\right) - \pi} \]
  11. Step-by-step derivation
    1. sub-neg11.8%

      \[\leadsto \color{blue}{2 \cdot \left(u \cdot \pi\right) + \left(-\pi\right)} \]
    2. associate-*r*11.8%

      \[\leadsto \color{blue}{\left(2 \cdot u\right) \cdot \pi} + \left(-\pi\right) \]
    3. mul-1-neg11.8%

      \[\leadsto \left(2 \cdot u\right) \cdot \pi + \color{blue}{-1 \cdot \pi} \]
    4. distribute-rgt-out11.8%

      \[\leadsto \color{blue}{\pi \cdot \left(2 \cdot u + -1\right)} \]
    5. metadata-eval11.8%

      \[\leadsto \pi \cdot \left(\color{blue}{\left(--2\right)} \cdot u + -1\right) \]
    6. distribute-lft-neg-in11.8%

      \[\leadsto \pi \cdot \left(\color{blue}{\left(--2 \cdot u\right)} + -1\right) \]
    7. *-commutative11.8%

      \[\leadsto \pi \cdot \left(\left(-\color{blue}{u \cdot -2}\right) + -1\right) \]
    8. distribute-rgt-neg-in11.8%

      \[\leadsto \pi \cdot \left(\color{blue}{u \cdot \left(--2\right)} + -1\right) \]
    9. metadata-eval11.8%

      \[\leadsto \pi \cdot \left(u \cdot \color{blue}{2} + -1\right) \]
  12. Simplified11.8%

    \[\leadsto \color{blue}{\pi \cdot \left(u \cdot 2 + -1\right)} \]
  13. Final simplification11.8%

    \[\leadsto \pi \cdot \left(-1 + u \cdot 2\right) \]
  14. Add Preprocessing

Alternative 8: 11.4% accurate, 216.5× speedup?

\[\begin{array}{l} \\ -\pi \end{array} \]
(FPCore (u s) :precision binary32 (- PI))
float code(float u, float s) {
	return -((float) M_PI);
}
function code(u, s)
	return Float32(-Float32(pi))
end
function tmp = code(u, s)
	tmp = -single(pi);
end
\begin{array}{l}

\\
-\pi
\end{array}
Derivation
  1. Initial program 99.1%

    \[\left(-s\right) \cdot \log \left(\frac{1}{u \cdot \left(\frac{1}{1 + e^{\frac{-\pi}{s}}} - \frac{1}{1 + e^{\frac{\pi}{s}}}\right) + \frac{1}{1 + e^{\frac{\pi}{s}}}} - 1\right) \]
  2. Simplified99.1%

    \[\leadsto \color{blue}{\left(-s\right) \cdot \log \left(\frac{1}{\frac{u}{1 + e^{\frac{\pi}{-s}}} + \frac{1 - u}{1 + e^{\frac{\pi}{s}}}} + -1\right)} \]
  3. Add Preprocessing
  4. Taylor expanded in u around 0 11.6%

    \[\leadsto \color{blue}{-1 \cdot \pi} \]
  5. Step-by-step derivation
    1. neg-mul-111.6%

      \[\leadsto \color{blue}{-\pi} \]
  6. Simplified11.6%

    \[\leadsto \color{blue}{-\pi} \]
  7. Final simplification11.6%

    \[\leadsto -\pi \]
  8. Add Preprocessing

Reproduce

?
herbie shell --seed 2024060 
(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))))