Beckmann Sample, near normal, slope_x

Percentage Accurate: 57.5% → 99.0%
Time: 10.9s
Alternatives: 10
Speedup: 3.1×

Specification

?
\[\left(\left(cosTheta\_i > 0.9999 \land cosTheta\_i \leq 1\right) \land \left(2.328306437 \cdot 10^{-10} \leq u1 \land u1 \leq 1\right)\right) \land \left(2.328306437 \cdot 10^{-10} \leq u2 \land u2 \leq 1\right)\]
\[\begin{array}{l} \\ \sqrt{-\log \left(1 - u1\right)} \cdot \cos \left(\left(2 \cdot \pi\right) \cdot u2\right) \end{array} \]
(FPCore (cosTheta_i u1 u2)
 :precision binary32
 (* (sqrt (- (log (- 1.0 u1)))) (cos (* (* 2.0 PI) u2))))
float code(float cosTheta_i, float u1, float u2) {
	return sqrtf(-logf((1.0f - u1))) * cosf(((2.0f * ((float) M_PI)) * u2));
}
function code(cosTheta_i, u1, u2)
	return Float32(sqrt(Float32(-log(Float32(Float32(1.0) - u1)))) * cos(Float32(Float32(Float32(2.0) * Float32(pi)) * u2)))
end
function tmp = code(cosTheta_i, u1, u2)
	tmp = sqrt(-log((single(1.0) - u1))) * cos(((single(2.0) * single(pi)) * u2));
end
\begin{array}{l}

\\
\sqrt{-\log \left(1 - u1\right)} \cdot \cos \left(\left(2 \cdot \pi\right) \cdot u2\right)
\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 10 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} \\ \sqrt{-\log \left(1 - u1\right)} \cdot \cos \left(\left(2 \cdot \pi\right) \cdot u2\right) \end{array} \]
(FPCore (cosTheta_i u1 u2)
 :precision binary32
 (* (sqrt (- (log (- 1.0 u1)))) (cos (* (* 2.0 PI) u2))))
float code(float cosTheta_i, float u1, float u2) {
	return sqrtf(-logf((1.0f - u1))) * cosf(((2.0f * ((float) M_PI)) * u2));
}
function code(cosTheta_i, u1, u2)
	return Float32(sqrt(Float32(-log(Float32(Float32(1.0) - u1)))) * cos(Float32(Float32(Float32(2.0) * Float32(pi)) * u2)))
end
function tmp = code(cosTheta_i, u1, u2)
	tmp = sqrt(-log((single(1.0) - u1))) * cos(((single(2.0) * single(pi)) * u2));
end
\begin{array}{l}

\\
\sqrt{-\log \left(1 - u1\right)} \cdot \cos \left(\left(2 \cdot \pi\right) \cdot u2\right)
\end{array}

Alternative 1: 99.0% accurate, 1.0× speedup?

\[\begin{array}{l} \\ \cos \left(\left(2 \cdot \pi\right) \cdot u2\right) \cdot \sqrt{-\mathsf{log1p}\left(-u1\right)} \end{array} \]
(FPCore (cosTheta_i u1 u2)
 :precision binary32
 (* (cos (* (* 2.0 PI) u2)) (sqrt (- (log1p (- u1))))))
float code(float cosTheta_i, float u1, float u2) {
	return cosf(((2.0f * ((float) M_PI)) * u2)) * sqrtf(-log1pf(-u1));
}
function code(cosTheta_i, u1, u2)
	return Float32(cos(Float32(Float32(Float32(2.0) * Float32(pi)) * u2)) * sqrt(Float32(-log1p(Float32(-u1)))))
end
\begin{array}{l}

\\
\cos \left(\left(2 \cdot \pi\right) \cdot u2\right) \cdot \sqrt{-\mathsf{log1p}\left(-u1\right)}
\end{array}
Derivation
  1. Initial program 57.6%

    \[\sqrt{-\log \left(1 - u1\right)} \cdot \cos \left(\left(2 \cdot \pi\right) \cdot u2\right) \]
  2. Step-by-step derivation
    1. sub-neg57.6%

      \[\leadsto \sqrt{-\log \color{blue}{\left(1 + \left(-u1\right)\right)}} \cdot \cos \left(\left(2 \cdot \pi\right) \cdot u2\right) \]
    2. log1p-define99.0%

      \[\leadsto \sqrt{-\color{blue}{\mathsf{log1p}\left(-u1\right)}} \cdot \cos \left(\left(2 \cdot \pi\right) \cdot u2\right) \]
  3. Simplified99.0%

    \[\leadsto \color{blue}{\sqrt{-\mathsf{log1p}\left(-u1\right)} \cdot \cos \left(\left(2 \cdot \pi\right) \cdot u2\right)} \]
  4. Add Preprocessing
  5. Final simplification99.0%

    \[\leadsto \cos \left(\left(2 \cdot \pi\right) \cdot u2\right) \cdot \sqrt{-\mathsf{log1p}\left(-u1\right)} \]
  6. Add Preprocessing

Alternative 2: 96.1% accurate, 1.0× speedup?

\[\begin{array}{l} \\ \begin{array}{l} t_0 := \cos \left(\left(2 \cdot \pi\right) \cdot u2\right)\\ \mathbf{if}\;t\_0 \leq 0.9999988079071045:\\ \;\;\;\;t\_0 \cdot \sqrt{u1 \cdot \left(1 + u1 \cdot \left(0.5 + u1 \cdot 0.3333333333333333\right)\right)}\\ \mathbf{else}:\\ \;\;\;\;\sqrt{-\mathsf{log1p}\left(-u1\right)}\\ \end{array} \end{array} \]
(FPCore (cosTheta_i u1 u2)
 :precision binary32
 (let* ((t_0 (cos (* (* 2.0 PI) u2))))
   (if (<= t_0 0.9999988079071045)
     (* t_0 (sqrt (* u1 (+ 1.0 (* u1 (+ 0.5 (* u1 0.3333333333333333)))))))
     (sqrt (- (log1p (- u1)))))))
float code(float cosTheta_i, float u1, float u2) {
	float t_0 = cosf(((2.0f * ((float) M_PI)) * u2));
	float tmp;
	if (t_0 <= 0.9999988079071045f) {
		tmp = t_0 * sqrtf((u1 * (1.0f + (u1 * (0.5f + (u1 * 0.3333333333333333f))))));
	} else {
		tmp = sqrtf(-log1pf(-u1));
	}
	return tmp;
}
function code(cosTheta_i, u1, u2)
	t_0 = cos(Float32(Float32(Float32(2.0) * Float32(pi)) * u2))
	tmp = Float32(0.0)
	if (t_0 <= Float32(0.9999988079071045))
		tmp = Float32(t_0 * sqrt(Float32(u1 * Float32(Float32(1.0) + Float32(u1 * Float32(Float32(0.5) + Float32(u1 * Float32(0.3333333333333333))))))));
	else
		tmp = sqrt(Float32(-log1p(Float32(-u1))));
	end
	return tmp
end
\begin{array}{l}

\\
\begin{array}{l}
t_0 := \cos \left(\left(2 \cdot \pi\right) \cdot u2\right)\\
\mathbf{if}\;t\_0 \leq 0.9999988079071045:\\
\;\;\;\;t\_0 \cdot \sqrt{u1 \cdot \left(1 + u1 \cdot \left(0.5 + u1 \cdot 0.3333333333333333\right)\right)}\\

\mathbf{else}:\\
\;\;\;\;\sqrt{-\mathsf{log1p}\left(-u1\right)}\\


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if (cos.f32 (*.f32 (*.f32 #s(literal 2 binary32) (PI.f32)) u2)) < 0.999998808

    1. Initial program 59.0%

      \[\sqrt{-\log \left(1 - u1\right)} \cdot \cos \left(\left(2 \cdot \pi\right) \cdot u2\right) \]
    2. Step-by-step derivation
      1. sub-neg59.0%

        \[\leadsto \sqrt{-\log \color{blue}{\left(1 + \left(-u1\right)\right)}} \cdot \cos \left(\left(2 \cdot \pi\right) \cdot u2\right) \]
      2. log1p-define98.1%

        \[\leadsto \sqrt{-\color{blue}{\mathsf{log1p}\left(-u1\right)}} \cdot \cos \left(\left(2 \cdot \pi\right) \cdot u2\right) \]
    3. Simplified98.1%

      \[\leadsto \color{blue}{\sqrt{-\mathsf{log1p}\left(-u1\right)} \cdot \cos \left(\left(2 \cdot \pi\right) \cdot u2\right)} \]
    4. Add Preprocessing
    5. Taylor expanded in u1 around 0 89.9%

      \[\leadsto \sqrt{\color{blue}{u1 \cdot \left(1 + u1 \cdot \left(0.5 + 0.3333333333333333 \cdot u1\right)\right)}} \cdot \cos \left(\left(2 \cdot \pi\right) \cdot u2\right) \]
    6. Step-by-step derivation
      1. *-commutative89.9%

        \[\leadsto \sqrt{u1 \cdot \left(1 + u1 \cdot \left(0.5 + \color{blue}{u1 \cdot 0.3333333333333333}\right)\right)} \cdot \cos \left(\left(2 \cdot \pi\right) \cdot u2\right) \]
    7. Simplified89.9%

      \[\leadsto \sqrt{\color{blue}{u1 \cdot \left(1 + u1 \cdot \left(0.5 + u1 \cdot 0.3333333333333333\right)\right)}} \cdot \cos \left(\left(2 \cdot \pi\right) \cdot u2\right) \]

    if 0.999998808 < (cos.f32 (*.f32 (*.f32 #s(literal 2 binary32) (PI.f32)) u2))

    1. Initial program 56.8%

      \[\sqrt{-\log \left(1 - u1\right)} \cdot \cos \left(\left(2 \cdot \pi\right) \cdot u2\right) \]
    2. Step-by-step derivation
      1. sub-neg56.8%

        \[\leadsto \sqrt{-\log \color{blue}{\left(1 + \left(-u1\right)\right)}} \cdot \cos \left(\left(2 \cdot \pi\right) \cdot u2\right) \]
      2. log1p-define99.5%

        \[\leadsto \sqrt{-\color{blue}{\mathsf{log1p}\left(-u1\right)}} \cdot \cos \left(\left(2 \cdot \pi\right) \cdot u2\right) \]
    3. Simplified99.5%

      \[\leadsto \color{blue}{\sqrt{-\mathsf{log1p}\left(-u1\right)} \cdot \cos \left(\left(2 \cdot \pi\right) \cdot u2\right)} \]
    4. Add Preprocessing
    5. Taylor expanded in u2 around 0 98.8%

      \[\leadsto \sqrt{-\mathsf{log1p}\left(-u1\right)} \cdot \color{blue}{1} \]
  3. Recombined 2 regimes into one program.
  4. Final simplification95.6%

    \[\leadsto \begin{array}{l} \mathbf{if}\;\cos \left(\left(2 \cdot \pi\right) \cdot u2\right) \leq 0.9999988079071045:\\ \;\;\;\;\cos \left(\left(2 \cdot \pi\right) \cdot u2\right) \cdot \sqrt{u1 \cdot \left(1 + u1 \cdot \left(0.5 + u1 \cdot 0.3333333333333333\right)\right)}\\ \mathbf{else}:\\ \;\;\;\;\sqrt{-\mathsf{log1p}\left(-u1\right)}\\ \end{array} \]
  5. Add Preprocessing

Alternative 3: 94.7% accurate, 1.0× speedup?

\[\begin{array}{l} \\ \begin{array}{l} t_0 := \cos \left(\left(2 \cdot \pi\right) \cdot u2\right)\\ \mathbf{if}\;t\_0 \leq 0.9999988079071045:\\ \;\;\;\;t\_0 \cdot \sqrt{u1 \cdot \left(1 + u1 \cdot 0.5\right)}\\ \mathbf{else}:\\ \;\;\;\;\sqrt{-\mathsf{log1p}\left(-u1\right)}\\ \end{array} \end{array} \]
(FPCore (cosTheta_i u1 u2)
 :precision binary32
 (let* ((t_0 (cos (* (* 2.0 PI) u2))))
   (if (<= t_0 0.9999988079071045)
     (* t_0 (sqrt (* u1 (+ 1.0 (* u1 0.5)))))
     (sqrt (- (log1p (- u1)))))))
float code(float cosTheta_i, float u1, float u2) {
	float t_0 = cosf(((2.0f * ((float) M_PI)) * u2));
	float tmp;
	if (t_0 <= 0.9999988079071045f) {
		tmp = t_0 * sqrtf((u1 * (1.0f + (u1 * 0.5f))));
	} else {
		tmp = sqrtf(-log1pf(-u1));
	}
	return tmp;
}
function code(cosTheta_i, u1, u2)
	t_0 = cos(Float32(Float32(Float32(2.0) * Float32(pi)) * u2))
	tmp = Float32(0.0)
	if (t_0 <= Float32(0.9999988079071045))
		tmp = Float32(t_0 * sqrt(Float32(u1 * Float32(Float32(1.0) + Float32(u1 * Float32(0.5))))));
	else
		tmp = sqrt(Float32(-log1p(Float32(-u1))));
	end
	return tmp
end
\begin{array}{l}

\\
\begin{array}{l}
t_0 := \cos \left(\left(2 \cdot \pi\right) \cdot u2\right)\\
\mathbf{if}\;t\_0 \leq 0.9999988079071045:\\
\;\;\;\;t\_0 \cdot \sqrt{u1 \cdot \left(1 + u1 \cdot 0.5\right)}\\

\mathbf{else}:\\
\;\;\;\;\sqrt{-\mathsf{log1p}\left(-u1\right)}\\


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if (cos.f32 (*.f32 (*.f32 #s(literal 2 binary32) (PI.f32)) u2)) < 0.999998808

    1. Initial program 59.0%

      \[\sqrt{-\log \left(1 - u1\right)} \cdot \cos \left(\left(2 \cdot \pi\right) \cdot u2\right) \]
    2. Step-by-step derivation
      1. sub-neg59.0%

        \[\leadsto \sqrt{-\log \color{blue}{\left(1 + \left(-u1\right)\right)}} \cdot \cos \left(\left(2 \cdot \pi\right) \cdot u2\right) \]
      2. log1p-define98.1%

        \[\leadsto \sqrt{-\color{blue}{\mathsf{log1p}\left(-u1\right)}} \cdot \cos \left(\left(2 \cdot \pi\right) \cdot u2\right) \]
    3. Simplified98.1%

      \[\leadsto \color{blue}{\sqrt{-\mathsf{log1p}\left(-u1\right)} \cdot \cos \left(\left(2 \cdot \pi\right) \cdot u2\right)} \]
    4. Add Preprocessing
    5. Taylor expanded in u1 around 0 86.2%

      \[\leadsto \sqrt{\color{blue}{u1 \cdot \left(1 + 0.5 \cdot u1\right)}} \cdot \cos \left(\left(2 \cdot \pi\right) \cdot u2\right) \]
    6. Step-by-step derivation
      1. *-commutative86.2%

        \[\leadsto \sqrt{u1 \cdot \left(1 + \color{blue}{u1 \cdot 0.5}\right)} \cdot \cos \left(\left(2 \cdot \pi\right) \cdot u2\right) \]
    7. Simplified86.2%

      \[\leadsto \sqrt{\color{blue}{u1 \cdot \left(1 + u1 \cdot 0.5\right)}} \cdot \cos \left(\left(2 \cdot \pi\right) \cdot u2\right) \]

    if 0.999998808 < (cos.f32 (*.f32 (*.f32 #s(literal 2 binary32) (PI.f32)) u2))

    1. Initial program 56.8%

      \[\sqrt{-\log \left(1 - u1\right)} \cdot \cos \left(\left(2 \cdot \pi\right) \cdot u2\right) \]
    2. Step-by-step derivation
      1. sub-neg56.8%

        \[\leadsto \sqrt{-\log \color{blue}{\left(1 + \left(-u1\right)\right)}} \cdot \cos \left(\left(2 \cdot \pi\right) \cdot u2\right) \]
      2. log1p-define99.5%

        \[\leadsto \sqrt{-\color{blue}{\mathsf{log1p}\left(-u1\right)}} \cdot \cos \left(\left(2 \cdot \pi\right) \cdot u2\right) \]
    3. Simplified99.5%

      \[\leadsto \color{blue}{\sqrt{-\mathsf{log1p}\left(-u1\right)} \cdot \cos \left(\left(2 \cdot \pi\right) \cdot u2\right)} \]
    4. Add Preprocessing
    5. Taylor expanded in u2 around 0 98.8%

      \[\leadsto \sqrt{-\mathsf{log1p}\left(-u1\right)} \cdot \color{blue}{1} \]
  3. Recombined 2 regimes into one program.
  4. Final simplification94.3%

    \[\leadsto \begin{array}{l} \mathbf{if}\;\cos \left(\left(2 \cdot \pi\right) \cdot u2\right) \leq 0.9999988079071045:\\ \;\;\;\;\cos \left(\left(2 \cdot \pi\right) \cdot u2\right) \cdot \sqrt{u1 \cdot \left(1 + u1 \cdot 0.5\right)}\\ \mathbf{else}:\\ \;\;\;\;\sqrt{-\mathsf{log1p}\left(-u1\right)}\\ \end{array} \]
  5. Add Preprocessing

Alternative 4: 93.9% accurate, 1.4× speedup?

\[\begin{array}{l} \\ \cos \left(\left(2 \cdot \pi\right) \cdot u2\right) \cdot \sqrt{u1 \cdot \left(1 + u1 \cdot \left(0.5 + u1 \cdot \left(0.3333333333333333 + u1 \cdot 0.25\right)\right)\right)} \end{array} \]
(FPCore (cosTheta_i u1 u2)
 :precision binary32
 (*
  (cos (* (* 2.0 PI) u2))
  (sqrt
   (* u1 (+ 1.0 (* u1 (+ 0.5 (* u1 (+ 0.3333333333333333 (* u1 0.25))))))))))
float code(float cosTheta_i, float u1, float u2) {
	return cosf(((2.0f * ((float) M_PI)) * u2)) * sqrtf((u1 * (1.0f + (u1 * (0.5f + (u1 * (0.3333333333333333f + (u1 * 0.25f))))))));
}
function code(cosTheta_i, u1, u2)
	return Float32(cos(Float32(Float32(Float32(2.0) * Float32(pi)) * u2)) * sqrt(Float32(u1 * Float32(Float32(1.0) + Float32(u1 * Float32(Float32(0.5) + Float32(u1 * Float32(Float32(0.3333333333333333) + Float32(u1 * Float32(0.25))))))))))
end
function tmp = code(cosTheta_i, u1, u2)
	tmp = cos(((single(2.0) * single(pi)) * u2)) * sqrt((u1 * (single(1.0) + (u1 * (single(0.5) + (u1 * (single(0.3333333333333333) + (u1 * single(0.25)))))))));
end
\begin{array}{l}

\\
\cos \left(\left(2 \cdot \pi\right) \cdot u2\right) \cdot \sqrt{u1 \cdot \left(1 + u1 \cdot \left(0.5 + u1 \cdot \left(0.3333333333333333 + u1 \cdot 0.25\right)\right)\right)}
\end{array}
Derivation
  1. Initial program 57.6%

    \[\sqrt{-\log \left(1 - u1\right)} \cdot \cos \left(\left(2 \cdot \pi\right) \cdot u2\right) \]
  2. Step-by-step derivation
    1. sub-neg57.6%

      \[\leadsto \sqrt{-\log \color{blue}{\left(1 + \left(-u1\right)\right)}} \cdot \cos \left(\left(2 \cdot \pi\right) \cdot u2\right) \]
    2. log1p-define99.0%

      \[\leadsto \sqrt{-\color{blue}{\mathsf{log1p}\left(-u1\right)}} \cdot \cos \left(\left(2 \cdot \pi\right) \cdot u2\right) \]
  3. Simplified99.0%

    \[\leadsto \color{blue}{\sqrt{-\mathsf{log1p}\left(-u1\right)} \cdot \cos \left(\left(2 \cdot \pi\right) \cdot u2\right)} \]
  4. Add Preprocessing
  5. Taylor expanded in u1 around 0 93.8%

    \[\leadsto \sqrt{\color{blue}{u1 \cdot \left(1 + u1 \cdot \left(0.5 + u1 \cdot \left(0.3333333333333333 + 0.25 \cdot u1\right)\right)\right)}} \cdot \cos \left(\left(2 \cdot \pi\right) \cdot u2\right) \]
  6. Step-by-step derivation
    1. *-commutative93.8%

      \[\leadsto \sqrt{u1 \cdot \left(1 + u1 \cdot \left(0.5 + u1 \cdot \left(0.3333333333333333 + \color{blue}{u1 \cdot 0.25}\right)\right)\right)} \cdot \cos \left(\left(2 \cdot \pi\right) \cdot u2\right) \]
  7. Simplified93.8%

    \[\leadsto \sqrt{\color{blue}{u1 \cdot \left(1 + u1 \cdot \left(0.5 + u1 \cdot \left(0.3333333333333333 + u1 \cdot 0.25\right)\right)\right)}} \cdot \cos \left(\left(2 \cdot \pi\right) \cdot u2\right) \]
  8. Final simplification93.8%

    \[\leadsto \cos \left(\left(2 \cdot \pi\right) \cdot u2\right) \cdot \sqrt{u1 \cdot \left(1 + u1 \cdot \left(0.5 + u1 \cdot \left(0.3333333333333333 + u1 \cdot 0.25\right)\right)\right)} \]
  9. Add Preprocessing

Alternative 5: 90.5% accurate, 1.4× speedup?

\[\begin{array}{l} \\ \begin{array}{l} t_0 := \left(2 \cdot \pi\right) \cdot u2\\ \mathbf{if}\;t\_0 \leq 0.012000000104308128:\\ \;\;\;\;\sqrt{-\mathsf{log1p}\left(-u1\right)}\\ \mathbf{else}:\\ \;\;\;\;\cos t\_0 \cdot \sqrt{u1}\\ \end{array} \end{array} \]
(FPCore (cosTheta_i u1 u2)
 :precision binary32
 (let* ((t_0 (* (* 2.0 PI) u2)))
   (if (<= t_0 0.012000000104308128)
     (sqrt (- (log1p (- u1))))
     (* (cos t_0) (sqrt u1)))))
float code(float cosTheta_i, float u1, float u2) {
	float t_0 = (2.0f * ((float) M_PI)) * u2;
	float tmp;
	if (t_0 <= 0.012000000104308128f) {
		tmp = sqrtf(-log1pf(-u1));
	} else {
		tmp = cosf(t_0) * sqrtf(u1);
	}
	return tmp;
}
function code(cosTheta_i, u1, u2)
	t_0 = Float32(Float32(Float32(2.0) * Float32(pi)) * u2)
	tmp = Float32(0.0)
	if (t_0 <= Float32(0.012000000104308128))
		tmp = sqrt(Float32(-log1p(Float32(-u1))));
	else
		tmp = Float32(cos(t_0) * sqrt(u1));
	end
	return tmp
end
\begin{array}{l}

\\
\begin{array}{l}
t_0 := \left(2 \cdot \pi\right) \cdot u2\\
\mathbf{if}\;t\_0 \leq 0.012000000104308128:\\
\;\;\;\;\sqrt{-\mathsf{log1p}\left(-u1\right)}\\

\mathbf{else}:\\
\;\;\;\;\cos t\_0 \cdot \sqrt{u1}\\


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if (*.f32 (*.f32 #s(literal 2 binary32) (PI.f32)) u2) < 0.0120000001

    1. Initial program 57.2%

      \[\sqrt{-\log \left(1 - u1\right)} \cdot \cos \left(\left(2 \cdot \pi\right) \cdot u2\right) \]
    2. Step-by-step derivation
      1. sub-neg57.2%

        \[\leadsto \sqrt{-\log \color{blue}{\left(1 + \left(-u1\right)\right)}} \cdot \cos \left(\left(2 \cdot \pi\right) \cdot u2\right) \]
      2. log1p-define99.4%

        \[\leadsto \sqrt{-\color{blue}{\mathsf{log1p}\left(-u1\right)}} \cdot \cos \left(\left(2 \cdot \pi\right) \cdot u2\right) \]
    3. Simplified99.4%

      \[\leadsto \color{blue}{\sqrt{-\mathsf{log1p}\left(-u1\right)} \cdot \cos \left(\left(2 \cdot \pi\right) \cdot u2\right)} \]
    4. Add Preprocessing
    5. Taylor expanded in u2 around 0 96.5%

      \[\leadsto \sqrt{-\mathsf{log1p}\left(-u1\right)} \cdot \color{blue}{1} \]

    if 0.0120000001 < (*.f32 (*.f32 #s(literal 2 binary32) (PI.f32)) u2)

    1. Initial program 58.5%

      \[\sqrt{-\log \left(1 - u1\right)} \cdot \cos \left(\left(2 \cdot \pi\right) \cdot u2\right) \]
    2. Step-by-step derivation
      1. sub-neg58.5%

        \[\leadsto \sqrt{-\log \color{blue}{\left(1 + \left(-u1\right)\right)}} \cdot \cos \left(\left(2 \cdot \pi\right) \cdot u2\right) \]
      2. log1p-define97.8%

        \[\leadsto \sqrt{-\color{blue}{\mathsf{log1p}\left(-u1\right)}} \cdot \cos \left(\left(2 \cdot \pi\right) \cdot u2\right) \]
    3. Simplified97.8%

      \[\leadsto \color{blue}{\sqrt{-\mathsf{log1p}\left(-u1\right)} \cdot \cos \left(\left(2 \cdot \pi\right) \cdot u2\right)} \]
    4. Add Preprocessing
    5. Taylor expanded in u1 around 0 74.6%

      \[\leadsto \sqrt{\color{blue}{u1}} \cdot \cos \left(\left(2 \cdot \pi\right) \cdot u2\right) \]
  3. Recombined 2 regimes into one program.
  4. Final simplification90.6%

    \[\leadsto \begin{array}{l} \mathbf{if}\;\left(2 \cdot \pi\right) \cdot u2 \leq 0.012000000104308128:\\ \;\;\;\;\sqrt{-\mathsf{log1p}\left(-u1\right)}\\ \mathbf{else}:\\ \;\;\;\;\cos \left(\left(2 \cdot \pi\right) \cdot u2\right) \cdot \sqrt{u1}\\ \end{array} \]
  5. Add Preprocessing

Alternative 6: 79.9% accurate, 1.5× speedup?

\[\begin{array}{l} \\ \sqrt{-\mathsf{log1p}\left(-u1\right)} \end{array} \]
(FPCore (cosTheta_i u1 u2) :precision binary32 (sqrt (- (log1p (- u1)))))
float code(float cosTheta_i, float u1, float u2) {
	return sqrtf(-log1pf(-u1));
}
function code(cosTheta_i, u1, u2)
	return sqrt(Float32(-log1p(Float32(-u1))))
end
\begin{array}{l}

\\
\sqrt{-\mathsf{log1p}\left(-u1\right)}
\end{array}
Derivation
  1. Initial program 57.6%

    \[\sqrt{-\log \left(1 - u1\right)} \cdot \cos \left(\left(2 \cdot \pi\right) \cdot u2\right) \]
  2. Step-by-step derivation
    1. sub-neg57.6%

      \[\leadsto \sqrt{-\log \color{blue}{\left(1 + \left(-u1\right)\right)}} \cdot \cos \left(\left(2 \cdot \pi\right) \cdot u2\right) \]
    2. log1p-define99.0%

      \[\leadsto \sqrt{-\color{blue}{\mathsf{log1p}\left(-u1\right)}} \cdot \cos \left(\left(2 \cdot \pi\right) \cdot u2\right) \]
  3. Simplified99.0%

    \[\leadsto \color{blue}{\sqrt{-\mathsf{log1p}\left(-u1\right)} \cdot \cos \left(\left(2 \cdot \pi\right) \cdot u2\right)} \]
  4. Add Preprocessing
  5. Taylor expanded in u2 around 0 80.1%

    \[\leadsto \sqrt{-\mathsf{log1p}\left(-u1\right)} \cdot \color{blue}{1} \]
  6. Final simplification80.1%

    \[\leadsto \sqrt{-\mathsf{log1p}\left(-u1\right)} \]
  7. Add Preprocessing

Alternative 7: 76.4% accurate, 2.7× speedup?

\[\begin{array}{l} \\ \sqrt{u1 \cdot \left(1 + u1 \cdot \left(0.5 + u1 \cdot \left(0.3333333333333333 + u1 \cdot 0.25\right)\right)\right)} \end{array} \]
(FPCore (cosTheta_i u1 u2)
 :precision binary32
 (sqrt
  (* u1 (+ 1.0 (* u1 (+ 0.5 (* u1 (+ 0.3333333333333333 (* u1 0.25)))))))))
float code(float cosTheta_i, float u1, float u2) {
	return sqrtf((u1 * (1.0f + (u1 * (0.5f + (u1 * (0.3333333333333333f + (u1 * 0.25f))))))));
}
real(4) function code(costheta_i, u1, u2)
    real(4), intent (in) :: costheta_i
    real(4), intent (in) :: u1
    real(4), intent (in) :: u2
    code = sqrt((u1 * (1.0e0 + (u1 * (0.5e0 + (u1 * (0.3333333333333333e0 + (u1 * 0.25e0))))))))
end function
function code(cosTheta_i, u1, u2)
	return sqrt(Float32(u1 * Float32(Float32(1.0) + Float32(u1 * Float32(Float32(0.5) + Float32(u1 * Float32(Float32(0.3333333333333333) + Float32(u1 * Float32(0.25)))))))))
end
function tmp = code(cosTheta_i, u1, u2)
	tmp = sqrt((u1 * (single(1.0) + (u1 * (single(0.5) + (u1 * (single(0.3333333333333333) + (u1 * single(0.25)))))))));
end
\begin{array}{l}

\\
\sqrt{u1 \cdot \left(1 + u1 \cdot \left(0.5 + u1 \cdot \left(0.3333333333333333 + u1 \cdot 0.25\right)\right)\right)}
\end{array}
Derivation
  1. Initial program 57.6%

    \[\sqrt{-\log \left(1 - u1\right)} \cdot \cos \left(\left(2 \cdot \pi\right) \cdot u2\right) \]
  2. Step-by-step derivation
    1. sub-neg57.6%

      \[\leadsto \sqrt{-\log \color{blue}{\left(1 + \left(-u1\right)\right)}} \cdot \cos \left(\left(2 \cdot \pi\right) \cdot u2\right) \]
    2. log1p-define99.0%

      \[\leadsto \sqrt{-\color{blue}{\mathsf{log1p}\left(-u1\right)}} \cdot \cos \left(\left(2 \cdot \pi\right) \cdot u2\right) \]
  3. Simplified99.0%

    \[\leadsto \color{blue}{\sqrt{-\mathsf{log1p}\left(-u1\right)} \cdot \cos \left(\left(2 \cdot \pi\right) \cdot u2\right)} \]
  4. Add Preprocessing
  5. Taylor expanded in u2 around 0 80.1%

    \[\leadsto \sqrt{-\mathsf{log1p}\left(-u1\right)} \cdot \color{blue}{1} \]
  6. Taylor expanded in u1 around 0 77.2%

    \[\leadsto \sqrt{\color{blue}{u1 \cdot \left(1 + u1 \cdot \left(0.5 + u1 \cdot \left(0.3333333333333333 + 0.25 \cdot u1\right)\right)\right)}} \cdot 1 \]
  7. Step-by-step derivation
    1. *-commutative93.8%

      \[\leadsto \sqrt{u1 \cdot \left(1 + u1 \cdot \left(0.5 + u1 \cdot \left(0.3333333333333333 + \color{blue}{u1 \cdot 0.25}\right)\right)\right)} \cdot \cos \left(\left(2 \cdot \pi\right) \cdot u2\right) \]
  8. Simplified77.2%

    \[\leadsto \sqrt{\color{blue}{u1 \cdot \left(1 + u1 \cdot \left(0.5 + u1 \cdot \left(0.3333333333333333 + u1 \cdot 0.25\right)\right)\right)}} \cdot 1 \]
  9. Final simplification77.2%

    \[\leadsto \sqrt{u1 \cdot \left(1 + u1 \cdot \left(0.5 + u1 \cdot \left(0.3333333333333333 + u1 \cdot 0.25\right)\right)\right)} \]
  10. Add Preprocessing

Alternative 8: 75.2% accurate, 2.8× speedup?

\[\begin{array}{l} \\ \sqrt{u1 \cdot \left(1 + u1 \cdot \left(0.5 + u1 \cdot 0.3333333333333333\right)\right)} \end{array} \]
(FPCore (cosTheta_i u1 u2)
 :precision binary32
 (sqrt (* u1 (+ 1.0 (* u1 (+ 0.5 (* u1 0.3333333333333333)))))))
float code(float cosTheta_i, float u1, float u2) {
	return sqrtf((u1 * (1.0f + (u1 * (0.5f + (u1 * 0.3333333333333333f))))));
}
real(4) function code(costheta_i, u1, u2)
    real(4), intent (in) :: costheta_i
    real(4), intent (in) :: u1
    real(4), intent (in) :: u2
    code = sqrt((u1 * (1.0e0 + (u1 * (0.5e0 + (u1 * 0.3333333333333333e0))))))
end function
function code(cosTheta_i, u1, u2)
	return sqrt(Float32(u1 * Float32(Float32(1.0) + Float32(u1 * Float32(Float32(0.5) + Float32(u1 * Float32(0.3333333333333333)))))))
end
function tmp = code(cosTheta_i, u1, u2)
	tmp = sqrt((u1 * (single(1.0) + (u1 * (single(0.5) + (u1 * single(0.3333333333333333)))))));
end
\begin{array}{l}

\\
\sqrt{u1 \cdot \left(1 + u1 \cdot \left(0.5 + u1 \cdot 0.3333333333333333\right)\right)}
\end{array}
Derivation
  1. Initial program 57.6%

    \[\sqrt{-\log \left(1 - u1\right)} \cdot \cos \left(\left(2 \cdot \pi\right) \cdot u2\right) \]
  2. Step-by-step derivation
    1. sub-neg57.6%

      \[\leadsto \sqrt{-\log \color{blue}{\left(1 + \left(-u1\right)\right)}} \cdot \cos \left(\left(2 \cdot \pi\right) \cdot u2\right) \]
    2. log1p-define99.0%

      \[\leadsto \sqrt{-\color{blue}{\mathsf{log1p}\left(-u1\right)}} \cdot \cos \left(\left(2 \cdot \pi\right) \cdot u2\right) \]
  3. Simplified99.0%

    \[\leadsto \color{blue}{\sqrt{-\mathsf{log1p}\left(-u1\right)} \cdot \cos \left(\left(2 \cdot \pi\right) \cdot u2\right)} \]
  4. Add Preprocessing
  5. Taylor expanded in u1 around 0 92.1%

    \[\leadsto \sqrt{\color{blue}{u1 \cdot \left(1 + u1 \cdot \left(0.5 + 0.3333333333333333 \cdot u1\right)\right)}} \cdot \cos \left(\left(2 \cdot \pi\right) \cdot u2\right) \]
  6. Step-by-step derivation
    1. *-commutative92.1%

      \[\leadsto \sqrt{u1 \cdot \left(1 + u1 \cdot \left(0.5 + \color{blue}{u1 \cdot 0.3333333333333333}\right)\right)} \cdot \cos \left(\left(2 \cdot \pi\right) \cdot u2\right) \]
  7. Simplified92.1%

    \[\leadsto \sqrt{\color{blue}{u1 \cdot \left(1 + u1 \cdot \left(0.5 + u1 \cdot 0.3333333333333333\right)\right)}} \cdot \cos \left(\left(2 \cdot \pi\right) \cdot u2\right) \]
  8. Step-by-step derivation
    1. add-log-exp90.8%

      \[\leadsto \sqrt{u1 \cdot \left(1 + u1 \cdot \left(0.5 + u1 \cdot 0.3333333333333333\right)\right)} \cdot \color{blue}{\log \left(e^{\cos \left(\left(2 \cdot \pi\right) \cdot u2\right)}\right)} \]
    2. associate-*l*90.8%

      \[\leadsto \sqrt{u1 \cdot \left(1 + u1 \cdot \left(0.5 + u1 \cdot 0.3333333333333333\right)\right)} \cdot \log \left(e^{\cos \color{blue}{\left(2 \cdot \left(\pi \cdot u2\right)\right)}}\right) \]
  9. Applied egg-rr90.8%

    \[\leadsto \sqrt{u1 \cdot \left(1 + u1 \cdot \left(0.5 + u1 \cdot 0.3333333333333333\right)\right)} \cdot \color{blue}{\log \left(e^{\cos \left(2 \cdot \left(\pi \cdot u2\right)\right)}\right)} \]
  10. Taylor expanded in u2 around 0 76.1%

    \[\leadsto \color{blue}{\sqrt{u1 \cdot \left(1 + u1 \cdot \left(0.5 + 0.3333333333333333 \cdot u1\right)\right)}} \]
  11. Final simplification76.1%

    \[\leadsto \sqrt{u1 \cdot \left(1 + u1 \cdot \left(0.5 + u1 \cdot 0.3333333333333333\right)\right)} \]
  12. Add Preprocessing

Alternative 9: 72.7% accurate, 2.9× speedup?

\[\begin{array}{l} \\ \sqrt{u1 \cdot \left(1 + u1 \cdot 0.5\right)} \end{array} \]
(FPCore (cosTheta_i u1 u2)
 :precision binary32
 (sqrt (* u1 (+ 1.0 (* u1 0.5)))))
float code(float cosTheta_i, float u1, float u2) {
	return sqrtf((u1 * (1.0f + (u1 * 0.5f))));
}
real(4) function code(costheta_i, u1, u2)
    real(4), intent (in) :: costheta_i
    real(4), intent (in) :: u1
    real(4), intent (in) :: u2
    code = sqrt((u1 * (1.0e0 + (u1 * 0.5e0))))
end function
function code(cosTheta_i, u1, u2)
	return sqrt(Float32(u1 * Float32(Float32(1.0) + Float32(u1 * Float32(0.5)))))
end
function tmp = code(cosTheta_i, u1, u2)
	tmp = sqrt((u1 * (single(1.0) + (u1 * single(0.5)))));
end
\begin{array}{l}

\\
\sqrt{u1 \cdot \left(1 + u1 \cdot 0.5\right)}
\end{array}
Derivation
  1. Initial program 57.6%

    \[\sqrt{-\log \left(1 - u1\right)} \cdot \cos \left(\left(2 \cdot \pi\right) \cdot u2\right) \]
  2. Step-by-step derivation
    1. sub-neg57.6%

      \[\leadsto \sqrt{-\log \color{blue}{\left(1 + \left(-u1\right)\right)}} \cdot \cos \left(\left(2 \cdot \pi\right) \cdot u2\right) \]
    2. log1p-define99.0%

      \[\leadsto \sqrt{-\color{blue}{\mathsf{log1p}\left(-u1\right)}} \cdot \cos \left(\left(2 \cdot \pi\right) \cdot u2\right) \]
  3. Simplified99.0%

    \[\leadsto \color{blue}{\sqrt{-\mathsf{log1p}\left(-u1\right)} \cdot \cos \left(\left(2 \cdot \pi\right) \cdot u2\right)} \]
  4. Add Preprocessing
  5. Taylor expanded in u2 around 0 80.1%

    \[\leadsto \sqrt{-\mathsf{log1p}\left(-u1\right)} \cdot \color{blue}{1} \]
  6. Taylor expanded in u1 around 0 73.4%

    \[\leadsto \sqrt{\color{blue}{u1 \cdot \left(1 + 0.5 \cdot u1\right)}} \cdot 1 \]
  7. Step-by-step derivation
    1. *-commutative88.2%

      \[\leadsto \sqrt{u1 \cdot \left(1 + \color{blue}{u1 \cdot 0.5}\right)} \cdot \cos \left(\left(2 \cdot \pi\right) \cdot u2\right) \]
  8. Simplified73.4%

    \[\leadsto \sqrt{\color{blue}{u1 \cdot \left(1 + u1 \cdot 0.5\right)}} \cdot 1 \]
  9. Final simplification73.4%

    \[\leadsto \sqrt{u1 \cdot \left(1 + u1 \cdot 0.5\right)} \]
  10. Add Preprocessing

Alternative 10: 64.9% accurate, 3.1× speedup?

\[\begin{array}{l} \\ \sqrt{u1} \end{array} \]
(FPCore (cosTheta_i u1 u2) :precision binary32 (sqrt u1))
float code(float cosTheta_i, float u1, float u2) {
	return sqrtf(u1);
}
real(4) function code(costheta_i, u1, u2)
    real(4), intent (in) :: costheta_i
    real(4), intent (in) :: u1
    real(4), intent (in) :: u2
    code = sqrt(u1)
end function
function code(cosTheta_i, u1, u2)
	return sqrt(u1)
end
function tmp = code(cosTheta_i, u1, u2)
	tmp = sqrt(u1);
end
\begin{array}{l}

\\
\sqrt{u1}
\end{array}
Derivation
  1. Initial program 57.6%

    \[\sqrt{-\log \left(1 - u1\right)} \cdot \cos \left(\left(2 \cdot \pi\right) \cdot u2\right) \]
  2. Step-by-step derivation
    1. sub-neg57.6%

      \[\leadsto \sqrt{-\log \color{blue}{\left(1 + \left(-u1\right)\right)}} \cdot \cos \left(\left(2 \cdot \pi\right) \cdot u2\right) \]
    2. log1p-define99.0%

      \[\leadsto \sqrt{-\color{blue}{\mathsf{log1p}\left(-u1\right)}} \cdot \cos \left(\left(2 \cdot \pi\right) \cdot u2\right) \]
  3. Simplified99.0%

    \[\leadsto \color{blue}{\sqrt{-\mathsf{log1p}\left(-u1\right)} \cdot \cos \left(\left(2 \cdot \pi\right) \cdot u2\right)} \]
  4. Add Preprocessing
  5. Taylor expanded in u2 around 0 80.1%

    \[\leadsto \sqrt{-\mathsf{log1p}\left(-u1\right)} \cdot \color{blue}{1} \]
  6. Taylor expanded in u1 around 0 -0.0%

    \[\leadsto \color{blue}{\sqrt{u1} \cdot {\left(\sqrt{-1}\right)}^{2}} \]
  7. Step-by-step derivation
    1. *-commutative-0.0%

      \[\leadsto \color{blue}{{\left(\sqrt{-1}\right)}^{2} \cdot \sqrt{u1}} \]
    2. unpow2-0.0%

      \[\leadsto \color{blue}{\left(\sqrt{-1} \cdot \sqrt{-1}\right)} \cdot \sqrt{u1} \]
    3. rem-square-sqrt4.9%

      \[\leadsto \color{blue}{-1} \cdot \sqrt{u1} \]
    4. neg-mul-14.9%

      \[\leadsto \color{blue}{-\sqrt{u1}} \]
  8. Simplified4.9%

    \[\leadsto \color{blue}{-\sqrt{u1}} \]
  9. Step-by-step derivation
    1. add-sqr-sqrt-0.0%

      \[\leadsto \color{blue}{\sqrt{-\sqrt{u1}} \cdot \sqrt{-\sqrt{u1}}} \]
    2. sqrt-unprod65.4%

      \[\leadsto \color{blue}{\sqrt{\left(-\sqrt{u1}\right) \cdot \left(-\sqrt{u1}\right)}} \]
    3. sqr-neg65.4%

      \[\leadsto \sqrt{\color{blue}{\sqrt{u1} \cdot \sqrt{u1}}} \]
    4. add-sqr-sqrt65.4%

      \[\leadsto \sqrt{\color{blue}{u1}} \]
    5. pow1/265.4%

      \[\leadsto \color{blue}{{u1}^{0.5}} \]
  10. Applied egg-rr65.4%

    \[\leadsto \color{blue}{{u1}^{0.5}} \]
  11. Taylor expanded in u1 around 0 65.4%

    \[\leadsto \color{blue}{\sqrt{u1}} \]
  12. Add Preprocessing

Reproduce

?
herbie shell --seed 2024150 
(FPCore (cosTheta_i u1 u2)
  :name "Beckmann 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 (- (log (- 1.0 u1)))) (cos (* (* 2.0 PI) u2))))