Skip to content

Commit d46c2e9

Browse files
committed
CR round 9 minor changes.
- Removed some auto usages where possible. - More comments. - Miscellaneous.
1 parent ee70a74 commit d46c2e9

File tree

2 files changed

+18
-17
lines changed

2 files changed

+18
-17
lines changed

src/s2/util/math/exactfloat/bignum.cc

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -118,12 +118,12 @@ std::optional<Bignum> Bignum::FromString(absl::string_view s) {
118118

119119
const auto end = s.cend();
120120
while (begin < end) {
121-
int64_t chunk_len = std::min(static_cast<size_t>(std::distance(begin, end)),
122-
kMaxChunkDigits);
123-
121+
size_t chunk_len = std::min(static_cast<size_t>(std::distance(begin, end)),
122+
kMaxChunkDigits);
124123
Bigit chunk = 0;
125124
auto result = std::from_chars(begin, begin + chunk_len, chunk);
126-
if (result.ec != std::errc() || (result.ptr - begin) != chunk_len) {
125+
if (result.ec != std::errc() ||
126+
static_cast<size_t>(result.ptr - begin) != chunk_len) {
127127
return std::nullopt;
128128
}
129129
begin += chunk_len;
@@ -234,7 +234,7 @@ Bignum& Bignum::operator>>=(int nbit) {
234234
// Then, handle the within-bigit shift, if any.
235235
if (nrem != 0) {
236236
Bigit carry = 0;
237-
for (auto i = static_cast<int64_t>(bigits_.size()) - 1; i >= 0; --i) {
237+
for (int64_t i = static_cast<int64_t>(bigits_.size()) - 1; i >= 0; --i) {
238238
const Bigit old_val = bigits_[i];
239239
bigits_[i] = (old_val >> nrem) | carry;
240240
carry = old_val << (kBigitBits - nrem);
@@ -299,7 +299,7 @@ inline Bigit AddBigit(Bigit a, Bigit b, Bigit* absl_nonnull carry) {
299299
_addcarry_u64(*carry, a, b, reinterpret_cast<unsigned long long*>(&out));
300300
return out;
301301
#else
302-
auto sum = absl::uint128(a) + b + *carry;
302+
absl::uint128 sum = absl::uint128(a) + b + *carry;
303303
*carry = absl::Uint128High64(sum);
304304
return static_cast<Bigit>(sum);
305305
#endif
@@ -313,7 +313,7 @@ inline Bigit SubBigit(Bigit a, Bigit b, Bigit* absl_nonnull borrow) {
313313
// See notes in AddBigit on why using an intrinsic is the right choice here.
314314
#ifdef __x86_64__
315315
Bigit out;
316-
*borrow = _subborrow_u64(*borrow, a, b,
316+
*borrow = _subborrow_u64(static_cast<char>(*borrow), a, b,
317317
reinterpret_cast<unsigned long long*>(&out));
318318
return out;
319319
#else
@@ -325,7 +325,7 @@ inline Bigit SubBigit(Bigit a, Bigit b, Bigit* absl_nonnull borrow) {
325325

326326
// Computes a * b + carry and updates the carry.
327327
inline Bigit MulBigit(Bigit a, Bigit b, Bigit* absl_nonnull carry) {
328-
auto sum = absl::uint128(a) * b + *carry;
328+
absl::uint128 sum = absl::uint128(a) * b + *carry;
329329
*carry = absl::Uint128High64(sum);
330330
return static_cast<Bigit>(sum);
331331
}
@@ -347,7 +347,7 @@ inline void MulAddBigit(Bigit* absl_nonnull sum, Bigit a, Bigit b,
347347
// overflow flag rather than the carry flag, which lets you do two
348348
// extended-precision add operations in parallel without having them stomp on
349349
// each other's carry flags. )
350-
auto term = absl::uint128(a) * b + *carry + *sum;
350+
absl::uint128 term = absl::uint128(a) * b + *carry + *sum;
351351
*carry = absl::Uint128High64(term);
352352
*sum = static_cast<Bigit>(term);
353353
}
@@ -402,7 +402,7 @@ ABSL_ATTRIBUTE_NOINLINE inline Bigit AddInPlace(absl::Span<Bigit> a,
402402
// This allows for using a pre-allocated buffer to store the result of an
403403
// addition followed by trimming down to size:
404404
//
405-
// auto out = arena.Alloc(std::max(a.size(), b.size()) + 1);
405+
// absl::Span<Bigit> out = arena.Alloc(std::max(a.size(), b.size()) + 1);
406406
// out = out.first(AddInto(out, a, b));
407407
//
408408
// Which is used in the Karatsuba multiplication, where we don't have the option
@@ -430,7 +430,7 @@ inline size_t Add(absl::Span<Bigit> dst, absl::Span<const Bigit> a,
430430
}
431431

432432
// Copy remaining digits from the longer operand and propagate carry.
433-
auto longer = (a.size() > b.size()) ? a : b;
433+
absl::Span<const Bigit> longer = (a.size() > b.size()) ? a : b;
434434

435435
// Dispatch four at a time for the remaining part.
436436
const size_t size = longer.size();
@@ -575,7 +575,7 @@ inline void MulQuadratic(absl::Span<Bigit> dst, absl::Span<const Bigit> a,
575575
// Each call to MulAdd and MulAddInPlace only updates a.size() elements of out
576576
// so we manually set the carries as we go. We grab a span to the upper half
577577
// of out starting at a.size() to facilitate this.
578-
auto upper = dst.subspan(a.size());
578+
absl::Span<Bigit> upper = dst.subspan(a.size());
579579
upper[0] = MulWithCarry(dst, a, b[0], 0);
580580

581581
const size_t size = b.size();
@@ -643,7 +643,8 @@ class Arena {
643643
};
644644

645645
// Returns the total arena size needed to multiply two number of a_size and
646-
// b_size bigits using the recursive Karatsuba implementation.
646+
// b_size bigits using the recursive Karatsuba implementation. This is enough
647+
// space for all the required recursive calls of KaratsubaMulRecursive.
647648
inline size_t ArenaSize(size_t a_size, size_t b_size) {
648649
// Each step of Karatsuba splits at:
649650
// N = (std::max(a.size() + b.size() + 1) / 2
@@ -742,7 +743,7 @@ inline void KaratsubaMulRecursive(absl::Span<Bigit> dst,
742743
}
743744

744745
// Compute z1 = asum*bsum - z0 - z2 = (a0 + a1)*(b0 + b1) - z0 - z2
745-
auto z1 = arena->Alloc(asum.size() + bsum.size());
746+
absl::Span<Bigit> z1 = arena->Alloc(asum.size() + bsum.size());
746747

747748
// Compute asum * bsum into the beginning of z1
748749
KaratsubaMulRecursive(z1, asum, bsum, arena);
@@ -755,7 +756,7 @@ inline void KaratsubaMulRecursive(absl::Span<Bigit> dst,
755756

756757
// We need to add z1*10^half, which we can do by simply adding z1 at a shifted
757758
// position in the output.
758-
auto dst_z1 = dst.subspan(half);
759+
absl::Span<Bigit> dst_z1 = dst.subspan(half);
759760

760761
// Although the value of z1 is guaranteed to fit in the available space of
761762
// dst, it may have one or more high-order zero bigits because it was sized

src/s2/util/math/exactfloat/exactfloat.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,12 +67,12 @@ ExactFloat::ExactFloat(double v) {
6767
// Calculates abs(v) without UB. SafeAbs(INT_MIN) == INT_MIN.
6868
// Generates the same code as std::abs().
6969
// https://godbolt.org/z/eT6KW1zGb
70-
int SafeAbs(int v) { return v < 0 ? -static_cast<unsigned>(v) : v; }
70+
unsigned SafeAbs(int v) { return v < 0 ? -static_cast<unsigned>(v) : v; }
7171

7272
ExactFloat::ExactFloat(int v) {
7373
sign_ = (v >= 0) ? 1 : -1;
7474
bn_exp_ = 0;
75-
bn_ = Bignum(static_cast<unsigned>(SafeAbs(v)));
75+
bn_ = Bignum(SafeAbs(v));
7676
Canonicalize();
7777
}
7878

0 commit comments

Comments
 (0)