]> www.ginac.de Git - cln.git/blob - src/float/lfloat/misc/cl_LF_shorten.cc
* All Files have been modified for inclusion of namespace cln;
[cln.git] / src / float / lfloat / misc / cl_LF_shorten.cc
1 // shorten().
2
3 // General includes.
4 #include "cl_sysdep.h"
5
6 // Specification.
7 #include "cl_LF.h"
8
9
10 // Implementation.
11
12 #include "cl_LF_impl.h"
13 #include "cl_DS.h"
14 #include "cl_F.h"
15
16 namespace cln {
17
18 const cl_LF shorten (const cl_LF& x, uintC len)
19 {
20       // x = 0.0 braucht nicht abgefangen zu werden, da bei Mantisse 0 dann
21       // sowieso abgerundet wird, die Mantisse also 0 bleibt.
22       var Lfloat y = allocate_lfloat(len,TheLfloat(x)->expo,TheLfloat(x)->sign); // neues LF
23       { var uintC oldlen = TheLfloat(x)->len; // alte Länge, > len
24         // Mantisse von x nach y kopieren:
25         copy_loop_msp(arrayMSDptr(TheLfloat(x)->data,oldlen),arrayMSDptr(TheLfloat(y)->data,len),len);
26         // Entscheiden, ob auf- oder abrunden:
27         var uintD* ptr = arrayMSDptr(TheLfloat(x)->data,oldlen) mspop len;
28         if ( ((sintD)mspref(ptr,0) >= 0) // nächstes Bit eine 0 -> abrunden
29              || ( ((mspref(ptr,0) & ((uintD)bit(intDsize-1)-1)) ==0) // eine 1 und alles weitere Nullen?
30                   && !test_loop_msp(ptr mspop 1,oldlen-len-1)
31                   // round-to-even
32                   && ((lspref(ptr,0) & bit(0)) ==0)
33            )    )
34           // abrunden
35           {}
36           else
37           // aufrunden
38           { if ( inc_loop_lsp(arrayLSDptr(TheLfloat(y)->data,len),len) )
39               // Übertrag durch Aufrunden
40               { mspref(arrayMSDptr(TheLfloat(y)->data,len),0) = bit(intDsize-1); // Mantisse := 10...0
41                 // Exponent erhöhen:
42                 if (++(TheLfloat(y)->expo) == LF_exp_high+1) { cl_error_floating_point_overflow(); }
43           }   }
44       }
45       return y;
46 }
47
48 }  // namespace cln