Browse code

ARM asm for AV_RN*()

ARMv6 and later support unaligned loads and stores for single
word/halfword but not double/multiple. GCC is ignorant of this and
will always use bytewise accesses for unaligned data. Casting to an
int32_t pointer is dangerous since a load/store double or multiple
instruction might be used (this happens with some code in FFmpeg).
Implementing the AV_[RW]* macros with inline asm using only supported
instructions gives fast and safe unaligned accesses. ARM RVCT does
the right thing with generic code.

This gives an overall speedup of up to 10%.

Originally committed as revision 18601 to svn://svn.ffmpeg.org/ffmpeg/trunk

Måns Rullgård authored on 2009/04/18 09:00:28
Showing 2 changed files
1 1
new file mode 100644
... ...
@@ -0,0 +1,78 @@
0
+/*
1
+ * This file is part of FFmpeg.
2
+ *
3
+ * FFmpeg is free software; you can redistribute it and/or
4
+ * modify it under the terms of the GNU Lesser General Public
5
+ * License as published by the Free Software Foundation; either
6
+ * version 2.1 of the License, or (at your option) any later version.
7
+ *
8
+ * FFmpeg is distributed in the hope that it will be useful,
9
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
10
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11
+ * Lesser General Public License for more details.
12
+ *
13
+ * You should have received a copy of the GNU Lesser General Public
14
+ * License along with FFmpeg; if not, write to the Free Software
15
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
16
+ */
17
+
18
+#ifndef AVUTIL_ARM_INTREADWRITE_H
19
+#define AVUTIL_ARM_INTREADWRITE_H
20
+
21
+#include <stdint.h>
22
+#include "config.h"
23
+
24
+#if HAVE_FAST_UNALIGNED && HAVE_INLINE_ASM
25
+
26
+#define AV_RN16 AV_RN16
27
+static inline uint16_t AV_RN16(const void *p)
28
+{
29
+    uint16_t v;
30
+    __asm__ ("ldrh %0, %1" : "=r"(v) : "m"(*(const uint16_t *)p));
31
+    return v;
32
+}
33
+
34
+#define AV_WN16 AV_WN16
35
+static inline void AV_WN16(void *p, uint16_t v)
36
+{
37
+    __asm__ ("strh %1, %0" : "=m"(*(uint16_t *)p) : "r"(v));
38
+}
39
+
40
+#define AV_RN32 AV_RN32
41
+static inline uint32_t AV_RN32(const void *p)
42
+{
43
+    uint32_t v;
44
+    __asm__ ("ldr  %0, %1" : "=r"(v) : "m"(*(const uint32_t *)p));
45
+    return v;
46
+}
47
+
48
+#define AV_WN32 AV_WN32
49
+static inline void AV_WN32(void *p, uint32_t v)
50
+{
51
+    __asm__ ("str  %1, %0" : "=m"(*(uint32_t *)p) : "r"(v));
52
+}
53
+
54
+#define AV_RN64 AV_RN64
55
+static inline uint64_t AV_RN64(const void *p)
56
+{
57
+    union { uint64_t v; uint32_t hl[2]; } v;
58
+    __asm__ ("ldr   %0, %2  \n\t"
59
+             "ldr   %1, %3  \n\t"
60
+             : "=r"(v.hl[0]), "=r"(v.hl[1])
61
+             : "m"(*(const uint32_t*)p), "m"(*((const uint32_t*)p+1)));
62
+    return v.v;
63
+}
64
+
65
+#define AV_WN64 AV_WN64
66
+static inline void AV_WN64(void *p, uint64_t v)
67
+{
68
+    union { uint64_t v; uint32_t hl[2]; } vv = { v };
69
+    __asm__ ("str  %2, %0  \n\t"
70
+             "str  %3, %1  \n\t"
71
+             : "=m"(*(uint32_t*)p), "=m"(*((uint32_t*)p+1))
72
+             : "r"(vv.hl[0]), "r"(vv.hl[1]));
73
+}
74
+
75
+#endif /* HAVE_INLINE_ASM */
76
+
77
+#endif /* AVUTIL_ARM_INTREADWRITE_H */
... ...
@@ -29,6 +29,9 @@
29 29
  * defined, even if these are implemented as inline functions.
30 30
  */
31 31
 
32
+#if   ARCH_ARM
33
+#   include "arm/intreadwrite.h"
34
+#endif
32 35
 
33 36
 /*
34 37
  * Define AV_[RW]N helper macros to simplify definitions not provided