xref: /llvm-project/libc/benchmarks/gpu/BenchmarkLogger.cpp (revision 5ff3ff33ff930e4ec49da7910612d8a41eb068cb)
102b57dedSjameshu15869 #include "benchmarks/gpu/BenchmarkLogger.h"
202b57dedSjameshu15869 #include "src/__support/CPP/string.h"
302b57dedSjameshu15869 #include "src/__support/CPP/string_view.h"
402b57dedSjameshu15869 #include "src/__support/OSUtil/io.h"               // write_to_stderr
502b57dedSjameshu15869 #include "src/__support/big_int.h"                 // is_big_int
6*5ff3ff33SPetr Hosek #include "src/__support/macros/config.h"
702b57dedSjameshu15869 #include "src/__support/macros/properties/types.h" // LIBC_TYPES_HAS_INT128
802b57dedSjameshu15869 #include "src/__support/uint128.h"
902b57dedSjameshu15869 
1002b57dedSjameshu15869 #include <stdint.h>
1102b57dedSjameshu15869 
12*5ff3ff33SPetr Hosek namespace LIBC_NAMESPACE_DECL {
1302b57dedSjameshu15869 namespace benchmarks {
1402b57dedSjameshu15869 
1502b57dedSjameshu15869 // cpp::string_view specialization
1602b57dedSjameshu15869 template <>
1702b57dedSjameshu15869 BenchmarkLogger &
1802b57dedSjameshu15869     BenchmarkLogger::operator<< <cpp::string_view>(cpp::string_view str) {
1902b57dedSjameshu15869   LIBC_NAMESPACE::write_to_stderr(str);
2002b57dedSjameshu15869   return *this;
2102b57dedSjameshu15869 }
2202b57dedSjameshu15869 
2302b57dedSjameshu15869 // cpp::string specialization
2402b57dedSjameshu15869 template <>
2502b57dedSjameshu15869 BenchmarkLogger &BenchmarkLogger::operator<< <cpp::string>(cpp::string str) {
2602b57dedSjameshu15869   return *this << static_cast<cpp::string_view>(str);
2702b57dedSjameshu15869 }
2802b57dedSjameshu15869 
2902b57dedSjameshu15869 // const char* specialization
3002b57dedSjameshu15869 template <>
3102b57dedSjameshu15869 BenchmarkLogger &BenchmarkLogger::operator<< <const char *>(const char *str) {
3202b57dedSjameshu15869   return *this << cpp::string_view(str);
3302b57dedSjameshu15869 }
3402b57dedSjameshu15869 
3502b57dedSjameshu15869 // char* specialization
3602b57dedSjameshu15869 template <> BenchmarkLogger &BenchmarkLogger::operator<< <char *>(char *str) {
3702b57dedSjameshu15869   return *this << cpp::string_view(str);
3802b57dedSjameshu15869 }
3902b57dedSjameshu15869 
4002b57dedSjameshu15869 // char specialization
4102b57dedSjameshu15869 template <> BenchmarkLogger &BenchmarkLogger::operator<<(char ch) {
4202b57dedSjameshu15869   return *this << cpp::string_view(&ch, 1);
4302b57dedSjameshu15869 }
4402b57dedSjameshu15869 
4502b57dedSjameshu15869 // bool specialization
4602b57dedSjameshu15869 template <> BenchmarkLogger &BenchmarkLogger::operator<<(bool cond) {
4702b57dedSjameshu15869   return *this << (cond ? "true" : "false");
4802b57dedSjameshu15869 }
4902b57dedSjameshu15869 
5002b57dedSjameshu15869 // void * specialization
5102b57dedSjameshu15869 template <> BenchmarkLogger &BenchmarkLogger::operator<<(void *addr) {
5202b57dedSjameshu15869   return *this << "0x" << cpp::to_string(reinterpret_cast<uintptr_t>(addr));
5302b57dedSjameshu15869 }
5402b57dedSjameshu15869 
5502b57dedSjameshu15869 template <typename T> BenchmarkLogger &BenchmarkLogger::operator<<(T t) {
5602b57dedSjameshu15869   if constexpr (is_big_int_v<T> ||
5702b57dedSjameshu15869                 (cpp::is_integral_v<T> && cpp::is_unsigned_v<T> &&
5802b57dedSjameshu15869                  (sizeof(T) > sizeof(uint64_t)))) {
5902b57dedSjameshu15869     static_assert(sizeof(T) % 8 == 0, "Unsupported size of UInt");
6002b57dedSjameshu15869     const IntegerToString<T, radix::Hex::WithPrefix> buffer(t);
6102b57dedSjameshu15869     return *this << buffer.view();
6202b57dedSjameshu15869   } else {
6302b57dedSjameshu15869     return *this << cpp::to_string(t);
6402b57dedSjameshu15869   }
6502b57dedSjameshu15869 }
6602b57dedSjameshu15869 
6702b57dedSjameshu15869 // is_integral specializations
6802b57dedSjameshu15869 // char is already specialized to handle character
6902b57dedSjameshu15869 template BenchmarkLogger &BenchmarkLogger::operator<< <short>(short);
7002b57dedSjameshu15869 template BenchmarkLogger &BenchmarkLogger::operator<< <int>(int);
7102b57dedSjameshu15869 template BenchmarkLogger &BenchmarkLogger::operator<< <long>(long);
7202b57dedSjameshu15869 template BenchmarkLogger &BenchmarkLogger::operator<< <long long>(long long);
7302b57dedSjameshu15869 template BenchmarkLogger &
7402b57dedSjameshu15869     BenchmarkLogger::operator<< <unsigned char>(unsigned char);
7502b57dedSjameshu15869 template BenchmarkLogger &
7602b57dedSjameshu15869     BenchmarkLogger::operator<< <unsigned short>(unsigned short);
7702b57dedSjameshu15869 template BenchmarkLogger &
7802b57dedSjameshu15869     BenchmarkLogger::operator<< <unsigned int>(unsigned int);
7902b57dedSjameshu15869 template BenchmarkLogger &
8002b57dedSjameshu15869     BenchmarkLogger::operator<< <unsigned long>(unsigned long);
8102b57dedSjameshu15869 template BenchmarkLogger &
8202b57dedSjameshu15869     BenchmarkLogger::operator<< <unsigned long long>(unsigned long long);
8302b57dedSjameshu15869 
8402b57dedSjameshu15869 #ifdef LIBC_TYPES_HAS_INT128
8502b57dedSjameshu15869 template BenchmarkLogger &
8602b57dedSjameshu15869     BenchmarkLogger::operator<< <__uint128_t>(__uint128_t);
8702b57dedSjameshu15869 #endif // LIBC_TYPES_HAS_INT128
8802b57dedSjameshu15869 template BenchmarkLogger &BenchmarkLogger::operator<< <UInt<128>>(UInt<128>);
8902b57dedSjameshu15869 template BenchmarkLogger &BenchmarkLogger::operator<< <UInt<192>>(UInt<192>);
9002b57dedSjameshu15869 template BenchmarkLogger &BenchmarkLogger::operator<< <UInt<256>>(UInt<256>);
9102b57dedSjameshu15869 template BenchmarkLogger &BenchmarkLogger::operator<< <UInt<320>>(UInt<320>);
9202b57dedSjameshu15869 
9302b57dedSjameshu15869 // TODO: Add floating point formatting once it's supported by StringStream.
9402b57dedSjameshu15869 
9502b57dedSjameshu15869 BenchmarkLogger log;
9602b57dedSjameshu15869 
9702b57dedSjameshu15869 } // namespace benchmarks
98*5ff3ff33SPetr Hosek } // namespace LIBC_NAMESPACE_DECL
99