]> www.ginac.de Git - cln.git/blob - src/float/ffloat/elem/cl_FF_from_I.cc
913cdd6f42192cee9cc8560f8837f335757c25db
[cln.git] / src / float / ffloat / elem / cl_FF_from_I.cc
1 // cl_I_to_FF().
2
3 // General includes.
4 #include "cl_sysdep.h"
5
6 // Specification.
7 #include "cl_FF.h"
8
9
10 // Implementation.
11
12 #include "cl_integer.h"
13 #include "cl_I.h"
14 #include "cl_DS.h"
15 #include "cl_F.h"
16
17 const cl_FF cl_I_to_FF (const cl_I& x)
18 {
19 // Methode:
20 // x=0 -> Ergebnis 0.0
21 // Merke Vorzeichen von x.
22 // x:=(abs x)
23 // Exponent:=(integer-length x)
24 //   Greife die 25 höchstwertigen Bits heraus (angeführt von einer 1).
25 //   Runde das letzte Bit weg:
26 //     Bit 0 = 0 -> abrunden,
27 //     Bit 0 = 1 und Rest =0 -> round-to-even,
28 //     Bit 0 = 1 und Rest >0 -> aufrunden.
29 //   Dabei um ein Bit nach rechts schieben.
30 //   Bei Aufrundung auf 2^24 (rounding overflow) Mantisse um 1 Bit nach rechts
31 //     schieben und Exponent incrementieren.
32       if (eq(x,0)) { return cl_FF_0; }
33       var cl_signean sign = -(cl_signean)minusp(x); // Vorzeichen
34       var cl_I abs_x = (sign==0 ? x : -x);
35       var uintL exp = integer_length(abs_x); // (integer-length x)
36       // NDS zu |x|>0 bilden:
37       var const uintD* MSDptr;
38       var uintC len;
39       I_to_NDS_nocopy(abs_x, MSDptr=,len=,,cl_false,);
40       // MSDptr/len/LSDptr ist die NDS zu x, len>0.
41       // Führende Digits holen: Brauche FF_mant_len+1 Bits, dazu intDsize
42       // Bits (die NDS kann mit bis zu intDsize Nullbits anfangen).
43       // Dann werden diese Bits um (exp mod intDsize) nach rechts geschoben.
44       var uintD msd = msprefnext(MSDptr); // erstes Digit
45       #if (intDsize==64)
46       var uintD msdd = 0; // weiteres Digit
47       if (--len == 0) goto ok;
48       msdd = msprefnext(MSDptr);
49       #else // (intDsize<=32)
50       var uint32 msdd = 0; // weitere min(len-1,32/intDsize) Digits
51       #define NEXT_DIGIT(i)  \
52         { if (--len == 0) goto ok;                                   \
53           msdd |= (uint32)msprefnext(MSDptr) << (32-(i+1)*intDsize); \
54         }
55       DOCONSTTIMES(32/intDsize,NEXT_DIGIT);
56       #undef NEXT_DIGIT
57       #endif
58       --len; ok:
59       #if (intDsize==64)
60       // Die NDS besteht aus msd, msdd, und len weiteren Digits.
61       // Das höchste in 2^intDsize*msd+msdd gesetzte Bit ist Bit Nummer
62       // intDsize-1 + (exp mod intDsize).
63       var uintL shiftcount = exp % intDsize;
64       var uint64 mant = // führende 64 Bits
65         (shiftcount==0
66          ? msdd
67          : ((msd << (64-shiftcount)) | (msdd >> shiftcount))
68         );
69       // Das höchste in mant gesetzte Bit ist Bit Nummer 63.
70       if ( ((mant & bit(62-FF_mant_len)) ==0) // Bit 39 =0 -> abrunden
71            || ( ((mant & (bit(62-FF_mant_len)-1)) ==0) // Bit 39 =1 und Bits 38..0 =0
72                 && ((msdd & (bit(shiftcount)-1)) ==0) // und weitere Bits aus msdd =0
73                 && (!test_loop_msp(MSDptr,len)) // und alle weiteren Digits =0
74                 // round-to-even, je nach Bit 40 :
75                 && ((mant & bit(63-FF_mant_len)) ==0)
76          )    )
77         // abrunden
78         { mant = mant >> (63-FF_mant_len); }
79         else
80         // aufrunden
81         { mant = mant >> (63-FF_mant_len);
82           mant += 1;
83           if (mant >= bit(FF_mant_len+1)) // rounding overflow?
84             { mant = mant>>1; exp = exp+1; }
85         }
86       #else
87       // Die NDS besteht aus msd, msdd, und len weiteren Digits.
88       // Das höchste in 2^32*msd+msdd gesetzte Bit ist Bit Nummer
89       // 31 + (exp mod intDsize).
90       var uintL shiftcount = exp % intDsize;
91       var uint32 mant = // führende 32 Bits
92         (shiftcount==0
93          ? msdd
94          : (((uint32)msd << (32-shiftcount)) | (msdd >> shiftcount))
95         );
96       // Das höchste in mant gesetzte Bit ist Bit Nummer 31.
97       if ( ((mant & bit(30-FF_mant_len)) ==0) // Bit 7 =0 -> abrunden
98            || ( ((mant & (bit(30-FF_mant_len)-1)) ==0) // Bit 7 =1 und Bits 6..0 =0
99                 && ((msdd & (bit(shiftcount)-1)) ==0) // und weitere Bits aus msdd =0
100                 && (!test_loop_msp(MSDptr,len)) // und alle weiteren Digits =0
101                 // round-to-even, je nach Bit 8 :
102                 && ((mant & bit(31-FF_mant_len)) ==0)
103          )    )
104         // abrunden
105         { mant = mant >> (31-FF_mant_len); }
106         else
107         // aufrunden
108         { mant = mant >> (31-FF_mant_len);
109           mant += 1;
110           if (mant >= bit(FF_mant_len+1)) // rounding overflow?
111             { mant = mant>>1; exp = exp+1; }
112         }
113       #endif
114       return encode_FF(sign,(sintL)exp,mant);
115 }