summaryrefslogtreecommitdiffstats
path: root/baseline/source/ammunition/arithm.c
blob: 94808464c52cee5bd01ff95aa57a30cc09e137f0 (plain) (blame)
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
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
/*
  FILE NAME:   arithm.c

  TITLE:       Package for arbitrary precision integer arithmetic

  DESCRIPTION: This abstract data implements arbitrary precision
      integer and unsigned integer numbers by machine independent
      way.  The implementation of the package functions are not
      sufficiently efficient in order to use for run-time.  The
      package functions are oriented to implement constant-folding in
      compilers.  This package is necessary because host machine may
      not support such arithmetic for target machine.  For example,
      VAX does not support does not support more 32-bits integer
      numbers arithmetic.  The numbers are represented by bytes in
      big endian mode, negative integer numbers are represented in
      complementary code.  All sizes are given in bytes and must be
      positive.  Results of executions of all functions can coincide
      with a operand(s).  All functions of addition, subtraction,
      multiplication, division, evaluation of remainder, shift,
      changing size and transformation of string into number fix
      overflow.  The overflow is fixed when result can not be
      represented by number of given size.

*/

#include "arithm.h"
#include "ammunition_string.h"


/* This variable can have only two values 0 or 1.  The value `1'
   corresponds to overflow.  The variable value are modified by all
   functions of addition, subtract, multiplication, division,
   evaluation of remainder, shift, changing size and transformation of
   string into number fix overflow. */

int ammunition_overflow_bit;


/* The following function adds unsigned integers.  The function
   returns 1 if unsigned integer overflow is fixed, 0 otherwise.
   Result can be placed in any operand. */

int ammunition_add_unsigned_integer_without_overflow_reaction
( int size, const void *op1, const void *op2, void *result )
{
  int digit_num;
  int carry;
  unsigned int sum;

  _Pragma( "loopbound min 4 max 4" )
  for ( digit_num = size - 1, carry = 0; digit_num >= 0; digit_num-- ) {
    sum = ( ( ( unsigned char * ) op1 ) [digit_num]
            + ( ( unsigned char * ) op2 ) [digit_num] + carry );
    if ( sum > UCHAR_MAX ) {
      sum -= UCHAR_MAX + 1;
      carry = 1;
    } else
      carry = 0;
    ( ( unsigned char * ) result ) [digit_num] = sum;
  }
  return carry != 0;
}

/* The following function adds unsigned integers.  The function
   returns 1 if unsigned integer overflow (the first operand is less
   than the second) is fixed, 0 otherwise.  Result can be placed in
   any operand. */

int ammunition_subtract_unsigned_integer_without_overflow_reaction
( int size, const void *op1, const void *op2, void *result )
{
  int digit_num;
  int carry;
  int subtraction;

  _Pragma( "loopbound min 4 max 4" )
  for ( digit_num = size - 1, carry = 0; digit_num >= 0; digit_num-- ) {
    subtraction = ( ( ( unsigned char * ) op1 ) [digit_num]
                    - ( ( unsigned char * ) op2 ) [digit_num] - carry );
    if ( subtraction < 0 ) {
      subtraction += UCHAR_MAX + 1;
      carry = 1;
    } else
      carry = 0;
    ( ( unsigned char * ) result ) [digit_num] = subtraction;
  }
  return carry != 0;
}

/* The following function makes complementary code of number.  Result
   can be placed in operand. */

void ammunition_make_complementary_code
( int size, const void *operand, void *result )
{
  int digit_num;
  int carry;
  int subtraction;

  _Pragma( "loopbound min 2 max 6" )
  for ( digit_num = size - 1, carry = 0; digit_num >= 0; digit_num-- ) {
    subtraction = ( 0 - ( ( unsigned char * ) operand ) [digit_num] - carry );
    if ( subtraction != 0 ) {
      subtraction += UCHAR_MAX + 1;
      carry = 1;
    } else
      carry = 0;
    ( ( unsigned char * ) result ) [digit_num] = subtraction;
  }
}

/* The following function multiplys unsigned integer by digit (byte
   size).  The function returns 1 if unsigned integer overflow is
   fixed, 0 otherwise. */

int ammunition_multiply_unsigned_integer_by_digit_without_overflow_reaction
( int size, void *operand, unsigned int digit )
{
  int digit_num;
  unsigned int carry;
  unsigned int sum;

  _Pragma( "loopbound min 4 max 4" )
  for ( digit_num = size - 1, carry = 0; digit_num >= 0; digit_num-- ) {
    sum = ( ( ( unsigned char * ) operand ) [digit_num] * digit + carry );
    if ( sum > UCHAR_MAX ) {
      carry = sum / ( UCHAR_MAX + 1 );
      sum %= UCHAR_MAX + 1;
    } else
      carry = 0;
    ( ( unsigned char * ) operand ) [digit_num] = sum;
  }
  return carry != 0;
}


/* Originally reaction on all integer and unsigned integer overflow is
   equal to the following function.  The function does nothing. */

void
ammunition_arithmetic_overflow_reaction ( void )
{}


/* Originally reaction on all integer and unsigned integer overflow is
   equal to the following function.  The function does nothing. */

void
ammunition_arithmetic_unsigned_overflow_reaction ( void )
{}


/* This page contains functions for arbitrary precision addition. */

/* The function adds unsigned integers and fixes overflow reaction if
   it is needed.  The function makes this with the aid of function
   `add_unsigned_integer_without_overflow_reaction'.  Result can be
   placed in any operand. */

void
ammunition_add_unsigned_integer ( int size, const void *op1, const void *op2,
                                  void *result )
{
  ammunition_overflow_bit
    = ammunition_add_unsigned_integer_without_overflow_reaction (
        size,  op1, op2, result );
  if ( ammunition_overflow_bit != 0 )
    ammunition_arithmetic_unsigned_overflow_reaction();
}

/* The function adds integers and fixes overflow reaction if it is
   needed.  The function makes this with the aid of function
   `add_unsigned_integer_without_overflow_reaction'.  Result can be
   placed in any operand. */

void
ammunition_add_integer ( int size, const void *op1, const void *op2,
                         void *result )
{
  int op1_sign;
  int sign_equality;

  op1_sign = INTEGER_SIGN ( op1 );
  sign_equality = INTEGER_SIGN ( op1 ) == INTEGER_SIGN ( op2 );
  ammunition_add_unsigned_integer_without_overflow_reaction (
        size, op1, op2, result );
  ammunition_overflow_bit = sign_equality &&
                            ( op1_sign != INTEGER_SIGN ( result ) );
  if ( ammunition_overflow_bit != 0 )
    ammunition_arithmetic_overflow_reaction();
}



/* This page contains functions for arbitrary precision subtraction. */

/* The function subtracts unsigned integers and fixes overflow
   reaction if it is needed.  The function makes this with the aid of
   function `subtract_unsigned_integer_without_overflow_reaction'.
   Result can be placed in any operand. */

void
ammunition_subtract_unsigned_integer ( int size, const void *op1,
                                       const void *op2,
                                       void *result )
{
  ammunition_overflow_bit
    = ammunition_subtract_unsigned_integer_without_overflow_reaction (
        size, op1, op2, result );
  if ( ammunition_overflow_bit != 0 )
    ammunition_arithmetic_unsigned_overflow_reaction();
}

/* The function subtracts integers and fixes overflow reaction if it
   is needed.  The function makes this with the aid of function
   `subtract_unsigned_integer_without_overflow_reaction'.  Result can
   be placed in any operand. */

void
ammunition_subtract_integer ( int size, const void *op1, const void *op2,
                              void *result )
{
  int op1_sign;
  int sign_unequality;

  op1_sign = INTEGER_SIGN ( op1 );
  sign_unequality = INTEGER_SIGN ( op1 ) != INTEGER_SIGN ( op2 );
  ammunition_subtract_unsigned_integer_without_overflow_reaction (
        size, op1, op2, result );
  ammunition_overflow_bit = sign_unequality &&
                            ( op1_sign != INTEGER_SIGN ( result ) );
  if ( ammunition_overflow_bit != 0 )
    ammunition_arithmetic_overflow_reaction();
}



/* This page contains functions for arbitrary precision multiplication. */

/* The following function multiplys unsigned integers.  The function
   returns 1 if unsigned integer overflow is fixed, 0 otherwise.
   Result can be placed in any operand. */

int ammunition_multiply_unsigned_integer_without_overflow_reaction
( int size, const void *op1, const void *op2, void *result )
{
  int op1_digit_num;
  int op2_digit_num;
  int carry;
  unsigned long int partial_sum;
  int result_digit_number;
  int overflow_flag;
  unsigned char long_result [2 * MAX_INTEGER_OPERAND_SIZE];

  ammunition_memset ( long_result + size, 0, ( size_x ) size );
  _Pragma( "loopbound min 4 max 4" )
  for ( op2_digit_num = size - 1; op2_digit_num >= 0; op2_digit_num-- ) {
    if ( ( ( unsigned char * ) op2 ) [op2_digit_num] != 0 ) {
      _Pragma( "loopbound min 4 max 4" )
      for ( op1_digit_num = size - 1, carry = 0; op1_digit_num >= 0;
            op1_digit_num-- ) {
        partial_sum
          = ( ( ( unsigned char * ) op1 ) [op1_digit_num]
              * ( ( unsigned char * ) op2 ) [op2_digit_num]
              + long_result [op1_digit_num + op2_digit_num + 1]
              + carry );
        long_result [op1_digit_num + op2_digit_num + 1]
          = ( unsigned char ) ( partial_sum % ( UCHAR_MAX + 1 ) );
        carry = partial_sum / ( UCHAR_MAX + 1 );
      }
      long_result [op2_digit_num] = carry;
    } else
      long_result [op2_digit_num] = 0;
  }
  overflow_flag = 0;
  _Pragma( "loopbound min 1 max 4" )
  for ( result_digit_number = size - 1; result_digit_number >= 0;
        result_digit_number-- ) {
    if ( long_result [result_digit_number] != 0 ) {
      overflow_flag = 1;
      break;
    }
  }
  ammunition_memcpy ( result, long_result + size, ( size_x ) size );
  return overflow_flag;
}

/* The following function multiplys unsigned integers and fixes
   overflow reaction if it is needed.  The function makes this with
   the aid of function
   `multiply_unsigned_integer_without_overflow_reaction'.  Result can
   be placed in any operand. */

void
ammunition_multiply_unsigned_integer ( int size, const void *op1,
                                       const void *op2,
                                       void *result )
{
  ammunition_overflow_bit =
    ammunition_multiply_unsigned_integer_without_overflow_reaction ( 
        size, op1, op2, result );
  if ( ammunition_overflow_bit )
    ammunition_arithmetic_unsigned_overflow_reaction();
}

/* The function multiplys integers and fixes overflow reaction if it
   is needed.  The function makes this with the aid of function
   `multiply_unsigned_integer_without_overflow_reaction'.  Result can
   be placed in any operand. */

void
ammunition_multiply_integer ( int size, const void *op1, const void *op2,
                              void *result )
{
  int negative_result_flag;
  unsigned char op1_complementary [MAX_INTEGER_OPERAND_SIZE];
  unsigned char op2_complementary [MAX_INTEGER_OPERAND_SIZE];
  unsigned const char *abs_op1;
  unsigned const char *abs_op2;
  int unsigned_result_sign;

  negative_result_flag = INTEGER_SIGN ( op1 ) != INTEGER_SIGN ( op2 );
  if ( INTEGER_SIGN ( op1 ) ) {
    /* May be integer overflow. But result is correct because
       it is unsigned. */
    ammunition_make_complementary_code ( size, op1, op1_complementary );
    abs_op1 = ( unsigned const char * )op1_complementary;
  } else
    abs_op1 = ( unsigned const char * )op1;
  if ( INTEGER_SIGN ( op2 ) ) {
    /* May be integer overflow.  But result is correct because
       it is unsigned. */
    ammunition_make_complementary_code ( size, op2, op2_complementary );
    abs_op2 = ( unsigned const char * )op2_complementary;
  } else
    abs_op2 = ( unsigned const char * )op2;
  ammunition_overflow_bit =
    ammunition_multiply_unsigned_integer_without_overflow_reaction ( 
        size, abs_op1, abs_op2, result );
  unsigned_result_sign = INTEGER_SIGN ( result );
  if ( negative_result_flag )
    ammunition_make_complementary_code ( size, result, result );
  if ( unsigned_result_sign
       && ( !negative_result_flag
            || INTEGER_SIGN ( result ) != unsigned_result_sign ) )
    /* Unsigned result can not be represented as integer. */
    ammunition_overflow_bit = 1;
  if ( ammunition_overflow_bit )
    ammunition_arithmetic_overflow_reaction();
}



/* This page contains functions for arbitrary precision division. */

/* The following function divides unsigned integers.  The function
   returns 1 if unsigned integer overflow (division by zero) is fixed,
   0 otherwise.  Result can be placed in any operand.  See algorithm
   in Knuth's book. */

int ammunition_divide_unsigned_integer_without_overflow_reaction
( int size, const void *op1, const void *op2, void *result )
{
  int scaled_op1_digit_num;
  unsigned int q_approximation;
  int first_nonzero_digit_number;
  int op2_digit_number;
  unsigned int scale;
  unsigned char scaled_op1 [MAX_INTEGER_OPERAND_SIZE + 1];
  unsigned char normalized_op2 [MAX_INTEGER_OPERAND_SIZE];
  unsigned char extended_normalized_op2 [MAX_INTEGER_OPERAND_SIZE + 1];

  _Pragma( "loopbound min 4 max 4" )
  for ( op2_digit_number = 0; op2_digit_number < size; op2_digit_number++ ) {
    if ( ( ( unsigned char * ) op2 ) [op2_digit_number] != 0 )
      break;
  }
  first_nonzero_digit_number = op2_digit_number;
  if ( first_nonzero_digit_number == size ) {
    /* Zero divisor */
    ammunition_memset ( result, 0, ( size_x ) size );
    return 1 /* TRUE */;
  } else
    if ( first_nonzero_digit_number == size - 1 ) {
      /* Division by digit. */
      int digit_num;
      int digit;
      unsigned long divisable;
      unsigned long remainder;

      digit = ( ( unsigned char * ) op2 ) [first_nonzero_digit_number];
      ammunition_memcpy ( result, op1, ( size_x ) size );
      remainder = 0;
      _Pragma( "loopbound min 4 max 4" )
      for ( digit_num = 0; digit_num < size; digit_num++ ) {
        divisable = ( remainder * ( UCHAR_MAX + 1 )
                      + ( ( unsigned char * ) result ) [digit_num] );
        remainder = divisable % digit;
        ( ( unsigned char * ) result ) [digit_num]
          = ( unsigned char ) ( divisable / digit );
      }
      return 0 /* FALSE */;
    }
  /* Normalization of divisor. */
  scale = ( UCHAR_MAX + 1 ) / ( ( ( unsigned char * ) op2 ) [op2_digit_number] +
                                1 );
  ammunition_memcpy ( scaled_op1 + 1, op1, ( size_x ) size );
  *scaled_op1 = 0;

  ammunition_multiply_unsigned_integer_by_digit_without_overflow_reaction
  ( size + 1, scaled_op1, scale );

  ammunition_memcpy ( normalized_op2, op2, ( size_x ) size );

  ammunition_multiply_unsigned_integer_by_digit_without_overflow_reaction
  ( size, normalized_op2, scale );

  _Pragma( "loopbound min 0 max 0" )
  for ( scaled_op1_digit_num = 0;
        scaled_op1_digit_num <= first_nonzero_digit_number;
        scaled_op1_digit_num++ ) {
    /* Division of `scaled_op1[scaled_op1_digit_number]..scaled_op1[size]' by
       `normalized_op2[first_nonzero_digit_number]..normalized_op2[size-1]'
       for evaluation of one digit of quotient
       `result[size-1-first_nonzero_digit_number-scaled_op1_digit_number]'.
    */
    if ( scaled_op1 [scaled_op1_digit_num]
         == normalized_op2 [first_nonzero_digit_number] )
      q_approximation = UCHAR_MAX;
    else
      q_approximation
        = ( scaled_op1 [scaled_op1_digit_num] * ( UCHAR_MAX + 1 )
            + scaled_op1 [scaled_op1_digit_num + 1] )
          / normalized_op2 [first_nonzero_digit_number];

    _Pragma( "loopbound min 0 max 0" )
    while ( normalized_op2 [first_nonzero_digit_number + 1] * q_approximation
            > ( ( ( unsigned long int ) scaled_op1 [scaled_op1_digit_num]
                  * ( UCHAR_MAX + 1 )
                  + scaled_op1 [scaled_op1_digit_num + 1]
                  - q_approximation
                  * normalized_op2 [first_nonzero_digit_number] )
                * ( UCHAR_MAX + 1 ) + scaled_op1 [scaled_op1_digit_num + 2] ) )
      q_approximation --;

    /* Multiply and subtract */
    ammunition_memcpy ( extended_normalized_op2 + 1,
                        normalized_op2 + first_nonzero_digit_number,
                        ( size_x ) ( size - first_nonzero_digit_number ) );
    *extended_normalized_op2 = 0;
    ammunition_multiply_unsigned_integer_by_digit_without_overflow_reaction
    ( size - first_nonzero_digit_number + 1, extended_normalized_op2,
      q_approximation );
    if ( ammunition_subtract_unsigned_integer_without_overflow_reaction
         ( size - first_nonzero_digit_number + 1,
           scaled_op1 + scaled_op1_digit_num, extended_normalized_op2,
           scaled_op1 + scaled_op1_digit_num ) ) {
      /* Negative result.  Compensation by addition. */
      q_approximation--;
      ammunition_memcpy ( extended_normalized_op2 + 1,
                          normalized_op2 + first_nonzero_digit_number,
                          ( size_x ) ( size - first_nonzero_digit_number ) );
      *extended_normalized_op2 = 0;

      ammunition_add_unsigned_integer_without_overflow_reaction
      ( size - first_nonzero_digit_number + 1,
        scaled_op1 + scaled_op1_digit_num, extended_normalized_op2,
        scaled_op1 + scaled_op1_digit_num );

    }
    ( ( unsigned char * ) result ) [size - 1 - first_nonzero_digit_number
                                    + scaled_op1_digit_num] = q_approximation;
  }
  ammunition_memset ( result, 0,
                      ( size_x ) ( size - 1 - first_nonzero_digit_number ) );
  return 0 /* TRUE */;
}

/* The function divides unsigned integers and fixes overflow reaction
   if it is needed.  The function makes this with the aid of function
   `divide_unsigned_integer_without_overflow_reaction'.  Result can be
   placed in any operand. */

void
ammunition_divide_unsigned_integer ( int size, const void *op1, const void *op2,
                                     void *result )
{
  ammunition_overflow_bit =
    ammunition_divide_unsigned_integer_without_overflow_reaction ( 
        size, op1, op2, result );
  if ( ammunition_overflow_bit )
    ammunition_arithmetic_unsigned_overflow_reaction();
}

/* The function divides integers and fixes overflow reaction if it is
   needed.  The function makes this with the aid of function
   `divide_unsigned_integer_without_overflow_reaction'.  Result can be
   placed in any operand. */

void
ammunition_divide_integer ( int size, const void *op1, const void *op2,
                            void *result )
{
  int negative_result_flag;
  unsigned char op1_complementary [MAX_INTEGER_OPERAND_SIZE];
  unsigned char op2_complementary [MAX_INTEGER_OPERAND_SIZE];
  unsigned const char *abs_op1;
  unsigned const char *abs_op2;
  int unsigned_result_sign;

  negative_result_flag = INTEGER_SIGN ( op1 ) != INTEGER_SIGN ( op2 );
  if ( INTEGER_SIGN ( op1 ) ) {
    /* May be integer overflow for minimal int. But result is correct because
       it is unsigned. */
    ammunition_make_complementary_code ( size, op1, op1_complementary );
    abs_op1 = ( unsigned const char * )op1_complementary;
  } else
    abs_op1 = ( unsigned const char * )op1;
  if ( INTEGER_SIGN ( op2 ) ) {
    /* May be integer overflow for minimal int.  But result is correct
       because it is unsigned. */
    ammunition_make_complementary_code ( size, op2, op2_complementary );
    abs_op2 = ( unsigned const char * )op2_complementary;
  } else
    abs_op2 = ( unsigned const char * )op2;
  ammunition_overflow_bit =
    ammunition_divide_unsigned_integer_without_overflow_reaction ( 
        size, abs_op1, abs_op2, result );
  unsigned_result_sign = INTEGER_SIGN ( result );
  if ( negative_result_flag )
    ammunition_make_complementary_code ( size, result, result );
  if ( unsigned_result_sign
       && ( !negative_result_flag
            || INTEGER_SIGN ( result ) != unsigned_result_sign ) )
    /* Unsigned result can not be represented as integer. */
    ammunition_overflow_bit = 1;
  if ( ammunition_overflow_bit )
    ammunition_arithmetic_overflow_reaction();
}



/* This page contains functions for arbitrary precision evaluation of
   remainder. */

/* The function evaluates remainder of division of unsigned integers
   as `op1 - (op1/op2)*op2' and fixes overflow reaction if it is
   needed.  Result can be placed in any operand. */

void
ammunition_unsigned_integer_remainder ( int size, const void *op1,
                                        const void *op2,
                                        void *result )
{
  unsigned char temporary [MAX_INTEGER_OPERAND_SIZE];

  ammunition_divide_unsigned_integer ( size, op1, op2, temporary );
  if ( ammunition_overflow_bit )
    /* Reaction on zero is called from `divide_unsigned_integer'. */
    ammunition_memset ( result, 0, ( size_x ) size );
  else {
    ammunition_multiply_unsigned_integer ( size, temporary, op2, temporary );
    ammunition_subtract_unsigned_integer ( size, op1, temporary, result );
  }
}


/* This page contains functions for arbitrary precision number shifts. */

/* This function makes right shift of unsigned integer of given size
   on given number of bits.  If number of bits is negative the
   function makes shift to left actually with the aid of function
   `unsigned_integer_shift_left'.  The function fixes overflow when
   result can not be represented by number of given size, i.e. in
   other words the opposite unsigned shift (to left) results in number
   not equal to source operand.  Result can be placed in operand. */

void
ammunition_unsigned_integer_shift_right ( int size, const void *operand,
    int bits, void *result )
{
  int byte_number;
  unsigned byte;
  unsigned carry;
  int bit_shift;
  int byte_shift;


  if ( bits < 0 )
    ammunition_unsigned_integer_shift_left ( size, operand, -bits, result );
  else {
    ammunition_overflow_bit = 0;
    byte_shift = bits / CHAR_BIT;
    bit_shift = bits % CHAR_BIT;
    _Pragma( "loopbound min 0 max 3" )
    for ( byte_number = ( byte_shift >= size ? 0 : size - byte_shift );
          byte_number < size; byte_number++ )
      if ( ( ( unsigned char * ) operand ) [byte_number] != 0 ) {
        ammunition_overflow_bit = 1;
        break;
      }
    if ( byte_shift >= size )
      ammunition_memset ( result, 0, ( size_x ) size );
    else {
      ammunition_memmove ( ( char * ) result + byte_shift, operand,
                           ( size_x ) ( size - byte_shift ) );
      ammunition_memset ( result, 0, ( size_x ) byte_shift );
      if ( bit_shift == 0 )
        return;
      _Pragma( "loopbound min 3 max 3" )
      for ( byte_number = byte_shift, carry = 0; byte_number < size;
            byte_number++ ) {
        byte = ( ( unsigned char * ) result ) [byte_number];
        ( ( unsigned char * ) result ) [byte_number]
          = carry | ( byte >> bit_shift );
        carry = ( byte << ( CHAR_BIT - bit_shift ) ) & UCHAR_MAX;
      }
      if ( carry != 0 )
        ammunition_overflow_bit = 1;
    }
    if ( ammunition_overflow_bit )
      ammunition_arithmetic_unsigned_overflow_reaction();
  }
}

/* This function makes right arithmetic shift of integer of given size
   on given number of bits.  If number of bits is negative the
   function makes shift to left actually with the aid of function
   `integer_shift_left'.  The function fixes overflow when result can
   not be represented by number of given size, i.e. in other words the
   opposite shift (to left) results in number not equal to source
   operand.  Result can be placed in operand. */

void
ammunition_integer_shift_right ( int size, const void *operand, int bits,
                                 void *result )
{
  int byte_number;
  unsigned byte;
  unsigned carry;
  int bit_shift;
  int byte_shift;
  int operand_sign;

  if ( bits < 0 )
    ammunition_integer_shift_left ( size, operand, -bits, result );
  else {
    operand_sign = INTEGER_SIGN ( operand );
    ammunition_overflow_bit = 0;
    byte_shift = bits / CHAR_BIT;
    bit_shift = bits % CHAR_BIT;
    _Pragma( "loopbound min 0 max 3" )
    for ( byte_number = ( byte_shift >= size ? 0 : size - byte_shift );
          byte_number < size; byte_number++ )
      if ( ( ( unsigned char * ) operand ) [byte_number] != 0 ) {
        ammunition_overflow_bit = 1;
        break;
      }
    if ( byte_shift >= size )
      ammunition_memset ( result, 
                          ( operand_sign ? UCHAR_MAX : 0 ), ( size_x ) size );
    else {
      ammunition_memmove ( ( char * ) result + byte_shift, operand,
                           ( size_x ) ( size - byte_shift ) );
      ammunition_memset ( result, ( operand_sign ? UCHAR_MAX : 0 ),
                          ( size_x ) byte_shift );
      if ( bit_shift == 0 )
        return;
      carry = ( ( ( operand_sign ? UCHAR_MAX : 0 ) << ( CHAR_BIT - bit_shift ) )
                & UCHAR_MAX );
      _Pragma( "loopbound min 3 max 3" )
      for ( byte_number = byte_shift; byte_number < size; byte_number++ ) {
        byte = ( ( unsigned char * ) result ) [byte_number];
        ( ( unsigned char * ) result ) [byte_number]
          = carry | ( byte >> bit_shift );
        carry = ( byte << ( CHAR_BIT - bit_shift ) ) & UCHAR_MAX;
      }
      if ( carry != 0 )
        ammunition_overflow_bit = 1;
    }
    if ( ammunition_overflow_bit )
      ammunition_arithmetic_overflow_reaction();
  }
}

/* This function makes left shift of unsigned integer of given size on
   given number of bits.  If number of bits is negative the function
   makes shift to left actually with the aid of function
   `unsigned_integer_shift_right'.  The function fixes overflow when
   result can not be represented by number of given size, i.e. i.e. in
   other words the opposite shift (to right) results in number not
   equal to source operand.  Result can be placed in operand. */

void
ammunition_unsigned_integer_shift_left ( int size, const void *operand,
    int bits, void *result )
{
  int byte_number;
  unsigned byte;
  unsigned carry;
  int bit_shift;
  int byte_shift;

  if ( bits < 0 )
    ammunition_unsigned_integer_shift_right ( size, operand, -bits, result );
  else {
    ammunition_overflow_bit = 0;
    byte_shift = bits / CHAR_BIT;
    bit_shift = bits % CHAR_BIT;
    _Pragma( "loopbound min 0 max 2" )
    for ( byte_number = 0; byte_number < byte_shift && byte_number < size;
          byte_number++ )
      if ( ( ( unsigned char * ) operand ) [byte_number] != 0 ) {
        ammunition_overflow_bit = 1;
        break;
      }
    if ( byte_shift >= size )
      ammunition_memset ( result, 0, ( size_x ) size );
    else {
      ammunition_memmove ( result, ( char * ) operand + byte_shift,
                           ( size_x ) ( size - byte_shift ) );
      ammunition_memset ( ( char * ) result + ( size - byte_shift ), 0,
                          ( size_x ) byte_shift );
      if ( bit_shift == 0 )
        return;
      _Pragma( "loopbound min 2 max 3" )
      for ( byte_number = size - byte_shift - 1, carry = 0;
            byte_number >= 0; byte_number-- ) {
        byte = ( ( unsigned char * ) result ) [byte_number];
        ( ( unsigned char * ) result ) [byte_number]
          = carry | ( byte << bit_shift );
        carry = byte >> ( CHAR_BIT - bit_shift );
      }
      if ( carry != 0 )
        ammunition_overflow_bit = 1;
    }
    if ( ammunition_overflow_bit )
      ammunition_arithmetic_unsigned_overflow_reaction();
  }
}

/* This function makes left arithmetic shift of integer of given size
   on given number of bits.  If number of bits is negative the
   function makes shift to left actually with the aid of function
   `integer_shift_right'.  The function fixes overflow when result can
   not be represented by number of given size, i.e. in other words the
   opposite shift (to right) results in number not equal to source
   operand.  Result can be placed in operand. */

void
ammunition_integer_shift_left ( int size, const void *operand, int bits,
                                void *result )
{
  int byte_number;
  unsigned byte;
  unsigned carry;
  int bit_shift;
  int byte_shift;
  int operand_sign;

  if ( bits < 0 )
    ammunition_integer_shift_right ( size, operand, -bits, result );
  else {
    operand_sign = INTEGER_SIGN ( operand );
    ammunition_overflow_bit = 0;
    byte_shift = bits / CHAR_BIT;
    bit_shift = bits % CHAR_BIT;
    _Pragma( "loopbound min 0 max 2" )
    for ( byte_number = 0; byte_number < byte_shift && byte_number < size;
          byte_number++ )
      if ( ( ( unsigned char * ) operand ) [byte_number]
           != ( operand_sign ? UCHAR_MAX : 0 ) ) {
        ammunition_overflow_bit = 1;
        break;
      }
    if ( byte_shift >= size )
      ammunition_memset ( result, 0, ( size_x ) size );
    else {
      ammunition_memmove ( result, ( char * ) operand + byte_shift,
                           ( size_x ) ( size - byte_shift ) );
      ammunition_memset ( ( char * ) result + ( size - byte_shift ), 0,
                          ( size_x ) byte_shift );
      if ( bit_shift == 0 )
        return;
      _Pragma( "loopbound min 2 max 3" )
      for ( byte_number = size - byte_shift - 1, carry = 0;
            byte_number >= 0; byte_number-- ) {
        byte = ( ( unsigned char * ) result ) [byte_number];
        ( ( unsigned char * ) result ) [byte_number]
          = carry | ( byte << bit_shift );
        carry = byte >> ( CHAR_BIT - bit_shift );
      }
      if ( carry != ( ( unsigned ) ( operand_sign ? UCHAR_MAX : 0 )
                      >> ( CHAR_BIT - bit_shift ) ) )
        ammunition_overflow_bit = 1;
    }
    if ( operand_sign != INTEGER_SIGN ( result ) )
      ammunition_overflow_bit = 1;
    if ( ammunition_overflow_bit )
      ammunition_arithmetic_overflow_reaction();
  }
}



/* This page contains functions for bitwise operations of arbitrary
   precision numbers. */

/* This function makes bitwise `or' of two integers of given size. */

void
ammunition_integer_or ( int size, const void *op1, const void *op2,
                        void *result )
{
  int byte_number;

  _Pragma( "loopbound min 4 max 4" )
  for ( byte_number = 0; byte_number < size; byte_number++ ) {
    ( ( unsigned char * ) result ) [byte_number]
      = ( ( unsigned char * ) op1 ) [byte_number]
        | ( ( unsigned char * ) op2 ) [byte_number];
  }
}

/* This function makes bitwise `or' of two unsigned integers of given
   size. */

void
ammunition_unsigned_integer_or ( int size, const void *op1, const void *op2,
                                 void *result )
{
  ammunition_integer_or ( size, op1, op2, result );
}


/* This function makes bitwise `and' of two integers of given size. */

void
ammunition_integer_and ( int size, const void *op1, const void *op2,
                         void *result )
{
  int byte_number;

  _Pragma( "loopbound min 4 max 4" )
  for ( byte_number = 0; byte_number < size; byte_number++ ) {
    ( ( unsigned char * ) result ) [byte_number]
      = ( ( unsigned char * ) op1 ) [byte_number]
        & ( ( unsigned char * ) op2 ) [byte_number];
  }
}

/* This function makes bitwise `and' of two unsigned integers of given
   size. */

void
ammunition_unsigned_integer_and ( int size, const void *op1, const void *op2,
                                  void *result )
{
  ammunition_integer_and ( size, op1, op2, result );
}


/* This function makes bitwise `not' of integer of given size. */

void
ammunition_integer_not ( int size, const void *operand, void *result )
{
  int byte_number;

  _Pragma( "loopbound min 4 max 4" )
  for ( byte_number = 0; byte_number < size; byte_number++ ) {
    ( ( unsigned char * ) result ) [byte_number]
      = ( ( unsigned char * ) operand ) [byte_number] ^ UCHAR_MAX;
  }
}

/* This function makes bitwise `not' of unsigned integer of given
   size. */

void
ammunition_unsigned_integer_not ( int size, const void *operand, void *result )
{
  ammunition_integer_not ( size, operand, result );
}



/* This page contains functions for comparison of arbitrary precision
   numbers. */

/* This function compares two unsigned integers of given size on
   equality.  The function returns 1 if unsigned integers are equal, 0
   otherwise. */

int
ammunition_eq_unsigned_integer ( int size, const void *op1, const void *op2 )
{
  return ammunition_memcmp ( op1, op2, ( size_x ) size ) == 0;
}

/* This function compares two integers of given size on equality.  The
   function returns 1 if integers are equal, 0 otherwise. */

int
ammunition_eq_integer ( int size, const void *op1, const void *op2 )
{
  return ammunition_memcmp ( op1, op2, ( size_x ) size ) == 0;
}

/* This function compares two unsigned integers of given size on
   inequality.  The function returns 1 if unsigned integers are not
   equal, 0 otherwise. */

int
ammunition_ne_unsigned_integer ( int size, const void *op1, const void *op2 )
{
  return ammunition_memcmp ( op1, op2, ( size_x ) size ) != 0;
}

/* This function compares two integers of given size on inequality.
   The function returns 1 if integers are not equal, 0 otherwise. */

int
ammunition_ne_integer ( int size, const void *op1, const void *op2 )
{
  return ammunition_memcmp ( op1, op2, ( size_x ) size ) != 0;
}


/* This function compares two memory parts of given size on that the
   first operand is greater than the second.  The bytes are described
   as unsigned.  The function returns 1 if the first operand is
   greater than the second, - 1 if the first operand is less than the
   second, 0 otherwise. */

int ammunition_bytes_comparison ( const void *op1, const void *op2, int size )
{
  const unsigned char *str1 = ( unsigned const char * )op1;
  const unsigned char *str2 = ( unsigned const char * )op2;

  _Pragma( "loopbound min 1 max 4" )
  while ( size > 0 && *str1 == *str2 ) {
    str1++;
    str2++;
    size--;
  }
  if ( size <= 0 )
    return 0;
  else
    if ( *str1 > *str2 )
      return 1;
    else
      return -1;
}

/* This function compares two unsigned integers of given size on that
   the first operand is greater than the second.  The function returns
   1 if the first unsigned integer is greater than the second, 0
   otherwise. */

int
ammunition_gt_unsigned_integer ( int size, const void *op1, const void *op2 )
{
  return ammunition_bytes_comparison ( op1, op2, size ) > 0;
}

/* This function compares two integers of given size on that the first
   operand is greater than the second.  The function returns 1 if the
   first integer is greater than the second, 0 otherwise. */

int ammunition_gt_integer ( int size, const void *op1, const void *op2 )
{
  if ( INTEGER_SIGN ( op1 ) == 0 ) {
    if ( INTEGER_SIGN ( op2 ) == 0 )
      return ammunition_bytes_comparison ( op1, op2, size ) > 0;
    else
      return 1; /* TRUE */
  } else
    if ( INTEGER_SIGN ( op2 ) == 0 )
      return 0; /*FALSE*/
    else
      return ammunition_bytes_comparison ( op1, op2, size ) > 0;
}

/* This function compares two unsigned integers of given size on that
   the first operand is less than the second.  The function returns 1
   if the first unsigned integer is less than the second, 0
   otherwise. */

int
ammunition_lt_unsigned_integer ( int size, const void *op1, const void *op2 )
{
  return ammunition_bytes_comparison ( op1, op2, size ) < 0;
}

/* This function compares two integers of given size on that the first
   operand is less than the second.  The function returns 1 if the
   first integer is less than the second, 0 otherwise. */

int
ammunition_lt_integer ( int size, const void *op1, const void *op2 )
{
  if ( INTEGER_SIGN ( op1 ) == 0 ) {
    if ( INTEGER_SIGN ( op2 ) == 0 )
      return ammunition_bytes_comparison ( op1, op2, size ) < 0;
    else
      return 0; /*FALSE*/
  } else
    if ( INTEGER_SIGN ( op2 ) == 0 )
      return 1; /* TRUE */
    else
      return ammunition_bytes_comparison ( op1, op2, size ) < 0;
}

/* This function compares two unsigned integers of given size on that
   the first operand is greater than or equal to the second.  The
   function returns 1 if the first unsigned integer is greater than or
   equal to the second, 0 otherwise. */

int
ammunition_ge_unsigned_integer ( int size, const void *op1, const void *op2 )
{
  return ammunition_bytes_comparison ( op1, op2, size ) >= 0;
}

/* This function compares two integers of given size on that the first
   operand is greater than or equal to the second.  The function
   returns 1 if the first integer is greater than or equal to the
   second, 0 otherwise. */

int
ammunition_ge_integer ( int size, const void *op1, const void *op2 )
{
  if ( INTEGER_SIGN ( op1 ) == 0 ) {
    if ( INTEGER_SIGN ( op2 ) == 0 )
      return ammunition_bytes_comparison ( op1, op2, size ) >= 0;
    else
      return 1; /* TRUE */
  } else
    if ( INTEGER_SIGN ( op2 ) == 0 )
      return 0; /*FALSE*/
    else
      return ammunition_bytes_comparison ( op1, op2, size ) >= 0;
}

/* This function compares two unsigned integers of given size on that
   the first operand is less than or equal to the second.  The
   function returns 1 if the first unsigned integer is less than or
   equal to the second, 0 otherwise. */

int
ammunition_le_unsigned_integer ( int size, const void *op1, const void *op2 )
{
  return ammunition_bytes_comparison ( op1, op2, size ) <= 0;
}

/* This function compares two integers of given size on that the first
   operand is less than or equal to the second.  The function returns
   1 if the first integer is less than or equal to the second, 0
   otherwise. */

int
ammunition_le_integer ( int size, const void *op1, const void *op2 )
{
  if ( INTEGER_SIGN ( op1 ) == 0 ) {
    if ( INTEGER_SIGN ( op2 ) == 0 )
      return ammunition_bytes_comparison ( op1, op2, size ) <= 0;
    else
      return 0; /*FALSE*/
  } else
    if ( INTEGER_SIGN ( op2 ) == 0 )
      return 1; /* TRUE */
    else
      return ammunition_bytes_comparison ( op1, op2, size ) <= 0;
}



/* This page contains functions for changing size of arbitrary
   precision numbers. */

/* The function changes size of unsigned integer.  The function fixes
   overflow when result can not be represented by number of given
   size.  Result can be placed in operand. */

void
ammunition_change_unsigned_integer_size ( int operand_size, const void *operand,
    int result_size, void *result )
{
  int operand_digit_number;

  ammunition_overflow_bit = 0;
  if ( operand_size <= result_size ) {
    ammunition_memmove ( ( char * ) result + result_size - operand_size,
                         operand, ( size_x ) operand_size );
    ammunition_memset ( result, 0, ( size_x ) ( result_size - operand_size ) );
  } else {
    _Pragma( "loopbound min 2 max 2" )
    for ( operand_digit_number = 0;
          operand_digit_number < operand_size - result_size;
          operand_digit_number++ ) {
      if ( ( ( unsigned char * ) operand ) [operand_digit_number] != 0 ) {
        ammunition_overflow_bit = 1;
        break;
      }
    }
    ammunition_memmove ( result, 
                         ( char * ) operand + operand_size - result_size,
                         ( size_x ) result_size );
  }
  if ( ammunition_overflow_bit )
    ammunition_arithmetic_unsigned_overflow_reaction();
}

/* The function changes size of integer.  The function fixes overflow
   when result can not be represented by number of given size.  Result
   can be placed in operand. */

void
ammunition_change_integer_size ( int operand_size, const void *operand,
                                 int result_size, void *result )
{
  int operand_digit_number;
  int operand_sign;

  ammunition_overflow_bit = 0;
  operand_sign = INTEGER_SIGN ( operand );
  if ( operand_size <= result_size ) {
    ammunition_memmove ( ( char * ) result + result_size - operand_size,
                         operand, ( size_x ) operand_size );
    ammunition_memset ( result, ( operand_sign ? UCHAR_MAX : 0 ),
                        ( size_x ) ( result_size - operand_size ) );
  } else {
    _Pragma( "loopbound min 2 max 2" )
    for ( operand_digit_number = 0;
          operand_digit_number < operand_size - result_size;
          operand_digit_number++ ) {
      if ( ( ( unsigned char * ) operand ) [operand_digit_number]
           != ( operand_sign ? UCHAR_MAX : 0 ) ) {
        ammunition_overflow_bit = 1;
        break;
      }
    }
    ammunition_memmove ( result, 
                         ( char * ) operand + operand_size - result_size,
                         ( size_x ) result_size );
    if ( operand_sign != INTEGER_SIGN ( result ) )
      ammunition_overflow_bit = 1;
  }
  if ( ammunition_overflow_bit )
    ammunition_arithmetic_overflow_reaction();
}



/* This page contains functions for conversion of arbitrary precision
   numbers to ascii representation. */

/* This function transforms unsigned integer of given size to BASE
   ascii representation.  BASE should be between 2 and 36 including
   them.  Digits more 9 are represented by 'a', 'b' etc.  Sign is
   absent in result string.  The function returns the result
   string. */

char *
ammunition_unsigned_integer_to_based_string ( int size, const void *operand,
    int base,
    char *result )
{
  int digit_num;
  int i;
  unsigned long divisable;
  unsigned long remainder;
  int nonzero_flag;
  int length;
  int temporary;
  unsigned char operand_copy [MAX_INTEGER_OPERAND_SIZE];

  ammunition_memcpy ( operand_copy, operand, ( size_x ) size );
  length = 0;
  _Pragma( "loopbound min 1 max 10" )
  do {
    nonzero_flag = 0 /* FALSE */;
    _Pragma( "loopbound min 2 max 6" )
    for ( digit_num = 0, remainder = 0; digit_num < size; digit_num++ ) {
      divisable = remainder * ( UCHAR_MAX + 1 ) + operand_copy [digit_num];
      remainder = divisable % base;
      operand_copy [digit_num] = ( unsigned char ) ( divisable / base );
      if ( operand_copy [digit_num] != 0 )
        nonzero_flag = 1 /* TRUE */;
    }
    result [length++] = ( unsigned char ) ( remainder < 10 ? '0' + remainder
                                            : 'a' + remainder - 10 );
  } while ( nonzero_flag );
  result [length] = '\0';
  _Pragma( "loopbound min 0 max 5" )
  for ( i = 0; i < length / 2; i++ ) {
    temporary = result [i];
    result [i] = result [length - i - 1];
    result [length - i - 1] = temporary;
  }
  return result;
}


/* This function transforms unsigned integer of given size to decimal
   ascii representation.  Sign is absent in result string.  The
   function returns the result string. */

char *
ammunition_unsigned_integer_to_string ( int size, const void *operand,
                                        char *result )
{
  return ammunition_unsigned_integer_to_based_string ( size, operand, 10,
         result );
}


/* This function transforms integer of given size to BASE ascii
   representation.  BASE should be between 2 and 36 including them.
   Digits more 9 are represented by 'a', 'b' etc.  Sign is present in
   result string only for negative numbers.  The function returns the
   result string. */

char *
ammunition_integer_to_based_string ( int size, const void *operand, int base,
                                     char *result )
{
  unsigned char operand_copy [MAX_INTEGER_OPERAND_SIZE];

  if ( !INTEGER_SIGN ( operand ) )
    return ammunition_unsigned_integer_to_based_string ( size, operand, base,
           result );
  ammunition_memcpy ( operand_copy, operand, ( size_x ) size );
  /* May be integer overflow. But result is correct because it is unsigned. */
  ammunition_make_complementary_code ( size, operand_copy, operand_copy );
  *result = '-';
  ammunition_unsigned_integer_to_based_string ( size, operand_copy, base,
      result + 1 );
  return result;
}



/* This function transforms integer of given size to decimal ascii
   representation.  Sign is present in result string only for negative
   numbers.  The function returns the result string. */

char *
ammunition_integer_to_string ( int size, const void *operand, char *result )
{
  return ammunition_integer_to_based_string ( size, operand, 10, result );
}

/* This page contains functions for conversion of decimal ascii
   representation to arbitrary precision numbers.  */

/* The function adds digit (byte size) to unsigned integer.  The
   function returns 1 if unsigned integer overflow is fixed, 0
   otherwise. */

int ammunition_add_digit_to_unsigned_integer_without_overflow_reaction
( int size, void *operand, unsigned int digit )
{
  int digit_num;
  unsigned int carry;
  unsigned int sum;

  _Pragma( "loopbound min 4 max 4" )
  for ( digit_num = size - 1, carry = digit; digit_num >= 0;
        digit_num-- ) {
    sum = ( ( unsigned char * ) operand ) [digit_num] + carry;
    if ( sum > UCHAR_MAX ) {
      carry = sum / ( UCHAR_MAX + 1 );
      sum %= UCHAR_MAX + 1;
    } else
      carry = 0;
    ( ( unsigned char * ) operand ) [digit_num] = sum;
  }
  return carry != 0;
}

/* This function transforms source string (decimal ascii
   representation without sign) to given size unsigned integer and
   returns pointer to first non digit in the source string through a
   parameter.  If the string started with invalid integer
   representation the result will be zero and returns the operand
   through the parameter.  The function returns 1 if unsigned integer
   overflow is fixed, 0 otherwise. */

int ammunition_string_to_unsigned_integer_without_overflow_reaction
( int size, const char *operand, void *result, char **first_nondigit )
{
  int overflow_flag;

  ammunition_memset ( result, 0, ( size_x ) size );
  _Pragma( "loopbound min 0 max 10" )
  for ( overflow_flag = 0; ammunition_isdigit ( *operand ); operand++ ) {
    overflow_flag
      = overflow_flag ||
        ammunition_multiply_unsigned_integer_by_digit_without_overflow_reaction 
        ( size, result, 10 );
    overflow_flag
      = overflow_flag
        || ammunition_add_digit_to_unsigned_integer_without_overflow_reaction
        ( size, result, *operand - '0' );
  }
  *first_nondigit = ( char * ) operand;
  return overflow_flag;
}

/* This function skips all white spaces at the begin of source string
   and transforms tail of the source string (decimal ascii
   representation without sign) to given size unsigned integer with
   the aid of function
   `string_to_unsigned_integer_without_overflow_reaction'.  If the
   string started with invalid unsigned integer representation the
   result will be zero.  The function fixes overflow when result can
   not be represented by number of given size.  The function returns
   address of the first nondigit in the source string. */

char *
ammunition_unsigned_integer_from_string ( int size, const char *operand,
    void *result )
{
  char *first_nondigit;

  _Pragma( "loopbound min 0 max 0" )
  while ( ammunition_isspace ( *operand ) )
    operand++;
  ammunition_overflow_bit
    = ammunition_string_to_unsigned_integer_without_overflow_reaction
      ( size, operand, result, &first_nondigit );
  if ( ammunition_overflow_bit )
    ammunition_arithmetic_unsigned_overflow_reaction();
  return first_nondigit;
}

/* This function skips all white spaces at the begin of source string
   and transforms tail of the source string (decimal ascii
   representation with possible sign `+' or `-') to given size integer
   with the aid of function
   `string_to_unsigned_integer_without_overflow_reaction'.  If the
   string started with invalid integer representation the result will
   be zero.  The function fixes overflow when result can not be
   represented by number of given size.  the function returns Address
   of the first nondigit in the source string. */

char *
ammunition_integer_from_string ( int size, const char *operand, void *result )
{
  int negative_number_flag;
  char *first_nondigit;
  int unsigned_result_sign;

  _Pragma( "loopbound min 0 max 0" )
  while ( ammunition_isspace ( *operand ) )
    operand++;
  negative_number_flag = 0; /* FALSE */
  if ( *operand == '+' )
    operand++;
  else
    if ( *operand == '-' ) {
      operand++;
      negative_number_flag = 1; /* TRUE */
    }
  ammunition_overflow_bit
    = ammunition_string_to_unsigned_integer_without_overflow_reaction
      ( size, operand, result, &first_nondigit );
  unsigned_result_sign = INTEGER_SIGN ( result );
  if ( negative_number_flag )
    /* May be integer overflow when `result' is correct.  But result
       is correct because it is unsigned. */
    ammunition_make_complementary_code ( size, result, result );
  ammunition_overflow_bit
    = ammunition_overflow_bit 
      || ( unsigned_result_sign 
           && ( !negative_number_flag 
                || INTEGER_SIGN ( result ) != unsigned_result_sign ) );
  if ( ammunition_overflow_bit )
    ammunition_arithmetic_unsigned_overflow_reaction();
  return first_nondigit;
}