10
10
11
11
#include < cstdlib>
12
12
#include < cctype>
13
- #include < cassert>
14
13
15
14
#include < sstream>
16
15
#include < ostream>
17
16
#include < limits>
18
17
19
18
#include " arith_tools.h"
20
-
19
+ # include " invariant.h "
21
20
22
21
typedef BigInt::ullong_t ullong_t ; // NOLINT(readability/identifiers)
23
22
typedef BigInt::llong_t llong_t ; // NOLINT(readability/identifiers)
@@ -192,23 +191,21 @@ const mp_integer binary2integer(const std::string &n, bool is_signed)
192
191
193
192
mp_integer::ullong_t integer2ulong (const mp_integer &n)
194
193
{
195
- assert (n.is_ulong ());
194
+ PRECONDITION (n.is_ulong ());
196
195
return n.to_ulong ();
197
196
}
198
197
199
198
std::size_t integer2size_t (const mp_integer &n)
200
199
{
201
- assert (n>=0 );
200
+ PRECONDITION (n>=0 && n<=std::numeric_limits<std:: size_t >:: max () );
202
201
mp_integer::ullong_t ull=integer2ulong (n);
203
- assert (ull <= std::numeric_limits<std::size_t >::max ());
204
- return (std::size_t )ull;
202
+ return (std::size_t ) ull;
205
203
}
206
204
207
205
unsigned integer2unsigned (const mp_integer &n)
208
206
{
209
- assert (n>=0 );
207
+ PRECONDITION (n>=0 && n<=std::numeric_limits< unsigned >:: max () );
210
208
mp_integer::ullong_t ull=integer2ulong (n);
211
- assert (ull <= std::numeric_limits<unsigned >::max ());
212
209
return (unsigned )ull;
213
210
}
214
211
@@ -217,6 +214,7 @@ unsigned integer2unsigned(const mp_integer &n)
217
214
// / (currently long long)
218
215
mp_integer bitwise_or (const mp_integer &a, const mp_integer &b)
219
216
{
217
+ PRECONDITION (a.is_ulong () && b.is_ulong ());
220
218
ullong_t result=a.to_ulong ()|b.to_ulong ();
221
219
return result;
222
220
}
@@ -226,6 +224,7 @@ mp_integer bitwise_or(const mp_integer &a, const mp_integer &b)
226
224
// / (currently long long)
227
225
mp_integer bitwise_and (const mp_integer &a, const mp_integer &b)
228
226
{
227
+ PRECONDITION (a.is_ulong () && b.is_ulong ());
229
228
ullong_t result=a.to_ulong ()&b.to_ulong ();
230
229
return result;
231
230
}
@@ -235,6 +234,7 @@ mp_integer bitwise_and(const mp_integer &a, const mp_integer &b)
235
234
// / (currently long long)
236
235
mp_integer bitwise_xor (const mp_integer &a, const mp_integer &b)
237
236
{
237
+ PRECONDITION (a.is_ulong () && b.is_ulong ());
238
238
ullong_t result=a.to_ulong ()^b.to_ulong ();
239
239
return result;
240
240
}
@@ -244,6 +244,7 @@ mp_integer bitwise_xor(const mp_integer &a, const mp_integer &b)
244
244
// / (currently long long)
245
245
mp_integer bitwise_neg (const mp_integer &a)
246
246
{
247
+ PRECONDITION (a.is_ulong ());
247
248
ullong_t result=~a.to_ulong ();
248
249
return result;
249
250
}
@@ -256,6 +257,7 @@ mp_integer arith_left_shift(
256
257
const mp_integer &b,
257
258
std::size_t true_size)
258
259
{
260
+ PRECONDITION (a.is_long () && b.is_ulong ());
259
261
ullong_t shift=b.to_ulong ();
260
262
if (shift>true_size && a!=mp_integer (0 ))
261
263
throw " shift value out of range" ;
@@ -276,6 +278,7 @@ mp_integer arith_right_shift(
276
278
const mp_integer &b,
277
279
std::size_t true_size)
278
280
{
281
+ PRECONDITION (a.is_long () && b.is_ulong ());
279
282
llong_t number=a.to_long ();
280
283
ullong_t shift=b.to_ulong ();
281
284
if (shift>true_size)
@@ -295,6 +298,7 @@ mp_integer logic_left_shift(
295
298
const mp_integer &b,
296
299
std::size_t true_size)
297
300
{
301
+ PRECONDITION (a.is_long () && b.is_ulong ());
298
302
ullong_t shift=b.to_ulong ();
299
303
if (shift>true_size && a!=mp_integer (0 ))
300
304
throw " shift value out of range" ;
@@ -320,6 +324,7 @@ mp_integer logic_right_shift(
320
324
const mp_integer &b,
321
325
std::size_t true_size)
322
326
{
327
+ PRECONDITION (a.is_long () && b.is_ulong ());
323
328
ullong_t shift=b.to_ulong ();
324
329
if (shift>true_size)
325
330
throw " shift value out of range" ;
@@ -336,6 +341,7 @@ mp_integer rotate_right(
336
341
const mp_integer &b,
337
342
std::size_t true_size)
338
343
{
344
+ PRECONDITION (a.is_ulong () && b.is_ulong ());
339
345
ullong_t number=a.to_ulong ();
340
346
ullong_t shift=b.to_ulong ();
341
347
if (shift>true_size)
@@ -355,6 +361,7 @@ mp_integer rotate_left(
355
361
const mp_integer &b,
356
362
std::size_t true_size)
357
363
{
364
+ PRECONDITION (a.is_ulong () && b.is_ulong ());
358
365
ullong_t number=a.to_ulong ();
359
366
ullong_t shift=b.to_ulong ();
360
367
if (shift>true_size)
0 commit comments