From 6280bddf55e675219cacc25a6a12bc5ddc0fdc74 Mon Sep 17 00:00:00 2001 From: Abseil Team Date: Thu, 14 Dec 2017 12:36:12 -0800 Subject: Changes imported from Abseil "staging" branch: - 8320b38cd9f4f271fb6b278bd1e10d93f6ac3856 Use overloads for int32/int64/uint32/uint64 rather than i... by Jorg Brown - f8b582b8deb3f78a3c6de2114b3ec4640f5427dd Internal change by Juemin Yang - 240ff55ebf493ab1233ebe6976853a5fa2b3ec46 Remove the internal LowLevelAlloc's dependence on kLinker... by Greg Falcon GitOrigin-RevId: 8320b38cd9f4f271fb6b278bd1e10d93f6ac3856 Change-Id: If5004efa2b43856948390ab357b8e9403e4461b4 --- absl/strings/numbers.cc | 28 +++++++---------- absl/strings/numbers.h | 26 +++++++++------- absl/strings/numbers_test.cc | 74 +++++++++++++++++++++++++++++++------------- 3 files changed, 79 insertions(+), 49 deletions(-) (limited to 'absl/strings') diff --git a/absl/strings/numbers.cc b/absl/strings/numbers.cc index ac73f5308225..31f07c72c949 100644 --- a/absl/strings/numbers.cc +++ b/absl/strings/numbers.cc @@ -135,16 +135,12 @@ bool SimpleAtob(absl::string_view str, bool* value) { } // ---------------------------------------------------------------------- -// FastInt32ToBuffer() -// FastUInt32ToBuffer() -// FastInt64ToBuffer() -// FastUInt64ToBuffer() +// FastIntToBuffer() overloads // // Like the Fast*ToBuffer() functions above, these are intended for speed. // Unlike the Fast*ToBuffer() functions, however, these functions write -// their output to the beginning of the buffer (hence the name, as the -// output is left-aligned). The caller is responsible for ensuring that -// the buffer has enough space to hold the output. +// their output to the beginning of the buffer. The caller is responsible +// for ensuring that the buffer has enough space to hold the output. // // Returns a pointer to the end of the std::string (i.e. the null character // terminating the std::string). @@ -160,7 +156,7 @@ const char one_ASCII_final_digits[10][2] { } // namespace -char* numbers_internal::FastUInt32ToBuffer(uint32_t i, char* buffer) { +char* numbers_internal::FastIntToBuffer(uint32_t i, char* buffer) { uint32_t digits; // The idea of this implementation is to trim the number of divides to as few // as possible, and also reducing memory stores and branches, by going in @@ -230,7 +226,7 @@ char* numbers_internal::FastUInt32ToBuffer(uint32_t i, char* buffer) { goto lt100_000_000; } -char* numbers_internal::FastInt32ToBuffer(int32_t i, char* buffer) { +char* numbers_internal::FastIntToBuffer(int32_t i, char* buffer) { uint32_t u = i; if (i < 0) { *buffer++ = '-'; @@ -239,12 +235,12 @@ char* numbers_internal::FastInt32ToBuffer(int32_t i, char* buffer) { // we write the equivalent expression "0 - u" instead. u = 0 - u; } - return numbers_internal::FastUInt32ToBuffer(u, buffer); + return numbers_internal::FastIntToBuffer(u, buffer); } -char* numbers_internal::FastUInt64ToBuffer(uint64_t i, char* buffer) { +char* numbers_internal::FastIntToBuffer(uint64_t i, char* buffer) { uint32_t u32 = static_cast(i); - if (u32 == i) return numbers_internal::FastUInt32ToBuffer(u32, buffer); + if (u32 == i) return numbers_internal::FastIntToBuffer(u32, buffer); // Here we know i has at least 10 decimal digits. uint64_t top_1to11 = i / 1000000000; @@ -252,12 +248,12 @@ char* numbers_internal::FastUInt64ToBuffer(uint64_t i, char* buffer) { uint32_t top_1to11_32 = static_cast(top_1to11); if (top_1to11_32 == top_1to11) { - buffer = numbers_internal::FastUInt32ToBuffer(top_1to11_32, buffer); + buffer = numbers_internal::FastIntToBuffer(top_1to11_32, buffer); } else { // top_1to11 has more than 32 bits too; print it in two steps. uint32_t top_8to9 = static_cast(top_1to11 / 100); uint32_t mid_2 = static_cast(top_1to11 - top_8to9 * 100); - buffer = numbers_internal::FastUInt32ToBuffer(top_8to9, buffer); + buffer = numbers_internal::FastIntToBuffer(top_8to9, buffer); PutTwoDigits(mid_2, buffer); buffer += 2; } @@ -283,13 +279,13 @@ char* numbers_internal::FastUInt64ToBuffer(uint64_t i, char* buffer) { return buffer + 1; } -char* numbers_internal::FastInt64ToBuffer(int64_t i, char* buffer) { +char* numbers_internal::FastIntToBuffer(int64_t i, char* buffer) { uint64_t u = i; if (i < 0) { *buffer++ = '-'; u = 0 - u; } - return numbers_internal::FastUInt64ToBuffer(u, buffer); + return numbers_internal::FastIntToBuffer(u, buffer); } // Returns the number of leading 0 bits in a 64-bit value. diff --git a/absl/strings/numbers.h b/absl/strings/numbers.h index 1f3bbcfae596..adf706a4e26e 100644 --- a/absl/strings/numbers.h +++ b/absl/strings/numbers.h @@ -81,14 +81,6 @@ bool safe_strto64_base(absl::string_view text, int64_t* value, int base); bool safe_strtou32_base(absl::string_view text, uint32_t* value, int base); bool safe_strtou64_base(absl::string_view text, uint64_t* value, int base); -// These functions are intended for speed. All functions take an output buffer -// as an argument and return a pointer to the last byte they wrote, which is the -// terminating '\0'. At most `kFastToBufferSize` bytes are written. -char* FastInt32ToBuffer(int32_t i, char* buffer); -char* FastUInt32ToBuffer(uint32_t i, char* buffer); -char* FastInt64ToBuffer(int64_t i, char* buffer); -char* FastUInt64ToBuffer(uint64_t i, char* buffer); - static const int kFastToBufferSize = 32; static const int kSixDigitsToBufferSize = 16; @@ -100,6 +92,16 @@ static const int kSixDigitsToBufferSize = 16; // Required buffer size is `kSixDigitsToBufferSize`. size_t SixDigitsToBuffer(double d, char* buffer); +// These functions are intended for speed. All functions take an output buffer +// as an argument and return a pointer to the last byte they wrote, which is the +// terminating '\0'. At most `kFastToBufferSize` bytes are written. +char* FastIntToBuffer(int32_t, char*); +char* FastIntToBuffer(uint32_t, char*); +char* FastIntToBuffer(int64_t, char*); +char* FastIntToBuffer(uint64_t, char*); + +// For enums and integer types that are not an exact match for the types above, +// use templates to call the appropriate one of the four overloads above. template char* FastIntToBuffer(int_type i, char* buffer) { static_assert(sizeof(i) <= 64 / 8, @@ -109,15 +111,15 @@ char* FastIntToBuffer(int_type i, char* buffer) { // If one day something like std::is_signed works, switch to it. if (static_cast(1) - 2 < 0) { // Signed if (sizeof(i) > 32 / 8) { // 33-bit to 64-bit - return numbers_internal::FastInt64ToBuffer(i, buffer); + return FastIntToBuffer(static_cast(i), buffer); } else { // 32-bit or less - return numbers_internal::FastInt32ToBuffer(i, buffer); + return FastIntToBuffer(static_cast(i), buffer); } } else { // Unsigned if (sizeof(i) > 32 / 8) { // 33-bit to 64-bit - return numbers_internal::FastUInt64ToBuffer(i, buffer); + return FastIntToBuffer(static_cast(i), buffer); } else { // 32-bit or less - return numbers_internal::FastUInt32ToBuffer(i, buffer); + return FastIntToBuffer(static_cast(i), buffer); } } } diff --git a/absl/strings/numbers_test.cc b/absl/strings/numbers_test.cc index ff3120f5c57b..a705255c706d 100644 --- a/absl/strings/numbers_test.cc +++ b/absl/strings/numbers_test.cc @@ -110,13 +110,38 @@ TEST(ToString, PerfectDtoa) { } } +template +struct MyInteger { + integer i; + explicit constexpr MyInteger(integer i) : i(i) {} + constexpr operator integer() const { return i; } + + constexpr MyInteger operator+(MyInteger other) const { return i + other.i; } + constexpr MyInteger operator-(MyInteger other) const { return i - other.i; } + constexpr MyInteger operator*(MyInteger other) const { return i * other.i; } + constexpr MyInteger operator/(MyInteger other) const { return i / other.i; } + + constexpr bool operator<(MyInteger other) const { return i < other.i; } + constexpr bool operator<=(MyInteger other) const { return i <= other.i; } + constexpr bool operator==(MyInteger other) const { return i == other.i; } + constexpr bool operator>=(MyInteger other) const { return i >= other.i; } + constexpr bool operator>(MyInteger other) const { return i > other.i; } + constexpr bool operator!=(MyInteger other) const { return i != other.i; } + + integer as_integer() const { return i; } +}; + +typedef MyInteger MyInt64; +typedef MyInteger MyUInt64; + void CheckInt32(int32_t x) { char buffer[absl::numbers_internal::kFastToBufferSize]; - char* actual = absl::numbers_internal::FastInt32ToBuffer(x, buffer); + char* actual = absl::numbers_internal::FastIntToBuffer(x, buffer); std::string expected = std::to_string(x); - ASSERT_TRUE(expected == std::string(buffer, actual)) - << "Expected \"" << expected << "\", Actual \"" << actual << "\", Input " - << x; + EXPECT_EQ(expected, std::string(buffer, actual)) << " Input " << x; + + char* generic_actual = absl::numbers_internal::FastIntToBuffer(x, buffer); + EXPECT_EQ(expected, std::string(buffer, generic_actual)) << " Input " << x; } void CheckInt64(int64_t x) { @@ -124,40 +149,47 @@ void CheckInt64(int64_t x) { buffer[0] = '*'; buffer[23] = '*'; buffer[24] = '*'; - char* actual = absl::numbers_internal::FastInt64ToBuffer(x, &buffer[1]); + char* actual = absl::numbers_internal::FastIntToBuffer(x, &buffer[1]); std::string expected = std::to_string(x); - ASSERT_TRUE(expected == std::string(&buffer[1], actual)) - << "Expected \"" << expected << "\", Actual \"" << actual << "\", Input " - << x; - ASSERT_EQ(buffer[0], '*'); - ASSERT_EQ(buffer[23], '*'); - ASSERT_EQ(buffer[24], '*'); + EXPECT_EQ(expected, std::string(&buffer[1], actual)) << " Input " << x; + EXPECT_EQ(buffer[0], '*'); + EXPECT_EQ(buffer[23], '*'); + EXPECT_EQ(buffer[24], '*'); + + char* my_actual = + absl::numbers_internal::FastIntToBuffer(MyInt64(x), &buffer[1]); + EXPECT_EQ(expected, std::string(&buffer[1], my_actual)) << " Input " << x; } void CheckUInt32(uint32_t x) { char buffer[absl::numbers_internal::kFastToBufferSize]; - char* actual = absl::numbers_internal::FastUInt32ToBuffer(x, buffer); + char* actual = absl::numbers_internal::FastIntToBuffer(x, buffer); std::string expected = std::to_string(x); - ASSERT_TRUE(expected == std::string(buffer, actual)) - << "Expected \"" << expected << "\", Actual \"" << actual << "\", Input " - << x; + EXPECT_EQ(expected, std::string(buffer, actual)) << " Input " << x; + + char* generic_actual = absl::numbers_internal::FastIntToBuffer(x, buffer); + EXPECT_EQ(expected, std::string(buffer, generic_actual)) << " Input " << x; } void CheckUInt64(uint64_t x) { char buffer[absl::numbers_internal::kFastToBufferSize + 1]; - char* actual = absl::numbers_internal::FastUInt64ToBuffer(x, &buffer[1]); + char* actual = absl::numbers_internal::FastIntToBuffer(x, &buffer[1]); std::string expected = std::to_string(x); - ASSERT_TRUE(expected == std::string(&buffer[1], actual)) - << "Expected \"" << expected << "\", Actual \"" << actual << "\", Input " - << x; + EXPECT_EQ(expected, std::string(&buffer[1], actual)) << " Input " << x; + + char* generic_actual = absl::numbers_internal::FastIntToBuffer(x, &buffer[1]); + EXPECT_EQ(expected, std::string(&buffer[1], generic_actual)) << " Input " << x; + + char* my_actual = + absl::numbers_internal::FastIntToBuffer(MyUInt64(x), &buffer[1]); + EXPECT_EQ(expected, std::string(&buffer[1], my_actual)) << " Input " << x; } void CheckHex64(uint64_t v) { char expected[16 + 1]; std::string actual = absl::StrCat(absl::Hex(v, absl::kZeroPad16)); snprintf(expected, sizeof(expected), "%016" PRIx64, static_cast(v)); - ASSERT_TRUE(expected == actual) - << "Expected \"" << expected << "\", Actual \"" << actual << "\""; + EXPECT_EQ(expected, actual) << " Input " << v; } TEST(Numbers, TestFastPrints) { -- cgit 1.4.1