HairBSDF, sample_f, cosTheta

Percentage Accurate: 99.5% → 99.5%
Time: 3.4s
Alternatives: 11
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)\]
\[1 + v \cdot \log \left(u + \left(1 - u\right) \cdot e^{\frac{-2}{v}}\right) \]
(FPCore (u v)
  :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))))))))
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)
use fmin_fmax_functions
    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
1 + v \cdot \log \left(u + \left(1 - u\right) \cdot e^{\frac{-2}{v}}\right)

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 11 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?

\[\left(10^{-5} \leq u \land u \leq 1\right) \land \left(0 \leq v \land v \leq 109.746574\right)\]
\[1 + v \cdot \log \left(u + \left(1 - u\right) \cdot e^{\frac{-2}{v}}\right) \]
(FPCore (u v)
  :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))))))))
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)
use fmin_fmax_functions
    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
1 + v \cdot \log \left(u + \left(1 - u\right) \cdot e^{\frac{-2}{v}}\right)

Alternative 1: 99.5% accurate, 1.0× speedup?

\[\left(10^{-5} \leq u \land u \leq 1\right) \land \left(0 \leq v \land v \leq 109.746574\right)\]
\[\mathsf{fma}\left(v, \log \left(u + e^{\frac{-2}{v}} \cdot \left(1 - u\right)\right), 1\right) \]
(FPCore (u v)
  :precision binary32
  :pre (and (and (<= 1e-5 u) (<= u 1.0))
     (and (<= 0.0 v) (<= v 109.746574)))
  (fma v (log (+ u (* (exp (/ -2.0 v)) (- 1.0 u)))) 1.0))
float code(float u, float v) {
	return fmaf(v, logf((u + (expf((-2.0f / v)) * (1.0f - u)))), 1.0f);
}
function code(u, v)
	return fma(v, log(Float32(u + Float32(exp(Float32(Float32(-2.0) / v)) * Float32(Float32(1.0) - u)))), Float32(1.0))
end
\mathsf{fma}\left(v, \log \left(u + e^{\frac{-2}{v}} \cdot \left(1 - u\right)\right), 1\right)
Derivation
  1. Initial program 99.5%

    \[1 + v \cdot \log \left(u + \left(1 - u\right) \cdot e^{\frac{-2}{v}}\right) \]
  2. Step-by-step derivation
    1. Applied rewrites99.5%

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

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

      Alternative 2: 99.5% accurate, 1.0× speedup?

      \[\left(10^{-5} \leq u \land u \leq 1\right) \land \left(0 \leq v \land v \leq 109.746574\right)\]
      \[\mathsf{fma}\left(v, \log \left(\mathsf{fma}\left(e^{\frac{-2}{v}}, 1 - u, u\right)\right), 1\right) \]
      (FPCore (u v)
        :precision binary32
        :pre (and (and (<= 1e-5 u) (<= u 1.0))
           (and (<= 0.0 v) (<= v 109.746574)))
        (fma v (log (fma (exp (/ -2.0 v)) (- 1.0 u) u)) 1.0))
      float code(float u, float v) {
      	return fmaf(v, logf(fmaf(expf((-2.0f / v)), (1.0f - u), u)), 1.0f);
      }
      
      function code(u, v)
      	return fma(v, log(fma(exp(Float32(Float32(-2.0) / v)), Float32(Float32(1.0) - u), u)), Float32(1.0))
      end
      
      \mathsf{fma}\left(v, \log \left(\mathsf{fma}\left(e^{\frac{-2}{v}}, 1 - u, u\right)\right), 1\right)
      
      Derivation
      1. Initial program 99.5%

        \[1 + v \cdot \log \left(u + \left(1 - u\right) \cdot e^{\frac{-2}{v}}\right) \]
      2. Step-by-step derivation
        1. Applied rewrites99.5%

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

        Alternative 3: 96.4% accurate, 1.2× speedup?

        \[\left(10^{-5} \leq u \land u \leq 1\right) \land \left(0 \leq v \land v \leq 109.746574\right)\]
        \[1 + v \cdot \log \left(u + e^{\frac{-2}{v}}\right) \]
        (FPCore (u v)
          :precision binary32
          :pre (and (and (<= 1e-5 u) (<= u 1.0))
             (and (<= 0.0 v) (<= v 109.746574)))
          (+ 1.0 (* v (log (+ u (exp (/ -2.0 v)))))))
        float code(float u, float v) {
        	return 1.0f + (v * logf((u + expf((-2.0f / v)))));
        }
        
        real(4) function code(u, v)
        use fmin_fmax_functions
            real(4), intent (in) :: u
            real(4), intent (in) :: v
            code = 1.0e0 + (v * log((u + exp(((-2.0e0) / v)))))
        end function
        
        function code(u, v)
        	return Float32(Float32(1.0) + Float32(v * log(Float32(u + exp(Float32(Float32(-2.0) / v))))))
        end
        
        function tmp = code(u, v)
        	tmp = single(1.0) + (v * log((u + exp((single(-2.0) / v)))));
        end
        
        1 + v \cdot \log \left(u + e^{\frac{-2}{v}}\right)
        
        Derivation
        1. Initial program 99.5%

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

          \[\leadsto 1 + v \cdot \log \left(u + e^{\frac{-2}{v}}\right) \]
        3. Step-by-step derivation
          1. Applied rewrites96.4%

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

          Alternative 4: 96.4% accurate, 1.2× speedup?

          \[\left(10^{-5} \leq u \land u \leq 1\right) \land \left(0 \leq v \land v \leq 109.746574\right)\]
          \[\mathsf{fma}\left(v, \log \left(e^{\frac{-2}{v}} + u\right), 1\right) \]
          (FPCore (u v)
            :precision binary32
            :pre (and (and (<= 1e-5 u) (<= u 1.0))
               (and (<= 0.0 v) (<= v 109.746574)))
            (fma v (log (+ (exp (/ -2.0 v)) u)) 1.0))
          float code(float u, float v) {
          	return fmaf(v, logf((expf((-2.0f / v)) + u)), 1.0f);
          }
          
          function code(u, v)
          	return fma(v, log(Float32(exp(Float32(Float32(-2.0) / v)) + u)), Float32(1.0))
          end
          
          \mathsf{fma}\left(v, \log \left(e^{\frac{-2}{v}} + u\right), 1\right)
          
          Derivation
          1. Initial program 99.5%

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

            \[\leadsto 1 + v \cdot \log \left(u + e^{\frac{-2}{v}}\right) \]
          3. Step-by-step derivation
            1. Applied rewrites96.4%

              \[\leadsto 1 + v \cdot \log \left(u + e^{\frac{-2}{v}}\right) \]
            2. Step-by-step derivation
              1. Applied rewrites96.4%

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

              Alternative 5: 94.8% accurate, 1.3× speedup?

              \[\left(10^{-5} \leq u \land u \leq 1\right) \land \left(0 \leq v \land v \leq 109.746574\right)\]
              \[1 + \log \left(-\mathsf{expm1}\left(\frac{-2}{v}\right) \cdot u\right) \cdot v \]
              (FPCore (u v)
                :precision binary32
                :pre (and (and (<= 1e-5 u) (<= u 1.0))
                   (and (<= 0.0 v) (<= v 109.746574)))
                (+ 1.0 (* (log (- (* (expm1 (/ -2.0 v)) u))) v)))
              float code(float u, float v) {
              	return 1.0f + (logf(-(expm1f((-2.0f / v)) * u)) * v);
              }
              
              function code(u, v)
              	return Float32(Float32(1.0) + Float32(log(Float32(-Float32(expm1(Float32(Float32(-2.0) / v)) * u))) * v))
              end
              
              1 + \log \left(-\mathsf{expm1}\left(\frac{-2}{v}\right) \cdot u\right) \cdot v
              
              Derivation
              1. Initial program 99.5%

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

                \[\leadsto 1 + v \cdot \log \left(-1 \cdot \left(u \cdot \left(e^{\frac{-2}{v}} - 1\right)\right)\right) \]
              3. Step-by-step derivation
                1. Applied rewrites94.8%

                  \[\leadsto 1 + v \cdot \log \left(-1 \cdot \left(u \cdot \mathsf{expm1}\left(\frac{-2}{v}\right)\right)\right) \]
                2. Step-by-step derivation
                  1. Applied rewrites94.8%

                    \[\leadsto 1 + \log \left(-\mathsf{expm1}\left(\frac{-2}{v}\right) \cdot u\right) \cdot v \]
                  2. Add Preprocessing

                  Alternative 6: 94.8% accurate, 1.3× speedup?

                  \[\left(10^{-5} \leq u \land u \leq 1\right) \land \left(0 \leq v \land v \leq 109.746574\right)\]
                  \[\mathsf{fma}\left(v, \log \left(-\mathsf{expm1}\left(\frac{-2}{v}\right) \cdot u\right), 1\right) \]
                  (FPCore (u v)
                    :precision binary32
                    :pre (and (and (<= 1e-5 u) (<= u 1.0))
                       (and (<= 0.0 v) (<= v 109.746574)))
                    (fma v (log (- (* (expm1 (/ -2.0 v)) u))) 1.0))
                  float code(float u, float v) {
                  	return fmaf(v, logf(-(expm1f((-2.0f / v)) * u)), 1.0f);
                  }
                  
                  function code(u, v)
                  	return fma(v, log(Float32(-Float32(expm1(Float32(Float32(-2.0) / v)) * u))), Float32(1.0))
                  end
                  
                  \mathsf{fma}\left(v, \log \left(-\mathsf{expm1}\left(\frac{-2}{v}\right) \cdot u\right), 1\right)
                  
                  Derivation
                  1. Initial program 99.5%

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

                    \[\leadsto 1 + v \cdot \log \left(-1 \cdot \left(u \cdot \left(e^{\frac{-2}{v}} - 1\right)\right)\right) \]
                  3. Step-by-step derivation
                    1. Applied rewrites94.8%

                      \[\leadsto 1 + v \cdot \log \left(-1 \cdot \left(u \cdot \mathsf{expm1}\left(\frac{-2}{v}\right)\right)\right) \]
                    2. Step-by-step derivation
                      1. Applied rewrites94.8%

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

                      Alternative 7: 87.1% accurate, 2.1× speedup?

                      \[\left(10^{-5} \leq u \land u \leq 1\right) \land \left(0 \leq v \land v \leq 109.746574\right)\]
                      \[\mathsf{fma}\left(v, \log \left(\left(1 - u\right) + u\right), 1\right) \]
                      (FPCore (u v)
                        :precision binary32
                        :pre (and (and (<= 1e-5 u) (<= u 1.0))
                           (and (<= 0.0 v) (<= v 109.746574)))
                        (fma v (log (+ (- 1.0 u) u)) 1.0))
                      float code(float u, float v) {
                      	return fmaf(v, logf(((1.0f - u) + u)), 1.0f);
                      }
                      
                      function code(u, v)
                      	return fma(v, log(Float32(Float32(Float32(1.0) - u) + u)), Float32(1.0))
                      end
                      
                      \mathsf{fma}\left(v, \log \left(\left(1 - u\right) + u\right), 1\right)
                      
                      Derivation
                      1. Initial program 99.5%

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

                        \[\leadsto 1 + v \cdot \log \left(u + \left(1 - u\right)\right) \]
                      3. Step-by-step derivation
                        1. Applied rewrites87.1%

                          \[\leadsto 1 + v \cdot \log \left(u + \left(1 - u\right)\right) \]
                        2. Step-by-step derivation
                          1. Applied rewrites87.1%

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

                          Alternative 8: 87.1% accurate, 2.4× speedup?

                          \[\left(10^{-5} \leq u \land u \leq 1\right) \land \left(0 \leq v \land v \leq 109.746574\right)\]
                          \[\mathsf{fma}\left(v, \log \left(1 + u\right), 1\right) \]
                          (FPCore (u v)
                            :precision binary32
                            :pre (and (and (<= 1e-5 u) (<= u 1.0))
                               (and (<= 0.0 v) (<= v 109.746574)))
                            (fma v (log (+ 1.0 u)) 1.0))
                          float code(float u, float v) {
                          	return fmaf(v, logf((1.0f + u)), 1.0f);
                          }
                          
                          function code(u, v)
                          	return fma(v, log(Float32(Float32(1.0) + u)), Float32(1.0))
                          end
                          
                          \mathsf{fma}\left(v, \log \left(1 + u\right), 1\right)
                          
                          Derivation
                          1. Initial program 99.5%

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

                            \[\leadsto 1 + v \cdot \log \left(u + \left(1 - u\right)\right) \]
                          3. Step-by-step derivation
                            1. Applied rewrites87.1%

                              \[\leadsto 1 + v \cdot \log \left(u + \left(1 - u\right)\right) \]
                            2. Taylor expanded in u around 0

                              \[\leadsto 1 + v \cdot \log \left(u + 1\right) \]
                            3. Step-by-step derivation
                              1. Applied rewrites87.1%

                                \[\leadsto 1 + v \cdot \log \left(u + 1\right) \]
                              2. Step-by-step derivation
                                1. Applied rewrites87.1%

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

                                Alternative 9: 46.9% accurate, 5.7× speedup?

                                \[\left(10^{-5} \leq u \land u \leq 1\right) \land \left(0 \leq v \land v \leq 109.746574\right)\]
                                \[1 + \left(u + u\right) \]
                                (FPCore (u v)
                                  :precision binary32
                                  :pre (and (and (<= 1e-5 u) (<= u 1.0))
                                     (and (<= 0.0 v) (<= v 109.746574)))
                                  (+ 1.0 (+ u u)))
                                float code(float u, float v) {
                                	return 1.0f + (u + u);
                                }
                                
                                real(4) function code(u, v)
                                use fmin_fmax_functions
                                    real(4), intent (in) :: u
                                    real(4), intent (in) :: v
                                    code = 1.0e0 + (u + u)
                                end function
                                
                                function code(u, v)
                                	return Float32(Float32(1.0) + Float32(u + u))
                                end
                                
                                function tmp = code(u, v)
                                	tmp = single(1.0) + (u + u);
                                end
                                
                                1 + \left(u + u\right)
                                
                                Derivation
                                1. Initial program 99.5%

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

                                  \[\leadsto 1 + -2 \cdot \left(1 - u\right) \]
                                3. Step-by-step derivation
                                  1. Applied rewrites7.8%

                                    \[\leadsto 1 + -2 \cdot \left(1 - u\right) \]
                                  2. Taylor expanded in u around inf

                                    \[\leadsto 1 + 2 \cdot u \]
                                  3. Step-by-step derivation
                                    1. Applied rewrites46.9%

                                      \[\leadsto 1 + 2 \cdot u \]
                                    2. Step-by-step derivation
                                      1. Applied rewrites46.9%

                                        \[\leadsto 1 + \left(u + u\right) \]
                                      2. Add Preprocessing

                                      Alternative 10: 7.8% accurate, 5.9× speedup?

                                      \[\left(10^{-5} \leq u \land u \leq 1\right) \land \left(0 \leq v \land v \leq 109.746574\right)\]
                                      \[\mathsf{fma}\left(2, u, -1\right) \]
                                      (FPCore (u v)
                                        :precision binary32
                                        :pre (and (and (<= 1e-5 u) (<= u 1.0))
                                           (and (<= 0.0 v) (<= v 109.746574)))
                                        (fma 2.0 u -1.0))
                                      float code(float u, float v) {
                                      	return fmaf(2.0f, u, -1.0f);
                                      }
                                      
                                      function code(u, v)
                                      	return fma(Float32(2.0), u, Float32(-1.0))
                                      end
                                      
                                      \mathsf{fma}\left(2, u, -1\right)
                                      
                                      Derivation
                                      1. Initial program 99.5%

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

                                        \[\leadsto \frac{u \cdot \left(v \cdot \left(1 + -1 \cdot e^{\frac{-2}{v}}\right)\right)}{e^{\frac{-2}{v}}} - 1 \]
                                      3. Step-by-step derivation
                                        1. Applied rewrites10.4%

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

                                          \[\leadsto 2 \cdot u - 1 \]
                                        3. Step-by-step derivation
                                          1. Applied rewrites7.8%

                                            \[\leadsto 2 \cdot u - 1 \]
                                          2. Step-by-step derivation
                                            1. Applied rewrites7.8%

                                              \[\leadsto \mathsf{fma}\left(2, u, -1\right) \]
                                            2. Add Preprocessing

                                            Alternative 11: 5.8% accurate, 35.6× speedup?

                                            \[\left(10^{-5} \leq u \land u \leq 1\right) \land \left(0 \leq v \land v \leq 109.746574\right)\]
                                            \[-1 \]
                                            (FPCore (u v)
                                              :precision binary32
                                              :pre (and (and (<= 1e-5 u) (<= u 1.0))
                                                 (and (<= 0.0 v) (<= v 109.746574)))
                                              -1.0)
                                            float code(float u, float v) {
                                            	return -1.0f;
                                            }
                                            
                                            real(4) function code(u, v)
                                            use fmin_fmax_functions
                                                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
                                            
                                            -1
                                            
                                            Derivation
                                            1. Initial program 99.5%

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

                                              \[\leadsto -1 \]
                                            3. Step-by-step derivation
                                              1. Applied rewrites5.8%

                                                \[\leadsto -1 \]
                                              2. Add Preprocessing

                                              Reproduce

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