]> www.ginac.de Git - cln.git/blob - src/integer/bitwise/cl_I_logcount.cc
2006-04-25 Bruno Haible <bruno@clisp.org>
[cln.git] / src / integer / bitwise / cl_I_logcount.cc
1 // logcount().
2
3 // General includes.
4 #include "cl_sysdep.h"
5
6 // Specification.
7 #include "cln/integer.h"
8
9
10 // Implementation.
11
12 #include "cl_I.h"
13 #include "cl_DS.h"
14 #include "cl_D.h"
15 #include "cl_low.h"
16
17 namespace cln {
18
19 uintC logcount (const cl_I& x)
20 {
21         if (fixnump(x))
22           { var uintV x32 = FN_to_V(x); // x als intDsize-Bit-Zahl
23             if (FN_V_minusp(x,(sintV)x32)) { x32 = ~ x32; } // falls <0, komplementieren
24             #if (intVsize>32)
25             #define x64 x32
26             logcount_64(); // Bits von x32 zählen
27             #undef x64
28             #else
29             logcount_32(); // Bits von x32 zählen
30             #endif
31             return x32;
32           }
33           else
34           { var const uintD* MSDptr;
35             var uintC len;
36             BN_to_NDS_nocopy(x, MSDptr=,len=,); // DS zu x bilden, len>0.
37             var uintC bitcount = 0; // Bitzähler
38             var const uintD* ptr = MSDptr; // läuft durch die Digits durch
39             var uintD sign = sign_of_sintD(mspref(ptr,0)); // Vorzeichen
40             dotimespC(len,len,
41               { bitcount += (uintC)logcountD(msprefnext(ptr) ^ sign); });
42             // 0 <= bitcount < intDsize*2^intCsize.
43             return bitcount;
44           }
45 }
46
47 }  // namespace cln