Disney BSSRDF, sample scattering profile, lower

Percentage Accurate: 61.2% → 99.8%
Time: 5.2s
Alternatives: 6
Speedup: N/A×

Specification

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

\\
s \cdot \log \left(\frac{1}{1 - 4 \cdot u}\right)
\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 6 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: 61.2% accurate, 1.0× speedup?

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

\\
s \cdot \log \left(\frac{1}{1 - 4 \cdot u}\right)
\end{array}

Alternative 1: 99.8% accurate, N/A× speedup?

\[\begin{array}{l} \\ \begin{array}{l} t_0 := \log \left(1 - u \cdot 4\right)\\ \mathbf{if}\;u \leq 0.003000000026077032:\\ \;\;\;\;\mathsf{fma}\left(4 \cdot s, u, \left(\left(\mathsf{fma}\left(u \cdot s, 21.333333333333332, \left(\left(u \cdot s\right) \cdot 64\right) \cdot u\right) - -8 \cdot s\right) \cdot u\right) \cdot u\right)\\ \mathbf{else}:\\ \;\;\;\;\left(-1 \cdot s\right) \cdot \frac{\mathsf{log1p}\left(\left(-1 \cdot u\right) \cdot 4\right) \cdot t\_0}{t\_0}\\ \end{array} \end{array} \]
(FPCore (s u)
 :precision binary32
 (let* ((t_0 (log (- 1.0 (* u 4.0)))))
   (if (<= u 0.003000000026077032)
     (fma
      (* 4.0 s)
      u
      (*
       (*
        (- (fma (* u s) 21.333333333333332 (* (* (* u s) 64.0) u)) (* -8.0 s))
        u)
       u))
     (* (* -1.0 s) (/ (* (log1p (* (* -1.0 u) 4.0)) t_0) t_0)))))
float code(float s, float u) {
	float t_0 = logf((1.0f - (u * 4.0f)));
	float tmp;
	if (u <= 0.003000000026077032f) {
		tmp = fmaf((4.0f * s), u, (((fmaf((u * s), 21.333333333333332f, (((u * s) * 64.0f) * u)) - (-8.0f * s)) * u) * u));
	} else {
		tmp = (-1.0f * s) * ((log1pf(((-1.0f * u) * 4.0f)) * t_0) / t_0);
	}
	return tmp;
}
function code(s, u)
	t_0 = log(Float32(Float32(1.0) - Float32(u * Float32(4.0))))
	tmp = Float32(0.0)
	if (u <= Float32(0.003000000026077032))
		tmp = fma(Float32(Float32(4.0) * s), u, Float32(Float32(Float32(fma(Float32(u * s), Float32(21.333333333333332), Float32(Float32(Float32(u * s) * Float32(64.0)) * u)) - Float32(Float32(-8.0) * s)) * u) * u));
	else
		tmp = Float32(Float32(Float32(-1.0) * s) * Float32(Float32(log1p(Float32(Float32(Float32(-1.0) * u) * Float32(4.0))) * t_0) / t_0));
	end
	return tmp
end
\begin{array}{l}

\\
\begin{array}{l}
t_0 := \log \left(1 - u \cdot 4\right)\\
\mathbf{if}\;u \leq 0.003000000026077032:\\
\;\;\;\;\mathsf{fma}\left(4 \cdot s, u, \left(\left(\mathsf{fma}\left(u \cdot s, 21.333333333333332, \left(\left(u \cdot s\right) \cdot 64\right) \cdot u\right) - -8 \cdot s\right) \cdot u\right) \cdot u\right)\\

\mathbf{else}:\\
\;\;\;\;\left(-1 \cdot s\right) \cdot \frac{\mathsf{log1p}\left(\left(-1 \cdot u\right) \cdot 4\right) \cdot t\_0}{t\_0}\\


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if u < 0.00300000003

    1. Initial program 50.0%

      \[s \cdot \log \left(\frac{1}{1 - 4 \cdot u}\right) \]
    2. Add Preprocessing
    3. Taylor expanded in u around 0

      \[\leadsto \color{blue}{u \cdot \left(4 \cdot s + u \cdot \left(8 \cdot s + u \cdot \left(\frac{64}{3} \cdot s + 64 \cdot \left(s \cdot u\right)\right)\right)\right)} \]
    4. Step-by-step derivation
      1. *-commutativeN/A

        \[\leadsto \left(4 \cdot s + u \cdot \left(8 \cdot s + u \cdot \left(\frac{64}{3} \cdot s + 64 \cdot \left(s \cdot u\right)\right)\right)\right) \cdot \color{blue}{u} \]
      2. lower-*.f32N/A

        \[\leadsto \left(4 \cdot s + u \cdot \left(8 \cdot s + u \cdot \left(\frac{64}{3} \cdot s + 64 \cdot \left(s \cdot u\right)\right)\right)\right) \cdot \color{blue}{u} \]
    5. Applied rewrites99.4%

      \[\leadsto \color{blue}{\mathsf{fma}\left(\mathsf{fma}\left(\mathsf{fma}\left(64, u \cdot s, 21.333333333333332 \cdot s\right), u, 8 \cdot s\right), u, 4 \cdot s\right) \cdot u} \]
    6. Applied rewrites100.0%

      \[\leadsto \mathsf{fma}\left(4 \cdot s, \color{blue}{u}, \left(\left(\mathsf{fma}\left(u \cdot s, 21.333333333333332, \left(\left(u \cdot s\right) \cdot 64\right) \cdot u\right) - -8 \cdot s\right) \cdot u\right) \cdot u\right) \]

    if 0.00300000003 < u

    1. Initial program 92.4%

      \[s \cdot \log \left(\frac{1}{1 - 4 \cdot u}\right) \]
    2. Add Preprocessing
    3. Step-by-step derivation
      1. lift-log.f32N/A

        \[\leadsto s \cdot \color{blue}{\log \left(\frac{1}{1 - 4 \cdot u}\right)} \]
      2. lift-/.f32N/A

        \[\leadsto s \cdot \log \color{blue}{\left(\frac{1}{1 - 4 \cdot u}\right)} \]
      3. lift--.f32N/A

        \[\leadsto s \cdot \log \left(\frac{1}{\color{blue}{1 - 4 \cdot u}}\right) \]
      4. lift-*.f32N/A

        \[\leadsto s \cdot \log \left(\frac{1}{1 - \color{blue}{4 \cdot u}}\right) \]
      5. log-divN/A

        \[\leadsto s \cdot \color{blue}{\left(\log 1 - \log \left(1 - 4 \cdot u\right)\right)} \]
      6. metadata-evalN/A

        \[\leadsto s \cdot \left(\color{blue}{0} - \log \left(1 - 4 \cdot u\right)\right) \]
      7. flip--N/A

        \[\leadsto s \cdot \color{blue}{\frac{0 \cdot 0 - \log \left(1 - 4 \cdot u\right) \cdot \log \left(1 - 4 \cdot u\right)}{0 + \log \left(1 - 4 \cdot u\right)}} \]
      8. lower-/.f32N/A

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

      \[\leadsto s \cdot \color{blue}{\frac{0 - \log \left(1 - u \cdot 4\right) \cdot \log \left(1 - u \cdot 4\right)}{0 + \log \left(1 - u \cdot 4\right)}} \]
    5. Step-by-step derivation
      1. lift-log.f32N/A

        \[\leadsto s \cdot \frac{0 - \color{blue}{\log \left(1 - u \cdot 4\right)} \cdot \log \left(1 - u \cdot 4\right)}{0 + \log \left(1 - u \cdot 4\right)} \]
      2. lift--.f32N/A

        \[\leadsto s \cdot \frac{0 - \log \color{blue}{\left(1 - u \cdot 4\right)} \cdot \log \left(1 - u \cdot 4\right)}{0 + \log \left(1 - u \cdot 4\right)} \]
      3. lift-*.f32N/A

        \[\leadsto s \cdot \frac{0 - \log \left(1 - \color{blue}{u \cdot 4}\right) \cdot \log \left(1 - u \cdot 4\right)}{0 + \log \left(1 - u \cdot 4\right)} \]
      4. fp-cancel-sub-sign-invN/A

        \[\leadsto s \cdot \frac{0 - \log \color{blue}{\left(1 + \left(\mathsf{neg}\left(u\right)\right) \cdot 4\right)} \cdot \log \left(1 - u \cdot 4\right)}{0 + \log \left(1 - u \cdot 4\right)} \]
      5. lower-log1p.f32N/A

        \[\leadsto s \cdot \frac{0 - \color{blue}{\mathsf{log1p}\left(\left(\mathsf{neg}\left(u\right)\right) \cdot 4\right)} \cdot \log \left(1 - u \cdot 4\right)}{0 + \log \left(1 - u \cdot 4\right)} \]
      6. lower-*.f32N/A

        \[\leadsto s \cdot \frac{0 - \mathsf{log1p}\left(\color{blue}{\left(\mathsf{neg}\left(u\right)\right) \cdot 4}\right) \cdot \log \left(1 - u \cdot 4\right)}{0 + \log \left(1 - u \cdot 4\right)} \]
      7. lower-neg.f3298.8

        \[\leadsto s \cdot \frac{0 - \mathsf{log1p}\left(\color{blue}{\left(-u\right)} \cdot 4\right) \cdot \log \left(1 - u \cdot 4\right)}{0 + \log \left(1 - u \cdot 4\right)} \]
    6. Applied rewrites98.8%

      \[\leadsto s \cdot \frac{0 - \color{blue}{\mathsf{log1p}\left(\left(-u\right) \cdot 4\right)} \cdot \log \left(1 - u \cdot 4\right)}{0 + \log \left(1 - u \cdot 4\right)} \]
  3. Recombined 2 regimes into one program.
  4. Final simplification99.8%

    \[\leadsto \begin{array}{l} \mathbf{if}\;u \leq 0.003000000026077032:\\ \;\;\;\;\mathsf{fma}\left(4 \cdot s, u, \left(\left(\mathsf{fma}\left(u \cdot s, 21.333333333333332, \left(\left(u \cdot s\right) \cdot 64\right) \cdot u\right) - -8 \cdot s\right) \cdot u\right) \cdot u\right)\\ \mathbf{else}:\\ \;\;\;\;\left(-1 \cdot s\right) \cdot \frac{\mathsf{log1p}\left(\left(-1 \cdot u\right) \cdot 4\right) \cdot \log \left(1 - u \cdot 4\right)}{\log \left(1 - u \cdot 4\right)}\\ \end{array} \]
  5. Add Preprocessing

Alternative 2: 99.3% accurate, N/A× speedup?

\[\begin{array}{l} \\ \begin{array}{l} t_0 := \log \left(1 - u \cdot 4\right)\\ \mathbf{if}\;u \leq 0.00800000037997961:\\ \;\;\;\;\mathsf{fma}\left(4 \cdot s, u, \left(\left(\mathsf{fma}\left(u \cdot s, 21.333333333333332, \left(\left(u \cdot s\right) \cdot 64\right) \cdot u\right) - -8 \cdot s\right) \cdot u\right) \cdot u\right)\\ \mathbf{else}:\\ \;\;\;\;\left(-1 \cdot s\right) \cdot \frac{t\_0 \cdot t\_0}{t\_0}\\ \end{array} \end{array} \]
(FPCore (s u)
 :precision binary32
 (let* ((t_0 (log (- 1.0 (* u 4.0)))))
   (if (<= u 0.00800000037997961)
     (fma
      (* 4.0 s)
      u
      (*
       (*
        (- (fma (* u s) 21.333333333333332 (* (* (* u s) 64.0) u)) (* -8.0 s))
        u)
       u))
     (* (* -1.0 s) (/ (* t_0 t_0) t_0)))))
float code(float s, float u) {
	float t_0 = logf((1.0f - (u * 4.0f)));
	float tmp;
	if (u <= 0.00800000037997961f) {
		tmp = fmaf((4.0f * s), u, (((fmaf((u * s), 21.333333333333332f, (((u * s) * 64.0f) * u)) - (-8.0f * s)) * u) * u));
	} else {
		tmp = (-1.0f * s) * ((t_0 * t_0) / t_0);
	}
	return tmp;
}
function code(s, u)
	t_0 = log(Float32(Float32(1.0) - Float32(u * Float32(4.0))))
	tmp = Float32(0.0)
	if (u <= Float32(0.00800000037997961))
		tmp = fma(Float32(Float32(4.0) * s), u, Float32(Float32(Float32(fma(Float32(u * s), Float32(21.333333333333332), Float32(Float32(Float32(u * s) * Float32(64.0)) * u)) - Float32(Float32(-8.0) * s)) * u) * u));
	else
		tmp = Float32(Float32(Float32(-1.0) * s) * Float32(Float32(t_0 * t_0) / t_0));
	end
	return tmp
end
\begin{array}{l}

\\
\begin{array}{l}
t_0 := \log \left(1 - u \cdot 4\right)\\
\mathbf{if}\;u \leq 0.00800000037997961:\\
\;\;\;\;\mathsf{fma}\left(4 \cdot s, u, \left(\left(\mathsf{fma}\left(u \cdot s, 21.333333333333332, \left(\left(u \cdot s\right) \cdot 64\right) \cdot u\right) - -8 \cdot s\right) \cdot u\right) \cdot u\right)\\

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


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if u < 0.00800000038

    1. Initial program 53.2%

      \[s \cdot \log \left(\frac{1}{1 - 4 \cdot u}\right) \]
    2. Add Preprocessing
    3. Taylor expanded in u around 0

      \[\leadsto \color{blue}{u \cdot \left(4 \cdot s + u \cdot \left(8 \cdot s + u \cdot \left(\frac{64}{3} \cdot s + 64 \cdot \left(s \cdot u\right)\right)\right)\right)} \]
    4. Step-by-step derivation
      1. *-commutativeN/A

        \[\leadsto \left(4 \cdot s + u \cdot \left(8 \cdot s + u \cdot \left(\frac{64}{3} \cdot s + 64 \cdot \left(s \cdot u\right)\right)\right)\right) \cdot \color{blue}{u} \]
      2. lower-*.f32N/A

        \[\leadsto \left(4 \cdot s + u \cdot \left(8 \cdot s + u \cdot \left(\frac{64}{3} \cdot s + 64 \cdot \left(s \cdot u\right)\right)\right)\right) \cdot \color{blue}{u} \]
    5. Applied rewrites99.3%

      \[\leadsto \color{blue}{\mathsf{fma}\left(\mathsf{fma}\left(\mathsf{fma}\left(64, u \cdot s, 21.333333333333332 \cdot s\right), u, 8 \cdot s\right), u, 4 \cdot s\right) \cdot u} \]
    6. Applied rewrites99.8%

      \[\leadsto \mathsf{fma}\left(4 \cdot s, \color{blue}{u}, \left(\left(\mathsf{fma}\left(u \cdot s, 21.333333333333332, \left(\left(u \cdot s\right) \cdot 64\right) \cdot u\right) - -8 \cdot s\right) \cdot u\right) \cdot u\right) \]

    if 0.00800000038 < u

    1. Initial program 95.0%

      \[s \cdot \log \left(\frac{1}{1 - 4 \cdot u}\right) \]
    2. Add Preprocessing
    3. Step-by-step derivation
      1. lift-log.f32N/A

        \[\leadsto s \cdot \color{blue}{\log \left(\frac{1}{1 - 4 \cdot u}\right)} \]
      2. lift-/.f32N/A

        \[\leadsto s \cdot \log \color{blue}{\left(\frac{1}{1 - 4 \cdot u}\right)} \]
      3. lift--.f32N/A

        \[\leadsto s \cdot \log \left(\frac{1}{\color{blue}{1 - 4 \cdot u}}\right) \]
      4. lift-*.f32N/A

        \[\leadsto s \cdot \log \left(\frac{1}{1 - \color{blue}{4 \cdot u}}\right) \]
      5. log-divN/A

        \[\leadsto s \cdot \color{blue}{\left(\log 1 - \log \left(1 - 4 \cdot u\right)\right)} \]
      6. metadata-evalN/A

        \[\leadsto s \cdot \left(\color{blue}{0} - \log \left(1 - 4 \cdot u\right)\right) \]
      7. flip--N/A

        \[\leadsto s \cdot \color{blue}{\frac{0 \cdot 0 - \log \left(1 - 4 \cdot u\right) \cdot \log \left(1 - 4 \cdot u\right)}{0 + \log \left(1 - 4 \cdot u\right)}} \]
      8. lower-/.f32N/A

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

      \[\leadsto s \cdot \color{blue}{\frac{0 - \log \left(1 - u \cdot 4\right) \cdot \log \left(1 - u \cdot 4\right)}{0 + \log \left(1 - u \cdot 4\right)}} \]
  3. Recombined 2 regimes into one program.
  4. Final simplification99.4%

    \[\leadsto \begin{array}{l} \mathbf{if}\;u \leq 0.00800000037997961:\\ \;\;\;\;\mathsf{fma}\left(4 \cdot s, u, \left(\left(\mathsf{fma}\left(u \cdot s, 21.333333333333332, \left(\left(u \cdot s\right) \cdot 64\right) \cdot u\right) - -8 \cdot s\right) \cdot u\right) \cdot u\right)\\ \mathbf{else}:\\ \;\;\;\;\left(-1 \cdot s\right) \cdot \frac{\log \left(1 - u \cdot 4\right) \cdot \log \left(1 - u \cdot 4\right)}{\log \left(1 - u \cdot 4\right)}\\ \end{array} \]
  5. Add Preprocessing

Alternative 3: 99.1% accurate, N/A× speedup?

\[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;u \leq 0.009999999776482582:\\ \;\;\;\;\mathsf{fma}\left(4 \cdot s, u, \left(\left(\mathsf{fma}\left(u \cdot s, 21.333333333333332, \left(\left(u \cdot s\right) \cdot 64\right) \cdot u\right) - -8 \cdot s\right) \cdot u\right) \cdot u\right)\\ \mathbf{else}:\\ \;\;\;\;s \cdot \log \left(\frac{1}{1 - 4 \cdot u}\right)\\ \end{array} \end{array} \]
(FPCore (s u)
 :precision binary32
 (if (<= u 0.009999999776482582)
   (fma
    (* 4.0 s)
    u
    (*
     (*
      (- (fma (* u s) 21.333333333333332 (* (* (* u s) 64.0) u)) (* -8.0 s))
      u)
     u))
   (* s (log (/ 1.0 (- 1.0 (* 4.0 u)))))))
float code(float s, float u) {
	float tmp;
	if (u <= 0.009999999776482582f) {
		tmp = fmaf((4.0f * s), u, (((fmaf((u * s), 21.333333333333332f, (((u * s) * 64.0f) * u)) - (-8.0f * s)) * u) * u));
	} else {
		tmp = s * logf((1.0f / (1.0f - (4.0f * u))));
	}
	return tmp;
}
function code(s, u)
	tmp = Float32(0.0)
	if (u <= Float32(0.009999999776482582))
		tmp = fma(Float32(Float32(4.0) * s), u, Float32(Float32(Float32(fma(Float32(u * s), Float32(21.333333333333332), Float32(Float32(Float32(u * s) * Float32(64.0)) * u)) - Float32(Float32(-8.0) * s)) * u) * u));
	else
		tmp = Float32(s * log(Float32(Float32(1.0) / Float32(Float32(1.0) - Float32(Float32(4.0) * u)))));
	end
	return tmp
end
\begin{array}{l}

\\
\begin{array}{l}
\mathbf{if}\;u \leq 0.009999999776482582:\\
\;\;\;\;\mathsf{fma}\left(4 \cdot s, u, \left(\left(\mathsf{fma}\left(u \cdot s, 21.333333333333332, \left(\left(u \cdot s\right) \cdot 64\right) \cdot u\right) - -8 \cdot s\right) \cdot u\right) \cdot u\right)\\

\mathbf{else}:\\
\;\;\;\;s \cdot \log \left(\frac{1}{1 - 4 \cdot u}\right)\\


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if u < 0.00999999978

    1. Initial program 53.4%

      \[s \cdot \log \left(\frac{1}{1 - 4 \cdot u}\right) \]
    2. Add Preprocessing
    3. Taylor expanded in u around 0

      \[\leadsto \color{blue}{u \cdot \left(4 \cdot s + u \cdot \left(8 \cdot s + u \cdot \left(\frac{64}{3} \cdot s + 64 \cdot \left(s \cdot u\right)\right)\right)\right)} \]
    4. Step-by-step derivation
      1. *-commutativeN/A

        \[\leadsto \left(4 \cdot s + u \cdot \left(8 \cdot s + u \cdot \left(\frac{64}{3} \cdot s + 64 \cdot \left(s \cdot u\right)\right)\right)\right) \cdot \color{blue}{u} \]
      2. lower-*.f32N/A

        \[\leadsto \left(4 \cdot s + u \cdot \left(8 \cdot s + u \cdot \left(\frac{64}{3} \cdot s + 64 \cdot \left(s \cdot u\right)\right)\right)\right) \cdot \color{blue}{u} \]
    5. Applied rewrites99.2%

      \[\leadsto \color{blue}{\mathsf{fma}\left(\mathsf{fma}\left(\mathsf{fma}\left(64, u \cdot s, 21.333333333333332 \cdot s\right), u, 8 \cdot s\right), u, 4 \cdot s\right) \cdot u} \]
    6. Applied rewrites99.7%

      \[\leadsto \mathsf{fma}\left(4 \cdot s, \color{blue}{u}, \left(\left(\mathsf{fma}\left(u \cdot s, 21.333333333333332, \left(\left(u \cdot s\right) \cdot 64\right) \cdot u\right) - -8 \cdot s\right) \cdot u\right) \cdot u\right) \]

    if 0.00999999978 < u

    1. Initial program 95.2%

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

Alternative 4: 93.7% accurate, N/A× speedup?

\[\begin{array}{l} \\ \mathsf{fma}\left(4 \cdot s, u, \left(\left(\mathsf{fma}\left(u \cdot s, 21.333333333333332, \left(\left(u \cdot s\right) \cdot 64\right) \cdot u\right) - -8 \cdot s\right) \cdot u\right) \cdot u\right) \end{array} \]
(FPCore (s u)
 :precision binary32
 (fma
  (* 4.0 s)
  u
  (*
   (* (- (fma (* u s) 21.333333333333332 (* (* (* u s) 64.0) u)) (* -8.0 s)) u)
   u)))
float code(float s, float u) {
	return fmaf((4.0f * s), u, (((fmaf((u * s), 21.333333333333332f, (((u * s) * 64.0f) * u)) - (-8.0f * s)) * u) * u));
}
function code(s, u)
	return fma(Float32(Float32(4.0) * s), u, Float32(Float32(Float32(fma(Float32(u * s), Float32(21.333333333333332), Float32(Float32(Float32(u * s) * Float32(64.0)) * u)) - Float32(Float32(-8.0) * s)) * u) * u))
end
\begin{array}{l}

\\
\mathsf{fma}\left(4 \cdot s, u, \left(\left(\mathsf{fma}\left(u \cdot s, 21.333333333333332, \left(\left(u \cdot s\right) \cdot 64\right) \cdot u\right) - -8 \cdot s\right) \cdot u\right) \cdot u\right)
\end{array}
Derivation
  1. Initial program 58.4%

    \[s \cdot \log \left(\frac{1}{1 - 4 \cdot u}\right) \]
  2. Add Preprocessing
  3. Taylor expanded in u around 0

    \[\leadsto \color{blue}{u \cdot \left(4 \cdot s + u \cdot \left(8 \cdot s + u \cdot \left(\frac{64}{3} \cdot s + 64 \cdot \left(s \cdot u\right)\right)\right)\right)} \]
  4. Step-by-step derivation
    1. *-commutativeN/A

      \[\leadsto \left(4 \cdot s + u \cdot \left(8 \cdot s + u \cdot \left(\frac{64}{3} \cdot s + 64 \cdot \left(s \cdot u\right)\right)\right)\right) \cdot \color{blue}{u} \]
    2. lower-*.f32N/A

      \[\leadsto \left(4 \cdot s + u \cdot \left(8 \cdot s + u \cdot \left(\frac{64}{3} \cdot s + 64 \cdot \left(s \cdot u\right)\right)\right)\right) \cdot \color{blue}{u} \]
  5. Applied rewrites95.0%

    \[\leadsto \color{blue}{\mathsf{fma}\left(\mathsf{fma}\left(\mathsf{fma}\left(64, u \cdot s, 21.333333333333332 \cdot s\right), u, 8 \cdot s\right), u, 4 \cdot s\right) \cdot u} \]
  6. Applied rewrites95.5%

    \[\leadsto \mathsf{fma}\left(4 \cdot s, \color{blue}{u}, \left(\left(\mathsf{fma}\left(u \cdot s, 21.333333333333332, \left(\left(u \cdot s\right) \cdot 64\right) \cdot u\right) - -8 \cdot s\right) \cdot u\right) \cdot u\right) \]
  7. Add Preprocessing

Alternative 5: 93.3% accurate, N/A× speedup?

\[\begin{array}{l} \\ \mathsf{fma}\left(\mathsf{fma}\left(\mathsf{fma}\left(64, u \cdot s, 21.333333333333332 \cdot s\right), u, 8 \cdot s\right), u, 4 \cdot s\right) \cdot u \end{array} \]
(FPCore (s u)
 :precision binary32
 (*
  (fma
   (fma (fma 64.0 (* u s) (* 21.333333333333332 s)) u (* 8.0 s))
   u
   (* 4.0 s))
  u))
float code(float s, float u) {
	return fmaf(fmaf(fmaf(64.0f, (u * s), (21.333333333333332f * s)), u, (8.0f * s)), u, (4.0f * s)) * u;
}
function code(s, u)
	return Float32(fma(fma(fma(Float32(64.0), Float32(u * s), Float32(Float32(21.333333333333332) * s)), u, Float32(Float32(8.0) * s)), u, Float32(Float32(4.0) * s)) * u)
end
\begin{array}{l}

\\
\mathsf{fma}\left(\mathsf{fma}\left(\mathsf{fma}\left(64, u \cdot s, 21.333333333333332 \cdot s\right), u, 8 \cdot s\right), u, 4 \cdot s\right) \cdot u
\end{array}
Derivation
  1. Initial program 58.4%

    \[s \cdot \log \left(\frac{1}{1 - 4 \cdot u}\right) \]
  2. Add Preprocessing
  3. Taylor expanded in u around 0

    \[\leadsto \color{blue}{u \cdot \left(4 \cdot s + u \cdot \left(8 \cdot s + u \cdot \left(\frac{64}{3} \cdot s + 64 \cdot \left(s \cdot u\right)\right)\right)\right)} \]
  4. Step-by-step derivation
    1. *-commutativeN/A

      \[\leadsto \left(4 \cdot s + u \cdot \left(8 \cdot s + u \cdot \left(\frac{64}{3} \cdot s + 64 \cdot \left(s \cdot u\right)\right)\right)\right) \cdot \color{blue}{u} \]
    2. lower-*.f32N/A

      \[\leadsto \left(4 \cdot s + u \cdot \left(8 \cdot s + u \cdot \left(\frac{64}{3} \cdot s + 64 \cdot \left(s \cdot u\right)\right)\right)\right) \cdot \color{blue}{u} \]
  5. Applied rewrites95.0%

    \[\leadsto \color{blue}{\mathsf{fma}\left(\mathsf{fma}\left(\mathsf{fma}\left(64, u \cdot s, 21.333333333333332 \cdot s\right), u, 8 \cdot s\right), u, 4 \cdot s\right) \cdot u} \]
  6. Add Preprocessing

Alternative 6: 93.1% accurate, N/A× speedup?

\[\begin{array}{l} \\ \begin{array}{l} t_0 := 8 + u \cdot \left(21.333333333333332 - -64 \cdot u\right)\\ \frac{s \cdot \left(64 + {u}^{3} \cdot {t\_0}^{3}\right)}{\left(16 + \left(u \cdot u\right) \cdot {t\_0}^{2}\right) - 4 \cdot \left(u \cdot t\_0\right)} \cdot u \end{array} \end{array} \]
(FPCore (s u)
 :precision binary32
 (let* ((t_0 (+ 8.0 (* u (- 21.333333333333332 (* -64.0 u))))))
   (*
    (/
     (* s (+ 64.0 (* (pow u 3.0) (pow t_0 3.0))))
     (- (+ 16.0 (* (* u u) (pow t_0 2.0))) (* 4.0 (* u t_0))))
    u)))
float code(float s, float u) {
	float t_0 = 8.0f + (u * (21.333333333333332f - (-64.0f * u)));
	return ((s * (64.0f + (powf(u, 3.0f) * powf(t_0, 3.0f)))) / ((16.0f + ((u * u) * powf(t_0, 2.0f))) - (4.0f * (u * t_0)))) * 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(s, u)
use fmin_fmax_functions
    real(4), intent (in) :: s
    real(4), intent (in) :: u
    real(4) :: t_0
    t_0 = 8.0e0 + (u * (21.333333333333332e0 - ((-64.0e0) * u)))
    code = ((s * (64.0e0 + ((u ** 3.0e0) * (t_0 ** 3.0e0)))) / ((16.0e0 + ((u * u) * (t_0 ** 2.0e0))) - (4.0e0 * (u * t_0)))) * u
end function
function code(s, u)
	t_0 = Float32(Float32(8.0) + Float32(u * Float32(Float32(21.333333333333332) - Float32(Float32(-64.0) * u))))
	return Float32(Float32(Float32(s * Float32(Float32(64.0) + Float32((u ^ Float32(3.0)) * (t_0 ^ Float32(3.0))))) / Float32(Float32(Float32(16.0) + Float32(Float32(u * u) * (t_0 ^ Float32(2.0)))) - Float32(Float32(4.0) * Float32(u * t_0)))) * u)
end
function tmp = code(s, u)
	t_0 = single(8.0) + (u * (single(21.333333333333332) - (single(-64.0) * u)));
	tmp = ((s * (single(64.0) + ((u ^ single(3.0)) * (t_0 ^ single(3.0))))) / ((single(16.0) + ((u * u) * (t_0 ^ single(2.0)))) - (single(4.0) * (u * t_0)))) * u;
end
\begin{array}{l}

\\
\begin{array}{l}
t_0 := 8 + u \cdot \left(21.333333333333332 - -64 \cdot u\right)\\
\frac{s \cdot \left(64 + {u}^{3} \cdot {t\_0}^{3}\right)}{\left(16 + \left(u \cdot u\right) \cdot {t\_0}^{2}\right) - 4 \cdot \left(u \cdot t\_0\right)} \cdot u
\end{array}
\end{array}
Derivation
  1. Initial program 58.4%

    \[s \cdot \log \left(\frac{1}{1 - 4 \cdot u}\right) \]
  2. Add Preprocessing
  3. Taylor expanded in u around 0

    \[\leadsto \color{blue}{u \cdot \left(4 \cdot s + u \cdot \left(8 \cdot s + u \cdot \left(\frac{64}{3} \cdot s + 64 \cdot \left(s \cdot u\right)\right)\right)\right)} \]
  4. Step-by-step derivation
    1. *-commutativeN/A

      \[\leadsto \left(4 \cdot s + u \cdot \left(8 \cdot s + u \cdot \left(\frac{64}{3} \cdot s + 64 \cdot \left(s \cdot u\right)\right)\right)\right) \cdot \color{blue}{u} \]
    2. lower-*.f32N/A

      \[\leadsto \left(4 \cdot s + u \cdot \left(8 \cdot s + u \cdot \left(\frac{64}{3} \cdot s + 64 \cdot \left(s \cdot u\right)\right)\right)\right) \cdot \color{blue}{u} \]
  5. Applied rewrites95.0%

    \[\leadsto \color{blue}{\mathsf{fma}\left(\mathsf{fma}\left(\mathsf{fma}\left(64, u \cdot s, 21.333333333333332 \cdot s\right), u, 8 \cdot s\right), u, 4 \cdot s\right) \cdot u} \]
  6. Taylor expanded in s around 0

    \[\leadsto \left(s \cdot \left(4 + u \cdot \left(8 + u \cdot \left(\frac{64}{3} + 64 \cdot u\right)\right)\right)\right) \cdot u \]
  7. Step-by-step derivation
    1. *-commutativeN/A

      \[\leadsto \left(\left(4 + u \cdot \left(8 + u \cdot \left(\frac{64}{3} + 64 \cdot u\right)\right)\right) \cdot s\right) \cdot u \]
    2. *-commutativeN/A

      \[\leadsto \left(\left(4 + \left(8 + u \cdot \left(\frac{64}{3} + 64 \cdot u\right)\right) \cdot u\right) \cdot s\right) \cdot u \]
    3. +-commutativeN/A

      \[\leadsto \left(\left(4 + \left(8 + u \cdot \left(64 \cdot u + \frac{64}{3}\right)\right) \cdot u\right) \cdot s\right) \cdot u \]
    4. *-commutativeN/A

      \[\leadsto \left(\left(4 + \left(8 + \left(64 \cdot u + \frac{64}{3}\right) \cdot u\right) \cdot u\right) \cdot s\right) \cdot u \]
    5. +-commutativeN/A

      \[\leadsto \left(\left(4 + \left(\left(64 \cdot u + \frac{64}{3}\right) \cdot u + 8\right) \cdot u\right) \cdot s\right) \cdot u \]
    6. +-commutativeN/A

      \[\leadsto \left(\left(\left(\left(64 \cdot u + \frac{64}{3}\right) \cdot u + 8\right) \cdot u + 4\right) \cdot s\right) \cdot u \]
    7. lower-*.f32N/A

      \[\leadsto \left(\left(\left(\left(64 \cdot u + \frac{64}{3}\right) \cdot u + 8\right) \cdot u + 4\right) \cdot s\right) \cdot u \]
  8. Applied rewrites94.8%

    \[\leadsto \left(\mathsf{fma}\left(\mathsf{fma}\left(21.333333333333332 - -64 \cdot u, u, 8\right), u, 4\right) \cdot s\right) \cdot u \]
  9. Applied rewrites94.6%

    \[\leadsto \left(\frac{\mathsf{fma}\left({\left(\mathsf{fma}\left(21.333333333333332 - -64 \cdot u, u, 8\right)\right)}^{3}, \left(u \cdot u\right) \cdot u, 64\right)}{\mathsf{fma}\left(\mathsf{fma}\left(21.333333333333332 - -64 \cdot u, u, 8\right) \cdot u, \mathsf{fma}\left(21.333333333333332 - -64 \cdot u, u, 8\right) \cdot u, 16 - \left(\mathsf{fma}\left(21.333333333333332 - -64 \cdot u, u, 8\right) \cdot u\right) \cdot 4\right)} \cdot s\right) \cdot u \]
  10. Taylor expanded in s around 0

    \[\leadsto \frac{s \cdot \left(64 + {u}^{3} \cdot {\left(8 + u \cdot \left(\frac{64}{3} - -64 \cdot u\right)\right)}^{3}\right)}{\left(16 + {u}^{2} \cdot {\left(8 + u \cdot \left(\frac{64}{3} - -64 \cdot u\right)\right)}^{2}\right) - 4 \cdot \left(u \cdot \left(8 + u \cdot \left(\frac{64}{3} - -64 \cdot u\right)\right)\right)} \cdot u \]
  11. Step-by-step derivation
    1. lower-/.f32N/A

      \[\leadsto \frac{s \cdot \left(64 + {u}^{3} \cdot {\left(8 + u \cdot \left(\frac{64}{3} - -64 \cdot u\right)\right)}^{3}\right)}{\left(16 + {u}^{2} \cdot {\left(8 + u \cdot \left(\frac{64}{3} - -64 \cdot u\right)\right)}^{2}\right) - 4 \cdot \left(u \cdot \left(8 + u \cdot \left(\frac{64}{3} - -64 \cdot u\right)\right)\right)} \cdot u \]
  12. Applied rewrites94.8%

    \[\leadsto \frac{s \cdot \left(64 + {u}^{3} \cdot {\left(8 + u \cdot \left(21.333333333333332 - -64 \cdot u\right)\right)}^{3}\right)}{\left(16 + \left(u \cdot u\right) \cdot {\left(8 + u \cdot \left(21.333333333333332 - -64 \cdot u\right)\right)}^{2}\right) - 4 \cdot \left(u \cdot \left(8 + u \cdot \left(21.333333333333332 - -64 \cdot u\right)\right)\right)} \cdot u \]
  13. Add Preprocessing

Reproduce

?
herbie shell --seed 2025065 
(FPCore (s u)
  :name "Disney BSSRDF, sample scattering profile, lower"
  :precision binary32
  :pre (and (and (<= 0.0 s) (<= s 256.0)) (and (<= 2.328306437e-10 u) (<= u 0.25)))
  (* s (log (/ 1.0 (- 1.0 (* 4.0 u))))))