| [00e80b6] | 1 | // | 
|---|
| [4c8f86b3] | 2 | // Cforall Version 1.0.0 Copyright (C) 2016 University of Waterloo | 
|---|
|  | 3 | // | 
|---|
|  | 4 | // The contents of this file are covered under the licence agreement in the | 
|---|
|  | 5 | // file "LICENCE" distributed with Cforall. | 
|---|
| [00e80b6] | 6 | // | 
|---|
|  | 7 | // gmp -- | 
|---|
|  | 8 | // | 
|---|
| [4c8f86b3] | 9 | // Author           : Peter A. Buhr | 
|---|
|  | 10 | // Created On       : Tue Apr 19 08:43:43 2016 | 
|---|
|  | 11 | // Last Modified By : Peter A. Buhr | 
|---|
| [53a6c2a] | 12 | // Last Modified On : Fri Jul  7 09:33:20 2017 | 
|---|
|  | 13 | // Update Count     : 15 | 
|---|
| [00e80b6] | 14 | // | 
|---|
| [4c8f86b3] | 15 |  | 
|---|
|  | 16 | // https://gmplib.org/gmp-man-6.1.1.pdf | 
|---|
| [2893f6d] | 17 |  | 
|---|
| [53a6c2a] | 18 | #pragma once | 
|---|
|  | 19 |  | 
|---|
| [4c8f86b3] | 20 | #include <gmp.h>                                                                                // GNU multi-precise integers | 
|---|
|  | 21 | #include <fstream>                                                                              // sout | 
|---|
|  | 22 |  | 
|---|
|  | 23 | struct Int { mpz_t mpz; };                                                              // wrap GMP implementation | 
|---|
|  | 24 |  | 
|---|
|  | 25 | // constructor | 
|---|
| [00e80b6] | 26 | static inline void ?{}( Int & this ) { mpz_init( this.mpz ); } | 
|---|
|  | 27 | static inline void ?{}( Int & this, Int init ) { mpz_init_set( this.mpz, init.mpz ); } | 
|---|
|  | 28 | static inline void ?{}( Int & this, zero_t ) { mpz_init_set_si( this.mpz, 0 ); } | 
|---|
|  | 29 | static inline void ?{}( Int & this, one_t ) { mpz_init_set_si( this.mpz, 1 ); } | 
|---|
|  | 30 | static inline void ?{}( Int & this, signed long int init ) { mpz_init_set_si( this.mpz, init ); } | 
|---|
|  | 31 | static inline void ?{}( Int & this, unsigned long int init ) { mpz_init_set_ui( this.mpz, init ); } | 
|---|
|  | 32 | static inline void ?{}( Int & this, const char * val ) { if ( mpz_init_set_str( this.mpz, val, 0 ) ) abort(); } | 
|---|
|  | 33 | static inline void ^?{}( Int & this ) { mpz_clear( this.mpz ); } | 
|---|
| [4c8f86b3] | 34 |  | 
|---|
|  | 35 | // assignment | 
|---|
| [00e80b6] | 36 | static inline Int ?=?( Int & lhs, Int rhs ) { mpz_set( lhs.mpz, rhs.mpz ); return lhs; } | 
|---|
|  | 37 | static inline Int ?=?( Int & lhs, long int rhs ) { mpz_set_si( lhs.mpz, rhs ); return lhs; } | 
|---|
|  | 38 | static inline Int ?=?( Int & lhs, unsigned long int rhs ) { mpz_set_ui( lhs.mpz, rhs ); return lhs; } | 
|---|
|  | 39 | static inline Int ?=?( Int & lhs, const char * rhs ) { if ( mpz_set_str( lhs.mpz, rhs, 0 ) ) { printf( "invalid string conversion\n" ); abort(); } return lhs; } | 
|---|
|  | 40 |  | 
|---|
|  | 41 | static inline char ?=?( char & lhs, Int rhs ) { char val = mpz_get_si( rhs.mpz ); lhs = val; return lhs; } | 
|---|
|  | 42 | static inline short int ?=?( short int & lhs, Int rhs ) { short int val = mpz_get_si( rhs.mpz ); lhs = val; return lhs; } | 
|---|
|  | 43 | static inline int ?=?( int & lhs, Int rhs ) { int val = mpz_get_si( rhs.mpz ); lhs = val; return lhs; } | 
|---|
|  | 44 | static inline long int ?=?( long int & lhs, Int rhs ) { long int val = mpz_get_si( rhs.mpz ); lhs = val; return lhs; } | 
|---|
|  | 45 | static inline unsigned char ?=?( unsigned char & lhs, Int rhs ) { unsigned char val = mpz_get_ui( rhs.mpz ); lhs = val; return lhs; } | 
|---|
|  | 46 | static inline unsigned short int ?=?( unsigned short int & lhs, Int rhs ) { unsigned short int val = mpz_get_ui( rhs.mpz ); lhs = val; return lhs; } | 
|---|
|  | 47 | static inline unsigned int ?=?( unsigned int & lhs, Int rhs ) { unsigned int val = mpz_get_ui( rhs.mpz ); lhs = val; return lhs; } | 
|---|
|  | 48 | static inline unsigned long int ?=?( unsigned long int & lhs, Int rhs ) { unsigned long int val = mpz_get_ui( rhs.mpz ); lhs = val; return lhs; } | 
|---|
| [4c8f86b3] | 49 |  | 
|---|
|  | 50 | // conversions | 
|---|
| [8f61052c] | 51 | static inline long int narrow( Int val ) { return mpz_get_si( val.mpz ); } | 
|---|
|  | 52 | static inline unsigned long int narrow( Int val ) { return mpz_get_ui( val.mpz ); } | 
|---|
| [4c8f86b3] | 53 |  | 
|---|
|  | 54 | // comparison | 
|---|
| [8f61052c] | 55 | static inline int ?==?( Int oper1, Int oper2 ) { return mpz_cmp( oper1.mpz, oper2.mpz ) == 0; } | 
|---|
|  | 56 | static inline int ?==?( Int oper1, long int oper2 ) { return mpz_cmp_si( oper1.mpz, oper2 ) == 0; } | 
|---|
|  | 57 | static inline int ?==?( long int oper2, Int oper1 ) { return mpz_cmp_si( oper1.mpz, oper2 ) == 0; } | 
|---|
|  | 58 | static inline int ?==?( Int oper1, unsigned long int oper2 ) { return mpz_cmp_ui( oper1.mpz, oper2 ) == 0; } | 
|---|
|  | 59 | static inline int ?==?( unsigned long int oper2, Int oper1 ) { return mpz_cmp_ui( oper1.mpz, oper2 ) == 0; } | 
|---|
| [4c8f86b3] | 60 |  | 
|---|
| [8f61052c] | 61 | static inline int ?!=?( Int oper1, Int oper2 ) { return ! ( oper1 == oper2 ); } | 
|---|
|  | 62 | static inline int ?!=?( Int oper1, long int oper2 ) { return ! ( oper1 == oper2 ); } | 
|---|
|  | 63 | static inline int ?!=?( long int oper1, Int oper2 ) { return ! ( oper1 == oper2 ); } | 
|---|
|  | 64 | static inline int ?!=?( Int oper1, unsigned long int oper2 ) { return ! ( oper1 == oper2 ); } | 
|---|
|  | 65 | static inline int ?!=?( unsigned long int oper1, Int oper2 ) { return ! ( oper1 == oper2 ); } | 
|---|
| [4c8f86b3] | 66 |  | 
|---|
| [8f61052c] | 67 | static inline int ?<?( Int oper1, Int oper2 ) { return mpz_cmp( oper1.mpz, oper2.mpz ) < 0; } | 
|---|
|  | 68 | static inline int ?<?( Int oper1, long int oper2 ) { return mpz_cmp_si( oper1.mpz, oper2 ) < 0; } | 
|---|
|  | 69 | static inline int ?<?( long int oper2, Int oper1 ) { return mpz_cmp_si( oper1.mpz, oper2 ) < 0; } | 
|---|
|  | 70 | static inline int ?<?( Int oper1, unsigned long int oper2 ) { return mpz_cmp_ui( oper1.mpz, oper2 ) < 0; } | 
|---|
|  | 71 | static inline int ?<?( unsigned long int oper2, Int oper1 ) { return mpz_cmp_ui( oper1.mpz, oper2 ) < 0; } | 
|---|
| [4c8f86b3] | 72 |  | 
|---|
| [8f61052c] | 73 | static inline int ?<=?( Int oper1, Int oper2 ) { return mpz_cmp( oper1.mpz, oper2.mpz ) <= 0; } | 
|---|
|  | 74 | static inline int ?<=?( Int oper1, long int oper2 ) { return mpz_cmp_si( oper1.mpz, oper2 ) <= 0; } | 
|---|
|  | 75 | static inline int ?<=?( long int oper2, Int oper1 ) { return mpz_cmp_si( oper1.mpz, oper2 ) <= 0; } | 
|---|
|  | 76 | static inline int ?<=?( Int oper1, unsigned long int oper2 ) { return mpz_cmp_ui( oper1.mpz, oper2 ) <= 0; } | 
|---|
|  | 77 | static inline int ?<=?( unsigned long int oper2, Int oper1 ) { return mpz_cmp_ui( oper1.mpz, oper2 ) <= 0; } | 
|---|
| [4c8f86b3] | 78 |  | 
|---|
| [8f61052c] | 79 | static inline int ?>?( Int oper1, Int oper2 ) { return ! ( oper1 <= oper2 ); } | 
|---|
|  | 80 | static inline int ?>?( Int oper1, long int oper2 ) { return ! ( oper1 <= oper2 ); } | 
|---|
|  | 81 | static inline int ?>?( long int oper1, Int oper2 ) { return ! ( oper1 <= oper2 ); } | 
|---|
|  | 82 | static inline int ?>?( Int oper1, unsigned long int oper2 ) { return ! ( oper1 <= oper2 ); } | 
|---|
|  | 83 | static inline int ?>?( unsigned long int oper1, Int oper2 ) { return ! ( oper1 <= oper2 ); } | 
|---|
| [4c8f86b3] | 84 |  | 
|---|
| [8f61052c] | 85 | static inline int ?>=?( Int oper1, Int oper2 ) { return ! ( oper1 < oper2 ); } | 
|---|
|  | 86 | static inline int ?>=?( Int oper1, long int oper2 ) { return ! ( oper1 < oper2 ); } | 
|---|
|  | 87 | static inline int ?>=?( long int oper1, Int oper2 ) { return ! ( oper1 < oper2 ); } | 
|---|
|  | 88 | static inline int ?>=?( Int oper1, unsigned long int oper2 ) { return ! ( oper1 < oper2 ); } | 
|---|
|  | 89 | static inline int ?>=?( unsigned long int oper1, Int oper2 ) { return ! ( oper1 < oper2 ); } | 
|---|
| [4c8f86b3] | 90 |  | 
|---|
|  | 91 | // arithmetic | 
|---|
| [8f61052c] | 92 | static inline Int +?( Int oper ) { Int pos; mpz_set( pos.mpz, oper.mpz ); return pos; } | 
|---|
|  | 93 | static inline Int -?( Int oper ) { Int neg; mpz_neg( neg.mpz, oper.mpz ); return neg; } | 
|---|
|  | 94 | static inline Int ~?( Int oper ) { Int comp; mpz_com( comp.mpz, oper.mpz ); return comp; } | 
|---|
| [4c8f86b3] | 95 |  | 
|---|
| [8f61052c] | 96 | static inline Int ?&?( Int oper1, Int oper2 ) { Int conjunction; mpz_and( conjunction.mpz, oper1.mpz, oper2.mpz ); return conjunction; } | 
|---|
|  | 97 | static inline Int ?&?( Int oper1, long int oper2 ) { Int conjunction, temp; mpz_set_si( temp.mpz, oper2 ); mpz_and( conjunction.mpz, oper1.mpz, temp.mpz ); return conjunction; } | 
|---|
|  | 98 | static inline Int ?&?( long int oper1, Int oper2 ) { Int conjunction, temp; mpz_set_si( temp.mpz, oper1 ); mpz_and( conjunction.mpz, temp.mpz, oper2.mpz ); return conjunction; } | 
|---|
|  | 99 | static inline Int ?&?( Int oper1, unsigned long int oper2 ) { Int conjunction, temp; mpz_set_ui( temp.mpz, oper2 ); mpz_and( conjunction.mpz, oper1.mpz, temp.mpz ); return conjunction; } | 
|---|
|  | 100 | static inline Int ?&?( unsigned long int oper1, Int oper2 ) { Int conjunction, temp; mpz_set_ui( temp.mpz, oper1 ); mpz_and( conjunction.mpz, temp.mpz, oper2.mpz ); return conjunction; } | 
|---|
| [00e80b6] | 101 | static inline Int ?&=?( Int & lhs, Int rhs ) { return lhs = lhs & rhs; } | 
|---|
| [4c8f86b3] | 102 |  | 
|---|
| [8f61052c] | 103 | static inline Int ?|?( Int oper1, Int oper2 ) { Int disjunction; mpz_ior( disjunction.mpz, oper1.mpz, oper2.mpz ); return disjunction; } | 
|---|
|  | 104 | static inline Int ?|?( Int oper1, long int oper2 ) { Int disjunction, temp; mpz_set_si( temp.mpz, oper2 ); mpz_ior( disjunction.mpz, oper1.mpz, temp.mpz ); return disjunction; } | 
|---|
|  | 105 | static inline Int ?|?( long int oper1, Int oper2 ) { Int disjunction, temp; mpz_set_si( temp.mpz, oper1 ); mpz_ior( disjunction.mpz, temp.mpz, oper2.mpz ); return disjunction; } | 
|---|
|  | 106 | static inline Int ?|?( Int oper1, unsigned long int oper2 ) { Int disjunction, temp; mpz_set_ui( temp.mpz, oper2 ); mpz_ior( disjunction.mpz, oper1.mpz, temp.mpz ); return disjunction; } | 
|---|
|  | 107 | static inline Int ?|?( unsigned long int oper1, Int oper2 ) { Int disjunction, temp; mpz_set_ui( temp.mpz, oper1 ); mpz_ior( disjunction.mpz, temp.mpz, oper2.mpz ); return disjunction; } | 
|---|
| [00e80b6] | 108 | static inline Int ?|=?( Int & lhs, Int rhs ) { return lhs = lhs | rhs; } | 
|---|
| [4c8f86b3] | 109 |  | 
|---|
| [8f61052c] | 110 | static inline Int ?^?( Int oper1, Int oper2 ) { Int disjunction; mpz_xor( disjunction.mpz, oper1.mpz, oper2.mpz ); return disjunction; } | 
|---|
|  | 111 | static inline Int ?^?( Int oper1, long int oper2 ) { Int disjunction, temp; mpz_set_si( temp.mpz, oper2 ); mpz_ior( disjunction.mpz, oper1.mpz, temp.mpz ); return disjunction; } | 
|---|
|  | 112 | static inline Int ?^?( long int oper1, Int oper2 ) { Int disjunction, temp; mpz_set_si( temp.mpz, oper1 ); mpz_ior( disjunction.mpz, temp.mpz, oper2.mpz ); return disjunction; } | 
|---|
|  | 113 | static inline Int ?^?( Int oper1, unsigned long int oper2 ) { Int disjunction, temp; mpz_set_ui( temp.mpz, oper2 ); mpz_ior( disjunction.mpz, oper1.mpz, temp.mpz ); return disjunction; } | 
|---|
|  | 114 | static inline Int ?^?( unsigned long int oper1, Int oper2 ) { Int disjunction, temp; mpz_set_ui( temp.mpz, oper1 ); mpz_ior( disjunction.mpz, temp.mpz, oper2.mpz ); return disjunction; } | 
|---|
| [00e80b6] | 115 | static inline Int ?^=?( Int & lhs, Int rhs ) { return lhs = lhs ^ rhs; } | 
|---|
| [4c8f86b3] | 116 |  | 
|---|
| [8f61052c] | 117 | static inline Int ?+?( Int addend1, Int addend2 ) { Int sum; mpz_add( sum.mpz, addend1.mpz, addend2.mpz ); return sum; } | 
|---|
|  | 118 | static inline Int ?+?( Int addend1, long int addend2 ) { Int sum; if ( addend2 >= 0 ) mpz_add_ui( sum.mpz, addend1.mpz, addend2 ); else mpz_sub_ui( sum.mpz, addend1.mpz, -addend2 ); return sum; } | 
|---|
|  | 119 | static inline Int ?+?( long int addend2, Int addend1 ) { Int sum; if ( addend2 >= 0 ) mpz_add_ui( sum.mpz, addend1.mpz, addend2 ); else mpz_sub_ui( sum.mpz, addend1.mpz, -addend2 ); return sum; } | 
|---|
|  | 120 | static inline Int ?+?( Int addend1, unsigned long int addend2 ) { Int sum; mpz_add_ui( sum.mpz, addend1.mpz, addend2 ); return sum; } | 
|---|
|  | 121 | static inline Int ?+?( unsigned long int addend2, Int addend1 ) { Int sum; mpz_add_ui( sum.mpz, addend1.mpz, addend2 ); return sum; } | 
|---|
| [00e80b6] | 122 | static inline Int ?+=?( Int & lhs, Int rhs ) { return lhs = lhs + rhs; } | 
|---|
|  | 123 | static inline Int ?+=?( Int & lhs, long int rhs ) { return lhs = lhs + rhs; } | 
|---|
|  | 124 | static inline Int ?+=?( Int & lhs, unsigned long int rhs ) { return lhs = lhs + rhs; } | 
|---|
|  | 125 | static inline Int ++?( Int & lhs ) { return lhs += 1; } | 
|---|
|  | 126 | static inline Int ?++( Int & lhs ) { Int ret = lhs; lhs += 1; return ret; } | 
|---|
| [4c8f86b3] | 127 |  | 
|---|
| [8f61052c] | 128 | static inline Int ?-?( Int minuend, Int subtrahend ) { Int diff; mpz_sub( diff.mpz, minuend.mpz, subtrahend.mpz ); return diff; } | 
|---|
|  | 129 | static inline Int ?-?( Int minuend, long int subtrahend ) { Int diff; if ( subtrahend >= 0 ) mpz_sub_ui( diff.mpz, minuend.mpz, subtrahend ); else mpz_add_ui( diff.mpz, minuend.mpz, -subtrahend ); return diff; } | 
|---|
|  | 130 | static inline Int ?-?( long int minuend, Int subtrahend ) { Int diff; if ( subtrahend >= 0 ) mpz_ui_sub( diff.mpz, minuend, subtrahend.mpz ); else { mpz_add_ui( diff.mpz, subtrahend.mpz, -minuend ); mpz_neg( diff.mpz, diff.mpz ); } return diff; } | 
|---|
|  | 131 | static inline Int ?-?( Int minuend, unsigned long int subtrahend ) { Int diff; mpz_sub_ui( diff.mpz, minuend.mpz, subtrahend ); return diff; } | 
|---|
|  | 132 | static inline Int ?-?( unsigned long int minuend, Int subtrahend ) { Int diff; mpz_ui_sub( diff.mpz, minuend, subtrahend.mpz ); return diff; } | 
|---|
| [00e80b6] | 133 | static inline Int ?-=?( Int & lhs, Int rhs ) { return lhs = lhs - rhs; } | 
|---|
|  | 134 | static inline Int ?-=?( Int & lhs, long int rhs ) { return lhs = lhs - rhs; } | 
|---|
|  | 135 | static inline Int ?-=?( Int & lhs, unsigned long int rhs ) { return lhs = lhs - rhs; } | 
|---|
|  | 136 | static inline Int --?( Int & lhs ) { return lhs -= 1; } | 
|---|
|  | 137 | static inline Int ?--( Int & lhs ) { Int ret = lhs; lhs -= 1; return ret; } | 
|---|
| [4c8f86b3] | 138 |  | 
|---|
| [8f61052c] | 139 | static inline Int ?*?( Int multiplicator, Int multiplicand ) { Int product; mpz_mul( product.mpz, multiplicator.mpz, multiplicand.mpz ); return product; } | 
|---|
|  | 140 | static inline Int ?*?( Int multiplicator, long int multiplicand ) { Int product; mpz_mul_si( product.mpz, multiplicator.mpz, multiplicand ); return product; } | 
|---|
|  | 141 | static inline Int ?*?( long int multiplicand, Int multiplicator ) { Int product; mpz_mul_si( product.mpz, multiplicator.mpz, multiplicand ); return product; } | 
|---|
|  | 142 | static inline Int ?*?( Int multiplicator, unsigned long int multiplicand ) { Int product; mpz_mul_ui( product.mpz, multiplicator.mpz, multiplicand ); return product; } | 
|---|
|  | 143 | static inline Int ?*?( unsigned long int multiplicand, Int multiplicator ) { Int product; mpz_mul_ui( product.mpz, multiplicator.mpz, multiplicand ); return product; } | 
|---|
| [00e80b6] | 144 | static inline Int ?*=?( Int & lhs, Int rhs ) { return lhs = lhs * rhs; } | 
|---|
|  | 145 | static inline Int ?*=?( Int & lhs, long int rhs ) { return lhs = lhs * rhs; } | 
|---|
|  | 146 | static inline Int ?*=?( Int & lhs, unsigned long int rhs ) { return lhs = lhs * rhs; } | 
|---|
| [4c8f86b3] | 147 |  | 
|---|
| [2893f6d] | 148 | // some code for operators "/" and "%" taken from g++ gmpxx.h | 
|---|
| [8f61052c] | 149 | static inline Int ?/?( Int dividend, Int divisor ) { Int quotient; mpz_tdiv_q( quotient.mpz, dividend.mpz, divisor.mpz ); return quotient; } | 
|---|
|  | 150 | static inline Int ?/?( Int dividend, unsigned long int divisor ) { Int quotient; mpz_tdiv_q_ui( quotient.mpz, dividend.mpz, divisor ); return quotient; } | 
|---|
|  | 151 | static inline Int ?/?( unsigned long int dividend, Int divisor ) { | 
|---|
| [4c8f86b3] | 152 | Int quotient; | 
|---|
|  | 153 | if ( mpz_sgn( divisor.mpz ) >= 0 ) { | 
|---|
|  | 154 | if ( mpz_fits_ulong_p( divisor.mpz ) ) | 
|---|
|  | 155 | mpz_set_ui( quotient.mpz, dividend / mpz_get_ui( divisor.mpz ) ); | 
|---|
|  | 156 | else | 
|---|
|  | 157 | mpz_set_ui( quotient.mpz, 0 ); | 
|---|
|  | 158 | } else { | 
|---|
|  | 159 | mpz_neg( quotient.mpz, divisor.mpz ); | 
|---|
|  | 160 | if ( mpz_fits_ulong_p( quotient.mpz ) ) { | 
|---|
|  | 161 | mpz_set_ui( quotient.mpz, dividend / mpz_get_ui( quotient.mpz ) ); | 
|---|
|  | 162 | mpz_neg( quotient.mpz, quotient.mpz ); | 
|---|
|  | 163 | } else | 
|---|
|  | 164 | mpz_set_ui( quotient.mpz, 0 ); | 
|---|
|  | 165 | } // if | 
|---|
|  | 166 | return quotient; | 
|---|
|  | 167 | } // ?/? | 
|---|
| [8f61052c] | 168 | static inline Int ?/?( Int dividend, long int divisor ) { | 
|---|
| [4c8f86b3] | 169 | Int quotient; | 
|---|
|  | 170 | if ( divisor >= 0 ) | 
|---|
|  | 171 | mpz_tdiv_q_ui( quotient.mpz, dividend.mpz, divisor ); | 
|---|
|  | 172 | else { | 
|---|
|  | 173 | mpz_tdiv_q_ui( quotient.mpz, dividend.mpz, -divisor ); | 
|---|
|  | 174 | mpz_neg( quotient.mpz, quotient.mpz ); | 
|---|
|  | 175 | } // if | 
|---|
|  | 176 | return quotient; | 
|---|
|  | 177 | } // ?/? | 
|---|
| [8f61052c] | 178 | static inline Int ?/?( long int dividend, Int divisor ) { | 
|---|
| [4c8f86b3] | 179 | Int quotient; | 
|---|
|  | 180 | if ( mpz_fits_slong_p( divisor.mpz ) ) | 
|---|
|  | 181 | mpz_set_si( quotient.mpz, dividend / mpz_get_si( divisor.mpz ) ); | 
|---|
|  | 182 | else { | 
|---|
|  | 183 | // if divisor is bigger than a long then the quotient must be zero, unless dividend==LONG_MIN and | 
|---|
|  | 184 | // dividend==-LONG_MIN in which case the quotient is -1 | 
|---|
|  | 185 | mpz_set_si( quotient.mpz, mpz_cmpabs_ui( divisor.mpz, (dividend >= 0 ? dividend : -dividend)) == 0 ? -1 : 0 ); | 
|---|
|  | 186 | } // if | 
|---|
|  | 187 | return quotient; | 
|---|
|  | 188 | } // ?/? | 
|---|
| [00e80b6] | 189 | static inline Int ?/=?( Int & lhs, Int rhs ) { return lhs = lhs / rhs; } | 
|---|
|  | 190 | static inline Int ?/=?( Int & lhs, long int rhs ) { return lhs = lhs / rhs; } | 
|---|
|  | 191 | static inline Int ?/=?( Int & lhs, unsigned long int rhs ) { return lhs = lhs / rhs; } | 
|---|
| [4c8f86b3] | 192 |  | 
|---|
| [8f61052c] | 193 | static inline [ Int, Int ] div( Int dividend, Int divisor ) { Int quotient, remainder; mpz_fdiv_qr( quotient.mpz, remainder.mpz, dividend.mpz, divisor.mpz ); return [ quotient, remainder ]; } | 
|---|
|  | 194 | static inline [ Int, Int ] div( Int dividend, unsigned long int divisor ) { Int quotient, remainder; mpz_fdiv_qr_ui( quotient.mpz, remainder.mpz, dividend.mpz, divisor ); return [ quotient, remainder ]; } | 
|---|
| [4c8f86b3] | 195 |  | 
|---|
| [8f61052c] | 196 | static inline Int ?%?( Int dividend, Int divisor ) { Int remainder; mpz_tdiv_r( remainder.mpz, dividend.mpz, divisor.mpz ); return remainder; } | 
|---|
|  | 197 | static inline Int ?%?( Int dividend, unsigned long int divisor ) { Int remainder; mpz_tdiv_r_ui( remainder.mpz, dividend.mpz, divisor ); return remainder; } | 
|---|
|  | 198 | static inline Int ?%?( unsigned long int dividend, Int divisor ) { | 
|---|
| [4c8f86b3] | 199 | Int remainder; | 
|---|
|  | 200 | if ( mpz_sgn( divisor.mpz ) >= 0 ) { | 
|---|
|  | 201 | if ( mpz_fits_ulong_p( divisor.mpz ) ) | 
|---|
|  | 202 | mpz_set_ui( remainder.mpz, dividend % mpz_get_ui( divisor.mpz ) ); | 
|---|
|  | 203 | else | 
|---|
|  | 204 | mpz_set_ui( remainder.mpz, dividend ); | 
|---|
|  | 205 | } else { | 
|---|
|  | 206 | mpz_neg( remainder.mpz, divisor.mpz ); | 
|---|
|  | 207 | if ( mpz_fits_ulong_p( remainder.mpz ) ) | 
|---|
|  | 208 | mpz_set_ui( remainder.mpz, dividend % mpz_get_ui( remainder.mpz ) ); | 
|---|
|  | 209 | else | 
|---|
|  | 210 | mpz_set_ui( remainder.mpz, dividend ); | 
|---|
|  | 211 | } // if | 
|---|
|  | 212 | return remainder; | 
|---|
|  | 213 | } // ?%? | 
|---|
| [8f61052c] | 214 | static inline Int ?%?( Int dividend, long int divisor ) { | 
|---|
| [4c8f86b3] | 215 | Int remainder; | 
|---|
|  | 216 | mpz_tdiv_r_ui( remainder.mpz, dividend.mpz, (divisor >= 0 ? divisor : -divisor)); | 
|---|
|  | 217 | return remainder; | 
|---|
|  | 218 | } // ?%? | 
|---|
| [8f61052c] | 219 | static inline Int ?%?( long int dividend, Int divisor ) { | 
|---|
| [4c8f86b3] | 220 | Int remainder; | 
|---|
|  | 221 | if ( mpz_fits_slong_p( divisor.mpz ) ) | 
|---|
|  | 222 | mpz_set_si( remainder.mpz, dividend % mpz_get_si( divisor.mpz ) ); | 
|---|
|  | 223 | else { | 
|---|
|  | 224 | // if divisor is bigger than a long then the remainder is dividend unchanged, unless dividend==LONG_MIN and | 
|---|
|  | 225 | // dividend==-LONG_MIN in which case the remainder is 0 | 
|---|
|  | 226 | mpz_set_si( remainder.mpz, mpz_cmpabs_ui( divisor.mpz, (dividend >= 0 ? dividend : -dividend)) == 0 ? 0 : dividend); | 
|---|
|  | 227 | } // if | 
|---|
|  | 228 | return remainder; | 
|---|
|  | 229 | } // ?%? | 
|---|
| [00e80b6] | 230 | static inline Int ?%=?( Int & lhs, Int rhs ) { return lhs = lhs % rhs; } | 
|---|
|  | 231 | static inline Int ?%=?( Int & lhs, long int rhs ) { return lhs = lhs % rhs; } | 
|---|
|  | 232 | static inline Int ?%=?( Int & lhs, unsigned long int rhs ) { return lhs = lhs % rhs; } | 
|---|
| [4c8f86b3] | 233 |  | 
|---|
| [8f61052c] | 234 | static inline Int ?<<?( Int shiften, mp_bitcnt_t shift ) { Int shifted; mpz_mul_2exp( shifted.mpz, shiften.mpz, shift ); return shifted; } | 
|---|
| [00e80b6] | 235 | static inline Int ?<<=?( Int & lhs, mp_bitcnt_t shift ) { return lhs = lhs << shift; } | 
|---|
| [8f61052c] | 236 | static inline Int ?>>?( Int shiften, mp_bitcnt_t shift ) { Int shifted; mpz_fdiv_q_2exp( shifted.mpz, shiften.mpz, shift ); return shifted; } | 
|---|
| [00e80b6] | 237 | static inline Int ?>>=?( Int & lhs, mp_bitcnt_t shift ) { return lhs = lhs >> shift; } | 
|---|
| [4c8f86b3] | 238 |  | 
|---|
|  | 239 | // number functions | 
|---|
| [8f61052c] | 240 | static inline Int abs( Int oper ) { Int positive; mpz_abs( positive.mpz, oper.mpz ); return positive; } | 
|---|
|  | 241 | static inline Int fact( unsigned long int N ) { Int factorial; mpz_fac_ui( factorial.mpz, N ); return factorial; } | 
|---|
|  | 242 | static inline Int gcd( Int oper1, Int oper2 ) { Int gcdret; mpz_gcd( gcdret.mpz, oper1.mpz, oper2.mpz ); return gcdret; } | 
|---|
|  | 243 | static inline Int pow( Int base, unsigned long int exponent ) { Int power; mpz_pow_ui( power.mpz, base.mpz, exponent ); return power; } | 
|---|
|  | 244 | static inline Int pow( unsigned long int base, unsigned long int exponent ) { Int power; mpz_ui_pow_ui( power.mpz, base, exponent ); return power; } | 
|---|
|  | 245 | static inline void srandom( gmp_randstate_t state ) { gmp_randinit_default( state ); } | 
|---|
|  | 246 | static inline Int random( gmp_randstate_t state, mp_bitcnt_t n ) { Int rand; mpz_urandomb( rand.mpz, state, n ); return rand; } | 
|---|
|  | 247 | static inline Int random( gmp_randstate_t state, Int n ) { Int rand; mpz_urandomm( rand.mpz, state, n.mpz ); return rand; } | 
|---|
|  | 248 | static inline Int random( gmp_randstate_t state, mp_size_t max_size ) { Int rand; mpz_random( rand.mpz, max_size ); return rand; } | 
|---|
|  | 249 | static inline int sgn( Int oper ) { return mpz_sgn( oper.mpz ); } | 
|---|
|  | 250 | static inline Int sqrt( Int oper ) { Int root; mpz_sqrt( root.mpz, oper.mpz ); return root; } | 
|---|
| [4c8f86b3] | 251 |  | 
|---|
|  | 252 | // I/O | 
|---|
| [8f61052c] | 253 | static inline forall( dtype istype | istream( istype ) ) | 
|---|
| [4c8f86b3] | 254 | istype * ?|?( istype * is, Int * mp ) { | 
|---|
|  | 255 | gmp_scanf( "%Zd", mp ); | 
|---|
|  | 256 | return is; | 
|---|
|  | 257 | } // ?|? | 
|---|
|  | 258 |  | 
|---|
| [8f61052c] | 259 | static inline forall( dtype ostype | ostream( ostype ) ) | 
|---|
| [4c8f86b3] | 260 | ostype * ?|?( ostype * os, Int mp ) { | 
|---|
|  | 261 | if ( sepPrt( os ) ) fmt( os, "%s", sepGetCur( os ) ); | 
|---|
|  | 262 | gmp_printf( "%Zd", mp.mpz ); | 
|---|
|  | 263 | sepOn( os ); | 
|---|
|  | 264 | return os; | 
|---|
|  | 265 | } // ?|? | 
|---|
|  | 266 |  | 
|---|
|  | 267 | // Local Variables: // | 
|---|
|  | 268 | // mode: c // | 
|---|
|  | 269 | // tab-width: 4 // | 
|---|
|  | 270 | // End: // | 
|---|