]> www.ginac.de Git - cln.git/blob - src/float/conv/cl_DF_to_double.cc
98dd214fbafb7dc9d9161be22f60c69f380bdbec
[cln.git] / src / float / conv / cl_DF_to_double.cc
1 // cl_DF_to_double().
2
3 // General includes.
4 #include "cl_sysdep.h"
5
6 // Specification.
7 #include "cl_dfloat.h"
8
9
10 // Implementation.
11
12 #include "cl_DF.h"
13
14 double cl_double_approx (const cl_DF& obj)
15 {
16         union { dfloat eksplicit; double machine_double; } u;
17         #define val u.eksplicit
18         val = TheDfloat(obj)->dfloat_value;
19         // Der Exponent muß um DF_exp_mid-1022 erniedrigt werden.
20         if (DF_exp_mid>1022)
21           #if (cl_word_size==64)
22           { var uintL exp = (val >> DF_mant_len) & (bit(DF_exp_len)-1); // e
23             if (exp < DF_exp_mid-1022+1)
24               { // produziere denormalisiertes Float
25                 val = (val & minus_bit(DF_exp_len+DF_mant_len)) // selbes Vorzeichen
26                       | ((sint64)0 << DF_mant_len) // Exponent 0
27                       | (((val & (bit(DF_mant_len)-1)) | bit(DF_mant_len)) // Mantisse shiften
28                          >> (DF_exp_mid-1022+1 - exp) // shiften
29                         );
30               }
31               else
32               { val -= (sint64)(DF_exp_mid - 1022) << DF_mant_len; }
33           }
34           #else
35           { var uintL exp = (val.semhi >> (DF_mant_len-32)) & (bit(DF_exp_len)-1); // e
36             if (exp < DF_exp_mid-1022+1)
37               { // produziere denormalisiertes Float
38                 var uintL shiftcount = DF_exp_mid-1022+1 - exp;
39                 val.mlo = val.mlo >> shiftcount; // Mantisse shiften
40                 val.mlo |= val.semhi << (32-shiftcount);
41                 val.semhi = (val.semhi & minus_bit(DF_exp_len+DF_mant_len-32)) // selbes Vorzeichen
42                             | ((sint32)0 << (DF_mant_len-32)) // Exponent 0
43                             | (((val.semhi & (bit(DF_mant_len-32)-1)) | bit(DF_mant_len-32)) // Mantisse shiften
44                                >> shiftcount // shiften
45                               );
46               }
47               else
48               { val.semhi -= (sint32)(DF_exp_mid - 1022) << (DF_mant_len-32); }
49           }
50           #endif
51         #undef val
52         return u.machine_double;
53 }