Botan 1.10.17

mp_misc.cpp
Go to the documentation of this file.
1 /*
2 * MP Misc Functions
3 * (C) 1999-2008 Jack Lloyd
4 *
5 * Distributed under the terms of the Botan license
6 */
7
8 #include <botan/internal/mp_core.h>
9 #include <botan/internal/mp_asm.h>
10
11 namespace Botan {
12
13 extern "C" {
14
15 /*
16 * Core Division Operation
17 */
18 size_t bigint_divcore(word q, word y2, word y1,
19 word x3, word x2, word x1)
20 {
21 // Compute (y2,y1) * q
22
23 word y3 = 0;
24 y1 = word_madd2(q, y1, &y3);
25 y2 = word_madd2(q, y2, &y3);
26
27 // Return (y3,y2,y1) >? (x3,x2,x1)
28
29 if(y3 > x3) return 1;
30 if(y3 < x3) return 0;
31 if(y2 > x2) return 1;
32 if(y2 < x2) return 0;
33 if(y1 > x1) return 1;
34 if(y1 < x1) return 0;
35 return 0;
36 }
37
38 /*
39 * Compare two MP integers
40 */
41 s32bit bigint_cmp(const word x[], size_t x_size,
42 const word y[], size_t y_size)
43 {
44 if(x_size < y_size) { return (-bigint_cmp(y, y_size, x, x_size)); }
45
46 while(x_size > y_size)
47 {
48 if(x[x_size-1])
49 return 1;
50 x_size--;
51 }
52
53 for(size_t j = x_size; j > 0; --j)
54 {
55 if(x[j-1] > y[j-1])
56 return 1;
57 if(x[j-1] < y[j-1])
58 return -1;
59 }
60
61 return 0;
62 }
63
64 /*
65 * Do a 2-word/1-word Division
66 */
67 word bigint_divop(word n1, word n0, word d)
68 {
69 word high = n1 % d, quotient = 0;
70
71 for(size_t j = 0; j != MP_WORD_BITS; ++j)
72 {
73 word high_top_bit = (high & MP_WORD_TOP_BIT);
74
75 high <<= 1;
76 high |= (n0 >> (MP_WORD_BITS-1-j)) & 1;
77 quotient <<= 1;
78
79 if(high_top_bit || high >= d)
80 {
81 high -= d;
82 quotient |= 1;
83 }
84 }
85
86 return quotient;
87 }
88
89 /*
90 * Do a 2-word/1-word Modulo
91 */
92 word bigint_modop(word n1, word n0, word d)
93 {
94 word z = bigint_divop(n1, n0, d);
95 word dummy = 0;
96 z = word_madd2(z, d, &dummy);
97 return (n0-z);
98 }
99
100 }
101
102 }
Botan::bigint_divop
word bigint_divop(word n1, word n0, word d)
Definition: mp_misc.cpp:67
Botan::s32bit
signed int s32bit
Definition: types.h:37
Botan::word_madd2
word word_madd2(word a, word b, word *c)
Definition: mp_asm.h:86
Botan
Definition: algo_base.h:14
Botan::bigint_divcore
size_t bigint_divcore(word q, word y2, word y1, word x3, word x2, word x1)
Definition: mp_misc.cpp:18
Botan::MP_WORD_TOP_BIT
const word MP_WORD_TOP_BIT
Definition: mp_types.h:28
Botan::bigint_cmp
s32bit bigint_cmp(const word x[], size_t x_size, const word y[], size_t y_size)
Definition: mp_misc.cpp:41
Botan::bigint_modop
word bigint_modop(word n1, word n0, word d)
Definition: mp_misc.cpp:92
Botan::MP_WORD_BITS
const size_t MP_WORD_BITS
Definition: mp_core.h:18