File: big_ops2.cpp

package info (click to toggle)
monotone 0.31-6
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 20,680 kB
  • ctags: 14,801
  • sloc: cpp: 87,711; ansic: 64,862; sh: 5,691; lisp: 954; perl: 783; makefile: 509; python: 265; sql: 98; sed: 16
file content (182 lines) | stat: -rw-r--r-- 5,007 bytes parent folder | download
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
/*************************************************
* BigInt Assignment Operators Source File        *
* (C) 1999-2005 The Botan Project                *
*************************************************/

#include <botan/bigint.h>
#include <botan/numthry.h>
#include <botan/mp_core.h>
#include <botan/bit_ops.h>

namespace Botan {

/*************************************************
* Addition Operator                              *
*************************************************/
BigInt& BigInt::operator+=(const BigInt& n)
   {
   if((sign() == n.sign()))
      {
      const u32bit reg_size = std::max(sig_words(), n.sig_words()) + 1;
      grow_to(reg_size);
      bigint_add2(get_reg(), reg_size-1, n.data(), n.sig_words());
      }
   else
      (*this) = (*this) + n;
   return (*this);
   }

/*************************************************
* Subtraction Operator                           *
*************************************************/
BigInt& BigInt::operator-=(const BigInt& n)
   {
   s32bit relative_size = bigint_cmp(data(), sig_words(),
                                     n.data(), n.sig_words());

   if(relative_size == 0)
      {
      if(sign() == n.sign())
         (*this) = 0;
      else
         (*this) <<= 1;
      return (*this);
      }

   const u32bit reg_size = std::max(sig_words(), n.sig_words()) + 1;
   grow_to(reg_size);

   if(relative_size == -1)
      {
      if(sign() == n.sign())
         (*this) = (*this) - n;
      else
         bigint_add2(get_reg(), reg_size-1, n.data(), n.sig_words());
      set_sign(n.reverse_sign());
      }
   if(relative_size == 1)
      {
      if(sign() == n.sign())
         bigint_sub2(get_reg(), sig_words(), n.data(), n.sig_words());
      else
         bigint_add2(get_reg(), reg_size-1, n.data(), n.sig_words());
      }
   return (*this);
   }

/*************************************************
* Multiplication Operator                        *
*************************************************/
BigInt& BigInt::operator*=(const BigInt& n)
   {
   if(is_zero()) return (*this);
   if(n.is_zero()) { (*this) = 0; return (*this); }

   if(sign() != n.sign())
      set_sign(Negative);
   else
      set_sign(Positive);

   const u32bit words = sig_words();
   const u32bit n_words = n.sig_words();

   if(words == 1 || n_words == 1)
      {
      grow_to(words + n_words);
      if(n_words == 1)
         bigint_linmul2(get_reg(), words, n.word_at(0));
      else
         bigint_linmul3(get_reg(), n.data(), n_words, word_at(0));
      return (*this);
      }

   BigInt z(sign(), size() + n.size());
   bigint_mul3(z.get_reg(), z.size(),
               data(),   size(),   words,
               n.data(), n.size(), n_words);
   (*this) = z;
   return (*this);
   }

/*************************************************
* Division Operator                              *
*************************************************/
BigInt& BigInt::operator/=(const BigInt& n)
   {
   if(n.sig_words() == 1 && power_of_2(n.word_at(0)))
      (*this) >>= (n.bits() - 1);
   else
      (*this) = (*this) / n;
   return (*this);
   }

/*************************************************
* Modulo Operator                                *
*************************************************/
BigInt& BigInt::operator%=(const BigInt& mod)
   {
   return (*this = (*this) % mod);
   }

/*************************************************
* Modulo Operator                                *
*************************************************/
word BigInt::operator%=(word mod)
   {
   if(mod == 0)
      throw BigInt::DivideByZero();

   if(power_of_2(mod))
      {
      word result = (word_at(0) & (mod - 1));
      clear();
      reg.grow_to(2);
      reg[0] = result;
      return result;
      }

   word remainder = 0;
   u32bit size = sig_words();

   for(u32bit j = size; j > 0; j--)
      remainder = bigint_modop(remainder, word_at(j-1), mod);
   clear();
   reg.grow_to(2);
   reg[0] = remainder;
   return word_at(0);
   }

/*************************************************
* Left Shift Operator                            *
*************************************************/
BigInt& BigInt::operator<<=(u32bit shift)
   {
   if(shift == 0) return (*this);
   const u32bit shift_words = shift / MP_WORD_BITS,
                shift_bits  = shift % MP_WORD_BITS;

   grow_to(sig_words() + shift_words + (shift_bits ? 1 : 0));
   bigint_shl1(get_reg(), sig_words(), shift_words, shift_bits);
   return (*this);
   }

/*************************************************
* Right Shift Operator                           *
*************************************************/
BigInt& BigInt::operator>>=(u32bit shift)
   {
   if(shift == 0) return (*this);

   if(bits() <= shift)
      {
      (*this) = 0;
      return (*this);
      }

   const u32bit shift_words = shift / MP_WORD_BITS,
                shift_bits  = shift % MP_WORD_BITS;
   bigint_shr1(get_reg(), sig_words(), shift_words, shift_bits);
   return (*this);
   }

}