print.h 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. // Copyright 2022 Google LLC
  2. // SPDX-License-Identifier: Apache-2.0
  3. //
  4. // Licensed under the Apache License, Version 2.0 (the "License");
  5. // you may not use this file except in compliance with the License.
  6. // You may obtain a copy of the License at
  7. //
  8. // http://www.apache.org/licenses/LICENSE-2.0
  9. //
  10. // Unless required by applicable law or agreed to in writing, software
  11. // distributed under the License is distributed on an "AS IS" BASIS,
  12. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. // See the License for the specific language governing permissions and
  14. // limitations under the License.
  15. #ifndef HWY_PRINT_H_
  16. #define HWY_PRINT_H_
  17. // Helpers for printing vector lanes.
  18. #include <stddef.h>
  19. #include <stdio.h>
  20. #include "hwy/base.h"
  21. #include "hwy/highway_export.h"
  22. namespace hwy {
  23. namespace detail {
  24. // For implementing value comparisons etc. as type-erased functions to reduce
  25. // template bloat.
  26. struct TypeInfo {
  27. size_t sizeof_t;
  28. bool is_float;
  29. bool is_signed;
  30. bool is_bf16;
  31. };
  32. template <typename T>
  33. HWY_INLINE TypeInfo MakeTypeInfo() {
  34. TypeInfo info;
  35. info.sizeof_t = sizeof(T);
  36. info.is_float = IsFloat<T>();
  37. info.is_signed = IsSigned<T>();
  38. info.is_bf16 = IsSame<T, bfloat16_t>();
  39. return info;
  40. }
  41. HWY_DLLEXPORT void TypeName(const TypeInfo& info, size_t N, char* string100);
  42. HWY_DLLEXPORT void ToString(const TypeInfo& info, const void* ptr,
  43. char* string100);
  44. HWY_DLLEXPORT void PrintArray(const TypeInfo& info, const char* caption,
  45. const void* array_void, size_t N,
  46. size_t lane_u = 0, size_t max_lanes = 7);
  47. } // namespace detail
  48. template <typename T>
  49. HWY_NOINLINE void PrintValue(T value) {
  50. char str[100];
  51. detail::ToString(hwy::detail::MakeTypeInfo<T>(), &value, str);
  52. fprintf(stderr, "%s,", str);
  53. }
  54. template <typename T>
  55. HWY_NOINLINE void PrintArray(const T* value, size_t count) {
  56. detail::PrintArray(hwy::detail::MakeTypeInfo<T>(), "", value, count, 0,
  57. count);
  58. }
  59. } // namespace hwy
  60. #endif // HWY_PRINT_H_