1 | // gc_mops.h - corresponds to mycpp/mops.py
|
2 |
|
3 | #ifndef MYCPP_GC_MOPS_H
|
4 | #define MYCPP_GC_MOPS_H
|
5 |
|
6 | #include <stdint.h>
|
7 |
|
8 | #include "mycpp/common.h" // DCHECK
|
9 | #include "mycpp/gc_tuple.h"
|
10 |
|
11 | class BigStr;
|
12 |
|
13 | namespace mops {
|
14 |
|
15 | // BigInt library
|
16 | // TODO: Make it arbitrary size. Right now it's int64_t, which is distinct
|
17 | // from int.
|
18 |
|
19 | typedef int64_t BigInt;
|
20 |
|
21 | // For convenience
|
22 | extern const BigInt ZERO;
|
23 | extern const BigInt ONE;
|
24 | extern const BigInt MINUS_ONE;
|
25 | extern const BigInt MINUS_TWO;
|
26 |
|
27 | BigStr* ToStr(BigInt b);
|
28 | BigStr* ToOctal(BigInt b);
|
29 | BigStr* ToHexUpper(BigInt b);
|
30 | BigStr* ToHexLower(BigInt b);
|
31 |
|
32 | BigInt FromStr(BigStr* s, int base = 10);
|
33 | Tuple2<bool, BigInt> FromFloat(double f);
|
34 |
|
35 | inline int BigTruncate(BigInt b) {
|
36 | return static_cast<int>(b);
|
37 | }
|
38 |
|
39 | inline BigInt IntWiden(int b) {
|
40 | return static_cast<BigInt>(b);
|
41 | }
|
42 |
|
43 | inline BigInt FromC(int64_t i) {
|
44 | return i;
|
45 | }
|
46 |
|
47 | inline BigInt FromBool(bool b) {
|
48 | return b ? BigInt(1) : BigInt(0);
|
49 | }
|
50 |
|
51 | inline double ToFloat(BigInt b) {
|
52 | return static_cast<double>(b);
|
53 | }
|
54 |
|
55 | inline BigInt Negate(BigInt b) {
|
56 | return -b;
|
57 | }
|
58 |
|
59 | inline BigInt Add(BigInt a, BigInt b) {
|
60 | return a + b;
|
61 | }
|
62 |
|
63 | inline BigInt Sub(BigInt a, BigInt b) {
|
64 | return a - b;
|
65 | }
|
66 |
|
67 | inline BigInt Mul(BigInt a, BigInt b) {
|
68 | return a * b;
|
69 | }
|
70 |
|
71 | inline BigInt Div(BigInt a, BigInt b) {
|
72 | // Same check as in mops.py
|
73 | DCHECK(b != 0); // divisor can't be zero
|
74 | return a / b;
|
75 | }
|
76 |
|
77 | inline BigInt Rem(BigInt a, BigInt b) {
|
78 | // Same check as in mops.py
|
79 | DCHECK(b != 0); // divisor can't be zero
|
80 | return a % b;
|
81 | }
|
82 |
|
83 | inline bool Equal(BigInt a, BigInt b) {
|
84 | return a == b;
|
85 | }
|
86 |
|
87 | inline bool Greater(BigInt a, BigInt b) {
|
88 | return a > b;
|
89 | }
|
90 |
|
91 | inline BigInt LShift(BigInt a, BigInt b) {
|
92 | DCHECK(b >= 0);
|
93 | return a << b;
|
94 | }
|
95 |
|
96 | inline BigInt RShift(BigInt a, BigInt b) {
|
97 | DCHECK(b >= 0);
|
98 | return a >> b;
|
99 | }
|
100 |
|
101 | inline BigInt BitAnd(BigInt a, BigInt b) {
|
102 | return a & b;
|
103 | }
|
104 |
|
105 | inline BigInt BitOr(BigInt a, BigInt b) {
|
106 | return a | b;
|
107 | }
|
108 |
|
109 | inline BigInt BitXor(BigInt a, BigInt b) {
|
110 | return a ^ b;
|
111 | }
|
112 |
|
113 | inline BigInt BitNot(BigInt a) {
|
114 | return ~a;
|
115 | }
|
116 |
|
117 | } // namespace mops
|
118 |
|
119 | #endif // MYCPP_GC_MOPS_H
|