OILS / mycpp / gc_mops.h View on Github | oilshell.org

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