7 BN_ULONG bn_div_2word();
9 int BN_div2(dv, rm, num, div,ctx)
16 int norm_shift,i,j,nm,nd,loop;
17 BIGNUM *tmp,wnum,*snum,*sdiv,*res;
18 BN_ULONG *resp,*wnump;
23 BN_print(stdout,num); printf(" number\n");
24 BN_print(stdout,div); printf(" divisor\n");
28 BNerr(BN_F_BN_DIV,BN_R_DIV_BY_ZERO);
32 if (BN_cmp(num,div) < 0)
35 { if (BN_copy(rm,num) == NULL) return(0); }
36 if (dv != NULL) BN_zero(dv);
40 tmp=ctx->bn[ctx->tos];
41 snum=ctx->bn[ctx->tos+1];
42 sdiv=ctx->bn[ctx->tos+2];
44 res=ctx->bn[ctx->tos+3];
47 /* First we normalise the numbers */
48 norm_shift=BN_BITS2-((BN_num_bits(div))%BN_BITS2);
49 BN_lshift(sdiv,div,norm_shift);
51 BN_lshift(snum,num,norm_shift);
56 BN_print(stdout,snum); printf(" shifted num, forget last word\n");
57 BN_print(stdout,sdiv); printf(" shifted div\n");
60 /* Lets setup a 'win'dow into snum
61 * This is the part that corresponds to the current
62 * 'area' being divided */
63 wnum.d= &(snum->d[loop]);
65 wnum.max= snum->max; /* a bit of a lie */
68 /* Get the top 2 words of sdiv */
73 /* pointer to the 'top' of snum */
74 wnump= &(snum->d[num_n-1]);
79 resp= &(res->d[loop-1]);
80 bn_expand(res,(loop+1)*BN_BITS2);
83 bn_expand(tmp,(div_n+1)*BN_BITS2);
86 printf("wnum="); BN_print(stdout,&wnum); printf(" initial sub check\n");
87 printf("div ="); BN_print(stdout,sdiv); printf(" loop=%d\n",loop);
89 if (BN_cmp(&wnum,sdiv) >= 0)
91 BN_sub(&wnum,&wnum,sdiv);
99 BN_print(stdout,res); printf(" initial result\n");
100 BN_print(stdout,&wnum); printf(" wnum\n");
103 for (i=0; i<loop-1; i++)
113 BN_print(stderr,&wnum); printf(" to divide\n");
118 t1=((BN_ULLONG)n0<<BN_BITS2)|wnump[-1];
126 printf("t1=%08X / d0=%08X = %X (%X)\n",t1,d0,q,t2);
132 t3=t1-(BN_ULLONG)q*d0;
134 printf("d1*q= %X n01-q*d0 = %X\n",t2,t3);
136 if ((t3>>BN_BITS2) ||
137 (t2 <= ((t3<<BN_BITS2)+wnump[-2])))
140 printf("reduce q\n");
144 l0=bn_mul_word(tmp->d,sdiv->d,div_n,q);
149 for (j=div_n+1; j>0; j--)
150 if (tmp->d[j-1]) break;
154 printf("q=%08X\n",q);
155 BN_print(stdout,&wnum); printf(" number\n");
156 BN_print(stdout,tmp); printf(" subtract\n");
158 BN_print(stdout,snum); printf(" shifted number before\n");
159 BN_print(stdout,&wnum); printf(" wnum before\n");
162 BN_sub(&wnum,&wnum,tmp);
163 snum->top=snum->top+wnum.top-j;
166 BN_print(stdout,&wnum); printf(" wnum after\n");
167 BN_print(stdout,snum); printf(" shifted number after\n");
174 BN_add(&wnum,&wnum,sdiv);
175 snum->top+=wnum.top-j;
176 fprintf(stderr,"addback\n");
178 BN_print(stdout,snum); printf("after addback************************:\n");
183 BN_print(stdout,res); printf(" result\n");
188 BN_rshift(rm,snum,norm_shift);
207 for (i=0; i<10240; i++)
212 BN_div2(d,c,a,b,ctx);
213 BN_div(dd,cc,a,b,ctx);
214 if ((BN_cmp(d,dd) != 0) || (BN_cmp(c,cc) != 0))
216 BN_print(stderr,a); fprintf(stderr," / ");
217 BN_print(stderr,b); fprintf(stderr," d=");
218 BN_print(stderr,d); fprintf(stderr," r= ");
219 BN_print(stderr,c); fprintf(stderr,"\nd=");
220 BN_print(stderr,dd); fprintf(stderr," r= ");
221 BN_print(stderr,cc); fprintf(stderr,"\n");
230 for (i=0; i<2000000; i++)
232 BN_div2(d,c,a,b,ctx);
236 /* for (i=0; i<0xffffffff; i++)
248 /* for (x=1; x<256*256; x++) */
253 a&= ~(0xFFFFFF<<(BN_num_bits_word(c)));
255 r=bn_div_2word(a,b,c);
257 rr=(BN_ULONG)((((BN_ULLONG)a<<BN_BITS2)|b)/c);
259 if ((i & 0xfffff) == 0) fprintf(stderr,"%d\n",i,r,rr);
261 fprintf(stderr,"%6d/%3d = %4d %4d\n",(a<<8)|b,c,r,rr); */
264 fprintf(stderr,"%8d %02X%02X / %02X = %02X %02X\n",
273 /* Divide h-l by d and return the result. */
274 BN_ULONG bn_div_2word(l,h,d)
277 BN_ULONG dh,dl,q,ret=0,th,tl,t,top;
280 if (d == 0) return(-1);
282 i=BN_num_bits_word(d);
283 if ((i != BN_BITS2) && (h > 1<<i))
285 fprintf(stderr,"Division would overflow\n");
294 h=(h<<i)|(l>>(BN_BITS2-i));
297 dh=(d&BN_MASK2h)>>BN_BITS4;
301 if ((h>>BN_BITS4) == dh)
312 ((l&BN_MASK2h)>>BN_BITS4))))
319 tl=(tl<<BN_BITS4)&BN_MASK2h;
326 fprintf(stderr,"add back\n");
332 if (--count == 0) break;
335 h=((h<<BN_BITS4)|(l>>BN_BITS4))&BN_MASK2;
336 l=(l&BN_MASK2l)<<BN_BITS4;