Logistic function

Percentage Accurate: 99.8% → 99.8%
Time: 7.6s
Alternatives: 8
Speedup: 1.0×

Specification

?
\[0 \leq s \land s \leq 1.0651631\]
\[\begin{array}{l} \\ \frac{1}{1 + e^{\frac{-x}{s}}} \end{array} \]
(FPCore (x s) :precision binary32 (/ 1.0 (+ 1.0 (exp (/ (- x) s)))))
float code(float x, float s) {
	return 1.0f / (1.0f + expf((-x / s)));
}
module fmin_fmax_functions
    implicit none
    private
    public fmax
    public fmin

    interface fmax
        module procedure fmax88
        module procedure fmax44
        module procedure fmax84
        module procedure fmax48
    end interface
    interface fmin
        module procedure fmin88
        module procedure fmin44
        module procedure fmin84
        module procedure fmin48
    end interface
contains
    real(8) function fmax88(x, y) result (res)
        real(8), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(x, max(x, y), y /= y), x /= x)
    end function
    real(4) function fmax44(x, y) result (res)
        real(4), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(y, merge(x, max(x, y), y /= y), x /= x)
    end function
    real(8) function fmax84(x, y) result(res)
        real(8), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(dble(y), merge(x, max(x, dble(y)), y /= y), x /= x)
    end function
    real(8) function fmax48(x, y) result(res)
        real(4), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(dble(x), max(dble(x), y), y /= y), x /= x)
    end function
    real(8) function fmin88(x, y) result (res)
        real(8), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(x, min(x, y), y /= y), x /= x)
    end function
    real(4) function fmin44(x, y) result (res)
        real(4), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(y, merge(x, min(x, y), y /= y), x /= x)
    end function
    real(8) function fmin84(x, y) result(res)
        real(8), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(dble(y), merge(x, min(x, dble(y)), y /= y), x /= x)
    end function
    real(8) function fmin48(x, y) result(res)
        real(4), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(dble(x), min(dble(x), y), y /= y), x /= x)
    end function
end module

real(4) function code(x, s)
use fmin_fmax_functions
    real(4), intent (in) :: x
    real(4), intent (in) :: s
    code = 1.0e0 / (1.0e0 + exp((-x / s)))
end function
function code(x, s)
	return Float32(Float32(1.0) / Float32(Float32(1.0) + exp(Float32(Float32(-x) / s))))
end
function tmp = code(x, s)
	tmp = single(1.0) / (single(1.0) + exp((-x / s)));
end
\begin{array}{l}

\\
\frac{1}{1 + e^{\frac{-x}{s}}}
\end{array}

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.8% accurate, 1.0× speedup?

\[\begin{array}{l} \\ \frac{1}{1 + e^{\frac{-x}{s}}} \end{array} \]
(FPCore (x s) :precision binary32 (/ 1.0 (+ 1.0 (exp (/ (- x) s)))))
float code(float x, float s) {
	return 1.0f / (1.0f + expf((-x / s)));
}
module fmin_fmax_functions
    implicit none
    private
    public fmax
    public fmin

    interface fmax
        module procedure fmax88
        module procedure fmax44
        module procedure fmax84
        module procedure fmax48
    end interface
    interface fmin
        module procedure fmin88
        module procedure fmin44
        module procedure fmin84
        module procedure fmin48
    end interface
contains
    real(8) function fmax88(x, y) result (res)
        real(8), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(x, max(x, y), y /= y), x /= x)
    end function
    real(4) function fmax44(x, y) result (res)
        real(4), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(y, merge(x, max(x, y), y /= y), x /= x)
    end function
    real(8) function fmax84(x, y) result(res)
        real(8), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(dble(y), merge(x, max(x, dble(y)), y /= y), x /= x)
    end function
    real(8) function fmax48(x, y) result(res)
        real(4), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(dble(x), max(dble(x), y), y /= y), x /= x)
    end function
    real(8) function fmin88(x, y) result (res)
        real(8), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(x, min(x, y), y /= y), x /= x)
    end function
    real(4) function fmin44(x, y) result (res)
        real(4), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(y, merge(x, min(x, y), y /= y), x /= x)
    end function
    real(8) function fmin84(x, y) result(res)
        real(8), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(dble(y), merge(x, min(x, dble(y)), y /= y), x /= x)
    end function
    real(8) function fmin48(x, y) result(res)
        real(4), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(dble(x), min(dble(x), y), y /= y), x /= x)
    end function
end module

real(4) function code(x, s)
use fmin_fmax_functions
    real(4), intent (in) :: x
    real(4), intent (in) :: s
    code = 1.0e0 / (1.0e0 + exp((-x / s)))
end function
function code(x, s)
	return Float32(Float32(1.0) / Float32(Float32(1.0) + exp(Float32(Float32(-x) / s))))
end
function tmp = code(x, s)
	tmp = single(1.0) / (single(1.0) + exp((-x / s)));
end
\begin{array}{l}

\\
\frac{1}{1 + e^{\frac{-x}{s}}}
\end{array}

Alternative 1: 99.8% accurate, 1.0× speedup?

\[\begin{array}{l} \\ \frac{1}{1 + e^{\frac{-x}{s}}} \end{array} \]
(FPCore (x s) :precision binary32 (/ 1.0 (+ 1.0 (exp (/ (- x) s)))))
float code(float x, float s) {
	return 1.0f / (1.0f + expf((-x / s)));
}
module fmin_fmax_functions
    implicit none
    private
    public fmax
    public fmin

    interface fmax
        module procedure fmax88
        module procedure fmax44
        module procedure fmax84
        module procedure fmax48
    end interface
    interface fmin
        module procedure fmin88
        module procedure fmin44
        module procedure fmin84
        module procedure fmin48
    end interface
contains
    real(8) function fmax88(x, y) result (res)
        real(8), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(x, max(x, y), y /= y), x /= x)
    end function
    real(4) function fmax44(x, y) result (res)
        real(4), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(y, merge(x, max(x, y), y /= y), x /= x)
    end function
    real(8) function fmax84(x, y) result(res)
        real(8), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(dble(y), merge(x, max(x, dble(y)), y /= y), x /= x)
    end function
    real(8) function fmax48(x, y) result(res)
        real(4), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(dble(x), max(dble(x), y), y /= y), x /= x)
    end function
    real(8) function fmin88(x, y) result (res)
        real(8), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(x, min(x, y), y /= y), x /= x)
    end function
    real(4) function fmin44(x, y) result (res)
        real(4), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(y, merge(x, min(x, y), y /= y), x /= x)
    end function
    real(8) function fmin84(x, y) result(res)
        real(8), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(dble(y), merge(x, min(x, dble(y)), y /= y), x /= x)
    end function
    real(8) function fmin48(x, y) result(res)
        real(4), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(dble(x), min(dble(x), y), y /= y), x /= x)
    end function
end module

real(4) function code(x, s)
use fmin_fmax_functions
    real(4), intent (in) :: x
    real(4), intent (in) :: s
    code = 1.0e0 / (1.0e0 + exp((-x / s)))
end function
function code(x, s)
	return Float32(Float32(1.0) / Float32(Float32(1.0) + exp(Float32(Float32(-x) / s))))
end
function tmp = code(x, s)
	tmp = single(1.0) / (single(1.0) + exp((-x / s)));
end
\begin{array}{l}

\\
\frac{1}{1 + e^{\frac{-x}{s}}}
\end{array}
Derivation
  1. Initial program 99.8%

    \[\frac{1}{1 + e^{\frac{-x}{s}}} \]
  2. Add Preprocessing

Alternative 2: 60.6% accurate, 0.8× speedup?

\[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;\frac{-x}{s} \leq -2:\\ \;\;\;\;\frac{1}{\left(2 \cdot s\right) \cdot \frac{1}{s}}\\ \mathbf{else}:\\ \;\;\;\;\frac{1}{2 - s \cdot \left(\frac{1}{s} \cdot \frac{x}{s}\right)}\\ \end{array} \end{array} \]
(FPCore (x s)
 :precision binary32
 (if (<= (/ (- x) s) -2.0)
   (/ 1.0 (* (* 2.0 s) (/ 1.0 s)))
   (/ 1.0 (- 2.0 (* s (* (/ 1.0 s) (/ x s)))))))
float code(float x, float s) {
	float tmp;
	if ((-x / s) <= -2.0f) {
		tmp = 1.0f / ((2.0f * s) * (1.0f / s));
	} else {
		tmp = 1.0f / (2.0f - (s * ((1.0f / s) * (x / s))));
	}
	return tmp;
}
module fmin_fmax_functions
    implicit none
    private
    public fmax
    public fmin

    interface fmax
        module procedure fmax88
        module procedure fmax44
        module procedure fmax84
        module procedure fmax48
    end interface
    interface fmin
        module procedure fmin88
        module procedure fmin44
        module procedure fmin84
        module procedure fmin48
    end interface
contains
    real(8) function fmax88(x, y) result (res)
        real(8), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(x, max(x, y), y /= y), x /= x)
    end function
    real(4) function fmax44(x, y) result (res)
        real(4), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(y, merge(x, max(x, y), y /= y), x /= x)
    end function
    real(8) function fmax84(x, y) result(res)
        real(8), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(dble(y), merge(x, max(x, dble(y)), y /= y), x /= x)
    end function
    real(8) function fmax48(x, y) result(res)
        real(4), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(dble(x), max(dble(x), y), y /= y), x /= x)
    end function
    real(8) function fmin88(x, y) result (res)
        real(8), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(x, min(x, y), y /= y), x /= x)
    end function
    real(4) function fmin44(x, y) result (res)
        real(4), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(y, merge(x, min(x, y), y /= y), x /= x)
    end function
    real(8) function fmin84(x, y) result(res)
        real(8), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(dble(y), merge(x, min(x, dble(y)), y /= y), x /= x)
    end function
    real(8) function fmin48(x, y) result(res)
        real(4), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(dble(x), min(dble(x), y), y /= y), x /= x)
    end function
end module

real(4) function code(x, s)
use fmin_fmax_functions
    real(4), intent (in) :: x
    real(4), intent (in) :: s
    real(4) :: tmp
    if ((-x / s) <= (-2.0e0)) then
        tmp = 1.0e0 / ((2.0e0 * s) * (1.0e0 / s))
    else
        tmp = 1.0e0 / (2.0e0 - (s * ((1.0e0 / s) * (x / s))))
    end if
    code = tmp
end function
function code(x, s)
	tmp = Float32(0.0)
	if (Float32(Float32(-x) / s) <= Float32(-2.0))
		tmp = Float32(Float32(1.0) / Float32(Float32(Float32(2.0) * s) * Float32(Float32(1.0) / s)));
	else
		tmp = Float32(Float32(1.0) / Float32(Float32(2.0) - Float32(s * Float32(Float32(Float32(1.0) / s) * Float32(x / s)))));
	end
	return tmp
end
function tmp_2 = code(x, s)
	tmp = single(0.0);
	if ((-x / s) <= single(-2.0))
		tmp = single(1.0) / ((single(2.0) * s) * (single(1.0) / s));
	else
		tmp = single(1.0) / (single(2.0) - (s * ((single(1.0) / s) * (x / s))));
	end
	tmp_2 = tmp;
end
\begin{array}{l}

\\
\begin{array}{l}
\mathbf{if}\;\frac{-x}{s} \leq -2:\\
\;\;\;\;\frac{1}{\left(2 \cdot s\right) \cdot \frac{1}{s}}\\

\mathbf{else}:\\
\;\;\;\;\frac{1}{2 - s \cdot \left(\frac{1}{s} \cdot \frac{x}{s}\right)}\\


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if (/.f32 (neg.f32 x) s) < -2

    1. Initial program 99.8%

      \[\frac{1}{1 + e^{\frac{-x}{s}}} \]
    2. Taylor expanded in x around 0

      \[\leadsto \frac{1}{\color{blue}{2 + -1 \cdot \frac{x}{s}}} \]
    3. Applied rewrites40.7%

      \[\leadsto \frac{1}{\color{blue}{2 + -1 \cdot \frac{x}{s}}} \]
    4. Applied rewrites40.9%

      \[\leadsto \frac{1}{\frac{4 \cdot \left(2 - \frac{x}{s}\right)}{\color{blue}{4}}} \]
    5. Applied rewrites40.6%

      \[\leadsto \frac{1}{\left(\left(2 - \frac{x}{s}\right) \cdot s\right) \cdot \color{blue}{\frac{1}{s}}} \]
    6. Taylor expanded in x around 0

      \[\leadsto \frac{1}{\left(2 \cdot s\right) \cdot \frac{\color{blue}{1}}{s}} \]
    7. Applied rewrites34.7%

      \[\leadsto \frac{1}{\left(2 \cdot s\right) \cdot \frac{\color{blue}{1}}{s}} \]

    if -2 < (/.f32 (neg.f32 x) s)

    1. Initial program 99.8%

      \[\frac{1}{1 + e^{\frac{-x}{s}}} \]
    2. Taylor expanded in x around 0

      \[\leadsto \frac{1}{\color{blue}{2 + -1 \cdot \frac{x}{s}}} \]
    3. Applied rewrites40.7%

      \[\leadsto \frac{1}{\color{blue}{2 + -1 \cdot \frac{x}{s}}} \]
    4. Applied rewrites40.7%

      \[\leadsto \frac{1}{2 - \color{blue}{\frac{x}{s}}} \]
    5. Applied rewrites52.1%

      \[\leadsto \frac{1}{2 - s \cdot \color{blue}{\left(\frac{1}{s} \cdot \frac{x}{s}\right)}} \]
  3. Recombined 2 regimes into one program.
  4. Add Preprocessing

Alternative 3: 60.4% accurate, 0.9× speedup?

\[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;\frac{-x}{s} \leq -2:\\ \;\;\;\;\frac{1}{\left(2 \cdot s\right) \cdot \frac{1}{s}}\\ \mathbf{else}:\\ \;\;\;\;\frac{1}{\frac{2 - \frac{x}{s}}{s} \cdot s}\\ \end{array} \end{array} \]
(FPCore (x s)
 :precision binary32
 (if (<= (/ (- x) s) -2.0)
   (/ 1.0 (* (* 2.0 s) (/ 1.0 s)))
   (/ 1.0 (* (/ (- 2.0 (/ x s)) s) s))))
float code(float x, float s) {
	float tmp;
	if ((-x / s) <= -2.0f) {
		tmp = 1.0f / ((2.0f * s) * (1.0f / s));
	} else {
		tmp = 1.0f / (((2.0f - (x / s)) / s) * s);
	}
	return tmp;
}
module fmin_fmax_functions
    implicit none
    private
    public fmax
    public fmin

    interface fmax
        module procedure fmax88
        module procedure fmax44
        module procedure fmax84
        module procedure fmax48
    end interface
    interface fmin
        module procedure fmin88
        module procedure fmin44
        module procedure fmin84
        module procedure fmin48
    end interface
contains
    real(8) function fmax88(x, y) result (res)
        real(8), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(x, max(x, y), y /= y), x /= x)
    end function
    real(4) function fmax44(x, y) result (res)
        real(4), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(y, merge(x, max(x, y), y /= y), x /= x)
    end function
    real(8) function fmax84(x, y) result(res)
        real(8), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(dble(y), merge(x, max(x, dble(y)), y /= y), x /= x)
    end function
    real(8) function fmax48(x, y) result(res)
        real(4), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(dble(x), max(dble(x), y), y /= y), x /= x)
    end function
    real(8) function fmin88(x, y) result (res)
        real(8), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(x, min(x, y), y /= y), x /= x)
    end function
    real(4) function fmin44(x, y) result (res)
        real(4), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(y, merge(x, min(x, y), y /= y), x /= x)
    end function
    real(8) function fmin84(x, y) result(res)
        real(8), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(dble(y), merge(x, min(x, dble(y)), y /= y), x /= x)
    end function
    real(8) function fmin48(x, y) result(res)
        real(4), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(dble(x), min(dble(x), y), y /= y), x /= x)
    end function
end module

real(4) function code(x, s)
use fmin_fmax_functions
    real(4), intent (in) :: x
    real(4), intent (in) :: s
    real(4) :: tmp
    if ((-x / s) <= (-2.0e0)) then
        tmp = 1.0e0 / ((2.0e0 * s) * (1.0e0 / s))
    else
        tmp = 1.0e0 / (((2.0e0 - (x / s)) / s) * s)
    end if
    code = tmp
end function
function code(x, s)
	tmp = Float32(0.0)
	if (Float32(Float32(-x) / s) <= Float32(-2.0))
		tmp = Float32(Float32(1.0) / Float32(Float32(Float32(2.0) * s) * Float32(Float32(1.0) / s)));
	else
		tmp = Float32(Float32(1.0) / Float32(Float32(Float32(Float32(2.0) - Float32(x / s)) / s) * s));
	end
	return tmp
end
function tmp_2 = code(x, s)
	tmp = single(0.0);
	if ((-x / s) <= single(-2.0))
		tmp = single(1.0) / ((single(2.0) * s) * (single(1.0) / s));
	else
		tmp = single(1.0) / (((single(2.0) - (x / s)) / s) * s);
	end
	tmp_2 = tmp;
end
\begin{array}{l}

\\
\begin{array}{l}
\mathbf{if}\;\frac{-x}{s} \leq -2:\\
\;\;\;\;\frac{1}{\left(2 \cdot s\right) \cdot \frac{1}{s}}\\

\mathbf{else}:\\
\;\;\;\;\frac{1}{\frac{2 - \frac{x}{s}}{s} \cdot s}\\


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if (/.f32 (neg.f32 x) s) < -2

    1. Initial program 99.8%

      \[\frac{1}{1 + e^{\frac{-x}{s}}} \]
    2. Taylor expanded in x around 0

      \[\leadsto \frac{1}{\color{blue}{2 + -1 \cdot \frac{x}{s}}} \]
    3. Applied rewrites40.7%

      \[\leadsto \frac{1}{\color{blue}{2 + -1 \cdot \frac{x}{s}}} \]
    4. Applied rewrites40.9%

      \[\leadsto \frac{1}{\frac{4 \cdot \left(2 - \frac{x}{s}\right)}{\color{blue}{4}}} \]
    5. Applied rewrites40.6%

      \[\leadsto \frac{1}{\left(\left(2 - \frac{x}{s}\right) \cdot s\right) \cdot \color{blue}{\frac{1}{s}}} \]
    6. Taylor expanded in x around 0

      \[\leadsto \frac{1}{\left(2 \cdot s\right) \cdot \frac{\color{blue}{1}}{s}} \]
    7. Applied rewrites34.7%

      \[\leadsto \frac{1}{\left(2 \cdot s\right) \cdot \frac{\color{blue}{1}}{s}} \]

    if -2 < (/.f32 (neg.f32 x) s)

    1. Initial program 99.8%

      \[\frac{1}{1 + e^{\frac{-x}{s}}} \]
    2. Taylor expanded in x around 0

      \[\leadsto \frac{1}{\color{blue}{2 + -1 \cdot \frac{x}{s}}} \]
    3. Applied rewrites40.7%

      \[\leadsto \frac{1}{\color{blue}{2 + -1 \cdot \frac{x}{s}}} \]
    4. Applied rewrites40.7%

      \[\leadsto \frac{1}{2 - \color{blue}{\frac{x}{s}}} \]
    5. Applied rewrites52.1%

      \[\leadsto \frac{1}{2 - s \cdot \color{blue}{\left(\frac{1}{s} \cdot \frac{x}{s}\right)}} \]
    6. Applied rewrites52.0%

      \[\leadsto \frac{1}{\frac{2 - \frac{x}{s}}{s} \cdot \color{blue}{s}} \]
  3. Recombined 2 regimes into one program.
  4. Add Preprocessing

Alternative 4: 60.3% accurate, 1.0× speedup?

\[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;-x \leq 4.0000000126843074 \cdot 10^{-28}:\\ \;\;\;\;0.5\\ \mathbf{else}:\\ \;\;\;\;\frac{1}{2 - \frac{x}{s \cdot s} \cdot s}\\ \end{array} \end{array} \]
(FPCore (x s)
 :precision binary32
 (if (<= (- x) 4.0000000126843074e-28)
   0.5
   (/ 1.0 (- 2.0 (* (/ x (* s s)) s)))))
float code(float x, float s) {
	float tmp;
	if (-x <= 4.0000000126843074e-28f) {
		tmp = 0.5f;
	} else {
		tmp = 1.0f / (2.0f - ((x / (s * s)) * s));
	}
	return tmp;
}
module fmin_fmax_functions
    implicit none
    private
    public fmax
    public fmin

    interface fmax
        module procedure fmax88
        module procedure fmax44
        module procedure fmax84
        module procedure fmax48
    end interface
    interface fmin
        module procedure fmin88
        module procedure fmin44
        module procedure fmin84
        module procedure fmin48
    end interface
contains
    real(8) function fmax88(x, y) result (res)
        real(8), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(x, max(x, y), y /= y), x /= x)
    end function
    real(4) function fmax44(x, y) result (res)
        real(4), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(y, merge(x, max(x, y), y /= y), x /= x)
    end function
    real(8) function fmax84(x, y) result(res)
        real(8), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(dble(y), merge(x, max(x, dble(y)), y /= y), x /= x)
    end function
    real(8) function fmax48(x, y) result(res)
        real(4), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(dble(x), max(dble(x), y), y /= y), x /= x)
    end function
    real(8) function fmin88(x, y) result (res)
        real(8), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(x, min(x, y), y /= y), x /= x)
    end function
    real(4) function fmin44(x, y) result (res)
        real(4), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(y, merge(x, min(x, y), y /= y), x /= x)
    end function
    real(8) function fmin84(x, y) result(res)
        real(8), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(dble(y), merge(x, min(x, dble(y)), y /= y), x /= x)
    end function
    real(8) function fmin48(x, y) result(res)
        real(4), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(dble(x), min(dble(x), y), y /= y), x /= x)
    end function
end module

real(4) function code(x, s)
use fmin_fmax_functions
    real(4), intent (in) :: x
    real(4), intent (in) :: s
    real(4) :: tmp
    if (-x <= 4.0000000126843074e-28) then
        tmp = 0.5e0
    else
        tmp = 1.0e0 / (2.0e0 - ((x / (s * s)) * s))
    end if
    code = tmp
end function
function code(x, s)
	tmp = Float32(0.0)
	if (Float32(-x) <= Float32(4.0000000126843074e-28))
		tmp = Float32(0.5);
	else
		tmp = Float32(Float32(1.0) / Float32(Float32(2.0) - Float32(Float32(x / Float32(s * s)) * s)));
	end
	return tmp
end
function tmp_2 = code(x, s)
	tmp = single(0.0);
	if (-x <= single(4.0000000126843074e-28))
		tmp = single(0.5);
	else
		tmp = single(1.0) / (single(2.0) - ((x / (s * s)) * s));
	end
	tmp_2 = tmp;
end
\begin{array}{l}

\\
\begin{array}{l}
\mathbf{if}\;-x \leq 4.0000000126843074 \cdot 10^{-28}:\\
\;\;\;\;0.5\\

\mathbf{else}:\\
\;\;\;\;\frac{1}{2 - \frac{x}{s \cdot s} \cdot s}\\


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if (neg.f32 x) < 4.00000001e-28

    1. Initial program 99.8%

      \[\frac{1}{1 + e^{\frac{-x}{s}}} \]
    2. Taylor expanded in x around 0

      \[\leadsto \color{blue}{\frac{1}{2}} \]
    3. Applied rewrites34.7%

      \[\leadsto \color{blue}{0.5} \]

    if 4.00000001e-28 < (neg.f32 x)

    1. Initial program 99.8%

      \[\frac{1}{1 + e^{\frac{-x}{s}}} \]
    2. Taylor expanded in x around 0

      \[\leadsto \frac{1}{\color{blue}{2 + -1 \cdot \frac{x}{s}}} \]
    3. Applied rewrites40.7%

      \[\leadsto \frac{1}{\color{blue}{2 + -1 \cdot \frac{x}{s}}} \]
    4. Applied rewrites40.7%

      \[\leadsto \frac{1}{2 - \color{blue}{\frac{x}{s}}} \]
    5. Applied rewrites52.1%

      \[\leadsto \frac{1}{2 - s \cdot \color{blue}{\left(\frac{1}{s} \cdot \frac{x}{s}\right)}} \]
    6. Applied rewrites50.7%

      \[\leadsto \frac{1}{2 - \frac{x}{s \cdot s} \cdot \color{blue}{s}} \]
  3. Recombined 2 regimes into one program.
  4. Add Preprocessing

Alternative 5: 49.2% accurate, 0.6× speedup?

\[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;1 + e^{\frac{-x}{s}} \leq 1.5:\\ \;\;\;\;\frac{1}{\left(2 \cdot s\right) \cdot \frac{1}{s}}\\ \mathbf{else}:\\ \;\;\;\;\frac{1}{\frac{s - x}{s} - -1}\\ \end{array} \end{array} \]
(FPCore (x s)
 :precision binary32
 (if (<= (+ 1.0 (exp (/ (- x) s))) 1.5)
   (/ 1.0 (* (* 2.0 s) (/ 1.0 s)))
   (/ 1.0 (- (/ (- s x) s) -1.0))))
float code(float x, float s) {
	float tmp;
	if ((1.0f + expf((-x / s))) <= 1.5f) {
		tmp = 1.0f / ((2.0f * s) * (1.0f / s));
	} else {
		tmp = 1.0f / (((s - x) / s) - -1.0f);
	}
	return tmp;
}
module fmin_fmax_functions
    implicit none
    private
    public fmax
    public fmin

    interface fmax
        module procedure fmax88
        module procedure fmax44
        module procedure fmax84
        module procedure fmax48
    end interface
    interface fmin
        module procedure fmin88
        module procedure fmin44
        module procedure fmin84
        module procedure fmin48
    end interface
contains
    real(8) function fmax88(x, y) result (res)
        real(8), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(x, max(x, y), y /= y), x /= x)
    end function
    real(4) function fmax44(x, y) result (res)
        real(4), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(y, merge(x, max(x, y), y /= y), x /= x)
    end function
    real(8) function fmax84(x, y) result(res)
        real(8), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(dble(y), merge(x, max(x, dble(y)), y /= y), x /= x)
    end function
    real(8) function fmax48(x, y) result(res)
        real(4), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(dble(x), max(dble(x), y), y /= y), x /= x)
    end function
    real(8) function fmin88(x, y) result (res)
        real(8), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(x, min(x, y), y /= y), x /= x)
    end function
    real(4) function fmin44(x, y) result (res)
        real(4), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(y, merge(x, min(x, y), y /= y), x /= x)
    end function
    real(8) function fmin84(x, y) result(res)
        real(8), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(dble(y), merge(x, min(x, dble(y)), y /= y), x /= x)
    end function
    real(8) function fmin48(x, y) result(res)
        real(4), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(dble(x), min(dble(x), y), y /= y), x /= x)
    end function
end module

real(4) function code(x, s)
use fmin_fmax_functions
    real(4), intent (in) :: x
    real(4), intent (in) :: s
    real(4) :: tmp
    if ((1.0e0 + exp((-x / s))) <= 1.5e0) then
        tmp = 1.0e0 / ((2.0e0 * s) * (1.0e0 / s))
    else
        tmp = 1.0e0 / (((s - x) / s) - (-1.0e0))
    end if
    code = tmp
end function
function code(x, s)
	tmp = Float32(0.0)
	if (Float32(Float32(1.0) + exp(Float32(Float32(-x) / s))) <= Float32(1.5))
		tmp = Float32(Float32(1.0) / Float32(Float32(Float32(2.0) * s) * Float32(Float32(1.0) / s)));
	else
		tmp = Float32(Float32(1.0) / Float32(Float32(Float32(s - x) / s) - Float32(-1.0)));
	end
	return tmp
end
function tmp_2 = code(x, s)
	tmp = single(0.0);
	if ((single(1.0) + exp((-x / s))) <= single(1.5))
		tmp = single(1.0) / ((single(2.0) * s) * (single(1.0) / s));
	else
		tmp = single(1.0) / (((s - x) / s) - single(-1.0));
	end
	tmp_2 = tmp;
end
\begin{array}{l}

\\
\begin{array}{l}
\mathbf{if}\;1 + e^{\frac{-x}{s}} \leq 1.5:\\
\;\;\;\;\frac{1}{\left(2 \cdot s\right) \cdot \frac{1}{s}}\\

\mathbf{else}:\\
\;\;\;\;\frac{1}{\frac{s - x}{s} - -1}\\


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if (+.f32 #s(literal 1 binary32) (exp.f32 (/.f32 (neg.f32 x) s))) < 1.5

    1. Initial program 99.8%

      \[\frac{1}{1 + e^{\frac{-x}{s}}} \]
    2. Taylor expanded in x around 0

      \[\leadsto \frac{1}{\color{blue}{2 + -1 \cdot \frac{x}{s}}} \]
    3. Applied rewrites40.7%

      \[\leadsto \frac{1}{\color{blue}{2 + -1 \cdot \frac{x}{s}}} \]
    4. Applied rewrites40.9%

      \[\leadsto \frac{1}{\frac{4 \cdot \left(2 - \frac{x}{s}\right)}{\color{blue}{4}}} \]
    5. Applied rewrites40.6%

      \[\leadsto \frac{1}{\left(\left(2 - \frac{x}{s}\right) \cdot s\right) \cdot \color{blue}{\frac{1}{s}}} \]
    6. Taylor expanded in x around 0

      \[\leadsto \frac{1}{\left(2 \cdot s\right) \cdot \frac{\color{blue}{1}}{s}} \]
    7. Applied rewrites34.7%

      \[\leadsto \frac{1}{\left(2 \cdot s\right) \cdot \frac{\color{blue}{1}}{s}} \]

    if 1.5 < (+.f32 #s(literal 1 binary32) (exp.f32 (/.f32 (neg.f32 x) s)))

    1. Initial program 99.8%

      \[\frac{1}{1 + e^{\frac{-x}{s}}} \]
    2. Taylor expanded in x around 0

      \[\leadsto \frac{1}{\color{blue}{2 + -1 \cdot \frac{x}{s}}} \]
    3. Applied rewrites40.7%

      \[\leadsto \frac{1}{\color{blue}{2 + -1 \cdot \frac{x}{s}}} \]
    4. Applied rewrites40.7%

      \[\leadsto \frac{1}{\frac{s - x}{s} - \color{blue}{-1}} \]
  3. Recombined 2 regimes into one program.
  4. Add Preprocessing

Alternative 6: 49.2% accurate, 0.6× speedup?

\[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;1 + e^{\frac{-x}{s}} \leq 1.5:\\ \;\;\;\;0.5\\ \mathbf{else}:\\ \;\;\;\;\frac{1}{\frac{s - x}{s} - -1}\\ \end{array} \end{array} \]
(FPCore (x s)
 :precision binary32
 (if (<= (+ 1.0 (exp (/ (- x) s))) 1.5) 0.5 (/ 1.0 (- (/ (- s x) s) -1.0))))
float code(float x, float s) {
	float tmp;
	if ((1.0f + expf((-x / s))) <= 1.5f) {
		tmp = 0.5f;
	} else {
		tmp = 1.0f / (((s - x) / s) - -1.0f);
	}
	return tmp;
}
module fmin_fmax_functions
    implicit none
    private
    public fmax
    public fmin

    interface fmax
        module procedure fmax88
        module procedure fmax44
        module procedure fmax84
        module procedure fmax48
    end interface
    interface fmin
        module procedure fmin88
        module procedure fmin44
        module procedure fmin84
        module procedure fmin48
    end interface
contains
    real(8) function fmax88(x, y) result (res)
        real(8), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(x, max(x, y), y /= y), x /= x)
    end function
    real(4) function fmax44(x, y) result (res)
        real(4), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(y, merge(x, max(x, y), y /= y), x /= x)
    end function
    real(8) function fmax84(x, y) result(res)
        real(8), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(dble(y), merge(x, max(x, dble(y)), y /= y), x /= x)
    end function
    real(8) function fmax48(x, y) result(res)
        real(4), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(dble(x), max(dble(x), y), y /= y), x /= x)
    end function
    real(8) function fmin88(x, y) result (res)
        real(8), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(x, min(x, y), y /= y), x /= x)
    end function
    real(4) function fmin44(x, y) result (res)
        real(4), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(y, merge(x, min(x, y), y /= y), x /= x)
    end function
    real(8) function fmin84(x, y) result(res)
        real(8), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(dble(y), merge(x, min(x, dble(y)), y /= y), x /= x)
    end function
    real(8) function fmin48(x, y) result(res)
        real(4), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(dble(x), min(dble(x), y), y /= y), x /= x)
    end function
end module

real(4) function code(x, s)
use fmin_fmax_functions
    real(4), intent (in) :: x
    real(4), intent (in) :: s
    real(4) :: tmp
    if ((1.0e0 + exp((-x / s))) <= 1.5e0) then
        tmp = 0.5e0
    else
        tmp = 1.0e0 / (((s - x) / s) - (-1.0e0))
    end if
    code = tmp
end function
function code(x, s)
	tmp = Float32(0.0)
	if (Float32(Float32(1.0) + exp(Float32(Float32(-x) / s))) <= Float32(1.5))
		tmp = Float32(0.5);
	else
		tmp = Float32(Float32(1.0) / Float32(Float32(Float32(s - x) / s) - Float32(-1.0)));
	end
	return tmp
end
function tmp_2 = code(x, s)
	tmp = single(0.0);
	if ((single(1.0) + exp((-x / s))) <= single(1.5))
		tmp = single(0.5);
	else
		tmp = single(1.0) / (((s - x) / s) - single(-1.0));
	end
	tmp_2 = tmp;
end
\begin{array}{l}

\\
\begin{array}{l}
\mathbf{if}\;1 + e^{\frac{-x}{s}} \leq 1.5:\\
\;\;\;\;0.5\\

\mathbf{else}:\\
\;\;\;\;\frac{1}{\frac{s - x}{s} - -1}\\


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if (+.f32 #s(literal 1 binary32) (exp.f32 (/.f32 (neg.f32 x) s))) < 1.5

    1. Initial program 99.8%

      \[\frac{1}{1 + e^{\frac{-x}{s}}} \]
    2. Taylor expanded in x around 0

      \[\leadsto \color{blue}{\frac{1}{2}} \]
    3. Applied rewrites34.7%

      \[\leadsto \color{blue}{0.5} \]

    if 1.5 < (+.f32 #s(literal 1 binary32) (exp.f32 (/.f32 (neg.f32 x) s)))

    1. Initial program 99.8%

      \[\frac{1}{1 + e^{\frac{-x}{s}}} \]
    2. Taylor expanded in x around 0

      \[\leadsto \frac{1}{\color{blue}{2 + -1 \cdot \frac{x}{s}}} \]
    3. Applied rewrites40.7%

      \[\leadsto \frac{1}{\color{blue}{2 + -1 \cdot \frac{x}{s}}} \]
    4. Applied rewrites40.7%

      \[\leadsto \frac{1}{\frac{s - x}{s} - \color{blue}{-1}} \]
  3. Recombined 2 regimes into one program.
  4. Add Preprocessing

Alternative 7: 49.2% accurate, 0.7× speedup?

\[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;1 + e^{\frac{-x}{s}} \leq 1.5:\\ \;\;\;\;0.5\\ \mathbf{else}:\\ \;\;\;\;\frac{1}{2 - \frac{x}{s}}\\ \end{array} \end{array} \]
(FPCore (x s)
 :precision binary32
 (if (<= (+ 1.0 (exp (/ (- x) s))) 1.5) 0.5 (/ 1.0 (- 2.0 (/ x s)))))
float code(float x, float s) {
	float tmp;
	if ((1.0f + expf((-x / s))) <= 1.5f) {
		tmp = 0.5f;
	} else {
		tmp = 1.0f / (2.0f - (x / s));
	}
	return tmp;
}
module fmin_fmax_functions
    implicit none
    private
    public fmax
    public fmin

    interface fmax
        module procedure fmax88
        module procedure fmax44
        module procedure fmax84
        module procedure fmax48
    end interface
    interface fmin
        module procedure fmin88
        module procedure fmin44
        module procedure fmin84
        module procedure fmin48
    end interface
contains
    real(8) function fmax88(x, y) result (res)
        real(8), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(x, max(x, y), y /= y), x /= x)
    end function
    real(4) function fmax44(x, y) result (res)
        real(4), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(y, merge(x, max(x, y), y /= y), x /= x)
    end function
    real(8) function fmax84(x, y) result(res)
        real(8), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(dble(y), merge(x, max(x, dble(y)), y /= y), x /= x)
    end function
    real(8) function fmax48(x, y) result(res)
        real(4), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(dble(x), max(dble(x), y), y /= y), x /= x)
    end function
    real(8) function fmin88(x, y) result (res)
        real(8), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(x, min(x, y), y /= y), x /= x)
    end function
    real(4) function fmin44(x, y) result (res)
        real(4), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(y, merge(x, min(x, y), y /= y), x /= x)
    end function
    real(8) function fmin84(x, y) result(res)
        real(8), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(dble(y), merge(x, min(x, dble(y)), y /= y), x /= x)
    end function
    real(8) function fmin48(x, y) result(res)
        real(4), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(dble(x), min(dble(x), y), y /= y), x /= x)
    end function
end module

real(4) function code(x, s)
use fmin_fmax_functions
    real(4), intent (in) :: x
    real(4), intent (in) :: s
    real(4) :: tmp
    if ((1.0e0 + exp((-x / s))) <= 1.5e0) then
        tmp = 0.5e0
    else
        tmp = 1.0e0 / (2.0e0 - (x / s))
    end if
    code = tmp
end function
function code(x, s)
	tmp = Float32(0.0)
	if (Float32(Float32(1.0) + exp(Float32(Float32(-x) / s))) <= Float32(1.5))
		tmp = Float32(0.5);
	else
		tmp = Float32(Float32(1.0) / Float32(Float32(2.0) - Float32(x / s)));
	end
	return tmp
end
function tmp_2 = code(x, s)
	tmp = single(0.0);
	if ((single(1.0) + exp((-x / s))) <= single(1.5))
		tmp = single(0.5);
	else
		tmp = single(1.0) / (single(2.0) - (x / s));
	end
	tmp_2 = tmp;
end
\begin{array}{l}

\\
\begin{array}{l}
\mathbf{if}\;1 + e^{\frac{-x}{s}} \leq 1.5:\\
\;\;\;\;0.5\\

\mathbf{else}:\\
\;\;\;\;\frac{1}{2 - \frac{x}{s}}\\


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if (+.f32 #s(literal 1 binary32) (exp.f32 (/.f32 (neg.f32 x) s))) < 1.5

    1. Initial program 99.8%

      \[\frac{1}{1 + e^{\frac{-x}{s}}} \]
    2. Taylor expanded in x around 0

      \[\leadsto \color{blue}{\frac{1}{2}} \]
    3. Applied rewrites34.7%

      \[\leadsto \color{blue}{0.5} \]

    if 1.5 < (+.f32 #s(literal 1 binary32) (exp.f32 (/.f32 (neg.f32 x) s)))

    1. Initial program 99.8%

      \[\frac{1}{1 + e^{\frac{-x}{s}}} \]
    2. Taylor expanded in x around 0

      \[\leadsto \frac{1}{\color{blue}{2 + -1 \cdot \frac{x}{s}}} \]
    3. Applied rewrites40.7%

      \[\leadsto \frac{1}{\color{blue}{2 + -1 \cdot \frac{x}{s}}} \]
    4. Applied rewrites40.7%

      \[\leadsto \frac{1}{2 - \color{blue}{\frac{x}{s}}} \]
  3. Recombined 2 regimes into one program.
  4. Add Preprocessing

Alternative 8: 34.7% accurate, 22.5× speedup?

\[\begin{array}{l} \\ 0.5 \end{array} \]
(FPCore (x s) :precision binary32 0.5)
float code(float x, float s) {
	return 0.5f;
}
module fmin_fmax_functions
    implicit none
    private
    public fmax
    public fmin

    interface fmax
        module procedure fmax88
        module procedure fmax44
        module procedure fmax84
        module procedure fmax48
    end interface
    interface fmin
        module procedure fmin88
        module procedure fmin44
        module procedure fmin84
        module procedure fmin48
    end interface
contains
    real(8) function fmax88(x, y) result (res)
        real(8), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(x, max(x, y), y /= y), x /= x)
    end function
    real(4) function fmax44(x, y) result (res)
        real(4), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(y, merge(x, max(x, y), y /= y), x /= x)
    end function
    real(8) function fmax84(x, y) result(res)
        real(8), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(dble(y), merge(x, max(x, dble(y)), y /= y), x /= x)
    end function
    real(8) function fmax48(x, y) result(res)
        real(4), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(dble(x), max(dble(x), y), y /= y), x /= x)
    end function
    real(8) function fmin88(x, y) result (res)
        real(8), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(x, min(x, y), y /= y), x /= x)
    end function
    real(4) function fmin44(x, y) result (res)
        real(4), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(y, merge(x, min(x, y), y /= y), x /= x)
    end function
    real(8) function fmin84(x, y) result(res)
        real(8), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(dble(y), merge(x, min(x, dble(y)), y /= y), x /= x)
    end function
    real(8) function fmin48(x, y) result(res)
        real(4), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(dble(x), min(dble(x), y), y /= y), x /= x)
    end function
end module

real(4) function code(x, s)
use fmin_fmax_functions
    real(4), intent (in) :: x
    real(4), intent (in) :: s
    code = 0.5e0
end function
function code(x, s)
	return Float32(0.5)
end
function tmp = code(x, s)
	tmp = single(0.5);
end
\begin{array}{l}

\\
0.5
\end{array}
Derivation
  1. Initial program 99.8%

    \[\frac{1}{1 + e^{\frac{-x}{s}}} \]
  2. Taylor expanded in x around 0

    \[\leadsto \color{blue}{\frac{1}{2}} \]
  3. Applied rewrites34.7%

    \[\leadsto \color{blue}{0.5} \]
  4. Add Preprocessing

Reproduce

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