beta-0.89.2
[luatex.git] / source / libs / gmp / gmp-src / mpf / get_ui.c
blobeb9b30e69c066e8a94f8b7a6742a170ef0dd79f9
1 /* mpf_get_ui -- mpf to ulong 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 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 /* Any fraction bits are truncated, meaning simply discarded.
37 For values bigger than a ulong, the low bits are returned (the low
38 absolute value bits actually), like mpz_get_ui, but this isn't
39 documented.
41 Notice this is equivalent to mpz_set_f + mpz_get_ui.
44 Implementation:
46 The limb just above the radix point for us to extract is ptr[size-exp].
48 We need to check that the size-exp index falls in our available data
49 range, 0 to size-1 inclusive. We test this without risk of an overflow
50 involving exp by requiring size>=exp (giving size-exp >= 0) and exp>0
51 (giving size-exp <= size-1).
53 Notice if size==0 there's no fetch, since of course size>=exp and exp>0
54 can only be true if size>0. So there's no special handling for size==0,
55 it comes out as 0 the same as any other time we have no data at our
56 target index.
58 For nails, the second limb above the radix point is also required, this
59 is ptr[size-exp+1].
61 Again we need to check that size-exp+1 falls in our data range, 0 to
62 size-1 inclusive. We test without risk of overflow by requiring
63 size+1>=exp (giving size-exp+1 >= 0) and exp>1 (giving size-exp+1 <=
64 size-1).
66 And again if size==0 these second fetch conditions are not satisfied
67 either since size+1>=exp and exp>1 are only true if size>0.
69 The code is arranged with exp>0 wrapping the exp>1 test since exp>1 is
70 mis-compiled by alpha gcc prior to version 3.4. It re-writes it as
71 exp-1>0, which is incorrect when exp==MP_EXP_T_MIN. By having exp>0
72 tested first we ensure MP_EXP_T_MIN doesn't reach exp>1. */
74 unsigned long
75 mpf_get_ui (mpf_srcptr f) __GMP_NOTHROW
77 mp_size_t size;
78 mp_exp_t exp;
79 mp_srcptr fp;
80 mp_limb_t fl;
82 exp = EXP (f);
83 size = SIZ (f);
84 fp = PTR (f);
86 fl = 0;
87 if (exp > 0)
89 /* there are some limbs above the radix point */
91 size = ABS (size);
92 if (size >= exp)
93 fl = fp[size-exp];
95 #if BITS_PER_ULONG > GMP_NUMB_BITS
96 if (exp > 1 && size+1 >= exp)
97 fl += (fp[size-exp+1] << GMP_NUMB_BITS);
98 #endif
101 return (unsigned long) fl;