get_si.c
Upload User: qaz666999
Upload Date: 2022-08-06
Package Size: 2570k
Code Size: 1k
Category:

Algorithm

Development Platform:

Unix_Linux

  1. /* mpz_get_si(integer) -- Return the least significant digit from INTEGER.
  2. Copyright 1991, 1993, 1994, 1995, 2000, 2001, 2002, 2006 Free Software
  3. Foundation, Inc.
  4. This file is part of the GNU MP Library.
  5. The GNU MP Library is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU Lesser General Public License as published by
  7. the Free Software Foundation; either version 3 of the License, or (at your
  8. option) any later version.
  9. The GNU MP Library is distributed in the hope that it will be useful, but
  10. WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  11. or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
  12. License for more details.
  13. You should have received a copy of the GNU Lesser General Public License
  14. along with the GNU MP Library.  If not, see http://www.gnu.org/licenses/.  */
  15. #include "gmp.h"
  16. #include "gmp-impl.h"
  17. signed long int
  18. mpz_get_si (mpz_srcptr z)
  19. {
  20.   mp_ptr zp = z->_mp_d;
  21.   mp_size_t size = z->_mp_size;
  22.   mp_limb_t zl = zp[0];
  23. #if GMP_NAIL_BITS != 0
  24.   if (ULONG_MAX > GMP_NUMB_MAX && ABS (size) >= 2)
  25.     zl |= zp[1] << GMP_NUMB_BITS;
  26. #endif
  27.   if (size > 0)
  28.     return (long) zl & LONG_MAX;
  29.   else if (size < 0)
  30.     /* This expression is necessary to properly handle 0x80000000 */
  31.     return ~(((long) zl - 1L) & LONG_MAX);
  32.   else
  33.     return 0;
  34. }