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