HairBSDF, gamma for a refracted ray

Percentage Accurate: 91.8% → 98.0%
Time: 5.7s
Alternatives: 4
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
  (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 4 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.8% 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.0% accurate, 0.8× speedup?

\[\sin^{-1} \left(\frac{h}{eta \cdot \mathsf{fma}\left(\frac{-0.5 \cdot sinTheta\_O}{\sqrt{1 - sinTheta\_O \cdot sinTheta\_O} \cdot eta}, \frac{sinTheta\_O}{eta}, 1\right)}\right) \]
(FPCore (sinTheta_O h eta)
  :precision binary32
  (asin
 (/
  h
  (*
   eta
   (fma
    (/
     (* -0.5 sinTheta_O)
     (* (sqrt (- 1.0 (* sinTheta_O sinTheta_O))) eta))
    (/ sinTheta_O eta)
    1.0)))))
float code(float sinTheta_O, float h, float eta) {
	return asinf((h / (eta * fmaf(((-0.5f * sinTheta_O) / (sqrtf((1.0f - (sinTheta_O * sinTheta_O))) * eta)), (sinTheta_O / eta), 1.0f))));
}
function code(sinTheta_O, h, eta)
	return asin(Float32(h / Float32(eta * fma(Float32(Float32(Float32(-0.5) * sinTheta_O) / Float32(sqrt(Float32(Float32(1.0) - Float32(sinTheta_O * sinTheta_O))) * eta)), Float32(sinTheta_O / eta), Float32(1.0)))))
end
\sin^{-1} \left(\frac{h}{eta \cdot \mathsf{fma}\left(\frac{-0.5 \cdot sinTheta\_O}{\sqrt{1 - sinTheta\_O \cdot sinTheta\_O} \cdot eta}, \frac{sinTheta\_O}{eta}, 1\right)}\right)
Derivation
  1. Initial program 91.8%

    \[\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}{\color{blue}{eta \cdot \left(1 + \frac{-1}{2} \cdot \frac{{sinTheta\_O}^{2}}{{eta}^{2} \cdot \sqrt{1 - {sinTheta\_O}^{2}}}\right)}}\right) \]
  3. Step-by-step derivation
    1. lower-*.f32N/A

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

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

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

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

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

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

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

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

      \[\leadsto \sin^{-1} \left(\frac{h}{eta \cdot \left(1 + \frac{-1}{2} \cdot \frac{{sinTheta\_O}^{2}}{{eta}^{2} \cdot \sqrt{1 - {sinTheta\_O}^{2}}}\right)}\right) \]
    10. lower-pow.f3291.6%

      \[\leadsto \sin^{-1} \left(\frac{h}{eta \cdot \left(1 + -0.5 \cdot \frac{{sinTheta\_O}^{2}}{{eta}^{2} \cdot \sqrt{1 - {sinTheta\_O}^{2}}}\right)}\right) \]
  4. Applied rewrites91.6%

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

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

      \[\leadsto \sin^{-1} \left(\frac{h}{eta \cdot \left(\frac{-1}{2} \cdot \frac{{sinTheta\_O}^{2}}{{eta}^{2} \cdot \sqrt{1 - {sinTheta\_O}^{2}}} + \color{blue}{1}\right)}\right) \]
  6. Applied rewrites98.0%

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

Alternative 2: 98.0% accurate, 1.1× speedup?

\[\sin^{-1} \left(\frac{h}{eta \cdot \mathsf{fma}\left(\frac{-0.5 \cdot sinTheta\_O}{eta}, \frac{sinTheta\_O}{eta}, 1\right)}\right) \]
(FPCore (sinTheta_O h eta)
  :precision binary32
  (asin
 (/
  h
  (* eta (fma (/ (* -0.5 sinTheta_O) eta) (/ sinTheta_O eta) 1.0)))))
float code(float sinTheta_O, float h, float eta) {
	return asinf((h / (eta * fmaf(((-0.5f * sinTheta_O) / eta), (sinTheta_O / eta), 1.0f))));
}
function code(sinTheta_O, h, eta)
	return asin(Float32(h / Float32(eta * fma(Float32(Float32(Float32(-0.5) * sinTheta_O) / eta), Float32(sinTheta_O / eta), Float32(1.0)))))
end
\sin^{-1} \left(\frac{h}{eta \cdot \mathsf{fma}\left(\frac{-0.5 \cdot sinTheta\_O}{eta}, \frac{sinTheta\_O}{eta}, 1\right)}\right)
Derivation
  1. Initial program 91.8%

    \[\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}{\color{blue}{eta \cdot \left(1 + \frac{-1}{2} \cdot \frac{{sinTheta\_O}^{2}}{{eta}^{2} \cdot \sqrt{1 - {sinTheta\_O}^{2}}}\right)}}\right) \]
  3. Step-by-step derivation
    1. lower-*.f32N/A

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

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

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

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

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

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

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

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

      \[\leadsto \sin^{-1} \left(\frac{h}{eta \cdot \left(1 + \frac{-1}{2} \cdot \frac{{sinTheta\_O}^{2}}{{eta}^{2} \cdot \sqrt{1 - {sinTheta\_O}^{2}}}\right)}\right) \]
    10. lower-pow.f3291.6%

      \[\leadsto \sin^{-1} \left(\frac{h}{eta \cdot \left(1 + -0.5 \cdot \frac{{sinTheta\_O}^{2}}{{eta}^{2} \cdot \sqrt{1 - {sinTheta\_O}^{2}}}\right)}\right) \]
  4. Applied rewrites91.6%

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

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

      \[\leadsto \sin^{-1} \left(\frac{h}{eta \cdot \left(\frac{-1}{2} \cdot \frac{{sinTheta\_O}^{2}}{{eta}^{2} \cdot \sqrt{1 - {sinTheta\_O}^{2}}} + \color{blue}{1}\right)}\right) \]
  6. Applied rewrites98.0%

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

    \[\leadsto \sin^{-1} \left(\frac{h}{eta \cdot \mathsf{fma}\left(\frac{-0.5 \cdot sinTheta\_O}{eta}, \frac{sinTheta\_O}{eta}, 1\right)}\right) \]
  8. Step-by-step derivation
    1. Applied rewrites98.0%

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

    Alternative 3: 98.0% accurate, 1.5× speedup?

    \[\sin^{-1} \left(\frac{h}{eta + -0.5 \cdot \left(\frac{sinTheta\_O}{eta} \cdot sinTheta\_O\right)}\right) \]
    (FPCore (sinTheta_O h eta)
      :precision binary32
      (asin (/ h (+ eta (* -0.5 (* (/ sinTheta_O eta) sinTheta_O))))))
    float code(float sinTheta_O, float h, float eta) {
    	return asinf((h / (eta + (-0.5f * ((sinTheta_O / eta) * 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 / (eta + ((-0.5e0) * ((sintheta_o / eta) * sintheta_o)))))
    end function
    
    function code(sinTheta_O, h, eta)
    	return asin(Float32(h / Float32(eta + Float32(Float32(-0.5) * Float32(Float32(sinTheta_O / eta) * sinTheta_O)))))
    end
    
    function tmp = code(sinTheta_O, h, eta)
    	tmp = asin((h / (eta + (single(-0.5) * ((sinTheta_O / eta) * sinTheta_O)))));
    end
    
    \sin^{-1} \left(\frac{h}{eta + -0.5 \cdot \left(\frac{sinTheta\_O}{eta} \cdot sinTheta\_O\right)}\right)
    
    Derivation
    1. Initial program 91.8%

      \[\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}{\color{blue}{eta \cdot \left(1 + \frac{-1}{2} \cdot \frac{{sinTheta\_O}^{2}}{{eta}^{2} \cdot \sqrt{1 - {sinTheta\_O}^{2}}}\right)}}\right) \]
    3. Step-by-step derivation
      1. lower-*.f32N/A

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

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

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

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

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

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

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

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

        \[\leadsto \sin^{-1} \left(\frac{h}{eta \cdot \left(1 + \frac{-1}{2} \cdot \frac{{sinTheta\_O}^{2}}{{eta}^{2} \cdot \sqrt{1 - {sinTheta\_O}^{2}}}\right)}\right) \]
      10. lower-pow.f3291.6%

        \[\leadsto \sin^{-1} \left(\frac{h}{eta \cdot \left(1 + -0.5 \cdot \frac{{sinTheta\_O}^{2}}{{eta}^{2} \cdot \sqrt{1 - {sinTheta\_O}^{2}}}\right)}\right) \]
    4. Applied rewrites91.6%

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

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

        \[\leadsto \sin^{-1} \left(\frac{h}{eta \cdot \left(\frac{-1}{2} \cdot \frac{{sinTheta\_O}^{2}}{{eta}^{2} \cdot \sqrt{1 - {sinTheta\_O}^{2}}} + \color{blue}{1}\right)}\right) \]
    6. Applied rewrites98.0%

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

      \[\leadsto \sin^{-1} \left(\frac{h}{eta \cdot \mathsf{fma}\left(\frac{-0.5 \cdot sinTheta\_O}{eta}, \frac{sinTheta\_O}{eta}, 1\right)}\right) \]
    8. Step-by-step derivation
      1. Applied rewrites98.0%

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

          \[\leadsto \sin^{-1} \left(\frac{h}{eta \cdot \color{blue}{\mathsf{fma}\left(\frac{\frac{-1}{2} \cdot sinTheta\_O}{eta}, \frac{sinTheta\_O}{eta}, 1\right)}}\right) \]
        2. *-commutativeN/A

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

          \[\leadsto \sin^{-1} \left(\frac{h}{\left(\frac{\frac{-1}{2} \cdot sinTheta\_O}{eta} \cdot \frac{sinTheta\_O}{eta} + 1\right) \cdot eta}\right) \]
        4. +-commutativeN/A

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

          \[\leadsto \sin^{-1} \left(\frac{h}{\left(1 + \frac{\frac{-1}{2} \cdot sinTheta\_O}{eta} \cdot \frac{sinTheta\_O}{eta}\right) \cdot eta}\right) \]
        6. associate-*r/N/A

          \[\leadsto \sin^{-1} \left(\frac{h}{\left(1 + \frac{\frac{\frac{-1}{2} \cdot sinTheta\_O}{eta} \cdot sinTheta\_O}{eta}\right) \cdot eta}\right) \]
        7. sum-to-mult-revN/A

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

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

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

          \[\leadsto \sin^{-1} \left(\frac{h}{eta + \frac{\frac{-1}{2} \cdot sinTheta\_O}{eta} \cdot sinTheta\_O}\right) \]
        11. associate-/l*N/A

          \[\leadsto \sin^{-1} \left(\frac{h}{eta + \left(\frac{-1}{2} \cdot \frac{sinTheta\_O}{eta}\right) \cdot sinTheta\_O}\right) \]
        12. associate-*l*N/A

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

          \[\leadsto \sin^{-1} \left(\frac{h}{eta + \frac{-1}{2} \cdot \color{blue}{\left(\frac{sinTheta\_O}{eta} \cdot sinTheta\_O\right)}}\right) \]
      3. Applied rewrites98.0%

        \[\leadsto \sin^{-1} \left(\frac{h}{eta + \color{blue}{-0.5 \cdot \left(\frac{sinTheta\_O}{eta} \cdot sinTheta\_O\right)}}\right) \]
      4. Add Preprocessing

      Alternative 4: 95.6% accurate, 3.5× 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 91.8%

        \[\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.6%

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

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

      Reproduce

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