mpir/mpz/set_ux.c
gladman 57ff0963b9
2011-03-14 15:51:54 +00:00

52 lines
1.5 KiB
C

/*
mpz_set_ux(z, v) -- Set z to the uintmax_t value v
Copyright 2011 Brian Gladman
This file is part of the GNU MP Library.
The GNU MP Library is free software; you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 2.1 of the License, or (at your
option) any later version.
The GNU MP Library is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
License for more details.
You should have received a copy of the GNU Lesser General Public License
along with the GNU MP Library; see the file COPYING.LIB. If not, write to
the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
MA 02110-1301, USA. */
#define HAVE_STDINT_H
#include "mpir.h"
#include "gmp-impl.h"
#ifdef HAVE_UINTMAX_T
#define NLIMBS ((8 * SIZEOF_UINTMAX + GMP_NUMB_BITS - 1) / GMP_NUMB_BITS)
void
mpz_set_ux (mpz_ptr z, uintmax_t v)
{ uintmax_t i;
#if GMP_NUMB_BITS >= 8 * SIZEOF_UINTMAX
z->_mp_d[0] = (mp_limb_t)v;
z->_mp_size = (v ? NLIMBS : 0);
#else
if(NLIMBS > z->_mp_alloc)
MPZ_REALLOC(z, NLIMBS);
for( i = 0 ; i < NLIMBS && v ; ++i )
{
z->_mp_d[i] = v & GMP_NUMB_MASK;
v >>= GMP_NUMB_BITS;
}
z->_mp_size = (v ? i : 0);
#endif
}
#endif