]> www.ginac.de Git - cln.git/blob - src/float/conv/cl_DF_to_FF.cc
36c1aea0605078390f6355c6069bdf20a1273e4e
[cln.git] / src / float / conv / cl_DF_to_FF.cc
1 // cl_DF_to_FF().
2
3 // General includes.
4 #include "cl_sysdep.h"
5
6 // Specification.
7 #include "cl_F.h"
8
9
10 // Implementation.
11
12 #include "cl_DF.h"
13 #include "cl_FF.h"
14
15 const cl_FF cl_DF_to_FF (const cl_DF& x)
16 {
17         // x entpacken:
18         var cl_signean sign;
19         var sintL exp;
20         #if (cl_word_size==64)
21         var uint64 mant;
22         DF_decode(x, { return cl_FF_0; }, sign=,exp=,mant=);
23         // 52-23=29 Bits wegrunden:
24         var const int shiftcount = DF_mant_len-FF_mant_len;
25         if ( ((mant & bit(shiftcount-1)) ==0) // Bit 28 war 0 -> abrunden
26              || ( ((mant & (bit(shiftcount-1)-1)) ==0) // war 1, Bits 27..0 >0 -> aufrunden
27                   // round-to-even
28                   && ((mant & bit(shiftcount)) ==0)
29            )    )
30           // abrunden
31           { mant = mant >> shiftcount; }
32           else
33           // aufrunden
34           { mant = mant >> shiftcount;
35             mant = mant+1;
36             if (mant >= bit(FF_mant_len+1))
37               // Überlauf durchs Runden
38               { mant = mant>>1; exp = exp+1; } // Mantisse rechts schieben
39           }
40         return encode_FF(sign,exp,mant);
41         #else
42         var uint32 manthi;
43         var uint32 mantlo;
44         DF_decode2(x, { return cl_FF_0; }, sign=,exp=,manthi=,mantlo=);
45         // 52-23=29 Bits wegrunden:
46         var const int shiftcount = DF_mant_len-FF_mant_len;
47         manthi = (manthi << (32-shiftcount)) | (mantlo >> shiftcount);
48         if ( ((mantlo & bit(shiftcount-1)) ==0) // Bit 28 war 0 -> abrunden
49              || ( ((mantlo & (bit(shiftcount-1)-1)) ==0) // war 1, Bits 27..0 >0 -> aufrunden
50                   // round-to-even
51                   && ((mantlo & bit(shiftcount)) ==0)
52            )    )
53           // abrunden
54           {}
55           else
56           // aufrunden
57           { manthi = manthi+1;
58             if (manthi >= bit(FF_mant_len+1))
59               // Überlauf durchs Runden
60               { manthi = manthi>>1; exp = exp+1; } // Mantisse rechts schieben
61           }
62         return encode_FF(sign,exp,manthi);
63         #endif
64 }