Commit bd40a41c by Abseil Team Committed by Shaindel Schwartz

--

f28d30df5769bb832dec3ff36d2fcd2bcdf494a3 by Shaindel Schwartz <shaindel@google.com>:

Internal change

PiperOrigin-RevId: 201046831

--
711715a78b7e53dfaafd4d7f08a74e76db22af88 by Mark Barolak <mbar@google.com>:

Internal fix

PiperOrigin-RevId: 201043684

--
64b53edd6bf1fa48f74e7f5d33f00f80d5089147 by Shaindel Schwartz <shaindel@google.com>:

Remove extra whitespace

PiperOrigin-RevId: 201041989

--
0bdd2a0b33657b688e4a04aeba9ebba47e4dc6ca by Shaindel Schwartz <shaindel@google.com>:

Whitespace fix.

PiperOrigin-RevId: 201034413

--
3deb0ac296ef1b74c4789e114a8a8bf53253f26b by Shaindel Schwartz <shaindel@google.com>:

Scrub build tags. No functional changes.

PiperOrigin-RevId: 201032927

--
da75d0f8b73baa7e8f4e9a092bba546012ed3b71 by Alex Strelnikov <strel@google.com>:

Internal change.

PiperOrigin-RevId: 201026131

--
6815d80caa19870d0c441b6b9816c68db41393a5 by Tom Manshreck <shreck@google.com>:

Add documentation for our LTS snapshot branches

PiperOrigin-RevId: 201025191

--
64c3b02006f39e6a8127bbabf9ec947fb45b6504 by Greg Falcon <gfalcon@google.com>:

Provide absl::from_chars for double and float types.  This is a forward-compatible implementation of std::from_chars from C++17.

This provides exact "round_to_nearest" conversions, and has some nice properties:

* Works with string_view (it can convert numbers from non-NUL-terminated buffers)
* Never allocates memory
* Faster than the standard library strtod() in our toolchain
* Uses integer math in its calculations, so is unaffected by floating point environment
* Unaffected by C locale

Also change SimpleAtod/SimpleAtoi to use this new API under the hood.

PiperOrigin-RevId: 201003324

--
542869258eb100779497c899103dc96aced52749 by Greg Falcon <gfalcon@google.com>:

Internal change

PiperOrigin-RevId: 200999200

--
3aba192775c7f80e2cd7f221b0a73537823c54ea by Gennadiy Rozental <rogeeff@google.com>:

Internal change

PiperOrigin-RevId: 200947470

--
daf9b9feedd748d5364a4c06165b7cb7604d3e1e by Mark Barolak <mbar@google.com>:

Add an absl:: qualification to a usage of base_internal::SchedulingMode outside of an absl:: namespace.

PiperOrigin-RevId: 200748234

--
a8d265290a22d629f3d9bf9f872c204200bfe8c8 by Mark Barolak <mbar@google.com>:

Add a missing namespace closing comment to optional.h.

PiperOrigin-RevId: 200739934

--
f05af8ee1c6b864dad2df7c907d424209a3e3202 by Abseil Team <absl-team@google.com>:

Internal change

PiperOrigin-RevId: 200719115
GitOrigin-RevId: f28d30df5769bb832dec3ff36d2fcd2bcdf494a3
Change-Id: Ie4fa601078fd4aa57286372611f1d114fdec82c0
parent f44e1eed
# Long Term Support (LTS) Branches
This repository contains periodic snapshots of the Abseil codebase that are
Long Term Support (LTS) branches. An LTS branch allows you to use a known
version of Abseil without interfering with other projects which may also, in
turn, use Abseil. (For more information about our releases, see the
[Abseil Release Management](https://abseil.io/about/releases) guide.
## LTS Branches
The following lists LTS branches and the date they have been released:
* [LTS Branch June 18, 2018](https://github.com/abseil/abseil-cpp/tree/lts_2018_06_18/)
......@@ -371,11 +371,6 @@ cc_test(
size = "small",
srcs = ["internal/sysinfo_test.cc"],
copts = ABSL_TEST_COPTS,
tags = [
"no_test_android_arm",
"no_test_android_arm64",
"no_test_android_x86",
],
deps = [
":base",
"//absl/synchronization",
......
......@@ -84,7 +84,7 @@ inline void absl::base_internal::SpinLockWake(std::atomic<uint32_t> *w,
inline void absl::base_internal::SpinLockDelay(
std::atomic<uint32_t> *w, uint32_t value, int loop,
base_internal::SchedulingMode scheduling_mode) {
absl::base_internal::SchedulingMode scheduling_mode) {
AbslInternalSpinLockDelay(w, value, loop, scheduling_mode);
}
......
......@@ -32,7 +32,12 @@ cc_library(
name = "strings",
srcs = [
"ascii.cc",
"charconv.cc",
"escaping.cc",
"internal/charconv_bigint.cc",
"internal/charconv_bigint.h",
"internal/charconv_parse.cc",
"internal/charconv_parse.h",
"internal/memutil.cc",
"internal/memutil.h",
"internal/stl_type_traits.h",
......@@ -48,6 +53,7 @@ cc_library(
],
hdrs = [
"ascii.h",
"charconv.h",
"escaping.h",
"match.h",
"numbers.h",
......@@ -144,11 +150,6 @@ cc_test(
size = "small",
srcs = ["ascii_test.cc"],
copts = ABSL_TEST_COPTS,
tags = [
"no_test_android_arm",
"no_test_android_arm64",
"no_test_android_x86",
],
visibility = ["//visibility:private"],
deps = [
":strings",
......@@ -398,12 +399,6 @@ cc_test(
"numbers_test.cc",
],
copts = ABSL_TEST_COPTS,
tags = [
"no_test_android_arm",
"no_test_android_arm64",
"no_test_android_x86",
"no_test_loonix",
],
visibility = ["//visibility:private"],
deps = [
":strings",
......@@ -429,11 +424,6 @@ cc_test(
name = "char_map_test",
srcs = ["internal/char_map_test.cc"],
copts = ABSL_TEST_COPTS,
tags = [
"no_test_android_arm",
"no_test_android_arm64",
"no_test_android_x86",
],
deps = [
":internal",
"@com_google_googletest//:gtest_main",
......@@ -450,3 +440,55 @@ cc_test(
"@com_github_google_benchmark//:benchmark_main",
],
)
cc_test(
name = "charconv_test",
srcs = ["charconv_test.cc"],
copts = ABSL_TEST_COPTS,
deps = [
":strings",
"//absl/base",
"@com_google_googletest//:gtest_main",
],
)
cc_test(
name = "charconv_parse_test",
srcs = [
"internal/charconv_parse.h",
"internal/charconv_parse_test.cc",
],
copts = ABSL_TEST_COPTS,
deps = [
":strings",
"//absl/base",
"@com_google_googletest//:gtest_main",
],
)
cc_test(
name = "charconv_bigint_test",
srcs = [
"internal/charconv_bigint.h",
"internal/charconv_bigint_test.cc",
"internal/charconv_parse.h",
],
copts = ABSL_TEST_COPTS,
deps = [
":strings",
"//absl/base",
"@com_google_googletest//:gtest_main",
],
)
cc_test(
name = "charconv_benchmark",
srcs = [
"charconv_benchmark.cc",
],
deps = [
":strings",
"//absl/base",
"@com_github_google_benchmark//:benchmark_main",
],
)
......@@ -17,6 +17,7 @@
list(APPEND STRINGS_PUBLIC_HEADERS
"ascii.h"
"charconv.h"
"escaping.h"
"match.h"
"numbers.h"
......@@ -33,6 +34,8 @@ list(APPEND STRINGS_PUBLIC_HEADERS
list(APPEND STRINGS_INTERNAL_HEADERS
"internal/bits.h"
"internal/char_map.h"
"internal/charconv_bigint.h"
"internal/charconv_parse.h"
"internal/memutil.h"
"internal/ostringstream.h"
"internal/resize_uninitialized.h"
......@@ -47,7 +50,10 @@ list(APPEND STRINGS_INTERNAL_HEADERS
# add string library
list(APPEND STRINGS_SRC
"ascii.cc"
"charconv.cc"
"escaping.cc"
"internal/charconv_bigint.cc"
"internal/charconv_parse.cc"
"internal/memutil.cc"
"internal/memutil.h"
"internal/utf8.cc"
......@@ -301,5 +307,43 @@ absl_test(
)
# test charconv_test
set(CHARCONV_TEST_SRC "charconv_test.cc")
set(CHARCONV_TEST_PUBLIC_LIBRARIES absl::strings)
absl_test(
TARGET
charconv_test
SOURCES
${CHARCONV_TEST_SRC}
PUBLIC_LIBRARIES
${CHARCONV_TEST_PUBLIC_LIBRARIES}
)
# test charconv_parse_test
set(CHARCONV_PARSE_TEST_SRC "internal/charconv_parse_test.cc")
set(CHARCONV_PARSE_TEST_PUBLIC_LIBRARIES absl::strings)
absl_test(
TARGET
charconv_parse_test
SOURCES
${CHARCONV_PARSE_TEST_SRC}
PUBLIC_LIBRARIES
${CHARCONV_PARSE_TEST_PUBLIC_LIBRARIES}
)
# test charconv_bigint_test
set(CHARCONV_BIGINT_TEST_SRC "internal/charconv_bigint_test.cc")
set(CHARCONV_BIGINT_TEST_PUBLIC_LIBRARIES absl::strings)
absl_test(
TARGET
charconv_bigint_test
SOURCES
${CHARCONV_BIGINT_TEST_SRC}
PUBLIC_LIBRARIES
${CHARCONV_BIGINT_TEST_PUBLIC_LIBRARIES}
)
// Copyright 2018 The Abseil Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#ifndef ABSL_STRINGS_CHARCONV_H_
#define ABSL_STRINGS_CHARCONV_H_
#include <system_error> // NOLINT(build/c++11)
namespace absl {
// Workalike compatibilty version of std::chars_format from C++17.
//
// This is an bitfield enumerator which can be passed to absl::from_chars to
// configure the std::string-to-float conversion.
enum class chars_format {
scientific = 1,
fixed = 2,
hex = 4,
general = fixed | scientific,
};
// The return result of a std::string-to-number conversion.
//
// `ec` will be set to `invalid_argument` if a well-formed number was not found
// at the start of the input range, `result_out_of_range` if a well-formed
// number was found, but it was out of the representable range of the requested
// type, or to std::errc() otherwise.
//
// If a well-formed number was found, `ptr` is set to one past the sequence of
// characters that were successfully parsed. If none was found, `ptr` is set
// to the `first` argument to from_chars.
struct from_chars_result {
const char* ptr;
std::errc ec;
};
// Workalike compatibilty version of std::from_chars from C++17. Currently
// this only supports the `double` and `float` types.
//
// This interface incorporates the proposed resolutions for library issues
// DR 3800 and DR 3801. If these are adopted with different wording,
// Abseil's behavior will change to match the standard. (The behavior most
// likely to change is for DR 3801, which says what `value` will be set to in
// the case of overflow and underflow. Code that wants to avoid possible
// breaking changes in this area should not depend on `value` when the returned
// from_chars_result indicates a range error.)
//
// Searches the range [first, last) for the longest matching pattern beginning
// at `first` that represents a floating point number. If one is found, store
// the result in `value`.
//
// The matching pattern format is almost the same as that of strtod(), except
// that C locale is not respected, and an initial '+' character in the input
// range will never be matched.
//
// If `fmt` is set, it must be one of the enumerator values of the chars_format.
// (This is despite the fact that chars_format is a bitmask type.) If set to
// `scientific`, a matching number must contain an exponent. If set to `fixed`,
// then an exponent will never match. (For example, the std::string "1e5" will be
// parsed as "1".) If set to `hex`, then a hexadecimal float is parsed in the
// format that strtod() accepts, except that a "0x" prefix is NOT matched.
// (In particular, in `hex` mode, the input "0xff" results in the largest
// matching pattern "0".)
absl::from_chars_result from_chars(const char* first, const char* last,
double& value, // NOLINT
chars_format fmt = chars_format::general);
absl::from_chars_result from_chars(const char* first, const char* last,
float& value, // NOLINT
chars_format fmt = chars_format::general);
// std::chars_format is specified as a bitmask type, which means the following
// operations must be provided:
inline constexpr chars_format operator&(chars_format lhs, chars_format rhs) {
return static_cast<chars_format>(static_cast<int>(lhs) &
static_cast<int>(rhs));
}
inline constexpr chars_format operator|(chars_format lhs, chars_format rhs) {
return static_cast<chars_format>(static_cast<int>(lhs) |
static_cast<int>(rhs));
}
inline constexpr chars_format operator^(chars_format lhs, chars_format rhs) {
return static_cast<chars_format>(static_cast<int>(lhs) ^
static_cast<int>(rhs));
}
inline constexpr chars_format operator~(chars_format arg) {
return static_cast<chars_format>(~static_cast<int>(arg));
}
inline chars_format& operator&=(chars_format& lhs, chars_format rhs) {
lhs = lhs & rhs;
return lhs;
}
inline chars_format& operator|=(chars_format& lhs, chars_format rhs) {
lhs = lhs | rhs;
return lhs;
}
inline chars_format& operator^=(chars_format& lhs, chars_format rhs) {
lhs = lhs ^ rhs;
return lhs;
}
} // namespace absl
#endif // ABSL_STRINGS_CHARCONV_H_
// Copyright 2018 The Abseil Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "absl/strings/charconv.h"
#include <cstdlib>
#include <cstring>
#include <string>
#include "benchmark/benchmark.h"
namespace {
void BM_Strtod_Pi(benchmark::State& state) {
const char* pi = "3.14159";
for (auto s : state) {
benchmark::DoNotOptimize(pi);
benchmark::DoNotOptimize(strtod(pi, nullptr));
}
}
BENCHMARK(BM_Strtod_Pi);
void BM_Absl_Pi(benchmark::State& state) {
const char* pi = "3.14159";
const char* pi_end = pi + strlen(pi);
for (auto s : state) {
benchmark::DoNotOptimize(pi);
double v;
absl::from_chars(pi, pi_end, v);
benchmark::DoNotOptimize(v);
}
}
BENCHMARK(BM_Absl_Pi);
void BM_Strtod_Pi_float(benchmark::State& state) {
const char* pi = "3.14159";
for (auto s : state) {
benchmark::DoNotOptimize(pi);
benchmark::DoNotOptimize(strtof(pi, nullptr));
}
}
BENCHMARK(BM_Strtod_Pi_float);
void BM_Absl_Pi_float(benchmark::State& state) {
const char* pi = "3.14159";
const char* pi_end = pi + strlen(pi);
for (auto s : state) {
benchmark::DoNotOptimize(pi);
float v;
absl::from_chars(pi, pi_end, v);
benchmark::DoNotOptimize(v);
}
}
BENCHMARK(BM_Absl_Pi_float);
void BM_Strtod_HardLarge(benchmark::State& state) {
const char* num = "272104041512242479.e200";
for (auto s : state) {
benchmark::DoNotOptimize(num);
benchmark::DoNotOptimize(strtod(num, nullptr));
}
}
BENCHMARK(BM_Strtod_HardLarge);
void BM_Absl_HardLarge(benchmark::State& state) {
const char* numstr = "272104041512242479.e200";
const char* numstr_end = numstr + strlen(numstr);
for (auto s : state) {
benchmark::DoNotOptimize(numstr);
double v;
absl::from_chars(numstr, numstr_end, v);
benchmark::DoNotOptimize(v);
}
}
BENCHMARK(BM_Absl_HardLarge);
void BM_Strtod_HardSmall(benchmark::State& state) {
const char* num = "94080055902682397.e-242";
for (auto s : state) {
benchmark::DoNotOptimize(num);
benchmark::DoNotOptimize(strtod(num, nullptr));
}
}
BENCHMARK(BM_Strtod_HardSmall);
void BM_Absl_HardSmall(benchmark::State& state) {
const char* numstr = "94080055902682397.e-242";
const char* numstr_end = numstr + strlen(numstr);
for (auto s : state) {
benchmark::DoNotOptimize(numstr);
double v;
absl::from_chars(numstr, numstr_end, v);
benchmark::DoNotOptimize(v);
}
}
BENCHMARK(BM_Absl_HardSmall);
void BM_Strtod_HugeMantissa(benchmark::State& state) {
std::string huge(200, '3');
const char* num = huge.c_str();
for (auto s : state) {
benchmark::DoNotOptimize(num);
benchmark::DoNotOptimize(strtod(num, nullptr));
}
}
BENCHMARK(BM_Strtod_HugeMantissa);
void BM_Absl_HugeMantissa(benchmark::State& state) {
std::string huge(200, '3');
const char* num = huge.c_str();
const char* num_end = num + 200;
for (auto s : state) {
benchmark::DoNotOptimize(num);
double v;
absl::from_chars(num, num_end, v);
benchmark::DoNotOptimize(v);
}
}
BENCHMARK(BM_Absl_HugeMantissa);
std::string MakeHardCase(int length) {
// The number 1.1521...e-297 is exactly halfway between 12345 * 2**-1000 and
// the next larger representable number. The digits of this number are in
// the std::string below.
const std::string digits =
"1."
"152113937042223790993097181572444900347587985074226836242307364987727724"
"831384300183638649152607195040591791364113930628852279348613864894524591"
"272746490313676832900762939595690019745859128071117417798540258114233761"
"012939937017879509401007964861774960297319002612457273148497158989073482"
"171377406078223015359818300988676687994537274548940612510414856761641652"
"513434981938564294004070500716200446656421722229202383105446378511678258"
"370570631774499359748259931676320916632111681001853983492795053244971606"
"922718923011680846577744433974087653954904214152517799883551075537146316"
"168973685866425605046988661997658648354773076621610279716804960009043764"
"038392994055171112475093876476783502487512538082706095923790634572014823"
"78877699375152587890625" +
std::string(5000, '0');
// generate the hard cases on either side for the given length.
// Lengths between 3 and 1000 are reasonable.
return digits.substr(0, length) + "1e-297";
}
void BM_Strtod_Big_And_Difficult(benchmark::State& state) {
std::string testcase = MakeHardCase(state.range(0));
const char* begin = testcase.c_str();
for (auto s : state) {
benchmark::DoNotOptimize(begin);
benchmark::DoNotOptimize(strtod(begin, nullptr));
}
}
BENCHMARK(BM_Strtod_Big_And_Difficult)->Range(3, 5000);
void BM_Absl_Big_And_Difficult(benchmark::State& state) {
std::string testcase = MakeHardCase(state.range(0));
const char* begin = testcase.c_str();
const char* end = begin + testcase.size();
for (auto s : state) {
benchmark::DoNotOptimize(begin);
double v;
absl::from_chars(begin, end, v);
benchmark::DoNotOptimize(v);
}
}
BENCHMARK(BM_Absl_Big_And_Difficult)->Range(3, 5000);
} // namespace
// ------------------------------------------------------------------------
// Benchmark Time CPU Iterations
// ------------------------------------------------------------------------
// BM_Strtod_Pi 96 ns 96 ns 6337454
// BM_Absl_Pi 35 ns 35 ns 20031996
// BM_Strtod_Pi_float 91 ns 91 ns 7745851
// BM_Absl_Pi_float 35 ns 35 ns 20430298
// BM_Strtod_HardLarge 133 ns 133 ns 5288341
// BM_Absl_HardLarge 181 ns 181 ns 3855615
// BM_Strtod_HardSmall 279 ns 279 ns 2517243
// BM_Absl_HardSmall 287 ns 287 ns 2458744
// BM_Strtod_HugeMantissa 433 ns 433 ns 1604293
// BM_Absl_HugeMantissa 160 ns 160 ns 4403671
// BM_Strtod_Big_And_Difficult/3 236 ns 236 ns 2942496
// BM_Strtod_Big_And_Difficult/8 232 ns 232 ns 2983796
// BM_Strtod_Big_And_Difficult/64 437 ns 437 ns 1591951
// BM_Strtod_Big_And_Difficult/512 1738 ns 1738 ns 402519
// BM_Strtod_Big_And_Difficult/4096 3943 ns 3943 ns 176128
// BM_Strtod_Big_And_Difficult/5000 4397 ns 4397 ns 157878
// BM_Absl_Big_And_Difficult/3 39 ns 39 ns 17799583
// BM_Absl_Big_And_Difficult/8 43 ns 43 ns 16096859
// BM_Absl_Big_And_Difficult/64 550 ns 550 ns 1259717
// BM_Absl_Big_And_Difficult/512 4167 ns 4167 ns 171414
// BM_Absl_Big_And_Difficult/4096 9160 ns 9159 ns 76297
// BM_Absl_Big_And_Difficult/5000 9738 ns 9738 ns 70140
// Copyright 2018 The Abseil Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "absl/strings/internal/charconv_bigint.h"
#include <string>
#include "gtest/gtest.h"
namespace absl {
namespace strings_internal {
TEST(BigUnsigned, ShiftLeft) {
{
// Check that 3 * 2**100 is calculated correctly
BigUnsigned<4> num(3u);
num.ShiftLeft(100);
EXPECT_EQ(num, BigUnsigned<4>("3802951800684688204490109616128"));
}
{
// Test that overflow is truncated properly.
// 15 is 4 bits long, and BigUnsigned<4> is a 128-bit bigint.
// Shifting left by 125 bits should truncate off the high bit, so that
// 15 << 125 == 7 << 125
// after truncation.
BigUnsigned<4> a(15u);
BigUnsigned<4> b(7u);
BigUnsigned<4> c(3u);
a.ShiftLeft(125);
b.ShiftLeft(125);
c.ShiftLeft(125);
EXPECT_EQ(a, b);
EXPECT_NE(a, c);
}
{
// Same test, larger bigint:
BigUnsigned<84> a(15u);
BigUnsigned<84> b(7u);
BigUnsigned<84> c(3u);
a.ShiftLeft(84 * 32 - 3);
b.ShiftLeft(84 * 32 - 3);
c.ShiftLeft(84 * 32 - 3);
EXPECT_EQ(a, b);
EXPECT_NE(a, c);
}
{
// Check that incrementally shifting has the same result as doing it all at
// once (attempting to capture corner cases.)
const std::string seed = "1234567890123456789012345678901234567890";
BigUnsigned<84> a(seed);
for (int i = 1; i <= 84 * 32; ++i) {
a.ShiftLeft(1);
BigUnsigned<84> b(seed);
b.ShiftLeft(i);
EXPECT_EQ(a, b);
}
// And we should have fully rotated all bits off by now:
EXPECT_EQ(a, BigUnsigned<84>(0u));
}
}
TEST(BigUnsigned, MultiplyByUint32) {
const BigUnsigned<84> factorial_100(
"933262154439441526816992388562667004907159682643816214685929638952175999"
"932299156089414639761565182862536979208272237582511852109168640000000000"
"00000000000000");
BigUnsigned<84> a(1u);
for (uint32_t i = 1; i <= 100; ++i) {
a.MultiplyBy(i);
}
EXPECT_EQ(a, BigUnsigned<84>(factorial_100));
}
TEST(BigUnsigned, MultiplyByBigUnsigned) {
{
// Put the terms of factorial_200 into two bigints, and multiply them
// together.
const BigUnsigned<84> factorial_200(
"7886578673647905035523632139321850622951359776871732632947425332443594"
"4996340334292030428401198462390417721213891963883025764279024263710506"
"1926624952829931113462857270763317237396988943922445621451664240254033"
"2918641312274282948532775242424075739032403212574055795686602260319041"
"7032406235170085879617892222278962370389737472000000000000000000000000"
"0000000000000000000000000");
BigUnsigned<84> evens(1u);
BigUnsigned<84> odds(1u);
for (uint32_t i = 1; i < 200; i += 2) {
odds.MultiplyBy(i);
evens.MultiplyBy(i + 1);
}
evens.MultiplyBy(odds);
EXPECT_EQ(evens, factorial_200);
}
{
// Multiply various powers of 10 together.
for (int a = 0 ; a < 700; a += 25) {
SCOPED_TRACE(a);
BigUnsigned<84> a_value("3" + std::string(a, '0'));
for (int b = 0; b < (700 - a); b += 25) {
SCOPED_TRACE(b);
BigUnsigned<84> b_value("2" + std::string(b, '0'));
BigUnsigned<84> expected_product("6" + std::string(a + b, '0'));
b_value.MultiplyBy(a_value);
EXPECT_EQ(b_value, expected_product);
}
}
}
}
TEST(BigUnsigned, MultiplyByOverflow) {
{
// Check that multiplcation overflow predictably truncates.
// A big int with all bits on.
BigUnsigned<4> all_bits_on("340282366920938463463374607431768211455");
// Modulo 2**128, this is equal to -1. Therefore the square of this,
// modulo 2**128, should be 1.
all_bits_on.MultiplyBy(all_bits_on);
EXPECT_EQ(all_bits_on, BigUnsigned<4>(1u));
}
{
// Try multiplying a large bigint by 2**50, and compare the result to
// shifting.
BigUnsigned<4> value_1("12345678901234567890123456789012345678");
BigUnsigned<4> value_2("12345678901234567890123456789012345678");
BigUnsigned<4> two_to_fiftieth(1u);
two_to_fiftieth.ShiftLeft(50);
value_1.ShiftLeft(50);
value_2.MultiplyBy(two_to_fiftieth);
EXPECT_EQ(value_1, value_2);
}
}
TEST(BigUnsigned, FiveToTheNth) {
{
// Sanity check that MultiplyByFiveToTheNth gives consistent answers, up to
// and including overflow.
for (int i = 0; i < 1160; ++i) {
SCOPED_TRACE(i);
BigUnsigned<84> value_1(123u);
BigUnsigned<84> value_2(123u);
value_1.MultiplyByFiveToTheNth(i);
for (int j = 0; j < i; j++) {
value_2.MultiplyBy(5u);
}
EXPECT_EQ(value_1, value_2);
}
}
{
// Check that the faster, table-lookup-based static method returns the same
// result that multiplying in-place would return, up to and including
// overflow.
for (int i = 0; i < 1160; ++i) {
SCOPED_TRACE(i);
BigUnsigned<84> value_1(1u);
value_1.MultiplyByFiveToTheNth(i);
BigUnsigned<84> value_2 = BigUnsigned<84>::FiveToTheNth(i);
EXPECT_EQ(value_1, value_2);
}
}
}
TEST(BigUnsigned, TenToTheNth) {
{
// Sanity check MultiplyByTenToTheNth.
for (int i = 0; i < 800; ++i) {
SCOPED_TRACE(i);
BigUnsigned<84> value_1(123u);
BigUnsigned<84> value_2(123u);
value_1.MultiplyByTenToTheNth(i);
for (int j = 0; j < i; j++) {
value_2.MultiplyBy(10u);
}
EXPECT_EQ(value_1, value_2);
}
}
{
// Alternate testing approach, taking advantage of the decimal parser.
for (int i = 0; i < 200; ++i) {
SCOPED_TRACE(i);
BigUnsigned<84> value_1(135u);
value_1.MultiplyByTenToTheNth(i);
BigUnsigned<84> value_2("135" + std::string(i, '0'));
EXPECT_EQ(value_1, value_2);
}
}
}
} // namespace strings_internal
} // namespace absl
// Copyright 2018 The Abseil Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#ifndef ABSL_STRINGS_INTERNAL_CHARCONV_PARSE_H_
#define ABSL_STRINGS_INTERNAL_CHARCONV_PARSE_H_
#include <cstdint>
#include "absl/strings/charconv.h"
namespace absl {
namespace strings_internal {
// Enum indicating whether a parsed float is a number or special value.
enum class FloatType { kNumber, kInfinity, kNan };
// The decomposed parts of a parsed `float` or `double`.
struct ParsedFloat {
// Representation of the parsed mantissa, with the decimal point adjusted to
// make it an integer.
//
// During decimal scanning, this contains 19 significant digits worth of
// mantissa value. If digits beyond this point are found, they
// are truncated, and if any of these dropped digits are nonzero, then
// `mantissa` is inexact, and the full mantissa is stored in [subrange_begin,
// subrange_end).
//
// During hexadecimal scanning, this contains 15 significant hex digits worth
// of mantissa value. Digits beyond this point are sticky -- they are
// truncated, but if any dropped digits are nonzero, the low bit of mantissa
// will be set. (This allows for precise rounding, and avoids the need
// to store the full mantissa in [subrange_begin, subrange_end).)
uint64_t mantissa = 0;
// Floating point expontent. This reflects any decimal point adjustments and
// any truncated digits from the mantissa. The absolute value of the parsed
// number is represented by mantissa * (base ** exponent), where base==10 for
// decimal floats, and base==2 for hexadecimal floats.
int exponent = 0;
// The literal exponent value scanned from the input, or 0 if none was
// present. This does not reflect any adjustments applied to mantissa.
int literal_exponent = 0;
// The type of number scanned.
FloatType type = FloatType::kNumber;
// When non-null, [subrange_begin, subrange_end) marks a range of characters
// that require further processing. The meaning is dependent on float type.
// If type == kNumber and this is set, this is a "wide input": the input
// mantissa contained more than 19 digits. The range contains the full
// mantissa. It plus `literal_exponent` need to be examined to find the best
// floating point match.
// If type == kNan and this is set, the range marks the contents of a
// matched parenthesized character region after the NaN.
const char* subrange_begin = nullptr;
const char* subrange_end = nullptr;
// One-past-the-end of the successfully parsed region, or nullptr if no
// matching pattern was found.
const char* end = nullptr;
};
// Read the floating point number in the provided range, and populate
// ParsedFloat accordingly.
//
// format_flags is a bitmask value specifying what patterns this API will match.
// `scientific` and `fixed` are honored per std::from_chars rules
// ([utility.from.chars], C++17): if exactly one of these bits is set, then an
// exponent is required, or dislallowed, respectively.
//
// Template parameter `base` must be either 10 or 16. For base 16, a "0x" is
// *not* consumed. The `hex` bit from format_flags is ignored by ParseFloat.
template <int base>
ParsedFloat ParseFloat(const char* begin, const char* end,
absl::chars_format format_flags);
extern template ParsedFloat ParseFloat<10>(const char* begin, const char* end,
absl::chars_format format_flags);
extern template ParsedFloat ParseFloat<16>(const char* begin, const char* end,
absl::chars_format format_flags);
} // namespace strings_internal
} // namespace absl
#endif // ABSL_STRINGS_INTERNAL_CHARCONV_PARSE_H_
......@@ -32,6 +32,7 @@
#include "absl/base/internal/raw_logging.h"
#include "absl/strings/ascii.h"
#include "absl/strings/charconv.h"
#include "absl/strings/internal/bits.h"
#include "absl/strings/internal/memutil.h"
#include "absl/strings/str_cat.h"
......@@ -40,51 +41,54 @@ namespace absl {
bool SimpleAtof(absl::string_view str, float* value) {
*value = 0.0;
if (str.empty()) return false;
char buf[32];
std::unique_ptr<char[]> bigbuf;
char* ptr = buf;
if (str.size() > sizeof(buf) - 1) {
bigbuf.reset(new char[str.size() + 1]);
ptr = bigbuf.get();
}
memcpy(ptr, str.data(), str.size());
ptr[str.size()] = '\0';
char* endptr;
*value = strtof(ptr, &endptr);
if (endptr != ptr) {
while (absl::ascii_isspace(*endptr)) ++endptr;
}
// Ignore range errors from strtod/strtof.
// The values it returns on underflow and
// overflow are the right fallback in a
// robust setting.
return *ptr != '\0' && *endptr == '\0';
str = StripAsciiWhitespace(str);
if (!str.empty() && str[0] == '+') {
str.remove_prefix(1);
}
auto result = absl::from_chars(str.data(), str.data() + str.size(), *value);
if (result.ec == std::errc::invalid_argument) {
return false;
}
if (result.ptr != str.data() + str.size()) {
// not all non-whitespace characters consumed
return false;
}
// from_chars() with DR 3801's current wording will return max() on
// overflow. SimpleAtof returns infinity instead.
if (result.ec == std::errc::result_out_of_range) {
if (*value > 1.0) {
*value = std::numeric_limits<float>::infinity();
} else if (*value < -1.0) {
*value = -std::numeric_limits<float>::infinity();
}
}
return true;
}
bool SimpleAtod(absl::string_view str, double* value) {
*value = 0.0;
if (str.empty()) return false;
char buf[32];
std::unique_ptr<char[]> bigbuf;
char* ptr = buf;
if (str.size() > sizeof(buf) - 1) {
bigbuf.reset(new char[str.size() + 1]);
ptr = bigbuf.get();
}
memcpy(ptr, str.data(), str.size());
ptr[str.size()] = '\0';
char* endptr;
*value = strtod(ptr, &endptr);
if (endptr != ptr) {
while (absl::ascii_isspace(*endptr)) ++endptr;
}
// Ignore range errors from strtod. The values it
// returns on underflow and overflow are the right
// fallback in a robust setting.
return *ptr != '\0' && *endptr == '\0';
str = StripAsciiWhitespace(str);
if (!str.empty() && str[0] == '+') {
str.remove_prefix(1);
}
auto result = absl::from_chars(str.data(), str.data() + str.size(), *value);
if (result.ec == std::errc::invalid_argument) {
return false;
}
if (result.ptr != str.data() + str.size()) {
// not all non-whitespace characters consumed
return false;
}
// from_chars() with DR 3801's current wording will return max() on
// overflow. SimpleAtod returns infinity instead.
if (result.ec == std::errc::result_out_of_range) {
if (*value > 1.0) {
*value = std::numeric_limits<double>::infinity();
} else if (*value < -1.0) {
*value = -std::numeric_limits<double>::infinity();
}
}
return true;
}
namespace {
......
......@@ -149,12 +149,6 @@ cc_test(
size = "large",
srcs = ["mutex_test.cc"],
copts = ABSL_TEST_COPTS,
tags = [
"no_test_android_arm",
"no_test_android_arm64",
"no_test_android_x86",
"no_test_loonix", # Too slow.
],
deps = [
":synchronization",
":thread_pool",
......
......@@ -44,6 +44,7 @@ cc_library(
"//absl/base",
"//absl/base:core_headers",
"//absl/numeric:int128",
"//absl/strings",
"//absl/time/internal/cctz:civil_time",
"//absl/time/internal/cctz:time_zone",
],
......@@ -80,9 +81,6 @@ cc_test(
"time_zone_test.cc",
],
copts = ABSL_TEST_COPTS,
tags = [
"no_test_loonix",
],
deps = [
":test_util",
":time",
......
......@@ -53,7 +53,7 @@ list(APPEND TIME_SRC
${TIME_PUBLIC_HEADERS}
${TIME_INTERNAL_HEADERS}
)
set(TIME_PUBLIC_LIBRARIES absl::base absl::stacktrace absl::int128)
set(TIME_PUBLIC_LIBRARIES absl::base absl::stacktrace absl::int128 absl::strings)
absl_library(
TARGET
......
......@@ -896,8 +896,7 @@ bool ParseDuration(const std::string& dur_string, Duration* d) {
return true;
}
// TODO(absl-team): Remove once dependencies are removed.
bool ParseFlag(const std::string& text, Duration* dst, std::string* /* err */) {
bool ParseFlag(const std::string& text, Duration* dst, std::string* ) {
return ParseDuration(text, dst);
}
......
......@@ -129,7 +129,6 @@ bool ParseTime(const std::string& format, const std::string& input, absl::TimeZo
return b;
}
// TODO(absl-team): Remove once dependencies are removed.
// Functions required to support absl::Time flags.
bool ParseFlag(const std::string& text, absl::Time* t, std::string* error) {
return absl::ParseTime(RFC3339_full, text, absl::UTCTimeZone(), t, error);
......
......@@ -64,6 +64,7 @@
#include <utility>
#include "absl/base/port.h" // Needed for string vs std::string
#include "absl/strings/string_view.h"
#include "absl/time/internal/cctz/include/cctz/time_zone.h"
namespace absl {
......@@ -491,9 +492,6 @@ inline std::ostream& operator<<(std::ostream& os, Duration d) {
// `ZeroDuration()`. Parses "inf" and "-inf" as +/- `InfiniteDuration()`.
bool ParseDuration(const std::string& dur_string, Duration* d);
// Flag Support
// TODO(absl-team): Remove once dependencies are removed.
// ParseFlag()
//
bool ParseFlag(const std::string& text, Duration* dst, std::string* error);
......@@ -993,8 +991,6 @@ bool ParseTime(const std::string& format, const std::string& input, Time* time,
bool ParseTime(const std::string& format, const std::string& input, TimeZone tz,
Time* time, std::string* err);
// TODO(absl-team): Remove once dependencies are removed.
// ParseFlag()
// UnparseFlag()
//
......
......@@ -48,7 +48,7 @@ using std::optional;
using std::make_optional;
using std::nullopt_t;
using std::nullopt;
}
} // namespace absl
#else // ABSL_HAVE_STD_OPTIONAL
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment