| 1 |
/* |
| 2 |
* Ikarus Scheme -- A compiler for R6RS Scheme. |
| 3 |
* Copyright (C) 2006,2007,2008 Abdulaziz Ghuloum |
| 4 |
* |
| 5 |
* This program is free software: you can redistribute it and/or modify |
| 6 |
* it under the terms of the GNU General Public License version 3 as |
| 7 |
* published by the Free Software Foundation. |
| 8 |
* |
| 9 |
* This program is distributed in the hope that it will be useful, but |
| 10 |
* WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 12 |
* General Public License for more details. |
| 13 |
* |
| 14 |
* You should have received a copy of the GNU General Public License |
| 15 |
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 16 |
*/ |
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
#include "ikarus-data.h" |
| 21 |
#include <stdlib.h> |
| 22 |
#include <stdio.h> |
| 23 |
#include <string.h> |
| 24 |
#include <gmp.h> |
| 25 |
|
| 26 |
#define bnfst_limb_count(x) \ |
| 27 |
(((unsigned long int)(x)) >> bignum_length_shift) |
| 28 |
#define bnfst_negative(x) \ |
| 29 |
(((unsigned long int)(x)) & bignum_sign_mask) |
| 30 |
|
| 31 |
|
| 32 |
#define most_positive_fixnum \ |
| 33 |
(((unsigned long int)-1) >> (fx_shift+1)) |
| 34 |
#define most_negative_fixnum (most_positive_fixnum+1) |
| 35 |
// #define most_positive_fixnum 0x1FFFFFFF |
| 36 |
// #define most_negative_fixnum 0x20000000 |
| 37 |
|
| 38 |
#define max_digits_per_limb ((wordsize==4)?10:20) |
| 39 |
|
| 40 |
#ifdef NDEBUG |
| 41 |
#define verify_bignum(x,caller) (x) |
| 42 |
#else |
| 43 |
static ikptr |
| 44 |
verify_bignum(ikptr x, char* caller){ |
| 45 |
if(tagof(x) != vector_tag){ |
| 46 |
fprintf(stderr, "Error in (%s) invalid primary tag 0x%016lx\n", caller, x); |
| 47 |
exit(-1); |
| 48 |
} |
| 49 |
ikptr fst = ref(x, -vector_tag); |
| 50 |
long int limb_count = ((unsigned long int) fst) >> bignum_length_shift; |
| 51 |
if(limb_count <= 0){ |
| 52 |
fprintf(stderr, |
| 53 |
"Error in (%s) invalid limb count in fst=0x%016lx\n", |
| 54 |
caller, (long int)fst); |
| 55 |
exit(-1); |
| 56 |
} |
| 57 |
int pos; |
| 58 |
if((long int)fst & bignum_sign_mask){ |
| 59 |
pos = 0; |
| 60 |
} else { |
| 61 |
pos = 1; |
| 62 |
} |
| 63 |
mp_limb_t last_limb = |
| 64 |
(mp_limb_t) ref(x, off_bignum_data + (limb_count - 1) * wordsize); |
| 65 |
if(last_limb == 0){ |
| 66 |
fprintf(stderr, |
| 67 |
"Error in (%s) invalid last limb = 0x%016lx", caller, last_limb); |
| 68 |
exit(-1); |
| 69 |
} |
| 70 |
if(limb_count == 1){ |
| 71 |
if(pos){ |
| 72 |
if(last_limb <= most_positive_fixnum){ |
| 73 |
fprintf(stderr, |
| 74 |
"Error in (%s) should be a positive fixnum: 0x%016lx\n", |
| 75 |
caller, last_limb); |
| 76 |
exit(-1); |
| 77 |
} |
| 78 |
} else { |
| 79 |
if(last_limb <= most_negative_fixnum){ |
| 80 |
fprintf(stderr, |
| 81 |
"Error in (%s) should be a negative fixnum: 0x%016lx\n", |
| 82 |
caller, last_limb); |
| 83 |
exit(-1); |
| 84 |
} |
| 85 |
} |
| 86 |
} |
| 87 |
/* ok */ |
| 88 |
return x; |
| 89 |
} |
| 90 |
#endif |
| 91 |
|
| 92 |
#define BN(x) verify_bignum(x,"BN") |
| 93 |
|
| 94 |
#if 0 |
| 95 |
ikptr |
| 96 |
ikrt_isbignum(ikptr x){ |
| 97 |
if(tagof(x) == vector_tag){ |
| 98 |
ikptr fst = ref(x, -vector_tag); |
| 99 |
if (bignum_tag == (bignum_mask & (int)fst)){ |
| 100 |
return true_object; |
| 101 |
} |
| 102 |
} |
| 103 |
return false_object; |
| 104 |
} |
| 105 |
#endif |
| 106 |
|
| 107 |
ikptr |
| 108 |
ikrt_positive_bn(ikptr x){ |
| 109 |
ikptr fst = ref(x, -vector_tag); |
| 110 |
if(bnfst_negative(fst)){ |
| 111 |
return false_object; |
| 112 |
} else { |
| 113 |
return true_object; |
| 114 |
} |
| 115 |
} |
| 116 |
|
| 117 |
ikptr |
| 118 |
ikrt_even_bn(ikptr x){ |
| 119 |
long int fst = (long int)ref(x, wordsize-vector_tag); |
| 120 |
if(fst & 1){ |
| 121 |
return false_object; |
| 122 |
} else { |
| 123 |
return true_object; |
| 124 |
} |
| 125 |
} |
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
ikptr |
| 130 |
ikrt_fxfxplus(ikptr x, ikptr y, ikpcb* pcb){ |
| 131 |
long int n1 = unfix(x); |
| 132 |
long int n2 = unfix(y); |
| 133 |
long int r = n1 + n2; |
| 134 |
ikptr q = fix(r); |
| 135 |
if(r == unfix(q)){ |
| 136 |
return q; |
| 137 |
} |
| 138 |
else { |
| 139 |
ikptr bn = ik_safe_alloc(pcb, align(disp_bignum_data + wordsize)); |
| 140 |
if(r > 0){ |
| 141 |
ref(bn, 0) = (ikptr)(bignum_tag | (1 << bignum_length_shift)); |
| 142 |
ref(bn, disp_bignum_data) = (ikptr)r; |
| 143 |
} |
| 144 |
else { |
| 145 |
ref(bn, 0) = |
| 146 |
(ikptr)(bignum_tag | |
| 147 |
(1 << bignum_length_shift) | |
| 148 |
(1 << bignum_sign_shift)); |
| 149 |
ref(bn, disp_bignum_data) = (ikptr)-r; |
| 150 |
} |
| 151 |
return verify_bignum(bn+vector_tag, "fxfx+"); |
| 152 |
} |
| 153 |
} |
| 154 |
|
| 155 |
ikptr |
| 156 |
ikrt_fxbnplus(ikptr x, ikptr y, ikpcb* pcb){ |
| 157 |
if(x == 0){ return y ; } |
| 158 |
ikptr fst = ref(y, -vector_tag); |
| 159 |
long int limb_count = bnfst_limb_count(fst); |
| 160 |
long int intx = unfix(x); |
| 161 |
if(intx > 0){ |
| 162 |
if(!bnfst_negative(fst)){ |
| 163 |
/* positive fx + positive bn = even bigger positive */ |
| 164 |
pcb->root0 = &y; |
| 165 |
ikptr r = ik_safe_alloc(pcb, align(disp_bignum_data+(limb_count+1)*wordsize)); |
| 166 |
pcb->root0 = 0; |
| 167 |
mp_limb_t carry = |
| 168 |
mpn_add_1((mp_limb_t*)(long)(r+disp_bignum_data), |
| 169 |
(mp_limb_t*)(long)(y - vector_tag + disp_bignum_data), |
| 170 |
limb_count, |
| 171 |
intx); |
| 172 |
if(carry){ |
| 173 |
ref(r, disp_bignum_data + limb_count*wordsize) = (ikptr)1; |
| 174 |
ref(r, 0) = (ikptr) |
| 175 |
(((limb_count + 1) << bignum_length_shift) | |
| 176 |
(0 << bignum_sign_shift) | |
| 177 |
bignum_tag); |
| 178 |
return verify_bignum(r+vector_tag, "fxbn+1"); |
| 179 |
} else { |
| 180 |
ref(r, 0) = (ikptr) |
| 181 |
((limb_count << bignum_length_shift) | |
| 182 |
(0 << bignum_sign_shift) | |
| 183 |
bignum_tag); |
| 184 |
return verify_bignum(r+vector_tag, "fxbn+2"); |
| 185 |
} |
| 186 |
} |
| 187 |
else { |
| 188 |
/* positive fx + negative bn = smaller negative bn */ |
| 189 |
pcb->root0 = &y; |
| 190 |
ikptr r = ik_safe_alloc(pcb, align(disp_bignum_data+limb_count*wordsize)); |
| 191 |
pcb->root0 = 0; |
| 192 |
mp_limb_t borrow = |
| 193 |
mpn_sub_1((mp_limb_t*)(long)(r+disp_bignum_data), |
| 194 |
(mp_limb_t*)(long)(y - vector_tag + disp_bignum_data), |
| 195 |
limb_count, |
| 196 |
intx); |
| 197 |
if(borrow){ |
| 198 |
fprintf(stderr, "Error: BUG in borrow1 %ld\n", borrow); |
| 199 |
exit(-1); |
| 200 |
} |
| 201 |
long int result_size = |
| 202 |
(ref(r, disp_bignum_data + (limb_count-1)*wordsize)) |
| 203 |
? limb_count |
| 204 |
: (limb_count - 1); |
| 205 |
if(result_size == 0){ |
| 206 |
return 0; |
| 207 |
} |
| 208 |
if(result_size == 1){ |
| 209 |
mp_limb_t last = |
| 210 |
(mp_limb_t) ref(r, disp_bignum_data + (result_size-1)*wordsize); |
| 211 |
if(last <= most_negative_fixnum){ |
| 212 |
return fix(-(long int)last); |
| 213 |
} |
| 214 |
} |
| 215 |
ref(r, 0) = (ikptr) |
| 216 |
((result_size << bignum_length_shift) | |
| 217 |
(1 << bignum_sign_shift) | |
| 218 |
bignum_tag); |
| 219 |
return verify_bignum(r+vector_tag, "fxbn+3"); |
| 220 |
} |
| 221 |
} |
| 222 |
else { |
| 223 |
if(! bnfst_negative(fst)){ |
| 224 |
/* negative fx + positive bn = smaller positive */ |
| 225 |
pcb->root0 = &y; |
| 226 |
ikptr r = ik_safe_alloc(pcb, align(disp_bignum_data+limb_count*wordsize)); |
| 227 |
pcb->root0 = 0; |
| 228 |
mp_limb_t borrow = |
| 229 |
mpn_sub_1((mp_limb_t*)(long)(r+disp_bignum_data), |
| 230 |
(mp_limb_t*)(long)(y - vector_tag + disp_bignum_data), |
| 231 |
limb_count, |
| 232 |
- intx); |
| 233 |
if(borrow){ |
| 234 |
fprintf(stderr, "Error: BUG in borrow2\n"); |
| 235 |
exit(-1); |
| 236 |
} |
| 237 |
long int result_size = |
| 238 |
(ref(r, disp_bignum_data + (limb_count-1)*wordsize) == 0) |
| 239 |
? (limb_count - 1) |
| 240 |
: limb_count; |
| 241 |
if(result_size == 0){ |
| 242 |
return 0; |
| 243 |
} |
| 244 |
if(result_size == 1){ |
| 245 |
mp_limb_t last = |
| 246 |
(mp_limb_t) ref(r, disp_bignum_data + (result_size-1)*wordsize); |
| 247 |
if(last <= most_positive_fixnum){ |
| 248 |
return fix((long int)last); |
| 249 |
} |
| 250 |
} |
| 251 |
ref(r, 0) = (ikptr) |
| 252 |
((result_size << bignum_length_shift) | |
| 253 |
(0 << bignum_sign_shift) | |
| 254 |
bignum_tag); |
| 255 |
return verify_bignum(r+vector_tag, "fxbn+4"); |
| 256 |
} else { |
| 257 |
/* negative fx + negative bn = larger negative */ |
| 258 |
pcb->root0 = &y; |
| 259 |
ikptr r = ik_safe_alloc(pcb, align(disp_bignum_data+(limb_count+1)*wordsize)); |
| 260 |
pcb->root0 = 0; |
| 261 |
mp_limb_t carry = |
| 262 |
mpn_add_1((mp_limb_t*)(long)(r+disp_bignum_data), |
| 263 |
(mp_limb_t*)(long)(y - vector_tag + disp_bignum_data), |
| 264 |
limb_count, |
| 265 |
-intx); |
| 266 |
if(carry){ |
| 267 |
ref(r, disp_bignum_data + limb_count*wordsize) = (ikptr)1; |
| 268 |
ref(r, 0) = (ikptr) |
| 269 |
(((limb_count + 1) << bignum_length_shift) | |
| 270 |
(1 << bignum_sign_shift) | |
| 271 |
bignum_tag); |
| 272 |
return verify_bignum(r+vector_tag, "fxbn+5"); |
| 273 |
} else { |
| 274 |
ref(r, 0) = (ikptr) |
| 275 |
((limb_count << bignum_length_shift) | |
| 276 |
(1 << bignum_sign_shift) | |
| 277 |
bignum_tag); |
| 278 |
return verify_bignum(r+vector_tag, "fxbn+5"); |
| 279 |
} |
| 280 |
} |
| 281 |
} |
| 282 |
} |
| 283 |
|
| 284 |
|
| 285 |
|
| 286 |
|
| 287 |
ikptr |
| 288 |
ikrt_bnbnplus(ikptr x, ikptr y, ikpcb* pcb){ |
| 289 |
unsigned long int xfst = (unsigned long int)ref(x, -vector_tag); |
| 290 |
unsigned long int yfst = (unsigned long int)ref(y, -vector_tag); |
| 291 |
long int xsign = xfst & bignum_sign_mask; |
| 292 |
long int ysign = yfst & bignum_sign_mask; |
| 293 |
long int xlimbs = xfst >> bignum_length_shift; |
| 294 |
long int ylimbs = yfst >> bignum_length_shift; |
| 295 |
if(xsign == ysign){ |
| 296 |
long int n1,n2; |
| 297 |
ikptr s1,s2; |
| 298 |
if(xlimbs > ylimbs){ |
| 299 |
n1 = xlimbs; n2 = ylimbs; s1 = x; s2 = y; |
| 300 |
} else { |
| 301 |
n1 = ylimbs; n2 = xlimbs; s1 = y; s2 = x; |
| 302 |
} |
| 303 |
pcb->root0 = &s1; |
| 304 |
pcb->root1 = &s2; |
| 305 |
ikptr res = ik_safe_alloc(pcb, align(disp_bignum_data + (n1+1)*wordsize)); |
| 306 |
pcb->root0 = 0; |
| 307 |
pcb->root1 = 0; |
| 308 |
mp_limb_t carry = |
| 309 |
mpn_add((mp_limb_t*)(long)(res+disp_bignum_data), |
| 310 |
(mp_limb_t*)(long)(s1-vector_tag+disp_bignum_data), |
| 311 |
n1, |
| 312 |
(mp_limb_t*)(long)(s2-vector_tag+disp_bignum_data), |
| 313 |
n2); |
| 314 |
if(carry){ |
| 315 |
ref(res, disp_vector_data + xlimbs*wordsize) = (ikptr)1; |
| 316 |
ref(res, 0) = (ikptr) |
| 317 |
(((n1+1) << bignum_length_shift) | |
| 318 |
xsign | |
| 319 |
bignum_tag); |
| 320 |
return verify_bignum(res+vector_tag, "bnbn+1"); |
| 321 |
} else { |
| 322 |
ref(res, 0) = (ikptr) |
| 323 |
((n1 << bignum_length_shift) | |
| 324 |
xsign | |
| 325 |
bignum_tag); |
| 326 |
return verify_bignum(res+vector_tag, "bnbn+2"); |
| 327 |
} |
| 328 |
} |
| 329 |
else { |
| 330 |
ikptr s1=x, s2=y; |
| 331 |
long int n1=xlimbs, n2=ylimbs; |
| 332 |
long int result_sign = xsign; |
| 333 |
while((xlimbs == ylimbs) && |
| 334 |
(ref(x, -vector_tag+disp_bignum_data+(xlimbs-1)*wordsize) == |
| 335 |
ref(y, -vector_tag+disp_bignum_data+(xlimbs-1)*wordsize))){ |
| 336 |
xlimbs -= 1; |
| 337 |
ylimbs -= 1; |
| 338 |
if(xlimbs == 0){ return 0; } |
| 339 |
} |
| 340 |
/* |x| != |y| */ |
| 341 |
if(xlimbs <= ylimbs){ |
| 342 |
if(xlimbs == ylimbs){ |
| 343 |
if((ref(y, -vector_tag+disp_bignum_data+(xlimbs-1)*wordsize) > |
| 344 |
ref(x, -vector_tag+disp_bignum_data+(xlimbs-1)*wordsize))){ |
| 345 |
s1 = y; n1 = ylimbs; |
| 346 |
s2 = x; n2 = xlimbs; |
| 347 |
result_sign = ysign; |
| 348 |
} |
| 349 |
} else { |
| 350 |
s1 = y; n1 = ylimbs; |
| 351 |
s2 = x; n2 = xlimbs; |
| 352 |
result_sign = ysign; |
| 353 |
} |
| 354 |
} |
| 355 |
/* |s1| > |s2| */ |
| 356 |
pcb->root0 = &s1; |
| 357 |
pcb->root1 = &s2; |
| 358 |
ikptr res = ik_safe_alloc(pcb, align(disp_bignum_data + n1 * wordsize)); |
| 359 |
pcb->root0 = 0; |
| 360 |
pcb->root1 = 0; |
| 361 |
mp_limb_t burrow = |
| 362 |
mpn_sub((mp_limb_t*)(long)(res + disp_bignum_data), |
| 363 |
(mp_limb_t*)(long)(s1 - vector_tag + disp_bignum_data), |
| 364 |
n1, |
| 365 |
(mp_limb_t*)(long)(s2 - vector_tag + disp_bignum_data), |
| 366 |
n2); |
| 367 |
if(burrow){ |
| 368 |
fprintf(stderr, "BUG: Burrow error in bnbn+\n"); |
| 369 |
exit(-1); |
| 370 |
} |
| 371 |
long int len = n1; |
| 372 |
while(ref(res, disp_bignum_data + (len-1)*wordsize) == 0){ |
| 373 |
len--; |
| 374 |
if(len == 0){ |
| 375 |
return 0; |
| 376 |
} |
| 377 |
} |
| 378 |
if(result_sign == 0){ |
| 379 |
/* positive result */ |
| 380 |
if(len == 1){ |
| 381 |
mp_limb_t fst_limb = (mp_limb_t) ref(res, disp_bignum_data); |
| 382 |
if(fst_limb <= most_positive_fixnum){ |
| 383 |
return fix((long int)fst_limb); |
| 384 |
} |
| 385 |
} |
| 386 |
ref(res, 0) = (ikptr) |
| 387 |
((len << bignum_length_shift) | |
| 388 |
result_sign | |
| 389 |
bignum_tag); |
| 390 |
return verify_bignum(res+vector_tag, "bnbn+3"); |
| 391 |
} else { |
| 392 |
/* negative result */ |
| 393 |
if(len == 1){ |
| 394 |
mp_limb_t fst_limb = (mp_limb_t) ref(res, disp_bignum_data); |
| 395 |
if(fst_limb <= most_negative_fixnum){ |
| 396 |
return fix(-(long int)fst_limb); |
| 397 |
} |
| 398 |
} |
| 399 |
ref(res, 0) = (ikptr) |
| 400 |
((len << bignum_length_shift) | |
| 401 |
result_sign | |
| 402 |
bignum_tag); |
| 403 |
return verify_bignum(res+vector_tag, "bnbn+4"); |
| 404 |
} |
| 405 |
} |
| 406 |
} |
| 407 |
|
| 408 |
|
| 409 |
|
| 410 |
|
| 411 |
ikptr |
| 412 |
ikrt_fxfxminus(ikptr x, ikptr y, ikpcb* pcb){ |
| 413 |
long int n1 = unfix(x); |
| 414 |
long int n2 = unfix(y); |
| 415 |
long int r = n1 - n2; |
| 416 |
if(r >= 0){ |
| 417 |
if(((unsigned long int)r) <= most_positive_fixnum){ |
| 418 |
return fix(r); |
| 419 |
} else { |
| 420 |
ikptr bn = ik_safe_alloc(pcb, align(disp_bignum_data + wordsize)); |
| 421 |
ref(bn, 0) = (ikptr) (bignum_tag | (1 << bignum_length_shift)); |
| 422 |
ref(bn, disp_bignum_data) = (ikptr)r; |
| 423 |
return verify_bignum(bn+vector_tag,"fxfx-1"); |
| 424 |
} |
| 425 |
} else { |
| 426 |
ikptr fxr = fix(r); |
| 427 |
if(unfix(fxr) == r){ |
| 428 |
return fxr; |
| 429 |
} else { |
| 430 |
ikptr bn = ik_safe_alloc(pcb, align(disp_bignum_data + wordsize)); |
| 431 |
ref(bn, 0) = (ikptr) |
| 432 |
(bignum_tag | |
| 433 |
(1 << bignum_sign_shift) | |
| 434 |
(1 << bignum_length_shift)); |
| 435 |
ref(bn, disp_bignum_data) = (ikptr)(-r); |
| 436 |
return verify_bignum(bn+vector_tag, "fxfx-2"); |
| 437 |
} |
| 438 |
} |
| 439 |
} |
| 440 |
|
| 441 |
|
| 442 |
ikptr |
| 443 |
ikrt_bnnegate(ikptr x, ikpcb* pcb){ |
| 444 |
ikptr fst = ref(x, -vector_tag); |
| 445 |
long int limb_count = bnfst_limb_count(fst); |
| 446 |
if(limb_count == 1){ |
| 447 |
if(! bnfst_negative(fst)){ |
| 448 |
/* positive bignum */ |
| 449 |
mp_limb_t limb = |
| 450 |
(mp_limb_t) ref(x, disp_bignum_data - vector_tag); |
| 451 |
if(limb == (most_positive_fixnum + 1)){ |
| 452 |
return fix(-(long int)limb); |
| 453 |
} |
| 454 |
} |
| 455 |
} |
| 456 |
pcb->root0 = &x; |
| 457 |
ikptr bn = ik_safe_alloc(pcb, align(disp_bignum_data + limb_count * wordsize)); |
| 458 |
pcb->root0 = 0; |
| 459 |
memcpy((char*)(long)bn+disp_bignum_data, |
| 460 |
(char*)(long)x-vector_tag+disp_bignum_data, |
| 461 |
limb_count*wordsize); |
| 462 |
ref(bn, 0) = (ikptr) |
| 463 |
(bignum_tag | |
| 464 |
((1 << bignum_sign_shift) - (bignum_sign_mask & (long int)fst)) | |
| 465 |
(limb_count << bignum_length_shift)); |
| 466 |
return verify_bignum(bn+vector_tag, "bnneg"); |
| 467 |
} |
| 468 |
|
| 469 |
ikptr |
| 470 |
ikrt_fxbnminus(ikptr x, ikptr y, ikpcb* pcb){ |
| 471 |
if(x == 0){ return ikrt_bnnegate(y, pcb) ; } |
| 472 |
ikptr fst = ref(y, -vector_tag); |
| 473 |
long int limb_count = bnfst_limb_count(fst); |
| 474 |
long int intx = unfix(x); |
| 475 |
if(intx > 0){ |
| 476 |
if(bnfst_negative(fst)){ |
| 477 |
/* positive fx - negative bn = positive bn */ |
| 478 |
pcb->root0 = &y; |
| 479 |
ikptr r = ik_safe_alloc(pcb, align(disp_bignum_data+(limb_count+1)*wordsize)); |
| 480 |
pcb->root0 = 0; |
| 481 |
long int carry = |
| 482 |
mpn_add_1((mp_limb_t*)(long)(r+disp_bignum_data), |
| 483 |
(mp_limb_t*)(long)(y - vector_tag + disp_bignum_data), |
| 484 |
limb_count, |
| 485 |
intx); |
| 486 |
if(carry){ |
| 487 |
ref(r, disp_bignum_data + limb_count*wordsize) = (ikptr)1; |
| 488 |
ref(r, 0) = (ikptr) |
| 489 |
(((limb_count + 1) << bignum_length_shift) | |
| 490 |
(0 << bignum_sign_shift) | |
| 491 |
bignum_tag); |
| 492 |
return verify_bignum(r+vector_tag, "fxbn-1"); |
| 493 |
} else { |
| 494 |
ref(r, 0) = (ikptr) |
| 495 |
((limb_count << bignum_length_shift) | |
| 496 |
(0 << bignum_sign_shift) | |
| 497 |
bignum_tag); |
| 498 |
return verify_bignum(r+vector_tag, "fxbn-2"); |
| 499 |
} |
| 500 |
} |
| 501 |
else { |
| 502 |
/* positive fx - positive bn = smaller negative bn/fx */ |
| 503 |
pcb->root0 = &y; |
| 504 |
ikptr r = ik_safe_alloc(pcb, align(disp_bignum_data+limb_count*wordsize)); |
| 505 |
pcb->root0 = 0; |
| 506 |
long int borrow = |
| 507 |
mpn_sub_1((mp_limb_t*)(long)(r+disp_bignum_data), |
| 508 |
(mp_limb_t*)(long)(y - vector_tag + disp_bignum_data), |
| 509 |
limb_count, |
| 510 |
intx); |
| 511 |
if(borrow){ |
| 512 |
fprintf(stderr, "Error: BUG in borrow3\n"); |
| 513 |
exit(-1); |
| 514 |
} |
| 515 |
long int result_size = |
| 516 |
(ref(r, disp_bignum_data + (limb_count-1)*wordsize)) |
| 517 |
? limb_count |
| 518 |
: (limb_count - 1); |
| 519 |
if(result_size == 0){ |
| 520 |
return 0; |
| 521 |
} |
| 522 |
if(result_size == 1){ |
| 523 |
unsigned long int last = |
| 524 |
(unsigned long int) ref(r, disp_bignum_data + (result_size-1)*wordsize); |
| 525 |
if(last <= most_negative_fixnum){ |
| 526 |
return fix(-(long int)last); |
| 527 |
} |
| 528 |
} |
| 529 |
ref(r, 0) = (ikptr) |
| 530 |
((result_size << bignum_length_shift) | |
| 531 |
(1 << bignum_sign_shift) | |
| 532 |
bignum_tag); |
| 533 |
return verify_bignum(r+vector_tag, "fxbn-"); |
| 534 |
} |
| 535 |
} |
| 536 |
else { |
| 537 |
if(bnfst_negative(fst)){ |
| 538 |
/* negative fx - negative bn = smaller positive */ |
| 539 |
pcb->root0 = &y; |
| 540 |
ikptr r = ik_safe_alloc(pcb, align(disp_bignum_data+limb_count*wordsize)); |
| 541 |
pcb->root0 = 0; |
| 542 |
long int borrow = |
| 543 |
mpn_sub_1((mp_limb_t*)(long)(r+disp_bignum_data), |
| 544 |
(mp_limb_t*)(long)(y - vector_tag + disp_bignum_data), |
| 545 |
limb_count, |
| 546 |
- intx); |
| 547 |
if(borrow){ |
| 548 |
fprintf(stderr, "Error: BUG in borrow4\n"); |
| 549 |
exit(-1); |
| 550 |
} |
| 551 |
long int result_size = |
| 552 |
(ref(r, disp_bignum_data + (limb_count-1)*wordsize) == 0) |
| 553 |
? (limb_count - 1) |
| 554 |
: limb_count; |
| 555 |
if(result_size == 0){ |
| 556 |
return 0; |
| 557 |
} |
| 558 |
if(result_size == 1){ |
| 559 |
unsigned long int last = |
| 560 |
(unsigned long int) ref(r, disp_bignum_data + (result_size-1)*wordsize); |
| 561 |
if(last <= most_positive_fixnum){ |
| 562 |
return fix((long int)last); |
| 563 |
} |
| 564 |
} |
| 565 |
ref(r, 0) = (ikptr) |
| 566 |
((result_size << bignum_length_shift) | |
| 567 |
(0 << bignum_sign_shift) | |
| 568 |
bignum_tag); |
| 569 |
return verify_bignum(r+vector_tag,"fxbn-"); |
| 570 |
} else { |
| 571 |
/* negative fx - positive bn = larger negative */ |
| 572 |
pcb->root0 = &y; |
| 573 |
ikptr r = ik_safe_alloc(pcb, align(disp_bignum_data+(limb_count+1)*wordsize)); |
| 574 |
pcb->root0 = 0; |
| 575 |
long int carry = |
| 576 |
mpn_add_1((mp_limb_t*)(long)(r+disp_bignum_data), |
| 577 |
(mp_limb_t*)(long)(y - vector_tag + disp_bignum_data), |
| 578 |
limb_count, |
| 579 |
-intx); |
| 580 |
if(carry){ |
| 581 |
ref(r, disp_bignum_data + limb_count*wordsize) = (ikptr)1; |
| 582 |
ref(r, 0) = (ikptr) |
| 583 |
(((limb_count + 1) << bignum_length_shift) | |
| 584 |
(1 << bignum_sign_shift) | |
| 585 |
bignum_tag); |
| 586 |
return verify_bignum(r+vector_tag, "fxbn-"); |
| 587 |
} else { |
| 588 |
ref(r, 0) = (ikptr) |
| 589 |
((limb_count << bignum_length_shift) | |
| 590 |
(1 << bignum_sign_shift) | |
| 591 |
bignum_tag); |
| 592 |
return verify_bignum(r+vector_tag, "fxbn-"); |
| 593 |
} |
| 594 |
} |
| 595 |
} |
| 596 |
} |
| 597 |
|
| 598 |
ikptr |
| 599 |
ikrt_bnfxminus(ikptr x, ikptr y, ikpcb* pcb){ |
| 600 |
if(y == 0){ return x; } |
| 601 |
ikptr fst = ref(x, -vector_tag); |
| 602 |
long int limb_count = bnfst_limb_count(fst); |
| 603 |
long int inty = unfix(y); |
| 604 |
if(inty < 0){ |
| 605 |
if(!bnfst_negative(fst)){ |
| 606 |
/* - negative fx + positive bn = positive bn */ |
| 607 |
pcb->root0 = &x; |
| 608 |
ikptr r = ik_safe_alloc(pcb, align(disp_bignum_data+(limb_count+1)*wordsize)); |
| 609 |
pcb->root0 = 0; |
| 610 |
long int carry = |
| 611 |
mpn_add_1((mp_limb_t*)(long)(r+disp_bignum_data), |
| 612 |
(mp_limb_t*)(long)(x - vector_tag + disp_bignum_data), |
| 613 |
limb_count, |
| 614 |
-inty); |
| 615 |
if(carry){ |
| 616 |
ref(r, disp_bignum_data + limb_count*wordsize) = (ikptr)1; |
| 617 |
ref(r, 0) = (ikptr) |
| 618 |
(((limb_count + 1) << bignum_length_shift) | |
| 619 |
(0 << bignum_sign_shift) | |
| 620 |
bignum_tag); |
| 621 |
return verify_bignum(r+vector_tag,"bnfx-"); |
| 622 |
} else { |
| 623 |
ref(r, 0) = (ikptr) |
| 624 |
((limb_count << bignum_length_shift) | |
| 625 |
(0 << bignum_sign_shift) | |
| 626 |
bignum_tag); |
| 627 |
return verify_bignum(r+vector_tag,"bnfx-"); |
| 628 |
} |
| 629 |
} |
| 630 |
else { |
| 631 |
/* - negative fx + negative bn = smaller negative bn/fx */ |
| 632 |
pcb->root0 = &x; |
| 633 |
ikptr r = ik_safe_alloc(pcb, align(disp_bignum_data+limb_count*wordsize)); |
| 634 |
pcb->root0 = 0; |
| 635 |
long int borrow = |
| 636 |
mpn_sub_1((mp_limb_t*)(long)(r+disp_bignum_data), |
| 637 |
(mp_limb_t*)(long)(x - vector_tag + disp_bignum_data), |
| 638 |
limb_count, |
| 639 |
-inty); |
| 640 |
if(borrow){ |
| 641 |
fprintf(stderr, "Error: BUG in borrow5\n"); |
| 642 |
exit(-1); |
| 643 |
} |
| 644 |
long int result_size = |
| 645 |
(ref(r, disp_bignum_data + (limb_count-1)*wordsize)) |
| 646 |
? limb_count |
| 647 |
: (limb_count - 1); |
| 648 |
if(result_size == 0){ |
| 649 |
return 0; |
| 650 |
} |
| 651 |
if(result_size == 1){ |
| 652 |
unsigned long int last = |
| 653 |
(unsigned long int) ref(r, disp_bignum_data + (result_size-1)*wordsize); |
| 654 |
if(last <= most_negative_fixnum){ |
| 655 |
return fix(-(long int)last); |
| 656 |
} |
| 657 |
} |
| 658 |
ref(r, 0) = (ikptr) |
| 659 |
((result_size << bignum_length_shift) | |
| 660 |
(1 << bignum_sign_shift) | |
| 661 |
bignum_tag); |
| 662 |
return verify_bignum(r+vector_tag,"bnfx-"); |
| 663 |
} |
| 664 |
} |
| 665 |
else { |
| 666 |
if((bignum_sign_mask & (long int)fst) == 0){ |
| 667 |
/* - positive fx + positive bn = smaller positive */ |
| 668 |
pcb->root0 = &x; |
| 669 |
ikptr r = ik_safe_alloc(pcb, align(disp_bignum_data+limb_count*wordsize)); |
| 670 |
pcb->root0 = 0; |
| 671 |
long int borrow = |
| 672 |
mpn_sub_1((mp_limb_t*)(long)(r+disp_bignum_data), |
| 673 |
(mp_limb_t*)(long)(x - vector_tag + disp_bignum_data), |
| 674 |
limb_count, |
| 675 |
inty); |
| 676 |
if(borrow){ |
| 677 |
fprintf(stderr, "Error: BUG in borrow6\n"); |
| 678 |
exit(-1); |
| 679 |
} |
| 680 |
long int result_size = |
| 681 |
(ref(r, disp_bignum_data + (limb_count-1)*wordsize) == 0) |
| 682 |
? (limb_count - 1) |
| 683 |
: limb_count; |
| 684 |
if(result_size == 0){ |
| 685 |
return 0; |
| 686 |
} |
| 687 |
if(result_size == 1){ |
| 688 |
unsigned long int last = |
| 689 |
(unsigned long int) ref(r, disp_bignum_data + (result_size-1)*wordsize); |
| 690 |
if(last <= most_positive_fixnum){ |
| 691 |
return fix((long int)last); |
| 692 |
} |
| 693 |
} |
| 694 |
ref(r, 0) = (ikptr) |
| 695 |
((result_size << bignum_length_shift) | |
| 696 |
(0 << bignum_sign_shift) | |
| 697 |
bignum_tag); |
| 698 |
return verify_bignum(r+vector_tag, "bnfx-"); |
| 699 |
} else { |
| 700 |
/* - positive fx + negative bn = larger negative */ |
| 701 |
pcb->root0 = &x; |
| 702 |
ikptr r = ik_safe_alloc(pcb, align(disp_bignum_data+(limb_count+1)*wordsize)); |
| 703 |
pcb->root0 = 0; |
| 704 |
long int carry = |
| 705 |
mpn_add_1((mp_limb_t*)(long)(r+disp_bignum_data), |
| 706 |
(mp_limb_t*)(long)(x - vector_tag + disp_bignum_data), |
| 707 |
limb_count, |
| 708 |
inty); |
| 709 |
if(carry){ |
| 710 |
ref(r, disp_bignum_data + limb_count*wordsize) = (ikptr)1; |
| 711 |
ref(r, 0) = (ikptr) |
| 712 |
(((limb_count + 1) << bignum_length_shift) | |
| 713 |
(1 << bignum_sign_shift) | |
| 714 |
bignum_tag); |
| 715 |
return verify_bignum(r+vector_tag, "bnfx-"); |
| 716 |
} else { |
| 717 |
ref(r, 0) = (ikptr) |
| 718 |
((limb_count << bignum_length_shift) | |
| 719 |
(1 << bignum_sign_shift) | |
| 720 |
bignum_tag); |
| 721 |
return verify_bignum(r+vector_tag, "bnfx-"); |
| 722 |
} |
| 723 |
} |
| 724 |
} |
| 725 |
} |
| 726 |
|
| 727 |
|
| 728 |
|
| 729 |
ikptr |
| 730 |
ikrt_bnbnminus(ikptr x, ikptr y, ikpcb* pcb){ |
| 731 |
if(x == y) { return 0; } |
| 732 |
unsigned long int xfst = (unsigned long int)ref(x, -vector_tag); |
| 733 |
unsigned long int yfst = (unsigned long int)ref(y, -vector_tag); |
| 734 |
long int xsign = xfst & bignum_sign_mask; |
| 735 |
long int ysign = yfst & bignum_sign_mask; |
| 736 |
long int xlimbs = xfst >> bignum_length_shift; |
| 737 |
long int ylimbs = yfst >> bignum_length_shift; |
| 738 |
if(xsign != ysign){ |
| 739 |
long int n1,n2; |
| 740 |
ikptr s1,s2; |
| 741 |
if(xlimbs >= ylimbs){ |
| 742 |
n1 = xlimbs; n2 = ylimbs; s1 = x; s2 = y; |
| 743 |
} else { |
| 744 |
n1 = ylimbs; n2 = xlimbs; s1 = y; s2 = x; |
| 745 |
} |
| 746 |
pcb->root0 = &s1; |
| 747 |
pcb->root1 = &s2; |
| 748 |
ikptr res = ik_safe_alloc(pcb, align(disp_bignum_data + (n1+1)*wordsize)); |
| 749 |
pcb->root0 = 0; |
| 750 |
pcb->root1 = 0; |
| 751 |
mp_limb_t carry = |
| 752 |
mpn_add((mp_limb_t*)(long)(res+disp_bignum_data), |
| 753 |
(mp_limb_t*)(long)(s1-vector_tag+disp_bignum_data), |
| 754 |
n1, |
| 755 |
(mp_limb_t*)(long)(s2-vector_tag+disp_bignum_data), |
| 756 |
n2); |
| 757 |
if(carry){ |
| 758 |
ref(res, disp_vector_data + xlimbs*wordsize) = (ikptr)1; |
| 759 |
ref(res, 0) = (ikptr) |
| 760 |
(((n1+1) << bignum_length_shift) | |
| 761 |
xsign | |
| 762 |
bignum_tag); |
| 763 |
return verify_bignum(res+vector_tag, "bnbn-"); |
| 764 |
} else { |
| 765 |
ref(res, 0) = (ikptr) |
| 766 |
((n1 << bignum_length_shift) | |
| 767 |
xsign | |
| 768 |
bignum_tag); |
| 769 |
return verify_bignum(res+vector_tag, "bnbn-"); |
| 770 |
} |
| 771 |
} |
| 772 |
else { |
| 773 |
/* same sign */ |
| 774 |
if(xlimbs == ylimbs){ |
| 775 |
while((ref(x, -vector_tag+disp_bignum_data+(xlimbs-1)*wordsize) == |
| 776 |
ref(y, -vector_tag+disp_bignum_data+(xlimbs-1)*wordsize))){ |
| 777 |
xlimbs -= 1; |
| 778 |
if(xlimbs == 0){ return 0; } |
| 779 |
} |
| 780 |
ylimbs = xlimbs; |
| 781 |
} |
| 782 |
ikptr s1=x, s2=y; |
| 783 |
long int n1=xlimbs, n2=ylimbs; |
| 784 |
long int result_sign = xsign; |
| 785 |
/* |x| != |y| */ |
| 786 |
if(xlimbs <= ylimbs){ |
| 787 |
if(xlimbs == ylimbs){ |
| 788 |
if((ref(y, -vector_tag+disp_bignum_data+(xlimbs-1)*wordsize) > |
| 789 |
ref(x, -vector_tag+disp_bignum_data+(xlimbs-1)*wordsize))){ |
| 790 |
s1 = y; n1 = ylimbs; |
| 791 |
s2 = x; n2 = xlimbs; |
| 792 |
result_sign = (1 << bignum_sign_shift) - ysign; |
| 793 |
} |
| 794 |
} else { |
| 795 |
s1 = y; n1 = ylimbs; |
| 796 |
s2 = x; n2 = xlimbs; |
| 797 |
result_sign = (1 << bignum_sign_shift) - ysign; |
| 798 |
} |
| 799 |
} |
| 800 |
/* |s1| > |s2| */ |
| 801 |
pcb->root0 = &s1; |
| 802 |
pcb->root1 = &s2; |
| 803 |
ikptr res = ik_safe_alloc(pcb, align(disp_bignum_data + n1 * wordsize)); |
| 804 |
bzero((void*)(res+disp_bignum_data), n1*wordsize); |
| 805 |
pcb->root0 = 0; |
| 806 |
pcb->root1 = 0; |
| 807 |
long int burrow = |
| 808 |
mpn_sub((mp_limb_t*)(long)(res + disp_bignum_data), |
| 809 |
(mp_limb_t*)(long)(s1 - vector_tag + disp_bignum_data), |
| 810 |
n1, |
| 811 |
(mp_limb_t*)(long)(s2 - vector_tag + disp_bignum_data), |
| 812 |
n2); |
| 813 |
if(burrow){ |
| 814 |
fprintf(stderr, "BUG: Burrow error in bnbn-\n"); |
| 815 |
exit(-1); |
| 816 |
} |
| 817 |
long int len = n1; |
| 818 |
while(ref(res, disp_bignum_data + (len-1)*wordsize) == 0){ |
| 819 |
len--; |
| 820 |
if(len == 0){ |
| 821 |
return 0; |
| 822 |
} |
| 823 |
} |
| 824 |
if(result_sign == 0){ |
| 825 |
/* positive result */ |
| 826 |
if(len == 1){ |
| 827 |
unsigned long int fst_limb = |
| 828 |
(unsigned long int) ref(res, disp_bignum_data); |
| 829 |
if(fst_limb <= most_positive_fixnum){ |
| 830 |
return fix((long int)fst_limb); |
| 831 |
} |
| 832 |
} |
| 833 |
ref(res, 0) = (ikptr) |
| 834 |
((len << bignum_length_shift) | |
| 835 |
result_sign | |
| 836 |
bignum_tag); |
| 837 |
return verify_bignum(res+vector_tag, "bnbn-"); |
| 838 |
} else { |
| 839 |
/* negative result */ |
| 840 |
if(len == 1){ |
| 841 |
unsigned long int fst_limb = |
| 842 |
(unsigned long int) ref(res, disp_bignum_data); |
| 843 |
if(fst_limb <= most_negative_fixnum){ |
| 844 |
return fix(-(long int)fst_limb); |
| 845 |
} |
| 846 |
} |
| 847 |
ref(res, 0) = (ikptr) |
| 848 |
((len << bignum_length_shift) | |
| 849 |
result_sign | |
| 850 |
bignum_tag); |
| 851 |
return verify_bignum(res+vector_tag, "bnbn-"); |
| 852 |
} |
| 853 |
} |
| 854 |
} |
| 855 |
|
| 856 |
|
| 857 |
ikptr |
| 858 |
ikrt_fxfxmult(ikptr x, ikptr y, ikpcb* pcb){ |
| 859 |
long int n1 = unfix(x); |
| 860 |
long int n2 = unfix(y); |
| 861 |
mp_limb_t lo = 0; |
| 862 |
mp_limb_t s1 = n1; |
| 863 |
mp_limb_t s2 = n2; |
| 864 |
long int sign = 0; |
| 865 |
if(n1 < 0){ |
| 866 |
s1 = -n1; |
| 867 |
sign = 1 - sign; |
| 868 |
} |
| 869 |
if(n2 < 0){ |
| 870 |
s2 = -n2; |
| 871 |
sign = 1 - sign; |
| 872 |
} |
| 873 |
mp_limb_t hi = mpn_mul_1(&lo, &s1, 1, s2); |
| 874 |
if(hi == 0){ |
| 875 |
if(sign){ |
| 876 |
if(lo <= most_negative_fixnum){ |
| 877 |
return fix(-((long int)lo)); |
| 878 |
} |
| 879 |
} else { |
| 880 |
if(lo <= most_positive_fixnum){ |
| 881 |
return fix((long int)lo); |
| 882 |
} |
| 883 |
} |
| 884 |
ikptr r = ik_safe_alloc(pcb, disp_bignum_data + wordsize); |
| 885 |
ref(r, 0) = (ikptr) |
| 886 |
(bignum_tag | |
| 887 |
(sign << bignum_sign_shift) | |
| 888 |
(1 << bignum_length_shift)); |
| 889 |
ref(r, disp_bignum_data) = (ikptr)lo; |
| 890 |
return BN(r+vector_tag); |
| 891 |
} else { |
| 892 |
ikptr r = ik_safe_alloc(pcb, align(disp_bignum_data + 2*wordsize)); |
| 893 |
ref(r, 0) = (ikptr) |
| 894 |
(bignum_tag | |
| 895 |
(sign << bignum_sign_shift) | |
| 896 |
(2 << bignum_length_shift)); |
| 897 |
ref(r, disp_bignum_data) = (ikptr)lo; |
| 898 |
ref(r, disp_bignum_data+wordsize) = (ikptr)hi; |
| 899 |
return BN(r+vector_tag); |
| 900 |
} |
| 901 |
} |
| 902 |
|
| 903 |
ikptr |
| 904 |
normalize_bignum(long int limbs, int sign, ikptr r){ |
| 905 |
while(ref(r, disp_bignum_data + (limbs-1)*wordsize) == 0){ |
| 906 |
limbs--; |
| 907 |
if(limbs == 0){ return 0;} |
| 908 |
} |
| 909 |
if(limbs == 1){ |
| 910 |
mp_limb_t last = (mp_limb_t) ref(r, disp_bignum_data); |
| 911 |
if(sign == 0){ |
| 912 |
if(last <= most_positive_fixnum){ |
| 913 |
return fix(last); |
| 914 |
} |
| 915 |
} else { |
| 916 |
if(last <= most_negative_fixnum){ |
| 917 |
return fix(-(last)); |
| 918 |
} |
| 919 |
} |
| 920 |
} |
| 921 |
ref(r, 0) = (ikptr) (bignum_tag | sign | (limbs << bignum_length_shift)); |
| 922 |
return BN(r+vector_tag); |
| 923 |
} |
| 924 |
|
| 925 |
|
| 926 |
ikptr |
| 927 |
ikrt_fxbnmult(ikptr x, ikptr y, ikpcb* pcb){ |
| 928 |
long int n2 = unfix(x); |
| 929 |
if(n2 == 0) { return 0; } |
| 930 |
mp_limb_t s2 = (n2>0) ? n2 : (- n2); |
| 931 |
ikptr fst = ref(y, -vector_tag); |
| 932 |
long int limb_count = bnfst_limb_count(fst); |
| 933 |
pcb->root0 = &y; |
| 934 |
ikptr r = ik_safe_alloc(pcb, align(disp_bignum_data + (limb_count+1)*wordsize)); |
| 935 |
pcb->root0 = 0; |
| 936 |
mp_limb_t hi = mpn_mul_1((mp_limb_t*)(long)(r+disp_bignum_data), |
| 937 |
(mp_limb_t*)(long)(y-vector_tag+disp_bignum_data), |
| 938 |
limb_count, |
| 939 |
s2); |
| 940 |
ref(r, disp_bignum_data + limb_count * wordsize) = (ikptr)hi; |
| 941 |
long int sign = |
| 942 |
((n2 > 0) ? |
| 943 |
(bignum_sign_mask & (long int)fst) : |
| 944 |
((1 << bignum_sign_shift) - (bignum_sign_mask&(long int)fst))); |
| 945 |
return normalize_bignum(limb_count+1, sign, r); |
| 946 |
} |
| 947 |
|
| 948 |
ikptr |
| 949 |
ikrt_bnbnmult(ikptr x, ikptr y, ikpcb* pcb){ |
| 950 |
long int f1 = (long int)ref(x, -vector_tag); |
| 951 |
long int f2 = (long int)ref(y, -vector_tag); |
| 952 |
long int n1 = bnfst_limb_count(f1); |
| 953 |
long int n2 = bnfst_limb_count(f2); |
| 954 |
long int nr = n1 + n2; |
| 955 |
pcb->root0 = &x; |
| 956 |
pcb->root1 = &y; |
| 957 |
ikptr bn = ik_safe_alloc(pcb, align(disp_bignum_data + nr*wordsize)); |
| 958 |
pcb->root0 = 0; |
| 959 |
pcb->root1 = 0; |
| 960 |
mp_limb_t r; |
| 961 |
if(n1 >= n2){ |
| 962 |
r = mpn_mul((mp_limb_t*)(long)(bn+disp_bignum_data), |
| 963 |
(mp_limb_t*)(long)(x-vector_tag+disp_bignum_data), |
| 964 |
n1, |
| 965 |
(mp_limb_t*)(long)(y-vector_tag+disp_bignum_data), |
| 966 |
n2); |
| 967 |
} else { |
| 968 |
r = mpn_mul((mp_limb_t*)(long)(bn+disp_bignum_data), |
| 969 |
(mp_limb_t*)(long)(y-vector_tag+disp_bignum_data), |
| 970 |
n2, |
| 971 |
(mp_limb_t*)(long)(x-vector_tag+disp_bignum_data), |
| 972 |
n1); |
| 973 |
} |
| 974 |
long int sign = |
| 975 |
((bignum_sign_mask & f1) ? |
| 976 |
((1 << bignum_sign_shift) - (bignum_sign_mask & f2)) : |
| 977 |
(bignum_sign_mask & f2)); |
| 978 |
return normalize_bignum(nr, sign, bn); |
| 979 |
} |
| 980 |
|
| 981 |
|
| 982 |
|
| 983 |
|
| 984 |
ikptr |
| 985 |
ikrt_bnbncomp(ikptr bn1, ikptr bn2){ |
| 986 |
ikptr f1 = ref(bn1, -vector_tag); |
| 987 |
ikptr f2 = ref(bn2, -vector_tag); |
| 988 |
if(bnfst_negative(f1)){ |
| 989 |
if(bnfst_negative(f2)){ |
| 990 |
/* both negative */ |
| 991 |
long int n1 = ((mp_limb_t) f1) >> bignum_length_shift; |
| 992 |
long int n2 = ((mp_limb_t) f2) >> bignum_length_shift; |
| 993 |
if(n1 < n2) { |
| 994 |
return fix(1); |
| 995 |
} else if(n1 > n2){ |
| 996 |
return fix(-1); |
| 997 |
} else { |
| 998 |
long int i; |
| 999 |
for(i=(n1-1); i>=0; i--){ |
| 1000 |
mp_limb_t t1 = |
| 1001 |
(mp_limb_t) ref(bn1,disp_bignum_data-vector_tag+i*wordsize); |
| 1002 |
mp_limb_t t2 = |
| 1003 |
(mp_limb_t) ref(bn2,disp_bignum_data-vector_tag+i*wordsize); |
| 1004 |
if(t1 < t2){ |
| 1005 |
return fix(1); |
| 1006 |
} else if(t1 > t2){ |
| 1007 |
return fix(-1); |
| 1008 |
} |
| 1009 |
} |
| 1010 |
} |
| 1011 |
return 0; |
| 1012 |
} else { |
| 1013 |
/* n1 negative, n2 positive */ |
| 1014 |
return fix(-1); |
| 1015 |
} |
| 1016 |
} else { |
| 1017 |
if(bnfst_negative(f2)){ |
| 1018 |
/* n1 positive, n2 negative */ |
| 1019 |
return fix(1); |
| 1020 |
} else { |
| 1021 |
/* both positive */ |
| 1022 |
long int n1 = ((mp_limb_t) f1) >> bignum_length_shift; |
| 1023 |
long int n2 = ((mp_limb_t) f2) >> bignum_length_shift; |
| 1024 |
if(n1 < n2) { |
| 1025 |
return fix(-1); |
| 1026 |
} else if(n1 > n2){ |
| 1027 |
return fix(1); |
| 1028 |
} else { |
| 1029 |
long int i; |
| 1030 |
for(i=(n1-1); i>=0; i--){ |
| 1031 |
mp_limb_t t1 = |
| 1032 |
(mp_limb_t) ref(bn1,disp_bignum_data-vector_tag+i*wordsize); |
| 1033 |
mp_limb_t t2 = |
| 1034 |
(mp_limb_t) ref(bn2,disp_bignum_data-vector_tag+i*wordsize); |
| 1035 |
if(t1 < t2){ |
| 1036 |
return fix(-1); |
| 1037 |
} else if(t1 > t2){ |
| 1038 |
return fix(1); |
| 1039 |
} |
| 1040 |
} |
| 1041 |
} |
| 1042 |
return 0; |
| 1043 |
} |
| 1044 |
} |
| 1045 |
} |
| 1046 |
|
| 1047 |
|
| 1048 |
static inline int |
| 1049 |
count_leading_ffs(int n, mp_limb_t* x){ |
| 1050 |
int idx; |
| 1051 |
for(idx=0; idx<n; idx++){ |
| 1052 |
if(x[idx] != (mp_limb_t)-1){ |
| 1053 |
return idx; |
| 1054 |
} |
| 1055 |
} |
| 1056 |
return n; |
| 1057 |
} |
| 1058 |
|
| 1059 |
|
| 1060 |
static void |
| 1061 |
copy_limbs(mp_limb_t* src, mp_limb_t* dst, int n1, int n2){ |
| 1062 |
while(n1 < n2){ |
| 1063 |
dst[n1] = src[n1]; |
| 1064 |
n1++; |
| 1065 |
} |
| 1066 |
} |
| 1067 |
|
| 1068 |
static void |
| 1069 |
bits_compliment(mp_limb_t* src, mp_limb_t* dst, long int n){ |
| 1070 |
mp_limb_t carry = 1; |
| 1071 |
long int i; |
| 1072 |
for(i=0; i<n; i++){ |
| 1073 |
mp_limb_t d = src[i]; |
| 1074 |
mp_limb_t c = carry + ~ d; |
| 1075 |
dst[i] = c; |
| 1076 |
carry = (carry && ! d); |
| 1077 |
} |
| 1078 |
} |
| 1079 |
|
| 1080 |
static void |
| 1081 |
bits_compliment2(mp_limb_t* src, mp_limb_t* dst, int n1, int n2){ |
| 1082 |
mp_limb_t carry = 1; |
| 1083 |
int i; |
| 1084 |
for(i=0; i<n1; i++){ |
| 1085 |
mp_limb_t d = src[i]; |
| 1086 |
mp_limb_t c = carry + ~ d; |
| 1087 |
dst[i] = c; |
| 1088 |
carry = (carry && ! d); |
| 1089 |
} |
| 1090 |
for(i=n1; i<n2; i++){ |
| 1091 |
mp_limb_t d = 0; |
| 1092 |
mp_limb_t c = carry + ~ d; |
| 1093 |
dst[i] = c; |
| 1094 |
carry = (carry && ! d); |
| 1095 |
} |
| 1096 |
} |
| 1097 |
|
| 1098 |
static int |
| 1099 |
bits_compliment_carry(mp_limb_t* src, mp_limb_t* dst, int n1, int n2, mp_limb_t carry){ |
| 1100 |
int i; |
| 1101 |
for(i=n1; i<n2; i++){ |
| 1102 |
mp_limb_t d = src[i]; |
| 1103 |
mp_limb_t c = carry + ~ d; |
| 1104 |
dst[i] = c; |
| 1105 |
carry = (carry && ! d); |
| 1106 |
} |
| 1107 |
return carry; |
| 1108 |
} |
| 1109 |
|
| 1110 |
|
| 1111 |
|
| 1112 |
|
| 1113 |
static void |
| 1114 |
bits_compliment_with_carry(mp_limb_t* src, mp_limb_t* dst, long int n, long int carry){ |
| 1115 |
long int i; |
| 1116 |
for(i=0; i<n; i++){ |
| 1117 |
mp_limb_t d = src[i]; |
| 1118 |
mp_limb_t c = carry + ~ d; |
| 1119 |
dst[i] = c; |
| 1120 |
carry = (carry && ! d); |
| 1121 |
} |
| 1122 |
} |
| 1123 |
|
| 1124 |
static void |
| 1125 |
bits_compliment_logand(mp_limb_t* s1, mp_limb_t* s2, mp_limb_t* dst, int n){ |
| 1126 |
int carry = 1; |
| 1127 |
int i; |
| 1128 |
for(i=0; i<n; i++){ |
| 1129 |
mp_limb_t d = s1[i]; |
| 1130 |
mp_limb_t c = carry + ~ d; |
| 1131 |
dst[i] = c & s2[i]; |
| 1132 |
carry = (carry && ! d); |
| 1133 |
} |
| 1134 |
} |
| 1135 |
|
| 1136 |
|
| 1137 |
|
| 1138 |
static int |
| 1139 |
bits_compliment_logor(mp_limb_t* s1, mp_limb_t* s2, mp_limb_t* dst, int n){ |
| 1140 |
int carry = 1; |
| 1141 |
int i; |
| 1142 |
for(i=0; i<n; i++){ |
| 1143 |
mp_limb_t d = s1[i]; |
| 1144 |
mp_limb_t c = carry + ~ d; |
| 1145 |
dst[i] = c | s2[i]; |
| 1146 |
carry = (carry && ! d); |
| 1147 |
} |
| 1148 |
return carry; |
| 1149 |
} |
| 1150 |
|
| 1151 |
|
| 1152 |
static long int |
| 1153 |
bits_carry(mp_limb_t* s, int n){ |
| 1154 |
/* |
| 1155 |
int carry = 1; |
| 1156 |
int i; |
| 1157 |
for(i=0; i<n; i++){ |
| 1158 |
mp_limb_t d = s[i]; |
| 1159 |
carry = (carry && ! d); |
| 1160 |
} |
| 1161 |
return carry; |
| 1162 |
*/ |
| 1163 |
int i; |
| 1164 |
for(i=0; i<n; i++){ |
| 1165 |
if (s[i] != 0){ |
| 1166 |
return 0; |
| 1167 |
} |
| 1168 |
} |
| 1169 |
return 1; |
| 1170 |
} |
| 1171 |
|
| 1172 |
ikptr |
| 1173 |
ikrt_bnlognot(ikptr x, ikpcb* pcb){ |
| 1174 |
ikptr fst = ref(x, -vector_tag); |
| 1175 |
long int n = bnfst_limb_count(fst); |
| 1176 |
if(bnfst_negative(fst)){ |
| 1177 |
/* negative */ |
| 1178 |
pcb->root0 = &x; |
| 1179 |
ikptr r = ik_safe_alloc(pcb, align(disp_bignum_data + n*wordsize)); |
| 1180 |
pcb->root0 = 0; |
| 1181 |
mp_limb_t* s1 = (mp_limb_t*)(long)(x+disp_bignum_data-vector_tag); |
| 1182 |
mp_limb_t* rd = (mp_limb_t*)(long)(r+disp_bignum_data); |
| 1183 |
int i; |
| 1184 |
for(i=0; (i<n) && (s1[i] == 0); i++) { |
| 1185 |
rd[i] = -1; |
| 1186 |
} |
| 1187 |
rd[i] = s1[i] - 1; |
| 1188 |
for(i++; i<n; i++){ |
| 1189 |
rd[i] = s1[i]; |
| 1190 |
} |
| 1191 |
return normalize_bignum(n, 0, r); |
| 1192 |
} else { |
| 1193 |
/* positive */ |
| 1194 |
long int i; |
| 1195 |
mp_limb_t* s1 = (mp_limb_t*)(long)(x+disp_bignum_data-vector_tag); |
| 1196 |
for(i=0; (i<n) && (s1[i] == (mp_limb_t)-1); i++) {/*nothing*/} |
| 1197 |
if(i==n){ |
| 1198 |
pcb->root0 = &x; |
| 1199 |
ikptr r = ik_safe_alloc(pcb, align(disp_bignum_data + (n+1)*wordsize)); |
| 1200 |
pcb->root0 = 0; |
| 1201 |
bzero((char*)(long)r+disp_bignum_data, n*wordsize); |
| 1202 |
((mp_limb_t*)(long)(r+disp_bignum_data))[n] = 1; |
| 1203 |
ref(r, 0) = (ikptr) |
| 1204 |
(bignum_tag | (1<<bignum_sign_shift) | ((n+1) << bignum_length_shift)); |
| 1205 |
return r+vector_tag; |
| 1206 |
} else { |
| 1207 |
pcb->root0 = &x; |
| 1208 |
ikptr r = ik_safe_alloc(pcb, align(disp_bignum_data + n*wordsize)); |
| 1209 |
pcb->root0 = 0; |
| 1210 |
mp_limb_t* s1 = (mp_limb_t*)(long)(x+disp_bignum_data-vector_tag); |
| 1211 |
mp_limb_t* rd = (mp_limb_t*)(long)(r+disp_bignum_data); |
| 1212 |
int j; |
| 1213 |
for(j=0; j<i; j++){ rd[j] = 0; } |
| 1214 |
rd[i] = s1[i] + 1; |
| 1215 |
for(j=i+1; j<n; j++){ rd[j] = s1[j]; } |
| 1216 |
ref(r, 0) = (ikptr) |
| 1217 |
(bignum_tag | (1<<bignum_sign_shift) | (n << bignum_length_shift)); |
| 1218 |
return r+vector_tag; |
| 1219 |
} |
| 1220 |
} |
| 1221 |
} |
| 1222 |
|
| 1223 |
|
| 1224 |
ikptr |
| 1225 |
ikrt_fxbnlogand(ikptr x, ikptr y, ikpcb* pcb){ |
| 1226 |
long int n1 = unfix(x); |
| 1227 |
ikptr fst = ref(y, -vector_tag); |
| 1228 |
if(n1 >= 0){ |
| 1229 |
/* x is positive */ |
| 1230 |
if(bnfst_negative(fst)){ |
| 1231 |
/* y is negative */ |
| 1232 |
return fix(n1 & (1+~(long int)ref(y, disp_vector_data-vector_tag))); |
| 1233 |
} else { |
| 1234 |
/* y is positive */ |
| 1235 |
return fix(n1 & (long int)ref(y, disp_vector_data-vector_tag)); |
| 1236 |
} |
| 1237 |
} else { |
| 1238 |
/* x is negative */ |
| 1239 |
if(n1 == -1){ return y; } |
| 1240 |
if(bnfst_negative(fst)){ |
| 1241 |
/* y is negative */ |
| 1242 |
long int len = bnfst_limb_count(fst); |
| 1243 |
pcb->root0 = &y; |
| 1244 |
ikptr r = ik_safe_alloc(pcb, align(disp_bignum_data + (len+1)*wordsize)); |
| 1245 |
pcb->root0 = 0; |
| 1246 |
mp_limb_t* s2 = (mp_limb_t*)(long)(y+disp_bignum_data-vector_tag); |
| 1247 |
mp_limb_t* s = (mp_limb_t*)(long)(r+disp_bignum_data); |
| 1248 |
bits_compliment2(s2, s, len, len+1); |
| 1249 |
s[0] = s[0] & n1; |
| 1250 |
bits_compliment2(s, s, len+1, len+1); |
| 1251 |
return normalize_bignum(len+1, 1<<bignum_sign_shift, r); |
| 1252 |
} else { |
| 1253 |
/* y is positive */ |
| 1254 |
long int len = bnfst_limb_count(fst); |
| 1255 |
pcb->root0 = &y; |
| 1256 |
ikptr r = ik_safe_alloc(pcb, align(disp_bignum_data + len * wordsize)); |
| 1257 |
pcb->root0 = 0; |
| 1258 |
ref(r, 0) = fst; |
| 1259 |
ref(r, disp_bignum_data) = (ikptr) |
| 1260 |
(((long int)ref(y, disp_bignum_data - vector_tag)) & n1); |
| 1261 |
int i; |
| 1262 |
for(i=1; i<len; i++){ |
| 1263 |
ref(r, disp_bignum_data+i*wordsize) = |
| 1264 |
ref(y, disp_bignum_data-vector_tag+i*wordsize); |
| 1265 |
} |
| 1266 |
return BN(r+vector_tag); |
| 1267 |
} |
| 1268 |
} |
| 1269 |
} |
| 1270 |
|
| 1271 |
ikptr |
| 1272 |
ikrt_bnbnlogand(ikptr x, ikptr y, ikpcb* pcb){ |
| 1273 |
ikptr xfst = ref(x, -vector_tag); |
| 1274 |
ikptr yfst = ref(y, -vector_tag); |
| 1275 |
long int n1 = bnfst_limb_count(xfst); |
| 1276 |
long int n2 = bnfst_limb_count(yfst); |
| 1277 |
if(bnfst_negative(xfst)){ |
| 1278 |
if(bnfst_negative(yfst)){ |
| 1279 |
if(n1 >= n2){ |
| 1280 |
pcb->root0 = &x; |
| 1281 |
pcb->root1 = &y; |
| 1282 |
ikptr r = ik_safe_alloc(pcb, align(disp_bignum_data + (n1+1)*wordsize)); |
| 1283 |
pcb->root0 = 0; |
| 1284 |
pcb->root1 = 0; |
| 1285 |
mp_limb_t* s1 = (mp_limb_t*)(long)(x+disp_bignum_data-vector_tag); |
| 1286 |
mp_limb_t* s2 = (mp_limb_t*)(long)(y+disp_bignum_data-vector_tag); |
| 1287 |
mp_limb_t* s = (mp_limb_t*)(long)(r+disp_bignum_data); |
| 1288 |
bits_compliment2(s1, s, n1, n1+1); |
| 1289 |
bits_compliment_logand(s2, s, s, n2); |
| 1290 |
bits_compliment2(s, s, n1+1, n1+1); |
| 1291 |
return normalize_bignum(n1+1, 1<<bignum_sign_shift, r); |
| 1292 |
} else { |
| 1293 |
return ikrt_bnbnlogand(y,x,pcb); |
| 1294 |
} |
| 1295 |
} else { |
| 1296 |
return ikrt_bnbnlogand(y,x,pcb); |
| 1297 |
} |
| 1298 |
} else { |
| 1299 |
if(bnfst_negative(yfst)){ |
| 1300 |
/* x positive, y negative */ |
| 1301 |
/* the result is at most n1 words long */ |
| 1302 |
pcb->root0 = &x; |
| 1303 |
pcb->root1 = &y; |
| 1304 |
ikptr r = ik_safe_alloc(pcb, align(disp_bignum_data + n1*wordsize)); |
| 1305 |
pcb->root0 = 0; |
| 1306 |
pcb->root1 = 0; |
| 1307 |
mp_limb_t* s1 = (mp_limb_t*)(long)(x+disp_bignum_data-vector_tag); |
| 1308 |
mp_limb_t* s2 = (mp_limb_t*)(long)(y+disp_bignum_data-vector_tag); |
| 1309 |
mp_limb_t* s = (mp_limb_t*)(long)(r+disp_bignum_data); |
| 1310 |
if(n1 <= n2){ |
| 1311 |
bits_compliment_logand(s2, s1, s, n1); |
| 1312 |
} else { |
| 1313 |
bits_compliment_logand(s2, s1, s, n2); |
| 1314 |
copy_limbs(s1, s, n2, n1); |
| 1315 |
} |
| 1316 |
return normalize_bignum(n1, 0, r); |
| 1317 |
} else { |
| 1318 |
/* both positive */ |
| 1319 |
int n = (n1<n2)?n1:n2; |
| 1320 |
long int i; |
| 1321 |
for(i=n-1; i>=0; i--){ |
| 1322 |
long int l1 = |
| 1323 |
(long int) ref(x, disp_bignum_data-vector_tag+i*wordsize); |
| 1324 |
long int l2 = |
| 1325 |
(long int) ref(y, disp_bignum_data-vector_tag+i*wordsize); |
| 1326 |
unsigned long int last = l1 & l2; |
| 1327 |
if(last){ |
| 1328 |
if((i == 0) && (last < most_positive_fixnum)){ |
| 1329 |
return fix(last); |
| 1330 |
} |
| 1331 |
pcb->root0 = &x; |
| 1332 |
pcb->root1 = &y; |
| 1333 |
ikptr r = ik_safe_alloc(pcb, align(disp_bignum_data+(i+1)*wordsize)); |
| 1334 |
pcb->root0 = 0; |
| 1335 |
pcb->root1 = 0; |
| 1336 |
ref(r, 0) = (ikptr) (bignum_tag | ((i+1)<<bignum_length_shift)); |
| 1337 |
ref(r, disp_bignum_data + i*wordsize) = (ikptr)last; |
| 1338 |
int j; |
| 1339 |
for(j=0; j<i; j++){ |
| 1340 |
ref(r, disp_bignum_data + j*wordsize) = (ikptr) |
| 1341 |
(((long int)ref(x, disp_bignum_data-vector_tag+j*wordsize)) |
| 1342 |
& |
| 1343 |
((long int)ref(y, disp_bignum_data-vector_tag+j*wordsize))); |
| 1344 |
} |
| 1345 |
return r+vector_tag; |
| 1346 |
} |
| 1347 |
} |
| 1348 |
return 0; |
| 1349 |
} |
| 1350 |
} |
| 1351 |
} |
| 1352 |
|
| 1353 |
|
| 1354 |
ikptr |
| 1355 |
ikrt_fxbnlogor(ikptr x, ikptr y, ikpcb* pcb){ |
| 1356 |
long int n1 = unfix(x); |
| 1357 |
ikptr fst = ref(y, -vector_tag); |
| 1358 |
if(n1 < 0){ |
| 1359 |
/* x is negative */ |
| 1360 |
if(bnfst_negative(fst)){ |
| 1361 |
/* y is negative */ |
| 1362 |
return fix(n1 | (1+~(long int)ref(y, disp_vector_data-vector_tag))); |
| 1363 |
} else { |
| 1364 |
/* y is positive */ |
| 1365 |
return fix(n1 | (long int)ref(y, disp_vector_data-vector_tag)); |
| 1366 |
} |
| 1367 |
} else { |
| 1368 |
/* x is non negative */ |
| 1369 |
if(n1 == 0){ return y; } |
| 1370 |
/* x is positive */ |
| 1371 |
if(bnfst_negative(fst)){ |
| 1372 |
/* y is negative */ |
| 1373 |
long int len = bnfst_limb_count(fst); |
| 1374 |
pcb->root0 = &y; |
| 1375 |
ikptr r = ik_safe_alloc(pcb, align(disp_bignum_data + (len+1)*wordsize)); |
| 1376 |
pcb->root0 = 0; |
| 1377 |
mp_limb_t* s2 = (mp_limb_t*)(long)(y+disp_bignum_data-vector_tag); |
| 1378 |
mp_limb_t* s = (mp_limb_t*)(long)(r+disp_bignum_data); |
| 1379 |
bits_compliment2(s2, s, len, len+1); |
| 1380 |
s[0] = s[0] | n1; |
| 1381 |
bits_compliment2(s, s, len+1, len+1); |
| 1382 |
return normalize_bignum(len+1, 1<<bignum_sign_shift, r); |
| 1383 |
} else { |
| 1384 |
/* y is positive */ |
| 1385 |
long int len = bnfst_limb_count(fst); |
| 1386 |
pcb->root0 = &y; |
| 1387 |
ikptr r = ik_safe_alloc(pcb, align(disp_bignum_data + len * wordsize)); |
| 1388 |
pcb->root0 = 0; |
| 1389 |
ref(r, 0) = fst; |
| 1390 |
ref(r, disp_bignum_data) = (ikptr) |
| 1391 |
(((long int)ref(y, disp_bignum_data - vector_tag)) | n1); |
| 1392 |
int i; |
| 1393 |
for(i=1; i<len; i++){ |
| 1394 |
ref(r, disp_bignum_data+i*wordsize) = |
| 1395 |
ref(y, disp_bignum_data-vector_tag+i*wordsize); |
| 1396 |
} |
| 1397 |
return BN(r+vector_tag); |
| 1398 |
} |
| 1399 |
} |
| 1400 |
} |
| 1401 |
|
| 1402 |
ikptr |
| 1403 |
ikrt_bnbnlogor(ikptr x, ikptr y, ikpcb* pcb){ |
| 1404 |
ikptr xfst = ref(x, -vector_tag); |
| 1405 |
ikptr yfst = ref(y, -vector_tag); |
| 1406 |
long int n1 = bnfst_limb_count(xfst); |
| 1407 |
long int n2 = bnfst_limb_count(yfst); |
| 1408 |
if(bnfst_negative(xfst)){ |
| 1409 |
if(bnfst_negative(yfst)){ |
| 1410 |
if(n1 >= n2){ |
| 1411 |
pcb->root0 = &x; |
| 1412 |
pcb->root1 = &y; |
| 1413 |
ikptr r = ik_safe_alloc(pcb, align(disp_bignum_data + n1*wordsize)); |
| 1414 |
pcb->root0 = 0; |
| 1415 |
pcb->root1 = 0; |
| 1416 |
mp_limb_t* s1 = (mp_limb_t*)(long)(x+disp_bignum_data-vector_tag); |
| 1417 |
mp_limb_t* s2 = (mp_limb_t*)(long)(y+disp_bignum_data-vector_tag); |
| 1418 |
mp_limb_t* s = (mp_limb_t*)(long)(r+disp_bignum_data); |
| 1419 |
bits_compliment2(s2, s, n2, n1); |
| 1420 |
int carry = bits_compliment_logor(s1, s, s, n1); |
| 1421 |
bits_compliment_carry(s,s,n1,n1,carry); |
| 1422 |
bits_compliment2(s, s, n1, n1); |
| 1423 |
return normalize_bignum(n1, 1<<bignum_sign_shift, r); |
| 1424 |
} else { |
| 1425 |
return ikrt_bnbnlogor(y,x,pcb); |
| 1426 |
} |
| 1427 |
} else { |
| 1428 |
return ikrt_bnbnlogor(y,x,pcb); |
| 1429 |
} |
| 1430 |
} else { |
| 1431 |
if(bnfst_negative(yfst)){ |
| 1432 |
/* x positive, y negative */ |
| 1433 |
/* the result is at most n2 words long */ |
| 1434 |
pcb->root0 = &x; |
| 1435 |
pcb->root1 = &y; |
| 1436 |
ikptr r = ik_safe_alloc(pcb, align(disp_bignum_data + n2*wordsize)); |
| 1437 |
pcb->root0 = 0; |
| 1438 |
pcb->root1 = 0; |
| 1439 |
mp_limb_t* s1 = (mp_limb_t*)(long)(x+disp_bignum_data-vector_tag); |
| 1440 |
mp_limb_t* s2 = (mp_limb_t*)(long)(y+disp_bignum_data-vector_tag); |
| 1441 |
mp_limb_t* s = (mp_limb_t*)(long)(r+disp_bignum_data); |
| 1442 |
if(n2 <= n1){ |
| 1443 |
bits_compliment_logor(s2, s1, s, n2); |
| 1444 |
bits_compliment2(s, s, n2, n2); |
| 1445 |
} else { |
| 1446 |
int carry = bits_compliment_logor(s2, s1, s, n1); |
| 1447 |
bits_compliment_carry(s2, s, n1, n2, carry); |
| 1448 |
bits_compliment_carry(s, s, 0, n2, 1); |
| 1449 |
} |
| 1450 |
return normalize_bignum(n2, 1<<bignum_sign_shift, r); |
| 1451 |
} else { |
| 1452 |
/* both positive */ |
| 1453 |
int n = (n1>n2)?n1:n2; |
| 1454 |
pcb->root0 = &x; |
| 1455 |
pcb->root1 = &y; |
| 1456 |
ikptr r = ik_safe_alloc(pcb, align(disp_bignum_data+n*wordsize)); |
| 1457 |
mp_limb_t* s = (mp_limb_t*)(long)(r+disp_bignum_data); |
| 1458 |
mp_limb_t* s1 = (mp_limb_t*)(long)(x+disp_bignum_data-vector_tag); |
| 1459 |
mp_limb_t* s2 = (mp_limb_t*)(long)(y+disp_bignum_data-vector_tag); |
| 1460 |
pcb->root0 = 0; |
| 1461 |
pcb->root1 = 0; |
| 1462 |
long int i; |
| 1463 |
if(n == n1){ |
| 1464 |
for(i=0; i<n2; i++){ |
| 1465 |
s[i] = s1[i] | s2[i]; |
| 1466 |
} |
| 1467 |
for(i=n2; i<n1; i++){ |
| 1468 |
s[i] = s1[i]; |
| 1469 |
} |
| 1470 |
} else { |
| 1471 |
for(i=0; i<n1; i++){ |
| 1472 |
s[i] = s1[i] | s2[i]; |
| 1473 |
} |
| 1474 |
for(i=n1; i<n2; i++){ |
| 1475 |
s[i] = s2[i]; |
| 1476 |
} |
| 1477 |
} |
| 1478 |
return normalize_bignum(n, 0, r); |
| 1479 |
} |
| 1480 |
} |
| 1481 |
} |
| 1482 |
|
| 1483 |
static void |
| 1484 |
copy_bits_shifting_right(mp_limb_t* src, mp_limb_t* dst, int n, int m){ |
| 1485 |
mp_limb_t carry = src[0] >> m; |
| 1486 |
int i; |
| 1487 |
for(i=1; i<n; i++){ |
| 1488 |
mp_limb_t b = src[i]; |
| 1489 |
dst[i-1] = (b << (mp_bits_per_limb-m)) | carry; |
| 1490 |
carry = b >> m; |
| 1491 |
} |
| 1492 |
dst[n-1] = carry; |
| 1493 |
} |
| 1494 |
|
| 1495 |
static void |
| 1496 |
copy_bits_shifting_left(mp_limb_t* src, mp_limb_t* dst, int n, int m){ |
| 1497 |
mp_limb_t carry = 0; |
| 1498 |
int i; |
| 1499 |
for(i=0; i<n; i++){ |
| 1500 |
mp_limb_t b = src[i]; |
| 1501 |
dst[i] = (b << m) | carry; |
| 1502 |
carry = b >> (mp_bits_per_limb-m); |
| 1503 |
} |
| 1504 |
dst[n] = carry; |
| 1505 |
} |
| 1506 |
|
| 1507 |
|
| 1508 |
|
| 1509 |
|
| 1510 |
|
| 1511 |
ikptr |
| 1512 |
ikrt_bignum_shift_right(ikptr x, ikptr y, ikpcb* pcb){ |
| 1513 |
int limb_shift = (wordsize == 4 ? 5 : 6); |
| 1514 |
long int m = unfix(y); |
| 1515 |
ikptr fst = ref(x, -vector_tag); |
| 1516 |
long int n = bnfst_limb_count(fst); |
| 1517 |
long int whole_limb_shift = m >> limb_shift; |
| 1518 |
long int bit_shift = m & (mp_bits_per_limb-1); |
| 1519 |
long int new_limb_count = n - whole_limb_shift; |
| 1520 |
if(bnfst_negative(fst)){ |
| 1521 |
if(new_limb_count <= 0){ |
| 1522 |
return fix(-1); |
| 1523 |
} |
| 1524 |
if(bit_shift == 0){ |
| 1525 |
pcb->root0 = &x; |
| 1526 |
ikptr r = ik_safe_alloc(pcb, align(disp_bignum_data + new_limb_count * wordsize)); |
| 1527 |
pcb->root0 = 0; |
| 1528 |
bits_compliment_with_carry( |
| 1529 |
(mp_limb_t*)(long)(x+off_bignum_data+whole_limb_shift*wordsize), |
| 1530 |
(mp_limb_t*)(long)(r+disp_bignum_data), |
| 1531 |
new_limb_count, |
| 1532 |
bits_carry((mp_limb_t*)(long)(x+off_bignum_data), whole_limb_shift)); |
| 1533 |
bits_compliment( |
| 1534 |
(mp_limb_t*)(long)(r+disp_bignum_data), |
| 1535 |
(mp_limb_t*)(long)(r+disp_bignum_data), |
| 1536 |
new_limb_count); |
| 1537 |
return normalize_bignum(new_limb_count, 1 << bignum_sign_shift, r); |
| 1538 |
} else { |
| 1539 |
pcb->root0 = &x; |
| 1540 |
ikptr r = ik_safe_alloc(pcb, align(disp_bignum_data + new_limb_count * wordsize)); |
| 1541 |
pcb->root0 = 0; |
| 1542 |
bits_compliment_with_carry( |
| 1543 |
(mp_limb_t*)(long)(x+off_bignum_data+whole_limb_shift*wordsize), |
| 1544 |
(mp_limb_t*)(long)(r+disp_bignum_data), |
| 1545 |
new_limb_count, |
| 1546 |
bits_carry((mp_limb_t*)(long)(x+off_bignum_data), whole_limb_shift)); |
| 1547 |
copy_bits_shifting_right( |
| 1548 |
(mp_limb_t*)(long)(r+disp_bignum_data), |
| 1549 |
(mp_limb_t*)(long)(r+disp_bignum_data), |
| 1550 |
new_limb_count, |
| 1551 |
bit_shift); |
| 1552 |
*((mp_limb_t*)(r+disp_bignum_data+(new_limb_count-1)*wordsize)) |
| 1553 |
|= (-1L << (mp_bits_per_limb - bit_shift)); |
| 1554 |
bits_compliment( |
| 1555 |
(mp_limb_t*)(long)(r+disp_bignum_data), |
| 1556 |
(mp_limb_t*)(long)(r+disp_bignum_data), |
| 1557 |
new_limb_count); |
| 1558 |
return normalize_bignum(new_limb_count, 1 << bignum_sign_shift, r); |
| 1559 |
fprintf(stderr, "not yet for negative bignum_shift\n"); |
| 1560 |
exit(-1); |
| 1561 |
} |
| 1562 |
} else { |
| 1563 |
if(new_limb_count <= 0){ |
| 1564 |
return 0; |
| 1565 |
} |
| 1566 |
pcb->root0 = &x; |
| 1567 |
ikptr r = ik_safe_alloc(pcb, align(disp_bignum_data + new_limb_count * wordsize)); |
| 1568 |
pcb->root0 = 0; |
| 1569 |
if(bit_shift == 0){ |
| 1570 |
memcpy((char*)(long)r+disp_bignum_data, |
| 1571 |
(char*)(long)x+off_bignum_data+whole_limb_shift*wordsize, |
| 1572 |
new_limb_count * wordsize); |
| 1573 |
return normalize_bignum(new_limb_count, 0, r); |
| 1574 |
} else { |
| 1575 |
copy_bits_shifting_right( |
| 1576 |
(mp_limb_t*)(long)(x+off_bignum_data+whole_limb_shift*wordsize), |
| 1577 |
(mp_limb_t*)(long)(r+disp_bignum_data), |
| 1578 |
new_limb_count, |
| 1579 |
bit_shift); |
| 1580 |
return normalize_bignum(new_limb_count, 0, r); |
| 1581 |
} |
| 1582 |
} |
| 1583 |
} |
| 1584 |
|
| 1585 |
|
| 1586 |
ikptr |
| 1587 |
ikrt_fixnum_shift_left(ikptr x, ikptr y, ikpcb* pcb){ |
| 1588 |
int limb_shift = (wordsize == 4 ? 5 : 6); |
| 1589 |
long int m = unfix(y); |
| 1590 |
long int n = unfix(x); |
| 1591 |
long int limb_count = (m >> limb_shift) + 2; |
| 1592 |
long int bit_shift = m & (mp_bits_per_limb-1); |
| 1593 |
ikptr r = ik_safe_alloc(pcb, align(disp_bignum_data + limb_count * wordsize)); |
| 1594 |
unsigned long int* s = (unsigned long int*)(long)(r+disp_bignum_data); |
| 1595 |
bzero(s, limb_count * wordsize); |
| 1596 |
if(n >= 0){ |
| 1597 |
if(bit_shift){ |
| 1598 |
s[limb_count-1] = n >> (mp_bits_per_limb - bit_shift); |
| 1599 |
} |
| 1600 |
s[limb_count-2] = n << bit_shift; |
| 1601 |
} else { |
| 1602 |
if(bit_shift){ |
| 1603 |
s[limb_count-1] = (-n) >> (mp_bits_per_limb - bit_shift); |
| 1604 |
} |
| 1605 |
s[limb_count-2] = (-n) << bit_shift; |
| 1606 |
} |
| 1607 |
return normalize_bignum(limb_count, (n>=0)?(0):(1<<bignum_sign_shift), r); |
| 1608 |
} |
| 1609 |
|
| 1610 |
|
| 1611 |
ikptr |
| 1612 |
ikrt_bignum_shift_left(ikptr x, ikptr y, ikpcb* pcb){ |
| 1613 |
int limb_shift = (wordsize == 4 ? 5 : 6); |
| 1614 |
long int m = unfix(y); |
| 1615 |
ikptr fst = ref(x, -vector_tag); |
| 1616 |
long int n = bnfst_limb_count(fst); |
| 1617 |
long int whole_limb_shift = m >> limb_shift; |
| 1618 |
long int bit_shift = m & (mp_bits_per_limb-1); |
| 1619 |
if(bit_shift == 0){ |
| 1620 |
long int limb_count = n + whole_limb_shift; |
| 1621 |
pcb->root0 = &x; |
| 1622 |
ikptr r = ik_safe_alloc(pcb, align(disp_bignum_data + limb_count * wordsize)); |
| 1623 |
pcb->root0 = 0; |
| 1624 |
unsigned int* s = (unsigned int*)(long)(r+disp_bignum_data); |
| 1625 |
bzero(s, whole_limb_shift*wordsize); |
| 1626 |
memcpy(((char*)s) + whole_limb_shift*wordsize, |
| 1627 |
((char*)x) + off_bignum_data, |
| 1628 |
n*wordsize); |
| 1629 |
return normalize_bignum(limb_count, bnfst_negative(fst), r); |
| 1630 |
} else { |
| 1631 |
int limb_count = n + whole_limb_shift + 1; |
| 1632 |
pcb->root0 = &x; |
| 1633 |
ikptr r = ik_safe_alloc(pcb, align(disp_bignum_data + limb_count * wordsize)); |
| 1634 |
pcb->root0 = 0; |
| 1635 |
mp_limb_t* s = (mp_limb_t*)(r+disp_bignum_data); |
| 1636 |
bzero(s, whole_limb_shift*wordsize); |
| 1637 |
copy_bits_shifting_left( |
| 1638 |
(mp_limb_t*)(x+off_bignum_data), |
| 1639 |
(mp_limb_t*)(s+whole_limb_shift), |
| 1640 |
n, |
| 1641 |
bit_shift); |
| 1642 |
return normalize_bignum(limb_count, bnfst_negative(fst), r); |
| 1643 |
} |
| 1644 |
} |
| 1645 |
|
| 1646 |
|
| 1647 |
#if 0 |
| 1648 |
From TFM: |
| 1649 |
void |
| 1650 |
mpn_tdiv_qr ( |
| 1651 |
mp limb t *qp, /* quotient placed here */ |
| 1652 |
mp limb t *rp, /* remainder placed here */ |
| 1653 |
mp size t qxn, /* must be zero! */ |
| 1654 |
const mp limb t *np, /* first number */ |
| 1655 |
mp size t nn, /* its length */ |
| 1656 |
const mp limb t *dp, /* second number */ |
| 1657 |
mp size t dn /* its length */ |
| 1658 |
) |
| 1659 |
|
| 1660 |
Divide {np, nn} by {dp, dn} and put the quotient at {qp,nn-dn+1} |
| 1661 |
and the remainder at {rp, dn}. The quotient is rounded towards 0. |
| 1662 |
No overlap is permitted between arguments. nn must be greater than |
| 1663 |
or equal to dn. The most significant limb of dp must be non-zero. |
| 1664 |
The qxn operand must be zero. |
| 1665 |
#endif |
| 1666 |
|
| 1667 |
ikptr |
| 1668 |
ikrt_bnbndivrem(ikptr x, ikptr y, ikpcb* pcb){ |
| 1669 |
ikptr xfst = ref(x, -vector_tag); |
| 1670 |
ikptr yfst = ref(y, -vector_tag); |
| 1671 |
mp_size_t xn = bnfst_limb_count(xfst); |
| 1672 |
mp_size_t yn = bnfst_limb_count(yfst); |
| 1673 |
if(xn < yn){ |
| 1674 |
/* quotient is zero, remainder is x */ |
| 1675 |
pcb->root0 = &x; |
| 1676 |
pcb->root1 = &y; |
| 1677 |
ikptr rv = ik_safe_alloc(pcb, pair_size); |
| 1678 |
pcb->root0 = 0; |
| 1679 |
pcb->root1 = 0; |
| 1680 |
ref(rv, disp_car) = 0; |
| 1681 |
ref(rv, disp_cdr) = x; |
| 1682 |
return rv+pair_tag; |
| 1683 |
} |
| 1684 |
mp_size_t qn = xn - yn + 1; |
| 1685 |
mp_size_t rn = yn; |
| 1686 |
/* |
| 1687 |
ikptr q = ik_unsafe_alloc(pcb, align(disp_bignum_data + qn*wordsize)); |
| 1688 |
ikptr r = ik_unsafe_alloc(pcb, align(disp_bignum_data + rn*wordsize)); |
| 1689 |
*/ |
| 1690 |
pcb->root0 = &x; |
| 1691 |
pcb->root1 = &y; |
| 1692 |
ikptr q = ik_safe_alloc(pcb, |
| 1693 |
align(disp_bignum_data + qn*wordsize) + |
| 1694 |
align(disp_bignum_data + rn*wordsize)); |
| 1695 |
ikptr r = q + align(disp_bignum_data + qn*wordsize); |
| 1696 |
pcb->root0 = 0; |
| 1697 |
pcb->root1 = 0; |
| 1698 |
mpn_tdiv_qr ( |
| 1699 |
(mp_limb_t*)(long)(q+disp_bignum_data), |
| 1700 |
(mp_limb_t*)(long)(r+disp_bignum_data), |
| 1701 |
0, |
| 1702 |
(mp_limb_t*)(long)(x+off_bignum_data), |
| 1703 |
xn, |
| 1704 |
(mp_limb_t*)(long)(y+off_bignum_data), |
| 1705 |
yn); |
| 1706 |
|
| 1707 |
if(bnfst_negative(xfst)){ |
| 1708 |
/* x is negative => remainder is negative */ |
| 1709 |
r = normalize_bignum(rn, 1 << bignum_sign_shift, r); |
| 1710 |
} else { |
| 1711 |
r = normalize_bignum(rn, 0, r); |
| 1712 |
} |
| 1713 |
|
| 1714 |
if(bnfst_negative(yfst)){ |
| 1715 |
/* y is negative => quotient is opposite of x */ |
| 1716 |
long int sign = bignum_sign_mask - bnfst_negative(xfst); |
| 1717 |
q = normalize_bignum(qn, sign, q); |
| 1718 |
} else { |
| 1719 |
/* y is positive => quotient is same as x */ |
| 1720 |
long int sign = bnfst_negative(xfst); |
| 1721 |
q = normalize_bignum(qn, sign, q); |
| 1722 |
} |
| 1723 |
pcb->root0 = &q; |
| 1724 |
pcb->root1 = &r; |
| 1725 |
ikptr rv = ik_safe_alloc(pcb, pair_size); |
| 1726 |
pcb->root0 = 0; |
| 1727 |
pcb->root1 = 0; |
| 1728 |
ref(rv, disp_car) = q; |
| 1729 |
ref(rv, disp_cdr) = r; |
| 1730 |
return rv+pair_tag; |
| 1731 |
} |
| 1732 |
|
| 1733 |
|
| 1734 |
#if 0 |
| 1735 |
[Function] |
| 1736 |
|
| 1737 |
mp_limb_t |
| 1738 |
mpn_divrem_1 ( |
| 1739 |
mp limb t *r1p, |
| 1740 |
mp size t qxn, |
| 1741 |
mp limb t *s2p, |
| 1742 |
mp size t s2n, |
| 1743 |
mp limb t s3limb |
| 1744 |
) |
| 1745 |
|
| 1746 |
Divide {s2p, s2n} by s3limb, and write the quotient at r1p. Return the remainder. |
| 1747 |
The integer quotient is written to {r1p+qxn, s2n} and in addition qxn fraction limbs are |
| 1748 |
developed and written to {r1p, qxn}. Either or both s2n and qxn can be zero. For most |
| 1749 |
usages, qxn will be zero. |
| 1750 |
#endif |
| 1751 |
|
| 1752 |
ikptr |
| 1753 |
ikrt_bnfxdivrem(ikptr x, ikptr y, ikpcb* pcb){ |
| 1754 |
long int yint = unfix(y); |
| 1755 |
ikptr fst = ref(x, -vector_tag); |
| 1756 |
mp_size_t s2n = bnfst_limb_count(fst); |
| 1757 |
pcb->root0 = &x; |
| 1758 |
ikptr quot = ik_safe_alloc(pcb, align(s2n*wordsize + disp_bignum_data)); |
| 1759 |
pcb->root0 = 0; |
| 1760 |
mp_limb_t* s2p = (mp_limb_t*)(long)(x+off_bignum_data); |
| 1761 |
mp_limb_t rv = mpn_divrem_1( |
| 1762 |
(mp_limb_t*)(long)(quot+disp_bignum_data), |
| 1763 |
0, |
| 1764 |
s2p, |
| 1765 |
s2n, |
| 1766 |
labs(yint)); |
| 1767 |
|
| 1768 |
ikptr rem; |
| 1769 |
|
| 1770 |
if(yint < 0){ |
| 1771 |
/* y is negative => quotient is opposite of x */ |
| 1772 |
long int sign = bignum_sign_mask - bnfst_negative(fst); |
| 1773 |
quot = normalize_bignum(s2n, sign, quot); |
| 1774 |
} else { |
| 1775 |
/* y is positive => quotient is same as x */ |
| 1776 |
long int sign = bnfst_negative(fst); |
| 1777 |
quot = normalize_bignum(s2n, sign, quot); |
| 1778 |
} |
| 1779 |
|
| 1780 |
/* the remainder is always less than |y|, so it will |
| 1781 |
always be a fixnum. (if y == most_negative_fixnum, |
| 1782 |
then |remainder| will be at most most_positive_fixnum). */ |
| 1783 |
if(bnfst_negative(fst)){ |
| 1784 |
/* x is negative => remainder is negative */ |
| 1785 |
rem = (ikptr) -(rv << fx_shift); |
| 1786 |
} else { |
| 1787 |
rem = fix(rv); |
| 1788 |
} |
| 1789 |
pcb->root0 = " |
| 1790 |
pcb->root1 = &rem; |
| 1791 |
ikptr p = ik_safe_alloc(pcb, pair_size); |
| 1792 |
pcb->root0 = 0; |
| 1793 |
pcb->root1 = 0; |
| 1794 |
ref(p, disp_car) = quot; |
| 1795 |
ref(p, disp_cdr) = rem; |
| 1796 |
return p+pair_tag; |
| 1797 |
} |
| 1798 |
|
| 1799 |
ikptr |
| 1800 |
ikrt_bnfx_modulo(ikptr x, ikptr y /*, ikpcb* pcb */){ |
| 1801 |
long int yint = unfix(y); |
| 1802 |
mp_limb_t* s2p = (mp_limb_t*)(long)(x+off_bignum_data); |
| 1803 |
ikptr fst = ref(x, -vector_tag); |
| 1804 |
mp_size_t s2n = bnfst_limb_count(fst); |
| 1805 |
if(yint < 0){ |
| 1806 |
if(bnfst_negative(fst)){ |
| 1807 |
/* x negative, y negative */ |
| 1808 |
mp_limb_t m = mpn_mod_1(s2p, s2n, -yint); |
| 1809 |
return fix(-m); |
| 1810 |
} else { |
| 1811 |
/* x positive, y negative */ |
| 1812 |
mp_limb_t m = mpn_mod_1(s2p, s2n, -yint); |
| 1813 |
return fix(yint+m); |
| 1814 |
} |
| 1815 |
} else { |
| 1816 |
if(bnfst_negative(fst)){ |
| 1817 |
/* x negative, y positive */ |
| 1818 |
mp_limb_t m = mpn_mod_1(s2p, s2n, yint); |
| 1819 |
return fix(yint-m); |
| 1820 |
} else { |
| 1821 |
/* x positive, y positive */ |
| 1822 |
mp_limb_t m = mpn_mod_1(s2p, s2n, yint); |
| 1823 |
return fix(m); |
| 1824 |
} |
| 1825 |
} |
| 1826 |
} |
| 1827 |
|
| 1828 |
|
| 1829 |
static int |
| 1830 |
limb_length(unsigned long int n){ |
| 1831 |
int i=0; |
| 1832 |
while(n != 0){ |
| 1833 |
n = n >> 1; |
| 1834 |
i++; |
| 1835 |
} |
| 1836 |
return i; |
| 1837 |
} |
| 1838 |
|
| 1839 |
|
| 1840 |
ikptr |
| 1841 |
ikrt_bignum_length(ikptr x){ |
| 1842 |
ikptr fst = ref(x, -vector_tag); |
| 1843 |
mp_limb_t* sp = (mp_limb_t*)(long)(x+off_bignum_data); |
| 1844 |
mp_size_t sn = bnfst_limb_count(fst); |
| 1845 |
mp_limb_t last = sp[sn-1]; |
| 1846 |
int n0 = limb_length(last); |
| 1847 |
if(((unsigned long int) fst) & bignum_sign_mask){ |
| 1848 |
/* negative */ |
| 1849 |
if (last == (mp_limb_t)(1L<<(n0-1))){ |
| 1850 |
/* single bit set in last limb */ |
| 1851 |
int i; |
| 1852 |
for(i=0; i<(sn-1); i++){ |
| 1853 |
if(sp[i] != 0){ |
| 1854 |
/* another bit set */ |
| 1855 |
return fix((sn-1)*mp_bits_per_limb + n0); |
| 1856 |
} |
| 1857 |
} |
| 1858 |
/* number is - #b100000000000000000000000000 */ |
| 1859 |
/* fxnot(n) = #b011111111111111111111111111 */ |
| 1860 |
/* so, subtract 1. */ |
| 1861 |
return fix((sn-1)*mp_bits_per_limb + n0 - 1); |
| 1862 |
} else { |
| 1863 |
return fix((sn-1)*mp_bits_per_limb + n0); |
| 1864 |
} |
| 1865 |
} else { |
| 1866 |
return fix((sn-1)*mp_bits_per_limb + n0); |
| 1867 |
} |
| 1868 |
} |
| 1869 |
|
| 1870 |
|
| 1871 |
ikptr |
| 1872 |
ikrt_bignum_to_bytevector(ikptr x, ikpcb* pcb){ |
| 1873 |
/* FIXME: avoid calling malloc, instead, use the heap pointer itself |
| 1874 |
* as a buffer to hold the temporary data after ensuring that it has enough |
| 1875 |
* space */ |
| 1876 |
ikptr fst = ref(x, -vector_tag); |
| 1877 |
long int limb_count = bnfst_limb_count(fst); |
| 1878 |
if(limb_count <= 0){ |
| 1879 |
fprintf(stderr, "BUG: nbtostring: invalid length %ld\n", limb_count); |
| 1880 |
exit(-1); |
| 1881 |
} |
| 1882 |
long int sign_bit = bignum_sign_mask & (long int) fst; |
| 1883 |
long int nbsize = limb_count * sizeof(mp_limb_t); |
| 1884 |
long int strsize = limb_count * max_digits_per_limb; |
| 1885 |
long int mem_req = nbsize + strsize + 1; |
| 1886 |
unsigned char* mem = malloc(mem_req); |
| 1887 |
if(! mem){ |
| 1888 |
fprintf(stderr, "Error allocating space for bignum\n"); |
| 1889 |
exit(-1); |
| 1890 |
} |
| 1891 |
memcpy((char*)(long)mem, |
| 1892 |
(char*)(long)x - vector_tag + disp_bignum_data, |
| 1893 |
nbsize); |
| 1894 |
mp_size_t bytes = |
| 1895 |
mpn_get_str(mem+nbsize, /* output string */ |
| 1896 |
10, /* base */ |
| 1897 |
(mp_limb_t*) mem, /* limb */ |
| 1898 |
limb_count /* number of limbs */ |
| 1899 |
); |
| 1900 |
unsigned char* string_start = mem + nbsize; |
| 1901 |
while(*string_start == 0){ |
| 1902 |
string_start++; |
| 1903 |
bytes--; |
| 1904 |
} |
| 1905 |
ikptr bv = ik_safe_alloc(pcb, align(bytes + disp_bytevector_data + (sign_bit?1:0))); |
| 1906 |
ref(bv, 0) = fix(bytes + (sign_bit?1:0)); |
| 1907 |
char* dest = (char*)(long)(bv + disp_bytevector_data); |
| 1908 |
if(sign_bit){ |
| 1909 |
*dest = '-'; |
| 1910 |
dest++; |
| 1911 |
} |
| 1912 |
{ |
| 1913 |
long int i = 0; |
| 1914 |
while(i < bytes){ |
| 1915 |
dest[i] = string_start[i] + '0'; |
| 1916 |
i++; |
| 1917 |
} |
| 1918 |
dest[bytes] = 0; |
| 1919 |
} |
| 1920 |
free(mem); |
| 1921 |
return bv + bytevector_tag; |
| 1922 |
} |
| 1923 |
|
| 1924 |
|
| 1925 |
ikptr |
| 1926 |
ikrt_fxrandom(ikptr x){ |
| 1927 |
long int mask = 1; |
| 1928 |
long int n = unfix(x); |
| 1929 |
{ |
| 1930 |
while(mask < n){ |
| 1931 |
mask = (mask << 1) | 1; |
| 1932 |
} |
| 1933 |
} |
| 1934 |
while(1){ |
| 1935 |
long r = rand() & mask; |
| 1936 |
if(r < n){ |
| 1937 |
return fix(r); |
| 1938 |
} |
| 1939 |
} |
| 1940 |
} |
| 1941 |
|
| 1942 |
static int |
| 1943 |
limb_size(mp_limb_t x){ |
| 1944 |
int i = 0; |
| 1945 |
while(x){ |
| 1946 |
i++; |
| 1947 |
x = x>>1; |
| 1948 |
} |
| 1949 |
return i; |
| 1950 |
} |
| 1951 |
|
| 1952 |
static int |
| 1953 |
all_zeros(mp_limb_t* start, mp_limb_t* end){ |
| 1954 |
while(start <= end){ |
| 1955 |
if(*end) return 0; |
| 1956 |
end--; |
| 1957 |
} |
| 1958 |
return 1; |
| 1959 |
} |
| 1960 |
|
| 1961 |
#define PRECISION 53 |
| 1962 |
|
| 1963 |
static ikptr |
| 1964 |
ikrt_bignum_to_flonum64(ikptr bn, ikptr more_bits, ikptr fl){ |
| 1965 |
ikptr fst = ref(bn, -vector_tag); |
| 1966 |
long int limb_count = bnfst_limb_count(fst); |
| 1967 |
mp_limb_t* sp = (mp_limb_t*)(long)(bn+off_bignum_data); |
| 1968 |
double pos_result; |
| 1969 |
if(limb_count == 1){ |
| 1970 |
pos_result = sp[0]; |
| 1971 |
} else { |
| 1972 |
mp_limb_t hi = sp[limb_count-1]; |
| 1973 |
int bc = limb_size(hi); |
| 1974 |
if(bc < 64){ |
| 1975 |
mp_limb_t mi = sp[limb_count-2]; |
| 1976 |
hi = (hi << (64-bc)) | (mi >> bc); |
| 1977 |
} |
| 1978 |
/* now hi has 64 full bits */ |
| 1979 |
mp_limb_t mask = ((1L<<(64-PRECISION)) - 1); |
| 1980 |
if((hi & mask) == ((mask+1)>>1)){ |
| 1981 |
/* exactly at break point */ |
| 1982 |
if(((sp[limb_count-2] << (64-bc)) == 0) && |
| 1983 |
all_zeros(sp, sp+limb_count-3) && |
| 1984 |
(more_bits == 0)){ |
| 1985 |
if(hi & (1L<<(64-PRECISION))){ |
| 1986 |
/* odd number, round to even */ |
| 1987 |
hi = hi | mask; |
| 1988 |
} |
| 1989 |
} else { |
| 1990 |
/* round up */ |
| 1991 |
hi = hi | mask; |
| 1992 |
} |
| 1993 |
} else if ((hi & mask) > ((mask+1)>>1)){ |
| 1994 |
/* also round up */ |
| 1995 |
hi = hi | mask; |
| 1996 |
} else { |
| 1997 |
/* keep it to round down */ |
| 1998 |
} |
| 1999 |
pos_result = hi; |
| 2000 |
int bignum_bits = bc + (mp_bits_per_limb * (limb_count-1)); |
| 2001 |
int exponent = bignum_bits - mp_bits_per_limb; |
| 2002 |
while(exponent){ |
| 2003 |
pos_result *= 2.0; |
| 2004 |
exponent -= 1; |
| 2005 |
} |
| 2006 |
} |
| 2007 |
if(bnfst_negative(fst)){ |
| 2008 |
flonum_data(fl) = - pos_result; |
| 2009 |
} else { |
| 2010 |
flonum_data(fl) = pos_result; |
| 2011 |
} |
| 2012 |
return fl; |
| 2013 |
} |
| 2014 |
|
| 2015 |
ikptr |
| 2016 |
ikrt_bignum_to_flonum(ikptr bn, ikptr more_bits, ikptr fl){ |
| 2017 |
if(mp_bits_per_limb == 64){ |
| 2018 |
return ikrt_bignum_to_flonum64(bn, more_bits, fl); |
| 2019 |
} |
| 2020 |
ikptr fst = ref(bn, -vector_tag); |
| 2021 |
long int limb_count = bnfst_limb_count(fst); |
| 2022 |
mp_limb_t* sp = (mp_limb_t*)(long)(bn+off_bignum_data); |
| 2023 |
double pos_result; |
| 2024 |
if(limb_count == 1){ |
| 2025 |
pos_result = sp[0]; |
| 2026 |
} else if (limb_count == 2){ |
| 2027 |
mp_limb_t lo = sp[0]; |
| 2028 |
mp_limb_t hi = sp[1]; |
| 2029 |
pos_result = hi; |
| 2030 |
pos_result = pos_result * 4294967296.0; |
| 2031 |
pos_result = pos_result + lo; |
| 2032 |
} else { |
| 2033 |
mp_limb_t hi = sp[limb_count-1]; |
| 2034 |
mp_limb_t mi = sp[limb_count-2]; |
| 2035 |
int bc = limb_size(hi); |
| 2036 |
if(bc < 32){ |
| 2037 |
mp_limb_t lo = sp[limb_count-3]; |
| 2038 |
hi = (hi << (32-bc)) | (mi >> bc); |
| 2039 |
mi = (mi << (32-bc)) | (lo >> bc); |
| 2040 |
} |
| 2041 |
/* now hi has 32 full bits, and mi has 32 full bits */ |
| 2042 |
mp_limb_t mask = ((1<<(64-PRECISION)) - 1); |
| 2043 |
if((mi & mask) == ((mask+1)>>1)){ |
| 2044 |
/* exactly at break point */ |
| 2045 |
if(((sp[limb_count-3] << (32-bc)) == 0) && |
| 2046 |
all_zeros(sp, sp+limb_count-4) && |
| 2047 |
(more_bits == 0)){ |
| 2048 |
if(mi & (1<<(64-PRECISION))){ |
| 2049 |
/* odd number, round to even */ |
| 2050 |
mi = mi | mask; |
| 2051 |
} |
| 2052 |
} else { |
| 2053 |
/* round up */ |
| 2054 |
mi = mi | mask; |
| 2055 |
} |
| 2056 |
} else if ((mi & mask) > ((mask+1)>>1)){ |
| 2057 |
/* also round up */ |
| 2058 |
mi = mi | mask; |
| 2059 |
} else { |
| 2060 |
/* keep it to round down */ |
| 2061 |
} |
| 2062 |
pos_result = hi; |
| 2063 |
pos_result = pos_result * 4294967296.0; |
| 2064 |
pos_result = pos_result + mi; |
| 2065 |
int bignum_bits = bc + (mp_bits_per_limb * (limb_count-1)); |
| 2066 |
int exponent = bignum_bits - (2 * mp_bits_per_limb); |
| 2067 |
while(exponent){ |
| 2068 |
pos_result *= 2.0; |
| 2069 |
exponent -= 1; |
| 2070 |
} |
| 2071 |
} |
| 2072 |
if(bnfst_negative(fst)){ |
| 2073 |
flonum_data(fl) = - pos_result; |
| 2074 |
} else { |
| 2075 |
flonum_data(fl) = pos_result; |
| 2076 |
} |
| 2077 |
return fl; |
| 2078 |
} |
| 2079 |
|
| 2080 |
ikptr |
| 2081 |
ikrt_exact_fixnum_sqrt(ikptr fx /*, ikpcb* pcb*/){ |
| 2082 |
mp_limb_t x = unfix(fx); |
| 2083 |
mp_limb_t s; |
| 2084 |
mp_limb_t r; |
| 2085 |
mpn_sqrtrem(&s, &r, &x, 1); |
| 2086 |
return fix(s); |
| 2087 |
} |
| 2088 |
|
| 2089 |
ikptr |
| 2090 |
ikrt_exact_bignum_sqrt(ikptr bn, ikpcb* pcb){ |
| 2091 |
ikptr fst = ref(bn, -vector_tag); |
| 2092 |
long int limb_count = bnfst_limb_count(fst); |
| 2093 |
long int result_limb_count = (limb_count + 1)/2; |
| 2094 |
pcb->root0 = &bn; |
| 2095 |
ikptr s = ik_safe_alloc(pcb, |
| 2096 |
align(disp_bignum_data+result_limb_count*wordsize)) |
| 2097 |
+ vector_tag; |
| 2098 |
ref(s, -vector_tag) = |
| 2099 |
(ikptr) (bignum_tag | (result_limb_count << bignum_length_shift)); |
| 2100 |
pcb->root1 = &s; |
| 2101 |
ikptr r = ik_safe_alloc(pcb, |
| 2102 |
align(disp_bignum_data+limb_count*wordsize)) |
| 2103 |
+ vector_tag; |
| 2104 |
ref(r, -vector_tag) = |
| 2105 |
(ikptr) (bignum_tag | (limb_count << bignum_length_shift)); |
| 2106 |
pcb->root0 = &r; |
| 2107 |
ikptr pair = ik_safe_alloc(pcb, pair_size) + pair_tag; |
| 2108 |
pcb->root0 = 0; |
| 2109 |
pcb->root1 = 0; |
| 2110 |
mp_size_t r_actual_limbs = mpn_sqrtrem( |
| 2111 |
(mp_limb_t*) (s+off_bignum_data), |
| 2112 |
(mp_limb_t*) (r+off_bignum_data), |
| 2113 |
(mp_limb_t*) (bn+off_bignum_data), |
| 2114 |
limb_count); |
| 2115 |
ref(pair, off_car) = normalize_bignum(result_limb_count, 0, s-vector_tag); |
| 2116 |
if(r_actual_limbs == 0) { |
| 2117 |
/* perfect square */ |
| 2118 |
ref(pair, off_cdr) = 0; |
| 2119 |
} else { |
| 2120 |
ref(pair, off_cdr) = normalize_bignum(r_actual_limbs, 0, r-vector_tag); |
| 2121 |
} |
| 2122 |
return pair; |
| 2123 |
} |
| 2124 |
|
| 2125 |
|
| 2126 |
ikptr |
| 2127 |
ikrt_flonum_hash(ikptr x /*, ikpcb* pcb */) { |
| 2128 |
short* buf = (short*)(x+off_flonum_data); |
| 2129 |
return fix(((long)buf[0]) ^ |
| 2130 |
((long)buf[1] << 3) ^ |
| 2131 |
((long)buf[3] << 7) ^ |
| 2132 |
((long)buf[2] << 11)); |
| 2133 |
} |
| 2134 |
ikptr |
| 2135 |
ikrt_bignum_hash(ikptr bn /*, ikpcb* pcb */) { |
| 2136 |
ikptr fst = ref(bn, -vector_tag); |
| 2137 |
long int limb_count = bnfst_limb_count(fst); |
| 2138 |
long h = (long)fst; |
| 2139 |
mp_limb_t* dat = (mp_limb_t*)(bn+off_bignum_data); |
| 2140 |
long i; |
| 2141 |
for (i=0; i<limb_count; i++){ |
| 2142 |
h = (h^dat[i]) << 3; |
| 2143 |
} |
| 2144 |
return fix(h); |
| 2145 |
} |
| 2146 |
|
| 2147 |
|
| 2148 |
|