12 #include "cl_integer.h"
17 const cl_FF cl_I_to_FF (const cl_I& x)
20 // x=0 -> Ergebnis 0.0
21 // Merke Vorzeichen von x.
23 // Exponent:=(integer-length x)
24 // Greife die 25 höchstwertigen Bits heraus (angeführt von einer 1).
25 // Runde das letzte Bit weg:
26 // Bit 0 = 0 -> abrunden,
27 // Bit 0 = 1 und Rest =0 -> round-to-even,
28 // Bit 0 = 1 und Rest >0 -> aufrunden.
29 // Dabei um ein Bit nach rechts schieben.
30 // Bei Aufrundung auf 2^24 (rounding overflow) Mantisse um 1 Bit nach rechts
31 // schieben und Exponent incrementieren.
32 if (eq(x,0)) { return cl_FF_0; }
33 var cl_signean sign = -(cl_signean)minusp(x); // Vorzeichen
34 var cl_I abs_x = (sign==0 ? x : -x);
35 var uintL exp = integer_length(abs_x); // (integer-length x)
36 // NDS zu |x|>0 bilden:
37 var const uintD* MSDptr;
39 I_to_NDS_nocopy(abs_x, MSDptr=,len=,,cl_false,);
40 // MSDptr/len/LSDptr ist die NDS zu x, len>0.
41 // Führende Digits holen: Brauche FF_mant_len+1 Bits, dazu intDsize
42 // Bits (die NDS kann mit bis zu intDsize Nullbits anfangen).
43 // Dann werden diese Bits um (exp mod intDsize) nach rechts geschoben.
44 var uintD msd = msprefnext(MSDptr); // erstes Digit
46 var uintD msdd = 0; // weiteres Digit
47 if (--len == 0) goto ok;
48 msdd = msprefnext(MSDptr);
49 #else // (intDsize<=32)
50 var uint32 msdd = 0; // weitere min(len-1,32/intDsize) Digits
51 #define NEXT_DIGIT(i) \
52 { if (--len == 0) goto ok; \
53 msdd |= (uint32)msprefnext(MSDptr) << (32-(i+1)*intDsize); \
55 DOCONSTTIMES(32/intDsize,NEXT_DIGIT);
60 // Die NDS besteht aus msd, msdd, und len weiteren Digits.
61 // Das höchste in 2^intDsize*msd+msdd gesetzte Bit ist Bit Nummer
62 // intDsize-1 + (exp mod intDsize).
63 var uintL shiftcount = exp % intDsize;
64 var uint64 mant = // führende 64 Bits
67 : ((msd << (64-shiftcount)) | (msdd >> shiftcount))
69 // Das höchste in mant gesetzte Bit ist Bit Nummer 63.
70 if ( ((mant & bit(62-FF_mant_len)) ==0) // Bit 39 =0 -> abrunden
71 || ( ((mant & (bit(62-FF_mant_len)-1)) ==0) // Bit 39 =1 und Bits 38..0 =0
72 && ((msdd & (bit(shiftcount)-1)) ==0) // und weitere Bits aus msdd =0
73 && (!test_loop_msp(MSDptr,len)) // und alle weiteren Digits =0
74 // round-to-even, je nach Bit 40 :
75 && ((mant & bit(63-FF_mant_len)) ==0)
78 { mant = mant >> (63-FF_mant_len); }
81 { mant = mant >> (63-FF_mant_len);
83 if (mant >= bit(FF_mant_len+1)) // rounding overflow?
84 { mant = mant>>1; exp = exp+1; }
87 // Die NDS besteht aus msd, msdd, und len weiteren Digits.
88 // Das höchste in 2^32*msd+msdd gesetzte Bit ist Bit Nummer
89 // 31 + (exp mod intDsize).
90 var uintL shiftcount = exp % intDsize;
91 var uint32 mant = // führende 32 Bits
94 : (((uint32)msd << (32-shiftcount)) | (msdd >> shiftcount))
96 // Das höchste in mant gesetzte Bit ist Bit Nummer 31.
97 if ( ((mant & bit(30-FF_mant_len)) ==0) // Bit 7 =0 -> abrunden
98 || ( ((mant & (bit(30-FF_mant_len)-1)) ==0) // Bit 7 =1 und Bits 6..0 =0
99 && ((msdd & (bit(shiftcount)-1)) ==0) // und weitere Bits aus msdd =0
100 && (!test_loop_msp(MSDptr,len)) // und alle weiteren Digits =0
101 // round-to-even, je nach Bit 8 :
102 && ((mant & bit(31-FF_mant_len)) ==0)
105 { mant = mant >> (31-FF_mant_len); }
108 { mant = mant >> (31-FF_mant_len);
110 if (mant >= bit(FF_mant_len+1)) // rounding overflow?
111 { mant = mant>>1; exp = exp+1; }
114 return encode_FF(sign,(sintL)exp,mant);