[FFmpeg-devel] [PATCH] Make HAVE_FAST_UNALIGNED allow unaligned memory accesses

Mans Rullgard mans
Tue Jul 15 21:16:21 CEST 2008


If HAVE_FAST_UNALIGNED is defined, potentially unaligned data is
accessed through normal pointers.  Otherwise, compiler-specific
code is used to perform unaligned accesses, falling back to
byte-wise access if no compiler support is available.
---
 libavutil/intreadwrite.h |  130 +++++++++++++++++++++++++++------------------
 1 files changed, 78 insertions(+), 52 deletions(-)

diff --git a/libavutil/intreadwrite.h b/libavutil/intreadwrite.h
index cbf5163..12dec54 100644
--- a/libavutil/intreadwrite.h
+++ b/libavutil/intreadwrite.h
@@ -23,6 +23,8 @@
 #include "config.h"
 #include "bswap.h"
 
+#if !defined(HAVE_FAST_UNALIGNED)
+
 #ifdef __GNUC__
 
 struct unaligned_64 { uint64_t l; } __attribute__((packed));
@@ -37,7 +39,19 @@ struct unaligned_16 { uint16_t l; } __attribute__((packed));
 #define AV_WN32(a, b) (((struct unaligned_32 *) (a))->l) = (b)
 #define AV_WN64(a, b) (((struct unaligned_64 *) (a))->l) = (b)
 
-#else /* __GNUC__ */
+#elif defined(__DECC)
+
+#define AV_RN16(a) (*((const __unaligned uint16_t*)(a)))
+#define AV_RN32(a) (*((const __unaligned uint32_t*)(a)))
+#define AV_RN64(a) (*((const __unaligned uint64_t*)(a)))
+
+#define AV_WN16(a, b) *((__unaligned uint16_t*)(a)) = (b)
+#define AV_WN32(a, b) *((__unaligned uint32_t*)(a)) = (b)
+#define AV_WN64(a, b) *((__unaligned uint64_t*)(a)) = (b)
+
+#endif
+
+#else /* !HAVE_FAST_UNALIGNED */
 
 #define AV_RN16(a) (*((const uint16_t*)(a)))
 #define AV_RN32(a) (*((const uint32_t*)(a)))
@@ -47,7 +61,7 @@ struct unaligned_16 { uint16_t l; } __attribute__((packed));
 #define AV_WN32(a, b) *((uint32_t*)(a)) = (b)
 #define AV_WN64(a, b) *((uint64_t*)(a)) = (b)
 
-#endif /* !__GNUC__ */
+#endif /* !HAVE_FAST_UNALIGNED */
 
 /* endian macros */
 #define AV_RB8(x)     (((const uint8_t*)(x))[0])
@@ -56,21 +70,48 @@ struct unaligned_16 { uint16_t l; } __attribute__((packed));
 #define AV_RL8(x)     AV_RB8(x)
 #define AV_WL8(p, d)  AV_WB8(p, d)
 
-#ifdef HAVE_FAST_UNALIGNED
+#ifdef AV_RN16
+
 # ifdef WORDS_BIGENDIAN
 #  define AV_RB16(x)    AV_RN16(x)
 #  define AV_WB16(p, d) AV_WN16(p, d)
 
 #  define AV_RL16(x)    bswap_16(AV_RN16(x))
 #  define AV_WL16(p, d) AV_WN16(p, bswap_16(d))
+
+#  define AV_RB32(x)    AV_RN32(x)
+#  define AV_WB32(p, d) AV_WN32(p, d)
+
+#  define AV_RL32(x)    bswap_32(AV_RN32(x))
+#  define AV_WL32(p, d) AV_WN32(p, bswap_32(d))
+
+#  define AV_RB64(x)    AV_RN64(x)
+#  define AV_WB64(p, d) AV_WN64(p, d)
+
+#  define AV_RL64(x)    bswap_64(AV_RN64(x))
+#  define AV_WL64(p, d) AV_WN64(p, bswap_64(d))
 # else /* WORDS_BIGENDIAN */
 #  define AV_RB16(x)    bswap_16(AV_RN16(x))
 #  define AV_WB16(p, d) AV_WN16(p, bswap_16(d))
 
 #  define AV_RL16(x)    AV_RN16(x)
 #  define AV_WL16(p, d) AV_WN16(p, d)
+
+#  define AV_RB32(x)    bswap_32(AV_RN32(x))
+#  define AV_WB32(p, d) AV_WN32(p, bswap_32(d))
+
+#  define AV_RL32(x)    AV_RN32(x)
+#  define AV_WL32(p, d) AV_WN32(p, d)
+
+#  define AV_RB64(x)    bswap_64(AV_RN64(x))
+#  define AV_WB64(p, d) AV_WN64(p, bswap_64(d))
+
+#  define AV_RL64(x)    AV_RN64(x)
+#  define AV_WL64(p, d) AV_WN64(p, d)
 # endif
-#else /* HAVE_FAST_UNALIGNED */
+
+#else  /* AV_RN16 */
+
 #define AV_RB16(x)  ((((const uint8_t*)(x))[0] << 8) | ((const uint8_t*)(x))[1])
 #define AV_WB16(p, d) do { \
                     ((uint8_t*)(p))[1] = (d); \
@@ -81,39 +122,7 @@ struct unaligned_16 { uint16_t l; } __attribute__((packed));
 #define AV_WL16(p, d) do { \
                     ((uint8_t*)(p))[0] = (d); \
                     ((uint8_t*)(p))[1] = (d)>>8; } while(0)
-#endif
-
-#define AV_RB24(x)  ((((const uint8_t*)(x))[0] << 16) | \
-                     (((const uint8_t*)(x))[1] <<  8) | \
-                      ((const uint8_t*)(x))[2])
-#define AV_WB24(p, d) do { \
-                    ((uint8_t*)(p))[2] = (d); \
-                    ((uint8_t*)(p))[1] = (d)>>8; \
-                    ((uint8_t*)(p))[0] = (d)>>16; } while(0)
 
-#define AV_RL24(x)  ((((const uint8_t*)(x))[2] << 16) | \
-                     (((const uint8_t*)(x))[1] <<  8) | \
-                      ((const uint8_t*)(x))[0])
-#define AV_WL24(p, d) do { \
-                    ((uint8_t*)(p))[0] = (d); \
-                    ((uint8_t*)(p))[1] = (d)>>8; \
-                    ((uint8_t*)(p))[2] = (d)>>16; } while(0)
-
-#ifdef HAVE_FAST_UNALIGNED
-# ifdef WORDS_BIGENDIAN
-#  define AV_RB32(x)    AV_RN32(x)
-#  define AV_WB32(p, d) AV_WN32(p, d)
-
-#  define AV_RL32(x)    bswap_32(AV_RN32(x))
-#  define AV_WL32(p, d) AV_WN32(p, bswap_32(d))
-# else /* WORDS_BIGENDIAN */
-#  define AV_RB32(x)    bswap_32(AV_RN32(x))
-#  define AV_WB32(p, d) AV_WN32(p, bswap_32(d))
-
-#  define AV_RL32(x)    AV_RN32(x)
-#  define AV_WL32(p, d) AV_WN32(p, d)
-# endif
-#else /* HAVE_FAST_UNALIGNED */
 #define AV_RB32(x)  ((((const uint8_t*)(x))[0] << 24) | \
                      (((const uint8_t*)(x))[1] << 16) | \
                      (((const uint8_t*)(x))[2] <<  8) | \
@@ -133,23 +142,7 @@ struct unaligned_16 { uint16_t l; } __attribute__((packed));
                     ((uint8_t*)(p))[1] = (d)>>8; \
                     ((uint8_t*)(p))[2] = (d)>>16; \
                     ((uint8_t*)(p))[3] = (d)>>24; } while(0)
-#endif
-
-#ifdef HAVE_FAST_UNALIGNED
-# ifdef WORDS_BIGENDIAN
-#  define AV_RB64(x)    AV_RN64(x)
-#  define AV_WB64(p, d) AV_WN64(p, d)
 
-#  define AV_RL64(x)    bswap_64(AV_RN64(x))
-#  define AV_WL64(p, d) AV_WN64(p, bswap_64(d))
-# else /* WORDS_BIGENDIAN */
-#  define AV_RB64(x)    bswap_64(AV_RN64(x))
-#  define AV_WB64(p, d) AV_WN64(p, bswap_64(d))
-
-#  define AV_RL64(x)    AV_RN64(x)
-#  define AV_WL64(p, d) AV_WN64(p, d)
-# endif
-#else /* HAVE_FAST_UNALIGNED */
 #define AV_RB64(x)  (((uint64_t)((const uint8_t*)(x))[0] << 56) | \
                      ((uint64_t)((const uint8_t*)(x))[1] << 48) | \
                      ((uint64_t)((const uint8_t*)(x))[2] << 40) | \
@@ -185,6 +178,39 @@ struct unaligned_16 { uint16_t l; } __attribute__((packed));
                     ((uint8_t*)(p))[5] = (d)>>40; \
                     ((uint8_t*)(p))[6] = (d)>>48; \
                     ((uint8_t*)(p))[7] = (d)>>56; } while(0)
+
+#ifdef WORDS_BIGENDIAN
+# define AV_RN16 AV_RB16
+# define AV_RN32 AV_RB32
+# define AV_RN64 AV_RB64
+# define AV_WN16 AV_WB16
+# define AV_WN32 AV_WB32
+# define AV_WN64 AV_WB64
+#else
+# define AV_RN16 AV_RL16
+# define AV_RN32 AV_RL32
+# define AV_RN64 AV_RL64
+# define AV_WN16 AV_WL16
+# define AV_WN32 AV_WL32
+# define AV_WN64 AV_WL64
 #endif
 
+#endif  /* AV_RN16 */
+
+#define AV_RB24(x)  ((((const uint8_t*)(x))[0] << 16) | \
+                     (((const uint8_t*)(x))[1] <<  8) | \
+                      ((const uint8_t*)(x))[2])
+#define AV_WB24(p, d) do { \
+                    ((uint8_t*)(p))[2] = (d); \
+                    ((uint8_t*)(p))[1] = (d)>>8; \
+                    ((uint8_t*)(p))[0] = (d)>>16; } while(0)
+
+#define AV_RL24(x)  ((((const uint8_t*)(x))[2] << 16) | \
+                     (((const uint8_t*)(x))[1] <<  8) | \
+                      ((const uint8_t*)(x))[0])
+#define AV_WL24(p, d) do { \
+                    ((uint8_t*)(p))[0] = (d); \
+                    ((uint8_t*)(p))[1] = (d)>>8; \
+                    ((uint8_t*)(p))[2] = (d)>>16; } while(0)
+
 #endif /* FFMPEG_INTREADWRITE_H */
-- 
1.5.6.2





More information about the ffmpeg-devel mailing list