]> www.ginac.de Git - cln.git/blob - src/integer/bitwise/cl_I_logcount.cc
* All Files have been modified for inclusion of namespace cln;
[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 uintL logcount (const cl_I& x)
20 {
21         if (fixnump(x))
22           { var uint32 x32 = FN_to_L(x); // x als 32-Bit-Zahl
23             if (FN_L_minusp(x,(sint32)x32)) { x32 = ~ x32; } // falls <0, komplementieren
24             logcount_32(); // Bits von x32 zählen
25             return x32;
26           }
27           else
28           { var const uintD* MSDptr;
29             var uintC len;
30             BN_to_NDS_nocopy(x, MSDptr=,len=,); // DS zu x bilden, len>0.
31             var uintL bitcount = 0; // Bitzähler
32             var const uintD* ptr = MSDptr; // läuft durch die Digits durch
33             var uintD sign = sign_of_sintD(mspref(ptr,0)); // Vorzeichen
34             dotimespC(len,len,
35               { bitcount += (uintL)logcountD(msprefnext(ptr) ^ sign); });
36             // 0 <= bitcount < intDsize*2^intCsize.
37             return bitcount;
38           }
39 }
40
41 }  // namespace cln