--- libecb/ecb.h 2011/07/20 00:38:30 1.54 +++ libecb/ecb.h 2011/08/24 16:07:55 1.66 @@ -50,7 +50,7 @@ /* many compilers define _GNUC_ to some versions but then only implement * what their idiot authors think are the "more important" extensions, - * causing enourmous grief in return for some better fake benchmark numbers. + * causing enormous grief in return for some better fake benchmark numbers. * or so. * we try to detect these and simply assume they are not gcc - if they have * an issue with that they should have done it right in the first place. @@ -65,42 +65,74 @@ /*****************************************************************************/ +/* ECB_NO_THREADS - ecb is not used by multiple threads, ever */ +/* ECB_NO_SMP - ecb might be used in multiple threads, but only on a single cpu */ + +#if ECB_NO_THREADS || ECB_NO_SMP + #define ECB_MEMORY_FENCE do { } while (0) +#endif + #ifndef ECB_MEMORY_FENCE #if ECB_GCC_VERSION(2,5) - #if __x86 + #if __i386__ #define ECB_MEMORY_FENCE __asm__ __volatile__ ("lock; orb $0, -1(%%esp)" : : : "memory") - #define ECB_MEMORY_FENCE_ACQUIRE ECB_MEMORY_FENCE - #define ECB_MEMORY_FENCE_RELEASE ECB_MEMORY_FENCE /* better be safe than sorry */ + #define ECB_MEMORY_FENCE_ACQUIRE ECB_MEMORY_FENCE /* non-lock xchg might be enough */ + #define ECB_MEMORY_FENCE_RELEASE do { } while (0) /* unlikely to change in future cpus */ #elif __amd64 #define ECB_MEMORY_FENCE __asm__ __volatile__ ("mfence" : : : "memory") #define ECB_MEMORY_FENCE_ACQUIRE __asm__ __volatile__ ("lfence" : : : "memory") - #define ECB_MEMORY_FENCE_RELEASE __asm__ __volatile__ ("sfence") + #define ECB_MEMORY_FENCE_RELEASE __asm__ __volatile__ ("sfence") /* play safe - not needed in any current cpu */ + #elif __powerpc__ || __ppc__ || __powerpc64__ || __ppc64__ + #define ECB_MEMORY_FENCE __asm__ __volatile__ ("sync" : : : "memory") + #elif defined(__ARM_ARCH_6__ ) || defined(__ARM_ARCH_6J__ ) \ + || defined(__ARM_ARCH_6K__) || defined(__ARM_ARCH_6ZK__) + #define ECB_MEMORY_FENCE __asm__ __volatile__ ("mcr p15,0,%0,c7,c10,5" : : "r" (0) : "memory") + #elif defined(__ARM_ARCH_7__ ) || defined(__ARM_ARCH_7A__ ) \ + || defined(__ARM_ARCH_7M__) || defined(__ARM_ARCH_7R__ ) + #define ECB_MEMORY_FENCE __asm__ __volatile__ ("dmb" : : : "memory") #endif #endif #endif #ifndef ECB_MEMORY_FENCE - #if ECB_GCC_VERSION(4,4) + #if ECB_GCC_VERSION(4,4) || defined(__INTEL_COMPILER) #define ECB_MEMORY_FENCE __sync_synchronize () - #define ECB_MEMORY_FENCE_ACQUIRE ({ char dummy = 0; __sync_lock_test_and_set (&dummy, 1); }) - #define ECB_MEMORY_FENCE_RELEASE ({ char dummy = 1; __sync_lock_release (&dummy ); }) - #elif _MSC_VER >= 1400 - #define ECB_MEMORY_FENCE do { } while (0) - #define ECB_MEMORY_FENCE_ACQUIRE ECB_MEMORY_FENCE - #define ECB_MEMORY_FENCE_RELEASE ECB_MEMORY_FENCE - #elif defined(_WIN32) && defined(MemoryBarrier) - #define ECB_MEMORY_FENCE MemoryBarrier () - #define ECB_MEMORY_FENCE_ACQUIRE ECB_MEMORY_FENCE - #define ECB_MEMORY_FENCE_RELEASE ECB_MEMORY_FENCE + /*#define ECB_MEMORY_FENCE_ACQUIRE ({ char dummy = 0; __sync_lock_test_and_set (&dummy, 1); }) */ + /*#define ECB_MEMORY_FENCE_RELEASE ({ char dummy = 1; __sync_lock_release (&dummy ); }) */ + #elif _MSC_VER >= 1400 /* VC++ 2005 */ + #pragma intrinsic(_ReadBarrier,_WriteBarrier,_ReadWriteBarrier) + #define ECB_MEMORY_FENCE _ReadWriteBarrier () + #define ECB_MEMORY_FENCE_ACQUIRE _ReadWriteBarrier () /* according to msdn, _ReadBarrier is not a load fence */ + #define ECB_MEMORY_FENCE_RELEASE _WriteBarrier () + #elif defined(_WIN32) + #include + #define ECB_MEMORY_FENCE MemoryBarrier () /* actually just xchg on x86... scary */ #endif #endif #ifndef ECB_MEMORY_FENCE - #include + #if !ECB_AVOID_PTHREADS + /* + * if you get undefined symbol references to pthread_mutex_lock, + * or failure to find pthread.h, then you should implement + * the ECB_MEMORY_FENCE operations for your cpu/compiler + * OR provide pthread.h and link against the posix thread library + * of your system. + */ + #include + #define ECB_NEEDS_PTHREADS 1 + #define ECB_MEMORY_FENCE_NEEDS_PTHREADS 1 + + static pthread_mutex_t ecb_mf_lock = PTHREAD_MUTEX_INITIALIZER; + #define ECB_MEMORY_FENCE do { pthread_mutex_lock (&ecb_mf_lock); pthread_mutex_unlock (&ecb_mf_lock); } while (0) + #endif +#endif - static pthread_mutex_t ecb_mf_lock = PTHREAD_MUTEX_INITIALIZER; - #define ECB_MEMORY_FENCE do { pthread_mutex_lock (&ecb_mf_lock); pthread_mutex_unlock (&ecb_mf_lock); } while (0) +#if !defined(ECB_MEMORY_FENCE_ACQUIRE) && defined(ECB_MEMORY_FENCE) #define ECB_MEMORY_FENCE_ACQUIRE ECB_MEMORY_FENCE +#endif + +#if !defined(ECB_MEMORY_FENCE_RELEASE) && defined(ECB_MEMORY_FENCE) #define ECB_MEMORY_FENCE_RELEASE ECB_MEMORY_FENCE #endif @@ -234,9 +266,6 @@ return x >> 24; } - /* you have the choice beetween something with a table lookup, */ - /* something using lots of bit arithmetic and a simple loop */ - /* we went for the loop */ ecb_function_ int ecb_ld32 (uint32_t x) ecb_const; ecb_function_ int ecb_ld32 (uint32_t x) {