UniformSampleCone, y

Percentage Accurate: 57.5% → 98.3%
Time: 6.0s
Alternatives: 1

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\\ \sin \left(\left(uy \cdot 2\right) \cdot \pi\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))))
   (* (sin (* (* uy 2.0) PI)) (sqrt (- 1.0 (* t_0 t_0))))))
float code(float ux, float uy, float maxCos) {
	float t_0 = (1.0f - ux) + (ux * maxCos);
	return sinf(((uy * 2.0f) * ((float) M_PI))) * sqrtf((1.0f - (t_0 * t_0)));
}
function code(ux, uy, maxCos)
	t_0 = Float32(Float32(Float32(1.0) - ux) + Float32(ux * maxCos))
	return Float32(sin(Float32(Float32(uy * Float32(2.0)) * Float32(pi))) * sqrt(Float32(Float32(1.0) - Float32(t_0 * t_0))))
end
function tmp = code(ux, uy, maxCos)
	t_0 = (single(1.0) - ux) + (ux * maxCos);
	tmp = sin(((uy * single(2.0)) * single(pi))) * sqrt((single(1.0) - (t_0 * t_0)));
end
\begin{array}{l}

\\
\begin{array}{l}
t_0 := \left(1 - ux\right) + ux \cdot maxCos\\
\sin \left(\left(uy \cdot 2\right) \cdot \pi\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 1 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\\ \sin \left(\left(uy \cdot 2\right) \cdot \pi\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))))
   (* (sin (* (* uy 2.0) PI)) (sqrt (- 1.0 (* t_0 t_0))))))
float code(float ux, float uy, float maxCos) {
	float t_0 = (1.0f - ux) + (ux * maxCos);
	return sinf(((uy * 2.0f) * ((float) M_PI))) * sqrtf((1.0f - (t_0 * t_0)));
}
function code(ux, uy, maxCos)
	t_0 = Float32(Float32(Float32(1.0) - ux) + Float32(ux * maxCos))
	return Float32(sin(Float32(Float32(uy * Float32(2.0)) * Float32(pi))) * sqrt(Float32(Float32(1.0) - Float32(t_0 * t_0))))
end
function tmp = code(ux, uy, maxCos)
	t_0 = (single(1.0) - ux) + (ux * maxCos);
	tmp = sin(((uy * single(2.0)) * single(pi))) * sqrt((single(1.0) - (t_0 * t_0)));
end
\begin{array}{l}

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

Alternative 1: 98.3% accurate, 0.6× speedup?

\[\begin{array}{l} \\ \sin \left(\left(uy \cdot 2\right) \cdot \pi\right) \cdot \sqrt{\left(\mathsf{fma}\left(-1, {\left(maxCos - 1\right)}^{2}, \frac{\mathsf{fma}\left(-2, maxCos, 2\right)}{ux}\right) \cdot ux\right) \cdot ux} \end{array} \]
(FPCore (ux uy maxCos)
 :precision binary32
 (*
  (sin (* (* uy 2.0) PI))
  (sqrt
   (*
    (* (fma -1.0 (pow (- maxCos 1.0) 2.0) (/ (fma -2.0 maxCos 2.0) ux)) ux)
    ux))))
float code(float ux, float uy, float maxCos) {
	return sinf(((uy * 2.0f) * ((float) M_PI))) * sqrtf(((fmaf(-1.0f, powf((maxCos - 1.0f), 2.0f), (fmaf(-2.0f, maxCos, 2.0f) / ux)) * ux) * ux));
}
function code(ux, uy, maxCos)
	return Float32(sin(Float32(Float32(uy * Float32(2.0)) * Float32(pi))) * sqrt(Float32(Float32(fma(Float32(-1.0), (Float32(maxCos - Float32(1.0)) ^ Float32(2.0)), Float32(fma(Float32(-2.0), maxCos, Float32(2.0)) / ux)) * ux) * ux)))
end
\begin{array}{l}

\\
\sin \left(\left(uy \cdot 2\right) \cdot \pi\right) \cdot \sqrt{\left(\mathsf{fma}\left(-1, {\left(maxCos - 1\right)}^{2}, \frac{\mathsf{fma}\left(-2, maxCos, 2\right)}{ux}\right) \cdot ux\right) \cdot ux}
\end{array}
Derivation
  1. Initial program 53.9%

    \[\sin \left(\left(uy \cdot 2\right) \cdot \pi\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 \sin \left(\left(uy \cdot 2\right) \cdot \pi\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. Step-by-step derivation
    1. *-commutativeN/A

      \[\leadsto \sin \left(\left(uy \cdot 2\right) \cdot \pi\right) \cdot \sqrt{\left(\left(2 + -1 \cdot \left(ux \cdot {\left(maxCos - 1\right)}^{2}\right)\right) - 2 \cdot maxCos\right) \cdot \color{blue}{ux}} \]
    2. lower-*.f32N/A

      \[\leadsto \sin \left(\left(uy \cdot 2\right) \cdot \pi\right) \cdot \sqrt{\left(\left(2 + -1 \cdot \left(ux \cdot {\left(maxCos - 1\right)}^{2}\right)\right) - 2 \cdot maxCos\right) \cdot \color{blue}{ux}} \]
    3. lower--.f32N/A

      \[\leadsto \sin \left(\left(uy \cdot 2\right) \cdot \pi\right) \cdot \sqrt{\left(\left(2 + -1 \cdot \left(ux \cdot {\left(maxCos - 1\right)}^{2}\right)\right) - 2 \cdot maxCos\right) \cdot ux} \]
    4. +-commutativeN/A

      \[\leadsto \sin \left(\left(uy \cdot 2\right) \cdot \pi\right) \cdot \sqrt{\left(\left(-1 \cdot \left(ux \cdot {\left(maxCos - 1\right)}^{2}\right) + 2\right) - 2 \cdot maxCos\right) \cdot ux} \]
    5. associate-*r*N/A

      \[\leadsto \sin \left(\left(uy \cdot 2\right) \cdot \pi\right) \cdot \sqrt{\left(\left(\left(-1 \cdot ux\right) \cdot {\left(maxCos - 1\right)}^{2} + 2\right) - 2 \cdot maxCos\right) \cdot ux} \]
    6. mul-1-negN/A

      \[\leadsto \sin \left(\left(uy \cdot 2\right) \cdot \pi\right) \cdot \sqrt{\left(\left(\left(\mathsf{neg}\left(ux\right)\right) \cdot {\left(maxCos - 1\right)}^{2} + 2\right) - 2 \cdot maxCos\right) \cdot ux} \]
    7. lower-fma.f32N/A

      \[\leadsto \sin \left(\left(uy \cdot 2\right) \cdot \pi\right) \cdot \sqrt{\left(\mathsf{fma}\left(\mathsf{neg}\left(ux\right), {\left(maxCos - 1\right)}^{2}, 2\right) - 2 \cdot maxCos\right) \cdot ux} \]
    8. lower-neg.f32N/A

      \[\leadsto \sin \left(\left(uy \cdot 2\right) \cdot \pi\right) \cdot \sqrt{\left(\mathsf{fma}\left(-ux, {\left(maxCos - 1\right)}^{2}, 2\right) - 2 \cdot maxCos\right) \cdot ux} \]
    9. lower-pow.f32N/A

      \[\leadsto \sin \left(\left(uy \cdot 2\right) \cdot \pi\right) \cdot \sqrt{\left(\mathsf{fma}\left(-ux, {\left(maxCos - 1\right)}^{2}, 2\right) - 2 \cdot maxCos\right) \cdot ux} \]
    10. lower--.f32N/A

      \[\leadsto \sin \left(\left(uy \cdot 2\right) \cdot \pi\right) \cdot \sqrt{\left(\mathsf{fma}\left(-ux, {\left(maxCos - 1\right)}^{2}, 2\right) - 2 \cdot maxCos\right) \cdot ux} \]
    11. *-commutativeN/A

      \[\leadsto \sin \left(\left(uy \cdot 2\right) \cdot \pi\right) \cdot \sqrt{\left(\mathsf{fma}\left(-ux, {\left(maxCos - 1\right)}^{2}, 2\right) - maxCos \cdot 2\right) \cdot ux} \]
    12. lower-*.f3298.3

      \[\leadsto \sin \left(\left(uy \cdot 2\right) \cdot \pi\right) \cdot \sqrt{\left(\mathsf{fma}\left(-ux, {\left(maxCos - 1\right)}^{2}, 2\right) - maxCos \cdot 2\right) \cdot ux} \]
  5. Applied rewrites98.3%

    \[\leadsto \sin \left(\left(uy \cdot 2\right) \cdot \pi\right) \cdot \sqrt{\color{blue}{\left(\mathsf{fma}\left(-ux, {\left(maxCos - 1\right)}^{2}, 2\right) - maxCos \cdot 2\right) \cdot ux}} \]
  6. Taylor expanded in ux around inf

    \[\leadsto \sin \left(\left(uy \cdot 2\right) \cdot \pi\right) \cdot \sqrt{\left(ux \cdot \left(\left(-1 \cdot {\left(maxCos - 1\right)}^{2} + 2 \cdot \frac{1}{ux}\right) - 2 \cdot \frac{maxCos}{ux}\right)\right) \cdot ux} \]
  7. Step-by-step derivation
    1. *-commutativeN/A

      \[\leadsto \sin \left(\left(uy \cdot 2\right) \cdot \pi\right) \cdot \sqrt{\left(\left(\left(-1 \cdot {\left(maxCos - 1\right)}^{2} + 2 \cdot \frac{1}{ux}\right) - 2 \cdot \frac{maxCos}{ux}\right) \cdot ux\right) \cdot ux} \]
    2. lower-*.f32N/A

      \[\leadsto \sin \left(\left(uy \cdot 2\right) \cdot \pi\right) \cdot \sqrt{\left(\left(\left(-1 \cdot {\left(maxCos - 1\right)}^{2} + 2 \cdot \frac{1}{ux}\right) - 2 \cdot \frac{maxCos}{ux}\right) \cdot ux\right) \cdot ux} \]
    3. associate--l+N/A

      \[\leadsto \sin \left(\left(uy \cdot 2\right) \cdot \pi\right) \cdot \sqrt{\left(\left(-1 \cdot {\left(maxCos - 1\right)}^{2} + \left(2 \cdot \frac{1}{ux} - 2 \cdot \frac{maxCos}{ux}\right)\right) \cdot ux\right) \cdot ux} \]
    4. associate-*r/N/A

      \[\leadsto \sin \left(\left(uy \cdot 2\right) \cdot \pi\right) \cdot \sqrt{\left(\left(-1 \cdot {\left(maxCos - 1\right)}^{2} + \left(\frac{2 \cdot 1}{ux} - 2 \cdot \frac{maxCos}{ux}\right)\right) \cdot ux\right) \cdot ux} \]
    5. metadata-evalN/A

      \[\leadsto \sin \left(\left(uy \cdot 2\right) \cdot \pi\right) \cdot \sqrt{\left(\left(-1 \cdot {\left(maxCos - 1\right)}^{2} + \left(\frac{2}{ux} - 2 \cdot \frac{maxCos}{ux}\right)\right) \cdot ux\right) \cdot ux} \]
    6. associate-*r/N/A

      \[\leadsto \sin \left(\left(uy \cdot 2\right) \cdot \pi\right) \cdot \sqrt{\left(\left(-1 \cdot {\left(maxCos - 1\right)}^{2} + \left(\frac{2}{ux} - \frac{2 \cdot maxCos}{ux}\right)\right) \cdot ux\right) \cdot ux} \]
    7. div-subN/A

      \[\leadsto \sin \left(\left(uy \cdot 2\right) \cdot \pi\right) \cdot \sqrt{\left(\left(-1 \cdot {\left(maxCos - 1\right)}^{2} + \frac{2 - 2 \cdot maxCos}{ux}\right) \cdot ux\right) \cdot ux} \]
    8. lower-fma.f32N/A

      \[\leadsto \sin \left(\left(uy \cdot 2\right) \cdot \pi\right) \cdot \sqrt{\left(\mathsf{fma}\left(-1, {\left(maxCos - 1\right)}^{2}, \frac{2 - 2 \cdot maxCos}{ux}\right) \cdot ux\right) \cdot ux} \]
    9. lift-pow.f32N/A

      \[\leadsto \sin \left(\left(uy \cdot 2\right) \cdot \pi\right) \cdot \sqrt{\left(\mathsf{fma}\left(-1, {\left(maxCos - 1\right)}^{2}, \frac{2 - 2 \cdot maxCos}{ux}\right) \cdot ux\right) \cdot ux} \]
    10. lift--.f32N/A

      \[\leadsto \sin \left(\left(uy \cdot 2\right) \cdot \pi\right) \cdot \sqrt{\left(\mathsf{fma}\left(-1, {\left(maxCos - 1\right)}^{2}, \frac{2 - 2 \cdot maxCos}{ux}\right) \cdot ux\right) \cdot ux} \]
    11. lower-/.f32N/A

      \[\leadsto \sin \left(\left(uy \cdot 2\right) \cdot \pi\right) \cdot \sqrt{\left(\mathsf{fma}\left(-1, {\left(maxCos - 1\right)}^{2}, \frac{2 - 2 \cdot maxCos}{ux}\right) \cdot ux\right) \cdot ux} \]
    12. metadata-evalN/A

      \[\leadsto \sin \left(\left(uy \cdot 2\right) \cdot \pi\right) \cdot \sqrt{\left(\mathsf{fma}\left(-1, {\left(maxCos - 1\right)}^{2}, \frac{2 - \left(\mathsf{neg}\left(-2\right)\right) \cdot maxCos}{ux}\right) \cdot ux\right) \cdot ux} \]
    13. fp-cancel-sign-sub-invN/A

      \[\leadsto \sin \left(\left(uy \cdot 2\right) \cdot \pi\right) \cdot \sqrt{\left(\mathsf{fma}\left(-1, {\left(maxCos - 1\right)}^{2}, \frac{2 + -2 \cdot maxCos}{ux}\right) \cdot ux\right) \cdot ux} \]
    14. +-commutativeN/A

      \[\leadsto \sin \left(\left(uy \cdot 2\right) \cdot \pi\right) \cdot \sqrt{\left(\mathsf{fma}\left(-1, {\left(maxCos - 1\right)}^{2}, \frac{-2 \cdot maxCos + 2}{ux}\right) \cdot ux\right) \cdot ux} \]
    15. lower-fma.f3298.3

      \[\leadsto \sin \left(\left(uy \cdot 2\right) \cdot \pi\right) \cdot \sqrt{\left(\mathsf{fma}\left(-1, {\left(maxCos - 1\right)}^{2}, \frac{\mathsf{fma}\left(-2, maxCos, 2\right)}{ux}\right) \cdot ux\right) \cdot ux} \]
  8. Applied rewrites98.3%

    \[\leadsto \sin \left(\left(uy \cdot 2\right) \cdot \pi\right) \cdot \sqrt{\left(\mathsf{fma}\left(-1, {\left(maxCos - 1\right)}^{2}, \frac{\mathsf{fma}\left(-2, maxCos, 2\right)}{ux}\right) \cdot ux\right) \cdot ux} \]
  9. Add Preprocessing

Reproduce

?
herbie shell --seed 2025059 
(FPCore (ux uy maxCos)
  :name "UniformSampleCone, y"
  :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)))
  (* (sin (* (* uy 2.0) PI)) (sqrt (- 1.0 (* (+ (- 1.0 ux) (* ux maxCos)) (+ (- 1.0 ux) (* ux maxCos)))))))