-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathlong_math.h
More file actions
564 lines (427 loc) · 17.9 KB
/
Copy pathlong_math.h
File metadata and controls
564 lines (427 loc) · 17.9 KB
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
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
////////////////////////////////////////////////////////////////////////////////////////////////////
//
// Simple Long Integer Math for C++
// version 2.0
//
////////////////////////////////////////////////////////////////////////////////////////////////////
//
// Licensed under the MIT License <http://opensource.org/licenses/MIT>.
// SPDX-License-Identifier: MIT
//
// Copyright (c) 2020-2026 Yury Kalmykov <y_kalmykov@mail.ru>.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
//
////////////////////////////////////////////////////////////////////////////////////////////////////
#pragma once
#if defined(_MSC_VER)
#include <intrin.h>
namespace slim
{
////////////////////////////////////////////////////////////////////////////////////////////////////
// standalone routines
////////////////////////////////////////////////////////////////////////////////////////////////////
// calculate number of set bits
constexpr uint_t popcnt(uint8_t value) noexcept;
constexpr uint_t popcnt(uint16_t value) noexcept;
constexpr uint_t popcnt(uint32_t value) noexcept;
constexpr uint_t popcnt(uint64_t value) noexcept;
// calculate leading zero bits
constexpr uint_t nlz(uint8_t value) noexcept;
constexpr uint_t nlz(uint16_t value) noexcept;
constexpr uint_t nlz(uint32_t value) noexcept;
constexpr uint_t nlz(uint64_t value) noexcept;
// shift bits to left
constexpr uint64_t shl2(uint64_t value_hi, uint64_t value_lo, uint_t shift) noexcept;
// shift bits to right
constexpr uint64_t shr2(uint64_t value_hi, uint64_t value_lo, uint_t shift) noexcept;
// add with carry
constexpr bool add(uint8_t& value1, uint8_t value2) noexcept;
constexpr bool add(uint16_t& value1, uint16_t value2) noexcept;
constexpr bool add(uint32_t& value1, uint32_t value2) noexcept;
constexpr bool add(uint64_t& value1, uint64_t value2) noexcept;
constexpr bool addc(uint8_t& value1, uint8_t value2, bool carry) noexcept;
constexpr bool addc(uint16_t& value1, uint16_t value2, bool carry) noexcept;
constexpr bool addc(uint32_t& value1, uint32_t value2, bool carry) noexcept;
constexpr bool addc(uint64_t& value1, uint64_t value2, bool carry) noexcept;
// subtract with borrow
constexpr bool sub(uint8_t& value1, uint8_t value2) noexcept;
constexpr bool sub(uint16_t& value1, uint16_t value2) noexcept;
constexpr bool sub(uint32_t& value1, uint32_t value2) noexcept;
constexpr bool sub(uint64_t& value1, uint64_t value2) noexcept;
constexpr bool subb(uint8_t& value1, uint8_t value2, bool borrow) noexcept;
constexpr bool subb(uint16_t& value1, uint16_t value2, bool borrow) noexcept;
constexpr bool subb(uint32_t& value1, uint32_t value2, bool borrow) noexcept;
constexpr bool subb(uint64_t& value1, uint64_t value2, bool borrow) noexcept;
// multiply with carry
constexpr uint8_t mul(uint8_t& value1, uint8_t value2) noexcept;
constexpr uint16_t mul(uint16_t& value1, uint16_t value2) noexcept;
constexpr uint32_t mul(uint32_t& value1, uint32_t value2) noexcept;
constexpr uint64_t mul(uint64_t& value1, uint64_t value2) noexcept;
constexpr uint8_t mulc(uint8_t& value1, uint8_t value2, uint8_t carry) noexcept;
constexpr uint16_t mulc(uint16_t& value1, uint16_t value2, uint16_t carry) noexcept;
constexpr uint32_t mulc(uint32_t& value1, uint32_t value2, uint32_t carry) noexcept;
constexpr uint64_t mulc(uint64_t& value1, uint64_t value2, uint64_t carry) noexcept;
////////////////////////////////////////////////////////////////////////////////////////////////////
// standalone routines
////////////////////////////////////////////////////////////////////////////////////////////////////
constexpr uint_t popcnt(uint8_t value) noexcept
{
if (std::is_constant_evaluated())
return popcnt<uint8_t>(value);
else
return __popcnt16(value);
}
////////////////////////////////////////////////////////////////////////////////////////////////////
constexpr uint_t popcnt(uint16_t value) noexcept
{
if (std::is_constant_evaluated())
return popcnt<uint16_t>(value);
else
return __popcnt16(value);
}
////////////////////////////////////////////////////////////////////////////////////////////////////
constexpr uint_t popcnt(uint32_t value) noexcept
{
if (std::is_constant_evaluated())
return popcnt<uint32_t>(value);
else
return __popcnt(value);
}
////////////////////////////////////////////////////////////////////////////////////////////////////
constexpr uint_t popcnt(uint64_t value) noexcept
{
if (std::is_constant_evaluated())
return popcnt<uint64_t>(value);
else {
#ifdef _M_X64
return __popcnt64(value);
#else
return __popcnt(static_cast<uint32_t>(half_hi(value))) + __popcnt(static_cast<uint32_t>(half_lo(value)));
#endif // _M_X64
}
}
////////////////////////////////////////////////////////////////////////////////////////////////////
constexpr uint_t nlz(uint8_t value) noexcept
{
if (std::is_constant_evaluated()) {
return nlz<uint8_t>(value);
} else {
unsigned long result = 0;
result = static_cast<uint8_t>(_BitScanReverse(&result, value) ? (31 - result) - 24 : bit_count_v<uint8_t>);
return static_cast<uint8_t>(result);
}
}
////////////////////////////////////////////////////////////////////////////////////////////////////
constexpr uint_t nlz(uint16_t value) noexcept
{
if (std::is_constant_evaluated()) {
return nlz<uint16_t>(value);
} else {
unsigned long result = 0;
result = static_cast<uint16_t>(_BitScanReverse(&result, value) ? (31 - result) - 16 : bit_count_v<uint16_t>);
return static_cast<uint16_t>(result);
}
}
////////////////////////////////////////////////////////////////////////////////////////////////////
constexpr uint_t nlz(uint32_t value) noexcept
{
if (std::is_constant_evaluated()) {
return nlz<uint32_t>(value);
} else {
unsigned long result = 0;
result = static_cast<uint32_t>(_BitScanReverse(&result, value) ? 31 - result : bit_count_v<uint32_t>);
return static_cast<uint32_t>(result);
}
}
////////////////////////////////////////////////////////////////////////////////////////////////////
constexpr uint_t nlz(uint64_t value) noexcept
{
if (std::is_constant_evaluated()) {
return nlz<uint64_t>(value);
} else {
#ifdef _M_X64
unsigned long result = 0;
result = _BitScanReverse64(&result, value) ? 63 - result : static_cast<long>(bit_count_v<uint64_t>);
return static_cast<uint64_t>(result);
#else
const uint32_t value_hi = static_cast<uint32_t>(half_hi(value));
const uint32_t value_lo = static_cast<uint32_t>(half_lo(value));
const uint_t count = nlz(value_hi);
return count < bit_count_v<uint32_t> ? count : count + nlz(value_lo);
#endif // _M_X64
}
}
////////////////////////////////////////////////////////////////////////////////////////////////////
constexpr uint64_t shl2(uint64_t value_hi, uint64_t value_lo, uint_t shift) noexcept
{
if (std::is_constant_evaluated())
return shl2<uint64_t>(value_hi, value_lo, shift);
else {
#ifdef _M_X64
if (shift < bit_count_v<uint64_t>)
return __shiftleft128(value_lo, value_hi, static_cast<uint8_t>(shift));
else
return value_lo << (shift - bit_count_v<uint64_t>);
#else
return shl2<uint64_t>(value_hi, value_lo, shift);
#endif // _M_X64
}
}
////////////////////////////////////////////////////////////////////////////////////////////////////
constexpr uint64_t shr2(uint64_t value_hi, uint64_t value_lo, uint_t shift) noexcept
{
if (std::is_constant_evaluated())
return shr2<uint64_t>(value_hi, value_lo, shift);
else {
#ifdef _M_X64
if (shift < bit_count_v<uint64_t>)
return __shiftright128(value_lo, value_hi, static_cast<uint8_t>(shift));
else
return value_hi >> (shift - bit_count_v<uint64_t>);
#else
return shr2<uint64_t>(value_hi, value_lo, shift);
#endif // _M_X64
}
}
////////////////////////////////////////////////////////////////////////////////////////////////////
constexpr bool add(uint8_t& value1, uint8_t value2) noexcept
{
if (std::is_constant_evaluated())
return add<uint8_t>(value1, value2);
else
return _addcarry_u8(0, value1, value2, &value1);
}
////////////////////////////////////////////////////////////////////////////////////////////////////
constexpr bool add(uint16_t& value1, uint16_t value2) noexcept
{
if (std::is_constant_evaluated())
return add<uint16_t>(value1, value2);
else
return _addcarry_u16(0, value1, value2, &value1);
}
////////////////////////////////////////////////////////////////////////////////////////////////////
constexpr bool add(uint32_t& value1, uint32_t value2) noexcept
{
if (std::is_constant_evaluated())
return add<uint32_t>(value1, value2);
else
return _addcarry_u32(0, value1, value2, &value1);
}
////////////////////////////////////////////////////////////////////////////////////////////////////
constexpr bool add(uint64_t& value1, uint64_t value2) noexcept
{
if (std::is_constant_evaluated())
return add<uint64_t>(value1, value2);
else {
#ifdef _M_X64
return _addcarry_u64(0, value1, value2, &value1);
#else
return add<uint64_t>(value1, value2);
#endif // _M_X64
}
}
////////////////////////////////////////////////////////////////////////////////////////////////////
constexpr bool addc(uint8_t& value1, uint8_t value2, bool carry) noexcept
{
if (std::is_constant_evaluated())
return addc<uint8_t>(value1, value2, carry);
else
return _addcarry_u8(carry, value1, value2, &value1);
}
////////////////////////////////////////////////////////////////////////////////////////////////////
constexpr bool addc(uint16_t& value1, uint16_t value2, bool carry) noexcept
{
if (std::is_constant_evaluated())
return addc<uint16_t>(value1, value2, carry);
else
return _addcarry_u16(carry, value1, value2, &value1);
}
////////////////////////////////////////////////////////////////////////////////////////////////////
constexpr bool addc(uint32_t& value1, uint32_t value2, bool carry) noexcept
{
if (std::is_constant_evaluated())
return addc<uint32_t>(value1, value2, carry);
else
return _addcarry_u32(carry, value1, value2, &value1);
}
////////////////////////////////////////////////////////////////////////////////////////////////////
constexpr bool addc(uint64_t& value1, uint64_t value2, bool carry) noexcept
{
if (std::is_constant_evaluated())
return addc<uint64_t>(value1, value2, carry);
else {
#ifdef _M_X64
return _addcarry_u64(carry, value1, value2, &value1);
#else
return addc<uint64_t>(value1, value2, carry);
#endif // _M_X64
}
}
////////////////////////////////////////////////////////////////////////////////////////////////////
constexpr bool sub(uint8_t& value1, uint8_t value2) noexcept
{
if (std::is_constant_evaluated())
return sub<uint8_t>(value1, value2);
else
return _subborrow_u8(0, value1, value2, &value1);
}
////////////////////////////////////////////////////////////////////////////////////////////////////
constexpr bool sub(uint16_t& value1, uint16_t value2) noexcept
{
if (std::is_constant_evaluated())
return sub<uint16_t>(value1, value2);
else
return _subborrow_u16(0, value1, value2, &value1);
}
////////////////////////////////////////////////////////////////////////////////////////////////////
constexpr bool sub(uint32_t& value1, uint32_t value2) noexcept
{
if (std::is_constant_evaluated())
return sub<uint32_t>(value1, value2);
else
return _subborrow_u32(0, value1, value2, &value1);
}
////////////////////////////////////////////////////////////////////////////////////////////////////
constexpr bool sub(uint64_t& value1, uint64_t value2) noexcept
{
if (std::is_constant_evaluated())
return sub<uint64_t>(value1, value2);
else {
#ifdef _M_X64
return _subborrow_u64(0, value1, value2, &value1);
#else
return sub<uint64_t>(value1, value2);
#endif // _M_X64
}
}
////////////////////////////////////////////////////////////////////////////////////////////////////
constexpr bool subb(uint8_t& value1, uint8_t value2, bool borrow) noexcept
{
if (std::is_constant_evaluated())
return subb<uint8_t>(value1, value2, borrow);
else
return _subborrow_u8(borrow, value1, value2, &value1);
}
////////////////////////////////////////////////////////////////////////////////////////////////////
constexpr bool subb(uint16_t& value1, uint16_t value2, bool borrow) noexcept
{
if (std::is_constant_evaluated())
return subb<uint16_t>(value1, value2, borrow);
else
return _subborrow_u16(static_cast<uint8_t>(borrow), value1, value2, &value1);
}
////////////////////////////////////////////////////////////////////////////////////////////////////
constexpr bool subb(uint32_t& value1, uint32_t value2, bool borrow) noexcept
{
if (std::is_constant_evaluated())
return subb<uint32_t>(value1, value2, borrow);
else
return _subborrow_u32(static_cast<uint8_t>(borrow), value1, value2, &value1);
}
////////////////////////////////////////////////////////////////////////////////////////////////////
constexpr bool subb(uint64_t& value1, uint64_t value2, bool borrow) noexcept
{
if (std::is_constant_evaluated())
return subb<uint64_t>(value1, value2, borrow);
else {
#ifdef _M_X64
return _subborrow_u64(static_cast<uint8_t>(borrow), value1, value2, &value1);
#else
return subb<uint64_t>(value1, value2, borrow);
#endif // _M_X64
}
}
////////////////////////////////////////////////////////////////////////////////////////////////////
constexpr uint8_t mul(uint8_t& value1, uint8_t value2) noexcept
{
const uint16_t result = uint16_t(value1) * uint16_t(value2);
value1 = static_cast<uint8_t>(result);
return static_cast<uint8_t>(half_hi(result));
}
////////////////////////////////////////////////////////////////////////////////////////////////////
constexpr uint16_t mul(uint16_t& value1, uint16_t value2) noexcept
{
const uint32_t result = uint32_t(value1) * uint32_t(value2);
value1 = static_cast<uint16_t>(result);
return static_cast<uint16_t>(half_hi(result));
}
////////////////////////////////////////////////////////////////////////////////////////////////////
constexpr uint32_t mul(uint32_t& value1, uint32_t value2) noexcept
{
const uint64_t result = uint64_t(value1) * uint64_t(value2);
value1 = static_cast<uint32_t>(result);
return static_cast<uint32_t>(half_hi(result));
}
////////////////////////////////////////////////////////////////////////////////////////////////////
constexpr uint64_t mul(uint64_t& value1, uint64_t value2) noexcept
{
if (std::is_constant_evaluated())
return mul<uint64_t>(value1, value2);
else {
#ifdef _M_X64
uint64_t result_hi;
value1 = _umul128(value1, value2, &result_hi);
return result_hi;
#else
return mul<uint64_t>(value1, value2);
#endif // _M_X64
}
}
////////////////////////////////////////////////////////////////////////////////////////////////////
constexpr uint8_t mulc(uint8_t& value1, uint8_t value2, uint8_t carry) noexcept
{
const uint16_t result = uint16_t(value1) * uint16_t(value2) + uint16_t(carry);
value1 = static_cast<uint8_t>(result);
return static_cast<uint8_t>(half_hi(result));
}
////////////////////////////////////////////////////////////////////////////////////////////////////
constexpr uint16_t mulc(uint16_t& value1, uint16_t value2, uint16_t carry) noexcept
{
const uint32_t result = uint32_t(value1) * uint32_t(value2) + uint32_t(carry);
value1 = static_cast<uint16_t>(result);
return static_cast<uint16_t>(half_hi(result));
}
////////////////////////////////////////////////////////////////////////////////////////////////////
constexpr uint32_t mulc(uint32_t& value1, uint32_t value2, uint32_t carry) noexcept
{
const uint64_t result = uint64_t(value1) * uint64_t(value2) + uint64_t(carry);
value1 = static_cast<uint32_t>(result);
return static_cast<uint32_t>(half_hi(result));
}
////////////////////////////////////////////////////////////////////////////////////////////////////
constexpr uint64_t mulc(uint64_t& value1, uint64_t value2, uint64_t carry) noexcept
{
if (std::is_constant_evaluated())
return mulc<uint64_t>(value1, value2, carry);
else {
#ifdef _M_X64
uint64_t result_hi;
value1 = _umul128(value1, value2, &result_hi);
return result_hi + add(value1, carry);
#else
return mulc<uint64_t>(value1, value2, carry);
#endif // _M_X64
}
}
} // namespace slim
#endif // defined(_MSC_VER)
////////////////////////////////////////////////////////////////////////////////////////////////////
// End of long_math.h
////////////////////////////////////////////////////////////////////////////////////////////////////