Disney BSSRDF, PDF of scattering profile

Percentage Accurate: 99.5% → 99.5%
Time: 1.2min
Alternatives: 3
Speedup: 1.0×

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 3 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} \\ \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.5% accurate, 0.7× speedup?

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

\\
\frac{0.25}{s \cdot \left(2 \cdot \pi\right)} \cdot \frac{e^{\frac{r}{-s}}}{r} + 0.75 \cdot \frac{e^{\frac{r}{3 \cdot \left(-s\right)}}}{r \cdot \left(6 \cdot \left(s \cdot \left(\pi \cdot \log e\right)\right)\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. Step-by-step derivation
    1. times-frac99.6%

      \[\leadsto \color{blue}{\frac{0.25}{\left(2 \cdot \pi\right) \cdot s} \cdot \frac{e^{\frac{-r}{s}}}{r}} + \frac{0.75 \cdot e^{\frac{-r}{3 \cdot s}}}{\left(\left(6 \cdot \pi\right) \cdot s\right) \cdot r} \]
    2. *-commutative99.6%

      \[\leadsto \frac{0.25}{\color{blue}{s \cdot \left(2 \cdot \pi\right)}} \cdot \frac{e^{\frac{-r}{s}}}{r} + \frac{0.75 \cdot e^{\frac{-r}{3 \cdot s}}}{\left(\left(6 \cdot \pi\right) \cdot s\right) \cdot r} \]
    3. distribute-frac-neg99.6%

      \[\leadsto \frac{0.25}{s \cdot \left(2 \cdot \pi\right)} \cdot \frac{e^{\color{blue}{-\frac{r}{s}}}}{r} + \frac{0.75 \cdot e^{\frac{-r}{3 \cdot s}}}{\left(\left(6 \cdot \pi\right) \cdot s\right) \cdot r} \]
    4. associate-/l*99.7%

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

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

      \[\leadsto \frac{0.25}{s \cdot \left(2 \cdot \pi\right)} \cdot \frac{e^{-\frac{r}{s}}}{r} + 0.75 \cdot \frac{e^{\frac{-r}{s \cdot 3}}}{\color{blue}{r \cdot \left(\left(6 \cdot \pi\right) \cdot s\right)}} \]
    7. associate-*l*99.6%

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

    \[\leadsto \color{blue}{\frac{0.25}{s \cdot \left(2 \cdot \pi\right)} \cdot \frac{e^{-\frac{r}{s}}}{r} + 0.75 \cdot \frac{e^{\frac{-r}{s \cdot 3}}}{r \cdot \left(6 \cdot \left(\pi \cdot s\right)\right)}} \]
  4. Add Preprocessing
  5. Step-by-step derivation
    1. log1p-expm1-u99.6%

      \[\leadsto \frac{0.25}{s \cdot \left(2 \cdot \pi\right)} \cdot \frac{e^{-\frac{r}{s}}}{r} + 0.75 \cdot \frac{e^{\frac{-r}{s \cdot 3}}}{r \cdot \left(6 \cdot \left(\color{blue}{\mathsf{log1p}\left(\mathsf{expm1}\left(\pi\right)\right)} \cdot s\right)\right)} \]
    2. expm1-undefine99.6%

      \[\leadsto \frac{0.25}{s \cdot \left(2 \cdot \pi\right)} \cdot \frac{e^{-\frac{r}{s}}}{r} + 0.75 \cdot \frac{e^{\frac{-r}{s \cdot 3}}}{r \cdot \left(6 \cdot \left(\mathsf{log1p}\left(\color{blue}{e^{\pi} - 1}\right) \cdot s\right)\right)} \]
    3. *-un-lft-identity99.6%

      \[\leadsto \frac{0.25}{s \cdot \left(2 \cdot \pi\right)} \cdot \frac{e^{-\frac{r}{s}}}{r} + 0.75 \cdot \frac{e^{\frac{-r}{s \cdot 3}}}{r \cdot \left(6 \cdot \left(\mathsf{log1p}\left(e^{\color{blue}{1 \cdot \pi}} - 1\right) \cdot s\right)\right)} \]
    4. exp-prod99.7%

      \[\leadsto \frac{0.25}{s \cdot \left(2 \cdot \pi\right)} \cdot \frac{e^{-\frac{r}{s}}}{r} + 0.75 \cdot \frac{e^{\frac{-r}{s \cdot 3}}}{r \cdot \left(6 \cdot \left(\mathsf{log1p}\left(\color{blue}{{\left(e^{1}\right)}^{\pi}} - 1\right) \cdot s\right)\right)} \]
    5. pow-to-exp99.7%

      \[\leadsto \frac{0.25}{s \cdot \left(2 \cdot \pi\right)} \cdot \frac{e^{-\frac{r}{s}}}{r} + 0.75 \cdot \frac{e^{\frac{-r}{s \cdot 3}}}{r \cdot \left(6 \cdot \left(\mathsf{log1p}\left(\color{blue}{e^{\log \left(e^{1}\right) \cdot \pi}} - 1\right) \cdot s\right)\right)} \]
    6. expm1-define99.7%

      \[\leadsto \frac{0.25}{s \cdot \left(2 \cdot \pi\right)} \cdot \frac{e^{-\frac{r}{s}}}{r} + 0.75 \cdot \frac{e^{\frac{-r}{s \cdot 3}}}{r \cdot \left(6 \cdot \left(\mathsf{log1p}\left(\color{blue}{\mathsf{expm1}\left(\log \left(e^{1}\right) \cdot \pi\right)}\right) \cdot s\right)\right)} \]
    7. log1p-expm1-u99.7%

      \[\leadsto \frac{0.25}{s \cdot \left(2 \cdot \pi\right)} \cdot \frac{e^{-\frac{r}{s}}}{r} + 0.75 \cdot \frac{e^{\frac{-r}{s \cdot 3}}}{r \cdot \left(6 \cdot \left(\color{blue}{\left(\log \left(e^{1}\right) \cdot \pi\right)} \cdot s\right)\right)} \]
    8. exp-1-e99.7%

      \[\leadsto \frac{0.25}{s \cdot \left(2 \cdot \pi\right)} \cdot \frac{e^{-\frac{r}{s}}}{r} + 0.75 \cdot \frac{e^{\frac{-r}{s \cdot 3}}}{r \cdot \left(6 \cdot \left(\left(\log \color{blue}{e} \cdot \pi\right) \cdot s\right)\right)} \]
  6. Applied egg-rr99.7%

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

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

Alternative 2: 99.5% accurate, 1.0× speedup?

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

\\
\frac{0.25}{s \cdot \left(2 \cdot \pi\right)} \cdot \frac{e^{\frac{r}{-s}}}{r} + 0.75 \cdot \frac{e^{\frac{r}{3 \cdot \left(-s\right)}}}{r \cdot \left(6 \cdot \left(s \cdot \pi\right)\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. Step-by-step derivation
    1. times-frac99.6%

      \[\leadsto \color{blue}{\frac{0.25}{\left(2 \cdot \pi\right) \cdot s} \cdot \frac{e^{\frac{-r}{s}}}{r}} + \frac{0.75 \cdot e^{\frac{-r}{3 \cdot s}}}{\left(\left(6 \cdot \pi\right) \cdot s\right) \cdot r} \]
    2. *-commutative99.6%

      \[\leadsto \frac{0.25}{\color{blue}{s \cdot \left(2 \cdot \pi\right)}} \cdot \frac{e^{\frac{-r}{s}}}{r} + \frac{0.75 \cdot e^{\frac{-r}{3 \cdot s}}}{\left(\left(6 \cdot \pi\right) \cdot s\right) \cdot r} \]
    3. distribute-frac-neg99.6%

      \[\leadsto \frac{0.25}{s \cdot \left(2 \cdot \pi\right)} \cdot \frac{e^{\color{blue}{-\frac{r}{s}}}}{r} + \frac{0.75 \cdot e^{\frac{-r}{3 \cdot s}}}{\left(\left(6 \cdot \pi\right) \cdot s\right) \cdot r} \]
    4. associate-/l*99.7%

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

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

      \[\leadsto \frac{0.25}{s \cdot \left(2 \cdot \pi\right)} \cdot \frac{e^{-\frac{r}{s}}}{r} + 0.75 \cdot \frac{e^{\frac{-r}{s \cdot 3}}}{\color{blue}{r \cdot \left(\left(6 \cdot \pi\right) \cdot s\right)}} \]
    7. associate-*l*99.6%

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

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

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

Alternative 3: 99.5% accurate, 1.0× speedup?

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

\\
\frac{0.25 \cdot e^{\frac{r}{-s}}}{\left(s \cdot \left(2 \cdot \pi\right)\right) \cdot r} + \frac{0.75 \cdot e^{\frac{r}{3 \cdot \left(-s\right)}}}{r \cdot \left(s \cdot \left(\pi \cdot 6\right)\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. Final simplification99.6%

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

Reproduce

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