--- libecb/ecb.h 2012/06/28 19:38:29 1.104 +++ libecb/ecb.h 2012/10/08 15:43:13 1.113 @@ -31,7 +31,7 @@ #define ECB_H /* 16 bits major, 16 bits minor */ -#define ECB_VERSION 0x00010001 +#define ECB_VERSION 0x00010002 #ifdef _WIN32 typedef signed char int8_t; @@ -150,9 +150,16 @@ #if ECB_GCC_VERSION(4,7) /* see comment below (stdatomic.h) about the C11 memory model. */ #define ECB_MEMORY_FENCE __atomic_thread_fence (__ATOMIC_SEQ_CST) - /*#elif defined __clang && __has_feature (cxx_atomic)*/ - /* see comment below (stdatomic.h) about the C11 memory model. */ - /*#define ECB_MEMORY_FENCE __c11_atomic_thread_fence (__ATOMIC_SEQ_CST)*/ + + /* The __has_feature syntax from clang is so misdesigned that we cannot use it + * without risking compile time errors with other compilers. We *could* + * define our own ecb_clang_has_feature, but I just can't be bothered to work + * around this shit time and again. + * #elif defined __clang && __has_feature (cxx_atomic) + * // see comment below (stdatomic.h) about the C11 memory model. + * #define ECB_MEMORY_FENCE __c11_atomic_thread_fence (__ATOMIC_SEQ_CST) + */ + #elif ECB_GCC_VERSION(4,4) || defined __INTEL_COMPILER || defined __clang__ #define ECB_MEMORY_FENCE __sync_synchronize () #elif _MSC_VER >= 1400 /* VC++ 2005 */ @@ -477,17 +484,17 @@ ecb_byteorder_helper (void) { /* the union code still generates code under pressure in gcc, */ - /* but less than using pointers, and always seem to */ + /* but less than using pointers, and always seems to */ /* successfully return a constant. */ /* the reason why we have this horrible preprocessor mess */ /* is to avoid it in all cases, at least on common architectures */ - /* and yes, gcc defines __BYTE_ORDER__, g++ does not */ + /* or when using a recent enough gcc version (>= 4.6) */ #if __i386 || __i386__ || _M_X86 || __amd64 || __amd64__ || _M_X64 return 0x44; #elif __BYTE_ORDER__ && __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ return 0x44; #elif __BYTE_ORDER__ && __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__ - retrurn 0x11; + return 0x11; #else union { @@ -556,29 +563,11 @@ #include /* for memcpy */ #else #define ECB_STDFP 0 + #include /* for frexp*, ldexp* */ #endif #ifndef ECB_NO_LIBM - #if __STDC_IEC_559__ || ECB_STDFP - /* we assume this is defined for most C and many C++ compilers */ - ecb_inline ecb_bool ecb_float_ieee (void) ecb_const; - ecb_inline ecb_bool ecb_float_ieee (void) { return 1; } - ecb_inline ecb_bool ecb_double_ieee (void) ecb_const; - ecb_inline ecb_bool ecb_double_ieee (void) { return 1; } - #elif ECB_CPP - #include - ecb_inline ecb_bool ecb_float_ieee (void) ecb_const; - ecb_inline ecb_bool ecb_float_ieee (void) { return std::numeric_limits::is_iec559; } - ecb_inline ecb_bool ecb_double_ieee (void) ecb_const; - ecb_inline ecb_bool ecb_double_ieee (void) { return std::numeric_limits::is_iec559; } - #else - ecb_inline ecb_bool ecb_float_ieee (void) ecb_const; - ecb_inline ecb_bool ecb_float_ieee (void) { return 0; } - ecb_inline ecb_bool ecb_double_ieee (void) ecb_const; - ecb_inline ecb_bool ecb_double_ieee (void) { return 0; } - #endif - /* convert a float to ieee single/binary32 */ ecb_function_ uint32_t ecb_float_to_binary32 (float x) ecb_const; ecb_function_ uint32_t @@ -589,14 +578,14 @@ #if ECB_STDFP memcpy (&r, &x, 4); #else - /* slow emulation, works for anything but nan's and -0 */ - ECB_EXTERN_C float frexpf (float v, int *e); + /* slow emulation, works for anything but -0 */ uint32_t m; int e; - if (x == 0e0f ) return 0; + if (x == 0e0f ) return 0x00000000U; if (x > +3.40282346638528860e+38f) return 0x7f800000U; if (x < -3.40282346638528860e+38f) return 0xff800000U; + if (x != x ) return 0x7fbfffffU; m = frexpf (x, &e) * 0x1000000U; @@ -605,7 +594,7 @@ if (r) m = -m; - if (e < -125) + if (e <= -126) { m &= 0xffffffU; m >>= (-125 - e); @@ -630,8 +619,6 @@ memcpy (&r, &x, 4); #else /* emulation, only works for normals and subnormals and +0 */ - ECB_EXTERN_C float ldexpf (float x, int e); - int neg = x >> 31; int e = (x >> 23) & 0xffU; @@ -643,7 +630,7 @@ e = 1; /* we distrust ldexpf a bit and do the 2**-24 scaling by an extra multiply */ - r = ldexpf (x * (1.f / 0x1000000U), e - 126); + r = ldexpf (x * (0.5f / 0x800000U), e - 126); r = neg ? -r : r; #endif @@ -661,16 +648,15 @@ #if ECB_STDFP memcpy (&r, &x, 8); #else - /* slow emulation, works for anything but nan's and -0 */ - ECB_EXTERN_C double frexp (double v, int *e); + /* slow emulation, works for anything but -0 */ uint64_t m; int e; - if (x == 0e0 ) return 0; + if (x == 0e0 ) return 0x0000000000000000U; if (x > +1.79769313486231470e+308) return 0x7ff0000000000000U; if (x < -1.79769313486231470e+308) return 0xfff0000000000000U; + if (x != x ) return 0X7ff7ffffffffffffU; - m = frexpf (x, &e) * 0x1000000U; m = frexp (x, &e) * 0x20000000000000U; r = m & 0x8000000000000000;; @@ -678,7 +664,7 @@ if (r) m = -m; - if (e < -1021) + if (e <= -1022) { m &= 0x1fffffffffffffU; m >>= (-1021 - e); @@ -703,8 +689,6 @@ memcpy (&r, &x, 8); #else /* emulation, only works for normals and subnormals and +0 */ - ECB_EXTERN_C double ldexp (double x, int e); - int neg = x >> 63; int e = (x >> 52) & 0x7ffU; @@ -715,8 +699,8 @@ else e = 1; - /* we distrust ldexpf a bit and do the 2**-53 scaling by an extra multiply */ - r = ldexp (x * (1. / 0x20000000000000U), e - 1022); + /* we distrust ldexp a bit and do the 2**-53 scaling by an extra multiply */ + r = ldexp (x * (0.5 / 0x10000000000000U), e - 1022); r = neg ? -r : r; #endif