xref: /llvm-project/libc/benchmarks/gpu/BenchmarkLogger.cpp (revision ce9035f5bd3aa09cbd899489cdbc7f6c18acf1e3)
1 #include "benchmarks/gpu/BenchmarkLogger.h"
2 #include "src/__support/CPP/string.h"
3 #include "src/__support/CPP/string_view.h"
4 #include "src/__support/OSUtil/io.h"               // write_to_stderr
5 #include "src/__support/big_int.h"                 // is_big_int
6 #include "src/__support/macros/properties/types.h" // LIBC_TYPES_HAS_INT128
7 #include "src/__support/uint128.h"
8 
9 #include <stdint.h>
10 
11 namespace LIBC_NAMESPACE {
12 namespace benchmarks {
13 
14 // cpp::string_view specialization
15 template <>
16 BenchmarkLogger &
17     BenchmarkLogger::operator<< <cpp::string_view>(cpp::string_view str) {
18   LIBC_NAMESPACE::write_to_stderr(str);
19   return *this;
20 }
21 
22 // cpp::string specialization
23 template <>
24 BenchmarkLogger &BenchmarkLogger::operator<< <cpp::string>(cpp::string str) {
25   return *this << static_cast<cpp::string_view>(str);
26 }
27 
28 // const char* specialization
29 template <>
30 BenchmarkLogger &BenchmarkLogger::operator<< <const char *>(const char *str) {
31   return *this << cpp::string_view(str);
32 }
33 
34 // char* specialization
35 template <> BenchmarkLogger &BenchmarkLogger::operator<< <char *>(char *str) {
36   return *this << cpp::string_view(str);
37 }
38 
39 // char specialization
40 template <> BenchmarkLogger &BenchmarkLogger::operator<<(char ch) {
41   return *this << cpp::string_view(&ch, 1);
42 }
43 
44 // bool specialization
45 template <> BenchmarkLogger &BenchmarkLogger::operator<<(bool cond) {
46   return *this << (cond ? "true" : "false");
47 }
48 
49 // void * specialization
50 template <> BenchmarkLogger &BenchmarkLogger::operator<<(void *addr) {
51   return *this << "0x" << cpp::to_string(reinterpret_cast<uintptr_t>(addr));
52 }
53 
54 template <typename T> BenchmarkLogger &BenchmarkLogger::operator<<(T t) {
55   if constexpr (is_big_int_v<T> ||
56                 (cpp::is_integral_v<T> && cpp::is_unsigned_v<T> &&
57                  (sizeof(T) > sizeof(uint64_t)))) {
58     static_assert(sizeof(T) % 8 == 0, "Unsupported size of UInt");
59     const IntegerToString<T, radix::Hex::WithPrefix> buffer(t);
60     return *this << buffer.view();
61   } else {
62     return *this << cpp::to_string(t);
63   }
64 }
65 
66 // is_integral specializations
67 // char is already specialized to handle character
68 template BenchmarkLogger &BenchmarkLogger::operator<< <short>(short);
69 template BenchmarkLogger &BenchmarkLogger::operator<< <int>(int);
70 template BenchmarkLogger &BenchmarkLogger::operator<< <long>(long);
71 template BenchmarkLogger &BenchmarkLogger::operator<< <long long>(long long);
72 template BenchmarkLogger &
73     BenchmarkLogger::operator<< <unsigned char>(unsigned char);
74 template BenchmarkLogger &
75     BenchmarkLogger::operator<< <unsigned short>(unsigned short);
76 template BenchmarkLogger &
77     BenchmarkLogger::operator<< <unsigned int>(unsigned int);
78 template BenchmarkLogger &
79     BenchmarkLogger::operator<< <unsigned long>(unsigned long);
80 template BenchmarkLogger &
81     BenchmarkLogger::operator<< <unsigned long long>(unsigned long long);
82 
83 #ifdef LIBC_TYPES_HAS_INT128
84 template BenchmarkLogger &
85     BenchmarkLogger::operator<< <__uint128_t>(__uint128_t);
86 #endif // LIBC_TYPES_HAS_INT128
87 template BenchmarkLogger &BenchmarkLogger::operator<< <UInt<128>>(UInt<128>);
88 template BenchmarkLogger &BenchmarkLogger::operator<< <UInt<192>>(UInt<192>);
89 template BenchmarkLogger &BenchmarkLogger::operator<< <UInt<256>>(UInt<256>);
90 template BenchmarkLogger &BenchmarkLogger::operator<< <UInt<320>>(UInt<320>);
91 
92 // TODO: Add floating point formatting once it's supported by StringStream.
93 
94 BenchmarkLogger log;
95 
96 } // namespace benchmarks
97 } // namespace LIBC_NAMESPACE
98