tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

print.cc (5042B)


      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 
     16 #include "hwy/print.h"
     17 
     18 #include <stdio.h>
     19 
     20 #include "hwy/base.h"
     21 #include "hwy/detect_compiler_arch.h"
     22 
     23 namespace hwy {
     24 namespace detail {
     25 
     26 HWY_DLLEXPORT void TypeName(const TypeInfo& info, size_t N, char* string100) {
     27  const char prefix = info.is_float ? 'f' : (info.is_signed ? 'i' : 'u');
     28  // Omit the xN suffix for scalars.
     29  if (N == 1) {
     30    // NOLINTNEXTLINE
     31    snprintf(string100, 64, "%c%d", prefix,
     32             static_cast<int>(info.sizeof_t * 8));
     33  } else {
     34    // NOLINTNEXTLINE
     35    snprintf(string100, 64, "%c%dx%d", prefix,
     36             static_cast<int>(info.sizeof_t * 8), static_cast<int>(N));
     37  }
     38 }
     39 
     40 // The NOLINT are to suppress the warning about passing 100 instead of
     41 // `sizeof(string100)`, which is a pointer.
     42 HWY_DLLEXPORT void ToString(const TypeInfo& info, const void* ptr,
     43                            char* string100) {
     44  if (info.sizeof_t == 1) {
     45    if (info.is_signed) {
     46      int8_t byte;
     47      CopyBytes<1>(ptr, &byte);  // endian-safe: we ensured sizeof(T)=1.
     48      snprintf(string100, 100, "%d", byte);  // NOLINT
     49    } else {
     50      uint8_t byte;
     51      CopyBytes<1>(ptr, &byte);  // endian-safe: we ensured sizeof(T)=1.
     52      snprintf(string100, 100, "0x%02X", byte);  // NOLINT
     53    }
     54  } else if (info.sizeof_t == 2) {
     55    if (info.is_bf16) {
     56      const double value = static_cast<double>(F32FromBF16Mem(ptr));
     57      // NOLINTNEXTLINE
     58      snprintf(string100, 100, hwy::ScalarAbs(value) < 1E-3 ? "%.3E" : "%.3f",
     59               value);
     60    } else if (info.is_float) {
     61      const double value = static_cast<double>(F32FromF16Mem(ptr));
     62      // NOLINTNEXTLINE
     63      snprintf(string100, 100, hwy::ScalarAbs(value) < 1E-4 ? "%.4E" : "%.4f",
     64               value);
     65    } else {
     66      uint16_t bits;
     67      CopyBytes<2>(ptr, &bits);
     68      snprintf(string100, 100, "0x%04X", bits);  // NOLINT
     69    }
     70  } else if (info.sizeof_t == 4) {
     71    if (info.is_float) {
     72      float value;
     73      CopyBytes<4>(ptr, &value);
     74      // NOLINTNEXTLINE
     75      snprintf(string100, 100, hwy::ScalarAbs(value) < 1E-6f ? "%.9E" : "%.9f",
     76               static_cast<double>(value));
     77    } else if (info.is_signed) {
     78      int32_t value;
     79      CopyBytes<4>(ptr, &value);
     80      snprintf(string100, 100, "%d", value);  // NOLINT
     81    } else {
     82      uint32_t value;
     83      CopyBytes<4>(ptr, &value);
     84      snprintf(string100, 100, "%u", value);  // NOLINT
     85    }
     86  } else if (info.sizeof_t == 8) {
     87    if (info.is_float) {
     88      double value;
     89      CopyBytes<8>(ptr, &value);
     90      // NOLINTNEXTLINE
     91      snprintf(string100, 100, hwy::ScalarAbs(value) < 1E-9 ? "%.18E" : "%.18f",
     92               value);
     93    } else {
     94      const uint8_t* ptr8 = reinterpret_cast<const uint8_t*>(ptr);
     95      uint32_t lo, hi;
     96      CopyBytes<4>(ptr8 + (HWY_IS_LITTLE_ENDIAN ? 0 : 4), &lo);
     97      CopyBytes<4>(ptr8 + (HWY_IS_LITTLE_ENDIAN ? 4 : 0), &hi);
     98      snprintf(string100, 100, "0x%08x%08x", hi, lo);  // NOLINT
     99    }
    100  } else if (info.sizeof_t == 16) {
    101    HWY_ASSERT(!info.is_float && !info.is_signed && !info.is_bf16);
    102    const uint8_t* ptr8 = reinterpret_cast<const uint8_t*>(ptr);
    103    uint32_t words[4];
    104    CopyBytes<4>(ptr8 + (HWY_IS_LITTLE_ENDIAN ? 0 : 12), &words[0]);
    105    CopyBytes<4>(ptr8 + (HWY_IS_LITTLE_ENDIAN ? 4 : 8), &words[1]);
    106    CopyBytes<4>(ptr8 + (HWY_IS_LITTLE_ENDIAN ? 8 : 4), &words[2]);
    107    CopyBytes<4>(ptr8 + (HWY_IS_LITTLE_ENDIAN ? 12 : 0), &words[3]);
    108    // NOLINTNEXTLINE
    109    snprintf(string100, 100, "0x%08x%08x_%08x%08x", words[3], words[2],
    110             words[1], words[0]);
    111  }
    112 }
    113 
    114 HWY_DLLEXPORT void PrintArray(const TypeInfo& info, const char* caption,
    115                              const void* array_void, size_t N, size_t lane_u,
    116                              size_t max_lanes) {
    117  const uint8_t* array_bytes = reinterpret_cast<const uint8_t*>(array_void);
    118 
    119  char type_name[100];
    120  TypeName(info, N, type_name);
    121 
    122  const intptr_t lane = intptr_t(lane_u);
    123  const size_t begin = static_cast<size_t>(HWY_MAX(0, lane - 2));
    124  const size_t end = HWY_MIN(begin + max_lanes, N);
    125  fprintf(stderr, "%s %s [%d+ ->]:\n  ", type_name, caption,
    126          static_cast<int>(begin));
    127  for (size_t i = begin; i < end; ++i) {
    128    const void* ptr = array_bytes + i * info.sizeof_t;
    129    char str[100];
    130    ToString(info, ptr, str);
    131    fprintf(stderr, "%s,", str);
    132  }
    133  if (begin >= end) fprintf(stderr, "(out of bounds)");
    134  fprintf(stderr, "\n");
    135 }
    136 
    137 }  // namespace detail
    138 }  // namespace hwy