Browse code

bswap: make generic implementation more compiler-friendly

With these changes, gcc 4.5 and later recognise it as a bswap
and use the proper instructions on ARM and x86. On x86, the
16-bit bswap is recognised from gcc 4.1.

Signed-off-by: Mans Rullgard <mans@mansr.com>

Mans Rullgard authored on 2011/12/12 09:50:08
Showing 3 changed files
... ...
@@ -51,6 +51,7 @@ static av_always_inline av_const unsigned av_bswap16(unsigned x)
51 51
 }
52 52
 #endif
53 53
 
54
+#if !AV_GCC_VERSION_AT_LEAST(4,5)
54 55
 #define av_bswap32 av_bswap32
55 56
 static av_always_inline av_const uint32_t av_bswap32(uint32_t x)
56 57
 {
... ...
@@ -66,6 +67,7 @@ static av_always_inline av_const uint32_t av_bswap32(uint32_t x)
66 66
 #endif /* HAVE_ARMV6 */
67 67
     return x;
68 68
 }
69
+#endif /* !AV_GCC_VERSION_AT_LEAST(4,5) */
69 70
 
70 71
 #endif /* __ARMCC_VERSION */
71 72
 
... ...
@@ -65,23 +65,14 @@ static av_always_inline av_const uint16_t av_bswap16(uint16_t x)
65 65
 #ifndef av_bswap32
66 66
 static av_always_inline av_const uint32_t av_bswap32(uint32_t x)
67 67
 {
68
-    x= ((x<<8)&0xFF00FF00) | ((x>>8)&0x00FF00FF);
69
-    x= (x>>16) | (x<<16);
70
-    return x;
68
+    return AV_BSWAP32C(x);
71 69
 }
72 70
 #endif
73 71
 
74 72
 #ifndef av_bswap64
75 73
 static inline uint64_t av_const av_bswap64(uint64_t x)
76 74
 {
77
-    union {
78
-        uint64_t ll;
79
-        uint32_t l[2];
80
-    } w, r;
81
-    w.ll = x;
82
-    r.l[0] = av_bswap32 (w.l[1]);
83
-    r.l[1] = av_bswap32 (w.l[0]);
84
-    return r.ll;
75
+    return (uint64_t)av_bswap32(x) << 32 | av_bswap32(x >> 32);
85 76
 }
86 77
 #endif
87 78
 
... ...
@@ -28,13 +28,16 @@
28 28
 #include "config.h"
29 29
 #include "libavutil/attributes.h"
30 30
 
31
+#if !AV_GCC_VERSION_AT_LEAST(4,1)
31 32
 #define av_bswap16 av_bswap16
32 33
 static av_always_inline av_const unsigned av_bswap16(unsigned x)
33 34
 {
34 35
     __asm__("rorw $8, %w0" : "+r"(x));
35 36
     return x;
36 37
 }
38
+#endif /* !AV_GCC_VERSION_AT_LEAST(4,1) */
37 39
 
40
+#if !AV_GCC_VERSION_AT_LEAST(4,5)
38 41
 #define av_bswap32 av_bswap32
39 42
 static av_always_inline av_const uint32_t av_bswap32(uint32_t x)
40 43
 {
... ...
@@ -57,5 +60,6 @@ static inline uint64_t av_const av_bswap64(uint64_t x)
57 57
     return x;
58 58
 }
59 59
 #endif
60
+#endif /* !AV_GCC_VERSION_AT_LEAST(4,5) */
60 61
 
61 62
 #endif /* AVUTIL_X86_BSWAP_H */