HairBSDF, sample_f, cosTheta

Percentage Accurate: 99.5% → 99.5%
Time: 15.8s
Alternatives: 21
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 21 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, 0.9× speedup?

\[\begin{array}{l} \\ \mathsf{fma}\left(v, \log \left(\mathsf{fma}\left(\mathsf{fma}\left(u, -u, 1\right), \frac{e^{\frac{-2}{v}}}{1 + u}, u\right)\right), 1\right) \end{array} \]
(FPCore (u v)
 :precision binary32
 (fma v (log (fma (fma u (- u) 1.0) (/ (exp (/ -2.0 v)) (+ 1.0 u)) u)) 1.0))
float code(float u, float v) {
	return fmaf(v, logf(fmaf(fmaf(u, -u, 1.0f), (expf((-2.0f / v)) / (1.0f + u)), u)), 1.0f);
}
function code(u, v)
	return fma(v, log(fma(fma(u, Float32(-u), Float32(1.0)), Float32(exp(Float32(Float32(-2.0) / v)) / Float32(Float32(1.0) + u)), u)), Float32(1.0))
end
\begin{array}{l}

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

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

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

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

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

      \[\leadsto \color{blue}{\mathsf{fma}\left(v, \log \left(u + \frac{e^{\frac{-2}{v}} \cdot \left(1 - {u}^{2}\right)}{1 + u}\right), 1\right)} \]
  6. Applied rewrites99.6%

    \[\leadsto \color{blue}{\mathsf{fma}\left(v, \log \left(\mathsf{fma}\left(\mathsf{fma}\left(u, -u, 1\right), \frac{e^{\frac{-2}{v}}}{u + 1}, u\right)\right), 1\right)} \]
  7. Final simplification99.6%

    \[\leadsto \mathsf{fma}\left(v, \log \left(\mathsf{fma}\left(\mathsf{fma}\left(u, -u, 1\right), \frac{e^{\frac{-2}{v}}}{1 + u}, u\right)\right), 1\right) \]
  8. Add Preprocessing

Alternative 2: 97.5% accurate, 1.0× speedup?

\[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;v \leq 0.20000000298023224:\\ \;\;\;\;\mathsf{fma}\left(v, \log \left(\left(-u\right) \cdot \mathsf{expm1}\left(\frac{-2}{v}\right)\right), 1\right)\\ \mathbf{else}:\\ \;\;\;\;1 + v \cdot \mathsf{fma}\left(u, \mathsf{expm1}\left(\frac{2}{v}\right), \frac{-2}{v}\right)\\ \end{array} \end{array} \]
(FPCore (u v)
 :precision binary32
 (if (<= v 0.20000000298023224)
   (fma v (log (* (- u) (expm1 (/ -2.0 v)))) 1.0)
   (+ 1.0 (* v (fma u (expm1 (/ 2.0 v)) (/ -2.0 v))))))
float code(float u, float v) {
	float tmp;
	if (v <= 0.20000000298023224f) {
		tmp = fmaf(v, logf((-u * expm1f((-2.0f / v)))), 1.0f);
	} else {
		tmp = 1.0f + (v * fmaf(u, expm1f((2.0f / v)), (-2.0f / v)));
	}
	return tmp;
}
function code(u, v)
	tmp = Float32(0.0)
	if (v <= Float32(0.20000000298023224))
		tmp = fma(v, log(Float32(Float32(-u) * expm1(Float32(Float32(-2.0) / v)))), Float32(1.0));
	else
		tmp = Float32(Float32(1.0) + Float32(v * fma(u, expm1(Float32(Float32(2.0) / v)), Float32(Float32(-2.0) / v))));
	end
	return tmp
end
\begin{array}{l}

\\
\begin{array}{l}
\mathbf{if}\;v \leq 0.20000000298023224:\\
\;\;\;\;\mathsf{fma}\left(v, \log \left(\left(-u\right) \cdot \mathsf{expm1}\left(\frac{-2}{v}\right)\right), 1\right)\\

\mathbf{else}:\\
\;\;\;\;1 + v \cdot \mathsf{fma}\left(u, \mathsf{expm1}\left(\frac{2}{v}\right), \frac{-2}{v}\right)\\


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if v < 0.200000003

    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. Applied rewrites100.0%

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

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

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

        \[\leadsto \color{blue}{\mathsf{fma}\left(v, \log \left(u + \frac{e^{\frac{-2}{v}} \cdot \left(1 - {u}^{2}\right)}{1 + u}\right), 1\right)} \]
    6. Applied rewrites100.0%

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

      \[\leadsto \mathsf{fma}\left(v, \log \left(-1 \cdot \left(u \cdot \left(e^{\frac{-2}{v}} - 1\right)\right)\right), 1\right) \]
    8. Step-by-step derivation
      1. Applied rewrites99.7%

        \[\leadsto \mathsf{fma}\left(v, \log \left(\mathsf{expm1}\left(\frac{-2}{v}\right) \cdot \left(-u\right)\right), 1\right) \]

      if 0.200000003 < v

      1. Initial program 93.0%

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

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

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

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

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

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

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

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

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

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

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

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

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

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

          \[\leadsto 1 + v \cdot \mathsf{fma}\left(u, \mathsf{expm1}\left(\frac{2}{v}\right), \mathsf{neg}\left(\frac{\color{blue}{2}}{v}\right)\right) \]
        14. distribute-neg-fracN/A

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

          \[\leadsto 1 + v \cdot \mathsf{fma}\left(u, \mathsf{expm1}\left(\frac{2}{v}\right), \frac{\color{blue}{-2}}{v}\right) \]
        16. lower-/.f3265.9

          \[\leadsto 1 + v \cdot \mathsf{fma}\left(u, \mathsf{expm1}\left(\frac{2}{v}\right), \color{blue}{\frac{-2}{v}}\right) \]
      5. Applied rewrites65.9%

        \[\leadsto 1 + v \cdot \color{blue}{\mathsf{fma}\left(u, \mathsf{expm1}\left(\frac{2}{v}\right), \frac{-2}{v}\right)} \]
    9. Recombined 2 regimes into one program.
    10. Final simplification97.5%

      \[\leadsto \begin{array}{l} \mathbf{if}\;v \leq 0.20000000298023224:\\ \;\;\;\;\mathsf{fma}\left(v, \log \left(\left(-u\right) \cdot \mathsf{expm1}\left(\frac{-2}{v}\right)\right), 1\right)\\ \mathbf{else}:\\ \;\;\;\;1 + v \cdot \mathsf{fma}\left(u, \mathsf{expm1}\left(\frac{2}{v}\right), \frac{-2}{v}\right)\\ \end{array} \]
    11. Add Preprocessing

    Reproduce

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