This file is indexed.

/usr/include/polymake/next/Integer.h is in polymake 3.0r2-2+b1.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

   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
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
/* Copyright (c) 1997-2015
   Ewgenij Gawrilow, Michael Joswig (Technische Universitaet Berlin, Germany)
   http://www.polymake.org

   This program is free software; you can redistribute it and/or modify it
   under the terms of the GNU General Public License as published by the
   Free Software Foundation; either version 2, or (at your option) any
   later version: http://www.gnu.org/licenses/gpl.txt.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.
--------------------------------------------------------------------------------
*/

/** @file Integer.h
    @brief Implementation of pm::Integer class
*/

#ifndef POLYMAKE_INTEGER_H
#define POLYMAKE_INTEGER_H

#include "polymake/internal/operations.h"
#include "polymake/internal/comparators.h"
#include "polymake/internal/converters.h"
#include "polymake/numerical_functions.h"
#include "polymake/internal/CharBuffer.h"

#include <gmp.h>
#include <limits>
#include <climits>
#include <stdexcept>

namespace pm {

/** @namespace GMP
    @brief Wrapper classes for @ref GMP "GMP"'s number types.
*/

namespace GMP {

/** @brief Exception type
    A constructor of Integer or Rational from const char* throws an exception
    of this type in case of a syntax error.
*/
class error : public std::domain_error {
public:
   error(const std::string& what_arg) : std::domain_error(what_arg) {}
};

/// Exception type: "not a number".
class NaN : public error {
public:
   NaN();
};

/// Exception type: "division by zero".
class ZeroDivide : public error {
public:
   ZeroDivide();
};

class TempInteger : public MP_INT {
protected:
   /// never instantiate this class: it is a pure masquerade
   TempInteger();
   ~TempInteger();
};

}

// forward declarations needed for friend and specializations

class Integer; class Rational; class AccurateFloat;

Integer gcd(const Integer& a, const Integer& b);
Integer gcd(const Integer& a, long b);

Integer lcm(const Integer& a, const Integer& b);
Integer lcm(const Integer& a, long b);

ExtGCD<Integer> ext_gcd(const Integer& a, const Integer& b);

Div<Integer> div(const Integer& a, const Integer& b);
Div<Integer> div(const Integer& a, long b);

Integer div_exact(const Integer& a, const Integer& b);
Integer div_exact(const Integer& a, long b);

int isfinite(const Integer& a);
int isinf(const Integer& a);

}
namespace std {

template <>
class numeric_limits<pm::Integer>;
template <>
class numeric_limits<pm::Rational>;
template <>
class numeric_limits<pm::AccurateFloat>;

}
namespace pm {

/** @class Integer
    @brief Wrapper class for @ref GMP "GMP"'s mpz_t type.
 */

class Integer {
protected:
   /// GMP's representation
   mpz_t rep;

public:

#if defined(__GMP_PLUSPLUS__)
   // convert to the GMP's own C++ wrapper
   mpz_class gmp() const
   {
      return mpz_class(rep);
   }
   
   // construct an Integer from the GMP's own C++ wrapper
   Integer(const mpz_class& i)
   {
      mpz_init_set(rep,i.get_mpz_t());
   }
#endif

   /// Initialize to 0.
   Integer() { mpz_init(rep); }

   Integer(const Integer& i)
   {
      if (__builtin_expect(isfinite(i),1))
         mpz_init_set(rep, i.rep);
      else
         _init_set_inf(rep, i.rep);
   }

   /// conversion
   Integer(long i)
   {
      mpz_init_set_si(rep, i);
   }

   /// conversion
   Integer(long long i)
   {
      mpz_init_set_si(rep, i);
   }

   /// conversion
   Integer(unsigned long i)
   {
      mpz_init_set_ui(rep, i);
   }

   /// conversion
   Integer(int i)
   {
      mpz_init_set_si(rep, i);
   }

   /// conversion
   Integer(unsigned int i)
   {
      mpz_init_set_ui(rep, i);
   }

   /// conversion
   Integer(double d)
   {
      const int is=isinf(d);
      if (__builtin_expect(is, 0))
         _init_set_inf(rep,is);
      else
         mpz_init_set_d(rep, d);
   }

   /// Recognizes automatically number base 10, 8, or 16.
   explicit Integer(const char* s)
   {
      mpz_init(rep);
      try {
         _set(s);
      }
      catch (const GMP::error&) {
         mpz_clear(rep);
         throw;
      }
   }

   explicit Integer(mpz_srcptr src)
   {
      mpz_init_set(rep, src);
   }

   /// take over the GMP structure without copying
   explicit Integer(GMP::TempInteger& tmp)
   {
      rep[0]=tmp;
   }
protected:
   static void _init_set_inf(mpz_ptr rep, int sign)
   {
      rep->_mp_alloc=0;
      rep->_mp_size=sign;
      rep->_mp_d=NULL;
   }
   static void _init_set_inf(mpz_ptr rep, mpz_srcptr b)
   {
      _init_set_inf(rep, b->_mp_size);
   }
   static void _init_set_inf(mpz_ptr rep, mpz_srcptr b, int inv)
   {
      _init_set_inf(rep, b->_mp_size<0 ? -inv : inv);
   }
   static void _set_inf(mpz_ptr rep, int sign)
   {
      mpz_clear(rep);
      _init_set_inf(rep,sign);
   }
   static void _set_inf(mpz_ptr rep, mpz_srcptr b)
   {
      _set_inf(rep, b->_mp_size);
   }
   static void _set_inf(mpz_ptr rep, mpz_srcptr b, int inv)
   {
      _set_inf(rep, b->_mp_size<0 ? -inv : inv);
   }
   static void _inf_inv_sign(mpz_ptr rep, long s, bool division=false)
   {
      if (s<0)
         rep->_mp_size*=-1;
      else if (s==0 && !division)
         throw GMP::NaN();
   }

   template <typename T>
   explicit Integer(maximal<T>, int s=1)
   {
      _init_set_inf(rep,s);
   }

   template <typename T>
   Integer(maximal<T>, mpz_srcptr b)
   {
      _init_set_inf(rep,b);
   }

   template <typename T>
   Integer(maximal<T>, mpz_srcptr b, int inv)
   {
      _init_set_inf(rep,b,inv);
   }
public:

   /// Performs division with rounding via truncation.
   explicit inline Integer(const Rational& b);

   explicit Integer(const AccurateFloat& b);

   /// Performs division with rounding via truncation.
   inline Integer& operator= (const Rational& b);

   Integer& operator= (const AccurateFloat& b);

   template <typename Arg>
   Integer(void (*f)(mpz_ptr,Arg), Arg a)
   {
      mpz_init(rep);
      f(rep,a);
   }

   template <typename Arg1, typename Arg2>
   Integer(void (*f)(mpz_ptr,Arg1,Arg2), Arg1 a, Arg2 b)
   {
      mpz_init(rep);
      f(rep,a,b);
   }

   template <typename Arg1, typename Arg2>
   Integer(Arg2 (*f)(mpz_ptr,Arg1,Arg2), Arg1 a, Arg2 b)
   {
      mpz_init(rep);
      f(rep,a,b);
   }

   struct Reserve {};

   /// Reserve space for n bits.
   Integer(size_t n, Reserve)
   {
      mpz_init2(rep, n);
   }

   static
   mpz_srcptr _tmp_negate(mpz_ptr temp, mpz_srcptr src)
   {
      temp->_mp_alloc =  src->_mp_alloc;
      temp->_mp_size  = -src->_mp_size;
      temp->_mp_d     =  src->_mp_d;
      return temp;
   }

   ~Integer()
   {
      mpz_clear(rep);
#if POLYMAKE_DEBUG
      POLYMAKE_DEBUG_METHOD(Integer,dump);
#endif
   }

   /// Assignment.
   Integer& operator= (const Integer& b)
   {
      const bool f1=isfinite(*this), f2=isfinite(b);
      if (__builtin_expect(f1 && f2, 1))
         mpz_set(rep, b.rep);
      else if (f2)
         mpz_init_set(rep, b.rep);
      else
         _set_inf(rep, b.rep);
      return *this;
   }

   /// Assignment with conversion.
   Integer& operator= (long b)
   {
      if (__builtin_expect(isfinite(*this),1))
         mpz_set_si(rep, b);
      else
         mpz_init_set_si(rep, b);
      return *this;
   }

   /// Assignment with conversion.
   Integer& operator= (unsigned long b)
   {
      if (__builtin_expect(isfinite(*this),1))
         mpz_set_ui(rep, b);
      else
         mpz_init_set_ui(rep, b);
      return *this;
   }

   /// Assignment with conversion.
   Integer& operator= (int b) { return operator=(long(b)); }

   /// Assignment with conversion.
   Integer& operator= (double d)
   {
      const bool f1=isfinite(*this);
      const int i2=isinf(d);
      if (__builtin_expect(f1 && !i2, 1))
         mpz_set_d(rep, d);
      else if (!i2)
         mpz_init_set_d(rep, d);
      else
         _set_inf(rep, i2);
      return *this;
   }

   /// Fill with bits coming from an open file.
   /// The current allocated size is preserved (see constructor with Reserve() argument).
   /// @retval true if filled successfully, false if eof or read error occured.
   bool fill_from_file(int fd);

protected:
   void _set(const char *s);

public:
   /// Recognizes automatically number base 10, 8, or 16.
   Integer& set(const char *s)
   {
      if (__builtin_expect(!isfinite(*this),0))
         mpz_init(rep);
      _set(s);
      return *this;
   }

   /// for the rare case of unwrapped GMP objects coexisting with us
   Integer& set(mpz_srcptr src)
   {
      if (__builtin_expect(isfinite(*this),1))
         mpz_set(rep, src);
      else
         mpz_init_set(rep, src);
      return *this;
   }

   /// Cast.
   double to_double() const
   {
      const int i=isinf(*this);
      if (__builtin_expect(i,0))
         return i*std::numeric_limits<double>::infinity();
      return mpz_get_d(rep);
   }

   /// Cast.
   long to_long() const
   {
      if (!mpz_fits_slong_p(rep) || !isfinite(*this))
         throw GMP::error("Integer: value too big");
      return mpz_get_si(rep);
   }

   /// Cast.
   int to_int() const
   {
      if (!mpz_fits_sint_p(rep) || !isfinite(*this))
         throw GMP::error("Integer: value too big");
      return mpz_get_si(rep);
   }

   /// Converts integer to string.
   std::string to_string(int base=10) const;

   bool non_zero() const
   {
      return mpz_sgn(rep);
   }

   /// Efficiently swapping two Integer objects.
   void swap(Integer& b) { mpz_swap(rep, b.rep); }

   /** Accelerated combination of copy constructor and destructor.
       Aimed to be used in container classes only! */
   friend void relocate(Integer* from, Integer* to)
   {
      to->rep[0] = from->rep[0];
   }

   /// Increment.
   Integer& operator++()
   {
      if (__builtin_expect(isfinite(*this),1)) mpz_add_ui(rep, rep, 1);
      return *this;
   }

   /// Decrement.
   Integer& operator--()
   {
      if (__builtin_expect(isfinite(*this),1)) mpz_sub_ui(rep, rep, 1);
      return *this;
   }

   /// In-place negation.
   Integer& negate()
   {
      rep[0]._mp_size*=-1;
      return *this;
   }

   /// Addition.
   Integer& operator+= (const Integer& b)
   {
      const bool f1=isfinite(*this), f2=isfinite(b);
      if (__builtin_expect(f1 && f2, 1))
         mpz_add(rep, rep, b.rep);
      else if (f1)
         _set_inf(rep, b.rep);
      else if (!f2 && isinf(*this)!=isinf(b))
         throw GMP::NaN();
      return *this;
   }

   /// Addition.
   Integer& operator+= (long b)
   {
      if (__builtin_expect(isfinite(*this),1)) {
         if (b>=0) mpz_add_ui(rep, rep, b);
         else mpz_sub_ui(rep, rep, -b);
      }
      return *this;
   }

   /// Subtraction.
   Integer& operator-= (const Integer& b)
   {
      const bool f1=isfinite(*this), f2=isfinite(b);
      if (__builtin_expect(f1 && f2, 1))
         mpz_sub(rep, rep, b.rep);
      else if (f1)
         _set_inf(rep, b.rep, -1);
      else if (!f2 && isinf(*this)==isinf(b))
         throw GMP::NaN();
      return *this;
   }

   /// Subtraction.
   Integer& operator-= (long b)
   {
      if (__builtin_expect(isfinite(*this),1)) {
         if (b>=0)
            mpz_sub_ui(rep, rep, b);
         else
            mpz_add_ui(rep, rep, -b);
      }
      return *this;
   }

   /// Multiplication.
   Integer& operator*= (const Integer& b)
   {
      const bool f1=isfinite(*this), f2=isfinite(b);
      if (__builtin_expect(f1 && f2, 1))
         mpz_mul(rep, rep, b.rep);
      else
         _inf_inv_sign(rep, mpz_sgn(b.rep));
      return *this;
   }

   /// Multiplication.
   Integer& operator*= (long b)
   {
      if (__builtin_expect(isfinite(*this),1))
         mpz_mul_si(rep, rep, b);
      else
         _inf_inv_sign(rep,b);
      return *this;
   }

   /// Division with rounding via truncation.
   Integer& operator/= (const Integer& b)
   {
      const bool f1=isfinite(*this), f2=isfinite(b);
      if (__builtin_expect(f2,1)) {
         const int s2=mpz_sgn(b.rep);
         if (__builtin_expect(f1,1)) {
            if (__builtin_expect(s2,1))
               mpz_tdiv_q(rep, rep, b.rep);
            else
               throw GMP::ZeroDivide();
         } else
            _inf_inv_sign(rep,s2,true);
      } else if (f1)
         mpz_set_ui(rep, 0);
      else
         throw GMP::NaN();
      return *this;
   }

   /// Division with rounding via truncation.
   Integer& operator/= (long b)
   {
      if (__builtin_expect(isfinite(*this),1)) {
         if (__builtin_expect(b,1)) {
            if (b>0)
               mpz_tdiv_q_ui(rep, rep, b);
            else {
               mpz_tdiv_q_ui(rep, rep, -b);
               negate();
            }
         } else
            throw GMP::ZeroDivide();
      } else
         _inf_inv_sign(rep,b,true);
      return *this;
   }

   /// b != infinity; but 0/0 allowed
   Integer& div_exact(const Integer& b)
   {
      const int s2=mpz_sgn(b.rep);
      if (__builtin_expect(isfinite(*this),1)) {
         if (__builtin_expect(s2,1))
            mpz_divexact(rep, rep, b.rep);
      } else
         _inf_inv_sign(rep,s2,true);
      return *this;
   }

   /// 0/0 allowed
   Integer& div_exact(long b)
   {
      if (__builtin_expect(isfinite(*this),1)) {
         if (__builtin_expect(b,1)) {
            if (b>0)
               mpz_divexact_ui(rep, rep, b);
            else {
               mpz_divexact_ui(rep, rep, -b);
               negate();
            }
         }
      } else
         _inf_inv_sign(rep,b,true);
      return *this;
   }

   /// Remainder of division.
   Integer& operator%= (const Integer& b)
   {
      const bool f1=isfinite(*this), f2=isfinite(b);
      if (__builtin_expect(!b.non_zero(), 0))
         throw GMP::ZeroDivide();
      if (__builtin_expect(f1 && f2, 1)) {
         mpz_tdiv_r(rep, rep, b.rep);
      } else if (f2)
         mpz_set_ui(rep, 0);
      else
         throw GMP::NaN();
      return *this;
   }

   /// Remainder of division.
   Integer& operator%= (long b)
   {
      if (__builtin_expect(!b,0))
         throw GMP::ZeroDivide();
      if (__builtin_expect(isfinite(*this),1))
         mpz_tdiv_r_ui(rep, rep, abs(b));
      else
         mpz_set_ui(rep, 0);
      return *this;
   }

   /// Multiply with 2**k.
   Integer& operator<<= (unsigned long k)
   {
      if (__builtin_expect(isfinite(*this),1))
         mpz_mul_2exp(rep, rep, k);
      return *this;
   }

   /// Divide through 2**k, truncate to zero.
   Integer& operator>>= (unsigned long k)
   {
      if (__builtin_expect(isfinite(*this),1))
         mpz_tdiv_q_2exp(rep, rep, k);
      return *this;
   }

   /// Addition.
   friend Integer operator+ (const Integer& a, const Integer& b)
   {
      const bool f1=isfinite(a), f2=isfinite(b);
      if (__builtin_expect(f1 && f2, 1))
         return Integer(mpz_add, a.rep, b.rep);
      if (f2) return a;
      if (!f1 && isinf(a)!=isinf(b))
         throw GMP::NaN();
      return b;
   }

   /// Addition.
   friend Integer operator+ (const Integer& a, long b)
   {
      if (__builtin_expect(isfinite(a),1))
         return Integer(b>=0 ? mpz_add_ui : mpz_sub_ui, a.rep, (unsigned long)abs(b));
      return a;
   }

   /// Subtraction.
   friend Integer operator- (const Integer& a, const Integer& b)
   {
      const bool f1=isfinite(a), f2=isfinite(b);
      if (__builtin_expect(f1 && f2, 1))
         return Integer(mpz_sub, a.rep, b.rep);
      if (f2) return a;
      if (!f1 && isinf(a)==isinf(b))
         throw GMP::NaN();
      return Integer(maximal<Integer>(), b.rep, -1);
   }

   /// Subtraction.
   friend Integer operator- (const Integer& a, long b)
   {
      if (__builtin_expect(isfinite(a),1))
         return Integer(b>=0 ? mpz_sub_ui : mpz_add_ui, a.rep, (unsigned long)abs(b));
      return a;
   }

   /// Subtraction.
   friend Integer operator- (long a, const Integer& b)
   {
      if (__builtin_expect(isfinite(b),1)) {
         mpz_t minus_b;
         return Integer(a>=0 ? mpz_add_ui : mpz_sub_ui,
                        _tmp_negate(minus_b,b.rep),  (unsigned long)abs(a));
      }
      return Integer(maximal<Integer>(), b.rep, -1);
   }

   /// Subtraction.
   friend Integer operator- (const Integer& a)
   {
      if (__builtin_expect(isfinite(a),1))
         return Integer(mpz_neg, a.rep);
      return Integer(maximal<Integer>(), a.rep, -1);
   }

   /// Multiplication.
   friend Integer operator* (const Integer& a, const Integer& b)
   {
      const bool f1=isfinite(a), f2=isfinite(b);
      if (__builtin_expect(f1 && f2, 1))
         return Integer(mpz_mul, a.rep, b.rep);
      const int s=mpz_sgn(a.rep)*mpz_sgn(b.rep);
      if (!s) throw GMP::NaN();
      return Integer(maximal<Integer>(), s);
   }

   /// Multiplication.
   friend Integer operator* (const Integer& a, long b)
   {
      if (__builtin_expect(isfinite(a),1))
         return Integer(mpz_mul_si, a.rep, b);
      if (!b) throw GMP::NaN();
      return Integer(maximal<Integer>(), a.rep, sign(b));
   }

   /// Division with rounding via truncation.
   friend Integer operator/ (const Integer& a, const Integer& b)
   {
      const bool f1=isfinite(a), f2=isfinite(b);
      if (__builtin_expect(f2,1)) {
         const int s2=mpz_sgn(b.rep);
         if (__builtin_expect(f1,1)) {
            if (__builtin_expect(s2,1))
               return Integer(mpz_tdiv_q, a.rep, b.rep);
            else
               throw GMP::ZeroDivide();
         }
         return Integer(maximal<Integer>(), a.rep, s2<0 ? -1 : 1);
      } else if (f1)
         return Integer();
      else
         throw GMP::NaN();
   }

   /// Division.
   friend Integer operator/ (const Integer& a, long b)
   {
      if (__builtin_expect(isfinite(a),1)) {
         if (__builtin_expect(b,1)) {
            if (b>0)
               return Integer(mpz_tdiv_q_ui, a.rep, (unsigned long)b);
            mpz_t minus_a;
            return Integer(mpz_tdiv_q_ui, _tmp_negate(minus_a,a.rep), (unsigned long)(-b));
         } else
            throw GMP::ZeroDivide();
      }
      return Integer(maximal<Integer>(), a.rep, b<0 ? -1 : 1);
   }

   /// Division.
   friend int operator/ (int a, const Integer& b)
   {
      if (__builtin_expect(!b.non_zero(), 0))
         throw GMP::ZeroDivide();
      return isfinite(b) && mpz_fits_sint_p(b.rep) ? a/mpz_get_si(b.rep) : 0;
   }

   /// Division.
   friend long operator/ (long a, const Integer& b)
   {
      if (__builtin_expect(!b.non_zero(), 0))
         throw GMP::ZeroDivide();
      return isfinite(b) && mpz_fits_slong_p(b.rep) ? a/mpz_get_si(b.rep) : 0;
   }

   /// Remainder of division.
   friend Integer operator% (const Integer& a, const Integer& b)
   {
      const bool f1=isfinite(a), f2=isfinite(b);
      if (__builtin_expect(!b.non_zero(), 0))
         throw GMP::ZeroDivide();
      if (__builtin_expect(f1 && f2, 1)) {
         return Integer(mpz_tdiv_r, a.rep, b.rep);
      }
      if (!f2)
         throw GMP::NaN();
      return Integer();
   }

   /// Remainder of division.
   friend long operator% (const Integer& a, long b)
   {
      if (__builtin_expect(!b, 0))
         throw GMP::ZeroDivide();
      if (__builtin_expect(isfinite(a),1)) {
         long r=mpz_tdiv_ui(a.rep, b);
         return mpz_sgn(a.rep)>=0 ? r : -r;
      }
      return 0;
   }

   /// Remainder of division.
   friend Integer operator% (int a, const Integer& b)
   {
      if (__builtin_expect(!b.non_zero(), 0))
         throw GMP::ZeroDivide();
      return isfinite(b) && mpz_fits_sint_p(b.rep) ? Integer(a%mpz_get_si(b.rep)) : b;
   }

   /// Remainder of division.
   friend Integer operator% (long a, const Integer& b)
   {
      if (__builtin_expect(!b.non_zero(), 0))
         throw GMP::ZeroDivide();
      return isfinite(b) && mpz_fits_slong_p(b.rep) ? Integer(a%mpz_get_si(b.rep)) : b;
   }

   /// Obtain both the quotient and remainder of a division
   friend
   Div<Integer> div(const Integer& a, const Integer& b);

   friend
   Div<Integer> div(const Integer& a, long b);

   /// Multiply with 2**k.
   friend Integer operator<< (const Integer& a, unsigned long k)
   {
      if (__builtin_expect(isfinite(a),1))
         return Integer(mpz_mul_2exp, a.rep, k);
      return a;
   }

   /// Divide through 2**k, truncate to 0.
   friend Integer operator>> (const Integer& a, unsigned long k)
   {
      if (__builtin_expect(isfinite(a),1))
         return Integer(mpz_tdiv_q_2exp, a.rep, k);
      return a;
   }

   /// Test for bits.
   bool bit(unsigned long i) const { return mpz_tstbit(rep, i); }

   /// Parity.
   bool odd() const { return isfinite(*this) && bit(0); }
   /// Parity.
   bool even() const { return !odd(); }

   friend int isfinite(const Integer& a);
   friend int isinf(const Integer& a);

   /// Comparison. The magnitude of the return value is arbitrary, only its sign is relevant.
   int compare(const Integer& b) const
   {
      const int i1=isinf(*this), i2=isinf(b);
      if (__builtin_expect(i1 | i2, 0))
         return i1-i2;
      return mpz_cmp(rep, b.rep);
   }

   /// Comparison.
   int compare(long b) const
   {
      const int i1=isinf(*this);
      if (__builtin_expect(i1,0))
         return i1;
      return mpz_cmp_si(rep, b);
   }

   /// Comparison.
   int compare(int b) const { return compare(long(b)); }

   /// Comparison.
   int compare(double b) const
   {
      const int i1=isinf(*this);
      if (__builtin_expect(i1,0))
         return i1-isinf(b);
      return mpz_cmp_d(rep, b);
   }

   typedef list comparable_with(int, long, double);

   /// Equality
   friend bool operator== (const Integer& a, long b)
   {
      return isfinite(a) && mpz_fits_slong_p(a.rep) && mpz_get_si(a.rep)==b;
   }

   /// Less than.
   friend bool operator< (const Integer& a, long b)
   {
      const int i1=isinf(a);
      if (__builtin_expect(i1,0)) return i1<0;
      return mpz_fits_slong_p(a.rep) ? mpz_get_si(a.rep)<b : mpz_sgn(a.rep)<0;
   }

   /// Greater than.
   friend bool operator> (const Integer& a, long b)
   {
      const int i1=isinf(a);
      if (__builtin_expect(i1,0)) return i1>0;
      return mpz_fits_slong_p(a.rep) ? mpz_get_si(a.rep)>b : mpz_sgn(a.rep)>0;
   }

   /// Equality of absolute values.
   friend bool abs_equal(const Integer& a, const Integer& b)
   {
      const bool f1=isfinite(a), f2=isfinite(b);
      if (__builtin_expect(f1 && f2, 1))
         return !mpz_cmpabs(a.rep, b.rep);
      return f1==f2;
   }

   /// Equality of absolute values.
   friend bool abs_equal(const Integer& a, long b)
   {
      return isfinite(a) && !mpz_cmpabs_ui(a.rep, abs(b));
   }

   /// Equality of absolute values.
   friend bool abs_equal(const Integer& a, double b)
   {
      if (__builtin_expect(isfinite(a),1))
         return !mpz_cmpabs_d(a.rep, b);
      return isinf(b);
   }

   /// Factorial.
   static Integer fac(long k)
   {
      if (k<0) throw std::runtime_error("fac not defined for negative values");
      return Integer(mpz_fac_ui, (unsigned long)k);
   }

   /// Power.
   static Integer pow(const Integer& a, unsigned long k)
   {
      if (__builtin_expect(isfinite(a),1))
         return Integer(mpz_pow_ui, a.rep, k);
      return Integer(maximal<Integer>(), k%2 ? isinf(a) : 1);
   }

   /// Power.
   static Integer pow(unsigned long a, unsigned long k)
   {
      return Integer(mpz_ui_pow_ui, a, k);
   }

   /// Square Root.
   friend Integer sqrt(const Integer& a)
   {
      if (__builtin_expect(isfinite(a),1))
         return Integer(mpz_sqrt, a.rep);
      return Integer(maximal<Integer>(), 1);
   }

   /// Absolute value.
   friend Integer abs(const Integer& a)
   {
      if (__builtin_expect(isfinite(a),1))
         return Integer(mpz_abs, a.rep);
      return Integer(maximal<Integer>(), 1);
   }

   /// Greatest common divisor.
   friend
   Integer gcd(const Integer& a, const Integer& b);

   /// Greatest common divisor.
   friend
   Integer gcd(const Integer& a, long b);

   /// Least common multiple.
   friend
   Integer lcm(const Integer& a, const Integer& b);

   friend
   Integer lcm(const Integer& a, long b);

   /// Extended gcd algorithm: g=a*p+b*q, a=m*g, b=n*g.
   friend
   ExtGCD<Integer> ext_gcd(const Integer& a, const Integer& b);

   friend
   Integer div_exact(const Integer& a, const Integer& b);

   friend
   Integer div_exact(const Integer& a, long b);

   /// Binomial coefficient.
   static
   Integer binom(const Integer& n, long k);

   /// Binomial coefficient.
   static
   Integer binom(long n, long k);

   /// @param allow_sign whether leading whitespaces and sign are expected
   void read(std::istream& is, bool allow_sign=true);

   /// Calculates the size of the buffer needed to store an ASCII representation of an Integer.
   size_t strsize(std::ios::fmtflags flags) const;

   /** Produces a printable representation of an Integer.
       @param buf buffer of size not less than the return value of strsize().
   */
   void putstr(std::ios::fmtflags flags, char *buf) const;

   friend class Rational;
   friend struct spec_object_traits<Integer>;
   template <typename> friend class std::numeric_limits;

   mpz_srcptr get_rep() const { return rep; }

   /// Output to stream.
   friend
   std::ostream& operator<< (std::ostream& os, const Integer& a)
   {
      const std::ios::fmtflags flags=os.flags();
      a.putstr(flags, OutCharBuffer::reserve(os, a.strsize(flags)));
      return os;
   }

   /// Input from stream.
   friend
   std::istream& operator>> (std::istream& is, Integer& a)
   {
      a.read(is);
      return is;
   }
#if POLYMAKE_DEBUG
   void dump() const { std::cerr << *this << std::flush; }
#endif
};

}
namespace std {

inline void swap(pm::Integer& i1, pm::Integer& i2) { i1.swap(i2); }

template <>
class numeric_limits<pm::Integer> : public numeric_limits<long> {
public:
   static const bool has_infinity=true;
   static pm::Integer min() throw() { return pm::Integer(pm::maximal<pm::Integer>(),-1); }
   static pm::Integer infinity() throw() { return pm::Integer(pm::maximal<pm::Integer>()); }
   static pm::Integer max() throw() { return pm::Integer(pm::maximal<pm::Integer>()); }
   static const int digits=INT_MAX;
   static const int digits10=INT_MAX;
   static const bool is_bounded=false;
};

}
namespace pm {

inline Integer operator+ (const Integer& a) { return a; }

inline const Integer operator++ (Integer& a, int) { Integer copy(a); ++a; return copy; }
inline const Integer operator-- (Integer& a, int) { Integer copy(a); --a; return copy; }

inline Integer operator+ (long a, const Integer& b) { return b+a; }
inline Integer operator+ (const Integer& a, int b) { return a+long(b); }
inline Integer operator+ (int a, const Integer& b) { return b+long(a); }

inline Integer operator- (const Integer& a, int b) { return a-long(b); }
inline Integer operator- (int a, const Integer& b) { return long(a)-b; }

inline Integer operator* (long a, const Integer& b) { return b*a; }
inline Integer operator* (const Integer& a, int b) { return a*long(b); }
inline Integer operator* (int a, const Integer& b) { return b*long(a); }

inline Integer operator/ (const Integer& a, int b) { return a/long(b); }
inline Integer operator% (const Integer& a, int b) { return a%long(b); }

inline Integer operator<< (const Integer& a, unsigned int k)
{
   return a << static_cast<unsigned long>(k);
}

inline Integer operator>> (const Integer& a, unsigned int k)
{
   return a >> static_cast<unsigned long>(k);
}

inline Integer operator<< (const Integer& a, long k)
{
   if (k<0) return a >> static_cast<unsigned long>(-k);
   return a << static_cast<unsigned long>(k);
}

inline Integer operator>> (const Integer& a, long k)
{
   if (k<0) return a << static_cast<unsigned long>(-k);
   return a >> static_cast<unsigned long>(k);
}

inline Integer operator<< (const Integer& a, int k) { return a << long(k); }
inline Integer operator>> (const Integer& a, int k) { return a >> long(k); }

inline bool operator== (const Integer& a, const Integer& b) { return a.compare(b)==0; }
inline bool operator!= (const Integer& a, const Integer& b) { return a.compare(b)!=0; }
inline bool operator< (const Integer& a, const Integer& b) { return a.compare(b)<0; }
inline bool operator> (const Integer& a, const Integer& b) { return a.compare(b)>0; }
inline bool operator<= (const Integer& a, const Integer& b) { return a.compare(b)<=0; }
inline bool operator>= (const Integer& a, const Integer& b) { return a.compare(b)>=0; }

namespace GMP {
   inline bool operator== (const TempInteger& a, const TempInteger& b) { return mpz_cmp(&a,&b)==0; }
   inline bool operator!= (const TempInteger& a, const TempInteger& b) { return mpz_cmp(&a,&b)!=0; }
   inline bool operator< (const TempInteger& a, const TempInteger& b) { return mpz_cmp(&a,&b)<0; }
   inline bool operator> (const TempInteger& a, const TempInteger& b) { return mpz_cmp(&a,&b)>0; }
   inline bool operator<= (const TempInteger& a, const TempInteger& b) { return mpz_cmp(&a,&b)<=0; }
   inline bool operator>= (const TempInteger& a, const TempInteger& b) { return mpz_cmp(&a,&b)>=0; }
}

inline bool operator== (long a, const Integer& b) { return b==a; }
inline bool operator== (const Integer& a, int b) { return a==long(b); }
inline bool operator== (int a, const Integer& b) { return b==long(a); }
inline bool operator== (const Integer& a, double b) { return !a.compare(b); }
inline bool operator== (double a, const Integer& b) { return !b.compare(a); }
inline bool abs_equal(long a, const Integer& b) { return abs_equal(b,a); }
inline bool abs_equal(double a, const Integer& b) { return abs_equal(b,a); }
inline bool abs_equal(const Integer& a, int b) { return abs_equal(a,long(b)); }
inline bool abs_equal(int a, const Integer& b) { return abs_equal(b,long(a)); }

inline bool operator!= (const Integer& a, long b) { return !(a==b); }
inline bool operator!= (const Integer& a, int b) { return !(a==long(b)); }
inline bool operator!= (long a, const Integer& b) { return !(b==a); }
inline bool operator!= (int a, const Integer& b) { return !(b==long(a)); }
inline bool operator!= (const Integer& a, double b) { return !(a==b); }
inline bool operator!= (double a, const Integer& b) { return !(b==a); }

inline bool operator< (const Integer& a, int b) { return a<long(b); }
inline bool operator< (long a, const Integer& b) { return b>a; }
inline bool operator< (int a, const Integer& b) { return b>long(a); }
inline bool operator< (const Integer& a, double b) { return a.compare(b)<0; }
inline bool operator< (double a, const Integer& b) { return b.compare(a)>0; }

inline bool operator> (const Integer& a, int b) { return a>long(b); }
inline bool operator> (long a, const Integer& b) { return b<a; }
inline bool operator> (int a, const Integer& b) { return b<long(a); }
inline bool operator> (const Integer& a, double b) { return a.compare(b)>0; }
inline bool operator> (double a, const Integer& b) { return b.compare(a)<0; }

inline bool operator<= (const Integer& a, long b) { return !(a>b); }
inline bool operator<= (const Integer& a, int b) { return !(a>long(b)); }
inline bool operator<= (long a, const Integer& b) { return !(b<a); }
inline bool operator<= (int a, const Integer& b) { return !(b<long(a)); }
inline bool operator<= (const Integer& a, double b) { return !(a>b); }
inline bool operator<= (double a, const Integer& b) { return !(b<a); }

inline bool operator>= (const Integer& a, long b) { return !(a<b); }
inline bool operator>= (const Integer& a, int b) { return !(a<long(b)); }
inline bool operator>= (long a, const Integer& b) { return !(b>a); }
inline bool operator>= (int a, const Integer& b) { return !(b>long(a)); }
inline bool operator>= (const Integer& a, double b) { return !(a<b); }
inline bool operator>= (double a, const Integer& b) { return !(b>a); }

template <> inline
cmp_value sign(const Integer& a) { return sign(a.get_rep()->_mp_size); }

/// Returns a positive number if not equal to infinity, otherwise zero.
inline
int isfinite(const Integer& a)
{
   return a.rep[0]._mp_alloc;
}

/// Returns the sign if equal to +/-infinity, otherwise zero.
inline
int isinf(const Integer& a)
{
   return isfinite(a) ? 0 : a.rep[0]._mp_size;
}

inline
Integer gcd(const Integer& a, const Integer& b)
{
   const bool f1=isfinite(a), f2=isfinite(b);
   if (__builtin_expect(f1 && f2, 1))
      return Integer(mpz_gcd, a.rep, b.rep);
   if (f1) return a;
   return b;
}

inline
Integer gcd(const Integer& a, long b)
{
   if (__builtin_expect(isfinite(a),1))
      return Integer(mpz_gcd_ui, a.rep, (unsigned long)b);
   return b;
}

inline
Integer gcd(long a, const Integer& b) { return gcd(b,a); }

inline
Integer lcm(const Integer& a, const Integer& b)
{
   const bool f1=isfinite(a), f2=isfinite(b);
   if (__builtin_expect(f1 && f2, 1))
      return Integer(mpz_lcm, a.rep, b.rep);
   return Integer(maximal<Integer>(), 1);
}

inline
Integer lcm(const Integer& a, long b)
{
   if (__builtin_expect(isfinite(a),1))
      return Integer(mpz_lcm_ui, a.rep, (unsigned long)b);
   return Integer(maximal<Integer>(), 1);
}

inline
Integer lcm(long a, const Integer& b) { return lcm(b,a); }

inline
ExtGCD<Integer> ext_gcd(const Integer& a, const Integer& b)
{
   ExtGCD<Integer> res;
   const bool f1=isfinite(a), f2=isfinite(b);
   if (__builtin_expect(f1 && f2, 1)) {
      mpz_gcdext(res.g.rep, res.p.rep, res.q.rep, a.rep, b.rep);
      mpz_divexact(res.k1.rep, a.rep, res.g.rep);
      mpz_divexact(res.k2.rep, b.rep, res.g.rep);
   } else if (f1) {
      res.g=a; res.p=1; res.q=0; res.k1=1; res.k2=b;
   } else {
      res.g=b; res.p=0; res.q=1; res.k1=a; res.k2=1;
   }
   return res;
}

inline
Div<Integer> div(const Integer& a, const Integer& b)
{
   Div<Integer> res;
   const bool f1=isfinite(a), f2=isfinite(b);
   if (__builtin_expect(!b.non_zero(), 0))
      throw GMP::ZeroDivide();
   if (__builtin_expect(f1 && f2, 1))
      mpz_tdiv_qr(res.quot.rep, res.rem.rep, a.rep, b.rep);
   else if (f1)
      throw GMP::NaN();
   else if (f2)
      Integer::_set_inf(res.quot.rep, b.rep), res.rem=0;
   else
      throw GMP::NaN();
   return res;
}

inline
Div<Integer> div(const Integer& a, long b)
{
   Div<Integer> res;
   const bool f1=isfinite(a);
   if (__builtin_expect(b==0, 0))
      throw GMP::ZeroDivide();
   if (__builtin_expect(f1, 1))
      mpz_tdiv_qr_ui(res.quot.rep, res.rem.rep, a.rep, b);
   else
      Integer::_set_inf(res.quot.rep, b), res.rem=0;
   return res;
}

inline
Integer div_exact(const Integer& a, const Integer& b)
{
   if (__builtin_expect(isfinite(a), 1)) {
      if (__builtin_expect(!b.non_zero(), 0)) return a;     // allow 0/0
      return Integer(mpz_divexact, a.rep, b.rep);
   }
   return Integer(maximal<Integer>(), mpz_sgn(a.rep)*mpz_sgn(b.rep));
}

inline
Integer div_exact(const Integer& a, long b)
{
   if (__builtin_expect(isfinite(a), 1)) {
      if (__builtin_expect(!b,0)) return a;     // allow 0/0
      if (b>0)
         return Integer(mpz_divexact_ui, a.rep, (unsigned long)b);
      mpz_t minus_a;
      return Integer(mpz_divexact_ui, a._tmp_negate(minus_a,a.rep), (unsigned long)(-b));
   }
   return Integer(maximal<Integer>(), mpz_sgn(a.rep)*pm::sign(b));
}

/// Logarithm (rounded down).
inline
int log2_floor(const Integer& a)
{
   if (__builtin_expect(!isfinite(a), 0)) throw GMP::NaN();
   int n=mpz_size(a.get_rep())-1;
   return n>=0 ? n*mp_bits_per_limb + log2_floor(mpz_getlimbn(a.get_rep(),n)) : 0;
}

/// Logarithm (rounded up). 
inline
int log2_ceil(const Integer& a)
{
   if (__builtin_expect(!isfinite(a),0)) throw GMP::NaN();
   int n=mpz_size(a.get_rep())-1;
   return n>=0 ? n*mp_bits_per_limb + log2_ceil(mpz_getlimbn(a.get_rep(),n)) : 0;
}

namespace operations {

template <>
struct divexact_scalar<Integer,Integer,Integer> {
   typedef Integer first_argument_type;
   typedef Integer second_argument_type;
   typedef const Integer result_type;

   result_type operator() (const Integer& a, const Integer& b) const { return div_exact(a,b); }
   void assign(Integer& a, const Integer& b) const { a.div_exact(b); }
};

template <>
struct divexact_scalar<Integer,long,Integer> {
   typedef Integer first_argument_type;
   typedef long second_argument_type;
   typedef const Integer result_type;

   result_type operator() (const Integer& a, long b) const { return div_exact(a,b); }
   void assign(Integer& a, long b) const { a.div_exact(b); }
};

template <>
struct divexact_scalar<Integer,int,Integer> : divexact_scalar<Integer,long,Integer> {
   typedef int second_argument_type;
};

template <typename T>
struct cmp_GMP_based : cmp_extremal, cmp_partial_scalar {

   typedef T first_argument_type;
   typedef T second_argument_type;
   typedef cmp_value result_type;

   using cmp_extremal::operator();
   using cmp_partial_scalar::operator();

   cmp_value operator() (const T& a, const T& b) const
   {
      return sign(a.compare(b));
   }

   template <typename T2>
   typename enable_if<cmp_value, (pass_by_reference<T2>::value && list_contains<typename T::comparable_with, T2>::value)>::type
   operator() (const T& a, const T2& b) const
   {
      return sign(a.compare(b));
   }

   template <typename T2>
   typename enable_if<cmp_value, (pass_by_value<T2>::value && list_contains<typename T::comparable_with, T2>::value)>::type
   operator() (const T& a, const T2 b) const
   {
      return sign(a.compare(b));
   }

   template <typename T2>
   typename enable_if<cmp_value, (!list_contains<typename T::comparable_with, T2>::value && list_contains<typename T2::comparable_with, T>::value)>::type
   operator() (const T& a, const T2& b) const
   {
      return sign(-b.compare(a));
   }

   template <typename T2>
   typename enable_if<cmp_value, (pass_by_reference<T2>::value && list_contains<typename T::comparable_with, T2>::value)>::type
   operator() (const T2& a, const T& b) const
   {
      return sign(-b.compare(a));
   }

   template <typename T2>
   typename enable_if<cmp_value, (pass_by_value<T2>::value && list_contains<typename T::comparable_with, T2>::value)>::type
   operator() (const T2 a, const T& b) const
   {
      return sign(-b.compare(a));
   }

   template <typename T2>
   typename enable_if<cmp_value, (!list_contains<typename T::comparable_with, T2>::value && list_contains<typename T2::comparable_with, T>::value)>::type
   operator() (const T2& a, const T& b) const
   {
      return sign(a.compare(b));
   }
};

template <>
struct cmp_scalar<Integer, Integer, true> : cmp_GMP_based<Integer> {};

} // end namespace operations

template <>
class conv<Integer, int> {
public:
   typedef Integer argument_type;
   typedef int result_type;
   result_type operator() (const Integer& a) const { return a.to_int(); }
};

template <>
class conv<Integer, long> {
public:
   typedef Integer argument_type;
   typedef long result_type;
   result_type operator() (const Integer& a) const { return a.to_long(); }
};

template <>
class conv<Integer, double> {
public:
   typedef Integer argument_type;
   typedef double result_type;
   result_type operator() (const Integer& a) const { return a.to_double(); }
};

template <>
class conv<Integer, std::string> {
public:
   typedef Integer argument_type;
   typedef std::string result_type;
   result_type operator() (const Integer& a) const { return a.to_string(); }
};

template <>
struct spec_object_traits<GMP::TempInteger> : spec_object_traits<is_scalar> {};

template <>
class conv<GMP::TempInteger, Integer> : conv_by_cast<GMP::TempInteger&, Integer> {};

template <>
struct spec_object_traits<Integer> : spec_object_traits<is_scalar> {
   static
   bool is_zero(const Integer& a)
   {
      return !a.non_zero();
   }

   static
   bool is_one(const Integer& a)
   {
      return a==1L;
   }

   static const Integer& zero();
   static const Integer& one();
};

template <> struct hash_func<MP_INT, is_opaque> {
protected:
   size_t _do(mpz_srcptr a) const
   {
      size_t result=0;
      for (int i=0, n=mpz_size(a); i<n; ++i)
         (result <<= 1) ^= mpz_getlimbn(a, i);
      return result;
   }
public:
   size_t operator() (const MP_INT& a) const { return _do(&a); }
};

template <>
struct hash_func<Integer, is_scalar> : hash_func<MP_INT>
{
   size_t operator() (const Integer& a) const
   {
      return __builtin_expect(isfinite(a), 1) ? _do(a.get_rep()) : 0;
   }
};

template <>
struct hash_func<GMP::TempInteger, is_scalar> : hash_func<MP_INT>
{
   size_t operator() (const GMP::TempInteger& a) const { return hash_func<MP_INT>::operator()(reinterpret_cast<const MP_INT&>(a)); }
};

namespace GMP {

inline
std::ostream& operator<< (std::ostream& os, const TempInteger& a)
{
   return os << reinterpret_cast<const Integer&>(a);
}

}
}

namespace polymake {
   using pm::Integer;
}

#endif // POLYMAKE_INTEGER_H

// Local Variables:
// mode:C++
// c-basic-offset:3
// indent-tabs-mode:nil
// End: