]> www.ginac.de Git - cln.git/blob - src/integer/conv/cl_I_from_L2.cc
* include/cln/number.h (As): Fix it in namespace by suffixing `_As'
[cln.git] / src / integer / conv / cl_I_from_L2.cc
1 // L2_to_I() helper.
2
3 // General includes.
4 #include "cl_sysdep.h"
5
6 // Specification.
7 #include "cl_I.h"
8
9
10 // Implementation.
11
12 #include "cln/number.h"
13 #include "cl_DS.h"
14
15 namespace cln {
16
17 cl_private_thing cl_I_constructor_from_L2 (sint32 wert_hi, uint32 wert_lo)
18 {
19         if (wert_hi == 0) {
20                 if ((wert_lo & minus_bit(cl_value_len-1)) == 0)
21                         return (cl_private_thing)(cl_combine(cl_FN_tag,wert_lo));
22         }
23         elif (wert_hi == ~(sint32)0) {
24                 if ((~wert_lo & minus_bit(cl_value_len-1)) == 0)
25                         return (cl_private_thing)(cl_combine(cl_FN_tag,(sint32)wert_lo));
26         }
27         // Bignum erzeugen:
28         // (dessen Länge  bn_minlength <= n <= ceiling(64/intDsize)  erfüllt)
29         #define FILL_1_DIGIT(l,i,from) \
30                 arrayLSref(ptr->data,l,i) = (uintD)from;
31         #define FILL_2_DIGIT(l,i,from) \
32                 arrayLSref(ptr->data,l,i) = (uintD)from; \
33                 arrayLSref(ptr->data,l,i+1) = (uintD)(from>>intDsize);
34         #define FILL_3_DIGIT(l,i,from) \
35                 arrayLSref(ptr->data,l,i) = (uintD)from; from>>=intDsize; \
36                 arrayLSref(ptr->data,l,i+1) = (uintD)from; \
37                 arrayLSref(ptr->data,l,i+2) = (uintD)(from>>intDsize);
38         #define FILL_4_DIGIT(l,i,from) \
39                 arrayLSref(ptr->data,l,i) = (uintD)from; from>>=intDsize; \
40                 arrayLSref(ptr->data,l,i+1) = (uintD)from; from>>=intDsize; \
41                 arrayLSref(ptr->data,l,i+2) = (uintD)from; \
42                 arrayLSref(ptr->data,l,i+3) = (uintD)(from>>intDsize);
43         #if (intDsize==64)
44         #define FILL_1  FILL_1_DIGIT(1,0,highlow64(wert_hi,wert_lo));
45         #endif
46         #if (32/intDsize==1)
47         #define FILL_1  FILL_1_DIGIT(1,0,wert_lo);
48         #define FILL_2  FILL_1_DIGIT(2,1,wert_hi); FILL_1_DIGIT(2,0,wert_lo);
49         #endif
50         #if (32/intDsize==2)
51         #define FILL_1  FILL_1_DIGIT(1,0,wert_lo);
52         #define FILL_2  FILL_2_DIGIT(2,0,wert_lo);
53         #define FILL_3  FILL_1_DIGIT(3,2,wert_hi); FILL_2_DIGIT(3,0,wert_lo);
54         #define FILL_4  FILL_2_DIGIT(4,2,wert_hi); FILL_2_DIGIT(4,0,wert_lo);
55         #endif
56         #if (32/intDsize==4)
57         #define FILL_1  FILL_1_DIGIT(1,0,wert_lo);
58         #define FILL_2  FILL_2_DIGIT(2,0,wert_lo);
59         #define FILL_3  FILL_3_DIGIT(3,0,wert_lo);
60         #define FILL_4  FILL_4_DIGIT(4,0,wert_lo);
61         #define FILL_5  FILL_1_DIGIT(5,4,wert_hi); FILL_4_DIGIT(5,0,wert_lo);
62         #define FILL_6  FILL_2_DIGIT(6,4,wert_hi); FILL_4_DIGIT(6,0,wert_lo);
63         #define FILL_7  FILL_3_DIGIT(7,4,wert_hi); FILL_4_DIGIT(7,0,wert_lo);
64         #define FILL_8  FILL_4_DIGIT(8,4,wert_hi); FILL_4_DIGIT(8,0,wert_lo);
65         #endif
66         if (wert_hi >= 0) {
67                 #define IF_LENGTH(i)  \
68                   if ((bn_minlength <= i) && (i*intDsize <= 64))        \
69                     if (!((i+1)*intDsize <= 64)                         \
70                         || (i*intDsize-1 < 32                           \
71                             ? ((wert_hi == 0) && (wert_lo < (uint32)bitc(i*intDsize-1))) \
72                             : ((uint32)wert_hi < (uint32)bitc(i*intDsize-1-32)) \
73                        )   )
74                 #define ALLOC(i)  \
75                   var cl_heap_bignum* ptr = allocate_bignum(i);
76                 #define OK  \
77                   return (cl_private_thing)(ptr);
78                 IF_LENGTH(1)
79                         bignum1: { ALLOC(1); FILL_1; OK; }
80                 #if (intDsize <= 32)
81                 IF_LENGTH(2)
82                         bignum2: { ALLOC(2); FILL_2; OK; }
83                 #if (intDsize <= 16)
84                 IF_LENGTH(3)
85                         bignum3: { ALLOC(3); FILL_3; OK; }
86                 IF_LENGTH(4)
87                         bignum4: { ALLOC(4); FILL_4; OK; }
88                 #if (intDsize <= 8)
89                 IF_LENGTH(5)
90                         bignum5: { ALLOC(5); FILL_5; OK; }
91                 IF_LENGTH(6)
92                         bignum6: { ALLOC(6); FILL_6; OK; }
93                 IF_LENGTH(7)
94                         bignum7: { ALLOC(7); FILL_7; OK; }
95                 IF_LENGTH(8)
96                         bignum8: { ALLOC(8); FILL_8; OK; }
97                 #endif
98                 #endif
99                 #endif
100                 #undef IF_LENGTH
101         } else {
102                 #define IF_LENGTH(i)  \
103                   if ((bn_minlength <= i) && (i*intDsize <= 64))        \
104                     if (!((i+1)*intDsize <= 64)                         \
105                         || (i*intDsize-1 < 32                           \
106                             ? ((wert_hi == ~(sint32)0) && (wert_lo >= (uint32)(-bitc(i*intDsize-1)))) \
107                             : ((uint32)wert_hi >= (uint32)(-bitc(i*intDsize-1-32))) \
108                        )   )
109                 IF_LENGTH(1)
110                         goto bignum1;
111                 #if (intDsize <= 32)
112                 IF_LENGTH(2)
113                         goto bignum2;
114                 #if (intDsize <= 16)
115                 IF_LENGTH(3)
116                         goto bignum3;
117                 IF_LENGTH(4)
118                         goto bignum4;
119                 #if (intDsize <= 8)
120                 IF_LENGTH(5)
121                         goto bignum5;
122                 IF_LENGTH(6)
123                         goto bignum6;
124                 IF_LENGTH(7)
125                         goto bignum7;
126                 IF_LENGTH(8)
127                         goto bignum8;
128                 #endif
129                 #endif
130                 #endif
131                 #undef IF_LENGTH
132         }
133 }
134
135 }  // namespace cln