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