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
12namespace LIBC_NAMESPACE_DECL {
13namespace benchmarks {
14
15// cpp::string_view specialization
16template <>
17BenchmarkLogger &
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
24template <>
25BenchmarkLogger &BenchmarkLogger::operator<< <cpp::string>(cpp::string str) {
26 return *this << static_cast<cpp::string_view>(str);
27}
28
29// const char* specialization
30template <>
31BenchmarkLogger &BenchmarkLogger::operator<< <const char *>(const char *str) {
32 return *this << cpp::string_view(str);
33}
34
35// char* specialization
36template <> BenchmarkLogger &BenchmarkLogger::operator<< <char *>(char *str) {
37 return *this << cpp::string_view(str);
38}
39
40// char specialization
41template <> BenchmarkLogger &BenchmarkLogger::operator<<(char ch) {
42 return *this << cpp::string_view(&ch, 1);
43}
44
45// bool specialization
46template <> BenchmarkLogger &BenchmarkLogger::operator<<(bool cond) {
47 return *this << (cond ? "true" : "false");
48}
49
50// void * specialization
51template <> BenchmarkLogger &BenchmarkLogger::operator<<(void *addr) {
52 return *this << "0x" << cpp::to_string(reinterpret_cast<uintptr_t>(addr));
53}
54
55template <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
69template BenchmarkLogger &BenchmarkLogger::operator<< <short>(short);
70template BenchmarkLogger &BenchmarkLogger::operator<< <int>(int);
71template BenchmarkLogger &BenchmarkLogger::operator<< <long>(long);
72template BenchmarkLogger &BenchmarkLogger::operator<< <long long>(long long);
73template BenchmarkLogger &
74 BenchmarkLogger::operator<< <unsigned char>(unsigned char);
75template BenchmarkLogger &
76 BenchmarkLogger::operator<< <unsigned short>(unsigned short);
77template BenchmarkLogger &
78 BenchmarkLogger::operator<< <unsigned int>(unsigned int);
79template BenchmarkLogger &
80 BenchmarkLogger::operator<< <unsigned long>(unsigned long);
81template BenchmarkLogger &
82 BenchmarkLogger::operator<< <unsigned long long>(unsigned long long);
83
84#ifdef LIBC_TYPES_HAS_INT128
85template BenchmarkLogger &
86 BenchmarkLogger::operator<< <__uint128_t>(__uint128_t);
87#endif // LIBC_TYPES_HAS_INT128
88template BenchmarkLogger &BenchmarkLogger::operator<< <UInt<128>>(UInt<128>);
89template BenchmarkLogger &BenchmarkLogger::operator<< <UInt<192>>(UInt<192>);
90template BenchmarkLogger &BenchmarkLogger::operator<< <UInt<256>>(UInt<256>);
91template BenchmarkLogger &BenchmarkLogger::operator<< <UInt<320>>(UInt<320>);
92
93// TODO: Add floating point formatting once it's supported by StringStream.
94
95BenchmarkLogger log;
96
97} // namespace benchmarks
98} // namespace LIBC_NAMESPACE_DECL
99

source code of libc/benchmarks/gpu/BenchmarkLogger.cpp