Codec.Picture.Jpg.FastDct:referenceDct from JuicyPixels-3.2.6.1

?

Percentage Accurate: 27.0% → 31.5%
Time: 20.1s
Precision: binary64
Cost: 54212

?

\[\left(x \cdot \cos \left(\frac{\left(\left(y \cdot 2 + 1\right) \cdot z\right) \cdot t}{16}\right)\right) \cdot \cos \left(\frac{\left(\left(a \cdot 2 + 1\right) \cdot b\right) \cdot t}{16}\right) \]
\[\begin{array}{l} \mathbf{if}\;\left(x \cdot \cos \left(\frac{\left(\left(y \cdot 2 + 1\right) \cdot z\right) \cdot t}{16}\right)\right) \cdot \cos \left(\frac{t \cdot \left(\left(1 + 2 \cdot a\right) \cdot b\right)}{16}\right) \leq 10^{+288}:\\ \;\;\;\;x \cdot \left(\cos \left(\mathsf{fma}\left(y, 2, 1\right) \cdot \left(t \cdot \frac{z}{16}\right)\right) \cdot \log \left(e^{\cos \left(\mathsf{fma}\left(2, a, 1\right) \cdot \left(b \cdot \left(t \cdot 0.0625\right)\right)\right)}\right)\right)\\ \mathbf{else}:\\ \;\;\;\;x\\ \end{array} \]
(FPCore (x y z t a b)
 :precision binary64
 (*
  (* x (cos (/ (* (* (+ (* y 2.0) 1.0) z) t) 16.0)))
  (cos (/ (* (* (+ (* a 2.0) 1.0) b) t) 16.0))))
(FPCore (x y z t a b)
 :precision binary64
 (if (<=
      (*
       (* x (cos (/ (* (* (+ (* y 2.0) 1.0) z) t) 16.0)))
       (cos (/ (* t (* (+ 1.0 (* 2.0 a)) b)) 16.0)))
      1e+288)
   (*
    x
    (*
     (cos (* (fma y 2.0 1.0) (* t (/ z 16.0))))
     (log (exp (cos (* (fma 2.0 a 1.0) (* b (* t 0.0625))))))))
   x))
double code(double x, double y, double z, double t, double a, double b) {
	return (x * cos((((((y * 2.0) + 1.0) * z) * t) / 16.0))) * cos((((((a * 2.0) + 1.0) * b) * t) / 16.0));
}
double code(double x, double y, double z, double t, double a, double b) {
	double tmp;
	if (((x * cos((((((y * 2.0) + 1.0) * z) * t) / 16.0))) * cos(((t * ((1.0 + (2.0 * a)) * b)) / 16.0))) <= 1e+288) {
		tmp = x * (cos((fma(y, 2.0, 1.0) * (t * (z / 16.0)))) * log(exp(cos((fma(2.0, a, 1.0) * (b * (t * 0.0625)))))));
	} else {
		tmp = x;
	}
	return tmp;
}
function code(x, y, z, t, a, b)
	return Float64(Float64(x * cos(Float64(Float64(Float64(Float64(Float64(y * 2.0) + 1.0) * z) * t) / 16.0))) * cos(Float64(Float64(Float64(Float64(Float64(a * 2.0) + 1.0) * b) * t) / 16.0)))
end
function code(x, y, z, t, a, b)
	tmp = 0.0
	if (Float64(Float64(x * cos(Float64(Float64(Float64(Float64(Float64(y * 2.0) + 1.0) * z) * t) / 16.0))) * cos(Float64(Float64(t * Float64(Float64(1.0 + Float64(2.0 * a)) * b)) / 16.0))) <= 1e+288)
		tmp = Float64(x * Float64(cos(Float64(fma(y, 2.0, 1.0) * Float64(t * Float64(z / 16.0)))) * log(exp(cos(Float64(fma(2.0, a, 1.0) * Float64(b * Float64(t * 0.0625))))))));
	else
		tmp = x;
	end
	return tmp
end
code[x_, y_, z_, t_, a_, b_] := N[(N[(x * N[Cos[N[(N[(N[(N[(N[(y * 2.0), $MachinePrecision] + 1.0), $MachinePrecision] * z), $MachinePrecision] * t), $MachinePrecision] / 16.0), $MachinePrecision]], $MachinePrecision]), $MachinePrecision] * N[Cos[N[(N[(N[(N[(N[(a * 2.0), $MachinePrecision] + 1.0), $MachinePrecision] * b), $MachinePrecision] * t), $MachinePrecision] / 16.0), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]
code[x_, y_, z_, t_, a_, b_] := If[LessEqual[N[(N[(x * N[Cos[N[(N[(N[(N[(N[(y * 2.0), $MachinePrecision] + 1.0), $MachinePrecision] * z), $MachinePrecision] * t), $MachinePrecision] / 16.0), $MachinePrecision]], $MachinePrecision]), $MachinePrecision] * N[Cos[N[(N[(t * N[(N[(1.0 + N[(2.0 * a), $MachinePrecision]), $MachinePrecision] * b), $MachinePrecision]), $MachinePrecision] / 16.0), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], 1e+288], N[(x * N[(N[Cos[N[(N[(y * 2.0 + 1.0), $MachinePrecision] * N[(t * N[(z / 16.0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision] * N[Log[N[Exp[N[Cos[N[(N[(2.0 * a + 1.0), $MachinePrecision] * N[(b * N[(t * 0.0625), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]], $MachinePrecision]], $MachinePrecision]), $MachinePrecision]), $MachinePrecision], x]
\left(x \cdot \cos \left(\frac{\left(\left(y \cdot 2 + 1\right) \cdot z\right) \cdot t}{16}\right)\right) \cdot \cos \left(\frac{\left(\left(a \cdot 2 + 1\right) \cdot b\right) \cdot t}{16}\right)
\begin{array}{l}
\mathbf{if}\;\left(x \cdot \cos \left(\frac{\left(\left(y \cdot 2 + 1\right) \cdot z\right) \cdot t}{16}\right)\right) \cdot \cos \left(\frac{t \cdot \left(\left(1 + 2 \cdot a\right) \cdot b\right)}{16}\right) \leq 10^{+288}:\\
\;\;\;\;x \cdot \left(\cos \left(\mathsf{fma}\left(y, 2, 1\right) \cdot \left(t \cdot \frac{z}{16}\right)\right) \cdot \log \left(e^{\cos \left(\mathsf{fma}\left(2, a, 1\right) \cdot \left(b \cdot \left(t \cdot 0.0625\right)\right)\right)}\right)\right)\\

\mathbf{else}:\\
\;\;\;\;x\\


\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.

Herbie found 3 alternatives:

AlternativeAccuracySpeedup

Accuracy vs Speed

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.

Bogosity?

Bogosity

Target

Original27.0%
Target29.7%
Herbie31.5%
\[x \cdot \cos \left(\frac{b}{16} \cdot \frac{t}{\left(1 - a \cdot 2\right) + {\left(a \cdot 2\right)}^{2}}\right) \]

Derivation?

  1. Split input into 2 regimes
  2. if (*.f64 (*.f64 x (cos.f64 (/.f64 (*.f64 (*.f64 (+.f64 (*.f64 y 2) 1) z) t) 16))) (cos.f64 (/.f64 (*.f64 (*.f64 (+.f64 (*.f64 a 2) 1) b) t) 16))) < 1e288

    1. Initial program 40.9%

      \[\left(x \cdot \cos \left(\frac{\left(\left(y \cdot 2 + 1\right) \cdot z\right) \cdot t}{16}\right)\right) \cdot \cos \left(\frac{\left(\left(a \cdot 2 + 1\right) \cdot b\right) \cdot t}{16}\right) \]
    2. Simplified40.9%

      \[\leadsto \color{blue}{x \cdot \left(\cos \left(\mathsf{fma}\left(y, 2, 1\right) \cdot \left(\frac{z}{16} \cdot t\right)\right) \cdot \cos \left(\left(\mathsf{fma}\left(2, a, 1\right) \cdot b\right) \cdot \frac{t}{16}\right)\right)} \]
      Step-by-step derivation

      [Start]40.9%

      \[ \left(x \cdot \cos \left(\frac{\left(\left(y \cdot 2 + 1\right) \cdot z\right) \cdot t}{16}\right)\right) \cdot \cos \left(\frac{\left(\left(a \cdot 2 + 1\right) \cdot b\right) \cdot t}{16}\right) \]

      associate-*l* [=>]40.9%

      \[ \color{blue}{x \cdot \left(\cos \left(\frac{\left(\left(y \cdot 2 + 1\right) \cdot z\right) \cdot t}{16}\right) \cdot \cos \left(\frac{\left(\left(a \cdot 2 + 1\right) \cdot b\right) \cdot t}{16}\right)\right)} \]

      associate-/l* [=>]40.4%

      \[ x \cdot \left(\cos \color{blue}{\left(\frac{\left(y \cdot 2 + 1\right) \cdot z}{\frac{16}{t}}\right)} \cdot \cos \left(\frac{\left(\left(a \cdot 2 + 1\right) \cdot b\right) \cdot t}{16}\right)\right) \]

      associate-*r/ [<=]40.8%

      \[ x \cdot \left(\cos \color{blue}{\left(\left(y \cdot 2 + 1\right) \cdot \frac{z}{\frac{16}{t}}\right)} \cdot \cos \left(\frac{\left(\left(a \cdot 2 + 1\right) \cdot b\right) \cdot t}{16}\right)\right) \]

      associate-/r/ [=>]40.9%

      \[ x \cdot \left(\cos \left(\left(y \cdot 2 + 1\right) \cdot \color{blue}{\left(\frac{z}{16} \cdot t\right)}\right) \cdot \cos \left(\frac{\left(\left(a \cdot 2 + 1\right) \cdot b\right) \cdot t}{16}\right)\right) \]

      fma-def [=>]40.9%

      \[ x \cdot \left(\cos \left(\color{blue}{\mathsf{fma}\left(y, 2, 1\right)} \cdot \left(\frac{z}{16} \cdot t\right)\right) \cdot \cos \left(\frac{\left(\left(a \cdot 2 + 1\right) \cdot b\right) \cdot t}{16}\right)\right) \]

      associate-*r/ [<=]40.9%

      \[ x \cdot \left(\cos \left(\mathsf{fma}\left(y, 2, 1\right) \cdot \left(\frac{z}{16} \cdot t\right)\right) \cdot \cos \color{blue}{\left(\left(\left(a \cdot 2 + 1\right) \cdot b\right) \cdot \frac{t}{16}\right)}\right) \]

      *-commutative [=>]40.9%

      \[ x \cdot \left(\cos \left(\mathsf{fma}\left(y, 2, 1\right) \cdot \left(\frac{z}{16} \cdot t\right)\right) \cdot \cos \left(\left(\left(\color{blue}{2 \cdot a} + 1\right) \cdot b\right) \cdot \frac{t}{16}\right)\right) \]

      fma-def [=>]40.9%

      \[ x \cdot \left(\cos \left(\mathsf{fma}\left(y, 2, 1\right) \cdot \left(\frac{z}{16} \cdot t\right)\right) \cdot \cos \left(\left(\color{blue}{\mathsf{fma}\left(2, a, 1\right)} \cdot b\right) \cdot \frac{t}{16}\right)\right) \]
    3. Applied egg-rr41.2%

      \[\leadsto x \cdot \left(\cos \left(\mathsf{fma}\left(y, 2, 1\right) \cdot \left(\frac{z}{16} \cdot t\right)\right) \cdot \color{blue}{\log \left(e^{\cos \left(\mathsf{fma}\left(2, a, 1\right) \cdot \left(b \cdot \left(t \cdot 0.0625\right)\right)\right)}\right)}\right) \]
      Step-by-step derivation

      [Start]40.9%

      \[ x \cdot \left(\cos \left(\mathsf{fma}\left(y, 2, 1\right) \cdot \left(\frac{z}{16} \cdot t\right)\right) \cdot \cos \left(\left(\mathsf{fma}\left(2, a, 1\right) \cdot b\right) \cdot \frac{t}{16}\right)\right) \]

      add-log-exp [=>]40.9%

      \[ x \cdot \left(\cos \left(\mathsf{fma}\left(y, 2, 1\right) \cdot \left(\frac{z}{16} \cdot t\right)\right) \cdot \color{blue}{\log \left(e^{\cos \left(\left(\mathsf{fma}\left(2, a, 1\right) \cdot b\right) \cdot \frac{t}{16}\right)}\right)}\right) \]

      associate-*l* [=>]41.2%

      \[ x \cdot \left(\cos \left(\mathsf{fma}\left(y, 2, 1\right) \cdot \left(\frac{z}{16} \cdot t\right)\right) \cdot \log \left(e^{\cos \color{blue}{\left(\mathsf{fma}\left(2, a, 1\right) \cdot \left(b \cdot \frac{t}{16}\right)\right)}}\right)\right) \]

      div-inv [=>]41.2%

      \[ x \cdot \left(\cos \left(\mathsf{fma}\left(y, 2, 1\right) \cdot \left(\frac{z}{16} \cdot t\right)\right) \cdot \log \left(e^{\cos \left(\mathsf{fma}\left(2, a, 1\right) \cdot \left(b \cdot \color{blue}{\left(t \cdot \frac{1}{16}\right)}\right)\right)}\right)\right) \]

      metadata-eval [=>]41.2%

      \[ x \cdot \left(\cos \left(\mathsf{fma}\left(y, 2, 1\right) \cdot \left(\frac{z}{16} \cdot t\right)\right) \cdot \log \left(e^{\cos \left(\mathsf{fma}\left(2, a, 1\right) \cdot \left(b \cdot \left(t \cdot \color{blue}{0.0625}\right)\right)\right)}\right)\right) \]

    if 1e288 < (*.f64 (*.f64 x (cos.f64 (/.f64 (*.f64 (*.f64 (+.f64 (*.f64 y 2) 1) z) t) 16))) (cos.f64 (/.f64 (*.f64 (*.f64 (+.f64 (*.f64 a 2) 1) b) t) 16)))

    1. Initial program 0.9%

      \[\left(x \cdot \cos \left(\frac{\left(\left(y \cdot 2 + 1\right) \cdot z\right) \cdot t}{16}\right)\right) \cdot \cos \left(\frac{\left(\left(a \cdot 2 + 1\right) \cdot b\right) \cdot t}{16}\right) \]
    2. Simplified2.2%

      \[\leadsto \color{blue}{x \cdot \left(\cos \left(\left(z \cdot t\right) \cdot \left(0.0625 + \frac{y}{8}\right)\right) \cdot \cos \left(\left(t \cdot b\right) \cdot \left(0.0625 + \frac{a}{8}\right)\right)\right)} \]
      Step-by-step derivation

      [Start]0.9%

      \[ \left(x \cdot \cos \left(\frac{\left(\left(y \cdot 2 + 1\right) \cdot z\right) \cdot t}{16}\right)\right) \cdot \cos \left(\frac{\left(\left(a \cdot 2 + 1\right) \cdot b\right) \cdot t}{16}\right) \]

      associate-*l* [=>]0.9%

      \[ \color{blue}{x \cdot \left(\cos \left(\frac{\left(\left(y \cdot 2 + 1\right) \cdot z\right) \cdot t}{16}\right) \cdot \cos \left(\frac{\left(\left(a \cdot 2 + 1\right) \cdot b\right) \cdot t}{16}\right)\right)} \]
    3. Taylor expanded in t around 0 6.1%

      \[\leadsto x \cdot \left(\cos \left(\left(z \cdot t\right) \cdot \left(0.0625 + \frac{y}{8}\right)\right) \cdot \color{blue}{1}\right) \]
    4. Taylor expanded in z around 0 12.2%

      \[\leadsto \color{blue}{x} \]
  3. Recombined 2 regimes into one program.
  4. Final simplification28.4%

    \[\leadsto \begin{array}{l} \mathbf{if}\;\left(x \cdot \cos \left(\frac{\left(\left(y \cdot 2 + 1\right) \cdot z\right) \cdot t}{16}\right)\right) \cdot \cos \left(\frac{t \cdot \left(\left(1 + 2 \cdot a\right) \cdot b\right)}{16}\right) \leq 10^{+288}:\\ \;\;\;\;x \cdot \left(\cos \left(\mathsf{fma}\left(y, 2, 1\right) \cdot \left(t \cdot \frac{z}{16}\right)\right) \cdot \log \left(e^{\cos \left(\mathsf{fma}\left(2, a, 1\right) \cdot \left(b \cdot \left(t \cdot 0.0625\right)\right)\right)}\right)\right)\\ \mathbf{else}:\\ \;\;\;\;x\\ \end{array} \]

Alternatives

Alternative 1
Accuracy31.5%
Cost54212
\[\begin{array}{l} \mathbf{if}\;\left(x \cdot \cos \left(\frac{\left(\left(y \cdot 2 + 1\right) \cdot z\right) \cdot t}{16}\right)\right) \cdot \cos \left(\frac{t \cdot \left(\left(1 + 2 \cdot a\right) \cdot b\right)}{16}\right) \leq 10^{+288}:\\ \;\;\;\;x \cdot \left(\cos \left(\mathsf{fma}\left(y, 2, 1\right) \cdot \left(t \cdot \frac{z}{16}\right)\right) \cdot \log \left(e^{\cos \left(\mathsf{fma}\left(2, a, 1\right) \cdot \left(b \cdot \left(t \cdot 0.0625\right)\right)\right)}\right)\right)\\ \mathbf{else}:\\ \;\;\;\;x\\ \end{array} \]
Alternative 2
Accuracy31.5%
Cost28612
\[\begin{array}{l} \mathbf{if}\;\left(x \cdot \cos \left(\frac{\left(\left(y \cdot 2 + 1\right) \cdot z\right) \cdot t}{16}\right)\right) \cdot \cos \left(\frac{t \cdot \left(\left(1 + 2 \cdot a\right) \cdot b\right)}{16}\right) \leq 10^{+288}:\\ \;\;\;\;x \cdot \left(\cos \left(\left(z \cdot t\right) \cdot \left(0.0625 + \frac{y}{8}\right)\right) \cdot \cos \left(\left(t \cdot b\right) \cdot \left(0.0625 + \frac{a}{8}\right)\right)\right)\\ \mathbf{else}:\\ \;\;\;\;x\\ \end{array} \]
Alternative 3
Accuracy30.3%
Cost64
\[x \]

Reproduce?

herbie shell --seed 2023171 
(FPCore (x y z t a b)
  :name "Codec.Picture.Jpg.FastDct:referenceDct from JuicyPixels-3.2.6.1"
  :precision binary64

  :herbie-target
  (* x (cos (* (/ b 16.0) (/ t (+ (- 1.0 (* a 2.0)) (pow (* a 2.0) 2.0))))))

  (* (* x (cos (/ (* (* (+ (* y 2.0) 1.0) z) t) 16.0))) (cos (/ (* (* (+ (* a 2.0) 1.0) b) t) 16.0))))