]> www.ginac.de Git - cln.git/blob - src/integer/conv/cl_I_from_UL.cc
* include/cln/number.h (As): Fix it in namespace by suffixing `_As'
[cln.git] / src / integer / conv / cl_I_from_UL.cc
1 // UL_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
14 #if (cl_value_len <= 32)
15
16 #include "cl_DS.h"
17
18 namespace cln {
19
20 cl_private_thing cl_I_constructor_from_UL (uint32 wert)
21 {
22         if ((wert & minus_bit(cl_value_len-1)) == 0)
23            // Bits, die nicht in den Fixnum-Wert >= 0 reinpassen.
24                 return (cl_private_thing)(cl_combine(cl_FN_tag,wert));
25         // Bignum erzeugen:
26         // (dessen Länge  bn_minlength <= n <= ceiling((32+1)/intDsize)  erfüllt)
27         #define UL_maxlength  ceiling(32+1,intDsize)
28         #define IF_LENGTH(i)  \
29           if ((bn_minlength <= i) && (i <= UL_maxlength))       \
30             if (!(i+1 <= UL_maxlength)                          \
31                 || ((uint32)wert < (uint32)bitc(i*intDsize-1))  \
32                )
33         IF_LENGTH(1)
34                 { var cl_heap_bignum* ptr = allocate_bignum(1);
35                   arrayLSref(ptr->data,1,0) = wert;
36                   return (cl_private_thing)(ptr);
37                 }
38         #if (intDsize <= 32)
39         IF_LENGTH(2)
40                 { var cl_heap_bignum* ptr = allocate_bignum(2);
41                   arrayLSref(ptr->data,2,0) = (uintD)wert;
42                   #if (intDsize>=32)
43                   arrayLSref(ptr->data,2,1) = 0;
44                   #else
45                   arrayLSref(ptr->data,2,1) = (uintD)(wert>>intDsize);
46                   #endif
47                   return (cl_private_thing)(ptr);
48                 }
49         #if (intDsize <= 16)
50         IF_LENGTH(3)
51                 { var cl_heap_bignum* ptr = allocate_bignum(3);
52                   arrayLSref(ptr->data,3,0) = (uintD)wert; wert >>= intDsize;
53                   arrayLSref(ptr->data,3,1) = (uintD)wert;
54                   #if (2*intDsize>=32)
55                   arrayLSref(ptr->data,3,2) = 0;
56                   #else
57                   arrayLSref(ptr->data,3,2) = (uintD)(wert>>intDsize);
58                   #endif
59                   return (cl_private_thing)(ptr);
60                 }
61         #if (intDsize <= 8)
62         IF_LENGTH(4)
63                 { var cl_heap_bignum* ptr = allocate_bignum(4);
64                   arrayLSref(ptr->data,4,0) = (uintD)wert; wert >>= intDsize;
65                   arrayLSref(ptr->data,4,1) = (uintD)wert; wert >>= intDsize;
66                   arrayLSref(ptr->data,4,2) = (uintD)wert;
67                   #if (3*intDsize>=32)
68                   arrayLSref(ptr->data,4,3) = 0;
69                   #else
70                   arrayLSref(ptr->data,4,3) = (uintD)(wert>>intDsize);
71                   #endif
72                   return (cl_private_thing)(ptr);
73                 }
74         IF_LENGTH(5)
75                 { var cl_heap_bignum* ptr = allocate_bignum(5);
76                   arrayLSref(ptr->data,5,0) = (uintD)wert; wert >>= intDsize;
77                   arrayLSref(ptr->data,5,1) = (uintD)wert; wert >>= intDsize;
78                   arrayLSref(ptr->data,5,2) = (uintD)wert; wert >>= intDsize;
79                   arrayLSref(ptr->data,5,3) = (uintD)wert;
80                   #if (4*intDsize>=32)
81                   arrayLSref(ptr->data,5,4) = 0;
82                   #else
83                   arrayLSref(ptr->data,5,4) = (uintD)(wert>>intDsize);
84                   #endif
85                   return (cl_private_thing)(ptr);
86                 }
87         #endif
88         #endif
89         #endif
90         #undef IF_LENGTH
91         #undef UL_maxlength
92 }
93
94 }  // namespace cln
95
96 #endif