bug500, discussion (missed optimization)

Percentage Accurate: 54.0% → 96.9%
Time: 9.8s
Alternatives: 4
Speedup: 19.3×

Specification

?
\[\begin{array}{l} \\ \log \left(\frac{\sinh x}{x}\right) \end{array} \]
(FPCore (x) :precision binary64 (log (/ (sinh x) x)))
double code(double x) {
	return log((sinh(x) / x));
}
module fmin_fmax_functions
    implicit none
    private
    public fmax
    public fmin

    interface fmax
        module procedure fmax88
        module procedure fmax44
        module procedure fmax84
        module procedure fmax48
    end interface
    interface fmin
        module procedure fmin88
        module procedure fmin44
        module procedure fmin84
        module procedure fmin48
    end interface
contains
    real(8) function fmax88(x, y) result (res)
        real(8), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(x, max(x, y), y /= y), x /= x)
    end function
    real(4) function fmax44(x, y) result (res)
        real(4), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(y, merge(x, max(x, y), y /= y), x /= x)
    end function
    real(8) function fmax84(x, y) result(res)
        real(8), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(dble(y), merge(x, max(x, dble(y)), y /= y), x /= x)
    end function
    real(8) function fmax48(x, y) result(res)
        real(4), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(dble(x), max(dble(x), y), y /= y), x /= x)
    end function
    real(8) function fmin88(x, y) result (res)
        real(8), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(x, min(x, y), y /= y), x /= x)
    end function
    real(4) function fmin44(x, y) result (res)
        real(4), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(y, merge(x, min(x, y), y /= y), x /= x)
    end function
    real(8) function fmin84(x, y) result(res)
        real(8), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(dble(y), merge(x, min(x, dble(y)), y /= y), x /= x)
    end function
    real(8) function fmin48(x, y) result(res)
        real(4), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(dble(x), min(dble(x), y), y /= y), x /= x)
    end function
end module

real(8) function code(x)
use fmin_fmax_functions
    real(8), intent (in) :: x
    code = log((sinh(x) / x))
end function
public static double code(double x) {
	return Math.log((Math.sinh(x) / x));
}
def code(x):
	return math.log((math.sinh(x) / x))
function code(x)
	return log(Float64(sinh(x) / x))
end
function tmp = code(x)
	tmp = log((sinh(x) / x));
end
code[x_] := N[Log[N[(N[Sinh[x], $MachinePrecision] / x), $MachinePrecision]], $MachinePrecision]
\begin{array}{l}

\\
\log \left(\frac{\sinh x}{x}\right)
\end{array}

Sampling outcomes in binary64 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 4 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: 54.0% accurate, 1.0× speedup?

\[\begin{array}{l} \\ \log \left(\frac{\sinh x}{x}\right) \end{array} \]
(FPCore (x) :precision binary64 (log (/ (sinh x) x)))
double code(double x) {
	return log((sinh(x) / x));
}
module fmin_fmax_functions
    implicit none
    private
    public fmax
    public fmin

    interface fmax
        module procedure fmax88
        module procedure fmax44
        module procedure fmax84
        module procedure fmax48
    end interface
    interface fmin
        module procedure fmin88
        module procedure fmin44
        module procedure fmin84
        module procedure fmin48
    end interface
contains
    real(8) function fmax88(x, y) result (res)
        real(8), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(x, max(x, y), y /= y), x /= x)
    end function
    real(4) function fmax44(x, y) result (res)
        real(4), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(y, merge(x, max(x, y), y /= y), x /= x)
    end function
    real(8) function fmax84(x, y) result(res)
        real(8), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(dble(y), merge(x, max(x, dble(y)), y /= y), x /= x)
    end function
    real(8) function fmax48(x, y) result(res)
        real(4), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(dble(x), max(dble(x), y), y /= y), x /= x)
    end function
    real(8) function fmin88(x, y) result (res)
        real(8), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(x, min(x, y), y /= y), x /= x)
    end function
    real(4) function fmin44(x, y) result (res)
        real(4), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(y, merge(x, min(x, y), y /= y), x /= x)
    end function
    real(8) function fmin84(x, y) result(res)
        real(8), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(dble(y), merge(x, min(x, dble(y)), y /= y), x /= x)
    end function
    real(8) function fmin48(x, y) result(res)
        real(4), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(dble(x), min(dble(x), y), y /= y), x /= x)
    end function
end module

real(8) function code(x)
use fmin_fmax_functions
    real(8), intent (in) :: x
    code = log((sinh(x) / x))
end function
public static double code(double x) {
	return Math.log((Math.sinh(x) / x));
}
def code(x):
	return math.log((math.sinh(x) / x))
function code(x)
	return log(Float64(sinh(x) / x))
end
function tmp = code(x)
	tmp = log((sinh(x) / x));
end
code[x_] := N[Log[N[(N[Sinh[x], $MachinePrecision] / x), $MachinePrecision]], $MachinePrecision]
\begin{array}{l}

\\
\log \left(\frac{\sinh x}{x}\right)
\end{array}

Alternative 1: 96.9% accurate, 6.4× speedup?

\[\begin{array}{l} \\ \left(-0.027777777777777776 \cdot \frac{x}{\mathsf{fma}\left(x \cdot x, -0.005555555555555556, -0.16666666666666666\right)}\right) \cdot x \end{array} \]
(FPCore (x)
 :precision binary64
 (*
  (*
   -0.027777777777777776
   (/ x (fma (* x x) -0.005555555555555556 -0.16666666666666666)))
  x))
double code(double x) {
	return (-0.027777777777777776 * (x / fma((x * x), -0.005555555555555556, -0.16666666666666666))) * x;
}
function code(x)
	return Float64(Float64(-0.027777777777777776 * Float64(x / fma(Float64(x * x), -0.005555555555555556, -0.16666666666666666))) * x)
end
code[x_] := N[(N[(-0.027777777777777776 * N[(x / N[(N[(x * x), $MachinePrecision] * -0.005555555555555556 + -0.16666666666666666), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] * x), $MachinePrecision]
\begin{array}{l}

\\
\left(-0.027777777777777776 \cdot \frac{x}{\mathsf{fma}\left(x \cdot x, -0.005555555555555556, -0.16666666666666666\right)}\right) \cdot x
\end{array}
Derivation
  1. Initial program 52.7%

    \[\log \left(\frac{\sinh x}{x}\right) \]
  2. Add Preprocessing
  3. Taylor expanded in x around 0

    \[\leadsto \color{blue}{{x}^{2} \cdot \left(\frac{1}{6} + \frac{-1}{180} \cdot {x}^{2}\right)} \]
  4. Step-by-step derivation
    1. Applied rewrites96.2%

      \[\leadsto \color{blue}{\left(\mathsf{fma}\left(-0.005555555555555556, x \cdot x, 0.16666666666666666\right) \cdot x\right) \cdot x} \]
    2. Step-by-step derivation
      1. Applied rewrites96.2%

        \[\leadsto \frac{\mathsf{fma}\left(3.08641975308642 \cdot 10^{-5}, {x}^{4}, -0.027777777777777776\right) \cdot x}{\mathsf{fma}\left(-0.005555555555555556, x \cdot x, -0.16666666666666666\right)} \cdot x \]
      2. Taylor expanded in x around 0

        \[\leadsto \frac{\frac{-1}{36} \cdot x}{\mathsf{fma}\left(\frac{-1}{180}, x \cdot x, \frac{-1}{6}\right)} \cdot x \]
      3. Step-by-step derivation
        1. Applied rewrites96.6%

          \[\leadsto \frac{-0.027777777777777776 \cdot x}{\mathsf{fma}\left(-0.005555555555555556, x \cdot x, -0.16666666666666666\right)} \cdot x \]
        2. Step-by-step derivation
          1. Applied rewrites96.7%

            \[\leadsto \left(-0.027777777777777776 \cdot \frac{x}{\mathsf{fma}\left(x \cdot x, -0.005555555555555556, -0.16666666666666666\right)}\right) \cdot \color{blue}{x} \]
          2. Add Preprocessing

          Alternative 2: 96.5% accurate, 9.6× speedup?

          \[\begin{array}{l} \\ \mathsf{fma}\left(-0.005555555555555556, x \cdot x, 0.16666666666666666\right) \cdot \left(x \cdot x\right) \end{array} \]
          (FPCore (x)
           :precision binary64
           (* (fma -0.005555555555555556 (* x x) 0.16666666666666666) (* x x)))
          double code(double x) {
          	return fma(-0.005555555555555556, (x * x), 0.16666666666666666) * (x * x);
          }
          
          function code(x)
          	return Float64(fma(-0.005555555555555556, Float64(x * x), 0.16666666666666666) * Float64(x * x))
          end
          
          code[x_] := N[(N[(-0.005555555555555556 * N[(x * x), $MachinePrecision] + 0.16666666666666666), $MachinePrecision] * N[(x * x), $MachinePrecision]), $MachinePrecision]
          
          \begin{array}{l}
          
          \\
          \mathsf{fma}\left(-0.005555555555555556, x \cdot x, 0.16666666666666666\right) \cdot \left(x \cdot x\right)
          \end{array}
          
          Derivation
          1. Initial program 52.7%

            \[\log \left(\frac{\sinh x}{x}\right) \]
          2. Add Preprocessing
          3. Taylor expanded in x around 0

            \[\leadsto \color{blue}{{x}^{2} \cdot \left(\frac{1}{6} + {x}^{2} \cdot \left({x}^{2} \cdot \left(\frac{1}{2835} + \frac{-1}{37800} \cdot {x}^{2}\right) - \frac{1}{180}\right)\right)} \]
          4. Step-by-step derivation
            1. Applied rewrites96.4%

              \[\leadsto \color{blue}{\mathsf{fma}\left(\left(\mathsf{fma}\left(-2.6455026455026456 \cdot 10^{-5}, x \cdot x, 0.0003527336860670194\right) \cdot x\right) \cdot x - 0.005555555555555556, x \cdot x, 0.16666666666666666\right) \cdot \left(x \cdot x\right)} \]
            2. Taylor expanded in x around 0

              \[\leadsto \mathsf{fma}\left(\frac{-1}{180}, x \cdot x, \frac{1}{6}\right) \cdot \left(x \cdot x\right) \]
            3. Step-by-step derivation
              1. Applied rewrites96.3%

                \[\leadsto \mathsf{fma}\left(-0.005555555555555556, x \cdot x, 0.16666666666666666\right) \cdot \left(x \cdot x\right) \]
              2. Add Preprocessing

              Alternative 3: 96.5% accurate, 9.6× speedup?

              \[\begin{array}{l} \\ \left(\mathsf{fma}\left(-0.005555555555555556, x \cdot x, 0.16666666666666666\right) \cdot x\right) \cdot x \end{array} \]
              (FPCore (x)
               :precision binary64
               (* (* (fma -0.005555555555555556 (* x x) 0.16666666666666666) x) x))
              double code(double x) {
              	return (fma(-0.005555555555555556, (x * x), 0.16666666666666666) * x) * x;
              }
              
              function code(x)
              	return Float64(Float64(fma(-0.005555555555555556, Float64(x * x), 0.16666666666666666) * x) * x)
              end
              
              code[x_] := N[(N[(N[(-0.005555555555555556 * N[(x * x), $MachinePrecision] + 0.16666666666666666), $MachinePrecision] * x), $MachinePrecision] * x), $MachinePrecision]
              
              \begin{array}{l}
              
              \\
              \left(\mathsf{fma}\left(-0.005555555555555556, x \cdot x, 0.16666666666666666\right) \cdot x\right) \cdot x
              \end{array}
              
              Derivation
              1. Initial program 52.7%

                \[\log \left(\frac{\sinh x}{x}\right) \]
              2. Add Preprocessing
              3. Taylor expanded in x around 0

                \[\leadsto \color{blue}{{x}^{2} \cdot \left(\frac{1}{6} + \frac{-1}{180} \cdot {x}^{2}\right)} \]
              4. Step-by-step derivation
                1. Applied rewrites96.2%

                  \[\leadsto \color{blue}{\left(\mathsf{fma}\left(-0.005555555555555556, x \cdot x, 0.16666666666666666\right) \cdot x\right) \cdot x} \]
                2. Add Preprocessing

                Alternative 4: 96.4% accurate, 19.3× speedup?

                \[\begin{array}{l} \\ \left(x \cdot x\right) \cdot 0.16666666666666666 \end{array} \]
                (FPCore (x) :precision binary64 (* (* x x) 0.16666666666666666))
                double code(double x) {
                	return (x * x) * 0.16666666666666666;
                }
                
                module fmin_fmax_functions
                    implicit none
                    private
                    public fmax
                    public fmin
                
                    interface fmax
                        module procedure fmax88
                        module procedure fmax44
                        module procedure fmax84
                        module procedure fmax48
                    end interface
                    interface fmin
                        module procedure fmin88
                        module procedure fmin44
                        module procedure fmin84
                        module procedure fmin48
                    end interface
                contains
                    real(8) function fmax88(x, y) result (res)
                        real(8), intent (in) :: x
                        real(8), intent (in) :: y
                        res = merge(y, merge(x, max(x, y), y /= y), x /= x)
                    end function
                    real(4) function fmax44(x, y) result (res)
                        real(4), intent (in) :: x
                        real(4), intent (in) :: y
                        res = merge(y, merge(x, max(x, y), y /= y), x /= x)
                    end function
                    real(8) function fmax84(x, y) result(res)
                        real(8), intent (in) :: x
                        real(4), intent (in) :: y
                        res = merge(dble(y), merge(x, max(x, dble(y)), y /= y), x /= x)
                    end function
                    real(8) function fmax48(x, y) result(res)
                        real(4), intent (in) :: x
                        real(8), intent (in) :: y
                        res = merge(y, merge(dble(x), max(dble(x), y), y /= y), x /= x)
                    end function
                    real(8) function fmin88(x, y) result (res)
                        real(8), intent (in) :: x
                        real(8), intent (in) :: y
                        res = merge(y, merge(x, min(x, y), y /= y), x /= x)
                    end function
                    real(4) function fmin44(x, y) result (res)
                        real(4), intent (in) :: x
                        real(4), intent (in) :: y
                        res = merge(y, merge(x, min(x, y), y /= y), x /= x)
                    end function
                    real(8) function fmin84(x, y) result(res)
                        real(8), intent (in) :: x
                        real(4), intent (in) :: y
                        res = merge(dble(y), merge(x, min(x, dble(y)), y /= y), x /= x)
                    end function
                    real(8) function fmin48(x, y) result(res)
                        real(4), intent (in) :: x
                        real(8), intent (in) :: y
                        res = merge(y, merge(dble(x), min(dble(x), y), y /= y), x /= x)
                    end function
                end module
                
                real(8) function code(x)
                use fmin_fmax_functions
                    real(8), intent (in) :: x
                    code = (x * x) * 0.16666666666666666d0
                end function
                
                public static double code(double x) {
                	return (x * x) * 0.16666666666666666;
                }
                
                def code(x):
                	return (x * x) * 0.16666666666666666
                
                function code(x)
                	return Float64(Float64(x * x) * 0.16666666666666666)
                end
                
                function tmp = code(x)
                	tmp = (x * x) * 0.16666666666666666;
                end
                
                code[x_] := N[(N[(x * x), $MachinePrecision] * 0.16666666666666666), $MachinePrecision]
                
                \begin{array}{l}
                
                \\
                \left(x \cdot x\right) \cdot 0.16666666666666666
                \end{array}
                
                Derivation
                1. Initial program 52.7%

                  \[\log \left(\frac{\sinh x}{x}\right) \]
                2. Add Preprocessing
                3. Taylor expanded in x around 0

                  \[\leadsto \color{blue}{\frac{1}{6} \cdot {x}^{2}} \]
                4. Step-by-step derivation
                  1. Applied rewrites96.1%

                    \[\leadsto \color{blue}{\left(x \cdot x\right) \cdot 0.16666666666666666} \]
                  2. Add Preprocessing

                  Developer Target 1: 97.8% accurate, 1.0× speedup?

                  \[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;\left|x\right| < 0.085:\\ \;\;\;\;\left(x \cdot x\right) \cdot \mathsf{fma}\left(\mathsf{fma}\left(\mathsf{fma}\left(-2.6455026455026456 \cdot 10^{-5}, x \cdot x, 0.0003527336860670194\right), x \cdot x, -0.005555555555555556\right), x \cdot x, 0.16666666666666666\right)\\ \mathbf{else}:\\ \;\;\;\;\log \left(\frac{\sinh x}{x}\right)\\ \end{array} \end{array} \]
                  (FPCore (x)
                   :precision binary64
                   (if (< (fabs x) 0.085)
                     (*
                      (* x x)
                      (fma
                       (fma
                        (fma -2.6455026455026456e-5 (* x x) 0.0003527336860670194)
                        (* x x)
                        -0.005555555555555556)
                       (* x x)
                       0.16666666666666666))
                     (log (/ (sinh x) x))))
                  double code(double x) {
                  	double tmp;
                  	if (fabs(x) < 0.085) {
                  		tmp = (x * x) * fma(fma(fma(-2.6455026455026456e-5, (x * x), 0.0003527336860670194), (x * x), -0.005555555555555556), (x * x), 0.16666666666666666);
                  	} else {
                  		tmp = log((sinh(x) / x));
                  	}
                  	return tmp;
                  }
                  
                  function code(x)
                  	tmp = 0.0
                  	if (abs(x) < 0.085)
                  		tmp = Float64(Float64(x * x) * fma(fma(fma(-2.6455026455026456e-5, Float64(x * x), 0.0003527336860670194), Float64(x * x), -0.005555555555555556), Float64(x * x), 0.16666666666666666));
                  	else
                  		tmp = log(Float64(sinh(x) / x));
                  	end
                  	return tmp
                  end
                  
                  code[x_] := If[Less[N[Abs[x], $MachinePrecision], 0.085], N[(N[(x * x), $MachinePrecision] * N[(N[(N[(-2.6455026455026456e-5 * N[(x * x), $MachinePrecision] + 0.0003527336860670194), $MachinePrecision] * N[(x * x), $MachinePrecision] + -0.005555555555555556), $MachinePrecision] * N[(x * x), $MachinePrecision] + 0.16666666666666666), $MachinePrecision]), $MachinePrecision], N[Log[N[(N[Sinh[x], $MachinePrecision] / x), $MachinePrecision]], $MachinePrecision]]
                  
                  \begin{array}{l}
                  
                  \\
                  \begin{array}{l}
                  \mathbf{if}\;\left|x\right| < 0.085:\\
                  \;\;\;\;\left(x \cdot x\right) \cdot \mathsf{fma}\left(\mathsf{fma}\left(\mathsf{fma}\left(-2.6455026455026456 \cdot 10^{-5}, x \cdot x, 0.0003527336860670194\right), x \cdot x, -0.005555555555555556\right), x \cdot x, 0.16666666666666666\right)\\
                  
                  \mathbf{else}:\\
                  \;\;\;\;\log \left(\frac{\sinh x}{x}\right)\\
                  
                  
                  \end{array}
                  \end{array}
                  

                  Reproduce

                  ?
                  herbie shell --seed 2025026 
                  (FPCore (x)
                    :name "bug500, discussion (missed optimization)"
                    :precision binary64
                  
                    :alt
                    (! :herbie-platform default (if (< (fabs x) 17/200) (let ((x2 (* x x))) (* x2 (fma (fma (fma -1/37800 x2 1/2835) x2 -1/180) x2 1/6))) (log (/ (sinh x) x))))
                  
                    (log (/ (sinh x) x)))