xref: /dflybsd-src/contrib/gmp/mpz/com.c (revision 86d7f5d305c6adaa56ff4582ece9859d73106103)
186d7f5d3SJohn Marino /* mpz_com(mpz_ptr dst, mpz_ptr src) -- Assign the bit-complemented value of
286d7f5d3SJohn Marino    SRC to DST.
386d7f5d3SJohn Marino 
486d7f5d3SJohn Marino Copyright 1991, 1993, 1994, 1996, 2001, 2003 Free Software Foundation, Inc.
586d7f5d3SJohn Marino 
686d7f5d3SJohn Marino This file is part of the GNU MP Library.
786d7f5d3SJohn Marino 
886d7f5d3SJohn Marino The GNU MP Library is free software; you can redistribute it and/or modify
986d7f5d3SJohn Marino it under the terms of the GNU Lesser General Public License as published by
1086d7f5d3SJohn Marino the Free Software Foundation; either version 3 of the License, or (at your
1186d7f5d3SJohn Marino option) any later version.
1286d7f5d3SJohn Marino 
1386d7f5d3SJohn Marino The GNU MP Library is distributed in the hope that it will be useful, but
1486d7f5d3SJohn Marino WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
1586d7f5d3SJohn Marino or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
1686d7f5d3SJohn Marino License for more details.
1786d7f5d3SJohn Marino 
1886d7f5d3SJohn Marino You should have received a copy of the GNU Lesser General Public License
1986d7f5d3SJohn Marino along with the GNU MP Library.  If not, see http://www.gnu.org/licenses/.  */
2086d7f5d3SJohn Marino 
2186d7f5d3SJohn Marino #include "gmp.h"
2286d7f5d3SJohn Marino #include "gmp-impl.h"
2386d7f5d3SJohn Marino 
2486d7f5d3SJohn Marino void
mpz_com(mpz_ptr dst,mpz_srcptr src)2586d7f5d3SJohn Marino mpz_com (mpz_ptr dst, mpz_srcptr src)
2686d7f5d3SJohn Marino {
2786d7f5d3SJohn Marino   mp_size_t size = src->_mp_size;
2886d7f5d3SJohn Marino   mp_srcptr src_ptr;
2986d7f5d3SJohn Marino   mp_ptr dst_ptr;
3086d7f5d3SJohn Marino 
3186d7f5d3SJohn Marino   if (size >= 0)
3286d7f5d3SJohn Marino     {
3386d7f5d3SJohn Marino       /* As with infinite precision: one's complement, two's complement.
3486d7f5d3SJohn Marino 	 But this can be simplified using the identity -x = ~x + 1.
3586d7f5d3SJohn Marino 	 So we're going to compute (~~x) + 1 = x + 1!  */
3686d7f5d3SJohn Marino 
3786d7f5d3SJohn Marino       if (dst->_mp_alloc < size + 1)
3886d7f5d3SJohn Marino 	_mpz_realloc (dst, size + 1);
3986d7f5d3SJohn Marino 
4086d7f5d3SJohn Marino       src_ptr = src->_mp_d;
4186d7f5d3SJohn Marino       dst_ptr = dst->_mp_d;
4286d7f5d3SJohn Marino 
4386d7f5d3SJohn Marino       if (UNLIKELY (size == 0))
4486d7f5d3SJohn Marino 	{
4586d7f5d3SJohn Marino 	  /* special case, as mpn_add_1 wants size!=0 */
4686d7f5d3SJohn Marino 	  dst_ptr[0] = 1;
4786d7f5d3SJohn Marino 	  dst->_mp_size = -1;
4886d7f5d3SJohn Marino 	  return;
4986d7f5d3SJohn Marino 	}
5086d7f5d3SJohn Marino 
5186d7f5d3SJohn Marino       {
5286d7f5d3SJohn Marino 	mp_limb_t cy;
5386d7f5d3SJohn Marino 
5486d7f5d3SJohn Marino 	cy = mpn_add_1 (dst_ptr, src_ptr, size, (mp_limb_t) 1);
5586d7f5d3SJohn Marino 	if (cy)
5686d7f5d3SJohn Marino 	  {
5786d7f5d3SJohn Marino 	    dst_ptr[size] = cy;
5886d7f5d3SJohn Marino 	    size++;
5986d7f5d3SJohn Marino 	  }
6086d7f5d3SJohn Marino       }
6186d7f5d3SJohn Marino 
6286d7f5d3SJohn Marino       /* Store a negative size, to indicate ones-extension.  */
6386d7f5d3SJohn Marino       dst->_mp_size = -size;
6486d7f5d3SJohn Marino     }
6586d7f5d3SJohn Marino   else
6686d7f5d3SJohn Marino     {
6786d7f5d3SJohn Marino       /* As with infinite precision: two's complement, then one's complement.
6886d7f5d3SJohn Marino 	 But that can be simplified using the identity -x = ~(x - 1).
6986d7f5d3SJohn Marino 	 So we're going to compute ~~(x - 1) = x - 1!  */
7086d7f5d3SJohn Marino       size = -size;
7186d7f5d3SJohn Marino 
7286d7f5d3SJohn Marino       if (dst->_mp_alloc < size)
7386d7f5d3SJohn Marino 	_mpz_realloc (dst, size);
7486d7f5d3SJohn Marino 
7586d7f5d3SJohn Marino       src_ptr = src->_mp_d;
7686d7f5d3SJohn Marino       dst_ptr = dst->_mp_d;
7786d7f5d3SJohn Marino 
7886d7f5d3SJohn Marino       mpn_sub_1 (dst_ptr, src_ptr, size, (mp_limb_t) 1);
7986d7f5d3SJohn Marino       size -= dst_ptr[size - 1] == 0;
8086d7f5d3SJohn Marino 
8186d7f5d3SJohn Marino       /* Store a positive size, to indicate zero-extension.  */
8286d7f5d3SJohn Marino       dst->_mp_size = size;
8386d7f5d3SJohn Marino     }
8486d7f5d3SJohn Marino }
85