PageRenderTime 23ms CodeModel.GetById 13ms app.highlight 8ms RepoModel.GetById 0ms app.codeStats 0ms

/cln-1.3.2/src/float/conv/cl_LF_to_float.cc

#
C++ | 63 lines | 44 code | 8 blank | 11 comment | 7 complexity | 6d8833d4285ac465d24012644f348301 MD5 | raw file
Possible License(s): GPL-2.0
 1// cl_LF_to_float().
 2
 3// General includes.
 4#include "base/cl_sysdep.h"
 5
 6// Specification.
 7#include "cln/lfloat.h"
 8
 9
10// Implementation.
11
12#include "float/lfloat/cl_LF.h"
13#include "float/lfloat/cl_LF_impl.h"
14#include "float/ffloat/cl_FF.h"
15#include "base/digitseq/cl_DS.h"
16
17namespace cln {
18
19float 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