bn_div.c revision 280297
1139825Simp/* crypto/bn/bn_div.c */
294755Sbenno/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3256901Snwhitehorn * All rights reserved.
4256901Snwhitehorn *
5256901Snwhitehorn * This package is an SSL implementation written
694755Sbenno * by Eric Young (eay@cryptsoft.com).
794755Sbenno * The implementation was written so as to conform with Netscapes SSL.
894755Sbenno *
994755Sbenno * This library is free for commercial and non-commercial use as long as
1094755Sbenno * the following conditions are aheared to.  The following conditions
1194755Sbenno * apply to all code found in this distribution, be it the RC4, RSA,
1294755Sbenno * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
1394755Sbenno * included with this distribution is covered by the same copyright terms
1494755Sbenno * except that the holder is Tim Hudson (tjh@cryptsoft.com).
1594755Sbenno *
1694755Sbenno * Copyright remains Eric Young's, and as such any Copyright notices in
1794755Sbenno * the code are not to be removed.
18125702Sgrehan * If this package is used in a product, Eric Young should be given attribution
1994755Sbenno * as the author of the parts of the library used.
2094755Sbenno * This can be in the form of a textual message at program startup or
2194755Sbenno * in documentation (online or textual) provided with the package.
2294755Sbenno *
2394755Sbenno * Redistribution and use in source and binary forms, with or without
2494755Sbenno * modification, are permitted provided that the following conditions
2594755Sbenno * are met:
2694755Sbenno * 1. Redistributions of source code must retain the copyright
2794755Sbenno *    notice, this list of conditions and the following disclaimer.
2894755Sbenno * 2. Redistributions in binary form must reproduce the above copyright
2994755Sbenno *    notice, this list of conditions and the following disclaimer in the
3094755Sbenno *    documentation and/or other materials provided with the distribution.
3194755Sbenno * 3. All advertising materials mentioning features or use of this software
3294755Sbenno *    must display the following acknowledgement:
3394755Sbenno *    "This product includes cryptographic software written by
3494755Sbenno *     Eric Young (eay@cryptsoft.com)"
35227843Smarius *    The word 'cryptographic' can be left out if the rouines from the library
36227843Smarius *    being used are not cryptographic related :-).
37227843Smarius * 4. If you include any Windows specific code (or a derivative thereof) from
3894755Sbenno *    the apps directory (application code) you must include an acknowledgement:
3994755Sbenno *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
4094755Sbenno *
4194755Sbenno * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
4294755Sbenno * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43256901Snwhitehorn * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44256901Snwhitehorn * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45256901Snwhitehorn * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
4694755Sbenno * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47256901Snwhitehorn * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48256901Snwhitehorn * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49256901Snwhitehorn * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50256855Snwhitehorn * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51212239Smav * SUCH DAMAGE.
5294755Sbenno *
5394755Sbenno * The licence and distribution terms for any publically available version or
5494755Sbenno * derivative of this code cannot be changed.  i.e. this code cannot simply be
5594755Sbenno * copied and put under another distribution licence
5694755Sbenno * [including the GNU Public Licence.]
5794755Sbenno */
58261513Snwhitehorn
59256901Snwhitehorn#include <stdio.h>
60256901Snwhitehorn#include <openssl/bn.h>
61256901Snwhitehorn#include "cryptlib.h"
6294755Sbenno#include "bn_lcl.h"
6394755Sbenno
6494755Sbenno/* The old slow way */
65261513Snwhitehorn#if 0
66256901Snwhitehornint BN_div(BIGNUM *dv, BIGNUM *rem, const BIGNUM *m, const BIGNUM *d,
67256901Snwhitehorn           BN_CTX *ctx)
6894755Sbenno{
6994755Sbenno    int i, nm, nd;
70261513Snwhitehorn    int ret = 0;
71261513Snwhitehorn    BIGNUM *D;
72261513Snwhitehorn
73261513Snwhitehorn    bn_check_top(m);
74261513Snwhitehorn    bn_check_top(d);
7599652Sbenno    if (BN_is_zero(d)) {
76261513Snwhitehorn        BNerr(BN_F_BN_DIV, BN_R_DIV_BY_ZERO);
77261513Snwhitehorn        return (0);
78261513Snwhitehorn    }
79261513Snwhitehorn
80261513Snwhitehorn    if (BN_ucmp(m, d) < 0) {
81261513Snwhitehorn        if (rem != NULL) {
82261513Snwhitehorn            if (BN_copy(rem, m) == NULL)
83261513Snwhitehorn                return (0);
84261513Snwhitehorn        }
85261513Snwhitehorn        if (dv != NULL)
86261513Snwhitehorn            BN_zero(dv);
8799652Sbenno        return (1);
88261513Snwhitehorn    }
89261513Snwhitehorn
90261513Snwhitehorn    BN_CTX_start(ctx);
91261513Snwhitehorn    D = BN_CTX_get(ctx);
92261513Snwhitehorn    if (dv == NULL)
93261513Snwhitehorn        dv = BN_CTX_get(ctx);
9494755Sbenno    if (rem == NULL)
95261513Snwhitehorn        rem = BN_CTX_get(ctx);
96261513Snwhitehorn    if (D == NULL || dv == NULL || rem == NULL)
97261513Snwhitehorn        goto end;
9894755Sbenno
9994755Sbenno    nd = BN_num_bits(d);
10094755Sbenno    nm = BN_num_bits(m);
10194755Sbenno    if (BN_copy(D, d) == NULL)
10294755Sbenno        goto end;
103256901Snwhitehorn    if (BN_copy(rem, m) == NULL)
104261513Snwhitehorn        goto end;
105261513Snwhitehorn
106256901Snwhitehorn    /*
107256901Snwhitehorn     * The next 2 are needed so we can do a dv->d[0]|=1 later since
108261513Snwhitehorn     * BN_lshift1 will only work once there is a value :-)
109212239Smav     */
110261513Snwhitehorn    BN_zero(dv);
111261513Snwhitehorn    if (bn_wexpand(dv, 1) == NULL)
112261513Snwhitehorn        goto end;
113256901Snwhitehorn    dv->top = 1;
114256901Snwhitehorn
115261513Snwhitehorn    if (!BN_lshift(D, D, nm - nd))
116261513Snwhitehorn        goto end;
117261513Snwhitehorn    for (i = nm - nd; i >= 0; i--) {
118261513Snwhitehorn        if (!BN_lshift1(dv, dv))
119261513Snwhitehorn            goto end;
120261513Snwhitehorn        if (BN_ucmp(rem, D) >= 0) {
12194755Sbenno            dv->d[0] |= 1;
122256901Snwhitehorn            if (!BN_usub(rem, rem, D))
123261513Snwhitehorn                goto end;
124256855Snwhitehorn        }
125256855Snwhitehorn/* CAN IMPROVE (and have now :=) */
126256855Snwhitehorn        if (!BN_rshift1(D, D))
127256855Snwhitehorn            goto end;
128256855Snwhitehorn    }
129168885Sgrehan    rem->neg = BN_is_zero(rem) ? 0 : m->neg;
130227843Smarius    dv->neg = m->neg ^ d->neg;
13194755Sbenno    ret = 1;
13294755Sbenno end:
133261513Snwhitehorn    BN_CTX_end(ctx);
134261513Snwhitehorn    return (ret);
135261513Snwhitehorn}
136261513Snwhitehorn
137261513Snwhitehorn#else
138261513Snwhitehorn
139269594Sian# if !defined(OPENSSL_NO_ASM) && !defined(OPENSSL_NO_INLINE_ASM) \
140269594Sian    && !defined(PEDANTIC) && !defined(BN_DIV3W)
141261513Snwhitehorn#  if defined(__GNUC__) && __GNUC__>=2
14294755Sbenno#   if defined(__i386) || defined (__i386__)
143261513Snwhitehorn   /*-
144256901Snwhitehorn    * There were two reasons for implementing this template:
145256901Snwhitehorn    * - GNU C generates a call to a function (__udivdi3 to be exact)
146256901Snwhitehorn    *   in reply to ((((BN_ULLONG)n0)<<BN_BITS2)|n1)/d0 (I fail to
147256901Snwhitehorn    *   understand why...);
148256901Snwhitehorn    * - divl doesn't only calculate quotient, but also leaves
149256901Snwhitehorn    *   remainder in %edx which we can definitely use here:-)
150256901Snwhitehorn    *
151256901Snwhitehorn    *                                   <appro@fy.chalmers.se>
152256901Snwhitehorn    */
153256901Snwhitehorn#    undef bn_div_words
154256901Snwhitehorn#    define bn_div_words(n0,n1,d0)                \
155256901Snwhitehorn        ({  asm volatile (                      \
156256901Snwhitehorn                "divl   %4"                     \
157256901Snwhitehorn                : "=a"(q), "=d"(rem)            \
158256901Snwhitehorn                : "a"(n1), "d"(n0), "g"(d0)     \
159256901Snwhitehorn                : "cc");                        \
160256901Snwhitehorn            q;                                  \
161256901Snwhitehorn        })
162256901Snwhitehorn#    define REMAINDER_IS_ALREADY_CALCULATED
163261513Snwhitehorn#   elif defined(__x86_64) && defined(SIXTY_FOUR_BIT_LONG)
164256901Snwhitehorn   /*
165256901Snwhitehorn    * Same story here, but it's 128-bit by 64-bit division. Wow!
166256901Snwhitehorn    *                                   <appro@fy.chalmers.se>
167256901Snwhitehorn    */
168256901Snwhitehorn#    undef bn_div_words
16994755Sbenno#    define bn_div_words(n0,n1,d0)                \
170261513Snwhitehorn        ({  asm volatile (                      \
171256901Snwhitehorn                "divq   %4"                     \
172256901Snwhitehorn                : "=a"(q), "=d"(rem)            \
173256901Snwhitehorn                : "a"(n1), "d"(n0), "g"(d0)     \
174256901Snwhitehorn                : "cc");                        \
175256901Snwhitehorn            q;                                  \
176256901Snwhitehorn        })
177256901Snwhitehorn#    define REMAINDER_IS_ALREADY_CALCULATED
178256901Snwhitehorn#   endif                       /* __<cpu> */
179256901Snwhitehorn#  endif                        /* __GNUC__ */
180256901Snwhitehorn# endif                         /* OPENSSL_NO_ASM */
181256901Snwhitehorn
182261513Snwhitehorn/*-
183261513Snwhitehorn * BN_div computes  dv := num / divisor,  rounding towards
184261513Snwhitehorn * zero, and sets up rm  such that  dv*divisor + rm = num  holds.
185256901Snwhitehorn * Thus:
186261513Snwhitehorn *     dv->neg == num->neg ^ divisor->neg  (unless the result is zero)
187261513Snwhitehorn *     rm->neg == num->neg                 (unless the remainder is zero)
188261513Snwhitehorn * If 'dv' or 'rm' is NULL, the respective value is not returned.
189261513Snwhitehorn */
190261513Snwhitehornint BN_div(BIGNUM *dv, BIGNUM *rm, const BIGNUM *num, const BIGNUM *divisor,
191265888Snwhitehorn           BN_CTX *ctx)
192261513Snwhitehorn{
193261513Snwhitehorn    int norm_shift, i, loop;
194261513Snwhitehorn    BIGNUM *tmp, wnum, *snum, *sdiv, *res;
195261513Snwhitehorn    BN_ULONG *resp, *wnump;
196261513Snwhitehorn    BN_ULONG d0, d1;
197261513Snwhitehorn    int num_n, div_n;
198256901Snwhitehorn    int no_branch = 0;
199261513Snwhitehorn
20094755Sbenno    /*
201256901Snwhitehorn     * Invalid zero-padding would have particularly bad consequences so don't
202261513Snwhitehorn     * just rely on bn_check_top() here (bn_check_top() works only for
203261513Snwhitehorn     * BN_DEBUG builds)
204178367Smarcel     */
205178367Smarcel    if ((num->top > 0 && num->d[num->top - 1] == 0) ||
206178367Smarcel        (divisor->top > 0 && divisor->d[divisor->top - 1] == 0)) {
207261513Snwhitehorn        BNerr(BN_F_BN_DIV, BN_R_NOT_INITIALIZED);
208178367Smarcel        return 0;
209261513Snwhitehorn    }
210261513Snwhitehorn
211256901Snwhitehorn    bn_check_top(num);
212256901Snwhitehorn    bn_check_top(divisor);
21394755Sbenno
21499652Sbenno    if ((BN_get_flags(num, BN_FLG_CONSTTIME) != 0)
21599652Sbenno        || (BN_get_flags(divisor, BN_FLG_CONSTTIME) != 0)) {
216257075Snwhitehorn        no_branch = 1;
217171805Smarcel    }
218261513Snwhitehorn
219261513Snwhitehorn    bn_check_top(dv);
220261513Snwhitehorn    bn_check_top(rm);
221261513Snwhitehorn    /*- bn_check_top(num); *//*
222261513Snwhitehorn     * 'num' has been checked already
223261513Snwhitehorn     */
224257075Snwhitehorn    /*- bn_check_top(divisor); *//*
225257075Snwhitehorn     * 'divisor' has been checked already
226257075Snwhitehorn     */
227257075Snwhitehorn
228257075Snwhitehorn    if (BN_is_zero(divisor)) {
229257075Snwhitehorn        BNerr(BN_F_BN_DIV, BN_R_DIV_BY_ZERO);
230257075Snwhitehorn        return (0);
231257075Snwhitehorn    }
232257075Snwhitehorn
233171805Smarcel    if (!no_branch && BN_ucmp(num, divisor) < 0) {
234256901Snwhitehorn        if (rm != NULL) {
235256901Snwhitehorn            if (BN_copy(rm, num) == NULL)
236256901Snwhitehorn                return (0);
237256901Snwhitehorn        }
238256901Snwhitehorn        if (dv != NULL)
239256901Snwhitehorn            BN_zero(dv);
240256901Snwhitehorn        return (1);
241124468Sgrehan    }
242256901Snwhitehorn
243256969Snwhitehorn    BN_CTX_start(ctx);
244256901Snwhitehorn    tmp = BN_CTX_get(ctx);
245256969Snwhitehorn    snum = BN_CTX_get(ctx);
24699652Sbenno    sdiv = BN_CTX_get(ctx);
247256901Snwhitehorn    if (dv == NULL)
248256901Snwhitehorn        res = BN_CTX_get(ctx);
249256901Snwhitehorn    else
250256901Snwhitehorn        res = dv;
251261513Snwhitehorn    if (sdiv == NULL || res == NULL || tmp == NULL || snum == NULL)
252256901Snwhitehorn        goto err;
253256901Snwhitehorn
254256901Snwhitehorn    /* First we normalise the numbers */
255256901Snwhitehorn    norm_shift = BN_BITS2 - ((BN_num_bits(divisor)) % BN_BITS2);
256256901Snwhitehorn    if (!(BN_lshift(sdiv, divisor, norm_shift)))
257261513Snwhitehorn        goto err;
258256901Snwhitehorn    sdiv->neg = 0;
259256901Snwhitehorn    norm_shift += BN_BITS2;
260256901Snwhitehorn    if (!(BN_lshift(snum, num, norm_shift)))
26194755Sbenno        goto err;
262178367Smarcel    snum->neg = 0;
26394755Sbenno
26494755Sbenno    if (no_branch) {
265256901Snwhitehorn        /*
266261513Snwhitehorn         * Since we don't know whether snum is larger than sdiv, we pad snum
26794755Sbenno         * with enough zeroes without changing its value.
268256901Snwhitehorn         */
269261513Snwhitehorn        if (snum->top <= sdiv->top + 1) {
27094755Sbenno            if (bn_wexpand(snum, sdiv->top + 2) == NULL)
271256901Snwhitehorn                goto err;
272256901Snwhitehorn            for (i = snum->top; i < sdiv->top + 2; i++)
273256901Snwhitehorn                snum->d[i] = 0;
27494755Sbenno            snum->top = sdiv->top + 2;
275256901Snwhitehorn        } else {
276256901Snwhitehorn            if (bn_wexpand(snum, snum->top + 1) == NULL)
277256901Snwhitehorn                goto err;
278256901Snwhitehorn            snum->d[snum->top] = 0;
279124468Sgrehan            snum->top++;
280256901Snwhitehorn        }
28194755Sbenno    }
28294755Sbenno
283256901Snwhitehorn    div_n = sdiv->top;
284261513Snwhitehorn    num_n = snum->top;
285124468Sgrehan    loop = num_n - div_n;
286256901Snwhitehorn    /*
287124468Sgrehan     * Lets setup a 'window' into snum This is the part that corresponds to
288256901Snwhitehorn     * the current 'area' being divided
289261513Snwhitehorn     */
290256901Snwhitehorn    wnum.neg = 0;
291256901Snwhitehorn    wnum.d = &(snum->d[loop]);
292256901Snwhitehorn    wnum.top = div_n;
293125702Sgrehan    /*
294256901Snwhitehorn     * only needed when BN_ucmp messes up the values between top and max
295261513Snwhitehorn     */
296256901Snwhitehorn    wnum.dmax = snum->dmax - loop; /* so we don't step out of bounds */
297256901Snwhitehorn
298124468Sgrehan    /* Get the top 2 words of sdiv */
299256901Snwhitehorn    /* div_n=sdiv->top; */
300256901Snwhitehorn    d0 = sdiv->d[div_n - 1];
301256855Snwhitehorn    d1 = (div_n == 1) ? 0 : sdiv->d[div_n - 2];
302256901Snwhitehorn
303256901Snwhitehorn    /* pointer to the 'top' of snum */
304125702Sgrehan    wnump = &(snum->d[num_n - 1]);
305256901Snwhitehorn
306256901Snwhitehorn    /* Setup to 'res' */
307261513Snwhitehorn    res->neg = (num->neg ^ divisor->neg);
308256901Snwhitehorn    if (!bn_wexpand(res, (loop + 1)))
309256901Snwhitehorn        goto err;
310124468Sgrehan    res->top = loop - no_branch;
311124468Sgrehan    resp = &(res->d[loop - 1]);
31299652Sbenno
313261513Snwhitehorn    /* space for temp */
31499652Sbenno    if (!bn_wexpand(tmp, (div_n + 1)))
31599652Sbenno        goto err;
316261513Snwhitehorn
317256901Snwhitehorn    if (!no_branch) {
318171805Smarcel        if (BN_ucmp(&wnum, sdiv) >= 0) {
319256901Snwhitehorn            /*
320256901Snwhitehorn             * If BN_DEBUG_RAND is defined BN_ucmp changes (via bn_pollute)
32199652Sbenno             * the const bignum arguments => clean the values between top and
322256901Snwhitehorn             * max again
323256901Snwhitehorn             */
324257075Snwhitehorn            bn_clear_top2max(&wnum);
325256901Snwhitehorn            bn_sub_words(wnum.d, wnum.d, sdiv->d, div_n);
326256901Snwhitehorn            *resp = 1;
327256901Snwhitehorn        } else
328256901Snwhitehorn            res->top--;
329256901Snwhitehorn    }
330256901Snwhitehorn
331256901Snwhitehorn    /*
332256901Snwhitehorn     * if res->top == 0 then clear the neg value otherwise decrease the resp
333256901Snwhitehorn     * pointer
334256901Snwhitehorn     */
335256901Snwhitehorn    if (res->top == 0)
336256901Snwhitehorn        res->neg = 0;
337256901Snwhitehorn    else
338256901Snwhitehorn        resp--;
339256901Snwhitehorn
340256901Snwhitehorn    for (i = 0; i < loop - 1; i++, wnump--, resp--) {
341256901Snwhitehorn        BN_ULONG q, l0;
342256901Snwhitehorn        /*
343256901Snwhitehorn         * the first part of the loop uses the top two words of snum and sdiv
344256901Snwhitehorn         * to calculate a BN_ULONG q such that | wnum - sdiv * q | < sdiv
345256901Snwhitehorn         */
346256901Snwhitehorn# if defined(BN_DIV3W) && !defined(OPENSSL_NO_ASM)
34799652Sbenno        BN_ULONG bn_div_3_words(BN_ULONG *, BN_ULONG, BN_ULONG);
34899652Sbenno        q = bn_div_3_words(wnump, d1, d0);
34999652Sbenno# else
350256901Snwhitehorn        BN_ULONG n0, n1, rem = 0;
351256901Snwhitehorn
352256901Snwhitehorn        n0 = wnump[0];
353171805Smarcel        n1 = wnump[-1];
354256901Snwhitehorn        if (n0 == d0)
355256901Snwhitehorn            q = BN_MASK2;
356256901Snwhitehorn        else {                  /* n0 < d0 */
357256901Snwhitehorn
358256901Snwhitehorn#  ifdef BN_LLONG
359256901Snwhitehorn            BN_ULLONG t2;
360171805Smarcel
36199652Sbenno#   if defined(BN_LLONG) && defined(BN_DIV2W) && !defined(bn_div_words)
362256901Snwhitehorn            q = (BN_ULONG)(((((BN_ULLONG) n0) << BN_BITS2) | n1) / d0);
363256901Snwhitehorn#   else
364256901Snwhitehorn            q = bn_div_words(n0, n1, d0);
365256901Snwhitehorn#    ifdef BN_DEBUG_LEVITTE
366256901Snwhitehorn            fprintf(stderr, "DEBUG: bn_div_words(0x%08X,0x%08X,0x%08\
367256901SnwhitehornX) -> 0x%08X\n", n0, n1, d0, q);
36899652Sbenno#    endif
36999652Sbenno#   endif
37099652Sbenno
37199652Sbenno#   ifndef REMAINDER_IS_ALREADY_CALCULATED
37299652Sbenno            /*
373261513Snwhitehorn             * rem doesn't have to be BN_ULLONG. The least we
374256901Snwhitehorn             * know it's less that d0, isn't it?
37599652Sbenno             */
376261513Snwhitehorn            rem = (n1 - q * d0) & BN_MASK2;
377256901Snwhitehorn#   endif
378261513Snwhitehorn            t2 = (BN_ULLONG) d1 *q;
37999652Sbenno
380261513Snwhitehorn            for (;;) {
381261513Snwhitehorn                if (t2 <= ((((BN_ULLONG) rem) << BN_BITS2) | wnump[-2]))
382261513Snwhitehorn                    break;
383261513Snwhitehorn                q--;
384256901Snwhitehorn                rem += d0;
385256901Snwhitehorn                if (rem < d0)
386256901Snwhitehorn                    break;      /* don't let rem overflow */
387256901Snwhitehorn                t2 -= d1;
388256901Snwhitehorn            }
389256901Snwhitehorn#  else                         /* !BN_LLONG */
390256901Snwhitehorn            BN_ULONG t2l, t2h;
391256901Snwhitehorn
392124468Sgrehan            q = bn_div_words(n0, n1, d0);
39399652Sbenno#   ifdef BN_DEBUG_LEVITTE
394256901Snwhitehorn            fprintf(stderr, "DEBUG: bn_div_words(0x%08X,0x%08X,0x%08\
395256901SnwhitehornX) -> 0x%08X\n", n0, n1, d0, q);
396256901Snwhitehorn#   endif
397256901Snwhitehorn#   ifndef REMAINDER_IS_ALREADY_CALCULATED
398256901Snwhitehorn            rem = (n1 - q * d0) & BN_MASK2;
39999652Sbenno#   endif
40099652Sbenno
401256901Snwhitehorn#   if defined(BN_UMULT_LOHI)
402261513Snwhitehorn            BN_UMULT_LOHI(t2l, t2h, d1, q);
403256901Snwhitehorn#   elif defined(BN_UMULT_HIGH)
40499652Sbenno            t2l = d1 * q;
405256901Snwhitehorn            t2h = BN_UMULT_HIGH(d1, q);
40699652Sbenno#   else
407256901Snwhitehorn            {
408256901Snwhitehorn                BN_ULONG ql, qh;
409256901Snwhitehorn                t2l = LBITS(d1);
410256901Snwhitehorn                t2h = HBITS(d1);
411256855Snwhitehorn                ql = LBITS(q);
412256901Snwhitehorn                qh = HBITS(q);
413256901Snwhitehorn                mul64(t2l, t2h, ql, qh); /* t2=(BN_ULLONG)d1*q; */
41499652Sbenno            }
415256901Snwhitehorn#   endif
416261513Snwhitehorn
417256901Snwhitehorn            for (;;) {
418261513Snwhitehorn                if ((t2h < rem) || ((t2h == rem) && (t2l <= wnump[-2])))
419256901Snwhitehorn                    break;
420256901Snwhitehorn                q--;
421256901Snwhitehorn                rem += d0;
42299652Sbenno                if (rem < d0)
423124468Sgrehan                    break;      /* don't let rem overflow */
424256855Snwhitehorn                if (t2l < d1)
425261513Snwhitehorn                    t2h--;
426168885Sgrehan                t2l -= d1;
427261513Snwhitehorn            }
428168885Sgrehan#  endif                        /* !BN_LLONG */
429256901Snwhitehorn        }
430256901Snwhitehorn# endif                         /* !BN_DIV3W */
431168885Sgrehan
432168885Sgrehan        l0 = bn_mul_words(tmp->d, sdiv->d, div_n, q);
433261513Snwhitehorn        tmp->d[div_n] = l0;
434261513Snwhitehorn        wnum.d--;
435256901Snwhitehorn        /*
436261513Snwhitehorn         * ingore top values of the bignums just sub the two BN_ULONG arrays
437261513Snwhitehorn         * with bn_sub_words
438256901Snwhitehorn         */
439256901Snwhitehorn        if (bn_sub_words(wnum.d, wnum.d, tmp->d, div_n + 1)) {
440256901Snwhitehorn            /*
441256901Snwhitehorn             * Note: As we have considered only the leading two BN_ULONGs in
442256901Snwhitehorn             * the calculation of q, sdiv * q might be greater than wnum (but
443256901Snwhitehorn             * then (q-1) * sdiv is less or equal than wnum)
444256901Snwhitehorn             */
445256901Snwhitehorn            q--;
446256901Snwhitehorn            if (bn_add_words(wnum.d, wnum.d, sdiv->d, div_n))
447256901Snwhitehorn                /*
448256901Snwhitehorn                 * we can't have an overflow here (assuming that q != 0, but
449256901Snwhitehorn                 * if q == 0 then tmp is zero anyway)
450256901Snwhitehorn                 */
451256901Snwhitehorn                (*wnump)++;
452261513Snwhitehorn        }
453256901Snwhitehorn        /* store part of the result */
454256901Snwhitehorn        *resp = q;
455256901Snwhitehorn    }
456256901Snwhitehorn    bn_correct_top(snum);
457256901Snwhitehorn    if (rm != NULL) {
458256901Snwhitehorn        /*
459256901Snwhitehorn         * Keep a copy of the neg flag in num because if rm==num BN_rshift()
460256969Snwhitehorn         * will overwrite it.
461256901Snwhitehorn         */
462256901Snwhitehorn        int neg = num->neg;
463256914Snwhitehorn        BN_rshift(rm, snum, norm_shift);
464256914Snwhitehorn        if (!BN_is_zero(rm))
465256914Snwhitehorn            rm->neg = neg;
466256914Snwhitehorn        bn_check_top(rm);
467256914Snwhitehorn    }
468256914Snwhitehorn    if (no_branch)
469256901Snwhitehorn        bn_correct_top(res);
470256901Snwhitehorn    BN_CTX_end(ctx);
471256901Snwhitehorn    return (1);
472256901Snwhitehorn err:
473256901Snwhitehorn    bn_check_top(rm);
474256901Snwhitehorn    BN_CTX_end(ctx);
475256901Snwhitehorn    return (0);
476256901Snwhitehorn}
477256901Snwhitehorn#endif
478256901Snwhitehorn