Rust f32::asinh

Percentage Accurate: 38.3% → 99.6%
Time: 9.2s
Alternatives: 3
Speedup: 1.1×

Specification

?
\[\begin{array}{l} \\ \sinh^{-1} x \end{array} \]
(FPCore (x) :precision binary32 (asinh x))
float code(float x) {
	return asinhf(x);
}
function code(x)
	return asinh(x)
end
function tmp = code(x)
	tmp = asinh(x);
end
\begin{array}{l}

\\
\sinh^{-1} x
\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 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: 38.3% accurate, 1.0× speedup?

\[\begin{array}{l} \\ \mathsf{copysign}\left(\log \left(\left|x\right| + \sqrt{x \cdot x + 1}\right), x\right) \end{array} \]
(FPCore (x)
 :precision binary32
 (copysign (log (+ (fabs x) (sqrt (+ (* x x) 1.0)))) x))
float code(float x) {
	return copysignf(logf((fabsf(x) + sqrtf(((x * x) + 1.0f)))), x);
}
function code(x)
	return copysign(log(Float32(abs(x) + sqrt(Float32(Float32(x * x) + Float32(1.0))))), x)
end
function tmp = code(x)
	tmp = sign(x) * abs(log((abs(x) + sqrt(((x * x) + single(1.0))))));
end
\begin{array}{l}

\\
\mathsf{copysign}\left(\log \left(\left|x\right| + \sqrt{x \cdot x + 1}\right), x\right)
\end{array}

Alternative 1: 99.6% accurate, 1.1× speedup?

\[\begin{array}{l} \\ \mathsf{copysign}\left(\sinh^{-1} \left(\left|x\right|\right), x\right) \end{array} \]
(FPCore (x) :precision binary32 (copysign (asinh (fabs x)) x))
float code(float x) {
	return copysignf(asinhf(fabsf(x)), x);
}
function code(x)
	return copysign(asinh(abs(x)), x)
end
function tmp = code(x)
	tmp = sign(x) * abs(asinh(abs(x)));
end
\begin{array}{l}

\\
\mathsf{copysign}\left(\sinh^{-1} \left(\left|x\right|\right), x\right)
\end{array}
Derivation
  1. Initial program 39.9%

    \[\mathsf{copysign}\left(\log \left(\left|x\right| + \sqrt{x \cdot x + 1}\right), x\right) \]
  2. Add Preprocessing
  3. Step-by-step derivation
    1. lift-log.f32N/A

      \[\leadsto \mathsf{copysign}\left(\color{blue}{\log \left(\left|x\right| + \sqrt{x \cdot x + 1}\right)}, x\right) \]
    2. lift-+.f32N/A

      \[\leadsto \mathsf{copysign}\left(\log \color{blue}{\left(\left|x\right| + \sqrt{x \cdot x + 1}\right)}, x\right) \]
    3. lift-sqrt.f32N/A

      \[\leadsto \mathsf{copysign}\left(\log \left(\left|x\right| + \color{blue}{\sqrt{x \cdot x + 1}}\right), x\right) \]
    4. lift-+.f32N/A

      \[\leadsto \mathsf{copysign}\left(\log \left(\left|x\right| + \sqrt{\color{blue}{x \cdot x + 1}}\right), x\right) \]
    5. lift-*.f32N/A

      \[\leadsto \mathsf{copysign}\left(\log \left(\left|x\right| + \sqrt{\color{blue}{x \cdot x} + 1}\right), x\right) \]
    6. sqr-abs-revN/A

      \[\leadsto \mathsf{copysign}\left(\log \left(\left|x\right| + \sqrt{\color{blue}{\left|x\right| \cdot \left|x\right|} + 1}\right), x\right) \]
    7. lift-fabs.f32N/A

      \[\leadsto \mathsf{copysign}\left(\log \left(\left|x\right| + \sqrt{\color{blue}{\left|x\right|} \cdot \left|x\right| + 1}\right), x\right) \]
    8. lift-fabs.f32N/A

      \[\leadsto \mathsf{copysign}\left(\log \left(\left|x\right| + \sqrt{\left|x\right| \cdot \color{blue}{\left|x\right|} + 1}\right), x\right) \]
    9. asinh-def-revN/A

      \[\leadsto \mathsf{copysign}\left(\color{blue}{\sinh^{-1} \left(\left|x\right|\right)}, x\right) \]
    10. lower-asinh.f3299.5

      \[\leadsto \mathsf{copysign}\left(\color{blue}{\sinh^{-1} \left(\left|x\right|\right)}, x\right) \]
  4. Applied rewrites99.5%

    \[\leadsto \color{blue}{\mathsf{copysign}\left(\sinh^{-1} \left(\left|x\right|\right), x\right)} \]
  5. Add Preprocessing

Alternative 2: 20.6% accurate, 0.5× speedup?

\[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;\mathsf{copysign}\left(\log \left(\left|x\right| + \sqrt{x \cdot x + 1}\right), x\right) \leq 3:\\ \;\;\;\;\mathsf{copysign}\left(\left(\frac{0.5}{\left|x\right| - -1} \cdot x\right) \cdot x, x\right)\\ \mathbf{else}:\\ \;\;\;\;\mathsf{copysign}\left(\log x, x\right)\\ \end{array} \end{array} \]
(FPCore (x)
 :precision binary32
 (if (<= (copysign (log (+ (fabs x) (sqrt (+ (* x x) 1.0)))) x) 3.0)
   (copysign (* (* (/ 0.5 (- (fabs x) -1.0)) x) x) x)
   (copysign (log x) x)))
float code(float x) {
	float tmp;
	if (copysignf(logf((fabsf(x) + sqrtf(((x * x) + 1.0f)))), x) <= 3.0f) {
		tmp = copysignf((((0.5f / (fabsf(x) - -1.0f)) * x) * x), x);
	} else {
		tmp = copysignf(logf(x), x);
	}
	return tmp;
}
function code(x)
	tmp = Float32(0.0)
	if (copysign(log(Float32(abs(x) + sqrt(Float32(Float32(x * x) + Float32(1.0))))), x) <= Float32(3.0))
		tmp = copysign(Float32(Float32(Float32(Float32(0.5) / Float32(abs(x) - Float32(-1.0))) * x) * x), x);
	else
		tmp = copysign(log(x), x);
	end
	return tmp
end
function tmp_2 = code(x)
	tmp = single(0.0);
	if ((sign(x) * abs(log((abs(x) + sqrt(((x * x) + single(1.0))))))) <= single(3.0))
		tmp = sign(x) * abs((((single(0.5) / (abs(x) - single(-1.0))) * x) * x));
	else
		tmp = sign(x) * abs(log(x));
	end
	tmp_2 = tmp;
end
\begin{array}{l}

\\
\begin{array}{l}
\mathbf{if}\;\mathsf{copysign}\left(\log \left(\left|x\right| + \sqrt{x \cdot x + 1}\right), x\right) \leq 3:\\
\;\;\;\;\mathsf{copysign}\left(\left(\frac{0.5}{\left|x\right| - -1} \cdot x\right) \cdot x, x\right)\\

\mathbf{else}:\\
\;\;\;\;\mathsf{copysign}\left(\log x, x\right)\\


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if (copysign.f32 (log.f32 (+.f32 (fabs.f32 x) (sqrt.f32 (+.f32 (*.f32 x x) #s(literal 1 binary32))))) x) < 3

    1. Initial program 35.7%

      \[\mathsf{copysign}\left(\log \left(\left|x\right| + \sqrt{x \cdot x + 1}\right), x\right) \]
    2. Add Preprocessing
    3. Taylor expanded in x around 0

      \[\leadsto \mathsf{copysign}\left(\color{blue}{\log \left(1 + \left|x\right|\right) + \frac{1}{2} \cdot \frac{{x}^{2}}{1 + \left|x\right|}}, x\right) \]
    4. Step-by-step derivation
      1. +-commutativeN/A

        \[\leadsto \mathsf{copysign}\left(\color{blue}{\frac{1}{2} \cdot \frac{{x}^{2}}{1 + \left|x\right|} + \log \left(1 + \left|x\right|\right)}, x\right) \]
      2. associate-*r/N/A

        \[\leadsto \mathsf{copysign}\left(\color{blue}{\frac{\frac{1}{2} \cdot {x}^{2}}{1 + \left|x\right|}} + \log \left(1 + \left|x\right|\right), x\right) \]
      3. associate-*l/N/A

        \[\leadsto \mathsf{copysign}\left(\color{blue}{\frac{\frac{1}{2}}{1 + \left|x\right|} \cdot {x}^{2}} + \log \left(1 + \left|x\right|\right), x\right) \]
      4. metadata-evalN/A

        \[\leadsto \mathsf{copysign}\left(\frac{\color{blue}{\frac{1}{2} \cdot 1}}{1 + \left|x\right|} \cdot {x}^{2} + \log \left(1 + \left|x\right|\right), x\right) \]
      5. associate-*r/N/A

        \[\leadsto \mathsf{copysign}\left(\color{blue}{\left(\frac{1}{2} \cdot \frac{1}{1 + \left|x\right|}\right)} \cdot {x}^{2} + \log \left(1 + \left|x\right|\right), x\right) \]
      6. unpow2N/A

        \[\leadsto \mathsf{copysign}\left(\left(\frac{1}{2} \cdot \frac{1}{1 + \left|x\right|}\right) \cdot \color{blue}{\left(x \cdot x\right)} + \log \left(1 + \left|x\right|\right), x\right) \]
      7. associate-*r*N/A

        \[\leadsto \mathsf{copysign}\left(\color{blue}{\left(\left(\frac{1}{2} \cdot \frac{1}{1 + \left|x\right|}\right) \cdot x\right) \cdot x} + \log \left(1 + \left|x\right|\right), x\right) \]
      8. lower-fma.f32N/A

        \[\leadsto \mathsf{copysign}\left(\color{blue}{\mathsf{fma}\left(\left(\frac{1}{2} \cdot \frac{1}{1 + \left|x\right|}\right) \cdot x, x, \log \left(1 + \left|x\right|\right)\right)}, x\right) \]
    5. Applied rewrites62.4%

      \[\leadsto \mathsf{copysign}\left(\color{blue}{\mathsf{fma}\left(\frac{0.5}{\left|x\right| - -1} \cdot x, x, \mathsf{log1p}\left(\left|x\right|\right)\right)}, x\right) \]
    6. Taylor expanded in x around inf

      \[\leadsto \mathsf{copysign}\left(\frac{1}{2} \cdot \color{blue}{\frac{{x}^{2}}{1 + \left|x\right|}}, x\right) \]
    7. Step-by-step derivation
      1. Applied rewrites12.6%

        \[\leadsto \mathsf{copysign}\left(\frac{0.5 \cdot \left(x \cdot x\right)}{\color{blue}{1 + \left|x\right|}}, x\right) \]
      2. Step-by-step derivation
        1. Applied rewrites13.0%

          \[\leadsto \mathsf{copysign}\left(\left(\frac{0.5}{\left|x\right| - -1} \cdot \left(-x\right)\right) \cdot \left(-x\right), x\right) \]

        if 3 < (copysign.f32 (log.f32 (+.f32 (fabs.f32 x) (sqrt.f32 (+.f32 (*.f32 x x) #s(literal 1 binary32))))) x)

        1. Initial program 54.9%

          \[\mathsf{copysign}\left(\log \left(\left|x\right| + \sqrt{x \cdot x + 1}\right), x\right) \]
        2. Add Preprocessing
        3. Taylor expanded in x around inf

          \[\leadsto \mathsf{copysign}\left(\color{blue}{-1 \cdot \log \left(\frac{1}{x}\right)}, x\right) \]
        4. Step-by-step derivation
          1. mul-1-negN/A

            \[\leadsto \mathsf{copysign}\left(\color{blue}{\mathsf{neg}\left(\log \left(\frac{1}{x}\right)\right)}, x\right) \]
          2. log-recN/A

            \[\leadsto \mathsf{copysign}\left(\mathsf{neg}\left(\color{blue}{\left(\mathsf{neg}\left(\log x\right)\right)}\right), x\right) \]
          3. remove-double-negN/A

            \[\leadsto \mathsf{copysign}\left(\color{blue}{\log x}, x\right) \]
          4. lower-log.f3244.3

            \[\leadsto \mathsf{copysign}\left(\color{blue}{\log x}, x\right) \]
        5. Applied rewrites44.3%

          \[\leadsto \mathsf{copysign}\left(\color{blue}{\log x}, x\right) \]
      3. Recombined 2 regimes into one program.
      4. Final simplification19.8%

        \[\leadsto \begin{array}{l} \mathbf{if}\;\mathsf{copysign}\left(\log \left(\left|x\right| + \sqrt{x \cdot x + 1}\right), x\right) \leq 3:\\ \;\;\;\;\mathsf{copysign}\left(\left(\frac{0.5}{\left|x\right| - -1} \cdot x\right) \cdot x, x\right)\\ \mathbf{else}:\\ \;\;\;\;\mathsf{copysign}\left(\log x, x\right)\\ \end{array} \]
      5. Add Preprocessing

      Alternative 3: 12.7% accurate, 1.8× speedup?

      \[\begin{array}{l} \\ \mathsf{copysign}\left(\left(0.5 \cdot \frac{x}{\left|x\right| - -1}\right) \cdot x, x\right) \end{array} \]
      (FPCore (x)
       :precision binary32
       (copysign (* (* 0.5 (/ x (- (fabs x) -1.0))) x) x))
      float code(float x) {
      	return copysignf(((0.5f * (x / (fabsf(x) - -1.0f))) * x), x);
      }
      
      function code(x)
      	return copysign(Float32(Float32(Float32(0.5) * Float32(x / Float32(abs(x) - Float32(-1.0)))) * x), x)
      end
      
      function tmp = code(x)
      	tmp = sign(x) * abs(((single(0.5) * (x / (abs(x) - single(-1.0)))) * x));
      end
      
      \begin{array}{l}
      
      \\
      \mathsf{copysign}\left(\left(0.5 \cdot \frac{x}{\left|x\right| - -1}\right) \cdot x, x\right)
      \end{array}
      
      Derivation
      1. Initial program 39.9%

        \[\mathsf{copysign}\left(\log \left(\left|x\right| + \sqrt{x \cdot x + 1}\right), x\right) \]
      2. Add Preprocessing
      3. Taylor expanded in x around 0

        \[\leadsto \mathsf{copysign}\left(\color{blue}{\log \left(1 + \left|x\right|\right) + \frac{1}{2} \cdot \frac{{x}^{2}}{1 + \left|x\right|}}, x\right) \]
      4. Step-by-step derivation
        1. +-commutativeN/A

          \[\leadsto \mathsf{copysign}\left(\color{blue}{\frac{1}{2} \cdot \frac{{x}^{2}}{1 + \left|x\right|} + \log \left(1 + \left|x\right|\right)}, x\right) \]
        2. associate-*r/N/A

          \[\leadsto \mathsf{copysign}\left(\color{blue}{\frac{\frac{1}{2} \cdot {x}^{2}}{1 + \left|x\right|}} + \log \left(1 + \left|x\right|\right), x\right) \]
        3. associate-*l/N/A

          \[\leadsto \mathsf{copysign}\left(\color{blue}{\frac{\frac{1}{2}}{1 + \left|x\right|} \cdot {x}^{2}} + \log \left(1 + \left|x\right|\right), x\right) \]
        4. metadata-evalN/A

          \[\leadsto \mathsf{copysign}\left(\frac{\color{blue}{\frac{1}{2} \cdot 1}}{1 + \left|x\right|} \cdot {x}^{2} + \log \left(1 + \left|x\right|\right), x\right) \]
        5. associate-*r/N/A

          \[\leadsto \mathsf{copysign}\left(\color{blue}{\left(\frac{1}{2} \cdot \frac{1}{1 + \left|x\right|}\right)} \cdot {x}^{2} + \log \left(1 + \left|x\right|\right), x\right) \]
        6. unpow2N/A

          \[\leadsto \mathsf{copysign}\left(\left(\frac{1}{2} \cdot \frac{1}{1 + \left|x\right|}\right) \cdot \color{blue}{\left(x \cdot x\right)} + \log \left(1 + \left|x\right|\right), x\right) \]
        7. associate-*r*N/A

          \[\leadsto \mathsf{copysign}\left(\color{blue}{\left(\left(\frac{1}{2} \cdot \frac{1}{1 + \left|x\right|}\right) \cdot x\right) \cdot x} + \log \left(1 + \left|x\right|\right), x\right) \]
        8. lower-fma.f32N/A

          \[\leadsto \mathsf{copysign}\left(\color{blue}{\mathsf{fma}\left(\left(\frac{1}{2} \cdot \frac{1}{1 + \left|x\right|}\right) \cdot x, x, \log \left(1 + \left|x\right|\right)\right)}, x\right) \]
      5. Applied rewrites51.2%

        \[\leadsto \mathsf{copysign}\left(\color{blue}{\mathsf{fma}\left(\frac{0.5}{\left|x\right| - -1} \cdot x, x, \mathsf{log1p}\left(\left|x\right|\right)\right)}, x\right) \]
      6. Taylor expanded in x around inf

        \[\leadsto \mathsf{copysign}\left(\frac{1}{2} \cdot \color{blue}{\frac{{x}^{2}}{1 + \left|x\right|}}, x\right) \]
      7. Step-by-step derivation
        1. Applied rewrites12.2%

          \[\leadsto \mathsf{copysign}\left(\frac{0.5 \cdot \left(x \cdot x\right)}{\color{blue}{1 + \left|x\right|}}, x\right) \]
        2. Step-by-step derivation
          1. Applied rewrites12.6%

            \[\leadsto \mathsf{copysign}\left(\left(0.5 \cdot \frac{x}{\left|x\right| - -1}\right) \cdot x, x\right) \]
          2. Add Preprocessing

          Developer Target 1: 99.6% accurate, 0.6× speedup?

          \[\begin{array}{l} \\ \begin{array}{l} t_0 := \frac{1}{\left|x\right|}\\ \mathsf{copysign}\left(\mathsf{log1p}\left(\left|x\right| + \frac{\left|x\right|}{\mathsf{hypot}\left(1, t\_0\right) + t\_0}\right), x\right) \end{array} \end{array} \]
          (FPCore (x)
           :precision binary32
           (let* ((t_0 (/ 1.0 (fabs x))))
             (copysign (log1p (+ (fabs x) (/ (fabs x) (+ (hypot 1.0 t_0) t_0)))) x)))
          float code(float x) {
          	float t_0 = 1.0f / fabsf(x);
          	return copysignf(log1pf((fabsf(x) + (fabsf(x) / (hypotf(1.0f, t_0) + t_0)))), x);
          }
          
          function code(x)
          	t_0 = Float32(Float32(1.0) / abs(x))
          	return copysign(log1p(Float32(abs(x) + Float32(abs(x) / Float32(hypot(Float32(1.0), t_0) + t_0)))), x)
          end
          
          \begin{array}{l}
          
          \\
          \begin{array}{l}
          t_0 := \frac{1}{\left|x\right|}\\
          \mathsf{copysign}\left(\mathsf{log1p}\left(\left|x\right| + \frac{\left|x\right|}{\mathsf{hypot}\left(1, t\_0\right) + t\_0}\right), x\right)
          \end{array}
          \end{array}
          

          Reproduce

          ?
          herbie shell --seed 2024357 
          (FPCore (x)
            :name "Rust f32::asinh"
            :precision binary32
          
            :alt
            (! :herbie-platform default (let* ((ax (fabs x)) (ix (/ 1 ax))) (copysign (log1p (+ ax (/ ax (+ (hypot 1 ix) ix)))) x)))
          
            (copysign (log (+ (fabs x) (sqrt (+ (* x x) 1.0)))) x))