1 /* mpf_get_si -- mpf to long conversion
3 Copyright 2001, 2002, 2004 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 the GNU Lesser General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or (at your
10 option) any later version.
12 The GNU MP Library is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
14 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
15 License for more details.
17 You should have received a copy of the GNU Lesser General Public License
18 along with the GNU MP Library. If not, see http://www.gnu.org/licenses/. */
24 /* Any fraction bits are truncated, meaning simply discarded.
26 For values bigger than a long, the low bits are returned, like
27 mpz_get_si, but this isn't documented.
29 Notice this is equivalent to mpz_set_f + mpz_get_si.
34 fl is established in basically the same way as for mpf_get_ui, see that
35 code for explanations of the conditions.
37 However unlike mpf_get_ui we need an explicit return 0 for exp<=0. When
38 f is a negative fraction (ie. size<0 and exp<=0) we can't let fl==0 go
39 through to the zany final "~ ((fl - 1) & LONG_MAX)", that would give
40 -0x80000000 instead of the desired 0. */
43 mpf_get_si (mpf_srcptr f
) __GMP_NOTHROW
46 mp_size_t size
, abs_size
;
54 /* fraction alone truncates to zero
55 this also covers zero, since we have exp==0 for zero */
59 /* there are some limbs above the radix point */
62 abs_size
= ABS (size
);
64 fl
= fp
[abs_size
-exp
];
66 #if BITS_PER_ULONG > GMP_NUMB_BITS
67 if (exp
> 1 && abs_size
+1 >= exp
)
68 fl
|= fp
[abs_size
- exp
+ 1] << GMP_NUMB_BITS
;
74 /* this form necessary to correctly handle -0x80..00 */
75 return -1 - (long) ((fl
- 1) & LONG_MAX
);