1 from libc.stdint cimport *
5 # special values needed for some initializations
7 cdef posit8_one = cposit.ui32_to_p8(1)
8 cdef posit16_one = cposit.ui32_to_p16(1)
13 # the wrapped posit value
14 cdef cposit.posit8_t _c_posit
16 # factory function constructors that bypass __init__
19 cdef Posit8 from_c_posit(cposit.posit8_t f):
20 """Factory function to create a Posit8 object directly from
23 cdef Posit8 obj = Posit8.__new__(Posit8)
28 def from_bits(uint8_t value):
29 """Factory function to create a Posit8 object from a bit pattern
30 represented as an integer.
32 cdef Posit8 obj = Posit8.__new__(Posit8)
33 obj._c_posit.v = value
37 def from_double(double value):
38 """Factory function to create a Posit8 object from a double.
40 cdef Posit8 obj = Posit8.__new__(Posit8)
41 obj._c_posit = cposit.convertDoubleToP8(value)
44 # convenience interface for use inside Python
46 def __init__(self, value):
47 if isinstance(value, int):
48 self._c_posit.v = value
51 self._c_posit = cposit.convertDoubleToP8(f)
54 return cposit.convertP8ToDouble(self._c_posit)
57 return int(cposit.convertP8ToDouble(self._c_posit))
60 return repr(cposit.convertP8ToDouble(self._c_posit))
63 return 'Posit8(' + repr(cposit.convertP8ToDouble(self._c_posit)) + ')'
65 cpdef uint8_t get_bits(self):
66 return self._c_posit.v
67 bits = property(get_bits)
71 cpdef Posit8 round(self):
72 cdef cposit.posit8_t f = cposit.p8_roundToInt(self._c_posit)
73 return Posit8.from_c_posit(f)
76 return self.roundToInt()
78 cpdef Posit8 add(self, Posit8 other):
79 cdef cposit.posit8_t f = cposit.p8_add(self._c_posit, other._c_posit)
80 return Posit8.from_c_posit(f)
82 def __add__(self, Posit8 other):
83 return self.add(other)
85 cpdef Posit8 sub(self, Posit8 other):
86 cdef cposit.posit8_t f = cposit.p8_sub(self._c_posit, other._c_posit)
87 return Posit8.from_c_posit(f)
89 def __sub__(self, Posit8 other):
90 return self.sub(other)
92 cpdef Posit8 mul(self, Posit8 other):
93 cdef cposit.posit8_t f = cposit.p8_mul(self._c_posit, other._c_posit)
94 return Posit8.from_c_posit(f)
96 def __mul__(self, Posit8 other):
97 return self.mul(other)
99 cpdef Posit8 fma(self, Posit8 a2, Posit8 a3):
100 cdef cposit.posit8_t f = cposit.p8_mulAdd(self._c_posit, a2._c_posit, a3._c_posit)
101 return Posit8.from_c_posit(f)
103 cpdef Posit8 fam(self, Posit8 a1, Posit8 a2):
104 cdef cposit.posit8_t f = cposit.p8_mulAdd(a1._c_posit, a2._c_posit, self._c_posit)
105 return Posit8.from_c_posit(f)
107 cpdef Posit8 div(self, Posit8 other):
108 cdef cposit.posit8_t f = cposit.p8_div(self._c_posit, other._c_posit)
109 return Posit8.from_c_posit(f)
111 def __truediv__(self, Posit8 other):
112 return self.div(other)
114 cpdef Posit8 sqrt(self):
115 cdef cposit.posit8_t f = cposit.p8_sqrt(self._c_posit)
116 return Posit8.from_c_posit(f)
118 # in-place arithmetic
120 cpdef void iround(self):
121 self._c_posit = cposit.p8_roundToInt(self._c_posit)
123 cpdef void iadd(self, Posit8 other):
124 self._c_posit = cposit.p8_add(self._c_posit, other._c_posit)
126 def __iadd__(self, Posit8 other):
130 cpdef void isub(self, Posit8 other):
131 self._c_posit = cposit.p8_sub(self._c_posit, other._c_posit)
133 def __isub__(self, Posit8 other):
137 cpdef void imul(self, Posit8 other):
138 self._c_posit = cposit.p8_mul(self._c_posit, other._c_posit)
140 def __imul__(self, Posit8 other):
144 cpdef void ifma(self, Posit8 a2, Posit8 a3):
145 self._c_posit = cposit.p8_mulAdd(self._c_posit, a2._c_posit, a3._c_posit)
147 cpdef void ifam(self, Posit8 a1, Posit8 a2):
148 self._c_posit = cposit.p8_mulAdd(a1._c_posit, a2._c_posit, self._c_posit)
150 cpdef void idiv(self, Posit8 other):
151 self._c_posit = cposit.p8_div(self._c_posit, other._c_posit)
153 def __itruediv__(self, Posit8 other):
157 cpdef void isqrt(self):
158 self._c_posit = cposit.p8_sqrt(self._c_posit)
162 cpdef bint eq(self, Posit8 other):
163 return cposit.p8_eq(self._c_posit, other._c_posit)
165 cpdef bint le(self, Posit8 other):
166 return cposit.p8_le(self._c_posit, other._c_posit)
168 cpdef bint lt(self, Posit8 other):
169 return cposit.p8_lt(self._c_posit, other._c_posit)
171 def __lt__(self, Posit8 other):
172 return self.lt(other)
174 def __le__(self, Posit8 other):
175 return self.le(other)
177 def __eq__(self, Posit8 other):
178 return self.eq(other)
180 def __ne__(self, Posit8 other):
181 return not self.eq(other)
183 def __ge__(self, Posit8 other):
184 return other.le(self)
186 def __gt__(self, Posit8 other):
187 return other.lt(self)
189 # conversion to other posit types
192 cdef cposit.posit16_t f = cposit.p8_to_p16(self._c_posit)
193 return Posit16.from_c_posit(f)
195 cpdef to_quire(self):
196 cdef cposit.quire8_t f
198 f = cposit.q8_fdp_add(f, self._c_posit, posit8_one)
199 return Quire8.from_c_quire(f)
204 # the wrapped quire value
205 cdef cposit.quire8_t _c_quire
207 # factory function constructors that bypass init
210 cdef Quire8 from_c_quire(cposit.quire8_t f):
211 """Factory function to create a Quire8 object directly from
214 cdef Quire8 obj = Quire8.__new__(Quire8)
219 def from_bits(uint32_t value):
220 """Factory function to create a Quire8 object from a bit pattern
221 represented as an integer.
223 cdef Quire8 obj = Quire8.__new__(Quire8)
224 obj._c_quire.v = value
227 # convenience interface for use inside Python
229 def __init__(self, value):
230 if isinstance(value, int):
231 self._c_quire.v = value
234 cposit.q8_clr(self._c_quire)
235 self._c_quire = cposit.q8_fdp_add(self._c_quire, cposit.convertDoubleToP8(f), posit8_one)
238 return cposit.convertP8ToDouble(cposit.q8_to_p8(self._c_quire))
241 return int(cposit.convertP8ToDouble(cposit.q8_to_p8(self._c_quire)))
244 return repr(cposit.convertP8ToDouble(cposit.q8_to_p8(self._c_quire)))
247 return 'Quire8(' + repr(cposit.convertP8ToDouble(cposit.q8_to_p8(self._c_quire))) + ')'
249 cpdef uint32_t get_bits(self):
250 return self._c_quire.v
251 bits = property(get_bits)
255 cpdef Quire8 qam(self, Posit8 a1, Posit8 a2):
256 cdef cposit.quire8_t f = cposit.q8_fdp_add(self._c_quire, a1._c_posit, a2._c_posit)
257 return Quire8.from_c_quire(f)
259 cpdef Quire8 qsm(self, Posit8 a1, Posit8 a2):
260 cdef cposit.quire8_t f = cposit.q8_fdp_sub(self._c_quire, a1._c_posit, a2._c_posit)
261 return Quire8.from_c_quire(f)
263 cpdef void iqam(self, Posit8 a1, Posit8 a2):
264 self._c_quire = cposit.q8_fdp_add(self._c_quire, a1._c_posit, a2._c_posit)
266 cpdef void iqsm(self, Posit8 a1, Posit8 a2):
267 self._c_quire = cposit.q8_fdp_sub(self._c_quire, a1._c_posit, a2._c_posit)
269 cpdef void iclr(self):
270 cposit.q8_clr(self._c_quire)
272 # conversion back to posit
274 cpdef Posit8 to_posit(self):
275 cpdef cposit.posit8_t f = cposit.q8_to_p8(self._c_quire)
276 return Posit8.from_c_posit(f)
279 # external, non-method arithmetic
281 cpdef Posit8 p8_round(Posit8 a1):
282 cdef cposit.posit8_t f = cposit.p8_roundToInt(a1._c_posit)
283 return Posit8.from_c_posit(f)
285 cpdef Posit8 p8_add(Posit8 a1, Posit8 a2):
286 cdef cposit.posit8_t f = cposit.p8_add(a1._c_posit, a2._c_posit)
287 return Posit8.from_c_posit(f)
289 cpdef Posit8 p8_sub(Posit8 a1, Posit8 a2):
290 cdef cposit.posit8_t f = cposit.p8_sub(a1._c_posit, a2._c_posit)
291 return Posit8.from_c_posit(f)
293 cpdef Posit8 p8_mul(Posit8 a1, Posit8 a2):
294 cdef cposit.posit8_t f = cposit.p8_mul(a1._c_posit, a2._c_posit)
295 return Posit8.from_c_posit(f)
297 cpdef Posit8 p8_fma(Posit8 a1, Posit8 a2, Posit8 a3):
298 cdef cposit.posit8_t f = cposit.p8_mulAdd(a1._c_posit, a2._c_posit, a3._c_posit)
299 return Posit8.from_c_posit(f)
301 cpdef Posit8 p8_fam(Posit8 a3, Posit8 a1, Posit8 a2):
302 cdef cposit.posit8_t f = cposit.p8_mulAdd(a1._c_posit, a2._c_posit, a3._c_posit)
303 return Posit8.from_c_posit(f)
305 cpdef Posit8 p8_div(Posit8 a1, Posit8 a2):
306 cdef cposit.posit8_t f = cposit.p8_div(a1._c_posit, a2._c_posit)
307 return Posit8.from_c_posit(f)
309 cpdef Posit8 p8_sqrt(Posit8 a1):
310 cdef cposit.posit8_t f = cposit.p8_sqrt(a1._c_posit)
311 return Posit8.from_c_posit(f)
313 cpdef bint p8_eq(Posit8 a1, Posit8 a2):
314 return cposit.p8_eq(a1._c_posit, a2._c_posit)
316 cpdef bint p8_le(Posit8 a1, Posit8 a2):
317 return cposit.p8_le(a1._c_posit, a2._c_posit)
319 cpdef bint p8_lt(Posit8 a1, Posit8 a2):
320 return cposit.p8_lt(a1._c_posit, a2._c_posit)
322 cpdef Posit16 p8_to_p16(Posit8 a1):
323 cdef cposit.posit16_t f = cposit.p8_to_p16(a1._c_posit)
324 return Posit16.from_c_posit(f)
326 cpdef Quire8 p8_to_q8(Posit8 a1):
327 cdef cposit.quire8_t f
329 f = cposit.q8_fdp_add(f, a1._c_posit, posit8_one)
330 return Quire8.from_c_quire(f)
332 cpdef Quire8 q8_qam(Quire8 a3, Posit8 a1, Posit8 a2):
333 cdef cposit.quire8_t f = cposit.q8_fdp_add(a3._c_quire, a1._c_posit, a2._c_posit)
334 return Quire8.from_c_quire(f)
336 cpdef Quire8 q8_qsm(Quire8 a3, Posit8 a1, Posit8 a2):
337 cdef cposit.quire8_t f = cposit.q8_fdp_sub(a3._c_quire, a1._c_posit, a2._c_posit)
338 return Quire8.from_c_quire(f)
340 cpdef Posit8 q8_to_p8(Quire8 a1):
341 cpdef cposit.posit8_t f = cposit.q8_to_p8(a1._c_quire)
342 return Posit8.from_c_posit(f)
347 # the wrapped posit value
348 cdef cposit.posit16_t _c_posit
350 # factory function constructors that bypass __init__
353 cdef Posit16 from_c_posit(cposit.posit16_t f):
354 """Factory function to create a Posit16 object directly from
357 cdef Posit16 obj = Posit16.__new__(Posit16)
362 def from_bits(uint16_t value):
363 """Factory function to create a Posit16 object from a bit pattern
364 represented as an integer.
366 cdef Posit16 obj = Posit16.__new__(Posit16)
367 obj._c_posit.v = value
371 def from_double(double value):
372 """Factory function to create a Posit16 object from a double.
374 cdef Posit16 obj = Posit16.__new__(Posit16)
375 obj._c_posit = cposit.convertDoubleToP16(value)
378 # convenience interface for use inside Python
380 def __init__(self, value):
381 if isinstance(value, int):
382 self._c_posit.v = value
385 self._c_posit = cposit.convertDoubleToP16(f)
388 return cposit.convertP16ToDouble(self._c_posit)
391 return int(cposit.convertP16ToDouble(self._c_posit))
394 return repr(cposit.convertP16ToDouble(self._c_posit))
397 return 'Posit16(' + repr(cposit.convertP16ToDouble(self._c_posit)) + ')'
399 cpdef uint16_t get_bits(self):
400 return self._c_posit.v
401 bits = property(get_bits)
405 cpdef Posit16 round(self):
406 cdef cposit.posit16_t f = cposit.p16_roundToInt(self._c_posit)
407 return Posit16.from_c_posit(f)
410 return self.roundToInt()
412 cpdef Posit16 add(self, Posit16 other):
413 cdef cposit.posit16_t f = cposit.p16_add(self._c_posit, other._c_posit)
414 return Posit16.from_c_posit(f)
416 def __add__(self, Posit16 other):
417 return self.add(other)
419 cpdef Posit16 sub(self, Posit16 other):
420 cdef cposit.posit16_t f = cposit.p16_sub(self._c_posit, other._c_posit)
421 return Posit16.from_c_posit(f)
423 def __sub__(self, Posit16 other):
424 return self.sub(other)
426 cpdef Posit16 mul(self, Posit16 other):
427 cdef cposit.posit16_t f = cposit.p16_mul(self._c_posit, other._c_posit)
428 return Posit16.from_c_posit(f)
430 def __mul__(self, Posit16 other):
431 return self.mul(other)
433 cpdef Posit16 fma(self, Posit16 a2, Posit16 a3):
434 cdef cposit.posit16_t f = cposit.p16_mulAdd(self._c_posit, a2._c_posit, a3._c_posit)
435 return Posit16.from_c_posit(f)
437 cpdef Posit16 fam(self, Posit16 a1, Posit16 a2):
438 cdef cposit.posit16_t f = cposit.p16_mulAdd(a1._c_posit, a2._c_posit, self._c_posit)
439 return Posit16.from_c_posit(f)
441 cpdef Posit16 div(self, Posit16 other):
442 cdef cposit.posit16_t f = cposit.p16_div(self._c_posit, other._c_posit)
443 return Posit16.from_c_posit(f)
445 def __truediv__(self, Posit16 other):
446 return self.div(other)
448 cpdef Posit16 sqrt(self):
449 cdef cposit.posit16_t f = cposit.p16_sqrt(self._c_posit)
450 return Posit16.from_c_posit(f)
452 # in-place arithmetic
454 cpdef void iround(self):
455 self._c_posit = cposit.p16_roundToInt(self._c_posit)
457 cpdef void iadd(self, Posit16 other):
458 self._c_posit = cposit.p16_add(self._c_posit, other._c_posit)
460 def __iadd__(self, Posit16 other):
464 cpdef void isub(self, Posit16 other):
465 self._c_posit = cposit.p16_sub(self._c_posit, other._c_posit)
467 def __isub__(self, Posit16 other):
471 cpdef void imul(self, Posit16 other):
472 self._c_posit = cposit.p16_mul(self._c_posit, other._c_posit)
474 def __imul__(self, Posit16 other):
478 cpdef void ifma(self, Posit16 a2, Posit16 a3):
479 self._c_posit = cposit.p16_mulAdd(self._c_posit, a2._c_posit, a3._c_posit)
481 cpdef void ifam(self, Posit16 a1, Posit16 a2):
482 self._c_posit = cposit.p16_mulAdd(a1._c_posit, a2._c_posit, self._c_posit)
484 cpdef void idiv(self, Posit16 other):
485 self._c_posit = cposit.p16_div(self._c_posit, other._c_posit)
487 def __itruediv__(self, Posit16 other):
491 cpdef void isqrt(self):
492 self._c_posit = cposit.p16_sqrt(self._c_posit)
496 cpdef bint eq(self, Posit16 other):
497 return cposit.p16_eq(self._c_posit, other._c_posit)
499 cpdef bint le(self, Posit16 other):
500 return cposit.p16_le(self._c_posit, other._c_posit)
502 cpdef bint lt(self, Posit16 other):
503 return cposit.p16_lt(self._c_posit, other._c_posit)
505 def __lt__(self, Posit16 other):
506 return self.lt(other)
508 def __le__(self, Posit16 other):
509 return self.le(other)
511 def __eq__(self, Posit16 other):
512 return self.eq(other)
514 def __ne__(self, Posit16 other):
515 return not self.eq(other)
517 def __ge__(self, Posit16 other):
518 return other.le(self)
520 def __gt__(self, Posit16 other):
521 return other.lt(self)
523 # conversion to other posit types
526 cdef cposit.posit8_t f = cposit.p16_to_p8(self._c_posit)
527 return Posit8.from_c_posit(f)
529 cpdef to_quire(self):
530 cdef cposit.quire16_t f
532 f = cposit.q16_fdp_add(f, self._c_posit, posit16_one)
533 return Quire16.from_c_quire(f)
538 # the wrapped quire value
539 cdef cposit.quire16_t _c_quire
541 # factory function constructors that bypass init
544 cdef Quire16 from_c_quire(cposit.quire16_t f):
545 """Factory function to create a Quire16 object directly from
548 cdef Quire16 obj = Quire16.__new__(Quire16)
553 def from_bits(value):
554 """Factory function to create a Quire16 object from a bit pattern
555 represented as an integer.
557 cdef Quire16 obj = Quire16.__new__(Quire16)
559 if not isinstance(value, int):
560 raise TypeError('expecting int, got {}'.format(repr(value)))
562 for idx in range(1, -1, -1):
563 obj._c_quire.v[idx] = value & 0xffffffffffffffff
567 raise OverflowError('value too large to fit in uint64_t[2]')
571 # convenience interface for use inside Python
573 def __init__(self, value):
574 if isinstance(value, int):
575 for idx in range(1, -1, -1):
576 self._c_quire.v[idx] = value & 0xffffffffffffffff
579 raise OverflowError('value too large to fit in uint64_t[2]')
582 cposit.q16_clr(self._c_quire)
583 self._c_quire = cposit.q16_fdp_add(self._c_quire, cposit.convertDoubleToP16(f), posit16_one)
586 return cposit.convertP16ToDouble(cposit.q16_to_p16(self._c_quire))
589 return int(cposit.convertP16ToDouble(cposit.q16_to_p16(self._c_quire)))
592 return repr(cposit.convertP16ToDouble(cposit.q16_to_p16(self._c_quire)))
595 return 'Quire16(' + repr(cposit.convertP16ToDouble(cposit.q16_to_p16(self._c_quire))) + ')'
599 for u in self._c_quire.v:
603 bits = property(get_bits)
607 cpdef Quire16 qam(self, Posit16 a1, Posit16 a2):
608 cdef cposit.quire16_t f = cposit.q16_fdp_add(self._c_quire, a1._c_posit, a2._c_posit)
609 return Quire16.from_c_quire(f)
611 cpdef Quire16 qsm(self, Posit16 a1, Posit16 a2):
612 cdef cposit.quire16_t f = cposit.q16_fdp_sub(self._c_quire, a1._c_posit, a2._c_posit)
613 return Quire16.from_c_quire(f)
615 cpdef void iqam(self, Posit16 a1, Posit16 a2):
616 self._c_quire = cposit.q16_fdp_add(self._c_quire, a1._c_posit, a2._c_posit)
618 cpdef void iqsm(self, Posit16 a1, Posit16 a2):
619 self._c_quire = cposit.q16_fdp_sub(self._c_quire, a1._c_posit, a2._c_posit)
621 cpdef void iclr(self):
622 cposit.q16_clr(self._c_quire)
624 # conversion back to posit
626 cpdef Posit16 to_posit(self):
627 cpdef cposit.posit16_t f = cposit.q16_to_p16(self._c_quire)
628 return Posit16.from_c_posit(f)
631 # external, non-method arithmetic
633 cpdef Posit16 p16_round(Posit16 a1):
634 cdef cposit.posit16_t f = cposit.p16_roundToInt(a1._c_posit)
635 return Posit16.from_c_posit(f)
637 cpdef Posit16 p16_add(Posit16 a1, Posit16 a2):
638 cdef cposit.posit16_t f = cposit.p16_add(a1._c_posit, a2._c_posit)
639 return Posit16.from_c_posit(f)
641 cpdef Posit16 p16_sub(Posit16 a1, Posit16 a2):
642 cdef cposit.posit16_t f = cposit.p16_sub(a1._c_posit, a2._c_posit)
643 return Posit16.from_c_posit(f)
645 cpdef Posit16 p16_mul(Posit16 a1, Posit16 a2):
646 cdef cposit.posit16_t f = cposit.p16_mul(a1._c_posit, a2._c_posit)
647 return Posit16.from_c_posit(f)
649 cpdef Posit16 p16_fma(Posit16 a1, Posit16 a2, Posit16 a3):
650 cdef cposit.posit16_t f = cposit.p16_mulAdd(a1._c_posit, a2._c_posit, a3._c_posit)
651 return Posit16.from_c_posit(f)
653 cpdef Posit16 p16_fam(Posit16 a3, Posit16 a1, Posit16 a2):
654 cdef cposit.posit16_t f = cposit.p16_mulAdd(a1._c_posit, a2._c_posit, a3._c_posit)
655 return Posit16.from_c_posit(f)
657 cpdef Posit16 p16_div(Posit16 a1, Posit16 a2):
658 cdef cposit.posit16_t f = cposit.p16_div(a1._c_posit, a2._c_posit)
659 return Posit16.from_c_posit(f)
661 cpdef Posit16 p16_sqrt(Posit16 a1):
662 cdef cposit.posit16_t f = cposit.p16_sqrt(a1._c_posit)
663 return Posit16.from_c_posit(f)
665 cpdef bint p16_eq(Posit16 a1, Posit16 a2):
666 return cposit.p16_eq(a1._c_posit, a2._c_posit)
668 cpdef bint p16_le(Posit16 a1, Posit16 a2):
669 return cposit.p16_le(a1._c_posit, a2._c_posit)
671 cpdef bint p16_lt(Posit16 a1, Posit16 a2):
672 return cposit.p16_lt(a1._c_posit, a2._c_posit)
674 cpdef Posit8 p16_to_p8(Posit16 a1):
675 cdef cposit.posit8_t f = cposit.p16_to_p8(a1._c_posit)
676 return Posit8.from_c_posit(f)
678 cpdef Quire16 p16_to_q16(Posit16 a1):
679 cdef cposit.quire16_t f
681 f = cposit.q16_fdp_add(f, a1._c_posit, posit16_one)
682 return Quire16.from_c_quire(f)
684 cpdef Quire16 q16_qam(Quire16 a3, Posit16 a1, Posit16 a2):
685 cdef cposit.quire16_t f = cposit.q16_fdp_add(a3._c_quire, a1._c_posit, a2._c_posit)
686 return Quire16.from_c_quire(f)
688 cpdef Quire16 q16_qsm(Quire16 a3, Posit16 a1, Posit16 a2):
689 cdef cposit.quire16_t f = cposit.q16_fdp_sub(a3._c_quire, a1._c_posit, a2._c_posit)
690 return Quire16.from_c_quire(f)
692 cpdef Posit16 q16_to_p16(Quire16 a1):
693 cpdef cposit.posit16_t f = cposit.q16_to_p16(a1._c_quire)
694 return Posit16.from_c_posit(f)