git repos / blockattack-game

blame: source/code/Libs/include/cereal/external/rapidjson/internal/biginteger.h

normal view · raw

6c5f2c01 sago007 2017-03-15 17:56 1
// Tencent is pleased to support the open source community by making RapidJSON available.
6c5f2c01 sago007 2017-03-15 17:56 2
// 
6c5f2c01 sago007 2017-03-15 17:56 3
// Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip. All rights reserved.
6c5f2c01 sago007 2017-03-15 17:56 4
//
6c5f2c01 sago007 2017-03-15 17:56 5
// Licensed under the MIT License (the "License"); you may not use this file except
6c5f2c01 sago007 2017-03-15 17:56 6
// in compliance with the License. You may obtain a copy of the License at
6c5f2c01 sago007 2017-03-15 17:56 7
//
6c5f2c01 sago007 2017-03-15 17:56 8
// http://opensource.org/licenses/MIT
6c5f2c01 sago007 2017-03-15 17:56 9
//
6c5f2c01 sago007 2017-03-15 17:56 10
// Unless required by applicable law or agreed to in writing, software distributed 
6c5f2c01 sago007 2017-03-15 17:56 11
// under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR 
6c5f2c01 sago007 2017-03-15 17:56 12
// CONDITIONS OF ANY KIND, either express or implied. See the License for the 
6c5f2c01 sago007 2017-03-15 17:56 13
// specific language governing permissions and limitations under the License.
6c5f2c01 sago007 2017-03-15 17:56 14
6c5f2c01 sago007 2017-03-15 17:56 15
#ifndef CEREAL_RAPIDJSON_BIGINTEGER_H_
6c5f2c01 sago007 2017-03-15 17:56 16
#define CEREAL_RAPIDJSON_BIGINTEGER_H_
6c5f2c01 sago007 2017-03-15 17:56 17
6c5f2c01 sago007 2017-03-15 17:56 18
#include "../rapidjson.h"
6c5f2c01 sago007 2017-03-15 17:56 19
6c5f2c01 sago007 2017-03-15 17:56 20
#if defined(_MSC_VER) && defined(_M_AMD64)
6c5f2c01 sago007 2017-03-15 17:56 21
#include <intrin.h> // for _umul128
6c5f2c01 sago007 2017-03-15 17:56 22
#pragma intrinsic(_umul128)
6c5f2c01 sago007 2017-03-15 17:56 23
#endif
6c5f2c01 sago007 2017-03-15 17:56 24
6c5f2c01 sago007 2017-03-15 17:56 25
CEREAL_RAPIDJSON_NAMESPACE_BEGIN
6c5f2c01 sago007 2017-03-15 17:56 26
namespace internal {
6c5f2c01 sago007 2017-03-15 17:56 27
6c5f2c01 sago007 2017-03-15 17:56 28
class BigInteger {
6c5f2c01 sago007 2017-03-15 17:56 29
public:
6c5f2c01 sago007 2017-03-15 17:56 30
    typedef uint64_t Type;
6c5f2c01 sago007 2017-03-15 17:56 31
6c5f2c01 sago007 2017-03-15 17:56 32
    BigInteger(const BigInteger& rhs) : count_(rhs.count_) {
6c5f2c01 sago007 2017-03-15 17:56 33
        std::memcpy(digits_, rhs.digits_, count_ * sizeof(Type));
6c5f2c01 sago007 2017-03-15 17:56 34
    }
6c5f2c01 sago007 2017-03-15 17:56 35
6c5f2c01 sago007 2017-03-15 17:56 36
    explicit BigInteger(uint64_t u) : count_(1) {
6c5f2c01 sago007 2017-03-15 17:56 37
        digits_[0] = u;
6c5f2c01 sago007 2017-03-15 17:56 38
    }
6c5f2c01 sago007 2017-03-15 17:56 39
6c5f2c01 sago007 2017-03-15 17:56 40
    BigInteger(const char* decimals, size_t length) : count_(1) {
6c5f2c01 sago007 2017-03-15 17:56 41
        CEREAL_RAPIDJSON_ASSERT(length > 0);
6c5f2c01 sago007 2017-03-15 17:56 42
        digits_[0] = 0;
6c5f2c01 sago007 2017-03-15 17:56 43
        size_t i = 0;
6c5f2c01 sago007 2017-03-15 17:56 44
        const size_t kMaxDigitPerIteration = 19;  // 2^64 = 18446744073709551616 > 10^19
6c5f2c01 sago007 2017-03-15 17:56 45
        while (length >= kMaxDigitPerIteration) {
6c5f2c01 sago007 2017-03-15 17:56 46
            AppendDecimal64(decimals + i, decimals + i + kMaxDigitPerIteration);
6c5f2c01 sago007 2017-03-15 17:56 47
            length -= kMaxDigitPerIteration;
6c5f2c01 sago007 2017-03-15 17:56 48
            i += kMaxDigitPerIteration;
6c5f2c01 sago007 2017-03-15 17:56 49
        }
6c5f2c01 sago007 2017-03-15 17:56 50
6c5f2c01 sago007 2017-03-15 17:56 51
        if (length > 0)
6c5f2c01 sago007 2017-03-15 17:56 52
            AppendDecimal64(decimals + i, decimals + i + length);
6c5f2c01 sago007 2017-03-15 17:56 53
    }
6c5f2c01 sago007 2017-03-15 17:56 54
    
6c5f2c01 sago007 2017-03-15 17:56 55
    BigInteger& operator=(const BigInteger &rhs)
6c5f2c01 sago007 2017-03-15 17:56 56
    {
6c5f2c01 sago007 2017-03-15 17:56 57
        if (this != &rhs) {
6c5f2c01 sago007 2017-03-15 17:56 58
            count_ = rhs.count_;
6c5f2c01 sago007 2017-03-15 17:56 59
            std::memcpy(digits_, rhs.digits_, count_ * sizeof(Type));
6c5f2c01 sago007 2017-03-15 17:56 60
        }
6c5f2c01 sago007 2017-03-15 17:56 61
        return *this;
6c5f2c01 sago007 2017-03-15 17:56 62
    }
6c5f2c01 sago007 2017-03-15 17:56 63
    
6c5f2c01 sago007 2017-03-15 17:56 64
    BigInteger& operator=(uint64_t u) {
6c5f2c01 sago007 2017-03-15 17:56 65
        digits_[0] = u;            
6c5f2c01 sago007 2017-03-15 17:56 66
        count_ = 1;
6c5f2c01 sago007 2017-03-15 17:56 67
        return *this;
6c5f2c01 sago007 2017-03-15 17:56 68
    }
6c5f2c01 sago007 2017-03-15 17:56 69
6c5f2c01 sago007 2017-03-15 17:56 70
    BigInteger& operator+=(uint64_t u) {
6c5f2c01 sago007 2017-03-15 17:56 71
        Type backup = digits_[0];
6c5f2c01 sago007 2017-03-15 17:56 72
        digits_[0] += u;
6c5f2c01 sago007 2017-03-15 17:56 73
        for (size_t i = 0; i < count_ - 1; i++) {
6c5f2c01 sago007 2017-03-15 17:56 74
            if (digits_[i] >= backup)
6c5f2c01 sago007 2017-03-15 17:56 75
                return *this; // no carry
6c5f2c01 sago007 2017-03-15 17:56 76
            backup = digits_[i + 1];
6c5f2c01 sago007 2017-03-15 17:56 77
            digits_[i + 1] += 1;
6c5f2c01 sago007 2017-03-15 17:56 78
        }
6c5f2c01 sago007 2017-03-15 17:56 79
6c5f2c01 sago007 2017-03-15 17:56 80
        // Last carry
6c5f2c01 sago007 2017-03-15 17:56 81
        if (digits_[count_ - 1] < backup)
6c5f2c01 sago007 2017-03-15 17:56 82
            PushBack(1);
6c5f2c01 sago007 2017-03-15 17:56 83
6c5f2c01 sago007 2017-03-15 17:56 84
        return *this;
6c5f2c01 sago007 2017-03-15 17:56 85
    }
6c5f2c01 sago007 2017-03-15 17:56 86
6c5f2c01 sago007 2017-03-15 17:56 87
    BigInteger& operator*=(uint64_t u) {
6c5f2c01 sago007 2017-03-15 17:56 88
        if (u == 0) return *this = 0;
6c5f2c01 sago007 2017-03-15 17:56 89
        if (u == 1) return *this;
6c5f2c01 sago007 2017-03-15 17:56 90
        if (*this == 1) return *this = u;
6c5f2c01 sago007 2017-03-15 17:56 91
6c5f2c01 sago007 2017-03-15 17:56 92
        uint64_t k = 0;
6c5f2c01 sago007 2017-03-15 17:56 93
        for (size_t i = 0; i < count_; i++) {
6c5f2c01 sago007 2017-03-15 17:56 94
            uint64_t hi;
6c5f2c01 sago007 2017-03-15 17:56 95
            digits_[i] = MulAdd64(digits_[i], u, k, &hi);
6c5f2c01 sago007 2017-03-15 17:56 96
            k = hi;
6c5f2c01 sago007 2017-03-15 17:56 97
        }
6c5f2c01 sago007 2017-03-15 17:56 98
        
6c5f2c01 sago007 2017-03-15 17:56 99
        if (k > 0)
6c5f2c01 sago007 2017-03-15 17:56 100
            PushBack(k);
6c5f2c01 sago007 2017-03-15 17:56 101
6c5f2c01 sago007 2017-03-15 17:56 102
        return *this;
6c5f2c01 sago007 2017-03-15 17:56 103
    }
6c5f2c01 sago007 2017-03-15 17:56 104
6c5f2c01 sago007 2017-03-15 17:56 105
    BigInteger& operator*=(uint32_t u) {
6c5f2c01 sago007 2017-03-15 17:56 106
        if (u == 0) return *this = 0;
6c5f2c01 sago007 2017-03-15 17:56 107
        if (u == 1) return *this;
6c5f2c01 sago007 2017-03-15 17:56 108
        if (*this == 1) return *this = u;
6c5f2c01 sago007 2017-03-15 17:56 109
6c5f2c01 sago007 2017-03-15 17:56 110
        uint64_t k = 0;
6c5f2c01 sago007 2017-03-15 17:56 111
        for (size_t i = 0; i < count_; i++) {
6c5f2c01 sago007 2017-03-15 17:56 112
            const uint64_t c = digits_[i] >> 32;
6c5f2c01 sago007 2017-03-15 17:56 113
            const uint64_t d = digits_[i] & 0xFFFFFFFF;
6c5f2c01 sago007 2017-03-15 17:56 114
            const uint64_t uc = u * c;
6c5f2c01 sago007 2017-03-15 17:56 115
            const uint64_t ud = u * d;
6c5f2c01 sago007 2017-03-15 17:56 116
            const uint64_t p0 = ud + k;
6c5f2c01 sago007 2017-03-15 17:56 117
            const uint64_t p1 = uc + (p0 >> 32);
6c5f2c01 sago007 2017-03-15 17:56 118
            digits_[i] = (p0 & 0xFFFFFFFF) | (p1 << 32);
6c5f2c01 sago007 2017-03-15 17:56 119
            k = p1 >> 32;
6c5f2c01 sago007 2017-03-15 17:56 120
        }
6c5f2c01 sago007 2017-03-15 17:56 121
        
6c5f2c01 sago007 2017-03-15 17:56 122
        if (k > 0)
6c5f2c01 sago007 2017-03-15 17:56 123
            PushBack(k);
6c5f2c01 sago007 2017-03-15 17:56 124
6c5f2c01 sago007 2017-03-15 17:56 125
        return *this;
6c5f2c01 sago007 2017-03-15 17:56 126
    }
6c5f2c01 sago007 2017-03-15 17:56 127
6c5f2c01 sago007 2017-03-15 17:56 128
    BigInteger& operator<<=(size_t shift) {
6c5f2c01 sago007 2017-03-15 17:56 129
        if (IsZero() || shift == 0) return *this;
6c5f2c01 sago007 2017-03-15 17:56 130
6c5f2c01 sago007 2017-03-15 17:56 131
        size_t offset = shift / kTypeBit;
6c5f2c01 sago007 2017-03-15 17:56 132
        size_t interShift = shift % kTypeBit;
6c5f2c01 sago007 2017-03-15 17:56 133
        CEREAL_RAPIDJSON_ASSERT(count_ + offset <= kCapacity);
6c5f2c01 sago007 2017-03-15 17:56 134
6c5f2c01 sago007 2017-03-15 17:56 135
        if (interShift == 0) {
6c5f2c01 sago007 2017-03-15 17:56 136
            std::memmove(&digits_[count_ - 1 + offset], &digits_[count_ - 1], count_ * sizeof(Type));
6c5f2c01 sago007 2017-03-15 17:56 137
            count_ += offset;
6c5f2c01 sago007 2017-03-15 17:56 138
        }
6c5f2c01 sago007 2017-03-15 17:56 139
        else {
6c5f2c01 sago007 2017-03-15 17:56 140
            digits_[count_] = 0;
6c5f2c01 sago007 2017-03-15 17:56 141
            for (size_t i = count_; i > 0; i--)
6c5f2c01 sago007 2017-03-15 17:56 142
                digits_[i + offset] = (digits_[i] << interShift) | (digits_[i - 1] >> (kTypeBit - interShift));
6c5f2c01 sago007 2017-03-15 17:56 143
            digits_[offset] = digits_[0] << interShift;
6c5f2c01 sago007 2017-03-15 17:56 144
            count_ += offset;
6c5f2c01 sago007 2017-03-15 17:56 145
            if (digits_[count_])
6c5f2c01 sago007 2017-03-15 17:56 146
                count_++;
6c5f2c01 sago007 2017-03-15 17:56 147
        }
6c5f2c01 sago007 2017-03-15 17:56 148
6c5f2c01 sago007 2017-03-15 17:56 149
        std::memset(digits_, 0, offset * sizeof(Type));
6c5f2c01 sago007 2017-03-15 17:56 150
6c5f2c01 sago007 2017-03-15 17:56 151
        return *this;
6c5f2c01 sago007 2017-03-15 17:56 152
    }
6c5f2c01 sago007 2017-03-15 17:56 153
6c5f2c01 sago007 2017-03-15 17:56 154
    bool operator==(const BigInteger& rhs) const {
6c5f2c01 sago007 2017-03-15 17:56 155
        return count_ == rhs.count_ && std::memcmp(digits_, rhs.digits_, count_ * sizeof(Type)) == 0;
6c5f2c01 sago007 2017-03-15 17:56 156
    }
6c5f2c01 sago007 2017-03-15 17:56 157
6c5f2c01 sago007 2017-03-15 17:56 158
    bool operator==(const Type rhs) const {
6c5f2c01 sago007 2017-03-15 17:56 159
        return count_ == 1 && digits_[0] == rhs;
6c5f2c01 sago007 2017-03-15 17:56 160
    }
6c5f2c01 sago007 2017-03-15 17:56 161
6c5f2c01 sago007 2017-03-15 17:56 162
    BigInteger& MultiplyPow5(unsigned exp) {
6c5f2c01 sago007 2017-03-15 17:56 163
        static const uint32_t kPow5[12] = {
6c5f2c01 sago007 2017-03-15 17:56 164
            5,
6c5f2c01 sago007 2017-03-15 17:56 165
            5 * 5,
6c5f2c01 sago007 2017-03-15 17:56 166
            5 * 5 * 5,
6c5f2c01 sago007 2017-03-15 17:56 167
            5 * 5 * 5 * 5,
6c5f2c01 sago007 2017-03-15 17:56 168
            5 * 5 * 5 * 5 * 5,
6c5f2c01 sago007 2017-03-15 17:56 169
            5 * 5 * 5 * 5 * 5 * 5,
6c5f2c01 sago007 2017-03-15 17:56 170
            5 * 5 * 5 * 5 * 5 * 5 * 5,
6c5f2c01 sago007 2017-03-15 17:56 171
            5 * 5 * 5 * 5 * 5 * 5 * 5 * 5,
6c5f2c01 sago007 2017-03-15 17:56 172
            5 * 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5,
6c5f2c01 sago007 2017-03-15 17:56 173
            5 * 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5,
6c5f2c01 sago007 2017-03-15 17:56 174
            5 * 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5,
6c5f2c01 sago007 2017-03-15 17:56 175
            5 * 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5
6c5f2c01 sago007 2017-03-15 17:56 176
        };
6c5f2c01 sago007 2017-03-15 17:56 177
        if (exp == 0) return *this;
6c5f2c01 sago007 2017-03-15 17:56 178
        for (; exp >= 27; exp -= 27) *this *= CEREAL_RAPIDJSON_UINT64_C2(0X6765C793, 0XFA10079D); // 5^27
6c5f2c01 sago007 2017-03-15 17:56 179
        for (; exp >= 13; exp -= 13) *this *= static_cast<uint32_t>(1220703125u); // 5^13
6c5f2c01 sago007 2017-03-15 17:56 180
        if (exp > 0)                 *this *= kPow5[exp - 1];
6c5f2c01 sago007 2017-03-15 17:56 181
        return *this;
6c5f2c01 sago007 2017-03-15 17:56 182
    }
6c5f2c01 sago007 2017-03-15 17:56 183
6c5f2c01 sago007 2017-03-15 17:56 184
    // Compute absolute difference of this and rhs.
6c5f2c01 sago007 2017-03-15 17:56 185
    // Assume this != rhs
6c5f2c01 sago007 2017-03-15 17:56 186
    bool Difference(const BigInteger& rhs, BigInteger* out) const {
6c5f2c01 sago007 2017-03-15 17:56 187
        int cmp = Compare(rhs);
6c5f2c01 sago007 2017-03-15 17:56 188
        CEREAL_RAPIDJSON_ASSERT(cmp != 0);
6c5f2c01 sago007 2017-03-15 17:56 189
        const BigInteger *a, *b;  // Makes a > b
6c5f2c01 sago007 2017-03-15 17:56 190
        bool ret;
6c5f2c01 sago007 2017-03-15 17:56 191
        if (cmp < 0) { a = &rhs; b = this; ret = true; }
6c5f2c01 sago007 2017-03-15 17:56 192
        else         { a = this; b = &rhs; ret = false; }
6c5f2c01 sago007 2017-03-15 17:56 193
6c5f2c01 sago007 2017-03-15 17:56 194
        Type borrow = 0;
6c5f2c01 sago007 2017-03-15 17:56 195
        for (size_t i = 0; i < a->count_; i++) {
6c5f2c01 sago007 2017-03-15 17:56 196
            Type d = a->digits_[i] - borrow;
6c5f2c01 sago007 2017-03-15 17:56 197
            if (i < b->count_)
6c5f2c01 sago007 2017-03-15 17:56 198
                d -= b->digits_[i];
6c5f2c01 sago007 2017-03-15 17:56 199
            borrow = (d > a->digits_[i]) ? 1 : 0;
6c5f2c01 sago007 2017-03-15 17:56 200
            out->digits_[i] = d;
6c5f2c01 sago007 2017-03-15 17:56 201
            if (d != 0)
6c5f2c01 sago007 2017-03-15 17:56 202
                out->count_ = i + 1;
6c5f2c01 sago007 2017-03-15 17:56 203
        }
6c5f2c01 sago007 2017-03-15 17:56 204
6c5f2c01 sago007 2017-03-15 17:56 205
        return ret;
6c5f2c01 sago007 2017-03-15 17:56 206
    }
6c5f2c01 sago007 2017-03-15 17:56 207
6c5f2c01 sago007 2017-03-15 17:56 208
    int Compare(const BigInteger& rhs) const {
6c5f2c01 sago007 2017-03-15 17:56 209
        if (count_ != rhs.count_)
6c5f2c01 sago007 2017-03-15 17:56 210
            return count_ < rhs.count_ ? -1 : 1;
6c5f2c01 sago007 2017-03-15 17:56 211
6c5f2c01 sago007 2017-03-15 17:56 212
        for (size_t i = count_; i-- > 0;)
6c5f2c01 sago007 2017-03-15 17:56 213
            if (digits_[i] != rhs.digits_[i])
6c5f2c01 sago007 2017-03-15 17:56 214
                return digits_[i] < rhs.digits_[i] ? -1 : 1;
6c5f2c01 sago007 2017-03-15 17:56 215
6c5f2c01 sago007 2017-03-15 17:56 216
        return 0;
6c5f2c01 sago007 2017-03-15 17:56 217
    }
6c5f2c01 sago007 2017-03-15 17:56 218
6c5f2c01 sago007 2017-03-15 17:56 219
    size_t GetCount() const { return count_; }
6c5f2c01 sago007 2017-03-15 17:56 220
    Type GetDigit(size_t index) const { CEREAL_RAPIDJSON_ASSERT(index < count_); return digits_[index]; }
6c5f2c01 sago007 2017-03-15 17:56 221
    bool IsZero() const { return count_ == 1 && digits_[0] == 0; }
6c5f2c01 sago007 2017-03-15 17:56 222
6c5f2c01 sago007 2017-03-15 17:56 223
private:
6c5f2c01 sago007 2017-03-15 17:56 224
    void AppendDecimal64(const char* begin, const char* end) {
6c5f2c01 sago007 2017-03-15 17:56 225
        uint64_t u = ParseUint64(begin, end);
6c5f2c01 sago007 2017-03-15 17:56 226
        if (IsZero())
6c5f2c01 sago007 2017-03-15 17:56 227
            *this = u;
6c5f2c01 sago007 2017-03-15 17:56 228
        else {
6c5f2c01 sago007 2017-03-15 17:56 229
            unsigned exp = static_cast<unsigned>(end - begin);
6c5f2c01 sago007 2017-03-15 17:56 230
            (MultiplyPow5(exp) <<= exp) += u;   // *this = *this * 10^exp + u
6c5f2c01 sago007 2017-03-15 17:56 231
        }
6c5f2c01 sago007 2017-03-15 17:56 232
    }
6c5f2c01 sago007 2017-03-15 17:56 233
6c5f2c01 sago007 2017-03-15 17:56 234
    void PushBack(Type digit) {
6c5f2c01 sago007 2017-03-15 17:56 235
        CEREAL_RAPIDJSON_ASSERT(count_ < kCapacity);
6c5f2c01 sago007 2017-03-15 17:56 236
        digits_[count_++] = digit;
6c5f2c01 sago007 2017-03-15 17:56 237
    }
6c5f2c01 sago007 2017-03-15 17:56 238
6c5f2c01 sago007 2017-03-15 17:56 239
    static uint64_t ParseUint64(const char* begin, const char* end) {
6c5f2c01 sago007 2017-03-15 17:56 240
        uint64_t r = 0;
6c5f2c01 sago007 2017-03-15 17:56 241
        for (const char* p = begin; p != end; ++p) {
6c5f2c01 sago007 2017-03-15 17:56 242
            CEREAL_RAPIDJSON_ASSERT(*p >= '0' && *p <= '9');
6c5f2c01 sago007 2017-03-15 17:56 243
            r = r * 10u + static_cast<unsigned>(*p - '0');
6c5f2c01 sago007 2017-03-15 17:56 244
        }
6c5f2c01 sago007 2017-03-15 17:56 245
        return r;
6c5f2c01 sago007 2017-03-15 17:56 246
    }
6c5f2c01 sago007 2017-03-15 17:56 247
6c5f2c01 sago007 2017-03-15 17:56 248
    // Assume a * b + k < 2^128
6c5f2c01 sago007 2017-03-15 17:56 249
    static uint64_t MulAdd64(uint64_t a, uint64_t b, uint64_t k, uint64_t* outHigh) {
6c5f2c01 sago007 2017-03-15 17:56 250
#if defined(_MSC_VER) && defined(_M_AMD64)
6c5f2c01 sago007 2017-03-15 17:56 251
        uint64_t low = _umul128(a, b, outHigh) + k;
6c5f2c01 sago007 2017-03-15 17:56 252
        if (low < k)
6c5f2c01 sago007 2017-03-15 17:56 253
            (*outHigh)++;
6c5f2c01 sago007 2017-03-15 17:56 254
        return low;
6c5f2c01 sago007 2017-03-15 17:56 255
#elif (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6)) && defined(__x86_64__)
6c5f2c01 sago007 2017-03-15 17:56 256
        __extension__ typedef unsigned __int128 uint128;
6c5f2c01 sago007 2017-03-15 17:56 257
        uint128 p = static_cast<uint128>(a) * static_cast<uint128>(b);
6c5f2c01 sago007 2017-03-15 17:56 258
        p += k;
6c5f2c01 sago007 2017-03-15 17:56 259
        *outHigh = static_cast<uint64_t>(p >> 64);
6c5f2c01 sago007 2017-03-15 17:56 260
        return static_cast<uint64_t>(p);
6c5f2c01 sago007 2017-03-15 17:56 261
#else
6c5f2c01 sago007 2017-03-15 17:56 262
        const uint64_t a0 = a & 0xFFFFFFFF, a1 = a >> 32, b0 = b & 0xFFFFFFFF, b1 = b >> 32;
6c5f2c01 sago007 2017-03-15 17:56 263
        uint64_t x0 = a0 * b0, x1 = a0 * b1, x2 = a1 * b0, x3 = a1 * b1;
6c5f2c01 sago007 2017-03-15 17:56 264
        x1 += (x0 >> 32); // can't give carry
6c5f2c01 sago007 2017-03-15 17:56 265
        x1 += x2;
6c5f2c01 sago007 2017-03-15 17:56 266
        if (x1 < x2)
6c5f2c01 sago007 2017-03-15 17:56 267
            x3 += (static_cast<uint64_t>(1) << 32);
6c5f2c01 sago007 2017-03-15 17:56 268
        uint64_t lo = (x1 << 32) + (x0 & 0xFFFFFFFF);
6c5f2c01 sago007 2017-03-15 17:56 269
        uint64_t hi = x3 + (x1 >> 32);
6c5f2c01 sago007 2017-03-15 17:56 270
6c5f2c01 sago007 2017-03-15 17:56 271
        lo += k;
6c5f2c01 sago007 2017-03-15 17:56 272
        if (lo < k)
6c5f2c01 sago007 2017-03-15 17:56 273
            hi++;
6c5f2c01 sago007 2017-03-15 17:56 274
        *outHigh = hi;
6c5f2c01 sago007 2017-03-15 17:56 275
        return lo;
6c5f2c01 sago007 2017-03-15 17:56 276
#endif
6c5f2c01 sago007 2017-03-15 17:56 277
    }
6c5f2c01 sago007 2017-03-15 17:56 278
6c5f2c01 sago007 2017-03-15 17:56 279
    static const size_t kBitCount = 3328;  // 64bit * 54 > 10^1000
6c5f2c01 sago007 2017-03-15 17:56 280
    static const size_t kCapacity = kBitCount / sizeof(Type);
6c5f2c01 sago007 2017-03-15 17:56 281
    static const size_t kTypeBit = sizeof(Type) * 8;
6c5f2c01 sago007 2017-03-15 17:56 282
6c5f2c01 sago007 2017-03-15 17:56 283
    Type digits_[kCapacity];
6c5f2c01 sago007 2017-03-15 17:56 284
    size_t count_;
6c5f2c01 sago007 2017-03-15 17:56 285
};
6c5f2c01 sago007 2017-03-15 17:56 286
6c5f2c01 sago007 2017-03-15 17:56 287
} // namespace internal
6c5f2c01 sago007 2017-03-15 17:56 288
CEREAL_RAPIDJSON_NAMESPACE_END
6c5f2c01 sago007 2017-03-15 17:56 289
6c5f2c01 sago007 2017-03-15 17:56 290
#endif // CEREAL_RAPIDJSON_BIGINTEGER_H_
1970-01-01 00:00 291