[BACK]Return to udiv_w_sdiv.c CVS log [TXT][DIR] Up to [local] / OpenXM_contrib / gmp / mpn / generic

Annotation of OpenXM_contrib/gmp/mpn/generic/udiv_w_sdiv.c, Revision 1.1.1.3

1.1       maekawa     1: /* mpn_udiv_w_sdiv -- implement udiv_qrnnd on machines with only signed
                      2:    division.
                      3:
                      4:    Contributed by Peter L. Montgomery.
                      5:
1.1.1.2   maekawa     6:    THIS IS AN INTERNAL FUNCTION WITH A MUTABLE INTERFACE.  IT IS ONLY SAFE
                      7:    TO REACH THIS FUNCTION THROUGH DOCUMENTED INTERFACES.  IN FACT, IT IS
                      8:    ALMOST GUARANTEED THAT THIS FUNCTION WILL CHANGE OR DISAPPEAR IN A FUTURE
                      9:    GNU MP RELEASE.
                     10:
                     11:
1.1.1.3 ! ohara      12: Copyright 1992, 1994, 1996, 2000 Free Software Foundation, Inc.
1.1       maekawa    13:
                     14: This file is part of the GNU MP Library.
                     15:
                     16: The GNU MP Library is free software; you can redistribute it and/or modify
1.1.1.2   maekawa    17: it under the terms of the GNU Lesser General Public License as published by
                     18: the Free Software Foundation; either version 2.1 of the License, or (at your
1.1       maekawa    19: option) any later version.
                     20:
                     21: The GNU MP Library is distributed in the hope that it will be useful, but
                     22: WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
1.1.1.2   maekawa    23: or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
1.1       maekawa    24: License for more details.
                     25:
1.1.1.2   maekawa    26: You should have received a copy of the GNU Lesser General Public License
1.1       maekawa    27: along with the GNU MP Library; see the file COPYING.LIB.  If not, write to
                     28: the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
                     29: MA 02111-1307, USA. */
                     30:
                     31: #include "gmp.h"
                     32: #include "gmp-impl.h"
                     33: #include "longlong.h"
                     34:
                     35: mp_limb_t
                     36: mpn_udiv_w_sdiv (rp, a1, a0, d)
                     37:      mp_limb_t *rp, a1, a0, d;
                     38: {
                     39:   mp_limb_t q, r;
                     40:   mp_limb_t c0, c1, b1;
1.1.1.3 ! ohara      41:
        !            42:   ASSERT (d != 0);
        !            43:   ASSERT (a1 < d);
1.1       maekawa    44:
                     45:   if ((mp_limb_signed_t) d >= 0)
                     46:     {
                     47:       if (a1 < d - a1 - (a0 >> (BITS_PER_MP_LIMB - 1)))
                     48:        {
                     49:          /* dividend, divisor, and quotient are nonnegative */
                     50:          sdiv_qrnnd (q, r, a1, a0, d);
                     51:        }
                     52:       else
                     53:        {
                     54:          /* Compute c1*2^32 + c0 = a1*2^32 + a0 - 2^31*d */
                     55:          sub_ddmmss (c1, c0, a1, a0, d >> 1, d << (BITS_PER_MP_LIMB - 1));
                     56:          /* Divide (c1*2^32 + c0) by d */
                     57:          sdiv_qrnnd (q, r, c1, c0, d);
                     58:          /* Add 2^31 to quotient */
                     59:          q += (mp_limb_t) 1 << (BITS_PER_MP_LIMB - 1);
                     60:        }
                     61:     }
                     62:   else
                     63:     {
                     64:       b1 = d >> 1;                     /* d/2, between 2^30 and 2^31 - 1 */
                     65:       c1 = a1 >> 1;                    /* A/2 */
                     66:       c0 = (a1 << (BITS_PER_MP_LIMB - 1)) + (a0 >> 1);
                     67:
                     68:       if (a1 < b1)                     /* A < 2^32*b1, so A/2 < 2^31*b1 */
                     69:        {
                     70:          sdiv_qrnnd (q, r, c1, c0, b1); /* (A/2) / (d/2) */
                     71:
                     72:          r = 2*r + (a0 & 1);           /* Remainder from A/(2*b1) */
                     73:          if ((d & 1) != 0)
                     74:            {
                     75:              if (r >= q)
                     76:                r = r - q;
                     77:              else if (q - r <= d)
                     78:                {
                     79:                  r = r - q + d;
                     80:                  q--;
                     81:                }
                     82:              else
                     83:                {
                     84:                  r = r - q + 2*d;
                     85:                  q -= 2;
                     86:                }
                     87:            }
                     88:        }
                     89:       else if (c1 < b1)                        /* So 2^31 <= (A/2)/b1 < 2^32 */
                     90:        {
                     91:          c1 = (b1 - 1) - c1;
                     92:          c0 = ~c0;                     /* logical NOT */
                     93:
                     94:          sdiv_qrnnd (q, r, c1, c0, b1); /* (A/2) / (d/2) */
                     95:
                     96:          q = ~q;                       /* (A/2)/b1 */
                     97:          r = (b1 - 1) - r;
                     98:
                     99:          r = 2*r + (a0 & 1);           /* A/(2*b1) */
                    100:
                    101:          if ((d & 1) != 0)
                    102:            {
                    103:              if (r >= q)
                    104:                r = r - q;
                    105:              else if (q - r <= d)
                    106:                {
                    107:                  r = r - q + d;
                    108:                  q--;
                    109:                }
                    110:              else
                    111:                {
                    112:                  r = r - q + 2*d;
                    113:                  q -= 2;
                    114:                }
                    115:            }
                    116:        }
                    117:       else                             /* Implies c1 = b1 */
                    118:        {                               /* Hence a1 = d - 1 = 2*b1 - 1 */
                    119:          if (a0 >= -d)
                    120:            {
                    121:              q = -1;
                    122:              r = a0 + d;
                    123:            }
                    124:          else
                    125:            {
                    126:              q = -2;
                    127:              r = a0 + 2*d;
                    128:            }
                    129:        }
                    130:     }
                    131:
                    132:   *rp = r;
                    133:   return q;
                    134: }

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>