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

Annotation of OpenXM_contrib/gmp/mpn/generic/diveby3.c, Revision 1.1.1.1

1.1       maekawa     1: /* mpn_divexact_by3 -- mpn division by 3, expecting no remainder. */
                      2:
                      3: /*
                      4: Copyright (C) 2000 Free Software Foundation, Inc.
                      5:
                      6: This file is part of the GNU MP Library.
                      7:
                      8: The GNU MP Library is free software; you can redistribute it and/or modify
                      9: it under the terms of the GNU Lesser General Public License as published by
                     10: the Free Software Foundation; either version 2.1 of the License, or (at your
                     11: option) any later version.
                     12:
                     13: The GNU MP Library is distributed in the hope that it will be useful, but
                     14: WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
                     15: or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
                     16: License for more details.
                     17:
                     18: You should have received a copy of the GNU Lesser General Public License
                     19: along with the GNU MP Library; see the file COPYING.LIB.  If not, write to
                     20: the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
                     21: MA 02111-1307, USA.
                     22: */
                     23:
                     24:
                     25: #include "gmp.h"
                     26: #include "gmp-impl.h"
                     27:
                     28:
                     29: /* Multiplicative inverse of 3, modulo 2^BITS_PER_MP_LIMB.
                     30:    0xAAAAAAAB for 32 bits, 0xAAAAAAAAAAAAAAAB for 64 bits. */
                     31: #define INVERSE_3      ((MP_LIMB_T_MAX / 3) * 2 + 1)
                     32:
                     33:
                     34: /* The "c += ..."s are adding the high limb of 3*l to c.  That high limb
                     35:    will be 0, 1 or 2.  Doing two separate "+="s seems to turn out better
                     36:    code on gcc (as of 2.95.2 at least).
                     37:
                     38:    When a subtraction of a 0,1,2 carry value causes a borrow, that leaves a
                     39:    limb value of either 0xFF...FF or 0xFF...FE and the multiply by INVERSE_3
                     40:    gives 0x55...55 or 0xAA...AA respectively, producing a further borrow of
                     41:    only 0 or 1 respectively.  Hence the carry out of each stage and for the
                     42:    return value is always only 0, 1 or 2.  */
                     43:
                     44: mp_limb_t
                     45: #if __STDC__
                     46: mpn_divexact_by3c (mp_ptr dst, mp_srcptr src, mp_size_t size, mp_limb_t c)
                     47: #else
                     48: mpn_divexact_by3c (dst, src, size, c)
                     49:      mp_ptr    dst;
                     50:      mp_srcptr src;
                     51:      mp_size_t size;
                     52:      mp_limb_t c;
                     53: #endif
                     54: {
                     55:   mp_size_t  i;
                     56:
                     57:   ASSERT (size >= 1);
                     58:
                     59:   i = 0;
                     60:   do
                     61:     {
                     62:       mp_limb_t  l, s;
                     63:
                     64:       s = src[i];
                     65:       l = s - c;
                     66:       c = (l > s);
                     67:
                     68:       l *= INVERSE_3;
                     69:       dst[i] = l;
                     70:
                     71:       c += (l > MP_LIMB_T_MAX/3);
                     72:       c += (l > (MP_LIMB_T_MAX/3)*2);
                     73:     }
                     74:   while (++i < size);
                     75:
                     76:   return c;
                     77: }

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