diff options
| author | Joshua Bakita <bakitajoshua@gmail.com> | 2019-10-07 19:13:39 -0400 |
|---|---|---|
| committer | Joshua Bakita <bakitajoshua@gmail.com> | 2019-10-07 19:13:39 -0400 |
| commit | 386b7d3366f1359a265da207a9cafa3edf553b64 (patch) | |
| tree | c76120c2c138faed822e4ae386be6ef22a738a78 /baseline/source/ammunition/arithm.c | |
| parent | 54a3f7091a2146b29c73a6fdc4b62a5c4ad7a3d8 (diff) | |
Reorganize and commit all the modified TACLeBench code and run scripts
Diffstat (limited to 'baseline/source/ammunition/arithm.c')
| -rw-r--r-- | baseline/source/ammunition/arithm.c | 1384 |
1 files changed, 1384 insertions, 0 deletions
diff --git a/baseline/source/ammunition/arithm.c b/baseline/source/ammunition/arithm.c new file mode 100644 index 0000000..9480846 --- /dev/null +++ b/baseline/source/ammunition/arithm.c | |||
| @@ -0,0 +1,1384 @@ | |||
| 1 | /* | ||
| 2 | FILE NAME: arithm.c | ||
| 3 | |||
| 4 | TITLE: Package for arbitrary precision integer arithmetic | ||
| 5 | |||
| 6 | DESCRIPTION: This abstract data implements arbitrary precision | ||
| 7 | integer and unsigned integer numbers by machine independent | ||
| 8 | way. The implementation of the package functions are not | ||
| 9 | sufficiently efficient in order to use for run-time. The | ||
| 10 | package functions are oriented to implement constant-folding in | ||
| 11 | compilers. This package is necessary because host machine may | ||
| 12 | not support such arithmetic for target machine. For example, | ||
| 13 | VAX does not support does not support more 32-bits integer | ||
| 14 | numbers arithmetic. The numbers are represented by bytes in | ||
| 15 | big endian mode, negative integer numbers are represented in | ||
| 16 | complementary code. All sizes are given in bytes and must be | ||
| 17 | positive. Results of executions of all functions can coincide | ||
| 18 | with a operand(s). All functions of addition, subtraction, | ||
| 19 | multiplication, division, evaluation of remainder, shift, | ||
| 20 | changing size and transformation of string into number fix | ||
| 21 | overflow. The overflow is fixed when result can not be | ||
| 22 | represented by number of given size. | ||
| 23 | |||
| 24 | */ | ||
| 25 | |||
| 26 | #include "arithm.h" | ||
| 27 | #include "ammunition_string.h" | ||
| 28 | |||
| 29 | |||
| 30 | /* This variable can have only two values 0 or 1. The value `1' | ||
| 31 | corresponds to overflow. The variable value are modified by all | ||
| 32 | functions of addition, subtract, multiplication, division, | ||
| 33 | evaluation of remainder, shift, changing size and transformation of | ||
| 34 | string into number fix overflow. */ | ||
| 35 | |||
| 36 | int ammunition_overflow_bit; | ||
| 37 | |||
| 38 | |||
| 39 | /* The following function adds unsigned integers. The function | ||
| 40 | returns 1 if unsigned integer overflow is fixed, 0 otherwise. | ||
| 41 | Result can be placed in any operand. */ | ||
| 42 | |||
| 43 | int ammunition_add_unsigned_integer_without_overflow_reaction | ||
| 44 | ( int size, const void *op1, const void *op2, void *result ) | ||
| 45 | { | ||
| 46 | int digit_num; | ||
| 47 | int carry; | ||
| 48 | unsigned int sum; | ||
| 49 | |||
| 50 | _Pragma( "loopbound min 4 max 4" ) | ||
| 51 | for ( digit_num = size - 1, carry = 0; digit_num >= 0; digit_num-- ) { | ||
| 52 | sum = ( ( ( unsigned char * ) op1 ) [digit_num] | ||
| 53 | + ( ( unsigned char * ) op2 ) [digit_num] + carry ); | ||
| 54 | if ( sum > UCHAR_MAX ) { | ||
| 55 | sum -= UCHAR_MAX + 1; | ||
| 56 | carry = 1; | ||
| 57 | } else | ||
| 58 | carry = 0; | ||
| 59 | ( ( unsigned char * ) result ) [digit_num] = sum; | ||
| 60 | } | ||
| 61 | return carry != 0; | ||
| 62 | } | ||
| 63 | |||
| 64 | /* The following function adds unsigned integers. The function | ||
| 65 | returns 1 if unsigned integer overflow (the first operand is less | ||
| 66 | than the second) is fixed, 0 otherwise. Result can be placed in | ||
| 67 | any operand. */ | ||
| 68 | |||
| 69 | int ammunition_subtract_unsigned_integer_without_overflow_reaction | ||
| 70 | ( int size, const void *op1, const void *op2, void *result ) | ||
| 71 | { | ||
| 72 | int digit_num; | ||
| 73 | int carry; | ||
| 74 | int subtraction; | ||
| 75 | |||
| 76 | _Pragma( "loopbound min 4 max 4" ) | ||
| 77 | for ( digit_num = size - 1, carry = 0; digit_num >= 0; digit_num-- ) { | ||
| 78 | subtraction = ( ( ( unsigned char * ) op1 ) [digit_num] | ||
| 79 | - ( ( unsigned char * ) op2 ) [digit_num] - carry ); | ||
| 80 | if ( subtraction < 0 ) { | ||
| 81 | subtraction += UCHAR_MAX + 1; | ||
| 82 | carry = 1; | ||
| 83 | } else | ||
| 84 | carry = 0; | ||
| 85 | ( ( unsigned char * ) result ) [digit_num] = subtraction; | ||
| 86 | } | ||
| 87 | return carry != 0; | ||
| 88 | } | ||
| 89 | |||
| 90 | /* The following function makes complementary code of number. Result | ||
| 91 | can be placed in operand. */ | ||
| 92 | |||
| 93 | void ammunition_make_complementary_code | ||
| 94 | ( int size, const void *operand, void *result ) | ||
| 95 | { | ||
| 96 | int digit_num; | ||
| 97 | int carry; | ||
| 98 | int subtraction; | ||
| 99 | |||
| 100 | _Pragma( "loopbound min 2 max 6" ) | ||
| 101 | for ( digit_num = size - 1, carry = 0; digit_num >= 0; digit_num-- ) { | ||
| 102 | subtraction = ( 0 - ( ( unsigned char * ) operand ) [digit_num] - carry ); | ||
| 103 | if ( subtraction != 0 ) { | ||
| 104 | subtraction += UCHAR_MAX + 1; | ||
| 105 | carry = 1; | ||
| 106 | } else | ||
| 107 | carry = 0; | ||
| 108 | ( ( unsigned char * ) result ) [digit_num] = subtraction; | ||
| 109 | } | ||
| 110 | } | ||
| 111 | |||
| 112 | /* The following function multiplys unsigned integer by digit (byte | ||
| 113 | size). The function returns 1 if unsigned integer overflow is | ||
| 114 | fixed, 0 otherwise. */ | ||
| 115 | |||
| 116 | int ammunition_multiply_unsigned_integer_by_digit_without_overflow_reaction | ||
| 117 | ( int size, void *operand, unsigned int digit ) | ||
| 118 | { | ||
| 119 | int digit_num; | ||
| 120 | unsigned int carry; | ||
| 121 | unsigned int sum; | ||
| 122 | |||
| 123 | _Pragma( "loopbound min 4 max 4" ) | ||
| 124 | for ( digit_num = size - 1, carry = 0; digit_num >= 0; digit_num-- ) { | ||
| 125 | sum = ( ( ( unsigned char * ) operand ) [digit_num] * digit + carry ); | ||
| 126 | if ( sum > UCHAR_MAX ) { | ||
| 127 | carry = sum / ( UCHAR_MAX + 1 ); | ||
| 128 | sum %= UCHAR_MAX + 1; | ||
| 129 | } else | ||
| 130 | carry = 0; | ||
| 131 | ( ( unsigned char * ) operand ) [digit_num] = sum; | ||
| 132 | } | ||
| 133 | return carry != 0; | ||
| 134 | } | ||
| 135 | |||
| 136 | |||
| 137 | /* Originally reaction on all integer and unsigned integer overflow is | ||
| 138 | equal to the following function. The function does nothing. */ | ||
| 139 | |||
| 140 | void | ||
| 141 | ammunition_arithmetic_overflow_reaction ( void ) | ||
| 142 | {} | ||
| 143 | |||
| 144 | |||
| 145 | /* Originally reaction on all integer and unsigned integer overflow is | ||
| 146 | equal to the following function. The function does nothing. */ | ||
| 147 | |||
| 148 | void | ||
| 149 | ammunition_arithmetic_unsigned_overflow_reaction ( void ) | ||
| 150 | {} | ||
| 151 | |||
| 152 | |||
| 153 | /* This page contains functions for arbitrary precision addition. */ | ||
| 154 | |||
| 155 | /* The function adds unsigned integers and fixes overflow reaction if | ||
| 156 | it is needed. The function makes this with the aid of function | ||
| 157 | `add_unsigned_integer_without_overflow_reaction'. Result can be | ||
| 158 | placed in any operand. */ | ||
| 159 | |||
| 160 | void | ||
| 161 | ammunition_add_unsigned_integer ( int size, const void *op1, const void *op2, | ||
| 162 | void *result ) | ||
| 163 | { | ||
| 164 | ammunition_overflow_bit | ||
| 165 | = ammunition_add_unsigned_integer_without_overflow_reaction ( | ||
| 166 | size, op1, op2, result ); | ||
| 167 | if ( ammunition_overflow_bit != 0 ) | ||
| 168 | ammunition_arithmetic_unsigned_overflow_reaction(); | ||
| 169 | } | ||
| 170 | |||
| 171 | /* The function adds integers and fixes overflow reaction if it is | ||
| 172 | needed. The function makes this with the aid of function | ||
| 173 | `add_unsigned_integer_without_overflow_reaction'. Result can be | ||
| 174 | placed in any operand. */ | ||
| 175 | |||
| 176 | void | ||
| 177 | ammunition_add_integer ( int size, const void *op1, const void *op2, | ||
| 178 | void *result ) | ||
| 179 | { | ||
| 180 | int op1_sign; | ||
| 181 | int sign_equality; | ||
| 182 | |||
| 183 | op1_sign = INTEGER_SIGN ( op1 ); | ||
| 184 | sign_equality = INTEGER_SIGN ( op1 ) == INTEGER_SIGN ( op2 ); | ||
| 185 | ammunition_add_unsigned_integer_without_overflow_reaction ( | ||
| 186 | size, op1, op2, result ); | ||
| 187 | ammunition_overflow_bit = sign_equality && | ||
| 188 | ( op1_sign != INTEGER_SIGN ( result ) ); | ||
| 189 | if ( ammunition_overflow_bit != 0 ) | ||
| 190 | ammunition_arithmetic_overflow_reaction(); | ||
| 191 | } | ||
| 192 | |||
| 193 | |||
| 194 | |||
| 195 | /* This page contains functions for arbitrary precision subtraction. */ | ||
| 196 | |||
| 197 | /* The function subtracts unsigned integers and fixes overflow | ||
| 198 | reaction if it is needed. The function makes this with the aid of | ||
| 199 | function `subtract_unsigned_integer_without_overflow_reaction'. | ||
| 200 | Result can be placed in any operand. */ | ||
| 201 | |||
| 202 | void | ||
| 203 | ammunition_subtract_unsigned_integer ( int size, const void *op1, | ||
| 204 | const void *op2, | ||
| 205 | void *result ) | ||
| 206 | { | ||
| 207 | ammunition_overflow_bit | ||
| 208 | = ammunition_subtract_unsigned_integer_without_overflow_reaction ( | ||
| 209 | size, op1, op2, result ); | ||
| 210 | if ( ammunition_overflow_bit != 0 ) | ||
| 211 | ammunition_arithmetic_unsigned_overflow_reaction(); | ||
| 212 | } | ||
| 213 | |||
| 214 | /* The function subtracts integers and fixes overflow reaction if it | ||
| 215 | is needed. The function makes this with the aid of function | ||
| 216 | `subtract_unsigned_integer_without_overflow_reaction'. Result can | ||
| 217 | be placed in any operand. */ | ||
| 218 | |||
| 219 | void | ||
| 220 | ammunition_subtract_integer ( int size, const void *op1, const void *op2, | ||
| 221 | void *result ) | ||
| 222 | { | ||
| 223 | int op1_sign; | ||
| 224 | int sign_unequality; | ||
| 225 | |||
| 226 | op1_sign = INTEGER_SIGN ( op1 ); | ||
| 227 | sign_unequality = INTEGER_SIGN ( op1 ) != INTEGER_SIGN ( op2 ); | ||
| 228 | ammunition_subtract_unsigned_integer_without_overflow_reaction ( | ||
| 229 | size, op1, op2, result ); | ||
| 230 | ammunition_overflow_bit = sign_unequality && | ||
| 231 | ( op1_sign != INTEGER_SIGN ( result ) ); | ||
| 232 | if ( ammunition_overflow_bit != 0 ) | ||
| 233 | ammunition_arithmetic_overflow_reaction(); | ||
| 234 | } | ||
| 235 | |||
| 236 | |||
| 237 | |||
| 238 | /* This page contains functions for arbitrary precision multiplication. */ | ||
| 239 | |||
| 240 | /* The following function multiplys unsigned integers. The function | ||
| 241 | returns 1 if unsigned integer overflow is fixed, 0 otherwise. | ||
| 242 | Result can be placed in any operand. */ | ||
| 243 | |||
| 244 | int ammunition_multiply_unsigned_integer_without_overflow_reaction | ||
| 245 | ( int size, const void *op1, const void *op2, void *result ) | ||
| 246 | { | ||
| 247 | int op1_digit_num; | ||
| 248 | int op2_digit_num; | ||
| 249 | int carry; | ||
| 250 | unsigned long int partial_sum; | ||
| 251 | int result_digit_number; | ||
| 252 | int overflow_flag; | ||
| 253 | unsigned char long_result [2 * MAX_INTEGER_OPERAND_SIZE]; | ||
| 254 | |||
| 255 | ammunition_memset ( long_result + size, 0, ( size_x ) size ); | ||
| 256 | _Pragma( "loopbound min 4 max 4" ) | ||
| 257 | for ( op2_digit_num = size - 1; op2_digit_num >= 0; op2_digit_num-- ) { | ||
| 258 | if ( ( ( unsigned char * ) op2 ) [op2_digit_num] != 0 ) { | ||
| 259 | _Pragma( "loopbound min 4 max 4" ) | ||
| 260 | for ( op1_digit_num = size - 1, carry = 0; op1_digit_num >= 0; | ||
| 261 | op1_digit_num-- ) { | ||
| 262 | partial_sum | ||
| 263 | = ( ( ( unsigned char * ) op1 ) [op1_digit_num] | ||
| 264 | * ( ( unsigned char * ) op2 ) [op2_digit_num] | ||
| 265 | + long_result [op1_digit_num + op2_digit_num + 1] | ||
| 266 | + carry ); | ||
| 267 | long_result [op1_digit_num + op2_digit_num + 1] | ||
| 268 | = ( unsigned char ) ( partial_sum % ( UCHAR_MAX + 1 ) ); | ||
| 269 | carry = partial_sum / ( UCHAR_MAX + 1 ); | ||
| 270 | } | ||
| 271 | long_result [op2_digit_num] = carry; | ||
| 272 | } else | ||
| 273 | long_result [op2_digit_num] = 0; | ||
| 274 | } | ||
| 275 | overflow_flag = 0; | ||
| 276 | _Pragma( "loopbound min 1 max 4" ) | ||
| 277 | for ( result_digit_number = size - 1; result_digit_number >= 0; | ||
| 278 | result_digit_number-- ) { | ||
| 279 | if ( long_result [result_digit_number] != 0 ) { | ||
| 280 | overflow_flag = 1; | ||
| 281 | break; | ||
| 282 | } | ||
| 283 | } | ||
| 284 | ammunition_memcpy ( result, long_result + size, ( size_x ) size ); | ||
| 285 | return overflow_flag; | ||
| 286 | } | ||
| 287 | |||
| 288 | /* The following function multiplys unsigned integers and fixes | ||
| 289 | overflow reaction if it is needed. The function makes this with | ||
| 290 | the aid of function | ||
| 291 | `multiply_unsigned_integer_without_overflow_reaction'. Result can | ||
| 292 | be placed in any operand. */ | ||
| 293 | |||
| 294 | void | ||
| 295 | ammunition_multiply_unsigned_integer ( int size, const void *op1, | ||
| 296 | const void *op2, | ||
| 297 | void *result ) | ||
| 298 | { | ||
| 299 | ammunition_overflow_bit = | ||
| 300 | ammunition_multiply_unsigned_integer_without_overflow_reaction ( | ||
| 301 | size, op1, op2, result ); | ||
| 302 | if ( ammunition_overflow_bit ) | ||
| 303 | ammunition_arithmetic_unsigned_overflow_reaction(); | ||
| 304 | } | ||
| 305 | |||
| 306 | /* The function multiplys integers and fixes overflow reaction if it | ||
| 307 | is needed. The function makes this with the aid of function | ||
| 308 | `multiply_unsigned_integer_without_overflow_reaction'. Result can | ||
| 309 | be placed in any operand. */ | ||
| 310 | |||
| 311 | void | ||
| 312 | ammunition_multiply_integer ( int size, const void *op1, const void *op2, | ||
| 313 | void *result ) | ||
| 314 | { | ||
| 315 | int negative_result_flag; | ||
| 316 | unsigned char op1_complementary [MAX_INTEGER_OPERAND_SIZE]; | ||
| 317 | unsigned char op2_complementary [MAX_INTEGER_OPERAND_SIZE]; | ||
| 318 | unsigned const char *abs_op1; | ||
| 319 | unsigned const char *abs_op2; | ||
| 320 | int unsigned_result_sign; | ||
| 321 | |||
| 322 | negative_result_flag = INTEGER_SIGN ( op1 ) != INTEGER_SIGN ( op2 ); | ||
| 323 | if ( INTEGER_SIGN ( op1 ) ) { | ||
| 324 | /* May be integer overflow. But result is correct because | ||
| 325 | it is unsigned. */ | ||
| 326 | ammunition_make_complementary_code ( size, op1, op1_complementary ); | ||
| 327 | abs_op1 = ( unsigned const char * )op1_complementary; | ||
| 328 | } else | ||
| 329 | abs_op1 = ( unsigned const char * )op1; | ||
| 330 | if ( INTEGER_SIGN ( op2 ) ) { | ||
| 331 | /* May be integer overflow. But result is correct because | ||
| 332 | it is unsigned. */ | ||
| 333 | ammunition_make_complementary_code ( size, op2, op2_complementary ); | ||
| 334 | abs_op2 = ( unsigned const char * )op2_complementary; | ||
| 335 | } else | ||
| 336 | abs_op2 = ( unsigned const char * )op2; | ||
| 337 | ammunition_overflow_bit = | ||
| 338 | ammunition_multiply_unsigned_integer_without_overflow_reaction ( | ||
| 339 | size, abs_op1, abs_op2, result ); | ||
| 340 | unsigned_result_sign = INTEGER_SIGN ( result ); | ||
| 341 | if ( negative_result_flag ) | ||
| 342 | ammunition_make_complementary_code ( size, result, result ); | ||
| 343 | if ( unsigned_result_sign | ||
| 344 | && ( !negative_result_flag | ||
| 345 | || INTEGER_SIGN ( result ) != unsigned_result_sign ) ) | ||
| 346 | /* Unsigned result can not be represented as integer. */ | ||
| 347 | ammunition_overflow_bit = 1; | ||
| 348 | if ( ammunition_overflow_bit ) | ||
| 349 | ammunition_arithmetic_overflow_reaction(); | ||
| 350 | } | ||
| 351 | |||
| 352 | |||
| 353 | |||
| 354 | /* This page contains functions for arbitrary precision division. */ | ||
| 355 | |||
| 356 | /* The following function divides unsigned integers. The function | ||
| 357 | returns 1 if unsigned integer overflow (division by zero) is fixed, | ||
| 358 | 0 otherwise. Result can be placed in any operand. See algorithm | ||
| 359 | in Knuth's book. */ | ||
| 360 | |||
| 361 | int ammunition_divide_unsigned_integer_without_overflow_reaction | ||
| 362 | ( int size, const void *op1, const void *op2, void *result ) | ||
| 363 | { | ||
| 364 | int scaled_op1_digit_num; | ||
| 365 | unsigned int q_approximation; | ||
| 366 | int first_nonzero_digit_number; | ||
| 367 | int op2_digit_number; | ||
| 368 | unsigned int scale; | ||
| 369 | unsigned char scaled_op1 [MAX_INTEGER_OPERAND_SIZE + 1]; | ||
| 370 | unsigned char normalized_op2 [MAX_INTEGER_OPERAND_SIZE]; | ||
| 371 | unsigned char extended_normalized_op2 [MAX_INTEGER_OPERAND_SIZE + 1]; | ||
| 372 | |||
| 373 | _Pragma( "loopbound min 4 max 4" ) | ||
| 374 | for ( op2_digit_number = 0; op2_digit_number < size; op2_digit_number++ ) { | ||
| 375 | if ( ( ( unsigned char * ) op2 ) [op2_digit_number] != 0 ) | ||
| 376 | break; | ||
| 377 | } | ||
| 378 | first_nonzero_digit_number = op2_digit_number; | ||
| 379 | if ( first_nonzero_digit_number == size ) { | ||
| 380 | /* Zero divisor */ | ||
| 381 | ammunition_memset ( result, 0, ( size_x ) size ); | ||
| 382 | return 1 /* TRUE */; | ||
| 383 | } else | ||
| 384 | if ( first_nonzero_digit_number == size - 1 ) { | ||
| 385 | /* Division by digit. */ | ||
| 386 | int digit_num; | ||
| 387 | int digit; | ||
| 388 | unsigned long divisable; | ||
| 389 | unsigned long remainder; | ||
| 390 | |||
| 391 | digit = ( ( unsigned char * ) op2 ) [first_nonzero_digit_number]; | ||
| 392 | ammunition_memcpy ( result, op1, ( size_x ) size ); | ||
| 393 | remainder = 0; | ||
| 394 | _Pragma( "loopbound min 4 max 4" ) | ||
| 395 | for ( digit_num = 0; digit_num < size; digit_num++ ) { | ||
| 396 | divisable = ( remainder * ( UCHAR_MAX + 1 ) | ||
| 397 | + ( ( unsigned char * ) result ) [digit_num] ); | ||
| 398 | remainder = divisable % digit; | ||
| 399 | ( ( unsigned char * ) result ) [digit_num] | ||
| 400 | = ( unsigned char ) ( divisable / digit ); | ||
| 401 | } | ||
| 402 | return 0 /* FALSE */; | ||
| 403 | } | ||
| 404 | /* Normalization of divisor. */ | ||
| 405 | scale = ( UCHAR_MAX + 1 ) / ( ( ( unsigned char * ) op2 ) [op2_digit_number] + | ||
| 406 | 1 ); | ||
| 407 | ammunition_memcpy ( scaled_op1 + 1, op1, ( size_x ) size ); | ||
| 408 | *scaled_op1 = 0; | ||
| 409 | |||
| 410 | ammunition_multiply_unsigned_integer_by_digit_without_overflow_reaction | ||
| 411 | ( size + 1, scaled_op1, scale ); | ||
| 412 | |||
| 413 | ammunition_memcpy ( normalized_op2, op2, ( size_x ) size ); | ||
| 414 | |||
| 415 | ammunition_multiply_unsigned_integer_by_digit_without_overflow_reaction | ||
| 416 | ( size, normalized_op2, scale ); | ||
| 417 | |||
| 418 | _Pragma( "loopbound min 0 max 0" ) | ||
| 419 | for ( scaled_op1_digit_num = 0; | ||
| 420 | scaled_op1_digit_num <= first_nonzero_digit_number; | ||
| 421 | scaled_op1_digit_num++ ) { | ||
| 422 | /* Division of `scaled_op1[scaled_op1_digit_number]..scaled_op1[size]' by | ||
| 423 | `normalized_op2[first_nonzero_digit_number]..normalized_op2[size-1]' | ||
| 424 | for evaluation of one digit of quotient | ||
| 425 | `result[size-1-first_nonzero_digit_number-scaled_op1_digit_number]'. | ||
| 426 | */ | ||
| 427 | if ( scaled_op1 [scaled_op1_digit_num] | ||
| 428 | == normalized_op2 [first_nonzero_digit_number] ) | ||
| 429 | q_approximation = UCHAR_MAX; | ||
| 430 | else | ||
| 431 | q_approximation | ||
| 432 | = ( scaled_op1 [scaled_op1_digit_num] * ( UCHAR_MAX + 1 ) | ||
| 433 | + scaled_op1 [scaled_op1_digit_num + 1] ) | ||
| 434 | / normalized_op2 [first_nonzero_digit_number]; | ||
| 435 | |||
| 436 | _Pragma( "loopbound min 0 max 0" ) | ||
| 437 | while ( normalized_op2 [first_nonzero_digit_number + 1] * q_approximation | ||
| 438 | > ( ( ( unsigned long int ) scaled_op1 [scaled_op1_digit_num] | ||
| 439 | * ( UCHAR_MAX + 1 ) | ||
| 440 | + scaled_op1 [scaled_op1_digit_num + 1] | ||
| 441 | - q_approximation | ||
| 442 | * normalized_op2 [first_nonzero_digit_number] ) | ||
| 443 | * ( UCHAR_MAX + 1 ) + scaled_op1 [scaled_op1_digit_num + 2] ) ) | ||
| 444 | q_approximation --; | ||
| 445 | |||
| 446 | /* Multiply and subtract */ | ||
| 447 | ammunition_memcpy ( extended_normalized_op2 + 1, | ||
| 448 | normalized_op2 + first_nonzero_digit_number, | ||
| 449 | ( size_x ) ( size - first_nonzero_digit_number ) ); | ||
| 450 | *extended_normalized_op2 = 0; | ||
| 451 | ammunition_multiply_unsigned_integer_by_digit_without_overflow_reaction | ||
| 452 | ( size - first_nonzero_digit_number + 1, extended_normalized_op2, | ||
| 453 | q_approximation ); | ||
| 454 | if ( ammunition_subtract_unsigned_integer_without_overflow_reaction | ||
| 455 | ( size - first_nonzero_digit_number + 1, | ||
| 456 | scaled_op1 + scaled_op1_digit_num, extended_normalized_op2, | ||
| 457 | scaled_op1 + scaled_op1_digit_num ) ) { | ||
| 458 | /* Negative result. Compensation by addition. */ | ||
| 459 | q_approximation--; | ||
| 460 | ammunition_memcpy ( extended_normalized_op2 + 1, | ||
| 461 | normalized_op2 + first_nonzero_digit_number, | ||
| 462 | ( size_x ) ( size - first_nonzero_digit_number ) ); | ||
| 463 | *extended_normalized_op2 = 0; | ||
| 464 | |||
| 465 | ammunition_add_unsigned_integer_without_overflow_reaction | ||
| 466 | ( size - first_nonzero_digit_number + 1, | ||
| 467 | scaled_op1 + scaled_op1_digit_num, extended_normalized_op2, | ||
| 468 | scaled_op1 + scaled_op1_digit_num ); | ||
| 469 | |||
| 470 | } | ||
| 471 | ( ( unsigned char * ) result ) [size - 1 - first_nonzero_digit_number | ||
| 472 | + scaled_op1_digit_num] = q_approximation; | ||
| 473 | } | ||
| 474 | ammunition_memset ( result, 0, | ||
| 475 | ( size_x ) ( size - 1 - first_nonzero_digit_number ) ); | ||
| 476 | return 0 /* TRUE */; | ||
| 477 | } | ||
| 478 | |||
| 479 | /* The function divides unsigned integers and fixes overflow reaction | ||
| 480 | if it is needed. The function makes this with the aid of function | ||
| 481 | `divide_unsigned_integer_without_overflow_reaction'. Result can be | ||
| 482 | placed in any operand. */ | ||
| 483 | |||
| 484 | void | ||
| 485 | ammunition_divide_unsigned_integer ( int size, const void *op1, const void *op2, | ||
| 486 | void *result ) | ||
| 487 | { | ||
| 488 | ammunition_overflow_bit = | ||
| 489 | ammunition_divide_unsigned_integer_without_overflow_reaction ( | ||
| 490 | size, op1, op2, result ); | ||
| 491 | if ( ammunition_overflow_bit ) | ||
| 492 | ammunition_arithmetic_unsigned_overflow_reaction(); | ||
| 493 | } | ||
| 494 | |||
| 495 | /* The function divides integers and fixes overflow reaction if it is | ||
| 496 | needed. The function makes this with the aid of function | ||
| 497 | `divide_unsigned_integer_without_overflow_reaction'. Result can be | ||
| 498 | placed in any operand. */ | ||
| 499 | |||
| 500 | void | ||
| 501 | ammunition_divide_integer ( int size, const void *op1, const void *op2, | ||
| 502 | void *result ) | ||
| 503 | { | ||
| 504 | int negative_result_flag; | ||
| 505 | unsigned char op1_complementary [MAX_INTEGER_OPERAND_SIZE]; | ||
| 506 | unsigned char op2_complementary [MAX_INTEGER_OPERAND_SIZE]; | ||
| 507 | unsigned const char *abs_op1; | ||
| 508 | unsigned const char *abs_op2; | ||
| 509 | int unsigned_result_sign; | ||
| 510 | |||
| 511 | negative_result_flag = INTEGER_SIGN ( op1 ) != INTEGER_SIGN ( op2 ); | ||
| 512 | if ( INTEGER_SIGN ( op1 ) ) { | ||
| 513 | /* May be integer overflow for minimal int. But result is correct because | ||
| 514 | it is unsigned. */ | ||
| 515 | ammunition_make_complementary_code ( size, op1, op1_complementary ); | ||
| 516 | abs_op1 = ( unsigned const char * )op1_complementary; | ||
| 517 | } else | ||
| 518 | abs_op1 = ( unsigned const char * )op1; | ||
| 519 | if ( INTEGER_SIGN ( op2 ) ) { | ||
| 520 | /* May be integer overflow for minimal int. But result is correct | ||
| 521 | because it is unsigned. */ | ||
| 522 | ammunition_make_complementary_code ( size, op2, op2_complementary ); | ||
| 523 | abs_op2 = ( unsigned const char * )op2_complementary; | ||
| 524 | } else | ||
| 525 | abs_op2 = ( unsigned const char * )op2; | ||
| 526 | ammunition_overflow_bit = | ||
| 527 | ammunition_divide_unsigned_integer_without_overflow_reaction ( | ||
| 528 | size, abs_op1, abs_op2, result ); | ||
| 529 | unsigned_result_sign = INTEGER_SIGN ( result ); | ||
| 530 | if ( negative_result_flag ) | ||
| 531 | ammunition_make_complementary_code ( size, result, result ); | ||
| 532 | if ( unsigned_result_sign | ||
| 533 | && ( !negative_result_flag | ||
| 534 | || INTEGER_SIGN ( result ) != unsigned_result_sign ) ) | ||
| 535 | /* Unsigned result can not be represented as integer. */ | ||
| 536 | ammunition_overflow_bit = 1; | ||
| 537 | if ( ammunition_overflow_bit ) | ||
| 538 | ammunition_arithmetic_overflow_reaction(); | ||
| 539 | } | ||
| 540 | |||
| 541 | |||
| 542 | |||
| 543 | /* This page contains functions for arbitrary precision evaluation of | ||
| 544 | remainder. */ | ||
| 545 | |||
| 546 | /* The function evaluates remainder of division of unsigned integers | ||
| 547 | as `op1 - (op1/op2)*op2' and fixes overflow reaction if it is | ||
| 548 | needed. Result can be placed in any operand. */ | ||
| 549 | |||
| 550 | void | ||
| 551 | ammunition_unsigned_integer_remainder ( int size, const void *op1, | ||
| 552 | const void *op2, | ||
| 553 | void *result ) | ||
| 554 | { | ||
| 555 | unsigned char temporary [MAX_INTEGER_OPERAND_SIZE]; | ||
| 556 | |||
| 557 | ammunition_divide_unsigned_integer ( size, op1, op2, temporary ); | ||
| 558 | if ( ammunition_overflow_bit ) | ||
| 559 | /* Reaction on zero is called from `divide_unsigned_integer'. */ | ||
| 560 | ammunition_memset ( result, 0, ( size_x ) size ); | ||
| 561 | else { | ||
| 562 | ammunition_multiply_unsigned_integer ( size, temporary, op2, temporary ); | ||
| 563 | ammunition_subtract_unsigned_integer ( size, op1, temporary, result ); | ||
| 564 | } | ||
| 565 | } | ||
| 566 | |||
| 567 | |||
| 568 | /* This page contains functions for arbitrary precision number shifts. */ | ||
| 569 | |||
| 570 | /* This function makes right shift of unsigned integer of given size | ||
| 571 | on given number of bits. If number of bits is negative the | ||
| 572 | function makes shift to left actually with the aid of function | ||
| 573 | `unsigned_integer_shift_left'. The function fixes overflow when | ||
| 574 | result can not be represented by number of given size, i.e. in | ||
| 575 | other words the opposite unsigned shift (to left) results in number | ||
| 576 | not equal to source operand. Result can be placed in operand. */ | ||
| 577 | |||
| 578 | void | ||
| 579 | ammunition_unsigned_integer_shift_right ( int size, const void *operand, | ||
| 580 | int bits, void *result ) | ||
| 581 | { | ||
| 582 | int byte_number; | ||
| 583 | unsigned byte; | ||
| 584 | unsigned carry; | ||
| 585 | int bit_shift; | ||
| 586 | int byte_shift; | ||
| 587 | |||
| 588 | |||
| 589 | if ( bits < 0 ) | ||
| 590 | ammunition_unsigned_integer_shift_left ( size, operand, -bits, result ); | ||
| 591 | else { | ||
| 592 | ammunition_overflow_bit = 0; | ||
| 593 | byte_shift = bits / CHAR_BIT; | ||
| 594 | bit_shift = bits % CHAR_BIT; | ||
| 595 | _Pragma( "loopbound min 0 max 3" ) | ||
| 596 | for ( byte_number = ( byte_shift >= size ? 0 : size - byte_shift ); | ||
| 597 | byte_number < size; byte_number++ ) | ||
| 598 | if ( ( ( unsigned char * ) operand ) [byte_number] != 0 ) { | ||
| 599 | ammunition_overflow_bit = 1; | ||
| 600 | break; | ||
| 601 | } | ||
| 602 | if ( byte_shift >= size ) | ||
| 603 | ammunition_memset ( result, 0, ( size_x ) size ); | ||
| 604 | else { | ||
| 605 | ammunition_memmove ( ( char * ) result + byte_shift, operand, | ||
| 606 | ( size_x ) ( size - byte_shift ) ); | ||
| 607 | ammunition_memset ( result, 0, ( size_x ) byte_shift ); | ||
| 608 | if ( bit_shift == 0 ) | ||
| 609 | return; | ||
| 610 | _Pragma( "loopbound min 3 max 3" ) | ||
| 611 | for ( byte_number = byte_shift, carry = 0; byte_number < size; | ||
| 612 | byte_number++ ) { | ||
| 613 | byte = ( ( unsigned char * ) result ) [byte_number]; | ||
| 614 | ( ( unsigned char * ) result ) [byte_number] | ||
| 615 | = carry | ( byte >> bit_shift ); | ||
| 616 | carry = ( byte << ( CHAR_BIT - bit_shift ) ) & UCHAR_MAX; | ||
| 617 | } | ||
| 618 | if ( carry != 0 ) | ||
| 619 | ammunition_overflow_bit = 1; | ||
| 620 | } | ||
| 621 | if ( ammunition_overflow_bit ) | ||
| 622 | ammunition_arithmetic_unsigned_overflow_reaction(); | ||
| 623 | } | ||
| 624 | } | ||
| 625 | |||
| 626 | /* This function makes right arithmetic shift of integer of given size | ||
| 627 | on given number of bits. If number of bits is negative the | ||
| 628 | function makes shift to left actually with the aid of function | ||
| 629 | `integer_shift_left'. The function fixes overflow when result can | ||
| 630 | not be represented by number of given size, i.e. in other words the | ||
| 631 | opposite shift (to left) results in number not equal to source | ||
| 632 | operand. Result can be placed in operand. */ | ||
| 633 | |||
| 634 | void | ||
| 635 | ammunition_integer_shift_right ( int size, const void *operand, int bits, | ||
| 636 | void *result ) | ||
| 637 | { | ||
| 638 | int byte_number; | ||
| 639 | unsigned byte; | ||
| 640 | unsigned carry; | ||
| 641 | int bit_shift; | ||
| 642 | int byte_shift; | ||
| 643 | int operand_sign; | ||
| 644 | |||
| 645 | if ( bits < 0 ) | ||
| 646 | ammunition_integer_shift_left ( size, operand, -bits, result ); | ||
| 647 | else { | ||
| 648 | operand_sign = INTEGER_SIGN ( operand ); | ||
| 649 | ammunition_overflow_bit = 0; | ||
| 650 | byte_shift = bits / CHAR_BIT; | ||
| 651 | bit_shift = bits % CHAR_BIT; | ||
| 652 | _Pragma( "loopbound min 0 max 3" ) | ||
| 653 | for ( byte_number = ( byte_shift >= size ? 0 : size - byte_shift ); | ||
| 654 | byte_number < size; byte_number++ ) | ||
| 655 | if ( ( ( unsigned char * ) operand ) [byte_number] != 0 ) { | ||
| 656 | ammunition_overflow_bit = 1; | ||
| 657 | break; | ||
| 658 | } | ||
| 659 | if ( byte_shift >= size ) | ||
| 660 | ammunition_memset ( result, | ||
| 661 | ( operand_sign ? UCHAR_MAX : 0 ), ( size_x ) size ); | ||
| 662 | else { | ||
| 663 | ammunition_memmove ( ( char * ) result + byte_shift, operand, | ||
| 664 | ( size_x ) ( size - byte_shift ) ); | ||
| 665 | ammunition_memset ( result, ( operand_sign ? UCHAR_MAX : 0 ), | ||
| 666 | ( size_x ) byte_shift ); | ||
| 667 | if ( bit_shift == 0 ) | ||
| 668 | return; | ||
| 669 | carry = ( ( ( operand_sign ? UCHAR_MAX : 0 ) << ( CHAR_BIT - bit_shift ) ) | ||
| 670 | & UCHAR_MAX ); | ||
| 671 | _Pragma( "loopbound min 3 max 3" ) | ||
| 672 | for ( byte_number = byte_shift; byte_number < size; byte_number++ ) { | ||
| 673 | byte = ( ( unsigned char * ) result ) [byte_number]; | ||
| 674 | ( ( unsigned char * ) result ) [byte_number] | ||
| 675 | = carry | ( byte >> bit_shift ); | ||
| 676 | carry = ( byte << ( CHAR_BIT - bit_shift ) ) & UCHAR_MAX; | ||
| 677 | } | ||
| 678 | if ( carry != 0 ) | ||
| 679 | ammunition_overflow_bit = 1; | ||
| 680 | } | ||
| 681 | if ( ammunition_overflow_bit ) | ||
| 682 | ammunition_arithmetic_overflow_reaction(); | ||
| 683 | } | ||
| 684 | } | ||
| 685 | |||
| 686 | /* This function makes left shift of unsigned integer of given size on | ||
| 687 | given number of bits. If number of bits is negative the function | ||
| 688 | makes shift to left actually with the aid of function | ||
| 689 | `unsigned_integer_shift_right'. The function fixes overflow when | ||
| 690 | result can not be represented by number of given size, i.e. i.e. in | ||
| 691 | other words the opposite shift (to right) results in number not | ||
| 692 | equal to source operand. Result can be placed in operand. */ | ||
| 693 | |||
| 694 | void | ||
| 695 | ammunition_unsigned_integer_shift_left ( int size, const void *operand, | ||
| 696 | int bits, void *result ) | ||
| 697 | { | ||
| 698 | int byte_number; | ||
| 699 | unsigned byte; | ||
| 700 | unsigned carry; | ||
| 701 | int bit_shift; | ||
| 702 | int byte_shift; | ||
| 703 | |||
| 704 | if ( bits < 0 ) | ||
| 705 | ammunition_unsigned_integer_shift_right ( size, operand, -bits, result ); | ||
| 706 | else { | ||
| 707 | ammunition_overflow_bit = 0; | ||
| 708 | byte_shift = bits / CHAR_BIT; | ||
| 709 | bit_shift = bits % CHAR_BIT; | ||
| 710 | _Pragma( "loopbound min 0 max 2" ) | ||
| 711 | for ( byte_number = 0; byte_number < byte_shift && byte_number < size; | ||
| 712 | byte_number++ ) | ||
| 713 | if ( ( ( unsigned char * ) operand ) [byte_number] != 0 ) { | ||
| 714 | ammunition_overflow_bit = 1; | ||
| 715 | break; | ||
| 716 | } | ||
| 717 | if ( byte_shift >= size ) | ||
| 718 | ammunition_memset ( result, 0, ( size_x ) size ); | ||
| 719 | else { | ||
| 720 | ammunition_memmove ( result, ( char * ) operand + byte_shift, | ||
| 721 | ( size_x ) ( size - byte_shift ) ); | ||
| 722 | ammunition_memset ( ( char * ) result + ( size - byte_shift ), 0, | ||
| 723 | ( size_x ) byte_shift ); | ||
| 724 | if ( bit_shift == 0 ) | ||
| 725 | return; | ||
| 726 | _Pragma( "loopbound min 2 max 3" ) | ||
| 727 | for ( byte_number = size - byte_shift - 1, carry = 0; | ||
| 728 | byte_number >= 0; byte_number-- ) { | ||
| 729 | byte = ( ( unsigned char * ) result ) [byte_number]; | ||
| 730 | ( ( unsigned char * ) result ) [byte_number] | ||
| 731 | = carry | ( byte << bit_shift ); | ||
| 732 | carry = byte >> ( CHAR_BIT - bit_shift ); | ||
| 733 | } | ||
| 734 | if ( carry != 0 ) | ||
| 735 | ammunition_overflow_bit = 1; | ||
| 736 | } | ||
| 737 | if ( ammunition_overflow_bit ) | ||
| 738 | ammunition_arithmetic_unsigned_overflow_reaction(); | ||
| 739 | } | ||
| 740 | } | ||
| 741 | |||
| 742 | /* This function makes left arithmetic shift of integer of given size | ||
| 743 | on given number of bits. If number of bits is negative the | ||
| 744 | function makes shift to left actually with the aid of function | ||
| 745 | `integer_shift_right'. The function fixes overflow when result can | ||
| 746 | not be represented by number of given size, i.e. in other words the | ||
| 747 | opposite shift (to right) results in number not equal to source | ||
| 748 | operand. Result can be placed in operand. */ | ||
| 749 | |||
| 750 | void | ||
| 751 | ammunition_integer_shift_left ( int size, const void *operand, int bits, | ||
| 752 | void *result ) | ||
| 753 | { | ||
| 754 | int byte_number; | ||
| 755 | unsigned byte; | ||
| 756 | unsigned carry; | ||
| 757 | int bit_shift; | ||
| 758 | int byte_shift; | ||
| 759 | int operand_sign; | ||
| 760 | |||
| 761 | if ( bits < 0 ) | ||
| 762 | ammunition_integer_shift_right ( size, operand, -bits, result ); | ||
| 763 | else { | ||
| 764 | operand_sign = INTEGER_SIGN ( operand ); | ||
| 765 | ammunition_overflow_bit = 0; | ||
| 766 | byte_shift = bits / CHAR_BIT; | ||
| 767 | bit_shift = bits % CHAR_BIT; | ||
| 768 | _Pragma( "loopbound min 0 max 2" ) | ||
| 769 | for ( byte_number = 0; byte_number < byte_shift && byte_number < size; | ||
| 770 | byte_number++ ) | ||
| 771 | if ( ( ( unsigned char * ) operand ) [byte_number] | ||
| 772 | != ( operand_sign ? UCHAR_MAX : 0 ) ) { | ||
| 773 | ammunition_overflow_bit = 1; | ||
| 774 | break; | ||
| 775 | } | ||
| 776 | if ( byte_shift >= size ) | ||
| 777 | ammunition_memset ( result, 0, ( size_x ) size ); | ||
| 778 | else { | ||
| 779 | ammunition_memmove ( result, ( char * ) operand + byte_shift, | ||
| 780 | ( size_x ) ( size - byte_shift ) ); | ||
| 781 | ammunition_memset ( ( char * ) result + ( size - byte_shift ), 0, | ||
| 782 | ( size_x ) byte_shift ); | ||
| 783 | if ( bit_shift == 0 ) | ||
| 784 | return; | ||
| 785 | _Pragma( "loopbound min 2 max 3" ) | ||
| 786 | for ( byte_number = size - byte_shift - 1, carry = 0; | ||
| 787 | byte_number >= 0; byte_number-- ) { | ||
| 788 | byte = ( ( unsigned char * ) result ) [byte_number]; | ||
| 789 | ( ( unsigned char * ) result ) [byte_number] | ||
| 790 | = carry | ( byte << bit_shift ); | ||
| 791 | carry = byte >> ( CHAR_BIT - bit_shift ); | ||
| 792 | } | ||
| 793 | if ( carry != ( ( unsigned ) ( operand_sign ? UCHAR_MAX : 0 ) | ||
| 794 | >> ( CHAR_BIT - bit_shift ) ) ) | ||
| 795 | ammunition_overflow_bit = 1; | ||
| 796 | } | ||
| 797 | if ( operand_sign != INTEGER_SIGN ( result ) ) | ||
| 798 | ammunition_overflow_bit = 1; | ||
| 799 | if ( ammunition_overflow_bit ) | ||
| 800 | ammunition_arithmetic_overflow_reaction(); | ||
| 801 | } | ||
| 802 | } | ||
| 803 | |||
| 804 | |||
| 805 | |||
| 806 | /* This page contains functions for bitwise operations of arbitrary | ||
| 807 | precision numbers. */ | ||
| 808 | |||
| 809 | /* This function makes bitwise `or' of two integers of given size. */ | ||
| 810 | |||
| 811 | void | ||
| 812 | ammunition_integer_or ( int size, const void *op1, const void *op2, | ||
| 813 | void *result ) | ||
| 814 | { | ||
| 815 | int byte_number; | ||
| 816 | |||
| 817 | _Pragma( "loopbound min 4 max 4" ) | ||
| 818 | for ( byte_number = 0; byte_number < size; byte_number++ ) { | ||
| 819 | ( ( unsigned char * ) result ) [byte_number] | ||
| 820 | = ( ( unsigned char * ) op1 ) [byte_number] | ||
| 821 | | ( ( unsigned char * ) op2 ) [byte_number]; | ||
| 822 | } | ||
| 823 | } | ||
| 824 | |||
| 825 | /* This function makes bitwise `or' of two unsigned integers of given | ||
| 826 | size. */ | ||
| 827 | |||
| 828 | void | ||
| 829 | ammunition_unsigned_integer_or ( int size, const void *op1, const void *op2, | ||
| 830 | void *result ) | ||
| 831 | { | ||
| 832 | ammunition_integer_or ( size, op1, op2, result ); | ||
| 833 | } | ||
| 834 | |||
| 835 | |||
| 836 | /* This function makes bitwise `and' of two integers of given size. */ | ||
| 837 | |||
| 838 | void | ||
| 839 | ammunition_integer_and ( int size, const void *op1, const void *op2, | ||
| 840 | void *result ) | ||
| 841 | { | ||
| 842 | int byte_number; | ||
| 843 | |||
| 844 | _Pragma( "loopbound min 4 max 4" ) | ||
| 845 | for ( byte_number = 0; byte_number < size; byte_number++ ) { | ||
| 846 | ( ( unsigned char * ) result ) [byte_number] | ||
| 847 | = ( ( unsigned char * ) op1 ) [byte_number] | ||
| 848 | & ( ( unsigned char * ) op2 ) [byte_number]; | ||
| 849 | } | ||
| 850 | } | ||
| 851 | |||
| 852 | /* This function makes bitwise `and' of two unsigned integers of given | ||
| 853 | size. */ | ||
| 854 | |||
| 855 | void | ||
| 856 | ammunition_unsigned_integer_and ( int size, const void *op1, const void *op2, | ||
| 857 | void *result ) | ||
| 858 | { | ||
| 859 | ammunition_integer_and ( size, op1, op2, result ); | ||
| 860 | } | ||
| 861 | |||
| 862 | |||
| 863 | /* This function makes bitwise `not' of integer of given size. */ | ||
| 864 | |||
| 865 | void | ||
| 866 | ammunition_integer_not ( int size, const void *operand, void *result ) | ||
| 867 | { | ||
| 868 | int byte_number; | ||
| 869 | |||
| 870 | _Pragma( "loopbound min 4 max 4" ) | ||
| 871 | for ( byte_number = 0; byte_number < size; byte_number++ ) { | ||
| 872 | ( ( unsigned char * ) result ) [byte_number] | ||
| 873 | = ( ( unsigned char * ) operand ) [byte_number] ^ UCHAR_MAX; | ||
| 874 | } | ||
| 875 | } | ||
| 876 | |||
| 877 | /* This function makes bitwise `not' of unsigned integer of given | ||
| 878 | size. */ | ||
| 879 | |||
| 880 | void | ||
| 881 | ammunition_unsigned_integer_not ( int size, const void *operand, void *result ) | ||
| 882 | { | ||
| 883 | ammunition_integer_not ( size, operand, result ); | ||
| 884 | } | ||
| 885 | |||
| 886 | |||
| 887 | |||
| 888 | /* This page contains functions for comparison of arbitrary precision | ||
| 889 | numbers. */ | ||
| 890 | |||
| 891 | /* This function compares two unsigned integers of given size on | ||
| 892 | equality. The function returns 1 if unsigned integers are equal, 0 | ||
| 893 | otherwise. */ | ||
| 894 | |||
| 895 | int | ||
| 896 | ammunition_eq_unsigned_integer ( int size, const void *op1, const void *op2 ) | ||
| 897 | { | ||
| 898 | return ammunition_memcmp ( op1, op2, ( size_x ) size ) == 0; | ||
| 899 | } | ||
| 900 | |||
| 901 | /* This function compares two integers of given size on equality. The | ||
| 902 | function returns 1 if integers are equal, 0 otherwise. */ | ||
| 903 | |||
| 904 | int | ||
| 905 | ammunition_eq_integer ( int size, const void *op1, const void *op2 ) | ||
| 906 | { | ||
| 907 | return ammunition_memcmp ( op1, op2, ( size_x ) size ) == 0; | ||
| 908 | } | ||
| 909 | |||
| 910 | /* This function compares two unsigned integers of given size on | ||
| 911 | inequality. The function returns 1 if unsigned integers are not | ||
| 912 | equal, 0 otherwise. */ | ||
| 913 | |||
| 914 | int | ||
| 915 | ammunition_ne_unsigned_integer ( int size, const void *op1, const void *op2 ) | ||
| 916 | { | ||
| 917 | return ammunition_memcmp ( op1, op2, ( size_x ) size ) != 0; | ||
| 918 | } | ||
| 919 | |||
| 920 | /* This function compares two integers of given size on inequality. | ||
| 921 | The function returns 1 if integers are not equal, 0 otherwise. */ | ||
| 922 | |||
| 923 | int | ||
| 924 | ammunition_ne_integer ( int size, const void *op1, const void *op2 ) | ||
| 925 | { | ||
| 926 | return ammunition_memcmp ( op1, op2, ( size_x ) size ) != 0; | ||
| 927 | } | ||
| 928 | |||
| 929 | |||
| 930 | /* This function compares two memory parts of given size on that the | ||
| 931 | first operand is greater than the second. The bytes are described | ||
| 932 | as unsigned. The function returns 1 if the first operand is | ||
| 933 | greater than the second, - 1 if the first operand is less than the | ||
| 934 | second, 0 otherwise. */ | ||
| 935 | |||
| 936 | int ammunition_bytes_comparison ( const void *op1, const void *op2, int size ) | ||
| 937 | { | ||
| 938 | const unsigned char *str1 = ( unsigned const char * )op1; | ||
| 939 | const unsigned char *str2 = ( unsigned const char * )op2; | ||
| 940 | |||
| 941 | _Pragma( "loopbound min 1 max 4" ) | ||
| 942 | while ( size > 0 && *str1 == *str2 ) { | ||
| 943 | str1++; | ||
| 944 | str2++; | ||
| 945 | size--; | ||
| 946 | } | ||
| 947 | if ( size <= 0 ) | ||
| 948 | return 0; | ||
| 949 | else | ||
| 950 | if ( *str1 > *str2 ) | ||
| 951 | return 1; | ||
| 952 | else | ||
| 953 | return -1; | ||
| 954 | } | ||
| 955 | |||
| 956 | /* This function compares two unsigned integers of given size on that | ||
| 957 | the first operand is greater than the second. The function returns | ||
| 958 | 1 if the first unsigned integer is greater than the second, 0 | ||
| 959 | otherwise. */ | ||
| 960 | |||
| 961 | int | ||
| 962 | ammunition_gt_unsigned_integer ( int size, const void *op1, const void *op2 ) | ||
| 963 | { | ||
| 964 | return ammunition_bytes_comparison ( op1, op2, size ) > 0; | ||
| 965 | } | ||
| 966 | |||
| 967 | /* This function compares two integers of given size on that the first | ||
| 968 | operand is greater than the second. The function returns 1 if the | ||
| 969 | first integer is greater than the second, 0 otherwise. */ | ||
| 970 | |||
| 971 | int ammunition_gt_integer ( int size, const void *op1, const void *op2 ) | ||
| 972 | { | ||
| 973 | if ( INTEGER_SIGN ( op1 ) == 0 ) { | ||
| 974 | if ( INTEGER_SIGN ( op2 ) == 0 ) | ||
| 975 | return ammunition_bytes_comparison ( op1, op2, size ) > 0; | ||
| 976 | else | ||
| 977 | return 1; /* TRUE */ | ||
| 978 | } else | ||
| 979 | if ( INTEGER_SIGN ( op2 ) == 0 ) | ||
| 980 | return 0; /*FALSE*/ | ||
| 981 | else | ||
| 982 | return ammunition_bytes_comparison ( op1, op2, size ) > 0; | ||
| 983 | } | ||
| 984 | |||
| 985 | /* This function compares two unsigned integers of given size on that | ||
| 986 | the first operand is less than the second. The function returns 1 | ||
| 987 | if the first unsigned integer is less than the second, 0 | ||
| 988 | otherwise. */ | ||
| 989 | |||
| 990 | int | ||
| 991 | ammunition_lt_unsigned_integer ( int size, const void *op1, const void *op2 ) | ||
| 992 | { | ||
| 993 | return ammunition_bytes_comparison ( op1, op2, size ) < 0; | ||
| 994 | } | ||
| 995 | |||
| 996 | /* This function compares two integers of given size on that the first | ||
| 997 | operand is less than the second. The function returns 1 if the | ||
| 998 | first integer is less than the second, 0 otherwise. */ | ||
| 999 | |||
| 1000 | int | ||
| 1001 | ammunition_lt_integer ( int size, const void *op1, const void *op2 ) | ||
| 1002 | { | ||
| 1003 | if ( INTEGER_SIGN ( op1 ) == 0 ) { | ||
| 1004 | if ( INTEGER_SIGN ( op2 ) == 0 ) | ||
| 1005 | return ammunition_bytes_comparison ( op1, op2, size ) < 0; | ||
| 1006 | else | ||
| 1007 | return 0; /*FALSE*/ | ||
| 1008 | } else | ||
| 1009 | if ( INTEGER_SIGN ( op2 ) == 0 ) | ||
| 1010 | return 1; /* TRUE */ | ||
| 1011 | else | ||
| 1012 | return ammunition_bytes_comparison ( op1, op2, size ) < 0; | ||
| 1013 | } | ||
| 1014 | |||
| 1015 | /* This function compares two unsigned integers of given size on that | ||
| 1016 | the first operand is greater than or equal to the second. The | ||
| 1017 | function returns 1 if the first unsigned integer is greater than or | ||
| 1018 | equal to the second, 0 otherwise. */ | ||
| 1019 | |||
| 1020 | int | ||
| 1021 | ammunition_ge_unsigned_integer ( int size, const void *op1, const void *op2 ) | ||
| 1022 | { | ||
| 1023 | return ammunition_bytes_comparison ( op1, op2, size ) >= 0; | ||
| 1024 | } | ||
| 1025 | |||
| 1026 | /* This function compares two integers of given size on that the first | ||
| 1027 | operand is greater than or equal to the second. The function | ||
| 1028 | returns 1 if the first integer is greater than or equal to the | ||
| 1029 | second, 0 otherwise. */ | ||
| 1030 | |||
| 1031 | int | ||
| 1032 | ammunition_ge_integer ( int size, const void *op1, const void *op2 ) | ||
| 1033 | { | ||
| 1034 | if ( INTEGER_SIGN ( op1 ) == 0 ) { | ||
| 1035 | if ( INTEGER_SIGN ( op2 ) == 0 ) | ||
| 1036 | return ammunition_bytes_comparison ( op1, op2, size ) >= 0; | ||
| 1037 | else | ||
| 1038 | return 1; /* TRUE */ | ||
| 1039 | } else | ||
| 1040 | if ( INTEGER_SIGN ( op2 ) == 0 ) | ||
| 1041 | return 0; /*FALSE*/ | ||
| 1042 | else | ||
| 1043 | return ammunition_bytes_comparison ( op1, op2, size ) >= 0; | ||
| 1044 | } | ||
| 1045 | |||
| 1046 | /* This function compares two unsigned integers of given size on that | ||
| 1047 | the first operand is less than or equal to the second. The | ||
| 1048 | function returns 1 if the first unsigned integer is less than or | ||
| 1049 | equal to the second, 0 otherwise. */ | ||
| 1050 | |||
| 1051 | int | ||
| 1052 | ammunition_le_unsigned_integer ( int size, const void *op1, const void *op2 ) | ||
| 1053 | { | ||
| 1054 | return ammunition_bytes_comparison ( op1, op2, size ) <= 0; | ||
| 1055 | } | ||
| 1056 | |||
| 1057 | /* This function compares two integers of given size on that the first | ||
| 1058 | operand is less than or equal to the second. The function returns | ||
| 1059 | 1 if the first integer is less than or equal to the second, 0 | ||
| 1060 | otherwise. */ | ||
| 1061 | |||
| 1062 | int | ||
| 1063 | ammunition_le_integer ( int size, const void *op1, const void *op2 ) | ||
| 1064 | { | ||
| 1065 | if ( INTEGER_SIGN ( op1 ) == 0 ) { | ||
| 1066 | if ( INTEGER_SIGN ( op2 ) == 0 ) | ||
| 1067 | return ammunition_bytes_comparison ( op1, op2, size ) <= 0; | ||
| 1068 | else | ||
| 1069 | return 0; /*FALSE*/ | ||
| 1070 | } else | ||
| 1071 | if ( INTEGER_SIGN ( op2 ) == 0 ) | ||
| 1072 | return 1; /* TRUE */ | ||
| 1073 | else | ||
| 1074 | return ammunition_bytes_comparison ( op1, op2, size ) <= 0; | ||
| 1075 | } | ||
| 1076 | |||
| 1077 | |||
| 1078 | |||
| 1079 | /* This page contains functions for changing size of arbitrary | ||
| 1080 | precision numbers. */ | ||
| 1081 | |||
| 1082 | /* The function changes size of unsigned integer. The function fixes | ||
| 1083 | overflow when result can not be represented by number of given | ||
| 1084 | size. Result can be placed in operand. */ | ||
| 1085 | |||
| 1086 | void | ||
| 1087 | ammunition_change_unsigned_integer_size ( int operand_size, const void *operand, | ||
| 1088 | int result_size, void *result ) | ||
| 1089 | { | ||
| 1090 | int operand_digit_number; | ||
| 1091 | |||
| 1092 | ammunition_overflow_bit = 0; | ||
| 1093 | if ( operand_size <= result_size ) { | ||
| 1094 | ammunition_memmove ( ( char * ) result + result_size - operand_size, | ||
| 1095 | operand, ( size_x ) operand_size ); | ||
| 1096 | ammunition_memset ( result, 0, ( size_x ) ( result_size - operand_size ) ); | ||
| 1097 | } else { | ||
| 1098 | _Pragma( "loopbound min 2 max 2" ) | ||
| 1099 | for ( operand_digit_number = 0; | ||
| 1100 | operand_digit_number < operand_size - result_size; | ||
| 1101 | operand_digit_number++ ) { | ||
| 1102 | if ( ( ( unsigned char * ) operand ) [operand_digit_number] != 0 ) { | ||
| 1103 | ammunition_overflow_bit = 1; | ||
| 1104 | break; | ||
| 1105 | } | ||
| 1106 | } | ||
| 1107 | ammunition_memmove ( result, | ||
| 1108 | ( char * ) operand + operand_size - result_size, | ||
| 1109 | ( size_x ) result_size ); | ||
| 1110 | } | ||
| 1111 | if ( ammunition_overflow_bit ) | ||
| 1112 | ammunition_arithmetic_unsigned_overflow_reaction(); | ||
| 1113 | } | ||
| 1114 | |||
| 1115 | /* The function changes size of integer. The function fixes overflow | ||
| 1116 | when result can not be represented by number of given size. Result | ||
| 1117 | can be placed in operand. */ | ||
| 1118 | |||
| 1119 | void | ||
| 1120 | ammunition_change_integer_size ( int operand_size, const void *operand, | ||
| 1121 | int result_size, void *result ) | ||
| 1122 | { | ||
| 1123 | int operand_digit_number; | ||
| 1124 | int operand_sign; | ||
| 1125 | |||
| 1126 | ammunition_overflow_bit = 0; | ||
| 1127 | operand_sign = INTEGER_SIGN ( operand ); | ||
| 1128 | if ( operand_size <= result_size ) { | ||
| 1129 | ammunition_memmove ( ( char * ) result + result_size - operand_size, | ||
| 1130 | operand, ( size_x ) operand_size ); | ||
| 1131 | ammunition_memset ( result, ( operand_sign ? UCHAR_MAX : 0 ), | ||
| 1132 | ( size_x ) ( result_size - operand_size ) ); | ||
| 1133 | } else { | ||
| 1134 | _Pragma( "loopbound min 2 max 2" ) | ||
| 1135 | for ( operand_digit_number = 0; | ||
| 1136 | operand_digit_number < operand_size - result_size; | ||
| 1137 | operand_digit_number++ ) { | ||
| 1138 | if ( ( ( unsigned char * ) operand ) [operand_digit_number] | ||
| 1139 | != ( operand_sign ? UCHAR_MAX : 0 ) ) { | ||
| 1140 | ammunition_overflow_bit = 1; | ||
| 1141 | break; | ||
| 1142 | } | ||
| 1143 | } | ||
| 1144 | ammunition_memmove ( result, | ||
| 1145 | ( char * ) operand + operand_size - result_size, | ||
| 1146 | ( size_x ) result_size ); | ||
| 1147 | if ( operand_sign != INTEGER_SIGN ( result ) ) | ||
| 1148 | ammunition_overflow_bit = 1; | ||
| 1149 | } | ||
| 1150 | if ( ammunition_overflow_bit ) | ||
| 1151 | ammunition_arithmetic_overflow_reaction(); | ||
| 1152 | } | ||
| 1153 | |||
| 1154 | |||
| 1155 | |||
| 1156 | /* This page contains functions for conversion of arbitrary precision | ||
| 1157 | numbers to ascii representation. */ | ||
| 1158 | |||
| 1159 | /* This function transforms unsigned integer of given size to BASE | ||
| 1160 | ascii representation. BASE should be between 2 and 36 including | ||
| 1161 | them. Digits more 9 are represented by 'a', 'b' etc. Sign is | ||
| 1162 | absent in result string. The function returns the result | ||
| 1163 | string. */ | ||
| 1164 | |||
| 1165 | char * | ||
| 1166 | ammunition_unsigned_integer_to_based_string ( int size, const void *operand, | ||
| 1167 | int base, | ||
| 1168 | char *result ) | ||
| 1169 | { | ||
| 1170 | int digit_num; | ||
| 1171 | int i; | ||
| 1172 | unsigned long divisable; | ||
| 1173 | unsigned long remainder; | ||
| 1174 | int nonzero_flag; | ||
| 1175 | int length; | ||
| 1176 | int temporary; | ||
| 1177 | unsigned char operand_copy [MAX_INTEGER_OPERAND_SIZE]; | ||
| 1178 | |||
| 1179 | ammunition_memcpy ( operand_copy, operand, ( size_x ) size ); | ||
| 1180 | length = 0; | ||
| 1181 | _Pragma( "loopbound min 1 max 10" ) | ||
| 1182 | do { | ||
| 1183 | nonzero_flag = 0 /* FALSE */; | ||
| 1184 | _Pragma( "loopbound min 2 max 6" ) | ||
| 1185 | for ( digit_num = 0, remainder = 0; digit_num < size; digit_num++ ) { | ||
| 1186 | divisable = remainder * ( UCHAR_MAX + 1 ) + operand_copy [digit_num]; | ||
| 1187 | remainder = divisable % base; | ||
| 1188 | operand_copy [digit_num] = ( unsigned char ) ( divisable / base ); | ||
| 1189 | if ( operand_copy [digit_num] != 0 ) | ||
| 1190 | nonzero_flag = 1 /* TRUE */; | ||
| 1191 | } | ||
| 1192 | result [length++] = ( unsigned char ) ( remainder < 10 ? '0' + remainder | ||
| 1193 | : 'a' + remainder - 10 ); | ||
| 1194 | } while ( nonzero_flag ); | ||
| 1195 | result [length] = '\0'; | ||
| 1196 | _Pragma( "loopbound min 0 max 5" ) | ||
| 1197 | for ( i = 0; i < length / 2; i++ ) { | ||
| 1198 | temporary = result [i]; | ||
| 1199 | result [i] = result [length - i - 1]; | ||
| 1200 | result [length - i - 1] = temporary; | ||
| 1201 | } | ||
| 1202 | return result; | ||
| 1203 | } | ||
| 1204 | |||
| 1205 | |||
| 1206 | /* This function transforms unsigned integer of given size to decimal | ||
| 1207 | ascii representation. Sign is absent in result string. The | ||
| 1208 | function returns the result string. */ | ||
| 1209 | |||
| 1210 | char * | ||
| 1211 | ammunition_unsigned_integer_to_string ( int size, const void *operand, | ||
| 1212 | char *result ) | ||
| 1213 | { | ||
| 1214 | return ammunition_unsigned_integer_to_based_string ( size, operand, 10, | ||
| 1215 | result ); | ||
| 1216 | } | ||
| 1217 | |||
| 1218 | |||
| 1219 | /* This function transforms integer of given size to BASE ascii | ||
| 1220 | representation. BASE should be between 2 and 36 including them. | ||
| 1221 | Digits more 9 are represented by 'a', 'b' etc. Sign is present in | ||
| 1222 | result string only for negative numbers. The function returns the | ||
| 1223 | result string. */ | ||
| 1224 | |||
| 1225 | char * | ||
| 1226 | ammunition_integer_to_based_string ( int size, const void *operand, int base, | ||
| 1227 | char *result ) | ||
| 1228 | { | ||
| 1229 | unsigned char operand_copy [MAX_INTEGER_OPERAND_SIZE]; | ||
| 1230 | |||
| 1231 | if ( !INTEGER_SIGN ( operand ) ) | ||
| 1232 | return ammunition_unsigned_integer_to_based_string ( size, operand, base, | ||
| 1233 | result ); | ||
| 1234 | ammunition_memcpy ( operand_copy, operand, ( size_x ) size ); | ||
| 1235 | /* May be integer overflow. But result is correct because it is unsigned. */ | ||
| 1236 | ammunition_make_complementary_code ( size, operand_copy, operand_copy ); | ||
| 1237 | *result = '-'; | ||
| 1238 | ammunition_unsigned_integer_to_based_string ( size, operand_copy, base, | ||
| 1239 | result + 1 ); | ||
| 1240 | return result; | ||
| 1241 | } | ||
| 1242 | |||
| 1243 | |||
| 1244 | |||
| 1245 | /* This function transforms integer of given size to decimal ascii | ||
| 1246 | representation. Sign is present in result string only for negative | ||
| 1247 | numbers. The function returns the result string. */ | ||
| 1248 | |||
| 1249 | char * | ||
| 1250 | ammunition_integer_to_string ( int size, const void *operand, char *result ) | ||
| 1251 | { | ||
| 1252 | return ammunition_integer_to_based_string ( size, operand, 10, result ); | ||
| 1253 | } | ||
| 1254 | |||
| 1255 | /* This page contains functions for conversion of decimal ascii | ||
| 1256 | representation to arbitrary precision numbers. */ | ||
| 1257 | |||
| 1258 | /* The function adds digit (byte size) to unsigned integer. The | ||
| 1259 | function returns 1 if unsigned integer overflow is fixed, 0 | ||
| 1260 | otherwise. */ | ||
| 1261 | |||
| 1262 | int ammunition_add_digit_to_unsigned_integer_without_overflow_reaction | ||
| 1263 | ( int size, void *operand, unsigned int digit ) | ||
| 1264 | { | ||
| 1265 | int digit_num; | ||
| 1266 | unsigned int carry; | ||
| 1267 | unsigned int sum; | ||
| 1268 | |||
| 1269 | _Pragma( "loopbound min 4 max 4" ) | ||
| 1270 | for ( digit_num = size - 1, carry = digit; digit_num >= 0; | ||
| 1271 | digit_num-- ) { | ||
| 1272 | sum = ( ( unsigned char * ) operand ) [digit_num] + carry; | ||
| 1273 | if ( sum > UCHAR_MAX ) { | ||
| 1274 | carry = sum / ( UCHAR_MAX + 1 ); | ||
| 1275 | sum %= UCHAR_MAX + 1; | ||
| 1276 | } else | ||
| 1277 | carry = 0; | ||
| 1278 | ( ( unsigned char * ) operand ) [digit_num] = sum; | ||
| 1279 | } | ||
| 1280 | return carry != 0; | ||
| 1281 | } | ||
| 1282 | |||
| 1283 | /* This function transforms source string (decimal ascii | ||
| 1284 | representation without sign) to given size unsigned integer and | ||
| 1285 | returns pointer to first non digit in the source string through a | ||
| 1286 | parameter. If the string started with invalid integer | ||
| 1287 | representation the result will be zero and returns the operand | ||
| 1288 | through the parameter. The function returns 1 if unsigned integer | ||
| 1289 | overflow is fixed, 0 otherwise. */ | ||
| 1290 | |||
| 1291 | int ammunition_string_to_unsigned_integer_without_overflow_reaction | ||
| 1292 | ( int size, const char *operand, void *result, char **first_nondigit ) | ||
| 1293 | { | ||
| 1294 | int overflow_flag; | ||
| 1295 | |||
| 1296 | ammunition_memset ( result, 0, ( size_x ) size ); | ||
| 1297 | _Pragma( "loopbound min 0 max 10" ) | ||
| 1298 | for ( overflow_flag = 0; ammunition_isdigit ( *operand ); operand++ ) { | ||
| 1299 | overflow_flag | ||
| 1300 | = overflow_flag || | ||
| 1301 | ammunition_multiply_unsigned_integer_by_digit_without_overflow_reaction | ||
| 1302 | ( size, result, 10 ); | ||
| 1303 | overflow_flag | ||
| 1304 | = overflow_flag | ||
| 1305 | || ammunition_add_digit_to_unsigned_integer_without_overflow_reaction | ||
| 1306 | ( size, result, *operand - '0' ); | ||
| 1307 | } | ||
| 1308 | *first_nondigit = ( char * ) operand; | ||
| 1309 | return overflow_flag; | ||
| 1310 | } | ||
| 1311 | |||
| 1312 | /* This function skips all white spaces at the begin of source string | ||
| 1313 | and transforms tail of the source string (decimal ascii | ||
| 1314 | representation without sign) to given size unsigned integer with | ||
| 1315 | the aid of function | ||
| 1316 | `string_to_unsigned_integer_without_overflow_reaction'. If the | ||
| 1317 | string started with invalid unsigned integer representation the | ||
| 1318 | result will be zero. The function fixes overflow when result can | ||
| 1319 | not be represented by number of given size. The function returns | ||
| 1320 | address of the first nondigit in the source string. */ | ||
| 1321 | |||
| 1322 | char * | ||
| 1323 | ammunition_unsigned_integer_from_string ( int size, const char *operand, | ||
| 1324 | void *result ) | ||
| 1325 | { | ||
| 1326 | char *first_nondigit; | ||
| 1327 | |||
| 1328 | _Pragma( "loopbound min 0 max 0" ) | ||
| 1329 | while ( ammunition_isspace ( *operand ) ) | ||
| 1330 | operand++; | ||
| 1331 | ammunition_overflow_bit | ||
| 1332 | = ammunition_string_to_unsigned_integer_without_overflow_reaction | ||
| 1333 | ( size, operand, result, &first_nondigit ); | ||
| 1334 | if ( ammunition_overflow_bit ) | ||
| 1335 | ammunition_arithmetic_unsigned_overflow_reaction(); | ||
| 1336 | return first_nondigit; | ||
| 1337 | } | ||
| 1338 | |||
| 1339 | /* This function skips all white spaces at the begin of source string | ||
| 1340 | and transforms tail of the source string (decimal ascii | ||
| 1341 | representation with possible sign `+' or `-') to given size integer | ||
| 1342 | with the aid of function | ||
| 1343 | `string_to_unsigned_integer_without_overflow_reaction'. If the | ||
| 1344 | string started with invalid integer representation the result will | ||
| 1345 | be zero. The function fixes overflow when result can not be | ||
| 1346 | represented by number of given size. the function returns Address | ||
| 1347 | of the first nondigit in the source string. */ | ||
| 1348 | |||
| 1349 | char * | ||
| 1350 | ammunition_integer_from_string ( int size, const char *operand, void *result ) | ||
| 1351 | { | ||
| 1352 | int negative_number_flag; | ||
| 1353 | char *first_nondigit; | ||
| 1354 | int unsigned_result_sign; | ||
| 1355 | |||
| 1356 | _Pragma( "loopbound min 0 max 0" ) | ||
| 1357 | while ( ammunition_isspace ( *operand ) ) | ||
| 1358 | operand++; | ||
| 1359 | negative_number_flag = 0; /* FALSE */ | ||
| 1360 | if ( *operand == '+' ) | ||
| 1361 | operand++; | ||
| 1362 | else | ||
| 1363 | if ( *operand == '-' ) { | ||
| 1364 | operand++; | ||
| 1365 | negative_number_flag = 1; /* TRUE */ | ||
| 1366 | } | ||
| 1367 | ammunition_overflow_bit | ||
| 1368 | = ammunition_string_to_unsigned_integer_without_overflow_reaction | ||
| 1369 | ( size, operand, result, &first_nondigit ); | ||
| 1370 | unsigned_result_sign = INTEGER_SIGN ( result ); | ||
| 1371 | if ( negative_number_flag ) | ||
| 1372 | /* May be integer overflow when `result' is correct. But result | ||
| 1373 | is correct because it is unsigned. */ | ||
| 1374 | ammunition_make_complementary_code ( size, result, result ); | ||
| 1375 | ammunition_overflow_bit | ||
| 1376 | = ammunition_overflow_bit | ||
| 1377 | || ( unsigned_result_sign | ||
| 1378 | && ( !negative_number_flag | ||
| 1379 | || INTEGER_SIGN ( result ) != unsigned_result_sign ) ); | ||
| 1380 | if ( ammunition_overflow_bit ) | ||
| 1381 | ammunition_arithmetic_unsigned_overflow_reaction(); | ||
| 1382 | return first_nondigit; | ||
| 1383 | } | ||
| 1384 | |||
