X-Git-Url: https://codewiz.org/gitweb?a=blobdiff_plain;f=bertos%2Fcpu%2Fbyteorder.h;h=37d7a84167d97b24d3da9e150f878f461f207b97;hb=f1fab319eb3fe91c157b3a9564841aef62a03554;hp=ff1237adf3a52e8a6a9c8714f41ba23cdf341d21;hpb=2f8a515c8a7147b8e6cf6ab643bb275d73b030dd;p=bertos.git diff --git a/bertos/cpu/byteorder.h b/bertos/cpu/byteorder.h index ff1237ad..37d7a841 100644 --- a/bertos/cpu/byteorder.h +++ b/bertos/cpu/byteorder.h @@ -41,6 +41,7 @@ #include #include #include +#include #include /** @@ -131,7 +132,7 @@ INLINE float swab_float(float x) /* Avoid breaking strict aliasing rules. */ char *cx = (char *)(&x); STATIC_ASSERT(sizeof(float) == 4); - #define BYTEORDER_SWAP(a, b) ((a) ^= (b) ^= (a) ^= (b)) + #define BYTEORDER_SWAP(a, b) do { (a) ^= (b); (b) ^= (a); (a) ^= (b); } while(0) BYTEORDER_SWAP(cx[0], cx[3]); BYTEORDER_SWAP(cx[1], cx[2]); #undef BYTEORDER_SWAP @@ -168,6 +169,37 @@ INLINE float net_to_host_float(float x) return be_float_to_cpu(x); } +#if CPU_ARM +INLINE cpu_atomic_t +cpu_atomic_xchg(volatile cpu_atomic_t *ptr, cpu_atomic_t val) +{ + cpu_atomic_t ret; + + asm volatile( + "swp %0, %1, [%2]" + + : "=&r" (ret) + : "r" (val), "r" (ptr) + : "memory", "cc"); + + return ret; +} +#else /* CPU_ARM */ +#include + +INLINE cpu_atomic_t +cpu_atomic_xchg(volatile cpu_atomic_t *ptr, cpu_atomic_t val) +{ + cpu_atomic_t ret; + + ATOMIC( + ret = *ptr; + *ptr = val; + ); + return ret; +} +#endif /* CPU_ARM */ + #ifdef __cplusplus /// Type generic byte swapping.