Logistic distribution

Percentage Accurate: 99.6% → 99.2%
Time: 12.3s
Alternatives: 10
Speedup: 2.9×

Specification

?
\[0 \leq s \land s \leq 1.0651631\]
\[\begin{array}{l} \\ \begin{array}{l} t_0 := e^{\frac{-\left|x\right|}{s}}\\ t_1 := 1 + t_0\\ \frac{t_0}{\left(s \cdot t_1\right) \cdot t_1} \end{array} \end{array} \]
(FPCore (x s)
 :precision binary32
 (let* ((t_0 (exp (/ (- (fabs x)) s))) (t_1 (+ 1.0 t_0)))
   (/ t_0 (* (* s t_1) t_1))))
float code(float x, float s) {
	float t_0 = expf((-fabsf(x) / s));
	float t_1 = 1.0f + t_0;
	return t_0 / ((s * t_1) * t_1);
}
real(4) function code(x, s)
    real(4), intent (in) :: x
    real(4), intent (in) :: s
    real(4) :: t_0
    real(4) :: t_1
    t_0 = exp((-abs(x) / s))
    t_1 = 1.0e0 + t_0
    code = t_0 / ((s * t_1) * t_1)
end function
function code(x, s)
	t_0 = exp(Float32(Float32(-abs(x)) / s))
	t_1 = Float32(Float32(1.0) + t_0)
	return Float32(t_0 / Float32(Float32(s * t_1) * t_1))
end
function tmp = code(x, s)
	t_0 = exp((-abs(x) / s));
	t_1 = single(1.0) + t_0;
	tmp = t_0 / ((s * t_1) * t_1);
end
\begin{array}{l}

\\
\begin{array}{l}
t_0 := e^{\frac{-\left|x\right|}{s}}\\
t_1 := 1 + t_0\\
\frac{t_0}{\left(s \cdot t_1\right) \cdot t_1}
\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 10 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.6% accurate, 1.0× speedup?

\[\begin{array}{l} \\ \begin{array}{l} t_0 := e^{\frac{-\left|x\right|}{s}}\\ t_1 := 1 + t_0\\ \frac{t_0}{\left(s \cdot t_1\right) \cdot t_1} \end{array} \end{array} \]
(FPCore (x s)
 :precision binary32
 (let* ((t_0 (exp (/ (- (fabs x)) s))) (t_1 (+ 1.0 t_0)))
   (/ t_0 (* (* s t_1) t_1))))
float code(float x, float s) {
	float t_0 = expf((-fabsf(x) / s));
	float t_1 = 1.0f + t_0;
	return t_0 / ((s * t_1) * t_1);
}
real(4) function code(x, s)
    real(4), intent (in) :: x
    real(4), intent (in) :: s
    real(4) :: t_0
    real(4) :: t_1
    t_0 = exp((-abs(x) / s))
    t_1 = 1.0e0 + t_0
    code = t_0 / ((s * t_1) * t_1)
end function
function code(x, s)
	t_0 = exp(Float32(Float32(-abs(x)) / s))
	t_1 = Float32(Float32(1.0) + t_0)
	return Float32(t_0 / Float32(Float32(s * t_1) * t_1))
end
function tmp = code(x, s)
	t_0 = exp((-abs(x) / s));
	t_1 = single(1.0) + t_0;
	tmp = t_0 / ((s * t_1) * t_1);
end
\begin{array}{l}

\\
\begin{array}{l}
t_0 := e^{\frac{-\left|x\right|}{s}}\\
t_1 := 1 + t_0\\
\frac{t_0}{\left(s \cdot t_1\right) \cdot t_1}
\end{array}
\end{array}

Alternative 1: 99.2% accurate, 2.0× speedup?

\[\begin{array}{l} x = |x|\\ \\ \frac{\frac{1}{s}}{\left(1 + e^{\frac{-x}{s}}\right) \cdot \left(1 + e^{\frac{\left|x\right|}{s}}\right)} \end{array} \]
NOTE: x should be positive before calling this function
(FPCore (x s)
 :precision binary32
 (/ (/ 1.0 s) (* (+ 1.0 (exp (/ (- x) s))) (+ 1.0 (exp (/ (fabs x) s))))))
x = abs(x);
float code(float x, float s) {
	return (1.0f / s) / ((1.0f + expf((-x / s))) * (1.0f + expf((fabsf(x) / s))));
}
NOTE: x should be positive before calling this function
real(4) function code(x, s)
    real(4), intent (in) :: x
    real(4), intent (in) :: s
    code = (1.0e0 / s) / ((1.0e0 + exp((-x / s))) * (1.0e0 + exp((abs(x) / s))))
end function
x = abs(x)
function code(x, s)
	return Float32(Float32(Float32(1.0) / s) / Float32(Float32(Float32(1.0) + exp(Float32(Float32(-x) / s))) * Float32(Float32(1.0) + exp(Float32(abs(x) / s)))))
end
x = abs(x)
function tmp = code(x, s)
	tmp = (single(1.0) / s) / ((single(1.0) + exp((-x / s))) * (single(1.0) + exp((abs(x) / s))));
end
\begin{array}{l}
x = |x|\\
\\
\frac{\frac{1}{s}}{\left(1 + e^{\frac{-x}{s}}\right) \cdot \left(1 + e^{\frac{\left|x\right|}{s}}\right)}
\end{array}
Derivation
  1. Initial program 99.4%

    \[\frac{e^{\frac{-\left|x\right|}{s}}}{\left(s \cdot \left(1 + e^{\frac{-\left|x\right|}{s}}\right)\right) \cdot \left(1 + e^{\frac{-\left|x\right|}{s}}\right)} \]
  2. Simplified99.5%

    \[\leadsto \color{blue}{\frac{1}{\mathsf{fma}\left(s, e^{\frac{\left|x\right|}{-s}}, s\right) \cdot \left(1 + e^{\frac{\left|x\right|}{s}}\right)}} \]
  3. Step-by-step derivation
    1. *-un-lft-identity99.5%

      \[\leadsto \frac{1}{\mathsf{fma}\left(s, e^{\frac{\color{blue}{1 \cdot \left|x\right|}}{-s}}, s\right) \cdot \left(1 + e^{\frac{\left|x\right|}{s}}\right)} \]
    2. neg-mul-199.5%

      \[\leadsto \frac{1}{\mathsf{fma}\left(s, e^{\frac{1 \cdot \left|x\right|}{\color{blue}{-1 \cdot s}}}, s\right) \cdot \left(1 + e^{\frac{\left|x\right|}{s}}\right)} \]
    3. times-frac99.5%

      \[\leadsto \frac{1}{\mathsf{fma}\left(s, e^{\color{blue}{\frac{1}{-1} \cdot \frac{\left|x\right|}{s}}}, s\right) \cdot \left(1 + e^{\frac{\left|x\right|}{s}}\right)} \]
    4. metadata-eval99.5%

      \[\leadsto \frac{1}{\mathsf{fma}\left(s, e^{\color{blue}{-1} \cdot \frac{\left|x\right|}{s}}, s\right) \cdot \left(1 + e^{\frac{\left|x\right|}{s}}\right)} \]
    5. metadata-eval99.5%

      \[\leadsto \frac{1}{\mathsf{fma}\left(s, e^{\color{blue}{\frac{-1}{1}} \cdot \frac{\left|x\right|}{s}}, s\right) \cdot \left(1 + e^{\frac{\left|x\right|}{s}}\right)} \]
    6. times-frac99.5%

      \[\leadsto \frac{1}{\mathsf{fma}\left(s, e^{\color{blue}{\frac{-1 \cdot \left|x\right|}{1 \cdot s}}}, s\right) \cdot \left(1 + e^{\frac{\left|x\right|}{s}}\right)} \]
    7. neg-mul-199.5%

      \[\leadsto \frac{1}{\mathsf{fma}\left(s, e^{\frac{\color{blue}{-\left|x\right|}}{1 \cdot s}}, s\right) \cdot \left(1 + e^{\frac{\left|x\right|}{s}}\right)} \]
    8. *-un-lft-identity99.5%

      \[\leadsto \frac{1}{\mathsf{fma}\left(s, e^{\frac{-\left|x\right|}{\color{blue}{s}}}, s\right) \cdot \left(1 + e^{\frac{\left|x\right|}{s}}\right)} \]
    9. distribute-frac-neg99.5%

      \[\leadsto \frac{1}{\mathsf{fma}\left(s, e^{\color{blue}{-\frac{\left|x\right|}{s}}}, s\right) \cdot \left(1 + e^{\frac{\left|x\right|}{s}}\right)} \]
    10. rec-exp99.4%

      \[\leadsto \frac{1}{\mathsf{fma}\left(s, \color{blue}{\frac{1}{e^{\frac{\left|x\right|}{s}}}}, s\right) \cdot \left(1 + e^{\frac{\left|x\right|}{s}}\right)} \]
    11. add-sqr-sqrt45.6%

      \[\leadsto \frac{1}{\mathsf{fma}\left(s, \frac{1}{e^{\frac{\left|\color{blue}{\sqrt{x} \cdot \sqrt{x}}\right|}{s}}}, s\right) \cdot \left(1 + e^{\frac{\left|x\right|}{s}}\right)} \]
    12. fabs-sqr45.6%

      \[\leadsto \frac{1}{\mathsf{fma}\left(s, \frac{1}{e^{\frac{\color{blue}{\sqrt{x} \cdot \sqrt{x}}}{s}}}, s\right) \cdot \left(1 + e^{\frac{\left|x\right|}{s}}\right)} \]
    13. add-sqr-sqrt96.1%

      \[\leadsto \frac{1}{\mathsf{fma}\left(s, \frac{1}{e^{\frac{\color{blue}{x}}{s}}}, s\right) \cdot \left(1 + e^{\frac{\left|x\right|}{s}}\right)} \]
  4. Applied egg-rr96.1%

    \[\leadsto \frac{1}{\mathsf{fma}\left(s, \color{blue}{\frac{1}{e^{\frac{x}{s}}}}, s\right) \cdot \left(1 + e^{\frac{\left|x\right|}{s}}\right)} \]
  5. Step-by-step derivation
    1. rec-exp96.1%

      \[\leadsto \frac{1}{\mathsf{fma}\left(s, \color{blue}{e^{-\frac{x}{s}}}, s\right) \cdot \left(1 + e^{\frac{\left|x\right|}{s}}\right)} \]
    2. distribute-neg-frac96.1%

      \[\leadsto \frac{1}{\mathsf{fma}\left(s, e^{\color{blue}{\frac{-x}{s}}}, s\right) \cdot \left(1 + e^{\frac{\left|x\right|}{s}}\right)} \]
  6. Simplified96.1%

    \[\leadsto \frac{1}{\mathsf{fma}\left(s, \color{blue}{e^{\frac{-x}{s}}}, s\right) \cdot \left(1 + e^{\frac{\left|x\right|}{s}}\right)} \]
  7. Taylor expanded in s around 0 96.1%

    \[\leadsto \color{blue}{\frac{1}{s \cdot \left(\left(1 + e^{-1 \cdot \frac{x}{s}}\right) \cdot \left(1 + e^{\frac{\left|x\right|}{s}}\right)\right)}} \]
  8. Step-by-step derivation
    1. associate-/r*96.2%

      \[\leadsto \color{blue}{\frac{\frac{1}{s}}{\left(1 + e^{-1 \cdot \frac{x}{s}}\right) \cdot \left(1 + e^{\frac{\left|x\right|}{s}}\right)}} \]
    2. associate-*r/96.2%

      \[\leadsto \frac{\frac{1}{s}}{\left(1 + e^{\color{blue}{\frac{-1 \cdot x}{s}}}\right) \cdot \left(1 + e^{\frac{\left|x\right|}{s}}\right)} \]
    3. mul-1-neg96.2%

      \[\leadsto \frac{\frac{1}{s}}{\left(1 + e^{\frac{\color{blue}{-x}}{s}}\right) \cdot \left(1 + e^{\frac{\left|x\right|}{s}}\right)} \]
  9. Simplified96.2%

    \[\leadsto \color{blue}{\frac{\frac{1}{s}}{\left(1 + e^{\frac{-x}{s}}\right) \cdot \left(1 + e^{\frac{\left|x\right|}{s}}\right)}} \]
  10. Final simplification96.2%

    \[\leadsto \frac{\frac{1}{s}}{\left(1 + e^{\frac{-x}{s}}\right) \cdot \left(1 + e^{\frac{\left|x\right|}{s}}\right)} \]

Alternative 2: 99.6% accurate, 2.9× speedup?

\[\begin{array}{l} x = |x|\\ \\ \frac{1}{s \cdot \left(\left(1 + e^{\frac{-x}{s}}\right) \cdot \left(1 + e^{\frac{x}{s}}\right)\right)} \end{array} \]
NOTE: x should be positive before calling this function
(FPCore (x s)
 :precision binary32
 (/ 1.0 (* s (* (+ 1.0 (exp (/ (- x) s))) (+ 1.0 (exp (/ x s)))))))
x = abs(x);
float code(float x, float s) {
	return 1.0f / (s * ((1.0f + expf((-x / s))) * (1.0f + expf((x / s)))));
}
NOTE: x should be positive before calling this function
real(4) function code(x, s)
    real(4), intent (in) :: x
    real(4), intent (in) :: s
    code = 1.0e0 / (s * ((1.0e0 + exp((-x / s))) * (1.0e0 + exp((x / s)))))
end function
x = abs(x)
function code(x, s)
	return Float32(Float32(1.0) / Float32(s * Float32(Float32(Float32(1.0) + exp(Float32(Float32(-x) / s))) * Float32(Float32(1.0) + exp(Float32(x / s))))))
end
x = abs(x)
function tmp = code(x, s)
	tmp = single(1.0) / (s * ((single(1.0) + exp((-x / s))) * (single(1.0) + exp((x / s)))));
end
\begin{array}{l}
x = |x|\\
\\
\frac{1}{s \cdot \left(\left(1 + e^{\frac{-x}{s}}\right) \cdot \left(1 + e^{\frac{x}{s}}\right)\right)}
\end{array}
Derivation
  1. Initial program 99.4%

    \[\frac{e^{\frac{-\left|x\right|}{s}}}{\left(s \cdot \left(1 + e^{\frac{-\left|x\right|}{s}}\right)\right) \cdot \left(1 + e^{\frac{-\left|x\right|}{s}}\right)} \]
  2. Simplified99.5%

    \[\leadsto \color{blue}{\frac{1}{\mathsf{fma}\left(s, e^{\frac{\left|x\right|}{-s}}, s\right) \cdot \left(1 + e^{\frac{\left|x\right|}{s}}\right)}} \]
  3. Step-by-step derivation
    1. *-un-lft-identity99.5%

      \[\leadsto \frac{1}{\mathsf{fma}\left(s, e^{\frac{\color{blue}{1 \cdot \left|x\right|}}{-s}}, s\right) \cdot \left(1 + e^{\frac{\left|x\right|}{s}}\right)} \]
    2. neg-mul-199.5%

      \[\leadsto \frac{1}{\mathsf{fma}\left(s, e^{\frac{1 \cdot \left|x\right|}{\color{blue}{-1 \cdot s}}}, s\right) \cdot \left(1 + e^{\frac{\left|x\right|}{s}}\right)} \]
    3. times-frac99.5%

      \[\leadsto \frac{1}{\mathsf{fma}\left(s, e^{\color{blue}{\frac{1}{-1} \cdot \frac{\left|x\right|}{s}}}, s\right) \cdot \left(1 + e^{\frac{\left|x\right|}{s}}\right)} \]
    4. metadata-eval99.5%

      \[\leadsto \frac{1}{\mathsf{fma}\left(s, e^{\color{blue}{-1} \cdot \frac{\left|x\right|}{s}}, s\right) \cdot \left(1 + e^{\frac{\left|x\right|}{s}}\right)} \]
    5. metadata-eval99.5%

      \[\leadsto \frac{1}{\mathsf{fma}\left(s, e^{\color{blue}{\frac{-1}{1}} \cdot \frac{\left|x\right|}{s}}, s\right) \cdot \left(1 + e^{\frac{\left|x\right|}{s}}\right)} \]
    6. times-frac99.5%

      \[\leadsto \frac{1}{\mathsf{fma}\left(s, e^{\color{blue}{\frac{-1 \cdot \left|x\right|}{1 \cdot s}}}, s\right) \cdot \left(1 + e^{\frac{\left|x\right|}{s}}\right)} \]
    7. neg-mul-199.5%

      \[\leadsto \frac{1}{\mathsf{fma}\left(s, e^{\frac{\color{blue}{-\left|x\right|}}{1 \cdot s}}, s\right) \cdot \left(1 + e^{\frac{\left|x\right|}{s}}\right)} \]
    8. *-un-lft-identity99.5%

      \[\leadsto \frac{1}{\mathsf{fma}\left(s, e^{\frac{-\left|x\right|}{\color{blue}{s}}}, s\right) \cdot \left(1 + e^{\frac{\left|x\right|}{s}}\right)} \]
    9. distribute-frac-neg99.5%

      \[\leadsto \frac{1}{\mathsf{fma}\left(s, e^{\color{blue}{-\frac{\left|x\right|}{s}}}, s\right) \cdot \left(1 + e^{\frac{\left|x\right|}{s}}\right)} \]
    10. rec-exp99.4%

      \[\leadsto \frac{1}{\mathsf{fma}\left(s, \color{blue}{\frac{1}{e^{\frac{\left|x\right|}{s}}}}, s\right) \cdot \left(1 + e^{\frac{\left|x\right|}{s}}\right)} \]
    11. add-sqr-sqrt45.6%

      \[\leadsto \frac{1}{\mathsf{fma}\left(s, \frac{1}{e^{\frac{\left|\color{blue}{\sqrt{x} \cdot \sqrt{x}}\right|}{s}}}, s\right) \cdot \left(1 + e^{\frac{\left|x\right|}{s}}\right)} \]
    12. fabs-sqr45.6%

      \[\leadsto \frac{1}{\mathsf{fma}\left(s, \frac{1}{e^{\frac{\color{blue}{\sqrt{x} \cdot \sqrt{x}}}{s}}}, s\right) \cdot \left(1 + e^{\frac{\left|x\right|}{s}}\right)} \]
    13. add-sqr-sqrt96.1%

      \[\leadsto \frac{1}{\mathsf{fma}\left(s, \frac{1}{e^{\frac{\color{blue}{x}}{s}}}, s\right) \cdot \left(1 + e^{\frac{\left|x\right|}{s}}\right)} \]
  4. Applied egg-rr96.1%

    \[\leadsto \frac{1}{\mathsf{fma}\left(s, \color{blue}{\frac{1}{e^{\frac{x}{s}}}}, s\right) \cdot \left(1 + e^{\frac{\left|x\right|}{s}}\right)} \]
  5. Step-by-step derivation
    1. rec-exp96.1%

      \[\leadsto \frac{1}{\mathsf{fma}\left(s, \color{blue}{e^{-\frac{x}{s}}}, s\right) \cdot \left(1 + e^{\frac{\left|x\right|}{s}}\right)} \]
    2. distribute-neg-frac96.1%

      \[\leadsto \frac{1}{\mathsf{fma}\left(s, e^{\color{blue}{\frac{-x}{s}}}, s\right) \cdot \left(1 + e^{\frac{\left|x\right|}{s}}\right)} \]
  6. Simplified96.1%

    \[\leadsto \frac{1}{\mathsf{fma}\left(s, \color{blue}{e^{\frac{-x}{s}}}, s\right) \cdot \left(1 + e^{\frac{\left|x\right|}{s}}\right)} \]
  7. Step-by-step derivation
    1. add-sqr-sqrt45.6%

      \[\leadsto \frac{1}{\mathsf{fma}\left(s, e^{\frac{-x}{s}}, s\right) \cdot \left(1 + e^{\frac{\left|\color{blue}{\sqrt{x} \cdot \sqrt{x}}\right|}{s}}\right)} \]
    2. fabs-sqr45.6%

      \[\leadsto \frac{1}{\mathsf{fma}\left(s, e^{\frac{-x}{s}}, s\right) \cdot \left(1 + e^{\frac{\color{blue}{\sqrt{x} \cdot \sqrt{x}}}{s}}\right)} \]
    3. add-sqr-sqrt99.5%

      \[\leadsto \frac{1}{\mathsf{fma}\left(s, e^{\frac{-x}{s}}, s\right) \cdot \left(1 + e^{\frac{\color{blue}{x}}{s}}\right)} \]
    4. expm1-log1p-u99.4%

      \[\leadsto \frac{1}{\mathsf{fma}\left(s, e^{\frac{-x}{s}}, s\right) \cdot \color{blue}{\mathsf{expm1}\left(\mathsf{log1p}\left(1 + e^{\frac{x}{s}}\right)\right)}} \]
    5. expm1-udef99.4%

      \[\leadsto \frac{1}{\mathsf{fma}\left(s, e^{\frac{-x}{s}}, s\right) \cdot \color{blue}{\left(e^{\mathsf{log1p}\left(1 + e^{\frac{x}{s}}\right)} - 1\right)}} \]
  8. Applied egg-rr99.4%

    \[\leadsto \frac{1}{\mathsf{fma}\left(s, e^{\frac{-x}{s}}, s\right) \cdot \color{blue}{\left(e^{\mathsf{log1p}\left(1 + e^{\frac{x}{s}}\right)} - 1\right)}} \]
  9. Step-by-step derivation
    1. expm1-def99.4%

      \[\leadsto \frac{1}{\mathsf{fma}\left(s, e^{\frac{-x}{s}}, s\right) \cdot \color{blue}{\mathsf{expm1}\left(\mathsf{log1p}\left(1 + e^{\frac{x}{s}}\right)\right)}} \]
    2. expm1-log1p99.5%

      \[\leadsto \frac{1}{\mathsf{fma}\left(s, e^{\frac{-x}{s}}, s\right) \cdot \color{blue}{\left(1 + e^{\frac{x}{s}}\right)}} \]
  10. Simplified99.5%

    \[\leadsto \frac{1}{\mathsf{fma}\left(s, e^{\frac{-x}{s}}, s\right) \cdot \color{blue}{\left(1 + e^{\frac{x}{s}}\right)}} \]
  11. Taylor expanded in s around 0 99.4%

    \[\leadsto \frac{1}{\color{blue}{s \cdot \left(\left(1 + e^{-1 \cdot \frac{x}{s}}\right) \cdot \left(1 + e^{\frac{x}{s}}\right)\right)}} \]
  12. Step-by-step derivation
    1. *-commutative99.4%

      \[\leadsto \frac{1}{s \cdot \color{blue}{\left(\left(1 + e^{\frac{x}{s}}\right) \cdot \left(1 + e^{-1 \cdot \frac{x}{s}}\right)\right)}} \]
    2. neg-mul-199.4%

      \[\leadsto \frac{1}{s \cdot \left(\left(1 + e^{\frac{x}{s}}\right) \cdot \left(1 + e^{\color{blue}{-\frac{x}{s}}}\right)\right)} \]
  13. Simplified99.4%

    \[\leadsto \frac{1}{\color{blue}{s \cdot \left(\left(1 + e^{\frac{x}{s}}\right) \cdot \left(1 + e^{-\frac{x}{s}}\right)\right)}} \]
  14. Final simplification99.4%

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

Alternative 3: 99.5% accurate, 2.9× speedup?

\[\begin{array}{l} x = |x|\\ \\ \frac{1}{\left(1 + e^{\frac{x}{s}}\right) \cdot \left(s + s \cdot e^{\frac{-x}{s}}\right)} \end{array} \]
NOTE: x should be positive before calling this function
(FPCore (x s)
 :precision binary32
 (/ 1.0 (* (+ 1.0 (exp (/ x s))) (+ s (* s (exp (/ (- x) s)))))))
x = abs(x);
float code(float x, float s) {
	return 1.0f / ((1.0f + expf((x / s))) * (s + (s * expf((-x / s)))));
}
NOTE: x should be positive before calling this function
real(4) function code(x, s)
    real(4), intent (in) :: x
    real(4), intent (in) :: s
    code = 1.0e0 / ((1.0e0 + exp((x / s))) * (s + (s * exp((-x / s)))))
end function
x = abs(x)
function code(x, s)
	return Float32(Float32(1.0) / Float32(Float32(Float32(1.0) + exp(Float32(x / s))) * Float32(s + Float32(s * exp(Float32(Float32(-x) / s))))))
end
x = abs(x)
function tmp = code(x, s)
	tmp = single(1.0) / ((single(1.0) + exp((x / s))) * (s + (s * exp((-x / s)))));
end
\begin{array}{l}
x = |x|\\
\\
\frac{1}{\left(1 + e^{\frac{x}{s}}\right) \cdot \left(s + s \cdot e^{\frac{-x}{s}}\right)}
\end{array}
Derivation
  1. Initial program 99.4%

    \[\frac{e^{\frac{-\left|x\right|}{s}}}{\left(s \cdot \left(1 + e^{\frac{-\left|x\right|}{s}}\right)\right) \cdot \left(1 + e^{\frac{-\left|x\right|}{s}}\right)} \]
  2. Simplified99.5%

    \[\leadsto \color{blue}{\frac{1}{\mathsf{fma}\left(s, e^{\frac{\left|x\right|}{-s}}, s\right) \cdot \left(1 + e^{\frac{\left|x\right|}{s}}\right)}} \]
  3. Step-by-step derivation
    1. *-un-lft-identity99.5%

      \[\leadsto \frac{1}{\mathsf{fma}\left(s, e^{\frac{\color{blue}{1 \cdot \left|x\right|}}{-s}}, s\right) \cdot \left(1 + e^{\frac{\left|x\right|}{s}}\right)} \]
    2. neg-mul-199.5%

      \[\leadsto \frac{1}{\mathsf{fma}\left(s, e^{\frac{1 \cdot \left|x\right|}{\color{blue}{-1 \cdot s}}}, s\right) \cdot \left(1 + e^{\frac{\left|x\right|}{s}}\right)} \]
    3. times-frac99.5%

      \[\leadsto \frac{1}{\mathsf{fma}\left(s, e^{\color{blue}{\frac{1}{-1} \cdot \frac{\left|x\right|}{s}}}, s\right) \cdot \left(1 + e^{\frac{\left|x\right|}{s}}\right)} \]
    4. metadata-eval99.5%

      \[\leadsto \frac{1}{\mathsf{fma}\left(s, e^{\color{blue}{-1} \cdot \frac{\left|x\right|}{s}}, s\right) \cdot \left(1 + e^{\frac{\left|x\right|}{s}}\right)} \]
    5. metadata-eval99.5%

      \[\leadsto \frac{1}{\mathsf{fma}\left(s, e^{\color{blue}{\frac{-1}{1}} \cdot \frac{\left|x\right|}{s}}, s\right) \cdot \left(1 + e^{\frac{\left|x\right|}{s}}\right)} \]
    6. times-frac99.5%

      \[\leadsto \frac{1}{\mathsf{fma}\left(s, e^{\color{blue}{\frac{-1 \cdot \left|x\right|}{1 \cdot s}}}, s\right) \cdot \left(1 + e^{\frac{\left|x\right|}{s}}\right)} \]
    7. neg-mul-199.5%

      \[\leadsto \frac{1}{\mathsf{fma}\left(s, e^{\frac{\color{blue}{-\left|x\right|}}{1 \cdot s}}, s\right) \cdot \left(1 + e^{\frac{\left|x\right|}{s}}\right)} \]
    8. *-un-lft-identity99.5%

      \[\leadsto \frac{1}{\mathsf{fma}\left(s, e^{\frac{-\left|x\right|}{\color{blue}{s}}}, s\right) \cdot \left(1 + e^{\frac{\left|x\right|}{s}}\right)} \]
    9. distribute-frac-neg99.5%

      \[\leadsto \frac{1}{\mathsf{fma}\left(s, e^{\color{blue}{-\frac{\left|x\right|}{s}}}, s\right) \cdot \left(1 + e^{\frac{\left|x\right|}{s}}\right)} \]
    10. rec-exp99.4%

      \[\leadsto \frac{1}{\mathsf{fma}\left(s, \color{blue}{\frac{1}{e^{\frac{\left|x\right|}{s}}}}, s\right) \cdot \left(1 + e^{\frac{\left|x\right|}{s}}\right)} \]
    11. add-sqr-sqrt45.6%

      \[\leadsto \frac{1}{\mathsf{fma}\left(s, \frac{1}{e^{\frac{\left|\color{blue}{\sqrt{x} \cdot \sqrt{x}}\right|}{s}}}, s\right) \cdot \left(1 + e^{\frac{\left|x\right|}{s}}\right)} \]
    12. fabs-sqr45.6%

      \[\leadsto \frac{1}{\mathsf{fma}\left(s, \frac{1}{e^{\frac{\color{blue}{\sqrt{x} \cdot \sqrt{x}}}{s}}}, s\right) \cdot \left(1 + e^{\frac{\left|x\right|}{s}}\right)} \]
    13. add-sqr-sqrt96.1%

      \[\leadsto \frac{1}{\mathsf{fma}\left(s, \frac{1}{e^{\frac{\color{blue}{x}}{s}}}, s\right) \cdot \left(1 + e^{\frac{\left|x\right|}{s}}\right)} \]
  4. Applied egg-rr96.1%

    \[\leadsto \frac{1}{\mathsf{fma}\left(s, \color{blue}{\frac{1}{e^{\frac{x}{s}}}}, s\right) \cdot \left(1 + e^{\frac{\left|x\right|}{s}}\right)} \]
  5. Step-by-step derivation
    1. rec-exp96.1%

      \[\leadsto \frac{1}{\mathsf{fma}\left(s, \color{blue}{e^{-\frac{x}{s}}}, s\right) \cdot \left(1 + e^{\frac{\left|x\right|}{s}}\right)} \]
    2. distribute-neg-frac96.1%

      \[\leadsto \frac{1}{\mathsf{fma}\left(s, e^{\color{blue}{\frac{-x}{s}}}, s\right) \cdot \left(1 + e^{\frac{\left|x\right|}{s}}\right)} \]
  6. Simplified96.1%

    \[\leadsto \frac{1}{\mathsf{fma}\left(s, \color{blue}{e^{\frac{-x}{s}}}, s\right) \cdot \left(1 + e^{\frac{\left|x\right|}{s}}\right)} \]
  7. Step-by-step derivation
    1. add-sqr-sqrt45.6%

      \[\leadsto \frac{1}{\mathsf{fma}\left(s, e^{\frac{-x}{s}}, s\right) \cdot \left(1 + e^{\frac{\left|\color{blue}{\sqrt{x} \cdot \sqrt{x}}\right|}{s}}\right)} \]
    2. fabs-sqr45.6%

      \[\leadsto \frac{1}{\mathsf{fma}\left(s, e^{\frac{-x}{s}}, s\right) \cdot \left(1 + e^{\frac{\color{blue}{\sqrt{x} \cdot \sqrt{x}}}{s}}\right)} \]
    3. add-sqr-sqrt99.5%

      \[\leadsto \frac{1}{\mathsf{fma}\left(s, e^{\frac{-x}{s}}, s\right) \cdot \left(1 + e^{\frac{\color{blue}{x}}{s}}\right)} \]
    4. expm1-log1p-u99.4%

      \[\leadsto \frac{1}{\mathsf{fma}\left(s, e^{\frac{-x}{s}}, s\right) \cdot \color{blue}{\mathsf{expm1}\left(\mathsf{log1p}\left(1 + e^{\frac{x}{s}}\right)\right)}} \]
    5. expm1-udef99.4%

      \[\leadsto \frac{1}{\mathsf{fma}\left(s, e^{\frac{-x}{s}}, s\right) \cdot \color{blue}{\left(e^{\mathsf{log1p}\left(1 + e^{\frac{x}{s}}\right)} - 1\right)}} \]
  8. Applied egg-rr99.4%

    \[\leadsto \frac{1}{\mathsf{fma}\left(s, e^{\frac{-x}{s}}, s\right) \cdot \color{blue}{\left(e^{\mathsf{log1p}\left(1 + e^{\frac{x}{s}}\right)} - 1\right)}} \]
  9. Step-by-step derivation
    1. expm1-def99.4%

      \[\leadsto \frac{1}{\mathsf{fma}\left(s, e^{\frac{-x}{s}}, s\right) \cdot \color{blue}{\mathsf{expm1}\left(\mathsf{log1p}\left(1 + e^{\frac{x}{s}}\right)\right)}} \]
    2. expm1-log1p99.5%

      \[\leadsto \frac{1}{\mathsf{fma}\left(s, e^{\frac{-x}{s}}, s\right) \cdot \color{blue}{\left(1 + e^{\frac{x}{s}}\right)}} \]
  10. Simplified99.5%

    \[\leadsto \frac{1}{\mathsf{fma}\left(s, e^{\frac{-x}{s}}, s\right) \cdot \color{blue}{\left(1 + e^{\frac{x}{s}}\right)}} \]
  11. Taylor expanded in x around inf 99.4%

    \[\leadsto \frac{1}{\color{blue}{\left(1 + e^{\frac{x}{s}}\right) \cdot \left(s + s \cdot e^{-1 \cdot \frac{x}{s}}\right)}} \]
  12. Step-by-step derivation
    1. mul-1-neg99.4%

      \[\leadsto \frac{1}{\left(1 + e^{\frac{x}{s}}\right) \cdot \left(s + s \cdot e^{\color{blue}{-\frac{x}{s}}}\right)} \]
    2. exp-neg99.4%

      \[\leadsto \frac{1}{\left(1 + e^{\frac{x}{s}}\right) \cdot \left(s + s \cdot \color{blue}{\frac{1}{e^{\frac{x}{s}}}}\right)} \]
  13. Applied egg-rr99.4%

    \[\leadsto \frac{1}{\left(1 + e^{\frac{x}{s}}\right) \cdot \left(s + s \cdot \color{blue}{\frac{1}{e^{\frac{x}{s}}}}\right)} \]
  14. Step-by-step derivation
    1. rec-exp99.4%

      \[\leadsto \frac{1}{\left(1 + e^{\frac{x}{s}}\right) \cdot \left(s + s \cdot \color{blue}{e^{-\frac{x}{s}}}\right)} \]
    2. distribute-neg-frac99.4%

      \[\leadsto \frac{1}{\left(1 + e^{\frac{x}{s}}\right) \cdot \left(s + s \cdot e^{\color{blue}{\frac{-x}{s}}}\right)} \]
  15. Simplified99.4%

    \[\leadsto \frac{1}{\left(1 + e^{\frac{x}{s}}\right) \cdot \left(s + s \cdot \color{blue}{e^{\frac{-x}{s}}}\right)} \]
  16. Final simplification99.4%

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

Alternative 4: 95.1% accurate, 5.5× speedup?

\[\begin{array}{l} x = |x|\\ \\ \frac{1}{\left(s + s\right) + e^{\frac{x}{s}} \cdot \left(s + s\right)} \end{array} \]
NOTE: x should be positive before calling this function
(FPCore (x s)
 :precision binary32
 (/ 1.0 (+ (+ s s) (* (exp (/ x s)) (+ s s)))))
x = abs(x);
float code(float x, float s) {
	return 1.0f / ((s + s) + (expf((x / s)) * (s + s)));
}
NOTE: x should be positive before calling this function
real(4) function code(x, s)
    real(4), intent (in) :: x
    real(4), intent (in) :: s
    code = 1.0e0 / ((s + s) + (exp((x / s)) * (s + s)))
end function
x = abs(x)
function code(x, s)
	return Float32(Float32(1.0) / Float32(Float32(s + s) + Float32(exp(Float32(x / s)) * Float32(s + s))))
end
x = abs(x)
function tmp = code(x, s)
	tmp = single(1.0) / ((s + s) + (exp((x / s)) * (s + s)));
end
\begin{array}{l}
x = |x|\\
\\
\frac{1}{\left(s + s\right) + e^{\frac{x}{s}} \cdot \left(s + s\right)}
\end{array}
Derivation
  1. Initial program 99.4%

    \[\frac{e^{\frac{-\left|x\right|}{s}}}{\left(s \cdot \left(1 + e^{\frac{-\left|x\right|}{s}}\right)\right) \cdot \left(1 + e^{\frac{-\left|x\right|}{s}}\right)} \]
  2. Simplified99.5%

    \[\leadsto \color{blue}{\frac{1}{\mathsf{fma}\left(s, e^{\frac{\left|x\right|}{-s}}, s\right) \cdot \left(1 + e^{\frac{\left|x\right|}{s}}\right)}} \]
  3. Taylor expanded in s around inf 94.8%

    \[\leadsto \frac{1}{\mathsf{fma}\left(s, \color{blue}{1}, s\right) \cdot \left(1 + e^{\frac{\left|x\right|}{s}}\right)} \]
  4. Step-by-step derivation
    1. distribute-lft-in94.8%

      \[\leadsto \frac{1}{\color{blue}{\mathsf{fma}\left(s, 1, s\right) \cdot 1 + \mathsf{fma}\left(s, 1, s\right) \cdot e^{\frac{\left|x\right|}{s}}}} \]
    2. *-rgt-identity94.8%

      \[\leadsto \frac{1}{\color{blue}{\mathsf{fma}\left(s, 1, s\right)} + \mathsf{fma}\left(s, 1, s\right) \cdot e^{\frac{\left|x\right|}{s}}} \]
    3. fma-udef94.8%

      \[\leadsto \frac{1}{\color{blue}{\left(s \cdot 1 + s\right)} + \mathsf{fma}\left(s, 1, s\right) \cdot e^{\frac{\left|x\right|}{s}}} \]
    4. *-rgt-identity94.8%

      \[\leadsto \frac{1}{\left(\color{blue}{s} + s\right) + \mathsf{fma}\left(s, 1, s\right) \cdot e^{\frac{\left|x\right|}{s}}} \]
    5. fma-udef94.8%

      \[\leadsto \frac{1}{\left(s + s\right) + \color{blue}{\left(s \cdot 1 + s\right)} \cdot e^{\frac{\left|x\right|}{s}}} \]
    6. *-rgt-identity94.8%

      \[\leadsto \frac{1}{\left(s + s\right) + \left(\color{blue}{s} + s\right) \cdot e^{\frac{\left|x\right|}{s}}} \]
    7. add-sqr-sqrt43.9%

      \[\leadsto \frac{1}{\left(s + s\right) + \left(s + s\right) \cdot e^{\frac{\left|\color{blue}{\sqrt{x} \cdot \sqrt{x}}\right|}{s}}} \]
    8. fabs-sqr43.9%

      \[\leadsto \frac{1}{\left(s + s\right) + \left(s + s\right) \cdot e^{\frac{\color{blue}{\sqrt{x} \cdot \sqrt{x}}}{s}}} \]
    9. add-sqr-sqrt60.5%

      \[\leadsto \frac{1}{\left(s + s\right) + \left(s + s\right) \cdot e^{\frac{\color{blue}{x}}{s}}} \]
  5. Applied egg-rr60.5%

    \[\leadsto \frac{1}{\color{blue}{\left(s + s\right) + \left(s + s\right) \cdot e^{\frac{x}{s}}}} \]
  6. Final simplification60.5%

    \[\leadsto \frac{1}{\left(s + s\right) + e^{\frac{x}{s}} \cdot \left(s + s\right)} \]

Alternative 5: 95.1% accurate, 5.7× speedup?

\[\begin{array}{l} x = |x|\\ \\ \frac{0.5}{s \cdot \left(1 + e^{\frac{x}{s}}\right)} \end{array} \]
NOTE: x should be positive before calling this function
(FPCore (x s) :precision binary32 (/ 0.5 (* s (+ 1.0 (exp (/ x s))))))
x = abs(x);
float code(float x, float s) {
	return 0.5f / (s * (1.0f + expf((x / s))));
}
NOTE: x should be positive before calling this function
real(4) function code(x, s)
    real(4), intent (in) :: x
    real(4), intent (in) :: s
    code = 0.5e0 / (s * (1.0e0 + exp((x / s))))
end function
x = abs(x)
function code(x, s)
	return Float32(Float32(0.5) / Float32(s * Float32(Float32(1.0) + exp(Float32(x / s)))))
end
x = abs(x)
function tmp = code(x, s)
	tmp = single(0.5) / (s * (single(1.0) + exp((x / s))));
end
\begin{array}{l}
x = |x|\\
\\
\frac{0.5}{s \cdot \left(1 + e^{\frac{x}{s}}\right)}
\end{array}
Derivation
  1. Initial program 99.4%

    \[\frac{e^{\frac{-\left|x\right|}{s}}}{\left(s \cdot \left(1 + e^{\frac{-\left|x\right|}{s}}\right)\right) \cdot \left(1 + e^{\frac{-\left|x\right|}{s}}\right)} \]
  2. Simplified99.5%

    \[\leadsto \color{blue}{\frac{1}{\mathsf{fma}\left(s, e^{\frac{\left|x\right|}{-s}}, s\right) \cdot \left(1 + e^{\frac{\left|x\right|}{s}}\right)}} \]
  3. Taylor expanded in s around inf 94.8%

    \[\leadsto \frac{1}{\mathsf{fma}\left(s, \color{blue}{1}, s\right) \cdot \left(1 + e^{\frac{\left|x\right|}{s}}\right)} \]
  4. Step-by-step derivation
    1. add-log-exp78.1%

      \[\leadsto \color{blue}{\log \left(e^{\frac{1}{\mathsf{fma}\left(s, 1, s\right) \cdot \left(1 + e^{\frac{\left|x\right|}{s}}\right)}}\right)} \]
    2. associate-/r*78.1%

      \[\leadsto \log \left(e^{\color{blue}{\frac{\frac{1}{\mathsf{fma}\left(s, 1, s\right)}}{1 + e^{\frac{\left|x\right|}{s}}}}}\right) \]
    3. fma-udef78.1%

      \[\leadsto \log \left(e^{\frac{\frac{1}{\color{blue}{s \cdot 1 + s}}}{1 + e^{\frac{\left|x\right|}{s}}}}\right) \]
    4. *-rgt-identity78.1%

      \[\leadsto \log \left(e^{\frac{\frac{1}{\color{blue}{s} + s}}{1 + e^{\frac{\left|x\right|}{s}}}}\right) \]
    5. add-sqr-sqrt37.7%

      \[\leadsto \log \left(e^{\frac{\frac{1}{s + s}}{1 + e^{\frac{\left|\color{blue}{\sqrt{x} \cdot \sqrt{x}}\right|}{s}}}}\right) \]
    6. fabs-sqr37.7%

      \[\leadsto \log \left(e^{\frac{\frac{1}{s + s}}{1 + e^{\frac{\color{blue}{\sqrt{x} \cdot \sqrt{x}}}{s}}}}\right) \]
    7. add-sqr-sqrt43.6%

      \[\leadsto \log \left(e^{\frac{\frac{1}{s + s}}{1 + e^{\frac{\color{blue}{x}}{s}}}}\right) \]
  5. Applied egg-rr43.6%

    \[\leadsto \color{blue}{\log \left(e^{\frac{\frac{1}{s + s}}{1 + e^{\frac{x}{s}}}}\right)} \]
  6. Taylor expanded in s around 0 60.5%

    \[\leadsto \color{blue}{\frac{0.5}{s \cdot \left(1 + e^{\frac{x}{s}}\right)}} \]
  7. Final simplification60.5%

    \[\leadsto \frac{0.5}{s \cdot \left(1 + e^{\frac{x}{s}}\right)} \]

Alternative 6: 65.1% accurate, 56.4× speedup?

\[\begin{array}{l} x = |x|\\ \\ \frac{1}{s \cdot 4 + \frac{x \cdot x}{s}} \end{array} \]
NOTE: x should be positive before calling this function
(FPCore (x s) :precision binary32 (/ 1.0 (+ (* s 4.0) (/ (* x x) s))))
x = abs(x);
float code(float x, float s) {
	return 1.0f / ((s * 4.0f) + ((x * x) / s));
}
NOTE: x should be positive before calling this function
real(4) function code(x, s)
    real(4), intent (in) :: x
    real(4), intent (in) :: s
    code = 1.0e0 / ((s * 4.0e0) + ((x * x) / s))
end function
x = abs(x)
function code(x, s)
	return Float32(Float32(1.0) / Float32(Float32(s * Float32(4.0)) + Float32(Float32(x * x) / s)))
end
x = abs(x)
function tmp = code(x, s)
	tmp = single(1.0) / ((s * single(4.0)) + ((x * x) / s));
end
\begin{array}{l}
x = |x|\\
\\
\frac{1}{s \cdot 4 + \frac{x \cdot x}{s}}
\end{array}
Derivation
  1. Initial program 99.4%

    \[\frac{e^{\frac{-\left|x\right|}{s}}}{\left(s \cdot \left(1 + e^{\frac{-\left|x\right|}{s}}\right)\right) \cdot \left(1 + e^{\frac{-\left|x\right|}{s}}\right)} \]
  2. Simplified99.5%

    \[\leadsto \color{blue}{\frac{1}{\mathsf{fma}\left(s, e^{\frac{\left|x\right|}{-s}}, s\right) \cdot \left(1 + e^{\frac{\left|x\right|}{s}}\right)}} \]
  3. Taylor expanded in s around -inf 42.6%

    \[\leadsto \frac{1}{\color{blue}{-2 \cdot \left|x\right| + \left(-1 \cdot \frac{-2 \cdot {\left(\left|x\right|\right)}^{2} + {\left(\left|x\right|\right)}^{2}}{s} + \left(2 \cdot \left|x\right| + 4 \cdot s\right)\right)}} \]
  4. Step-by-step derivation
    1. +-commutative42.6%

      \[\leadsto \frac{1}{-2 \cdot \left|x\right| + \color{blue}{\left(\left(2 \cdot \left|x\right| + 4 \cdot s\right) + -1 \cdot \frac{-2 \cdot {\left(\left|x\right|\right)}^{2} + {\left(\left|x\right|\right)}^{2}}{s}\right)}} \]
    2. mul-1-neg42.6%

      \[\leadsto \frac{1}{-2 \cdot \left|x\right| + \left(\left(2 \cdot \left|x\right| + 4 \cdot s\right) + \color{blue}{\left(-\frac{-2 \cdot {\left(\left|x\right|\right)}^{2} + {\left(\left|x\right|\right)}^{2}}{s}\right)}\right)} \]
    3. distribute-lft1-in64.5%

      \[\leadsto \frac{1}{-2 \cdot \left|x\right| + \left(\left(2 \cdot \left|x\right| + 4 \cdot s\right) + \left(-\frac{\color{blue}{\left(-2 + 1\right) \cdot {\left(\left|x\right|\right)}^{2}}}{s}\right)\right)} \]
    4. metadata-eval64.5%

      \[\leadsto \frac{1}{-2 \cdot \left|x\right| + \left(\left(2 \cdot \left|x\right| + 4 \cdot s\right) + \left(-\frac{\color{blue}{-1} \cdot {\left(\left|x\right|\right)}^{2}}{s}\right)\right)} \]
    5. associate-*r/64.5%

      \[\leadsto \frac{1}{-2 \cdot \left|x\right| + \left(\left(2 \cdot \left|x\right| + 4 \cdot s\right) + \left(-\color{blue}{-1 \cdot \frac{{\left(\left|x\right|\right)}^{2}}{s}}\right)\right)} \]
    6. mul-1-neg64.5%

      \[\leadsto \frac{1}{-2 \cdot \left|x\right| + \left(\left(2 \cdot \left|x\right| + 4 \cdot s\right) + \left(-\color{blue}{\left(-\frac{{\left(\left|x\right|\right)}^{2}}{s}\right)}\right)\right)} \]
    7. remove-double-neg64.5%

      \[\leadsto \frac{1}{-2 \cdot \left|x\right| + \left(\left(2 \cdot \left|x\right| + 4 \cdot s\right) + \color{blue}{\frac{{\left(\left|x\right|\right)}^{2}}{s}}\right)} \]
    8. associate-+r+64.5%

      \[\leadsto \frac{1}{\color{blue}{\left(-2 \cdot \left|x\right| + \left(2 \cdot \left|x\right| + 4 \cdot s\right)\right) + \frac{{\left(\left|x\right|\right)}^{2}}{s}}} \]
  5. Simplified64.9%

    \[\leadsto \frac{1}{\color{blue}{\left(0 + s \cdot 4\right) + \frac{x \cdot x}{s}}} \]
  6. Final simplification64.9%

    \[\leadsto \frac{1}{s \cdot 4 + \frac{x \cdot x}{s}} \]

Alternative 7: 30.0% accurate, 68.9× speedup?

\[\begin{array}{l} x = |x|\\ \\ \frac{1}{s \cdot 4 + x \cdot 2} \end{array} \]
NOTE: x should be positive before calling this function
(FPCore (x s) :precision binary32 (/ 1.0 (+ (* s 4.0) (* x 2.0))))
x = abs(x);
float code(float x, float s) {
	return 1.0f / ((s * 4.0f) + (x * 2.0f));
}
NOTE: x should be positive before calling this function
real(4) function code(x, s)
    real(4), intent (in) :: x
    real(4), intent (in) :: s
    code = 1.0e0 / ((s * 4.0e0) + (x * 2.0e0))
end function
x = abs(x)
function code(x, s)
	return Float32(Float32(1.0) / Float32(Float32(s * Float32(4.0)) + Float32(x * Float32(2.0))))
end
x = abs(x)
function tmp = code(x, s)
	tmp = single(1.0) / ((s * single(4.0)) + (x * single(2.0)));
end
\begin{array}{l}
x = |x|\\
\\
\frac{1}{s \cdot 4 + x \cdot 2}
\end{array}
Derivation
  1. Initial program 99.4%

    \[\frac{e^{\frac{-\left|x\right|}{s}}}{\left(s \cdot \left(1 + e^{\frac{-\left|x\right|}{s}}\right)\right) \cdot \left(1 + e^{\frac{-\left|x\right|}{s}}\right)} \]
  2. Simplified99.5%

    \[\leadsto \color{blue}{\frac{1}{\mathsf{fma}\left(s, e^{\frac{\left|x\right|}{-s}}, s\right) \cdot \left(1 + e^{\frac{\left|x\right|}{s}}\right)}} \]
  3. Taylor expanded in s around inf 94.8%

    \[\leadsto \frac{1}{\mathsf{fma}\left(s, \color{blue}{1}, s\right) \cdot \left(1 + e^{\frac{\left|x\right|}{s}}\right)} \]
  4. Step-by-step derivation
    1. distribute-lft-in94.8%

      \[\leadsto \frac{1}{\color{blue}{\mathsf{fma}\left(s, 1, s\right) \cdot 1 + \mathsf{fma}\left(s, 1, s\right) \cdot e^{\frac{\left|x\right|}{s}}}} \]
    2. *-rgt-identity94.8%

      \[\leadsto \frac{1}{\color{blue}{\mathsf{fma}\left(s, 1, s\right)} + \mathsf{fma}\left(s, 1, s\right) \cdot e^{\frac{\left|x\right|}{s}}} \]
    3. fma-udef94.8%

      \[\leadsto \frac{1}{\color{blue}{\left(s \cdot 1 + s\right)} + \mathsf{fma}\left(s, 1, s\right) \cdot e^{\frac{\left|x\right|}{s}}} \]
    4. *-rgt-identity94.8%

      \[\leadsto \frac{1}{\left(\color{blue}{s} + s\right) + \mathsf{fma}\left(s, 1, s\right) \cdot e^{\frac{\left|x\right|}{s}}} \]
    5. fma-udef94.8%

      \[\leadsto \frac{1}{\left(s + s\right) + \color{blue}{\left(s \cdot 1 + s\right)} \cdot e^{\frac{\left|x\right|}{s}}} \]
    6. *-rgt-identity94.8%

      \[\leadsto \frac{1}{\left(s + s\right) + \left(\color{blue}{s} + s\right) \cdot e^{\frac{\left|x\right|}{s}}} \]
    7. add-sqr-sqrt43.9%

      \[\leadsto \frac{1}{\left(s + s\right) + \left(s + s\right) \cdot e^{\frac{\left|\color{blue}{\sqrt{x} \cdot \sqrt{x}}\right|}{s}}} \]
    8. fabs-sqr43.9%

      \[\leadsto \frac{1}{\left(s + s\right) + \left(s + s\right) \cdot e^{\frac{\color{blue}{\sqrt{x} \cdot \sqrt{x}}}{s}}} \]
    9. add-sqr-sqrt60.5%

      \[\leadsto \frac{1}{\left(s + s\right) + \left(s + s\right) \cdot e^{\frac{\color{blue}{x}}{s}}} \]
  5. Applied egg-rr60.5%

    \[\leadsto \frac{1}{\color{blue}{\left(s + s\right) + \left(s + s\right) \cdot e^{\frac{x}{s}}}} \]
  6. Taylor expanded in s around inf 29.3%

    \[\leadsto \frac{1}{\color{blue}{2 \cdot x + 4 \cdot s}} \]
  7. Final simplification29.3%

    \[\leadsto \frac{1}{s \cdot 4 + x \cdot 2} \]

Alternative 8: 50.7% accurate, 68.9× speedup?

\[\begin{array}{l} x = |x|\\ \\ \frac{\frac{0.5}{s}}{\frac{x}{s} + 2} \end{array} \]
NOTE: x should be positive before calling this function
(FPCore (x s) :precision binary32 (/ (/ 0.5 s) (+ (/ x s) 2.0)))
x = abs(x);
float code(float x, float s) {
	return (0.5f / s) / ((x / s) + 2.0f);
}
NOTE: x should be positive before calling this function
real(4) function code(x, s)
    real(4), intent (in) :: x
    real(4), intent (in) :: s
    code = (0.5e0 / s) / ((x / s) + 2.0e0)
end function
x = abs(x)
function code(x, s)
	return Float32(Float32(Float32(0.5) / s) / Float32(Float32(x / s) + Float32(2.0)))
end
x = abs(x)
function tmp = code(x, s)
	tmp = (single(0.5) / s) / ((x / s) + single(2.0));
end
\begin{array}{l}
x = |x|\\
\\
\frac{\frac{0.5}{s}}{\frac{x}{s} + 2}
\end{array}
Derivation
  1. Initial program 99.4%

    \[\frac{e^{\frac{-\left|x\right|}{s}}}{\left(s \cdot \left(1 + e^{\frac{-\left|x\right|}{s}}\right)\right) \cdot \left(1 + e^{\frac{-\left|x\right|}{s}}\right)} \]
  2. Simplified99.5%

    \[\leadsto \color{blue}{\frac{1}{\mathsf{fma}\left(s, e^{\frac{\left|x\right|}{-s}}, s\right) \cdot \left(1 + e^{\frac{\left|x\right|}{s}}\right)}} \]
  3. Taylor expanded in s around inf 94.8%

    \[\leadsto \frac{1}{\mathsf{fma}\left(s, \color{blue}{1}, s\right) \cdot \left(1 + e^{\frac{\left|x\right|}{s}}\right)} \]
  4. Step-by-step derivation
    1. add-log-exp78.1%

      \[\leadsto \color{blue}{\log \left(e^{\frac{1}{\mathsf{fma}\left(s, 1, s\right) \cdot \left(1 + e^{\frac{\left|x\right|}{s}}\right)}}\right)} \]
    2. associate-/r*78.1%

      \[\leadsto \log \left(e^{\color{blue}{\frac{\frac{1}{\mathsf{fma}\left(s, 1, s\right)}}{1 + e^{\frac{\left|x\right|}{s}}}}}\right) \]
    3. fma-udef78.1%

      \[\leadsto \log \left(e^{\frac{\frac{1}{\color{blue}{s \cdot 1 + s}}}{1 + e^{\frac{\left|x\right|}{s}}}}\right) \]
    4. *-rgt-identity78.1%

      \[\leadsto \log \left(e^{\frac{\frac{1}{\color{blue}{s} + s}}{1 + e^{\frac{\left|x\right|}{s}}}}\right) \]
    5. add-sqr-sqrt37.7%

      \[\leadsto \log \left(e^{\frac{\frac{1}{s + s}}{1 + e^{\frac{\left|\color{blue}{\sqrt{x} \cdot \sqrt{x}}\right|}{s}}}}\right) \]
    6. fabs-sqr37.7%

      \[\leadsto \log \left(e^{\frac{\frac{1}{s + s}}{1 + e^{\frac{\color{blue}{\sqrt{x} \cdot \sqrt{x}}}{s}}}}\right) \]
    7. add-sqr-sqrt43.6%

      \[\leadsto \log \left(e^{\frac{\frac{1}{s + s}}{1 + e^{\frac{\color{blue}{x}}{s}}}}\right) \]
  5. Applied egg-rr43.6%

    \[\leadsto \color{blue}{\log \left(e^{\frac{\frac{1}{s + s}}{1 + e^{\frac{x}{s}}}}\right)} \]
  6. Taylor expanded in s around 0 60.5%

    \[\leadsto \color{blue}{\frac{0.5}{s \cdot \left(1 + e^{\frac{x}{s}}\right)}} \]
  7. Step-by-step derivation
    1. associate-/r*60.5%

      \[\leadsto \color{blue}{\frac{\frac{0.5}{s}}{1 + e^{\frac{x}{s}}}} \]
  8. Simplified60.5%

    \[\leadsto \color{blue}{\frac{\frac{0.5}{s}}{1 + e^{\frac{x}{s}}}} \]
  9. Taylor expanded in x around 0 50.5%

    \[\leadsto \frac{\frac{0.5}{s}}{\color{blue}{2 + \frac{x}{s}}} \]
  10. Final simplification50.5%

    \[\leadsto \frac{\frac{0.5}{s}}{\frac{x}{s} + 2} \]

Alternative 9: 85.4% accurate, 121.0× speedup?

\[\begin{array}{l} x = |x|\\ \\ \begin{array}{l} \mathbf{if}\;x \leq 2.0000000390829628 \cdot 10^{-24}:\\ \;\;\;\;\frac{0.25}{s}\\ \mathbf{else}:\\ \;\;\;\;0\\ \end{array} \end{array} \]
NOTE: x should be positive before calling this function
(FPCore (x s)
 :precision binary32
 (if (<= x 2.0000000390829628e-24) (/ 0.25 s) 0.0))
x = abs(x);
float code(float x, float s) {
	float tmp;
	if (x <= 2.0000000390829628e-24f) {
		tmp = 0.25f / s;
	} else {
		tmp = 0.0f;
	}
	return tmp;
}
NOTE: x should be positive before calling this function
real(4) function code(x, s)
    real(4), intent (in) :: x
    real(4), intent (in) :: s
    real(4) :: tmp
    if (x <= 2.0000000390829628e-24) then
        tmp = 0.25e0 / s
    else
        tmp = 0.0e0
    end if
    code = tmp
end function
x = abs(x)
function code(x, s)
	tmp = Float32(0.0)
	if (x <= Float32(2.0000000390829628e-24))
		tmp = Float32(Float32(0.25) / s);
	else
		tmp = Float32(0.0);
	end
	return tmp
end
x = abs(x)
function tmp_2 = code(x, s)
	tmp = single(0.0);
	if (x <= single(2.0000000390829628e-24))
		tmp = single(0.25) / s;
	else
		tmp = single(0.0);
	end
	tmp_2 = tmp;
end
\begin{array}{l}
x = |x|\\
\\
\begin{array}{l}
\mathbf{if}\;x \leq 2.0000000390829628 \cdot 10^{-24}:\\
\;\;\;\;\frac{0.25}{s}\\

\mathbf{else}:\\
\;\;\;\;0\\


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if x < 2.00000004e-24

    1. Initial program 99.1%

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

      \[\leadsto \color{blue}{\frac{1}{\mathsf{fma}\left(s, e^{\frac{\left|x\right|}{-s}}, s\right) \cdot \left(1 + e^{\frac{\left|x\right|}{s}}\right)}} \]
    3. Taylor expanded in s around inf 37.9%

      \[\leadsto \color{blue}{\frac{0.25}{s}} \]

    if 2.00000004e-24 < x

    1. Initial program 99.9%

      \[\frac{e^{\frac{-\left|x\right|}{s}}}{\left(s \cdot \left(1 + e^{\frac{-\left|x\right|}{s}}\right)\right) \cdot \left(1 + e^{\frac{-\left|x\right|}{s}}\right)} \]
    2. Simplified99.9%

      \[\leadsto \color{blue}{\frac{1}{\mathsf{fma}\left(s, e^{\frac{\left|x\right|}{-s}}, s\right) \cdot \left(1 + e^{\frac{\left|x\right|}{s}}\right)}} \]
    3. Taylor expanded in s around inf 96.5%

      \[\leadsto \frac{1}{\mathsf{fma}\left(s, \color{blue}{1}, s\right) \cdot \left(1 + e^{\frac{\left|x\right|}{s}}\right)} \]
    4. Step-by-step derivation
      1. add-log-exp92.3%

        \[\leadsto \color{blue}{\log \left(e^{\frac{1}{\mathsf{fma}\left(s, 1, s\right) \cdot \left(1 + e^{\frac{\left|x\right|}{s}}\right)}}\right)} \]
      2. associate-/r*92.3%

        \[\leadsto \log \left(e^{\color{blue}{\frac{\frac{1}{\mathsf{fma}\left(s, 1, s\right)}}{1 + e^{\frac{\left|x\right|}{s}}}}}\right) \]
      3. fma-udef92.3%

        \[\leadsto \log \left(e^{\frac{\frac{1}{\color{blue}{s \cdot 1 + s}}}{1 + e^{\frac{\left|x\right|}{s}}}}\right) \]
      4. *-rgt-identity92.3%

        \[\leadsto \log \left(e^{\frac{\frac{1}{\color{blue}{s} + s}}{1 + e^{\frac{\left|x\right|}{s}}}}\right) \]
      5. add-sqr-sqrt92.3%

        \[\leadsto \log \left(e^{\frac{\frac{1}{s + s}}{1 + e^{\frac{\left|\color{blue}{\sqrt{x} \cdot \sqrt{x}}\right|}{s}}}}\right) \]
      6. fabs-sqr92.3%

        \[\leadsto \log \left(e^{\frac{\frac{1}{s + s}}{1 + e^{\frac{\color{blue}{\sqrt{x} \cdot \sqrt{x}}}{s}}}}\right) \]
      7. add-sqr-sqrt92.3%

        \[\leadsto \log \left(e^{\frac{\frac{1}{s + s}}{1 + e^{\frac{\color{blue}{x}}{s}}}}\right) \]
    5. Applied egg-rr92.3%

      \[\leadsto \color{blue}{\log \left(e^{\frac{\frac{1}{s + s}}{1 + e^{\frac{x}{s}}}}\right)} \]
    6. Taylor expanded in s around inf 90.7%

      \[\leadsto \log \color{blue}{1} \]
  3. Recombined 2 regimes into one program.
  4. Final simplification58.7%

    \[\leadsto \begin{array}{l} \mathbf{if}\;x \leq 2.0000000390829628 \cdot 10^{-24}:\\ \;\;\;\;\frac{0.25}{s}\\ \mathbf{else}:\\ \;\;\;\;0\\ \end{array} \]

Alternative 10: 27.6% accurate, 206.7× speedup?

\[\begin{array}{l} x = |x|\\ \\ \frac{0.25}{s} \end{array} \]
NOTE: x should be positive before calling this function
(FPCore (x s) :precision binary32 (/ 0.25 s))
x = abs(x);
float code(float x, float s) {
	return 0.25f / s;
}
NOTE: x should be positive before calling this function
real(4) function code(x, s)
    real(4), intent (in) :: x
    real(4), intent (in) :: s
    code = 0.25e0 / s
end function
x = abs(x)
function code(x, s)
	return Float32(Float32(0.25) / s)
end
x = abs(x)
function tmp = code(x, s)
	tmp = single(0.25) / s;
end
\begin{array}{l}
x = |x|\\
\\
\frac{0.25}{s}
\end{array}
Derivation
  1. Initial program 99.4%

    \[\frac{e^{\frac{-\left|x\right|}{s}}}{\left(s \cdot \left(1 + e^{\frac{-\left|x\right|}{s}}\right)\right) \cdot \left(1 + e^{\frac{-\left|x\right|}{s}}\right)} \]
  2. Simplified99.5%

    \[\leadsto \color{blue}{\frac{1}{\mathsf{fma}\left(s, e^{\frac{\left|x\right|}{-s}}, s\right) \cdot \left(1 + e^{\frac{\left|x\right|}{s}}\right)}} \]
  3. Taylor expanded in s around inf 27.3%

    \[\leadsto \color{blue}{\frac{0.25}{s}} \]
  4. Final simplification27.3%

    \[\leadsto \frac{0.25}{s} \]

Reproduce

?
herbie shell --seed 2023308 
(FPCore (x s)
  :name "Logistic distribution"
  :precision binary32
  :pre (and (<= 0.0 s) (<= s 1.0651631))
  (/ (exp (/ (- (fabs x)) s)) (* (* s (+ 1.0 (exp (/ (- (fabs x)) s)))) (+ 1.0 (exp (/ (- (fabs x)) s))))))