]> www.ginac.de Git - cln.git/blob - src/float/lfloat/elem/cl_LF_I_mul.cc
Initial revision
[cln.git] / src / float / lfloat / elem / cl_LF_I_mul.cc
1 // cl_LF_I_mul().
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_integer.h"
14 #include "cl_I.h"
15 #include "cl_DS.h"
16 #include "cl_F.h"
17
18 const cl_R cl_LF_I_mul (const cl_LF& x, const cl_I& y)
19 {
20 // Method:
21 // If y=0, return 0.
22 // If x=0.0, return x.
23 // If y is longer than x, convert y to a float and multiply.
24 // Else multiply the mantissa of x with the absolute value of y, then round.
25         if (eq(y,0)) { return 0; }
26         if (TheLfloat(x)->expo == 0) { return x; }
27         var cl_signean sign = -(cl_signean)minusp(y); // Vorzeichen von y
28         var cl_I abs_y = (sign==0 ? y : -y);
29         var uintL y_exp = integer_length(abs_y);
30         var uintC len = TheLfloat(x)->len;
31 #ifndef CL_LF_PEDANTIC
32         if (ceiling(y_exp,intDsize) > len)
33                 return x * cl_I_to_LF(y,len);
34 #endif
35         // x länger als y, direkt multiplizieren.
36         CL_ALLOCA_STACK;
37         var const uintD* y_MSDptr;
38         var uintC y_len;
39         var const uintD* y_LSDptr;
40         I_to_NDS_nocopy(abs_y, y_MSDptr=,y_len=,y_LSDptr=,cl_false,); // NDS zu y bilden, y_len>0
41         if (mspref(y_MSDptr,0)==0) y_len--; // NUDS zu y bilden, y_len>0
42         // Multiplizieren.
43         var uintD* prodMSDptr;
44         var uintC prodlen;
45         UDS_UDS_mul_UDS(len,arrayLSDptr(TheLfloat(x)->data,len),
46                         y_len,y_LSDptr,
47                         prodMSDptr=,prodlen=,);
48         // x fing mit 0 Nullbits an, y mit maximal intDsize-1 Nullbits,
49         // daher fängt das Produkt mit maximal intDsize Nullbits an.
50         var uintL shiftcount;
51         if (mspref(prodMSDptr,0)==0) {
52                 shiftcount = intDsize;
53                 msshrink(prodMSDptr); prodlen--;
54         } else {
55                 integerlengthD(mspref(prodMSDptr,0), shiftcount = intDsize -);
56                 if (shiftcount > 0)
57                         shiftleft_loop_lsp(prodMSDptr mspop (len+1),len+1,shiftcount,0);
58         }
59         // Produkt ist nun normalisiert: höchstes Bit =1.
60         // exponent := exponent(x) + intDsize*y_len - shiftcount
61         var uintL uexp = TheLfloat(x)->expo;
62         var uintL iexp = intDsize*y_len - shiftcount; // >= 0 !
63         uexp = uexp + iexp;
64         if ((uexp < iexp) || (uexp > LF_exp_high))
65                 cl_error_floating_point_overflow();
66         // Runden:
67         var uintD* midptr = prodMSDptr mspop len;
68         var uintC restlen = prodlen - len;
69         if ( (restlen==0)
70              || ((sintD)mspref(midptr,0) >= 0) // nächstes Bit =0 -> abrunden
71              || ( ((mspref(midptr,0) & ((uintD)bit(intDsize-1)-1)) ==0) // Bit =1, weitere Bits >0 -> aufrunden
72                   && !test_loop_msp(midptr mspop 1,restlen-1)
73                   // round-to-even
74                   && ((lspref(midptr,0) & bit(0)) ==0)
75            )    )
76           // abrunden
77           {}
78           else
79           // aufrunden
80           { if ( inc_loop_lsp(midptr,len) )
81               // Übertrag durchs Aufrunden
82               { mspref(prodMSDptr,0) = bit(intDsize-1); // Mantisse := 10...0
83                 if (++uexp == LF_exp_high+1) { cl_error_floating_point_overflow(); }
84           }   }
85         return encode_LFu(TheLfloat(x)->sign ^ sign, uexp, prodMSDptr, len);
86 }
87 // Bit complexity (N = max(length(x),length(y))): O(M(N)).
88