mycpp

Coverage Report

Created: 2024-06-09 06:28

/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
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
1
inline int BigTruncate(BigInt b) {
29
1
  return static_cast<int>(b);
30
1
}
31
32
0
inline BigInt IntWiden(int b) {
33
0
  return static_cast<BigInt>(b);
34
0
}
35
36
0
inline BigInt FromC(int64_t i) {
37
0
  return i;
38
0
}
39
40
0
inline BigInt FromBool(bool b) {
41
0
  return b ? BigInt(1) : BigInt(0);
42
0
}
43
44
1
inline float ToFloat(BigInt b) {
45
  // TODO: test this
46
1
  return static_cast<float>(b);
47
1
}
48
49
2
inline BigInt FromFloat(float f) {
50
  // TODO: test this
51
2
  return static_cast<BigInt>(f);
52
2
}
53
54
0
inline BigInt Negate(BigInt b) {
55
0
  return -b;
56
0
}
57
58
0
inline BigInt Add(BigInt a, BigInt b) {
59
0
  return a + b;
60
0
}
61
62
0
inline BigInt Sub(BigInt a, BigInt b) {
63
0
  return a - b;
64
0
}
65
66
0
inline BigInt Mul(BigInt a, BigInt b) {
67
0
  return a * b;
68
0
}
69
70
0
inline BigInt Div(BigInt a, BigInt b) {
71
0
  // Is the behavior of negative values defined in C++?  Avoid difference with
72
0
  // Python.
73
0
  DCHECK(a >= 0);
74
0
  DCHECK(b >= 0);
75
0
  return a / b;
76
0
}
77
78
0
inline BigInt Rem(BigInt a, BigInt b) {
79
0
  // Is the behavior of negative values defined in C++?  Avoid difference with
80
0
  // Python.
81
0
  DCHECK(a >= 0);
82
0
  DCHECK(b >= 0);
83
0
  return a % b;
84
0
}
85
86
0
inline bool Equal(BigInt a, BigInt b) {
87
0
  return a == b;
88
0
}
89
90
0
inline bool Greater(BigInt a, BigInt b) {
91
0
  return a > b;
92
0
}
93
94
0
inline BigInt LShift(BigInt a, BigInt b) {
95
0
  return a << b;
96
0
}
97
98
0
inline BigInt RShift(BigInt a, BigInt b) {
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