|
14 | 14 |
|
15 | 15 | # data-types
|
16 | 16 |
|
17 |
| -class UnsignedInteger: |
18 |
| - def __init__(self, bit_width, value): |
19 |
| - if isinstance(value, UnsignedInteger): |
20 |
| - value = value.value |
21 |
| - self.bit_width = bit_width |
22 |
| - self.value = value % (2**bit_width) |
23 |
| - |
24 |
| - def __bool__(self): |
25 |
| - return self.value != 0 |
26 |
| - |
27 |
| - def __add__(self, other): |
28 |
| - if isinstance(other, self.__class__): |
29 |
| - return UnsignedInteger(self.bit_width, (self.value + other.value) % (2**self.bit_width)) |
30 |
| - else: |
31 |
| - raise TypeError("Unsupported operand type") |
32 |
| - |
33 |
| - def __sub__(self, other): |
34 |
| - if isinstance(other, self.__class__): |
35 |
| - # if self.value < other.value: |
36 |
| - # raise ValueError("Result of subtraction cannot be negative") |
37 |
| - return UnsignedInteger(self.bit_width, (self.value - other.value) % (2**self.bit_width)) |
38 |
| - else: |
39 |
| - raise TypeError("Unsupported operand type") |
40 |
| - |
41 |
| - def __mul__(self, other): |
42 |
| - if isinstance(other, self.__class__): |
43 |
| - return UnsignedInteger(self.bit_width, (self.value * other.value) % (2**self.bit_width)) |
44 |
| - else: |
45 |
| - raise TypeError("Unsupported operand type") |
46 |
| - |
47 |
| - def __div__(self, other): |
48 |
| - if isinstance(other, self.__class__): |
49 |
| - if other.value == 0: |
50 |
| - raise ValueError("Division by zero") |
51 |
| - return UnsignedInteger(self.bit_width, self.value / other.value) |
52 |
| - else: |
53 |
| - raise TypeError("Unsupported operand type") |
54 |
| - |
55 |
| - def __floordiv__(self, other): |
56 |
| - if isinstance(other, self.__class__): |
57 |
| - if other.value == 0: |
58 |
| - raise ValueError("Division by zero") |
59 |
| - return UnsignedInteger(self.bit_width, self.value // other.value) |
60 |
| - else: |
61 |
| - raise TypeError("Unsupported operand type") |
62 |
| - |
63 |
| - def __mod__(self, other): |
64 |
| - if isinstance(other, self.__class__): |
65 |
| - if other.value == 0: |
66 |
| - raise ValueError("Modulo by zero") |
67 |
| - return UnsignedInteger(self.bit_width, self.value % other.value) |
68 |
| - else: |
69 |
| - raise TypeError("Unsupported operand type") |
70 |
| - |
71 |
| - def __pow__(self, other): |
72 |
| - if isinstance(other, self.__class__): |
73 |
| - return UnsignedInteger(self.bit_width, (self.value ** other.value) % (2**self.bit_width)) |
74 |
| - else: |
75 |
| - raise TypeError("Unsupported operand type") |
76 |
| - |
77 |
| - def __and__(self, other): |
78 |
| - if isinstance(other, self.__class__): |
79 |
| - return UnsignedInteger(self.bit_width, self.value & other.value) |
80 |
| - else: |
81 |
| - raise TypeError("Unsupported operand type") |
82 |
| - |
83 |
| - def __or__(self, other): |
84 |
| - if isinstance(other, self.__class__): |
85 |
| - return UnsignedInteger(self.bit_width, self.value | other.value) |
86 |
| - else: |
87 |
| - raise TypeError("Unsupported operand type") |
88 |
| - |
89 |
| - # unary operators |
90 |
| - def __neg__(self): |
91 |
| - return UnsignedInteger(self.bit_width, -self.value % (2**self.bit_width)) |
92 |
| - |
93 |
| - def __pos__(self): |
94 |
| - return UnsignedInteger(self.bit_width, self.value) |
95 |
| - |
96 |
| - def __abs__(self): |
97 |
| - return UnsignedInteger(self.bit_width, abs(self.value)) |
98 |
| - |
99 |
| - def __invert__(self): |
100 |
| - return UnsignedInteger(self.bit_width, ~self.value % (2**self.bit_width)) |
101 |
| - |
102 |
| - # comparator operators |
103 |
| - def __eq__(self, other): |
104 |
| - if isinstance(other, self.__class__): |
105 |
| - return self.value == other.value |
106 |
| - else: |
107 |
| - try: |
108 |
| - return self.value == other |
109 |
| - except: |
110 |
| - raise TypeError("Unsupported operand type") |
111 |
| - |
112 |
| - def __ne__(self, other): |
113 |
| - if isinstance(other, self.__class__): |
114 |
| - return self.value != other.value |
115 |
| - else: |
116 |
| - raise TypeError("Unsupported operand type") |
117 |
| - |
118 |
| - def __lt__(self, other): |
119 |
| - if isinstance(other, self.__class__): |
120 |
| - return self.value < other.value |
121 |
| - else: |
122 |
| - raise TypeError("Unsupported operand type") |
123 |
| - |
124 |
| - def __le__(self, other): |
125 |
| - if isinstance(other, self.__class__): |
126 |
| - return self.value <= other.value |
127 |
| - else: |
128 |
| - raise TypeError("Unsupported operand type") |
129 |
| - |
130 |
| - def __gt__(self, other): |
131 |
| - if isinstance(other, self.__class__): |
132 |
| - return self.value > other.value |
133 |
| - else: |
134 |
| - raise TypeError("Unsupported operand type") |
135 |
| - |
136 |
| - def __ge__(self, other): |
137 |
| - if isinstance(other, self.__class__): |
138 |
| - return self.value >= other.value |
139 |
| - else: |
140 |
| - raise TypeError("Unsupported operand type") |
141 |
| - |
142 |
| - def __lshift__(self, other): |
143 |
| - if isinstance(other, self.__class__): |
144 |
| - return UnsignedInteger(self.bit_width, self.value << other.value) |
145 |
| - else: |
146 |
| - raise TypeError("Unsupported operand type") |
147 |
| - |
148 |
| - def __rshift__(self, other): |
149 |
| - if isinstance(other, self.__class__): |
150 |
| - return UnsignedInteger(self.bit_width, self.value >> other.value) |
151 |
| - else: |
152 |
| - raise TypeError("Unsupported operand type") |
153 |
| - |
154 |
| - # conversion to integer |
155 |
| - def __int__(self): |
156 |
| - return self.value |
157 |
| - |
158 |
| - def __str__(self): |
159 |
| - return str(self.value) |
160 |
| - |
161 |
| - def __repr__(self): |
162 |
| - return f'UnsignedInteger({self.bit_width}, {str(self)})' |
163 |
| - |
164 |
| - def __index__(self): |
165 |
| - return self.value |
166 |
| - |
167 |
| - |
168 |
| - |
169 | 17 | type_to_convert_func = {
|
170 | 18 | "i1": bool,
|
171 | 19 | "i8": int,
|
172 | 20 | "i16": int,
|
173 | 21 | "i32": int,
|
174 | 22 | "i64": int,
|
175 |
| - "u8": lambda x: UnsignedInteger(8, x), |
176 |
| - "u16": lambda x: UnsignedInteger(16, x), |
177 |
| - "u32": lambda x: UnsignedInteger(32, x), |
178 |
| - "u64": lambda x: UnsignedInteger(64, x), |
| 23 | + "u8": int, |
| 24 | + "u16": int, |
| 25 | + "u32": int, |
| 26 | + "u64": int, |
179 | 27 | "f32": float,
|
180 | 28 | "f64": float,
|
181 | 29 | "c32": complex,
|
@@ -859,3 +707,11 @@ def __call__(self, *args, **kwargs):
|
859 | 707 | function = getattr(__import__("lpython_module_" + self.fn_name),
|
860 | 708 | self.fn_name)
|
861 | 709 | return function(*args, **kwargs)
|
| 710 | + |
| 711 | +def bitnot(x, bitsize): |
| 712 | + return (~x) % (2 ** bitsize) |
| 713 | + |
| 714 | +bitnot_u8 = lambda x: bitnot(x, 8) |
| 715 | +bitnot_u16 = lambda x: bitnot(x, 16) |
| 716 | +bitnot_u32 = lambda x: bitnot(x, 32) |
| 717 | +bitnot_u64 = lambda x: bitnot(x, 64) |
0 commit comments