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

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