]> www.ginac.de Git - cln.git/blob - src/float/conv/cl_LF_to_float.cc
* All Files have been modified for inclusion of namespace cln;
[cln.git] / src / float / conv / cl_LF_to_float.cc
1 // cl_LF_to_float().
2
3 // General includes.
4 #include "cl_sysdep.h"
5
6 // Specification.
7 #include "cln/lfloat.h"
8
9
10 // Implementation.
11
12 #include "cl_LF.h"
13 #include "cl_LF_impl.h"
14 #include "cl_FF.h"
15 #include "cl_DS.h"
16
17 namespace cln {
18
19 float float_approx (const cl_LF& x)
20 {
21         // x entpacken:
22         var cl_signean sign;
23         var sintL exp;
24         var uintD* ptr;
25         var uintC len;
26         LF_decode(x, { return 0.0; }, sign=,exp=,ptr=,len=,);
27         // intDsize*len-FF_mant_len-1 Bits der Mantisse wegrunden:
28         // erste k := ceiling(FF_mant_len+2,intDsize) Digits nach mant holen:
29         #if (intDsize==64)
30         var uint64 mant = get_max64_Dptr(FF_mant_len+2,ptr);
31         #else
32         var uint32 mant = get_max32_Dptr(FF_mant_len+2,ptr);
33         #endif
34         ptr = ptr mspop ceiling(FF_mant_len+2,intDsize);
35         var const int shiftcount = ceiling(FF_mant_len+2,intDsize)*intDsize-(FF_mant_len+1);
36         if ( ((mant & bit(shiftcount-1)) ==0) // Bit 7 war 0 -> abrunden
37              || ( ((mant & (bit(shiftcount-1)-1)) ==0) // war 1, Bits 6..0 >0 -> aufrunden
38                   && !test_loop_msp(ptr,len-ceiling(FF_mant_len+2,intDsize)) // weitere Bits /=0 -> aufrunden
39                   // round-to-even
40                   && ((mant & bit(shiftcount)) ==0)
41            )    )
42           // abrunden
43           { mant = mant >> shiftcount; }
44           else
45           // aufrunden
46           { mant = mant >> shiftcount;
47             mant = mant+1;
48             if (mant >= bit(FF_mant_len+1))
49               // Überlauf durchs Runden
50               { mant = mant>>1; exp = exp+1; } // Mantisse rechts schieben
51           }
52         union { ffloat eksplicit; float machine_float; } u;
53         if (exp > (sintL)(FF_exp_high-FF_exp_mid))
54           { u.eksplicit = make_FF_word(sign,bit(FF_exp_len)-1,0); } // Infinity
55         else
56         if (exp < (sintL)(FF_exp_low-FF_exp_mid))
57           { u.eksplicit = make_FF_word(sign,0,0); } // 0.0
58         else
59           { u.eksplicit = make_FF_word(sign,exp+FF_exp_mid,mant); }
60         return u.machine_float;
61 }
62
63 }  // namespace cln