update posit interface
[sfpy.git] / sfpy / cposit.pxd
1 from libc.stdint cimport *
2
3 # As far as I can tell, this is never enabled when the library is comiled...
4 # The option will have to be synced manually, as by the time Cython's c is
5 # compiled the library will already be stored as a .a file and any compile-time
6 # options used to build it will be long gone.
7 DEF SOFTPOSIT_EXACT = 0
8
9 cdef extern from '../SoftPosit/source/include/softposit.h':
10
11 # Transparent types so we can have access to the raw bits.
12
13 IF SOFTPOSIT_EXACT:
14
15 ctypedef struct posit8_t:
16 uint8_t v;
17 bint exact;
18
19 ctypedef struct quire8_t:
20 uint32_t v;
21 bint exact;
22
23 ctypedef struct posit16_t:
24 uint16_t v;
25 bint exact;
26
27 ctypedef struct quire16_t:
28 uint64_t v[2];
29 bint exact;
30
31 ctypedef struct posit32_t:
32 uint32_t v;
33 bint exact;
34
35 ctypedef struct quire32_t:
36 uint64_t v[8];
37 bint exact;
38
39 ELSE:
40
41 ctypedef struct posit8_t:
42 uint8_t v;
43
44 ctypedef struct posit16_t:
45 uint16_t v;
46
47 ctypedef struct posit32_t:
48 uint32_t v;
49
50 ctypedef struct quire8_t:
51 uint32_t v;
52
53 ctypedef struct quire16_t:
54 uint64_t v[2];
55
56 ctypedef struct quire32_t:
57 uint64_t v[8];
58
59 # /*----------------------------------------------------------------------------
60 # | Integer-to-posit conversion routines.
61 # *----------------------------------------------------------------------------*/
62
63 posit8_t ui32_to_p8( uint32_t );
64 posit16_t ui32_to_p16( uint32_t );
65 # posit32_t ui32_to_p32( uint32_t );
66 # posit64_t ui32_to_p64( uint32_t );
67
68 posit8_t ui64_to_p8( uint64_t );
69 posit16_t ui64_to_p16( uint64_t );
70 # posit32_t ui64_to_p32( uint64_t );
71 # posit64_t ui64_to_p64( uint64_t );
72
73 posit8_t i32_to_p8( int32_t );
74 posit16_t i32_to_p16( int32_t );
75 # posit32_t i32_to_p32( int32_t );
76 # posit64_t i32_to_p64( int32_t );
77
78 posit8_t i64_to_p8( int64_t );
79 posit16_t i64_to_p16( int64_t );
80 # posit32_t i64_to_p32( int64_t );
81 # posit64_t i64_to_p64( int64_t );
82
83 # /*----------------------------------------------------------------------------
84 # | 8-bit (quad-precision) posit operations.
85 # *----------------------------------------------------------------------------*/
86
87 bint isNaRP8UI( uint8_t );
88
89 uint_fast32_t p8_to_ui32( posit8_t );
90 uint_fast64_t p8_to_ui64( posit8_t );
91 int_fast32_t p8_to_i32( posit8_t );
92 int_fast64_t p8_to_i64( posit8_t );
93
94 posit16_t p8_to_p16( posit8_t );
95 # posit32_t p8_to_p32( posit8_t );
96 # posit64_t p8_to_p64( posit8_t );
97
98 posit8_t p8_roundToInt( posit8_t );
99 posit8_t p8_add( posit8_t, posit8_t );
100 posit8_t p8_sub( posit8_t, posit8_t );
101 posit8_t p8_mul( posit8_t, posit8_t );
102 posit8_t p8_mulAdd( posit8_t, posit8_t, posit8_t );
103 posit8_t p8_div( posit8_t, posit8_t );
104 posit8_t p8_sqrt( posit8_t );
105 bint p8_eq( posit8_t, posit8_t );
106 bint p8_le( posit8_t, posit8_t );
107 bint p8_lt( posit8_t, posit8_t );
108
109 # Quire 8
110 quire8_t q8_fdp_add( quire8_t, posit8_t, posit8_t );
111 quire8_t q8_fdp_sub( quire8_t, posit8_t, posit8_t );
112 posit8_t q8_to_p8( quire8_t );
113
114 bint isNaRQ8( quire8_t );
115 bint isQ8Zero( quire8_t );
116 quire8_t q8_clr( quire8_t );
117 quire8_t castQ8( uint32_t );
118 posit8_t castP8( uint8_t );
119 uint8_t castUI8( posit8_t );
120 posit8_t negP8( posit8_t );
121
122 # Helper
123 double convertP8ToDouble( posit8_t );
124 posit8_t convertDoubleToP8( double );
125
126 # /*----------------------------------------------------------------------------
127 # | 16-bit (half-precision) posit operations.
128 # *----------------------------------------------------------------------------*/
129
130 bint isNaRP16UI( uint16_t );
131
132 uint_fast32_t p16_to_ui32( posit16_t );
133 uint_fast64_t p16_to_ui64( posit16_t );
134 int_fast32_t p16_to_i32( posit16_t);
135 int_fast64_t p16_to_i64( posit16_t );
136 posit8_t p16_to_p8( posit16_t );
137 # posit32_t p16_to_p32( posit16_t );
138 # posit64_t p16_to_p64( posit16_t );
139
140 posit16_t p16_roundToInt( posit16_t);
141 posit16_t p16_add( posit16_t, posit16_t );
142 posit16_t p16_sub( posit16_t, posit16_t );
143 posit16_t p16_mul( posit16_t, posit16_t );
144 posit16_t p16_mulAdd( posit16_t, posit16_t, posit16_t );
145 posit16_t p16_div( posit16_t, posit16_t );
146 posit16_t p16_sqrt( posit16_t );
147 bint p16_eq( posit16_t, posit16_t );
148 bint p16_le( posit16_t, posit16_t );
149 bint p16_lt( posit16_t, posit16_t );
150
151 # Quire 16
152 quire16_t q16_fdp_add( quire16_t, posit16_t, posit16_t );
153 quire16_t q16_fdp_sub( quire16_t, posit16_t, posit16_t );
154 # posit16_t convertQ16ToP16( quire16_t );
155 posit16_t q16_to_p16( quire16_t );
156
157 bint isNaRQ16( quire16_t );
158 bint isQ16Zero( quire16_t );
159 # quire16_t q16_TwosComplement( quire16_t );
160
161 # void printBinary( uint64_t*, int );
162 # void printHex( uint64_t );
163
164 quire16_t q16_clr( quire16_t );
165 quire16_t castQ16( uint16_t, uint16_t );
166 posit16_t castP16( uint16_t );
167 uint16_t castUI16( posit16_t );
168 posit16_t negP16( posit16_t );
169
170 # Helper
171 double convertP16ToDouble( posit16_t );
172 # posit16_t convertFloatToP16( float );
173 posit16_t convertDoubleToP16( double );