| 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 |
|
| 10 | class BigStr;
|
| 11 |
|
| 12 | namespace mops {
|
| 13 |
|
| 14 | // BigInt library
|
| 15 | // TODO: Make it arbitrary size. Right now it's int64_t, which is distinct
|
| 16 | // from int.
|
| 17 |
|
| 18 | typedef int64_t BigInt;
|
| 19 |
|
| 20 | // For convenience
|
| 21 | extern const BigInt ZERO;
|
| 22 | extern const BigInt ONE;
|
| 23 | extern const BigInt MINUS_ONE;
|
| 24 | extern const BigInt MINUS_TWO;
|
| 25 |
|
| 26 | BigStr* ToStr(BigInt b);
|
| 27 | BigStr* ToOctal(BigInt b);
|
| 28 | BigStr* ToHexUpper(BigInt b);
|
| 29 | BigStr* ToHexLower(BigInt b);
|
| 30 |
|
| 31 | BigInt FromStr(BigStr* s, int base = 10);
|
| 32 |
|
| 33 | inline int BigTruncate(BigInt b) {
|
| 34 | return static_cast<int>(b);
|
| 35 | }
|
| 36 |
|
| 37 | inline BigInt IntWiden(int b) {
|
| 38 | return static_cast<BigInt>(b);
|
| 39 | }
|
| 40 |
|
| 41 | inline BigInt FromC(int64_t i) {
|
| 42 | return i;
|
| 43 | }
|
| 44 |
|
| 45 | inline BigInt FromBool(bool b) {
|
| 46 | return b ? BigInt(1) : BigInt(0);
|
| 47 | }
|
| 48 |
|
| 49 | inline float ToFloat(BigInt b) {
|
| 50 | // TODO: test this
|
| 51 | return static_cast<float>(b);
|
| 52 | }
|
| 53 |
|
| 54 | inline BigInt FromFloat(float f) {
|
| 55 | // TODO: test this
|
| 56 | return static_cast<BigInt>(f);
|
| 57 | }
|
| 58 |
|
| 59 | inline BigInt Negate(BigInt b) {
|
| 60 | return -b;
|
| 61 | }
|
| 62 |
|
| 63 | inline BigInt Add(BigInt a, BigInt b) {
|
| 64 | return a + b;
|
| 65 | }
|
| 66 |
|
| 67 | inline BigInt Sub(BigInt a, BigInt b) {
|
| 68 | return a - b;
|
| 69 | }
|
| 70 |
|
| 71 | inline BigInt Mul(BigInt a, BigInt b) {
|
| 72 | return a * b;
|
| 73 | }
|
| 74 |
|
| 75 | inline BigInt Div(BigInt a, BigInt b) {
|
| 76 | // Is the behavior of negative values defined in C++? Avoid difference with
|
| 77 | // Python.
|
| 78 | DCHECK(a >= 0);
|
| 79 | DCHECK(b >= 0);
|
| 80 | return a / b;
|
| 81 | }
|
| 82 |
|
| 83 | inline BigInt Rem(BigInt a, BigInt b) {
|
| 84 | // Is the behavior of negative values defined in C++? Avoid difference with
|
| 85 | // Python.
|
| 86 | DCHECK(a >= 0);
|
| 87 | DCHECK(b >= 0);
|
| 88 | return a % b;
|
| 89 | }
|
| 90 |
|
| 91 | inline bool Equal(BigInt a, BigInt b) {
|
| 92 | return a == b;
|
| 93 | }
|
| 94 |
|
| 95 | inline bool Greater(BigInt a, BigInt b) {
|
| 96 | return a > b;
|
| 97 | }
|
| 98 |
|
| 99 | inline BigInt LShift(BigInt a, BigInt b) {
|
| 100 | return a << b;
|
| 101 | }
|
| 102 |
|
| 103 | inline BigInt RShift(BigInt a, BigInt b) {
|
| 104 | return a >> b;
|
| 105 | }
|
| 106 |
|
| 107 | inline BigInt BitAnd(BigInt a, BigInt b) {
|
| 108 | return a & b;
|
| 109 | }
|
| 110 |
|
| 111 | inline BigInt BitOr(BigInt a, BigInt b) {
|
| 112 | return a | b;
|
| 113 | }
|
| 114 |
|
| 115 | inline BigInt BitXor(BigInt a, BigInt b) {
|
| 116 | return a ^ b;
|
| 117 | }
|
| 118 |
|
| 119 | inline BigInt BitNot(BigInt a) {
|
| 120 | return ~a;
|
| 121 | }
|
| 122 |
|
| 123 | } // namespace mops
|
| 124 |
|
| 125 | #endif // MYCPP_GC_MOPS_H
|