5 # the wrapped posit value
6 cdef cposit.posit8_t _c_posit
8 # factory function constructors that bypass __init__
11 cdef Posit8 from_c_posit(cposit.posit8_t f):
12 """Factory function to create a Posit8 object directly from
15 cdef Posit8 obj = Posit8.__new__(Posit8)
20 def from_bits(int value):
21 """Factory function to create a Posit8 object from a bit pattern
22 represented as an integer.
24 cdef Posit8 obj = Posit8.__new__(Posit8)
25 obj._c_posit = cposit.castP8(value)
29 def from_double(float value):
30 """Factory function to create a Posit8 object from a double.
32 cdef Posit8 obj = Posit8.__new__(Posit8)
33 obj._c_posit = cposit.convertDoubleToP8(value)
36 # convenience interface for use inside Python
38 def __init__(self, value):
39 if isinstance(value, int):
40 self._c_posit = cposit.castP8(value)
43 self._c_posit = cposit.convertDoubleToP8(f)
46 return repr(cposit.convertP8ToDouble(self._c_posit))
49 return 'Posit8(' + repr(cposit.convertP8ToDouble(self._c_posit)) + ')'
52 cdef int u = cposit.castUI8(self._c_posit)
54 bits = property(get_bits)
59 cdef cposit.posit8_t f = cposit.p8_roundToInt(self._c_posit)
60 return Posit8.from_c_posit(f)
62 def __round__(self, Posit8 other):
63 return self.roundToInt(other)
65 cpdef add(self, Posit8 other):
66 cdef cposit.posit8_t f = cposit.p8_add(self._c_posit, other._c_posit)
67 return Posit8.from_c_posit(f)
69 def __add__(self, Posit8 other):
70 return self.add(other)
72 cpdef sub(self, Posit8 other):
73 cdef cposit.posit8_t f = cposit.p8_sub(self._c_posit, other._c_posit)
74 return Posit8.from_c_posit(f)
76 def __sub__(self, Posit8 other):
77 return self.sub(other)
79 cpdef mul(self, Posit8 other):
80 cdef cposit.posit8_t f = cposit.p8_mul(self._c_posit, other._c_posit)
81 return Posit8.from_c_posit(f)
83 def __mul__(self, Posit8 other):
84 return self.mul(other)
86 cpdef fma(self, Posit8 a2, Posit8 a3):
87 cdef cposit.posit8_t f = cposit.p8_mulAdd(self._c_posit, a2._c_posit, a3._c_posit)
88 return Posit8.from_c_posit(f)
90 cpdef div(self, Posit8 other):
91 cdef cposit.posit8_t f = cposit.p8_div(self._c_posit, other._c_posit)
92 return Posit8.from_c_posit(f)
94 def __truediv__(self, Posit8 other):
95 return self.div(other)
98 cdef cposit.posit8_t f = cposit.p8_sqrt(self._c_posit)
99 return Posit8.from_c_posit(f)
101 # in-place arithmetic
104 self._c_posit = cposit.p8_roundToInt(self._c_posit)
106 cpdef iadd(self, Posit8 other):
107 self._c_posit = cposit.p8_add(self._c_posit, other._c_posit)
109 def __iadd__(self, Posit8 other):
113 cpdef isub(self, Posit8 other):
114 self._c_posit = cposit.p8_sub(self._c_posit, other._c_posit)
116 def __isub__(self, Posit8 other):
120 cpdef imul(self, Posit8 other):
121 self._c_posit = cposit.p8_mul(self._c_posit, other._c_posit)
123 def __imul__(self, Posit8 other):
127 cpdef ifma(self, Posit8 a2, Posit8 a3):
128 self._c_posit = cposit.p8_mulAdd(self._c_posit, a2._c_posit, a3._c_posit)
130 cpdef idiv(self, Posit8 other):
131 self._c_posit = cposit.p8_div(self._c_posit, other._c_posit)
133 def __itruediv__(self, Posit8 other):
138 self._c_posit = cposit.p8_sqrt(self._c_posit)
142 cpdef eq(self, Posit8 other):
143 cdef bint b = cposit.p8_eq(self._c_posit, other._c_posit)
146 cpdef le(self, Posit8 other):
147 cdef bint b = cposit.p8_le(self._c_posit, other._c_posit)
150 cpdef lt(self, Posit8 other):
151 cdef bint b = cposit.p8_lt(self._c_posit, other._c_posit)
154 def __lt__(self, Posit8 other):
155 return self.lt(other)
157 def __le__(self, Posit8 other):
158 return self.le(other)
160 def __eq__(self, Posit8 other):
161 return self.eq(other)
163 def __ne__(self, Posit8 other):
164 return not self.eq(other)
166 def __ge__(self, Posit8 other):
167 return other.le(self)
169 def __gt__(self, Posit8 other):
170 return other.lt(self)
172 # conversion to other posit types
175 cdef cposit.posit16_t f = cposit.p8_to_p16(self._c_posit)
176 return Posit16.from_c_posit(f)
181 # the wrapped quire value
182 cdef cposit.quire8_t _c_quire
184 # limited interface for now
187 cposit.q8_clr(self._c_quire)
190 return repr(cposit.convertP8ToDouble(cposit.q8_to_p8(self._c_quire)))
193 return 'Quire8(' + repr(cposit.convertP8ToDouble(cposit.q8_to_p8(self._c_quire))) + ')'
195 cpdef fdp_add(self, Posit8 a2, Posit8 a3):
196 self._c_quire = cposit.q8_fdp_add(self._c_quire, a2._c_posit, a3._c_posit)
198 cpdef fdp_sub(self, Posit8 a2, Posit8 a3):
199 self._c_quire = cposit.q8_fdp_sub(self._c_quire, a2._c_posit, a3._c_posit)
202 cpdef cposit.posit8_t f = cposit.q8_to_p8(self._c_quire)
203 return Posit8.from_c_posit(f)
204 p8 = property(get_p8)
209 # the wrapped posit value
210 cdef cposit.posit16_t _c_posit
212 # factory function constructors that bypass __init__
215 cdef Posit16 from_c_posit(cposit.posit16_t f):
216 """Factory function to create a Posit16 object directly from
219 cdef Posit16 obj = Posit16.__new__(Posit16)
224 def from_bits(int value):
225 """Factory function to create a Posit16 object from a bit pattern
226 represented as an integer.
228 cdef Posit16 obj = Posit16.__new__(Posit16)
229 obj._c_posit = cposit.castP16(value)
233 def from_double(float value):
234 """Factory function to create a Posit16 object from a double.
236 cdef Posit16 obj = Posit16.__new__(Posit16)
237 obj._c_posit = cposit.convertDoubleToP16(value)
240 # convenience interface for use inside Python
242 def __init__(self, value):
243 if isinstance(value, int):
244 self._c_posit = cposit.castP16(value)
247 self._c_posit = cposit.convertDoubleToP16(f)
250 return repr(cposit.convertP16ToDouble(self._c_posit))
253 return 'Posit16(' + repr(cposit.convertP16ToDouble(self._c_posit)) + ')'
255 cpdef get_bits(self):
256 cdef int u = cposit.castUI16(self._c_posit)
258 bits = property(get_bits)
263 cdef cposit.posit16_t f = cposit.p16_roundToInt(self._c_posit)
264 return Posit16.from_c_posit(f)
266 def __round__(self, Posit16 other):
267 return self.roundToInt(other)
269 cpdef add(self, Posit16 other):
270 cdef cposit.posit16_t f = cposit.p16_add(self._c_posit, other._c_posit)
271 return Posit16.from_c_posit(f)
273 def __add__(self, Posit16 other):
274 return self.add(other)
276 cpdef sub(self, Posit16 other):
277 cdef cposit.posit16_t f = cposit.p16_sub(self._c_posit, other._c_posit)
278 return Posit16.from_c_posit(f)
280 def __sub__(self, Posit16 other):
281 return self.sub(other)
283 cpdef mul(self, Posit16 other):
284 cdef cposit.posit16_t f = cposit.p16_mul(self._c_posit, other._c_posit)
285 return Posit16.from_c_posit(f)
287 def __mul__(self, Posit16 other):
288 return self.mul(other)
290 cpdef fma(self, Posit16 a2, Posit16 a3):
291 cdef cposit.posit16_t f = cposit.p16_mulAdd(self._c_posit, a2._c_posit, a3._c_posit)
292 return Posit16.from_c_posit(f)
294 cpdef div(self, Posit16 other):
295 cdef cposit.posit16_t f = cposit.p16_div(self._c_posit, other._c_posit)
296 return Posit16.from_c_posit(f)
298 def __truediv__(self, Posit16 other):
299 return self.div(other)
302 cdef cposit.posit16_t f = cposit.p16_sqrt(self._c_posit)
303 return Posit16.from_c_posit(f)
305 # in-place arithmetic
308 self._c_posit = cposit.p16_roundToInt(self._c_posit)
310 cpdef iadd(self, Posit16 other):
311 self._c_posit = cposit.p16_add(self._c_posit, other._c_posit)
313 def __iadd__(self, Posit16 other):
317 cpdef isub(self, Posit16 other):
318 self._c_posit = cposit.p16_sub(self._c_posit, other._c_posit)
320 def __isub__(self, Posit16 other):
324 cpdef imul(self, Posit16 other):
325 self._c_posit = cposit.p16_mul(self._c_posit, other._c_posit)
327 def __imul__(self, Posit16 other):
331 cpdef ifma(self, Posit16 a2, Posit16 a3):
332 self._c_posit = cposit.p16_mulAdd(self._c_posit, a2._c_posit, a3._c_posit)
334 cpdef idiv(self, Posit16 other):
335 self._c_posit = cposit.p16_div(self._c_posit, other._c_posit)
337 def __itruediv__(self, Posit16 other):
342 self._c_posit = cposit.p16_sqrt(self._c_posit)
346 cpdef eq(self, Posit16 other):
347 cdef bint b = cposit.p16_eq(self._c_posit, other._c_posit)
350 cpdef le(self, Posit16 other):
351 cdef bint b = cposit.p16_le(self._c_posit, other._c_posit)
354 cpdef lt(self, Posit16 other):
355 cdef bint b = cposit.p16_lt(self._c_posit, other._c_posit)
358 def __lt__(self, Posit16 other):
359 return self.lt(other)
361 def __le__(self, Posit16 other):
362 return self.le(other)
364 def __eq__(self, Posit16 other):
365 return self.eq(other)
367 def __ne__(self, Posit16 other):
368 return not self.eq(other)
370 def __ge__(self, Posit16 other):
371 return other.le(self)
373 def __gt__(self, Posit16 other):
374 return other.lt(self)
376 # conversion to other posit types
379 cdef cposit.posit8_t f = cposit.p16_to_p8(self._c_posit)
380 return Posit8.from_c_posit(f)
385 # the wrapped quire value
386 cdef cposit.quire16_t _c_quire
388 # limited interface for now
391 cposit.q16_clr(self._c_quire)
394 return repr(cposit.convertP16ToDouble(cposit.q16_to_p16(self._c_quire)))
397 return 'Quire16(' + repr(cposit.convertP16ToDouble(cposit.q16_to_p16(self._c_quire))) + ')'
399 cpdef fdp_add(self, Posit16 a2, Posit16 a3):
400 self._c_quire = cposit.q16_fdp_add(self._c_quire, a2._c_posit, a3._c_posit)
402 cpdef fdp_sub(self, Posit16 a2, Posit16 a3):
403 self._c_quire = cposit.q16_fdp_sub(self._c_quire, a2._c_posit, a3._c_posit)
406 cpdef cposit.posit16_t f = cposit.q16_to_p16(self._c_quire)
407 return Posit16.from_c_posit(f)
408 p16 = property(get_p16)