HairBSDF, gamma for a refracted ray

Percentage Accurate: 92.2% → 97.4%
Time: 9.3s
Alternatives: 2
Speedup: 3.1×

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)\]
\[\begin{array}{l} \\ \sin^{-1} \left(\frac{h}{\sqrt{eta \cdot eta - \frac{sinTheta_O \cdot sinTheta_O}{\sqrt{1 - sinTheta_O \cdot sinTheta_O}}}}\right) \end{array} \]
(FPCore (sinTheta_O h eta)
 :precision binary32
 (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)
    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
\begin{array}{l}

\\
\sin^{-1} \left(\frac{h}{\sqrt{eta \cdot eta - \frac{sinTheta_O \cdot sinTheta_O}{\sqrt{1 - sinTheta_O \cdot sinTheta_O}}}}\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 2 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: 92.2% accurate, 1.0× speedup?

\[\begin{array}{l} \\ \sin^{-1} \left(\frac{h}{\sqrt{eta \cdot eta - \frac{sinTheta_O \cdot sinTheta_O}{\sqrt{1 - sinTheta_O \cdot sinTheta_O}}}}\right) \end{array} \]
(FPCore (sinTheta_O h eta)
 :precision binary32
 (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)
    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
\begin{array}{l}

\\
\sin^{-1} \left(\frac{h}{\sqrt{eta \cdot eta - \frac{sinTheta_O \cdot sinTheta_O}{\sqrt{1 - sinTheta_O \cdot sinTheta_O}}}}\right)
\end{array}

Alternative 1: 97.4% accurate, 2.8× speedup?

\[\begin{array}{l} \\ \sin^{-1} \left(\frac{h}{eta + -0.5 \cdot \frac{sinTheta_O \cdot sinTheta_O}{eta}}\right) \end{array} \]
(FPCore (sinTheta_O h eta)
 :precision binary32
 (asin (/ h (+ eta (* -0.5 (/ (* sinTheta_O sinTheta_O) eta))))))
float code(float sinTheta_O, float h, float eta) {
	return asinf((h / (eta + (-0.5f * ((sinTheta_O * sinTheta_O) / eta)))));
}
real(4) function code(sintheta_o, h, eta)
    real(4), intent (in) :: sintheta_o
    real(4), intent (in) :: h
    real(4), intent (in) :: eta
    code = asin((h / (eta + ((-0.5e0) * ((sintheta_o * sintheta_o) / eta)))))
end function
function code(sinTheta_O, h, eta)
	return asin(Float32(h / Float32(eta + Float32(Float32(-0.5) * Float32(Float32(sinTheta_O * sinTheta_O) / eta)))))
end
function tmp = code(sinTheta_O, h, eta)
	tmp = asin((h / (eta + (single(-0.5) * ((sinTheta_O * sinTheta_O) / eta)))));
end
\begin{array}{l}

\\
\sin^{-1} \left(\frac{h}{eta + -0.5 \cdot \frac{sinTheta_O \cdot sinTheta_O}{eta}}\right)
\end{array}
Derivation
  1. Initial program 94.4%

    \[\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 98.0%

    \[\leadsto \sin^{-1} \left(\frac{h}{\color{blue}{eta + -0.5 \cdot \frac{{sinTheta_O}^{2}}{eta}}}\right) \]
  3. Step-by-step derivation
    1. unpow298.0%

      \[\leadsto \sin^{-1} \left(\frac{h}{eta + -0.5 \cdot \frac{\color{blue}{sinTheta_O \cdot sinTheta_O}}{eta}}\right) \]
  4. Simplified98.0%

    \[\leadsto \sin^{-1} \left(\frac{h}{\color{blue}{eta + -0.5 \cdot \frac{sinTheta_O \cdot sinTheta_O}{eta}}}\right) \]
  5. Final simplification98.0%

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

Alternative 2: 95.3% accurate, 3.1× speedup?

\[\begin{array}{l} \\ \sin^{-1} \left(\frac{h}{eta}\right) \end{array} \]
(FPCore (sinTheta_O h eta) :precision binary32 (asin (/ h eta)))
float code(float sinTheta_O, float h, float eta) {
	return asinf((h / eta));
}
real(4) function code(sintheta_o, h, eta)
    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
\begin{array}{l}

\\
\sin^{-1} \left(\frac{h}{eta}\right)
\end{array}
Derivation
  1. Initial program 94.4%

    \[\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 95.9%

    \[\leadsto \sin^{-1} \left(\frac{h}{\color{blue}{eta}}\right) \]
  3. Final simplification95.9%

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

Reproduce

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