PageRenderTime 26ms CodeModel.GetById 14ms 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. // General includes.
  3. #include "base/cl_sysdep.h"
  4. // Specification.
  5. #include "cln/lfloat.h"
  6. // Implementation.
  7. #include "float/lfloat/cl_LF.h"
  8. #include "float/lfloat/cl_LF_impl.h"
  9. #include "float/ffloat/cl_FF.h"
  10. #include "base/digitseq/cl_DS.h"
  11. namespace cln {
  12. float float_approx (const cl_LF& x)
  13. {
  14. // x entpacken:
  15. var cl_signean sign;
  16. var sintL exp;
  17. var uintD* ptr;
  18. var uintC len;
  19. LF_decode(x, { return 0.0; }, sign=,exp=,ptr=,len=,);
  20. // intDsize*len-FF_mant_len-1 Bits der Mantisse wegrunden:
  21. // erste k := ceiling(FF_mant_len+2,intDsize) Digits nach mant holen:
  22. #if (intDsize==64)
  23. var uint64 mant = get_max64_Dptr(FF_mant_len+2,ptr);
  24. #else
  25. var uint32 mant = get_max32_Dptr(FF_mant_len+2,ptr);
  26. #endif
  27. ptr = ptr mspop ceiling(FF_mant_len+2,intDsize);
  28. var const int shiftcount = ceiling(FF_mant_len+2,intDsize)*intDsize-(FF_mant_len+1);
  29. if ( ((mant & bit(shiftcount-1)) ==0) // Bit 7 war 0 -> abrunden
  30. || ( ((mant & (bit(shiftcount-1)-1)) ==0) // war 1, Bits 6..0 >0 -> aufrunden
  31. && !test_loop_msp(ptr,len-ceiling(FF_mant_len+2,intDsize)) // weitere Bits /=0 -> aufrunden
  32. // round-to-even
  33. && ((mant & bit(shiftcount)) ==0)
  34. ) )
  35. // abrunden
  36. { mant = mant >> shiftcount; }
  37. else
  38. // aufrunden
  39. { mant = mant >> shiftcount;
  40. mant = mant+1;
  41. if (mant >= bit(FF_mant_len+1))
  42. // ?œberlauf durchs Runden
  43. { mant = mant>>1; exp = exp+1; } // Mantisse rechts schieben
  44. }
  45. union { ffloat eksplicit; float machine_float; } u;
  46. if (exp > (sintL)(FF_exp_high-FF_exp_mid))
  47. { u.eksplicit = make_FF_word(sign,bit(FF_exp_len)-1,0); } // Infinity
  48. else
  49. if (exp < (sintL)(FF_exp_low-FF_exp_mid))
  50. { u.eksplicit = make_FF_word(sign,0,0); } // 0.0
  51. else
  52. { u.eksplicit = make_FF_word(sign,exp+FF_exp_mid,mant); }
  53. return u.machine_float;
  54. }
  55. } // namespace cln