]> www.ginac.de Git - cln.git/blob - src/integer/conv/cl_I_from_UL2.cc
* src/integer/conv/cl_I_to_digits (I_to_digits): Fix an elusive stack
[cln.git] / src / integer / conv / cl_I_from_UL2.cc
1 // UL2_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_UL2 (uint32 wert_hi, uint32 wert_lo)
18 {
19         if ((wert_hi == 0)
20             && ((wert_lo & minus_bit(cl_value_len-1)) == 0)
21            )
22                 return (cl_private_thing)(cl_combine(cl_FN_tag,wert_lo));
23         // Bignum erzeugen:
24         // (dessen Länge  bn_minlength <= n <= ceiling((64+1)/intDsize)  erfüllt)
25         #define UL2_maxlength  ceiling(64+1,intDsize)
26         #define FILL_1_DIGIT(l,i,from) \
27                 arrayLSref(ptr->data,l,i) = (uintD)from;
28         #define FILL_2_DIGIT(l,i,from) \
29                 arrayLSref(ptr->data,l,i) = (uintD)from; \
30                 arrayLSref(ptr->data,l,i+1) = (uintD)(from>>intDsize);
31         #define FILL_3_DIGIT(l,i,from) \
32                 arrayLSref(ptr->data,l,i) = (uintD)from; from>>=intDsize; \
33                 arrayLSref(ptr->data,l,i+1) = (uintD)from; \
34                 arrayLSref(ptr->data,l,i+2) = (uintD)(from>>intDsize);
35         #define FILL_4_DIGIT(l,i,from) \
36                 arrayLSref(ptr->data,l,i) = (uintD)from; from>>=intDsize; \
37                 arrayLSref(ptr->data,l,i+1) = (uintD)from; from>>=intDsize; \
38                 arrayLSref(ptr->data,l,i+2) = (uintD)from; \
39                 arrayLSref(ptr->data,l,i+3) = (uintD)(from>>intDsize);
40         #if (intDsize==64)
41         #define FILL_1  FILL_1_DIGIT(1,0,highlow64(wert_hi,wert_lo));
42         #define FILL_2  FILL_1_DIGIT(2,1,0); FILL_1_DIGIT(2,0,highlow64(wert_hi,wert_lo));
43         #endif
44         #if (32/intDsize==1)
45         #define FILL_1  FILL_1_DIGIT(1,0,wert_lo);
46         #define FILL_2  FILL_1_DIGIT(2,1,wert_hi); FILL_1_DIGIT(2,0,wert_lo);
47         #define FILL_3  FILL_1_DIGIT(3,2,0); FILL_1_DIGIT(3,1,wert_hi); FILL_1_DIGIT(3,0,wert_lo);
48         #endif
49         #if (32/intDsize==2)
50         #define FILL_1  FILL_1_DIGIT(1,0,wert_lo);
51         #define FILL_2  FILL_2_DIGIT(2,0,wert_lo);
52         #define FILL_3  FILL_1_DIGIT(3,2,wert_hi); FILL_2_DIGIT(3,0,wert_lo);
53         #define FILL_4  FILL_2_DIGIT(4,2,wert_hi); FILL_2_DIGIT(4,0,wert_lo);
54         #define FILL_5  FILL_1_DIGIT(5,4,0); FILL_2_DIGIT(5,2,wert_hi); FILL_2_DIGIT(5,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         #define FILL_9  FILL_1_DIGIT(9,8,0); FILL_4_DIGIT(9,4,wert_hi); FILL_4_DIGIT(9,0,wert_lo);
66         #endif
67         #define IF_LENGTH(i)  \
68           if ((bn_minlength <= i) && (i <= UL2_maxlength))      \
69             if (!(i+1 <= UL2_maxlength)                         \
70                 || (i*intDsize-1 < 32                           \
71                     ? ((wert_hi == 0) && (wert_lo < (uint32)bitc(i*intDsize-1))) \
72                     : (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                 { ALLOC(1); FILL_1; OK; }
80         IF_LENGTH(2)
81                 { ALLOC(2); FILL_2; OK; }
82         #if (intDsize <= 32)
83         IF_LENGTH(3)
84                 { ALLOC(3); FILL_3; OK; }
85         #if (intDsize <= 16)
86         IF_LENGTH(4)
87                 { ALLOC(4); FILL_4; OK; }
88         IF_LENGTH(5)
89                 { ALLOC(5); FILL_5; OK; }
90         #if (intDsize <= 8)
91         IF_LENGTH(6)
92                 { ALLOC(6); FILL_6; OK; }
93         IF_LENGTH(7)
94                 { ALLOC(7); FILL_7; OK; }
95         IF_LENGTH(8)
96                 { ALLOC(8); FILL_8; OK; }
97         IF_LENGTH(9)
98                 { ALLOC(9); FILL_9; OK; }
99         #endif
100         #endif
101         #endif
102         #undef IF_LENGTH
103         #undef UL2_maxlength
104 }
105
106 }  // namespace cln