--- CBOR-XS/ecb.h 2013/10/25 23:09:45 1.1 +++ CBOR-XS/ecb.h 2013/11/17 05:26:14 1.4 @@ -1,7 +1,7 @@ /* * libecb - http://software.schmorp.de/pkg/libecb * - * Copyright (©) 2009-2012 Marc Alexander Lehmann + * Copyright (©) 2009-2013 Marc Alexander Lehmann * Copyright (©) 2011 Emanuele Giaquinta * All rights reserved. * @@ -583,11 +583,42 @@ #include /* for memcpy */ #else #define ECB_STDFP 0 - #include /* for frexp*, ldexp* */ #endif #ifndef ECB_NO_LIBM + #include /* for frexp*, ldexp*, INFINITY, NAN */ + + /* only the oldest of old doesn't have this one. solaris. */ + #ifdef INFINITY + #define ECB_INFINITY INFINITY + #else + #define ECB_INFINITY HUGE_VAL + #endif + + #ifdef NAN + #define ECB_NAN NAN + #else + #define ECB_NAN ECB_INFINITY + #endif + + /* converts an ieee half/binary16 to a float */ + ecb_function_ float ecb_binary16_to_float (uint16_t x) ecb_const; + ecb_function_ float + ecb_binary16_to_float (uint16_t x) + { + int e = (x >> 10) & 0x1f; + int m = x & 0x3ff; + float r; + + if (!e ) r = ldexpf (m , -24); + else if (e != 31) r = ldexpf (m + 0x400, e - 25); + else if (m ) r = ECB_NAN; + else r = ECB_INFINITY; + + return x & 0x8000 ? -r : r; + } + /* convert a float to ieee single/binary32 */ ecb_function_ uint32_t ecb_float_to_binary32 (float x) ecb_const; ecb_function_ uint32_t