xref: /netbsd/external/lgpl3/gmp/dist/mpf/iset_si.c (revision 671ea119)
14a1767b4Smrg /* mpf_init_set_si() -- Initialize a float and assign it from a signed int.
24a1767b4Smrg 
3*f81b1c5bSmrg Copyright 1993-1995, 2000, 2001, 2003, 2004, 2012 Free Software Foundation,
4*f81b1c5bSmrg Inc.
54a1767b4Smrg 
64a1767b4Smrg This file is part of the GNU MP Library.
74a1767b4Smrg 
84a1767b4Smrg The GNU MP Library is free software; you can redistribute it and/or modify
9*f81b1c5bSmrg it under the terms of either:
10*f81b1c5bSmrg 
11*f81b1c5bSmrg   * the GNU Lesser General Public License as published by the Free
12*f81b1c5bSmrg     Software Foundation; either version 3 of the License, or (at your
134a1767b4Smrg     option) any later version.
144a1767b4Smrg 
15*f81b1c5bSmrg or
16*f81b1c5bSmrg 
17*f81b1c5bSmrg   * the GNU General Public License as published by the Free Software
18*f81b1c5bSmrg     Foundation; either version 2 of the License, or (at your option) any
19*f81b1c5bSmrg     later version.
20*f81b1c5bSmrg 
21*f81b1c5bSmrg or both in parallel, as here.
22*f81b1c5bSmrg 
234a1767b4Smrg The GNU MP Library is distributed in the hope that it will be useful, but
244a1767b4Smrg WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
25*f81b1c5bSmrg or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
26*f81b1c5bSmrg for more details.
274a1767b4Smrg 
28*f81b1c5bSmrg You should have received copies of the GNU General Public License and the
29*f81b1c5bSmrg GNU Lesser General Public License along with the GNU MP Library.  If not,
30*f81b1c5bSmrg see https://www.gnu.org/licenses/.  */
314a1767b4Smrg 
324a1767b4Smrg #include "gmp-impl.h"
334a1767b4Smrg 
344a1767b4Smrg void
mpf_init_set_si(mpf_ptr r,long int val)354a1767b4Smrg mpf_init_set_si (mpf_ptr r, long int val)
364a1767b4Smrg {
374a1767b4Smrg   mp_size_t prec = __gmp_default_fp_limb_precision;
384a1767b4Smrg   mp_size_t size;
394a1767b4Smrg   mp_limb_t vl;
404a1767b4Smrg 
414a1767b4Smrg   r->_mp_prec = prec;
42*f81b1c5bSmrg   r->_mp_d = __GMP_ALLOCATE_FUNC_LIMBS (prec + 1);
434a1767b4Smrg 
44d25e02daSmrg   vl = (mp_limb_t) ABS_CAST (unsigned long int, val);
454a1767b4Smrg 
464a1767b4Smrg   r->_mp_d[0] = vl & GMP_NUMB_MASK;
474a1767b4Smrg   size = vl != 0;
484a1767b4Smrg 
494a1767b4Smrg #if BITS_PER_ULONG > GMP_NUMB_BITS
504a1767b4Smrg   vl >>= GMP_NUMB_BITS;
514a1767b4Smrg   r->_mp_d[1] = vl;
524a1767b4Smrg   size += (vl != 0);
534a1767b4Smrg #endif
544a1767b4Smrg 
554a1767b4Smrg   r->_mp_exp = size;
564a1767b4Smrg   r->_mp_size = val >= 0 ? size : -size;
574a1767b4Smrg }
58