tracy/server/TracyPrint.hpp

153 lines
3.3 KiB
C++
Raw Permalink Normal View History

2019-06-18 18:43:28 +00:00
#ifndef __TRACYPRINT_HPP__
#define __TRACYPRINT_HPP__
2020-01-31 00:30:33 +00:00
#if ( defined _MSC_VER && _MSVC_LANG >= 201703L ) || __cplusplus >= 201703L
# if __has_include(<charconv>) && __has_include(<type_traits>)
# include <charconv>
# include <type_traits>
# else
# define NO_CHARCONV
# endif
2020-01-31 00:30:33 +00:00
#else
# define NO_CHARCONV
#endif
#if defined _MSC_VER && _MSC_VER < 1924
# define NO_CHARCONV
#endif
#ifdef __GNUC__
# define NO_CHARCONV
#endif
#ifdef NO_CHARCONV
2020-01-31 00:30:33 +00:00
# include <stdio.h>
#endif
2020-01-31 00:19:08 +00:00
2022-04-26 19:23:22 +00:00
#include <stdint.h>
#include <string.h>
#include "../public/common/TracyForceInline.hpp"
2019-06-18 18:43:28 +00:00
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;
}
}
}
2020-01-31 00:19:08 +00:00
template<typename T>
static inline char* PrintFloat( char* begin, char* end, T value, int precision )
{
#ifndef NO_CHARCONV
2020-01-31 00:30:33 +00:00
return std::to_chars( begin, end, value, std::chars_format::fixed, precision ).ptr;
#else
2020-01-31 00:19:08 +00:00
return begin + sprintf( begin, "%.*f", precision, value );
2020-01-31 00:30:33 +00:00
#endif
2020-01-31 00:19:08 +00:00
}
2020-01-31 00:44:38 +00:00
template<typename T>
static inline char* PrintFloat( char* begin, char* end, T value )
{
#ifndef NO_CHARCONV
2020-01-31 00:44:38 +00:00
return std::to_chars( begin, end, value, std::chars_format::fixed ).ptr;
#else
return begin + sprintf( begin, "%f", value );
#endif
}
#ifndef NO_CHARCONV
template<typename T>
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<T> )
{
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
2019-06-18 18:43:28 +00:00
const char* TimeToString( int64_t ns );
2020-02-02 13:32:18 +00:00
const char* TimeToStringExact( int64_t ns );
2019-06-18 18:43:28 +00:00
const char* MemSizeToString( int64_t val );
2021-06-19 10:33:23 +00:00
const char* LocationToString( const char* fn, uint32_t line );
2019-06-18 18:43:28 +00:00
static tracy_force_inline void PrintStringPercent( char* buf, const char* string, double percent )
{
const auto ssz = strlen( string );
memcpy( buf, string, ssz );
memcpy( buf+ssz, " (", 2 );
auto end = PrintFloat( buf+ssz+2, buf+128, percent, 2 );
memcpy( end, "%)", 3 );
}
static tracy_force_inline void PrintStringPercent( char* buf, double percent )
{
memcpy( buf, "(", 2 );
auto end = PrintFloat( buf+1, buf+64, percent, 2 );
memcpy( end, "%)", 3 );
}
2019-06-18 18:43:28 +00:00
}
#endif