]> www.ginac.de Git - cln.git/blob - src/base/output/cl_output_dec.cc
Finalize CLN 1.3.7 release.
[cln.git] / src / base / output / cl_output_dec.cc
1 // fprintdecimal().
2
3 // General includes.
4 #include "base/cl_sysdep.h"
5
6 // Specification.
7 #include "cln/io.h"
8
9
10 // Implementation.
11
12 namespace cln {
13
14 // We don't use `stream << x' or `stream << dec << x', because an ostream
15 // carries so many attributes, and we don't want to modifies these attributes.
16
17 static void fprintdecimal_impl (std::ostream& stream, uintptr_t x)
18 {
19         #define bufsize (((sizeof(uintptr_t)*53)/22)+1) // 53/22 > 8*log(2)/log(10)
20         var char buf[bufsize+1];
21         var char* bufptr = &buf[bufsize];
22         *bufptr = '\0';
23         do {
24                 uintptr_t q = x / 10;
25                 uintptr_t r = x % 10;
26                 *--bufptr = '0'+r;
27                 x = q;
28         } while (x > 0);
29         fprint(stream,bufptr);
30         #undef bufsize
31 }
32
33 static void fprintdecimal_impl (std::ostream& stream, intptr_t x)
34 {
35         if (x >= 0)
36                 fprintdecimal(stream,(uintptr_t)x);
37         else {
38                 fprintchar(stream,'-');
39                 fprintdecimal(stream,(uintptr_t)(-1-x)+1);
40         }
41 }
42
43 void fprintdecimal (std::ostream& stream, unsigned int x)
44 {
45         fprintdecimal_impl(stream,(uintptr_t)x);
46 }
47 void fprintdecimal (std::ostream& stream, int x)
48 {
49         fprintdecimal_impl(stream,(intptr_t)x);
50 }
51
52 void fprintdecimal (std::ostream& stream, unsigned long x)
53 {
54         fprintdecimal_impl(stream,(uintptr_t)x);
55 }
56 void fprintdecimal (std::ostream& stream, long x)
57 {
58         fprintdecimal_impl(stream,(intptr_t)x);
59 }
60
61 void fprintdecimal (std::ostream& stream, unsigned long long x)
62 {
63 #if long_long_bitsize <= pointer_bitsize
64         fprintdecimal_impl(stream,(uintptr_t)x);
65 #else
66         #define bufsize (((sizeof(unsigned long long)*53)/22)+1) // 53/22 > 8*log(2)/log(10)
67         var char buf[bufsize+1];
68         var char* bufptr = &buf[bufsize];
69         *bufptr = '\0';
70         do {
71                 unsigned long long q = x / 10;
72                 unsigned long long r = x % 10;
73                 *--bufptr = '0'+r;
74                 x = q;
75         } while (x > 0);
76         fprint(stream,bufptr);
77         #undef bufsize
78 #endif
79 }
80
81 void fprintdecimal (std::ostream& stream, long long x)
82 {
83 #if long_long_bitsize <= pointer_bitsize
84         fprintdecimal_impl(stream,(intptr_t)x);
85 #else
86         if (x >= 0)
87                 fprintdecimal(stream,(unsigned long long)x);
88         else {
89                 fprintchar(stream,'-');
90                 fprintdecimal(stream,(unsigned long long)(-1-x)+1);
91         }
92 #endif
93 }
94
95 }  // namespace cln