Sample trimmed logistic on [-pi, pi]

Percentage Accurate: 98.9% → 98.9%
Time: 4.7s
Alternatives: 10
Speedup: 1.0×

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}

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: 98.9% 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: 98.9% 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}
Derivation
  1. Initial program 98.9%

    \[\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. Add Preprocessing

Alternative 2: 98.7% accurate, 2.2× speedup?

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

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

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

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

    \[\leadsto \left(-s\right) \cdot \log \left(\frac{1}{\color{blue}{u} + \frac{1}{1 + e^{\frac{\pi}{s}}}} - 1\right) \]
  4. Applied rewrites98.7%

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

Alternative 3: 97.9% accurate, 4.4× speedup?

\[\begin{array}{l} \\ \left(-s\right) \cdot \log \left(\frac{1 + -1 \cdot u}{u}\right) \end{array} \]
(FPCore (u s) :precision binary32 (* (- s) (log (/ (+ 1.0 (* -1.0 u)) u))))
float code(float u, float s) {
	return -s * logf(((1.0f + (-1.0f * u)) / u));
}
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(u, s)
use fmin_fmax_functions
    real(4), intent (in) :: u
    real(4), intent (in) :: s
    code = -s * log(((1.0e0 + ((-1.0e0) * u)) / u))
end function
function code(u, s)
	return Float32(Float32(-s) * log(Float32(Float32(Float32(1.0) + Float32(Float32(-1.0) * u)) / u)))
end
function tmp = code(u, s)
	tmp = -s * log(((single(1.0) + (single(-1.0) * u)) / u));
end
\begin{array}{l}

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

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

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

    \[\leadsto \left(-s\right) \cdot \log \color{blue}{\left(\frac{1}{u} - 1\right)} \]
  4. Applied rewrites97.8%

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

    \[\leadsto \left(-s\right) \cdot \log \left(\frac{1 + -1 \cdot u}{\color{blue}{u}}\right) \]
  6. Applied rewrites97.9%

    \[\leadsto \left(-s\right) \cdot \log \left(\frac{1 + -1 \cdot u}{\color{blue}{u}}\right) \]
  7. Add Preprocessing

Alternative 4: 97.8% accurate, 5.2× speedup?

\[\begin{array}{l} \\ \left(-s\right) \cdot \log \left(\frac{1}{u} - 1\right) \end{array} \]
(FPCore (u s) :precision binary32 (* (- s) (log (- (/ 1.0 u) 1.0))))
float code(float u, float s) {
	return -s * logf(((1.0f / u) - 1.0f));
}
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(u, s)
use fmin_fmax_functions
    real(4), intent (in) :: u
    real(4), intent (in) :: s
    code = -s * log(((1.0e0 / u) - 1.0e0))
end function
function code(u, s)
	return Float32(Float32(-s) * log(Float32(Float32(Float32(1.0) / u) - Float32(1.0))))
end
function tmp = code(u, s)
	tmp = -s * log(((single(1.0) / u) - single(1.0)));
end
\begin{array}{l}

\\
\left(-s\right) \cdot \log \left(\frac{1}{u} - 1\right)
\end{array}
Derivation
  1. Initial program 98.9%

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

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

    \[\leadsto \left(-s\right) \cdot \log \color{blue}{\left(\frac{1}{u} - 1\right)} \]
  4. Applied rewrites97.8%

    \[\leadsto \left(-s\right) \cdot \log \color{blue}{\left(\frac{1}{u} - 1\right)} \]
  5. Add Preprocessing

Alternative 5: 75.5% accurate, 6.0× speedup?

\[\begin{array}{l} \\ \left(-s\right) \cdot \log \left(\frac{1}{u}\right) \end{array} \]
(FPCore (u s) :precision binary32 (* (- s) (log (/ 1.0 u))))
float code(float u, float s) {
	return -s * logf((1.0f / u));
}
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(u, s)
use fmin_fmax_functions
    real(4), intent (in) :: u
    real(4), intent (in) :: s
    code = -s * log((1.0e0 / u))
end function
function code(u, s)
	return Float32(Float32(-s) * log(Float32(Float32(1.0) / u)))
end
function tmp = code(u, s)
	tmp = -s * log((single(1.0) / u));
end
\begin{array}{l}

\\
\left(-s\right) \cdot \log \left(\frac{1}{u}\right)
\end{array}
Derivation
  1. Initial program 98.9%

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

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

    \[\leadsto \left(-s\right) \cdot \log \color{blue}{\left(\frac{1}{u} - 1\right)} \]
  4. Applied rewrites97.8%

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

    \[\leadsto \left(-s\right) \cdot \log \left(\frac{1}{\color{blue}{u}}\right) \]
  6. Applied rewrites75.5%

    \[\leadsto \left(-s\right) \cdot \log \left(\frac{1}{\color{blue}{u}}\right) \]
  7. Add Preprocessing

Alternative 6: 75.5% accurate, 8.8× speedup?

\[\begin{array}{l} \\ \log u \cdot s \end{array} \]
(FPCore (u s) :precision binary32 (* (log u) s))
float code(float u, float s) {
	return logf(u) * 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(u, s)
use fmin_fmax_functions
    real(4), intent (in) :: u
    real(4), intent (in) :: s
    code = log(u) * s
end function
function code(u, s)
	return Float32(log(u) * s)
end
function tmp = code(u, s)
	tmp = log(u) * s;
end
\begin{array}{l}

\\
\log u \cdot s
\end{array}
Derivation
  1. Initial program 98.9%

    \[\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. Applied rewrites9.3%

    \[\leadsto \color{blue}{\log \left(\mathsf{fma}\left(u, 1, 1\right)\right) \cdot s} \]
  3. Taylor expanded in u around inf

    \[\leadsto \color{blue}{\left(-1 \cdot \log \left(\frac{1}{u}\right)\right)} \cdot s \]
  4. Applied rewrites75.5%

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

    \[\leadsto \log u \cdot s \]
  6. Applied rewrites75.5%

    \[\leadsto \log u \cdot s \]
  7. Add Preprocessing

Alternative 7: 23.9% accurate, 13.6× speedup?

\[\begin{array}{l} \\ \left(-0.5 - 1\right) \cdot s \end{array} \]
(FPCore (u s) :precision binary32 (* (- -0.5 1.0) s))
float code(float u, float s) {
	return (-0.5f - 1.0f) * 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(u, s)
use fmin_fmax_functions
    real(4), intent (in) :: u
    real(4), intent (in) :: s
    code = ((-0.5e0) - 1.0e0) * s
end function
function code(u, s)
	return Float32(Float32(Float32(-0.5) - Float32(1.0)) * s)
end
function tmp = code(u, s)
	tmp = (single(-0.5) - single(1.0)) * s;
end
\begin{array}{l}

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

    \[\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. Applied rewrites9.3%

    \[\leadsto \color{blue}{\log \left(\mathsf{fma}\left(u, 1, 1\right)\right) \cdot s} \]
  3. Taylor expanded in u around 0

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

    \[\leadsto \color{blue}{u \cdot \left(s + -0.5 \cdot \left(s \cdot u\right)\right)} \]
  5. Applied rewrites9.5%

    \[\leadsto \mathsf{fma}\left(s, -0.5, s\right) \cdot \color{blue}{\mathsf{fma}\left(s, -0.5, s\right)} \]
  6. Applied rewrites23.9%

    \[\leadsto \left(-0.5 - 1\right) \cdot \color{blue}{s} \]
  7. Add Preprocessing

Alternative 8: 12.3% accurate, 22.0× speedup?

\[\begin{array}{l} \\ u \cdot -1 \end{array} \]
(FPCore (u s) :precision binary32 (* u -1.0))
float code(float u, float s) {
	return u * -1.0f;
}
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(u, s)
use fmin_fmax_functions
    real(4), intent (in) :: u
    real(4), intent (in) :: s
    code = u * (-1.0e0)
end function
function code(u, s)
	return Float32(u * Float32(-1.0))
end
function tmp = code(u, s)
	tmp = u * single(-1.0);
end
\begin{array}{l}

\\
u \cdot -1
\end{array}
Derivation
  1. Initial program 98.9%

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

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

    \[\leadsto \color{blue}{1 \cdot -1} \]
  4. Applied rewrites12.3%

    \[\leadsto \color{blue}{u} \cdot -1 \]
  5. Add Preprocessing

Alternative 9: 11.3% accurate, 46.3× 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 98.9%

    \[\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. Taylor expanded in u around 0

    \[\leadsto \color{blue}{-1 \cdot \mathsf{PI}\left(\right)} \]
  3. Applied rewrites11.3%

    \[\leadsto \color{blue}{-1 \cdot \pi} \]
  4. Applied rewrites11.3%

    \[\leadsto \color{blue}{-\pi} \]
  5. Add Preprocessing

Alternative 10: 11.0% accurate, 87.7× speedup?

\[\begin{array}{l} \\ -1 \end{array} \]
(FPCore (u s) :precision binary32 -1.0)
float code(float u, float s) {
	return -1.0f;
}
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(u, s)
use fmin_fmax_functions
    real(4), intent (in) :: u
    real(4), intent (in) :: s
    code = -1.0e0
end function
function code(u, s)
	return Float32(-1.0)
end
function tmp = code(u, s)
	tmp = single(-1.0);
end
\begin{array}{l}

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

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

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

    \[\leadsto \color{blue}{1} \]
  4. Applied rewrites11.0%

    \[\leadsto \color{blue}{-1} \]
  5. Add Preprocessing

Reproduce

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