From ca31719b9feb21fd62737136dc75d704124ead00 Mon Sep 17 00:00:00 2001 From: Bruno Haible Date: Sun, 27 Oct 2019 19:47:04 +0100 Subject: [PATCH] 64-bit mingw port: Extend fprintdecimal and fprinthexadecimal up to 'long long'. --- include/cln/io.h | 66 ++++++++++++++++++++++++++++---- src/base/cl_as_exception.cc | 4 +- src/base/cl_debug.cc | 6 +-- src/base/output/cl_output_dec.cc | 44 +++++++++++++++++---- src/base/output/cl_output_hex.cc | 44 +++++++++++++++++---- src/base/ring/cl_no_ring.cc | 12 +++--- 6 files changed, 143 insertions(+), 33 deletions(-) diff --git a/include/cln/io.h b/include/cln/io.h index 361c927..0b73636 100644 --- a/include/cln/io.h +++ b/include/cln/io.h @@ -37,30 +37,80 @@ inline void fprint (std::ostream& stream, const char * string) } -extern void fprintdecimal (std::ostream& stream, unsigned long x); -extern void fprintdecimal (std::ostream& stream, long x); +extern void fprintdecimal_impl (std::ostream& stream, uintptr_t x); +extern void fprintdecimal_impl (std::ostream& stream, intptr_t x); inline void fprintdecimal (std::ostream& stream, unsigned int x) { - fprintdecimal(stream,(unsigned long)x); + fprintdecimal_impl(stream,(uintptr_t)x); } inline void fprintdecimal (std::ostream& stream, int x) { - fprintdecimal(stream,(long)x); + fprintdecimal_impl(stream,(intptr_t)x); } -extern void fprinthexadecimal (std::ostream& stream, unsigned long x); -extern void fprinthexadecimal (std::ostream& stream, long x); +inline void fprintdecimal (std::ostream& stream, unsigned long x) +{ + fprintdecimal_impl(stream,(uintptr_t)x); +} +inline void fprintdecimal (std::ostream& stream, long x) +{ + fprintdecimal_impl(stream,(intptr_t)x); +} + +#ifdef HAVE_LONGLONG +#if long_long_bitsize <= pointer_bitsize +inline void fprintdecimal (std::ostream& stream, unsigned long long x) +{ + fprintdecimal_impl(stream,(uintptr_t)x); +} +inline void fprintdecimal (std::ostream& stream, long long x) +{ + fprintdecimal_impl(stream,(intptr_t)x); +} +#else +extern void fprintdecimal (std::ostream& stream, unsigned long long x); +extern void fprintdecimal (std::ostream& stream, long long x); +#endif +#endif + +extern void fprinthexadecimal_impl (std::ostream& stream, uintptr_t x); +extern void fprinthexadecimal_impl (std::ostream& stream, intptr_t x); inline void fprinthexadecimal (std::ostream& stream, unsigned int x) { - fprinthexadecimal(stream,(unsigned long)x); + fprinthexadecimal_impl(stream,(uintptr_t)x); } inline void fprinthexadecimal (std::ostream& stream, int x) { - fprinthexadecimal(stream,(long)x); + fprinthexadecimal_impl(stream,(intptr_t)x); } +inline void fprinthexadecimal (std::ostream& stream, unsigned long x) +{ + fprinthexadecimal_impl(stream,(uintptr_t)x); +} +inline void fprinthexadecimal (std::ostream& stream, long x) +{ + fprinthexadecimal_impl(stream,(intptr_t)x); +} + +#ifdef HAVE_LONGLONG +#if long_long_bitsize <= pointer_bitsize +inline void fprinthexadecimal (std::ostream& stream, unsigned long long x) +{ + fprinthexadecimal_impl(stream,(uintptr_t)x); +} +inline void fprinthexadecimal (std::ostream& stream, long long x) +{ + fprinthexadecimal_impl(stream,(intptr_t)x); +} +#else +extern void fprinthexadecimal (std::ostream& stream, unsigned long long x); +extern void fprinthexadecimal (std::ostream& stream, long long x); +#endif +#endif + struct cl_print_flags; struct cl_print_number_flags; diff --git a/src/base/cl_as_exception.cc b/src/base/cl_as_exception.cc index 42713c2..51dcf68 100644 --- a/src/base/cl_as_exception.cc +++ b/src/base/cl_as_exception.cc @@ -30,9 +30,9 @@ as_error_msg (const cl_number& obj, const char * typestring, const char * filena fprint(buf, obj); #else fprint(buf, "@0x"); - fprinthexadecimal(buf, (unsigned long)(void*)&obj); + fprinthexadecimal(buf, (uintptr_t)(void*)&obj); fprint(buf, ": 0x"); - fprinthexadecimal(buf, (unsigned long)obj.word); + fprinthexadecimal(buf, (uintptr_t)obj.word); #endif return buf.str(); } diff --git a/src/base/cl_debug.cc b/src/base/cl_debug.cc index 6c034cd..9c28498 100644 --- a/src/base/cl_debug.cc +++ b/src/base/cl_debug.cc @@ -17,18 +17,18 @@ namespace cln { void cl_dprint_unknown (cl_heap* pointer) { fprint(cl_debugout, ""); } static void cl_dprint_unknown_immediate (cl_heap* pointer) { fprint(cl_debugout, ""); } diff --git a/src/base/output/cl_output_dec.cc b/src/base/output/cl_output_dec.cc index f39edc7..3741327 100644 --- a/src/base/output/cl_output_dec.cc +++ b/src/base/output/cl_output_dec.cc @@ -14,15 +14,15 @@ namespace cln { // We don't use `stream << x' or `stream << dec << x', because an ostream // carries so many attributes, and we don't want to modifies these attributes. -void fprintdecimal (std::ostream& stream, unsigned long x) +void fprintdecimal_impl (std::ostream& stream, uintptr_t x) { - #define bufsize 20 + #define bufsize (((sizeof(uintptr_t)*53)/22)+1) // 53/22 > 8*log(2)/log(10) var char buf[bufsize+1]; var char* bufptr = &buf[bufsize]; *bufptr = '\0'; do { - unsigned long q = x / 10; - unsigned long r = x % 10; + uintptr_t q = x / 10; + uintptr_t r = x % 10; *--bufptr = '0'+r; x = q; } while (x > 0); @@ -30,14 +30,44 @@ void fprintdecimal (std::ostream& stream, unsigned long x) #undef bufsize } -void fprintdecimal (std::ostream& stream, long x) +void fprintdecimal_impl (std::ostream& stream, intptr_t x) { if (x >= 0) - fprintdecimal(stream,(unsigned long)x); + fprintdecimal(stream,(uintptr_t)x); else { fprintchar(stream,'-'); - fprintdecimal(stream,(unsigned long)(-1-x)+1); + fprintdecimal(stream,(uintptr_t)(-1-x)+1); } } +#if defined(HAVE_LONGLONG) && (long_long_bitsize > pointer_bitsize) + +void fprintdecimal (std::ostream& stream, unsigned long long x) +{ + #define bufsize (((sizeof(unsigned long long)*53)/22)+1) // 53/22 > 8*log(2)/log(10) + var char buf[bufsize+1]; + var char* bufptr = &buf[bufsize]; + *bufptr = '\0'; + do { + unsigned long long q = x / 10; + unsigned long long r = x % 10; + *--bufptr = '0'+r; + x = q; + } while (x > 0); + fprint(stream,bufptr); + #undef bufsize +} + +void fprintdecimal (std::ostream& stream, long long x) +{ + if (x >= 0) + fprintdecimal(stream,(unsigned long long)x); + else { + fprintchar(stream,'-'); + fprintdecimal(stream,(unsigned long long)(-1-x)+1); + } +} + +#endif + } // namespace cln diff --git a/src/base/output/cl_output_hex.cc b/src/base/output/cl_output_hex.cc index ea3888b..a463da8 100644 --- a/src/base/output/cl_output_hex.cc +++ b/src/base/output/cl_output_hex.cc @@ -11,15 +11,15 @@ namespace cln { -void fprinthexadecimal (std::ostream& stream, unsigned long x) +void fprinthexadecimal_impl (std::ostream& stream, uintptr_t x) { - #define bufsize 16 + #define bufsize (sizeof(uintptr_t)*2) var char buf[bufsize+1]; var char* bufptr = &buf[bufsize]; *bufptr = '\0'; do { - unsigned long q = x / 16; - unsigned long r = x % 16; + uintptr_t q = x / 16; + uintptr_t r = x % 16; *--bufptr = (r<10 ? '0'+r : 'A'-10+r); x = q; } while (x > 0); @@ -27,14 +27,44 @@ void fprinthexadecimal (std::ostream& stream, unsigned long x) #undef bufsize } -void fprinthexadecimal (std::ostream& stream, long x) +void fprinthexadecimal_impl (std::ostream& stream, intptr_t x) { if (x >= 0) - fprintdecimal(stream,(unsigned long)x); + fprintdecimal(stream,(uintptr_t)x); else { fprintchar(stream,'-'); - fprintdecimal(stream,(unsigned long)(-1-x)+1); + fprintdecimal(stream,(uintptr_t)(-1-x)+1); } } +#if defined(HAVE_LONGLONG) && (long_long_bitsize > pointer_bitsize) + +void fprinthexadecimal (std::ostream& stream, unsigned long long x) +{ + #define bufsize (sizeof(unsigned long long)*2) + var char buf[bufsize+1]; + var char* bufptr = &buf[bufsize]; + *bufptr = '\0'; + do { + unsigned long long q = x / 16; + unsigned long long r = x % 16; + *--bufptr = (r<10 ? '0'+r : 'A'-10+r); + x = q; + } while (x > 0); + fprint(stream,bufptr); + #undef bufsize +} + +void fprinthexadecimal (std::ostream& stream, long long x) +{ + if (x >= 0) + fprintdecimal(stream,(unsigned long long)x); + else { + fprintchar(stream,'-'); + fprintdecimal(stream,(unsigned long long)(-1-x)+1); + } +} + +#endif + } // namespace cln diff --git a/src/base/ring/cl_no_ring.cc b/src/base/ring/cl_no_ring.cc index d15be0f..a5df996 100644 --- a/src/base/ring/cl_no_ring.cc +++ b/src/base/ring/cl_no_ring.cc @@ -23,9 +23,9 @@ uninitialized_error_msg (const _cl_ring_element& obj) { std::ostringstream buf; fprint(buf, "Uninitialized ring element @0x"); - fprinthexadecimal(buf, (unsigned long)(void*)&obj); + fprinthexadecimal(buf, (uintptr_t)(void*)&obj); fprint(buf, ": 0x"); - fprinthexadecimal(buf, (unsigned long)obj.rep.word); + fprinthexadecimal(buf, (uintptr_t)obj.rep.word); return buf.str(); } @@ -34,13 +34,13 @@ uninitialized_error_msg (const _cl_ring_element& obj_x, const _cl_ring_element& { std::ostringstream buf; fprint(buf, "Uninitialized ring elements @0x"); - fprinthexadecimal(buf, (unsigned long)(void*)&obj_x); + fprinthexadecimal(buf, (uintptr_t)(void*)&obj_x); fprint(buf, ": 0x"); - fprinthexadecimal(buf, (unsigned long)obj_x.rep.word); + fprinthexadecimal(buf, (uintptr_t)obj_x.rep.word); fprint(buf, ", @0x"); - fprinthexadecimal(buf, (unsigned long)(void*)&obj_y); + fprinthexadecimal(buf, (uintptr_t)(void*)&obj_y); fprint(buf, ": 0x"); - fprinthexadecimal(buf, (unsigned long)obj_y.rep.word); + fprinthexadecimal(buf, (uintptr_t)obj_y.rep.word); return buf.str(); } -- 2.49.0