… | |
… | |
541 | #if ECB_NO_THREADS || ECB_NO_SMP |
541 | #if ECB_NO_THREADS || ECB_NO_SMP |
542 | #define ECB_MEMORY_FENCE do { } while (0) |
542 | #define ECB_MEMORY_FENCE do { } while (0) |
543 | #endif |
543 | #endif |
544 | |
544 | |
545 | #ifndef ECB_MEMORY_FENCE |
545 | #ifndef ECB_MEMORY_FENCE |
546 | #if ECB_GCC_VERSION(2,5) |
546 | #if ECB_GCC_VERSION(2,5) || defined(__INTEL_COMPILER) || defined(__clang__) |
547 | #if __i386__ |
547 | #if __i386__ |
548 | #define ECB_MEMORY_FENCE __asm__ __volatile__ ("lock; orb $0, -1(%%esp)" : : : "memory") |
548 | #define ECB_MEMORY_FENCE __asm__ __volatile__ ("lock; orb $0, -1(%%esp)" : : : "memory") |
549 | #define ECB_MEMORY_FENCE_ACQUIRE ECB_MEMORY_FENCE /* non-lock xchg might be enough */ |
549 | #define ECB_MEMORY_FENCE_ACQUIRE ECB_MEMORY_FENCE /* non-lock xchg might be enough */ |
550 | #define ECB_MEMORY_FENCE_RELEASE do { } while (0) /* unlikely to change in future cpus */ |
550 | #define ECB_MEMORY_FENCE_RELEASE do { } while (0) /* unlikely to change in future cpus */ |
551 | #elif __amd64 |
551 | #elif __amd64 |
… | |
… | |
563 | #endif |
563 | #endif |
564 | #endif |
564 | #endif |
565 | #endif |
565 | #endif |
566 | |
566 | |
567 | #ifndef ECB_MEMORY_FENCE |
567 | #ifndef ECB_MEMORY_FENCE |
568 | #if ECB_GCC_VERSION(4,4) || defined(__INTEL_COMPILER) |
568 | #if ECB_GCC_VERSION(4,4) || defined(__INTEL_COMPILER) || defined(__clang__) |
569 | #define ECB_MEMORY_FENCE __sync_synchronize () |
569 | #define ECB_MEMORY_FENCE __sync_synchronize () |
570 | /*#define ECB_MEMORY_FENCE_ACQUIRE ({ char dummy = 0; __sync_lock_test_and_set (&dummy, 1); }) */ |
570 | /*#define ECB_MEMORY_FENCE_ACQUIRE ({ char dummy = 0; __sync_lock_test_and_set (&dummy, 1); }) */ |
571 | /*#define ECB_MEMORY_FENCE_RELEASE ({ char dummy = 1; __sync_lock_release (&dummy ); }) */ |
571 | /*#define ECB_MEMORY_FENCE_RELEASE ({ char dummy = 1; __sync_lock_release (&dummy ); }) */ |
572 | #elif _MSC_VER >= 1400 /* VC++ 2005 */ |
572 | #elif _MSC_VER >= 1400 /* VC++ 2005 */ |
573 | #pragma intrinsic(_ReadBarrier,_WriteBarrier,_ReadWriteBarrier) |
573 | #pragma intrinsic(_ReadBarrier,_WriteBarrier,_ReadWriteBarrier) |
… | |
… | |
843 | #define ecb_mod(m,n) ((m) % (n) + ((m) % (n) < 0 ? (n) : 0)) |
843 | #define ecb_mod(m,n) ((m) % (n) + ((m) % (n) < 0 ? (n) : 0)) |
844 | #else |
844 | #else |
845 | #define ecb_mod(m,n) ((m) < 0 ? ((n) - 1 - ((-1 - (m)) % (n))) : ((m) % (n))) |
845 | #define ecb_mod(m,n) ((m) < 0 ? ((n) - 1 - ((-1 - (m)) % (n))) : ((m) % (n))) |
846 | #endif |
846 | #endif |
847 | |
847 | |
|
|
848 | #if __cplusplus |
|
|
849 | template<typename T> |
|
|
850 | static inline T ecb_div_rd (T val, T div) |
|
|
851 | { |
|
|
852 | return val < 0 ? - ((-val + div - 1) / div) : (val ) / div; |
|
|
853 | } |
|
|
854 | template<typename T> |
|
|
855 | static inline T ecb_div_ru (T val, T div) |
|
|
856 | { |
|
|
857 | return val < 0 ? - ((-val ) / div) : (val + div - 1) / div; |
|
|
858 | } |
|
|
859 | #else |
|
|
860 | #define ecb_div_rd(val,div) ((val) < 0 ? - ((-(val) + (div) - 1) / (div)) : ((val) ) / (div)) |
|
|
861 | #define ecb_div_ru(val,div) ((val) < 0 ? - ((-(val) ) / (div)) : ((val) + (div) - 1) / (div)) |
|
|
862 | #endif |
|
|
863 | |
848 | #if ecb_cplusplus_does_not_suck |
864 | #if ecb_cplusplus_does_not_suck |
849 | /* does not work for local types (http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2008/n2657.htm) */ |
865 | /* does not work for local types (http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2008/n2657.htm) */ |
850 | template<typename T, int N> |
866 | template<typename T, int N> |
851 | static inline int ecb_array_length (const T (&arr)[N]) |
867 | static inline int ecb_array_length (const T (&arr)[N]) |
852 | { |
868 | { |
… | |
… | |
859 | #endif |
875 | #endif |
860 | |
876 | |
861 | /* ECB.H END */ |
877 | /* ECB.H END */ |
862 | |
878 | |
863 | #if ECB_MEMORY_FENCE_NEEDS_PTHREADS |
879 | #if ECB_MEMORY_FENCE_NEEDS_PTHREADS |
864 | /* if your architetcure doesn't need memory fences, e.g. because it is |
880 | /* if your architecture doesn't need memory fences, e.g. because it is |
865 | * single-cpu/core, or if you use libev in a project that doesn't use libev |
881 | * single-cpu/core, or if you use libev in a project that doesn't use libev |
866 | * from multiple threads, then you can define ECB_AVOID_PTHREADS when compiling |
882 | * from multiple threads, then you can define ECB_AVOID_PTHREADS when compiling |
867 | * libev, in which casess the memory fences become nops. |
883 | * libev, in which casess the memory fences become nops. |
868 | * alternatively, you can remove this #error and link against libpthread, |
884 | * alternatively, you can remove this #error and link against libpthread, |
869 | * which will then provide the memory fences. |
885 | * which will then provide the memory fences. |