2007-10-25 16:27:34 -04:00
|
|
|
/*
|
|
|
|
* Ikarus Scheme -- A compiler for R6RS Scheme.
|
2008-01-29 00:34:34 -05:00
|
|
|
* Copyright (C) 2006,2007,2008 Abdulaziz Ghuloum
|
2007-10-25 16:27:34 -04:00
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License version 3 as
|
|
|
|
* published by the Free Software Foundation.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful, but
|
|
|
|
* WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
2006-11-23 19:48:14 -05:00
|
|
|
|
2007-10-17 09:22:47 -04:00
|
|
|
#include "ikarus-data.h"
|
2006-11-23 19:48:14 -05:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <gmp.h>
|
|
|
|
|
2008-01-01 04:24:36 -05:00
|
|
|
#define bnfst_limb_count(x) \
|
|
|
|
(((unsigned long int)(x)) >> bignum_length_shift)
|
|
|
|
#define bnfst_negative(x) \
|
|
|
|
(((unsigned long int)(x)) & bignum_sign_mask)
|
|
|
|
|
2006-11-23 19:48:14 -05:00
|
|
|
|
2008-07-18 04:35:13 -04:00
|
|
|
#define most_positive_fixnum \
|
|
|
|
(((unsigned long int)-1) >> (fx_shift+1))
|
|
|
|
#define most_negative_fixnum (most_positive_fixnum+1)
|
|
|
|
// #define most_positive_fixnum 0x1FFFFFFF
|
|
|
|
// #define most_negative_fixnum 0x20000000
|
2006-11-23 19:48:14 -05:00
|
|
|
|
2008-07-19 17:41:06 -04:00
|
|
|
#define max_digits_per_limb ((wordsize==4)?10:20)
|
2006-11-23 19:48:14 -05:00
|
|
|
|
2007-01-21 20:36:22 -05:00
|
|
|
#ifdef NDEBUG
|
|
|
|
#define verify_bignum(x,caller) (x)
|
|
|
|
#else
|
2007-12-23 13:37:48 -05:00
|
|
|
static ikptr
|
|
|
|
verify_bignum(ikptr x, char* caller){
|
2007-01-21 19:20:37 -05:00
|
|
|
if(tagof(x) != vector_tag){
|
2008-06-04 03:54:53 -04:00
|
|
|
fprintf(stderr, "Error in (%s) invalid primary tag 0x%016lx\n", caller, x);
|
2007-01-21 19:20:37 -05:00
|
|
|
exit(-1);
|
|
|
|
}
|
2007-12-23 13:37:48 -05:00
|
|
|
ikptr fst = ref(x, -vector_tag);
|
2008-01-01 21:08:07 -05:00
|
|
|
long int limb_count = ((unsigned long int) fst) >> bignum_length_shift;
|
2007-01-21 19:20:37 -05:00
|
|
|
if(limb_count <= 0){
|
|
|
|
fprintf(stderr,
|
2008-06-04 03:54:53 -04:00
|
|
|
"Error in (%s) invalid limb count in fst=0x%016lx\n",
|
2008-01-01 21:08:07 -05:00
|
|
|
caller, (long int)fst);
|
2007-01-21 19:20:37 -05:00
|
|
|
exit(-1);
|
|
|
|
}
|
|
|
|
int pos;
|
2008-01-01 21:08:07 -05:00
|
|
|
if((long int)fst & bignum_sign_mask){
|
2007-01-21 19:20:37 -05:00
|
|
|
pos = 0;
|
|
|
|
} else {
|
|
|
|
pos = 1;
|
|
|
|
}
|
2008-01-01 21:08:07 -05:00
|
|
|
mp_limb_t last_limb =
|
|
|
|
(mp_limb_t) ref(x, off_bignum_data + (limb_count - 1) * wordsize);
|
2007-01-21 19:20:37 -05:00
|
|
|
if(last_limb == 0){
|
|
|
|
fprintf(stderr,
|
2008-06-04 03:54:53 -04:00
|
|
|
"Error in (%s) invalid last limb = 0x%016lx", caller, last_limb);
|
2007-01-21 19:20:37 -05:00
|
|
|
exit(-1);
|
|
|
|
}
|
|
|
|
if(limb_count == 1){
|
|
|
|
if(pos){
|
|
|
|
if(last_limb <= most_positive_fixnum){
|
|
|
|
fprintf(stderr,
|
2008-06-04 03:54:53 -04:00
|
|
|
"Error in (%s) should be a positive fixnum: 0x%016lx\n",
|
2007-01-21 19:20:37 -05:00
|
|
|
caller, last_limb);
|
|
|
|
exit(-1);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if(last_limb <= most_negative_fixnum){
|
|
|
|
fprintf(stderr,
|
2008-06-04 03:54:53 -04:00
|
|
|
"Error in (%s) should be a negative fixnum: 0x%016lx\n",
|
2007-01-21 19:20:37 -05:00
|
|
|
caller, last_limb);
|
|
|
|
exit(-1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/* ok */
|
|
|
|
return x;
|
|
|
|
}
|
2007-01-21 20:36:22 -05:00
|
|
|
#endif
|
2007-01-21 19:20:37 -05:00
|
|
|
|
|
|
|
#define BN(x) verify_bignum(x,"BN")
|
|
|
|
|
2007-03-04 13:02:39 -05:00
|
|
|
#if 0
|
2007-12-23 13:37:48 -05:00
|
|
|
ikptr
|
|
|
|
ikrt_isbignum(ikptr x){
|
2006-11-23 19:48:14 -05:00
|
|
|
if(tagof(x) == vector_tag){
|
2007-12-23 13:37:48 -05:00
|
|
|
ikptr fst = ref(x, -vector_tag);
|
2006-11-23 19:48:14 -05:00
|
|
|
if (bignum_tag == (bignum_mask & (int)fst)){
|
|
|
|
return true_object;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false_object;
|
|
|
|
}
|
2007-03-04 13:02:39 -05:00
|
|
|
#endif
|
2006-11-23 19:48:14 -05:00
|
|
|
|
2007-12-23 13:37:48 -05:00
|
|
|
ikptr
|
|
|
|
ikrt_positive_bn(ikptr x){
|
|
|
|
ikptr fst = ref(x, -vector_tag);
|
2008-01-01 04:24:36 -05:00
|
|
|
if(bnfst_negative(fst)){
|
2006-11-23 19:48:14 -05:00
|
|
|
return false_object;
|
|
|
|
} else {
|
|
|
|
return true_object;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-12-23 13:37:48 -05:00
|
|
|
ikptr
|
|
|
|
ikrt_even_bn(ikptr x){
|
2008-01-01 04:24:36 -05:00
|
|
|
long int fst = (long int)ref(x, wordsize-vector_tag);
|
2007-01-13 22:32:54 -05:00
|
|
|
if(fst & 1){
|
|
|
|
return false_object;
|
|
|
|
} else {
|
|
|
|
return true_object;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-11-23 19:48:14 -05:00
|
|
|
|
2007-12-23 13:37:48 -05:00
|
|
|
ikptr
|
|
|
|
ikrt_fxfxplus(ikptr x, ikptr y, ikpcb* pcb){
|
2008-01-01 04:24:36 -05:00
|
|
|
long int n1 = unfix(x);
|
|
|
|
long int n2 = unfix(y);
|
|
|
|
long int r = n1 + n2;
|
2007-12-23 13:37:48 -05:00
|
|
|
ikptr q = fix(r);
|
2006-11-23 19:48:14 -05:00
|
|
|
if(r == unfix(q)){
|
|
|
|
return q;
|
|
|
|
}
|
|
|
|
else {
|
2007-12-23 13:37:48 -05:00
|
|
|
ikptr bn = ik_safe_alloc(pcb, align(disp_bignum_data + wordsize));
|
2006-11-23 19:48:14 -05:00
|
|
|
if(r > 0){
|
2007-12-23 13:37:48 -05:00
|
|
|
ref(bn, 0) = (ikptr)(bignum_tag | (1 << bignum_length_shift));
|
|
|
|
ref(bn, disp_bignum_data) = (ikptr)r;
|
2006-11-23 19:48:14 -05:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
ref(bn, 0) =
|
2007-12-23 13:37:48 -05:00
|
|
|
(ikptr)(bignum_tag |
|
2006-11-23 19:48:14 -05:00
|
|
|
(1 << bignum_length_shift) |
|
|
|
|
(1 << bignum_sign_shift));
|
2007-12-23 13:37:48 -05:00
|
|
|
ref(bn, disp_bignum_data) = (ikptr)-r;
|
2006-11-23 19:48:14 -05:00
|
|
|
}
|
2007-01-21 19:20:37 -05:00
|
|
|
return verify_bignum(bn+vector_tag, "fxfx+");
|
2006-11-23 19:48:14 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-12-23 13:37:48 -05:00
|
|
|
ikptr
|
|
|
|
ikrt_fxbnplus(ikptr x, ikptr y, ikpcb* pcb){
|
2006-11-23 19:48:14 -05:00
|
|
|
if(x == 0){ return y ; }
|
2007-12-23 13:37:48 -05:00
|
|
|
ikptr fst = ref(y, -vector_tag);
|
2008-01-01 04:24:36 -05:00
|
|
|
long int limb_count = bnfst_limb_count(fst);
|
|
|
|
long int intx = unfix(x);
|
2006-11-23 19:48:14 -05:00
|
|
|
if(intx > 0){
|
2008-01-01 05:17:42 -05:00
|
|
|
if(!bnfst_negative(fst)){
|
2006-11-23 19:48:14 -05:00
|
|
|
/* positive fx + positive bn = even bigger positive */
|
2007-11-18 10:37:13 -05:00
|
|
|
pcb->root0 = &y;
|
2007-12-23 13:37:48 -05:00
|
|
|
ikptr r = ik_safe_alloc(pcb, align(disp_bignum_data+(limb_count+1)*wordsize));
|
2007-11-18 10:37:13 -05:00
|
|
|
pcb->root0 = 0;
|
2008-01-01 04:24:36 -05:00
|
|
|
mp_limb_t carry =
|
2008-01-01 21:42:52 -05:00
|
|
|
mpn_add_1((mp_limb_t*)(long)(r+disp_bignum_data),
|
|
|
|
(mp_limb_t*)(long)(y - vector_tag + disp_bignum_data),
|
2008-01-01 04:24:36 -05:00
|
|
|
limb_count,
|
|
|
|
intx);
|
2006-11-23 19:48:14 -05:00
|
|
|
if(carry){
|
2007-12-23 13:37:48 -05:00
|
|
|
ref(r, disp_bignum_data + limb_count*wordsize) = (ikptr)1;
|
|
|
|
ref(r, 0) = (ikptr)
|
2006-11-23 19:48:14 -05:00
|
|
|
(((limb_count + 1) << bignum_length_shift) |
|
|
|
|
(0 << bignum_sign_shift) |
|
|
|
|
bignum_tag);
|
2007-01-21 19:20:37 -05:00
|
|
|
return verify_bignum(r+vector_tag, "fxbn+1");
|
2006-11-23 19:48:14 -05:00
|
|
|
} else {
|
2007-12-23 13:37:48 -05:00
|
|
|
ref(r, 0) = (ikptr)
|
2006-11-23 19:48:14 -05:00
|
|
|
((limb_count << bignum_length_shift) |
|
|
|
|
(0 << bignum_sign_shift) |
|
|
|
|
bignum_tag);
|
2007-01-21 19:20:37 -05:00
|
|
|
return verify_bignum(r+vector_tag, "fxbn+2");
|
2006-11-23 19:48:14 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
/* positive fx + negative bn = smaller negative bn */
|
2007-11-18 10:37:13 -05:00
|
|
|
pcb->root0 = &y;
|
2007-12-23 13:37:48 -05:00
|
|
|
ikptr r = ik_safe_alloc(pcb, align(disp_bignum_data+limb_count*wordsize));
|
2007-11-18 10:37:13 -05:00
|
|
|
pcb->root0 = 0;
|
2008-01-01 04:24:36 -05:00
|
|
|
mp_limb_t borrow =
|
2008-01-01 21:42:52 -05:00
|
|
|
mpn_sub_1((mp_limb_t*)(long)(r+disp_bignum_data),
|
|
|
|
(mp_limb_t*)(long)(y - vector_tag + disp_bignum_data),
|
2008-01-01 04:24:36 -05:00
|
|
|
limb_count,
|
|
|
|
intx);
|
2006-11-23 19:48:14 -05:00
|
|
|
if(borrow){
|
2008-01-01 04:24:36 -05:00
|
|
|
fprintf(stderr, "Error: BUG in borrow1 %ld\n", borrow);
|
2006-11-23 19:48:14 -05:00
|
|
|
exit(-1);
|
|
|
|
}
|
2008-01-01 04:24:36 -05:00
|
|
|
long int result_size =
|
2006-11-23 19:48:14 -05:00
|
|
|
(ref(r, disp_bignum_data + (limb_count-1)*wordsize))
|
|
|
|
? limb_count
|
|
|
|
: (limb_count - 1);
|
|
|
|
if(result_size == 0){
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
if(result_size == 1){
|
2008-01-01 04:24:36 -05:00
|
|
|
mp_limb_t last =
|
|
|
|
(mp_limb_t) ref(r, disp_bignum_data + (result_size-1)*wordsize);
|
2006-11-23 19:48:14 -05:00
|
|
|
if(last <= most_negative_fixnum){
|
2008-01-01 04:24:36 -05:00
|
|
|
return fix(-(long int)last);
|
2006-11-23 19:48:14 -05:00
|
|
|
}
|
|
|
|
}
|
2007-12-23 13:37:48 -05:00
|
|
|
ref(r, 0) = (ikptr)
|
2006-11-23 19:48:14 -05:00
|
|
|
((result_size << bignum_length_shift) |
|
|
|
|
(1 << bignum_sign_shift) |
|
|
|
|
bignum_tag);
|
2007-01-21 19:20:37 -05:00
|
|
|
return verify_bignum(r+vector_tag, "fxbn+3");
|
2006-11-23 19:48:14 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
2008-01-01 05:17:42 -05:00
|
|
|
if(! bnfst_negative(fst)){
|
2006-11-23 19:48:14 -05:00
|
|
|
/* negative fx + positive bn = smaller positive */
|
2007-11-18 10:37:13 -05:00
|
|
|
pcb->root0 = &y;
|
2007-12-23 13:37:48 -05:00
|
|
|
ikptr r = ik_safe_alloc(pcb, align(disp_bignum_data+limb_count*wordsize));
|
2007-11-18 10:37:13 -05:00
|
|
|
pcb->root0 = 0;
|
2008-01-01 04:24:36 -05:00
|
|
|
mp_limb_t borrow =
|
2008-01-01 21:42:52 -05:00
|
|
|
mpn_sub_1((mp_limb_t*)(long)(r+disp_bignum_data),
|
|
|
|
(mp_limb_t*)(long)(y - vector_tag + disp_bignum_data),
|
2008-01-01 04:24:36 -05:00
|
|
|
limb_count,
|
|
|
|
- intx);
|
2006-11-23 19:48:14 -05:00
|
|
|
if(borrow){
|
2007-01-21 19:20:37 -05:00
|
|
|
fprintf(stderr, "Error: BUG in borrow2\n");
|
2006-11-23 19:48:14 -05:00
|
|
|
exit(-1);
|
|
|
|
}
|
2008-01-01 04:24:36 -05:00
|
|
|
long int result_size =
|
2006-11-23 19:48:14 -05:00
|
|
|
(ref(r, disp_bignum_data + (limb_count-1)*wordsize) == 0)
|
|
|
|
? (limb_count - 1)
|
|
|
|
: limb_count;
|
|
|
|
if(result_size == 0){
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
if(result_size == 1){
|
2008-01-01 04:24:36 -05:00
|
|
|
mp_limb_t last =
|
|
|
|
(mp_limb_t) ref(r, disp_bignum_data + (result_size-1)*wordsize);
|
2006-11-23 19:48:14 -05:00
|
|
|
if(last <= most_positive_fixnum){
|
2008-01-01 04:24:36 -05:00
|
|
|
return fix((long int)last);
|
2006-11-23 19:48:14 -05:00
|
|
|
}
|
|
|
|
}
|
2007-12-23 13:37:48 -05:00
|
|
|
ref(r, 0) = (ikptr)
|
2006-11-23 19:48:14 -05:00
|
|
|
((result_size << bignum_length_shift) |
|
|
|
|
(0 << bignum_sign_shift) |
|
|
|
|
bignum_tag);
|
2007-01-21 19:20:37 -05:00
|
|
|
return verify_bignum(r+vector_tag, "fxbn+4");
|
2006-11-23 19:48:14 -05:00
|
|
|
} else {
|
|
|
|
/* negative fx + negative bn = larger negative */
|
2007-11-18 10:37:13 -05:00
|
|
|
pcb->root0 = &y;
|
2007-12-23 13:37:48 -05:00
|
|
|
ikptr r = ik_safe_alloc(pcb, align(disp_bignum_data+(limb_count+1)*wordsize));
|
2007-11-18 10:37:13 -05:00
|
|
|
pcb->root0 = 0;
|
2008-01-01 04:24:36 -05:00
|
|
|
mp_limb_t carry =
|
2008-01-01 21:42:52 -05:00
|
|
|
mpn_add_1((mp_limb_t*)(long)(r+disp_bignum_data),
|
|
|
|
(mp_limb_t*)(long)(y - vector_tag + disp_bignum_data),
|
2008-01-01 04:24:36 -05:00
|
|
|
limb_count,
|
|
|
|
-intx);
|
2006-11-23 19:48:14 -05:00
|
|
|
if(carry){
|
2007-12-23 13:37:48 -05:00
|
|
|
ref(r, disp_bignum_data + limb_count*wordsize) = (ikptr)1;
|
|
|
|
ref(r, 0) = (ikptr)
|
2006-11-23 19:48:14 -05:00
|
|
|
(((limb_count + 1) << bignum_length_shift) |
|
|
|
|
(1 << bignum_sign_shift) |
|
|
|
|
bignum_tag);
|
2007-01-21 19:20:37 -05:00
|
|
|
return verify_bignum(r+vector_tag, "fxbn+5");
|
2006-11-23 19:48:14 -05:00
|
|
|
} else {
|
2007-12-23 13:37:48 -05:00
|
|
|
ref(r, 0) = (ikptr)
|
2006-11-23 19:48:14 -05:00
|
|
|
((limb_count << bignum_length_shift) |
|
|
|
|
(1 << bignum_sign_shift) |
|
|
|
|
bignum_tag);
|
2007-01-21 19:20:37 -05:00
|
|
|
return verify_bignum(r+vector_tag, "fxbn+5");
|
2006-11-23 19:48:14 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2007-12-23 13:37:48 -05:00
|
|
|
ikptr
|
|
|
|
ikrt_bnbnplus(ikptr x, ikptr y, ikpcb* pcb){
|
2008-01-01 04:24:36 -05:00
|
|
|
unsigned long int xfst = (unsigned long int)ref(x, -vector_tag);
|
|
|
|
unsigned long int yfst = (unsigned long int)ref(y, -vector_tag);
|
|
|
|
long int xsign = xfst & bignum_sign_mask;
|
|
|
|
long int ysign = yfst & bignum_sign_mask;
|
|
|
|
long int xlimbs = xfst >> bignum_length_shift;
|
|
|
|
long int ylimbs = yfst >> bignum_length_shift;
|
2006-11-23 19:48:14 -05:00
|
|
|
if(xsign == ysign){
|
2008-01-01 04:24:36 -05:00
|
|
|
long int n1,n2;
|
2007-12-23 13:37:48 -05:00
|
|
|
ikptr s1,s2;
|
2006-11-23 19:48:14 -05:00
|
|
|
if(xlimbs > ylimbs){
|
|
|
|
n1 = xlimbs; n2 = ylimbs; s1 = x; s2 = y;
|
|
|
|
} else {
|
|
|
|
n1 = ylimbs; n2 = xlimbs; s1 = y; s2 = x;
|
|
|
|
}
|
2007-11-18 10:37:13 -05:00
|
|
|
pcb->root0 = &s1;
|
|
|
|
pcb->root1 = &s2;
|
2007-12-23 13:37:48 -05:00
|
|
|
ikptr res = ik_safe_alloc(pcb, align(disp_bignum_data + (n1+1)*wordsize));
|
2007-11-18 10:37:13 -05:00
|
|
|
pcb->root0 = 0;
|
|
|
|
pcb->root1 = 0;
|
2008-01-01 04:24:36 -05:00
|
|
|
mp_limb_t carry =
|
2008-01-01 21:42:52 -05:00
|
|
|
mpn_add((mp_limb_t*)(long)(res+disp_bignum_data),
|
|
|
|
(mp_limb_t*)(long)(s1-vector_tag+disp_bignum_data),
|
2008-01-01 04:24:36 -05:00
|
|
|
n1,
|
2008-01-01 21:42:52 -05:00
|
|
|
(mp_limb_t*)(long)(s2-vector_tag+disp_bignum_data),
|
2008-01-01 04:24:36 -05:00
|
|
|
n2);
|
2006-11-23 19:48:14 -05:00
|
|
|
if(carry){
|
2007-12-23 13:37:48 -05:00
|
|
|
ref(res, disp_vector_data + xlimbs*wordsize) = (ikptr)1;
|
|
|
|
ref(res, 0) = (ikptr)
|
2006-11-23 19:48:14 -05:00
|
|
|
(((n1+1) << bignum_length_shift) |
|
|
|
|
xsign |
|
|
|
|
bignum_tag);
|
2007-01-21 19:20:37 -05:00
|
|
|
return verify_bignum(res+vector_tag, "bnbn+1");
|
2006-11-23 19:48:14 -05:00
|
|
|
} else {
|
2007-12-23 13:37:48 -05:00
|
|
|
ref(res, 0) = (ikptr)
|
2006-11-23 19:48:14 -05:00
|
|
|
((n1 << bignum_length_shift) |
|
|
|
|
xsign |
|
|
|
|
bignum_tag);
|
2007-01-21 19:20:37 -05:00
|
|
|
return verify_bignum(res+vector_tag, "bnbn+2");
|
2006-11-23 19:48:14 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
2007-12-23 13:37:48 -05:00
|
|
|
ikptr s1=x, s2=y;
|
2008-01-01 04:24:36 -05:00
|
|
|
long int n1=xlimbs, n2=ylimbs;
|
|
|
|
long int result_sign = xsign;
|
2006-11-23 19:48:14 -05:00
|
|
|
while((xlimbs == ylimbs) &&
|
|
|
|
(ref(x, -vector_tag+disp_bignum_data+(xlimbs-1)*wordsize) ==
|
|
|
|
ref(y, -vector_tag+disp_bignum_data+(xlimbs-1)*wordsize))){
|
|
|
|
xlimbs -= 1;
|
|
|
|
ylimbs -= 1;
|
|
|
|
if(xlimbs == 0){ return 0; }
|
|
|
|
}
|
|
|
|
/* |x| != |y| */
|
|
|
|
if(xlimbs <= ylimbs){
|
|
|
|
if(xlimbs == ylimbs){
|
|
|
|
if((ref(y, -vector_tag+disp_bignum_data+(xlimbs-1)*wordsize) >
|
|
|
|
ref(x, -vector_tag+disp_bignum_data+(xlimbs-1)*wordsize))){
|
|
|
|
s1 = y; n1 = ylimbs;
|
|
|
|
s2 = x; n2 = xlimbs;
|
|
|
|
result_sign = ysign;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
s1 = y; n1 = ylimbs;
|
|
|
|
s2 = x; n2 = xlimbs;
|
|
|
|
result_sign = ysign;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/* |s1| > |s2| */
|
2007-11-18 10:37:13 -05:00
|
|
|
pcb->root0 = &s1;
|
|
|
|
pcb->root1 = &s2;
|
2007-12-23 13:37:48 -05:00
|
|
|
ikptr res = ik_safe_alloc(pcb, align(disp_bignum_data + n1 * wordsize));
|
2007-11-18 10:37:13 -05:00
|
|
|
pcb->root0 = 0;
|
|
|
|
pcb->root1 = 0;
|
2008-01-01 04:24:36 -05:00
|
|
|
mp_limb_t burrow =
|
2008-01-01 21:42:52 -05:00
|
|
|
mpn_sub((mp_limb_t*)(long)(res + disp_bignum_data),
|
|
|
|
(mp_limb_t*)(long)(s1 - vector_tag + disp_bignum_data),
|
2008-01-01 04:24:36 -05:00
|
|
|
n1,
|
2008-01-01 21:42:52 -05:00
|
|
|
(mp_limb_t*)(long)(s2 - vector_tag + disp_bignum_data),
|
2008-01-01 04:24:36 -05:00
|
|
|
n2);
|
2006-11-23 19:48:14 -05:00
|
|
|
if(burrow){
|
|
|
|
fprintf(stderr, "BUG: Burrow error in bnbn+\n");
|
|
|
|
exit(-1);
|
|
|
|
}
|
2008-01-01 04:24:36 -05:00
|
|
|
long int len = n1;
|
2006-11-23 19:48:14 -05:00
|
|
|
while(ref(res, disp_bignum_data + (len-1)*wordsize) == 0){
|
|
|
|
len--;
|
|
|
|
if(len == 0){
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if(result_sign == 0){
|
|
|
|
/* positive result */
|
|
|
|
if(len == 1){
|
2008-01-01 04:24:36 -05:00
|
|
|
mp_limb_t fst_limb = (mp_limb_t) ref(res, disp_bignum_data);
|
2006-11-23 19:48:14 -05:00
|
|
|
if(fst_limb <= most_positive_fixnum){
|
2008-01-01 04:24:36 -05:00
|
|
|
return fix((long int)fst_limb);
|
2006-11-23 19:48:14 -05:00
|
|
|
}
|
|
|
|
}
|
2007-12-23 13:37:48 -05:00
|
|
|
ref(res, 0) = (ikptr)
|
2006-11-23 19:48:14 -05:00
|
|
|
((len << bignum_length_shift) |
|
|
|
|
result_sign |
|
|
|
|
bignum_tag);
|
2007-01-21 19:20:37 -05:00
|
|
|
return verify_bignum(res+vector_tag, "bnbn+3");
|
2006-11-23 19:48:14 -05:00
|
|
|
} else {
|
|
|
|
/* negative result */
|
|
|
|
if(len == 1){
|
2008-01-01 04:24:36 -05:00
|
|
|
mp_limb_t fst_limb = (mp_limb_t) ref(res, disp_bignum_data);
|
2006-11-23 19:48:14 -05:00
|
|
|
if(fst_limb <= most_negative_fixnum){
|
2008-01-01 04:24:36 -05:00
|
|
|
return fix(-(long int)fst_limb);
|
2006-11-23 19:48:14 -05:00
|
|
|
}
|
|
|
|
}
|
2007-12-23 13:37:48 -05:00
|
|
|
ref(res, 0) = (ikptr)
|
2006-11-23 19:48:14 -05:00
|
|
|
((len << bignum_length_shift) |
|
|
|
|
result_sign |
|
|
|
|
bignum_tag);
|
2007-01-21 19:20:37 -05:00
|
|
|
return verify_bignum(res+vector_tag, "bnbn+4");
|
2006-11-23 19:48:14 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2007-12-23 13:37:48 -05:00
|
|
|
ikptr
|
|
|
|
ikrt_fxfxminus(ikptr x, ikptr y, ikpcb* pcb){
|
2008-01-01 04:24:36 -05:00
|
|
|
long int n1 = unfix(x);
|
|
|
|
long int n2 = unfix(y);
|
|
|
|
long int r = n1 - n2;
|
2006-11-23 19:48:14 -05:00
|
|
|
if(r >= 0){
|
2008-01-01 04:24:36 -05:00
|
|
|
if(((unsigned long int)r) <= most_positive_fixnum){
|
2006-11-23 19:48:14 -05:00
|
|
|
return fix(r);
|
|
|
|
} else {
|
2007-12-23 13:37:48 -05:00
|
|
|
ikptr bn = ik_safe_alloc(pcb, align(disp_bignum_data + wordsize));
|
|
|
|
ref(bn, 0) = (ikptr) (bignum_tag | (1 << bignum_length_shift));
|
|
|
|
ref(bn, disp_bignum_data) = (ikptr)r;
|
2007-01-21 19:20:37 -05:00
|
|
|
return verify_bignum(bn+vector_tag,"fxfx-1");
|
2006-11-23 19:48:14 -05:00
|
|
|
}
|
|
|
|
} else {
|
2007-12-23 13:37:48 -05:00
|
|
|
ikptr fxr = fix(r);
|
2007-01-21 19:20:37 -05:00
|
|
|
if(unfix(fxr) == r){
|
|
|
|
return fxr;
|
2006-11-23 19:48:14 -05:00
|
|
|
} else {
|
2007-12-23 13:37:48 -05:00
|
|
|
ikptr bn = ik_safe_alloc(pcb, align(disp_bignum_data + wordsize));
|
|
|
|
ref(bn, 0) = (ikptr)
|
2006-11-23 19:48:14 -05:00
|
|
|
(bignum_tag |
|
|
|
|
(1 << bignum_sign_shift) |
|
|
|
|
(1 << bignum_length_shift));
|
2007-12-23 13:37:48 -05:00
|
|
|
ref(bn, disp_bignum_data) = (ikptr)(-r);
|
2007-01-21 19:20:37 -05:00
|
|
|
return verify_bignum(bn+vector_tag, "fxfx-2");
|
2006-11-23 19:48:14 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-12-23 13:37:48 -05:00
|
|
|
ikptr
|
|
|
|
ikrt_bnnegate(ikptr x, ikpcb* pcb){
|
|
|
|
ikptr fst = ref(x, -vector_tag);
|
2008-01-01 04:24:36 -05:00
|
|
|
long int limb_count = bnfst_limb_count(fst);
|
2006-11-23 19:48:14 -05:00
|
|
|
if(limb_count == 1){
|
2008-01-01 05:17:42 -05:00
|
|
|
if(! bnfst_negative(fst)){
|
2006-11-23 19:48:14 -05:00
|
|
|
/* positive bignum */
|
2008-01-01 04:24:36 -05:00
|
|
|
mp_limb_t limb =
|
|
|
|
(mp_limb_t) ref(x, disp_bignum_data - vector_tag);
|
2006-11-23 19:48:14 -05:00
|
|
|
if(limb == (most_positive_fixnum + 1)){
|
2008-01-01 04:24:36 -05:00
|
|
|
return fix(-(long int)limb);
|
2006-11-23 19:48:14 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2007-11-18 10:37:13 -05:00
|
|
|
pcb->root0 = &x;
|
2007-12-23 13:37:48 -05:00
|
|
|
ikptr bn = ik_safe_alloc(pcb, align(disp_bignum_data + limb_count * wordsize));
|
2007-11-18 10:37:13 -05:00
|
|
|
pcb->root0 = 0;
|
2008-01-01 21:42:52 -05:00
|
|
|
memcpy((char*)(long)bn+disp_bignum_data,
|
|
|
|
(char*)(long)x-vector_tag+disp_bignum_data,
|
2006-11-23 19:48:14 -05:00
|
|
|
limb_count*wordsize);
|
2007-12-23 13:37:48 -05:00
|
|
|
ref(bn, 0) = (ikptr)
|
2006-11-23 19:48:14 -05:00
|
|
|
(bignum_tag |
|
2008-01-01 04:24:36 -05:00
|
|
|
((1 << bignum_sign_shift) - (bignum_sign_mask & (long int)fst)) |
|
2006-11-23 19:48:14 -05:00
|
|
|
(limb_count << bignum_length_shift));
|
2007-01-21 19:20:37 -05:00
|
|
|
return verify_bignum(bn+vector_tag, "bnneg");
|
2006-11-23 19:48:14 -05:00
|
|
|
}
|
|
|
|
|
2007-12-23 13:37:48 -05:00
|
|
|
ikptr
|
|
|
|
ikrt_fxbnminus(ikptr x, ikptr y, ikpcb* pcb){
|
2006-11-23 19:48:14 -05:00
|
|
|
if(x == 0){ return ikrt_bnnegate(y, pcb) ; }
|
2007-12-23 13:37:48 -05:00
|
|
|
ikptr fst = ref(y, -vector_tag);
|
2008-01-01 04:24:36 -05:00
|
|
|
long int limb_count = bnfst_limb_count(fst);
|
|
|
|
long int intx = unfix(x);
|
2006-11-23 19:48:14 -05:00
|
|
|
if(intx > 0){
|
2008-01-01 04:24:36 -05:00
|
|
|
if(bnfst_negative(fst)){
|
2006-11-23 19:48:14 -05:00
|
|
|
/* positive fx - negative bn = positive bn */
|
2007-11-18 10:37:13 -05:00
|
|
|
pcb->root0 = &y;
|
2007-12-23 13:37:48 -05:00
|
|
|
ikptr r = ik_safe_alloc(pcb, align(disp_bignum_data+(limb_count+1)*wordsize));
|
2007-11-18 10:37:13 -05:00
|
|
|
pcb->root0 = 0;
|
2008-01-01 04:24:36 -05:00
|
|
|
long int carry =
|
2008-01-01 21:42:52 -05:00
|
|
|
mpn_add_1((mp_limb_t*)(long)(r+disp_bignum_data),
|
|
|
|
(mp_limb_t*)(long)(y - vector_tag + disp_bignum_data),
|
2008-01-01 04:24:36 -05:00
|
|
|
limb_count,
|
|
|
|
intx);
|
2006-11-23 19:48:14 -05:00
|
|
|
if(carry){
|
2007-12-23 13:37:48 -05:00
|
|
|
ref(r, disp_bignum_data + limb_count*wordsize) = (ikptr)1;
|
|
|
|
ref(r, 0) = (ikptr)
|
2006-11-23 19:48:14 -05:00
|
|
|
(((limb_count + 1) << bignum_length_shift) |
|
|
|
|
(0 << bignum_sign_shift) |
|
|
|
|
bignum_tag);
|
2007-01-21 19:20:37 -05:00
|
|
|
return verify_bignum(r+vector_tag, "fxbn-1");
|
2006-11-23 19:48:14 -05:00
|
|
|
} else {
|
2007-12-23 13:37:48 -05:00
|
|
|
ref(r, 0) = (ikptr)
|
2006-11-23 19:48:14 -05:00
|
|
|
((limb_count << bignum_length_shift) |
|
|
|
|
(0 << bignum_sign_shift) |
|
|
|
|
bignum_tag);
|
2007-01-21 19:20:37 -05:00
|
|
|
return verify_bignum(r+vector_tag, "fxbn-2");
|
2006-11-23 19:48:14 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
/* positive fx - positive bn = smaller negative bn/fx */
|
2007-11-18 10:37:13 -05:00
|
|
|
pcb->root0 = &y;
|
2007-12-23 13:37:48 -05:00
|
|
|
ikptr r = ik_safe_alloc(pcb, align(disp_bignum_data+limb_count*wordsize));
|
2007-11-18 10:37:13 -05:00
|
|
|
pcb->root0 = 0;
|
2008-01-01 04:24:36 -05:00
|
|
|
long int borrow =
|
2008-01-01 21:42:52 -05:00
|
|
|
mpn_sub_1((mp_limb_t*)(long)(r+disp_bignum_data),
|
|
|
|
(mp_limb_t*)(long)(y - vector_tag + disp_bignum_data),
|
2008-01-01 04:24:36 -05:00
|
|
|
limb_count,
|
|
|
|
intx);
|
2006-11-23 19:48:14 -05:00
|
|
|
if(borrow){
|
2007-01-21 19:20:37 -05:00
|
|
|
fprintf(stderr, "Error: BUG in borrow3\n");
|
2006-11-23 19:48:14 -05:00
|
|
|
exit(-1);
|
|
|
|
}
|
2008-01-01 04:24:36 -05:00
|
|
|
long int result_size =
|
2006-11-23 19:48:14 -05:00
|
|
|
(ref(r, disp_bignum_data + (limb_count-1)*wordsize))
|
|
|
|
? limb_count
|
|
|
|
: (limb_count - 1);
|
|
|
|
if(result_size == 0){
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
if(result_size == 1){
|
2008-01-01 04:24:36 -05:00
|
|
|
unsigned long int last =
|
|
|
|
(unsigned long int) ref(r, disp_bignum_data + (result_size-1)*wordsize);
|
2006-11-23 19:48:14 -05:00
|
|
|
if(last <= most_negative_fixnum){
|
2008-01-01 04:24:36 -05:00
|
|
|
return fix(-(long int)last);
|
2006-11-23 19:48:14 -05:00
|
|
|
}
|
|
|
|
}
|
2007-12-23 13:37:48 -05:00
|
|
|
ref(r, 0) = (ikptr)
|
2006-11-23 19:48:14 -05:00
|
|
|
((result_size << bignum_length_shift) |
|
|
|
|
(1 << bignum_sign_shift) |
|
|
|
|
bignum_tag);
|
2007-01-21 19:20:37 -05:00
|
|
|
return verify_bignum(r+vector_tag, "fxbn-");
|
2006-11-23 19:48:14 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
2008-01-01 04:24:36 -05:00
|
|
|
if(bnfst_negative(fst)){
|
2006-11-23 19:48:14 -05:00
|
|
|
/* negative fx - negative bn = smaller positive */
|
2007-11-18 10:37:13 -05:00
|
|
|
pcb->root0 = &y;
|
2007-12-23 13:37:48 -05:00
|
|
|
ikptr r = ik_safe_alloc(pcb, align(disp_bignum_data+limb_count*wordsize));
|
2007-11-18 10:37:13 -05:00
|
|
|
pcb->root0 = 0;
|
2008-01-01 04:24:36 -05:00
|
|
|
long int borrow =
|
2008-01-01 21:42:52 -05:00
|
|
|
mpn_sub_1((mp_limb_t*)(long)(r+disp_bignum_data),
|
|
|
|
(mp_limb_t*)(long)(y - vector_tag + disp_bignum_data),
|
2008-01-01 04:24:36 -05:00
|
|
|
limb_count,
|
|
|
|
- intx);
|
2006-11-23 19:48:14 -05:00
|
|
|
if(borrow){
|
2007-01-21 19:20:37 -05:00
|
|
|
fprintf(stderr, "Error: BUG in borrow4\n");
|
2006-11-23 19:48:14 -05:00
|
|
|
exit(-1);
|
|
|
|
}
|
2008-01-01 04:24:36 -05:00
|
|
|
long int result_size =
|
2006-11-23 19:48:14 -05:00
|
|
|
(ref(r, disp_bignum_data + (limb_count-1)*wordsize) == 0)
|
|
|
|
? (limb_count - 1)
|
|
|
|
: limb_count;
|
|
|
|
if(result_size == 0){
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
if(result_size == 1){
|
2008-01-01 04:24:36 -05:00
|
|
|
unsigned long int last =
|
|
|
|
(unsigned long int) ref(r, disp_bignum_data + (result_size-1)*wordsize);
|
2006-11-23 19:48:14 -05:00
|
|
|
if(last <= most_positive_fixnum){
|
2008-01-01 04:24:36 -05:00
|
|
|
return fix((long int)last);
|
2006-11-23 19:48:14 -05:00
|
|
|
}
|
|
|
|
}
|
2007-12-23 13:37:48 -05:00
|
|
|
ref(r, 0) = (ikptr)
|
2006-11-23 19:48:14 -05:00
|
|
|
((result_size << bignum_length_shift) |
|
|
|
|
(0 << bignum_sign_shift) |
|
|
|
|
bignum_tag);
|
2007-01-21 19:20:37 -05:00
|
|
|
return verify_bignum(r+vector_tag,"fxbn-");
|
2006-11-23 19:48:14 -05:00
|
|
|
} else {
|
|
|
|
/* negative fx - positive bn = larger negative */
|
2007-11-18 10:37:13 -05:00
|
|
|
pcb->root0 = &y;
|
2007-12-23 13:37:48 -05:00
|
|
|
ikptr r = ik_safe_alloc(pcb, align(disp_bignum_data+(limb_count+1)*wordsize));
|
2007-11-18 10:37:13 -05:00
|
|
|
pcb->root0 = 0;
|
2008-01-01 04:24:36 -05:00
|
|
|
long int carry =
|
2008-01-01 21:42:52 -05:00
|
|
|
mpn_add_1((mp_limb_t*)(long)(r+disp_bignum_data),
|
|
|
|
(mp_limb_t*)(long)(y - vector_tag + disp_bignum_data),
|
2008-01-01 04:24:36 -05:00
|
|
|
limb_count,
|
|
|
|
-intx);
|
2006-11-23 19:48:14 -05:00
|
|
|
if(carry){
|
2007-12-23 13:37:48 -05:00
|
|
|
ref(r, disp_bignum_data + limb_count*wordsize) = (ikptr)1;
|
|
|
|
ref(r, 0) = (ikptr)
|
2006-11-23 19:48:14 -05:00
|
|
|
(((limb_count + 1) << bignum_length_shift) |
|
|
|
|
(1 << bignum_sign_shift) |
|
|
|
|
bignum_tag);
|
2007-01-21 19:20:37 -05:00
|
|
|
return verify_bignum(r+vector_tag, "fxbn-");
|
2006-11-23 19:48:14 -05:00
|
|
|
} else {
|
2007-12-23 13:37:48 -05:00
|
|
|
ref(r, 0) = (ikptr)
|
2006-11-23 19:48:14 -05:00
|
|
|
((limb_count << bignum_length_shift) |
|
|
|
|
(1 << bignum_sign_shift) |
|
|
|
|
bignum_tag);
|
2007-01-21 19:20:37 -05:00
|
|
|
return verify_bignum(r+vector_tag, "fxbn-");
|
2006-11-23 19:48:14 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-12-23 13:37:48 -05:00
|
|
|
ikptr
|
|
|
|
ikrt_bnfxminus(ikptr x, ikptr y, ikpcb* pcb){
|
2006-11-23 19:48:14 -05:00
|
|
|
if(y == 0){ return x; }
|
2007-12-23 13:37:48 -05:00
|
|
|
ikptr fst = ref(x, -vector_tag);
|
2008-01-01 04:24:36 -05:00
|
|
|
long int limb_count = bnfst_limb_count(fst);
|
|
|
|
long int inty = unfix(y);
|
2006-11-23 19:48:14 -05:00
|
|
|
if(inty < 0){
|
2008-01-01 05:17:42 -05:00
|
|
|
if(!bnfst_negative(fst)){
|
2006-11-23 19:48:14 -05:00
|
|
|
/* - negative fx + positive bn = positive bn */
|
2007-11-18 10:37:13 -05:00
|
|
|
pcb->root0 = &x;
|
2007-12-23 13:37:48 -05:00
|
|
|
ikptr r = ik_safe_alloc(pcb, align(disp_bignum_data+(limb_count+1)*wordsize));
|
2007-11-18 10:37:13 -05:00
|
|
|
pcb->root0 = 0;
|
2008-01-01 04:24:36 -05:00
|
|
|
long int carry =
|
2008-01-01 21:42:52 -05:00
|
|
|
mpn_add_1((mp_limb_t*)(long)(r+disp_bignum_data),
|
|
|
|
(mp_limb_t*)(long)(x - vector_tag + disp_bignum_data),
|
2008-01-01 04:24:36 -05:00
|
|
|
limb_count,
|
|
|
|
-inty);
|
2006-11-23 19:48:14 -05:00
|
|
|
if(carry){
|
2007-12-23 13:37:48 -05:00
|
|
|
ref(r, disp_bignum_data + limb_count*wordsize) = (ikptr)1;
|
|
|
|
ref(r, 0) = (ikptr)
|
2006-11-23 19:48:14 -05:00
|
|
|
(((limb_count + 1) << bignum_length_shift) |
|
|
|
|
(0 << bignum_sign_shift) |
|
|
|
|
bignum_tag);
|
2007-01-21 19:20:37 -05:00
|
|
|
return verify_bignum(r+vector_tag,"bnfx-");
|
2006-11-23 19:48:14 -05:00
|
|
|
} else {
|
2007-12-23 13:37:48 -05:00
|
|
|
ref(r, 0) = (ikptr)
|
2006-11-23 19:48:14 -05:00
|
|
|
((limb_count << bignum_length_shift) |
|
|
|
|
(0 << bignum_sign_shift) |
|
|
|
|
bignum_tag);
|
2007-01-21 19:20:37 -05:00
|
|
|
return verify_bignum(r+vector_tag,"bnfx-");
|
2006-11-23 19:48:14 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
/* - negative fx + negative bn = smaller negative bn/fx */
|
2007-11-18 10:37:13 -05:00
|
|
|
pcb->root0 = &x;
|
2007-12-23 13:37:48 -05:00
|
|
|
ikptr r = ik_safe_alloc(pcb, align(disp_bignum_data+limb_count*wordsize));
|
2007-11-18 10:37:13 -05:00
|
|
|
pcb->root0 = 0;
|
2008-01-01 04:24:36 -05:00
|
|
|
long int borrow =
|
2008-01-01 21:42:52 -05:00
|
|
|
mpn_sub_1((mp_limb_t*)(long)(r+disp_bignum_data),
|
|
|
|
(mp_limb_t*)(long)(x - vector_tag + disp_bignum_data),
|
2008-01-01 04:24:36 -05:00
|
|
|
limb_count,
|
|
|
|
-inty);
|
2006-11-23 19:48:14 -05:00
|
|
|
if(borrow){
|
2007-01-21 19:20:37 -05:00
|
|
|
fprintf(stderr, "Error: BUG in borrow5\n");
|
2006-11-23 19:48:14 -05:00
|
|
|
exit(-1);
|
|
|
|
}
|
2008-01-01 04:24:36 -05:00
|
|
|
long int result_size =
|
2006-11-23 19:48:14 -05:00
|
|
|
(ref(r, disp_bignum_data + (limb_count-1)*wordsize))
|
|
|
|
? limb_count
|
|
|
|
: (limb_count - 1);
|
|
|
|
if(result_size == 0){
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
if(result_size == 1){
|
2008-01-01 04:24:36 -05:00
|
|
|
unsigned long int last =
|
|
|
|
(unsigned long int) ref(r, disp_bignum_data + (result_size-1)*wordsize);
|
2006-11-23 19:48:14 -05:00
|
|
|
if(last <= most_negative_fixnum){
|
2008-01-01 04:24:36 -05:00
|
|
|
return fix(-(long int)last);
|
2006-11-23 19:48:14 -05:00
|
|
|
}
|
|
|
|
}
|
2007-12-23 13:37:48 -05:00
|
|
|
ref(r, 0) = (ikptr)
|
2006-11-23 19:48:14 -05:00
|
|
|
((result_size << bignum_length_shift) |
|
|
|
|
(1 << bignum_sign_shift) |
|
|
|
|
bignum_tag);
|
2007-01-21 19:20:37 -05:00
|
|
|
return verify_bignum(r+vector_tag,"bnfx-");
|
2006-11-23 19:48:14 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
2008-01-01 04:24:36 -05:00
|
|
|
if((bignum_sign_mask & (long int)fst) == 0){
|
2006-11-23 19:48:14 -05:00
|
|
|
/* - positive fx + positive bn = smaller positive */
|
2007-11-18 10:37:13 -05:00
|
|
|
pcb->root0 = &x;
|
2007-12-23 13:37:48 -05:00
|
|
|
ikptr r = ik_safe_alloc(pcb, align(disp_bignum_data+limb_count*wordsize));
|
2007-11-18 10:37:13 -05:00
|
|
|
pcb->root0 = 0;
|
2008-01-01 04:24:36 -05:00
|
|
|
long int borrow =
|
2008-01-01 21:42:52 -05:00
|
|
|
mpn_sub_1((mp_limb_t*)(long)(r+disp_bignum_data),
|
|
|
|
(mp_limb_t*)(long)(x - vector_tag + disp_bignum_data),
|
2008-01-01 04:24:36 -05:00
|
|
|
limb_count,
|
|
|
|
inty);
|
2006-11-23 19:48:14 -05:00
|
|
|
if(borrow){
|
2007-01-21 19:20:37 -05:00
|
|
|
fprintf(stderr, "Error: BUG in borrow6\n");
|
2006-11-23 19:48:14 -05:00
|
|
|
exit(-1);
|
|
|
|
}
|
2008-01-01 04:24:36 -05:00
|
|
|
long int result_size =
|
2006-11-23 19:48:14 -05:00
|
|
|
(ref(r, disp_bignum_data + (limb_count-1)*wordsize) == 0)
|
|
|
|
? (limb_count - 1)
|
|
|
|
: limb_count;
|
|
|
|
if(result_size == 0){
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
if(result_size == 1){
|
2008-01-01 04:24:36 -05:00
|
|
|
unsigned long int last =
|
|
|
|
(unsigned long int) ref(r, disp_bignum_data + (result_size-1)*wordsize);
|
2006-11-23 19:48:14 -05:00
|
|
|
if(last <= most_positive_fixnum){
|
2008-01-01 04:24:36 -05:00
|
|
|
return fix((long int)last);
|
2006-11-23 19:48:14 -05:00
|
|
|
}
|
|
|
|
}
|
2007-12-23 13:37:48 -05:00
|
|
|
ref(r, 0) = (ikptr)
|
2006-11-23 19:48:14 -05:00
|
|
|
((result_size << bignum_length_shift) |
|
|
|
|
(0 << bignum_sign_shift) |
|
|
|
|
bignum_tag);
|
2007-01-21 19:20:37 -05:00
|
|
|
return verify_bignum(r+vector_tag, "bnfx-");
|
2006-11-23 19:48:14 -05:00
|
|
|
} else {
|
|
|
|
/* - positive fx + negative bn = larger negative */
|
2007-11-18 10:37:13 -05:00
|
|
|
pcb->root0 = &x;
|
2007-12-23 13:37:48 -05:00
|
|
|
ikptr r = ik_safe_alloc(pcb, align(disp_bignum_data+(limb_count+1)*wordsize));
|
2007-11-18 10:37:13 -05:00
|
|
|
pcb->root0 = 0;
|
2008-01-01 04:24:36 -05:00
|
|
|
long int carry =
|
2008-01-01 21:42:52 -05:00
|
|
|
mpn_add_1((mp_limb_t*)(long)(r+disp_bignum_data),
|
|
|
|
(mp_limb_t*)(long)(x - vector_tag + disp_bignum_data),
|
2008-01-01 04:24:36 -05:00
|
|
|
limb_count,
|
|
|
|
inty);
|
2006-11-23 19:48:14 -05:00
|
|
|
if(carry){
|
2007-12-23 13:37:48 -05:00
|
|
|
ref(r, disp_bignum_data + limb_count*wordsize) = (ikptr)1;
|
|
|
|
ref(r, 0) = (ikptr)
|
2006-11-23 19:48:14 -05:00
|
|
|
(((limb_count + 1) << bignum_length_shift) |
|
|
|
|
(1 << bignum_sign_shift) |
|
|
|
|
bignum_tag);
|
2007-01-21 19:20:37 -05:00
|
|
|
return verify_bignum(r+vector_tag, "bnfx-");
|
2006-11-23 19:48:14 -05:00
|
|
|
} else {
|
2007-12-23 13:37:48 -05:00
|
|
|
ref(r, 0) = (ikptr)
|
2006-11-23 19:48:14 -05:00
|
|
|
((limb_count << bignum_length_shift) |
|
|
|
|
(1 << bignum_sign_shift) |
|
|
|
|
bignum_tag);
|
2007-01-21 19:20:37 -05:00
|
|
|
return verify_bignum(r+vector_tag, "bnfx-");
|
2006-11-23 19:48:14 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2007-12-23 13:37:48 -05:00
|
|
|
ikptr
|
|
|
|
ikrt_bnbnminus(ikptr x, ikptr y, ikpcb* pcb){
|
2006-11-23 19:48:14 -05:00
|
|
|
if(x == y) { return 0; }
|
2008-01-01 04:24:36 -05:00
|
|
|
unsigned long int xfst = (unsigned long int)ref(x, -vector_tag);
|
|
|
|
unsigned long int yfst = (unsigned long int)ref(y, -vector_tag);
|
|
|
|
long int xsign = xfst & bignum_sign_mask;
|
|
|
|
long int ysign = yfst & bignum_sign_mask;
|
|
|
|
long int xlimbs = xfst >> bignum_length_shift;
|
|
|
|
long int ylimbs = yfst >> bignum_length_shift;
|
2006-11-23 19:48:14 -05:00
|
|
|
if(xsign != ysign){
|
2008-01-01 04:24:36 -05:00
|
|
|
long int n1,n2;
|
2007-12-23 13:37:48 -05:00
|
|
|
ikptr s1,s2;
|
2006-11-23 19:48:14 -05:00
|
|
|
if(xlimbs >= ylimbs){
|
|
|
|
n1 = xlimbs; n2 = ylimbs; s1 = x; s2 = y;
|
|
|
|
} else {
|
|
|
|
n1 = ylimbs; n2 = xlimbs; s1 = y; s2 = x;
|
|
|
|
}
|
2007-11-18 10:37:13 -05:00
|
|
|
pcb->root0 = &s1;
|
2008-10-04 01:59:34 -04:00
|
|
|
pcb->root1 = &s2;
|
2007-12-23 13:37:48 -05:00
|
|
|
ikptr res = ik_safe_alloc(pcb, align(disp_bignum_data + (n1+1)*wordsize));
|
2007-11-18 10:37:13 -05:00
|
|
|
pcb->root0 = 0;
|
2008-10-04 01:59:34 -04:00
|
|
|
pcb->root1 = 0;
|
2008-01-01 21:42:52 -05:00
|
|
|
mp_limb_t carry =
|
|
|
|
mpn_add((mp_limb_t*)(long)(res+disp_bignum_data),
|
|
|
|
(mp_limb_t*)(long)(s1-vector_tag+disp_bignum_data),
|
|
|
|
n1,
|
|
|
|
(mp_limb_t*)(long)(s2-vector_tag+disp_bignum_data),
|
|
|
|
n2);
|
2006-11-23 19:48:14 -05:00
|
|
|
if(carry){
|
2007-12-23 13:37:48 -05:00
|
|
|
ref(res, disp_vector_data + xlimbs*wordsize) = (ikptr)1;
|
|
|
|
ref(res, 0) = (ikptr)
|
2006-11-23 19:48:14 -05:00
|
|
|
(((n1+1) << bignum_length_shift) |
|
|
|
|
xsign |
|
|
|
|
bignum_tag);
|
2007-01-21 19:20:37 -05:00
|
|
|
return verify_bignum(res+vector_tag, "bnbn-");
|
2006-11-23 19:48:14 -05:00
|
|
|
} else {
|
2007-12-23 13:37:48 -05:00
|
|
|
ref(res, 0) = (ikptr)
|
2006-11-23 19:48:14 -05:00
|
|
|
((n1 << bignum_length_shift) |
|
|
|
|
xsign |
|
|
|
|
bignum_tag);
|
2007-01-21 19:20:37 -05:00
|
|
|
return verify_bignum(res+vector_tag, "bnbn-");
|
2006-11-23 19:48:14 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
/* same sign */
|
|
|
|
if(xlimbs == ylimbs){
|
|
|
|
while((ref(x, -vector_tag+disp_bignum_data+(xlimbs-1)*wordsize) ==
|
|
|
|
ref(y, -vector_tag+disp_bignum_data+(xlimbs-1)*wordsize))){
|
|
|
|
xlimbs -= 1;
|
|
|
|
if(xlimbs == 0){ return 0; }
|
|
|
|
}
|
|
|
|
ylimbs = xlimbs;
|
|
|
|
}
|
2007-12-23 13:37:48 -05:00
|
|
|
ikptr s1=x, s2=y;
|
2008-01-01 04:24:36 -05:00
|
|
|
long int n1=xlimbs, n2=ylimbs;
|
|
|
|
long int result_sign = xsign;
|
2006-11-23 19:48:14 -05:00
|
|
|
/* |x| != |y| */
|
|
|
|
if(xlimbs <= ylimbs){
|
|
|
|
if(xlimbs == ylimbs){
|
|
|
|
if((ref(y, -vector_tag+disp_bignum_data+(xlimbs-1)*wordsize) >
|
|
|
|
ref(x, -vector_tag+disp_bignum_data+(xlimbs-1)*wordsize))){
|
|
|
|
s1 = y; n1 = ylimbs;
|
|
|
|
s2 = x; n2 = xlimbs;
|
2007-03-03 23:17:04 -05:00
|
|
|
result_sign = (1 << bignum_sign_shift) - ysign;
|
2006-11-23 19:48:14 -05:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
s1 = y; n1 = ylimbs;
|
|
|
|
s2 = x; n2 = xlimbs;
|
2007-03-03 23:17:04 -05:00
|
|
|
result_sign = (1 << bignum_sign_shift) - ysign;
|
2006-11-23 19:48:14 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
/* |s1| > |s2| */
|
2007-11-18 10:37:13 -05:00
|
|
|
pcb->root0 = &s1;
|
2008-10-04 01:59:34 -04:00
|
|
|
pcb->root1 = &s2;
|
2007-12-23 13:37:48 -05:00
|
|
|
ikptr res = ik_safe_alloc(pcb, align(disp_bignum_data + n1 * wordsize));
|
2008-10-29 13:54:06 -04:00
|
|
|
bzero((void*)(res+disp_bignum_data), n1*wordsize);
|
2007-11-18 10:37:13 -05:00
|
|
|
pcb->root0 = 0;
|
2008-10-04 01:59:34 -04:00
|
|
|
pcb->root1 = 0;
|
2008-01-01 04:24:36 -05:00
|
|
|
long int burrow =
|
2008-01-01 21:42:52 -05:00
|
|
|
mpn_sub((mp_limb_t*)(long)(res + disp_bignum_data),
|
|
|
|
(mp_limb_t*)(long)(s1 - vector_tag + disp_bignum_data),
|
2008-01-01 04:24:36 -05:00
|
|
|
n1,
|
2008-01-01 21:42:52 -05:00
|
|
|
(mp_limb_t*)(long)(s2 - vector_tag + disp_bignum_data),
|
2008-01-01 04:24:36 -05:00
|
|
|
n2);
|
2006-11-23 19:48:14 -05:00
|
|
|
if(burrow){
|
|
|
|
fprintf(stderr, "BUG: Burrow error in bnbn-\n");
|
|
|
|
exit(-1);
|
|
|
|
}
|
2008-01-01 04:24:36 -05:00
|
|
|
long int len = n1;
|
2006-11-23 19:48:14 -05:00
|
|
|
while(ref(res, disp_bignum_data + (len-1)*wordsize) == 0){
|
|
|
|
len--;
|
|
|
|
if(len == 0){
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if(result_sign == 0){
|
|
|
|
/* positive result */
|
|
|
|
if(len == 1){
|
2008-01-01 04:24:36 -05:00
|
|
|
unsigned long int fst_limb =
|
|
|
|
(unsigned long int) ref(res, disp_bignum_data);
|
2006-11-23 19:48:14 -05:00
|
|
|
if(fst_limb <= most_positive_fixnum){
|
2008-01-01 04:24:36 -05:00
|
|
|
return fix((long int)fst_limb);
|
2006-11-23 19:48:14 -05:00
|
|
|
}
|
|
|
|
}
|
2007-12-23 13:37:48 -05:00
|
|
|
ref(res, 0) = (ikptr)
|
2006-11-23 19:48:14 -05:00
|
|
|
((len << bignum_length_shift) |
|
|
|
|
result_sign |
|
|
|
|
bignum_tag);
|
2007-01-21 19:20:37 -05:00
|
|
|
return verify_bignum(res+vector_tag, "bnbn-");
|
2006-11-23 19:48:14 -05:00
|
|
|
} else {
|
|
|
|
/* negative result */
|
|
|
|
if(len == 1){
|
2008-01-01 04:24:36 -05:00
|
|
|
unsigned long int fst_limb =
|
|
|
|
(unsigned long int) ref(res, disp_bignum_data);
|
2006-11-23 19:48:14 -05:00
|
|
|
if(fst_limb <= most_negative_fixnum){
|
2008-01-01 04:24:36 -05:00
|
|
|
return fix(-(long int)fst_limb);
|
2006-11-23 19:48:14 -05:00
|
|
|
}
|
|
|
|
}
|
2007-12-23 13:37:48 -05:00
|
|
|
ref(res, 0) = (ikptr)
|
2006-11-23 19:48:14 -05:00
|
|
|
((len << bignum_length_shift) |
|
|
|
|
result_sign |
|
|
|
|
bignum_tag);
|
2007-01-21 19:20:37 -05:00
|
|
|
return verify_bignum(res+vector_tag, "bnbn-");
|
2006-11-23 19:48:14 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-12-23 13:37:48 -05:00
|
|
|
ikptr
|
|
|
|
ikrt_fxfxmult(ikptr x, ikptr y, ikpcb* pcb){
|
2008-01-01 04:24:36 -05:00
|
|
|
long int n1 = unfix(x);
|
|
|
|
long int n2 = unfix(y);
|
2006-11-23 19:48:14 -05:00
|
|
|
mp_limb_t lo = 0;
|
|
|
|
mp_limb_t s1 = n1;
|
|
|
|
mp_limb_t s2 = n2;
|
2008-01-01 04:24:36 -05:00
|
|
|
long int sign = 0;
|
2006-11-23 19:48:14 -05:00
|
|
|
if(n1 < 0){
|
|
|
|
s1 = -n1;
|
|
|
|
sign = 1 - sign;
|
|
|
|
}
|
|
|
|
if(n2 < 0){
|
|
|
|
s2 = -n2;
|
|
|
|
sign = 1 - sign;
|
|
|
|
}
|
|
|
|
mp_limb_t hi = mpn_mul_1(&lo, &s1, 1, s2);
|
|
|
|
if(hi == 0){
|
|
|
|
if(sign){
|
|
|
|
if(lo <= most_negative_fixnum){
|
2008-01-01 04:24:36 -05:00
|
|
|
return fix(-((long int)lo));
|
2006-11-23 19:48:14 -05:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if(lo <= most_positive_fixnum){
|
2008-01-01 04:24:36 -05:00
|
|
|
return fix((long int)lo);
|
2006-11-23 19:48:14 -05:00
|
|
|
}
|
|
|
|
}
|
2007-12-23 13:37:48 -05:00
|
|
|
ikptr r = ik_safe_alloc(pcb, disp_bignum_data + wordsize);
|
|
|
|
ref(r, 0) = (ikptr)
|
2006-11-23 19:48:14 -05:00
|
|
|
(bignum_tag |
|
|
|
|
(sign << bignum_sign_shift) |
|
|
|
|
(1 << bignum_length_shift));
|
2007-12-23 13:37:48 -05:00
|
|
|
ref(r, disp_bignum_data) = (ikptr)lo;
|
2007-01-21 19:20:37 -05:00
|
|
|
return BN(r+vector_tag);
|
2006-11-23 19:48:14 -05:00
|
|
|
} else {
|
2007-12-23 13:37:48 -05:00
|
|
|
ikptr r = ik_safe_alloc(pcb, align(disp_bignum_data + 2*wordsize));
|
|
|
|
ref(r, 0) = (ikptr)
|
2006-11-23 19:48:14 -05:00
|
|
|
(bignum_tag |
|
|
|
|
(sign << bignum_sign_shift) |
|
|
|
|
(2 << bignum_length_shift));
|
2007-12-23 13:37:48 -05:00
|
|
|
ref(r, disp_bignum_data) = (ikptr)lo;
|
|
|
|
ref(r, disp_bignum_data+wordsize) = (ikptr)hi;
|
2007-01-21 19:20:37 -05:00
|
|
|
return BN(r+vector_tag);
|
2006-11-23 19:48:14 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-12-23 13:37:48 -05:00
|
|
|
ikptr
|
2008-01-01 04:24:36 -05:00
|
|
|
normalize_bignum(long int limbs, int sign, ikptr r){
|
2006-11-23 19:48:14 -05:00
|
|
|
while(ref(r, disp_bignum_data + (limbs-1)*wordsize) == 0){
|
|
|
|
limbs--;
|
|
|
|
if(limbs == 0){ return 0;}
|
|
|
|
}
|
|
|
|
if(limbs == 1){
|
2008-01-01 04:24:36 -05:00
|
|
|
mp_limb_t last = (mp_limb_t) ref(r, disp_bignum_data);
|
2006-11-23 19:48:14 -05:00
|
|
|
if(sign == 0){
|
|
|
|
if(last <= most_positive_fixnum){
|
2008-01-01 04:24:36 -05:00
|
|
|
return fix(last);
|
2006-11-23 19:48:14 -05:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if(last <= most_negative_fixnum){
|
2008-01-01 04:24:36 -05:00
|
|
|
return fix(-(last));
|
2006-11-23 19:48:14 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2007-12-23 13:37:48 -05:00
|
|
|
ref(r, 0) = (ikptr) (bignum_tag | sign | (limbs << bignum_length_shift));
|
2007-01-21 19:20:37 -05:00
|
|
|
return BN(r+vector_tag);
|
2006-11-23 19:48:14 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-12-23 13:37:48 -05:00
|
|
|
ikptr
|
|
|
|
ikrt_fxbnmult(ikptr x, ikptr y, ikpcb* pcb){
|
2008-01-01 04:24:36 -05:00
|
|
|
long int n2 = unfix(x);
|
2006-11-23 19:48:14 -05:00
|
|
|
if(n2 == 0) { return 0; }
|
|
|
|
mp_limb_t s2 = (n2>0) ? n2 : (- n2);
|
2007-12-23 13:37:48 -05:00
|
|
|
ikptr fst = ref(y, -vector_tag);
|
2008-01-01 04:24:36 -05:00
|
|
|
long int limb_count = bnfst_limb_count(fst);
|
2007-11-18 10:37:13 -05:00
|
|
|
pcb->root0 = &y;
|
2007-12-23 13:37:48 -05:00
|
|
|
ikptr r = ik_safe_alloc(pcb, align(disp_bignum_data + (limb_count+1)*wordsize));
|
2007-11-18 10:37:13 -05:00
|
|
|
pcb->root0 = 0;
|
2008-01-01 21:42:52 -05:00
|
|
|
mp_limb_t hi = mpn_mul_1((mp_limb_t*)(long)(r+disp_bignum_data),
|
|
|
|
(mp_limb_t*)(long)(y-vector_tag+disp_bignum_data),
|
2006-11-23 19:48:14 -05:00
|
|
|
limb_count,
|
|
|
|
s2);
|
2007-12-23 13:37:48 -05:00
|
|
|
ref(r, disp_bignum_data + limb_count * wordsize) = (ikptr)hi;
|
2008-01-01 04:24:36 -05:00
|
|
|
long int sign =
|
2006-11-23 19:48:14 -05:00
|
|
|
((n2 > 0) ?
|
2008-01-01 04:24:36 -05:00
|
|
|
(bignum_sign_mask & (long int)fst) :
|
|
|
|
((1 << bignum_sign_shift) - (bignum_sign_mask&(long int)fst)));
|
2006-11-23 19:48:14 -05:00
|
|
|
return normalize_bignum(limb_count+1, sign, r);
|
|
|
|
}
|
|
|
|
|
2007-12-23 13:37:48 -05:00
|
|
|
ikptr
|
|
|
|
ikrt_bnbnmult(ikptr x, ikptr y, ikpcb* pcb){
|
2008-01-01 04:24:36 -05:00
|
|
|
long int f1 = (long int)ref(x, -vector_tag);
|
|
|
|
long int f2 = (long int)ref(y, -vector_tag);
|
|
|
|
long int n1 = bnfst_limb_count(f1);
|
|
|
|
long int n2 = bnfst_limb_count(f2);
|
|
|
|
long int nr = n1 + n2;
|
2007-11-18 10:37:13 -05:00
|
|
|
pcb->root0 = &x;
|
|
|
|
pcb->root1 = &y;
|
2007-12-23 13:37:48 -05:00
|
|
|
ikptr bn = ik_safe_alloc(pcb, align(disp_bignum_data + nr*wordsize));
|
2007-11-18 10:37:13 -05:00
|
|
|
pcb->root0 = 0;
|
|
|
|
pcb->root1 = 0;
|
2006-11-23 19:48:14 -05:00
|
|
|
mp_limb_t r;
|
|
|
|
if(n1 >= n2){
|
2008-01-01 21:42:52 -05:00
|
|
|
r = mpn_mul((mp_limb_t*)(long)(bn+disp_bignum_data),
|
|
|
|
(mp_limb_t*)(long)(x-vector_tag+disp_bignum_data),
|
2006-11-23 19:48:14 -05:00
|
|
|
n1,
|
2008-01-01 21:42:52 -05:00
|
|
|
(mp_limb_t*)(long)(y-vector_tag+disp_bignum_data),
|
2006-11-23 19:48:14 -05:00
|
|
|
n2);
|
|
|
|
} else {
|
2008-01-01 21:42:52 -05:00
|
|
|
r = mpn_mul((mp_limb_t*)(long)(bn+disp_bignum_data),
|
|
|
|
(mp_limb_t*)(long)(y-vector_tag+disp_bignum_data),
|
2006-11-23 19:48:14 -05:00
|
|
|
n2,
|
2008-01-01 21:42:52 -05:00
|
|
|
(mp_limb_t*)(long)(x-vector_tag+disp_bignum_data),
|
2006-11-23 19:48:14 -05:00
|
|
|
n1);
|
|
|
|
}
|
2008-01-01 04:24:36 -05:00
|
|
|
long int sign =
|
2006-11-23 19:48:14 -05:00
|
|
|
((bignum_sign_mask & f1) ?
|
|
|
|
((1 << bignum_sign_shift) - (bignum_sign_mask & f2)) :
|
|
|
|
(bignum_sign_mask & f2));
|
|
|
|
return normalize_bignum(nr, sign, bn);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2007-12-23 13:37:48 -05:00
|
|
|
ikptr
|
|
|
|
ikrt_bnbncomp(ikptr bn1, ikptr bn2){
|
|
|
|
ikptr f1 = ref(bn1, -vector_tag);
|
|
|
|
ikptr f2 = ref(bn2, -vector_tag);
|
2008-01-01 04:24:36 -05:00
|
|
|
if(bnfst_negative(f1)){
|
|
|
|
if(bnfst_negative(f2)){
|
2006-11-23 19:48:14 -05:00
|
|
|
/* both negative */
|
2008-01-01 04:24:36 -05:00
|
|
|
long int n1 = ((mp_limb_t) f1) >> bignum_length_shift;
|
|
|
|
long int n2 = ((mp_limb_t) f2) >> bignum_length_shift;
|
2006-11-23 19:48:14 -05:00
|
|
|
if(n1 < n2) {
|
|
|
|
return fix(1);
|
|
|
|
} else if(n1 > n2){
|
|
|
|
return fix(-1);
|
|
|
|
} else {
|
2008-01-01 04:24:36 -05:00
|
|
|
long int i;
|
2006-11-23 19:48:14 -05:00
|
|
|
for(i=(n1-1); i>=0; i--){
|
2008-01-01 04:24:36 -05:00
|
|
|
mp_limb_t t1 =
|
|
|
|
(mp_limb_t) ref(bn1,disp_bignum_data-vector_tag+i*wordsize);
|
|
|
|
mp_limb_t t2 =
|
|
|
|
(mp_limb_t) ref(bn2,disp_bignum_data-vector_tag+i*wordsize);
|
2006-11-23 19:48:14 -05:00
|
|
|
if(t1 < t2){
|
|
|
|
return fix(1);
|
|
|
|
} else if(t1 > t2){
|
|
|
|
return fix(-1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
} else {
|
|
|
|
/* n1 negative, n2 positive */
|
|
|
|
return fix(-1);
|
|
|
|
}
|
|
|
|
} else {
|
2008-01-01 04:24:36 -05:00
|
|
|
if(bnfst_negative(f2)){
|
2006-11-23 19:48:14 -05:00
|
|
|
/* n1 positive, n2 negative */
|
|
|
|
return fix(1);
|
|
|
|
} else {
|
|
|
|
/* both positive */
|
2008-01-01 04:24:36 -05:00
|
|
|
long int n1 = ((mp_limb_t) f1) >> bignum_length_shift;
|
|
|
|
long int n2 = ((mp_limb_t) f2) >> bignum_length_shift;
|
2006-11-23 19:48:14 -05:00
|
|
|
if(n1 < n2) {
|
|
|
|
return fix(-1);
|
|
|
|
} else if(n1 > n2){
|
|
|
|
return fix(1);
|
|
|
|
} else {
|
2008-01-01 04:24:36 -05:00
|
|
|
long int i;
|
2006-11-23 19:48:14 -05:00
|
|
|
for(i=(n1-1); i>=0; i--){
|
2008-01-01 04:24:36 -05:00
|
|
|
mp_limb_t t1 =
|
|
|
|
(mp_limb_t) ref(bn1,disp_bignum_data-vector_tag+i*wordsize);
|
|
|
|
mp_limb_t t2 =
|
|
|
|
(mp_limb_t) ref(bn2,disp_bignum_data-vector_tag+i*wordsize);
|
2006-11-23 19:48:14 -05:00
|
|
|
if(t1 < t2){
|
|
|
|
return fix(-1);
|
|
|
|
} else if(t1 > t2){
|
|
|
|
return fix(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static inline int
|
2008-01-01 04:24:36 -05:00
|
|
|
count_leading_ffs(int n, mp_limb_t* x){
|
2006-11-23 19:48:14 -05:00
|
|
|
int idx;
|
|
|
|
for(idx=0; idx<n; idx++){
|
2008-06-04 03:54:53 -04:00
|
|
|
if(x[idx] != (mp_limb_t)-1){
|
2006-11-23 19:48:14 -05:00
|
|
|
return idx;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
2008-05-18 05:27:55 -04:00
|
|
|
|
|
|
|
static void
|
|
|
|
copy_limbs(mp_limb_t* src, mp_limb_t* dst, int n1, int n2){
|
|
|
|
while(n1 < n2){
|
|
|
|
dst[n1] = src[n1];
|
|
|
|
n1++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-09-12 19:08:45 -04:00
|
|
|
static void
|
2008-07-18 04:35:13 -04:00
|
|
|
bits_compliment(mp_limb_t* src, mp_limb_t* dst, long int n){
|
2008-01-01 04:24:36 -05:00
|
|
|
mp_limb_t carry = 1;
|
2008-07-18 04:35:13 -04:00
|
|
|
long int i;
|
2007-09-12 19:08:45 -04:00
|
|
|
for(i=0; i<n; i++){
|
2008-01-01 04:24:36 -05:00
|
|
|
mp_limb_t d = src[i];
|
|
|
|
mp_limb_t c = carry + ~ d;
|
2007-09-12 19:08:45 -04:00
|
|
|
dst[i] = c;
|
|
|
|
carry = (carry && ! d);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-05-18 05:27:55 -04:00
|
|
|
static void
|
|
|
|
bits_compliment2(mp_limb_t* src, mp_limb_t* dst, int n1, int n2){
|
|
|
|
mp_limb_t carry = 1;
|
|
|
|
int i;
|
|
|
|
for(i=0; i<n1; i++){
|
|
|
|
mp_limb_t d = src[i];
|
|
|
|
mp_limb_t c = carry + ~ d;
|
|
|
|
dst[i] = c;
|
|
|
|
carry = (carry && ! d);
|
|
|
|
}
|
|
|
|
for(i=n1; i<n2; i++){
|
|
|
|
mp_limb_t d = 0;
|
|
|
|
mp_limb_t c = carry + ~ d;
|
|
|
|
dst[i] = c;
|
|
|
|
carry = (carry && ! d);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
bits_compliment_carry(mp_limb_t* src, mp_limb_t* dst, int n1, int n2, mp_limb_t carry){
|
|
|
|
int i;
|
|
|
|
for(i=n1; i<n2; i++){
|
|
|
|
mp_limb_t d = src[i];
|
|
|
|
mp_limb_t c = carry + ~ d;
|
|
|
|
dst[i] = c;
|
|
|
|
carry = (carry && ! d);
|
|
|
|
}
|
|
|
|
return carry;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2007-09-12 23:34:21 -04:00
|
|
|
static void
|
2008-07-18 04:35:13 -04:00
|
|
|
bits_compliment_with_carry(mp_limb_t* src, mp_limb_t* dst, long int n, long int carry){
|
|
|
|
long int i;
|
2007-09-12 23:34:21 -04:00
|
|
|
for(i=0; i<n; i++){
|
2008-01-01 04:24:36 -05:00
|
|
|
mp_limb_t d = src[i];
|
|
|
|
mp_limb_t c = carry + ~ d;
|
2007-09-12 23:34:21 -04:00
|
|
|
dst[i] = c;
|
|
|
|
carry = (carry && ! d);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-09-12 19:08:45 -04:00
|
|
|
static void
|
2008-01-01 04:24:36 -05:00
|
|
|
bits_compliment_logand(mp_limb_t* s1, mp_limb_t* s2, mp_limb_t* dst, int n){
|
2007-09-12 19:08:45 -04:00
|
|
|
int carry = 1;
|
|
|
|
int i;
|
|
|
|
for(i=0; i<n; i++){
|
2008-01-01 04:24:36 -05:00
|
|
|
mp_limb_t d = s1[i];
|
|
|
|
mp_limb_t c = carry + ~ d;
|
2007-09-12 19:08:45 -04:00
|
|
|
dst[i] = c & s2[i];
|
|
|
|
carry = (carry && ! d);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-05-18 05:27:55 -04:00
|
|
|
|
|
|
|
|
|
|
|
static int
|
|
|
|
bits_compliment_logor(mp_limb_t* s1, mp_limb_t* s2, mp_limb_t* dst, int n){
|
|
|
|
int carry = 1;
|
|
|
|
int i;
|
|
|
|
for(i=0; i<n; i++){
|
|
|
|
mp_limb_t d = s1[i];
|
|
|
|
mp_limb_t c = carry + ~ d;
|
|
|
|
dst[i] = c | s2[i];
|
|
|
|
carry = (carry && ! d);
|
|
|
|
}
|
|
|
|
return carry;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-07-18 04:35:13 -04:00
|
|
|
static long int
|
|
|
|
bits_carry(mp_limb_t* s, int n){
|
2007-09-12 23:34:21 -04:00
|
|
|
/*
|
|
|
|
int carry = 1;
|
|
|
|
int i;
|
|
|
|
for(i=0; i<n; i++){
|
2008-01-01 04:24:36 -05:00
|
|
|
mp_limb_t d = s[i];
|
2007-09-12 23:34:21 -04:00
|
|
|
carry = (carry && ! d);
|
|
|
|
}
|
|
|
|
return carry;
|
|
|
|
*/
|
|
|
|
int i;
|
|
|
|
for(i=0; i<n; i++){
|
|
|
|
if (s[i] != 0){
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2007-12-23 13:37:48 -05:00
|
|
|
ikptr
|
|
|
|
ikrt_bnlognot(ikptr x, ikpcb* pcb){
|
|
|
|
ikptr fst = ref(x, -vector_tag);
|
2008-01-01 04:24:36 -05:00
|
|
|
long int n = bnfst_limb_count(fst);
|
|
|
|
if(bnfst_negative(fst)){
|
2007-11-08 20:57:11 -05:00
|
|
|
/* negative */
|
2007-11-18 10:37:13 -05:00
|
|
|
pcb->root0 = &x;
|
2007-12-23 13:37:48 -05:00
|
|
|
ikptr r = ik_safe_alloc(pcb, align(disp_bignum_data + n*wordsize));
|
2007-11-18 10:37:13 -05:00
|
|
|
pcb->root0 = 0;
|
2008-01-01 21:42:52 -05:00
|
|
|
mp_limb_t* s1 = (mp_limb_t*)(long)(x+disp_bignum_data-vector_tag);
|
|
|
|
mp_limb_t* rd = (mp_limb_t*)(long)(r+disp_bignum_data);
|
2007-11-08 20:57:11 -05:00
|
|
|
int i;
|
|
|
|
for(i=0; (i<n) && (s1[i] == 0); i++) {
|
|
|
|
rd[i] = -1;
|
|
|
|
}
|
|
|
|
rd[i] = s1[i] - 1;
|
|
|
|
for(i++; i<n; i++){
|
|
|
|
rd[i] = s1[i];
|
|
|
|
}
|
|
|
|
return normalize_bignum(n, 0, r);
|
|
|
|
} else {
|
|
|
|
/* positive */
|
2008-01-01 04:24:36 -05:00
|
|
|
long int i;
|
2008-01-01 21:42:52 -05:00
|
|
|
mp_limb_t* s1 = (mp_limb_t*)(long)(x+disp_bignum_data-vector_tag);
|
2008-06-04 03:54:53 -04:00
|
|
|
for(i=0; (i<n) && (s1[i] == (mp_limb_t)-1); i++) {/*nothing*/}
|
2007-11-08 20:57:11 -05:00
|
|
|
if(i==n){
|
2007-11-18 10:37:13 -05:00
|
|
|
pcb->root0 = &x;
|
2007-12-23 13:37:48 -05:00
|
|
|
ikptr r = ik_safe_alloc(pcb, align(disp_bignum_data + (n+1)*wordsize));
|
2007-11-18 10:37:13 -05:00
|
|
|
pcb->root0 = 0;
|
2008-01-01 21:42:52 -05:00
|
|
|
bzero((char*)(long)r+disp_bignum_data, n*wordsize);
|
|
|
|
((mp_limb_t*)(long)(r+disp_bignum_data))[n] = 1;
|
2007-12-23 13:37:48 -05:00
|
|
|
ref(r, 0) = (ikptr)
|
2007-11-08 20:57:11 -05:00
|
|
|
(bignum_tag | (1<<bignum_sign_shift) | ((n+1) << bignum_length_shift));
|
|
|
|
return r+vector_tag;
|
|
|
|
} else {
|
2007-11-18 10:37:13 -05:00
|
|
|
pcb->root0 = &x;
|
2007-12-23 13:37:48 -05:00
|
|
|
ikptr r = ik_safe_alloc(pcb, align(disp_bignum_data + n*wordsize));
|
2007-11-18 10:37:13 -05:00
|
|
|
pcb->root0 = 0;
|
2008-01-01 21:42:52 -05:00
|
|
|
mp_limb_t* s1 = (mp_limb_t*)(long)(x+disp_bignum_data-vector_tag);
|
|
|
|
mp_limb_t* rd = (mp_limb_t*)(long)(r+disp_bignum_data);
|
2007-11-08 20:57:11 -05:00
|
|
|
int j;
|
|
|
|
for(j=0; j<i; j++){ rd[j] = 0; }
|
|
|
|
rd[i] = s1[i] + 1;
|
|
|
|
for(j=i+1; j<n; j++){ rd[j] = s1[j]; }
|
2007-12-23 13:37:48 -05:00
|
|
|
ref(r, 0) = (ikptr)
|
2007-11-08 20:57:11 -05:00
|
|
|
(bignum_tag | (1<<bignum_sign_shift) | (n << bignum_length_shift));
|
|
|
|
return r+vector_tag;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2007-09-12 23:34:21 -04:00
|
|
|
|
2007-09-12 19:08:45 -04:00
|
|
|
|
2008-05-18 05:27:55 -04:00
|
|
|
ikptr
|
|
|
|
ikrt_fxbnlogand(ikptr x, ikptr y, ikpcb* pcb){
|
|
|
|
long int n1 = unfix(x);
|
|
|
|
ikptr fst = ref(y, -vector_tag);
|
|
|
|
if(n1 >= 0){
|
|
|
|
/* x is positive */
|
|
|
|
if(bnfst_negative(fst)){
|
|
|
|
/* y is negative */
|
|
|
|
return fix(n1 & (1+~(long int)ref(y, disp_vector_data-vector_tag)));
|
|
|
|
} else {
|
|
|
|
/* y is positive */
|
|
|
|
return fix(n1 & (long int)ref(y, disp_vector_data-vector_tag));
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
/* x is negative */
|
|
|
|
if(n1 == -1){ return y; }
|
|
|
|
if(bnfst_negative(fst)){
|
|
|
|
/* y is negative */
|
|
|
|
long int len = bnfst_limb_count(fst);
|
|
|
|
pcb->root0 = &y;
|
|
|
|
ikptr r = ik_safe_alloc(pcb, align(disp_bignum_data + (len+1)*wordsize));
|
|
|
|
pcb->root0 = 0;
|
|
|
|
mp_limb_t* s2 = (mp_limb_t*)(long)(y+disp_bignum_data-vector_tag);
|
|
|
|
mp_limb_t* s = (mp_limb_t*)(long)(r+disp_bignum_data);
|
|
|
|
bits_compliment2(s2, s, len, len+1);
|
|
|
|
s[0] = s[0] & n1;
|
|
|
|
bits_compliment2(s, s, len+1, len+1);
|
|
|
|
return normalize_bignum(len+1, 1<<bignum_sign_shift, r);
|
|
|
|
} else {
|
|
|
|
/* y is positive */
|
|
|
|
long int len = bnfst_limb_count(fst);
|
|
|
|
pcb->root0 = &y;
|
|
|
|
ikptr r = ik_safe_alloc(pcb, align(disp_bignum_data + len * wordsize));
|
|
|
|
pcb->root0 = 0;
|
|
|
|
ref(r, 0) = fst;
|
|
|
|
ref(r, disp_bignum_data) = (ikptr)
|
|
|
|
(((long int)ref(y, disp_bignum_data - vector_tag)) & n1);
|
|
|
|
int i;
|
|
|
|
for(i=1; i<len; i++){
|
|
|
|
ref(r, disp_bignum_data+i*wordsize) =
|
|
|
|
ref(y, disp_bignum_data-vector_tag+i*wordsize);
|
|
|
|
}
|
|
|
|
return BN(r+vector_tag);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-12-23 13:37:48 -05:00
|
|
|
ikptr
|
|
|
|
ikrt_bnbnlogand(ikptr x, ikptr y, ikpcb* pcb){
|
|
|
|
ikptr xfst = ref(x, -vector_tag);
|
|
|
|
ikptr yfst = ref(y, -vector_tag);
|
2008-01-01 04:24:36 -05:00
|
|
|
long int n1 = bnfst_limb_count(xfst);
|
|
|
|
long int n2 = bnfst_limb_count(yfst);
|
|
|
|
if(bnfst_negative(xfst)){
|
|
|
|
if(bnfst_negative(yfst)){
|
2007-09-12 19:08:45 -04:00
|
|
|
if(n1 >= n2){
|
2007-11-18 10:37:13 -05:00
|
|
|
pcb->root0 = &x;
|
|
|
|
pcb->root1 = &y;
|
2008-05-18 05:27:55 -04:00
|
|
|
ikptr r = ik_safe_alloc(pcb, align(disp_bignum_data + (n1+1)*wordsize));
|
2007-11-18 10:37:13 -05:00
|
|
|
pcb->root0 = 0;
|
|
|
|
pcb->root1 = 0;
|
2008-01-01 21:42:52 -05:00
|
|
|
mp_limb_t* s1 = (mp_limb_t*)(long)(x+disp_bignum_data-vector_tag);
|
|
|
|
mp_limb_t* s2 = (mp_limb_t*)(long)(y+disp_bignum_data-vector_tag);
|
|
|
|
mp_limb_t* s = (mp_limb_t*)(long)(r+disp_bignum_data);
|
2008-05-18 05:27:55 -04:00
|
|
|
bits_compliment2(s1, s, n1, n1+1);
|
2007-09-12 19:08:45 -04:00
|
|
|
bits_compliment_logand(s2, s, s, n2);
|
2008-05-18 05:27:55 -04:00
|
|
|
bits_compliment2(s, s, n1+1, n1+1);
|
|
|
|
return normalize_bignum(n1+1, 1<<bignum_sign_shift, r);
|
2007-09-12 19:08:45 -04:00
|
|
|
} else {
|
|
|
|
return ikrt_bnbnlogand(y,x,pcb);
|
|
|
|
}
|
2006-11-23 19:48:14 -05:00
|
|
|
} else {
|
|
|
|
return ikrt_bnbnlogand(y,x,pcb);
|
|
|
|
}
|
|
|
|
} else {
|
2008-01-01 04:24:36 -05:00
|
|
|
if(bnfst_negative(yfst)){
|
2006-11-23 19:48:14 -05:00
|
|
|
/* x positive, y negative */
|
|
|
|
/* the result is at most n1 words long */
|
2007-11-18 10:37:13 -05:00
|
|
|
pcb->root0 = &x;
|
|
|
|
pcb->root1 = &y;
|
2007-12-23 13:37:48 -05:00
|
|
|
ikptr r = ik_safe_alloc(pcb, align(disp_bignum_data + n1*wordsize));
|
2007-11-18 10:37:13 -05:00
|
|
|
pcb->root0 = 0;
|
|
|
|
pcb->root1 = 0;
|
2008-01-01 21:42:52 -05:00
|
|
|
mp_limb_t* s1 = (mp_limb_t*)(long)(x+disp_bignum_data-vector_tag);
|
|
|
|
mp_limb_t* s2 = (mp_limb_t*)(long)(y+disp_bignum_data-vector_tag);
|
|
|
|
mp_limb_t* s = (mp_limb_t*)(long)(r+disp_bignum_data);
|
2008-05-18 05:27:55 -04:00
|
|
|
if(n1 <= n2){
|
|
|
|
bits_compliment_logand(s2, s1, s, n1);
|
|
|
|
} else {
|
|
|
|
bits_compliment_logand(s2, s1, s, n2);
|
|
|
|
copy_limbs(s1, s, n2, n1);
|
|
|
|
}
|
2007-09-12 19:08:45 -04:00
|
|
|
return normalize_bignum(n1, 0, r);
|
2006-11-23 19:48:14 -05:00
|
|
|
} else {
|
|
|
|
/* both positive */
|
|
|
|
int n = (n1<n2)?n1:n2;
|
2008-01-01 04:24:36 -05:00
|
|
|
long int i;
|
2006-11-23 19:48:14 -05:00
|
|
|
for(i=n-1; i>=0; i--){
|
2008-01-01 04:24:36 -05:00
|
|
|
long int l1 =
|
|
|
|
(long int) ref(x, disp_bignum_data-vector_tag+i*wordsize);
|
|
|
|
long int l2 =
|
|
|
|
(long int) ref(y, disp_bignum_data-vector_tag+i*wordsize);
|
2008-05-18 05:27:55 -04:00
|
|
|
unsigned long int last = l1 & l2;
|
2006-11-23 19:48:14 -05:00
|
|
|
if(last){
|
|
|
|
if((i == 0) && (last < most_positive_fixnum)){
|
|
|
|
return fix(last);
|
|
|
|
}
|
2007-11-18 10:37:13 -05:00
|
|
|
pcb->root0 = &x;
|
|
|
|
pcb->root1 = &y;
|
2007-12-23 13:37:48 -05:00
|
|
|
ikptr r = ik_safe_alloc(pcb, align(disp_bignum_data+(i+1)*wordsize));
|
2007-11-18 10:37:13 -05:00
|
|
|
pcb->root0 = 0;
|
|
|
|
pcb->root1 = 0;
|
2007-12-23 13:37:48 -05:00
|
|
|
ref(r, 0) = (ikptr) (bignum_tag | ((i+1)<<bignum_length_shift));
|
|
|
|
ref(r, disp_bignum_data + i*wordsize) = (ikptr)last;
|
2006-11-23 19:48:14 -05:00
|
|
|
int j;
|
|
|
|
for(j=0; j<i; j++){
|
2007-12-23 13:37:48 -05:00
|
|
|
ref(r, disp_bignum_data + j*wordsize) = (ikptr)
|
2008-01-01 04:24:36 -05:00
|
|
|
(((long int)ref(x, disp_bignum_data-vector_tag+j*wordsize))
|
2006-11-23 19:48:14 -05:00
|
|
|
&
|
2008-01-01 04:24:36 -05:00
|
|
|
((long int)ref(y, disp_bignum_data-vector_tag+j*wordsize)));
|
2006-11-23 19:48:14 -05:00
|
|
|
}
|
|
|
|
return r+vector_tag;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-09-12 23:34:21 -04:00
|
|
|
|
2008-05-18 05:27:55 -04:00
|
|
|
ikptr
|
|
|
|
ikrt_fxbnlogor(ikptr x, ikptr y, ikpcb* pcb){
|
|
|
|
long int n1 = unfix(x);
|
|
|
|
ikptr fst = ref(y, -vector_tag);
|
|
|
|
if(n1 < 0){
|
|
|
|
/* x is negative */
|
|
|
|
if(bnfst_negative(fst)){
|
|
|
|
/* y is negative */
|
|
|
|
return fix(n1 | (1+~(long int)ref(y, disp_vector_data-vector_tag)));
|
|
|
|
} else {
|
|
|
|
/* y is positive */
|
|
|
|
return fix(n1 | (long int)ref(y, disp_vector_data-vector_tag));
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
/* x is non negative */
|
|
|
|
if(n1 == 0){ return y; }
|
|
|
|
/* x is positive */
|
|
|
|
if(bnfst_negative(fst)){
|
|
|
|
/* y is negative */
|
|
|
|
long int len = bnfst_limb_count(fst);
|
|
|
|
pcb->root0 = &y;
|
|
|
|
ikptr r = ik_safe_alloc(pcb, align(disp_bignum_data + (len+1)*wordsize));
|
|
|
|
pcb->root0 = 0;
|
|
|
|
mp_limb_t* s2 = (mp_limb_t*)(long)(y+disp_bignum_data-vector_tag);
|
|
|
|
mp_limb_t* s = (mp_limb_t*)(long)(r+disp_bignum_data);
|
|
|
|
bits_compliment2(s2, s, len, len+1);
|
|
|
|
s[0] = s[0] | n1;
|
|
|
|
bits_compliment2(s, s, len+1, len+1);
|
|
|
|
return normalize_bignum(len+1, 1<<bignum_sign_shift, r);
|
|
|
|
} else {
|
|
|
|
/* y is positive */
|
|
|
|
long int len = bnfst_limb_count(fst);
|
|
|
|
pcb->root0 = &y;
|
|
|
|
ikptr r = ik_safe_alloc(pcb, align(disp_bignum_data + len * wordsize));
|
|
|
|
pcb->root0 = 0;
|
|
|
|
ref(r, 0) = fst;
|
|
|
|
ref(r, disp_bignum_data) = (ikptr)
|
|
|
|
(((long int)ref(y, disp_bignum_data - vector_tag)) | n1);
|
|
|
|
int i;
|
|
|
|
for(i=1; i<len; i++){
|
|
|
|
ref(r, disp_bignum_data+i*wordsize) =
|
|
|
|
ref(y, disp_bignum_data-vector_tag+i*wordsize);
|
|
|
|
}
|
|
|
|
return BN(r+vector_tag);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ikptr
|
|
|
|
ikrt_bnbnlogor(ikptr x, ikptr y, ikpcb* pcb){
|
|
|
|
ikptr xfst = ref(x, -vector_tag);
|
|
|
|
ikptr yfst = ref(y, -vector_tag);
|
|
|
|
long int n1 = bnfst_limb_count(xfst);
|
|
|
|
long int n2 = bnfst_limb_count(yfst);
|
|
|
|
if(bnfst_negative(xfst)){
|
|
|
|
if(bnfst_negative(yfst)){
|
|
|
|
if(n1 >= n2){
|
|
|
|
pcb->root0 = &x;
|
|
|
|
pcb->root1 = &y;
|
|
|
|
ikptr r = ik_safe_alloc(pcb, align(disp_bignum_data + n1*wordsize));
|
|
|
|
pcb->root0 = 0;
|
|
|
|
pcb->root1 = 0;
|
|
|
|
mp_limb_t* s1 = (mp_limb_t*)(long)(x+disp_bignum_data-vector_tag);
|
|
|
|
mp_limb_t* s2 = (mp_limb_t*)(long)(y+disp_bignum_data-vector_tag);
|
|
|
|
mp_limb_t* s = (mp_limb_t*)(long)(r+disp_bignum_data);
|
|
|
|
bits_compliment2(s2, s, n2, n1);
|
|
|
|
int carry = bits_compliment_logor(s1, s, s, n1);
|
|
|
|
bits_compliment_carry(s,s,n1,n1,carry);
|
|
|
|
bits_compliment2(s, s, n1, n1);
|
|
|
|
return normalize_bignum(n1, 1<<bignum_sign_shift, r);
|
|
|
|
} else {
|
|
|
|
return ikrt_bnbnlogor(y,x,pcb);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return ikrt_bnbnlogor(y,x,pcb);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if(bnfst_negative(yfst)){
|
|
|
|
/* x positive, y negative */
|
|
|
|
/* the result is at most n2 words long */
|
|
|
|
pcb->root0 = &x;
|
|
|
|
pcb->root1 = &y;
|
|
|
|
ikptr r = ik_safe_alloc(pcb, align(disp_bignum_data + n2*wordsize));
|
|
|
|
pcb->root0 = 0;
|
|
|
|
pcb->root1 = 0;
|
|
|
|
mp_limb_t* s1 = (mp_limb_t*)(long)(x+disp_bignum_data-vector_tag);
|
|
|
|
mp_limb_t* s2 = (mp_limb_t*)(long)(y+disp_bignum_data-vector_tag);
|
|
|
|
mp_limb_t* s = (mp_limb_t*)(long)(r+disp_bignum_data);
|
|
|
|
if(n2 <= n1){
|
|
|
|
bits_compliment_logor(s2, s1, s, n2);
|
|
|
|
bits_compliment2(s, s, n2, n2);
|
|
|
|
} else {
|
|
|
|
int carry = bits_compliment_logor(s2, s1, s, n1);
|
|
|
|
bits_compliment_carry(s2, s, n1, n2, carry);
|
|
|
|
bits_compliment_carry(s, s, 0, n2, 1);
|
|
|
|
}
|
|
|
|
return normalize_bignum(n2, 1<<bignum_sign_shift, r);
|
|
|
|
} else {
|
|
|
|
/* both positive */
|
|
|
|
int n = (n1>n2)?n1:n2;
|
|
|
|
pcb->root0 = &x;
|
|
|
|
pcb->root1 = &y;
|
|
|
|
ikptr r = ik_safe_alloc(pcb, align(disp_bignum_data+n*wordsize));
|
|
|
|
mp_limb_t* s = (mp_limb_t*)(long)(r+disp_bignum_data);
|
|
|
|
mp_limb_t* s1 = (mp_limb_t*)(long)(x+disp_bignum_data-vector_tag);
|
|
|
|
mp_limb_t* s2 = (mp_limb_t*)(long)(y+disp_bignum_data-vector_tag);
|
|
|
|
pcb->root0 = 0;
|
|
|
|
pcb->root1 = 0;
|
|
|
|
long int i;
|
|
|
|
if(n == n1){
|
|
|
|
for(i=0; i<n2; i++){
|
|
|
|
s[i] = s1[i] | s2[i];
|
|
|
|
}
|
|
|
|
for(i=n2; i<n1; i++){
|
|
|
|
s[i] = s1[i];
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
for(i=0; i<n1; i++){
|
|
|
|
s[i] = s1[i] | s2[i];
|
|
|
|
}
|
|
|
|
for(i=n1; i<n2; i++){
|
|
|
|
s[i] = s2[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return normalize_bignum(n, 0, r);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-09-12 23:34:21 -04:00
|
|
|
static void
|
2008-01-01 04:24:36 -05:00
|
|
|
copy_bits_shifting_right(mp_limb_t* src, mp_limb_t* dst, int n, int m){
|
|
|
|
mp_limb_t carry = src[0] >> m;
|
2007-09-12 23:34:21 -04:00
|
|
|
int i;
|
|
|
|
for(i=1; i<n; i++){
|
2008-01-01 04:24:36 -05:00
|
|
|
mp_limb_t b = src[i];
|
2008-07-18 04:35:13 -04:00
|
|
|
dst[i-1] = (b << (mp_bits_per_limb-m)) | carry;
|
2007-09-12 23:34:21 -04:00
|
|
|
carry = b >> m;
|
|
|
|
}
|
|
|
|
dst[n-1] = carry;
|
|
|
|
}
|
|
|
|
|
2007-09-13 00:47:41 -04:00
|
|
|
static void
|
2008-07-30 11:17:20 -04:00
|
|
|
copy_bits_shifting_left(mp_limb_t* src, mp_limb_t* dst, int n, int m){
|
|
|
|
mp_limb_t carry = 0;
|
|
|
|
int i;
|
2007-09-13 00:47:41 -04:00
|
|
|
for(i=0; i<n; i++){
|
2008-07-30 11:17:20 -04:00
|
|
|
mp_limb_t b = src[i];
|
2007-09-13 00:47:41 -04:00
|
|
|
dst[i] = (b << m) | carry;
|
2008-07-18 04:35:13 -04:00
|
|
|
carry = b >> (mp_bits_per_limb-m);
|
2007-09-13 00:47:41 -04:00
|
|
|
}
|
|
|
|
dst[n] = carry;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-09-12 23:34:21 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
2007-12-23 13:37:48 -05:00
|
|
|
ikptr
|
|
|
|
ikrt_bignum_shift_right(ikptr x, ikptr y, ikpcb* pcb){
|
2008-07-18 04:35:13 -04:00
|
|
|
int limb_shift = (wordsize == 4 ? 5 : 6);
|
2008-01-01 04:24:36 -05:00
|
|
|
long int m = unfix(y);
|
2007-12-23 13:37:48 -05:00
|
|
|
ikptr fst = ref(x, -vector_tag);
|
2008-01-01 04:24:36 -05:00
|
|
|
long int n = bnfst_limb_count(fst);
|
2008-07-18 04:35:13 -04:00
|
|
|
long int whole_limb_shift = m >> limb_shift;
|
|
|
|
long int bit_shift = m & (mp_bits_per_limb-1);
|
2008-01-01 04:24:36 -05:00
|
|
|
long int new_limb_count = n - whole_limb_shift;
|
|
|
|
if(bnfst_negative(fst)){
|
2007-09-12 23:34:21 -04:00
|
|
|
if(new_limb_count <= 0){
|
|
|
|
return fix(-1);
|
|
|
|
}
|
|
|
|
if(bit_shift == 0){
|
2007-11-18 10:37:13 -05:00
|
|
|
pcb->root0 = &x;
|
2007-12-23 13:37:48 -05:00
|
|
|
ikptr r = ik_safe_alloc(pcb, align(disp_bignum_data + new_limb_count * wordsize));
|
2007-11-18 10:37:13 -05:00
|
|
|
pcb->root0 = 0;
|
2007-09-12 23:34:21 -04:00
|
|
|
bits_compliment_with_carry(
|
2008-01-01 21:42:52 -05:00
|
|
|
(mp_limb_t*)(long)(x+off_bignum_data+whole_limb_shift*wordsize),
|
|
|
|
(mp_limb_t*)(long)(r+disp_bignum_data),
|
2007-09-12 23:34:21 -04:00
|
|
|
new_limb_count,
|
2008-01-01 21:42:52 -05:00
|
|
|
bits_carry((mp_limb_t*)(long)(x+off_bignum_data), whole_limb_shift));
|
2007-09-12 23:34:21 -04:00
|
|
|
bits_compliment(
|
2008-01-01 21:42:52 -05:00
|
|
|
(mp_limb_t*)(long)(r+disp_bignum_data),
|
|
|
|
(mp_limb_t*)(long)(r+disp_bignum_data),
|
2007-09-12 23:34:21 -04:00
|
|
|
new_limb_count);
|
|
|
|
return normalize_bignum(new_limb_count, 1 << bignum_sign_shift, r);
|
|
|
|
} else {
|
2007-11-18 10:37:13 -05:00
|
|
|
pcb->root0 = &x;
|
2007-12-23 13:37:48 -05:00
|
|
|
ikptr r = ik_safe_alloc(pcb, align(disp_bignum_data + new_limb_count * wordsize));
|
2007-11-18 10:37:13 -05:00
|
|
|
pcb->root0 = 0;
|
2007-09-12 23:34:21 -04:00
|
|
|
bits_compliment_with_carry(
|
2008-01-01 21:42:52 -05:00
|
|
|
(mp_limb_t*)(long)(x+off_bignum_data+whole_limb_shift*wordsize),
|
|
|
|
(mp_limb_t*)(long)(r+disp_bignum_data),
|
2007-09-12 23:34:21 -04:00
|
|
|
new_limb_count,
|
2008-01-01 21:42:52 -05:00
|
|
|
bits_carry((mp_limb_t*)(long)(x+off_bignum_data), whole_limb_shift));
|
2007-09-12 23:34:21 -04:00
|
|
|
copy_bits_shifting_right(
|
2008-01-01 21:42:52 -05:00
|
|
|
(mp_limb_t*)(long)(r+disp_bignum_data),
|
|
|
|
(mp_limb_t*)(long)(r+disp_bignum_data),
|
2007-09-12 23:34:21 -04:00
|
|
|
new_limb_count,
|
|
|
|
bit_shift);
|
2008-01-01 04:24:36 -05:00
|
|
|
*((mp_limb_t*)(r+disp_bignum_data+(new_limb_count-1)*wordsize))
|
2008-07-18 04:35:13 -04:00
|
|
|
|= (-1L << (mp_bits_per_limb - bit_shift));
|
2007-09-12 23:34:21 -04:00
|
|
|
bits_compliment(
|
2008-01-01 21:42:52 -05:00
|
|
|
(mp_limb_t*)(long)(r+disp_bignum_data),
|
|
|
|
(mp_limb_t*)(long)(r+disp_bignum_data),
|
2007-09-12 23:34:21 -04:00
|
|
|
new_limb_count);
|
|
|
|
return normalize_bignum(new_limb_count, 1 << bignum_sign_shift, r);
|
|
|
|
fprintf(stderr, "not yet for negative bignum_shift\n");
|
|
|
|
exit(-1);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if(new_limb_count <= 0){
|
|
|
|
return 0;
|
|
|
|
}
|
2007-11-18 10:37:13 -05:00
|
|
|
pcb->root0 = &x;
|
2007-12-23 13:37:48 -05:00
|
|
|
ikptr r = ik_safe_alloc(pcb, align(disp_bignum_data + new_limb_count * wordsize));
|
2007-11-18 10:37:13 -05:00
|
|
|
pcb->root0 = 0;
|
2007-09-12 23:34:21 -04:00
|
|
|
if(bit_shift == 0){
|
2008-01-01 21:42:52 -05:00
|
|
|
memcpy((char*)(long)r+disp_bignum_data,
|
|
|
|
(char*)(long)x+off_bignum_data+whole_limb_shift*wordsize,
|
2008-01-01 21:08:07 -05:00
|
|
|
new_limb_count * wordsize);
|
2007-09-12 23:34:21 -04:00
|
|
|
return normalize_bignum(new_limb_count, 0, r);
|
|
|
|
} else {
|
|
|
|
copy_bits_shifting_right(
|
2008-01-01 21:42:52 -05:00
|
|
|
(mp_limb_t*)(long)(x+off_bignum_data+whole_limb_shift*wordsize),
|
|
|
|
(mp_limb_t*)(long)(r+disp_bignum_data),
|
2007-09-12 23:34:21 -04:00
|
|
|
new_limb_count,
|
|
|
|
bit_shift);
|
|
|
|
return normalize_bignum(new_limb_count, 0, r);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-12-23 13:37:48 -05:00
|
|
|
ikptr
|
|
|
|
ikrt_fixnum_shift_left(ikptr x, ikptr y, ikpcb* pcb){
|
2008-07-18 04:35:13 -04:00
|
|
|
int limb_shift = (wordsize == 4 ? 5 : 6);
|
|
|
|
long int m = unfix(y);
|
|
|
|
long int n = unfix(x);
|
|
|
|
long int limb_count = (m >> limb_shift) + 2;
|
|
|
|
long int bit_shift = m & (mp_bits_per_limb-1);
|
2007-12-23 13:37:48 -05:00
|
|
|
ikptr r = ik_safe_alloc(pcb, align(disp_bignum_data + limb_count * wordsize));
|
2008-07-18 04:35:13 -04:00
|
|
|
unsigned long int* s = (unsigned long int*)(long)(r+disp_bignum_data);
|
2007-09-13 00:08:41 -04:00
|
|
|
bzero(s, limb_count * wordsize);
|
|
|
|
if(n >= 0){
|
|
|
|
if(bit_shift){
|
2008-07-18 04:35:13 -04:00
|
|
|
s[limb_count-1] = n >> (mp_bits_per_limb - bit_shift);
|
2007-09-13 00:08:41 -04:00
|
|
|
}
|
|
|
|
s[limb_count-2] = n << bit_shift;
|
|
|
|
} else {
|
|
|
|
if(bit_shift){
|
2008-07-18 04:35:13 -04:00
|
|
|
s[limb_count-1] = (-n) >> (mp_bits_per_limb - bit_shift);
|
2007-09-13 00:08:41 -04:00
|
|
|
}
|
|
|
|
s[limb_count-2] = (-n) << bit_shift;
|
|
|
|
}
|
|
|
|
return normalize_bignum(limb_count, (n>=0)?(0):(1<<bignum_sign_shift), r);
|
2007-09-12 23:34:21 -04:00
|
|
|
}
|
|
|
|
|
2007-09-13 00:08:41 -04:00
|
|
|
|
2007-12-23 13:37:48 -05:00
|
|
|
ikptr
|
|
|
|
ikrt_bignum_shift_left(ikptr x, ikptr y, ikpcb* pcb){
|
2008-07-18 04:35:13 -04:00
|
|
|
int limb_shift = (wordsize == 4 ? 5 : 6);
|
2008-01-01 04:24:36 -05:00
|
|
|
long int m = unfix(y);
|
2007-12-23 13:37:48 -05:00
|
|
|
ikptr fst = ref(x, -vector_tag);
|
2008-01-01 04:24:36 -05:00
|
|
|
long int n = bnfst_limb_count(fst);
|
2008-07-18 04:35:13 -04:00
|
|
|
long int whole_limb_shift = m >> limb_shift;
|
|
|
|
long int bit_shift = m & (mp_bits_per_limb-1);
|
2007-09-13 00:47:41 -04:00
|
|
|
if(bit_shift == 0){
|
2008-01-01 04:24:36 -05:00
|
|
|
long int limb_count = n + whole_limb_shift;
|
2007-11-18 10:37:13 -05:00
|
|
|
pcb->root0 = &x;
|
2007-12-23 13:37:48 -05:00
|
|
|
ikptr r = ik_safe_alloc(pcb, align(disp_bignum_data + limb_count * wordsize));
|
2007-11-18 10:37:13 -05:00
|
|
|
pcb->root0 = 0;
|
2008-01-01 21:42:52 -05:00
|
|
|
unsigned int* s = (unsigned int*)(long)(r+disp_bignum_data);
|
2007-09-13 00:47:41 -04:00
|
|
|
bzero(s, whole_limb_shift*wordsize);
|
2008-01-01 21:42:52 -05:00
|
|
|
memcpy((char*)(long)s+whole_limb_shift,
|
|
|
|
(char*)(long)x+off_bignum_data, n*wordsize);
|
2008-01-01 04:24:36 -05:00
|
|
|
return normalize_bignum(limb_count, bnfst_negative(fst), r);
|
2007-09-13 00:47:41 -04:00
|
|
|
} else {
|
|
|
|
int limb_count = n + whole_limb_shift + 1;
|
2007-11-18 10:37:13 -05:00
|
|
|
pcb->root0 = &x;
|
2007-12-23 13:37:48 -05:00
|
|
|
ikptr r = ik_safe_alloc(pcb, align(disp_bignum_data + limb_count * wordsize));
|
2007-11-18 10:37:13 -05:00
|
|
|
pcb->root0 = 0;
|
2008-07-30 11:17:20 -04:00
|
|
|
mp_limb_t* s = (mp_limb_t*)(r+disp_bignum_data);
|
2007-09-13 00:47:41 -04:00
|
|
|
bzero(s, whole_limb_shift*wordsize);
|
|
|
|
copy_bits_shifting_left(
|
2008-07-30 11:17:20 -04:00
|
|
|
(mp_limb_t*)(x+off_bignum_data),
|
|
|
|
(mp_limb_t*)(s+whole_limb_shift),
|
2007-09-13 00:47:41 -04:00
|
|
|
n,
|
|
|
|
bit_shift);
|
2008-01-01 04:24:36 -05:00
|
|
|
return normalize_bignum(limb_count, bnfst_negative(fst), r);
|
2007-09-13 00:47:41 -04:00
|
|
|
}
|
2007-09-12 23:34:21 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-12-06 20:53:54 -05:00
|
|
|
#if 0
|
|
|
|
From TFM:
|
|
|
|
void
|
|
|
|
mpn_tdiv_qr (
|
|
|
|
mp limb t *qp, /* quotient placed here */
|
|
|
|
mp limb t *rp, /* remainder placed here */
|
|
|
|
mp size t qxn, /* must be zero! */
|
|
|
|
const mp limb t *np, /* first number */
|
|
|
|
mp size t nn, /* its length */
|
|
|
|
const mp limb t *dp, /* second number */
|
|
|
|
mp size t dn /* its length */
|
|
|
|
)
|
|
|
|
|
|
|
|
Divide {np, nn} by {dp, dn} and put the quotient at {qp,nn-dn+1}
|
|
|
|
and the remainder at {rp, dn}. The quotient is rounded towards 0.
|
|
|
|
No overlap is permitted between arguments. nn must be greater than
|
|
|
|
or equal to dn. The most significant limb of dp must be non-zero.
|
|
|
|
The qxn operand must be zero.
|
|
|
|
#endif
|
|
|
|
|
2007-12-23 13:37:48 -05:00
|
|
|
ikptr
|
|
|
|
ikrt_bnbndivrem(ikptr x, ikptr y, ikpcb* pcb){
|
|
|
|
ikptr xfst = ref(x, -vector_tag);
|
|
|
|
ikptr yfst = ref(y, -vector_tag);
|
2008-01-01 04:24:36 -05:00
|
|
|
mp_size_t xn = bnfst_limb_count(xfst);
|
|
|
|
mp_size_t yn = bnfst_limb_count(yfst);
|
2006-12-06 20:53:54 -05:00
|
|
|
if(xn < yn){
|
|
|
|
/* quotient is zero, remainder is x */
|
2007-11-18 10:37:13 -05:00
|
|
|
pcb->root0 = &x;
|
|
|
|
pcb->root1 = &y;
|
2007-12-23 13:37:48 -05:00
|
|
|
ikptr rv = ik_safe_alloc(pcb, pair_size);
|
2007-11-18 10:37:13 -05:00
|
|
|
pcb->root0 = 0;
|
|
|
|
pcb->root1 = 0;
|
2006-12-06 20:53:54 -05:00
|
|
|
ref(rv, disp_car) = 0;
|
|
|
|
ref(rv, disp_cdr) = x;
|
|
|
|
return rv+pair_tag;
|
|
|
|
}
|
|
|
|
mp_size_t qn = xn - yn + 1;
|
|
|
|
mp_size_t rn = yn;
|
2007-11-18 10:37:13 -05:00
|
|
|
/*
|
2007-12-23 13:37:48 -05:00
|
|
|
ikptr q = ik_unsafe_alloc(pcb, align(disp_bignum_data + qn*wordsize));
|
|
|
|
ikptr r = ik_unsafe_alloc(pcb, align(disp_bignum_data + rn*wordsize));
|
2007-11-18 10:37:13 -05:00
|
|
|
*/
|
|
|
|
pcb->root0 = &x;
|
|
|
|
pcb->root1 = &y;
|
2007-12-23 13:37:48 -05:00
|
|
|
ikptr q = ik_safe_alloc(pcb,
|
2007-11-18 10:37:13 -05:00
|
|
|
align(disp_bignum_data + qn*wordsize) +
|
|
|
|
align(disp_bignum_data + rn*wordsize));
|
2007-12-23 13:37:48 -05:00
|
|
|
ikptr r = q + align(disp_bignum_data + qn*wordsize);
|
2007-11-18 10:37:13 -05:00
|
|
|
pcb->root0 = 0;
|
|
|
|
pcb->root1 = 0;
|
2006-12-06 20:53:54 -05:00
|
|
|
mpn_tdiv_qr (
|
2008-01-01 21:42:52 -05:00
|
|
|
(mp_limb_t*)(long)(q+disp_bignum_data),
|
|
|
|
(mp_limb_t*)(long)(r+disp_bignum_data),
|
2006-12-06 20:53:54 -05:00
|
|
|
0,
|
2008-01-01 21:42:52 -05:00
|
|
|
(mp_limb_t*)(long)(x+off_bignum_data),
|
2006-12-06 20:53:54 -05:00
|
|
|
xn,
|
2008-01-01 21:42:52 -05:00
|
|
|
(mp_limb_t*)(long)(y+off_bignum_data),
|
2006-12-06 20:53:54 -05:00
|
|
|
yn);
|
|
|
|
|
2008-01-01 04:24:36 -05:00
|
|
|
if(bnfst_negative(xfst)){
|
2006-12-06 20:53:54 -05:00
|
|
|
/* x is negative => remainder is negative */
|
|
|
|
r = normalize_bignum(rn, 1 << bignum_sign_shift, r);
|
|
|
|
} else {
|
|
|
|
r = normalize_bignum(rn, 0, r);
|
|
|
|
}
|
|
|
|
|
2008-01-01 04:24:36 -05:00
|
|
|
if(bnfst_negative(yfst)){
|
2006-12-06 20:53:54 -05:00
|
|
|
/* y is negative => quotient is opposite of x */
|
2008-01-01 04:24:36 -05:00
|
|
|
long int sign = bignum_sign_mask - bnfst_negative(xfst);
|
2006-12-06 20:53:54 -05:00
|
|
|
q = normalize_bignum(qn, sign, q);
|
|
|
|
} else {
|
|
|
|
/* y is positive => quotient is same as x */
|
2008-01-01 04:24:36 -05:00
|
|
|
long int sign = bnfst_negative(xfst);
|
2006-12-06 20:53:54 -05:00
|
|
|
q = normalize_bignum(qn, sign, q);
|
|
|
|
}
|
2007-11-18 10:37:13 -05:00
|
|
|
pcb->root0 = &q;
|
|
|
|
pcb->root1 = &r;
|
2007-12-23 13:37:48 -05:00
|
|
|
ikptr rv = ik_safe_alloc(pcb, pair_size);
|
2007-11-18 10:37:13 -05:00
|
|
|
pcb->root0 = 0;
|
|
|
|
pcb->root1 = 0;
|
2006-12-06 20:53:54 -05:00
|
|
|
ref(rv, disp_car) = q;
|
|
|
|
ref(rv, disp_cdr) = r;
|
|
|
|
return rv+pair_tag;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#if 0
|
|
|
|
[Function]
|
|
|
|
|
|
|
|
mp_limb_t
|
|
|
|
mpn_divrem_1 (
|
|
|
|
mp limb t *r1p,
|
|
|
|
mp size t qxn,
|
|
|
|
mp limb t *s2p,
|
|
|
|
mp size t s2n,
|
|
|
|
mp limb t s3limb
|
|
|
|
)
|
|
|
|
|
|
|
|
Divide {s2p, s2n} by s3limb, and write the quotient at r1p. Return the remainder.
|
|
|
|
The integer quotient is written to {r1p+qxn, s2n} and in addition qxn fraction limbs are
|
|
|
|
developed and written to {r1p, qxn}. Either or both s2n and qxn can be zero. For most
|
|
|
|
usages, qxn will be zero.
|
|
|
|
#endif
|
|
|
|
|
2007-12-23 13:37:48 -05:00
|
|
|
ikptr
|
|
|
|
ikrt_bnfxdivrem(ikptr x, ikptr y, ikpcb* pcb){
|
2008-07-18 04:35:13 -04:00
|
|
|
long int yint = unfix(y);
|
2007-12-23 13:37:48 -05:00
|
|
|
ikptr fst = ref(x, -vector_tag);
|
2008-01-01 04:24:36 -05:00
|
|
|
mp_size_t s2n = bnfst_limb_count(fst);
|
2007-11-18 10:37:13 -05:00
|
|
|
pcb->root0 = &x;
|
2007-12-23 13:37:48 -05:00
|
|
|
ikptr quot = ik_safe_alloc(pcb, align(s2n*wordsize + disp_bignum_data));
|
2007-11-18 10:37:13 -05:00
|
|
|
pcb->root0 = 0;
|
2008-01-01 21:42:52 -05:00
|
|
|
mp_limb_t* s2p = (mp_limb_t*)(long)(x+off_bignum_data);
|
2006-12-06 20:53:54 -05:00
|
|
|
mp_limb_t rv = mpn_divrem_1(
|
2008-01-01 21:42:52 -05:00
|
|
|
(mp_limb_t*)(long)(quot+disp_bignum_data),
|
2006-12-06 20:53:54 -05:00
|
|
|
0,
|
|
|
|
s2p,
|
|
|
|
s2n,
|
2008-07-18 04:35:13 -04:00
|
|
|
labs(yint));
|
2006-12-06 20:53:54 -05:00
|
|
|
|
2007-12-23 13:37:48 -05:00
|
|
|
ikptr rem;
|
2006-12-06 20:53:54 -05:00
|
|
|
|
|
|
|
if(yint < 0){
|
|
|
|
/* y is negative => quotient is opposite of x */
|
2008-01-01 04:24:36 -05:00
|
|
|
long int sign = bignum_sign_mask - bnfst_negative(fst);
|
2006-12-06 20:53:54 -05:00
|
|
|
quot = normalize_bignum(s2n, sign, quot);
|
|
|
|
} else {
|
|
|
|
/* y is positive => quotient is same as x */
|
2008-01-01 04:24:36 -05:00
|
|
|
long int sign = bnfst_negative(fst);
|
2006-12-06 20:53:54 -05:00
|
|
|
quot = normalize_bignum(s2n, sign, quot);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* the remainder is always less than |y|, so it will
|
|
|
|
always be a fixnum. (if y == most_negative_fixnum,
|
|
|
|
then |remainder| will be at most most_positive_fixnum). */
|
2008-01-01 04:24:36 -05:00
|
|
|
if(bnfst_negative(fst)){
|
2006-12-06 20:53:54 -05:00
|
|
|
/* x is negative => remainder is negative */
|
2007-12-23 13:37:48 -05:00
|
|
|
rem = (ikptr) -(rv << fx_shift);
|
2006-12-06 20:53:54 -05:00
|
|
|
} else {
|
|
|
|
rem = fix(rv);
|
|
|
|
}
|
2007-11-18 10:37:13 -05:00
|
|
|
pcb->root0 = "
|
2008-10-04 01:59:34 -04:00
|
|
|
pcb->root1 = &rem;
|
2007-12-23 13:37:48 -05:00
|
|
|
ikptr p = ik_safe_alloc(pcb, pair_size);
|
2007-11-18 10:37:13 -05:00
|
|
|
pcb->root0 = 0;
|
2008-10-04 01:59:34 -04:00
|
|
|
pcb->root1 = 0;
|
2006-12-06 20:53:54 -05:00
|
|
|
ref(p, disp_car) = quot;
|
|
|
|
ref(p, disp_cdr) = rem;
|
|
|
|
return p+pair_tag;
|
|
|
|
}
|
|
|
|
|
2007-12-23 13:37:48 -05:00
|
|
|
ikptr
|
2008-06-04 03:54:53 -04:00
|
|
|
ikrt_bnfx_modulo(ikptr x, ikptr y /*, ikpcb* pcb */){
|
2007-11-04 23:01:41 -05:00
|
|
|
int yint = unfix(y);
|
2008-01-01 21:42:52 -05:00
|
|
|
mp_limb_t* s2p = (mp_limb_t*)(long)(x+off_bignum_data);
|
2007-12-23 13:37:48 -05:00
|
|
|
ikptr fst = ref(x, -vector_tag);
|
2008-01-01 04:24:36 -05:00
|
|
|
mp_size_t s2n = bnfst_limb_count(fst);
|
2007-11-04 23:01:41 -05:00
|
|
|
if(yint < 0){
|
2008-01-01 04:24:36 -05:00
|
|
|
if(bnfst_negative(fst)){
|
2007-11-04 23:01:41 -05:00
|
|
|
/* x negative, y negative */
|
|
|
|
mp_limb_t m = mpn_mod_1(s2p, s2n, -yint);
|
|
|
|
return fix(-m);
|
|
|
|
} else {
|
|
|
|
/* x positive, y negative */
|
|
|
|
mp_limb_t m = mpn_mod_1(s2p, s2n, -yint);
|
|
|
|
return fix(yint+m);
|
|
|
|
}
|
|
|
|
} else {
|
2008-01-01 04:24:36 -05:00
|
|
|
if(bnfst_negative(fst)){
|
2007-11-04 23:01:41 -05:00
|
|
|
/* x negative, y positive */
|
|
|
|
mp_limb_t m = mpn_mod_1(s2p, s2n, yint);
|
|
|
|
return fix(yint-m);
|
|
|
|
} else {
|
|
|
|
/* x positive, y positive */
|
|
|
|
mp_limb_t m = mpn_mod_1(s2p, s2n, yint);
|
|
|
|
return fix(m);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-12-23 17:33:13 -05:00
|
|
|
static int
|
2008-07-19 01:21:57 -04:00
|
|
|
limb_length(unsigned long int n){
|
2007-12-23 17:33:13 -05:00
|
|
|
int i=0;
|
|
|
|
while(n != 0){
|
|
|
|
n = n >> 1;
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
ikptr
|
|
|
|
ikrt_bignum_length(ikptr x){
|
|
|
|
ikptr fst = ref(x, -vector_tag);
|
2008-01-01 21:42:52 -05:00
|
|
|
mp_limb_t* sp = (mp_limb_t*)(long)(x+off_bignum_data);
|
2008-01-01 04:24:36 -05:00
|
|
|
mp_size_t sn = bnfst_limb_count(fst);
|
2007-12-23 17:33:13 -05:00
|
|
|
mp_limb_t last = sp[sn-1];
|
|
|
|
int n0 = limb_length(last);
|
2008-01-01 04:24:36 -05:00
|
|
|
if(((unsigned long int) fst) & bignum_sign_mask){
|
2007-12-23 17:33:13 -05:00
|
|
|
/* negative */
|
2008-07-19 01:21:57 -04:00
|
|
|
if (last == (mp_limb_t)(1L<<(n0-1))){
|
2007-12-23 17:33:13 -05:00
|
|
|
/* single bit set in last limb */
|
|
|
|
int i;
|
|
|
|
for(i=0; i<(sn-1); i++){
|
|
|
|
if(sp[i] != 0){
|
|
|
|
/* another bit set */
|
|
|
|
return fix((sn-1)*mp_bits_per_limb + n0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/* number is - #b100000000000000000000000000 */
|
|
|
|
/* fxnot(n) = #b011111111111111111111111111 */
|
|
|
|
/* so, subtract 1. */
|
|
|
|
return fix((sn-1)*mp_bits_per_limb + n0 - 1);
|
|
|
|
} else {
|
|
|
|
return fix((sn-1)*mp_bits_per_limb + n0);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return fix((sn-1)*mp_bits_per_limb + n0);
|
|
|
|
}
|
|
|
|
}
|
2007-11-04 23:01:41 -05:00
|
|
|
|
|
|
|
|
2007-12-23 13:37:48 -05:00
|
|
|
ikptr
|
|
|
|
ikrt_bignum_to_bytevector(ikptr x, ikpcb* pcb){
|
2006-11-23 19:48:14 -05:00
|
|
|
/* FIXME: avoid calling malloc, instead, use the heap pointer itself
|
|
|
|
* as a buffer to hold the temporary data after ensuring that it has enough
|
|
|
|
* space */
|
2007-12-23 13:37:48 -05:00
|
|
|
ikptr fst = ref(x, -vector_tag);
|
2008-01-01 04:24:36 -05:00
|
|
|
long int limb_count = bnfst_limb_count(fst);
|
2006-11-23 19:48:14 -05:00
|
|
|
if(limb_count <= 0){
|
2008-01-01 04:24:36 -05:00
|
|
|
fprintf(stderr, "BUG: nbtostring: invalid length %ld\n", limb_count);
|
2006-11-23 19:48:14 -05:00
|
|
|
exit(-1);
|
|
|
|
}
|
2008-01-01 04:24:36 -05:00
|
|
|
long int sign_bit = bignum_sign_mask & (long int) fst;
|
|
|
|
long int nbsize = limb_count * sizeof(mp_limb_t);
|
|
|
|
long int strsize = limb_count * max_digits_per_limb;
|
|
|
|
long int mem_req = nbsize + strsize + 1;
|
2006-11-23 19:48:14 -05:00
|
|
|
unsigned char* mem = malloc(mem_req);
|
|
|
|
if(! mem){
|
|
|
|
fprintf(stderr, "Error allocating space for bignum\n");
|
|
|
|
exit(-1);
|
|
|
|
}
|
2008-01-01 21:42:52 -05:00
|
|
|
memcpy((char*)(long)mem,
|
|
|
|
(char*)(long)x - vector_tag + disp_bignum_data,
|
|
|
|
nbsize);
|
2006-11-23 19:48:14 -05:00
|
|
|
mp_size_t bytes =
|
|
|
|
mpn_get_str(mem+nbsize, /* output string */
|
|
|
|
10, /* base */
|
|
|
|
(mp_limb_t*) mem, /* limb */
|
|
|
|
limb_count /* number of limbs */
|
|
|
|
);
|
|
|
|
unsigned char* string_start = mem + nbsize;
|
|
|
|
while(*string_start == 0){
|
|
|
|
string_start++;
|
|
|
|
bytes--;
|
|
|
|
}
|
2007-12-23 13:37:48 -05:00
|
|
|
ikptr bv = ik_safe_alloc(pcb, align(bytes + disp_bytevector_data + (sign_bit?1:0)));
|
2007-05-18 22:08:45 -04:00
|
|
|
ref(bv, 0) = fix(bytes + (sign_bit?1:0));
|
2008-01-01 21:08:07 -05:00
|
|
|
char* dest = (char*)(long)(bv + disp_bytevector_data);
|
2006-11-23 19:48:14 -05:00
|
|
|
if(sign_bit){
|
|
|
|
*dest = '-';
|
|
|
|
dest++;
|
|
|
|
}
|
|
|
|
{
|
2008-01-01 04:24:36 -05:00
|
|
|
long int i = 0;
|
2006-11-23 19:48:14 -05:00
|
|
|
while(i < bytes){
|
|
|
|
dest[i] = string_start[i] + '0';
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
dest[bytes] = 0;
|
|
|
|
}
|
|
|
|
free(mem);
|
2007-05-18 22:08:45 -04:00
|
|
|
return bv + bytevector_tag;
|
2006-11-23 19:48:14 -05:00
|
|
|
}
|
|
|
|
|
2007-08-30 21:44:52 -04:00
|
|
|
|
2007-12-23 13:37:48 -05:00
|
|
|
ikptr
|
|
|
|
ikrt_fxrandom(ikptr x){
|
2007-08-30 21:44:52 -04:00
|
|
|
int mask = 1;
|
|
|
|
int n = unfix(x);
|
|
|
|
{
|
|
|
|
while(mask < n){
|
|
|
|
mask = (mask << 1) | 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
while(1){
|
|
|
|
long r = random() & mask;
|
|
|
|
if(r < n){
|
|
|
|
return fix(r);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-01-12 20:52:23 -05:00
|
|
|
static int
|
|
|
|
limb_size(mp_limb_t x){
|
|
|
|
int i = 0;
|
|
|
|
while(x){
|
|
|
|
i++;
|
|
|
|
x = x>>1;
|
|
|
|
}
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
all_zeros(mp_limb_t* start, mp_limb_t* end){
|
|
|
|
while(start <= end){
|
|
|
|
if(*end) return 0;
|
|
|
|
end--;
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
#define PRECISION 53
|
2008-07-18 04:35:13 -04:00
|
|
|
|
|
|
|
static ikptr
|
|
|
|
ikrt_bignum_to_flonum64(ikptr bn, ikptr more_bits, ikptr fl){
|
|
|
|
ikptr fst = ref(bn, -vector_tag);
|
|
|
|
long int limb_count = bnfst_limb_count(fst);
|
|
|
|
mp_limb_t* sp = (mp_limb_t*)(long)(bn+off_bignum_data);
|
|
|
|
double pos_result;
|
|
|
|
if(limb_count == 1){
|
|
|
|
pos_result = sp[0];
|
|
|
|
} else {
|
|
|
|
mp_limb_t hi = sp[limb_count-1];
|
|
|
|
int bc = limb_size(hi);
|
2008-07-19 01:21:57 -04:00
|
|
|
if(bc < 64){
|
|
|
|
mp_limb_t mi = sp[limb_count-2];
|
|
|
|
hi = (hi << (64-bc)) | (mi >> bc);
|
2008-07-18 04:35:13 -04:00
|
|
|
}
|
2008-07-19 01:21:57 -04:00
|
|
|
/* now hi has 64 full bits */
|
|
|
|
mp_limb_t mask = ((1L<<(64-PRECISION)) - 1);
|
|
|
|
if((hi & mask) == ((mask+1)>>1)){
|
2008-07-18 04:35:13 -04:00
|
|
|
/* exactly at break point */
|
2008-07-19 01:21:57 -04:00
|
|
|
if(((sp[limb_count-2] << (64-bc)) == 0) &&
|
|
|
|
all_zeros(sp, sp+limb_count-3) &&
|
2008-07-18 04:35:13 -04:00
|
|
|
(more_bits == 0)){
|
2008-07-19 01:21:57 -04:00
|
|
|
if(hi & (1L<<(64-PRECISION))){
|
2008-07-18 04:35:13 -04:00
|
|
|
/* odd number, round to even */
|
2008-07-19 01:21:57 -04:00
|
|
|
hi = hi | mask;
|
2008-07-18 04:35:13 -04:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
/* round up */
|
2008-07-19 01:21:57 -04:00
|
|
|
hi = hi | mask;
|
2008-07-18 04:35:13 -04:00
|
|
|
}
|
2008-07-19 01:21:57 -04:00
|
|
|
} else if ((hi & mask) > ((mask+1)>>1)){
|
2008-07-18 04:35:13 -04:00
|
|
|
/* also round up */
|
2008-07-19 01:21:57 -04:00
|
|
|
hi = hi | mask;
|
2008-07-18 04:35:13 -04:00
|
|
|
} else {
|
|
|
|
/* keep it to round down */
|
|
|
|
}
|
|
|
|
pos_result = hi;
|
|
|
|
int bignum_bits = bc + (mp_bits_per_limb * (limb_count-1));
|
2008-07-19 01:21:57 -04:00
|
|
|
int exponent = bignum_bits - mp_bits_per_limb;
|
2008-07-18 04:35:13 -04:00
|
|
|
while(exponent){
|
|
|
|
pos_result *= 2.0;
|
|
|
|
exponent -= 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if(bnfst_negative(fst)){
|
|
|
|
flonum_data(fl) = - pos_result;
|
|
|
|
} else {
|
|
|
|
flonum_data(fl) = pos_result;
|
|
|
|
}
|
|
|
|
return fl;
|
|
|
|
}
|
|
|
|
|
2008-01-12 20:52:23 -05:00
|
|
|
ikptr
|
|
|
|
ikrt_bignum_to_flonum(ikptr bn, ikptr more_bits, ikptr fl){
|
2008-07-18 04:35:13 -04:00
|
|
|
if(mp_bits_per_limb == 64){
|
|
|
|
return ikrt_bignum_to_flonum64(bn, more_bits, fl);
|
2008-01-12 20:52:23 -05:00
|
|
|
}
|
|
|
|
ikptr fst = ref(bn, -vector_tag);
|
|
|
|
long int limb_count = bnfst_limb_count(fst);
|
|
|
|
mp_limb_t* sp = (mp_limb_t*)(long)(bn+off_bignum_data);
|
|
|
|
double pos_result;
|
|
|
|
if(limb_count == 1){
|
|
|
|
pos_result = sp[0];
|
|
|
|
} else if (limb_count == 2){
|
|
|
|
mp_limb_t lo = sp[0];
|
|
|
|
mp_limb_t hi = sp[1];
|
|
|
|
pos_result = hi;
|
|
|
|
pos_result = pos_result * 4294967296.0;
|
|
|
|
pos_result = pos_result + lo;
|
|
|
|
} else {
|
|
|
|
mp_limb_t hi = sp[limb_count-1];
|
|
|
|
mp_limb_t mi = sp[limb_count-2];
|
|
|
|
int bc = limb_size(hi);
|
|
|
|
if(bc < 32){
|
|
|
|
mp_limb_t lo = sp[limb_count-3];
|
|
|
|
hi = (hi << (32-bc)) | (mi >> bc);
|
|
|
|
mi = (mi << (32-bc)) | (lo >> bc);
|
|
|
|
}
|
|
|
|
/* now hi has 32 full bits, and mi has 32 full bits */
|
|
|
|
mp_limb_t mask = ((1<<(64-PRECISION)) - 1);
|
|
|
|
if((mi & mask) == ((mask+1)>>1)){
|
|
|
|
/* exactly at break point */
|
|
|
|
if(((sp[limb_count-3] << (32-bc)) == 0) &&
|
|
|
|
all_zeros(sp, sp+limb_count-4) &&
|
|
|
|
(more_bits == 0)){
|
|
|
|
if(mi & (1<<(64-PRECISION))){
|
|
|
|
/* odd number, round to even */
|
|
|
|
mi = mi | mask;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
/* round up */
|
|
|
|
mi = mi | mask;
|
|
|
|
}
|
|
|
|
} else if ((mi & mask) > ((mask+1)>>1)){
|
|
|
|
/* also round up */
|
|
|
|
mi = mi | mask;
|
|
|
|
} else {
|
|
|
|
/* keep it to round down */
|
|
|
|
}
|
|
|
|
pos_result = hi;
|
|
|
|
pos_result = pos_result * 4294967296.0;
|
|
|
|
pos_result = pos_result + mi;
|
|
|
|
int bignum_bits = bc + (mp_bits_per_limb * (limb_count-1));
|
|
|
|
int exponent = bignum_bits - (2 * mp_bits_per_limb);
|
|
|
|
while(exponent){
|
|
|
|
pos_result *= 2.0;
|
|
|
|
exponent -= 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if(bnfst_negative(fst)){
|
|
|
|
flonum_data(fl) = - pos_result;
|
|
|
|
} else {
|
|
|
|
flonum_data(fl) = pos_result;
|
|
|
|
}
|
|
|
|
return fl;
|
|
|
|
}
|
|
|
|
|
2008-07-19 17:41:06 -04:00
|
|
|
ikptr
|
|
|
|
ikrt_exact_fixnum_sqrt(ikptr fx /*, ikpcb* pcb*/){
|
|
|
|
mp_limb_t x = unfix(fx);
|
|
|
|
mp_limb_t s;
|
|
|
|
mp_limb_t r;
|
|
|
|
mpn_sqrtrem(&s, &r, &x, 1);
|
|
|
|
return fix(s);
|
|
|
|
}
|
|
|
|
|
2008-01-17 01:26:29 -05:00
|
|
|
ikptr
|
|
|
|
ikrt_exact_bignum_sqrt(ikptr bn, ikpcb* pcb){
|
|
|
|
ikptr fst = ref(bn, -vector_tag);
|
|
|
|
long int limb_count = bnfst_limb_count(fst);
|
|
|
|
long int result_limb_count = (limb_count + 1)/2;
|
|
|
|
pcb->root0 = &bn;
|
|
|
|
ikptr s = ik_safe_alloc(pcb,
|
|
|
|
align(disp_bignum_data+result_limb_count*wordsize))
|
|
|
|
+ vector_tag;
|
|
|
|
ref(s, -vector_tag) =
|
|
|
|
(ikptr) (bignum_tag | (result_limb_count << bignum_length_shift));
|
|
|
|
pcb->root1 = &s;
|
|
|
|
ikptr r = ik_safe_alloc(pcb,
|
|
|
|
align(disp_bignum_data+limb_count*wordsize))
|
|
|
|
+ vector_tag;
|
|
|
|
ref(r, -vector_tag) =
|
|
|
|
(ikptr) (bignum_tag | (limb_count << bignum_length_shift));
|
|
|
|
pcb->root0 = &r;
|
|
|
|
ikptr pair = ik_safe_alloc(pcb, pair_size) + pair_tag;
|
|
|
|
pcb->root0 = 0;
|
|
|
|
pcb->root1 = 0;
|
|
|
|
mp_size_t r_actual_limbs = mpn_sqrtrem(
|
|
|
|
(mp_limb_t*) (s+off_bignum_data),
|
|
|
|
(mp_limb_t*) (r+off_bignum_data),
|
|
|
|
(mp_limb_t*) (bn+off_bignum_data),
|
|
|
|
limb_count);
|
|
|
|
ref(pair, off_car) = normalize_bignum(result_limb_count, 0, s-vector_tag);
|
|
|
|
if(r_actual_limbs == 0) {
|
|
|
|
/* perfect square */
|
|
|
|
ref(pair, off_cdr) = 0;
|
|
|
|
} else {
|
|
|
|
ref(pair, off_cdr) = normalize_bignum(r_actual_limbs, 0, r-vector_tag);
|
|
|
|
}
|
|
|
|
return pair;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-10-31 23:09:03 -04:00
|
|
|
ikptr
|
|
|
|
ikrt_flonum_hash(ikptr x /*, ikpcb* pcb */) {
|
2008-10-31 23:53:15 -04:00
|
|
|
short* buf = (short*)(x+off_flonum_data);
|
|
|
|
return fix(((long)buf[0]) ^
|
|
|
|
((long)buf[1] << 3) ^
|
|
|
|
((long)buf[3] << 7) ^
|
|
|
|
((long)buf[2] << 11));
|
2008-10-31 23:09:03 -04:00
|
|
|
}
|
|
|
|
ikptr
|
2008-10-31 23:53:15 -04:00
|
|
|
ikrt_bignum_hash(ikptr bn /*, ikpcb* pcb */) {
|
|
|
|
ikptr fst = ref(bn, -vector_tag);
|
|
|
|
long int limb_count = bnfst_limb_count(fst);
|
|
|
|
long h = (long)fst;
|
|
|
|
mp_limb_t* dat = (mp_limb_t*)(bn+off_bignum_data);
|
|
|
|
long i;
|
|
|
|
for (i=0; i<limb_count; i++){
|
|
|
|
h = (h^dat[i]) << 3;
|
|
|
|
}
|
|
|
|
return fix(h);
|
2008-10-31 23:09:03 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|