xref: /netbsd-src/external/lgpl3/mpfr/dist/src/nbits_ulong.c (revision ba125506a622fe649968631a56eba5d42ff57863)
1 /* mpfr_nbits_ulong -- number of significant bits in an unsigned long
2    mpfr_nbits_uj    -- number of significant bits in an uintmax_t
3 
4 Copyright 2018-2023 Free Software Foundation, Inc.
5 Contributed by the AriC and Caramba projects, INRIA.
6 
7 This file is part of the GNU MPFR Library.
8 
9 The GNU MPFR Library is free software; you can redistribute it and/or modify
10 it under the terms of the GNU Lesser General Public License as published by
11 the Free Software Foundation; either version 3 of the License, or (at your
12 option) any later version.
13 
14 The GNU MPFR Library is distributed in the hope that it will be useful, but
15 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
16 or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
17 License for more details.
18 
19 You should have received a copy of the GNU Lesser General Public License
20 along with the GNU MPFR Library; see the file COPYING.LESSER.  If not, see
21 https://www.gnu.org/licenses/ or write to the Free Software Foundation, Inc.,
22 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. */
23 
24 #define MPFR_NEED_LONGLONG_H  /* for count_leading_zeros */
25 #define MPFR_NEED_INTMAX_H
26 #include "mpfr-impl.h"
27 
28 /* count the number of significant bits of n, i.e.,
29    nbits(unsigned long) - count_leading_zeros (n) */
30 int
mpfr_nbits_ulong(unsigned long n)31 mpfr_nbits_ulong (unsigned long n)
32 {
33   int cnt;
34 
35   MPFR_ASSERTD (n > 0);
36 
37 #ifdef MPFR_LONG_WITHIN_LIMB
38 
39   count_leading_zeros (cnt, (mp_limb_t) n);
40   cnt = GMP_NUMB_BITS - cnt;
41 
42 #else
43 
44   cnt = 0;
45 
46   while (n >= 0x10000)
47     {
48       n >>= 16;
49       cnt += 16;
50     }
51 
52   MPFR_ASSERTD (n <= 0xffff);
53 
54   if (n >= 0x100)
55     {
56       n >>= 8;
57       cnt += 8;
58     }
59 
60   MPFR_ASSERTD (n <= 0xff);
61 
62   if (n >= 0x10)
63     {
64       n >>= 4;
65       cnt += 4;
66     }
67 
68   MPFR_ASSERTD (n <= 0xf);
69 
70   if (n >= 4)
71     {
72       n >>= 2;
73       cnt += 2;
74     }
75 
76   MPFR_ASSERTD (n <= 3);
77 
78   /* now n = 1, 2, or 3 */
79   cnt += 1 + (n >= 2);
80 
81 #endif
82 
83   MPFR_ASSERTD (cnt >= 0);
84   return cnt;
85 }
86 
87 #ifdef _MPFR_H_HAVE_INTMAX_T
88 /* count the number of significant bits of n, i.e.,
89    nbits(uintmax_t) - count_leading_zeros (n) */
90 int
mpfr_nbits_uj(uintmax_t n)91 mpfr_nbits_uj (uintmax_t n)
92 {
93   int cnt;
94 
95   MPFR_ASSERTD (n > 0);
96 
97   cnt = 0;
98 
99   while (n >= 0x10000)
100     {
101       n >>= 16;
102       cnt += 16;
103     }
104 
105   MPFR_ASSERTD (n <= 0xffff);
106 
107   if (n >= 0x100)
108     {
109       n >>= 8;
110       cnt += 8;
111     }
112 
113   MPFR_ASSERTD (n <= 0xff);
114 
115   if (n >= 0x10)
116     {
117       n >>= 4;
118       cnt += 4;
119     }
120 
121   MPFR_ASSERTD (n <= 0xf);
122 
123   if (n >= 4)
124     {
125       n >>= 2;
126       cnt += 2;
127     }
128 
129   MPFR_ASSERTD (n <= 3);
130 
131   /* now n = 1, 2, or 3 */
132   cnt += 1 + (n >= 2);
133 
134   MPFR_ASSERTD (cnt >= 0);
135   return cnt;
136 }
137 #endif
138