]> www.ginac.de Git - cln.git/blob - src/float/conv/cl_DF_to_SF.cc
* */*: Convert encoding from ISO 8859-1 to UTF-8.
[cln.git] / src / float / conv / cl_DF_to_SF.cc
1 // cl_DF_to_SF().
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_SF.h"
14
15 namespace cln {
16
17 const cl_SF cl_DF_to_SF (const cl_DF& x)
18 {
19         // x entpacken:
20         var cl_signean sign;
21         var sintL exp;
22         #if (cl_word_size==64)
23         var uint64 mant;
24         DF_decode(x, { return SF_0; }, sign=,exp=,mant=);
25         // 52-16=36 Bits wegrunden:
26         var const int shiftcount = DF_mant_len-SF_mant_len;
27         if ( ((mant & bit(shiftcount-1)) ==0) // Bit 35 war 0 -> abrunden
28              || ( ((mant & (bit(shiftcount-1)-1)) ==0) // war 1, Bits 34..0 >0 -> aufrunden
29                   // round-to-even
30                   && ((mant & bit(shiftcount)) ==0)
31            )    )
32           // abrunden
33           { mant = mant >> shiftcount; }
34           else
35           // aufrunden
36           { mant = mant >> shiftcount;
37             mant = mant+1;
38             if (mant >= bit(SF_mant_len+1))
39               // Überlauf durchs Runden
40               { mant = mant>>1; exp = exp+1; } // Mantisse rechts schieben
41           }
42         return encode_SF(sign,exp,mant);
43         #else
44         var uint32 manthi;
45         var uint32 mantlo;
46         DF_decode2(x, { return SF_0; }, sign=,exp=,manthi=,mantlo=);
47         // 52-16=36 Bits wegrunden:
48         var const int shiftcount = DF_mant_len-SF_mant_len-32;
49         if ( ((manthi & bit(shiftcount-1)) ==0) // Bit 35 war 0 -> abrunden
50              || ( ((manthi & (bit(shiftcount-1)-1)) ==0) // war 1, Bits 34..0 >0 -> aufrunden
51                   && (mantlo==0)
52                   // round-to-even
53                   && ((manthi & bit(shiftcount)) ==0)
54            )    )
55           // abrunden
56           { manthi = manthi >> shiftcount; }
57           else
58           // aufrunden
59           { manthi = manthi >> shiftcount;
60             manthi = manthi+1;
61             if (manthi >= bit(SF_mant_len+1))
62               // Überlauf durchs Runden
63               { manthi = manthi>>1; exp = exp+1; } // Mantisse rechts schieben
64           }
65         return encode_SF(sign,exp,manthi);
66         #endif
67 }
68
69 }  // namespace cln