67#define INT16_TYPE int16_t
68#define UINT16_TYPE uint16_t
69#define INT32_TYPE int32_t
70#define UINT32_TYPE uint32_t
75int doubles2halfp(
void *target,
void *source,
int numel);
77int halfp2doubles(
void *target,
void *source,
int numel);
96 UINT16_TYPE *hp = (UINT16_TYPE *) target;
97 UINT32_TYPE *xp = (UINT32_TYPE *) source;
98 UINT16_TYPE hs, he, hm;
99 UINT32_TYPE x, xs, xe, xm;
102 static int checkieee = 1;
107 ip = (UINT32_TYPE *) &one;
114 if( *ip != 0x3FF00000u ) {
117 if(
sizeof(INT16_TYPE) != 2 ||
sizeof(INT32_TYPE) != 4 ) {
123 if( source == NULL || target == NULL ) {
129 if( (x & 0x7FFFFFFFu) == 0 ) {
130 *hp++ = (UINT16_TYPE) (x >> 16);
132 xs = x & 0x80000000u;
133 xe = x & 0x7F800000u;
134 xm = x & 0x007FFFFFu;
136 *hp++ = (UINT16_TYPE) (xs >> 16);
137 }
else if( xe == 0x7F800000u ) {
139 *hp++ = (UINT16_TYPE) ((xs >> 16) | 0x7C00u);
141 *hp++ = (UINT16_TYPE) (x >> 16);
144 hs = (UINT16_TYPE) (xs >> 16);
145 hes = ((int)(xe >> 23)) - 127 + 15;
147 *hp++ = (UINT16_TYPE) ((xs >> 16) | 0x7C00u);
148 }
else if( hes <= 0 ) {
149 if( (14 - hes) > 24 ) {
150 hm = (UINT16_TYPE) 0u;
153 hm = (UINT16_TYPE) (xm >> (14 - hes));
154 if( (xm >> (13 - hes)) & 0x00000001u )
155 hm += (UINT16_TYPE) 1u;
159 he = (UINT16_TYPE) (hes << 10);
160 hm = (UINT16_TYPE) (xm >> 13);
161 if( xm & 0x00001000u )
162 *hp++ = (hs | he | hm) + (UINT16_TYPE) 1u;
164 *hp++ = (hs | he | hm);
187int doubles2halfp(
void *target,
void *source,
int numel)
189 UINT16_TYPE *hp = (UINT16_TYPE *) target;
190 UINT32_TYPE *xp = (UINT32_TYPE *) source;
191 UINT16_TYPE hs, he, hm;
192 UINT32_TYPE x, xs, xe, xm;
195 static int checkieee = 1;
200 ip = (UINT32_TYPE *) &one;
207 if( *ip != 0x3FF00000u ) {
210 if(
sizeof(INT16_TYPE) != 2 ||
sizeof(INT32_TYPE) != 4 ) {
218 if( source == NULL || target == NULL ) {
224 if( (x & 0x7FFFFFFFu) == 0 ) {
225 *hp++ = (UINT16_TYPE) (x >> 16);
227 xs = x & 0x80000000u;
228 xe = x & 0x7FF00000u;
229 xm = x & 0x000FFFFFu;
231 *hp++ = (UINT16_TYPE) (xs >> 16);
232 }
else if( xe == 0x7FF00000u ) {
234 *hp++ = (UINT16_TYPE) ((xs >> 16) | 0x7C00u);
236 *hp++ = (UINT16_TYPE) 0xFE00u;
239 hs = (UINT16_TYPE) (xs >> 16);
240 hes = ((int)(xe >> 20)) - 1023 + 15;
242 *hp++ = (UINT16_TYPE) ((xs >> 16) | 0x7C00u);
243 }
else if( hes <= 0 ) {
244 if( (10 - hes) > 21 ) {
245 hm = (UINT16_TYPE) 0u;
248 hm = (UINT16_TYPE) (xm >> (11 - hes));
249 if( (xm >> (10 - hes)) & 0x00000001u )
250 hm += (UINT16_TYPE) 1u;
254 he = (UINT16_TYPE) (hes << 10);
255 hm = (UINT16_TYPE) (xm >> 10);
256 if( xm & 0x00000200u )
257 *hp++ = (hs | he | hm) + (UINT16_TYPE) 1u;
259 *hp++ = (hs | he | hm);
284 UINT16_TYPE *hp = (UINT16_TYPE *) source;
285 UINT32_TYPE *xp = (UINT32_TYPE *) target;
286 UINT16_TYPE h, hs, he, hm;
287 UINT32_TYPE xs, xe, xm;
291 static int checkieee = 1;
296 ip = (UINT32_TYPE *) &one;
303 if( *ip != 0x3FF00000u ) {
306 if(
sizeof(INT16_TYPE) != 2 ||
sizeof(INT32_TYPE) != 4 ) {
312 if( source == NULL || target == NULL )
317 if( (h & 0x7FFFu) == 0 ) {
318 *xp++ = ((UINT32_TYPE) h) << 16;
328 }
while( (hm & 0x0400u) == 0 );
329 xs = ((UINT32_TYPE) hs) << 16;
330 xes = ((INT32_TYPE) (he >> 10)) - 15 + 127 - e;
331 xe = (UINT32_TYPE) (xes << 23);
332 xm = ((UINT32_TYPE) (hm & 0x03FFu)) << 13;
333 *xp++ = (xs | xe | xm);
334 }
else if( he == 0x7C00u ) {
336 *xp++ = (((UINT32_TYPE) hs) << 16) | ((UINT32_TYPE) 0x7F800000u);
338 *xp++ = ((UINT32_TYPE) h) << 16;
341 xs = ((UINT32_TYPE) hs) << 16;
342 xes = ((INT32_TYPE) (he >> 10)) - 15 + 127;
343 xe = (UINT32_TYPE) (xes << 23);
344 xm = ((UINT32_TYPE) hm) << 13;
345 *xp++ = (xs | xe | xm);
367int halfp2doubles(
void *target,
void *source,
int numel)
369 UINT16_TYPE *hp = (UINT16_TYPE *) source;
370 UINT32_TYPE *xp = (UINT32_TYPE *) target;
371 UINT16_TYPE h, hs, he, hm;
372 UINT32_TYPE xs, xe, xm;
376 static int checkieee = 1;
381 ip = (UINT32_TYPE *) &one;
388 if( *ip != 0x3FF00000u ) {
391 if(
sizeof(INT16_TYPE) != 2 ||
sizeof(INT32_TYPE) != 4 ) {
399 if( source == NULL || target == NULL )
404 if( (h & 0x7FFFu) == 0 ) {
405 *xp++ = ((UINT32_TYPE) h) << 16;
415 }
while( (hm & 0x0400u) == 0 );
416 xs = ((UINT32_TYPE) hs) << 16;
417 xes = ((INT32_TYPE) (he >> 10)) - 15 + 1023 - e;
418 xe = (UINT32_TYPE) (xes << 20);
419 xm = ((UINT32_TYPE) (hm & 0x03FFu)) << 10;
420 *xp++ = (xs | xe | xm);
421 }
else if( he == 0x7C00u ) {
423 *xp++ = (((UINT32_TYPE) hs) << 16) | ((UINT32_TYPE) 0x7FF00000u);
425 *xp++ = (UINT32_TYPE) 0xFFF80000u;
428 xs = ((UINT32_TYPE) hs) << 16;
429 xes = ((INT32_TYPE) (he >> 10)) - 15 + 1023;
430 xe = (UINT32_TYPE) (xes << 20);
431 xm = ((UINT32_TYPE) hm) << 10;
432 *xp++ = (xs | xe | xm);
int singles2halfp(void *target, const void *source, int numel)
Converts an IEEE single (float) to a half-precision (float16).
int halfp2singles(void *target, const void *source, int numel)
Converts an half-precision (float16) to an IEEE single (float).