]> www.ginac.de Git - cln.git/blob - src/integer/conv/cl_I_from_Q2.cc
Avoid some "suggest explicit braces to avoid ambiguous ‘else’" warnings.
[cln.git] / src / integer / conv / cl_I_from_Q2.cc
1 // Q2_to_I() helper.
2
3 // General includes.
4 #include "base/cl_sysdep.h"
5
6 // Specification.
7 #include "integer/cl_I.h"
8
9
10 // Implementation.
11
12 #include "cln/number.h"
13
14 #ifdef intQsize
15
16 #include "base/digitseq/cl_DS.h"
17
18 namespace cln {
19
20 cl_private_thing cl_I_constructor_from_Q2 (sint64 wert_hi, uint64 wert_lo)
21 {
22         if (wert_hi == 0) {
23                 if ((wert_lo & minus_bit(cl_value_len-1)) == 0)
24                         return (cl_private_thing)(cl_combine(cl_FN_tag,wert_lo));
25         }
26         elif (wert_hi == ~(sint64)0) {
27                 if ((~wert_lo & minus_bit(cl_value_len-1)) == 0)
28                         return (cl_private_thing)(cl_combine(cl_FN_tag,(sint64)wert_lo));
29         }
30         // Create bignum with length n, where:
31         // bn_minlength <= n <= ceiling(128/intDsize)
32         #define FILL_1_DIGIT(l,i,from) \
33                 arrayLSref(ptr->data,l,i) = (uintD)from;
34         #define FILL_2_DIGIT(l,i,from) \
35                 arrayLSref(ptr->data,l,i) = (uintD)from; \
36                 arrayLSref(ptr->data,l,i+1) = (uintD)(from>>intDsize);
37         #define FILL_3_DIGIT(l,i,from) \
38                 arrayLSref(ptr->data,l,i) = (uintD)from; from>>=intDsize; \
39                 arrayLSref(ptr->data,l,i+1) = (uintD)from; \
40                 arrayLSref(ptr->data,l,i+2) = (uintD)(from>>intDsize);
41         #define FILL_4_DIGIT(l,i,from) \
42                 arrayLSref(ptr->data,l,i) = (uintD)from; from>>=intDsize; \
43                 arrayLSref(ptr->data,l,i+1) = (uintD)from; from>>=intDsize; \
44                 arrayLSref(ptr->data,l,i+2) = (uintD)from; \
45                 arrayLSref(ptr->data,l,i+3) = (uintD)(from>>intDsize);
46         #define FILL_8_DIGIT(l,i,from) \
47                 arrayLSref(ptr->data,l,i) = (uintD)from; from >>=intDsize; \
48                 arrayLSref(ptr->data,l,i+1) = (uintD)from; from >>=intDsize; \
49                 arrayLSref(ptr->data,l,i+2) = (uintD)from; from >>=intDsize; \
50                 arrayLSref(ptr->data,l,i+3) = (uintD)from; from >>=intDsize; \
51                 arrayLSref(ptr->data,l,i+4) = (uintD)from; from >>=intDsize; \
52                 arrayLSref(ptr->data,l,i+5) = (uintD)from; from >>=intDsize; \
53                 arrayLSref(ptr->data,l,i+6) = (uintD)from; \
54                 arrayLSref(ptr->data,l,i+7) = (uintD)from>>intDsize;
55         #if (intDsize==64)
56         #define FILL_1  FILL_1_DIGIT(1,0,wert_lo);
57         #define FILL_2  FILL_1_DIGIT(2,1,wert_hi); FILL_1_DIGIT(2,0,wert_lo);
58         #endif
59         #if (32/intDsize==1)
60         #define FILL_1  FILL_1_DIGIT(1,0,wert_lo);
61         #define FILL_2  FILL_2_DIGIT(2,0,wert_lo);
62         #define FILL_3  FILL_1_DIGIT(3,2,wert_hi); FILL_2_DIGIT(3,0,wert_lo);
63         #define FILL_4  FILL_2_DIGIT(4,2,wert_hi); FILL_2_DIGIT(4,0,wert_lo);
64         #endif
65         #if (32/intDsize==2)
66         #define FILL_1  FILL_1_DIGIT(1,0,wert_lo);
67         #define FILL_2  FILL_2_DIGIT(2,0,wert_lo);
68         #define FILL_3  FILL_3_DIGIT(3,0,wert_lo);
69         #define FILL_4  FILL_4_DIGIT(4,0,wert_lo);
70         #define FILL_5  FILL_1_DIGIT(5,4,wert_hi); FILL_4_DIGIT(5,0,wert_lo);
71         #define FILL_6  FILL_2_DIGIT(6,4,wert_hi); FILL_4_DIGIT(6,0,wert_lo);
72         #define FILL_7  FILL_3_DIGIT(7,4,wert_hi); FILL_4_DIGIT(7,0,wert_lo);
73         #define FILL_8  FILL_4_DIGIT(8,4,wert_hi); FILL_4_DIGIT(8,0,wert_lo);
74         #endif
75         #if (32/intDsize==4)
76         #define FILL_1  FILL_1_DIGIT(1,0,wert_lo);
77         #define FILL_2  FILL_2_DIGIT(2,0,wert_lo);
78         #define FILL_3  FILL_3_DIGIT(3,0,wert_lo);
79         #define FILL_4  FILL_4_DIGIT(4,0,wert_lo);
80         #define FILL_5  FILL_5_DIGIT(5,0,wert_lo);
81         #define FILL_6  FILL_6_DIGIT(6,0,wert_lo);
82         #define FILL_7  FILL_7_DIGIT(7,0,wert_lo);
83         #define FILL_8  FILL_8_DIGIT(8,0,wert_lo);
84         #define FILL_9  FILL_1_DIGIT(9,8,wert_hi);  FILL_8_DIGIT(9,0,wert_lo);
85         #define FILL_10 FILL_2_DIGIT(10,8,wert_hi); FILL_8_DIGIT(10,0,wert_lo);
86         #define FILL_11 FILL_3_DIGIT(11,8,wert_hi); FILL_8_DIGIT(11,0,wert_lo);
87         #define FILL_12 FILL_4_DIGIT(12,8,wert_hi); FILL_8_DIGIT(12,0,wert_lo);
88         #define FILL_13 FILL_5_DIGIT(13,8,wert_hi); FILL_8_DIGIT(13,0,wert_lo);
89         #define FILL_14 FILL_6_DIGIT(14,8,wert_hi); FILL_8_DIGIT(14,0,wert_lo);
90         #define FILL_15 FILL_7_DIGIT(15,8,wert_hi); FILL_8_DIGIT(15,0,wert_lo);
91         #define FILL_16 FILL_8_DIGIT(16,8,wert_hi); FILL_8_DIGIT(16,0,wert_lo);
92         #endif
93         if (wert_hi >= 0) {
94                 #define IF_LENGTH(i)  \
95                   if ((bn_minlength <= i) && (i*intDsize <= 128))       \
96                     if (!((i+1)*intDsize <= 128)                        \
97                         || (i*intDsize-1 < 64                           \
98                             ? ((wert_hi == 0) && (wert_lo < (uint64)bitc(i*intDsize-1))) \
99                             : ((uint64)wert_hi < (uint64)bitc(i*intDsize-1-64)) \
100                        )   )
101                 #define ALLOC(i)  \
102                   var cl_heap_bignum* ptr = allocate_bignum(i);
103                 #define OK  \
104                   return (cl_private_thing)(ptr);
105                 IF_LENGTH(1)
106                         bignum1: { ALLOC(1); FILL_1; OK; }
107                 IF_LENGTH(2)
108                         bignum2: { ALLOC(2); FILL_2; OK; }
109                 #if (intDsize <= 32)
110                 IF_LENGTH(3)
111                         bignum3: { ALLOC(3); FILL_3; OK; }
112                 IF_LENGTH(4)
113                         bignum4: { ALLOC(4); FILL_4; OK; }
114                 #if (intDsize <= 16)
115                 IF_LENGTH(5)
116                         bignum5: { ALLOC(5); FILL_5; OK; }
117                 IF_LENGTH(6)
118                         bignum6: { ALLOC(6); FILL_6; OK; }
119                 IF_LENGTH(7)
120                         bignum7: { ALLOC(7); FILL_7; OK; }
121                 IF_LENGTH(8)
122                         bignum8: { ALLOC(8); FILL_8; OK; }
123                 #if (intDsize <= 8)
124                 IF_LENGTH(9)
125                         bignum9: { ALLOC(9); FILL_9; OK; }
126                 IF_LENGTH(10)
127                         bignum10: { ALLOC(10); FILL_10; OK; }
128                 IF_LENGTH(11)
129                         bignum11: { ALLOC(11); FILL_11; OK; }
130                 IF_LENGTH(12)
131                         bignum12: { ALLOC(12); FILL_12; OK; }
132                 IF_LENGTH(13)
133                         bignum13: { ALLOC(13); FILL_13; OK; }
134                 IF_LENGTH(14)
135                         bignum14: { ALLOC(14); FILL_14; OK; }
136                 IF_LENGTH(15)
137                         bignum15: { ALLOC(15); FILL_15; OK; }
138                 IF_LENGTH(16)
139                         bignum16: { ALLOC(16); FILL_16; OK; }
140                 #endif
141                 #endif
142                 #endif
143                 #undef IF_LENGTH
144         } else {
145                 #define IF_LENGTH(i)  \
146                   if ((bn_minlength <= i) && (i*intDsize <= 128))       \
147                     if (!((i+1)*intDsize <= 128)                        \
148                         || (i*intDsize-1 < 64                           \
149                             ? ((wert_hi == ~(sint64)0) && (wert_lo >= (uint64)(-bitc(i*intDsize-1)))) \
150                             : ((uint64)wert_hi >= (uint64)(-bitc(i*intDsize-1-64))) \
151                        )   )
152                 IF_LENGTH(1)
153                         goto bignum1;
154                 IF_LENGTH(2)
155                         goto bignum2;
156                 #if (intDsize <= 32)
157                 IF_LENGTH(3)
158                         goto bignum3;
159                 IF_LENGTH(4)
160                         goto bignum4;
161                 #if (intDsize <= 16)
162                 IF_LENGTH(5)
163                         goto bignum5;
164                 IF_LENGTH(6)
165                         goto bignum6;
166                 IF_LENGTH(7)
167                         goto bignum7;
168                 IF_LENGTH(8)
169                         goto bignum8;
170                 #if (intDsize <= 8)
171                 IF_LENGTH(9)
172                         goto bignum9;
173                 IF_LENGTH(10)
174                         goto bignum10;
175                 IF_LENGTH(11)
176                         goto bignum11;
177                 IF_LENGTH(12)
178                         goto bignum12;
179                 IF_LENGTH(13)
180                         goto bignum13;
181                 IF_LENGTH(14)
182                         goto bignum14;
183                 IF_LENGTH(15)
184                         goto bignum15;
185                 IF_LENGTH(16)
186                         goto bignum16;
187                 #endif
188                 #endif
189                 #endif
190                 #undef IF_LENGTH
191         }
192 }
193
194 }  // namespace cln
195
196 #endif