Beckmann Sample, near normal, slope_x

Percentage Accurate: 57.8% → 98.9%
Time: 12.6s
Alternatives: 17
Speedup: 21.0×

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 17 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.8% 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: 98.9% accurate, 0.7× speedup?

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

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

    \[\sqrt{-\log \left(1 - u1\right)} \cdot \cos \left(\left(2 \cdot \pi\right) \cdot u2\right) \]
  2. Add Preprocessing
  3. Step-by-step derivation
    1. lift-log.f32N/A

      \[\leadsto \sqrt{\mathsf{neg}\left(\color{blue}{\log \left(1 - u1\right)}\right)} \cdot \cos \left(\left(2 \cdot \mathsf{PI}\left(\right)\right) \cdot u2\right) \]
    2. lift--.f32N/A

      \[\leadsto \sqrt{\mathsf{neg}\left(\log \color{blue}{\left(1 - u1\right)}\right)} \cdot \cos \left(\left(2 \cdot \mathsf{PI}\left(\right)\right) \cdot u2\right) \]
    3. sub-negN/A

      \[\leadsto \sqrt{\mathsf{neg}\left(\log \color{blue}{\left(1 + \left(\mathsf{neg}\left(u1\right)\right)\right)}\right)} \cdot \cos \left(\left(2 \cdot \mathsf{PI}\left(\right)\right) \cdot u2\right) \]
    4. lower-log1p.f32N/A

      \[\leadsto \sqrt{\mathsf{neg}\left(\color{blue}{\mathsf{log1p}\left(\mathsf{neg}\left(u1\right)\right)}\right)} \cdot \cos \left(\left(2 \cdot \mathsf{PI}\left(\right)\right) \cdot u2\right) \]
    5. lower-neg.f3299.1

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

    \[\leadsto \sqrt{-\color{blue}{\mathsf{log1p}\left(-u1\right)}} \cdot \cos \left(\left(2 \cdot \pi\right) \cdot u2\right) \]
  5. Step-by-step derivation
    1. lift-PI.f32N/A

      \[\leadsto \sqrt{\mathsf{neg}\left(\mathsf{log1p}\left(\mathsf{neg}\left(u1\right)\right)\right)} \cdot \cos \left(\left(2 \cdot \color{blue}{\mathsf{PI}\left(\right)}\right) \cdot u2\right) \]
    2. add-log-expN/A

      \[\leadsto \sqrt{\mathsf{neg}\left(\mathsf{log1p}\left(\mathsf{neg}\left(u1\right)\right)\right)} \cdot \cos \left(\left(2 \cdot \color{blue}{\log \left(e^{\mathsf{PI}\left(\right)}\right)}\right) \cdot u2\right) \]
    3. *-un-lft-identityN/A

      \[\leadsto \sqrt{\mathsf{neg}\left(\mathsf{log1p}\left(\mathsf{neg}\left(u1\right)\right)\right)} \cdot \cos \left(\left(2 \cdot \log \left(e^{\color{blue}{1 \cdot \mathsf{PI}\left(\right)}}\right)\right) \cdot u2\right) \]
    4. lift-PI.f32N/A

      \[\leadsto \sqrt{\mathsf{neg}\left(\mathsf{log1p}\left(\mathsf{neg}\left(u1\right)\right)\right)} \cdot \cos \left(\left(2 \cdot \log \left(e^{1 \cdot \color{blue}{\mathsf{PI}\left(\right)}}\right)\right) \cdot u2\right) \]
    5. exp-prodN/A

      \[\leadsto \sqrt{\mathsf{neg}\left(\mathsf{log1p}\left(\mathsf{neg}\left(u1\right)\right)\right)} \cdot \cos \left(\left(2 \cdot \log \color{blue}{\left({\left(e^{1}\right)}^{\mathsf{PI}\left(\right)}\right)}\right) \cdot u2\right) \]
    6. log-powN/A

      \[\leadsto \sqrt{\mathsf{neg}\left(\mathsf{log1p}\left(\mathsf{neg}\left(u1\right)\right)\right)} \cdot \cos \left(\left(2 \cdot \color{blue}{\left(\mathsf{PI}\left(\right) \cdot \log \left(e^{1}\right)\right)}\right) \cdot u2\right) \]
    7. lower-*.f32N/A

      \[\leadsto \sqrt{\mathsf{neg}\left(\mathsf{log1p}\left(\mathsf{neg}\left(u1\right)\right)\right)} \cdot \cos \left(\left(2 \cdot \color{blue}{\left(\mathsf{PI}\left(\right) \cdot \log \left(e^{1}\right)\right)}\right) \cdot u2\right) \]
    8. lower-log.f32N/A

      \[\leadsto \sqrt{\mathsf{neg}\left(\mathsf{log1p}\left(\mathsf{neg}\left(u1\right)\right)\right)} \cdot \cos \left(\left(2 \cdot \left(\mathsf{PI}\left(\right) \cdot \color{blue}{\log \left(e^{1}\right)}\right)\right) \cdot u2\right) \]
    9. exp-1-eN/A

      \[\leadsto \sqrt{\mathsf{neg}\left(\mathsf{log1p}\left(\mathsf{neg}\left(u1\right)\right)\right)} \cdot \cos \left(\left(2 \cdot \left(\mathsf{PI}\left(\right) \cdot \log \color{blue}{\mathsf{E}\left(\right)}\right)\right) \cdot u2\right) \]
    10. lower-E.f3299.1

      \[\leadsto \sqrt{-\mathsf{log1p}\left(-u1\right)} \cdot \cos \left(\left(2 \cdot \left(\pi \cdot \log \color{blue}{e}\right)\right) \cdot u2\right) \]
  6. Applied rewrites99.1%

    \[\leadsto \sqrt{-\mathsf{log1p}\left(-u1\right)} \cdot \cos \left(\left(2 \cdot \color{blue}{\left(\pi \cdot \log e\right)}\right) \cdot u2\right) \]
  7. Add Preprocessing

Alternative 2: 96.0% accurate, 0.6× speedup?

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

\\
\begin{array}{l}
t_0 := \cos \left(u2 \cdot \left(2 \cdot \pi\right)\right)\\
\mathbf{if}\;t\_0 \cdot \sqrt{-\log \left(1 - u1\right)} \leq 0.13500000536441803:\\
\;\;\;\;t\_0 \cdot \sqrt{-u1 \cdot \mathsf{fma}\left(u1, \mathsf{fma}\left(u1, -0.3333333333333333, -0.5\right), -1\right)}\\

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


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if (*.f32 (sqrt.f32 (neg.f32 (log.f32 (-.f32 #s(literal 1 binary32) u1)))) (cos.f32 (*.f32 (*.f32 #s(literal 2 binary32) (PI.f32)) u2))) < 0.135000005

    1. Initial program 49.7%

      \[\sqrt{-\log \left(1 - u1\right)} \cdot \cos \left(\left(2 \cdot \pi\right) \cdot u2\right) \]
    2. Add Preprocessing
    3. Taylor expanded in u1 around 0

      \[\leadsto \sqrt{\mathsf{neg}\left(\color{blue}{u1 \cdot \left(u1 \cdot \left(\frac{-1}{3} \cdot u1 - \frac{1}{2}\right) - 1\right)}\right)} \cdot \cos \left(\left(2 \cdot \mathsf{PI}\left(\right)\right) \cdot u2\right) \]
    4. Step-by-step derivation
      1. lower-*.f32N/A

        \[\leadsto \sqrt{\mathsf{neg}\left(\color{blue}{u1 \cdot \left(u1 \cdot \left(\frac{-1}{3} \cdot u1 - \frac{1}{2}\right) - 1\right)}\right)} \cdot \cos \left(\left(2 \cdot \mathsf{PI}\left(\right)\right) \cdot u2\right) \]
      2. sub-negN/A

        \[\leadsto \sqrt{\mathsf{neg}\left(u1 \cdot \color{blue}{\left(u1 \cdot \left(\frac{-1}{3} \cdot u1 - \frac{1}{2}\right) + \left(\mathsf{neg}\left(1\right)\right)\right)}\right)} \cdot \cos \left(\left(2 \cdot \mathsf{PI}\left(\right)\right) \cdot u2\right) \]
      3. metadata-evalN/A

        \[\leadsto \sqrt{\mathsf{neg}\left(u1 \cdot \left(u1 \cdot \left(\frac{-1}{3} \cdot u1 - \frac{1}{2}\right) + \color{blue}{-1}\right)\right)} \cdot \cos \left(\left(2 \cdot \mathsf{PI}\left(\right)\right) \cdot u2\right) \]
      4. lower-fma.f32N/A

        \[\leadsto \sqrt{\mathsf{neg}\left(u1 \cdot \color{blue}{\mathsf{fma}\left(u1, \frac{-1}{3} \cdot u1 - \frac{1}{2}, -1\right)}\right)} \cdot \cos \left(\left(2 \cdot \mathsf{PI}\left(\right)\right) \cdot u2\right) \]
      5. sub-negN/A

        \[\leadsto \sqrt{\mathsf{neg}\left(u1 \cdot \mathsf{fma}\left(u1, \color{blue}{\frac{-1}{3} \cdot u1 + \left(\mathsf{neg}\left(\frac{1}{2}\right)\right)}, -1\right)\right)} \cdot \cos \left(\left(2 \cdot \mathsf{PI}\left(\right)\right) \cdot u2\right) \]
      6. *-commutativeN/A

        \[\leadsto \sqrt{\mathsf{neg}\left(u1 \cdot \mathsf{fma}\left(u1, \color{blue}{u1 \cdot \frac{-1}{3}} + \left(\mathsf{neg}\left(\frac{1}{2}\right)\right), -1\right)\right)} \cdot \cos \left(\left(2 \cdot \mathsf{PI}\left(\right)\right) \cdot u2\right) \]
      7. metadata-evalN/A

        \[\leadsto \sqrt{\mathsf{neg}\left(u1 \cdot \mathsf{fma}\left(u1, u1 \cdot \frac{-1}{3} + \color{blue}{\frac{-1}{2}}, -1\right)\right)} \cdot \cos \left(\left(2 \cdot \mathsf{PI}\left(\right)\right) \cdot u2\right) \]
      8. lower-fma.f3298.2

        \[\leadsto \sqrt{-u1 \cdot \mathsf{fma}\left(u1, \color{blue}{\mathsf{fma}\left(u1, -0.3333333333333333, -0.5\right)}, -1\right)} \cdot \cos \left(\left(2 \cdot \pi\right) \cdot u2\right) \]
    5. Applied rewrites98.2%

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

    if 0.135000005 < (*.f32 (sqrt.f32 (neg.f32 (log.f32 (-.f32 #s(literal 1 binary32) u1)))) (cos.f32 (*.f32 (*.f32 #s(literal 2 binary32) (PI.f32)) u2)))

    1. Initial program 97.1%

      \[\sqrt{-\log \left(1 - u1\right)} \cdot \cos \left(\left(2 \cdot \pi\right) \cdot u2\right) \]
    2. Add Preprocessing
    3. Step-by-step derivation
      1. lift-log.f32N/A

        \[\leadsto \sqrt{\mathsf{neg}\left(\color{blue}{\log \left(1 - u1\right)}\right)} \cdot \cos \left(\left(2 \cdot \mathsf{PI}\left(\right)\right) \cdot u2\right) \]
      2. lift--.f32N/A

        \[\leadsto \sqrt{\mathsf{neg}\left(\log \color{blue}{\left(1 - u1\right)}\right)} \cdot \cos \left(\left(2 \cdot \mathsf{PI}\left(\right)\right) \cdot u2\right) \]
      3. sub-negN/A

        \[\leadsto \sqrt{\mathsf{neg}\left(\log \color{blue}{\left(1 + \left(\mathsf{neg}\left(u1\right)\right)\right)}\right)} \cdot \cos \left(\left(2 \cdot \mathsf{PI}\left(\right)\right) \cdot u2\right) \]
      4. lower-log1p.f32N/A

        \[\leadsto \sqrt{\mathsf{neg}\left(\color{blue}{\mathsf{log1p}\left(\mathsf{neg}\left(u1\right)\right)}\right)} \cdot \cos \left(\left(2 \cdot \mathsf{PI}\left(\right)\right) \cdot u2\right) \]
      5. lower-neg.f3299.3

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

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

      \[\leadsto \sqrt{\color{blue}{\log \left(\frac{1}{1 - u1 \cdot u1}\right) + \mathsf{log1p}\left(u1\right)}} \cdot \cos \left(\left(2 \cdot \pi\right) \cdot u2\right) \]
    6. Taylor expanded in u2 around 0

      \[\leadsto \color{blue}{\sqrt{\log \left(1 + u1\right) + \log \left(\frac{1}{1 - {u1}^{2}}\right)}} \]
    7. Step-by-step derivation
      1. lower-sqrt.f32N/A

        \[\leadsto \color{blue}{\sqrt{\log \left(1 + u1\right) + \log \left(\frac{1}{1 - {u1}^{2}}\right)}} \]
      2. log-recN/A

        \[\leadsto \sqrt{\log \left(1 + u1\right) + \color{blue}{\left(\mathsf{neg}\left(\log \left(1 - {u1}^{2}\right)\right)\right)}} \]
      3. unsub-negN/A

        \[\leadsto \sqrt{\color{blue}{\log \left(1 + u1\right) - \log \left(1 - {u1}^{2}\right)}} \]
      4. lower--.f32N/A

        \[\leadsto \sqrt{\color{blue}{\log \left(1 + u1\right) - \log \left(1 - {u1}^{2}\right)}} \]
      5. lower-log1p.f32N/A

        \[\leadsto \sqrt{\color{blue}{\mathsf{log1p}\left(u1\right)} - \log \left(1 - {u1}^{2}\right)} \]
      6. sub-negN/A

        \[\leadsto \sqrt{\mathsf{log1p}\left(u1\right) - \log \color{blue}{\left(1 + \left(\mathsf{neg}\left({u1}^{2}\right)\right)\right)}} \]
      7. lower-log1p.f32N/A

        \[\leadsto \sqrt{\mathsf{log1p}\left(u1\right) - \color{blue}{\mathsf{log1p}\left(\mathsf{neg}\left({u1}^{2}\right)\right)}} \]
      8. unpow2N/A

        \[\leadsto \sqrt{\mathsf{log1p}\left(u1\right) - \mathsf{log1p}\left(\mathsf{neg}\left(\color{blue}{u1 \cdot u1}\right)\right)} \]
      9. distribute-rgt-neg-inN/A

        \[\leadsto \sqrt{\mathsf{log1p}\left(u1\right) - \mathsf{log1p}\left(\color{blue}{u1 \cdot \left(\mathsf{neg}\left(u1\right)\right)}\right)} \]
      10. lower-*.f32N/A

        \[\leadsto \sqrt{\mathsf{log1p}\left(u1\right) - \mathsf{log1p}\left(\color{blue}{u1 \cdot \left(\mathsf{neg}\left(u1\right)\right)}\right)} \]
      11. lower-neg.f3285.1

        \[\leadsto \sqrt{\mathsf{log1p}\left(u1\right) - \mathsf{log1p}\left(u1 \cdot \color{blue}{\left(-u1\right)}\right)} \]
    8. Applied rewrites85.1%

      \[\leadsto \color{blue}{\sqrt{\mathsf{log1p}\left(u1\right) - \mathsf{log1p}\left(u1 \cdot \left(-u1\right)\right)}} \]
    9. Step-by-step derivation
      1. Applied rewrites85.2%

        \[\leadsto \color{blue}{\sqrt{-\mathsf{log1p}\left(-u1\right)}} \]
    10. Recombined 2 regimes into one program.
    11. Final simplification96.0%

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

    Reproduce

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