31 #include <google/protobuf/stubs/int128.h>
37 #include <google/protobuf/stubs/logging.h>
39 #include <google/protobuf/port_def.inc>
53 #define STEP(T, n, pos, sh) \
55 if ((n) >= (static_cast<T>(1) << (sh))) { \
76 return Fls64(hi) + 64;
82 uint128* quotient_ret, uint128* remainder_ret) {
84 GOOGLE_LOG(
FATAL) <<
"Division or mod by zero: dividend.hi=" << dividend.hi_
85 <<
", lo=" << dividend.lo_;
86 }
else if (dividend < divisor) {
88 *remainder_ret = dividend;
91 int dividend_bit_length =
Fls128(dividend);
92 int divisor_bit_length =
Fls128(divisor);
93 int difference = dividend_bit_length - divisor_bit_length;
98 if (shifted_divisor <= dividend) {
99 dividend -= shifted_divisor;
105 *quotient_ret = quotient;
106 *remainder_ret = dividend;
114 DivModImpl(*
this, divisor, "ient, &remainder);
121 DivModImpl(*
this, divisor, "ient, &remainder);
126 std::ostream&
operator<<(std::ostream& o,
const uint128&
b) {
127 std::ios_base::fmtflags
flags =
o.flags();
131 std::streamsize div_base_log;
132 switch (
flags & std::ios::basefield) {
139 div =
static_cast<uint64>(
144 div =
static_cast<uint64>(
153 std::ostringstream os;
154 std::ios_base::fmtflags copy_mask =
155 std::ios::basefield | std::ios::showbase | std::ios::uppercase;
156 os.setf(
flags & copy_mask, copy_mask);
164 os << std::noshowbase << std::setfill(
'0') << std::setw(div_base_log);
166 os << std::setw(div_base_log);
167 }
else if (mid.lo_ != 0) {
169 os << std::noshowbase << std::setfill(
'0') << std::setw(div_base_log);
175 std::streamsize
width =
o.width(0);
177 if ((
flags & std::ios::adjustfield) == std::ios::left) {
180 rep.insert(
static_cast<std::string::size_type
>(0),
192 #include <google/protobuf/port_undef.inc>