HairBSDF, sample_f, cosTheta

Percentage Accurate: 99.5% → 99.5%
Time: 7.5s
Alternatives: 4
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 4 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: 94.7% accurate, 2.1× 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
  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. Applied rewrites96.3%

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

    \[\leadsto 1 + v \cdot \color{blue}{\left(-3 \cdot \log \left(\frac{1}{u}\right) - -2 \cdot \log \left(\frac{1}{u}\right)\right)} \]
  5. Step-by-step derivation
    1. distribute-rgt-out--N/A

      \[\leadsto 1 + v \cdot \color{blue}{\left(\log \left(\frac{1}{u}\right) \cdot \left(-3 - -2\right)\right)} \]
    2. metadata-evalN/A

      \[\leadsto 1 + v \cdot \left(\log \left(\frac{1}{u}\right) \cdot \color{blue}{-1}\right) \]
    3. *-commutativeN/A

      \[\leadsto 1 + v \cdot \color{blue}{\left(-1 \cdot \log \left(\frac{1}{u}\right)\right)} \]
    4. mul-1-negN/A

      \[\leadsto 1 + v \cdot \color{blue}{\left(\mathsf{neg}\left(\log \left(\frac{1}{u}\right)\right)\right)} \]
    5. log-recN/A

      \[\leadsto 1 + v \cdot \left(\mathsf{neg}\left(\color{blue}{\left(\mathsf{neg}\left(\log u\right)\right)}\right)\right) \]
    6. remove-double-negN/A

      \[\leadsto 1 + v \cdot \color{blue}{\log u} \]
    7. lower-log.f3295.9

      \[\leadsto 1 + v \cdot \color{blue}{\log u} \]
  6. Applied rewrites95.9%

    \[\leadsto 1 + v \cdot \color{blue}{\log u} \]
  7. Add Preprocessing

Alternative 3: 86.9% accurate, 231.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
  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. Taylor expanded in v around 0

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

      \[\leadsto \color{blue}{1} \]
    2. Add Preprocessing

    Alternative 4: 5.6% accurate, 231.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
    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. Taylor expanded in u around 0

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

        \[\leadsto \color{blue}{-1} \]
      2. Add Preprocessing

      Reproduce

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