/japanese/canna-lib/files/

repository'/>
aboutsummaryrefslogtreecommitdiffstats
path: root/vendor/github.com/tangerine-network/mcl/include/mcl/conversion.hpp
blob: 7a04b7fa21bbe578c4ce8d2d655e73567cb67ae9 (plain) (blame)
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
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
#pragma once
#include <cybozu/itoa.hpp>
#include <cybozu/stream.hpp>
/**
    @file
    @brief convertion bin/dec/hex <=> array
    @author MITSUNARI Shigeo(@herumi)
    @license modified new BSD license
    http://opensource.org/licenses/BSD-3-Clause
*/
#ifdef _MSC_VER
    #pragma warning(push)
    #pragma warning(disable : 4127)
#endif

namespace mcl { namespace fp {

namespace local {

inline bool isSpace(char c)
{
    return c == ' ' || c == '\t' || c == '\r' || c == '\n';
}
template<class InputStream>
bool skipSpace(char *c, InputStream& is)
{
    for (;;) {
        if (!cybozu::readChar(c,  is)) return false;
        if (!isSpace(*c)) return true;
    }
}

#ifndef CYBOZU_DONT_USE_STRING
template<class InputStream>
void loadWord(std::string& s, InputStream& is)
{
    s.clear();
    char c;
    if (!skipSpace(&c, is)) return;
    s = c;
    for (;;) {
        if (!cybozu::readChar(&c,  is)) return;
        if (isSpace(c)) break;
        s += c;
    }
}
#endif

template<class InputStream>
size_t loadWord(char *buf, size_t bufSize, InputStream& is)
{
    if (bufSize == 0) return 0;
    char c;
    if (!skipSpace(&c, is)) return 0;
    size_t pos = 0;
    buf[pos++] = c;
    for (;;) {
        if (!cybozu::readChar(&c, is)) break;
        if (isSpace(c)) break;
        if (pos == bufSize) return 0;
        buf[pos++] = c;
    }
    return pos;
}


/*
    q = x[] / x
    @retval r = x[] % x
    @note accept q == x
*/
inline uint32_t divU32(uint32_t *q, const uint32_t *x, size_t xn, uint32_t y)
{
    if (xn == 0) return 0;
    uint32_t r = 0;
    for (int i = (int)xn - 1; i >= 0; i--) {
        uint64_t t = (uint64_t(r) << 32) | x[i];
        q[i] = uint32_t(t / y);
        r = uint32_t(t % y);
    }
    return r;
}

/*
    z[0, xn) = x[0, xn) * y
    return z[xn]
    @note accept z == x
*/
inline uint32_t mulU32(uint32_t *z, const uint32_t *x, size_t xn, uint32_t y)
{
    uint32_t H = 0;
    for (size_t i = 0; i < xn; i++) {
        uint32_t t = H;
        uint64_t v = uint64_t(x[i]) * y;
        uint32_t L = uint32_t(v);
        H = uint32_t(v >> 32);
        z[i] = t + L;
        if (z[i] < t) {
            H++;
        }
    }
    return H;
}

/*
    x[0, xn) += y
    return 1 if overflow else 0
*/
inline uint32_t addU32(uint32_t *x, size_t xn, uint32_t y)
{
    uint32_t t = x[0] + y;
    x[0] = t;
    if (t >= y) return 0;
    for (size_t i = 1; i < xn; i++) {
        t = x[i] + 1;
        x[i] = t;
        if (t != 0) return 0;
    }
    return 1;
}

inline uint32_t decToU32(const char *p, size_t size, bool *pb)
{
    assert(0 < size && size <= 9);
    uint32_t x = 0;
    for (size_t i = 0; i < size; i++) {
        char c = p[i];
        if (c < '0' || c > '9') {
            *pb = false;
            return 0;
        }
        x = x * 10 + uint32_t(c - '0');
    }
    *pb = true;
    return x;
}

inline bool hexCharToUint8(uint8_t *v, char _c)
{
    uint32_t c = uint8_t(_c); // cast is necessary
    if (c - '0' <= '9' - '0') {
        c = c - '0';
    } else if (c - 'a' <= 'f' - 'a') {
        c = (c - 'a') + 10;
    } else if (c - 'A' <= 'F' - 'A') {
        c = (c - 'A') + 10;
    } else {
        return false;
    }
    *v = uint8_t(c);
    return true;
}

template<class UT>
bool hexToUint(UT *px, const char *p, size_t size)
{
    assert(0 < size && size <= sizeof(UT) * 2);
    UT x = 0;
    for (size_t i = 0; i < size; i++) {
        uint8_t v;
        if (!hexCharToUint8(&v, p[i])) return false;
        x = x * 16 + v;
    }
    *px = x;
    return true;
}

template<class UT>
bool binToUint(UT *px, const char *p, size_t size)
{
    assert(0 < size && size <= sizeof(UT) * 8);
    UT x = 0;
    for (size_t i = 0; i < size; i++) {
        UT c = static_cast<uint8_t>(p[i]);
        if (c == '0') {
            x = x * 2;
        } else if (c == '1') {
            x = x * 2 + 1;
        } else {
            return false;
        }
    }
    *px = x;
    return true;
}

inline bool parsePrefix(size_t *readSize, bool *isMinus, int *base, const char *buf, size_t bufSize)
{
    if (bufSize == 0) return false;
    size_t pos = 0;
    if (*buf == '-') {
        if (bufSize == 1) return false;
        *isMinus = true;
        buf++;
        pos++;
    } else {
        *isMinus = false;
    }
    if (buf[0] == '0') {
        if (bufSize > 1 && buf[1] == 'x') {
            if (*base == 0 || *base == 16) {
                *base = 16;
                pos += 2;
            } else {
                return false;
            }
        } else if (bufSize > 1 && buf[1] == 'b') {
            if (*base == 0 || *base == 2) {
                *base = 2;
                pos += 2;
            } else {
                return false;
            }
        }
    }
    if (*base == 0) *base = 10;
    if (pos == bufSize) return false;
    *readSize = pos;
    return true;
}

} // mcl::fp::local

/*
    convert little endian x[0, xn) to buf
    return written size if success else 0
    data is buf[bufSize - retval, bufSize)
    start "0x" if withPrefix
*/
template<class T>
size_t arrayToHex(char *buf, size_t bufSize, const T *x, size_t n, bool withPrefix = false)
{
    size_t fullN = 0;
    if (n > 1) {
        size_t pos = n - 1;
        while (pos > 0) {
            if (x[pos]) break;
            pos--;
        }
        if (pos > 0) fullN = pos;
    }
    const T v = n == 0 ? 0 : x[fullN];
    const size_t topLen = cybozu::getHexLength(v);
    const size_t startPos = withPrefix ? 2 : 0;
    const size_t lenT = sizeof(T) * 2;
    const size_t totalSize = startPos + fullN * lenT + topLen;
    if (totalSize > bufSize) return 0;
    char *const top = buf + bufSize - totalSize;
    if (withPrefix) {
        top[0] = '0';
        top[1] = 'x';
    }
    cybozu::itohex(&top[startPos], topLen, v, false);
    for (size_t i = 0; i < fullN; i++) {
        cybozu::itohex(&top[startPos + topLen + i * lenT], lenT, x[fullN - 1 - i], false);
    }
    return totalSize;
}

/*
    convert little endian x[0, xn) to buf
    return written size if success else 0
    data is buf[bufSize - retval, bufSize)
    start "0b" if withPrefix
*/
template<class T>
size_t arrayToBin(char *buf, size_t bufSize, const T *x, size_t n, bool withPrefix)
{
    size_t fullN = 0;
    if (n > 1) {
        size_t pos = n - 1;
        while (pos > 0) {
            if (x[pos]) break;
            pos--;
        }
        if (pos > 0) fullN = pos;
    }
    const T v = n == 0 ? 0 : x[fullN];
    const size_t topLen = cybozu::getBinLength(v);
    const size_t startPos = withPrefix ? 2 : 0;
    const size_t lenT = sizeof(T) * 8;
    const size_t totalSize = startPos + fullN * lenT + topLen;
    if (totalSize > bufSize) return 0;
    char *const top = buf + bufSize - totalSize;
    if (withPrefix) {
        top[0] = '0';
        top[1] = 'b';
    }
    cybozu::itobin(&top[startPos], topLen, v);
    for (size_t i = 0; i < fullN; i++) {
        cybozu::itobin(&top[startPos + topLen + i * lenT], lenT, x[fullN - 1 - i]);
    }
    return totalSize;
}

/*
    convert hex string to x[0..xn)
    hex string = [0-9a-fA-F]+
*/
template<class UT>
inline size_t hexToArray(UT *x, size_t maxN, const char *buf, size_t bufSize)
{
    if (bufSize == 0) return 0;
    const size_t unitLen = sizeof(UT) * 2;
    const size_t q = bufSize / unitLen;
    const size_t r = bufSize % unitLen;
    const size_t requireSize = q + (r ? 1 : 0);
    if (maxN < requireSize) return 0;
    for (size_t i = 0; i < q; i++) {
        if (!local::hexToUint(&x[i], &buf[r + (q - 1 - i) * unitLen], unitLen)) return 0;
    }
    if (r) {
        if (!local::hexToUint(&x[q], buf, r)) return 0;
    }
    return requireSize;
}
/*
    convert bin string to x[0..xn)
    bin string = [01]+
*/
template<class UT>
inline size_t binToArray(UT *x, size_t maxN, const char *buf, size_t bufSize)
{
    if (bufSize == 0) return 0;
    const size_t unitLen = sizeof(UT) * 8;
    const size_t q = bufSize / unitLen;
    const size_t r = bufSize % unitLen;
    const size_t requireSize = q + (r ? 1 : 0);
    if (maxN < requireSize) return 0;
    for (size_t i = 0; i < q; i++) {
        if (!local::binToUint(&x[i], &buf[r + (q - 1 - i) * unitLen], unitLen)) return 0;
    }
    if (r) {
        if (!local::binToUint(&x[q], buf, r)) return 0;
    }
    return requireSize;
}

/*
    little endian x[0, xn) to buf
    return written size if success else 0
    data is buf[bufSize - retval, bufSize)
*/
template<class UT>
inline size_t arrayToDec(char *buf, size_t bufSize, const UT *x, size_t xn)
{
    const size_t maxN = 64;
    uint32_t t[maxN];
    if (sizeof(UT) == 8) {
        xn *= 2;
    }
    if (xn > maxN) return 0;
    memcpy(t, x, xn * sizeof(t[0]));

    const size_t width = 9;
    const uint32_t i1e9 = 1000000000U;
    size_t pos = 0;
    for (;;) {
        uint32_t r = local::divU32(t, t, xn, i1e9);
        while (xn > 0 && t[xn - 1] == 0) xn--;
        size_t len = cybozu::itoa_local::uintToDec(buf, bufSize - pos, r);
        if (len == 0) return 0;
        assert(0 < len && len <= width);
        if (xn == 0) return pos + len;
        // fill (width - len) '0'
        for (size_t j = 0; j < width - len; j++) {
            buf[bufSize - pos - width + j] = '0';
        }
        pos += width;
    }
}

/*
    convert buf[0, bufSize) to x[0, num)
    return written num if success else 0
*/
template<class UT>
inline size_t decToArray(UT *_x, size_t maxN, const char *buf, size_t bufSize)
{
    assert(sizeof(UT) == 4 || sizeof(UT) == 8);
    const size_t width = 9;
    const uint32_t i1e9 = 1000000000U;
    if (maxN == 0) return 0;
    if (sizeof(UT) == 8) {
        maxN *= 2;
    }
    uint32_t *x = reinterpret_cast<uint32_t*>(_x);
    size_t xn = 1;
    x[0] = 0;
    while (bufSize > 0) {
        size_t n = bufSize % width;
        if (n == 0) n = width;
        bool b;
        uint32_t v = local::decToU32(buf, n, &b);
        if (!b) return 0;
        uint32_t H = local::mulU32(x, x, xn, i1e9);
        if (H > 0) {
            if (xn == maxN) return 0;
            x[xn++] = H;
        }
        H = local::addU32(x, xn, v);
        if (H > 0) {
            if (xn == maxN) return 0;
            x[xn++] = H;
        }
        buf += n;
        bufSize -= n;
    }
    if (sizeof(UT) == 8 && (xn & 1)) {
        x[xn++] = 0;
    }
    return xn / (sizeof(UT) / 4);
}

/*
    return retavl is written size if success else 0
    REMARK : the top of string is buf + bufSize - retval
*/
template<class UT>
size_t arrayToStr(char *buf, size_t bufSize, const UT *x, size_t n, int base, bool withPrefix)
{
    switch (base) {
    case 0:
    case 10:
        return arrayToDec(buf, bufSize, x, n);