-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathDigitOpHelper.h
More file actions
416 lines (359 loc) · 13 KB
/
DigitOpHelper.h
File metadata and controls
416 lines (359 loc) · 13 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
#pragma once
#ifndef DIGITOPHELPER_H
#define DIGITOPHELPER_H
// data types
typedef unsigned long long UInt64;
typedef unsigned int UInt32;
#include "../Utils/Constants.h"
#include "DigitHelper.h"
#include "../Utils/Utils.h"
class DigitOpHelper
{
public:
/// <summary>
/// Adds two big integers using pointers.
/// </summary>
/// <param name="digitsPtr1">First big integer digits.</param>
/// <param name="length1">First big integer length.</param>
/// <param name="digitsPtr2">Second big integer digits.</param>
/// <param name="length2">Second big integer length.</param>
/// <param name="digitsResPtr">Resulting big integer digits.</param>
/// <returns>Resulting big integer length.</returns>
static UInt32 Add(const UInt32* digitsPtr1, const UInt32 vlength1,
const UInt32* digitsPtr2, const UInt32 vlength2, UInt32* digitsResPtr)
{
UInt32 length1 = vlength1;
UInt32 length2 = vlength2;
UInt64 c = 0;
if (length1 < length2)
{
// First must be bigger - swap
UInt32 lengthTemp = length1;
length1 = length2;
length2 = lengthTemp;
UInt32* ptrTemp = (UInt32*)digitsPtr1;
digitsPtr1 = digitsPtr2;
digitsPtr2 = ptrTemp;
} // end if
// Perform digits adding
for (UInt32 i = 0; i < length2; ++i)
{
c += (UInt64)(digitsPtr1[i]) + (UInt64)digitsPtr2[i];
digitsResPtr[i] = (UInt32)c;
c >>= 32;
} // end for
// Perform digits + carry moving
for (UInt32 i = length2; i < length1; ++i)
{
c += digitsPtr1[i];
digitsResPtr[i] = (UInt32)c;
c >>= 32;
} // end for
// Account last carry
if (c != 0)
{
digitsResPtr[length1++] = (UInt32)c;
} // end if
return length1;
} // end function Add
/// <summary>
/// Subtracts two big integers using pointers.
/// </summary>
/// <param name="digitsPtr1">First big integer digits.</param>
/// <param name="length1">First big integer length.</param>
/// <param name="digitsPtr2">Second big integer digits.</param>
/// <param name="length2">Second big integer length.</param>
/// <param name="digitsResPtr">Resulting big integer digits.</param>
/// <returns>Resulting big integer length.</returns>
static UInt32 Sub(const UInt32* digitsPtr1, const UInt32 vlength1,
const UInt32* digitsPtr2, const UInt32 vlength2, UInt32* digitsResPtr)
{
UInt32 length1 = vlength1;
UInt32 length2 = vlength2;
UInt64 c = 0;
// Perform digits subtraction
for (UInt32 i = 0; i < length2; ++i)
{
c = (UInt64)digitsPtr1[i] - (UInt64)digitsPtr2[i] - c;
digitsResPtr[i] = (UInt32)c;
c >>= 63;
} // end for
// Perform digits + carry moving
for (UInt32 i = length2; i < length1; ++i)
{
c = digitsPtr1[i] - c;
digitsResPtr[i] = (UInt32)c;
c >>= 63;
} // end for
return DigitHelper::GetRealDigitsLength(digitsResPtr, length1);
} // end function Sub
/// <summary>
/// Divides one big integer represented by it's digits on another one big ingeter.
/// Reminder is always filled (but not the result).
/// </summary>
/// <param name="digitsPtr1">First big integer digits.</param>
/// <param name="length1">First big integer length.</param>
/// <param name="int2">Second integer.</param>
/// <param name="divResPtr">Div result (can be null - not filled in this case).</param>
/// <param name="modRes">Remainder (always filled).</param>
/// <returns>Result length (0 if result is null).</returns>
static UInt32 DivMod(const UInt32* digitsPtr1, const UInt32 vlength1,
const UInt32 int2, UInt32* divResPtr, UInt32 &modRes)
{
UInt32 length1 = vlength1;
UInt64 c = 0;
UInt32 res;
for (UInt32 index = length1 - 1; index < length1; --index)
{
c = (c << Constants::DigitBitCount) + digitsPtr1[index];
res = (UInt32)(c / int2);
c -= (UInt64)(res) * int2;
divResPtr[index] = res;
} // end for
modRes = (UInt32)c;
return length1 - (divResPtr[length1 - 1] == 0 ? 1U : 0U);
} // end function DivMod
/// <summary>
/// Divides one big integer represented by it's digits on another one big ingeter.
/// Only remainder is filled.
/// </summary>
/// <param name="digitsPtr1">First big integer digits.</param>
/// <param name="length1">First big integer length.</param>
/// <param name="int2">Second integer.</param>
/// <returns>Remainder.</returns>
static UInt32 Mod(const UInt32* digitsPtr1, const UInt32 vlength1, const UInt32 int2)
{
UInt32 length1 = vlength1;
UInt64 c = 0;
UInt32 res;
for (UInt32* ptr1 = (UInt32*)digitsPtr1 + length1 - 1; ptr1 >= digitsPtr1; --ptr1)
{
c = (c << Constants::DigitBitCount) + *ptr1;
res = (UInt32)(c / int2);
c -= (UInt64)(res) * int2;
} // end for
return (UInt32)c;
} // end function Mod
/// <summary>
/// Compares 2 <see cref="IntX" /> objects represented by pointers only (not taking sign into account).
/// Returns "-1" if <paramref name="digitsPtr1" /> < <paramref name="digitsPtr2" />, "0" if equal and "1" if >.
/// </summary>
/// <param name="digitsPtr1">First big integer digits.</param>
/// <param name="length1">First big integer length.</param>
/// <param name="digitsPtr2">Second big integer digits.</param>
/// <param name="length2">Second big integer length.</param>
/// <returns>Comparsion result.</returns>
static int Cmp(const UInt32* digitsPtr1, const UInt32 vlength1,
const UInt32* digitsPtr2, const UInt32 vlength2)
{
UInt32 length1 = vlength1;
UInt32 length2 = vlength2;
// Maybe length comparing will be enough
int res = CmpLen(length1, length2);
if (res != -2) return res;
for (UInt32 index = length1 - 1; index < length1; --index)
{
if (digitsPtr1[index] != digitsPtr2[index]) return digitsPtr1[index] < digitsPtr2[index] ? -1 : 1;
} // end for
return 0;
} // end function Cmp
/// <summary>
/// Compares two integers lengths. Returns -2 if further comparing is needed.
/// </summary>
/// <param name="length1">First big integer length.</param>
/// <param name="length2">Second big integer length.</param>
/// <returns>Comparsion result.</returns>
static int CmpLen(const UInt32 length1, const UInt32 length2)
{
if (length1 < length2) return -1;
if (length1 > length2) return 1;
return length1 == 0 ? 0 : -2;
} // end function CmpLen
/// <summary>
/// Shifts big integer.
/// </summary>
/// <param name="digits">Big integer digits.</param>
/// <param name="offset">Big integer digits offset.</param>
/// <param name="length">Big integer length.</param>
/// <param name="digitsRes">Resulting big integer digits.</param>
/// <param name="resOffset">Resulting big integer digits offset.</param>
/// <param name="rightShift">Shift to the right (always between 1 an 31).</param>
static void Shr(
const UInt32 digits[],
const UInt32 offset,
const UInt32 length,
UInt32 digitsRes[],
UInt32 resOffset,
int rightShift)
{
UInt32 *digitsPtr = (UInt32 *)digits;
UInt32 *digitsResPtr = digitsRes;
Shr(digitsPtr + offset, length, digitsResPtr + resOffset, rightShift, resOffset != 0);
} //end function Shr
static UInt32 CypherShr(const UInt32* digitsPtr, const UInt32 vlength,
UInt32* digitsResPtr, const int rightShift, const bool resHasOffset)
{
int rightShiftRev;
const UInt32 *digitsPtrEndPrev, *digitsPtrNext;
UInt32 lastValue, length = vlength;
rightShiftRev = Constants::DigitBitCount - rightShift;
// Shift first digit in special way
if (resHasOffset)
*(digitsResPtr - 1) = digitsPtr[0] << rightShiftRev;
if (rightShift == 0)
{
// Handle special situation here - only memcpy is needed (maybe)
if (digitsPtr != digitsResPtr)
DigitHelper::DigitsBlockCopy(digitsPtr, digitsResPtr, length);
} // end if
else
{
// Shift all digits except last one
digitsPtrEndPrev = digitsPtr + length - 1;
digitsPtrNext = digitsPtr + 1;
while (digitsPtr < digitsPtrEndPrev)
{
*digitsResPtr = *digitsPtr >> rightShift | *digitsPtrNext << rightShiftRev;
++digitsPtr;
++digitsPtrNext;
++digitsResPtr;
} // end while
// Shift last digit in special way
lastValue = *digitsPtr >> rightShift;
if (lastValue != 0)
*digitsResPtr = lastValue;
else
--length;
} // end else
return length;
} // end function
/// <summary>
/// Shifts big integer.
/// </summary>
/// <param name="digitsPtr">Big integer digits.</param>
/// <param name="length">Big integer length.</param>
/// <param name="digitsResPtr">Resulting big integer digits.</param>
/// <param name="rightShift">Shift to the right (always between 1 an 31).</param>
/// <param name="resHasOffset">True if <paramref name="digitsResPtr" /> has offset.</param>
/// <returns>Resulting big integer length.</returns>
static unsigned int Shr(const UInt32* digitsPtr, const UInt32 vlength,
UInt32* digitsResPtr, const int rightShift, const bool resHasOffset)
{
UInt32 length = vlength;
int rightShiftRev = Constants::DigitBitCount - rightShift;
// Shift first digit in special way
if (resHasOffset)
{
*(digitsResPtr - 1) = digitsPtr[0] << rightShiftRev;
} // end for
if (rightShift == 0)
{
// Handle special situation here - only memcpy is needed (maybe)
if (digitsPtr != digitsResPtr)
{
DigitHelper::DigitsBlockCopy(digitsPtr, digitsResPtr, length);
} // end if
} // end if
else
{
// Shift all digits except last one
UInt32* digitsPtrEndPrev = (UInt32 *)digitsPtr + length - 1;
UInt32* digitsPtrNext = (UInt32 *)digitsPtr + 1;
for (; digitsPtr < digitsPtrEndPrev; ++digitsPtr, ++digitsPtrNext, ++digitsResPtr)
{
*digitsResPtr = (*digitsPtr >> rightShift) | (*digitsPtrNext << rightShiftRev);
}
// Shift last digit in special way
UInt32 lastValue = *digitsPtr >> rightShift;
if (lastValue != 0)
{
*digitsResPtr = lastValue;
} // end if
else
{
--length;
} // end else
} // end else
return length;
} // end function Shr
/// <summary>
/// Performs bitwise OR for two big integers.
/// </summary>
/// <param name="digits1">First big integer digits.</param>
/// <param name="length1">First big integer length.</param>
/// <param name="digits2">Second big integer digits.</param>
/// <param name="length2">Second big integer length.</param>
/// <param name="digitsRes">Resulting big integer digits.</param>
static void BitwiseOr(const UInt32 digits1[], const UInt32 length1,
const UInt32 digits2[], const UInt32 length2, UInt32 digitsRes[])
{
UInt32* digitsPtr1 = (UInt32*)digits1;
UInt32* digitsPtr2 = (UInt32*)digits2;
UInt32* digitsResPtr = digitsRes;
for (UInt32 i = 0; i < length2; i++)
{
digitsResPtr[i] = digitsPtr1[i] | digitsPtr2[i];
} // end for
DigitHelper::DigitsBlockCopy(digitsPtr1 + length2, digitsResPtr + length2, length1 - length2);
} // end function BitwiseOr
/// <summary>
/// Performs bitwise AND for two big integers.
/// </summary>
/// <param name="digits1">First big integer digits.</param>
/// <param name="digits2">Second big integer digits.</param>
/// <param name="length">Shorter big integer length.</param>
/// <param name="digitsRes">Resulting big integer digits.</param>
/// <returns>Resulting big integer length.</returns>
static UInt32 BitwiseAnd(const UInt32 digits1[], const UInt32 digits2[],
const UInt32 length, UInt32 digitsRes[])
{
UInt32* digitsPtr1 = (UInt32*)digits1;
UInt32* digitsPtr2 = (UInt32*)digits2;
UInt32* digitsResPtr = digitsRes;
for (UInt32 i = 0; i < length; i++)
{
digitsResPtr[i] = digitsPtr1[i] & digitsPtr2[i];
} // end for
return DigitHelper::GetRealDigitsLength(digitsResPtr, length);
} // end function BitwiseAnd
/// <summary>
/// Performs bitwise XOR for two big integers.
/// </summary>
/// <param name="digits1">First big integer digits.</param>
/// <param name="length1">First big integer length.</param>
/// <param name="digits2">Second big integer digits.</param>
/// <param name="length2">Second big integer length.</param>
/// <param name="digitsRes">Resulting big integer digits.</param>
/// <returns>Resulting big integer length.</returns>
static UInt32 ExclusiveOr(const UInt32 digits1[], const UInt32 length1,
const UInt32 digits2[], const UInt32 length2, UInt32 digitsRes[])
{
UInt32* digitsPtr1 = (UInt32*)digits1;
UInt32* digitsPtr2 = (UInt32*)digits2;
UInt32* digitsResPtr = digitsRes;
for (UInt32 i = 0; i < length2; i++)
{
digitsResPtr[i] = digitsPtr1[i] ^ digitsPtr2[i];
} // end for
DigitHelper::DigitsBlockCopy(digitsPtr1 + length2, digitsResPtr + length2, length1 - length2);
return DigitHelper::GetRealDigitsLength(digitsResPtr, length1);
} // end function ExclusiveOr
/// <summary>
/// Performs bitwise NOT for big integer.
/// </summary>
/// <param name="digits">Big integer digits.</param>
/// <param name="length">Big integer length.</param>
/// <param name="digitsRes">Resulting big integer digits.</param>
/// <returns>Resulting big integer length.</returns>
static UInt32 OnesComplement(const UInt32 digits[], const UInt32 length, UInt32 digitsRes[])
{
UInt32* digitsPtr = (UInt32 *)digits;
UInt32* digitsResPtr = digitsRes;
for (UInt32 i = 0; i < length; i++)
{
digitsResPtr[i] = ~digitsPtr[i];
} // end for
return DigitHelper::GetRealDigitsLength(digitsResPtr, length);
} // end function OnesComplement
}; // end class DigitOpHelper
#endif //!DIGITOPHELPER_H