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