12 #include "cln/number.h"
20 cl_private_thing cl_I_constructor_from_Q2 (sint64 wert_hi, uint64 wert_lo)
23 if ((wert_lo & minus_bit(cl_value_len-1)) == 0)
24 return (cl_private_thing)(cl_combine(cl_FN_tag,wert_lo));
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));
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;
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);
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);
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);
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);
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)) \
102 var cl_heap_bignum* ptr = allocate_bignum(i);
104 return (cl_private_thing)(ptr);
106 bignum1: { ALLOC(1); FILL_1; OK; }
108 bignum2: { ALLOC(2); FILL_2; OK; }
111 bignum3: { ALLOC(3); FILL_3; OK; }
113 bignum4: { ALLOC(4); FILL_4; OK; }
116 bignum5: { ALLOC(5); FILL_5; OK; }
118 bignum6: { ALLOC(6); FILL_6; OK; }
120 bignum7: { ALLOC(7); FILL_7; OK; }
122 bignum8: { ALLOC(8); FILL_8; OK; }
125 bignum9: { ALLOC(9); FILL_9; OK; }
127 bignum10: { ALLOC(10); FILL_10; OK; }
129 bignum11: { ALLOC(11); FILL_11; OK; }
131 bignum12: { ALLOC(12); FILL_12; OK; }
133 bignum13: { ALLOC(13); FILL_13; OK; }
135 bignum14: { ALLOC(14); FILL_14; OK; }
137 bignum15: { ALLOC(15); FILL_15; OK; }
139 bignum16: { ALLOC(16); FILL_16; OK; }
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))) \