OILS / mycpp / mops.py View on Github | oilshell.org

210 lines, 79 significant
1"""
2Math operations, e.g. for arbitrary precision integers
3
4They are currently int64_t, rather than C int, but we want to upgrade to
5heap-allocated integers.
6
7Regular int ops can use the normal operators + - * /, or maybe i_add() if we
8really want. Does that make code gen harder or worse?
9
10Float ops could be + - * / too, but it feels nicer to develop a formal
11interface?
12"""
13from __future__ import print_function
14
15from typing import Tuple
16
17
18class BigInt(object):
19
20 def __init__(self, i):
21 # type: (int) -> None
22 self.i = i
23
24 def __eq__(self, other):
25 # type: (object) -> bool
26
27 # Disabled check
28 # Prevent possible mistakes. Could do this with other operators
29 # raise AssertionError('Use mops.Equal()')
30
31 if not isinstance(other, BigInt):
32 raise AssertionError()
33
34 # Used for hashing
35 return self.i == other.i
36
37 def __gt__(self, other):
38 # type: (object) -> bool
39 raise AssertionError('Use functions in mops.py')
40
41 def __ge__(self, other):
42 # type: (object) -> bool
43 raise AssertionError('Use functions in mops.py')
44
45 def __hash__(self):
46 # type: () -> int
47 """For dict lookups."""
48 return hash(self.i)
49
50
51ZERO = BigInt(0)
52ONE = BigInt(1)
53MINUS_ONE = BigInt(-1)
54MINUS_TWO = BigInt(-2) # for printf
55
56
57def ToStr(b):
58 # type: (BigInt) -> str
59 return str(b.i)
60
61
62def ToOctal(b):
63 # type: (BigInt) -> str
64 return '%o' % b.i
65
66
67def ToHexUpper(b):
68 # type: (BigInt) -> str
69 return '%X' % b.i
70
71
72def ToHexLower(b):
73 # type: (BigInt) -> str
74 return '%x' % b.i
75
76
77def FromStr(s, base=10):
78 # type: (str, int) -> BigInt
79 return BigInt(int(s, base))
80
81
82def BigTruncate(b):
83 # type: (BigInt) -> int
84 """Only truncates in C++"""
85 return b.i
86
87
88def IntWiden(i):
89 # type: (int) -> BigInt
90 """Only widens in C++"""
91 return BigInt(i)
92
93
94def FromC(i):
95 # type: (int) -> BigInt
96 """A no-op in C, for RLIM_INFINITY"""
97 return BigInt(i)
98
99
100def FromBool(b):
101 # type: (bool) -> BigInt
102 """Only widens in C++"""
103 return BigInt(1) if b else BigInt(0)
104
105
106def ToFloat(b):
107 # type: (BigInt) -> float
108 """Used by float(42) in Oils"""
109 return float(b.i)
110
111
112def FromFloat(f):
113 # type: (float) -> Tuple[bool, BigInt]
114 """Used by int(3.14) in Oils"""
115 try:
116 big = int(f)
117 except ValueError: # NAN
118 return False, MINUS_ONE
119 except OverflowError: # INFINITY
120 return False, MINUS_ONE
121 return True, BigInt(big)
122
123
124# Can't use operator overloading
125
126
127def Negate(b):
128 # type: (BigInt) -> BigInt
129 return BigInt(-b.i)
130
131
132def Add(a, b):
133 # type: (BigInt, BigInt) -> BigInt
134 return BigInt(a.i + b.i)
135
136
137def Sub(a, b):
138 # type: (BigInt, BigInt) -> BigInt
139 return BigInt(a.i - b.i)
140
141
142def Mul(a, b):
143 # type: (BigInt, BigInt) -> BigInt
144 return BigInt(a.i * b.i)
145
146
147def Div(a, b):
148 # type: (BigInt, BigInt) -> BigInt
149 """
150 Divide, for positive integers only
151
152 Question: does Oils behave like C remainder when it's positive? Then we
153 could be more efficient with a different layering?
154 """
155 assert a.i >= 0 and b.i >= 0, (a.i, b.i)
156 return BigInt(a.i // b.i)
157
158
159def Rem(a, b):
160 # type: (BigInt, BigInt) -> BigInt
161 """
162 Remainder, for positive integers only
163 """
164 assert a.i >= 0 and b.i >= 0, (a.i, b.i)
165 return BigInt(a.i % b.i)
166
167
168def Equal(a, b):
169 # type: (BigInt, BigInt) -> bool
170 return a.i == b.i
171
172
173def Greater(a, b):
174 # type: (BigInt, BigInt) -> bool
175 return a.i > b.i
176
177
178# GreaterEq, Less, LessEq can all be expressed as the 2 ops above
179
180
181def LShift(a, b):
182 # type: (BigInt, BigInt) -> BigInt
183 assert b.i >= 0, b.i # Must be checked by caller
184 return BigInt(a.i << b.i)
185
186
187def RShift(a, b):
188 # type: (BigInt, BigInt) -> BigInt
189 assert b.i >= 0, b.i # Must be checked by caller
190 return BigInt(a.i >> b.i)
191
192
193def BitAnd(a, b):
194 # type: (BigInt, BigInt) -> BigInt
195 return BigInt(a.i & b.i)
196
197
198def BitOr(a, b):
199 # type: (BigInt, BigInt) -> BigInt
200 return BigInt(a.i | b.i)
201
202
203def BitXor(a, b):
204 # type: (BigInt, BigInt) -> BigInt
205 return BigInt(a.i ^ b.i)
206
207
208def BitNot(a):
209 # type: (BigInt) -> BigInt
210 return BigInt(~a.i)