HairBSDF, sample_f, cosTheta

Percentage Accurate: 99.5% → 99.5%
Time: 13.2s
Alternatives: 10
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 10 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.7× speedup?

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

\\
1 + v \cdot \log \left(u + e^{\mathsf{log1p}\left(-u\right) - \frac{2}{v}}\right)
\end{array}
Derivation
    &prev;&pcontext;&pcontext2;&ctx;
  1. Add Preprocessing

Alternative 2: 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
    &prev;&pcontext;&pcontext2;&ctx;
  1. Add Preprocessing

Alternative 3: 94.2% accurate, 2.0× speedup?

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

\\
1 - v \cdot \log \left(\frac{1}{u}\right)
\end{array}
Derivation
    &prev;&pcontext;&pcontext2;&ctx;
  1. Add Preprocessing

Alternative 4: 94.3% accurate, 2.0× speedup?

\[\begin{array}{l} \\ 1 + v \cdot \log u \end{array} \]
(FPCore (u v) :precision binary32 (+ 1.0 (* v (log u))))
float code(float u, float v) {
	return 1.0f + (v * logf(u));
}
real(4) function code(u, v)
    real(4), intent (in) :: u
    real(4), intent (in) :: v
    code = 1.0e0 + (v * log(u))
end function
function code(u, v)
	return Float32(Float32(1.0) + Float32(v * log(u)))
end
function tmp = code(u, v)
	tmp = single(1.0) + (v * log(u));
end
\begin{array}{l}

\\
1 + v \cdot \log u
\end{array}
Derivation
    &prev;&pcontext;&pcontext2;&ctx;
  1. Add Preprocessing

Alternative 5: 90.4% accurate, 14.1× speedup?

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

\\
\begin{array}{l}
\mathbf{if}\;v \leq 0.10000000149011612:\\
\;\;\;\;1\\

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


\end{array}
\end{array}
Derivation
    &prev;&pcontext;&pcontext2;&ctx;
  1. Add Preprocessing

Alternative 6: 90.4% accurate, 19.1× speedup?

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

\\
\begin{array}{l}
\mathbf{if}\;v \leq 0.10000000149011612:\\
\;\;\;\;1\\

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


\end{array}
\end{array}
Derivation
    &prev;&pcontext;&pcontext2;&ctx;
  1. Add Preprocessing

Alternative 7: 89.8% accurate, 23.3× speedup?

\[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;v \leq 0.10000000149011612:\\ \;\;\;\;1\\ \mathbf{else}:\\ \;\;\;\;1 + \left(1 - u\right) \cdot -2\\ \end{array} \end{array} \]
(FPCore (u v)
 :precision binary32
 (if (<= v 0.10000000149011612) 1.0 (+ 1.0 (* (- 1.0 u) -2.0))))
float code(float u, float v) {
	float tmp;
	if (v <= 0.10000000149011612f) {
		tmp = 1.0f;
	} else {
		tmp = 1.0f + ((1.0f - u) * -2.0f);
	}
	return tmp;
}
real(4) function code(u, v)
    real(4), intent (in) :: u
    real(4), intent (in) :: v
    real(4) :: tmp
    if (v <= 0.10000000149011612e0) then
        tmp = 1.0e0
    else
        tmp = 1.0e0 + ((1.0e0 - u) * (-2.0e0))
    end if
    code = tmp
end function
function code(u, v)
	tmp = Float32(0.0)
	if (v <= Float32(0.10000000149011612))
		tmp = Float32(1.0);
	else
		tmp = Float32(Float32(1.0) + Float32(Float32(Float32(1.0) - u) * Float32(-2.0)));
	end
	return tmp
end
function tmp_2 = code(u, v)
	tmp = single(0.0);
	if (v <= single(0.10000000149011612))
		tmp = single(1.0);
	else
		tmp = single(1.0) + ((single(1.0) - u) * single(-2.0));
	end
	tmp_2 = tmp;
end
\begin{array}{l}

\\
\begin{array}{l}
\mathbf{if}\;v \leq 0.10000000149011612:\\
\;\;\;\;1\\

\mathbf{else}:\\
\;\;\;\;1 + \left(1 - u\right) \cdot -2\\


\end{array}
\end{array}
Derivation
    &prev;&pcontext;&pcontext2;&ctx;
  1. Add Preprocessing

Alternative 8: 89.8% accurate, 29.9× speedup?

\[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;v \leq 0.10000000149011612:\\ \;\;\;\;1\\ \mathbf{else}:\\ \;\;\;\;-1 + u \cdot 2\\ \end{array} \end{array} \]
(FPCore (u v)
 :precision binary32
 (if (<= v 0.10000000149011612) 1.0 (+ -1.0 (* u 2.0))))
float code(float u, float v) {
	float tmp;
	if (v <= 0.10000000149011612f) {
		tmp = 1.0f;
	} else {
		tmp = -1.0f + (u * 2.0f);
	}
	return tmp;
}
real(4) function code(u, v)
    real(4), intent (in) :: u
    real(4), intent (in) :: v
    real(4) :: tmp
    if (v <= 0.10000000149011612e0) then
        tmp = 1.0e0
    else
        tmp = (-1.0e0) + (u * 2.0e0)
    end if
    code = tmp
end function
function code(u, v)
	tmp = Float32(0.0)
	if (v <= Float32(0.10000000149011612))
		tmp = Float32(1.0);
	else
		tmp = Float32(Float32(-1.0) + Float32(u * Float32(2.0)));
	end
	return tmp
end
function tmp_2 = code(u, v)
	tmp = single(0.0);
	if (v <= single(0.10000000149011612))
		tmp = single(1.0);
	else
		tmp = single(-1.0) + (u * single(2.0));
	end
	tmp_2 = tmp;
end
\begin{array}{l}

\\
\begin{array}{l}
\mathbf{if}\;v \leq 0.10000000149011612:\\
\;\;\;\;1\\

\mathbf{else}:\\
\;\;\;\;-1 + u \cdot 2\\


\end{array}
\end{array}
Derivation
    &prev;&pcontext;&pcontext2;&ctx;
  1. Add Preprocessing

Alternative 9: 5.9% accurate, 213.0× speedup?

\[\begin{array}{l} \\ -1 \end{array} \]
(FPCore (u v) :precision binary32 -1.0)
float code(float u, float v) {
	return -1.0f;
}
real(4) function code(u, v)
    real(4), intent (in) :: u
    real(4), intent (in) :: v
    code = -1.0e0
end function
function code(u, v)
	return Float32(-1.0)
end
function tmp = code(u, v)
	tmp = single(-1.0);
end
\begin{array}{l}

\\
-1
\end{array}
Derivation
    &prev;&pcontext;&pcontext2;&ctx;
  1. Add Preprocessing

Alternative 10: 86.6% accurate, 213.0× speedup?

\[\begin{array}{l} \\ 1 \end{array} \]
(FPCore (u v) :precision binary32 1.0)
float code(float u, float v) {
	return 1.0f;
}
real(4) function code(u, v)
    real(4), intent (in) :: u
    real(4), intent (in) :: v
    code = 1.0e0
end function
function code(u, v)
	return Float32(1.0)
end
function tmp = code(u, v)
	tmp = single(1.0);
end
\begin{array}{l}

\\
1
\end{array}
Derivation
    &prev;&pcontext;&pcontext2;&ctx;
  1. Add Preprocessing

Reproduce

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