
(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));
}
real(8) function code(x, y, z, t, a, b)
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}
Sampling outcomes in binary64 precision:
Herbie found 4 alternatives:
| Alternative | Accuracy | Speedup |
|---|
(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));
}
real(8) function code(x, y, z, t, a, b)
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}
x_m = (fabs.f64 x)
x_s = (copysign.f64 1 x)
(FPCore (x_s x_m y z t a b)
:precision binary64
(*
x_s
(if (<=
(*
(* x_m (cos (/ (* t (* z (+ 1.0 (* 2.0 y)))) 16.0)))
(cos (/ (* t (* (+ 1.0 (* 2.0 a)) b)) 16.0)))
5e+271)
(*
x_m
(*
(cos (* (* z (fma y 2.0 1.0)) (/ t 16.0)))
(cos (/ 1.0 (/ 16.0 (* t b))))))
(* x_m (expm1 (+ (log 2.0) (* -0.0009765625 (pow (* t z) 2.0))))))))x_m = fabs(x);
x_s = copysign(1.0, x);
double code(double x_s, double x_m, double y, double z, double t, double a, double b) {
double tmp;
if (((x_m * cos(((t * (z * (1.0 + (2.0 * y)))) / 16.0))) * cos(((t * ((1.0 + (2.0 * a)) * b)) / 16.0))) <= 5e+271) {
tmp = x_m * (cos(((z * fma(y, 2.0, 1.0)) * (t / 16.0))) * cos((1.0 / (16.0 / (t * b)))));
} else {
tmp = x_m * expm1((log(2.0) + (-0.0009765625 * pow((t * z), 2.0))));
}
return x_s * tmp;
}
x_m = abs(x) x_s = copysign(1.0, x) function code(x_s, x_m, y, z, t, a, b) tmp = 0.0 if (Float64(Float64(x_m * cos(Float64(Float64(t * Float64(z * Float64(1.0 + Float64(2.0 * y)))) / 16.0))) * cos(Float64(Float64(t * Float64(Float64(1.0 + Float64(2.0 * a)) * b)) / 16.0))) <= 5e+271) tmp = Float64(x_m * Float64(cos(Float64(Float64(z * fma(y, 2.0, 1.0)) * Float64(t / 16.0))) * cos(Float64(1.0 / Float64(16.0 / Float64(t * b)))))); else tmp = Float64(x_m * expm1(Float64(log(2.0) + Float64(-0.0009765625 * (Float64(t * z) ^ 2.0))))); end return Float64(x_s * tmp) end
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_, a_, b_] := N[(x$95$s * If[LessEqual[N[(N[(x$95$m * N[Cos[N[(N[(t * N[(z * N[(1.0 + N[(2.0 * y), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $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], 5e+271], N[(x$95$m * N[(N[Cos[N[(N[(z * N[(y * 2.0 + 1.0), $MachinePrecision]), $MachinePrecision] * N[(t / 16.0), $MachinePrecision]), $MachinePrecision]], $MachinePrecision] * N[Cos[N[(1.0 / N[(16.0 / N[(t * b), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]), $MachinePrecision], N[(x$95$m * N[(Exp[N[(N[Log[2.0], $MachinePrecision] + N[(-0.0009765625 * N[Power[N[(t * z), $MachinePrecision], 2.0], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]] - 1), $MachinePrecision]), $MachinePrecision]]), $MachinePrecision]
\begin{array}{l}
x_m = \left|x\right|
\\
x_s = \mathsf{copysign}\left(1, x\right)
\\
x\_s \cdot \begin{array}{l}
\mathbf{if}\;\left(x\_m \cdot \cos \left(\frac{t \cdot \left(z \cdot \left(1 + 2 \cdot y\right)\right)}{16}\right)\right) \cdot \cos \left(\frac{t \cdot \left(\left(1 + 2 \cdot a\right) \cdot b\right)}{16}\right) \leq 5 \cdot 10^{+271}:\\
\;\;\;\;x\_m \cdot \left(\cos \left(\left(z \cdot \mathsf{fma}\left(y, 2, 1\right)\right) \cdot \frac{t}{16}\right) \cdot \cos \left(\frac{1}{\frac{16}{t \cdot b}}\right)\right)\\
\mathbf{else}:\\
\;\;\;\;x\_m \cdot \mathsf{expm1}\left(\log 2 + -0.0009765625 \cdot {\left(t \cdot z\right)}^{2}\right)\\
\end{array}
\end{array}
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))) < 5.0000000000000003e271Initial program 46.8%
associate-*l*46.8%
*-commutative46.8%
*-commutative46.8%
associate-/l*46.8%
fma-define46.8%
associate-/l*46.8%
fma-define46.8%
Simplified46.8%
associate-*r/46.8%
fma-define46.8%
clear-num47.1%
associate-*l*46.9%
fma-define46.9%
Applied egg-rr46.9%
Taylor expanded in a around 0 47.7%
*-commutative47.7%
Simplified47.7%
if 5.0000000000000003e271 < (*.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))) Initial program 2.9%
Simplified4.7%
Taylor expanded in b around 0 7.3%
metadata-eval7.3%
div-inv7.3%
expm1-log1p-u7.3%
expm1-undefine7.3%
div-inv7.3%
metadata-eval7.3%
*-commutative7.3%
Applied egg-rr7.3%
Taylor expanded in z around 0 11.2%
+-commutative11.2%
*-commutative11.2%
fma-define11.2%
Simplified13.9%
Taylor expanded in y around 0 12.5%
*-commutative12.5%
expm1-define12.5%
*-commutative12.5%
unpow212.5%
unpow212.5%
swap-sqr14.1%
unpow214.1%
Simplified14.1%
Final simplification34.3%
x_m = (fabs.f64 x)
x_s = (copysign.f64 1 x)
(FPCore (x_s x_m y z t a b)
:precision binary64
(*
x_s
(*
(+
(exp (fma (pow (* t (* z (fma 2.0 y 1.0))) 2.0) -0.0009765625 (log 2.0)))
-1.0)
x_m)))x_m = fabs(x);
x_s = copysign(1.0, x);
double code(double x_s, double x_m, double y, double z, double t, double a, double b) {
return x_s * ((exp(fma(pow((t * (z * fma(2.0, y, 1.0))), 2.0), -0.0009765625, log(2.0))) + -1.0) * x_m);
}
x_m = abs(x) x_s = copysign(1.0, x) function code(x_s, x_m, y, z, t, a, b) return Float64(x_s * Float64(Float64(exp(fma((Float64(t * Float64(z * fma(2.0, y, 1.0))) ^ 2.0), -0.0009765625, log(2.0))) + -1.0) * x_m)) end
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_, a_, b_] := N[(x$95$s * N[(N[(N[Exp[N[(N[Power[N[(t * N[(z * N[(2.0 * y + 1.0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], 2.0], $MachinePrecision] * -0.0009765625 + N[Log[2.0], $MachinePrecision]), $MachinePrecision]], $MachinePrecision] + -1.0), $MachinePrecision] * x$95$m), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
x_m = \left|x\right|
\\
x_s = \mathsf{copysign}\left(1, x\right)
\\
x\_s \cdot \left(\left(e^{\mathsf{fma}\left({\left(t \cdot \left(z \cdot \mathsf{fma}\left(2, y, 1\right)\right)\right)}^{2}, -0.0009765625, \log 2\right)} + -1\right) \cdot x\_m\right)
\end{array}
Initial program 29.3%
Simplified29.9%
Taylor expanded in b around 0 31.2%
metadata-eval31.2%
div-inv31.2%
expm1-log1p-u31.2%
expm1-undefine31.2%
div-inv31.2%
metadata-eval31.2%
*-commutative31.2%
Applied egg-rr31.2%
Taylor expanded in z around 0 25.9%
+-commutative25.9%
*-commutative25.9%
fma-define25.9%
Simplified32.9%
Final simplification32.9%
x_m = (fabs.f64 x) x_s = (copysign.f64 1 x) (FPCore (x_s x_m y z t a b) :precision binary64 (* x_s (* x_m (expm1 (+ (log 2.0) (* -0.0009765625 (pow (* t z) 2.0)))))))
x_m = fabs(x);
x_s = copysign(1.0, x);
double code(double x_s, double x_m, double y, double z, double t, double a, double b) {
return x_s * (x_m * expm1((log(2.0) + (-0.0009765625 * pow((t * z), 2.0)))));
}
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, double a, double b) {
return x_s * (x_m * Math.expm1((Math.log(2.0) + (-0.0009765625 * Math.pow((t * z), 2.0)))));
}
x_m = math.fabs(x) x_s = math.copysign(1.0, x) def code(x_s, x_m, y, z, t, a, b): return x_s * (x_m * math.expm1((math.log(2.0) + (-0.0009765625 * math.pow((t * z), 2.0)))))
x_m = abs(x) x_s = copysign(1.0, x) function code(x_s, x_m, y, z, t, a, b) return Float64(x_s * Float64(x_m * expm1(Float64(log(2.0) + Float64(-0.0009765625 * (Float64(t * z) ^ 2.0)))))) end
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_, a_, b_] := N[(x$95$s * N[(x$95$m * N[(Exp[N[(N[Log[2.0], $MachinePrecision] + N[(-0.0009765625 * N[Power[N[(t * z), $MachinePrecision], 2.0], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]] - 1), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
x_m = \left|x\right|
\\
x_s = \mathsf{copysign}\left(1, x\right)
\\
x\_s \cdot \left(x\_m \cdot \mathsf{expm1}\left(\log 2 + -0.0009765625 \cdot {\left(t \cdot z\right)}^{2}\right)\right)
\end{array}
Initial program 29.3%
Simplified29.9%
Taylor expanded in b around 0 31.2%
metadata-eval31.2%
div-inv31.2%
expm1-log1p-u31.2%
expm1-undefine31.2%
div-inv31.2%
metadata-eval31.2%
*-commutative31.2%
Applied egg-rr31.2%
Taylor expanded in z around 0 25.9%
+-commutative25.9%
*-commutative25.9%
fma-define25.9%
Simplified32.9%
Taylor expanded in y around 0 30.6%
*-commutative30.6%
expm1-define30.6%
*-commutative30.6%
unpow230.6%
unpow230.6%
swap-sqr32.7%
unpow232.7%
Simplified32.7%
Final simplification32.7%
x_m = (fabs.f64 x) x_s = (copysign.f64 1 x) (FPCore (x_s x_m y z t a b) :precision binary64 (* x_s x_m))
x_m = fabs(x);
x_s = copysign(1.0, x);
double code(double x_s, double x_m, double y, double z, double t, double a, double b) {
return x_s * x_m;
}
x_m = abs(x)
x_s = copysign(1.0d0, x)
real(8) function code(x_s, x_m, y, z, t, a, b)
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
real(8), intent (in) :: a
real(8), intent (in) :: b
code = x_s * x_m
end function
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, double a, double b) {
return x_s * x_m;
}
x_m = math.fabs(x) x_s = math.copysign(1.0, x) def code(x_s, x_m, y, z, t, a, b): return x_s * x_m
x_m = abs(x) x_s = copysign(1.0, x) function code(x_s, x_m, y, z, t, a, b) return Float64(x_s * x_m) end
x_m = abs(x); x_s = sign(x) * abs(1.0); function tmp = code(x_s, x_m, y, z, t, a, b) tmp = x_s * x_m; end
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_, a_, b_] := N[(x$95$s * x$95$m), $MachinePrecision]
\begin{array}{l}
x_m = \left|x\right|
\\
x_s = \mathsf{copysign}\left(1, x\right)
\\
x\_s \cdot x\_m
\end{array}
Initial program 29.3%
Simplified29.9%
Taylor expanded in b around 0 31.2%
Taylor expanded in z around 0 31.9%
Final simplification31.9%
(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)))));
}
real(8) function code(x, y, z, t, a, b)
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}
herbie shell --seed 2024062
(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))))