[fc732a9] | 1 | /* emacs edit mode for this file is -*- C++ -*- */ |
---|
[341696] | 2 | /* $Id$ */ |
---|
[fc732a9] | 3 | |
---|
| 4 | //{{{ docu |
---|
| 5 | // |
---|
| 6 | // int_intdiv.cc - `InternalInteger' division algorithms. |
---|
| 7 | // |
---|
| 8 | //}}} |
---|
| 9 | |
---|
[e4fe2b] | 10 | #include "config.h" |
---|
[fc732a9] | 11 | |
---|
[64df343] | 12 | #include "canonicalform.h" |
---|
[fc732a9] | 13 | #include "imm.h" |
---|
| 14 | #include "int_cf.h" |
---|
| 15 | #include "int_int.h" |
---|
| 16 | #include "int_rat.h" |
---|
[ee668e] | 17 | #include <factory/cf_gmp.h> |
---|
[fc732a9] | 18 | #include "gmpext.h" |
---|
[6db552] | 19 | #include "templates/ftmpl_functions.h" |
---|
[fc732a9] | 20 | |
---|
| 21 | //{{{ InternalCF * InternalInteger::dividesame, dividecoeff ( InternalCF * c ) |
---|
| 22 | // docu: see CanonicalForm::operator /() |
---|
| 23 | InternalCF * |
---|
| 24 | InternalInteger::dividesame ( InternalCF * c ) |
---|
| 25 | { |
---|
| 26 | ASSERT( ! ::is_imm( c ) && c->levelcoeff() == IntegerDomain, |
---|
[806c18] | 27 | "type error: InternalInteger expected" ); |
---|
[fc732a9] | 28 | |
---|
| 29 | if ( c == this ) { |
---|
[806c18] | 30 | if ( deleteObject() ) delete this; |
---|
| 31 | return int2imm( 1 ); |
---|
[fc732a9] | 32 | } |
---|
| 33 | |
---|
| 34 | if ( cf_glob_switches.isOn( SW_RATIONAL ) ) { |
---|
[a52291] | 35 | mpz_t n, d; |
---|
| 36 | mpz_init_set( n, thempi ); |
---|
| 37 | mpz_init_set( d, MPI( c ) ); |
---|
[806c18] | 38 | if ( deleteObject() ) delete this; |
---|
| 39 | InternalRational * result = new InternalRational( n, d ); |
---|
| 40 | return result->normalize_myself(); |
---|
[fc732a9] | 41 | } |
---|
| 42 | |
---|
| 43 | if ( getRefCount() > 1 ) { |
---|
[806c18] | 44 | decRefCount(); |
---|
[a52291] | 45 | mpz_t mpiResult; |
---|
| 46 | mpz_init( mpiResult ); |
---|
| 47 | if ( mpz_sgn( MPI( c ) ) > 0 ) |
---|
| 48 | mpz_fdiv_q( mpiResult, thempi, MPI( c ) ); |
---|
[806c18] | 49 | else |
---|
[a52291] | 50 | mpz_cdiv_q( mpiResult, thempi, MPI( c ) ); |
---|
[806c18] | 51 | return normalizeMPI( mpiResult ); |
---|
[fc732a9] | 52 | } else { |
---|
[a52291] | 53 | if ( mpz_sgn( MPI( c ) ) > 0 ) |
---|
| 54 | mpz_fdiv_q( thempi, thempi, MPI( c ) ); |
---|
[806c18] | 55 | else |
---|
[a52291] | 56 | mpz_cdiv_q( thempi, thempi, MPI( c ) ); |
---|
[806c18] | 57 | return normalizeMyself(); |
---|
[fc732a9] | 58 | } |
---|
| 59 | } |
---|
| 60 | |
---|
| 61 | InternalCF * |
---|
| 62 | InternalInteger::dividecoeff ( InternalCF * c, bool invert ) |
---|
| 63 | { |
---|
| 64 | ASSERT( ::is_imm( c ) == INTMARK, |
---|
[806c18] | 65 | "type error: immediate integer expected" ); |
---|
[fc732a9] | 66 | ASSERT( invert || imm2int( c ) != 0, |
---|
[806c18] | 67 | "math error: divide by zero" ); |
---|
[fc732a9] | 68 | |
---|
| 69 | int intC = imm2int( c ); |
---|
| 70 | |
---|
| 71 | if ( cf_glob_switches.isOn( SW_RATIONAL ) ) { |
---|
[a52291] | 72 | mpz_t n, d; |
---|
[806c18] | 73 | if ( invert ) { |
---|
[a52291] | 74 | mpz_init_set_si( n, intC ); |
---|
| 75 | mpz_init_set( d, thempi ); |
---|
[806c18] | 76 | } else { |
---|
[a52291] | 77 | mpz_init_set( n, thempi ); |
---|
| 78 | mpz_init_set_si( d, intC ); |
---|
[806c18] | 79 | } |
---|
| 80 | if ( deleteObject() ) delete this; |
---|
| 81 | InternalRational * result = new InternalRational( n, d ); |
---|
| 82 | return result->normalize_myself(); |
---|
[fc732a9] | 83 | } |
---|
| 84 | |
---|
| 85 | if ( invert ) { |
---|
[a52291] | 86 | int mpiSign = mpz_sgn( thempi ); |
---|
[806c18] | 87 | if ( deleteObject() ) delete this; |
---|
| 88 | if ( intC >= 0 ) |
---|
| 89 | return int2imm( 0 ); |
---|
| 90 | else |
---|
| 91 | return int2imm( -mpiSign ); |
---|
[fc732a9] | 92 | } else if ( getRefCount() > 1 ) { |
---|
[806c18] | 93 | decRefCount(); |
---|
[a52291] | 94 | mpz_t mpiResult; |
---|
| 95 | mpz_init( mpiResult ); |
---|
[806c18] | 96 | if ( intC > 0 ) |
---|
[a52291] | 97 | mpz_fdiv_q_ui( mpiResult, thempi, intC ); |
---|
[806c18] | 98 | else { |
---|
[a52291] | 99 | mpz_fdiv_q_ui( mpiResult, thempi, -intC ); |
---|
| 100 | mpz_neg( mpiResult, mpiResult ); |
---|
[806c18] | 101 | } |
---|
| 102 | return normalizeMPI( mpiResult ); |
---|
[fc732a9] | 103 | } else { |
---|
[806c18] | 104 | if ( intC > 0 ) |
---|
[a52291] | 105 | mpz_fdiv_q_ui( thempi, thempi, intC ); |
---|
[806c18] | 106 | else { |
---|
[a52291] | 107 | mpz_fdiv_q_ui( thempi, thempi, -intC ); |
---|
| 108 | mpz_neg( thempi, thempi ); |
---|
[806c18] | 109 | } |
---|
| 110 | return normalizeMyself(); |
---|
[fc732a9] | 111 | } |
---|
| 112 | } |
---|
| 113 | //}}} |
---|
| 114 | |
---|
| 115 | //{{{ InternalCF * InternalInteger::divsame, divcoeff ( InternalCF * c ) |
---|
| 116 | // docu: see CanonicalForm::div() |
---|
| 117 | InternalCF * |
---|
| 118 | InternalInteger::divsame ( InternalCF * c ) |
---|
| 119 | { |
---|
| 120 | ASSERT( ! ::is_imm( c ) && c->levelcoeff() == IntegerDomain, |
---|
[806c18] | 121 | "type error: InternalInteger expected" ); |
---|
[fc732a9] | 122 | |
---|
[b391de] | 123 | if ( c == this ) { |
---|
[806c18] | 124 | if ( deleteObject() ) delete this; |
---|
| 125 | return int2imm( 1 ); |
---|
[b391de] | 126 | } |
---|
| 127 | |
---|
[fc732a9] | 128 | if ( getRefCount() > 1 ) { |
---|
[806c18] | 129 | deleteObject(); |
---|
[a52291] | 130 | mpz_t mpiResult; |
---|
| 131 | mpz_init( mpiResult ); |
---|
| 132 | mpz_divexact( mpiResult, thempi, MPI( c ) ); |
---|
[806c18] | 133 | return normalizeMPI( mpiResult ); |
---|
[fc732a9] | 134 | } else { |
---|
[a52291] | 135 | mpz_divexact( thempi, thempi, MPI( c ) ); |
---|
[806c18] | 136 | return normalizeMyself(); |
---|
[fc732a9] | 137 | } |
---|
| 138 | } |
---|
| 139 | |
---|
| 140 | InternalCF * |
---|
| 141 | InternalInteger::divcoeff ( InternalCF * c, bool invert ) |
---|
| 142 | { |
---|
| 143 | ASSERT( ::is_imm( c ) == INTMARK, |
---|
[806c18] | 144 | "type error: immediate integer expected" ); |
---|
[b391de] | 145 | ASSERT( invert || imm2int( c ) != 0, |
---|
[806c18] | 146 | "math error: divide by zero" ); |
---|
[b391de] | 147 | ASSERT( ! invert || imm2int( c ) == 0, |
---|
[806c18] | 148 | "math error: c does not divide CO" ); |
---|
[fc732a9] | 149 | |
---|
| 150 | if ( invert ) { |
---|
[806c18] | 151 | if ( deleteObject() ) delete this; |
---|
| 152 | // this may happen iff `c' == 0 |
---|
| 153 | return int2imm( 0 ); |
---|
[fc732a9] | 154 | } else if ( getRefCount() > 1 ) { |
---|
[806c18] | 155 | deleteObject(); |
---|
[a52291] | 156 | mpz_t mpiC; |
---|
| 157 | mpz_t mpiResult; |
---|
| 158 | mpz_init_set_si( mpiC, imm2int( c ) ); |
---|
| 159 | mpz_init( mpiResult ); |
---|
| 160 | mpz_divexact( mpiResult, thempi, mpiC ); |
---|
| 161 | mpz_clear( mpiC ); |
---|
[806c18] | 162 | return normalizeMPI( mpiResult ); |
---|
[fc732a9] | 163 | } else { |
---|
[a52291] | 164 | mpz_t mpiC; |
---|
| 165 | mpz_init_set_si( mpiC, imm2int( c ) ); |
---|
| 166 | mpz_divexact( thempi, thempi, mpiC ); |
---|
| 167 | mpz_clear( mpiC ); |
---|
[806c18] | 168 | return normalizeMyself(); |
---|
[fc732a9] | 169 | } |
---|
| 170 | } |
---|
| 171 | //}}} |
---|
| 172 | |
---|
| 173 | //{{{ InternalCF * InternalInteger::modulosame, modulocoeff ( InternalCF * c ) |
---|
| 174 | // docu: see CanonicalForm::operator %() |
---|
| 175 | InternalCF * |
---|
| 176 | InternalInteger::modulosame ( InternalCF * c ) |
---|
| 177 | { |
---|
| 178 | ASSERT( ! ::is_imm( c ) && c->levelcoeff() == IntegerDomain, |
---|
[806c18] | 179 | "type error: InternalInteger expected" ); |
---|
[fc732a9] | 180 | |
---|
[b391de] | 181 | if ( (c == this) || cf_glob_switches.isOn( SW_RATIONAL ) ) { |
---|
[806c18] | 182 | if ( deleteObject() ) delete this; |
---|
| 183 | return int2imm( 0 ); |
---|
[fc732a9] | 184 | } |
---|
| 185 | |
---|
| 186 | if ( getRefCount() > 1 ) { |
---|
[806c18] | 187 | decRefCount(); |
---|
[a52291] | 188 | mpz_t mpiResult; |
---|
| 189 | mpz_init( mpiResult ); |
---|
| 190 | mpz_mod( mpiResult, thempi, MPI( c ) ); |
---|
[806c18] | 191 | return uiNormalizeMPI( mpiResult ); |
---|
[fc732a9] | 192 | } else { |
---|
[a52291] | 193 | mpz_mod( thempi, thempi, MPI( c ) ); |
---|
[806c18] | 194 | return uiNormalizeMyself(); |
---|
[fc732a9] | 195 | } |
---|
| 196 | } |
---|
| 197 | |
---|
| 198 | InternalCF * |
---|
| 199 | InternalInteger::modulocoeff ( InternalCF * c, bool invert ) |
---|
| 200 | { |
---|
| 201 | ASSERT( ::is_imm( c ) == INTMARK, |
---|
[806c18] | 202 | "type error: immediate integer expected" ); |
---|
[fc732a9] | 203 | ASSERT( invert || imm2int( c ) != 0, |
---|
[806c18] | 204 | "math error: divide by zero" ); |
---|
[fc732a9] | 205 | |
---|
| 206 | if ( cf_glob_switches.isOn( SW_RATIONAL ) ) { |
---|
[806c18] | 207 | if ( deleteObject() ) delete this; |
---|
| 208 | return int2imm( 0 ); |
---|
[fc732a9] | 209 | } |
---|
| 210 | |
---|
| 211 | int intC = imm2int( c ); |
---|
| 212 | |
---|
| 213 | if ( invert ) { |
---|
[806c18] | 214 | if ( intC >= 0 ) { |
---|
| 215 | if ( deleteObject() ) delete this; |
---|
| 216 | return c; |
---|
| 217 | } else { |
---|
| 218 | // no checks for refCount == 1 are done. It is not worth ... |
---|
[a52291] | 219 | mpz_t mpiResult; |
---|
| 220 | mpz_init_set( mpiResult, thempi ); |
---|
| 221 | mpz_abs( mpiResult, mpiResult ); |
---|
| 222 | mpz_sub_ui( mpiResult, mpiResult, -intC ); |
---|
[806c18] | 223 | if ( deleteObject() ) delete this; |
---|
| 224 | return uiNormalizeMPI( mpiResult ); |
---|
| 225 | } |
---|
[fc732a9] | 226 | } else { |
---|
[a52291] | 227 | mpz_t dummy; |
---|
| 228 | mpz_init( dummy ); |
---|
| 229 | InternalCF * result = int2imm( mpz_mod_ui( dummy, thempi, tabs( intC ) ) ); |
---|
| 230 | mpz_clear( dummy ); |
---|
[806c18] | 231 | if ( deleteObject() ) delete this; |
---|
| 232 | return result; |
---|
[fc732a9] | 233 | } |
---|
| 234 | } |
---|
| 235 | //}}} |
---|
| 236 | |
---|
| 237 | //{{{ InternalCF * InternalInteger::modsame, modcoeff ( InternalCF * c ) |
---|
| 238 | // docu: see CanonicalForm::mod() |
---|
| 239 | InternalCF * |
---|
| 240 | InternalInteger::modsame ( InternalCF * c ) |
---|
| 241 | { |
---|
| 242 | return modulosame( c ); |
---|
| 243 | } |
---|
| 244 | |
---|
| 245 | InternalCF * |
---|
| 246 | InternalInteger::modcoeff ( InternalCF * c, bool invert ) |
---|
| 247 | { |
---|
| 248 | return modulocoeff( c, invert ); |
---|
| 249 | } |
---|
| 250 | //}}} |
---|
| 251 | |
---|
| 252 | //{{{ void InternalInteger::divremsame, divremcoeff ( InternalCF * c, InternalCF * & quot, InternalCF * & rem ) |
---|
| 253 | // docu: see CanonicalForm::divrem() |
---|
| 254 | void |
---|
| 255 | InternalInteger::divremsame ( InternalCF * c, InternalCF * & quot, InternalCF * & rem ) |
---|
| 256 | { |
---|
[b391de] | 257 | ASSERT( ! ::is_imm( c ) && c->levelcoeff() == IntegerDomain, |
---|
[806c18] | 258 | "type error: InternalInteger expected" ); |
---|
[b391de] | 259 | |
---|
| 260 | if ( c == this ) { |
---|
[806c18] | 261 | quot = int2imm( 1 ); |
---|
| 262 | rem = int2imm( 0 ); |
---|
| 263 | return; |
---|
[fc732a9] | 264 | } |
---|
[b391de] | 265 | |
---|
| 266 | if ( cf_glob_switches.isOn( SW_RATIONAL ) ) { |
---|
[a52291] | 267 | mpz_t n, d; |
---|
| 268 | mpz_init_set( n, thempi ); |
---|
| 269 | mpz_init_set( d, MPI( c ) ); |
---|
[806c18] | 270 | InternalRational * result = new InternalRational( n, d ); |
---|
| 271 | quot = result->normalize_myself(); |
---|
| 272 | rem = int2imm( 0 ); |
---|
| 273 | return; |
---|
[fc732a9] | 274 | } |
---|
[b391de] | 275 | |
---|
[a52291] | 276 | mpz_t q; |
---|
| 277 | mpz_t r; |
---|
| 278 | mpz_init( q ); mpz_init( r ); |
---|
| 279 | if ( mpz_sgn( MPI( c ) ) > 0 ) |
---|
| 280 | mpz_fdiv_qr( q, r, thempi, MPI( c ) ); |
---|
[b391de] | 281 | else |
---|
[a52291] | 282 | mpz_cdiv_qr( q, r, thempi, MPI( c ) ); |
---|
[b391de] | 283 | |
---|
| 284 | quot = normalizeMPI( q ); |
---|
| 285 | rem = uiNormalizeMPI( r ); |
---|
[fc732a9] | 286 | } |
---|
| 287 | |
---|
| 288 | void |
---|
| 289 | InternalInteger::divremcoeff ( InternalCF * c, InternalCF * & quot, InternalCF * & rem, bool invert ) |
---|
| 290 | { |
---|
[b391de] | 291 | ASSERT( ::is_imm( c ) == INTMARK, |
---|
[806c18] | 292 | "type error: immediate integer expected" ); |
---|
[b391de] | 293 | ASSERT( invert || imm2int( c ) != 0, |
---|
[806c18] | 294 | "math error: divide by zero" ); |
---|
[b391de] | 295 | |
---|
| 296 | int intC = imm2int( c ); |
---|
[fc732a9] | 297 | |
---|
| 298 | if ( cf_glob_switches.isOn( SW_RATIONAL ) ) { |
---|
[a52291] | 299 | mpz_t n, d; |
---|
[806c18] | 300 | if ( invert ) { |
---|
[a52291] | 301 | mpz_init_set_si( n, intC ); |
---|
| 302 | mpz_init_set( d, thempi ); |
---|
[806c18] | 303 | } else { |
---|
[a52291] | 304 | mpz_init_set( n, thempi ); |
---|
| 305 | mpz_init_set_si( d, intC ); |
---|
[806c18] | 306 | } |
---|
| 307 | InternalRational * result = new InternalRational( n, d ); |
---|
| 308 | quot = result->normalize_myself(); |
---|
| 309 | rem = int2imm( 0 ); |
---|
| 310 | return; |
---|
[fc732a9] | 311 | } |
---|
| 312 | |
---|
| 313 | if ( invert ) { |
---|
[806c18] | 314 | if ( intC >= 0 ) { |
---|
| 315 | rem = c; |
---|
| 316 | quot = int2imm( 0 ); |
---|
| 317 | } else { |
---|
[a52291] | 318 | mpz_t mpiResult; |
---|
| 319 | mpz_init_set( mpiResult, thempi ); |
---|
| 320 | mpz_abs( mpiResult, mpiResult ); |
---|
| 321 | mpz_sub_ui( mpiResult, mpiResult, -intC ); |
---|
[806c18] | 322 | rem = uiNormalizeMPI( mpiResult ); |
---|
[a52291] | 323 | quot = int2imm( -mpz_sgn( thempi ) ); |
---|
[806c18] | 324 | } |
---|
[b391de] | 325 | } else { |
---|
[a52291] | 326 | mpz_t q; |
---|
| 327 | mpz_t dummy; |
---|
| 328 | mpz_init( q ); mpz_init( dummy ); |
---|
[806c18] | 329 | if ( intC > 0 ) { |
---|
[a52291] | 330 | rem = int2imm( mpz_fdiv_qr_ui( q, dummy, thempi, intC ) ); |
---|
[806c18] | 331 | quot = normalizeMPI( q ); |
---|
| 332 | } else { |
---|
[a52291] | 333 | rem = int2imm( mpz_fdiv_qr_ui( q, dummy, thempi, -intC ) ); |
---|
| 334 | mpz_neg( q, q ); |
---|
[806c18] | 335 | quot = normalizeMPI( q ); |
---|
| 336 | } |
---|
[a52291] | 337 | mpz_clear( dummy ); |
---|
[fc732a9] | 338 | } |
---|
| 339 | } |
---|
| 340 | //}}} |
---|
| 341 | |
---|
| 342 | //{{{ bool InternalInteger::divremsamet, divremcoefft ( InternalCF * c, InternalCF * & quot, InternalCF * & rem ) |
---|
| 343 | // docu: see CanonicalForm::divremt() |
---|
| 344 | bool |
---|
| 345 | InternalInteger::divremsamet ( InternalCF * c, InternalCF * & quot, InternalCF * & rem ) |
---|
| 346 | { |
---|
| 347 | divremsame( c, quot, rem ); |
---|
| 348 | return true; |
---|
| 349 | } |
---|
| 350 | |
---|
| 351 | bool |
---|
| 352 | InternalInteger::divremcoefft ( InternalCF * c, InternalCF * & quot, InternalCF * & rem, bool invert ) |
---|
| 353 | { |
---|
| 354 | divremcoeff( c, quot, rem, invert ); |
---|
| 355 | return true; |
---|
| 356 | } |
---|
| 357 | //}}} |
---|