P4C
The P4 Compiler
Loading...
Searching...
No Matches
n4.h
1#ifndef LIB_N4_H_
2#define LIB_N4_H_
3
4#include <cstdint>
5#include <iomanip>
6#include <ostream>
7
8namespace P4 {
9
10class n4 {
11 /* format a value as 4 chars */
12 uint64_t val, div;
13
14 public:
15 explicit n4(uint64_t v, uint64_t d = 1) : val(v), div(d) {}
16 std::ostream &print(std::ostream &os) const {
17 uint64_t v;
18 if (val % div && val < div * 100) {
19 if ((v = val * 1000 + div / 2) < div * 1000) {
20 char ofill = os.fill('0');
21 os << '.' << std::setw(3) << v / div;
22 os.fill(ofill);
23 return os;
24 }
25 if ((v = val * 100 + div / 2) < div * 1000) {
26 char ofill = os.fill('0');
27 os << val / div << '.' << std::setw(2) << (v / div) % 100;
28 os.fill(ofill);
29 return os;
30 }
31 if ((v = val * 10 + div / 2) < div * 1000) {
32 os << val / div << '.' << (v / div) % 10;
33 return os;
34 }
35 }
36 v = (val + div / 2) / div;
37 if (v < 10000) {
38 os << std::setw(4) << v;
39 } else if (v < UINT64_C(999500)) {
40 os << std::setw(3) << (v + 500) / 1000 << 'K';
41 } else if (v < UINT64_C(9950000)) {
42 v = (v + UINT64_C(50000)) / UINT64_C(100000);
43 os << v / 10 << '.' << v % 10 << 'M';
44 } else if (v < UINT64_C(999500000)) {
45 os << std::setw(3) << (v + UINT64_C(500000)) / UINT64_C(1000000) << 'M';
46 } else if (v < UINT64_C(9950000000)) {
47 v = (v + UINT64_C(50000000)) / UINT64_C(100000000);
48 os << v / 10 << '.' << v % 10 << 'G';
49 } else if (v < UINT64_C(999500000000)) {
50 os << std::setw(3) << (v + UINT64_C(500000000)) / UINT64_C(1000000000) << 'G';
51 } else if (v < UINT64_C(9950000000000)) {
52 v = (v + UINT64_C(50000000000)) / UINT64_C(100000000000);
53 os << v / 10 << '.' << v % 10 << 'T';
54 } else {
55 os << std::setw(3) << (v + UINT64_C(500000000000)) / UINT64_C(1000000000000) << 'T';
56 }
57 return os;
58 }
59};
60
61inline std::ostream &operator<<(std::ostream &os, n4 v) { return v.print(os); }
62
63} // namespace P4
64
65#endif /* LIB_N4_H_ */
Definition n4.h:10
TODO: this is not really specific to BMV2, it should reside somewhere else.
Definition applyOptionsPragmas.cpp:24