6 mplogic calculates b1|b2 subject to the
7 following flag bits (fl)
9 bit 0: subtract 1 from b1
11 bit 2: subtract 1 from b2
13 bit 4: add 1 to output
16 it inverts appropriate bits automatically
17 depending on the signs of the inputs
21 mplogic(mpint *b1, mpint *b2, mpint *sum, int fl)
24 mpdigit *dp1, *dp2, *dpo, d1, d2, d;
28 assert(((b1->flags | b2->flags | sum->flags) & MPtimesafe) == 0);
29 if(b1->sign < 0) fl ^= 0x03;
30 if(b2->sign < 0) fl ^= 0x0c;
31 sum->sign = (int)(((fl|fl>>2)^fl>>4)<<30)>>31|1;
32 if(sum->sign < 0) fl ^= 0x30;
33 if(b2->top > b1->top){
37 fl = fl >> 2 & 0x03 | fl << 2 & 0x0c | fl & 0x30;
39 mpbits(sum, b1->top*Dbits+1);
46 for(i = 0; i < b1->top; i++){
52 if(d1 != (mpdigit)-1) c1 = 0;
53 if(d2 != (mpdigit)-1) c2 = 0;
54 if((fl & 2) != 0) d1 ^= -1;
55 if((fl & 8) != 0) d2 ^= -1;
57 if((fl & 32) != 0) d ^= -1;
69 mpor(mpint *b1, mpint *b2, mpint *sum)
71 mplogic(b1, b2, sum, 0);
75 mpand(mpint *b1, mpint *b2, mpint *sum)
77 mplogic(b1, b2, sum, 0x2a);
81 mpbic(mpint *b1, mpint *b2, mpint *sum)
83 mplogic(b1, b2, sum, 0x22);
87 mpnot(mpint *b, mpint *r)
95 mpxor(mpint *b1, mpint *b2, mpint *sum)
98 mpdigit *dp1, *dp2, *dpo, d1, d2, d;
102 assert(((b1->flags | b2->flags | sum->flags) & MPtimesafe) == 0);
103 if(b2->top > b1->top){
108 fl = (b1->sign & 10) ^ (b2->sign & 12);
109 sum->sign = (int)(fl << 28) >> 31 | 1;
110 mpbits(sum, b1->top*Dbits+1);
117 for(i = 0; i < b1->top; i++){
123 if(d1 != (mpdigit)-1) c1 = 0;
124 if(d2 != (mpdigit)-1) c2 = 0;
132 dpo[sum->top++] = co;
137 mptrunc(mpint *b, int n, mpint *r)
141 assert(((b->flags | r->flags) & MPtimesafe) == 0);
148 for(i = 0; i < r->top; i++){
150 r->p[i] = ~(b->p[i] - c);
157 r->p[d] &= (1<<m) - 1;
158 }else if(b->sign == 1){
165 for(i = 0; i < d; i++)
168 r->p[d] = b->p[d] & (1<<m)-1;
175 mpxtend(mpint *b, int n, mpint *r)
187 if((r->p[d] & 1<<m) == 0){
194 for(i = 0; i < r->top; i++){
195 r->p[i] = ~(r->p[i] - c);
203 mpasr(mpint *b, int n, mpint *r)
205 if(b->sign > 0 || n <= 0){