diff --git a/server/TracyPrint.cpp b/server/TracyPrint.cpp index dc96f5f8..41cc5a5b 100644 --- a/server/TracyPrint.cpp +++ b/server/TracyPrint.cpp @@ -214,44 +214,6 @@ const char* TimeToString( int64_t _ns ) return bufstart; } -const char* RealToString( double val ) -{ - enum { Pool = 8 }; - static char bufpool[Pool][64]; - static int bufsel = 0; - char* buf = bufpool[bufsel]; - bufsel = ( bufsel + 1 ) % Pool; - - *PrintFloat( buf, buf+64, val ) = '\0'; - auto ptr = buf; - if( *ptr == '-' ) ptr++; - - const auto vbegin = ptr; - - while( *ptr != '\0' && *ptr != '.' ) ptr++; - auto end = ptr; - while( *end != '\0' ) end++; - auto sz = end - ptr + 1; - - while( ptr - vbegin > 3 ) - { - ptr -= 3; - memmove( ptr+1, ptr, sz+3 ); - *ptr = ','; - sz += 4; - } - - while( *ptr != '\0' && *ptr != '.' ) ptr++; - - if( *ptr == '\0' ) return buf; - while( *ptr != '\0' ) ptr++; - ptr--; - while( *ptr == '0' ) ptr--; - if( *ptr != '.' && *ptr != ',' ) ptr++; - *ptr = '\0'; - return buf; -} - const char* MemSizeToString( int64_t val ) { enum { Pool = 8 }; @@ -328,4 +290,19 @@ const char* MemSizeToString( int64_t val ) return buf; } +namespace detail +{ + +char* RealToStringGetBuffer() +{ + enum { Pool = 8 }; + static char bufpool[Pool][64]; + static int bufsel = 0; + char* buf = bufpool[bufsel]; + bufsel = ( bufsel + 1 ) % Pool; + return buf; +} + +} + } diff --git a/server/TracyPrint.hpp b/server/TracyPrint.hpp index 75d15c94..082a0fe9 100644 --- a/server/TracyPrint.hpp +++ b/server/TracyPrint.hpp @@ -3,13 +3,63 @@ #if ( defined _MSC_VER && _MSVC_LANG >= 201703L ) || __cplusplus >= 201703L # include +# include #else # include #endif +#include "../common/TracyForceInline.hpp" + namespace tracy { +namespace detail +{ + +char* RealToStringGetBuffer(); + +static tracy_force_inline void RealToStringFloating( char* ptr, char* end ) +{ + if( *ptr == '-' ) ptr++; + const auto vbegin = ptr; + + while( *ptr != '\0' && *ptr != '.' ) ptr++; + auto sz = end - ptr + 1; + + while( ptr - vbegin > 3 ) + { + ptr -= 3; + memmove( ptr+1, ptr, sz+3 ); + *ptr = ','; + sz += 4; + } + + while( *ptr != '\0' && *ptr != '.' ) ptr++; + if( *ptr == '\0' ) return; + + while( *ptr != '\0' ) ptr++; + ptr--; + while( *ptr == '0' ) ptr--; + if( *ptr != '.' && *ptr != ',' ) ptr++; + *ptr = '\0'; +} + +static tracy_force_inline void RealToStringInteger( char* buf, char* end ) +{ + if( *buf == '-' ) buf++; + auto ptr = end; + auto sz = 1; + while( ptr - buf > 3 ) + { + ptr -= 3; + memmove( ptr+1, ptr, sz+3 ); + *ptr = ','; + sz += 4; + } +} + +} + template static inline char* PrintFloat( char* begin, char* end, T value, int precision ) { @@ -30,8 +80,34 @@ static inline char* PrintFloat( char* begin, char* end, T value ) #endif } +#if ( defined _MSC_VER && _MSVC_LANG >= 201703L ) || __cplusplus >= 201703L +template +static inline const char* RealToString( T val ) +{ + auto buf = detail::RealToStringGetBuffer(); + auto end = std::to_chars( buf, buf+64, val ).ptr; + *end = '\0'; + if constexpr ( std::is_integral_v ) + { + detail::RealToStringInteger( buf, end ); + } + else + { + detail::RealToStringFloating( buf, end ); + } + return buf; +} +#else +static inline const char* RealToString( double val ) +{ + auto buf = detail::RealToStringGetBuffer(); + const auto sz = sprintf( buf, "%f", val ); + detail::RealToStringFloating( buf, buf+sz ); + return buf; +} +#endif + const char* TimeToString( int64_t ns ); -const char* RealToString( double val ); const char* MemSizeToString( int64_t val ); }