Trowbridge-Reitz Sample, near normal, slope_y

Percentage Accurate: 98.3% → 98.3%
Time: 11.3s
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 \sin \left(6.28318530718 \cdot u2\right) \end{array} \]
(FPCore (cosTheta_i u1 u2)
 :precision binary32
 (* (sqrt (/ u1 (- 1.0 u1))) (sin (* 6.28318530718 u2))))
float code(float cosTheta_i, float u1, float u2) {
	return sqrtf((u1 / (1.0f - u1))) * sinf((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))) * sin((6.28318530718e0 * u2))
end function
function code(cosTheta_i, u1, u2)
	return Float32(sqrt(Float32(u1 / Float32(Float32(1.0) - u1))) * sin(Float32(Float32(6.28318530718) * u2)))
end
function tmp = code(cosTheta_i, u1, u2)
	tmp = sqrt((u1 / (single(1.0) - u1))) * sin((single(6.28318530718) * u2));
end
\begin{array}{l}

\\
\sqrt{\frac{u1}{1 - u1}} \cdot \sin \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: 98.3% accurate, 1.0× speedup?

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

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

Alternative 1: 98.3% accurate, 1.0× speedup?

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

\\
\sqrt{\frac{u1}{1 - u1}} \cdot \sin \left(6.28318530718 \cdot u2\right)
\end{array}
Derivation
  1. Initial program 98.5%

    \[\sqrt{\frac{u1}{1 - u1}} \cdot \sin \left(6.28318530718 \cdot u2\right) \]
  2. Add Preprocessing
  3. Add Preprocessing

Alternative 2: 97.6% accurate, 0.9× speedup?

\[\begin{array}{l} \\ \begin{array}{l} t_0 := \sqrt{\frac{u1}{1 - u1}}\\ \mathbf{if}\;6.28318530718 \cdot u2 \leq 0.699999988079071:\\ \;\;\;\;u2 \cdot \mathsf{fma}\left(6.28318530718, t\_0, \left(u2 \cdot u2\right) \cdot \left(t\_0 \cdot \mathsf{fma}\left(u2 \cdot u2, \mathsf{fma}\left(u2, u2 \cdot -76.70585975309672, 81.6052492761019\right), -41.341702240407926\right)\right)\right)\\ \mathbf{else}:\\ \;\;\;\;\sin \left(6.28318530718 \cdot u2\right) \cdot \sqrt{u1 \cdot \left(1 + \mathsf{fma}\left(u1, u1, u1\right)\right)}\\ \end{array} \end{array} \]
(FPCore (cosTheta_i u1 u2)
 :precision binary32
 (let* ((t_0 (sqrt (/ u1 (- 1.0 u1)))))
   (if (<= (* 6.28318530718 u2) 0.699999988079071)
     (*
      u2
      (fma
       6.28318530718
       t_0
       (*
        (* u2 u2)
        (*
         t_0
         (fma
          (* u2 u2)
          (fma u2 (* u2 -76.70585975309672) 81.6052492761019)
          -41.341702240407926)))))
     (* (sin (* 6.28318530718 u2)) (sqrt (* u1 (+ 1.0 (fma u1 u1 u1))))))))
float code(float cosTheta_i, float u1, float u2) {
	float t_0 = sqrtf((u1 / (1.0f - u1)));
	float tmp;
	if ((6.28318530718f * u2) <= 0.699999988079071f) {
		tmp = u2 * fmaf(6.28318530718f, t_0, ((u2 * u2) * (t_0 * fmaf((u2 * u2), fmaf(u2, (u2 * -76.70585975309672f), 81.6052492761019f), -41.341702240407926f))));
	} else {
		tmp = sinf((6.28318530718f * u2)) * sqrtf((u1 * (1.0f + fmaf(u1, u1, u1))));
	}
	return tmp;
}
function code(cosTheta_i, u1, u2)
	t_0 = sqrt(Float32(u1 / Float32(Float32(1.0) - u1)))
	tmp = Float32(0.0)
	if (Float32(Float32(6.28318530718) * u2) <= Float32(0.699999988079071))
		tmp = Float32(u2 * fma(Float32(6.28318530718), t_0, Float32(Float32(u2 * u2) * Float32(t_0 * fma(Float32(u2 * u2), fma(u2, Float32(u2 * Float32(-76.70585975309672)), Float32(81.6052492761019)), Float32(-41.341702240407926))))));
	else
		tmp = Float32(sin(Float32(Float32(6.28318530718) * u2)) * sqrt(Float32(u1 * Float32(Float32(1.0) + fma(u1, u1, u1)))));
	end
	return tmp
end
\begin{array}{l}

\\
\begin{array}{l}
t_0 := \sqrt{\frac{u1}{1 - u1}}\\
\mathbf{if}\;6.28318530718 \cdot u2 \leq 0.699999988079071:\\
\;\;\;\;u2 \cdot \mathsf{fma}\left(6.28318530718, t\_0, \left(u2 \cdot u2\right) \cdot \left(t\_0 \cdot \mathsf{fma}\left(u2 \cdot u2, \mathsf{fma}\left(u2, u2 \cdot -76.70585975309672, 81.6052492761019\right), -41.341702240407926\right)\right)\right)\\

\mathbf{else}:\\
\;\;\;\;\sin \left(6.28318530718 \cdot u2\right) \cdot \sqrt{u1 \cdot \left(1 + \mathsf{fma}\left(u1, u1, u1\right)\right)}\\


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

    1. Initial program 98.5%

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

      \[\leadsto \color{blue}{u2 \cdot \left(\frac{314159265359}{50000000000} \cdot \sqrt{\frac{u1}{1 - u1}} + {u2}^{2} \cdot \left(\frac{-31006276680305942139213528068663279}{750000000000000000000000000000000} \cdot \sqrt{\frac{u1}{1 - u1}} + {u2}^{2} \cdot \left(\frac{-302029322777818351566783844332719832329455959975176141755859165754785028165295919}{3937500000000000000000000000000000000000000000000000000000000000000000000000000} \cdot \left(\sqrt{\frac{u1}{1 - u1}} \cdot {u2}^{2}\right) + \frac{3060196847853821555298148281676017575122444629042460390799}{37500000000000000000000000000000000000000000000000000000} \cdot \sqrt{\frac{u1}{1 - u1}}\right)\right)\right)} \]
    4. Applied rewrites98.6%

      \[\leadsto \color{blue}{u2 \cdot \mathsf{fma}\left(6.28318530718, \sqrt{\frac{u1}{1 - u1}}, \left(u2 \cdot u2\right) \cdot \left(\sqrt{\frac{u1}{1 - u1}} \cdot \mathsf{fma}\left(u2 \cdot u2, \mathsf{fma}\left(u2, u2 \cdot -76.70585975309672, 81.6052492761019\right), -41.341702240407926\right)\right)\right)} \]

    if 0.699999988 < (*.f32 #s(literal 314159265359/50000000000 binary32) u2)

    1. Initial program 96.8%

      \[\sqrt{\frac{u1}{1 - u1}} \cdot \sin \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 \sin \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 \sin \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 \sin \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 \sin \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 \sin \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 \sin \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 \sin \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 \sin \left(\frac{314159265359}{50000000000} \cdot u2\right) \]
      8. lower-fma.f3288.7

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

      \[\leadsto \sqrt{\color{blue}{\mathsf{fma}\left(u1, \mathsf{fma}\left(u1, u1, u1\right), u1\right)}} \cdot \sin \left(6.28318530718 \cdot u2\right) \]
    6. Step-by-step derivation
      1. Applied rewrites88.7%

        \[\leadsto \sqrt{\left(1 + \mathsf{fma}\left(u1, u1, u1\right)\right) \cdot \color{blue}{u1}} \cdot \sin \left(6.28318530718 \cdot u2\right) \]
    7. Recombined 2 regimes into one program.
    8. Final simplification97.6%

      \[\leadsto \begin{array}{l} \mathbf{if}\;6.28318530718 \cdot u2 \leq 0.699999988079071:\\ \;\;\;\;u2 \cdot \mathsf{fma}\left(6.28318530718, \sqrt{\frac{u1}{1 - u1}}, \left(u2 \cdot u2\right) \cdot \left(\sqrt{\frac{u1}{1 - u1}} \cdot \mathsf{fma}\left(u2 \cdot u2, \mathsf{fma}\left(u2, u2 \cdot -76.70585975309672, 81.6052492761019\right), -41.341702240407926\right)\right)\right)\\ \mathbf{else}:\\ \;\;\;\;\sin \left(6.28318530718 \cdot u2\right) \cdot \sqrt{u1 \cdot \left(1 + \mathsf{fma}\left(u1, u1, u1\right)\right)}\\ \end{array} \]
    9. Add Preprocessing

    Reproduce

    ?
    herbie shell --seed 2024227 
    (FPCore (cosTheta_i u1 u2)
      :name "Trowbridge-Reitz Sample, near normal, slope_y"
      :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))) (sin (* 6.28318530718 u2))))