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
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
|
////////////////////////////////////////////////////////////////////////////////
// Author: Andy Rushton
// Copyright: (c) Southampton University 1999-2004
// (c) Andy Rushton 2004-2009
// License: BSD License, see ../docs/license.html
// The integer is represented as a sequence of bytes. They are stored such that
// element 0 is the lsB, which makes sense when seen as an integer offset but
// is counter-intuitive when you think that a string is usually read from left
// to right, 0 to size-1, in which case the lsB is on the *left*.
// This solution is compatible with 32-bit and 64-bit machines with either
// little-endian or big-endian representations of integers.
// Problem: I'm using std::string, which is an array of char. However, char is
// not well-defined - it could be signed or unsigned.
// In fact, there's no requirement for a char to even be one byte - it can be
// any size of one byte or more. However, it's just impossible to make any
// progress with that naffness (thanks to the C non-standardisation committee)
// and the practice is that char on every platform/compiler I've ever come
// across is that char = byte.
// The algorithms here use unsigned char to represent bit-patterns so I have to
// be careful to type-cast from char to unsigned char a lot. I use a typedef to
// make life easier.
////////////////////////////////////////////////////////////////////////////////
#include "inf.hpp"
#include <ctype.h>
////////////////////////////////////////////////////////////////////////////////
namespace stlplus
{
////////////////////////////////////////////////////////////////////////////////
// choose a sensible C type for a byte
typedef unsigned char byte;
////////////////////////////////////////////////////////////////////////////////
// local functions
// removes leading bytes that don't contribute to the value to create the minimum string representation
static void reduce_string(std::string& data)
{
while(data.size() > 1 &&
((byte(data[data.size()-1]) == byte(0) && byte(data[data.size()-2]) < byte(128)) ||
(byte(data[data.size()-1]) == byte(255) && byte(data[data.size()-2]) >= byte(128))))
{
data.erase(data.end()-1);
}
}
// generic implementations of type conversions from integer type to internal representation
// data: integer value for conversion
// result: internal representation
template <typename T>
static void convert_from_signed(const T& data, std::string& result)
{
result.erase();
bool lsb_first = little_endian();
byte* address = (byte*)&data;
for (size_t i = 0; i < sizeof(T); i++)
{
size_t offset = (lsb_first ? i : (sizeof(T) - i - 1));
result.append(1,address[offset]);
}
reduce_string(result);
}
template <typename T>
static void convert_from_unsigned(const T& data, std::string& result)
{
result.erase();
bool lsb_first = little_endian();
byte* address = (byte*)&data;
for (size_t i = 0; i < sizeof(T); i++)
{
size_t offset = (lsb_first ? i : (sizeof(T) - i - 1));
result.append(1,address[offset]);
}
// inf is signed - so there is a possible extra sign bit to add
result.append(1,std::string::value_type(0));
reduce_string(result);
}
// generic implementations of type conversions from internal representation to an integer type
// data : string representation of integer
// result: integer result of conversion
// return: flag indicating success - false = overflow
template <class T>
bool convert_to_signed(const std::string& data, T& result)
{
bool lsb_first = little_endian();
byte* address = (byte*)&result;
for (size_t i = 0; i < sizeof(T); i++)
{
size_t offset = lsb_first ? i : (sizeof(T) - i - 1);
if (i < data.size())
address[offset] = byte(data[i]);
else if (data.empty() || (byte(data[data.size()-1]) < byte(128)))
address[offset] = byte(0);
else
address[offset] = byte(255);
}
return data.size() <= sizeof(T);
}
template <class T>
bool convert_to_unsigned(const std::string& data, T& result)
{
bool lsb_first = little_endian();
byte* address = (byte*)&result;
for (size_t i = 0; i < sizeof(T); i++)
{
size_t offset = lsb_first ? i : (sizeof(T) - i - 1);
if (i < data.size())
address[offset] = byte(data[i]);
else
address[offset] = byte(0);
}
return data.size() <= sizeof(T);
}
////////////////////////////////////////////////////////////////////////////////
// Conversions to string
static char to_char [] = "0123456789abcdefghijklmnopqrstuvwxyz";
static int from_char [] =
{
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, -1, -1, -1, -1, -1, -1,
-1, 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, -1, -1, -1, -1, -1,
-1, 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, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1
};
static void convert_to_string(const stlplus::inf& data, std::string& result, unsigned radix = 10)
throw(std::invalid_argument)
{
// only support the C-style radixes plus 0b for binary
if (radix != 2 && radix != 8 && radix != 10 && radix != 16)
throw std::invalid_argument("invalid radix value");
inf local_i = data;
// untangle all the options
bool binary = radix == 2;
bool octal = radix == 8;
bool hex = radix == 16;
// the C representations for binary, octal and hex use 2's-complement representation
// all other represenations use sign-magnitude
if (hex || octal || binary)
{
// bit-pattern representation
// this is the binary representation optionally shown in octal or hex
// first generate the binary by masking the bits
for (unsigned j = local_i.bits(); j--; )
result += (local_i.bit(j) ? '1' : '0');
// the result is now the full width of the type - e.g. int will give a 32-bit result
// now interpret this as either binary, octal or hex and add the prefix
if (binary)
{
// trim down to the smallest string that preserves the value
while (true)
{
// do not trim to less than 1 bit (sign only)
if (result.size() <= 1) break;
// only trim if it doesn't change the sign and therefore the value
if (result[0] != result[1]) break;
result.erase(0,1);
}
// add the prefix
result.insert((std::string::size_type)0, "0b");
}
else if (octal)
{
// the result is currently binary
// trim down to the smallest string that preserves the value
while (true)
{
// do not trim to less than 2 bits (sign plus 1-bit magnitude)
if (result.size() <= 2) break;
// only trim if it doesn't change the sign and therefore the value
if (result[0] != result[1]) break;
result.erase(0,1);
}
// also ensure that the binary is a multiple of 3 bits to make the conversion to octal easier
while (result.size() % 3 != 0)
result.insert((std::string::size_type)0, 1, result[0]);
// now convert to octal
std::string octal_result;
for (unsigned i = 0; i < result.size()/3; i++)
{
// yuck - ugly or what?
if (result[i*3] == '0')
{
if (result[i*3+1] == '0')
{
if (result[i*3+2] == '0')
octal_result += '0';
else
octal_result += '1';
}
else
{
if (result[i*3+2] == '0')
octal_result += '2';
else
octal_result += '3';
}
}
else
{
if (result[i*3+1] == '0')
{
if (result[i*3+2] == '0')
octal_result += '4';
else
octal_result += '5';
}
else
{
if (result[i*3+2] == '0')
octal_result += '6';
else
octal_result += '7';
}
}
}
result = octal_result;
// add the prefix
result.insert((std::string::size_type)0, "0");
}
else
{
// similar to octal
while (true)
{
// do not trim to less than 2 bits (sign plus 1-bit magnitude)
if (result.size() <= 2) break;
// only trim if it doesn't change the sign and therefore the value
if (result[0] != result[1]) break;
result.erase(0,1);
}
// pad to a multiple of 4 characters
while (result.size() % 4 != 0)
result.insert((std::string::size_type)0, 1, result[0]);
// now convert to hex
std::string hex_result;
for (unsigned i = 0; i < result.size()/4; i++)
{
// yuck - ugly or what?
if (result[i*4] == '0')
{
if (result[i*4+1] == '0')
{
if (result[i*4+2] == '0')
{
if (result[i*4+3] == '0')
hex_result += '0';
else
hex_result += '1';
}
else
{
if (result[i*4+3] == '0')
hex_result += '2';
else
hex_result += '3';
}
}
else
{
if (result[i*4+2] == '0')
{
if (result[i*4+3] == '0')
hex_result += '4';
else
hex_result += '5';
}
else
{
if (result[i*4+3] == '0')
hex_result += '6';
else
hex_result += '7';
}
}
}
else
{
if (result[i*4+1] == '0')
{
if (result[i*4+2] == '0')
{
if (result[i*4+3] == '0')
hex_result += '8';
else
hex_result += '9';
}
else
{
if (result[i*4+3] == '0')
hex_result += 'a';
else
hex_result += 'b';
}
}
else
{
if (result[i*4+2] == '0')
{
if (result[i*4+3] == '0')
hex_result += 'c';
else
hex_result += 'd';
}
else
{
if (result[i*4+3] == '0')
hex_result += 'e';
else
hex_result += 'f';
}
}
}
}
result = hex_result;
// add the prefix
result.insert((std::string::size_type)0, "0x");
}
}
else
{
// convert to sign-magnitude
// the representation is:
// [sign]magnitude
bool negative = local_i.negative();
local_i.abs();
// create a representation of the magnitude by successive division
inf inf_radix(radix);
do
{
std::pair<inf,inf> divided = local_i.divide(inf_radix);
unsigned remainder = divided.second.to_unsigned();
char digit = to_char[remainder];
result.insert((std::string::size_type)0, 1, digit);
local_i = divided.first;
}
while(!local_i.zero());
// add the prefixes
// add a sign only for negative values
if (negative)
result.insert((std::string::size_type)0, 1, '-');
}
}
////////////////////////////////////////////////////////////////////////////////
// Conversions FROM string
void convert_from_string(const std::string& str, inf& result, unsigned radix = 10) throw(std::invalid_argument)
{
result = 0;
// only support the C-style radixes plus 0b for binary
// a radix of 0 means deduce the radix from the input - assume 10
if (radix != 0 && radix != 2 && radix != 8 && radix != 10 && radix != 16)
throw std::invalid_argument("invalid radix value");
unsigned i = 0;
// the radix passed as a parameter is just the default - it can be
// overridden by the C prefix
// Note: a leading zero is the C-style prefix for octal - I only make this
// override the default when the default radix is not specified
// first check for a C-style prefix
bool c_style = false;
if (i < str.size() && str[i] == '0')
{
// binary or hex
if (i+1 < str.size() && tolower(str[i+1]) == 'x')
{
c_style = true;
radix = 16;
i += 2;
}
else if (i+1 < str.size() && tolower(str[i+1]) == 'b')
{
c_style = true;
radix = 2;
i += 2;
}
else if (radix == 0)
{
c_style = true;
radix = 8;
i += 1;
}
}
if (radix == 0)
radix = 10;
if (c_style)
{
// the C style formats are bit patterns not integer values - these need
// to be sign-extended to get the right value
std::string binary;
if (radix == 2)
{
for (unsigned j = i; j < str.size(); j++)
{
switch(str[j])
{
case '0':
binary += '0';
break;
case '1':
binary += '1';
break;
default:
throw std::invalid_argument("invalid binary character in string " + str);
}
}
}
else if (radix == 8)
{
for (unsigned j = i; j < str.size(); j++)
{
switch(str[j])
{
case '0':
binary += "000";
break;
case '1':
binary += "001";
break;
case '2':
binary += "010";
break;
case '3':
binary += "011";
break;
case '4':
binary += "100";
break;
case '5':
binary += "101";
break;
case '6':
binary += "110";
break;
case '7':
binary += "111";
break;
default:
throw std::invalid_argument("invalid octal character in string " + str);
}
}
}
else
{
for (unsigned j = i; j < str.size(); j++)
{
switch(tolower(str[j]))
{
case '0':
binary += "0000";
break;
case '1':
binary += "0001";
break;
case '2':
binary += "0010";
break;
case '3':
binary += "0011";
break;
case '4':
binary += "0100";
break;
case '5':
binary += "0101";
break;
case '6':
binary += "0110";
break;
case '7':
binary += "0111";
break;
case '8':
binary += "1000";
break;
case '9':
binary += "1001";
break;
case 'a':
binary += "1010";
break;
case 'b':
binary += "1011";
break;
case 'c':
binary += "1100";
break;
case 'd':
binary += "1101";
break;
case 'e':
binary += "1110";
break;
case 'f':
binary += "1111";
break;
default:
throw std::invalid_argument("invalid hex character in string " + str);
}
}
}
// now convert the value
result.resize(binary.size());
for (unsigned j = 0; j < binary.size(); j++)
result.preset(binary.size() - j - 1, binary[j] == '1');
}
else
{
// sign-magnitude representation
// now scan for a sign and find whether this is a negative number
bool negative = false;
if (i < str.size())
{
switch (str[i])
{
case '-':
negative = true;
i++;
break;
case '+':
i++;
break;
}
}
for (; i < str.size(); i++)
{
result *= inf(radix);
unsigned char ascii = (unsigned char)str[i];
int ch = from_char[ascii] ;
if (ch == -1)
throw std::invalid_argument("invalid decimal character in string " + str);
result += inf(ch);
}
if (negative)
result.negate();
}
}
////////////////////////////////////////////////////////////////////////////////
// constructors - mostly implemented in terms of the assignment operators
inf::inf(void)
{
// void constructor initialises to zero - represented as a single-byte value containing zero
m_data.append(1,std::string::value_type(0));
}
inf::inf(short r)
{
operator=(r);
}
inf::inf(unsigned short r)
{
operator=(r);
}
inf::inf(int r)
{
operator=(r);
}
inf::inf(unsigned r)
{
operator=(r);
}
inf::inf(long r)
{
operator=(r);
}
inf::inf(unsigned long r)
{
operator=(r);
}
inf::inf (const std::string& r) throw(std::invalid_argument)
{
operator=(r);
}
inf::inf(const inf& r)
{
#ifdef __BORLANDC__
// work round bug in Borland compiler - copy constructor fails if string
// contains null characters, so do my own copy
for (unsigned i = 0; i < r.m_data.size(); i++)
m_data += r.m_data[i];
#else
m_data = r.m_data;
#endif
}
////////////////////////////////////////////////////////////////////////////////
inf::~inf(void)
{
}
////////////////////////////////////////////////////////////////////////////////
// assignments convert from iteger types to internal representation
inf& inf::operator = (short r)
{
convert_from_signed(r, m_data);
return *this;
}
inf& inf::operator = (unsigned short r)
{
convert_from_unsigned(r, m_data);
return *this;
}
inf& inf::operator = (int r)
{
convert_from_signed(r, m_data);
return *this;
}
inf& inf::operator = (unsigned r)
{
convert_from_unsigned(r, m_data);
return *this;
}
inf& inf::operator = (long r)
{
convert_from_signed(r, m_data);
return *this;
}
inf& inf::operator = (unsigned long r)
{
convert_from_unsigned(r, m_data);
return *this;
}
inf& inf::operator = (const std::string& r) throw(std::invalid_argument)
{
convert_from_string(r, *this);
return *this;
}
inf& inf::operator = (const inf& r)
{
m_data = r.m_data;
return *this;
}
////////////////////////////////////////////////////////////////////////////////
short inf::to_short(bool truncate) const throw(std::overflow_error)
{
short result = 0;
if (!convert_to_signed(m_data, result))
if (!truncate)
throw std::overflow_error("stlplus::inf::to_short");
return result;
}
unsigned short inf::to_unsigned_short(bool truncate) const throw(std::overflow_error)
{
unsigned short result = 0;
if (!convert_to_unsigned(m_data, result))
if (!truncate)
throw std::overflow_error("stlplus::inf::to_unsigned_short");
return result;
}
int inf::to_int(bool truncate) const throw(std::overflow_error)
{
int result = 0;
if (!convert_to_signed(m_data, result))
if (!truncate)
throw std::overflow_error("stlplus::inf::to_int");
return result;
}
unsigned inf::to_unsigned(bool truncate) const throw(std::overflow_error)
{
unsigned result = 0;
if (!convert_to_unsigned(m_data, result))
if (!truncate)
throw std::overflow_error("stlplus::inf::to_unsigned");
return result;
}
long inf::to_long(bool truncate) const throw(std::overflow_error)
{
long result = 0;
if (!convert_to_signed(m_data, result))
if (!truncate)
throw std::overflow_error("stlplus::inf::to_long");
return result;
}
unsigned long inf::to_unsigned_long(bool truncate) const throw(std::overflow_error)
{
unsigned long result = 0;
if (!convert_to_unsigned(m_data, result))
if (!truncate)
throw std::overflow_error("stlplus::inf::to_unsigned_long");
return result;
}
////////////////////////////////////////////////////////////////////////////////
// resize the inf regardless of the data
void inf::resize(unsigned bits)
{
if (bits == 0) bits = 1;
unsigned bytes = (bits+7)/8;
byte extend = negative() ? byte(255) : byte (0);
while(bytes > m_data.size())
m_data.append(1,extend);
}
// reduce the bit count to the minimum needed to preserve the value
void inf::reduce(void)
{
reduce_string(m_data);
}
////////////////////////////////////////////////////////////////////////////////
// the number of significant bits in the number
unsigned inf::bits (void) const
{
// The number of significant bits in the integer value - this is the number
// of indexable bits less any redundant sign bits at the msb
// This does not assume that the inf has been reduced to its minimum form
unsigned result = indexable_bits();
bool sign = bit(result-1);
while (result > 1 && (sign == bit(result-2)))
result--;
return result;
}
unsigned inf::size(void) const
{
return bits();
}
unsigned inf::indexable_bits (void) const
{
return 8 * unsigned(m_data.size());
}
////////////////////////////////////////////////////////////////////////////////
// bitwise operations
bool inf::bit (unsigned index) const throw(std::out_of_range)
{
if (index >= indexable_bits())
throw std::out_of_range(std::string("stlplus::inf::bit"));
// first split the offset into byte offset and bit offset
unsigned byte_offset = index/8;
unsigned bit_offset = index%8;
return (byte(m_data[byte_offset]) & (byte(1) << bit_offset)) != 0;
}
bool inf::operator [] (unsigned index) const throw(std::out_of_range)
{
return bit(index);
}
void inf::set (unsigned index) throw(std::out_of_range)
{
if (index >= indexable_bits())
throw std::out_of_range(std::string("stlplus::inf::set"));
// first split the offset into byte offset and bit offset
unsigned byte_offset = index/8;
unsigned bit_offset = index%8;
m_data[byte_offset] |= (byte(1) << bit_offset);
}
void inf::clear (unsigned index) throw(std::out_of_range)
{
if (index >= indexable_bits())
throw std::out_of_range(std::string("stlplus::inf::clear"));
// first split the offset into byte offset and bit offset
unsigned byte_offset = index/8;
unsigned bit_offset = index%8;
m_data[byte_offset] &= (~(byte(1) << bit_offset));
}
void inf::preset (unsigned index, bool value) throw(std::out_of_range)
{
if (value)
set(index);
else
clear(index);
}
inf inf::slice(unsigned low, unsigned high) const throw(std::out_of_range)
{
if (low >= indexable_bits())
throw std::out_of_range(std::string("stlplus::inf::slice: low index"));
if (high >= indexable_bits())
throw std::out_of_range(std::string("stlplus::inf::slice: high index"));
inf result;
if (high >= low)
{
// create a result the right size and filled with sign bits
std::string::size_type result_size = (high-low+1+7)/8;
result.m_data.erase();
byte extend = bit(high) ? byte(255) : byte (0);
while (result.m_data.size() < result_size)
result.m_data.append(1,extend);
// now set the relevant bits
for (unsigned i = low; i <= high; i++)
result.preset(i-low, bit(i));
}
return result;
}
////////////////////////////////////////////////////////////////////////////////
// testing operations
bool inf::negative (void) const
{
return bit(indexable_bits()-1);
}
bool inf::natural (void) const
{
return !negative();
}
bool inf::positive (void) const
{
return natural() && !zero();
}
bool inf::zero (void) const
{
for (std::string::size_type i = 0; i < m_data.size(); i++)
if (m_data[i] != 0)
return false;
return true;
}
bool inf::non_zero (void) const
{
return !zero();
}
bool inf::operator ! (void) const
{
return zero();
}
////////////////////////////////////////////////////////////////////////////////
// comparison operators
bool inf::operator == (const inf& r) const
{
// Two infs are equal if they are numerically equal, even if they are
// different sizes (i.e. they could be non-reduced values).
// This makes life a little more complicated than if I could assume that values were reduced.
byte l_extend = negative() ? byte(255) : byte (0);
byte r_extend = r.negative() ? byte(255) : byte (0);
std::string::size_type bytes = maximum(m_data.size(),r.m_data.size());
for (std::string::size_type i = bytes; i--; )
{
byte l_byte = (i < m_data.size() ? byte(m_data[i]) : l_extend);
byte r_byte = (i < r.m_data.size() ? byte(r.m_data[i]) : r_extend);
if (l_byte != r_byte)
return false;
}
return true;
}
bool inf::operator != (const inf& r) const
{
return !operator==(r);
}
bool inf::operator < (const inf& r) const
{
// This could be implemented in terms of subtraction. However, it can be
// simplified since there is no need to calculate the accurate difference,
// just the direction of the difference. I compare from msB down and as
// soon as a byte difference is found, that defines the ordering. The
// problem is that in 2's-complement, all negative values are greater than
// all natural values if you just do a straight unsigned comparison. I
// handle this by doing a preliminary test for different signs.
// For example, a 3-bit signed type has the coding:
// 000 = 0
// ...
// 011 = 3
// 100 = -4
// ...
// 111 = -1
// So, for natural values, the ordering of the integer values is the
// ordering of the bit patterns. Similarly, for negative values, the
// ordering of the integer values is the ordering of the bit patterns
// However, the bit patterns for the negative values are *greater than*
// the natural values. This is a side-effect of the naffness of
// 2's-complement representation
// first handle the case of comparing two values with different signs
bool l_sign = negative();
bool r_sign = r.negative();
if (l_sign != r_sign)
{
// one argument must be negative and the other natural
// the left is less if it is the negative one
return l_sign;
}
// the arguments are the same sign
// so the ordering is a simple unsigned byte-by-byte comparison
// However, this is complicated by the possibility that the values could be different lengths
byte l_extend = l_sign ? byte(255) : byte (0);
byte r_extend = r_sign ? byte(255) : byte (0);
std::string::size_type bytes = maximum(m_data.size(),r.m_data.size());
for (std::string::size_type i = bytes; i--; )
{
byte l_byte = (i < m_data.size() ? byte(m_data[i]) : l_extend);
byte r_byte = (i < r.m_data.size() ? byte(r.m_data[i]) : r_extend);
if (l_byte != r_byte)
return l_byte < r_byte;
}
// if I get here, the two are equal, so that is not less-than
return false;
}
bool inf::operator <= (const inf& r) const
{
return !(r < *this);
}
bool inf::operator > (const inf& r) const
{
return r < *this;
}
bool inf::operator >= (const inf& r) const
{
return !(*this < r);
}
////////////////////////////////////////////////////////////////////////////////
// logical operators
inf& inf::invert (void)
{
for (std::string::size_type i = 0; i < m_data.size(); i++)
m_data[i] = ~m_data[i];
return *this;
}
inf inf::operator ~ (void) const
{
inf result(*this);
result.invert();
return result;
}
inf& inf::operator &= (const inf& r)
{
// bitwise AND is extended to the length of the largest argument
byte l_extend = negative() ? byte(255) : byte (0);
byte r_extend = r.negative() ? byte(255) : byte (0);
std::string::size_type bytes = maximum(m_data.size(),r.m_data.size());
for (std::string::size_type i = 0; i < bytes; i++)
{
byte l_byte = (i < m_data.size() ? byte(m_data[i]) : l_extend);
byte r_byte = (i < r.m_data.size() ? byte(r.m_data[i]) : r_extend);
byte result = l_byte & r_byte;
if (i < m_data.size())
m_data[i] = result;
else
m_data.append(1,result);
}
// now reduce the result
reduce();
return *this;
}
inf inf::operator & (const inf& r) const
{
inf result(*this);
result &= r;
return result;
}
inf& inf::operator |= (const inf& r)
{
// bitwise OR is extended to the length of the largest argument
byte l_extend = negative() ? byte(255) : byte (0);
byte r_extend = r.negative() ? byte(255) : byte (0);
std::string::size_type bytes = maximum(m_data.size(),r.m_data.size());
for (std::string::size_type i = 0; i < bytes; i++)
{
byte l_byte = (i < m_data.size() ? byte(m_data[i]) : l_extend);
byte r_byte = (i < r.m_data.size() ? byte(r.m_data[i]) : r_extend);
byte result = l_byte | r_byte;
if (i < m_data.size())
m_data[i] = result;
else
m_data.append(1,result);
}
// now reduce the result
reduce();
return *this;
}
inf inf::operator | (const inf& r) const
{
inf result(*this);
result |= r;
return result;
}
inf& inf::operator ^= (const inf& r)
{
// bitwise XOR is extended to the length of the largest argument
byte l_extend = negative() ? byte(255) : byte (0);
byte r_extend = r.negative() ? byte(255) : byte (0);
std::string::size_type bytes = maximum(m_data.size(),r.m_data.size());
for (std::string::size_type i = 0; i < bytes; i++)
{
byte l_byte = (i < m_data.size() ? byte(m_data[i]) : l_extend);
byte r_byte = (i < r.m_data.size() ? byte(r.m_data[i]) : r_extend);
byte result = l_byte ^ r_byte;
if (i < m_data.size())
m_data[i] = result;
else
m_data.append(1,result);
}
// now reduce the result
reduce();
return *this;
}
inf inf::operator ^ (const inf& r) const
{
inf result(*this);
result ^= r;
return result;
}
////////////////////////////////////////////////////////////////////////////////
// shift operators all preserve the value by increasing the word size
inf& inf::operator <<= (unsigned shift)
{
// left shift is a shift towards the msb, with 0s being shifted in at the lsb
// split this into a byte shift followed by a bit shift
// first expand the value to be big enough for the result
std::string::size_type new_size = (indexable_bits() + shift + 7) / 8;
byte extend = negative() ? byte(255) : byte (0);
while (m_data.size() < new_size)
m_data.append(1,extend);
// now do the byte shift
unsigned byte_shift = shift/8;
if (byte_shift > 0)
{
for (std::string::size_type b = new_size; b--; )
m_data[b] = (b >= byte_shift) ? m_data[b-byte_shift] : byte(0);
}
// and finally the bit shift
unsigned bit_shift = shift%8;
if (bit_shift > 0)
{
for (std::string::size_type b = new_size; b--; )
{
byte current = byte(m_data[b]);
byte previous = b > 0 ? m_data[b-1] : byte(0);
m_data[b] = (current << bit_shift) | (previous >> (8 - bit_shift));
}
}
// now reduce the result
reduce();
return *this;
}
inf inf::operator << (unsigned shift) const
{
inf result(*this);
result <<= shift;
return result;
}
inf& inf::operator >>= (unsigned shift)
{
// right shift is a shift towards the lsb, with sign bits being shifted in at the msb
// split this into a byte shift followed by a bit shift
// a byte of sign bits
byte extend = negative() ? byte(255) : byte (0);
// do the byte shift
unsigned byte_shift = shift/8;
if (byte_shift > 0)
{
for (std::string::size_type b = 0; b < m_data.size(); b++)
m_data[b] = (b + byte_shift < m_data.size()) ? m_data[b+byte_shift] : extend;
}
// and finally the bit shift
unsigned bit_shift = shift%8;
if (bit_shift > 0)
{
for (std::string::size_type b = 0; b < m_data.size(); b++)
{
byte current = byte(m_data[b]);
byte next = ((b+1) < m_data.size()) ? m_data[b+1] : extend;
byte shifted = (current >> bit_shift) | (next << (8 - bit_shift));
m_data[b] = shifted;
}
}
// now reduce the result
reduce();
return *this;
}
inf inf::operator >> (unsigned shift) const
{
inf result(*this);
result >>= shift;
return result;
}
////////////////////////////////////////////////////////////////////////////////
// negation operators
inf& inf::negate (void)
{
// do 2's-complement negation
// equivalent to inversion plus one
invert();
operator += (inf(1));
return *this;
}
inf inf::operator - (void) const
{
inf result(*this);
result.negate();
return result;
}
inf& inf::abs(void)
{
if (negative()) negate();
return *this;
}
inf abs(const inf& i)
{
inf result = i;
result.abs();
return result;
}
////////////////////////////////////////////////////////////////////////////////
// addition operators
inf& inf::operator += (const inf& r)
{
// do 2's-complement addition
// Note that the addition can give a result that is larger than either argument
byte carry = 0;
std::string::size_type max_size = maximum(m_data.size(),r.m_data.size());
byte l_extend = negative() ? byte(255) : byte (0);
byte r_extend = r.negative() ? byte(255) : byte (0);
for (std::string::size_type i = 0; i < max_size; i++)
{
byte l_byte = (i < m_data.size() ? byte(m_data[i]) : l_extend);
byte r_byte = (i < r.m_data.size() ? byte(r.m_data[i]) : r_extend);
// calculate the addition in a type that is bigger than a byte in order to catch the carry-out
unsigned short result = ((unsigned short)(l_byte)) + ((unsigned short)(r_byte)) + carry;
// now truncate the result to get the lsB
if (i < m_data.size())
m_data[i] = byte(result);
else
m_data.append(1,byte(result));
// and capture the carry out by grabbing the second byte of the result
carry = byte(result >> 8);
}
// if the result overflowed or underflowed, add an extra byte to catch it
unsigned short result = ((unsigned short)(l_extend)) + ((unsigned short)(r_extend)) + carry;
if (byte(result) != (negative() ? byte(255) : byte(0)))
m_data.append(1,byte(result));
// now reduce the result
reduce();
return *this;
}
inf inf::operator + (const inf& r) const
{
inf result(*this);
result += r;
return result;
}
////////////////////////////////////////////////////////////////////////////////
// subtraction operators
inf& inf::operator -= (const inf& r)
{
// subtraction is defined in terms of negation and addition
inf negated = -r;
operator += (negated);
return *this;
}
inf inf::operator - (const inf& r) const
{
inf result(*this);
result -= r;
return result;
}
////////////////////////////////////////////////////////////////////////////////
// multiplication operators
inf& inf::operator *= (const inf& r)
{
// 2's complement multiplication
// one day I'll do a more efficient version than this based on the underlying representation
inf left(*this);
inf right = r;
// make the right value natural but preserve its sign for later
bool right_negative = right.negative();
right.abs();
// implemented as a series of conditional additions
operator = (0);
// left.resize(right.bits() + left.bits() - 1);
left <<= right.bits()-1;
for (unsigned i = right.bits(); i--; )
{
if (right[i])
operator += (left);
left >>= 1;
}
if (right_negative)
negate();
// now reduce the result
reduce();
return *this;
}
inf inf::operator * (const inf& r) const
{
inf result(*this);
result *= r;
return result;
}
////////////////////////////////////////////////////////////////////////////////
// division and remainder operators
std::pair<inf,inf> inf::divide(const inf& right) const throw(divide_by_zero)
{
if (right.zero())
throw divide_by_zero("stlplus::inf::divide");
inf numerator(*this);
inf denominator = right;
// make the numerator natural but preserve the sign for later
bool numerator_negative = numerator.negative();
numerator.abs();
// same with the denominator
bool denominator_negative = denominator.negative();
denominator.abs();
// the quotient and remainder will form the result
// start with the quotiont zero and the remainder equal to the whole of the
// numerator, then do trial subtraction from this
inf quotient;
inf remainder = numerator;
// there's nothing more to do if the numerator is smaller than the denominator
// but otherwise do the division
if (numerator.bits() >= denominator.bits())
{
// make the quotient big enough to take the result
quotient.resize(numerator.bits());
// start with the numerator shifted to the far left
unsigned shift = numerator.bits() - denominator.bits();
denominator <<= shift;
// do the division by repeated subtraction,
for (unsigned i = shift+1; i--; )
{
if (remainder >= denominator)
{
remainder -= denominator;
quotient.set(i);
}
denominator >>= 1;
}
}
// now adjust the signs
// x/(-y) == (-x)/y == -(x/y)
if (numerator_negative != denominator_negative)
quotient.negate();
quotient.reduce();
// x%(-y) == x%y and (-x)%y == -(x%y)
if (numerator_negative)
remainder.negate();
remainder.reduce();
return std::pair<inf,inf>(quotient,remainder);
}
inf& inf::operator /= (const inf& r) throw(divide_by_zero)
{
std::pair<inf,inf> result = divide(r);
operator=(result.first);
return *this;
}
inf inf::operator / (const inf& r) const throw(divide_by_zero)
{
std::pair<inf,inf> result = divide(r);
return result.first;
}
inf& inf::operator %= (const inf& r) throw(divide_by_zero)
{
std::pair<inf,inf> result = divide(r);
operator=(result.second);
return *this;
}
inf inf::operator % (const inf& r) const throw(divide_by_zero)
{
std::pair<inf,inf> result = divide(r);
return result.second;
}
////////////////////////////////////////////////////////////////////////////////
// prefix (void) and postfix (int) operators
inf& inf::operator ++ (void)
{
operator += (inf(1));
return *this;
}
inf inf::operator ++ (int)
{
inf old(*this);
operator += (inf(1));
return old;
}
inf& inf::operator -- (void)
{
operator -= (inf(1));
return *this;
}
inf inf::operator -- (int)
{
inf old(*this);
operator -= (inf(1));
return old;
}
////////////////////////////////////////////////////////////////////////////////
// string representation and I/O routines
std::string inf::to_string(unsigned radix) const
throw(std::invalid_argument)
{
std::string result;
convert_to_string(*this, result, radix);
return result;
}
inf& inf::from_string(const std::string& value, unsigned radix)
throw(std::invalid_argument)
{
convert_from_string(value, *this, radix);
return *this;
}
std::ostream& operator << (std::ostream& str, const inf& i)
{
try
{
// get radix
unsigned radix = 10;
if (str.flags() & std::ios_base::oct)
radix = 8;
if (str.flags() & std::ios_base::hex)
radix = 16;
// the field width is handled by iostream, so I don't need to handle it as well
// generate the string representation then print it
str << i.to_string(radix);
}
catch(const std::invalid_argument)
{
str.setstate(std::ios_base::badbit);
}
return str;
}
std::istream& operator >> (std::istream& str, inf& i)
{
try
{
// get radix
unsigned radix = 10;
if (str.flags() & std::ios_base::oct)
radix = 8;
if (str.flags() & std::ios_base::hex)
radix = 16;
// now get the string image of the value
std::string image;
str >> image;
// and convert to inf
i.from_string(image, radix);
}
catch(const std::invalid_argument)
{
str.setstate(std::ios_base::badbit);
}
return str;
}
////////////////////////////////////////////////////////////////////////////////
// diagnostic dump
// just convert to hex
std::string inf::image_debug(void) const
{
// create this dump in the human-readable form, i.e. msB to the left
std::string result = "0x";
for (std::string::size_type i = m_data.size(); i--; )
{
byte current = m_data[i];
byte msB = (current & byte(0xf0)) >> 4;
result += to_char[msB];
byte lsB = (current & byte(0x0f));
result += to_char[lsB];
}
return result;
}
const std::string& inf::get_bytes(void) const
{
return m_data;
}
void inf::set_bytes(const std::string& data)
{
m_data = data;
}
} // end namespace stlplus
|