Trowbridge-Reitz Sample, near normal, slope_x

Percentage Accurate: 99.0% → 99.0%
Time: 12.6s
Alternatives: 16
Speedup: 1.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{\frac{u1}{1 - u1}} \cdot \cos \left(6.28318530718 \cdot u2\right) \end{array} \]
(FPCore (cosTheta_i u1 u2)
 :precision binary32
 (* (sqrt (/ u1 (- 1.0 u1))) (cos (* 6.28318530718 u2))))
float code(float cosTheta_i, float u1, float u2) {
	return sqrtf((u1 / (1.0f - u1))) * cosf((6.28318530718f * u2));
}
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))) * cos((6.28318530718e0 * u2))
end function
function code(cosTheta_i, u1, u2)
	return Float32(sqrt(Float32(u1 / Float32(Float32(1.0) - u1))) * cos(Float32(Float32(6.28318530718) * u2)))
end
function tmp = code(cosTheta_i, u1, u2)
	tmp = sqrt((u1 / (single(1.0) - u1))) * cos((single(6.28318530718) * u2));
end
\begin{array}{l}

\\
\sqrt{\frac{u1}{1 - u1}} \cdot \cos \left(6.28318530718 \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 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: 99.0% accurate, 1.0× speedup?

\[\begin{array}{l} \\ \sqrt{\frac{u1}{1 - u1}} \cdot \cos \left(6.28318530718 \cdot u2\right) \end{array} \]
(FPCore (cosTheta_i u1 u2)
 :precision binary32
 (* (sqrt (/ u1 (- 1.0 u1))) (cos (* 6.28318530718 u2))))
float code(float cosTheta_i, float u1, float u2) {
	return sqrtf((u1 / (1.0f - u1))) * cosf((6.28318530718f * u2));
}
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))) * cos((6.28318530718e0 * u2))
end function
function code(cosTheta_i, u1, u2)
	return Float32(sqrt(Float32(u1 / Float32(Float32(1.0) - u1))) * cos(Float32(Float32(6.28318530718) * u2)))
end
function tmp = code(cosTheta_i, u1, u2)
	tmp = sqrt((u1 / (single(1.0) - u1))) * cos((single(6.28318530718) * u2));
end
\begin{array}{l}

\\
\sqrt{\frac{u1}{1 - u1}} \cdot \cos \left(6.28318530718 \cdot u2\right)
\end{array}

Alternative 1: 99.0% accurate, 0.9× speedup?

\[\begin{array}{l} \\ \sqrt{\frac{\mathsf{fma}\left(u1, u1, u1\right)}{-\mathsf{fma}\left(u1, u1, -1\right)}} \cdot \cos \left(6.28318530718 \cdot u2\right) \end{array} \]
(FPCore (cosTheta_i u1 u2)
 :precision binary32
 (* (sqrt (/ (fma u1 u1 u1) (- (fma u1 u1 -1.0)))) (cos (* 6.28318530718 u2))))
float code(float cosTheta_i, float u1, float u2) {
	return sqrtf((fmaf(u1, u1, u1) / -fmaf(u1, u1, -1.0f))) * cosf((6.28318530718f * u2));
}
function code(cosTheta_i, u1, u2)
	return Float32(sqrt(Float32(fma(u1, u1, u1) / Float32(-fma(u1, u1, Float32(-1.0))))) * cos(Float32(Float32(6.28318530718) * u2)))
end
\begin{array}{l}

\\
\sqrt{\frac{\mathsf{fma}\left(u1, u1, u1\right)}{-\mathsf{fma}\left(u1, u1, -1\right)}} \cdot \cos \left(6.28318530718 \cdot u2\right)
\end{array}
Derivation
  1. Initial program 99.0%

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

      \[\leadsto \sqrt{\color{blue}{\frac{u1}{1 - u1}}} \cdot \cos \left(\frac{314159265359}{50000000000} \cdot u2\right) \]
    2. lift--.f32N/A

      \[\leadsto \sqrt{\frac{u1}{\color{blue}{1 - u1}}} \cdot \cos \left(\frac{314159265359}{50000000000} \cdot u2\right) \]
    3. flip--N/A

      \[\leadsto \sqrt{\frac{u1}{\color{blue}{\frac{1 \cdot 1 - u1 \cdot u1}{1 + u1}}}} \cdot \cos \left(\frac{314159265359}{50000000000} \cdot u2\right) \]
    4. associate-/r/N/A

      \[\leadsto \sqrt{\color{blue}{\frac{u1}{1 \cdot 1 - u1 \cdot u1} \cdot \left(1 + u1\right)}} \cdot \cos \left(\frac{314159265359}{50000000000} \cdot u2\right) \]
    5. associate-*l/N/A

      \[\leadsto \sqrt{\color{blue}{\frac{u1 \cdot \left(1 + u1\right)}{1 \cdot 1 - u1 \cdot u1}}} \cdot \cos \left(\frac{314159265359}{50000000000} \cdot u2\right) \]
    6. +-commutativeN/A

      \[\leadsto \sqrt{\frac{u1 \cdot \color{blue}{\left(u1 + 1\right)}}{1 \cdot 1 - u1 \cdot u1}} \cdot \cos \left(\frac{314159265359}{50000000000} \cdot u2\right) \]
    7. distribute-rgt-outN/A

      \[\leadsto \sqrt{\frac{\color{blue}{u1 \cdot u1 + 1 \cdot u1}}{1 \cdot 1 - u1 \cdot u1}} \cdot \cos \left(\frac{314159265359}{50000000000} \cdot u2\right) \]
    8. frac-2negN/A

      \[\leadsto \sqrt{\color{blue}{\frac{\mathsf{neg}\left(\left(u1 \cdot u1 + 1 \cdot u1\right)\right)}{\mathsf{neg}\left(\left(1 \cdot 1 - u1 \cdot u1\right)\right)}}} \cdot \cos \left(\frac{314159265359}{50000000000} \cdot u2\right) \]
    9. lower-/.f32N/A

      \[\leadsto \sqrt{\color{blue}{\frac{\mathsf{neg}\left(\left(u1 \cdot u1 + 1 \cdot u1\right)\right)}{\mathsf{neg}\left(\left(1 \cdot 1 - u1 \cdot u1\right)\right)}}} \cdot \cos \left(\frac{314159265359}{50000000000} \cdot u2\right) \]
    10. lower-neg.f32N/A

      \[\leadsto \sqrt{\frac{\color{blue}{\mathsf{neg}\left(\left(u1 \cdot u1 + 1 \cdot u1\right)\right)}}{\mathsf{neg}\left(\left(1 \cdot 1 - u1 \cdot u1\right)\right)}} \cdot \cos \left(\frac{314159265359}{50000000000} \cdot u2\right) \]
    11. *-lft-identityN/A

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

      \[\leadsto \sqrt{\frac{\mathsf{neg}\left(\color{blue}{\mathsf{fma}\left(u1, u1, u1\right)}\right)}{\mathsf{neg}\left(\left(1 \cdot 1 - u1 \cdot u1\right)\right)}} \cdot \cos \left(\frac{314159265359}{50000000000} \cdot u2\right) \]
    13. metadata-evalN/A

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

      \[\leadsto \sqrt{\frac{\mathsf{neg}\left(\mathsf{fma}\left(u1, u1, u1\right)\right)}{\mathsf{neg}\left(\color{blue}{\left(1 + \left(\mathsf{neg}\left(u1 \cdot u1\right)\right)\right)}\right)}} \cdot \cos \left(\frac{314159265359}{50000000000} \cdot u2\right) \]
    15. distribute-neg-inN/A

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

      \[\leadsto \sqrt{\frac{\mathsf{neg}\left(\mathsf{fma}\left(u1, u1, u1\right)\right)}{\color{blue}{-1} + \left(\mathsf{neg}\left(\left(\mathsf{neg}\left(u1 \cdot u1\right)\right)\right)\right)}} \cdot \cos \left(\frac{314159265359}{50000000000} \cdot u2\right) \]
    17. distribute-rgt-neg-inN/A

      \[\leadsto \sqrt{\frac{\mathsf{neg}\left(\mathsf{fma}\left(u1, u1, u1\right)\right)}{-1 + \left(\mathsf{neg}\left(\color{blue}{u1 \cdot \left(\mathsf{neg}\left(u1\right)\right)}\right)\right)}} \cdot \cos \left(\frac{314159265359}{50000000000} \cdot u2\right) \]
    18. distribute-lft-neg-outN/A

      \[\leadsto \sqrt{\frac{\mathsf{neg}\left(\mathsf{fma}\left(u1, u1, u1\right)\right)}{-1 + \color{blue}{\left(\mathsf{neg}\left(u1\right)\right) \cdot \left(\mathsf{neg}\left(u1\right)\right)}}} \cdot \cos \left(\frac{314159265359}{50000000000} \cdot u2\right) \]
    19. sqr-negN/A

      \[\leadsto \sqrt{\frac{\mathsf{neg}\left(\mathsf{fma}\left(u1, u1, u1\right)\right)}{-1 + \color{blue}{u1 \cdot u1}}} \cdot \cos \left(\frac{314159265359}{50000000000} \cdot u2\right) \]
    20. lower-+.f32N/A

      \[\leadsto \sqrt{\frac{\mathsf{neg}\left(\mathsf{fma}\left(u1, u1, u1\right)\right)}{\color{blue}{-1 + u1 \cdot u1}}} \cdot \cos \left(\frac{314159265359}{50000000000} \cdot u2\right) \]
    21. lower-*.f3299.1

      \[\leadsto \sqrt{\frac{-\mathsf{fma}\left(u1, u1, u1\right)}{-1 + \color{blue}{u1 \cdot u1}}} \cdot \cos \left(6.28318530718 \cdot u2\right) \]
  4. Applied rewrites99.1%

    \[\leadsto \sqrt{\color{blue}{\frac{-\mathsf{fma}\left(u1, u1, u1\right)}{-1 + u1 \cdot u1}}} \cdot \cos \left(6.28318530718 \cdot u2\right) \]
  5. Step-by-step derivation
    1. lift-+.f32N/A

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

      \[\leadsto \sqrt{\frac{\mathsf{neg}\left(\mathsf{fma}\left(u1, u1, u1\right)\right)}{\color{blue}{u1 \cdot u1 + -1}}} \cdot \cos \left(\frac{314159265359}{50000000000} \cdot u2\right) \]
    3. lift-*.f32N/A

      \[\leadsto \sqrt{\frac{\mathsf{neg}\left(\mathsf{fma}\left(u1, u1, u1\right)\right)}{\color{blue}{u1 \cdot u1} + -1}} \cdot \cos \left(\frac{314159265359}{50000000000} \cdot u2\right) \]
    4. lower-fma.f3299.1

      \[\leadsto \sqrt{\frac{-\mathsf{fma}\left(u1, u1, u1\right)}{\color{blue}{\mathsf{fma}\left(u1, u1, -1\right)}}} \cdot \cos \left(6.28318530718 \cdot u2\right) \]
  6. Applied rewrites99.1%

    \[\leadsto \sqrt{\frac{-\mathsf{fma}\left(u1, u1, u1\right)}{\color{blue}{\mathsf{fma}\left(u1, u1, -1\right)}}} \cdot \cos \left(6.28318530718 \cdot u2\right) \]
  7. Final simplification99.1%

    \[\leadsto \sqrt{\frac{\mathsf{fma}\left(u1, u1, u1\right)}{-\mathsf{fma}\left(u1, u1, -1\right)}} \cdot \cos \left(6.28318530718 \cdot u2\right) \]
  8. Add Preprocessing

Alternative 2: 98.0% accurate, 0.5× speedup?

\[\begin{array}{l} \\ \begin{array}{l} t_0 := \cos \left(6.28318530718 \cdot u2\right)\\ t_1 := \sqrt{\frac{u1}{1 - u1}}\\ \mathbf{if}\;t\_0 \cdot t\_1 \leq 0.05000000074505806:\\ \;\;\;\;t\_0 \cdot \sqrt{\mathsf{fma}\left(u1, \mathsf{fma}\left(u1, u1, u1\right), u1\right)}\\ \mathbf{else}:\\ \;\;\;\;\mathsf{fma}\left(u2 \cdot u2, t\_1 \cdot \mathsf{fma}\left(u2 \cdot u2, \left(u2 \cdot u2\right) \cdot -85.45681720672748, \mathsf{fma}\left(u2, u2 \cdot 64.93939402268539, -19.739208802181317\right)\right), t\_1\right)\\ \end{array} \end{array} \]
(FPCore (cosTheta_i u1 u2)
 :precision binary32
 (let* ((t_0 (cos (* 6.28318530718 u2))) (t_1 (sqrt (/ u1 (- 1.0 u1)))))
   (if (<= (* t_0 t_1) 0.05000000074505806)
     (* t_0 (sqrt (fma u1 (fma u1 u1 u1) u1)))
     (fma
      (* u2 u2)
      (*
       t_1
       (fma
        (* u2 u2)
        (* (* u2 u2) -85.45681720672748)
        (fma u2 (* u2 64.93939402268539) -19.739208802181317)))
      t_1))))
float code(float cosTheta_i, float u1, float u2) {
	float t_0 = cosf((6.28318530718f * u2));
	float t_1 = sqrtf((u1 / (1.0f - u1)));
	float tmp;
	if ((t_0 * t_1) <= 0.05000000074505806f) {
		tmp = t_0 * sqrtf(fmaf(u1, fmaf(u1, u1, u1), u1));
	} else {
		tmp = fmaf((u2 * u2), (t_1 * fmaf((u2 * u2), ((u2 * u2) * -85.45681720672748f), fmaf(u2, (u2 * 64.93939402268539f), -19.739208802181317f))), t_1);
	}
	return tmp;
}
function code(cosTheta_i, u1, u2)
	t_0 = cos(Float32(Float32(6.28318530718) * u2))
	t_1 = sqrt(Float32(u1 / Float32(Float32(1.0) - u1)))
	tmp = Float32(0.0)
	if (Float32(t_0 * t_1) <= Float32(0.05000000074505806))
		tmp = Float32(t_0 * sqrt(fma(u1, fma(u1, u1, u1), u1)));
	else
		tmp = fma(Float32(u2 * u2), Float32(t_1 * fma(Float32(u2 * u2), Float32(Float32(u2 * u2) * Float32(-85.45681720672748)), fma(u2, Float32(u2 * Float32(64.93939402268539)), Float32(-19.739208802181317)))), t_1);
	end
	return tmp
end
\begin{array}{l}

\\
\begin{array}{l}
t_0 := \cos \left(6.28318530718 \cdot u2\right)\\
t_1 := \sqrt{\frac{u1}{1 - u1}}\\
\mathbf{if}\;t\_0 \cdot t\_1 \leq 0.05000000074505806:\\
\;\;\;\;t\_0 \cdot \sqrt{\mathsf{fma}\left(u1, \mathsf{fma}\left(u1, u1, u1\right), u1\right)}\\

\mathbf{else}:\\
\;\;\;\;\mathsf{fma}\left(u2 \cdot u2, t\_1 \cdot \mathsf{fma}\left(u2 \cdot u2, \left(u2 \cdot u2\right) \cdot -85.45681720672748, \mathsf{fma}\left(u2, u2 \cdot 64.93939402268539, -19.739208802181317\right)\right), t\_1\right)\\


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if (*.f32 (sqrt.f32 (/.f32 u1 (-.f32 #s(literal 1 binary32) u1))) (cos.f32 (*.f32 #s(literal 314159265359/50000000000 binary32) u2))) < 0.0500000007

    1. Initial program 98.9%

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

      \[\leadsto \sqrt{\color{blue}{u1 \cdot \left(1 + u1 \cdot \left(1 + u1\right)\right)}} \cdot \cos \left(\frac{314159265359}{50000000000} \cdot u2\right) \]
    4. Step-by-step derivation
      1. +-commutativeN/A

        \[\leadsto \sqrt{u1 \cdot \color{blue}{\left(u1 \cdot \left(1 + u1\right) + 1\right)}} \cdot \cos \left(\frac{314159265359}{50000000000} \cdot u2\right) \]
      2. distribute-lft-inN/A

        \[\leadsto \sqrt{\color{blue}{u1 \cdot \left(u1 \cdot \left(1 + u1\right)\right) + u1 \cdot 1}} \cdot \cos \left(\frac{314159265359}{50000000000} \cdot u2\right) \]
      3. *-rgt-identityN/A

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

        \[\leadsto \sqrt{\color{blue}{\mathsf{fma}\left(u1, u1 \cdot \left(1 + u1\right), u1\right)}} \cdot \cos \left(\frac{314159265359}{50000000000} \cdot u2\right) \]
      5. +-commutativeN/A

        \[\leadsto \sqrt{\mathsf{fma}\left(u1, u1 \cdot \color{blue}{\left(u1 + 1\right)}, u1\right)} \cdot \cos \left(\frac{314159265359}{50000000000} \cdot u2\right) \]
      6. distribute-lft-inN/A

        \[\leadsto \sqrt{\mathsf{fma}\left(u1, \color{blue}{u1 \cdot u1 + u1 \cdot 1}, u1\right)} \cdot \cos \left(\frac{314159265359}{50000000000} \cdot u2\right) \]
      7. *-rgt-identityN/A

        \[\leadsto \sqrt{\mathsf{fma}\left(u1, u1 \cdot u1 + \color{blue}{u1}, u1\right)} \cdot \cos \left(\frac{314159265359}{50000000000} \cdot u2\right) \]
      8. lower-fma.f3298.5

        \[\leadsto \sqrt{\mathsf{fma}\left(u1, \color{blue}{\mathsf{fma}\left(u1, u1, u1\right)}, u1\right)} \cdot \cos \left(6.28318530718 \cdot u2\right) \]
    5. Applied rewrites98.5%

      \[\leadsto \sqrt{\color{blue}{\mathsf{fma}\left(u1, \mathsf{fma}\left(u1, u1, u1\right), u1\right)}} \cdot \cos \left(6.28318530718 \cdot u2\right) \]

    if 0.0500000007 < (*.f32 (sqrt.f32 (/.f32 u1 (-.f32 #s(literal 1 binary32) u1))) (cos.f32 (*.f32 #s(literal 314159265359/50000000000 binary32) u2)))

    1. Initial program 99.2%

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

      \[\leadsto \color{blue}{\sqrt{\frac{u1}{1 - u1}} + {u2}^{2} \cdot \left(\frac{-98696044010906577398881}{5000000000000000000000} \cdot \sqrt{\frac{u1}{1 - u1}} + {u2}^{2} \cdot \left(\frac{-961389193575684075633145058384385882649239799132134631991269883031841}{11250000000000000000000000000000000000000000000000000000000000000000} \cdot \left(\sqrt{\frac{u1}{1 - u1}} \cdot {u2}^{2}\right) + \frac{9740909103402808085817682884085781839780052161}{150000000000000000000000000000000000000000000} \cdot \sqrt{\frac{u1}{1 - u1}}\right)\right)} \]
    4. Applied rewrites96.8%

      \[\leadsto \color{blue}{\mathsf{fma}\left(u2 \cdot u2, \sqrt{\frac{u1}{1 - u1}} \cdot \mathsf{fma}\left(u2 \cdot u2, \left(u2 \cdot u2\right) \cdot -85.45681720672748, \mathsf{fma}\left(u2, u2 \cdot 64.93939402268539, -19.739208802181317\right)\right), \sqrt{\frac{u1}{1 - u1}}\right)} \]
  3. Recombined 2 regimes into one program.
  4. Final simplification98.0%

    \[\leadsto \begin{array}{l} \mathbf{if}\;\cos \left(6.28318530718 \cdot u2\right) \cdot \sqrt{\frac{u1}{1 - u1}} \leq 0.05000000074505806:\\ \;\;\;\;\cos \left(6.28318530718 \cdot u2\right) \cdot \sqrt{\mathsf{fma}\left(u1, \mathsf{fma}\left(u1, u1, u1\right), u1\right)}\\ \mathbf{else}:\\ \;\;\;\;\mathsf{fma}\left(u2 \cdot u2, \sqrt{\frac{u1}{1 - u1}} \cdot \mathsf{fma}\left(u2 \cdot u2, \left(u2 \cdot u2\right) \cdot -85.45681720672748, \mathsf{fma}\left(u2, u2 \cdot 64.93939402268539, -19.739208802181317\right)\right), \sqrt{\frac{u1}{1 - u1}}\right)\\ \end{array} \]
  5. Add Preprocessing

Reproduce

?
herbie shell --seed 2024227 
(FPCore (cosTheta_i u1 u2)
  :name "Trowbridge-Reitz 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 (/ u1 (- 1.0 u1))) (cos (* 6.28318530718 u2))))