
(FPCore (cosTheta_i u1 u2) :precision binary32 (* (sqrt (/ u1 (- 1.0 u1))) (cos (* 6.28318530718 u2))))
float code(float cosTheta_i, float u1, float u2) {
return sqrtf((u1 / (1.0f - u1))) * cosf((6.28318530718f * u2));
}
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(costheta_i, u1, u2)
use fmin_fmax_functions
real(4), intent (in) :: costheta_i
real(4), intent (in) :: u1
real(4), intent (in) :: u2
code = sqrt((u1 / (1.0e0 - u1))) * cos((6.28318530718e0 * u2))
end function
function code(cosTheta_i, u1, u2) return Float32(sqrt(Float32(u1 / Float32(Float32(1.0) - u1))) * cos(Float32(Float32(6.28318530718) * u2))) end
function tmp = code(cosTheta_i, u1, u2) tmp = sqrt((u1 / (single(1.0) - u1))) * cos((single(6.28318530718) * u2)); end
\begin{array}{l}
\\
\sqrt{\frac{u1}{1 - u1}} \cdot \cos \left(6.28318530718 \cdot u2\right)
\end{array}
Sampling outcomes in binary32 precision:
Herbie found 4 alternatives:
| Alternative | Accuracy | Speedup |
|---|
(FPCore (cosTheta_i u1 u2) :precision binary32 (* (sqrt (/ u1 (- 1.0 u1))) (cos (* 6.28318530718 u2))))
float code(float cosTheta_i, float u1, float u2) {
return sqrtf((u1 / (1.0f - u1))) * cosf((6.28318530718f * u2));
}
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(costheta_i, u1, u2)
use fmin_fmax_functions
real(4), intent (in) :: costheta_i
real(4), intent (in) :: u1
real(4), intent (in) :: u2
code = sqrt((u1 / (1.0e0 - u1))) * cos((6.28318530718e0 * u2))
end function
function code(cosTheta_i, u1, u2) return Float32(sqrt(Float32(u1 / Float32(Float32(1.0) - u1))) * cos(Float32(Float32(6.28318530718) * u2))) end
function tmp = code(cosTheta_i, u1, u2) tmp = sqrt((u1 / (single(1.0) - u1))) * cos((single(6.28318530718) * u2)); end
\begin{array}{l}
\\
\sqrt{\frac{u1}{1 - u1}} \cdot \cos \left(6.28318530718 \cdot u2\right)
\end{array}
(FPCore (cosTheta_i u1 u2) :precision binary32 (* (sqrt (/ u1 (- 1.0 u1))) (cos (* 6.28318530718 u2))))
float code(float cosTheta_i, float u1, float u2) {
return sqrtf((u1 / (1.0f - u1))) * cosf((6.28318530718f * u2));
}
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(costheta_i, u1, u2)
use fmin_fmax_functions
real(4), intent (in) :: costheta_i
real(4), intent (in) :: u1
real(4), intent (in) :: u2
code = sqrt((u1 / (1.0e0 - u1))) * cos((6.28318530718e0 * u2))
end function
function code(cosTheta_i, u1, u2) return Float32(sqrt(Float32(u1 / Float32(Float32(1.0) - u1))) * cos(Float32(Float32(6.28318530718) * u2))) end
function tmp = code(cosTheta_i, u1, u2) tmp = sqrt((u1 / (single(1.0) - u1))) * cos((single(6.28318530718) * u2)); end
\begin{array}{l}
\\
\sqrt{\frac{u1}{1 - u1}} \cdot \cos \left(6.28318530718 \cdot u2\right)
\end{array}
Initial program 99.1%
(FPCore (cosTheta_i u1 u2) :precision binary32 (* (sqrt (/ (* u1 (+ 1.0 (+ u1 (* u1 u1)))) (- 1.0 (pow u1 3.0)))) (sin (fma 6.28318530718 u2 (/ PI 2.0)))))
float code(float cosTheta_i, float u1, float u2) {
return sqrtf(((u1 * (1.0f + (u1 + (u1 * u1)))) / (1.0f - powf(u1, 3.0f)))) * sinf(fmaf(6.28318530718f, u2, (((float) M_PI) / 2.0f)));
}
function code(cosTheta_i, u1, u2) return Float32(sqrt(Float32(Float32(u1 * Float32(Float32(1.0) + Float32(u1 + Float32(u1 * u1)))) / Float32(Float32(1.0) - (u1 ^ Float32(3.0))))) * sin(fma(Float32(6.28318530718), u2, Float32(Float32(pi) / Float32(2.0))))) end
\begin{array}{l}
\\
\sqrt{\frac{u1 \cdot \left(1 + \left(u1 + u1 \cdot u1\right)\right)}{1 - {u1}^{3}}} \cdot \sin \left(\mathsf{fma}\left(6.28318530718, u2, \frac{\pi}{2}\right)\right)
\end{array}
Initial program 99.1%
lift--.f32N/A
flip3--N/A
lower-/.f32N/A
metadata-evalN/A
lower--.f32N/A
lower-pow.f32N/A
metadata-evalN/A
lower-+.f32N/A
lower-fma.f32N/A
lower-*.f3299.0
Applied rewrites99.0%
Taylor expanded in u2 around inf
lower-*.f32N/A
lower-sqrt.f32N/A
lower-/.f32N/A
lower-*.f32N/A
lower-+.f32N/A
lower-+.f32N/A
pow2N/A
lift-*.f32N/A
lift-pow.f32N/A
lift--.f32N/A
sin-+PI/2-revN/A
lower-sin.f32N/A
Applied rewrites98.9%
(FPCore (cosTheta_i u1 u2)
:precision binary32
(let* ((t_0 (pow (/ u1 (- 1.0 u1)) 0.5)))
(fma
(fma
(- (* (* -85.45681720672748 t_0) (* u2 u2)) (* -64.93939402268539 t_0))
(* u2 u2)
(* -19.739208802181317 t_0))
(* u2 u2)
t_0)))
float code(float cosTheta_i, float u1, float u2) {
float t_0 = powf((u1 / (1.0f - u1)), 0.5f);
return fmaf(fmaf((((-85.45681720672748f * t_0) * (u2 * u2)) - (-64.93939402268539f * t_0)), (u2 * u2), (-19.739208802181317f * t_0)), (u2 * u2), t_0);
}
function code(cosTheta_i, u1, u2) t_0 = Float32(u1 / Float32(Float32(1.0) - u1)) ^ Float32(0.5) return fma(fma(Float32(Float32(Float32(Float32(-85.45681720672748) * t_0) * Float32(u2 * u2)) - Float32(Float32(-64.93939402268539) * t_0)), Float32(u2 * u2), Float32(Float32(-19.739208802181317) * t_0)), Float32(u2 * u2), t_0) end
\begin{array}{l}
\\
\begin{array}{l}
t_0 := {\left(\frac{u1}{1 - u1}\right)}^{0.5}\\
\mathsf{fma}\left(\mathsf{fma}\left(\left(-85.45681720672748 \cdot t\_0\right) \cdot \left(u2 \cdot u2\right) - -64.93939402268539 \cdot t\_0, u2 \cdot u2, -19.739208802181317 \cdot t\_0\right), u2 \cdot u2, t\_0\right)
\end{array}
\end{array}
Initial program 99.1%
Taylor expanded in u2 around 0
+-commutativeN/A
*-commutativeN/A
lower-fma.f32N/A
Applied rewrites94.0%
(FPCore (cosTheta_i u1 u2)
:precision binary32
(let* ((t_0 (pow (/ u1 (- 1.0 u1)) 0.5))
(t_1
(*
(* u2 u2)
(-
(*
(* u2 u2)
(-
(* (* (* -85.45681720672748 t_0) u2) u2)
(* -64.93939402268539 t_0)))
(* 19.739208802181317 t_0)))))
(/
(+ (pow t_0 3.0) (pow t_1 3.0))
(+ (* t_0 t_0) (- (* t_1 t_1) (* t_0 t_1))))))
float code(float cosTheta_i, float u1, float u2) {
float t_0 = powf((u1 / (1.0f - u1)), 0.5f);
float t_1 = (u2 * u2) * (((u2 * u2) * ((((-85.45681720672748f * t_0) * u2) * u2) - (-64.93939402268539f * t_0))) - (19.739208802181317f * t_0));
return (powf(t_0, 3.0f) + powf(t_1, 3.0f)) / ((t_0 * t_0) + ((t_1 * t_1) - (t_0 * t_1)));
}
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(costheta_i, u1, u2)
use fmin_fmax_functions
real(4), intent (in) :: costheta_i
real(4), intent (in) :: u1
real(4), intent (in) :: u2
real(4) :: t_0
real(4) :: t_1
t_0 = (u1 / (1.0e0 - u1)) ** 0.5e0
t_1 = (u2 * u2) * (((u2 * u2) * (((((-85.45681720672748e0) * t_0) * u2) * u2) - ((-64.93939402268539e0) * t_0))) - (19.739208802181317e0 * t_0))
code = ((t_0 ** 3.0e0) + (t_1 ** 3.0e0)) / ((t_0 * t_0) + ((t_1 * t_1) - (t_0 * t_1)))
end function
function code(cosTheta_i, u1, u2) t_0 = Float32(u1 / Float32(Float32(1.0) - u1)) ^ Float32(0.5) t_1 = Float32(Float32(u2 * u2) * Float32(Float32(Float32(u2 * u2) * Float32(Float32(Float32(Float32(Float32(-85.45681720672748) * t_0) * u2) * u2) - Float32(Float32(-64.93939402268539) * t_0))) - Float32(Float32(19.739208802181317) * t_0))) return Float32(Float32((t_0 ^ Float32(3.0)) + (t_1 ^ Float32(3.0))) / Float32(Float32(t_0 * t_0) + Float32(Float32(t_1 * t_1) - Float32(t_0 * t_1)))) end
function tmp = code(cosTheta_i, u1, u2) t_0 = (u1 / (single(1.0) - u1)) ^ single(0.5); t_1 = (u2 * u2) * (((u2 * u2) * ((((single(-85.45681720672748) * t_0) * u2) * u2) - (single(-64.93939402268539) * t_0))) - (single(19.739208802181317) * t_0)); tmp = ((t_0 ^ single(3.0)) + (t_1 ^ single(3.0))) / ((t_0 * t_0) + ((t_1 * t_1) - (t_0 * t_1))); end
\begin{array}{l}
\\
\begin{array}{l}
t_0 := {\left(\frac{u1}{1 - u1}\right)}^{0.5}\\
t_1 := \left(u2 \cdot u2\right) \cdot \left(\left(u2 \cdot u2\right) \cdot \left(\left(\left(-85.45681720672748 \cdot t\_0\right) \cdot u2\right) \cdot u2 - -64.93939402268539 \cdot t\_0\right) - 19.739208802181317 \cdot t\_0\right)\\
\frac{{t\_0}^{3} + {t\_1}^{3}}{t\_0 \cdot t\_0 + \left(t\_1 \cdot t\_1 - t\_0 \cdot t\_1\right)}
\end{array}
\end{array}
Initial program 99.1%
Taylor expanded in u2 around 0
+-commutativeN/A
*-commutativeN/A
lower-fma.f32N/A
Applied rewrites94.0%
Applied rewrites93.6%
herbie shell --seed 2025066
(FPCore (cosTheta_i u1 u2)
:name "Trowbridge-Reitz Sample, near normal, slope_x"
:precision binary32
:pre (and (and (and (> cosTheta_i 0.9999) (<= cosTheta_i 1.0)) (and (<= 2.328306437e-10 u1) (<= u1 1.0))) (and (<= 2.328306437e-10 u2) (<= u2 1.0)))
(* (sqrt (/ u1 (- 1.0 u1))) (cos (* 6.28318530718 u2))))