Skip to content

Commit

Permalink
Update more code to use standard int types. (#9851)
Browse files Browse the repository at this point in the history
  • Loading branch information
dlj-NaN committed Apr 26, 2022
1 parent 089e091 commit 125dfd3
Show file tree
Hide file tree
Showing 13 changed files with 287 additions and 284 deletions.
18 changes: 9 additions & 9 deletions src/google/protobuf/compiler/js/js_generator.cc
Expand Up @@ -406,7 +406,7 @@ std::string GetMessagesFileName(const GeneratorOptions& options, const SCC* scc,
GetSnakeFilename(scc->GetRepresentative()->file()->name()));
(*long_name_dict)[scc->GetRepresentative()] =
StrCat(snake_name, "_long_sccs_",
static_cast<uint64>((*long_name_dict).size()));
static_cast<uint64_t>((*long_name_dict).size()));
}
filename_base = (*long_name_dict)[scc->GetRepresentative()];
}
Expand Down Expand Up @@ -575,7 +575,7 @@ std::string JSOneofIndex(const OneofDescriptor* oneof) {
}

// Decodes a codepoint in \x0000 -- \xFFFF.
uint16 DecodeUTF8Codepoint(uint8* bytes, size_t* length) {
uint16_t DecodeUTF8Codepoint(uint8_t* bytes, size_t* length) {
if (*length == 0) {
return 0;
}
Expand Down Expand Up @@ -620,13 +620,13 @@ uint16 DecodeUTF8Codepoint(uint8* bytes, size_t* length) {
bool EscapeJSString(const std::string& in, std::string* out) {
size_t decoded = 0;
for (size_t i = 0; i < in.size(); i += decoded) {
uint16 codepoint = 0;
uint16_t codepoint = 0;
// Decode the next UTF-8 codepoint.
size_t have_bytes = in.size() - i;
uint8 bytes[3] = {
static_cast<uint8>(in[i]),
static_cast<uint8>(((i + 1) < in.size()) ? in[i + 1] : 0),
static_cast<uint8>(((i + 2) < in.size()) ? in[i + 2] : 0),
uint8_t bytes[3] = {
static_cast<uint8_t>(in[i]),
static_cast<uint8_t>(((i + 1) < in.size()) ? in[i + 1] : 0),
static_cast<uint8_t>(((i + 2) < in.size()) ? in[i + 2] : 0),
};
codepoint = DecodeUTF8Codepoint(bytes, &have_bytes);
if (have_bytes == 0) {
Expand Down Expand Up @@ -814,13 +814,13 @@ std::string JSFieldDefault(const FieldDescriptor* field) {
// integer values as signed integer values. In order to exactly match the
// output, we need to reinterpret as base-2 signed. Ugh.
return MaybeNumberString(
field, StrCat(static_cast<int32>(field->default_value_uint32())));
field, StrCat(static_cast<int32_t>(field->default_value_uint32())));
case FieldDescriptor::CPPTYPE_INT64:
return MaybeNumberString(field, StrCat(field->default_value_int64()));
case FieldDescriptor::CPPTYPE_UINT64:
// See above note for uint32 -- reinterpreting as signed.
return MaybeNumberString(
field, StrCat(static_cast<int64>(field->default_value_uint64())));
field, StrCat(static_cast<int64_t>(field->default_value_uint64())));
case FieldDescriptor::CPPTYPE_ENUM:
return StrCat(field->default_value_enum()->number());
case FieldDescriptor::CPPTYPE_BOOL:
Expand Down
2 changes: 1 addition & 1 deletion src/google/protobuf/compiler/js/js_generator.h
Expand Up @@ -151,7 +151,7 @@ class PROTOC_EXPORT Generator : public CodeGenerator {
const std::string& parameter, GeneratorContext* context,
std::string* error) const override;

uint64 GetSupportedFeatures() const override {
uint64_t GetSupportedFeatures() const override {
return FEATURE_PROTO3_OPTIONAL;
}

Expand Down
2 changes: 1 addition & 1 deletion src/google/protobuf/compiler/ruby/ruby_generator.cc
Expand Up @@ -158,7 +158,7 @@ std::string DefaultValueForField(const FieldDescriptor* field) {
for (int i = 0; i < default_str.length(); ++i) {
// Write the hex form of each byte.
os << "\\x" << std::hex << std::setw(2)
<< ((uint16)((unsigned char)default_str.at(i)));
<< ((uint16_t)((unsigned char)default_str.at(i)));
}
os << "\".force_encoding(\"ASCII-8BIT\")";
}
Expand Down
14 changes: 7 additions & 7 deletions src/google/protobuf/stubs/common.cc
Expand Up @@ -298,15 +298,15 @@ void DoNothing() {}
// TODO(xiaofeng): PROTOBUF_LITTLE_ENDIAN is unfortunately defined in
// google/protobuf/io/coded_stream.h and therefore can not be used here.
// Maybe move that macro definition here in the future.
uint32 ghtonl(uint32 x) {
uint32_t ghtonl(uint32_t x) {
union {
uint32 result;
uint8 result_array[4];
uint32_t result;
uint8_t result_array[4];
};
result_array[0] = static_cast<uint8>(x >> 24);
result_array[1] = static_cast<uint8>((x >> 16) & 0xFF);
result_array[2] = static_cast<uint8>((x >> 8) & 0xFF);
result_array[3] = static_cast<uint8>(x & 0xFF);
result_array[0] = static_cast<uint8_t>(x >> 24);
result_array[1] = static_cast<uint8_t>((x >> 16) & 0xFF);
result_array[2] = static_cast<uint8_t>((x >> 8) & 0xFF);
result_array[3] = static_cast<uint8_t>(x & 0xFF);
return result;
}

Expand Down
20 changes: 10 additions & 10 deletions src/google/protobuf/stubs/int128.cc
Expand Up @@ -57,22 +57,22 @@ const uint128_pod kuint128max = {uint64_t{0xFFFFFFFFFFFFFFFFu},
(pos) |= (sh); \
} \
} while (0)
static inline int Fls64(uint64 n) {
static inline int Fls64(uint64_t n) {
GOOGLE_DCHECK_NE(0, n);
int pos = 0;
STEP(uint64, n, pos, 0x20);
uint32 n32 = n;
STEP(uint32, n32, pos, 0x10);
STEP(uint32, n32, pos, 0x08);
STEP(uint32, n32, pos, 0x04);
STEP(uint64_t, n, pos, 0x20);
uint32_t n32 = n;
STEP(uint32_t, n32, pos, 0x10);
STEP(uint32_t, n32, pos, 0x08);
STEP(uint32_t, n32, pos, 0x04);
return pos + ((uint64_t{0x3333333322221100u} >> (n32 << 2)) & 0x3);
}
#undef STEP

// Like Fls64() above, but returns the 0-based position of the last set bit
// (i.e., most significant bit) in the given uint128. The argument may not be 0.
static inline int Fls128(uint128 n) {
if (uint64 hi = Uint128High64(n)) {
if (uint64_t hi = Uint128High64(n)) {
return Fls64(hi) + 64;
}
return Fls64(Uint128Low64(n));
Expand Down Expand Up @@ -132,16 +132,16 @@ std::ostream& operator<<(std::ostream& o, const uint128& b) {
switch (flags & std::ios::basefield) {
case std::ios::hex:
div =
static_cast<uint64>(uint64_t{0x1000000000000000u}); // 16^15
static_cast<uint64_t>(uint64_t{0x1000000000000000u}); // 16^15
div_base_log = 15;
break;
case std::ios::oct:
div = static_cast<uint64>(
div = static_cast<uint64_t>(
uint64_t{01000000000000000000000u}); // 8^21
div_base_log = 21;
break;
default: // std::ios::dec
div = static_cast<uint64>(
div = static_cast<uint64_t>(
uint64_t{10000000000000000000u}); // 10^19
div_base_log = 19;
break;
Expand Down
78 changes: 39 additions & 39 deletions src/google/protobuf/stubs/int128.h
Expand Up @@ -53,17 +53,17 @@ struct uint128_pod;
class PROTOBUF_EXPORT uint128 {
public:
UINT128_CONSTEXPR uint128(); // Sets to 0, but don't trust on this behavior.
UINT128_CONSTEXPR uint128(uint64 top, uint64 bottom);
UINT128_CONSTEXPR uint128(uint64_t top, uint64_t bottom);
#ifndef SWIG
UINT128_CONSTEXPR uint128(int bottom);
UINT128_CONSTEXPR uint128(uint32 bottom); // Top 96 bits = 0
UINT128_CONSTEXPR uint128(uint32_t bottom); // Top 96 bits = 0
#endif
UINT128_CONSTEXPR uint128(uint64 bottom); // hi_ = 0
UINT128_CONSTEXPR uint128(uint64_t bottom); // hi_ = 0
UINT128_CONSTEXPR uint128(const uint128_pod &val);

// Trivial copy constructor, assignment operator and destructor.

void Initialize(uint64 top, uint64 bottom);
void Initialize(uint64_t top, uint64_t bottom);

// Arithmetic operators.
uint128& operator+=(const uint128& b);
Expand All @@ -82,8 +82,8 @@ class PROTOBUF_EXPORT uint128 {
uint128& operator++();
uint128& operator--();

friend uint64 Uint128Low64(const uint128& v);
friend uint64 Uint128High64(const uint128& v);
friend uint64_t Uint128Low64(const uint128& v);
friend uint64_t Uint128High64(const uint128& v);

// We add "std::" to avoid including all of port.h.
PROTOBUF_EXPORT friend std::ostream& operator<<(std::ostream& o,
Expand All @@ -96,12 +96,12 @@ class PROTOBUF_EXPORT uint128 {
// Little-endian memory order optimizations can benefit from
// having lo_ first, hi_ last.
// See util/endian/endian.h and Load128/Store128 for storing a uint128.
uint64 lo_;
uint64 hi_;
uint64_t lo_;
uint64_t hi_;

// Not implemented, just declared for catching automatic type conversions.
uint128(uint8);
uint128(uint16);
uint128(uint8_t);
uint128(uint16_t);
uint128(float v);
uint128(double v);
};
Expand All @@ -114,8 +114,8 @@ struct uint128_pod {
// of static instances, which is the primary reason for this struct in the
// first place. This does not seem to defeat any optimizations wrt
// operations involving this struct.
uint64 hi;
uint64 lo;
uint64_t hi;
uint64_t lo;
};

PROTOBUF_EXPORT extern const uint128_pod kuint128max;
Expand All @@ -127,8 +127,8 @@ PROTOBUF_EXPORT extern std::ostream& operator<<(std::ostream& o,
// Methods to access low and high pieces of 128-bit value.
// Defined externally from uint128 to facilitate conversion
// to native 128-bit types when compilers support them.
inline uint64 Uint128Low64(const uint128& v) { return v.lo_; }
inline uint64 Uint128High64(const uint128& v) { return v.hi_; }
inline uint64_t Uint128Low64(const uint128& v) { return v.lo_; }
inline uint64_t Uint128High64(const uint128& v) { return v.hi_; }

// TODO: perhaps it would be nice to have int128, a signed 128-bit type?

Expand All @@ -144,22 +144,22 @@ inline bool operator!=(const uint128& lhs, const uint128& rhs) {
}

inline UINT128_CONSTEXPR uint128::uint128() : lo_(0), hi_(0) {}
inline UINT128_CONSTEXPR uint128::uint128(uint64 top, uint64 bottom)
inline UINT128_CONSTEXPR uint128::uint128(uint64_t top, uint64_t bottom)
: lo_(bottom), hi_(top) {}
inline UINT128_CONSTEXPR uint128::uint128(const uint128_pod& v)
: lo_(v.lo), hi_(v.hi) {}
inline UINT128_CONSTEXPR uint128::uint128(uint64 bottom)
inline UINT128_CONSTEXPR uint128::uint128(uint64_t bottom)
: lo_(bottom), hi_(0) {}
#ifndef SWIG
inline UINT128_CONSTEXPR uint128::uint128(uint32 bottom)
inline UINT128_CONSTEXPR uint128::uint128(uint32_t bottom)
: lo_(bottom), hi_(0) {}
inline UINT128_CONSTEXPR uint128::uint128(int bottom)
: lo_(bottom), hi_(static_cast<int64>((bottom < 0) ? -1 : 0)) {}
: lo_(bottom), hi_(static_cast<int64_t>((bottom < 0) ? -1 : 0)) {}
#endif

#undef UINT128_CONSTEXPR

inline void uint128::Initialize(uint64 top, uint64 bottom) {
inline void uint128::Initialize(uint64_t top, uint64_t bottom) {
hi_ = top;
lo_ = bottom;
}
Expand All @@ -183,9 +183,9 @@ CMP128(<=)
// Unary operators

inline uint128 operator-(const uint128& val) {
const uint64 hi_flip = ~Uint128High64(val);
const uint64 lo_flip = ~Uint128Low64(val);
const uint64 lo_add = lo_flip + 1;
const uint64_t hi_flip = ~Uint128High64(val);
const uint64_t lo_flip = ~Uint128Low64(val);
const uint64_t lo_add = lo_flip + 1;
if (lo_add < lo_flip) {
return uint128(hi_flip + 1, lo_add);
}
Expand Down Expand Up @@ -235,9 +235,9 @@ inline uint128 operator<<(const uint128& val, int amount) {
if (amount == 0) {
return val;
}
uint64 new_hi = (Uint128High64(val) << amount) |
(Uint128Low64(val) >> (64 - amount));
uint64 new_lo = Uint128Low64(val) << amount;
uint64_t new_hi = (Uint128High64(val) << amount) |
(Uint128Low64(val) >> (64 - amount));
uint64_t new_lo = Uint128Low64(val) << amount;
return uint128(new_hi, new_lo);
} else if (amount < 128) {
return uint128(Uint128Low64(val) << (amount - 64), 0);
Expand All @@ -252,9 +252,9 @@ inline uint128 operator>>(const uint128& val, int amount) {
if (amount == 0) {
return val;
}
uint64 new_hi = Uint128High64(val) >> amount;
uint64 new_lo = (Uint128Low64(val) >> amount) |
(Uint128High64(val) << (64 - amount));
uint64_t new_hi = Uint128High64(val) >> amount;
uint64_t new_lo = (Uint128Low64(val) >> amount) |
(Uint128High64(val) << (64 - amount));
return uint128(new_hi, new_lo);
} else if (amount < 128) {
return uint128(0, Uint128High64(val) >> (amount - 64));
Expand Down Expand Up @@ -319,7 +319,7 @@ inline uint128 operator%(const uint128& lhs, const uint128& rhs) {

inline uint128& uint128::operator+=(const uint128& b) {
hi_ += b.hi_;
uint64 lolo = lo_ + b.lo_;
uint64_t lolo = lo_ + b.lo_;
if (lolo < lo_)
++hi_;
lo_ = lolo;
Expand All @@ -335,19 +335,19 @@ inline uint128& uint128::operator-=(const uint128& b) {
}

inline uint128& uint128::operator*=(const uint128& b) {
uint64 a96 = hi_ >> 32;
uint64 a64 = hi_ & 0xffffffffu;
uint64 a32 = lo_ >> 32;
uint64 a00 = lo_ & 0xffffffffu;
uint64 b96 = b.hi_ >> 32;
uint64 b64 = b.hi_ & 0xffffffffu;
uint64 b32 = b.lo_ >> 32;
uint64 b00 = b.lo_ & 0xffffffffu;
uint64_t a96 = hi_ >> 32;
uint64_t a64 = hi_ & 0xffffffffu;
uint64_t a32 = lo_ >> 32;
uint64_t a00 = lo_ & 0xffffffffu;
uint64_t b96 = b.hi_ >> 32;
uint64_t b64 = b.hi_ & 0xffffffffu;
uint64_t b32 = b.lo_ >> 32;
uint64_t b00 = b.lo_ & 0xffffffffu;
// multiply [a96 .. a00] x [b96 .. b00]
// terms higher than c96 disappear off the high side
// terms c96 and c64 are safe to ignore carry bit
uint64 c96 = a96 * b00 + a64 * b32 + a32 * b64 + a00 * b96;
uint64 c64 = a64 * b00 + a32 * b32 + a00 * b64;
uint64_t c96 = a96 * b00 + a64 * b32 + a32 * b64 + a00 * b96;
uint64_t c64 = a64 * b00 + a32 * b32 + a00 * b64;
this->hi_ = (c96 << 32) + c64;
this->lo_ = 0;
// add terms after this one at a time to capture carry
Expand Down
20 changes: 10 additions & 10 deletions src/google/protobuf/stubs/int128_unittest.cc
Expand Up @@ -53,7 +53,7 @@ TEST(Int128, AllTests) {
uint128 bigger(2001, 1);
uint128 biggest(kuint128max);
uint128 high_low(1, 0);
uint128 low_high(0, kuint64max);
uint128 low_high(0, std::numeric_limits<uint64_t>::max());
EXPECT_LT(one, two);
EXPECT_GT(two, one);
EXPECT_LT(one, big);
Expand Down Expand Up @@ -121,8 +121,8 @@ TEST(Int128, AllTests) {
big_copy = big;
EXPECT_EQ(big >> 128, big_copy >>= 128);

EXPECT_EQ(Uint128High64(biggest), kuint64max);
EXPECT_EQ(Uint128Low64(biggest), kuint64max);
EXPECT_EQ(Uint128High64(biggest), std::numeric_limits<uint64_t>::max());
EXPECT_EQ(Uint128Low64(biggest), std::numeric_limits<uint64_t>::max());
EXPECT_EQ(zero + one, one);
EXPECT_EQ(one + one, two);
EXPECT_EQ(big_minus_one + one, big);
Expand All @@ -131,13 +131,13 @@ TEST(Int128, AllTests) {
EXPECT_EQ(zero - one, biggest);
EXPECT_EQ(big - big, zero);
EXPECT_EQ(big - one, big_minus_one);
EXPECT_EQ(big + kuint64max, bigger);
EXPECT_EQ(big + std::numeric_limits<uint64_t>::max(), bigger);
EXPECT_EQ(biggest + 1, zero);
EXPECT_EQ(zero - 1, biggest);
EXPECT_EQ(high_low - one, low_high);
EXPECT_EQ(low_high + one, high_low);
EXPECT_EQ(Uint128High64((uint128(1) << 64) - 1), 0);
EXPECT_EQ(Uint128Low64((uint128(1) << 64) - 1), kuint64max);
EXPECT_EQ(Uint128Low64((uint128(1) << 64) - 1), std::numeric_limits<uint64_t>::max());
EXPECT_TRUE(!!one);
EXPECT_TRUE(!!high_low);
EXPECT_FALSE(!!zero);
Expand Down Expand Up @@ -317,7 +317,7 @@ TEST(Int128, AliasTests) {
x1 += x1;
EXPECT_EQ(x2, x1);

uint128 x3(1, static_cast<uint64>(1) << 63);
uint128 x3(1, static_cast<uint64_t>(1) << 63);
uint128 x4(3, 0);
x3 += x3;
EXPECT_EQ(x4, x3);
Expand Down Expand Up @@ -403,10 +403,10 @@ TEST(Int128, DivideAndMod) {
EXPECT_EQ(expected_r, result_r);
}

static uint64 RandomUint64() {
uint64 v1 = rand();
uint64 v2 = rand();
uint64 v3 = rand();
static uint64_t RandomUint64() {
uint64_t v1 = rand();
uint64_t v2 = rand();
uint64_t v3 = rand();
return v1 * v2 + v3;
}

Expand Down

0 comments on commit 125dfd3

Please sign in to comment.