xref: /dragonfly/contrib/gmp/mpz/cmp_si.c (revision d2d4b659)
14b6a78b7SSimon Schubert /* mpz_cmp_si(u,v) -- Compare an integer U with a single-word int V.
24b6a78b7SSimon Schubert    Return positive, zero, or negative based on if U > V, U == V, or U < V.
34b6a78b7SSimon Schubert 
44b6a78b7SSimon Schubert Copyright 1991, 1993, 1994, 1995, 1996, 2000, 2001, 2002 Free Software
54b6a78b7SSimon Schubert Foundation, Inc.
64b6a78b7SSimon Schubert 
74b6a78b7SSimon Schubert This file is part of the GNU MP Library.
84b6a78b7SSimon Schubert 
94b6a78b7SSimon Schubert The GNU MP Library is free software; you can redistribute it and/or modify
104b6a78b7SSimon Schubert it under the terms of the GNU Lesser General Public License as published by
114b6a78b7SSimon Schubert the Free Software Foundation; either version 3 of the License, or (at your
124b6a78b7SSimon Schubert option) any later version.
134b6a78b7SSimon Schubert 
144b6a78b7SSimon Schubert The GNU MP Library is distributed in the hope that it will be useful, but
154b6a78b7SSimon Schubert WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
164b6a78b7SSimon Schubert or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
174b6a78b7SSimon Schubert License for more details.
184b6a78b7SSimon Schubert 
194b6a78b7SSimon Schubert You should have received a copy of the GNU Lesser General Public License
204b6a78b7SSimon Schubert along with the GNU MP Library.  If not, see http://www.gnu.org/licenses/.  */
214b6a78b7SSimon Schubert 
224b6a78b7SSimon Schubert #include "gmp.h"
234b6a78b7SSimon Schubert #include "gmp-impl.h"
244b6a78b7SSimon Schubert 
254b6a78b7SSimon Schubert int
_mpz_cmp_si(mpz_srcptr u,signed long int v_digit)26*d2d4b659SJohn Marino _mpz_cmp_si (mpz_srcptr u, signed long int v_digit) __GMP_NOTHROW
274b6a78b7SSimon Schubert {
284b6a78b7SSimon Schubert   mp_size_t usize = u->_mp_size;
294b6a78b7SSimon Schubert   mp_size_t vsize;
304b6a78b7SSimon Schubert   mp_limb_t u_digit;
31*d2d4b659SJohn Marino   unsigned long int absv_digit = (unsigned long int) v_digit;
324b6a78b7SSimon Schubert 
334b6a78b7SSimon Schubert #if GMP_NAIL_BITS != 0
344b6a78b7SSimon Schubert   /* FIXME.  This isn't very pretty.  */
354b6a78b7SSimon Schubert   mpz_t tmp;
364b6a78b7SSimon Schubert   mp_limb_t tt[2];
374b6a78b7SSimon Schubert   PTR(tmp) = tt;
384b6a78b7SSimon Schubert   ALLOC(tmp) = 2;
394b6a78b7SSimon Schubert   mpz_set_si (tmp, v_digit);
404b6a78b7SSimon Schubert   return mpz_cmp (u, tmp);
414b6a78b7SSimon Schubert #endif
424b6a78b7SSimon Schubert 
434b6a78b7SSimon Schubert   vsize = 0;
444b6a78b7SSimon Schubert   if (v_digit > 0)
454b6a78b7SSimon Schubert     vsize = 1;
464b6a78b7SSimon Schubert   else if (v_digit < 0)
474b6a78b7SSimon Schubert     {
484b6a78b7SSimon Schubert       vsize = -1;
49*d2d4b659SJohn Marino       absv_digit = -absv_digit;
504b6a78b7SSimon Schubert     }
514b6a78b7SSimon Schubert 
524b6a78b7SSimon Schubert   if (usize != vsize)
534b6a78b7SSimon Schubert     return usize - vsize;
544b6a78b7SSimon Schubert 
554b6a78b7SSimon Schubert   if (usize == 0)
564b6a78b7SSimon Schubert     return 0;
574b6a78b7SSimon Schubert 
584b6a78b7SSimon Schubert   u_digit = u->_mp_d[0];
594b6a78b7SSimon Schubert 
60*d2d4b659SJohn Marino   if (u_digit == (mp_limb_t) absv_digit)
614b6a78b7SSimon Schubert     return 0;
624b6a78b7SSimon Schubert 
63*d2d4b659SJohn Marino   if (u_digit > (mp_limb_t) absv_digit)
644b6a78b7SSimon Schubert     return usize;
654b6a78b7SSimon Schubert   else
664b6a78b7SSimon Schubert     return -usize;
674b6a78b7SSimon Schubert }
68