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

Algorithm

Development Platform:

Unix_Linux

  1. /* mpz_set_si(dest,val) -- Assign DEST with a small value VAL.
  2. Copyright 1991, 1993, 1994, 1995, 2000, 2001, 2002 Free Software Foundation,
  3. 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. void
  18. mpz_set_si (mpz_ptr dest, signed long int val)
  19. {
  20.   mp_size_t size;
  21.   mp_limb_t vl;
  22.   vl = (mp_limb_t) (unsigned long int) (val >= 0 ? val : -val);
  23.   dest->_mp_d[0] = vl & GMP_NUMB_MASK;
  24.   size = vl != 0;
  25. #if GMP_NAIL_BITS != 0
  26.   if (vl > GMP_NUMB_MAX)
  27.     {
  28.       MPZ_REALLOC (dest, 2);
  29.       dest->_mp_d[1] = vl >> GMP_NUMB_BITS;
  30.       size = 2;
  31.     }
  32. #endif
  33.   dest->_mp_size = val >= 0 ? size : -size;
  34. }