]> www.ginac.de Git - cln.git/blob - src/float/conv/cl_LF_to_double.cc
Initial revision
[cln.git] / src / float / conv / cl_LF_to_double.cc
1 // cl_LF_to_double().
2
3 // General includes.
4 #include "cl_sysdep.h"
5
6 // Specification.
7 #include "cl_lfloat.h"
8
9
10 // Implementation.
11
12 #include "cl_LF.h"
13 #include "cl_LF_impl.h"
14 #include "cl_DF.h"
15 #include "cl_DS.h"
16
17 double cl_double_approx (const cl_LF& x)
18 {
19         // x entpacken:
20         var cl_signean sign;
21         var sintL exp;
22         var uintD* ptr;
23         var uintC len;
24         LF_decode(x, { return 0.0; }, sign=,exp=,ptr=,len=,);
25         // intDsize*len-DF_mant_len-1 Bits der Mantisse wegrunden:
26         // erste k := ceiling(DF_mant_len+2,intDsize) Digits nach manthi,mantlo holen:
27         var const int shiftcount = ceiling(DF_mant_len+2,intDsize)*intDsize-(DF_mant_len+1);
28         union { dfloat eksplicit; double machine_double; } u;
29         #if (cl_word_size==64)
30         var uint64 mant = get_max64_Dptr(DF_mant_len+2,ptr);
31         ptr = ptr mspop ceiling(DF_mant_len+2,intDsize);
32         if ( ((mant & bit(shiftcount-1)) ==0) // Bit 10 war 0 -> abrunden
33              || ( ((mant & (bit(shiftcount-1)-1)) ==0) // war 1, Bits 9..0 >0 -> aufrunden
34                   && !test_loop_msp(ptr,len-ceiling(DF_mant_len+2,intDsize)) // weitere Bits /=0 -> aufrunden
35                   // round-to-even
36                   && ((mant & bit(shiftcount)) ==0)
37            )    )
38           // abrunden
39           { mant = mant >> shiftcount; }
40           else
41           // aufrunden
42           { mant = mant >> shiftcount;
43             mant = mant+1;
44             if (mant >= bit(DF_mant_len+1))
45               // Überlauf durchs Runden
46               { mant = mant>>1; exp = exp+1; } // Mantisse rechts schieben
47           }
48         if (exp > (sintL)(DF_exp_high-DF_exp_mid))
49           { u.eksplicit =
50                 ((sint64)sign & bit(63))
51               | ((uint64)(bit(DF_exp_len)-1) << DF_mant_len); // Infinity
52           }
53         else
54         if (exp < (sintL)(DF_exp_low-DF_exp_mid))
55           { u.eksplicit = ((sint64)sign & bit(63)); } // 0.0
56         else
57           { u.eksplicit =
58                 ((sint64)sign & bit(63))                  /* Vorzeichen */
59               | ((uint64)(exp+DF_exp_mid) << DF_mant_len) /* Exponent   */
60               | ((uint64)mant & (bit(DF_mant_len)-1));    /* Mantisse   */
61           }
62         #else
63         var uint32 manthi = get_max32_Dptr(DF_mant_len+2-32,ptr);
64         var uint32 mantlo = get_32_Dptr(ptr mspop ceiling(DF_mant_len+2-32,intDsize));
65         ptr = ptr mspop ceiling(DF_mant_len+2,intDsize);
66         if ( ((mantlo & bit(shiftcount-1)) ==0) // Bit 10 war 0 -> abrunden
67              || ( ((mantlo & (bit(shiftcount-1)-1)) ==0) // war 1, Bits 9..0 >0 -> aufrunden
68                   && !test_loop_msp(ptr,len-ceiling(DF_mant_len+2,intDsize)) // weitere Bits /=0 -> aufrunden
69                   // round-to-even
70                   && ((mantlo & bit(shiftcount)) ==0)
71            )    )
72           // abrunden
73           { mantlo = (manthi << (32-shiftcount)) | (mantlo >> shiftcount);
74             manthi = manthi >> shiftcount;
75           }
76           else
77           // aufrunden
78           { mantlo = (manthi << (32-shiftcount)) | (mantlo >> shiftcount);
79             manthi = manthi >> shiftcount;
80             mantlo = mantlo+1;
81             if (mantlo==0)
82               { manthi = manthi+1;
83                 if (manthi >= bit(DF_mant_len+1-32))
84                   // Überlauf durchs Runden
85                   { manthi = manthi>>1; exp = exp+1; } // Mantisse rechts schieben
86           }   }
87         if (exp > (sintL)(DF_exp_high-DF_exp_mid))
88           { u.eksplicit.semhi =
89                 ((sint32)sign & bit(31))
90               | ((uint32)(bit(DF_exp_len)-1) << (DF_mant_len-32)); // Infinity
91             u.eksplicit.mlo = 0;
92           }
93         else
94         if (exp < (sintL)(DF_exp_low-DF_exp_mid))
95           { u.eksplicit.semhi = ((sint32)sign & bit(31)); // 0.0
96             u.eksplicit.mlo = 0;
97           }
98         else
99           { u.eksplicit.semhi =
100                 ((sint32)sign & bit(31))                       /* Vorzeichen */
101               | ((uint32)(exp+DF_exp_mid) << (DF_mant_len-32)) /* Exponent   */
102               | ((uint32)manthi & (bit(DF_mant_len-32)-1));    /* Mantisse   */
103             u.eksplicit.mlo = mantlo;
104           }
105         #endif
106         return u.machine_double;
107 }