Curve intersection, scale width based on ribbon orientation

Percentage Accurate: 97.3% → 98.1%
Time: 1.1min
Alternatives: 3
Speedup: 19.5×

Specification

?
\[\left(\left(\left(0 \leq normAngle \land normAngle \leq \frac{\pi}{2}\right) \land \left(-1 \leq n0\_i \land n0\_i \leq 1\right)\right) \land \left(-1 \leq n1\_i \land n1\_i \leq 1\right)\right) \land \left(2.328306437 \cdot 10^{-10} \leq u \land u \leq 1\right)\]
\[\begin{array}{l} t_0 := \frac{1}{\sin normAngle}\\ \left(\sin \left(\left(1 - u\right) \cdot normAngle\right) \cdot t\_0\right) \cdot n0\_i + \left(\sin \left(u \cdot normAngle\right) \cdot t\_0\right) \cdot n1\_i \end{array} \]
(FPCore (normAngle u n0_i n1_i)
  :precision binary32
  (let* ((t_0 (/ 1.0 (sin normAngle))))
  (+
   (* (* (sin (* (- 1.0 u) normAngle)) t_0) n0_i)
   (* (* (sin (* u normAngle)) t_0) n1_i))))
float code(float normAngle, float u, float n0_i, float n1_i) {
	float t_0 = 1.0f / sinf(normAngle);
	return ((sinf(((1.0f - u) * normAngle)) * t_0) * n0_i) + ((sinf((u * normAngle)) * t_0) * n1_i);
}
real(4) function code(normangle, u, n0_i, n1_i)
use fmin_fmax_functions
    real(4), intent (in) :: normangle
    real(4), intent (in) :: u
    real(4), intent (in) :: n0_i
    real(4), intent (in) :: n1_i
    real(4) :: t_0
    t_0 = 1.0e0 / sin(normangle)
    code = ((sin(((1.0e0 - u) * normangle)) * t_0) * n0_i) + ((sin((u * normangle)) * t_0) * n1_i)
end function
function code(normAngle, u, n0_i, n1_i)
	t_0 = Float32(Float32(1.0) / sin(normAngle))
	return Float32(Float32(Float32(sin(Float32(Float32(Float32(1.0) - u) * normAngle)) * t_0) * n0_i) + Float32(Float32(sin(Float32(u * normAngle)) * t_0) * n1_i))
end
function tmp = code(normAngle, u, n0_i, n1_i)
	t_0 = single(1.0) / sin(normAngle);
	tmp = ((sin(((single(1.0) - u) * normAngle)) * t_0) * n0_i) + ((sin((u * normAngle)) * t_0) * n1_i);
end
\begin{array}{l}
t_0 := \frac{1}{\sin normAngle}\\
\left(\sin \left(\left(1 - u\right) \cdot normAngle\right) \cdot t\_0\right) \cdot n0\_i + \left(\sin \left(u \cdot normAngle\right) \cdot t\_0\right) \cdot n1\_i
\end{array}

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 3 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: 97.3% accurate, 1.0× speedup?

\[\begin{array}{l} t_0 := \frac{1}{\sin normAngle}\\ \left(\sin \left(\left(1 - u\right) \cdot normAngle\right) \cdot t\_0\right) \cdot n0\_i + \left(\sin \left(u \cdot normAngle\right) \cdot t\_0\right) \cdot n1\_i \end{array} \]
(FPCore (normAngle u n0_i n1_i)
  :precision binary32
  (let* ((t_0 (/ 1.0 (sin normAngle))))
  (+
   (* (* (sin (* (- 1.0 u) normAngle)) t_0) n0_i)
   (* (* (sin (* u normAngle)) t_0) n1_i))))
float code(float normAngle, float u, float n0_i, float n1_i) {
	float t_0 = 1.0f / sinf(normAngle);
	return ((sinf(((1.0f - u) * normAngle)) * t_0) * n0_i) + ((sinf((u * normAngle)) * t_0) * n1_i);
}
real(4) function code(normangle, u, n0_i, n1_i)
use fmin_fmax_functions
    real(4), intent (in) :: normangle
    real(4), intent (in) :: u
    real(4), intent (in) :: n0_i
    real(4), intent (in) :: n1_i
    real(4) :: t_0
    t_0 = 1.0e0 / sin(normangle)
    code = ((sin(((1.0e0 - u) * normangle)) * t_0) * n0_i) + ((sin((u * normangle)) * t_0) * n1_i)
end function
function code(normAngle, u, n0_i, n1_i)
	t_0 = Float32(Float32(1.0) / sin(normAngle))
	return Float32(Float32(Float32(sin(Float32(Float32(Float32(1.0) - u) * normAngle)) * t_0) * n0_i) + Float32(Float32(sin(Float32(u * normAngle)) * t_0) * n1_i))
end
function tmp = code(normAngle, u, n0_i, n1_i)
	t_0 = single(1.0) / sin(normAngle);
	tmp = ((sin(((single(1.0) - u) * normAngle)) * t_0) * n0_i) + ((sin((u * normAngle)) * t_0) * n1_i);
end
\begin{array}{l}
t_0 := \frac{1}{\sin normAngle}\\
\left(\sin \left(\left(1 - u\right) \cdot normAngle\right) \cdot t\_0\right) \cdot n0\_i + \left(\sin \left(u \cdot normAngle\right) \cdot t\_0\right) \cdot n1\_i
\end{array}

Alternative 1: 98.1% accurate, 19.5× speedup?

\[\mathsf{fma}\left(n1\_i - n0\_i, u, n0\_i\right) \]
(FPCore (normAngle u n0_i n1_i)
  :precision binary32
  (fma (- n1_i n0_i) u n0_i))
float code(float normAngle, float u, float n0_i, float n1_i) {
	return fmaf((n1_i - n0_i), u, n0_i);
}
function code(normAngle, u, n0_i, n1_i)
	return fma(Float32(n1_i - n0_i), u, n0_i)
end
\mathsf{fma}\left(n1\_i - n0\_i, u, n0\_i\right)
Derivation
  1. Initial program 97.3%

    \[\left(\sin \left(\left(1 - u\right) \cdot normAngle\right) \cdot \frac{1}{\sin normAngle}\right) \cdot n0\_i + \left(\sin \left(u \cdot normAngle\right) \cdot \frac{1}{\sin normAngle}\right) \cdot n1\_i \]
  2. Taylor expanded in normAngle around 0

    \[\leadsto \color{blue}{n0\_i \cdot \left(1 - u\right) + n1\_i \cdot u} \]
  3. Step-by-step derivation
    1. lower-fma.f32N/A

      \[\leadsto \mathsf{fma}\left(n0\_i, \color{blue}{1 - u}, n1\_i \cdot u\right) \]
    2. lower--.f32N/A

      \[\leadsto \mathsf{fma}\left(n0\_i, 1 - \color{blue}{u}, n1\_i \cdot u\right) \]
    3. lower-*.f3297.7%

      \[\leadsto \mathsf{fma}\left(n0\_i, 1 - u, n1\_i \cdot u\right) \]
  4. Applied rewrites97.7%

    \[\leadsto \color{blue}{\mathsf{fma}\left(n0\_i, 1 - u, n1\_i \cdot u\right)} \]
  5. Step-by-step derivation
    1. lift-fma.f32N/A

      \[\leadsto n0\_i \cdot \left(1 - u\right) + \color{blue}{n1\_i \cdot u} \]
    2. lift--.f32N/A

      \[\leadsto n0\_i \cdot \left(1 - u\right) + n1\_i \cdot u \]
    3. sub-flipN/A

      \[\leadsto n0\_i \cdot \left(1 + \left(\mathsf{neg}\left(u\right)\right)\right) + n1\_i \cdot u \]
    4. distribute-rgt-inN/A

      \[\leadsto \left(1 \cdot n0\_i + \left(\mathsf{neg}\left(u\right)\right) \cdot n0\_i\right) + \color{blue}{n1\_i} \cdot u \]
    5. *-lft-identityN/A

      \[\leadsto \left(n0\_i + \left(\mathsf{neg}\left(u\right)\right) \cdot n0\_i\right) + n1\_i \cdot u \]
    6. associate-+l+N/A

      \[\leadsto n0\_i + \color{blue}{\left(\left(\mathsf{neg}\left(u\right)\right) \cdot n0\_i + n1\_i \cdot u\right)} \]
    7. add-flip-revN/A

      \[\leadsto n0\_i + \left(\left(\mathsf{neg}\left(u\right)\right) \cdot n0\_i - \color{blue}{\left(\mathsf{neg}\left(n1\_i \cdot u\right)\right)}\right) \]
    8. lower-+.f32N/A

      \[\leadsto n0\_i + \color{blue}{\left(\left(\mathsf{neg}\left(u\right)\right) \cdot n0\_i - \left(\mathsf{neg}\left(n1\_i \cdot u\right)\right)\right)} \]
    9. *-commutativeN/A

      \[\leadsto n0\_i + \left(n0\_i \cdot \left(\mathsf{neg}\left(u\right)\right) - \left(\mathsf{neg}\left(\color{blue}{n1\_i \cdot u}\right)\right)\right) \]
    10. lift-*.f32N/A

      \[\leadsto n0\_i + \left(n0\_i \cdot \left(\mathsf{neg}\left(u\right)\right) - \left(\mathsf{neg}\left(n1\_i \cdot u\right)\right)\right) \]
    11. distribute-rgt-neg-inN/A

      \[\leadsto n0\_i + \left(n0\_i \cdot \left(\mathsf{neg}\left(u\right)\right) - n1\_i \cdot \color{blue}{\left(\mathsf{neg}\left(u\right)\right)}\right) \]
    12. distribute-rgt-out--N/A

      \[\leadsto n0\_i + \left(\mathsf{neg}\left(u\right)\right) \cdot \color{blue}{\left(n0\_i - n1\_i\right)} \]
    13. lower-*.f32N/A

      \[\leadsto n0\_i + \left(\mathsf{neg}\left(u\right)\right) \cdot \color{blue}{\left(n0\_i - n1\_i\right)} \]
    14. lower-neg.f32N/A

      \[\leadsto n0\_i + \left(-u\right) \cdot \left(\color{blue}{n0\_i} - n1\_i\right) \]
    15. lower--.f3297.9%

      \[\leadsto n0\_i + \left(-u\right) \cdot \left(n0\_i - \color{blue}{n1\_i}\right) \]
  6. Applied rewrites97.9%

    \[\leadsto n0\_i + \color{blue}{\left(-u\right) \cdot \left(n0\_i - n1\_i\right)} \]
  7. Step-by-step derivation
    1. lift-+.f32N/A

      \[\leadsto n0\_i + \color{blue}{\left(-u\right) \cdot \left(n0\_i - n1\_i\right)} \]
    2. +-commutativeN/A

      \[\leadsto \left(-u\right) \cdot \left(n0\_i - n1\_i\right) + \color{blue}{n0\_i} \]
    3. add-flipN/A

      \[\leadsto \left(-u\right) \cdot \left(n0\_i - n1\_i\right) - \color{blue}{\left(\mathsf{neg}\left(n0\_i\right)\right)} \]
    4. sub-flipN/A

      \[\leadsto \left(-u\right) \cdot \left(n0\_i - n1\_i\right) + \color{blue}{\left(\mathsf{neg}\left(\left(\mathsf{neg}\left(n0\_i\right)\right)\right)\right)} \]
    5. lift-*.f32N/A

      \[\leadsto \left(-u\right) \cdot \left(n0\_i - n1\_i\right) + \left(\mathsf{neg}\left(\color{blue}{\left(\mathsf{neg}\left(n0\_i\right)\right)}\right)\right) \]
    6. *-commutativeN/A

      \[\leadsto \left(n0\_i - n1\_i\right) \cdot \left(-u\right) + \left(\mathsf{neg}\left(\color{blue}{\left(\mathsf{neg}\left(n0\_i\right)\right)}\right)\right) \]
    7. lift-neg.f32N/A

      \[\leadsto \left(n0\_i - n1\_i\right) \cdot \left(\mathsf{neg}\left(u\right)\right) + \left(\mathsf{neg}\left(\left(\mathsf{neg}\left(n0\_i\right)\right)\right)\right) \]
    8. distribute-rgt-neg-outN/A

      \[\leadsto \left(\mathsf{neg}\left(\left(n0\_i - n1\_i\right) \cdot u\right)\right) + \left(\mathsf{neg}\left(\color{blue}{\left(\mathsf{neg}\left(n0\_i\right)\right)}\right)\right) \]
    9. distribute-lft-neg-inN/A

      \[\leadsto \left(\mathsf{neg}\left(\left(n0\_i - n1\_i\right)\right)\right) \cdot u + \left(\mathsf{neg}\left(\color{blue}{\left(\mathsf{neg}\left(n0\_i\right)\right)}\right)\right) \]
    10. lift--.f32N/A

      \[\leadsto \left(\mathsf{neg}\left(\left(n0\_i - n1\_i\right)\right)\right) \cdot u + \left(\mathsf{neg}\left(\left(\mathsf{neg}\left(n0\_i\right)\right)\right)\right) \]
    11. sub-negate-revN/A

      \[\leadsto \left(n1\_i - n0\_i\right) \cdot u + \left(\mathsf{neg}\left(\left(\mathsf{neg}\left(\color{blue}{n0\_i}\right)\right)\right)\right) \]
    12. remove-double-negN/A

      \[\leadsto \left(n1\_i - n0\_i\right) \cdot u + n0\_i \]
    13. lower-fma.f32N/A

      \[\leadsto \mathsf{fma}\left(n1\_i - n0\_i, \color{blue}{u}, n0\_i\right) \]
    14. lower--.f3298.1%

      \[\leadsto \mathsf{fma}\left(n1\_i - n0\_i, u, n0\_i\right) \]
  8. Applied rewrites98.1%

    \[\leadsto \mathsf{fma}\left(n1\_i - n0\_i, \color{blue}{u}, n0\_i\right) \]
  9. Add Preprocessing

Alternative 2: 82.0% accurate, 28.0× speedup?

\[\mathsf{fma}\left(u, n1\_i, n0\_i\right) \]
(FPCore (normAngle u n0_i n1_i)
  :precision binary32
  (fma u n1_i n0_i))
float code(float normAngle, float u, float n0_i, float n1_i) {
	return fmaf(u, n1_i, n0_i);
}
function code(normAngle, u, n0_i, n1_i)
	return fma(u, n1_i, n0_i)
end
\mathsf{fma}\left(u, n1\_i, n0\_i\right)
Derivation
  1. Initial program 97.3%

    \[\left(\sin \left(\left(1 - u\right) \cdot normAngle\right) \cdot \frac{1}{\sin normAngle}\right) \cdot n0\_i + \left(\sin \left(u \cdot normAngle\right) \cdot \frac{1}{\sin normAngle}\right) \cdot n1\_i \]
  2. Taylor expanded in normAngle around 0

    \[\leadsto \left(\sin \left(\left(1 - u\right) \cdot normAngle\right) \cdot \frac{1}{\sin normAngle}\right) \cdot n0\_i + \color{blue}{u} \cdot n1\_i \]
  3. Step-by-step derivation
    1. Applied rewrites97.6%

      \[\leadsto \left(\sin \left(\left(1 - u\right) \cdot normAngle\right) \cdot \frac{1}{\sin normAngle}\right) \cdot n0\_i + \color{blue}{u} \cdot n1\_i \]
    2. Taylor expanded in u around 0

      \[\leadsto \color{blue}{n0\_i} + u \cdot n1\_i \]
    3. Step-by-step derivation
      1. Applied rewrites81.9%

        \[\leadsto \color{blue}{n0\_i} + u \cdot n1\_i \]
      2. Step-by-step derivation
        1. lift-+.f32N/A

          \[\leadsto \color{blue}{n0\_i + u \cdot n1\_i} \]
        2. +-commutativeN/A

          \[\leadsto \color{blue}{u \cdot n1\_i + n0\_i} \]
        3. lift-*.f32N/A

          \[\leadsto \color{blue}{u \cdot n1\_i} + n0\_i \]
        4. lower-fma.f3282.0%

          \[\leadsto \color{blue}{\mathsf{fma}\left(u, n1\_i, n0\_i\right)} \]
      3. Applied rewrites82.0%

        \[\leadsto \color{blue}{\mathsf{fma}\left(u, n1\_i, n0\_i\right)} \]
      4. Add Preprocessing

      Alternative 3: 46.6% accurate, 167.8× speedup?

      \[n0\_i \]
      (FPCore (normAngle u n0_i n1_i)
        :precision binary32
        n0_i)
      float code(float normAngle, float u, float n0_i, float n1_i) {
      	return n0_i;
      }
      
      real(4) function code(normangle, u, n0_i, n1_i)
      use fmin_fmax_functions
          real(4), intent (in) :: normangle
          real(4), intent (in) :: u
          real(4), intent (in) :: n0_i
          real(4), intent (in) :: n1_i
          code = n0_i
      end function
      
      function code(normAngle, u, n0_i, n1_i)
      	return n0_i
      end
      
      function tmp = code(normAngle, u, n0_i, n1_i)
      	tmp = n0_i;
      end
      
      n0\_i
      
      Derivation
      1. Initial program 97.3%

        \[\left(\sin \left(\left(1 - u\right) \cdot normAngle\right) \cdot \frac{1}{\sin normAngle}\right) \cdot n0\_i + \left(\sin \left(u \cdot normAngle\right) \cdot \frac{1}{\sin normAngle}\right) \cdot n1\_i \]
      2. Taylor expanded in normAngle around 0

        \[\leadsto \color{blue}{n0\_i \cdot \left(1 - u\right) + n1\_i \cdot u} \]
      3. Step-by-step derivation
        1. lower-fma.f32N/A

          \[\leadsto \mathsf{fma}\left(n0\_i, \color{blue}{1 - u}, n1\_i \cdot u\right) \]
        2. lower--.f32N/A

          \[\leadsto \mathsf{fma}\left(n0\_i, 1 - \color{blue}{u}, n1\_i \cdot u\right) \]
        3. lower-*.f3297.7%

          \[\leadsto \mathsf{fma}\left(n0\_i, 1 - u, n1\_i \cdot u\right) \]
      4. Applied rewrites97.7%

        \[\leadsto \color{blue}{\mathsf{fma}\left(n0\_i, 1 - u, n1\_i \cdot u\right)} \]
      5. Taylor expanded in u around 0

        \[\leadsto n0\_i \]
      6. Step-by-step derivation
        1. Applied rewrites46.6%

          \[\leadsto n0\_i \]
        2. Add Preprocessing

        Reproduce

        ?
        herbie shell --seed 2025322 
        (FPCore (normAngle u n0_i n1_i)
          :name "Curve intersection, scale width based on ribbon orientation"
          :precision binary32
          :pre (and (and (and (and (<= 0.0 normAngle) (<= normAngle (/ PI 2.0))) (and (<= -1.0 n0_i) (<= n0_i 1.0))) (and (<= -1.0 n1_i) (<= n1_i 1.0))) (and (<= 2.328306437e-10 u) (<= u 1.0)))
          (+ (* (* (sin (* (- 1.0 u) normAngle)) (/ 1.0 (sin normAngle))) n0_i) (* (* (sin (* u normAngle)) (/ 1.0 (sin normAngle))) n1_i)))