1 // futruncate().
2 
3 // General includes.
4 #include "base/cl_sysdep.h"
5 
6 // Specification.
7 #include "float/sfloat/cl_SF.h"
8 
9 
10 // Implementation.
11 
12 namespace cln {
13 
futruncate(const cl_SF & x)14 const cl_SF futruncate (const cl_SF& x)
15 {
16 // Methode:
17 // x = 0.0 -> Ergebnis 0.0
18 // e<=0 -> Ergebnis 1.0 oder -1.0, je nach Vorzeichen von x.
19 // 1<=e<=16 -> Greife die letzten (17-e) Bits von x heraus.
20 //             Sind sie alle =0 -> Ergebnis x.
21 //             Sonst setze sie alle und erhöhe dann die letzte Stelle um 1.
22 //             Kein Überlauf der 16 Bit -> fertig.
23 //             Sonst (Ergebnis eine Zweierpotenz): Mantisse := .1000...000,
24 //               e:=e+1. (Test auf Überlauf wegen e<=17 überflüssig)
25 // e>=17 -> Ergebnis x.
26       var uintL uexp = SF_uexp(x); // e + SF_exp_mid
27       if (uexp==0) // 0.0 ?
28         { return x; }
29       if (uexp <= SF_exp_mid) // e<=0 ?
30         { // Exponent auf 1, Mantisse auf .1000...000 setzen.
31           return cl_SF_from_word(
32             (x.word & ~(((bit(SF_exp_len)-1)<<SF_exp_shift)
33                         + ((bit(SF_mant_len)-1)<<SF_mant_shift)))
34             | ((cl_uint)(SF_exp_mid+1) << SF_exp_shift)
35             | ((cl_uint)0 << SF_mant_shift)
36             );
37         }
38         else
39         { if (uexp > SF_exp_mid+SF_mant_len) // e > 16 ?
40             { return x; }
41             else
42             { var cl_uint mask = // Bitmaske: Bits 16-e..0 gesetzt, alle anderen gelöscht
43                 bit(SF_mant_len+SF_mant_shift + 1+SF_exp_mid-uexp) - bit(SF_mant_shift);
44               if ((x.word & mask)==0) // alle diese Bits =0 ?
45                 { return x; }
46               return cl_SF_from_word(
47                 (x.word | mask) // alle diese Bits setzen
48                 + bit(SF_mant_shift) // letzte Stelle erhöhen, dabei evtl. Exponenten incrementieren
49                 );
50         }   }
51 }
52 
53 }  // namespace cln
54