[BACK]Return to com.c CVS log [TXT][DIR] Up to [local] / OpenXM_contrib / gmp / mpz

Annotation of OpenXM_contrib/gmp/mpz/com.c, Revision 1.1.1.1

1.1       maekawa     1: /* mpz_com(mpz_ptr dst, mpz_ptr src) -- Assign the bit-complemented value of
                      2:    SRC to DST.
                      3:
                      4: Copyright (C) 1991, 1993, 1994, 1996 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 Library General Public License as published by
                     10: the Free Software Foundation; either version 2 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 Library General Public
                     16: License for more details.
                     17:
                     18: You should have received a copy of the GNU Library 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: #include "gmp.h"
                     24: #include "gmp-impl.h"
                     25:
                     26: void
                     27: #if __STDC__
                     28: mpz_com (mpz_ptr dst, mpz_srcptr src)
                     29: #else
                     30: mpz_com (dst, src)
                     31:      mpz_ptr dst;
                     32:      mpz_srcptr src;
                     33: #endif
                     34: {
                     35:   mp_size_t size = src->_mp_size;
                     36:   mp_srcptr src_ptr;
                     37:   mp_ptr dst_ptr;
                     38:
                     39:   if (size >= 0)
                     40:     {
                     41:       /* As with infinite precision: one's complement, two's complement.
                     42:         But this can be simplified using the identity -x = ~x + 1.
                     43:         So we're going to compute (~~x) + 1 = x + 1!  */
                     44:
                     45:       if (dst->_mp_alloc < size + 1)
                     46:        _mpz_realloc (dst, size + 1);
                     47:
                     48:       src_ptr = src->_mp_d;
                     49:       dst_ptr = dst->_mp_d;
                     50:
                     51:       if (size == 0)
                     52:        {
                     53:          /* Special case, as mpn_add wants the first arg's size >= the
                     54:             second arg's size.  */
                     55:          dst_ptr[0] = 1;
                     56:          dst->_mp_size = -1;
                     57:          return;
                     58:        }
                     59:
                     60:       {
                     61:        mp_limb_t cy;
                     62:
                     63:        cy = mpn_add_1 (dst_ptr, src_ptr, size, (mp_limb_t) 1);
                     64:        if (cy)
                     65:          {
                     66:            dst_ptr[size] = cy;
                     67:            size++;
                     68:          }
                     69:       }
                     70:
                     71:       /* Store a negative size, to indicate ones-extension.  */
                     72:       dst->_mp_size = -size;
                     73:     }
                     74:   else
                     75:     {
                     76:       /* As with infinite precision: two's complement, then one's complement.
                     77:         But that can be simplified using the identity -x = ~(x - 1).
                     78:         So we're going to compute ~~(x - 1) = x - 1!  */
                     79:       size = -size;
                     80:
                     81:       if (dst->_mp_alloc < size)
                     82:        _mpz_realloc (dst, size);
                     83:
                     84:       src_ptr = src->_mp_d;
                     85:       dst_ptr = dst->_mp_d;
                     86:
                     87:       mpn_sub_1 (dst_ptr, src_ptr, size, (mp_limb_t) 1);
                     88:       size -= dst_ptr[size - 1] == 0;
                     89:
                     90:       /* Store a positive size, to indicate zero-extension.  */
                     91:       dst->_mp_size = size;
                     92:     }
                     93: }

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