1 /* mpz_com(mpz_ptr dst, mpz_ptr src) -- Assign the bit-complemented value of
4 Copyright (C) 1991, 1993, 1994, 1996 Free Software Foundation, Inc.
6 This file is part of the GNU MP Library.
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.
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.
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. */
28 mpz_com (mpz_ptr dst, mpz_srcptr src)
35 mp_size_t size = src->_mp_size;
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! */
45 if (dst->_mp_alloc < size + 1)
46 _mpz_realloc (dst, size + 1);
53 /* Special case, as mpn_add wants the first arg's size >= the
63 cy = mpn_add_1 (dst_ptr, src_ptr, size, (mp_limb_t) 1);
71 /* Store a negative size, to indicate ones-extension. */
72 dst->_mp_size = -size;
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! */
81 if (dst->_mp_alloc < size)
82 _mpz_realloc (dst, size);
87 mpn_sub_1 (dst_ptr, src_ptr, size, (mp_limb_t) 1);
88 size -= dst_ptr[size - 1] == 0;
90 /* Store a positive size, to indicate zero-extension. */