-
Notifications
You must be signed in to change notification settings - Fork 716
/
Copy pathuintcore.h
521 lines (477 loc) · 17.8 KB
/
uintcore.h
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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license.
#pragma once
#include "seal/util/common.h"
#include "seal/util/defines.h"
#include "seal/util/pointer.h"
#include <algorithm>
#include <cstdint>
#include <cstring>
#include <limits>
#include <stdexcept>
namespace seal
{
namespace util
{
SEAL_NODISCARD std::string uint_to_hex_string(const std::uint64_t *value, std::size_t uint64_count);
SEAL_NODISCARD std::string uint_to_dec_string(
const std::uint64_t *value, std::size_t uint64_count, MemoryPool &pool);
inline void hex_string_to_uint(
const char *hex_string, int char_count, std::size_t uint64_count, std::uint64_t *result)
{
#ifdef SEAL_DEBUG
if (!hex_string && char_count > 0)
{
throw std::invalid_argument("hex_string");
}
if (uint64_count && !result)
{
throw std::invalid_argument("result");
}
if (unsigned_gt(
get_hex_string_bit_count(hex_string, char_count),
mul_safe(uint64_count, static_cast<size_t>(bits_per_uint64))))
{
throw std::invalid_argument("hex_string");
}
#endif
const char *hex_string_ptr = hex_string + char_count;
for (std::size_t uint64_index = 0; uint64_index < uint64_count; uint64_index++)
{
std::uint64_t value = 0;
for (int bit_index = 0; bit_index < bits_per_uint64; bit_index += bits_per_nibble)
{
if (hex_string_ptr == hex_string)
{
break;
}
char hex = *--hex_string_ptr;
int nibble = hex_to_nibble(hex);
if (nibble == -1)
{
throw std::invalid_argument("hex_value");
}
value |= static_cast<std::uint64_t>(nibble) << bit_index;
}
result[uint64_index] = value;
}
}
SEAL_NODISCARD inline auto allocate_uint(std::size_t uint64_count, MemoryPool &pool)
{
return allocate<std::uint64_t>(uint64_count, pool);
}
inline void set_zero_uint(std::size_t uint64_count, std::uint64_t *result)
{
#ifdef SEAL_DEBUG
if (!result && uint64_count)
{
throw std::invalid_argument("result");
}
#endif
std::fill_n(result, uint64_count, std::uint64_t(0));
}
SEAL_NODISCARD inline auto allocate_zero_uint(std::size_t uint64_count, MemoryPool &pool)
{
auto result(allocate_uint(uint64_count, pool));
set_zero_uint(uint64_count, result.get());
return result;
// The following looks better but seems to yield worse results.
// return allocate<std::uint64_t>(uint64_count, pool, std::uint64_t(0));
}
inline void set_uint(std::uint64_t value, std::size_t uint64_count, std::uint64_t *result)
{
#ifdef SEAL_DEBUG
if (!uint64_count)
{
throw std::invalid_argument("uint64_count");
}
if (!result)
{
throw std::invalid_argument("result");
}
#endif
*result++ = value;
for (; --uint64_count; result++)
{
*result = 0;
}
}
inline void set_uint(const std::uint64_t *value, std::size_t uint64_count, std::uint64_t *result)
{
#ifdef SEAL_DEBUG
if (!value && uint64_count)
{
throw std::invalid_argument("value");
}
if (!result && uint64_count)
{
throw std::invalid_argument("result");
}
#endif
if ((value == result) || !uint64_count)
{
return;
}
std::copy_n(value, uint64_count, result);
}
SEAL_NODISCARD inline bool is_zero_uint(const std::uint64_t *value, std::size_t uint64_count)
{
#ifdef SEAL_DEBUG
if (!value && uint64_count)
{
throw std::invalid_argument("value");
}
#endif
return std::all_of(value, value + uint64_count, [](auto coeff) -> bool { return !coeff; });
}
SEAL_NODISCARD inline bool is_equal_uint(
const std::uint64_t *value, std::size_t uint64_count, std::uint64_t scalar)
{
#ifdef SEAL_DEBUG
if (!value)
{
throw std::invalid_argument("value");
}
if (!uint64_count)
{
throw std::invalid_argument("uint64_count");
}
#endif
if (*value++ != scalar)
{
return false;
}
return std::all_of(value, value + uint64_count - 1, [](auto coeff) -> bool { return !coeff; });
}
SEAL_NODISCARD inline bool is_high_bit_set_uint(const std::uint64_t *value, std::size_t uint64_count)
{
#ifdef SEAL_DEBUG
if (!value)
{
throw std::invalid_argument("value");
}
if (!uint64_count)
{
throw std::invalid_argument("uint64_count");
}
#endif
return (value[uint64_count - 1] >> (bits_per_uint64 - 1)) != 0;
}
#ifndef SEAL_USE_MAYBE_UNUSED
#if (SEAL_COMPILER == SEAL_COMPILER_GCC)
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wunused-parameter"
#elif (SEAL_COMPILER == SEAL_COMPILER_CLANG)
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wunused-parameter"
#endif
#endif
SEAL_NODISCARD inline bool is_bit_set_uint(
const std::uint64_t *value, std::size_t uint64_count SEAL_MAYBE_UNUSED, int bit_index)
{
#ifdef SEAL_DEBUG
if (!value)
{
throw std::invalid_argument("value");
}
if (!uint64_count)
{
throw std::invalid_argument("uint64_count");
}
if (bit_index < 0 ||
static_cast<std::int64_t>(bit_index) >= static_cast<std::int64_t>(uint64_count) * bits_per_uint64)
{
throw std::invalid_argument("bit_index");
}
#endif
int uint64_index = bit_index / bits_per_uint64;
int sub_bit_index = bit_index - uint64_index * bits_per_uint64;
return ((value[static_cast<std::size_t>(uint64_index)] >> sub_bit_index) & 1) != 0;
}
inline void set_bit_uint(std::uint64_t *value, std::size_t uint64_count SEAL_MAYBE_UNUSED, int bit_index)
{
#ifdef SEAL_DEBUG
if (!value)
{
throw std::invalid_argument("value");
}
if (!uint64_count)
{
throw std::invalid_argument("uint64_count");
}
if (bit_index < 0 ||
static_cast<std::int64_t>(bit_index) >= static_cast<std::int64_t>(uint64_count) * bits_per_uint64)
{
throw std::invalid_argument("bit_index");
}
#endif
int uint64_index = bit_index / bits_per_uint64;
int sub_bit_index = bit_index % bits_per_uint64;
value[static_cast<std::size_t>(uint64_index)] |= std::uint64_t(1) << sub_bit_index;
}
#ifndef SEAL_USE_MAYBE_UNUSED
#if (SEAL_COMPILER == SEAL_COMPILER_GCC)
#pragma GCC diagnostic pop
#elif (SEAL_COMPILER == SEAL_COMPILER_CLANG)
#pragma clang diagnostic pop
#endif
#endif
SEAL_NODISCARD inline int get_significant_bit_count_uint(const std::uint64_t *value, std::size_t uint64_count)
{
#ifdef SEAL_DEBUG
if (!value && uint64_count)
{
throw std::invalid_argument("value");
}
if (!uint64_count)
{
throw std::invalid_argument("uint64_count");
}
#endif
value += uint64_count - 1;
for (; *value == 0 && uint64_count > 1; uint64_count--)
{
value--;
}
return static_cast<int>(uint64_count - 1) * bits_per_uint64 + get_significant_bit_count(*value);
}
SEAL_NODISCARD inline std::size_t get_significant_uint64_count_uint(
const std::uint64_t *value, std::size_t uint64_count)
{
#ifdef SEAL_DEBUG
if (!value && uint64_count)
{
throw std::invalid_argument("value");
}
if (!uint64_count)
{
throw std::invalid_argument("uint64_count");
}
#endif
value += uint64_count - 1;
for (; uint64_count && !*value; uint64_count--)
{
value--;
}
return uint64_count;
}
SEAL_NODISCARD inline std::size_t get_nonzero_uint64_count_uint(
const std::uint64_t *value, std::size_t uint64_count)
{
#ifdef SEAL_DEBUG
if (!value && uint64_count)
{
throw std::invalid_argument("value");
}
if (!uint64_count)
{
throw std::invalid_argument("uint64_count");
}
#endif
std::size_t nonzero_count = uint64_count;
value += uint64_count - 1;
for (; uint64_count; uint64_count--)
{
if (*value-- == 0)
{
nonzero_count--;
}
}
return nonzero_count;
}
inline void set_uint(
const std::uint64_t *value, std::size_t value_uint64_count, std::size_t result_uint64_count,
std::uint64_t *result)
{
#ifdef SEAL_DEBUG
if (!value && value_uint64_count)
{
throw std::invalid_argument("value");
}
if (!result && result_uint64_count)
{
throw std::invalid_argument("result");
}
#endif
if (value == result || !value_uint64_count)
{
// Fast path to handle self assignment.
std::fill(result + value_uint64_count, result + result_uint64_count, std::uint64_t(0));
}
else
{
std::size_t min_uint64_count = std::min<>(value_uint64_count, result_uint64_count);
std::copy_n(value, min_uint64_count, result);
std::fill(result + min_uint64_count, result + result_uint64_count, std::uint64_t(0));
}
}
/**
If the value is a power of two, return the power; otherwise, return -1.
*/
SEAL_NODISCARD inline int get_power_of_two(std::uint64_t value)
{
if (value == 0 || (value & (value - 1)) != 0)
{
return -1;
}
unsigned long result = 0;
SEAL_MSB_INDEX_UINT64(&result, value);
return static_cast<int>(result);
}
inline void filter_highbits_uint(std::uint64_t *operand, std::size_t uint64_count, int bit_count)
{
std::size_t bits_per_uint64_sz = static_cast<std::size_t>(bits_per_uint64);
#ifdef SEAL_DEBUG
if (!operand && uint64_count)
{
throw std::invalid_argument("operand");
}
if (bit_count < 0 || unsigned_gt(bit_count, mul_safe(uint64_count, bits_per_uint64_sz)))
{
throw std::invalid_argument("bit_count");
}
#endif
if (unsigned_eq(bit_count, mul_safe(uint64_count, bits_per_uint64_sz)))
{
return;
}
int uint64_index = bit_count / bits_per_uint64;
int subbit_index = bit_count - uint64_index * bits_per_uint64;
operand += uint64_index;
*operand++ &= (std::uint64_t(1) << subbit_index) - 1;
for (int long_index = uint64_index + 1; unsigned_lt(long_index, uint64_count); long_index++)
{
*operand++ = 0;
}
}
SEAL_NODISCARD inline auto duplicate_uint_if_needed(
const std::uint64_t *input, std::size_t uint64_count, std::size_t new_uint64_count, bool force,
MemoryPool &pool)
{
#ifdef SEAL_DEBUG
if (!input && uint64_count)
{
throw std::invalid_argument("uint");
}
#endif
if (!force && uint64_count >= new_uint64_count)
{
return ConstPointer<std::uint64_t>::Aliasing(input);
}
auto allocation(allocate_uint(new_uint64_count, pool));
set_uint(input, uint64_count, new_uint64_count, allocation.get());
return ConstPointer<std::uint64_t>(std::move(allocation));
}
SEAL_NODISCARD inline int compare_uint(
const std::uint64_t *operand1, const std::uint64_t *operand2, std::size_t uint64_count)
{
#ifdef SEAL_DEBUG
if (!operand1 && uint64_count)
{
throw std::invalid_argument("operand1");
}
if (!operand2 && uint64_count)
{
throw std::invalid_argument("operand2");
}
#endif
int result = 0;
operand1 += uint64_count - 1;
operand2 += uint64_count - 1;
for (; (result == 0) && uint64_count--; operand1--, operand2--)
{
result = (*operand1 > *operand2) - (*operand1 < *operand2);
}
return result;
}
SEAL_NODISCARD inline int compare_uint(
const std::uint64_t *operand1, std::size_t operand1_uint64_count, const std::uint64_t *operand2,
std::size_t operand2_uint64_count)
{
#ifdef SEAL_DEBUG
if (!operand1 && operand1_uint64_count)
{
throw std::invalid_argument("operand1");
}
if (!operand2 && operand2_uint64_count)
{
throw std::invalid_argument("operand2");
}
#endif
int result = 0;
operand1 += operand1_uint64_count - 1;
operand2 += operand2_uint64_count - 1;
std::size_t min_uint64_count = std::min<>(operand1_uint64_count, operand2_uint64_count);
operand1_uint64_count -= min_uint64_count;
for (; (result == 0) && operand1_uint64_count--; operand1--)
{
result = (*operand1 > 0);
}
operand2_uint64_count -= min_uint64_count;
for (; (result == 0) && operand2_uint64_count--; operand2--)
{
result = -(*operand2 > 0);
}
for (; (result == 0) && min_uint64_count--; operand1--, operand2--)
{
result = (*operand1 > *operand2) - (*operand1 < *operand2);
}
return result;
}
SEAL_NODISCARD inline bool is_greater_than_uint(
const std::uint64_t *operand1, const std::uint64_t *operand2, std::size_t uint64_count)
{
return compare_uint(operand1, operand2, uint64_count) > 0;
}
SEAL_NODISCARD inline bool is_greater_than_or_equal_uint(
const std::uint64_t *operand1, const std::uint64_t *operand2, std::size_t uint64_count)
{
return compare_uint(operand1, operand2, uint64_count) >= 0;
}
SEAL_NODISCARD inline bool is_less_than_uint(
const std::uint64_t *operand1, const std::uint64_t *operand2, std::size_t uint64_count)
{
return compare_uint(operand1, operand2, uint64_count) < 0;
}
SEAL_NODISCARD inline bool is_less_than_or_equal_uint(
const std::uint64_t *operand1, const std::uint64_t *operand2, std::size_t uint64_count)
{
return compare_uint(operand1, operand2, uint64_count) <= 0;
}
SEAL_NODISCARD inline bool is_equal_uint(
const std::uint64_t *operand1, const std::uint64_t *operand2, std::size_t uint64_count)
{
return compare_uint(operand1, operand2, uint64_count) == 0;
}
SEAL_NODISCARD inline bool is_greater_than_uint(
const std::uint64_t *operand1, std::size_t operand1_uint64_count, const std::uint64_t *operand2,
std::size_t operand2_uint64_count)
{
return compare_uint(operand1, operand1_uint64_count, operand2, operand2_uint64_count) > 0;
}
SEAL_NODISCARD inline bool is_greater_than_or_equal_uint(
const std::uint64_t *operand1, std::size_t operand1_uint64_count, const std::uint64_t *operand2,
std::size_t operand2_uint64_count)
{
return compare_uint(operand1, operand1_uint64_count, operand2, operand2_uint64_count) >= 0;
}
SEAL_NODISCARD inline bool is_less_than_uint(
const std::uint64_t *operand1, std::size_t operand1_uint64_count, const std::uint64_t *operand2,
std::size_t operand2_uint64_count)
{
return compare_uint(operand1, operand1_uint64_count, operand2, operand2_uint64_count) < 0;
}
SEAL_NODISCARD inline bool is_less_than_or_equal_uint(
const std::uint64_t *operand1, std::size_t operand1_uint64_count, const std::uint64_t *operand2,
std::size_t operand2_uint64_count)
{
return compare_uint(operand1, operand1_uint64_count, operand2, operand2_uint64_count) <= 0;
}
SEAL_NODISCARD inline bool is_equal_uint(
const std::uint64_t *operand1, std::size_t operand1_uint64_count, const std::uint64_t *operand2,
std::size_t operand2_uint64_count)
{
return compare_uint(operand1, operand1_uint64_count, operand2, operand2_uint64_count) == 0;
}
} // namespace util
} // namespace seal