summaryrefslogtreecommitdiff
path: root/number.cpp
blob: 02d655d7a453ccf815cbd9a9e49f515a4776e9f8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
#include <iomanip>
#include <sstream>
#include "number.h"

number::number(std::uint64_t n)
{
    if (n == 0) {
        return;
    }

    _operands.push_back(n & UINT32_MAX);

    if (n > UINT32_MAX) {
        _operands.push_back(n >> 32);
    }
}

/* Type Conversion {{{ */

std::string
number::to_hex_string() const
{
    if (_operands.empty()) {
        return std::string("0x0");
    }

    std::ostringstream ss;

    ss << "0x" << std::hex;

    for (auto it = _operands.crbegin(); it != _operands.crend(); ++it) {
        ss << *it;

        /* If more operands are present, successive ones must be padded. */
        ss << std::setfill('0');
        ss << std::setw(8);
    }

    return ss.str();
}

std::string
number::to_dec_string() const
{
    if (_operands.empty()) {
        return std::string("0");
    }

    std::ostringstream ss;

    ss << std::dec;

    if (_operands.size() == 1) {
        ss << _operands.front();
    } else {
        /* TODO */
        ss << "[Not implemented]";
    }

    return ss.str();
}

std::uint32_t
number::to_uint32() const
{
    auto size = _operands.size();

    if (size > 1) {
        throw std::out_of_range("> UINT32_MAX");
    }

    return (size == 0) ? 0 : _operands.front();
}

std::uint64_t
number::to_uint64() const
{
    auto size = _operands.size();

    if (size > 2) {
        throw std::out_of_range("> UINT64_MAX");
    }

    std::uint64_t result = 0;

    for (auto it = _operands.crbegin(); it != _operands.crend(); ++it) {
        result <<= 32;

        result |= (*it);
    }

    return result;
}

number::operator bool() const
{
    return *this != 0;
}

/* }}} */
/* Operations {{{ */

number
number::operator+(const number &n) const
{
    number result;

    auto it = _operands.cbegin();
    auto it_n = n._operands.cbegin();

    bool carry = false;

    while (it != _operands.cend() || it_n != n._operands.cend()) {
        const auto n1 = (it != _operands.cend()) ? *it++ : 0;
        const auto n2 = (it_n != n._operands.cend()) ? *it_n++ : 0;

        result._operands.push_back(n1 + n2 + (carry ? 1 : 0));

        if (carry && (n1 == UINT32_MAX)) {
            carry = true;
        } else {
            carry = ((UINT32_MAX - n1 - (carry ? 1 : 0)) < n2);
        }
    }

    if (carry) {
        result._operands.push_back(1);
    }

    return result;
}

number
number::operator*(const number &n) const
{
    number result;

    if (n < UINT32_MAX && *this < UINT32_MAX) {
        return number(1ull * to_uint32() * n.to_uint32());
    }

    int level = 0;

    for (auto it_low: _operands) {
        number intermediate;

        std::uint32_t carry = 0;

        for (auto it_high: n._operands) {
            std::uint64_t tmp = 1ull * it_low * it_high + carry;

            intermediate._operands.push_back(tmp & UINT32_MAX);

            carry = tmp >> 32;
        }

        if (carry) {
            intermediate._operands.push_back(carry);
        }

        for (auto j = 0; j < level; ++j) {
            intermediate <<= 32;
        }

        result += intermediate;

        ++level;
    }

    return result;
}

number
number::operator<<(std::uint32_t shift) const
{
    if (*this == 0) {
        return 0;
    }

    number result(*this);

    /*
     * Since, internally, we store 32-bits-wide integers, we can just insert
     * "shift / 32" new integers at the beginning.
     */
    for (unsigned int i = 0; i < shift / 32; ++i) {
        result._operands.push_front(0);
    }

    shift %= 32;

    std::uint32_t carry = 0;

    for (auto &operand: result._operands) {
        std::uint32_t tmp = ((1ull * operand) << shift) + carry;

        carry = ((1ull * operand) >> (32 - shift));

        operand = tmp;
    }

    if (carry) {
        result._operands.push_back(carry);
    }

    return result;
}

number
number::operator>>(std::uint32_t shift) const
{
    number result(*this);

    /*
     * Since, internally, we store 32-bits-wide integers, we can just get rid
     * of "shift / 32" first integers.
     */
    for (unsigned int i = 0; i < shift / 32; ++i) {
        if (result == 0) {
            return 0;
        }

        result._operands.pop_front();
    }

    shift %= 32;

    std::uint32_t carry = 0;

    for (auto it = result._operands.rbegin(); it != result._operands.rend(); ++it) {
        auto &operand = *it;

        std::uint32_t tmp = ((1ull * operand) >> shift) | carry;

        carry = ((1ull * operand) << (32 - shift));

        operand = tmp;
    }

    result.shrink_to_fit();

    return result;
}

/* }}} */
/* Comparison operators {{{ */

bool
number::operator<(const number &n) const
{
    const auto size = this->_operands.size();

    if (size < n._operands.size()) {
        return true;
    }

    if (size > n._operands.size()) {
        return false;
    }

    /* Because first item is the least significant. */
    auto l1(this->_operands);
    auto l2(n._operands);

    l1.reverse();
    l2.reverse();

    return l1 < l2;
}

bool
number::operator>(const number &n) const
{
    return n.operator<(*this);
}

bool
number::operator<=(const number &n) const
{
    return operator<(n) || operator==(n);
}

bool
number::operator>=(const number &n) const
{
    return operator>(n) || operator==(n);
}

bool
number::operator==(const number &n) const
{
    return _operands == n._operands;
}

bool
number::operator!=(const number &n) const
{
    return _operands != n._operands;
}

bool
number::operator!() const
{
    return !static_cast<bool>(*this);
}

/* }}} */
/* Assignment operators {{{ */

number &
number::operator+=(const number &n)
{
    *this = *this + n;

    return *this;
}

number &
number::operator*=(const number &n)
{
    *this = *this * n;

    return *this;
}

number &
number::operator<<=(std::uint32_t n)
{
    *this = *this << n;

    return *this;
}

number &
number::operator>>=(std::uint32_t n)
{
    *this = *this >> n;

    return *this;
}

number &
number::operator++()
{
    *this = *this + 1;

    return *this;
}

/* }}} */

void
number::shrink_to_fit()
{
    while (_operands.back() == 0) {
        _operands.pop_back();
    }
}

std::ostream &
operator<<(std::ostream &os, const number &n)
{
    return os << n.to_hex_string();
}