Disney BSSRDF, sample scattering profile, lower

Percentage Accurate: 61.6% → 96.5%
Time: 3.9s
Alternatives: 4
Speedup: 11.4×

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))));
}
real(4) function code(s, u)
    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 4 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.6% 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))));
}
real(4) function code(s, u)
    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: 96.5% accurate, 0.9× speedup?

\[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;u \cdot 4 \leq 0.004000000189989805:\\ \;\;\;\;\left(\left(8 \cdot u\right) \cdot u + u \cdot 4\right) \cdot s\\ \mathbf{else}:\\ \;\;\;\;\log \left(\frac{1}{1 - u \cdot 4}\right) \cdot s\\ \end{array} \end{array} \]
(FPCore (s u)
 :precision binary32
 (if (<= (* u 4.0) 0.004000000189989805)
   (* (+ (* (* 8.0 u) u) (* u 4.0)) s)
   (* (log (/ 1.0 (- 1.0 (* u 4.0)))) s)))
float code(float s, float u) {
	float tmp;
	if ((u * 4.0f) <= 0.004000000189989805f) {
		tmp = (((8.0f * u) * u) + (u * 4.0f)) * s;
	} else {
		tmp = logf((1.0f / (1.0f - (u * 4.0f)))) * s;
	}
	return tmp;
}
real(4) function code(s, u)
    real(4), intent (in) :: s
    real(4), intent (in) :: u
    real(4) :: tmp
    if ((u * 4.0e0) <= 0.004000000189989805e0) then
        tmp = (((8.0e0 * u) * u) + (u * 4.0e0)) * s
    else
        tmp = log((1.0e0 / (1.0e0 - (u * 4.0e0)))) * s
    end if
    code = tmp
end function
function code(s, u)
	tmp = Float32(0.0)
	if (Float32(u * Float32(4.0)) <= Float32(0.004000000189989805))
		tmp = Float32(Float32(Float32(Float32(Float32(8.0) * u) * u) + Float32(u * Float32(4.0))) * s);
	else
		tmp = Float32(log(Float32(Float32(1.0) / Float32(Float32(1.0) - Float32(u * Float32(4.0))))) * s);
	end
	return tmp
end
function tmp_2 = code(s, u)
	tmp = single(0.0);
	if ((u * single(4.0)) <= single(0.004000000189989805))
		tmp = (((single(8.0) * u) * u) + (u * single(4.0))) * s;
	else
		tmp = log((single(1.0) / (single(1.0) - (u * single(4.0))))) * s;
	end
	tmp_2 = tmp;
end
\begin{array}{l}

\\
\begin{array}{l}
\mathbf{if}\;u \cdot 4 \leq 0.004000000189989805:\\
\;\;\;\;\left(\left(8 \cdot u\right) \cdot u + u \cdot 4\right) \cdot s\\

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


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if (*.f32 #s(literal 4 binary32) u) < 0.00400000019

    1. Initial program 49.8%

      \[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. log-divN/A

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

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

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

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

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

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

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

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

      \[\leadsto s \cdot \frac{1}{\color{blue}{\frac{\frac{1}{4}}{u}}} \]
    6. Step-by-step derivation
      1. lower-/.f3285.6

        \[\leadsto s \cdot \frac{1}{\color{blue}{\frac{0.25}{u}}} \]
    7. Applied rewrites85.6%

      \[\leadsto s \cdot \frac{1}{\color{blue}{\frac{0.25}{u}}} \]
    8. Taylor expanded in u around 0

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

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

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

        \[\leadsto s \cdot \left(\color{blue}{\left(8 \cdot u + 4\right)} \cdot u\right) \]
      4. lower-fma.f3285.6

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

      \[\leadsto s \cdot \color{blue}{\left(\mathsf{fma}\left(8, u, 4\right) \cdot u\right)} \]
    11. Step-by-step derivation
      1. Applied rewrites97.8%

        \[\leadsto s \cdot \left(\left(8 \cdot u\right) \cdot u + \color{blue}{4 \cdot u}\right) \]

      if 0.00400000019 < (*.f32 #s(literal 4 binary32) u)

      1. Initial program 91.6%

        \[s \cdot \log \left(\frac{1}{1 - 4 \cdot u}\right) \]
      2. Add Preprocessing
    12. Recombined 2 regimes into one program.
    13. Final simplification96.0%

      \[\leadsto \begin{array}{l} \mathbf{if}\;u \cdot 4 \leq 0.004000000189989805:\\ \;\;\;\;\left(\left(8 \cdot u\right) \cdot u + u \cdot 4\right) \cdot s\\ \mathbf{else}:\\ \;\;\;\;\log \left(\frac{1}{1 - u \cdot 4}\right) \cdot s\\ \end{array} \]
    14. Add Preprocessing

    Alternative 2: 86.8% accurate, 5.2× speedup?

    \[\begin{array}{l} \\ \left(\left(8 \cdot u\right) \cdot u + u \cdot 4\right) \cdot s \end{array} \]
    (FPCore (s u) :precision binary32 (* (+ (* (* 8.0 u) u) (* u 4.0)) s))
    float code(float s, float u) {
    	return (((8.0f * u) * u) + (u * 4.0f)) * s;
    }
    
    real(4) function code(s, u)
        real(4), intent (in) :: s
        real(4), intent (in) :: u
        code = (((8.0e0 * u) * u) + (u * 4.0e0)) * s
    end function
    
    function code(s, u)
    	return Float32(Float32(Float32(Float32(Float32(8.0) * u) * u) + Float32(u * Float32(4.0))) * s)
    end
    
    function tmp = code(s, u)
    	tmp = (((single(8.0) * u) * u) + (u * single(4.0))) * s;
    end
    
    \begin{array}{l}
    
    \\
    \left(\left(8 \cdot u\right) \cdot u + u \cdot 4\right) \cdot s
    \end{array}
    
    Derivation
    1. Initial program 61.7%

      \[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. log-divN/A

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

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

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

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

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

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

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

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

      \[\leadsto s \cdot \frac{1}{\color{blue}{\frac{\frac{1}{4}}{u}}} \]
    6. Step-by-step derivation
      1. lower-/.f3273.1

        \[\leadsto s \cdot \frac{1}{\color{blue}{\frac{0.25}{u}}} \]
    7. Applied rewrites73.1%

      \[\leadsto s \cdot \frac{1}{\color{blue}{\frac{0.25}{u}}} \]
    8. Taylor expanded in u around 0

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

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

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

        \[\leadsto s \cdot \left(\color{blue}{\left(8 \cdot u + 4\right)} \cdot u\right) \]
      4. lower-fma.f3273.2

        \[\leadsto s \cdot \left(\color{blue}{\mathsf{fma}\left(8, u, 4\right)} \cdot u\right) \]
    10. Applied rewrites73.2%

      \[\leadsto s \cdot \color{blue}{\left(\mathsf{fma}\left(8, u, 4\right) \cdot u\right)} \]
    11. Step-by-step derivation
      1. Applied rewrites86.0%

        \[\leadsto s \cdot \left(\left(8 \cdot u\right) \cdot u + \color{blue}{4 \cdot u}\right) \]
      2. Final simplification86.0%

        \[\leadsto \left(\left(8 \cdot u\right) \cdot u + u \cdot 4\right) \cdot s \]
      3. Add Preprocessing

      Alternative 3: 86.7% accurate, 6.6× speedup?

      \[\begin{array}{l} \\ \left(\left(8 \cdot u + 4\right) \cdot u\right) \cdot s \end{array} \]
      (FPCore (s u) :precision binary32 (* (* (+ (* 8.0 u) 4.0) u) s))
      float code(float s, float u) {
      	return (((8.0f * u) + 4.0f) * u) * s;
      }
      
      real(4) function code(s, u)
          real(4), intent (in) :: s
          real(4), intent (in) :: u
          code = (((8.0e0 * u) + 4.0e0) * u) * s
      end function
      
      function code(s, u)
      	return Float32(Float32(Float32(Float32(Float32(8.0) * u) + Float32(4.0)) * u) * s)
      end
      
      function tmp = code(s, u)
      	tmp = (((single(8.0) * u) + single(4.0)) * u) * s;
      end
      
      \begin{array}{l}
      
      \\
      \left(\left(8 \cdot u + 4\right) \cdot u\right) \cdot s
      \end{array}
      
      Derivation
      1. Initial program 61.7%

        \[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. log-divN/A

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

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

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

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

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

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

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

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

        \[\leadsto s \cdot \frac{1}{\color{blue}{\frac{\frac{1}{4}}{u}}} \]
      6. Step-by-step derivation
        1. lower-/.f3273.1

          \[\leadsto s \cdot \frac{1}{\color{blue}{\frac{0.25}{u}}} \]
      7. Applied rewrites73.1%

        \[\leadsto s \cdot \frac{1}{\color{blue}{\frac{0.25}{u}}} \]
      8. Taylor expanded in u around 0

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

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

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

          \[\leadsto s \cdot \left(\color{blue}{\left(8 \cdot u + 4\right)} \cdot u\right) \]
        4. lower-fma.f3273.2

          \[\leadsto s \cdot \left(\color{blue}{\mathsf{fma}\left(8, u, 4\right)} \cdot u\right) \]
      10. Applied rewrites73.2%

        \[\leadsto s \cdot \color{blue}{\left(\mathsf{fma}\left(8, u, 4\right) \cdot u\right)} \]
      11. Step-by-step derivation
        1. Applied rewrites85.8%

          \[\leadsto s \cdot \left(\left(8 \cdot u + 4\right) \cdot u\right) \]
        2. Final simplification85.8%

          \[\leadsto \left(\left(8 \cdot u + 4\right) \cdot u\right) \cdot s \]
        3. Add Preprocessing

        Alternative 4: 73.7% accurate, 11.4× speedup?

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

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

          \[\leadsto s \cdot \color{blue}{\left(4 \cdot u\right)} \]
        4. Step-by-step derivation
          1. lower-*.f3273.2

            \[\leadsto s \cdot \color{blue}{\left(4 \cdot u\right)} \]
        5. Applied rewrites73.2%

          \[\leadsto s \cdot \color{blue}{\left(4 \cdot u\right)} \]
        6. Final simplification73.2%

          \[\leadsto \left(u \cdot 4\right) \cdot s \]
        7. Add Preprocessing

        Reproduce

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