8479304f5fdce26450135d4ef77d7659f445758d
[libav.git] / libavutil / arm / asm.S
1 /*
2 * Copyright (c) 2008 Mans Rullgard <mans@mansr.com>
3 *
4 * This file is part of Libav.
5 *
6 * Libav is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
10 *
11 * Libav is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with Libav; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20
21 #include "config.h"
22
23 #ifdef __ELF__
24 # define ELF
25 #else
26 # define ELF @
27 #endif
28
29 #if CONFIG_THUMB
30 # define A @
31 # define T
32 #else
33 # define A
34 # define T @
35 #endif
36
37 #if HAVE_AS_FUNC
38 # define FUNC
39 #else
40 # define FUNC @
41 #endif
42
43 #if HAVE_NEON
44 .arch armv7-a
45 #elif HAVE_ARMV6T2
46 .arch armv6t2
47 #elif HAVE_ARMV6
48 .arch armv6
49 #elif HAVE_ARMV5TE
50 .arch armv5te
51 #endif
52
53 #if HAVE_NEON
54 .fpu neon
55 #elif HAVE_VFP
56 .fpu vfp
57 #endif
58
59 .syntax unified
60 T .thumb
61 ELF .eabi_attribute 25, 1 @ Tag_ABI_align_preserved
62 ELF .section .note.GNU-stack,"",%progbits @ Mark stack as non-executable
63
64 .macro function name, export=0, align=2
65 .set .Lpic_idx, 0
66 .set .Lpic_gp, 0
67 .macro endfunc
68 .if .Lpic_idx
69 .align 2
70 .altmacro
71 put_pic %(.Lpic_idx - 1)
72 .noaltmacro
73 .endif
74 ELF .size \name, . - \name
75 FUNC .endfunc
76 .purgem endfunc
77 .endm
78 .text
79 .align \align
80 .if \export
81 .global EXTERN_ASM\name
82 ELF .type EXTERN_ASM\name, %function
83 FUNC .func EXTERN_ASM\name
84 EXTERN_ASM\name:
85 .else
86 ELF .type \name, %function
87 FUNC .func \name
88 \name:
89 .endif
90 .endm
91
92 .macro const name, align=2, relocate=0
93 .macro endconst
94 ELF .size \name, . - \name
95 .purgem endconst
96 .endm
97 .if HAVE_SECTION_DATA_REL_RO && \relocate
98 .section .data.rel.ro
99 .else
100 .section .rodata
101 .endif
102 .align \align
103 \name:
104 .endm
105
106 #if !HAVE_ARMV6T2_EXTERNAL
107 .macro movw rd, val
108 mov \rd, \val & 255
109 orr \rd, \val & ~255
110 .endm
111 #endif
112
113 .macro mov32 rd, val
114 #if HAVE_ARMV6T2_EXTERNAL
115 movw \rd, #(\val) & 0xffff
116 .if (\val) >> 16
117 movt \rd, #(\val) >> 16
118 .endif
119 #else
120 ldr \rd, =\val
121 #endif
122 .endm
123
124 .macro put_pic num
125 put_pic_\num
126 .endm
127
128 .macro do_def_pic num, val, label
129 .macro put_pic_\num
130 .if \num
131 .altmacro
132 put_pic %(\num - 1)
133 .noaltmacro
134 .endif
135 \label: .word \val
136 .purgem put_pic_\num
137 .endm
138 .endm
139
140 .macro def_pic val, label
141 .altmacro
142 do_def_pic %.Lpic_idx, \val, \label
143 .noaltmacro
144 .set .Lpic_idx, .Lpic_idx + 1
145 .endm
146
147 .macro ldpic rd, val, indir=0
148 ldr \rd, .Lpicoff\@
149 .Lpic\@:
150 .if \indir
151 A ldr \rd, [pc, \rd]
152 T add \rd, pc
153 T ldr \rd, [\rd]
154 .else
155 add \rd, pc
156 .endif
157 def_pic \val - (.Lpic\@ + (8 >> CONFIG_THUMB)), .Lpicoff\@
158 .endm
159
160 .macro movrel rd, val
161 #if CONFIG_PIC
162 ldpic \rd, \val
163 #elif HAVE_ARMV6T2_EXTERNAL && !defined(__APPLE__)
164 movw \rd, #:lower16:\val
165 movt \rd, #:upper16:\val
166 #else
167 ldr \rd, =\val
168 #endif
169 .endm
170
171 .macro movrelx rd, val, gp
172 #if CONFIG_PIC && defined(__ELF__)
173 .ifnb \gp
174 .if .Lpic_gp
175 .unreq gp
176 .endif
177 gp .req \gp
178 ldpic gp, _GLOBAL_OFFSET_TABLE_
179 .elseif !.Lpic_gp
180 gp .req r12
181 ldpic gp, _GLOBAL_OFFSET_TABLE_
182 .endif
183 .set .Lpic_gp, 1
184 ldr \rd, .Lpicoff\@
185 ldr \rd, [gp, \rd]
186 def_pic \val(GOT), .Lpicoff\@
187 #elif CONFIG_PIC && defined(__APPLE__)
188 ldpic \rd, .Lpic\@, indir=1
189 .non_lazy_symbol_pointer
190 .Lpic\@:
191 .indirect_symbol \val
192 .word 0
193 .text
194 #else
195 movrel \rd, \val
196 #endif
197 .endm
198
199 .macro add_sh rd, rn, rm, sh:vararg
200 A add \rd, \rn, \rm, \sh
201 T mov \rm, \rm, \sh
202 T add \rd, \rn, \rm
203 .endm
204
205 .macro ldr_pre rt, rn, rm:vararg
206 A ldr \rt, [\rn, \rm]!
207 T add \rn, \rn, \rm
208 T ldr \rt, [\rn]
209 .endm
210
211 .macro ldr_dpre rt, rn, rm:vararg
212 A ldr \rt, [\rn, -\rm]!
213 T sub \rn, \rn, \rm
214 T ldr \rt, [\rn]
215 .endm
216
217 .macro ldr_nreg rt, rn, rm:vararg
218 A ldr \rt, [\rn, -\rm]
219 T sub \rt, \rn, \rm
220 T ldr \rt, [\rt]
221 .endm
222
223 .macro ldr_post rt, rn, rm:vararg
224 A ldr \rt, [\rn], \rm
225 T ldr \rt, [\rn]
226 T add \rn, \rn, \rm
227 .endm
228
229 .macro ldrc_pre cc, rt, rn, rm:vararg
230 A ldr\cc \rt, [\rn, \rm]!
231 T itt \cc
232 T add\cc \rn, \rn, \rm
233 T ldr\cc \rt, [\rn]
234 .endm
235
236 .macro ldrd_reg rt, rt2, rn, rm
237 A ldrd \rt, \rt2, [\rn, \rm]
238 T add \rt, \rn, \rm
239 T ldrd \rt, \rt2, [\rt]
240 .endm
241
242 .macro ldrd_post rt, rt2, rn, rm
243 A ldrd \rt, \rt2, [\rn], \rm
244 T ldrd \rt, \rt2, [\rn]
245 T add \rn, \rn, \rm
246 .endm
247
248 .macro ldrh_pre rt, rn, rm
249 A ldrh \rt, [\rn, \rm]!
250 T add \rn, \rn, \rm
251 T ldrh \rt, [\rn]
252 .endm
253
254 .macro ldrh_dpre rt, rn, rm
255 A ldrh \rt, [\rn, -\rm]!
256 T sub \rn, \rn, \rm
257 T ldrh \rt, [\rn]
258 .endm
259
260 .macro ldrh_post rt, rn, rm
261 A ldrh \rt, [\rn], \rm
262 T ldrh \rt, [\rn]
263 T add \rn, \rn, \rm
264 .endm
265
266 .macro ldrb_post rt, rn, rm
267 A ldrb \rt, [\rn], \rm
268 T ldrb \rt, [\rn]
269 T add \rn, \rn, \rm
270 .endm
271
272 .macro str_post rt, rn, rm:vararg
273 A str \rt, [\rn], \rm
274 T str \rt, [\rn]
275 T add \rn, \rn, \rm
276 .endm
277
278 .macro strb_post rt, rn, rm:vararg
279 A strb \rt, [\rn], \rm
280 T strb \rt, [\rn]
281 T add \rn, \rn, \rm
282 .endm
283
284 .macro strd_post rt, rt2, rn, rm
285 A strd \rt, \rt2, [\rn], \rm
286 T strd \rt, \rt2, [\rn]
287 T add \rn, \rn, \rm
288 .endm
289
290 .macro strh_pre rt, rn, rm
291 A strh \rt, [\rn, \rm]!
292 T add \rn, \rn, \rm
293 T strh \rt, [\rn]
294 .endm
295
296 .macro strh_dpre rt, rn, rm
297 A strh \rt, [\rn, -\rm]!
298 T sub \rn, \rn, \rm
299 T strh \rt, [\rn]
300 .endm
301
302 .macro strh_post rt, rn, rm
303 A strh \rt, [\rn], \rm
304 T strh \rt, [\rn]
305 T add \rn, \rn, \rm
306 .endm
307
308 .macro strh_dpost rt, rn, rm
309 A strh \rt, [\rn], -\rm
310 T strh \rt, [\rn]
311 T sub \rn, \rn, \rm
312 .endm
313
314 #if HAVE_VFP_ARGS
315 ELF .eabi_attribute 28, 1
316 # define VFP
317 # define NOVFP @
318 #else
319 # define VFP @
320 # define NOVFP
321 #endif
322
323 #define GLUE(a, b) a ## b
324 #define JOIN(a, b) GLUE(a, b)
325 #define X(s) JOIN(EXTERN_ASM, s)