UniformSampleCone, y

Percentage Accurate: 57.4% → 98.3%
Time: 17.6s
Alternatives: 16
Speedup: 5.0×

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 16 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.4% 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.7× speedup?

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

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

    \[\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 \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. Step-by-step derivation
    1. lower-*.f32N/A

      \[\leadsto \sin \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)}} \]
    2. cancel-sign-sub-invN/A

      \[\leadsto \sin \left(\left(uy \cdot 2\right) \cdot \mathsf{PI}\left(\right)\right) \cdot \sqrt{ux \cdot \color{blue}{\left(\left(2 + -1 \cdot \left(ux \cdot {\left(maxCos - 1\right)}^{2}\right)\right) + \left(\mathsf{neg}\left(2\right)\right) \cdot maxCos\right)}} \]
    3. +-commutativeN/A

      \[\leadsto \sin \left(\left(uy \cdot 2\right) \cdot \mathsf{PI}\left(\right)\right) \cdot \sqrt{ux \cdot \left(\color{blue}{\left(-1 \cdot \left(ux \cdot {\left(maxCos - 1\right)}^{2}\right) + 2\right)} + \left(\mathsf{neg}\left(2\right)\right) \cdot maxCos\right)} \]
    4. metadata-evalN/A

      \[\leadsto \sin \left(\left(uy \cdot 2\right) \cdot \mathsf{PI}\left(\right)\right) \cdot \sqrt{ux \cdot \left(\left(-1 \cdot \left(ux \cdot {\left(maxCos - 1\right)}^{2}\right) + 2\right) + \color{blue}{-2} \cdot maxCos\right)} \]
    5. associate-+l+N/A

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

      \[\leadsto \sin \left(\left(uy \cdot 2\right) \cdot \mathsf{PI}\left(\right)\right) \cdot \sqrt{ux \cdot \left(\color{blue}{\left(\mathsf{neg}\left(ux \cdot {\left(maxCos - 1\right)}^{2}\right)\right)} + \left(2 + -2 \cdot maxCos\right)\right)} \]
    7. distribute-rgt-neg-inN/A

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

      \[\leadsto \sin \left(\left(uy \cdot 2\right) \cdot \mathsf{PI}\left(\right)\right) \cdot \sqrt{ux \cdot \color{blue}{\mathsf{fma}\left(ux, \mathsf{neg}\left({\left(maxCos - 1\right)}^{2}\right), 2 + -2 \cdot maxCos\right)}} \]
  5. Simplified98.4%

    \[\leadsto \sin \left(\left(uy \cdot 2\right) \cdot \pi\right) \cdot \sqrt{\color{blue}{ux \cdot \mathsf{fma}\left(ux, \left(maxCos + -1\right) \cdot \left(1 - maxCos\right), \mathsf{fma}\left(maxCos, -2, 2\right)\right)}} \]
  6. Applied egg-rr98.6%

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

    \[\leadsto \sin \left(\left(uy \cdot 2\right) \cdot \mathsf{PI}\left(\right)\right) \cdot \sqrt{\frac{\left(\mathsf{fma}\left(ux, \left(maxCos + -1\right) \cdot \left(1 - maxCos\right), \mathsf{fma}\left(maxCos, -2, 2\right)\right) \cdot \color{blue}{\left(\left(-1 \cdot ux + maxCos \cdot \left(2 + \left(-1 \cdot \left(maxCos \cdot ux\right) + 2 \cdot ux\right)\right)\right) - 2\right)}\right) \cdot ux}{\mathsf{fma}\left(maxCos + -1, \mathsf{fma}\left(maxCos, \mathsf{neg}\left(ux\right), ux\right), \mathsf{neg}\left(\mathsf{fma}\left(maxCos, -2, 2\right)\right)\right)}} \]
  8. Step-by-step derivation
    1. sub-negN/A

      \[\leadsto \sin \left(\left(uy \cdot 2\right) \cdot \mathsf{PI}\left(\right)\right) \cdot \sqrt{\frac{\left(\mathsf{fma}\left(ux, \left(maxCos + -1\right) \cdot \left(1 - maxCos\right), \mathsf{fma}\left(maxCos, -2, 2\right)\right) \cdot \color{blue}{\left(\left(-1 \cdot ux + maxCos \cdot \left(2 + \left(-1 \cdot \left(maxCos \cdot ux\right) + 2 \cdot ux\right)\right)\right) + \left(\mathsf{neg}\left(2\right)\right)\right)}\right) \cdot ux}{\mathsf{fma}\left(maxCos + -1, \mathsf{fma}\left(maxCos, \mathsf{neg}\left(ux\right), ux\right), \mathsf{neg}\left(\mathsf{fma}\left(maxCos, -2, 2\right)\right)\right)}} \]
    2. metadata-evalN/A

      \[\leadsto \sin \left(\left(uy \cdot 2\right) \cdot \mathsf{PI}\left(\right)\right) \cdot \sqrt{\frac{\left(\mathsf{fma}\left(ux, \left(maxCos + -1\right) \cdot \left(1 - maxCos\right), \mathsf{fma}\left(maxCos, -2, 2\right)\right) \cdot \left(\left(-1 \cdot ux + maxCos \cdot \left(2 + \left(-1 \cdot \left(maxCos \cdot ux\right) + 2 \cdot ux\right)\right)\right) + \color{blue}{-2}\right)\right) \cdot ux}{\mathsf{fma}\left(maxCos + -1, \mathsf{fma}\left(maxCos, \mathsf{neg}\left(ux\right), ux\right), \mathsf{neg}\left(\mathsf{fma}\left(maxCos, -2, 2\right)\right)\right)}} \]
    3. lower-+.f32N/A

      \[\leadsto \sin \left(\left(uy \cdot 2\right) \cdot \mathsf{PI}\left(\right)\right) \cdot \sqrt{\frac{\left(\mathsf{fma}\left(ux, \left(maxCos + -1\right) \cdot \left(1 - maxCos\right), \mathsf{fma}\left(maxCos, -2, 2\right)\right) \cdot \color{blue}{\left(\left(-1 \cdot ux + maxCos \cdot \left(2 + \left(-1 \cdot \left(maxCos \cdot ux\right) + 2 \cdot ux\right)\right)\right) + -2\right)}\right) \cdot ux}{\mathsf{fma}\left(maxCos + -1, \mathsf{fma}\left(maxCos, \mathsf{neg}\left(ux\right), ux\right), \mathsf{neg}\left(\mathsf{fma}\left(maxCos, -2, 2\right)\right)\right)}} \]
    4. +-commutativeN/A

      \[\leadsto \sin \left(\left(uy \cdot 2\right) \cdot \mathsf{PI}\left(\right)\right) \cdot \sqrt{\frac{\left(\mathsf{fma}\left(ux, \left(maxCos + -1\right) \cdot \left(1 - maxCos\right), \mathsf{fma}\left(maxCos, -2, 2\right)\right) \cdot \left(\color{blue}{\left(maxCos \cdot \left(2 + \left(-1 \cdot \left(maxCos \cdot ux\right) + 2 \cdot ux\right)\right) + -1 \cdot ux\right)} + -2\right)\right) \cdot ux}{\mathsf{fma}\left(maxCos + -1, \mathsf{fma}\left(maxCos, \mathsf{neg}\left(ux\right), ux\right), \mathsf{neg}\left(\mathsf{fma}\left(maxCos, -2, 2\right)\right)\right)}} \]
    5. lower-fma.f32N/A

      \[\leadsto \sin \left(\left(uy \cdot 2\right) \cdot \mathsf{PI}\left(\right)\right) \cdot \sqrt{\frac{\left(\mathsf{fma}\left(ux, \left(maxCos + -1\right) \cdot \left(1 - maxCos\right), \mathsf{fma}\left(maxCos, -2, 2\right)\right) \cdot \left(\color{blue}{\mathsf{fma}\left(maxCos, 2 + \left(-1 \cdot \left(maxCos \cdot ux\right) + 2 \cdot ux\right), -1 \cdot ux\right)} + -2\right)\right) \cdot ux}{\mathsf{fma}\left(maxCos + -1, \mathsf{fma}\left(maxCos, \mathsf{neg}\left(ux\right), ux\right), \mathsf{neg}\left(\mathsf{fma}\left(maxCos, -2, 2\right)\right)\right)}} \]
    6. +-commutativeN/A

      \[\leadsto \sin \left(\left(uy \cdot 2\right) \cdot \mathsf{PI}\left(\right)\right) \cdot \sqrt{\frac{\left(\mathsf{fma}\left(ux, \left(maxCos + -1\right) \cdot \left(1 - maxCos\right), \mathsf{fma}\left(maxCos, -2, 2\right)\right) \cdot \left(\mathsf{fma}\left(maxCos, \color{blue}{\left(-1 \cdot \left(maxCos \cdot ux\right) + 2 \cdot ux\right) + 2}, -1 \cdot ux\right) + -2\right)\right) \cdot ux}{\mathsf{fma}\left(maxCos + -1, \mathsf{fma}\left(maxCos, \mathsf{neg}\left(ux\right), ux\right), \mathsf{neg}\left(\mathsf{fma}\left(maxCos, -2, 2\right)\right)\right)}} \]
    7. +-commutativeN/A

      \[\leadsto \sin \left(\left(uy \cdot 2\right) \cdot \mathsf{PI}\left(\right)\right) \cdot \sqrt{\frac{\left(\mathsf{fma}\left(ux, \left(maxCos + -1\right) \cdot \left(1 - maxCos\right), \mathsf{fma}\left(maxCos, -2, 2\right)\right) \cdot \left(\mathsf{fma}\left(maxCos, \color{blue}{\left(2 \cdot ux + -1 \cdot \left(maxCos \cdot ux\right)\right)} + 2, -1 \cdot ux\right) + -2\right)\right) \cdot ux}{\mathsf{fma}\left(maxCos + -1, \mathsf{fma}\left(maxCos, \mathsf{neg}\left(ux\right), ux\right), \mathsf{neg}\left(\mathsf{fma}\left(maxCos, -2, 2\right)\right)\right)}} \]
    8. associate-*r*N/A

      \[\leadsto \sin \left(\left(uy \cdot 2\right) \cdot \mathsf{PI}\left(\right)\right) \cdot \sqrt{\frac{\left(\mathsf{fma}\left(ux, \left(maxCos + -1\right) \cdot \left(1 - maxCos\right), \mathsf{fma}\left(maxCos, -2, 2\right)\right) \cdot \left(\mathsf{fma}\left(maxCos, \left(2 \cdot ux + \color{blue}{\left(-1 \cdot maxCos\right) \cdot ux}\right) + 2, -1 \cdot ux\right) + -2\right)\right) \cdot ux}{\mathsf{fma}\left(maxCos + -1, \mathsf{fma}\left(maxCos, \mathsf{neg}\left(ux\right), ux\right), \mathsf{neg}\left(\mathsf{fma}\left(maxCos, -2, 2\right)\right)\right)}} \]
    9. distribute-rgt-outN/A

      \[\leadsto \sin \left(\left(uy \cdot 2\right) \cdot \mathsf{PI}\left(\right)\right) \cdot \sqrt{\frac{\left(\mathsf{fma}\left(ux, \left(maxCos + -1\right) \cdot \left(1 - maxCos\right), \mathsf{fma}\left(maxCos, -2, 2\right)\right) \cdot \left(\mathsf{fma}\left(maxCos, \color{blue}{ux \cdot \left(2 + -1 \cdot maxCos\right)} + 2, -1 \cdot ux\right) + -2\right)\right) \cdot ux}{\mathsf{fma}\left(maxCos + -1, \mathsf{fma}\left(maxCos, \mathsf{neg}\left(ux\right), ux\right), \mathsf{neg}\left(\mathsf{fma}\left(maxCos, -2, 2\right)\right)\right)}} \]
    10. lower-fma.f32N/A

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

      \[\leadsto \sin \left(\left(uy \cdot 2\right) \cdot \mathsf{PI}\left(\right)\right) \cdot \sqrt{\frac{\left(\mathsf{fma}\left(ux, \left(maxCos + -1\right) \cdot \left(1 - maxCos\right), \mathsf{fma}\left(maxCos, -2, 2\right)\right) \cdot \left(\mathsf{fma}\left(maxCos, \mathsf{fma}\left(ux, \color{blue}{2 + -1 \cdot maxCos}, 2\right), -1 \cdot ux\right) + -2\right)\right) \cdot ux}{\mathsf{fma}\left(maxCos + -1, \mathsf{fma}\left(maxCos, \mathsf{neg}\left(ux\right), ux\right), \mathsf{neg}\left(\mathsf{fma}\left(maxCos, -2, 2\right)\right)\right)}} \]
    12. mul-1-negN/A

      \[\leadsto \sin \left(\left(uy \cdot 2\right) \cdot \mathsf{PI}\left(\right)\right) \cdot \sqrt{\frac{\left(\mathsf{fma}\left(ux, \left(maxCos + -1\right) \cdot \left(1 - maxCos\right), \mathsf{fma}\left(maxCos, -2, 2\right)\right) \cdot \left(\mathsf{fma}\left(maxCos, \mathsf{fma}\left(ux, 2 + \color{blue}{\left(\mathsf{neg}\left(maxCos\right)\right)}, 2\right), -1 \cdot ux\right) + -2\right)\right) \cdot ux}{\mathsf{fma}\left(maxCos + -1, \mathsf{fma}\left(maxCos, \mathsf{neg}\left(ux\right), ux\right), \mathsf{neg}\left(\mathsf{fma}\left(maxCos, -2, 2\right)\right)\right)}} \]
    13. lower-neg.f32N/A

      \[\leadsto \sin \left(\left(uy \cdot 2\right) \cdot \mathsf{PI}\left(\right)\right) \cdot \sqrt{\frac{\left(\mathsf{fma}\left(ux, \left(maxCos + -1\right) \cdot \left(1 - maxCos\right), \mathsf{fma}\left(maxCos, -2, 2\right)\right) \cdot \left(\mathsf{fma}\left(maxCos, \mathsf{fma}\left(ux, 2 + \color{blue}{\left(\mathsf{neg}\left(maxCos\right)\right)}, 2\right), -1 \cdot ux\right) + -2\right)\right) \cdot ux}{\mathsf{fma}\left(maxCos + -1, \mathsf{fma}\left(maxCos, \mathsf{neg}\left(ux\right), ux\right), \mathsf{neg}\left(\mathsf{fma}\left(maxCos, -2, 2\right)\right)\right)}} \]
    14. mul-1-negN/A

      \[\leadsto \sin \left(\left(uy \cdot 2\right) \cdot \mathsf{PI}\left(\right)\right) \cdot \sqrt{\frac{\left(\mathsf{fma}\left(ux, \left(maxCos + -1\right) \cdot \left(1 - maxCos\right), \mathsf{fma}\left(maxCos, -2, 2\right)\right) \cdot \left(\mathsf{fma}\left(maxCos, \mathsf{fma}\left(ux, 2 + \left(\mathsf{neg}\left(maxCos\right)\right), 2\right), \color{blue}{\mathsf{neg}\left(ux\right)}\right) + -2\right)\right) \cdot ux}{\mathsf{fma}\left(maxCos + -1, \mathsf{fma}\left(maxCos, \mathsf{neg}\left(ux\right), ux\right), \mathsf{neg}\left(\mathsf{fma}\left(maxCos, -2, 2\right)\right)\right)}} \]
    15. lower-neg.f3298.6

      \[\leadsto \sin \left(\left(uy \cdot 2\right) \cdot \pi\right) \cdot \sqrt{\frac{\left(\mathsf{fma}\left(ux, \left(maxCos + -1\right) \cdot \left(1 - maxCos\right), \mathsf{fma}\left(maxCos, -2, 2\right)\right) \cdot \left(\mathsf{fma}\left(maxCos, \mathsf{fma}\left(ux, 2 + \left(-maxCos\right), 2\right), \color{blue}{-ux}\right) + -2\right)\right) \cdot ux}{\mathsf{fma}\left(maxCos + -1, \mathsf{fma}\left(maxCos, -ux, ux\right), -\mathsf{fma}\left(maxCos, -2, 2\right)\right)}} \]
  9. Simplified98.6%

    \[\leadsto \sin \left(\left(uy \cdot 2\right) \cdot \pi\right) \cdot \sqrt{\frac{\left(\mathsf{fma}\left(ux, \left(maxCos + -1\right) \cdot \left(1 - maxCos\right), \mathsf{fma}\left(maxCos, -2, 2\right)\right) \cdot \color{blue}{\left(\mathsf{fma}\left(maxCos, \mathsf{fma}\left(ux, 2 + \left(-maxCos\right), 2\right), -ux\right) + -2\right)}\right) \cdot ux}{\mathsf{fma}\left(maxCos + -1, \mathsf{fma}\left(maxCos, -ux, ux\right), -\mathsf{fma}\left(maxCos, -2, 2\right)\right)}} \]
  10. Final simplification98.6%

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

Alternative 2: 98.3% accurate, 1.0× speedup?

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

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

    \[\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 \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. Step-by-step derivation
    1. lower-*.f32N/A

      \[\leadsto \sin \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)}} \]
    2. cancel-sign-sub-invN/A

      \[\leadsto \sin \left(\left(uy \cdot 2\right) \cdot \mathsf{PI}\left(\right)\right) \cdot \sqrt{ux \cdot \color{blue}{\left(\left(2 + -1 \cdot \left(ux \cdot {\left(maxCos - 1\right)}^{2}\right)\right) + \left(\mathsf{neg}\left(2\right)\right) \cdot maxCos\right)}} \]
    3. +-commutativeN/A

      \[\leadsto \sin \left(\left(uy \cdot 2\right) \cdot \mathsf{PI}\left(\right)\right) \cdot \sqrt{ux \cdot \left(\color{blue}{\left(-1 \cdot \left(ux \cdot {\left(maxCos - 1\right)}^{2}\right) + 2\right)} + \left(\mathsf{neg}\left(2\right)\right) \cdot maxCos\right)} \]
    4. metadata-evalN/A

      \[\leadsto \sin \left(\left(uy \cdot 2\right) \cdot \mathsf{PI}\left(\right)\right) \cdot \sqrt{ux \cdot \left(\left(-1 \cdot \left(ux \cdot {\left(maxCos - 1\right)}^{2}\right) + 2\right) + \color{blue}{-2} \cdot maxCos\right)} \]
    5. associate-+l+N/A

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

      \[\leadsto \sin \left(\left(uy \cdot 2\right) \cdot \mathsf{PI}\left(\right)\right) \cdot \sqrt{ux \cdot \left(\color{blue}{\left(\mathsf{neg}\left(ux \cdot {\left(maxCos - 1\right)}^{2}\right)\right)} + \left(2 + -2 \cdot maxCos\right)\right)} \]
    7. distribute-rgt-neg-inN/A

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

      \[\leadsto \sin \left(\left(uy \cdot 2\right) \cdot \mathsf{PI}\left(\right)\right) \cdot \sqrt{ux \cdot \color{blue}{\mathsf{fma}\left(ux, \mathsf{neg}\left({\left(maxCos - 1\right)}^{2}\right), 2 + -2 \cdot maxCos\right)}} \]
  5. Simplified98.3%

    \[\leadsto \sin \left(\left(uy \cdot 2\right) \cdot \pi\right) \cdot \sqrt{\color{blue}{ux \cdot \mathsf{fma}\left(ux, \left(maxCos + -1\right) \cdot \left(1 - maxCos\right), \mathsf{fma}\left(maxCos, -2, 2\right)\right)}} \]
  6. Add Preprocessing

Reproduce

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