Welcome to mirror list, hosted at ThFree Co, Russian Federation.

TracyPrint.hpp « server - github.com/wolfpld/tracy.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: d38245e3591cba4fcbf047bff75808cb91ffdc77 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
#ifndef __TRACYPRINT_HPP__
#define __TRACYPRINT_HPP__

#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
#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
#  include <stdio.h>
#endif

#include <stdint.h>
#include <string.h>

#include "../public/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<typename T>
static inline char* PrintFloat( char* begin, char* end, T value, int precision )
{
#ifndef NO_CHARCONV
    return std::to_chars( begin, end, value, std::chars_format::fixed, precision ).ptr;
#else
    return begin + sprintf( begin, "%.*f", precision, value );
#endif
}

template<typename T>
static inline char* PrintFloat( char* begin, char* end, T value )
{
#ifndef NO_CHARCONV
    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

const char* TimeToString( int64_t ns );
const char* TimeToStringExact( int64_t ns );
const char* MemSizeToString( int64_t val );
const char* LocationToString( const char* fn, uint32_t line );

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 );
}

}

#endif