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