HairBSDF, sample_f, cosTheta

Percentage Accurate: 99.5% → 99.5%
Time: 11.8s
Alternatives: 5
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
  (+ 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 5 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?

\[1 + v \cdot \log \left(u + \left(1 - u\right) \cdot e^{\frac{-2}{v}}\right) \]
(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)
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: 96.3% accurate, 1.1× speedup?

\[\log \left(\mathsf{fma}\left(1, e^{\frac{-2}{v}}, u\right)\right) \cdot v - -1 \]
(FPCore (u v)
  :precision binary32
  (- (* (log (fma 1.0 (exp (/ -2.0 v)) u)) v) -1.0))
float code(float u, float v) {
	return (logf(fmaf(1.0f, expf((-2.0f / v)), u)) * v) - -1.0f;
}
function code(u, v)
	return Float32(Float32(log(fma(Float32(1.0), exp(Float32(Float32(-2.0) / v)), u)) * v) - Float32(-1.0))
end
\log \left(\mathsf{fma}\left(1, e^{\frac{-2}{v}}, u\right)\right) \cdot v - -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 + v \cdot \log \left(u + \color{blue}{1} \cdot e^{\frac{-2}{v}}\right) \]
  3. Step-by-step derivation
    1. Applied rewrites96.3%

      \[\leadsto 1 + v \cdot \log \left(u + \color{blue}{1} \cdot e^{\frac{-2}{v}}\right) \]
    2. Step-by-step derivation
      1. lift-+.f32N/A

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

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

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

        \[\leadsto v \cdot \log \left(u + 1 \cdot e^{\frac{-2}{v}}\right) - \color{blue}{-1} \]
      5. lower--.f3296.3%

        \[\leadsto \color{blue}{v \cdot \log \left(u + 1 \cdot e^{\frac{-2}{v}}\right) - -1} \]
    3. Applied rewrites96.3%

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

    Alternative 2: 96.3% accurate, 1.1× speedup?

    \[\mathsf{fma}\left(\log \left(\mathsf{fma}\left(1, e^{\frac{-2}{v}}, u\right)\right), v, 1\right) \]
    (FPCore (u v)
      :precision binary32
      (fma (log (fma 1.0 (exp (/ -2.0 v)) u)) v 1.0))
    float code(float u, float v) {
    	return fmaf(logf(fmaf(1.0f, expf((-2.0f / v)), u)), v, 1.0f);
    }
    
    function code(u, v)
    	return fma(log(fma(Float32(1.0), exp(Float32(Float32(-2.0) / v)), u)), v, Float32(1.0))
    end
    
    \mathsf{fma}\left(\log \left(\mathsf{fma}\left(1, e^{\frac{-2}{v}}, u\right)\right), v, 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 + \color{blue}{1} \cdot e^{\frac{-2}{v}}\right) \]
    3. Step-by-step derivation
      1. Applied rewrites96.3%

        \[\leadsto 1 + v \cdot \log \left(u + \color{blue}{1} \cdot e^{\frac{-2}{v}}\right) \]
      2. Step-by-step derivation
        1. lift-+.f32N/A

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

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

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

          \[\leadsto \color{blue}{\log \left(u + 1 \cdot e^{\frac{-2}{v}}\right) \cdot v} + 1 \]
        5. lower-fma.f3296.3%

          \[\leadsto \color{blue}{\mathsf{fma}\left(\log \left(u + 1 \cdot e^{\frac{-2}{v}}\right), v, 1\right)} \]
        6. lift-+.f32N/A

          \[\leadsto \mathsf{fma}\left(\log \color{blue}{\left(u + 1 \cdot e^{\frac{-2}{v}}\right)}, v, 1\right) \]
        7. +-commutativeN/A

          \[\leadsto \mathsf{fma}\left(\log \color{blue}{\left(1 \cdot e^{\frac{-2}{v}} + u\right)}, v, 1\right) \]
        8. add-flipN/A

          \[\leadsto \mathsf{fma}\left(\log \color{blue}{\left(1 \cdot e^{\frac{-2}{v}} - \left(\mathsf{neg}\left(u\right)\right)\right)}, v, 1\right) \]
        9. sub-flipN/A

          \[\leadsto \mathsf{fma}\left(\log \color{blue}{\left(1 \cdot e^{\frac{-2}{v}} + \left(\mathsf{neg}\left(\left(\mathsf{neg}\left(u\right)\right)\right)\right)\right)}, v, 1\right) \]
        10. lift-*.f32N/A

          \[\leadsto \mathsf{fma}\left(\log \left(\color{blue}{1 \cdot e^{\frac{-2}{v}}} + \left(\mathsf{neg}\left(\left(\mathsf{neg}\left(u\right)\right)\right)\right)\right), v, 1\right) \]
        11. remove-double-negN/A

          \[\leadsto \mathsf{fma}\left(\log \left(1 \cdot e^{\frac{-2}{v}} + \color{blue}{u}\right), v, 1\right) \]
        12. lower-fma.f3296.3%

          \[\leadsto \mathsf{fma}\left(\log \color{blue}{\left(\mathsf{fma}\left(1, e^{\frac{-2}{v}}, u\right)\right)}, v, 1\right) \]
      3. Applied rewrites96.3%

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

      Alternative 3: 86.8% accurate, 9.9× speedup?

      \[10 - 9 \]
      (FPCore (u v)
        :precision binary32
        (- 10.0 9.0))
      float code(float u, float v) {
      	return 10.0f - 9.0f;
      }
      
      real(4) function code(u, v)
      use fmin_fmax_functions
          real(4), intent (in) :: u
          real(4), intent (in) :: v
          code = 10.0e0 - 9.0e0
      end function
      
      function code(u, v)
      	return Float32(Float32(10.0) - Float32(9.0))
      end
      
      function tmp = code(u, v)
      	tmp = single(10.0) - single(9.0);
      end
      
      10 - 9
      
      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 + \color{blue}{-2 \cdot \left(1 - u\right)} \]
      3. Step-by-step derivation
        1. lower-*.f32N/A

          \[\leadsto 1 + -2 \cdot \color{blue}{\left(1 - u\right)} \]
        2. lower--.f327.8%

          \[\leadsto 1 + -2 \cdot \left(1 - \color{blue}{u}\right) \]
      4. Applied rewrites7.8%

        \[\leadsto 1 + \color{blue}{-2 \cdot \left(1 - u\right)} \]
      5. Step-by-step derivation
        1. +-rgt-identityN/A

          \[\leadsto \color{blue}{\left(1 + -2 \cdot \left(1 - u\right)\right) + 0} \]
        2. lift-+.f32N/A

          \[\leadsto \color{blue}{\left(1 + -2 \cdot \left(1 - u\right)\right)} + 0 \]
        3. +-commutativeN/A

          \[\leadsto \color{blue}{\left(-2 \cdot \left(1 - u\right) + 1\right)} + 0 \]
        4. +-commutativeN/A

          \[\leadsto \color{blue}{\left(1 + -2 \cdot \left(1 - u\right)\right)} + 0 \]
        5. lift-+.f32N/A

          \[\leadsto \color{blue}{\left(1 + -2 \cdot \left(1 - u\right)\right)} + 0 \]
        6. +-inversesN/A

          \[\leadsto \left(1 + -2 \cdot \left(1 - u\right)\right) + \color{blue}{\left(\left(\left(3 + 3\right) + 3\right) - \left(\left(3 + 3\right) + 3\right)\right)} \]
        7. associate-+r-N/A

          \[\leadsto \color{blue}{\left(\left(1 + -2 \cdot \left(1 - u\right)\right) + \left(\left(3 + 3\right) + 3\right)\right) - \left(\left(3 + 3\right) + 3\right)} \]
        8. lower--.f32N/A

          \[\leadsto \color{blue}{\left(\left(1 + -2 \cdot \left(1 - u\right)\right) + \left(\left(3 + 3\right) + 3\right)\right) - \left(\left(3 + 3\right) + 3\right)} \]
      6. Applied rewrites7.8%

        \[\leadsto \color{blue}{\left(\left(-2 \cdot \left(1 - u\right) - -1\right) + 9\right) - 9} \]
      7. Taylor expanded in v around 0

        \[\leadsto \color{blue}{10} - 9 \]
      8. Step-by-step derivation
        1. Applied rewrites86.8%

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

        Alternative 4: 5.8% accurate, 35.6× speedup?

        \[-1 \]
        (FPCore (u v)
          :precision binary32
          -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 \color{blue}{-1} \]
        3. Step-by-step derivation
          1. Applied rewrites5.8%

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

          Reproduce

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