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