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

120 lines, 57 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
10class BigStr;
11
12namespace mops {
13
14// BigInt library
15// TODO: Make it arbitrary size. Right now it's int64_t, which is distinct
16// from int.
17
18typedef int64_t BigInt;
19
20// For convenience
21extern const BigInt ZERO;
22extern const BigInt ONE;
23extern const BigInt MINUS_ONE;
24
25BigStr* ToStr(BigInt b);
26BigInt FromStr(BigStr* s, int base = 10);
27
28inline int BigTruncate(BigInt b) {
29 return static_cast<int>(b);
30}
31
32inline BigInt IntWiden(int b) {
33 return static_cast<BigInt>(b);
34}
35
36inline BigInt FromC(int64_t i) {
37 return i;
38}
39
40inline BigInt FromBool(bool b) {
41 return b ? BigInt(1) : BigInt(0);
42}
43
44inline float ToFloat(BigInt b) {
45 // TODO: test this
46 return static_cast<float>(b);
47}
48
49inline BigInt FromFloat(float f) {
50 // TODO: test this
51 return static_cast<BigInt>(f);
52}
53
54inline BigInt Negate(BigInt b) {
55 return -b;
56}
57
58inline BigInt Add(BigInt a, BigInt b) {
59 return a + b;
60}
61
62inline BigInt Sub(BigInt a, BigInt b) {
63 return a - b;
64}
65
66inline BigInt Mul(BigInt a, BigInt b) {
67 return a * b;
68}
69
70inline 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
78inline 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
86inline bool Equal(BigInt a, BigInt b) {
87 return a == b;
88}
89
90inline bool Greater(BigInt a, BigInt b) {
91 return a > b;
92}
93
94inline BigInt LShift(BigInt a, BigInt b) {
95 return a << b;
96}
97
98inline BigInt RShift(BigInt a, BigInt b) {
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