-
Notifications
You must be signed in to change notification settings - Fork 0
/
mbin_sos.c
267 lines (223 loc) · 5.43 KB
/
mbin_sos.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
/*-
* Copyright (c) 2009-2020 Hans Petter Selasky
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
/*
* Function to compute the Sum of Sums, SoS:
*
* +--------------> (y)
* | 1 0 0 0 0 0
* | 1 1 1 1 1 1
* | 1 2 3 4 5 6
* | 1 3 6 10 15 21
* | 1 4 10 20 35 56
* | 1 5 15 35 70 126
* | 1 6 21 56 126 252
* V
*
* (x)
*/
#include <stdint.h>
#include <assert.h>
#include "math_bin.h"
uint32_t
mbin_sos_32(int32_t x, int32_t y)
{
uint32_t rem;
uint32_t div;
uint32_t n;
uint32_t temp;
uint32_t fact;
/* handle some special cases */
if ((x < 0) || (y < 0))
return (0);
if (y == 0)
return (1);
if (x == 0)
return (0);
/* try to optimise */
if (x < (y + 1)) {
/* swap X and Y due to symmetry */
temp = (x - 1);
x = y + 1;
y = temp;
}
rem = 1;
div = 1;
for (n = 0; n != (uint32_t)y; n++) {
/* compute scaling factor */
temp = (~n) & (n + 1);
div *= (n + 1) / temp;
fact = (x + n);
/* remove common divisor */
while ((~fact) & (~temp) & 1) {
fact /= 2;
temp /= 2;
}
/* compute Sum of Sum */
rem *= fact;
rem /= temp;
}
/* for better accuracy modulo (2**n) division is used */
rem = mbin_div_odd32(rem, div);
return (rem);
}
/*
* This function computes the sum of squares for a 2**log2_level block
* starting at "start" and returns the result.
*/
uint64_t
mbin_sos_block_2nd_64(const uint64_t start, uint8_t log2_level)
{
uint64_t k0;
uint64_t k1;
uint64_t k2;
if (log2_level == 0) {
return (start * start);
} else if (start == 0) {
k0 = 2ULL << (3 * log2_level);
k1 = 1ULL << (2 * log2_level);
k2 = 1ULL << (1 * log2_level);
return (k0 - 3 * k1 + k2) / 6;
} else {
k0 = (1ULL << (3 * log2_level));
k2 = (1ULL << (1 * log2_level));
k1 = (k2 - 1 + 2 * start);
return ((((k1 * k1) << log2_level) + ((k0 - k2) / 3))) / 4;
}
}
/*
* This function computes the sum of squares up to and including "x"
* and returns the result:
* 0, 1, 5, 14, 30, 55, 91, 140, 204, 285, 385, 506, 650, 819, 1015, 1240 ...
*
* The return value can also be expressed like:
* (x**3) / 3 + (x**2) / 2 + (x / 6)
*/
uint64_t
mbin_sos_2nd_64(uint64_t x)
{
uint64_t result = 0;
uint64_t m0 = 1;
uint64_t start = 0;
uint8_t log2_m0 = 0;
x++;
while (m0 <= x) {
m0 *= 2;
log2_m0++;
}
m0 /= 2;
log2_m0--;
while (m0 != 0) {
if (x & m0) {
result += mbin_sos_block_2nd_64(start, log2_m0);
start = x & -m0;
}
m0 /= 2;
log2_m0--;
}
return (result);
}
uint64_t
mbin_sos_2nd_search_64(const uint64_t value)
{
uint64_t m = 1;
uint64_t x;
while (mbin_sos_2nd_64(m) <= value)
m *= 2;
m /= 2;
x = m;
while (m != 0) {
if (mbin_sos_2nd_64(m + x) <= value)
x += m;
m /= 2;
}
return (x);
}
/*
* This function computes the sum of squares up to and including
* (2**log2_level) under the given modulus, starting at "start".
*/
uint32_t
mbin_sos_block_2nd_mod_32(const uint64_t start, const uint64_t log2_level, const uint32_t mod)
{
uint64_t res;
uint64_t k0;
uint64_t k1;
uint64_t k2;
/* modulus must not be divisible by 2 or 3 */
assert(mod % 2);
assert(mod % 3);
if (log2_level == 0) {
res = start * start;
} else if (start == 0) {
k0 = mbin_power_mod_32(2, 3 * log2_level + 1, mod);
k1 = mbin_power_mod_32(2, 2 * log2_level, mod);
k2 = mbin_power_mod_32(2, log2_level, mod);
res = (3 * mod + k0 - 3 * k1 + k2);
while (res % 6)
res += mod;
res /= 6;
} else {
k0 = mbin_power_mod_32(2, 3 * log2_level, mod);
k2 = mbin_power_mod_32(2, log2_level, mod);
k1 = (mod + k2 - 1 + 2 * start) % mod;
res = mod + k0 - k2;
while (res % 3)
res += mod;
res /= 3;
res += ((k1 * k1) % mod) * k2;
while (res % 4)
res += mod;
res /= 4;
}
return (res % mod);
}
/*
* This function computes the sum of squares up to and including "x"
* and returns the result under the given modulus.
*/
uint32_t
mbin_sos_2nd_mod_32(uint64_t x, const uint32_t mod)
{
uint64_t result = 0;
uint64_t m0 = 1;
uint64_t start = 0;
uint8_t log2_m0 = 0;
x++;
while (m0 <= x) {
m0 *= 2;
log2_m0++;
}
m0 /= 2;
log2_m0--;
while (m0 != 0) {
if (x & m0) {
result += mbin_sos_block_2nd_mod_32(start, log2_m0, mod);
start = x & -m0;
}
m0 /= 2;
log2_m0--;
}
return (result % mod);
}