From 4f424dfea0d41dbb197deb9afff4c33e338add15 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Beno=C3=AEt=20Legat?= Date: Tue, 6 May 2025 17:34:36 +0200 Subject: [PATCH 1/3] Fix TimeToStringExact with mininmal int64 input --- server/TracyPrint.cpp | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/server/TracyPrint.cpp b/server/TracyPrint.cpp index 62bd8e45..f9daaf1a 100644 --- a/server/TracyPrint.cpp +++ b/server/TracyPrint.cpp @@ -246,7 +246,11 @@ const char* TimeToStringExact( int64_t _ns ) bufsel = ( bufsel + 1 ) % Pool; uint64_t ns; - if( _ns < 0 ) + if( _ns == std::numeric_limits::min() ) + { + ns = -(_ns + 1) + 1; + } + else if( _ns < 0 ) { *buf = '-'; buf++; From 98c7768fd068d08b1c045a2fda9a0c018882d0da Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Beno=C3=AEt=20Legat?= Date: Tue, 6 May 2025 17:49:01 +0200 Subject: [PATCH 2/3] Add _int64_abs function --- server/TracyPrint.cpp | 38 ++++++++++++++++++++------------------ 1 file changed, 20 insertions(+), 18 deletions(-) diff --git a/server/TracyPrint.cpp b/server/TracyPrint.cpp index f9daaf1a..d77c1375 100644 --- a/server/TracyPrint.cpp +++ b/server/TracyPrint.cpp @@ -71,7 +71,7 @@ static inline void PrintSmallInt( char*& buf, uint64_t v ) static inline void PrintSmallInt0( char*& buf, uint64_t v ) { - assert( v < 1000 ); + assert( v < uint64_t(1000ll) ); if( v >= 100 ) { memcpy( buf, IntTable100 + v/10*2, 2 ); @@ -155,6 +155,22 @@ static inline void PrintSecondsFrac( char*& buf, uint64_t v ) } } +uint64_t _int64_abs( int64_t x ) +{ + if( x == std::numeric_limits::min() ) + { + return -(x + 1) + 1; + } + else if( x < 0 ) + { + return -x; + } + else + { + return x; + } +} + const char* TimeToString( int64_t _ns ) { enum { Pool = 8 }; @@ -164,16 +180,11 @@ const char* TimeToString( int64_t _ns ) char* bufstart = buf; bufsel = ( bufsel + 1 ) % Pool; - uint64_t ns; + uint64_t ns = _int64_abs(_ns); if( _ns < 0 ) { *buf = '-'; buf++; - ns = -_ns; - } - else - { - ns = _ns; } if( ns < 1000 ) @@ -245,20 +256,11 @@ const char* TimeToStringExact( int64_t _ns ) char* bufstart = buf; bufsel = ( bufsel + 1 ) % Pool; - uint64_t ns; - if( _ns == std::numeric_limits::min() ) - { - ns = -(_ns + 1) + 1; - } - else if( _ns < 0 ) + uint64_t ns = _int64_abs(_ns); + if( _ns < 0 ) { *buf = '-'; buf++; - ns = -_ns; - } - else - { - ns = _ns; } const char* numStart = buf; From 45f9ece0b1f7d0c1bef9d24f90e2850abbe4d428 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Beno=C3=AEt=20Legat?= Date: Sun, 18 May 2025 09:30:38 +0200 Subject: [PATCH 3/3] Apply review comment --- server/TracyPrint.cpp | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/server/TracyPrint.cpp b/server/TracyPrint.cpp index d77c1375..9111ddb0 100644 --- a/server/TracyPrint.cpp +++ b/server/TracyPrint.cpp @@ -71,7 +71,7 @@ static inline void PrintSmallInt( char*& buf, uint64_t v ) static inline void PrintSmallInt0( char*& buf, uint64_t v ) { - assert( v < uint64_t(1000ll) ); + assert( v < 1000 ); if( v >= 100 ) { memcpy( buf, IntTable100 + v/10*2, 2 ); @@ -157,13 +157,13 @@ static inline void PrintSecondsFrac( char*& buf, uint64_t v ) uint64_t _int64_abs( int64_t x ) { - if( x == std::numeric_limits::min() ) + if( x < 0 ) { - return -(x + 1) + 1; - } - else if( x < 0 ) - { - return -x; + // `-x` does not work if `x` is `std::numeric_limits::min()`, + // see https://github.com/wolfpld/tracy/pull/1040 + // This works though: + // https://graphics.stanford.edu/~seander/bithacks.html#IntegerAbs + return -(uint64_t)x; } else {