]> www.ginac.de Git - cln.git/blob - src/integer/bitwise/cl_I_ilength.cc
Initial revision
[cln.git] / src / integer / bitwise / cl_I_ilength.cc
1 // integer_length().
2
3 // General includes.
4 #include "cl_sysdep.h"
5
6 // Specification.
7 #include "cl_integer.h"
8
9
10 // Implementation.
11
12 #include "cl_I.h"
13 #include "cl_DS.h"
14
15 uintL integer_length (const cl_I& x)
16 {
17         if (fixnump(x))
18           { var uintL bitcount = 0;
19             var uint32 x_ = FN_to_L(x); // x als 32-Bit-Zahl
20             if (FN_L_minusp(x,(sint32)x_)) { x_ = ~ x_; } // falls <0, komplementieren
21             if (!(x_==0)) { integerlength32(x_,bitcount=); }
22             return bitcount; // 0 <= bitcount < 32.
23           }
24           else
25           { var const uintD* MSDptr;
26             var uintC len;
27             BN_to_NDS_nocopy(x, MSDptr=,len=,); // normalisierte DS zu x bilden.
28             var uintL bitcount = intDsize*(uintL)(len-1); // Anzahl Digits mal intDsize
29             // MSDigit nehmen, testen, welches das höchste Bit ist, das vom
30             // Vorzeichenbit abweicht:
31             var uintD msd = mspref(MSDptr,0); // MSDigit
32             if ((sintD)msd < 0) { msd = ~msd; } // falls negativ, invertieren
33             // Position des höchsten Bits in msd suchen und entsprechend bit_count
34             // erhöhen (um höchstens intDsize-1):
35             if (!(msd == 0)) { integerlengthD(msd, bitcount += ); }
36             return bitcount; // 0 <= bitcount < intDsize*2^intCsize.
37           }
38 }