… | |
… | |
98 | #define ECB_GCC_VERSION(major,minor) (__GNUC__ > (major) || (__GNUC__ == (major) && __GNUC_MINOR__ >= (minor))) |
98 | #define ECB_GCC_VERSION(major,minor) (__GNUC__ > (major) || (__GNUC__ == (major) && __GNUC_MINOR__ >= (minor))) |
99 | #endif |
99 | #endif |
100 | |
100 | |
101 | #define ECB_CLANG_VERSION(major,minor) (__clang_major__ > (major) || (__clang_major__ == (major) && __clang_minor__ >= (minor))) |
101 | #define ECB_CLANG_VERSION(major,minor) (__clang_major__ > (major) || (__clang_major__ == (major) && __clang_minor__ >= (minor))) |
102 | |
102 | |
103 | #if __clang__ && defined(__has_builtin) |
103 | #if __clang__ && defined __has_builtin |
104 | #define ECB_CLANG_BUILTIN(x) __has_builtin(x) |
104 | #define ECB_CLANG_BUILTIN(x) __has_builtin (x) |
105 | #else |
105 | #else |
106 | #define ECB_CLANG_BUILTIN(x) 0 |
106 | #define ECB_CLANG_BUILTIN(x) 0 |
107 | #endif |
107 | #endif |
108 | |
108 | |
109 | #if __clang__ && defined(__has_extension) |
109 | #if __clang__ && defined __has_extension |
110 | #define ECB_CLANG_EXTENSION(x) __has_extension(x) |
110 | #define ECB_CLANG_EXTENSION(x) __has_extension (x) |
111 | #else |
111 | #else |
112 | #define ECB_CLANG_EXTENSION(x) 0 |
112 | #define ECB_CLANG_EXTENSION(x) 0 |
113 | #endif |
113 | #endif |
114 | |
114 | |
115 | #define ECB_CPP (__cplusplus+0) |
115 | #define ECB_CPP (__cplusplus+0) |
… | |
… | |
278 | #define ECB_MEMORY_FENCE_RELEASE ECB_MEMORY_FENCE |
278 | #define ECB_MEMORY_FENCE_RELEASE ECB_MEMORY_FENCE |
279 | #endif |
279 | #endif |
280 | |
280 | |
281 | /*****************************************************************************/ |
281 | /*****************************************************************************/ |
282 | |
282 | |
283 | #if __cplusplus |
283 | #if ECB_CPP |
284 | #define ecb_inline static inline |
284 | #define ecb_inline static inline |
285 | #elif ECB_GCC_VERSION(2,5) |
285 | #elif ECB_GCC_VERSION(2,5) |
286 | #define ecb_inline static __inline__ |
286 | #define ecb_inline static __inline__ |
287 | #elif ECB_C99 |
287 | #elif ECB_C99 |
288 | #define ecb_inline static inline |
288 | #define ecb_inline static inline |
… | |
… | |
334 | #else |
334 | #else |
335 | #define ecb_prefetch(addr,rw,locality) |
335 | #define ecb_prefetch(addr,rw,locality) |
336 | #endif |
336 | #endif |
337 | |
337 | |
338 | /* no emulation for ecb_decltype */ |
338 | /* no emulation for ecb_decltype */ |
339 | #if ECB_GCC_VERSION(4,5) |
339 | #if ECB_CPP11 |
|
|
340 | // older implementations might have problems with decltype(x)::type, work around it |
|
|
341 | template<class T> struct ecb_decltype_t { typedef T type; }; |
340 | #define ecb_decltype(x) __decltype (x) |
342 | #define ecb_decltype(x) ecb_decltype_t<decltype (x)>::type |
341 | #elif ECB_GCC_VERSION(3,0) |
343 | #elif ECB_GCC_VERSION(3,0) || ECB_CLANG_VERSION(2,8) |
342 | #define ecb_decltype(x) __typeof (x) |
344 | #define ecb_decltype(x) __typeof__ (x) |
343 | #elif ECB_CPP11 |
|
|
344 | #define ecb_decltype(x) decltype (x) |
|
|
345 | #endif |
345 | #endif |
346 | |
346 | |
347 | #if _MSC_VER >= 1300 |
347 | #if _MSC_VER >= 1300 |
348 | #define ecb_deprecated __declspec(deprecated) |
348 | #define ecb_deprecated __declspec (deprecated) |
349 | #else |
349 | #else |
350 | #define ecb_deprecated ecb_attribute ((__deprecated__)) |
350 | #define ecb_deprecated ecb_attribute ((__deprecated__)) |
351 | #endif |
351 | #endif |
352 | |
352 | |
353 | #define ecb_noinline ecb_attribute ((__noinline__)) |
353 | #define ecb_noinline ecb_attribute ((__noinline__)) |
354 | #define ecb_unused ecb_attribute ((__unused__)) |
354 | #define ecb_unused ecb_attribute ((__unused__)) |
355 | #define ecb_const ecb_attribute ((__const__)) |
355 | #define ecb_const ecb_attribute ((__const__)) |
356 | #define ecb_pure ecb_attribute ((__pure__)) |
356 | #define ecb_pure ecb_attribute ((__pure__)) |
357 | |
357 | |
358 | /* http://msdn.microsoft.com/en-us/library/k6ktzx3s.aspx __declspec(noreturn) */ |
358 | /* TODO http://msdn.microsoft.com/en-us/library/k6ktzx3s.aspx __declspec(noreturn) */ |
359 | #if ECB_C11 |
359 | #if ECB_C11 || __IBMC_NORETURN |
|
|
360 | /* http://pic.dhe.ibm.com/infocenter/compbg/v121v141/topic/com.ibm.xlcpp121.bg.doc/language_ref/noreturn.html */ |
360 | #define ecb_noreturn _Noreturn |
361 | #define ecb_noreturn _Noreturn |
361 | #else |
362 | #else |
362 | #define ecb_noreturn ecb_attribute ((__noreturn__)) |
363 | #define ecb_noreturn ecb_attribute ((__noreturn__)) |
363 | #endif |
364 | #endif |
364 | |
365 | |
… | |
… | |
599 | #define ecb_mod(m,n) ((m) % (n) + ((m) % (n) < 0 ? (n) : 0)) |
600 | #define ecb_mod(m,n) ((m) % (n) + ((m) % (n) < 0 ? (n) : 0)) |
600 | #else |
601 | #else |
601 | #define ecb_mod(m,n) ((m) < 0 ? ((n) - 1 - ((-1 - (m)) % (n))) : ((m) % (n))) |
602 | #define ecb_mod(m,n) ((m) < 0 ? ((n) - 1 - ((-1 - (m)) % (n))) : ((m) % (n))) |
602 | #endif |
603 | #endif |
603 | |
604 | |
604 | #if __cplusplus |
605 | #if ECB_CPP |
605 | template<typename T> |
606 | template<typename T> |
606 | static inline T ecb_div_rd (T val, T div) |
607 | static inline T ecb_div_rd (T val, T div) |
607 | { |
608 | { |
608 | return val < 0 ? - ((-val + div - 1) / div) : (val ) / div; |
609 | return val < 0 ? - ((-val + div - 1) / div) : (val ) / div; |
609 | } |
610 | } |
… | |
… | |
669 | #define ECB_NAN NAN |
670 | #define ECB_NAN NAN |
670 | #else |
671 | #else |
671 | #define ECB_NAN ECB_INFINITY |
672 | #define ECB_NAN ECB_INFINITY |
672 | #endif |
673 | #endif |
673 | |
674 | |
|
|
675 | #if ECB_C99 || _XOPEN_VERSION >= 600 || _POSIX_VERSION >= 200112L |
|
|
676 | #define ecb_ldexpf(x,e) ldexpf ((x), (e)) |
|
|
677 | #else |
|
|
678 | #define ecb_ldexpf(x,e) (float) ldexp ((x), (e)) |
|
|
679 | #endif |
|
|
680 | |
674 | /* converts an ieee half/binary16 to a float */ |
681 | /* converts an ieee half/binary16 to a float */ |
675 | ecb_function_ float ecb_binary16_to_float (uint16_t x) ecb_const; |
682 | ecb_function_ float ecb_binary16_to_float (uint16_t x) ecb_const; |
676 | ecb_function_ float |
683 | ecb_function_ float |
677 | ecb_binary16_to_float (uint16_t x) |
684 | ecb_binary16_to_float (uint16_t x) |
678 | { |
685 | { |
679 | int e = (x >> 10) & 0x1f; |
686 | int e = (x >> 10) & 0x1f; |
680 | int m = x & 0x3ff; |
687 | int m = x & 0x3ff; |
681 | float r; |
688 | float r; |
682 | |
689 | |
683 | if (!e ) r = ldexpf (m , -24); |
690 | if (!e ) r = ecb_ldexpf (m , -24); |
684 | else if (e != 31) r = ldexpf (m + 0x400, e - 25); |
691 | else if (e != 31) r = ecb_ldexpf (m + 0x400, e - 25); |
685 | else if (m ) r = ECB_NAN; |
692 | else if (m ) r = ECB_NAN; |
686 | else r = ECB_INFINITY; |
693 | else r = ECB_INFINITY; |
687 | |
694 | |
688 | return x & 0x8000 ? -r : r; |
695 | return x & 0x8000 ? -r : r; |
689 | } |
696 | } |
… | |
… | |
748 | x |= 0x800000U; |
755 | x |= 0x800000U; |
749 | else |
756 | else |
750 | e = 1; |
757 | e = 1; |
751 | |
758 | |
752 | /* we distrust ldexpf a bit and do the 2**-24 scaling by an extra multiply */ |
759 | /* we distrust ldexpf a bit and do the 2**-24 scaling by an extra multiply */ |
753 | r = ldexpf (x * (0.5f / 0x800000U), e - 126); |
760 | r = ecb_ldexpf (x * (0.5f / 0x800000U), e - 126); |
754 | |
761 | |
755 | r = neg ? -r : r; |
762 | r = neg ? -r : r; |
756 | #endif |
763 | #endif |
757 | |
764 | |
758 | return r; |
765 | return r; |