xref: /netbsd-src/external/lgpl3/gmp/dist/mpf/get_ui.c (revision e6c7e151de239c49d2e38720a061ed9d1fa99309)
1 /* mpf_get_ui -- mpf to ulong conversion
2 
3 Copyright 2001, 2002, 2004 Free Software Foundation, Inc.
4 
5 This file is part of the GNU MP Library.
6 
7 The GNU MP Library is free software; you can redistribute it and/or modify
8 it under the terms of either:
9 
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.
13 
14 or
15 
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.
19 
20 or both in parallel, as here.
21 
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.
26 
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/.  */
30 
31 #include "gmp.h"
32 #include "gmp-impl.h"
33 
34 
35 /* Any fraction bits are truncated, meaning simply discarded.
36 
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.
40 
41    Notice this is equivalent to mpz_set_f + mpz_get_ui.
42 
43 
44    Implementation:
45 
46    The limb just above the radix point for us to extract is ptr[size-exp].
47 
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).
52 
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.
57 
58    For nails, the second limb above the radix point is also required, this
59    is ptr[size-exp+1].
60 
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).
65 
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.
68 
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.  */
73 
74 unsigned long
75 mpf_get_ui (mpf_srcptr f) __GMP_NOTHROW
76 {
77   mp_size_t size;
78   mp_exp_t exp;
79   mp_srcptr fp;
80   mp_limb_t fl;
81 
82   exp = EXP (f);
83   size = SIZ (f);
84   fp = PTR (f);
85 
86   fl = 0;
87   if (exp > 0)
88     {
89       /* there are some limbs above the radix point */
90 
91       size = ABS (size);
92       if (size >= exp)
93         fl = fp[size-exp];
94 
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
99     }
100 
101   return (unsigned long) fl;
102 }
103