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

Percentage Accurate: 27.7% → 32.0%
Time: 6.2s
Alternatives: 3
Speedup: 269.0×

Specification

?
\[\begin{array}{l} \\ \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) \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))))
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));
}
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, y, z, t, a, b)
use fmin_fmax_functions
    real(8), intent (in) :: x
    real(8), intent (in) :: y
    real(8), intent (in) :: z
    real(8), intent (in) :: t
    real(8), intent (in) :: a
    real(8), intent (in) :: b
    code = (x * cos((((((y * 2.0d0) + 1.0d0) * z) * t) / 16.0d0))) * cos((((((a * 2.0d0) + 1.0d0) * b) * t) / 16.0d0))
end function
public static double code(double x, double y, double z, double t, double a, double b) {
	return (x * Math.cos((((((y * 2.0) + 1.0) * z) * t) / 16.0))) * Math.cos((((((a * 2.0) + 1.0) * b) * t) / 16.0));
}
def code(x, y, z, t, a, b):
	return (x * math.cos((((((y * 2.0) + 1.0) * z) * t) / 16.0))) * math.cos((((((a * 2.0) + 1.0) * b) * t) / 16.0))
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 tmp = code(x, y, z, t, a, b)
	tmp = (x * cos((((((y * 2.0) + 1.0) * z) * t) / 16.0))) * cos((((((a * 2.0) + 1.0) * b) * t) / 16.0));
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]
\begin{array}{l}

\\
\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)
\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: 27.7% accurate, 1.0× speedup?

\[\begin{array}{l} \\ \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) \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))))
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));
}
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, y, z, t, a, b)
use fmin_fmax_functions
    real(8), intent (in) :: x
    real(8), intent (in) :: y
    real(8), intent (in) :: z
    real(8), intent (in) :: t
    real(8), intent (in) :: a
    real(8), intent (in) :: b
    code = (x * cos((((((y * 2.0d0) + 1.0d0) * z) * t) / 16.0d0))) * cos((((((a * 2.0d0) + 1.0d0) * b) * t) / 16.0d0))
end function
public static double code(double x, double y, double z, double t, double a, double b) {
	return (x * Math.cos((((((y * 2.0) + 1.0) * z) * t) / 16.0))) * Math.cos((((((a * 2.0) + 1.0) * b) * t) / 16.0));
}
def code(x, y, z, t, a, b):
	return (x * math.cos((((((y * 2.0) + 1.0) * z) * t) / 16.0))) * math.cos((((((a * 2.0) + 1.0) * b) * t) / 16.0))
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 tmp = code(x, y, z, t, a, b)
	tmp = (x * cos((((((y * 2.0) + 1.0) * z) * t) / 16.0))) * cos((((((a * 2.0) + 1.0) * b) * t) / 16.0));
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]
\begin{array}{l}

\\
\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)
\end{array}

Alternative 1: 32.0% accurate, 0.5× speedup?

\[\begin{array}{l} t_m = \left|t\right| \\ b_m = \left|b\right| \\ x\_m = \left|x\right| \\ x\_s = \mathsf{copysign}\left(1, x\right) \\ \begin{array}{l} t_1 := x\_m \cdot \cos \left(\frac{\left(\left(y \cdot 2 + 1\right) \cdot z\right) \cdot t\_m}{16}\right)\\ x\_s \cdot \begin{array}{l} \mathbf{if}\;t\_1 \cdot \cos \left(\frac{\left(\left(a \cdot 2 + 1\right) \cdot b\_m\right) \cdot t\_m}{16}\right) \leq 10^{+258}:\\ \;\;\;\;t\_1 \cdot \sin \left(\left(b\_m \cdot t\_m\right) \cdot 0.0625 + \frac{\pi}{2}\right)\\ \mathbf{else}:\\ \;\;\;\;x\_m\\ \end{array} \end{array} \end{array} \]
t_m = (fabs.f64 t)
b_m = (fabs.f64 b)
x\_m = (fabs.f64 x)
x\_s = (copysign.f64 #s(literal 1 binary64) x)
(FPCore (x_s x_m y z t_m a b_m)
 :precision binary64
 (let* ((t_1 (* x_m (cos (/ (* (* (+ (* y 2.0) 1.0) z) t_m) 16.0)))))
   (*
    x_s
    (if (<= (* t_1 (cos (/ (* (* (+ (* a 2.0) 1.0) b_m) t_m) 16.0))) 1e+258)
      (* t_1 (sin (+ (* (* b_m t_m) 0.0625) (/ PI 2.0))))
      x_m))))
t_m = fabs(t);
b_m = fabs(b);
x\_m = fabs(x);
x\_s = copysign(1.0, x);
double code(double x_s, double x_m, double y, double z, double t_m, double a, double b_m) {
	double t_1 = x_m * cos((((((y * 2.0) + 1.0) * z) * t_m) / 16.0));
	double tmp;
	if ((t_1 * cos((((((a * 2.0) + 1.0) * b_m) * t_m) / 16.0))) <= 1e+258) {
		tmp = t_1 * sin((((b_m * t_m) * 0.0625) + (((double) M_PI) / 2.0)));
	} else {
		tmp = x_m;
	}
	return x_s * tmp;
}
t_m = Math.abs(t);
b_m = Math.abs(b);
x\_m = Math.abs(x);
x\_s = Math.copySign(1.0, x);
public static double code(double x_s, double x_m, double y, double z, double t_m, double a, double b_m) {
	double t_1 = x_m * Math.cos((((((y * 2.0) + 1.0) * z) * t_m) / 16.0));
	double tmp;
	if ((t_1 * Math.cos((((((a * 2.0) + 1.0) * b_m) * t_m) / 16.0))) <= 1e+258) {
		tmp = t_1 * Math.sin((((b_m * t_m) * 0.0625) + (Math.PI / 2.0)));
	} else {
		tmp = x_m;
	}
	return x_s * tmp;
}
t_m = math.fabs(t)
b_m = math.fabs(b)
x\_m = math.fabs(x)
x\_s = math.copysign(1.0, x)
def code(x_s, x_m, y, z, t_m, a, b_m):
	t_1 = x_m * math.cos((((((y * 2.0) + 1.0) * z) * t_m) / 16.0))
	tmp = 0
	if (t_1 * math.cos((((((a * 2.0) + 1.0) * b_m) * t_m) / 16.0))) <= 1e+258:
		tmp = t_1 * math.sin((((b_m * t_m) * 0.0625) + (math.pi / 2.0)))
	else:
		tmp = x_m
	return x_s * tmp
t_m = abs(t)
b_m = abs(b)
x\_m = abs(x)
x\_s = copysign(1.0, x)
function code(x_s, x_m, y, z, t_m, a, b_m)
	t_1 = Float64(x_m * cos(Float64(Float64(Float64(Float64(Float64(y * 2.0) + 1.0) * z) * t_m) / 16.0)))
	tmp = 0.0
	if (Float64(t_1 * cos(Float64(Float64(Float64(Float64(Float64(a * 2.0) + 1.0) * b_m) * t_m) / 16.0))) <= 1e+258)
		tmp = Float64(t_1 * sin(Float64(Float64(Float64(b_m * t_m) * 0.0625) + Float64(pi / 2.0))));
	else
		tmp = x_m;
	end
	return Float64(x_s * tmp)
end
t_m = abs(t);
b_m = abs(b);
x\_m = abs(x);
x\_s = sign(x) * abs(1.0);
function tmp_2 = code(x_s, x_m, y, z, t_m, a, b_m)
	t_1 = x_m * cos((((((y * 2.0) + 1.0) * z) * t_m) / 16.0));
	tmp = 0.0;
	if ((t_1 * cos((((((a * 2.0) + 1.0) * b_m) * t_m) / 16.0))) <= 1e+258)
		tmp = t_1 * sin((((b_m * t_m) * 0.0625) + (pi / 2.0)));
	else
		tmp = x_m;
	end
	tmp_2 = x_s * tmp;
end
t_m = N[Abs[t], $MachinePrecision]
b_m = N[Abs[b], $MachinePrecision]
x\_m = N[Abs[x], $MachinePrecision]
x\_s = N[With[{TMP1 = Abs[1.0], TMP2 = Sign[x]}, TMP1 * If[TMP2 == 0, 1, TMP2]], $MachinePrecision]
code[x$95$s_, x$95$m_, y_, z_, t$95$m_, a_, b$95$m_] := Block[{t$95$1 = N[(x$95$m * N[Cos[N[(N[(N[(N[(N[(y * 2.0), $MachinePrecision] + 1.0), $MachinePrecision] * z), $MachinePrecision] * t$95$m), $MachinePrecision] / 16.0), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]}, N[(x$95$s * If[LessEqual[N[(t$95$1 * N[Cos[N[(N[(N[(N[(N[(a * 2.0), $MachinePrecision] + 1.0), $MachinePrecision] * b$95$m), $MachinePrecision] * t$95$m), $MachinePrecision] / 16.0), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], 1e+258], N[(t$95$1 * N[Sin[N[(N[(N[(b$95$m * t$95$m), $MachinePrecision] * 0.0625), $MachinePrecision] + N[(Pi / 2.0), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], x$95$m]), $MachinePrecision]]
\begin{array}{l}
t_m = \left|t\right|
\\
b_m = \left|b\right|
\\
x\_m = \left|x\right|
\\
x\_s = \mathsf{copysign}\left(1, x\right)

\\
\begin{array}{l}
t_1 := x\_m \cdot \cos \left(\frac{\left(\left(y \cdot 2 + 1\right) \cdot z\right) \cdot t\_m}{16}\right)\\
x\_s \cdot \begin{array}{l}
\mathbf{if}\;t\_1 \cdot \cos \left(\frac{\left(\left(a \cdot 2 + 1\right) \cdot b\_m\right) \cdot t\_m}{16}\right) \leq 10^{+258}:\\
\;\;\;\;t\_1 \cdot \sin \left(\left(b\_m \cdot t\_m\right) \cdot 0.0625 + \frac{\pi}{2}\right)\\

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


\end{array}
\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if (*.f64 (*.f64 x (cos.f64 (/.f64 (*.f64 (*.f64 (+.f64 (*.f64 y #s(literal 2 binary64)) #s(literal 1 binary64)) z) t) #s(literal 16 binary64)))) (cos.f64 (/.f64 (*.f64 (*.f64 (+.f64 (*.f64 a #s(literal 2 binary64)) #s(literal 1 binary64)) b) t) #s(literal 16 binary64)))) < 1.00000000000000006e258

    1. Initial program 46.5%

      \[\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. Taylor expanded in a around 0

      \[\leadsto \left(x \cdot \cos \left(\frac{\left(\left(y \cdot 2 + 1\right) \cdot z\right) \cdot t}{16}\right)\right) \cdot \cos \color{blue}{\left(\frac{1}{16} \cdot \left(b \cdot t\right)\right)} \]
    3. Step-by-step derivation
      1. *-commutativeN/A

        \[\leadsto \left(x \cdot \cos \left(\frac{\left(\left(y \cdot 2 + 1\right) \cdot z\right) \cdot t}{16}\right)\right) \cdot \cos \left(\left(b \cdot t\right) \cdot \color{blue}{\frac{1}{16}}\right) \]
      2. lower-*.f64N/A

        \[\leadsto \left(x \cdot \cos \left(\frac{\left(\left(y \cdot 2 + 1\right) \cdot z\right) \cdot t}{16}\right)\right) \cdot \cos \left(\left(b \cdot t\right) \cdot \color{blue}{\frac{1}{16}}\right) \]
      3. lower-*.f6446.1

        \[\leadsto \left(x \cdot \cos \left(\frac{\left(\left(y \cdot 2 + 1\right) \cdot z\right) \cdot t}{16}\right)\right) \cdot \cos \left(\left(b \cdot t\right) \cdot 0.0625\right) \]
    4. Applied rewrites46.1%

      \[\leadsto \left(x \cdot \cos \left(\frac{\left(\left(y \cdot 2 + 1\right) \cdot z\right) \cdot t}{16}\right)\right) \cdot \cos \color{blue}{\left(\left(b \cdot t\right) \cdot 0.0625\right)} \]
    5. Step-by-step derivation
      1. lift-cos.f64N/A

        \[\leadsto \left(x \cdot \cos \left(\frac{\left(\left(y \cdot 2 + 1\right) \cdot z\right) \cdot t}{16}\right)\right) \cdot \color{blue}{\cos \left(\left(b \cdot t\right) \cdot \frac{1}{16}\right)} \]
      2. sin-+PI/2-revN/A

        \[\leadsto \left(x \cdot \cos \left(\frac{\left(\left(y \cdot 2 + 1\right) \cdot z\right) \cdot t}{16}\right)\right) \cdot \color{blue}{\sin \left(\left(b \cdot t\right) \cdot \frac{1}{16} + \frac{\mathsf{PI}\left(\right)}{2}\right)} \]
      3. lower-sin.f64N/A

        \[\leadsto \left(x \cdot \cos \left(\frac{\left(\left(y \cdot 2 + 1\right) \cdot z\right) \cdot t}{16}\right)\right) \cdot \color{blue}{\sin \left(\left(b \cdot t\right) \cdot \frac{1}{16} + \frac{\mathsf{PI}\left(\right)}{2}\right)} \]
      4. lift-/.f64N/A

        \[\leadsto \left(x \cdot \cos \left(\frac{\left(\left(y \cdot 2 + 1\right) \cdot z\right) \cdot t}{16}\right)\right) \cdot \sin \left(\left(b \cdot t\right) \cdot \frac{1}{16} + \color{blue}{\frac{\mathsf{PI}\left(\right)}{2}}\right) \]
      5. lift-PI.f64N/A

        \[\leadsto \left(x \cdot \cos \left(\frac{\left(\left(y \cdot 2 + 1\right) \cdot z\right) \cdot t}{16}\right)\right) \cdot \sin \left(\left(b \cdot t\right) \cdot \frac{1}{16} + \frac{\color{blue}{\pi}}{2}\right) \]
      6. lower-+.f6446.1

        \[\leadsto \left(x \cdot \cos \left(\frac{\left(\left(y \cdot 2 + 1\right) \cdot z\right) \cdot t}{16}\right)\right) \cdot \sin \color{blue}{\left(\left(b \cdot t\right) \cdot 0.0625 + \frac{\pi}{2}\right)} \]
    6. Applied rewrites46.1%

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

    if 1.00000000000000006e258 < (*.f64 (*.f64 x (cos.f64 (/.f64 (*.f64 (*.f64 (+.f64 (*.f64 y #s(literal 2 binary64)) #s(literal 1 binary64)) z) t) #s(literal 16 binary64)))) (cos.f64 (/.f64 (*.f64 (*.f64 (+.f64 (*.f64 a #s(literal 2 binary64)) #s(literal 1 binary64)) b) t) #s(literal 16 binary64))))

    1. Initial program 5.0%

      \[\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. Taylor expanded in t around 0

      \[\leadsto \color{blue}{x} \]
    3. Step-by-step derivation
      1. Applied rewrites14.9%

        \[\leadsto \color{blue}{x} \]
    4. Recombined 2 regimes into one program.
    5. Add Preprocessing

    Alternative 2: 30.2% accurate, 2.2× speedup?

    \[\begin{array}{l} t_m = \left|t\right| \\ b_m = \left|b\right| \\ x\_m = \left|x\right| \\ x\_s = \mathsf{copysign}\left(1, x\right) \\ x\_s \cdot \left(\sin \left(\mathsf{fma}\left(-0.0625, t\_m \cdot z, 0.5 \cdot \pi\right)\right) \cdot x\_m\right) \end{array} \]
    t_m = (fabs.f64 t)
    b_m = (fabs.f64 b)
    x\_m = (fabs.f64 x)
    x\_s = (copysign.f64 #s(literal 1 binary64) x)
    (FPCore (x_s x_m y z t_m a b_m)
     :precision binary64
     (* x_s (* (sin (fma -0.0625 (* t_m z) (* 0.5 PI))) x_m)))
    t_m = fabs(t);
    b_m = fabs(b);
    x\_m = fabs(x);
    x\_s = copysign(1.0, x);
    double code(double x_s, double x_m, double y, double z, double t_m, double a, double b_m) {
    	return x_s * (sin(fma(-0.0625, (t_m * z), (0.5 * ((double) M_PI)))) * x_m);
    }
    
    t_m = abs(t)
    b_m = abs(b)
    x\_m = abs(x)
    x\_s = copysign(1.0, x)
    function code(x_s, x_m, y, z, t_m, a, b_m)
    	return Float64(x_s * Float64(sin(fma(-0.0625, Float64(t_m * z), Float64(0.5 * pi))) * x_m))
    end
    
    t_m = N[Abs[t], $MachinePrecision]
    b_m = N[Abs[b], $MachinePrecision]
    x\_m = N[Abs[x], $MachinePrecision]
    x\_s = N[With[{TMP1 = Abs[1.0], TMP2 = Sign[x]}, TMP1 * If[TMP2 == 0, 1, TMP2]], $MachinePrecision]
    code[x$95$s_, x$95$m_, y_, z_, t$95$m_, a_, b$95$m_] := N[(x$95$s * N[(N[Sin[N[(-0.0625 * N[(t$95$m * z), $MachinePrecision] + N[(0.5 * Pi), $MachinePrecision]), $MachinePrecision]], $MachinePrecision] * x$95$m), $MachinePrecision]), $MachinePrecision]
    
    \begin{array}{l}
    t_m = \left|t\right|
    \\
    b_m = \left|b\right|
    \\
    x\_m = \left|x\right|
    \\
    x\_s = \mathsf{copysign}\left(1, x\right)
    
    \\
    x\_s \cdot \left(\sin \left(\mathsf{fma}\left(-0.0625, t\_m \cdot z, 0.5 \cdot \pi\right)\right) \cdot x\_m\right)
    \end{array}
    
    Derivation
    1. Initial program 27.7%

      \[\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. Taylor expanded in b around 0

      \[\leadsto \color{blue}{x \cdot \cos \left(\frac{1}{16} \cdot \left(t \cdot \left(z \cdot \left(1 + 2 \cdot y\right)\right)\right)\right)} \]
    3. Step-by-step derivation
      1. *-commutativeN/A

        \[\leadsto \cos \left(\frac{1}{16} \cdot \left(t \cdot \left(z \cdot \left(1 + 2 \cdot y\right)\right)\right)\right) \cdot \color{blue}{x} \]
      2. lower-*.f64N/A

        \[\leadsto \cos \left(\frac{1}{16} \cdot \left(t \cdot \left(z \cdot \left(1 + 2 \cdot y\right)\right)\right)\right) \cdot \color{blue}{x} \]
      3. lower-cos.f64N/A

        \[\leadsto \cos \left(\frac{1}{16} \cdot \left(t \cdot \left(z \cdot \left(1 + 2 \cdot y\right)\right)\right)\right) \cdot x \]
      4. associate-*r*N/A

        \[\leadsto \cos \left(\left(\frac{1}{16} \cdot t\right) \cdot \left(z \cdot \left(1 + 2 \cdot y\right)\right)\right) \cdot x \]
      5. +-commutativeN/A

        \[\leadsto \cos \left(\left(\frac{1}{16} \cdot t\right) \cdot \left(z \cdot \left(2 \cdot y + 1\right)\right)\right) \cdot x \]
      6. *-commutativeN/A

        \[\leadsto \cos \left(\left(\frac{1}{16} \cdot t\right) \cdot \left(z \cdot \left(y \cdot 2 + 1\right)\right)\right) \cdot x \]
      7. *-commutativeN/A

        \[\leadsto \cos \left(\left(\frac{1}{16} \cdot t\right) \cdot \left(\left(y \cdot 2 + 1\right) \cdot z\right)\right) \cdot x \]
      8. lower-*.f64N/A

        \[\leadsto \cos \left(\left(\frac{1}{16} \cdot t\right) \cdot \left(\left(y \cdot 2 + 1\right) \cdot z\right)\right) \cdot x \]
      9. lower-*.f64N/A

        \[\leadsto \cos \left(\left(\frac{1}{16} \cdot t\right) \cdot \left(\left(y \cdot 2 + 1\right) \cdot z\right)\right) \cdot x \]
      10. *-commutativeN/A

        \[\leadsto \cos \left(\left(\frac{1}{16} \cdot t\right) \cdot \left(\left(2 \cdot y + 1\right) \cdot z\right)\right) \cdot x \]
      11. +-commutativeN/A

        \[\leadsto \cos \left(\left(\frac{1}{16} \cdot t\right) \cdot \left(\left(1 + 2 \cdot y\right) \cdot z\right)\right) \cdot x \]
      12. lower-*.f64N/A

        \[\leadsto \cos \left(\left(\frac{1}{16} \cdot t\right) \cdot \left(\left(1 + 2 \cdot y\right) \cdot z\right)\right) \cdot x \]
      13. +-commutativeN/A

        \[\leadsto \cos \left(\left(\frac{1}{16} \cdot t\right) \cdot \left(\left(2 \cdot y + 1\right) \cdot z\right)\right) \cdot x \]
      14. lower-fma.f6429.0

        \[\leadsto \cos \left(\left(0.0625 \cdot t\right) \cdot \left(\mathsf{fma}\left(2, y, 1\right) \cdot z\right)\right) \cdot x \]
    4. Applied rewrites29.0%

      \[\leadsto \color{blue}{\cos \left(\left(0.0625 \cdot t\right) \cdot \left(\mathsf{fma}\left(2, y, 1\right) \cdot z\right)\right) \cdot x} \]
    5. Taylor expanded in y around 0

      \[\leadsto \cos \left(\left(\frac{1}{16} \cdot t\right) \cdot z\right) \cdot x \]
    6. Step-by-step derivation
      1. Applied rewrites30.1%

        \[\leadsto \cos \left(\left(0.0625 \cdot t\right) \cdot z\right) \cdot x \]
      2. Step-by-step derivation
        1. lift-cos.f64N/A

          \[\leadsto \cos \left(\left(\frac{1}{16} \cdot t\right) \cdot z\right) \cdot x \]
        2. cos-neg-revN/A

          \[\leadsto \cos \left(\mathsf{neg}\left(\left(\frac{1}{16} \cdot t\right) \cdot z\right)\right) \cdot x \]
        3. sin-+PI/2-revN/A

          \[\leadsto \sin \left(\left(\mathsf{neg}\left(\left(\frac{1}{16} \cdot t\right) \cdot z\right)\right) + \frac{\mathsf{PI}\left(\right)}{2}\right) \cdot x \]
        4. lower-sin.f64N/A

          \[\leadsto \sin \left(\left(\mathsf{neg}\left(\left(\frac{1}{16} \cdot t\right) \cdot z\right)\right) + \frac{\mathsf{PI}\left(\right)}{2}\right) \cdot x \]
        5. lift-/.f64N/A

          \[\leadsto \sin \left(\left(\mathsf{neg}\left(\left(\frac{1}{16} \cdot t\right) \cdot z\right)\right) + \frac{\mathsf{PI}\left(\right)}{2}\right) \cdot x \]
        6. lift-PI.f64N/A

          \[\leadsto \sin \left(\left(\mathsf{neg}\left(\left(\frac{1}{16} \cdot t\right) \cdot z\right)\right) + \frac{\pi}{2}\right) \cdot x \]
        7. lower-+.f64N/A

          \[\leadsto \sin \left(\left(\mathsf{neg}\left(\left(\frac{1}{16} \cdot t\right) \cdot z\right)\right) + \frac{\pi}{2}\right) \cdot x \]
        8. lower-neg.f6430.2

          \[\leadsto \sin \left(\left(-\left(0.0625 \cdot t\right) \cdot z\right) + \frac{\pi}{2}\right) \cdot x \]
        9. lift-*.f64N/A

          \[\leadsto \sin \left(\left(-\left(\frac{1}{16} \cdot t\right) \cdot z\right) + \frac{\pi}{2}\right) \cdot x \]
        10. *-commutativeN/A

          \[\leadsto \sin \left(\left(-z \cdot \left(\frac{1}{16} \cdot t\right)\right) + \frac{\pi}{2}\right) \cdot x \]
        11. lower-*.f6430.2

          \[\leadsto \sin \left(\left(-z \cdot \left(0.0625 \cdot t\right)\right) + \frac{\pi}{2}\right) \cdot x \]
      3. Applied rewrites30.2%

        \[\leadsto \sin \left(\left(-z \cdot \left(0.0625 \cdot t\right)\right) + \frac{\pi}{2}\right) \cdot x \]
      4. Taylor expanded in y around 0

        \[\leadsto \sin \left(\frac{1}{2} \cdot \mathsf{PI}\left(\right) - \frac{1}{16} \cdot \left(t \cdot z\right)\right) \cdot x \]
      5. Step-by-step derivation
        1. fp-cancel-sub-sign-invN/A

          \[\leadsto \sin \left(\frac{1}{2} \cdot \mathsf{PI}\left(\right) + \left(\mathsf{neg}\left(\frac{1}{16}\right)\right) \cdot \left(t \cdot z\right)\right) \cdot x \]
        2. metadata-evalN/A

          \[\leadsto \sin \left(\frac{1}{2} \cdot \mathsf{PI}\left(\right) + \frac{-1}{16} \cdot \left(t \cdot z\right)\right) \cdot x \]
        3. +-commutativeN/A

          \[\leadsto \sin \left(\frac{-1}{16} \cdot \left(t \cdot z\right) + \frac{1}{2} \cdot \mathsf{PI}\left(\right)\right) \cdot x \]
        4. lower-fma.f64N/A

          \[\leadsto \sin \left(\mathsf{fma}\left(\frac{-1}{16}, t \cdot z, \frac{1}{2} \cdot \mathsf{PI}\left(\right)\right)\right) \cdot x \]
        5. lift-*.f64N/A

          \[\leadsto \sin \left(\mathsf{fma}\left(\frac{-1}{16}, t \cdot z, \frac{1}{2} \cdot \mathsf{PI}\left(\right)\right)\right) \cdot x \]
        6. lower-*.f64N/A

          \[\leadsto \sin \left(\mathsf{fma}\left(\frac{-1}{16}, t \cdot z, \frac{1}{2} \cdot \mathsf{PI}\left(\right)\right)\right) \cdot x \]
        7. lift-PI.f6430.2

          \[\leadsto \sin \left(\mathsf{fma}\left(-0.0625, t \cdot z, 0.5 \cdot \pi\right)\right) \cdot x \]
      6. Applied rewrites30.2%

        \[\leadsto \sin \left(\mathsf{fma}\left(-0.0625, t \cdot z, 0.5 \cdot \pi\right)\right) \cdot x \]
      7. Add Preprocessing

      Alternative 3: 31.1% accurate, 269.0× speedup?

      \[\begin{array}{l} t_m = \left|t\right| \\ b_m = \left|b\right| \\ x\_m = \left|x\right| \\ x\_s = \mathsf{copysign}\left(1, x\right) \\ x\_s \cdot x\_m \end{array} \]
      t_m = (fabs.f64 t)
      b_m = (fabs.f64 b)
      x\_m = (fabs.f64 x)
      x\_s = (copysign.f64 #s(literal 1 binary64) x)
      (FPCore (x_s x_m y z t_m a b_m) :precision binary64 (* x_s x_m))
      t_m = fabs(t);
      b_m = fabs(b);
      x\_m = fabs(x);
      x\_s = copysign(1.0, x);
      double code(double x_s, double x_m, double y, double z, double t_m, double a, double b_m) {
      	return x_s * x_m;
      }
      
      t_m =     private
      b_m =     private
      x\_m =     private
      x\_s =     private
      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_s, x_m, y, z, t_m, a, b_m)
      use fmin_fmax_functions
          real(8), intent (in) :: x_s
          real(8), intent (in) :: x_m
          real(8), intent (in) :: y
          real(8), intent (in) :: z
          real(8), intent (in) :: t_m
          real(8), intent (in) :: a
          real(8), intent (in) :: b_m
          code = x_s * x_m
      end function
      
      t_m = Math.abs(t);
      b_m = Math.abs(b);
      x\_m = Math.abs(x);
      x\_s = Math.copySign(1.0, x);
      public static double code(double x_s, double x_m, double y, double z, double t_m, double a, double b_m) {
      	return x_s * x_m;
      }
      
      t_m = math.fabs(t)
      b_m = math.fabs(b)
      x\_m = math.fabs(x)
      x\_s = math.copysign(1.0, x)
      def code(x_s, x_m, y, z, t_m, a, b_m):
      	return x_s * x_m
      
      t_m = abs(t)
      b_m = abs(b)
      x\_m = abs(x)
      x\_s = copysign(1.0, x)
      function code(x_s, x_m, y, z, t_m, a, b_m)
      	return Float64(x_s * x_m)
      end
      
      t_m = abs(t);
      b_m = abs(b);
      x\_m = abs(x);
      x\_s = sign(x) * abs(1.0);
      function tmp = code(x_s, x_m, y, z, t_m, a, b_m)
      	tmp = x_s * x_m;
      end
      
      t_m = N[Abs[t], $MachinePrecision]
      b_m = N[Abs[b], $MachinePrecision]
      x\_m = N[Abs[x], $MachinePrecision]
      x\_s = N[With[{TMP1 = Abs[1.0], TMP2 = Sign[x]}, TMP1 * If[TMP2 == 0, 1, TMP2]], $MachinePrecision]
      code[x$95$s_, x$95$m_, y_, z_, t$95$m_, a_, b$95$m_] := N[(x$95$s * x$95$m), $MachinePrecision]
      
      \begin{array}{l}
      t_m = \left|t\right|
      \\
      b_m = \left|b\right|
      \\
      x\_m = \left|x\right|
      \\
      x\_s = \mathsf{copysign}\left(1, x\right)
      
      \\
      x\_s \cdot x\_m
      \end{array}
      
      Derivation
      1. Initial program 27.7%

        \[\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. Taylor expanded in t around 0

        \[\leadsto \color{blue}{x} \]
      3. Step-by-step derivation
        1. Applied rewrites31.1%

          \[\leadsto \color{blue}{x} \]
        2. Add Preprocessing

        Developer Target 1: 30.7% accurate, 1.1× speedup?

        \[\begin{array}{l} \\ x \cdot \cos \left(\frac{b}{16} \cdot \frac{t}{\left(1 - a \cdot 2\right) + {\left(a \cdot 2\right)}^{2}}\right) \end{array} \]
        (FPCore (x y z t a b)
         :precision binary64
         (* x (cos (* (/ b 16.0) (/ t (+ (- 1.0 (* a 2.0)) (pow (* a 2.0) 2.0)))))))
        double code(double x, double y, double z, double t, double a, double b) {
        	return x * cos(((b / 16.0) * (t / ((1.0 - (a * 2.0)) + pow((a * 2.0), 2.0)))));
        }
        
        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, y, z, t, a, b)
        use fmin_fmax_functions
            real(8), intent (in) :: x
            real(8), intent (in) :: y
            real(8), intent (in) :: z
            real(8), intent (in) :: t
            real(8), intent (in) :: a
            real(8), intent (in) :: b
            code = x * cos(((b / 16.0d0) * (t / ((1.0d0 - (a * 2.0d0)) + ((a * 2.0d0) ** 2.0d0)))))
        end function
        
        public static double code(double x, double y, double z, double t, double a, double b) {
        	return x * Math.cos(((b / 16.0) * (t / ((1.0 - (a * 2.0)) + Math.pow((a * 2.0), 2.0)))));
        }
        
        def code(x, y, z, t, a, b):
        	return x * math.cos(((b / 16.0) * (t / ((1.0 - (a * 2.0)) + math.pow((a * 2.0), 2.0)))))
        
        function code(x, y, z, t, a, b)
        	return Float64(x * cos(Float64(Float64(b / 16.0) * Float64(t / Float64(Float64(1.0 - Float64(a * 2.0)) + (Float64(a * 2.0) ^ 2.0))))))
        end
        
        function tmp = code(x, y, z, t, a, b)
        	tmp = x * cos(((b / 16.0) * (t / ((1.0 - (a * 2.0)) + ((a * 2.0) ^ 2.0)))));
        end
        
        code[x_, y_, z_, t_, a_, b_] := N[(x * N[Cos[N[(N[(b / 16.0), $MachinePrecision] * N[(t / N[(N[(1.0 - N[(a * 2.0), $MachinePrecision]), $MachinePrecision] + N[Power[N[(a * 2.0), $MachinePrecision], 2.0], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]
        
        \begin{array}{l}
        
        \\
        x \cdot \cos \left(\frac{b}{16} \cdot \frac{t}{\left(1 - a \cdot 2\right) + {\left(a \cdot 2\right)}^{2}}\right)
        \end{array}
        

        Reproduce

        ?
        herbie shell --seed 2025105 
        (FPCore (x y z t a b)
          :name "Codec.Picture.Jpg.FastDct:referenceDct from JuicyPixels-3.2.6.1"
          :precision binary64
        
          :alt
          (! :herbie-platform default (* x (cos (* (/ b 16) (/ t (+ (- 1 (* a 2)) (pow (* a 2) 2)))))))
        
          (* (* x (cos (/ (* (* (+ (* y 2.0) 1.0) z) t) 16.0))) (cos (/ (* (* (+ (* a 2.0) 1.0) b) t) 16.0))))