]> www.ginac.de Git - cln.git/blob - src/integer/conv/cl_I_to_UL.cc
* */*: Removed problematic stdin, stdout and stderr definitions.
[cln.git] / src / integer / conv / cl_I_to_UL.cc
1 // cl_I_to_UL().
2
3 // General includes.
4 #include "cl_sysdep.h"
5
6 // Specification.
7 #include "cln/integer.h"
8
9
10 // Implementation.
11
12 #include "cln/number.h"
13 #include "cl_I.h"
14 #include "cl_DS.h"
15 #include "cln/io.h"
16 #include "cln/integer_io.h"
17 #include "cln/abort.h"
18
19 namespace cln {
20
21 uint32 cl_I_to_UL (const cl_I& obj)
22 {
23         if (fixnump(obj)) {
24                 // Fixnum
25                 var sint32 wert = FN_to_L(obj);
26                 if (wert >= 0)
27                         return (uint32)wert;
28                 goto bad;
29         } else { // Bignum
30                 var cl_heap_bignum* bn = TheBignum(obj);
31                 var uintC len = bn->length;
32                 if ((sintD)mspref(arrayMSDptr(bn->data,len),0) < 0)
33                         goto bad;
34                 #define IF_LENGTH(i)  \
35                   if (bn_minlength <= i) /* genau i Digits überhaupt möglich? */\
36                     if (len == i) /* genau i Digits? */                         \
37                       /* 2^((i-1)*intDsize-1) <= obj < 2^(i*intDsize-1) */      \
38                       if ( (i*intDsize-1 > 32)                                  \
39                            && ( ((i-1)*intDsize-1 >= 32)                        \
40                                 || (mspref(arrayMSDptr(bn->data,len),0) >= (uintD)bitc(32-(i-1)*intDsize)) \
41                          )    )                                                 \
42                         goto bad;                                               \
43                         else
44                 IF_LENGTH(1)
45                         return get_uint1D_Dptr(arrayLSDptr(bn->data,1));
46                 IF_LENGTH(2)
47                         return get_uint2D_Dptr(arrayLSDptr(bn->data,2));
48                 IF_LENGTH(3)
49                         return get_uint3D_Dptr(arrayLSDptr(bn->data,3));
50                 IF_LENGTH(4)
51                         return get_uint4D_Dptr(arrayLSDptr(bn->data,4));
52                 IF_LENGTH(5)
53                         return get_uint4D_Dptr(arrayLSDptr(bn->data,5));
54                 #undef IF_LENGTH
55         }
56         bad: // unpassendes Objekt
57         fprint(std::cerr, "Not a 32-bit integer: ");
58         fprint(std::cerr, obj);
59         fprint(std::cerr, "\n");
60         cl_abort();
61 }
62
63 }  // namespace cln