
(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
\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:
Herbie found 5 alternatives:
| Alternative | Accuracy | Speedup |
|---|
(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
\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
(let* ((t_0
(*
(* (ratio-of-squares sinTheta_O eta) (/ 1.0 (sin (acos sinTheta_O))))
-0.5)))
(asin
(/
h
(*
(/
(- (* t_0 t_0) 1.0)
(-
(*
(*
(ratio-of-squares sinTheta_O eta)
(sqrt (ratio-square-sum 1.0 (* (- sinTheta_O) sinTheta_O))))
-0.5)
1.0))
eta)))))\begin{array}{l}
\\
\begin{array}{l}
t_0 := \left(\mathsf{ratio\_of\_squares}\left(sinTheta\_O, eta\right) \cdot \frac{1}{\sin \cos^{-1} sinTheta\_O}\right) \cdot -0.5\\
\sin^{-1} \left(\frac{h}{\frac{t\_0 \cdot t\_0 - 1}{\left(\mathsf{ratio\_of\_squares}\left(sinTheta\_O, eta\right) \cdot \sqrt{\mathsf{ratio\_square\_sum}\left(1, \left(\left(-sinTheta\_O\right) \cdot sinTheta\_O\right)\right)}\right) \cdot -0.5 - 1} \cdot eta}\right)
\end{array}
\end{array}
Initial program 92.0%
Taylor expanded in eta around inf
*-commutativeN/A
lower-*.f32N/A
Applied rewrites98.2%
lift-+.f32N/A
flip-+N/A
lower-/.f32N/A
Applied rewrites98.2%
lift-/.f32N/A
lift-acos.f32N/A
lift-sin.f32N/A
sin-acos-revN/A
metadata-evalN/A
pow2N/A
sqrt-divN/A
lower-sqrt.f32N/A
metadata-evalN/A
pow2N/A
fp-cancel-sub-sign-invN/A
lower-ratio-square-sum.f32N/A
lower-*.f32N/A
lower-neg.f3298.2
Applied rewrites98.2%
(FPCore (sinTheta_O h eta)
:precision binary32
(if (<=
(/ (* sinTheta_O sinTheta_O) (sqrt (- 1.0 (* sinTheta_O sinTheta_O))))
0.0)
(asin (/ h eta))
(asin (/ h (sqrt (- (* eta eta) (* sinTheta_O sinTheta_O)))))))
float code(float sinTheta_O, float h, float eta) {
float tmp;
if (((sinTheta_O * sinTheta_O) / sqrtf((1.0f - (sinTheta_O * sinTheta_O)))) <= 0.0f) {
tmp = asinf((h / eta));
} else {
tmp = asinf((h / sqrtf(((eta * eta) - (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 (((sintheta_o * sintheta_o) / sqrt((1.0e0 - (sintheta_o * sintheta_o)))) <= 0.0e0) then
tmp = asin((h / eta))
else
tmp = asin((h / sqrt(((eta * eta) - (sintheta_o * sintheta_o)))))
end if
code = tmp
end function
function code(sinTheta_O, h, eta) tmp = Float32(0.0) if (Float32(Float32(sinTheta_O * sinTheta_O) / sqrt(Float32(Float32(1.0) - Float32(sinTheta_O * sinTheta_O)))) <= Float32(0.0)) tmp = asin(Float32(h / eta)); else tmp = asin(Float32(h / sqrt(Float32(Float32(eta * eta) - Float32(sinTheta_O * sinTheta_O))))); end return tmp end
function tmp_2 = code(sinTheta_O, h, eta) tmp = single(0.0); if (((sinTheta_O * sinTheta_O) / sqrt((single(1.0) - (sinTheta_O * sinTheta_O)))) <= single(0.0)) tmp = asin((h / eta)); else tmp = asin((h / sqrt(((eta * eta) - (sinTheta_O * sinTheta_O))))); end tmp_2 = tmp; end
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;\frac{sinTheta\_O \cdot sinTheta\_O}{\sqrt{1 - sinTheta\_O \cdot sinTheta\_O}} \leq 0:\\
\;\;\;\;\sin^{-1} \left(\frac{h}{eta}\right)\\
\mathbf{else}:\\
\;\;\;\;\sin^{-1} \left(\frac{h}{\sqrt{eta \cdot eta - sinTheta\_O \cdot sinTheta\_O}}\right)\\
\end{array}
\end{array}
if (/.f32 (*.f32 sinTheta_O sinTheta_O) (sqrt.f32 (-.f32 #s(literal 1 binary32) (*.f32 sinTheta_O sinTheta_O)))) < 0.0Initial program 87.1%
Taylor expanded in sinTheta_O around 0
Applied rewrites98.2%
if 0.0 < (/.f32 (*.f32 sinTheta_O sinTheta_O) (sqrt.f32 (-.f32 #s(literal 1 binary32) (*.f32 sinTheta_O sinTheta_O)))) Initial program 99.0%
Taylor expanded in sinTheta_O around 0
pow2N/A
lift-*.f3298.2
Applied rewrites98.2%
(FPCore (sinTheta_O h eta)
:precision binary32
(asin
(/
h
(*
(+
(*
(* -0.5 (ratio-of-squares sinTheta_O eta))
(sqrt (ratio-square-sum 1.0 (* (- sinTheta_O) sinTheta_O))))
1.0)
eta))))\begin{array}{l}
\\
\sin^{-1} \left(\frac{h}{\left(\left(-0.5 \cdot \mathsf{ratio\_of\_squares}\left(sinTheta\_O, eta\right)\right) \cdot \sqrt{\mathsf{ratio\_square\_sum}\left(1, \left(\left(-sinTheta\_O\right) \cdot sinTheta\_O\right)\right)} + 1\right) \cdot eta}\right)
\end{array}
Initial program 92.0%
Taylor expanded in eta around inf
*-commutativeN/A
lower-*.f32N/A
Applied rewrites98.2%
lift-/.f32N/A
metadata-evalN/A
lift-acos.f32N/A
lift-sin.f32N/A
sin-acos-revN/A
pow2N/A
sqrt-divN/A
lower-sqrt.f32N/A
metadata-evalN/A
pow2N/A
fp-cancel-sub-sign-invN/A
lower-ratio-square-sum.f32N/A
lower-*.f32N/A
lower-neg.f3298.2
Applied rewrites98.2%
(FPCore (sinTheta_O h eta) :precision binary32 (asin (/ h (+ (* (* sinTheta_O (/ sinTheta_O eta)) -0.5) eta))))
float code(float sinTheta_O, float h, float eta) {
return asinf((h / (((sinTheta_O * (sinTheta_O / eta)) * -0.5f) + 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 / (((sintheta_o * (sintheta_o / eta)) * (-0.5e0)) + eta)))
end function
function code(sinTheta_O, h, eta) return asin(Float32(h / Float32(Float32(Float32(sinTheta_O * Float32(sinTheta_O / eta)) * Float32(-0.5)) + eta))) end
function tmp = code(sinTheta_O, h, eta) tmp = asin((h / (((sinTheta_O * (sinTheta_O / eta)) * single(-0.5)) + eta))); end
\begin{array}{l}
\\
\sin^{-1} \left(\frac{h}{\left(sinTheta\_O \cdot \frac{sinTheta\_O}{eta}\right) \cdot -0.5 + eta}\right)
\end{array}
Initial program 92.0%
Taylor expanded in sinTheta_O around 0
+-commutativeN/A
lower-+.f32N/A
*-commutativeN/A
lower-*.f32N/A
lower-/.f32N/A
pow2N/A
lift-*.f3297.3
Applied rewrites97.3%
lift-*.f32N/A
lift-/.f32N/A
associate-/l*N/A
lower-*.f32N/A
lower-/.f3298.2
Applied rewrites98.2%
(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
\begin{array}{l}
\\
\sin^{-1} \left(\frac{h}{eta}\right)
\end{array}
Initial program 92.0%
Taylor expanded in sinTheta_O around 0
Applied rewrites95.0%
herbie shell --seed 2025065
(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)))))))))