examples

Coverage Report

Created: 2025-04-27 05:36

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