xref: /dflybsd-src/contrib/gmp/mpz/com.c (revision 86d7f5d305c6adaa56ff4582ece9859d73106103)
1*86d7f5d3SJohn Marino /* mpz_com(mpz_ptr dst, mpz_ptr src) -- Assign the bit-complemented value of
2*86d7f5d3SJohn Marino    SRC to DST.
3*86d7f5d3SJohn Marino 
4*86d7f5d3SJohn Marino Copyright 1991, 1993, 1994, 1996, 2001, 2003 Free Software Foundation, Inc.
5*86d7f5d3SJohn Marino 
6*86d7f5d3SJohn Marino This file is part of the GNU MP Library.
7*86d7f5d3SJohn Marino 
8*86d7f5d3SJohn Marino The GNU MP Library is free software; you can redistribute it and/or modify
9*86d7f5d3SJohn Marino it under the terms of the GNU Lesser General Public License as published by
10*86d7f5d3SJohn Marino the Free Software Foundation; either version 3 of the License, or (at your
11*86d7f5d3SJohn Marino option) any later version.
12*86d7f5d3SJohn Marino 
13*86d7f5d3SJohn Marino The GNU MP Library is distributed in the hope that it will be useful, but
14*86d7f5d3SJohn Marino WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
15*86d7f5d3SJohn Marino or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
16*86d7f5d3SJohn Marino License for more details.
17*86d7f5d3SJohn Marino 
18*86d7f5d3SJohn Marino You should have received a copy of the GNU Lesser General Public License
19*86d7f5d3SJohn Marino along with the GNU MP Library.  If not, see http://www.gnu.org/licenses/.  */
20*86d7f5d3SJohn Marino 
21*86d7f5d3SJohn Marino #include "gmp.h"
22*86d7f5d3SJohn Marino #include "gmp-impl.h"
23*86d7f5d3SJohn Marino 
24*86d7f5d3SJohn Marino void
mpz_com(mpz_ptr dst,mpz_srcptr src)25*86d7f5d3SJohn Marino mpz_com (mpz_ptr dst, mpz_srcptr src)
26*86d7f5d3SJohn Marino {
27*86d7f5d3SJohn Marino   mp_size_t size = src->_mp_size;
28*86d7f5d3SJohn Marino   mp_srcptr src_ptr;
29*86d7f5d3SJohn Marino   mp_ptr dst_ptr;
30*86d7f5d3SJohn Marino 
31*86d7f5d3SJohn Marino   if (size >= 0)
32*86d7f5d3SJohn Marino     {
33*86d7f5d3SJohn Marino       /* As with infinite precision: one's complement, two's complement.
34*86d7f5d3SJohn Marino 	 But this can be simplified using the identity -x = ~x + 1.
35*86d7f5d3SJohn Marino 	 So we're going to compute (~~x) + 1 = x + 1!  */
36*86d7f5d3SJohn Marino 
37*86d7f5d3SJohn Marino       if (dst->_mp_alloc < size + 1)
38*86d7f5d3SJohn Marino 	_mpz_realloc (dst, size + 1);
39*86d7f5d3SJohn Marino 
40*86d7f5d3SJohn Marino       src_ptr = src->_mp_d;
41*86d7f5d3SJohn Marino       dst_ptr = dst->_mp_d;
42*86d7f5d3SJohn Marino 
43*86d7f5d3SJohn Marino       if (UNLIKELY (size == 0))
44*86d7f5d3SJohn Marino 	{
45*86d7f5d3SJohn Marino 	  /* special case, as mpn_add_1 wants size!=0 */
46*86d7f5d3SJohn Marino 	  dst_ptr[0] = 1;
47*86d7f5d3SJohn Marino 	  dst->_mp_size = -1;
48*86d7f5d3SJohn Marino 	  return;
49*86d7f5d3SJohn Marino 	}
50*86d7f5d3SJohn Marino 
51*86d7f5d3SJohn Marino       {
52*86d7f5d3SJohn Marino 	mp_limb_t cy;
53*86d7f5d3SJohn Marino 
54*86d7f5d3SJohn Marino 	cy = mpn_add_1 (dst_ptr, src_ptr, size, (mp_limb_t) 1);
55*86d7f5d3SJohn Marino 	if (cy)
56*86d7f5d3SJohn Marino 	  {
57*86d7f5d3SJohn Marino 	    dst_ptr[size] = cy;
58*86d7f5d3SJohn Marino 	    size++;
59*86d7f5d3SJohn Marino 	  }
60*86d7f5d3SJohn Marino       }
61*86d7f5d3SJohn Marino 
62*86d7f5d3SJohn Marino       /* Store a negative size, to indicate ones-extension.  */
63*86d7f5d3SJohn Marino       dst->_mp_size = -size;
64*86d7f5d3SJohn Marino     }
65*86d7f5d3SJohn Marino   else
66*86d7f5d3SJohn Marino     {
67*86d7f5d3SJohn Marino       /* As with infinite precision: two's complement, then one's complement.
68*86d7f5d3SJohn Marino 	 But that can be simplified using the identity -x = ~(x - 1).
69*86d7f5d3SJohn Marino 	 So we're going to compute ~~(x - 1) = x - 1!  */
70*86d7f5d3SJohn Marino       size = -size;
71*86d7f5d3SJohn Marino 
72*86d7f5d3SJohn Marino       if (dst->_mp_alloc < size)
73*86d7f5d3SJohn Marino 	_mpz_realloc (dst, size);
74*86d7f5d3SJohn Marino 
75*86d7f5d3SJohn Marino       src_ptr = src->_mp_d;
76*86d7f5d3SJohn Marino       dst_ptr = dst->_mp_d;
77*86d7f5d3SJohn Marino 
78*86d7f5d3SJohn Marino       mpn_sub_1 (dst_ptr, src_ptr, size, (mp_limb_t) 1);
79*86d7f5d3SJohn Marino       size -= dst_ptr[size - 1] == 0;
80*86d7f5d3SJohn Marino 
81*86d7f5d3SJohn Marino       /* Store a positive size, to indicate zero-extension.  */
82*86d7f5d3SJohn Marino       dst->_mp_size = size;
83*86d7f5d3SJohn Marino     }
84*86d7f5d3SJohn Marino }
85