13 #include "cl_LF_impl.h"
17 const cl_LF square (const cl_LF& x)
19 // Methode: wie operator*(x,x).
20 var uintC len = TheLfloat(x)->len;
21 var uintL uexp = TheLfloat(x)->expo;
22 if (uexp==0) // x=0.0 -> Ergebnis 0.0
24 // Exponenten addieren:
25 // (uexp-LF_exp_mid) + (uexp-LF_exp_mid) = (2*uexp-LF_exp_mid)-LF_exp_mid
29 if (uexp < LF_exp_mid+LF_exp_low)
30 { if (underflow_allowed())
31 { cl_error_floating_point_underflow(); }
33 { return encode_LF0(len); } // Ergebnis 0.0
38 if (uexp > (uintL)(LF_exp_mid+LF_exp_high+1)) { cl_error_floating_point_overflow(); }
40 uexp = uexp - LF_exp_mid;
41 // Nun ist LF_exp_low <= uexp <= LF_exp_high+1.
42 // neues Long-Float allozieren:
43 var Lfloat y = allocate_lfloat(len,uexp,0);
45 var const uintD* x_LSDptr = arrayLSDptr(TheLfloat(x)->data,len);
49 num_stack_alloc(2*len,MSDptr=,LSDptr=);
50 cl_UDS_mul_square(x_LSDptr,len,LSDptr);
51 {var uintD* midptr = MSDptr mspop len; // Pointer in die Mitte der 2*len Digits
52 if ((sintD)mspref(MSDptr,0) >= 0) // führendes Bit abtesten
53 { // erste n+1 Digits um 1 Bit nach links schieben:
54 shift1left_loop_lsp(midptr mspop 1,len+1);
55 // Exponenten decrementieren:
56 if ((TheLfloat(y)->expo)-- == LF_exp_low-1)
57 { if (underflow_allowed())
58 { cl_error_floating_point_underflow(); }
60 { return encode_LF0(len); } // Ergebnis 0.0
63 // erste Hälfte des Mantissenprodukts übertragen:
64 {var uintD* y_mantMSDptr = arrayMSDptr(TheLfloat(y)->data,len);
65 var uintD* y_mantLSDptr = copy_loop_msp(MSDptr,y_mantMSDptr,len);
67 if ( ((sintD)mspref(midptr,0) >= 0) // nächstes Bit =0 -> abrunden
68 || ( ((mspref(midptr,0) & ((uintD)bit(intDsize-1)-1)) ==0) // Bit =1, weitere Bits >0 -> aufrunden
69 && !test_loop_msp(midptr mspop 1,len-1)
71 && ((lspref(midptr,0) & bit(0)) ==0)
77 { if ( inc_loop_lsp(y_mantLSDptr,len) )
78 { // Übertrag durchs Aufrunden (kann nur auftreten,
79 // wenn vorhin um 1 Bit nach links geschoben wurde)
80 mspref(y_mantMSDptr,0) = bit(intDsize-1); // Mantisse := 10...0
81 (TheLfloat(y)->expo)++; // Exponent wieder zurück-erhöhen
83 // LF_exp_low <= exp <= LF_exp_high sicherstellen:
84 if (TheLfloat(y)->expo == LF_exp_high+1) { cl_error_floating_point_overflow(); }
88 // Bit complexity (N = length(x)): O(M(N)).