HairBSDF, gamma for a refracted ray

Percentage Accurate: 92.3% → 98.6%
Time: 9.5s
Alternatives: 3
Speedup: 3.6×

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
 (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))))))));
}
module fmin_fmax_functions
    implicit none
    private
    public fmax
    public fmin

    interface fmax
        module procedure fmax88
        module procedure fmax44
        module procedure fmax84
        module procedure fmax48
    end interface
    interface fmin
        module procedure fmin88
        module procedure fmin44
        module procedure fmin84
        module procedure fmin48
    end interface
contains
    real(8) function fmax88(x, y) result (res)
        real(8), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(x, max(x, y), y /= y), x /= x)
    end function
    real(4) function fmax44(x, y) result (res)
        real(4), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(y, merge(x, max(x, y), y /= y), x /= x)
    end function
    real(8) function fmax84(x, y) result(res)
        real(8), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(dble(y), merge(x, max(x, dble(y)), y /= y), x /= x)
    end function
    real(8) function fmax48(x, y) result(res)
        real(4), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(dble(x), max(dble(x), y), y /= y), x /= x)
    end function
    real(8) function fmin88(x, y) result (res)
        real(8), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(x, min(x, y), y /= y), x /= x)
    end function
    real(4) function fmin44(x, y) result (res)
        real(4), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(y, merge(x, min(x, y), y /= y), x /= x)
    end function
    real(8) function fmin84(x, y) result(res)
        real(8), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(dble(y), merge(x, min(x, dble(y)), y /= y), x /= x)
    end function
    real(8) function fmin48(x, y) result(res)
        real(4), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(dble(x), min(dble(x), y), y /= y), x /= x)
    end function
end module

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: 92.3% accurate, 1.0× speedup?

\[\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
 (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))))))));
}
module fmin_fmax_functions
    implicit none
    private
    public fmax
    public fmin

    interface fmax
        module procedure fmax88
        module procedure fmax44
        module procedure fmax84
        module procedure fmax48
    end interface
    interface fmin
        module procedure fmin88
        module procedure fmin44
        module procedure fmin84
        module procedure fmin48
    end interface
contains
    real(8) function fmax88(x, y) result (res)
        real(8), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(x, max(x, y), y /= y), x /= x)
    end function
    real(4) function fmax44(x, y) result (res)
        real(4), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(y, merge(x, max(x, y), y /= y), x /= x)
    end function
    real(8) function fmax84(x, y) result(res)
        real(8), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(dble(y), merge(x, max(x, dble(y)), y /= y), x /= x)
    end function
    real(8) function fmax48(x, y) result(res)
        real(4), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(dble(x), max(dble(x), y), y /= y), x /= x)
    end function
    real(8) function fmin88(x, y) result (res)
        real(8), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(x, min(x, y), y /= y), x /= x)
    end function
    real(4) function fmin44(x, y) result (res)
        real(4), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(y, merge(x, min(x, y), y /= y), x /= x)
    end function
    real(8) function fmin84(x, y) result(res)
        real(8), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(dble(y), merge(x, min(x, dble(y)), y /= y), x /= x)
    end function
    real(8) function fmin48(x, y) result(res)
        real(4), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(dble(x), min(dble(x), y), y /= y), x /= x)
    end function
end module

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, 0.9× speedup?

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

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


\end{array}
Derivation
  1. Split input into 2 regimes
  2. if eta < 4.99999984e-20

    1. Initial program 92.3%

      \[\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} \color{blue}{\left(\frac{h}{eta}\right)} \]
    3. Step-by-step derivation
      1. lower-/.f3295.3%

        \[\leadsto \sin^{-1} \left(\frac{h}{\color{blue}{eta}}\right) \]
    4. Applied rewrites95.3%

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

    if 4.99999984e-20 < eta

    1. Initial program 92.3%

      \[\sin^{-1} \left(\frac{h}{\sqrt{eta \cdot eta - \frac{sinTheta\_O \cdot sinTheta\_O}{\sqrt{1 - sinTheta\_O \cdot sinTheta\_O}}}}\right) \]
    2. Step-by-step derivation
      1. lift--.f32N/A

        \[\leadsto \sin^{-1} \left(\frac{h}{\sqrt{\color{blue}{eta \cdot eta - \frac{sinTheta\_O \cdot sinTheta\_O}{\sqrt{1 - sinTheta\_O \cdot sinTheta\_O}}}}}\right) \]
      2. lift-/.f32N/A

        \[\leadsto \sin^{-1} \left(\frac{h}{\sqrt{eta \cdot eta - \color{blue}{\frac{sinTheta\_O \cdot sinTheta\_O}{\sqrt{1 - sinTheta\_O \cdot sinTheta\_O}}}}}\right) \]
      3. lift-*.f32N/A

        \[\leadsto \sin^{-1} \left(\frac{h}{\sqrt{eta \cdot eta - \frac{\color{blue}{sinTheta\_O \cdot sinTheta\_O}}{\sqrt{1 - sinTheta\_O \cdot sinTheta\_O}}}}\right) \]
      4. associate-/l*N/A

        \[\leadsto \sin^{-1} \left(\frac{h}{\sqrt{eta \cdot eta - \color{blue}{sinTheta\_O \cdot \frac{sinTheta\_O}{\sqrt{1 - sinTheta\_O \cdot sinTheta\_O}}}}}\right) \]
      5. fp-cancel-sub-sign-invN/A

        \[\leadsto \sin^{-1} \left(\frac{h}{\sqrt{\color{blue}{eta \cdot eta + \left(\mathsf{neg}\left(sinTheta\_O\right)\right) \cdot \frac{sinTheta\_O}{\sqrt{1 - sinTheta\_O \cdot sinTheta\_O}}}}}\right) \]
      6. distribute-lft-neg-inN/A

        \[\leadsto \sin^{-1} \left(\frac{h}{\sqrt{eta \cdot eta + \color{blue}{\left(\mathsf{neg}\left(sinTheta\_O \cdot \frac{sinTheta\_O}{\sqrt{1 - sinTheta\_O \cdot sinTheta\_O}}\right)\right)}}}\right) \]
      7. distribute-rgt-neg-inN/A

        \[\leadsto \sin^{-1} \left(\frac{h}{\sqrt{eta \cdot eta + \color{blue}{sinTheta\_O \cdot \left(\mathsf{neg}\left(\frac{sinTheta\_O}{\sqrt{1 - sinTheta\_O \cdot sinTheta\_O}}\right)\right)}}}\right) \]
      8. fp-cancel-sign-sub-invN/A

        \[\leadsto \sin^{-1} \left(\frac{h}{\sqrt{\color{blue}{eta \cdot eta - \left(\mathsf{neg}\left(sinTheta\_O\right)\right) \cdot \left(\mathsf{neg}\left(\frac{sinTheta\_O}{\sqrt{1 - sinTheta\_O \cdot sinTheta\_O}}\right)\right)}}}\right) \]
      9. fp-cancel-sub-sign-invN/A

        \[\leadsto \sin^{-1} \left(\frac{h}{\sqrt{\color{blue}{eta \cdot eta + \left(\mathsf{neg}\left(\left(\mathsf{neg}\left(sinTheta\_O\right)\right)\right)\right) \cdot \left(\mathsf{neg}\left(\frac{sinTheta\_O}{\sqrt{1 - sinTheta\_O \cdot sinTheta\_O}}\right)\right)}}}\right) \]
      10. lift-*.f32N/A

        \[\leadsto \sin^{-1} \left(\frac{h}{\sqrt{\color{blue}{eta \cdot eta} + \left(\mathsf{neg}\left(\left(\mathsf{neg}\left(sinTheta\_O\right)\right)\right)\right) \cdot \left(\mathsf{neg}\left(\frac{sinTheta\_O}{\sqrt{1 - sinTheta\_O \cdot sinTheta\_O}}\right)\right)}}\right) \]
      11. remove-double-negN/A

        \[\leadsto \sin^{-1} \left(\frac{h}{\sqrt{eta \cdot eta + \color{blue}{sinTheta\_O} \cdot \left(\mathsf{neg}\left(\frac{sinTheta\_O}{\sqrt{1 - sinTheta\_O \cdot sinTheta\_O}}\right)\right)}}\right) \]
      12. distribute-rgt-neg-inN/A

        \[\leadsto \sin^{-1} \left(\frac{h}{\sqrt{eta \cdot eta + \color{blue}{\left(\mathsf{neg}\left(sinTheta\_O \cdot \frac{sinTheta\_O}{\sqrt{1 - sinTheta\_O \cdot sinTheta\_O}}\right)\right)}}}\right) \]
      13. associate-/l*N/A

        \[\leadsto \sin^{-1} \left(\frac{h}{\sqrt{eta \cdot eta + \left(\mathsf{neg}\left(\color{blue}{\frac{sinTheta\_O \cdot sinTheta\_O}{\sqrt{1 - sinTheta\_O \cdot sinTheta\_O}}}\right)\right)}}\right) \]
      14. lift-*.f32N/A

        \[\leadsto \sin^{-1} \left(\frac{h}{\sqrt{eta \cdot eta + \left(\mathsf{neg}\left(\frac{\color{blue}{sinTheta\_O \cdot sinTheta\_O}}{\sqrt{1 - sinTheta\_O \cdot sinTheta\_O}}\right)\right)}}\right) \]
      15. lift-/.f32N/A

        \[\leadsto \sin^{-1} \left(\frac{h}{\sqrt{eta \cdot eta + \left(\mathsf{neg}\left(\color{blue}{\frac{sinTheta\_O \cdot sinTheta\_O}{\sqrt{1 - sinTheta\_O \cdot sinTheta\_O}}}\right)\right)}}\right) \]
      16. lower-fma.f32N/A

        \[\leadsto \sin^{-1} \left(\frac{h}{\sqrt{\color{blue}{\mathsf{fma}\left(eta, eta, \mathsf{neg}\left(\frac{sinTheta\_O \cdot sinTheta\_O}{\sqrt{1 - sinTheta\_O \cdot sinTheta\_O}}\right)\right)}}}\right) \]
    3. Applied rewrites92.3%

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

Alternative 2: 98.6% accurate, 0.9× speedup?

\[\begin{array}{l} \mathbf{if}\;eta \leq 4.9999998413276127 \cdot 10^{-20}:\\ \;\;\;\;\sin^{-1} \left(\frac{h}{eta}\right)\\ \mathbf{else}:\\ \;\;\;\;\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
 (if (<= eta 4.9999998413276127e-20)
   (asin (/ h eta))
   (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) {
	float tmp;
	if (eta <= 4.9999998413276127e-20f) {
		tmp = asinf((h / eta));
	} else {
		tmp = asinf((h / sqrtf(((eta * eta) - ((sinTheta_O * sinTheta_O) / sqrtf((1.0f - (sinTheta_O * sinTheta_O))))))));
	}
	return tmp;
}
module fmin_fmax_functions
    implicit none
    private
    public fmax
    public fmin

    interface fmax
        module procedure fmax88
        module procedure fmax44
        module procedure fmax84
        module procedure fmax48
    end interface
    interface fmin
        module procedure fmin88
        module procedure fmin44
        module procedure fmin84
        module procedure fmin48
    end interface
contains
    real(8) function fmax88(x, y) result (res)
        real(8), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(x, max(x, y), y /= y), x /= x)
    end function
    real(4) function fmax44(x, y) result (res)
        real(4), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(y, merge(x, max(x, y), y /= y), x /= x)
    end function
    real(8) function fmax84(x, y) result(res)
        real(8), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(dble(y), merge(x, max(x, dble(y)), y /= y), x /= x)
    end function
    real(8) function fmax48(x, y) result(res)
        real(4), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(dble(x), max(dble(x), y), y /= y), x /= x)
    end function
    real(8) function fmin88(x, y) result (res)
        real(8), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(x, min(x, y), y /= y), x /= x)
    end function
    real(4) function fmin44(x, y) result (res)
        real(4), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(y, merge(x, min(x, y), y /= y), x /= x)
    end function
    real(8) function fmin84(x, y) result(res)
        real(8), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(dble(y), merge(x, min(x, dble(y)), y /= y), x /= x)
    end function
    real(8) function fmin48(x, y) result(res)
        real(4), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(dble(x), min(dble(x), y), y /= y), x /= x)
    end function
end module

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
    real(4) :: tmp
    if (eta <= 4.9999998413276127e-20) then
        tmp = asin((h / eta))
    else
        tmp = asin((h / sqrt(((eta * eta) - ((sintheta_o * sintheta_o) / sqrt((1.0e0 - (sintheta_o * sintheta_o))))))))
    end if
    code = tmp
end function
function code(sinTheta_O, h, eta)
	tmp = Float32(0.0)
	if (eta <= Float32(4.9999998413276127e-20))
		tmp = asin(Float32(h / eta));
	else
		tmp = 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
	return tmp
end
function tmp_2 = code(sinTheta_O, h, eta)
	tmp = single(0.0);
	if (eta <= single(4.9999998413276127e-20))
		tmp = asin((h / eta));
	else
		tmp = asin((h / sqrt(((eta * eta) - ((sinTheta_O * sinTheta_O) / sqrt((single(1.0) - (sinTheta_O * sinTheta_O))))))));
	end
	tmp_2 = tmp;
end
\begin{array}{l}
\mathbf{if}\;eta \leq 4.9999998413276127 \cdot 10^{-20}:\\
\;\;\;\;\sin^{-1} \left(\frac{h}{eta}\right)\\

\mathbf{else}:\\
\;\;\;\;\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}
Derivation
  1. Split input into 2 regimes
  2. if eta < 4.99999984e-20

    1. Initial program 92.3%

      \[\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} \color{blue}{\left(\frac{h}{eta}\right)} \]
    3. Step-by-step derivation
      1. lower-/.f3295.3%

        \[\leadsto \sin^{-1} \left(\frac{h}{\color{blue}{eta}}\right) \]
    4. Applied rewrites95.3%

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

    if 4.99999984e-20 < eta

    1. Initial program 92.3%

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

Alternative 3: 95.3% accurate, 3.6× speedup?

\[\sin^{-1} \left(\frac{h}{eta}\right) \]
(FPCore (sinTheta_O h eta) :precision binary32 (asin (/ h eta)))
float code(float sinTheta_O, float h, float eta) {
	return asinf((h / eta));
}
module fmin_fmax_functions
    implicit none
    private
    public fmax
    public fmin

    interface fmax
        module procedure fmax88
        module procedure fmax44
        module procedure fmax84
        module procedure fmax48
    end interface
    interface fmin
        module procedure fmin88
        module procedure fmin44
        module procedure fmin84
        module procedure fmin48
    end interface
contains
    real(8) function fmax88(x, y) result (res)
        real(8), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(x, max(x, y), y /= y), x /= x)
    end function
    real(4) function fmax44(x, y) result (res)
        real(4), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(y, merge(x, max(x, y), y /= y), x /= x)
    end function
    real(8) function fmax84(x, y) result(res)
        real(8), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(dble(y), merge(x, max(x, dble(y)), y /= y), x /= x)
    end function
    real(8) function fmax48(x, y) result(res)
        real(4), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(dble(x), max(dble(x), y), y /= y), x /= x)
    end function
    real(8) function fmin88(x, y) result (res)
        real(8), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(x, min(x, y), y /= y), x /= x)
    end function
    real(4) function fmin44(x, y) result (res)
        real(4), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(y, merge(x, min(x, y), y /= y), x /= x)
    end function
    real(8) function fmin84(x, y) result(res)
        real(8), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(dble(y), merge(x, min(x, dble(y)), y /= y), x /= x)
    end function
    real(8) function fmin48(x, y) result(res)
        real(4), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(dble(x), min(dble(x), y), y /= y), x /= x)
    end function
end module

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 92.3%

    \[\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} \color{blue}{\left(\frac{h}{eta}\right)} \]
  3. Step-by-step derivation
    1. lower-/.f3295.3%

      \[\leadsto \sin^{-1} \left(\frac{h}{\color{blue}{eta}}\right) \]
  4. Applied rewrites95.3%

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

Reproduce

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