HairBSDF, gamma for a refracted ray

Percentage Accurate: 91.9% → 98.6%
Time: 4.9s
Alternatives: 3
Speedup: 3.5×

Specification

?
\[\left(\left(-1 \leq sinTheta\_O \land sinTheta\_O \leq 1\right) \land \left(-1 \leq h \land h \leq 1\right)\right) \land \left(0 \leq eta \land eta \leq 10\right)\]
\[\sin^{-1} \left(\frac{h}{\sqrt{eta \cdot eta - \frac{sinTheta\_O \cdot sinTheta\_O}{\sqrt{1 - sinTheta\_O \cdot sinTheta\_O}}}}\right) \]
(FPCore (sinTheta_O h eta)
  :precision binary32
  :pre (and (and (and (<= -1.0 sinTheta_O) (<= sinTheta_O 1.0))
          (and (<= -1.0 h) (<= h 1.0)))
     (and (<= 0.0 eta) (<= eta 10.0)))
  (asin
 (/
  h
  (sqrt
   (-
    (* eta eta)
    (/
     (* sinTheta_O sinTheta_O)
     (sqrt (- 1.0 (* sinTheta_O sinTheta_O)))))))))
float code(float sinTheta_O, float h, float eta) {
	return asinf((h / sqrtf(((eta * eta) - ((sinTheta_O * sinTheta_O) / sqrtf((1.0f - (sinTheta_O * sinTheta_O))))))));
}
real(4) function code(sintheta_o, h, eta)
use fmin_fmax_functions
    real(4), intent (in) :: sintheta_o
    real(4), intent (in) :: h
    real(4), intent (in) :: eta
    code = asin((h / sqrt(((eta * eta) - ((sintheta_o * sintheta_o) / sqrt((1.0e0 - (sintheta_o * sintheta_o))))))))
end function
function code(sinTheta_O, h, eta)
	return asin(Float32(h / sqrt(Float32(Float32(eta * eta) - Float32(Float32(sinTheta_O * sinTheta_O) / sqrt(Float32(Float32(1.0) - Float32(sinTheta_O * sinTheta_O))))))))
end
function tmp = code(sinTheta_O, h, eta)
	tmp = asin((h / sqrt(((eta * eta) - ((sinTheta_O * sinTheta_O) / sqrt((single(1.0) - (sinTheta_O * sinTheta_O))))))));
end
\sin^{-1} \left(\frac{h}{\sqrt{eta \cdot eta - \frac{sinTheta\_O \cdot sinTheta\_O}{\sqrt{1 - sinTheta\_O \cdot sinTheta\_O}}}}\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 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: 91.9% accurate, 1.0× speedup?

\[\left(\left(-1 \leq sinTheta\_O \land sinTheta\_O \leq 1\right) \land \left(-1 \leq h \land h \leq 1\right)\right) \land \left(0 \leq eta \land eta \leq 10\right)\]
\[\sin^{-1} \left(\frac{h}{\sqrt{eta \cdot eta - \frac{sinTheta\_O \cdot sinTheta\_O}{\sqrt{1 - sinTheta\_O \cdot sinTheta\_O}}}}\right) \]
(FPCore (sinTheta_O h eta)
  :precision binary32
  :pre (and (and (and (<= -1.0 sinTheta_O) (<= sinTheta_O 1.0))
          (and (<= -1.0 h) (<= h 1.0)))
     (and (<= 0.0 eta) (<= eta 10.0)))
  (asin
 (/
  h
  (sqrt
   (-
    (* eta eta)
    (/
     (* sinTheta_O sinTheta_O)
     (sqrt (- 1.0 (* sinTheta_O sinTheta_O)))))))))
float code(float sinTheta_O, float h, float eta) {
	return asinf((h / sqrtf(((eta * eta) - ((sinTheta_O * sinTheta_O) / sqrtf((1.0f - (sinTheta_O * sinTheta_O))))))));
}
real(4) function code(sintheta_o, h, eta)
use fmin_fmax_functions
    real(4), intent (in) :: sintheta_o
    real(4), intent (in) :: h
    real(4), intent (in) :: eta
    code = asin((h / sqrt(((eta * eta) - ((sintheta_o * sintheta_o) / sqrt((1.0e0 - (sintheta_o * sintheta_o))))))))
end function
function code(sinTheta_O, h, eta)
	return asin(Float32(h / sqrt(Float32(Float32(eta * eta) - Float32(Float32(sinTheta_O * sinTheta_O) / sqrt(Float32(Float32(1.0) - Float32(sinTheta_O * sinTheta_O))))))))
end
function tmp = code(sinTheta_O, h, eta)
	tmp = asin((h / sqrt(((eta * eta) - ((sinTheta_O * sinTheta_O) / sqrt((single(1.0) - (sinTheta_O * sinTheta_O))))))));
end
\sin^{-1} \left(\frac{h}{\sqrt{eta \cdot eta - \frac{sinTheta\_O \cdot sinTheta\_O}{\sqrt{1 - sinTheta\_O \cdot sinTheta\_O}}}}\right)

Alternative 1: 98.6% accurate, 1.2× speedup?

\[\left(\left(-1 \leq sinTheta\_O \land sinTheta\_O \leq 1\right) \land \left(-1 \leq h \land h \leq 1\right)\right) \land \left(0 \leq eta \land eta \leq 10\right)\]
\[\begin{array}{l} \mathbf{if}\;\left|sinTheta\_O\right| \leq 5.499347374382884 \cdot 10^{-24}:\\ \;\;\;\;\sin^{-1} \left(\frac{h}{eta}\right)\\ \mathbf{else}:\\ \;\;\;\;\sin^{-1} \left(\frac{h}{\sqrt{\mathsf{fma}\left(eta, eta, \left(-\left|sinTheta\_O\right|\right) \cdot \left|sinTheta\_O\right|\right)}}\right)\\ \end{array} \]
(FPCore (sinTheta_O h eta)
  :precision binary32
  :pre (and (and (and (<= -1.0 sinTheta_O) (<= sinTheta_O 1.0))
          (and (<= -1.0 h) (<= h 1.0)))
     (and (<= 0.0 eta) (<= eta 10.0)))
  (if (<= (fabs sinTheta_O) 5.499347374382884e-24)
  (asin (/ h eta))
  (asin
   (/
    h
    (sqrt
     (fma eta eta (* (- (fabs sinTheta_O)) (fabs sinTheta_O))))))))
float code(float sinTheta_O, float h, float eta) {
	float tmp;
	if (fabsf(sinTheta_O) <= 5.499347374382884e-24f) {
		tmp = asinf((h / eta));
	} else {
		tmp = asinf((h / sqrtf(fmaf(eta, eta, (-fabsf(sinTheta_O) * fabsf(sinTheta_O))))));
	}
	return tmp;
}
function code(sinTheta_O, h, eta)
	tmp = Float32(0.0)
	if (abs(sinTheta_O) <= Float32(5.499347374382884e-24))
		tmp = asin(Float32(h / eta));
	else
		tmp = asin(Float32(h / sqrt(fma(eta, eta, Float32(Float32(-abs(sinTheta_O)) * abs(sinTheta_O))))));
	end
	return tmp
end
\begin{array}{l}
\mathbf{if}\;\left|sinTheta\_O\right| \leq 5.499347374382884 \cdot 10^{-24}:\\
\;\;\;\;\sin^{-1} \left(\frac{h}{eta}\right)\\

\mathbf{else}:\\
\;\;\;\;\sin^{-1} \left(\frac{h}{\sqrt{\mathsf{fma}\left(eta, eta, \left(-\left|sinTheta\_O\right|\right) \cdot \left|sinTheta\_O\right|\right)}}\right)\\


\end{array}
Derivation
  1. Split input into 2 regimes
  2. if sinTheta_O < 5.49934737e-24

    1. Initial program 91.9%

      \[\sin^{-1} \left(\frac{h}{\sqrt{eta \cdot eta - \frac{sinTheta\_O \cdot sinTheta\_O}{\sqrt{1 - sinTheta\_O \cdot sinTheta\_O}}}}\right) \]
    2. Taylor expanded in eta around inf

      \[\leadsto \sin^{-1} \left(\frac{h}{eta}\right) \]
    3. Step-by-step derivation
      1. Applied rewrites95.1%

        \[\leadsto \sin^{-1} \left(\frac{h}{eta}\right) \]

      if 5.49934737e-24 < sinTheta_O

      1. Initial program 91.9%

        \[\sin^{-1} \left(\frac{h}{\sqrt{eta \cdot eta - \frac{sinTheta\_O \cdot sinTheta\_O}{\sqrt{1 - sinTheta\_O \cdot sinTheta\_O}}}}\right) \]
      2. Taylor expanded in sinTheta_O around 0

        \[\leadsto \sin^{-1} \left(\frac{h}{\sqrt{eta \cdot eta - \frac{sinTheta\_O \cdot sinTheta\_O}{\sqrt{1}}}}\right) \]
      3. Step-by-step derivation
        1. Applied rewrites91.6%

          \[\leadsto \sin^{-1} \left(\frac{h}{\sqrt{eta \cdot eta - \frac{sinTheta\_O \cdot sinTheta\_O}{\sqrt{1}}}}\right) \]
        2. Step-by-step derivation
          1. Applied rewrites91.6%

            \[\leadsto \sin^{-1} \left(\frac{h}{\sqrt{\mathsf{fma}\left(sinTheta\_O, -\frac{sinTheta\_O}{\sqrt{1}}, eta \cdot eta\right)}}\right) \]
          2. Taylor expanded in sinTheta_O around 0

            \[\leadsto \sin^{-1} \left(\frac{h}{\sqrt{\mathsf{fma}\left(sinTheta\_O, -1 \cdot sinTheta\_O, eta \cdot eta\right)}}\right) \]
          3. Step-by-step derivation
            1. Applied rewrites91.6%

              \[\leadsto \sin^{-1} \left(\frac{h}{\sqrt{\mathsf{fma}\left(sinTheta\_O, -1 \cdot sinTheta\_O, eta \cdot eta\right)}}\right) \]
            2. Step-by-step derivation
              1. Applied rewrites91.6%

                \[\leadsto \sin^{-1} \left(\frac{h}{\sqrt{\mathsf{fma}\left(eta, eta, \left(-sinTheta\_O\right) \cdot sinTheta\_O\right)}}\right) \]
            3. Recombined 2 regimes into one program.
            4. Add Preprocessing

            Alternative 2: 97.7% accurate, 1.5× speedup?

            \[\left(\left(-1 \leq sinTheta\_O \land sinTheta\_O \leq 1\right) \land \left(-1 \leq h \land h \leq 1\right)\right) \land \left(0 \leq eta \land eta \leq 10\right)\]
            \[\sin^{-1} \left(\frac{h}{\mathsf{fma}\left(sinTheta\_O, \frac{sinTheta\_O}{eta} \cdot -0.5, eta\right)}\right) \]
            (FPCore (sinTheta_O h eta)
              :precision binary32
              :pre (and (and (and (<= -1.0 sinTheta_O) (<= sinTheta_O 1.0))
                      (and (<= -1.0 h) (<= h 1.0)))
                 (and (<= 0.0 eta) (<= eta 10.0)))
              (asin (/ h (fma sinTheta_O (* (/ sinTheta_O eta) -0.5) eta))))
            float code(float sinTheta_O, float h, float eta) {
            	return asinf((h / fmaf(sinTheta_O, ((sinTheta_O / eta) * -0.5f), eta)));
            }
            
            function code(sinTheta_O, h, eta)
            	return asin(Float32(h / fma(sinTheta_O, Float32(Float32(sinTheta_O / eta) * Float32(-0.5)), eta)))
            end
            
            \sin^{-1} \left(\frac{h}{\mathsf{fma}\left(sinTheta\_O, \frac{sinTheta\_O}{eta} \cdot -0.5, eta\right)}\right)
            
            Derivation
            1. Initial program 91.9%

              \[\sin^{-1} \left(\frac{h}{\sqrt{eta \cdot eta - \frac{sinTheta\_O \cdot sinTheta\_O}{\sqrt{1 - sinTheta\_O \cdot sinTheta\_O}}}}\right) \]
            2. Taylor expanded in sinTheta_O around 0

              \[\leadsto \sin^{-1} \left(\frac{h}{\sqrt{{eta}^{2}} + \frac{-1}{2} \cdot \frac{{sinTheta\_O}^{2}}{\sqrt{{eta}^{2}}}}\right) \]
            3. Step-by-step derivation
              1. Applied rewrites90.3%

                \[\leadsto \sin^{-1} \left(\frac{h}{\sqrt{{eta}^{2}} + -0.5 \cdot \frac{{sinTheta\_O}^{2}}{\sqrt{{eta}^{2}}}}\right) \]
              2. Step-by-step derivation
                1. Applied rewrites96.0%

                  \[\leadsto \sin^{-1} \left(\frac{h}{\mathsf{fma}\left(\sqrt{\left|eta\right|}, \sqrt{\left|eta\right|}, \frac{sinTheta\_O \cdot sinTheta\_O}{\left|eta\right|} \cdot -0.5\right)}\right) \]
                2. Step-by-step derivation
                  1. Applied rewrites97.7%

                    \[\leadsto \sin^{-1} \left(\frac{h}{\mathsf{fma}\left(sinTheta\_O, \frac{sinTheta\_O}{eta} \cdot -0.5, eta\right)}\right) \]
                  2. Add Preprocessing

                  Alternative 3: 95.1% accurate, 3.5× speedup?

                  \[\left(\left(-1 \leq sinTheta\_O \land sinTheta\_O \leq 1\right) \land \left(-1 \leq h \land h \leq 1\right)\right) \land \left(0 \leq eta \land eta \leq 10\right)\]
                  \[\sin^{-1} \left(\frac{h}{eta}\right) \]
                  (FPCore (sinTheta_O h eta)
                    :precision binary32
                    :pre (and (and (and (<= -1.0 sinTheta_O) (<= sinTheta_O 1.0))
                            (and (<= -1.0 h) (<= h 1.0)))
                       (and (<= 0.0 eta) (<= eta 10.0)))
                    (asin (/ h eta)))
                  float code(float sinTheta_O, float h, float eta) {
                  	return asinf((h / eta));
                  }
                  
                  real(4) function code(sintheta_o, h, eta)
                  use fmin_fmax_functions
                      real(4), intent (in) :: sintheta_o
                      real(4), intent (in) :: h
                      real(4), intent (in) :: eta
                      code = asin((h / eta))
                  end function
                  
                  function code(sinTheta_O, h, eta)
                  	return asin(Float32(h / eta))
                  end
                  
                  function tmp = code(sinTheta_O, h, eta)
                  	tmp = asin((h / eta));
                  end
                  
                  \sin^{-1} \left(\frac{h}{eta}\right)
                  
                  Derivation
                  1. Initial program 91.9%

                    \[\sin^{-1} \left(\frac{h}{\sqrt{eta \cdot eta - \frac{sinTheta\_O \cdot sinTheta\_O}{\sqrt{1 - sinTheta\_O \cdot sinTheta\_O}}}}\right) \]
                  2. Taylor expanded in eta around inf

                    \[\leadsto \sin^{-1} \left(\frac{h}{eta}\right) \]
                  3. Step-by-step derivation
                    1. Applied rewrites95.1%

                      \[\leadsto \sin^{-1} \left(\frac{h}{eta}\right) \]
                    2. Add Preprocessing

                    Reproduce

                    ?
                    herbie shell --seed 2026084 
                    (FPCore (sinTheta_O h eta)
                      :name "HairBSDF, gamma for a refracted ray"
                      :precision binary32
                      :pre (and (and (and (<= -1.0 sinTheta_O) (<= sinTheta_O 1.0)) (and (<= -1.0 h) (<= h 1.0))) (and (<= 0.0 eta) (<= eta 10.0)))
                      (asin (/ h (sqrt (- (* eta eta) (/ (* sinTheta_O sinTheta_O) (sqrt (- 1.0 (* sinTheta_O sinTheta_O)))))))))