You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
// x2 = (- b + sqrt(D)) / 2a = (- (+|b|) + sqrt(D)) / 2a = (-|b| + sqrt(D)) / 2a = -(|b| - sqrt(D)) / 2a ... When b is larger, There is a possibility to underflow.
36
+
roots[0] = -x;
37
+
roots[1] = -(c / (a * x)); // rationalize the numerator
38
+
}
39
+
return2;
40
+
}
41
+
42
+
// equation has 1 root
43
+
roots[0] = roots[1] = (b < 0.0) ? x : -x;
44
+
return1;
45
+
}
46
+
47
+
staticintpass_to_step(int pass)
48
+
{
49
+
// ax^2 + bx + c = 0
50
+
double a = 1.0;
51
+
double b = 1.0;
52
+
double c = -2.0 * static_cast<double>(pass);
53
+
std::array<double, 2> roots;
54
+
auto ret = solve_quadratic_equation(a, b, c, roots);
55
+
assert(ret > 0);
56
+
auto root = std::max(roots[0], roots[1]);
57
+
58
+
returnstatic_cast<int>(std::ceil(root));
59
+
}
60
+
61
+
staticintpass_to_stage(int pass, int step)
62
+
{
63
+
auto sum = step * (step + 1) / 2;
64
+
return sum - pass + 1;
65
+
}
66
+
67
+
staticintstage_to_offset(int stage)
68
+
{
69
+
returnstd::pow(2.0, stage - 1);
70
+
}
71
+
72
+
doublenext_lower_power_of_two(double x)
73
+
{
74
+
returnstd::pow(2.0, std::floor(std::log2(x)));
75
+
}
76
+
77
+
doublenext_higher_power_of_two(double x)
78
+
{
79
+
returnstd::pow(2.0, std::ceil(std::log2(x)));
80
+
}
81
+
82
+
intget_num_passes(int N)
83
+
{
84
+
auto n = static_cast<int>(std::log2(N));
85
+
return n * (n + 1) / 2;
86
+
}
87
+
88
+
params get_params(int pass)
89
+
{
90
+
auto step = pass_to_step(pass);
91
+
auto stage = pass_to_stage(pass, step);
92
+
auto seq_size = static_cast<int>(std::pow(2.0, step));
0 commit comments