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