beta-0.89.2
[luatex.git] / source / libs / gmp / gmp-src / mpf / fits_s.h
blob25f323e255c6919a53eaf1d1764f998bddbdbc47
1 /* mpf_fits_s*_p -- test whether an mpf fits a C signed type.
3 Copyright 2001, 2002, 2014 Free Software Foundation, Inc.
5 This file is part of the GNU MP Library.
7 The GNU MP Library is free software; you can redistribute it and/or modify
8 it under the terms of either:
10 * the GNU Lesser General Public License as published by the Free
11 Software Foundation; either version 3 of the License, or (at your
12 option) any later version.
16 * the GNU General Public License as published by the Free Software
17 Foundation; either version 2 of the License, or (at your option) any
18 later version.
20 or both in parallel, as here.
22 The GNU MP Library is distributed in the hope that it will be useful, but
23 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
24 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
25 for more details.
27 You should have received copies of the GNU General Public License and the
28 GNU Lesser General Public License along with the GNU MP Library. If not,
29 see https://www.gnu.org/licenses/. */
31 #include "gmp.h"
32 #include "gmp-impl.h"
35 /* Notice this is equivalent to mpz_set_f + mpz_fits_s*_p. */
37 int
38 FUNCTION (mpf_srcptr f) __GMP_NOTHROW
40 mp_size_t fs, fn;
41 mp_srcptr fp;
42 mp_exp_t exp;
43 mp_limb_t fl;
45 exp = EXP(f);
46 if (exp < 1)
47 return 1; /* -1 < f < 1 truncates to zero, so fits */
49 fs = SIZ (f);
50 fp = PTR(f);
51 fn = ABS (fs);
53 if (exp == 1)
55 fl = fp[fn-1];
57 #if GMP_NAIL_BITS != 0
58 else if (exp == 2 && MAXIMUM > GMP_NUMB_MAX)
60 fl = fp[fn-1];
61 if ((fl >> GMP_NAIL_BITS) != 0)
62 return 0;
63 fl = (fl << GMP_NUMB_BITS);
64 if (fn >= 2)
65 fl |= fp[fn-2];
67 #endif
68 else
69 return 0;
71 return fl <= (fs >= 0 ? (mp_limb_t) MAXIMUM : - (mp_limb_t) MINIMUM);