| 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 double ToFloat(BigInt b) {
 | 
| 50 |   return static_cast<double>(b);
 | 
| 51 | }
 | 
| 52 | 
 | 
| 53 | inline BigInt FromFloat(double f) {
 | 
| 54 |   return static_cast<BigInt>(f);
 | 
| 55 | }
 | 
| 56 | 
 | 
| 57 | inline BigInt Negate(BigInt b) {
 | 
| 58 |   return -b;
 | 
| 59 | }
 | 
| 60 | 
 | 
| 61 | inline BigInt Add(BigInt a, BigInt b) {
 | 
| 62 |   return a + b;
 | 
| 63 | }
 | 
| 64 | 
 | 
| 65 | inline BigInt Sub(BigInt a, BigInt b) {
 | 
| 66 |   return a - b;
 | 
| 67 | }
 | 
| 68 | 
 | 
| 69 | inline BigInt Mul(BigInt a, BigInt b) {
 | 
| 70 |   return a * b;
 | 
| 71 | }
 | 
| 72 | 
 | 
| 73 | inline BigInt Div(BigInt a, BigInt b) {
 | 
| 74 |   // Is the behavior of negative values defined in C++?  Avoid difference with
 | 
| 75 |   // Python.
 | 
| 76 |   DCHECK(a >= 0);
 | 
| 77 |   DCHECK(b >= 0);
 | 
| 78 |   return a / b;
 | 
| 79 | }
 | 
| 80 | 
 | 
| 81 | inline BigInt Rem(BigInt a, BigInt b) {
 | 
| 82 |   // Is the behavior of negative values defined in C++?  Avoid difference with
 | 
| 83 |   // Python.
 | 
| 84 |   DCHECK(a >= 0);
 | 
| 85 |   DCHECK(b >= 0);
 | 
| 86 |   return a % b;
 | 
| 87 | }
 | 
| 88 | 
 | 
| 89 | inline bool Equal(BigInt a, BigInt b) {
 | 
| 90 |   return a == b;
 | 
| 91 | }
 | 
| 92 | 
 | 
| 93 | inline bool Greater(BigInt a, BigInt b) {
 | 
| 94 |   return a > b;
 | 
| 95 | }
 | 
| 96 | 
 | 
| 97 | inline BigInt LShift(BigInt a, BigInt b) {
 | 
| 98 |   return a << b;
 | 
| 99 | }
 | 
| 100 | 
 | 
| 101 | inline BigInt RShift(BigInt a, BigInt b) {
 | 
| 102 |   return a >> b;
 | 
| 103 | }
 | 
| 104 | 
 | 
| 105 | inline BigInt BitAnd(BigInt a, BigInt b) {
 | 
| 106 |   return a & b;
 | 
| 107 | }
 | 
| 108 | 
 | 
| 109 | inline BigInt BitOr(BigInt a, BigInt b) {
 | 
| 110 |   return a | b;
 | 
| 111 | }
 | 
| 112 | 
 | 
| 113 | inline BigInt BitXor(BigInt a, BigInt b) {
 | 
| 114 |   return a ^ b;
 | 
| 115 | }
 | 
| 116 | 
 | 
| 117 | inline BigInt BitNot(BigInt a) {
 | 
| 118 |   return ~a;
 | 
| 119 | }
 | 
| 120 | 
 | 
| 121 | }  // namespace mops
 | 
| 122 | 
 | 
| 123 | #endif  // MYCPP_GC_MOPS_H
 |