Sample trimmed logistic on [-pi, pi]

Percentage Accurate: 99.0% → 99.0%
Time: 12.4s
Alternatives: 7
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 7 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.4× speedup?

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

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

    \[\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.0%

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

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

Alternative 2: 29.1% accurate, 3.4× speedup?

\[\begin{array}{l} \\ s \cdot \left(-4 \cdot \left(\left(--1\right) - \left(-1 + s \cdot \left(\pi \cdot \mathsf{fma}\left(u, 0.5, -0.25\right)\right)\right)\right)\right) \end{array} \]
(FPCore (u s)
 :precision binary32
 (* s (* -4.0 (- (- -1.0) (+ -1.0 (* s (* PI (fma u 0.5 -0.25))))))))
float code(float u, float s) {
	return s * (-4.0f * (-(-1.0f) - (-1.0f + (s * (((float) M_PI) * fmaf(u, 0.5f, -0.25f))))));
}
function code(u, s)
	return Float32(s * Float32(Float32(-4.0) * Float32(Float32(-Float32(-1.0)) - Float32(Float32(-1.0) + Float32(s * Float32(Float32(pi) * fma(u, Float32(0.5), Float32(-0.25))))))))
end
\begin{array}{l}

\\
s \cdot \left(-4 \cdot \left(\left(--1\right) - \left(-1 + s \cdot \left(\pi \cdot \mathsf{fma}\left(u, 0.5, -0.25\right)\right)\right)\right)\right)
\end{array}
Derivation
  1. Initial program 99.0%

    \[\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.0%

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

    \[\leadsto s \cdot \left(-\color{blue}{-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) \]
  4. Step-by-step derivation
    1. associate--r+10.7%

      \[\leadsto s \cdot \left(--4 \cdot \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}\right) \]
    2. cancel-sign-sub-inv10.7%

      \[\leadsto s \cdot \left(--4 \cdot \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}\right) \]
    3. distribute-rgt-out--10.7%

      \[\leadsto s \cdot \left(--4 \cdot \frac{\color{blue}{\left(u \cdot \pi\right) \cdot \left(0.25 - -0.25\right)} + \left(-0.25\right) \cdot \pi}{s}\right) \]
    4. *-commutative10.7%

      \[\leadsto s \cdot \left(--4 \cdot \frac{\color{blue}{\left(\pi \cdot u\right)} \cdot \left(0.25 - -0.25\right) + \left(-0.25\right) \cdot \pi}{s}\right) \]
    5. metadata-eval10.7%

      \[\leadsto s \cdot \left(--4 \cdot \frac{\left(\pi \cdot u\right) \cdot \color{blue}{0.5} + \left(-0.25\right) \cdot \pi}{s}\right) \]
    6. metadata-eval10.7%

      \[\leadsto s \cdot \left(--4 \cdot \frac{\left(\pi \cdot u\right) \cdot 0.5 + \color{blue}{-0.25} \cdot \pi}{s}\right) \]
    7. *-commutative10.7%

      \[\leadsto s \cdot \left(--4 \cdot \frac{\left(\pi \cdot u\right) \cdot 0.5 + \color{blue}{\pi \cdot -0.25}}{s}\right) \]
  5. Simplified10.7%

    \[\leadsto s \cdot \left(-\color{blue}{-4 \cdot \frac{\left(\pi \cdot u\right) \cdot 0.5 + \pi \cdot -0.25}{s}}\right) \]
  6. Step-by-step derivation
    1. expm1-log1p-u0.4%

      \[\leadsto s \cdot \left(--4 \cdot \color{blue}{\mathsf{expm1}\left(\mathsf{log1p}\left(\frac{\left(\pi \cdot u\right) \cdot 0.5 + \pi \cdot -0.25}{s}\right)\right)}\right) \]
    2. *-commutative0.4%

      \[\leadsto s \cdot \left(--4 \cdot \mathsf{expm1}\left(\mathsf{log1p}\left(\frac{\color{blue}{\left(u \cdot \pi\right)} \cdot 0.5 + \pi \cdot -0.25}{s}\right)\right)\right) \]
    3. *-commutative0.4%

      \[\leadsto s \cdot \left(--4 \cdot \mathsf{expm1}\left(\mathsf{log1p}\left(\frac{\color{blue}{0.5 \cdot \left(u \cdot \pi\right)} + \pi \cdot -0.25}{s}\right)\right)\right) \]
    4. fma-def0.4%

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

    \[\leadsto s \cdot \left(--4 \cdot \color{blue}{\mathsf{expm1}\left(\mathsf{log1p}\left(\frac{\mathsf{fma}\left(0.5, u \cdot \pi, \pi \cdot -0.25\right)}{s}\right)\right)}\right) \]
  8. Step-by-step derivation
    1. fma-udef0.4%

      \[\leadsto s \cdot \left(--4 \cdot \mathsf{expm1}\left(\mathsf{log1p}\left(\frac{\color{blue}{0.5 \cdot \left(u \cdot \pi\right) + \pi \cdot -0.25}}{s}\right)\right)\right) \]
    2. *-commutative0.4%

      \[\leadsto s \cdot \left(--4 \cdot \mathsf{expm1}\left(\mathsf{log1p}\left(\frac{0.5 \cdot \color{blue}{\left(\pi \cdot u\right)} + \pi \cdot -0.25}{s}\right)\right)\right) \]
  9. Applied egg-rr0.4%

    \[\leadsto s \cdot \left(--4 \cdot \mathsf{expm1}\left(\mathsf{log1p}\left(\frac{\color{blue}{0.5 \cdot \left(\pi \cdot u\right) + \pi \cdot -0.25}}{s}\right)\right)\right) \]
  10. Applied egg-rr28.8%

    \[\leadsto s \cdot \left(--4 \cdot \color{blue}{\left(\left(s \cdot \left(\pi \cdot \mathsf{fma}\left(u, 0.5, -0.25\right)\right) + -1\right) + -1\right)}\right) \]
  11. Final simplification28.8%

    \[\leadsto s \cdot \left(-4 \cdot \left(\left(--1\right) - \left(-1 + s \cdot \left(\pi \cdot \mathsf{fma}\left(u, 0.5, -0.25\right)\right)\right)\right)\right) \]

Alternative 3: 29.1% accurate, 3.4× speedup?

\[\begin{array}{l} \\ s \cdot \left(-4 \cdot \left(\left(--2\right) - s \cdot \left(\pi \cdot \mathsf{fma}\left(u, 0.5, -0.25\right)\right)\right)\right) \end{array} \]
(FPCore (u s)
 :precision binary32
 (* s (* -4.0 (- (- -2.0) (* s (* PI (fma u 0.5 -0.25)))))))
float code(float u, float s) {
	return s * (-4.0f * (-(-2.0f) - (s * (((float) M_PI) * fmaf(u, 0.5f, -0.25f)))));
}
function code(u, s)
	return Float32(s * Float32(Float32(-4.0) * Float32(Float32(-Float32(-2.0)) - Float32(s * Float32(Float32(pi) * fma(u, Float32(0.5), Float32(-0.25)))))))
end
\begin{array}{l}

\\
s \cdot \left(-4 \cdot \left(\left(--2\right) - s \cdot \left(\pi \cdot \mathsf{fma}\left(u, 0.5, -0.25\right)\right)\right)\right)
\end{array}
Derivation
  1. Initial program 99.0%

    \[\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.0%

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

    \[\leadsto s \cdot \left(-\color{blue}{-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) \]
  4. Step-by-step derivation
    1. associate--r+10.7%

      \[\leadsto s \cdot \left(--4 \cdot \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}\right) \]
    2. cancel-sign-sub-inv10.7%

      \[\leadsto s \cdot \left(--4 \cdot \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}\right) \]
    3. distribute-rgt-out--10.7%

      \[\leadsto s \cdot \left(--4 \cdot \frac{\color{blue}{\left(u \cdot \pi\right) \cdot \left(0.25 - -0.25\right)} + \left(-0.25\right) \cdot \pi}{s}\right) \]
    4. *-commutative10.7%

      \[\leadsto s \cdot \left(--4 \cdot \frac{\color{blue}{\left(\pi \cdot u\right)} \cdot \left(0.25 - -0.25\right) + \left(-0.25\right) \cdot \pi}{s}\right) \]
    5. metadata-eval10.7%

      \[\leadsto s \cdot \left(--4 \cdot \frac{\left(\pi \cdot u\right) \cdot \color{blue}{0.5} + \left(-0.25\right) \cdot \pi}{s}\right) \]
    6. metadata-eval10.7%

      \[\leadsto s \cdot \left(--4 \cdot \frac{\left(\pi \cdot u\right) \cdot 0.5 + \color{blue}{-0.25} \cdot \pi}{s}\right) \]
    7. *-commutative10.7%

      \[\leadsto s \cdot \left(--4 \cdot \frac{\left(\pi \cdot u\right) \cdot 0.5 + \color{blue}{\pi \cdot -0.25}}{s}\right) \]
  5. Simplified10.7%

    \[\leadsto s \cdot \left(-\color{blue}{-4 \cdot \frac{\left(\pi \cdot u\right) \cdot 0.5 + \pi \cdot -0.25}{s}}\right) \]
  6. Step-by-step derivation
    1. expm1-log1p-u0.4%

      \[\leadsto s \cdot \left(--4 \cdot \color{blue}{\mathsf{expm1}\left(\mathsf{log1p}\left(\frac{\left(\pi \cdot u\right) \cdot 0.5 + \pi \cdot -0.25}{s}\right)\right)}\right) \]
    2. *-commutative0.4%

      \[\leadsto s \cdot \left(--4 \cdot \mathsf{expm1}\left(\mathsf{log1p}\left(\frac{\color{blue}{\left(u \cdot \pi\right)} \cdot 0.5 + \pi \cdot -0.25}{s}\right)\right)\right) \]
    3. *-commutative0.4%

      \[\leadsto s \cdot \left(--4 \cdot \mathsf{expm1}\left(\mathsf{log1p}\left(\frac{\color{blue}{0.5 \cdot \left(u \cdot \pi\right)} + \pi \cdot -0.25}{s}\right)\right)\right) \]
    4. fma-def0.4%

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

    \[\leadsto s \cdot \left(--4 \cdot \color{blue}{\mathsf{expm1}\left(\mathsf{log1p}\left(\frac{\mathsf{fma}\left(0.5, u \cdot \pi, \pi \cdot -0.25\right)}{s}\right)\right)}\right) \]
  8. Step-by-step derivation
    1. fma-udef0.4%

      \[\leadsto s \cdot \left(--4 \cdot \mathsf{expm1}\left(\mathsf{log1p}\left(\frac{\color{blue}{0.5 \cdot \left(u \cdot \pi\right) + \pi \cdot -0.25}}{s}\right)\right)\right) \]
    2. *-commutative0.4%

      \[\leadsto s \cdot \left(--4 \cdot \mathsf{expm1}\left(\mathsf{log1p}\left(\frac{0.5 \cdot \color{blue}{\left(\pi \cdot u\right)} + \pi \cdot -0.25}{s}\right)\right)\right) \]
  9. Applied egg-rr0.4%

    \[\leadsto s \cdot \left(--4 \cdot \mathsf{expm1}\left(\mathsf{log1p}\left(\frac{\color{blue}{0.5 \cdot \left(\pi \cdot u\right) + \pi \cdot -0.25}}{s}\right)\right)\right) \]
  10. Applied egg-rr28.8%

    \[\leadsto s \cdot \left(--4 \cdot \color{blue}{\left(\left(s \cdot \left(\pi \cdot \mathsf{fma}\left(u, 0.5, -0.25\right)\right) + -1\right) + -1\right)}\right) \]
  11. Step-by-step derivation
    1. associate-+l+28.8%

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

      \[\leadsto s \cdot \left(--4 \cdot \left(s \cdot \left(\pi \cdot \mathsf{fma}\left(u, 0.5, -0.25\right)\right) + \color{blue}{-2}\right)\right) \]
  12. Simplified28.8%

    \[\leadsto s \cdot \left(--4 \cdot \color{blue}{\left(s \cdot \left(\pi \cdot \mathsf{fma}\left(u, 0.5, -0.25\right)\right) + -2\right)}\right) \]
  13. Final simplification28.8%

    \[\leadsto s \cdot \left(-4 \cdot \left(\left(--2\right) - s \cdot \left(\pi \cdot \mathsf{fma}\left(u, 0.5, -0.25\right)\right)\right)\right) \]

Alternative 4: 13.5% accurate, 3.5× speedup?

\[\begin{array}{l} \\ s \cdot \left(-4 \cdot \frac{-s}{\pi \cdot \mathsf{fma}\left(u, 0.5, -0.25\right)}\right) \end{array} \]
(FPCore (u s)
 :precision binary32
 (* s (* -4.0 (/ (- s) (* PI (fma u 0.5 -0.25))))))
float code(float u, float s) {
	return s * (-4.0f * (-s / (((float) M_PI) * fmaf(u, 0.5f, -0.25f))));
}
function code(u, s)
	return Float32(s * Float32(Float32(-4.0) * Float32(Float32(-s) / Float32(Float32(pi) * fma(u, Float32(0.5), Float32(-0.25))))))
end
\begin{array}{l}

\\
s \cdot \left(-4 \cdot \frac{-s}{\pi \cdot \mathsf{fma}\left(u, 0.5, -0.25\right)}\right)
\end{array}
Derivation
  1. Initial program 99.0%

    \[\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.0%

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

    \[\leadsto s \cdot \left(-\color{blue}{-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) \]
  4. Step-by-step derivation
    1. associate--r+10.7%

      \[\leadsto s \cdot \left(--4 \cdot \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}\right) \]
    2. cancel-sign-sub-inv10.7%

      \[\leadsto s \cdot \left(--4 \cdot \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}\right) \]
    3. distribute-rgt-out--10.7%

      \[\leadsto s \cdot \left(--4 \cdot \frac{\color{blue}{\left(u \cdot \pi\right) \cdot \left(0.25 - -0.25\right)} + \left(-0.25\right) \cdot \pi}{s}\right) \]
    4. *-commutative10.7%

      \[\leadsto s \cdot \left(--4 \cdot \frac{\color{blue}{\left(\pi \cdot u\right)} \cdot \left(0.25 - -0.25\right) + \left(-0.25\right) \cdot \pi}{s}\right) \]
    5. metadata-eval10.7%

      \[\leadsto s \cdot \left(--4 \cdot \frac{\left(\pi \cdot u\right) \cdot \color{blue}{0.5} + \left(-0.25\right) \cdot \pi}{s}\right) \]
    6. metadata-eval10.7%

      \[\leadsto s \cdot \left(--4 \cdot \frac{\left(\pi \cdot u\right) \cdot 0.5 + \color{blue}{-0.25} \cdot \pi}{s}\right) \]
    7. *-commutative10.7%

      \[\leadsto s \cdot \left(--4 \cdot \frac{\left(\pi \cdot u\right) \cdot 0.5 + \color{blue}{\pi \cdot -0.25}}{s}\right) \]
  5. Simplified10.7%

    \[\leadsto s \cdot \left(-\color{blue}{-4 \cdot \frac{\left(\pi \cdot u\right) \cdot 0.5 + \pi \cdot -0.25}{s}}\right) \]
  6. Step-by-step derivation
    1. expm1-log1p-u0.4%

      \[\leadsto s \cdot \left(--4 \cdot \color{blue}{\mathsf{expm1}\left(\mathsf{log1p}\left(\frac{\left(\pi \cdot u\right) \cdot 0.5 + \pi \cdot -0.25}{s}\right)\right)}\right) \]
    2. *-commutative0.4%

      \[\leadsto s \cdot \left(--4 \cdot \mathsf{expm1}\left(\mathsf{log1p}\left(\frac{\color{blue}{\left(u \cdot \pi\right)} \cdot 0.5 + \pi \cdot -0.25}{s}\right)\right)\right) \]
    3. *-commutative0.4%

      \[\leadsto s \cdot \left(--4 \cdot \mathsf{expm1}\left(\mathsf{log1p}\left(\frac{\color{blue}{0.5 \cdot \left(u \cdot \pi\right)} + \pi \cdot -0.25}{s}\right)\right)\right) \]
    4. fma-def0.4%

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

    \[\leadsto s \cdot \left(--4 \cdot \color{blue}{\mathsf{expm1}\left(\mathsf{log1p}\left(\frac{\mathsf{fma}\left(0.5, u \cdot \pi, \pi \cdot -0.25\right)}{s}\right)\right)}\right) \]
  8. Step-by-step derivation
    1. fma-udef0.4%

      \[\leadsto s \cdot \left(--4 \cdot \mathsf{expm1}\left(\mathsf{log1p}\left(\frac{\color{blue}{0.5 \cdot \left(u \cdot \pi\right) + \pi \cdot -0.25}}{s}\right)\right)\right) \]
    2. *-commutative0.4%

      \[\leadsto s \cdot \left(--4 \cdot \mathsf{expm1}\left(\mathsf{log1p}\left(\frac{0.5 \cdot \color{blue}{\left(\pi \cdot u\right)} + \pi \cdot -0.25}{s}\right)\right)\right) \]
  9. Applied egg-rr0.4%

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

    \[\leadsto s \cdot \left(--4 \cdot \color{blue}{\frac{s}{\pi \cdot \mathsf{fma}\left(u, 0.5, -0.25\right)}}\right) \]
  11. Final simplification13.0%

    \[\leadsto s \cdot \left(-4 \cdot \frac{-s}{\pi \cdot \mathsf{fma}\left(u, 0.5, -0.25\right)}\right) \]

Alternative 5: 13.3% accurate, 7.0× speedup?

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

\\
s \cdot \left(-s \cdot \pi\right)
\end{array}
Derivation
  1. Initial program 99.0%

    \[\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.0%

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

    \[\leadsto s \cdot \left(-\color{blue}{\frac{\pi}{s}}\right) \]
  4. Applied egg-rr12.8%

    \[\leadsto \color{blue}{0 - s \cdot \left(\pi \cdot s\right)} \]
  5. Step-by-step derivation
    1. neg-sub012.8%

      \[\leadsto \color{blue}{-s \cdot \left(\pi \cdot s\right)} \]
    2. distribute-rgt-neg-in12.8%

      \[\leadsto \color{blue}{s \cdot \left(-\pi \cdot s\right)} \]
    3. distribute-rgt-neg-in12.8%

      \[\leadsto s \cdot \color{blue}{\left(\pi \cdot \left(-s\right)\right)} \]
  6. Simplified12.8%

    \[\leadsto \color{blue}{s \cdot \left(\pi \cdot \left(-s\right)\right)} \]
  7. Final simplification12.8%

    \[\leadsto s \cdot \left(-s \cdot \pi\right) \]

Alternative 6: 11.3% accurate, 7.2× 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.0%

    \[\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.0%

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

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

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

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

    \[\leadsto -\pi \]

Alternative 7: 9.5% accurate, 121.7× speedup?

\[\begin{array}{l} \\ s \cdot \left(-s \cdot -4\right) \end{array} \]
(FPCore (u s) :precision binary32 (* s (- (* s -4.0))))
float code(float u, float s) {
	return s * -(s * -4.0f);
}
real(4) function code(u, s)
    real(4), intent (in) :: u
    real(4), intent (in) :: s
    code = s * -(s * (-4.0e0))
end function
function code(u, s)
	return Float32(s * Float32(-Float32(s * Float32(-4.0))))
end
function tmp = code(u, s)
	tmp = s * -(s * single(-4.0));
end
\begin{array}{l}

\\
s \cdot \left(-s \cdot -4\right)
\end{array}
Derivation
  1. Initial program 99.0%

    \[\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.0%

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

    \[\leadsto s \cdot \left(-\color{blue}{-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) \]
  4. Step-by-step derivation
    1. associate--r+10.7%

      \[\leadsto s \cdot \left(--4 \cdot \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}\right) \]
    2. cancel-sign-sub-inv10.7%

      \[\leadsto s \cdot \left(--4 \cdot \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}\right) \]
    3. distribute-rgt-out--10.7%

      \[\leadsto s \cdot \left(--4 \cdot \frac{\color{blue}{\left(u \cdot \pi\right) \cdot \left(0.25 - -0.25\right)} + \left(-0.25\right) \cdot \pi}{s}\right) \]
    4. *-commutative10.7%

      \[\leadsto s \cdot \left(--4 \cdot \frac{\color{blue}{\left(\pi \cdot u\right)} \cdot \left(0.25 - -0.25\right) + \left(-0.25\right) \cdot \pi}{s}\right) \]
    5. metadata-eval10.7%

      \[\leadsto s \cdot \left(--4 \cdot \frac{\left(\pi \cdot u\right) \cdot \color{blue}{0.5} + \left(-0.25\right) \cdot \pi}{s}\right) \]
    6. metadata-eval10.7%

      \[\leadsto s \cdot \left(--4 \cdot \frac{\left(\pi \cdot u\right) \cdot 0.5 + \color{blue}{-0.25} \cdot \pi}{s}\right) \]
    7. *-commutative10.7%

      \[\leadsto s \cdot \left(--4 \cdot \frac{\left(\pi \cdot u\right) \cdot 0.5 + \color{blue}{\pi \cdot -0.25}}{s}\right) \]
  5. Simplified10.7%

    \[\leadsto s \cdot \left(-\color{blue}{-4 \cdot \frac{\left(\pi \cdot u\right) \cdot 0.5 + \pi \cdot -0.25}{s}}\right) \]
  6. Step-by-step derivation
    1. expm1-log1p-u0.4%

      \[\leadsto s \cdot \left(--4 \cdot \color{blue}{\mathsf{expm1}\left(\mathsf{log1p}\left(\frac{\left(\pi \cdot u\right) \cdot 0.5 + \pi \cdot -0.25}{s}\right)\right)}\right) \]
    2. *-commutative0.4%

      \[\leadsto s \cdot \left(--4 \cdot \mathsf{expm1}\left(\mathsf{log1p}\left(\frac{\color{blue}{\left(u \cdot \pi\right)} \cdot 0.5 + \pi \cdot -0.25}{s}\right)\right)\right) \]
    3. *-commutative0.4%

      \[\leadsto s \cdot \left(--4 \cdot \mathsf{expm1}\left(\mathsf{log1p}\left(\frac{\color{blue}{0.5 \cdot \left(u \cdot \pi\right)} + \pi \cdot -0.25}{s}\right)\right)\right) \]
    4. fma-def0.4%

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

    \[\leadsto s \cdot \left(--4 \cdot \color{blue}{\mathsf{expm1}\left(\mathsf{log1p}\left(\frac{\mathsf{fma}\left(0.5, u \cdot \pi, \pi \cdot -0.25\right)}{s}\right)\right)}\right) \]
  8. Step-by-step derivation
    1. fma-udef0.4%

      \[\leadsto s \cdot \left(--4 \cdot \mathsf{expm1}\left(\mathsf{log1p}\left(\frac{\color{blue}{0.5 \cdot \left(u \cdot \pi\right) + \pi \cdot -0.25}}{s}\right)\right)\right) \]
    2. *-commutative0.4%

      \[\leadsto s \cdot \left(--4 \cdot \mathsf{expm1}\left(\mathsf{log1p}\left(\frac{0.5 \cdot \color{blue}{\left(\pi \cdot u\right)} + \pi \cdot -0.25}{s}\right)\right)\right) \]
  9. Applied egg-rr0.4%

    \[\leadsto s \cdot \left(--4 \cdot \mathsf{expm1}\left(\mathsf{log1p}\left(\frac{\color{blue}{0.5 \cdot \left(\pi \cdot u\right) + \pi \cdot -0.25}}{s}\right)\right)\right) \]
  10. Applied egg-rr9.4%

    \[\leadsto s \cdot \left(--4 \cdot \color{blue}{\frac{s \cdot \left(\pi \cdot \mathsf{fma}\left(u, 0.5, -0.25\right)\right)}{\pi \cdot \mathsf{fma}\left(u, 0.5, -0.25\right)}}\right) \]
  11. Step-by-step derivation
    1. associate-/l*9.4%

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

      \[\leadsto s \cdot \left(--4 \cdot \frac{s}{\color{blue}{1}}\right) \]
    3. /-rgt-identity9.4%

      \[\leadsto s \cdot \left(--4 \cdot \color{blue}{s}\right) \]
  12. Simplified9.4%

    \[\leadsto s \cdot \left(--4 \cdot \color{blue}{s}\right) \]
  13. Final simplification9.4%

    \[\leadsto s \cdot \left(-s \cdot -4\right) \]

Reproduce

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