UniformSampleCone, x

Percentage Accurate: 57.5% → 60.2%
Time: 2.9s
Alternatives: 6
Speedup: 0.9×

Specification

?
\[\left(\left(2.328306437 \cdot 10^{-10} \leq ux \land ux \leq 1\right) \land \left(2.328306437 \cdot 10^{-10} \leq uy \land uy \leq 1\right)\right) \land \left(0 \leq maxCos \land maxCos \leq 1\right)\]
\[\begin{array}{l} \\ \begin{array}{l} t_0 := \left(1 - ux\right) + ux \cdot maxCos\\ \cos \left(\left(uy \cdot 2\right) \cdot \mathsf{PI}\left(\right)\right) \cdot \sqrt{1 - t\_0 \cdot t\_0} \end{array} \end{array} \]
(FPCore (ux uy maxCos)
 :precision binary32
 (let* ((t_0 (+ (- 1.0 ux) (* ux maxCos))))
   (* (cos (* (* uy 2.0) (PI))) (sqrt (- 1.0 (* t_0 t_0))))))
\begin{array}{l}

\\
\begin{array}{l}
t_0 := \left(1 - ux\right) + ux \cdot maxCos\\
\cos \left(\left(uy \cdot 2\right) \cdot \mathsf{PI}\left(\right)\right) \cdot \sqrt{1 - t\_0 \cdot t\_0}
\end{array}
\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 6 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: 57.5% accurate, 1.0× speedup?

\[\begin{array}{l} \\ \begin{array}{l} t_0 := \left(1 - ux\right) + ux \cdot maxCos\\ \cos \left(\left(uy \cdot 2\right) \cdot \mathsf{PI}\left(\right)\right) \cdot \sqrt{1 - t\_0 \cdot t\_0} \end{array} \end{array} \]
(FPCore (ux uy maxCos)
 :precision binary32
 (let* ((t_0 (+ (- 1.0 ux) (* ux maxCos))))
   (* (cos (* (* uy 2.0) (PI))) (sqrt (- 1.0 (* t_0 t_0))))))
\begin{array}{l}

\\
\begin{array}{l}
t_0 := \left(1 - ux\right) + ux \cdot maxCos\\
\cos \left(\left(uy \cdot 2\right) \cdot \mathsf{PI}\left(\right)\right) \cdot \sqrt{1 - t\_0 \cdot t\_0}
\end{array}
\end{array}

Alternative 1: 60.2% accurate, 0.9× speedup?

\[\begin{array}{l} \\ \begin{array}{l} t_0 := \left(1 - ux\right) + ux \cdot maxCos\\ t_1 := \left(uy \cdot 2\right) \cdot \mathsf{PI}\left(\right)\\ \mathbf{if}\;t\_0 \leq 0.9999999403953552:\\ \;\;\;\;\cos t\_1 \cdot \sqrt{1 - t\_0 \cdot t\_0}\\ \mathbf{else}:\\ \;\;\;\;t\_1\\ \end{array} \end{array} \]
(FPCore (ux uy maxCos)
 :precision binary32
 (let* ((t_0 (+ (- 1.0 ux) (* ux maxCos))) (t_1 (* (* uy 2.0) (PI))))
   (if (<= t_0 0.9999999403953552)
     (* (cos t_1) (sqrt (- 1.0 (* t_0 t_0))))
     t_1)))
\begin{array}{l}

\\
\begin{array}{l}
t_0 := \left(1 - ux\right) + ux \cdot maxCos\\
t_1 := \left(uy \cdot 2\right) \cdot \mathsf{PI}\left(\right)\\
\mathbf{if}\;t\_0 \leq 0.9999999403953552:\\
\;\;\;\;\cos t\_1 \cdot \sqrt{1 - t\_0 \cdot t\_0}\\

\mathbf{else}:\\
\;\;\;\;t\_1\\


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if (+.f32 (-.f32 #s(literal 1 binary32) ux) (*.f32 ux maxCos)) < 0.99999994

    1. Initial program 69.1%

      \[\cos \left(\left(uy \cdot 2\right) \cdot \mathsf{PI}\left(\right)\right) \cdot \sqrt{1 - \left(\left(1 - ux\right) + ux \cdot maxCos\right) \cdot \left(\left(1 - ux\right) + ux \cdot maxCos\right)} \]
    2. Add Preprocessing

    if 0.99999994 < (+.f32 (-.f32 #s(literal 1 binary32) ux) (*.f32 ux maxCos))

    1. Initial program 6.8%

      \[\cos \left(\left(uy \cdot 2\right) \cdot \mathsf{PI}\left(\right)\right) \cdot \sqrt{1 - \left(\left(1 - ux\right) + ux \cdot maxCos\right) \cdot \left(\left(1 - ux\right) + ux \cdot maxCos\right)} \]
    2. Add Preprocessing
    3. Taylor expanded in ux around 0

      \[\leadsto \color{blue}{\sqrt{ux \cdot \left(2 - 2 \cdot maxCos\right)} \cdot \cos \left(2 \cdot \left(uy \cdot \mathsf{PI}\left(\right)\right)\right) + {ux}^{2} \cdot \left(\frac{-1}{2} \cdot \left(\sqrt{\frac{1}{ux \cdot \left(2 - 2 \cdot maxCos\right)}} \cdot \left(\cos \left(2 \cdot \left(uy \cdot \mathsf{PI}\left(\right)\right)\right) \cdot {\left(maxCos - 1\right)}^{2}\right)\right) + \frac{-1}{8} \cdot \left(\sqrt{\frac{ux}{{\left(2 - 2 \cdot maxCos\right)}^{3}}} \cdot \left(\cos \left(2 \cdot \left(uy \cdot \mathsf{PI}\left(\right)\right)\right) \cdot {\left(maxCos - 1\right)}^{4}\right)\right)\right)} \]
    4. Applied rewrites21.7%

      \[\leadsto \color{blue}{\left(uy \cdot 2\right) \cdot \mathsf{PI}\left(\right)} \]
  3. Recombined 2 regimes into one program.
  4. Add Preprocessing

Alternative 2: 22.3% accurate, 1.1× speedup?

\[\begin{array}{l} \\ \begin{array}{l} t_0 := \left(uy \cdot 2\right) \cdot \mathsf{PI}\left(\right)\\ \cos t\_0 \cdot \sqrt{t\_0} \end{array} \end{array} \]
(FPCore (ux uy maxCos)
 :precision binary32
 (let* ((t_0 (* (* uy 2.0) (PI)))) (* (cos t_0) (sqrt t_0))))
\begin{array}{l}

\\
\begin{array}{l}
t_0 := \left(uy \cdot 2\right) \cdot \mathsf{PI}\left(\right)\\
\cos t\_0 \cdot \sqrt{t\_0}
\end{array}
\end{array}
Derivation
  1. Initial program 54.0%

    \[\cos \left(\left(uy \cdot 2\right) \cdot \mathsf{PI}\left(\right)\right) \cdot \sqrt{1 - \left(\left(1 - ux\right) + ux \cdot maxCos\right) \cdot \left(\left(1 - ux\right) + ux \cdot maxCos\right)} \]
  2. Add Preprocessing
  3. Taylor expanded in ux around 0

    \[\leadsto \cos \left(\left(uy \cdot 2\right) \cdot \mathsf{PI}\left(\right)\right) \cdot \sqrt{\color{blue}{ux \cdot \left(\left(2 + -1 \cdot \left(ux \cdot {\left(maxCos - 1\right)}^{2}\right)\right) - 2 \cdot maxCos\right)}} \]
  4. Applied rewrites20.1%

    \[\leadsto \cos \left(\left(uy \cdot 2\right) \cdot \mathsf{PI}\left(\right)\right) \cdot \sqrt{\color{blue}{\left(\left(1 - ux\right) + ux \cdot maxCos\right) \cdot \left(\left(1 - ux\right) + ux \cdot maxCos\right)}} \]
  5. Taylor expanded in ux around 0

    \[\leadsto \cos \left(\left(uy \cdot 2\right) \cdot \mathsf{PI}\left(\right)\right) \cdot \sqrt{\color{blue}{ux \cdot \left(\left(2 + -1 \cdot \left(ux \cdot {\left(maxCos - 1\right)}^{2}\right)\right) - 2 \cdot maxCos\right)}} \]
  6. Applied rewrites22.2%

    \[\leadsto \cos \left(\left(uy \cdot 2\right) \cdot \mathsf{PI}\left(\right)\right) \cdot \sqrt{\color{blue}{\left(uy \cdot 2\right) \cdot \mathsf{PI}\left(\right)}} \]
  7. Add Preprocessing

Alternative 3: 22.4% accurate, 1.2× speedup?

\[\begin{array}{l} \\ \cos \left(\left(uy \cdot 2\right) \cdot \mathsf{PI}\left(\right)\right) \cdot \sqrt{uy \cdot 2} \end{array} \]
(FPCore (ux uy maxCos)
 :precision binary32
 (* (cos (* (* uy 2.0) (PI))) (sqrt (* uy 2.0))))
\begin{array}{l}

\\
\cos \left(\left(uy \cdot 2\right) \cdot \mathsf{PI}\left(\right)\right) \cdot \sqrt{uy \cdot 2}
\end{array}
Derivation
  1. Initial program 54.0%

    \[\cos \left(\left(uy \cdot 2\right) \cdot \mathsf{PI}\left(\right)\right) \cdot \sqrt{1 - \left(\left(1 - ux\right) + ux \cdot maxCos\right) \cdot \left(\left(1 - ux\right) + ux \cdot maxCos\right)} \]
  2. Add Preprocessing
  3. Taylor expanded in ux around 0

    \[\leadsto \cos \left(\left(uy \cdot 2\right) \cdot \mathsf{PI}\left(\right)\right) \cdot \sqrt{\color{blue}{ux \cdot \left(\left(2 + -1 \cdot \left(ux \cdot {\left(maxCos - 1\right)}^{2}\right)\right) - 2 \cdot maxCos\right)}} \]
  4. Applied rewrites20.1%

    \[\leadsto \cos \left(\left(uy \cdot 2\right) \cdot \mathsf{PI}\left(\right)\right) \cdot \sqrt{\color{blue}{\left(\left(1 - ux\right) + ux \cdot maxCos\right) \cdot \left(\left(1 - ux\right) + ux \cdot maxCos\right)}} \]
  5. Taylor expanded in ux around 0

    \[\leadsto \cos \left(\left(uy \cdot 2\right) \cdot \mathsf{PI}\left(\right)\right) \cdot \sqrt{\color{blue}{ux \cdot \left(2 - 2 \cdot maxCos\right)}} \]
  6. Applied rewrites22.2%

    \[\leadsto \cos \left(\left(uy \cdot 2\right) \cdot \mathsf{PI}\left(\right)\right) \cdot \sqrt{\color{blue}{uy \cdot 2}} \]
  7. Add Preprocessing

Alternative 4: 20.8% accurate, 1.4× speedup?

\[\begin{array}{l} \\ \cos \left(\left(uy \cdot 2\right) \cdot \mathsf{PI}\left(\right)\right) \end{array} \]
(FPCore (ux uy maxCos) :precision binary32 (cos (* (* uy 2.0) (PI))))
\begin{array}{l}

\\
\cos \left(\left(uy \cdot 2\right) \cdot \mathsf{PI}\left(\right)\right)
\end{array}
Derivation
  1. Initial program 54.0%

    \[\cos \left(\left(uy \cdot 2\right) \cdot \mathsf{PI}\left(\right)\right) \cdot \sqrt{1 - \left(\left(1 - ux\right) + ux \cdot maxCos\right) \cdot \left(\left(1 - ux\right) + ux \cdot maxCos\right)} \]
  2. Add Preprocessing
  3. Taylor expanded in ux around 0

    \[\leadsto \color{blue}{\frac{-1}{2} \cdot \left(\sqrt{\frac{{ux}^{3}}{2 - 2 \cdot maxCos}} \cdot \left(\cos \left(2 \cdot \left(uy \cdot \mathsf{PI}\left(\right)\right)\right) \cdot {\left(maxCos - 1\right)}^{2}\right)\right) + \sqrt{ux \cdot \left(2 - 2 \cdot maxCos\right)} \cdot \cos \left(2 \cdot \left(uy \cdot \mathsf{PI}\left(\right)\right)\right)} \]
  4. Applied rewrites20.4%

    \[\leadsto \color{blue}{\cos \left(\left(uy \cdot 2\right) \cdot \mathsf{PI}\left(\right)\right)} \]
  5. Add Preprocessing

Alternative 5: 19.5% accurate, 5.6× speedup?

\[\begin{array}{l} \\ \begin{array}{l} t_0 := \left(1 - ux\right) + ux \cdot maxCos\\ t\_0 \cdot t\_0 \end{array} \end{array} \]
(FPCore (ux uy maxCos)
 :precision binary32
 (let* ((t_0 (+ (- 1.0 ux) (* ux maxCos)))) (* t_0 t_0)))
float code(float ux, float uy, float maxCos) {
	float t_0 = (1.0f - ux) + (ux * maxCos);
	return t_0 * t_0;
}
real(4) function code(ux, uy, maxcos)
    real(4), intent (in) :: ux
    real(4), intent (in) :: uy
    real(4), intent (in) :: maxcos
    real(4) :: t_0
    t_0 = (1.0e0 - ux) + (ux * maxcos)
    code = t_0 * t_0
end function
function code(ux, uy, maxCos)
	t_0 = Float32(Float32(Float32(1.0) - ux) + Float32(ux * maxCos))
	return Float32(t_0 * t_0)
end
function tmp = code(ux, uy, maxCos)
	t_0 = (single(1.0) - ux) + (ux * maxCos);
	tmp = t_0 * t_0;
end
\begin{array}{l}

\\
\begin{array}{l}
t_0 := \left(1 - ux\right) + ux \cdot maxCos\\
t\_0 \cdot t\_0
\end{array}
\end{array}
Derivation
  1. Initial program 54.0%

    \[\cos \left(\left(uy \cdot 2\right) \cdot \mathsf{PI}\left(\right)\right) \cdot \sqrt{1 - \left(\left(1 - ux\right) + ux \cdot maxCos\right) \cdot \left(\left(1 - ux\right) + ux \cdot maxCos\right)} \]
  2. Add Preprocessing
  3. Taylor expanded in ux around 0

    \[\leadsto \color{blue}{\frac{-1}{2} \cdot \left(\sqrt{\frac{{ux}^{3}}{2 - 2 \cdot maxCos}} \cdot \left(\cos \left(2 \cdot \left(uy \cdot \mathsf{PI}\left(\right)\right)\right) \cdot {\left(maxCos - 1\right)}^{2}\right)\right) + \sqrt{ux \cdot \left(2 - 2 \cdot maxCos\right)} \cdot \cos \left(2 \cdot \left(uy \cdot \mathsf{PI}\left(\right)\right)\right)} \]
  4. Applied rewrites20.4%

    \[\leadsto \color{blue}{\cos \left(\left(uy \cdot 2\right) \cdot \mathsf{PI}\left(\right)\right)} \]
  5. Taylor expanded in ux around inf

    \[\leadsto \color{blue}{ux \cdot \left(\cos \left(2 \cdot \left(uy \cdot \mathsf{PI}\left(\right)\right)\right) \cdot \left(\sqrt{-1} \cdot \left(maxCos - 1\right)\right)\right)} \]
  6. Applied rewrites19.4%

    \[\leadsto \color{blue}{\left(\left(1 - ux\right) + ux \cdot maxCos\right) \cdot \left(\left(1 - ux\right) + ux \cdot maxCos\right)} \]
  7. Add Preprocessing

Alternative 6: 19.6% accurate, 39.0× speedup?

\[\begin{array}{l} \\ 1 - ux \end{array} \]
(FPCore (ux uy maxCos) :precision binary32 (- 1.0 ux))
float code(float ux, float uy, float maxCos) {
	return 1.0f - ux;
}
real(4) function code(ux, uy, maxcos)
    real(4), intent (in) :: ux
    real(4), intent (in) :: uy
    real(4), intent (in) :: maxcos
    code = 1.0e0 - ux
end function
function code(ux, uy, maxCos)
	return Float32(Float32(1.0) - ux)
end
function tmp = code(ux, uy, maxCos)
	tmp = single(1.0) - ux;
end
\begin{array}{l}

\\
1 - ux
\end{array}
Derivation
  1. Initial program 54.0%

    \[\cos \left(\left(uy \cdot 2\right) \cdot \mathsf{PI}\left(\right)\right) \cdot \sqrt{1 - \left(\left(1 - ux\right) + ux \cdot maxCos\right) \cdot \left(\left(1 - ux\right) + ux \cdot maxCos\right)} \]
  2. Add Preprocessing
  3. Taylor expanded in ux around 0

    \[\leadsto \color{blue}{\frac{-1}{2} \cdot \left(\sqrt{\frac{{ux}^{3}}{2 - 2 \cdot maxCos}} \cdot \left(\cos \left(2 \cdot \left(uy \cdot \mathsf{PI}\left(\right)\right)\right) \cdot {\left(maxCos - 1\right)}^{2}\right)\right) + \sqrt{ux \cdot \left(2 - 2 \cdot maxCos\right)} \cdot \cos \left(2 \cdot \left(uy \cdot \mathsf{PI}\left(\right)\right)\right)} \]
  4. Applied rewrites20.4%

    \[\leadsto \color{blue}{\cos \left(\left(uy \cdot 2\right) \cdot \mathsf{PI}\left(\right)\right)} \]
  5. Taylor expanded in ux around inf

    \[\leadsto \color{blue}{ux \cdot \left(\cos \left(2 \cdot \left(uy \cdot \mathsf{PI}\left(\right)\right)\right) \cdot \left(\sqrt{-1} \cdot \left(maxCos - 1\right)\right)\right)} \]
  6. Applied rewrites19.4%

    \[\leadsto \color{blue}{\left(\left(1 - ux\right) + ux \cdot maxCos\right) \cdot \left(\left(1 - ux\right) + ux \cdot maxCos\right)} \]
  7. Taylor expanded in ux around 0

    \[\leadsto \color{blue}{\sqrt{ux \cdot \left(2 - 2 \cdot maxCos\right)} \cdot \cos \left(2 \cdot \left(uy \cdot \mathsf{PI}\left(\right)\right)\right) + {ux}^{2} \cdot \left(\frac{-1}{2} \cdot \left(\sqrt{\frac{1}{ux \cdot \left(2 - 2 \cdot maxCos\right)}} \cdot \left(\cos \left(2 \cdot \left(uy \cdot \mathsf{PI}\left(\right)\right)\right) \cdot {\left(maxCos - 1\right)}^{2}\right)\right) + {ux}^{2} \cdot \left(\frac{-1}{8} \cdot \left(\sqrt{\frac{1}{{ux}^{3} \cdot {\left(2 - 2 \cdot maxCos\right)}^{3}}} \cdot \left(\cos \left(2 \cdot \left(uy \cdot \mathsf{PI}\left(\right)\right)\right) \cdot {\left(maxCos - 1\right)}^{4}\right)\right) + \frac{-1}{16} \cdot \left(\sqrt{\frac{1}{ux \cdot {\left(2 - 2 \cdot maxCos\right)}^{5}}} \cdot \left(\cos \left(2 \cdot \left(uy \cdot \mathsf{PI}\left(\right)\right)\right) \cdot {\left(maxCos - 1\right)}^{6}\right)\right)\right)\right)} \]
  8. Applied rewrites19.3%

    \[\leadsto \color{blue}{1 - ux} \]
  9. Add Preprocessing

Reproduce

?
herbie shell --seed 2024321 
(FPCore (ux uy maxCos)
  :name "UniformSampleCone, x"
  :precision binary32
  :pre (and (and (and (<= 2.328306437e-10 ux) (<= ux 1.0)) (and (<= 2.328306437e-10 uy) (<= uy 1.0))) (and (<= 0.0 maxCos) (<= maxCos 1.0)))
  (* (cos (* (* uy 2.0) (PI))) (sqrt (- 1.0 (* (+ (- 1.0 ux) (* ux maxCos)) (+ (- 1.0 ux) (* ux maxCos)))))))