rename fused multiply add-like operations:
[sfpy.git] / sfpy / posit.pyx
1 from libc.stdint cimport *
2 cimport cposit
3
4
5 # special values needed for some initializations
6
7 cdef posit8_one = cposit.ui32_to_p8(1)
8 cdef posit16_one = cposit.ui32_to_p16(1)
9
10
11 cdef class Posit8:
12
13 # the wrapped posit value
14 cdef cposit.posit8_t _c_posit
15
16 # factory function constructors that bypass __init__
17
18 @staticmethod
19 cdef Posit8 from_c_posit(cposit.posit8_t f):
20 """Factory function to create a Posit8 object directly from
21 a C posit8_t.
22 """
23 cdef Posit8 obj = Posit8.__new__(Posit8)
24 obj._c_posit = f
25 return obj
26
27 @staticmethod
28 def from_bits(uint8_t value):
29 """Factory function to create a Posit8 object from a bit pattern
30 represented as an integer.
31 """
32 cdef Posit8 obj = Posit8.__new__(Posit8)
33 obj._c_posit.v = value
34 return obj
35
36 @staticmethod
37 def from_double(double value):
38 """Factory function to create a Posit8 object from a double.
39 """
40 cdef Posit8 obj = Posit8.__new__(Posit8)
41 obj._c_posit = cposit.convertDoubleToP8(value)
42 return obj
43
44 # convenience interface for use inside Python
45
46 def __init__(self, value):
47 if isinstance(value, int):
48 self._c_posit.v = value
49 else:
50 f = float(value)
51 self._c_posit = cposit.convertDoubleToP8(f)
52
53 def __float__(self):
54 return cposit.convertP8ToDouble(self._c_posit)
55
56 def __int__(self):
57 return int(cposit.convertP8ToDouble(self._c_posit))
58
59 def __str__(self):
60 return repr(cposit.convertP8ToDouble(self._c_posit))
61
62 def __repr__(self):
63 return 'Posit8(' + repr(cposit.convertP8ToDouble(self._c_posit)) + ')'
64
65 cpdef uint8_t get_bits(self):
66 return self._c_posit.v
67 bits = property(get_bits)
68
69 # arithmetic
70
71 cpdef Posit8 round(self):
72 cdef cposit.posit8_t f = cposit.p8_roundToInt(self._c_posit)
73 return Posit8.from_c_posit(f)
74
75 def __round__(self):
76 return self.roundToInt()
77
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)
81
82 def __add__(self, Posit8 other):
83 return self.add(other)
84
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)
88
89 def __sub__(self, Posit8 other):
90 return self.sub(other)
91
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)
95
96 def __mul__(self, Posit8 other):
97 return self.mul(other)
98
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)
102
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)
106
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)
110
111 def __truediv__(self, Posit8 other):
112 return self.div(other)
113
114 cpdef Posit8 sqrt(self):
115 cdef cposit.posit8_t f = cposit.p8_sqrt(self._c_posit)
116 return Posit8.from_c_posit(f)
117
118 # in-place arithmetic
119
120 cpdef void iround(self):
121 self._c_posit = cposit.p8_roundToInt(self._c_posit)
122
123 cpdef void iadd(self, Posit8 other):
124 self._c_posit = cposit.p8_add(self._c_posit, other._c_posit)
125
126 def __iadd__(self, Posit8 other):
127 self.iadd(other)
128 return self
129
130 cpdef void isub(self, Posit8 other):
131 self._c_posit = cposit.p8_sub(self._c_posit, other._c_posit)
132
133 def __isub__(self, Posit8 other):
134 self.isub(other)
135 return self
136
137 cpdef void imul(self, Posit8 other):
138 self._c_posit = cposit.p8_mul(self._c_posit, other._c_posit)
139
140 def __imul__(self, Posit8 other):
141 self.imul(other)
142 return self
143
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)
146
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)
149
150 cpdef void idiv(self, Posit8 other):
151 self._c_posit = cposit.p8_div(self._c_posit, other._c_posit)
152
153 def __itruediv__(self, Posit8 other):
154 self.idiv(other)
155 return self
156
157 cpdef void isqrt(self):
158 self._c_posit = cposit.p8_sqrt(self._c_posit)
159
160 # comparison
161
162 cpdef bint eq(self, Posit8 other):
163 return cposit.p8_eq(self._c_posit, other._c_posit)
164
165 cpdef bint le(self, Posit8 other):
166 return cposit.p8_le(self._c_posit, other._c_posit)
167
168 cpdef bint lt(self, Posit8 other):
169 return cposit.p8_lt(self._c_posit, other._c_posit)
170
171 def __lt__(self, Posit8 other):
172 return self.lt(other)
173
174 def __le__(self, Posit8 other):
175 return self.le(other)
176
177 def __eq__(self, Posit8 other):
178 return self.eq(other)
179
180 def __ne__(self, Posit8 other):
181 return not self.eq(other)
182
183 def __ge__(self, Posit8 other):
184 return other.le(self)
185
186 def __gt__(self, Posit8 other):
187 return other.lt(self)
188
189 # conversion to other posit types
190
191 cpdef to_p16(self):
192 cdef cposit.posit16_t f = cposit.p8_to_p16(self._c_posit)
193 return Posit16.from_c_posit(f)
194
195 cpdef to_quire(self):
196 cdef cposit.quire8_t f
197 cposit.q8_clr(f)
198 f = cposit.q8_fdp_add(f, self._c_posit, posit8_one)
199 return Quire8.from_c_quire(f)
200
201
202 cdef class Quire8:
203
204 # the wrapped quire value
205 cdef cposit.quire8_t _c_quire
206
207 # factory function constructors that bypass init
208
209 @staticmethod
210 cdef Quire8 from_c_quire(cposit.quire8_t f):
211 """Factory function to create a Quire8 object directly from
212 a C quire8_t.
213 """
214 cdef Quire8 obj = Quire8.__new__(Quire8)
215 obj._c_quire = f
216 return obj
217
218 @staticmethod
219 def from_bits(uint32_t value):
220 """Factory function to create a Quire8 object from a bit pattern
221 represented as an integer.
222 """
223 cdef Quire8 obj = Quire8.__new__(Quire8)
224 obj._c_quire.v = value
225 return obj
226
227 # convenience interface for use inside Python
228
229 def __init__(self, value):
230 if isinstance(value, int):
231 self._c_quire.v = value
232 else:
233 f = float(value)
234 cposit.q8_clr(self._c_quire)
235 self._c_quire = cposit.q8_fdp_add(self._c_quire, cposit.convertDoubleToP8(f), posit8_one)
236
237 def __float__(self):
238 return cposit.convertP8ToDouble(cposit.q8_to_p8(self._c_quire))
239
240 def __int__(self):
241 return int(cposit.convertP8ToDouble(cposit.q8_to_p8(self._c_quire)))
242
243 def __str__(self):
244 return repr(cposit.convertP8ToDouble(cposit.q8_to_p8(self._c_quire)))
245
246 def __repr__(self):
247 return 'Quire8(' + repr(cposit.convertP8ToDouble(cposit.q8_to_p8(self._c_quire))) + ')'
248
249 cpdef uint32_t get_bits(self):
250 return self._c_quire.v
251 bits = property(get_bits)
252
253 # arithmetic
254
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)
258
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)
262
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)
265
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)
268
269 cpdef void iclr(self):
270 cposit.q8_clr(self._c_quire)
271
272 # conversion back to posit
273
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)
277
278
279 # external, non-method arithmetic
280
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)
284
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)
288
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)
292
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)
296
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)
300
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)
304
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)
308
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)
312
313 cpdef bint p8_eq(Posit8 a1, Posit8 a2):
314 return cposit.p8_eq(a1._c_posit, a2._c_posit)
315
316 cpdef bint p8_le(Posit8 a1, Posit8 a2):
317 return cposit.p8_le(a1._c_posit, a2._c_posit)
318
319 cpdef bint p8_lt(Posit8 a1, Posit8 a2):
320 return cposit.p8_lt(a1._c_posit, a2._c_posit)
321
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)
325
326 cpdef Quire8 p8_to_q8(Posit8 a1):
327 cdef cposit.quire8_t f
328 cposit.q8_clr(f)
329 f = cposit.q8_fdp_add(f, a1._c_posit, posit8_one)
330 return Quire8.from_c_quire(f)
331
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)
335
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)
339
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)
343
344
345 cdef class Posit16:
346
347 # the wrapped posit value
348 cdef cposit.posit16_t _c_posit
349
350 # factory function constructors that bypass __init__
351
352 @staticmethod
353 cdef Posit16 from_c_posit(cposit.posit16_t f):
354 """Factory function to create a Posit16 object directly from
355 a C posit16_t.
356 """
357 cdef Posit16 obj = Posit16.__new__(Posit16)
358 obj._c_posit = f
359 return obj
360
361 @staticmethod
362 def from_bits(uint16_t value):
363 """Factory function to create a Posit16 object from a bit pattern
364 represented as an integer.
365 """
366 cdef Posit16 obj = Posit16.__new__(Posit16)
367 obj._c_posit.v = value
368 return obj
369
370 @staticmethod
371 def from_double(double value):
372 """Factory function to create a Posit16 object from a double.
373 """
374 cdef Posit16 obj = Posit16.__new__(Posit16)
375 obj._c_posit = cposit.convertDoubleToP16(value)
376 return obj
377
378 # convenience interface for use inside Python
379
380 def __init__(self, value):
381 if isinstance(value, int):
382 self._c_posit.v = value
383 else:
384 f = float(value)
385 self._c_posit = cposit.convertDoubleToP16(f)
386
387 def __float__(self):
388 return cposit.convertP16ToDouble(self._c_posit)
389
390 def __int__(self):
391 return int(cposit.convertP16ToDouble(self._c_posit))
392
393 def __str__(self):
394 return repr(cposit.convertP16ToDouble(self._c_posit))
395
396 def __repr__(self):
397 return 'Posit16(' + repr(cposit.convertP16ToDouble(self._c_posit)) + ')'
398
399 cpdef uint16_t get_bits(self):
400 return self._c_posit.v
401 bits = property(get_bits)
402
403 # arithmetic
404
405 cpdef Posit16 round(self):
406 cdef cposit.posit16_t f = cposit.p16_roundToInt(self._c_posit)
407 return Posit16.from_c_posit(f)
408
409 def __round__(self):
410 return self.roundToInt()
411
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)
415
416 def __add__(self, Posit16 other):
417 return self.add(other)
418
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)
422
423 def __sub__(self, Posit16 other):
424 return self.sub(other)
425
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)
429
430 def __mul__(self, Posit16 other):
431 return self.mul(other)
432
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)
436
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)
440
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)
444
445 def __truediv__(self, Posit16 other):
446 return self.div(other)
447
448 cpdef Posit16 sqrt(self):
449 cdef cposit.posit16_t f = cposit.p16_sqrt(self._c_posit)
450 return Posit16.from_c_posit(f)
451
452 # in-place arithmetic
453
454 cpdef void iround(self):
455 self._c_posit = cposit.p16_roundToInt(self._c_posit)
456
457 cpdef void iadd(self, Posit16 other):
458 self._c_posit = cposit.p16_add(self._c_posit, other._c_posit)
459
460 def __iadd__(self, Posit16 other):
461 self.iadd(other)
462 return self
463
464 cpdef void isub(self, Posit16 other):
465 self._c_posit = cposit.p16_sub(self._c_posit, other._c_posit)
466
467 def __isub__(self, Posit16 other):
468 self.isub(other)
469 return self
470
471 cpdef void imul(self, Posit16 other):
472 self._c_posit = cposit.p16_mul(self._c_posit, other._c_posit)
473
474 def __imul__(self, Posit16 other):
475 self.imul(other)
476 return self
477
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)
480
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)
483
484 cpdef void idiv(self, Posit16 other):
485 self._c_posit = cposit.p16_div(self._c_posit, other._c_posit)
486
487 def __itruediv__(self, Posit16 other):
488 self.idiv(other)
489 return self
490
491 cpdef void isqrt(self):
492 self._c_posit = cposit.p16_sqrt(self._c_posit)
493
494 # comparison
495
496 cpdef bint eq(self, Posit16 other):
497 return cposit.p16_eq(self._c_posit, other._c_posit)
498
499 cpdef bint le(self, Posit16 other):
500 return cposit.p16_le(self._c_posit, other._c_posit)
501
502 cpdef bint lt(self, Posit16 other):
503 return cposit.p16_lt(self._c_posit, other._c_posit)
504
505 def __lt__(self, Posit16 other):
506 return self.lt(other)
507
508 def __le__(self, Posit16 other):
509 return self.le(other)
510
511 def __eq__(self, Posit16 other):
512 return self.eq(other)
513
514 def __ne__(self, Posit16 other):
515 return not self.eq(other)
516
517 def __ge__(self, Posit16 other):
518 return other.le(self)
519
520 def __gt__(self, Posit16 other):
521 return other.lt(self)
522
523 # conversion to other posit types
524
525 cpdef to_p8(self):
526 cdef cposit.posit8_t f = cposit.p16_to_p8(self._c_posit)
527 return Posit8.from_c_posit(f)
528
529 cpdef to_quire(self):
530 cdef cposit.quire16_t f
531 cposit.q16_clr(f)
532 f = cposit.q16_fdp_add(f, self._c_posit, posit16_one)
533 return Quire16.from_c_quire(f)
534
535
536 cdef class Quire16:
537
538 # the wrapped quire value
539 cdef cposit.quire16_t _c_quire
540
541 # factory function constructors that bypass init
542
543 @staticmethod
544 cdef Quire16 from_c_quire(cposit.quire16_t f):
545 """Factory function to create a Quire16 object directly from
546 a C quire16_t.
547 """
548 cdef Quire16 obj = Quire16.__new__(Quire16)
549 obj._c_quire = f
550 return obj
551
552 @staticmethod
553 def from_bits(value):
554 """Factory function to create a Quire16 object from a bit pattern
555 represented as an integer.
556 """
557 cdef Quire16 obj = Quire16.__new__(Quire16)
558
559 if not isinstance(value, int):
560 raise TypeError('expecting int, got {}'.format(repr(value)))
561
562 for idx in range(1, -1, -1):
563 obj._c_quire.v[idx] = value & 0xffffffffffffffff
564 value >>= 64
565
566 if not (value == 0):
567 raise OverflowError('value too large to fit in uint64_t[2]')
568
569 return obj
570
571 # convenience interface for use inside Python
572
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
577 value >>= 64
578 if not (value == 0):
579 raise OverflowError('value too large to fit in uint64_t[2]')
580 else:
581 f = float(value)
582 cposit.q16_clr(self._c_quire)
583 self._c_quire = cposit.q16_fdp_add(self._c_quire, cposit.convertDoubleToP16(f), posit16_one)
584
585 def __float__(self):
586 return cposit.convertP16ToDouble(cposit.q16_to_p16(self._c_quire))
587
588 def __int__(self):
589 return int(cposit.convertP16ToDouble(cposit.q16_to_p16(self._c_quire)))
590
591 def __str__(self):
592 return repr(cposit.convertP16ToDouble(cposit.q16_to_p16(self._c_quire)))
593
594 def __repr__(self):
595 return 'Quire16(' + repr(cposit.convertP16ToDouble(cposit.q16_to_p16(self._c_quire))) + ')'
596
597 def get_bits(self):
598 b = 0
599 for u in self._c_quire.v:
600 b <<= 64
601 b |= u
602 return b
603 bits = property(get_bits)
604
605 # arithmetic
606
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)
610
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)
614
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)
617
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)
620
621 cpdef void iclr(self):
622 cposit.q16_clr(self._c_quire)
623
624 # conversion back to posit
625
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)
629
630
631 # external, non-method arithmetic
632
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)
636
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)
640
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)
644
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)
648
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)
652
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)
656
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)
660
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)
664
665 cpdef bint p16_eq(Posit16 a1, Posit16 a2):
666 return cposit.p16_eq(a1._c_posit, a2._c_posit)
667
668 cpdef bint p16_le(Posit16 a1, Posit16 a2):
669 return cposit.p16_le(a1._c_posit, a2._c_posit)
670
671 cpdef bint p16_lt(Posit16 a1, Posit16 a2):
672 return cposit.p16_lt(a1._c_posit, a2._c_posit)
673
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)
677
678 cpdef Quire16 p16_to_q16(Posit16 a1):
679 cdef cposit.quire16_t f
680 cposit.q16_clr(f)
681 f = cposit.q16_fdp_add(f, a1._c_posit, posit16_one)
682 return Quire16.from_c_quire(f)
683
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)
687
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)
691
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)