Disney BSSRDF, PDF of scattering profile

Percentage Accurate: 99.6% → 99.6%
Time: 16.7s
Alternatives: 26
Speedup: N/A×

Specification

?
\[\left(0 \leq s \land s \leq 256\right) \land \left(10^{-6} < r \land r < 1000000\right)\]
\[\begin{array}{l} \\ \frac{0.25 \cdot e^{\frac{-r}{s}}}{\left(\left(2 \cdot \pi\right) \cdot s\right) \cdot r} + \frac{0.75 \cdot e^{\frac{-r}{3 \cdot s}}}{\left(\left(6 \cdot \pi\right) \cdot s\right) \cdot r} \end{array} \]
(FPCore (s r)
 :precision binary32
 (+
  (/ (* 0.25 (exp (/ (- r) s))) (* (* (* 2.0 PI) s) r))
  (/ (* 0.75 (exp (/ (- r) (* 3.0 s)))) (* (* (* 6.0 PI) s) r))))
float code(float s, float r) {
	return ((0.25f * expf((-r / s))) / (((2.0f * ((float) M_PI)) * s) * r)) + ((0.75f * expf((-r / (3.0f * s)))) / (((6.0f * ((float) M_PI)) * s) * r));
}
function code(s, r)
	return Float32(Float32(Float32(Float32(0.25) * exp(Float32(Float32(-r) / s))) / Float32(Float32(Float32(Float32(2.0) * Float32(pi)) * s) * r)) + Float32(Float32(Float32(0.75) * exp(Float32(Float32(-r) / Float32(Float32(3.0) * s)))) / Float32(Float32(Float32(Float32(6.0) * Float32(pi)) * s) * r)))
end
function tmp = code(s, r)
	tmp = ((single(0.25) * exp((-r / s))) / (((single(2.0) * single(pi)) * s) * r)) + ((single(0.75) * exp((-r / (single(3.0) * s)))) / (((single(6.0) * single(pi)) * s) * r));
end
\begin{array}{l}

\\
\frac{0.25 \cdot e^{\frac{-r}{s}}}{\left(\left(2 \cdot \pi\right) \cdot s\right) \cdot r} + \frac{0.75 \cdot e^{\frac{-r}{3 \cdot s}}}{\left(\left(6 \cdot \pi\right) \cdot s\right) \cdot r}
\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 26 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.6% accurate, 1.0× speedup?

\[\begin{array}{l} \\ \frac{0.25 \cdot e^{\frac{-r}{s}}}{\left(\left(2 \cdot \pi\right) \cdot s\right) \cdot r} + \frac{0.75 \cdot e^{\frac{-r}{3 \cdot s}}}{\left(\left(6 \cdot \pi\right) \cdot s\right) \cdot r} \end{array} \]
(FPCore (s r)
 :precision binary32
 (+
  (/ (* 0.25 (exp (/ (- r) s))) (* (* (* 2.0 PI) s) r))
  (/ (* 0.75 (exp (/ (- r) (* 3.0 s)))) (* (* (* 6.0 PI) s) r))))
float code(float s, float r) {
	return ((0.25f * expf((-r / s))) / (((2.0f * ((float) M_PI)) * s) * r)) + ((0.75f * expf((-r / (3.0f * s)))) / (((6.0f * ((float) M_PI)) * s) * r));
}
function code(s, r)
	return Float32(Float32(Float32(Float32(0.25) * exp(Float32(Float32(-r) / s))) / Float32(Float32(Float32(Float32(2.0) * Float32(pi)) * s) * r)) + Float32(Float32(Float32(0.75) * exp(Float32(Float32(-r) / Float32(Float32(3.0) * s)))) / Float32(Float32(Float32(Float32(6.0) * Float32(pi)) * s) * r)))
end
function tmp = code(s, r)
	tmp = ((single(0.25) * exp((-r / s))) / (((single(2.0) * single(pi)) * s) * r)) + ((single(0.75) * exp((-r / (single(3.0) * s)))) / (((single(6.0) * single(pi)) * s) * r));
end
\begin{array}{l}

\\
\frac{0.25 \cdot e^{\frac{-r}{s}}}{\left(\left(2 \cdot \pi\right) \cdot s\right) \cdot r} + \frac{0.75 \cdot e^{\frac{-r}{3 \cdot s}}}{\left(\left(6 \cdot \pi\right) \cdot s\right) \cdot r}
\end{array}

Alternative 1: 99.6% accurate, 1.0× speedup?

\[\begin{array}{l} \\ \frac{0.25 \cdot e^{\frac{r}{-s}}}{r \cdot \left(s \cdot \left(2 \cdot \pi\right)\right)} + \frac{\frac{e^{\frac{r}{s \cdot -3}}}{\pi \cdot 6}}{\left(r \cdot s\right) \cdot 1.3333333333333333} \end{array} \]
(FPCore (s r)
 :precision binary32
 (+
  (/ (* 0.25 (exp (/ r (- s)))) (* r (* s (* 2.0 PI))))
  (/ (/ (exp (/ r (* s -3.0))) (* PI 6.0)) (* (* r s) 1.3333333333333333))))
float code(float s, float r) {
	return ((0.25f * expf((r / -s))) / (r * (s * (2.0f * ((float) M_PI))))) + ((expf((r / (s * -3.0f))) / (((float) M_PI) * 6.0f)) / ((r * s) * 1.3333333333333333f));
}
function code(s, r)
	return Float32(Float32(Float32(Float32(0.25) * exp(Float32(r / Float32(-s)))) / Float32(r * Float32(s * Float32(Float32(2.0) * Float32(pi))))) + Float32(Float32(exp(Float32(r / Float32(s * Float32(-3.0)))) / Float32(Float32(pi) * Float32(6.0))) / Float32(Float32(r * s) * Float32(1.3333333333333333))))
end
function tmp = code(s, r)
	tmp = ((single(0.25) * exp((r / -s))) / (r * (s * (single(2.0) * single(pi))))) + ((exp((r / (s * single(-3.0)))) / (single(pi) * single(6.0))) / ((r * s) * single(1.3333333333333333)));
end
\begin{array}{l}

\\
\frac{0.25 \cdot e^{\frac{r}{-s}}}{r \cdot \left(s \cdot \left(2 \cdot \pi\right)\right)} + \frac{\frac{e^{\frac{r}{s \cdot -3}}}{\pi \cdot 6}}{\left(r \cdot s\right) \cdot 1.3333333333333333}
\end{array}
Derivation
  1. Initial program 99.7%

    \[\frac{0.25 \cdot e^{\frac{-r}{s}}}{\left(\left(2 \cdot \pi\right) \cdot s\right) \cdot r} + \frac{0.75 \cdot e^{\frac{-r}{3 \cdot s}}}{\left(\left(6 \cdot \pi\right) \cdot s\right) \cdot r} \]
  2. Add Preprocessing
  3. Step-by-step derivation
    1. lift-/.f32N/A

      \[\leadsto \frac{\frac{1}{4} \cdot e^{\frac{\mathsf{neg}\left(r\right)}{s}}}{\left(\left(2 \cdot \mathsf{PI}\left(\right)\right) \cdot s\right) \cdot r} + \color{blue}{\frac{\frac{3}{4} \cdot e^{\frac{\mathsf{neg}\left(r\right)}{3 \cdot s}}}{\left(\left(6 \cdot \mathsf{PI}\left(\right)\right) \cdot s\right) \cdot r}} \]
    2. clear-numN/A

      \[\leadsto \frac{\frac{1}{4} \cdot e^{\frac{\mathsf{neg}\left(r\right)}{s}}}{\left(\left(2 \cdot \mathsf{PI}\left(\right)\right) \cdot s\right) \cdot r} + \color{blue}{\frac{1}{\frac{\left(\left(6 \cdot \mathsf{PI}\left(\right)\right) \cdot s\right) \cdot r}{\frac{3}{4} \cdot e^{\frac{\mathsf{neg}\left(r\right)}{3 \cdot s}}}}} \]
    3. lift-*.f32N/A

      \[\leadsto \frac{\frac{1}{4} \cdot e^{\frac{\mathsf{neg}\left(r\right)}{s}}}{\left(\left(2 \cdot \mathsf{PI}\left(\right)\right) \cdot s\right) \cdot r} + \frac{1}{\frac{\color{blue}{\left(\left(6 \cdot \mathsf{PI}\left(\right)\right) \cdot s\right) \cdot r}}{\frac{3}{4} \cdot e^{\frac{\mathsf{neg}\left(r\right)}{3 \cdot s}}}} \]
    4. lift-*.f32N/A

      \[\leadsto \frac{\frac{1}{4} \cdot e^{\frac{\mathsf{neg}\left(r\right)}{s}}}{\left(\left(2 \cdot \mathsf{PI}\left(\right)\right) \cdot s\right) \cdot r} + \frac{1}{\frac{\color{blue}{\left(\left(6 \cdot \mathsf{PI}\left(\right)\right) \cdot s\right)} \cdot r}{\frac{3}{4} \cdot e^{\frac{\mathsf{neg}\left(r\right)}{3 \cdot s}}}} \]
    5. associate-*l*N/A

      \[\leadsto \frac{\frac{1}{4} \cdot e^{\frac{\mathsf{neg}\left(r\right)}{s}}}{\left(\left(2 \cdot \mathsf{PI}\left(\right)\right) \cdot s\right) \cdot r} + \frac{1}{\frac{\color{blue}{\left(6 \cdot \mathsf{PI}\left(\right)\right) \cdot \left(s \cdot r\right)}}{\frac{3}{4} \cdot e^{\frac{\mathsf{neg}\left(r\right)}{3 \cdot s}}}} \]
    6. lift-*.f32N/A

      \[\leadsto \frac{\frac{1}{4} \cdot e^{\frac{\mathsf{neg}\left(r\right)}{s}}}{\left(\left(2 \cdot \mathsf{PI}\left(\right)\right) \cdot s\right) \cdot r} + \frac{1}{\frac{\left(6 \cdot \mathsf{PI}\left(\right)\right) \cdot \left(s \cdot r\right)}{\color{blue}{\frac{3}{4} \cdot e^{\frac{\mathsf{neg}\left(r\right)}{3 \cdot s}}}}} \]
    7. *-commutativeN/A

      \[\leadsto \frac{\frac{1}{4} \cdot e^{\frac{\mathsf{neg}\left(r\right)}{s}}}{\left(\left(2 \cdot \mathsf{PI}\left(\right)\right) \cdot s\right) \cdot r} + \frac{1}{\frac{\left(6 \cdot \mathsf{PI}\left(\right)\right) \cdot \left(s \cdot r\right)}{\color{blue}{e^{\frac{\mathsf{neg}\left(r\right)}{3 \cdot s}} \cdot \frac{3}{4}}}} \]
    8. times-fracN/A

      \[\leadsto \frac{\frac{1}{4} \cdot e^{\frac{\mathsf{neg}\left(r\right)}{s}}}{\left(\left(2 \cdot \mathsf{PI}\left(\right)\right) \cdot s\right) \cdot r} + \frac{1}{\color{blue}{\frac{6 \cdot \mathsf{PI}\left(\right)}{e^{\frac{\mathsf{neg}\left(r\right)}{3 \cdot s}}} \cdot \frac{s \cdot r}{\frac{3}{4}}}} \]
    9. associate-/r*N/A

      \[\leadsto \frac{\frac{1}{4} \cdot e^{\frac{\mathsf{neg}\left(r\right)}{s}}}{\left(\left(2 \cdot \mathsf{PI}\left(\right)\right) \cdot s\right) \cdot r} + \color{blue}{\frac{\frac{1}{\frac{6 \cdot \mathsf{PI}\left(\right)}{e^{\frac{\mathsf{neg}\left(r\right)}{3 \cdot s}}}}}{\frac{s \cdot r}{\frac{3}{4}}}} \]
    10. clear-numN/A

      \[\leadsto \frac{\frac{1}{4} \cdot e^{\frac{\mathsf{neg}\left(r\right)}{s}}}{\left(\left(2 \cdot \mathsf{PI}\left(\right)\right) \cdot s\right) \cdot r} + \frac{\color{blue}{\frac{e^{\frac{\mathsf{neg}\left(r\right)}{3 \cdot s}}}{6 \cdot \mathsf{PI}\left(\right)}}}{\frac{s \cdot r}{\frac{3}{4}}} \]
    11. lower-/.f32N/A

      \[\leadsto \frac{\frac{1}{4} \cdot e^{\frac{\mathsf{neg}\left(r\right)}{s}}}{\left(\left(2 \cdot \mathsf{PI}\left(\right)\right) \cdot s\right) \cdot r} + \color{blue}{\frac{\frac{e^{\frac{\mathsf{neg}\left(r\right)}{3 \cdot s}}}{6 \cdot \mathsf{PI}\left(\right)}}{\frac{s \cdot r}{\frac{3}{4}}}} \]
  4. Applied rewrites99.7%

    \[\leadsto \frac{0.25 \cdot e^{\frac{-r}{s}}}{\left(\left(2 \cdot \pi\right) \cdot s\right) \cdot r} + \color{blue}{\frac{\frac{e^{\frac{r}{s \cdot -3}}}{\pi \cdot 6}}{\left(r \cdot s\right) \cdot 1.3333333333333333}} \]
  5. Final simplification99.7%

    \[\leadsto \frac{0.25 \cdot e^{\frac{r}{-s}}}{r \cdot \left(s \cdot \left(2 \cdot \pi\right)\right)} + \frac{\frac{e^{\frac{r}{s \cdot -3}}}{\pi \cdot 6}}{\left(r \cdot s\right) \cdot 1.3333333333333333} \]
  6. Add Preprocessing

Alternative 2: 99.5% accurate, 1.1× speedup?

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

\\
\frac{0.125 \cdot \left(e^{\frac{r}{s \cdot -3}} + e^{\frac{r}{-s}}\right)}{r \cdot \left(s \cdot \pi\right)}
\end{array}
Derivation
  1. Initial program 99.6%

    \[\frac{0.25 \cdot e^{\frac{-r}{s}}}{\left(\left(2 \cdot \pi\right) \cdot s\right) \cdot r} + \frac{0.75 \cdot e^{\frac{-r}{3 \cdot s}}}{\left(\left(6 \cdot \pi\right) \cdot s\right) \cdot r} \]
  2. Add Preprocessing
  3. Step-by-step derivation
    1. lift-/.f32N/A

      \[\leadsto \frac{\frac{1}{4} \cdot e^{\frac{\mathsf{neg}\left(r\right)}{s}}}{\left(\left(2 \cdot \mathsf{PI}\left(\right)\right) \cdot s\right) \cdot r} + \color{blue}{\frac{\frac{3}{4} \cdot e^{\frac{\mathsf{neg}\left(r\right)}{3 \cdot s}}}{\left(\left(6 \cdot \mathsf{PI}\left(\right)\right) \cdot s\right) \cdot r}} \]
    2. clear-numN/A

      \[\leadsto \frac{\frac{1}{4} \cdot e^{\frac{\mathsf{neg}\left(r\right)}{s}}}{\left(\left(2 \cdot \mathsf{PI}\left(\right)\right) \cdot s\right) \cdot r} + \color{blue}{\frac{1}{\frac{\left(\left(6 \cdot \mathsf{PI}\left(\right)\right) \cdot s\right) \cdot r}{\frac{3}{4} \cdot e^{\frac{\mathsf{neg}\left(r\right)}{3 \cdot s}}}}} \]
    3. lift-*.f32N/A

      \[\leadsto \frac{\frac{1}{4} \cdot e^{\frac{\mathsf{neg}\left(r\right)}{s}}}{\left(\left(2 \cdot \mathsf{PI}\left(\right)\right) \cdot s\right) \cdot r} + \frac{1}{\frac{\color{blue}{\left(\left(6 \cdot \mathsf{PI}\left(\right)\right) \cdot s\right) \cdot r}}{\frac{3}{4} \cdot e^{\frac{\mathsf{neg}\left(r\right)}{3 \cdot s}}}} \]
    4. lift-*.f32N/A

      \[\leadsto \frac{\frac{1}{4} \cdot e^{\frac{\mathsf{neg}\left(r\right)}{s}}}{\left(\left(2 \cdot \mathsf{PI}\left(\right)\right) \cdot s\right) \cdot r} + \frac{1}{\frac{\color{blue}{\left(\left(6 \cdot \mathsf{PI}\left(\right)\right) \cdot s\right)} \cdot r}{\frac{3}{4} \cdot e^{\frac{\mathsf{neg}\left(r\right)}{3 \cdot s}}}} \]
    5. associate-*l*N/A

      \[\leadsto \frac{\frac{1}{4} \cdot e^{\frac{\mathsf{neg}\left(r\right)}{s}}}{\left(\left(2 \cdot \mathsf{PI}\left(\right)\right) \cdot s\right) \cdot r} + \frac{1}{\frac{\color{blue}{\left(6 \cdot \mathsf{PI}\left(\right)\right) \cdot \left(s \cdot r\right)}}{\frac{3}{4} \cdot e^{\frac{\mathsf{neg}\left(r\right)}{3 \cdot s}}}} \]
    6. lift-*.f32N/A

      \[\leadsto \frac{\frac{1}{4} \cdot e^{\frac{\mathsf{neg}\left(r\right)}{s}}}{\left(\left(2 \cdot \mathsf{PI}\left(\right)\right) \cdot s\right) \cdot r} + \frac{1}{\frac{\left(6 \cdot \mathsf{PI}\left(\right)\right) \cdot \left(s \cdot r\right)}{\color{blue}{\frac{3}{4} \cdot e^{\frac{\mathsf{neg}\left(r\right)}{3 \cdot s}}}}} \]
    7. *-commutativeN/A

      \[\leadsto \frac{\frac{1}{4} \cdot e^{\frac{\mathsf{neg}\left(r\right)}{s}}}{\left(\left(2 \cdot \mathsf{PI}\left(\right)\right) \cdot s\right) \cdot r} + \frac{1}{\frac{\left(6 \cdot \mathsf{PI}\left(\right)\right) \cdot \left(s \cdot r\right)}{\color{blue}{e^{\frac{\mathsf{neg}\left(r\right)}{3 \cdot s}} \cdot \frac{3}{4}}}} \]
    8. times-fracN/A

      \[\leadsto \frac{\frac{1}{4} \cdot e^{\frac{\mathsf{neg}\left(r\right)}{s}}}{\left(\left(2 \cdot \mathsf{PI}\left(\right)\right) \cdot s\right) \cdot r} + \frac{1}{\color{blue}{\frac{6 \cdot \mathsf{PI}\left(\right)}{e^{\frac{\mathsf{neg}\left(r\right)}{3 \cdot s}}} \cdot \frac{s \cdot r}{\frac{3}{4}}}} \]
    9. associate-/r*N/A

      \[\leadsto \frac{\frac{1}{4} \cdot e^{\frac{\mathsf{neg}\left(r\right)}{s}}}{\left(\left(2 \cdot \mathsf{PI}\left(\right)\right) \cdot s\right) \cdot r} + \color{blue}{\frac{\frac{1}{\frac{6 \cdot \mathsf{PI}\left(\right)}{e^{\frac{\mathsf{neg}\left(r\right)}{3 \cdot s}}}}}{\frac{s \cdot r}{\frac{3}{4}}}} \]
    10. clear-numN/A

      \[\leadsto \frac{\frac{1}{4} \cdot e^{\frac{\mathsf{neg}\left(r\right)}{s}}}{\left(\left(2 \cdot \mathsf{PI}\left(\right)\right) \cdot s\right) \cdot r} + \frac{\color{blue}{\frac{e^{\frac{\mathsf{neg}\left(r\right)}{3 \cdot s}}}{6 \cdot \mathsf{PI}\left(\right)}}}{\frac{s \cdot r}{\frac{3}{4}}} \]
    11. lower-/.f32N/A

      \[\leadsto \frac{\frac{1}{4} \cdot e^{\frac{\mathsf{neg}\left(r\right)}{s}}}{\left(\left(2 \cdot \mathsf{PI}\left(\right)\right) \cdot s\right) \cdot r} + \color{blue}{\frac{\frac{e^{\frac{\mathsf{neg}\left(r\right)}{3 \cdot s}}}{6 \cdot \mathsf{PI}\left(\right)}}{\frac{s \cdot r}{\frac{3}{4}}}} \]
  4. Applied rewrites99.6%

    \[\leadsto \frac{0.25 \cdot e^{\frac{-r}{s}}}{\left(\left(2 \cdot \pi\right) \cdot s\right) \cdot r} + \color{blue}{\frac{\frac{e^{\frac{r}{s \cdot -3}}}{\pi \cdot 6}}{\left(r \cdot s\right) \cdot 1.3333333333333333}} \]
  5. Applied rewrites99.5%

    \[\leadsto \color{blue}{\frac{0.125 \cdot \left(e^{\frac{r}{-s}} + e^{\frac{r}{s \cdot -3}}\right)}{r \cdot \left(s \cdot \pi\right)}} \]
  6. Final simplification99.5%

    \[\leadsto \frac{0.125 \cdot \left(e^{\frac{r}{s \cdot -3}} + e^{\frac{r}{-s}}\right)}{r \cdot \left(s \cdot \pi\right)} \]
  7. Add Preprocessing

Reproduce

?
herbie shell --seed 2024227 
(FPCore (s r)
  :name "Disney BSSRDF, PDF of scattering profile"
  :precision binary32
  :pre (and (and (<= 0.0 s) (<= s 256.0)) (and (< 1e-6 r) (< r 1000000.0)))
  (+ (/ (* 0.25 (exp (/ (- r) s))) (* (* (* 2.0 PI) s) r)) (/ (* 0.75 (exp (/ (- r) (* 3.0 s)))) (* (* (* 6.0 PI) s) r))))