1 // cl_DF_to_FF().
2 
3 // General includes.
4 #include "base/cl_sysdep.h"
5 
6 // Specification.
7 #include "float/cl_F.h"
8 
9 
10 // Implementation.
11 
12 #include "float/dfloat/cl_DF.h"
13 #include "float/ffloat/cl_FF.h"
14 
15 namespace cln {
16 
cl_DF_to_FF(const cl_DF & x)17 const cl_FF cl_DF_to_FF (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 cl_FF_0; }, sign=,exp=,mant=);
25 	// 52-23=29 Bits wegrunden:
26 	var const int shiftcount = DF_mant_len-FF_mant_len;
27 	if ( ((mant & bit(shiftcount-1)) ==0) // Bit 28 war 0 -> abrunden
28 	     || ( ((mant & (bit(shiftcount-1)-1)) ==0) // war 1, Bits 27..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(FF_mant_len+1))
39 	      // Überlauf durchs Runden
40 	      { mant = mant>>1; exp = exp+1; } // Mantisse rechts schieben
41 	  }
42 	return encode_FF(sign,exp,mant);
43 	#else
44 	var uint32 manthi;
45 	var uint32 mantlo;
46 	DF_decode2(x, { return cl_FF_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 	return encode_FF(sign,exp,manthi);
65 	#endif
66 }
67 
68 }  // namespace cln
69