Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion tsl/platform/abi.cc
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ extern "C" char* __unDName(char* output_string, const char* name,
namespace tsl {
namespace port {

string MaybeAbiDemangle(const char* name) {
std::string MaybeAbiDemangle(const char* name) {
#if defined(_MSC_VER)
std::unique_ptr<char> demangled{__unDName(nullptr, name, 0, std::malloc,
std::free,
Expand Down
13 changes: 7 additions & 6 deletions tsl/platform/base64.cc
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ namespace tsl {
namespace {
// This array must have signed type.
// clang-format off
constexpr int8 kBase64Bytes[128] = {
constexpr int8_t kBase64Bytes[128] = {
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
Expand All @@ -50,7 +50,7 @@ constexpr char kPadChar = '=';
// Converts a char (8 bits) into a 6-bit value for decoding. If the input char
// is invalid for base64 encoding, the return value has at least its upper 25
// bits set.
inline uint32 Convert(char x) {
inline uint32_t Convert(char x) {
// If x < 128, then we look up x in the table. If x is valid, then the table
// will have a value <= 0x3F, otherwise the table will have -1. If x >= 128,
// we still do some table lookup, but the value is ignored since we explicitly
Expand All @@ -59,13 +59,14 @@ inline uint32 Convert(char x) {
const int8_t y = kBase64Bytes[x & 0x7F] | (x & 0x80);
// Casting from int8 to int32 preserves sign by sign extension. If y was
// negative, at least its 25 high bits of the return value are set.
const int32_t z = static_cast<int32>(y);
return static_cast<uint32>(z);
const int32_t z = static_cast<int32_t>(y);
return static_cast<uint32_t>(z);
}

absl::Status DecodeThreeChars(const char* codes, char* result) {
const uint32 packed = (Convert(codes[0]) << 18) | (Convert(codes[1]) << 12) |
(Convert(codes[2]) << 6) | (Convert(codes[3]));
const uint32_t packed = (Convert(codes[0]) << 18) |
(Convert(codes[1]) << 12) | (Convert(codes[2]) << 6) |
(Convert(codes[3]));
// Convert() return value has upper 25 bits set if input is invalid.
// Therefore `packed` has high bits set iff at least one of code is invalid.
if (TF_PREDICT_FALSE((packed & 0xFF000000) != 0)) {
Expand Down
48 changes: 24 additions & 24 deletions tsl/platform/coding.cc
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ limitations under the License.
namespace tsl {
namespace core {

void EncodeFixed16(char* buf, uint16 value) {
void EncodeFixed16(char* buf, uint16_t value) {
if (port::kLittleEndian) {
memcpy(buf, &value, sizeof(value));
} else {
Expand All @@ -32,7 +32,7 @@ void EncodeFixed16(char* buf, uint16 value) {
}
}

void EncodeFixed32(char* buf, uint32 value) {
void EncodeFixed32(char* buf, uint32_t value) {
if (port::kLittleEndian) {
memcpy(buf, &value, sizeof(value));
} else {
Expand All @@ -43,7 +43,7 @@ void EncodeFixed32(char* buf, uint32 value) {
}
}

void EncodeFixed64(char* buf, uint64 value) {
void EncodeFixed64(char* buf, uint64_t value) {
if (port::kLittleEndian) {
memcpy(buf, &value, sizeof(value));
} else {
Expand All @@ -58,25 +58,25 @@ void EncodeFixed64(char* buf, uint64 value) {
}
}

void PutFixed16(string* dst, uint16 value) {
void PutFixed16(std::string* dst, uint16_t value) {
char buf[sizeof(value)];
EncodeFixed16(buf, value);
dst->append(buf, sizeof(buf));
}

void PutFixed32(string* dst, uint32 value) {
void PutFixed32(std::string* dst, uint32_t value) {
char buf[sizeof(value)];
EncodeFixed32(buf, value);
dst->append(buf, sizeof(buf));
}

void PutFixed64(string* dst, uint64 value) {
void PutFixed64(std::string* dst, uint64_t value) {
char buf[sizeof(value)];
EncodeFixed64(buf, value);
dst->append(buf, sizeof(buf));
}

char* EncodeVarint32(char* dst, uint32 v) {
char* EncodeVarint32(char* dst, uint32_t v) {
// Operate on characters as unsigneds
unsigned char* ptr = reinterpret_cast<unsigned char*>(dst);
static const int B = 128;
Expand Down Expand Up @@ -104,19 +104,19 @@ char* EncodeVarint32(char* dst, uint32 v) {
return reinterpret_cast<char*>(ptr);
}

void PutVarint32(string* dst, uint32 v) {
void PutVarint32(std::string* dst, uint32_t v) {
char buf[5];
char* ptr = EncodeVarint32(buf, v);
dst->append(buf, ptr - buf);
}

void PutVarint32(tstring* dst, uint32 v) {
void PutVarint32(tstring* dst, uint32_t v) {
char buf[5];
char* ptr = EncodeVarint32(buf, v);
dst->append(buf, ptr - buf);
}

char* EncodeVarint64(char* dst, uint64 v) {
char* EncodeVarint64(char* dst, uint64_t v) {
static const int B = 128;
unsigned char* ptr = reinterpret_cast<unsigned char*>(dst);
while (v >= B) {
Expand All @@ -127,13 +127,13 @@ char* EncodeVarint64(char* dst, uint64 v) {
return reinterpret_cast<char*>(ptr);
}

void PutVarint64(string* dst, uint64 v) {
void PutVarint64(std::string* dst, uint64_t v) {
char buf[10];
char* ptr = EncodeVarint64(buf, v);
dst->append(buf, ptr - buf);
}

void PutVarint64(tstring* dst, uint64 v) {
void PutVarint64(tstring* dst, uint64_t v) {
char buf[10];
char* ptr = EncodeVarint64(buf, v);
dst->append(buf, ptr - buf);
Expand All @@ -148,9 +148,9 @@ int VarintLength(uint64_t v) {
return len;
}

const char* GetVarint32Ptr(const char* p, const char* limit, uint32* value) {
const char* GetVarint32Ptr(const char* p, const char* limit, uint32_t* value) {
if (p < limit) {
uint32 result = *(reinterpret_cast<const unsigned char*>(p));
uint32_t result = *(reinterpret_cast<const unsigned char*>(p));
if ((result & 128) == 0) {
*value = result;
return p + 1;
Expand All @@ -160,10 +160,10 @@ const char* GetVarint32Ptr(const char* p, const char* limit, uint32* value) {
}

const char* GetVarint32PtrFallback(const char* p, const char* limit,
uint32* value) {
uint32 result = 0;
for (uint32 shift = 0; shift <= 28 && p < limit; shift += 7) {
uint32 byte = *(reinterpret_cast<const unsigned char*>(p));
uint32_t* value) {
uint32_t result = 0;
for (uint32_t shift = 0; shift <= 28 && p < limit; shift += 7) {
uint32_t byte = *(reinterpret_cast<const unsigned char*>(p));
p++;
if (byte & 128) {
// More bytes are present
Expand All @@ -177,7 +177,7 @@ const char* GetVarint32PtrFallback(const char* p, const char* limit,
return nullptr;
}

bool GetVarint32(absl::string_view* input, uint32* value) {
bool GetVarint32(absl::string_view* input, uint32_t* value) {
const char* p = input->data();
const char* limit = p + input->size();
const char* q = GetVarint32Ptr(p, limit, value);
Expand All @@ -189,10 +189,10 @@ bool GetVarint32(absl::string_view* input, uint32* value) {
}
}

const char* GetVarint64Ptr(const char* p, const char* limit, uint64* value) {
uint64 result = 0;
for (uint32 shift = 0; shift <= 63 && p < limit; shift += 7) {
uint64 byte = *(reinterpret_cast<const unsigned char*>(p));
const char* GetVarint64Ptr(const char* p, const char* limit, uint64_t* value) {
uint64_t result = 0;
for (uint32_t shift = 0; shift <= 63 && p < limit; shift += 7) {
uint64_t byte = *(reinterpret_cast<const unsigned char*>(p));
p++;
if (byte & 128) {
// More bytes are present
Expand All @@ -206,7 +206,7 @@ const char* GetVarint64Ptr(const char* p, const char* limit, uint64* value) {
return nullptr;
}

bool GetVarint64(absl::string_view* input, uint64* value) {
bool GetVarint64(absl::string_view* input, uint64_t* value) {
const char* p = input->data();
const char* limit = p + input->size();
const char* q = GetVarint64Ptr(p, limit, value);
Expand Down
38 changes: 20 additions & 18 deletions tsl/platform/coding.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,32 +37,34 @@ static const int kMaxVarint64Bytes = 10;

// Lower-level versions of Put... that write directly into a character buffer
// REQUIRES: dst has enough space for the value being written
extern void EncodeFixed16(char* dst, uint16 value);
extern void EncodeFixed32(char* dst, uint32 value);
extern void EncodeFixed64(char* dst, uint64 value);
extern void PutFixed16(string* dst, uint16 value);
extern void PutFixed32(string* dst, uint32 value);
extern void PutFixed64(string* dst, uint64 value);
extern void EncodeFixed16(char* dst, uint16_t value);
extern void EncodeFixed32(char* dst, uint32_t value);
extern void EncodeFixed64(char* dst, uint64_t value);
extern void PutFixed16(std::string* dst, uint16_t value);
extern void PutFixed32(std::string* dst, uint32_t value);
extern void PutFixed64(std::string* dst, uint64_t value);

extern void PutVarint32(string* dst, uint32 value);
extern void PutVarint64(string* dst, uint64 value);
extern void PutVarint32(std::string* dst, uint32_t value);
extern void PutVarint64(std::string* dst, uint64_t value);

extern void PutVarint32(tstring* dst, uint32 value);
extern void PutVarint64(tstring* dst, uint64 value);
extern void PutVarint32(tstring* dst, uint32_t value);
extern void PutVarint64(tstring* dst, uint64_t value);

extern bool GetVarint32(absl::string_view* input, uint32* value);
extern bool GetVarint64(absl::string_view* input, uint64* value);
extern bool GetVarint32(absl::string_view* input, uint32_t* value);
extern bool GetVarint64(absl::string_view* input, uint64_t* value);

extern const char* GetVarint32Ptr(const char* p, const char* limit, uint32* v);
extern const char* GetVarint64Ptr(const char* p, const char* limit, uint64* v);
extern const char* GetVarint32Ptr(const char* p, const char* limit,
uint32_t* v);
extern const char* GetVarint64Ptr(const char* p, const char* limit,
uint64_t* v);

// Internal routine for use by fallback path of GetVarint32Ptr
extern const char* GetVarint32PtrFallback(const char* p, const char* limit,
uint32* value);
uint32_t* value);
extern const char* GetVarint32Ptr(const char* p, const char* limit,
uint32* value);
extern char* EncodeVarint32(char* dst, uint32 v);
extern char* EncodeVarint64(char* dst, uint64 v);
uint32_t* value);
extern char* EncodeVarint32(char* dst, uint32_t v);
extern char* EncodeVarint64(char* dst, uint64_t v);

// Returns the length of the varint32 or varint64 encoding of "v"
extern int VarintLength(uint64_t v);
Expand Down
26 changes: 13 additions & 13 deletions tsl/platform/cpu_info.cc
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ class CPUIDInfo {
CHECK(cpuid == nullptr) << __func__ << " ran more than once";
cpuid = new CPUIDInfo;

uint32 eax, ebx, ecx, edx;
uint32_t eax, ebx, ecx, edx;

// Get vendor string (issue CPUID with eax = 0)
GETCPUID(eax, ebx, ecx, edx, 0, 0);
Expand Down Expand Up @@ -173,15 +173,15 @@ class CPUIDInfo {
cpuid->have_ssse3_ = (ecx >> 9) & 0x1;
cpuid->have_hypervisor_ = (ecx >> 31) & 1;

const uint64 xcr0_xmm_mask = 0x2;
const uint64 xcr0_ymm_mask = 0x4;
const uint64 xcr0_maskreg_mask = 0x20;
const uint64 xcr0_zmm0_15_mask = 0x40;
const uint64 xcr0_zmm16_31_mask = 0x80;
const uint64_t xcr0_xmm_mask = 0x2;
const uint64_t xcr0_ymm_mask = 0x4;
const uint64_t xcr0_maskreg_mask = 0x20;
const uint64_t xcr0_zmm0_15_mask = 0x40;
const uint64_t xcr0_zmm16_31_mask = 0x80;

const uint64 xcr0_avx_mask = xcr0_xmm_mask | xcr0_ymm_mask;
const uint64 xcr0_avx512_mask = xcr0_avx_mask | xcr0_maskreg_mask |
xcr0_zmm0_15_mask | xcr0_zmm16_31_mask;
const uint64_t xcr0_avx_mask = xcr0_xmm_mask | xcr0_ymm_mask;
const uint64_t xcr0_avx512_mask = xcr0_avx_mask | xcr0_maskreg_mask |
xcr0_zmm0_15_mask | xcr0_zmm16_31_mask;

const bool have_avx =
// Does the OS support XGETBV instruction use by applications?
Expand All @@ -207,7 +207,7 @@ class CPUIDInfo {
// Architectures Software Developer's Manual Volume 2A: Instruction Set
// Reference, A-M CPUID).
GETCPUID(eax, ebx, ecx, edx, 7, 0);
const uint32 kMaxNumSubLeaves = eax;
const uint32_t kMaxNumSubLeaves = eax;

cpuid->have_adx_ = (ebx >> 19) & 0x1;
cpuid->have_avx2_ = have_avx && ((ebx >> 5) & 0x1);
Expand Down Expand Up @@ -312,7 +312,7 @@ class CPUIDInfo {
return false;
}

string vendor_str() const { return vendor_str_; }
std::string vendor_str() const { return vendor_str_; }
int family() const { return family_; }
int model_num() { return model_num_; }

Expand Down Expand Up @@ -364,7 +364,7 @@ class CPUIDInfo {
int have_sse4_2_ : 1;
int have_ssse3_ : 1;
int have_hypervisor_ : 1;
string vendor_str_;
std::string vendor_str_;
int family_;
int model_num_;
};
Expand Down Expand Up @@ -590,7 +590,7 @@ int CPUIDNumSMT() {
// Section: Detecting Hardware Multi-threads Support and Topology
// Uses CPUID Leaf 11 to enumerate system topology on Intel x86 architectures
// Other cases not supported
uint32 eax, ebx, ecx, edx;
uint32_t eax, ebx, ecx, edx;
// Check if system supports Leaf 11
GETCPUID(eax, ebx, ecx, edx, 0, 0);
if (eax >= 11) {
Expand Down
4 changes: 2 additions & 2 deletions tsl/platform/demangle.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ namespace port {

// If the compiler supports, demangle a mangled symbol name and return
// the demangled name. Otherwise, returns 'mangled' as is.
string Demangle(const char* mangled);
inline string Demangle(const string mangled) {
std::string Demangle(const char* mangled);
inline std::string Demangle(const std::string mangled) {
return Demangle(mangled.c_str());
}

Expand Down
24 changes: 14 additions & 10 deletions tsl/platform/hash.cc
Original file line number Diff line number Diff line change
Expand Up @@ -24,22 +24,26 @@ limitations under the License.
namespace tsl {

// 0xff is in case char is signed.
static inline uint32 ByteAs32(char c) { return static_cast<uint32>(c) & 0xff; }
static inline uint64 ByteAs64(char c) { return static_cast<uint64>(c) & 0xff; }
static inline uint32_t ByteAs32(char c) {
return static_cast<uint32_t>(c) & 0xff;
}
static inline uint64_t ByteAs64(char c) {
return static_cast<uint64_t>(c) & 0xff;
}

uint32 Hash32(const char* data, size_t n, uint32 seed) {
uint32_t Hash32(const char* data, size_t n, uint32_t seed) {
// 'm' and 'r' are mixing constants generated offline.
// They're not really 'magic', they just happen to work well.

const uint32 m = 0x5bd1e995;
const uint32_t m = 0x5bd1e995;
const int r = 24;

// Initialize the hash to a 'random' value
uint32 h = seed ^ n;
uint32_t h = seed ^ n;

// Mix 4 bytes at a time into the hash
while (n >= 4) {
uint32 k = core::DecodeFixed32(data);
uint32_t k = core::DecodeFixed32(data);

k *= m;
k ^= k >> r;
Expand Down Expand Up @@ -76,14 +80,14 @@ uint32 Hash32(const char* data, size_t n, uint32 seed) {
return h;
}

uint64 Hash64(const char* data, size_t n, uint64 seed) {
const uint64 m = 0xc6a4a7935bd1e995;
uint64_t Hash64(const char* data, size_t n, uint64_t seed) {
const uint64_t m = 0xc6a4a7935bd1e995;
const int r = 47;

uint64 h = seed ^ (n * m);
uint64_t h = seed ^ (n * m);

while (n >= 8) {
uint64 k = core::DecodeFixed64(data);
uint64_t k = core::DecodeFixed64(data);
data += 8;
n -= 8;

Expand Down
Loading
Loading