summaryrefslogtreecommitdiffhomepage
path: root/src/timer-ns.cpp
blob: f624ac3edc0bd0dec8e116279218e8db6075a33c (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
#include "nanosecond.inl"
#include "compat/assert.hpp"
#include "compat/debug.hpp"
#include <cr/Debug.h>
#include <mg/Functions.h>

namespace floormat {

Debug& operator<<(Debug& dbg, const Ns& val)
{
    const char* unit = "";
    double x = val.stamp;
    int precision;
    if (val >= 10*Second)
    {
        unit = "s";
        x *= 1e-9;
        precision = 1;
    }
    else if (val >= 1*Second)
    {
        unit = "ₘₛ";
        x *= 1e-6;
        precision = 2;

    }
    else if (val >= 100*Millisecond)
    {
        unit = "ₘₛ";
        x *= 1e-6;
        precision = 3;
    }
    else if (val >= 50*Microsecond)
    {
        unit = "ₘₛ";
        x *= 1e-6;
        precision = 4;
    }
    else if (val >= 1*Microsecond)
    {
        unit = "ₘₛ";
        x *= 1e-3;
        precision = 3;
    }
    else if (val > Ns{100})
    {
        char buf[64];
        std::snprintf(buf, sizeof buf, "%llu_%llu ₙₛ",
                      val.stamp / 1000, val.stamp % 1000);
        return dbg;
    }
    else
    {
        unit = " ₙₛ";
        precision = 0;
    }
    if (!precision)
        dbg << (uint64_t)x << Debug::nospace << unit;
    else
        dbg << fraction((float)x, precision) << Debug::nospace << unit;

    return dbg;
}

} // namespace floormat