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