HairBSDF, sample_f, cosTheta

Percentage Accurate: 99.5% → 99.5%
Time: 12.5s
Alternatives: 17
Speedup: 1.0×

Specification

?
\[\left(10^{-5} \leq u \land u \leq 1\right) \land \left(0 \leq v \land v \leq 109.746574\right)\]
\[\begin{array}{l} \\ 1 + v \cdot \log \left(u + \left(1 - u\right) \cdot e^{\frac{-2}{v}}\right) \end{array} \]
(FPCore (u v)
 :precision binary32
 (+ 1.0 (* v (log (+ u (* (- 1.0 u) (exp (/ -2.0 v))))))))
float code(float u, float v) {
	return 1.0f + (v * logf((u + ((1.0f - u) * expf((-2.0f / v))))));
}
real(4) function code(u, v)
    real(4), intent (in) :: u
    real(4), intent (in) :: v
    code = 1.0e0 + (v * log((u + ((1.0e0 - u) * exp(((-2.0e0) / v))))))
end function
function code(u, v)
	return Float32(Float32(1.0) + Float32(v * log(Float32(u + Float32(Float32(Float32(1.0) - u) * exp(Float32(Float32(-2.0) / v)))))))
end
function tmp = code(u, v)
	tmp = single(1.0) + (v * log((u + ((single(1.0) - u) * exp((single(-2.0) / v))))));
end
\begin{array}{l}

\\
1 + v \cdot \log \left(u + \left(1 - u\right) \cdot e^{\frac{-2}{v}}\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 17 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.5% accurate, 1.0× speedup?

\[\begin{array}{l} \\ 1 + v \cdot \log \left(u + \left(1 - u\right) \cdot e^{\frac{-2}{v}}\right) \end{array} \]
(FPCore (u v)
 :precision binary32
 (+ 1.0 (* v (log (+ u (* (- 1.0 u) (exp (/ -2.0 v))))))))
float code(float u, float v) {
	return 1.0f + (v * logf((u + ((1.0f - u) * expf((-2.0f / v))))));
}
real(4) function code(u, v)
    real(4), intent (in) :: u
    real(4), intent (in) :: v
    code = 1.0e0 + (v * log((u + ((1.0e0 - u) * exp(((-2.0e0) / v))))))
end function
function code(u, v)
	return Float32(Float32(1.0) + Float32(v * log(Float32(u + Float32(Float32(Float32(1.0) - u) * exp(Float32(Float32(-2.0) / v)))))))
end
function tmp = code(u, v)
	tmp = single(1.0) + (v * log((u + ((single(1.0) - u) * exp((single(-2.0) / v))))));
end
\begin{array}{l}

\\
1 + v \cdot \log \left(u + \left(1 - u\right) \cdot e^{\frac{-2}{v}}\right)
\end{array}

Alternative 1: 99.5% accurate, 1.0× speedup?

\[\begin{array}{l} \\ 1 + v \cdot \log \left(u + \left(1 - u\right) \cdot e^{\frac{-2}{v}}\right) \end{array} \]
(FPCore (u v)
 :precision binary32
 (+ 1.0 (* v (log (+ u (* (- 1.0 u) (exp (/ -2.0 v))))))))
float code(float u, float v) {
	return 1.0f + (v * logf((u + ((1.0f - u) * expf((-2.0f / v))))));
}
real(4) function code(u, v)
    real(4), intent (in) :: u
    real(4), intent (in) :: v
    code = 1.0e0 + (v * log((u + ((1.0e0 - u) * exp(((-2.0e0) / v))))))
end function
function code(u, v)
	return Float32(Float32(1.0) + Float32(v * log(Float32(u + Float32(Float32(Float32(1.0) - u) * exp(Float32(Float32(-2.0) / v)))))))
end
function tmp = code(u, v)
	tmp = single(1.0) + (v * log((u + ((single(1.0) - u) * exp((single(-2.0) / v))))));
end
\begin{array}{l}

\\
1 + v \cdot \log \left(u + \left(1 - u\right) \cdot e^{\frac{-2}{v}}\right)
\end{array}
Derivation
  1. Initial program 99.7%

    \[1 + v \cdot \log \left(u + \left(1 - u\right) \cdot e^{\frac{-2}{v}}\right) \]
  2. Add Preprocessing
  3. Add Preprocessing

Alternative 2: 91.3% accurate, 0.6× speedup?

\[\begin{array}{l} \\ \begin{array}{l} t_0 := \left(1 - u\right) \cdot \left(1 - u\right)\\ \mathbf{if}\;v \cdot \log \left(u + \left(1 - u\right) \cdot e^{\frac{-2}{v}}\right) \leq -0.7879999876022339:\\ \;\;\;\;\mathsf{fma}\left(v, \frac{\mathsf{fma}\left(1 - u, 2, \frac{\mathsf{fma}\left(0.5, \mathsf{fma}\left(-4, t\_0, \left(1 - u\right) \cdot 4\right), \frac{\mathsf{fma}\left(t\_0, -24, \mathsf{fma}\left(\left(1 - u\right) \cdot t\_0, 16, \left(1 - u\right) \cdot 8\right)\right)}{v} \cdot -0.16666666666666666\right)}{-v}\right)}{-v}, 1\right)\\ \mathbf{else}:\\ \;\;\;\;1\\ \end{array} \end{array} \]
(FPCore (u v)
 :precision binary32
 (let* ((t_0 (* (- 1.0 u) (- 1.0 u))))
   (if (<=
        (* v (log (+ u (* (- 1.0 u) (exp (/ -2.0 v))))))
        -0.7879999876022339)
     (fma
      v
      (/
       (fma
        (- 1.0 u)
        2.0
        (/
         (fma
          0.5
          (fma -4.0 t_0 (* (- 1.0 u) 4.0))
          (*
           (/ (fma t_0 -24.0 (fma (* (- 1.0 u) t_0) 16.0 (* (- 1.0 u) 8.0))) v)
           -0.16666666666666666))
         (- v)))
       (- v))
      1.0)
     1.0)))
float code(float u, float v) {
	float t_0 = (1.0f - u) * (1.0f - u);
	float tmp;
	if ((v * logf((u + ((1.0f - u) * expf((-2.0f / v)))))) <= -0.7879999876022339f) {
		tmp = fmaf(v, (fmaf((1.0f - u), 2.0f, (fmaf(0.5f, fmaf(-4.0f, t_0, ((1.0f - u) * 4.0f)), ((fmaf(t_0, -24.0f, fmaf(((1.0f - u) * t_0), 16.0f, ((1.0f - u) * 8.0f))) / v) * -0.16666666666666666f)) / -v)) / -v), 1.0f);
	} else {
		tmp = 1.0f;
	}
	return tmp;
}
function code(u, v)
	t_0 = Float32(Float32(Float32(1.0) - u) * Float32(Float32(1.0) - u))
	tmp = Float32(0.0)
	if (Float32(v * log(Float32(u + Float32(Float32(Float32(1.0) - u) * exp(Float32(Float32(-2.0) / v)))))) <= Float32(-0.7879999876022339))
		tmp = fma(v, Float32(fma(Float32(Float32(1.0) - u), Float32(2.0), Float32(fma(Float32(0.5), fma(Float32(-4.0), t_0, Float32(Float32(Float32(1.0) - u) * Float32(4.0))), Float32(Float32(fma(t_0, Float32(-24.0), fma(Float32(Float32(Float32(1.0) - u) * t_0), Float32(16.0), Float32(Float32(Float32(1.0) - u) * Float32(8.0)))) / v) * Float32(-0.16666666666666666))) / Float32(-v))) / Float32(-v)), Float32(1.0));
	else
		tmp = Float32(1.0);
	end
	return tmp
end
\begin{array}{l}

\\
\begin{array}{l}
t_0 := \left(1 - u\right) \cdot \left(1 - u\right)\\
\mathbf{if}\;v \cdot \log \left(u + \left(1 - u\right) \cdot e^{\frac{-2}{v}}\right) \leq -0.7879999876022339:\\
\;\;\;\;\mathsf{fma}\left(v, \frac{\mathsf{fma}\left(1 - u, 2, \frac{\mathsf{fma}\left(0.5, \mathsf{fma}\left(-4, t\_0, \left(1 - u\right) \cdot 4\right), \frac{\mathsf{fma}\left(t\_0, -24, \mathsf{fma}\left(\left(1 - u\right) \cdot t\_0, 16, \left(1 - u\right) \cdot 8\right)\right)}{v} \cdot -0.16666666666666666\right)}{-v}\right)}{-v}, 1\right)\\

\mathbf{else}:\\
\;\;\;\;1\\


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if (*.f32 v (log.f32 (+.f32 u (*.f32 (-.f32 #s(literal 1 binary32) u) (exp.f32 (/.f32 #s(literal -2 binary32) v)))))) < -0.787999988

    1. Initial program 92.7%

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

      \[\leadsto \color{blue}{1 + v \cdot \log \left(u + e^{\frac{-2}{v}} \cdot \left(1 - u\right)\right)} \]
    4. Step-by-step derivation
      1. +-commutativeN/A

        \[\leadsto \color{blue}{v \cdot \log \left(u + e^{\frac{-2}{v}} \cdot \left(1 - u\right)\right) + 1} \]
      2. lower-fma.f32N/A

        \[\leadsto \color{blue}{\mathsf{fma}\left(v, \log \left(u + e^{\frac{-2}{v}} \cdot \left(1 - u\right)\right), 1\right)} \]
      3. lower-log.f32N/A

        \[\leadsto \mathsf{fma}\left(v, \color{blue}{\log \left(u + e^{\frac{-2}{v}} \cdot \left(1 - u\right)\right)}, 1\right) \]
      4. +-commutativeN/A

        \[\leadsto \mathsf{fma}\left(v, \log \color{blue}{\left(e^{\frac{-2}{v}} \cdot \left(1 - u\right) + u\right)}, 1\right) \]
      5. lower-fma.f32N/A

        \[\leadsto \mathsf{fma}\left(v, \log \color{blue}{\left(\mathsf{fma}\left(e^{\frac{-2}{v}}, 1 - u, u\right)\right)}, 1\right) \]
      6. metadata-evalN/A

        \[\leadsto \mathsf{fma}\left(v, \log \left(\mathsf{fma}\left(e^{\frac{\color{blue}{\mathsf{neg}\left(2\right)}}{v}}, 1 - u, u\right)\right), 1\right) \]
      7. distribute-neg-fracN/A

        \[\leadsto \mathsf{fma}\left(v, \log \left(\mathsf{fma}\left(e^{\color{blue}{\mathsf{neg}\left(\frac{2}{v}\right)}}, 1 - u, u\right)\right), 1\right) \]
      8. metadata-evalN/A

        \[\leadsto \mathsf{fma}\left(v, \log \left(\mathsf{fma}\left(e^{\mathsf{neg}\left(\frac{\color{blue}{2 \cdot 1}}{v}\right)}, 1 - u, u\right)\right), 1\right) \]
      9. associate-*r/N/A

        \[\leadsto \mathsf{fma}\left(v, \log \left(\mathsf{fma}\left(e^{\mathsf{neg}\left(\color{blue}{2 \cdot \frac{1}{v}}\right)}, 1 - u, u\right)\right), 1\right) \]
      10. lower-exp.f32N/A

        \[\leadsto \mathsf{fma}\left(v, \log \left(\mathsf{fma}\left(\color{blue}{e^{\mathsf{neg}\left(2 \cdot \frac{1}{v}\right)}}, 1 - u, u\right)\right), 1\right) \]
      11. associate-*r/N/A

        \[\leadsto \mathsf{fma}\left(v, \log \left(\mathsf{fma}\left(e^{\mathsf{neg}\left(\color{blue}{\frac{2 \cdot 1}{v}}\right)}, 1 - u, u\right)\right), 1\right) \]
      12. metadata-evalN/A

        \[\leadsto \mathsf{fma}\left(v, \log \left(\mathsf{fma}\left(e^{\mathsf{neg}\left(\frac{\color{blue}{2}}{v}\right)}, 1 - u, u\right)\right), 1\right) \]
      13. distribute-neg-fracN/A

        \[\leadsto \mathsf{fma}\left(v, \log \left(\mathsf{fma}\left(e^{\color{blue}{\frac{\mathsf{neg}\left(2\right)}{v}}}, 1 - u, u\right)\right), 1\right) \]
      14. metadata-evalN/A

        \[\leadsto \mathsf{fma}\left(v, \log \left(\mathsf{fma}\left(e^{\frac{\color{blue}{-2}}{v}}, 1 - u, u\right)\right), 1\right) \]
      15. lower-/.f32N/A

        \[\leadsto \mathsf{fma}\left(v, \log \left(\mathsf{fma}\left(e^{\color{blue}{\frac{-2}{v}}}, 1 - u, u\right)\right), 1\right) \]
      16. lower--.f3292.7

        \[\leadsto \mathsf{fma}\left(v, \log \left(\mathsf{fma}\left(e^{\frac{-2}{v}}, \color{blue}{1 - u}, u\right)\right), 1\right) \]
    5. Applied rewrites92.7%

      \[\leadsto \color{blue}{\mathsf{fma}\left(v, \log \left(\mathsf{fma}\left(e^{\frac{-2}{v}}, 1 - u, u\right)\right), 1\right)} \]
    6. Taylor expanded in v around -inf

      \[\leadsto \mathsf{fma}\left(v, -1 \cdot \color{blue}{\frac{-1 \cdot \frac{\frac{-1}{6} \cdot \frac{-24 \cdot {\left(1 - u\right)}^{2} + \left(8 \cdot \left(1 - u\right) + 16 \cdot {\left(1 - u\right)}^{3}\right)}{v} + \frac{1}{2} \cdot \left(-4 \cdot {\left(1 - u\right)}^{2} + 4 \cdot \left(1 - u\right)\right)}{v} + 2 \cdot \left(1 - u\right)}{v}}, 1\right) \]
    7. Applied rewrites69.3%

      \[\leadsto \mathsf{fma}\left(v, \frac{\mathsf{fma}\left(1 - u, 2, \frac{\mathsf{fma}\left(0.5, \mathsf{fma}\left(-4, \left(1 - u\right) \cdot \left(1 - u\right), \left(1 - u\right) \cdot 4\right), \frac{\mathsf{fma}\left(\left(1 - u\right) \cdot \left(1 - u\right), -24, \mathsf{fma}\left(\left(1 - u\right) \cdot \left(\left(1 - u\right) \cdot \left(1 - u\right)\right), 16, \left(1 - u\right) \cdot 8\right)\right)}{v} \cdot -0.16666666666666666\right)}{-v}\right)}{\color{blue}{-v}}, 1\right) \]

    if -0.787999988 < (*.f32 v (log.f32 (+.f32 u (*.f32 (-.f32 #s(literal 1 binary32) u) (exp.f32 (/.f32 #s(literal -2 binary32) v))))))

    1. Initial program 100.0%

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

      \[\leadsto \color{blue}{1} \]
    4. Step-by-step derivation
      1. Applied rewrites92.8%

        \[\leadsto \color{blue}{1} \]
    5. Recombined 2 regimes into one program.
    6. Add Preprocessing

    Reproduce

    ?
    herbie shell --seed 2024223 
    (FPCore (u v)
      :name "HairBSDF, sample_f, cosTheta"
      :precision binary32
      :pre (and (and (<= 1e-5 u) (<= u 1.0)) (and (<= 0.0 v) (<= v 109.746574)))
      (+ 1.0 (* v (log (+ u (* (- 1.0 u) (exp (/ -2.0 v))))))))