08574852b58d6e9d3a28aeac61beec8ad0340c4f
[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_AS_FPU_DIRECTIVE
44 # define FPU
45 #else
46 # define FPU @
47 #endif
48
49 #if HAVE_AS_ARCH_DIRECTIVE
50 #if HAVE_NEON
51 .arch armv7-a
52 #elif HAVE_ARMV6T2
53 .arch armv6t2
54 #elif HAVE_ARMV6
55 .arch armv6
56 #elif HAVE_ARMV5TE
57 .arch armv5te
58 #endif
59 #endif
60 #if HAVE_AS_OBJECT_ARCH
61 ELF .object_arch armv4
62 #endif
63
64 #if HAVE_NEON
65 FPU .fpu neon
66 ELF .eabi_attribute 10, 0 @ suppress Tag_FP_arch
67 ELF .eabi_attribute 12, 0 @ suppress Tag_Advanced_SIMD_arch
68 #elif HAVE_VFP
69 FPU .fpu vfp
70 ELF .eabi_attribute 10, 0 @ suppress Tag_FP_arch
71 #endif
72
73 .syntax unified
74 T .thumb
75 ELF .eabi_attribute 25, 1 @ Tag_ABI_align_preserved
76 ELF .section .note.GNU-stack,"",%progbits @ Mark stack as non-executable
77
78 .macro function name, export=0, align=2
79 .set .Lpic_idx, 0
80 .set .Lpic_gp, 0
81 .macro endfunc
82 .if .Lpic_idx
83 .align 2
84 .altmacro
85 put_pic %(.Lpic_idx - 1)
86 .noaltmacro
87 .endif
88 .if .Lpic_gp
89 .unreq gp
90 .endif
91 ELF .size \name, . - \name
92 FUNC .endfunc
93 .purgem endfunc
94 .endm
95 .text
96 .align \align
97 .if \export
98 .global EXTERN_ASM\name
99 ELF .type EXTERN_ASM\name, %function
100 FUNC .func EXTERN_ASM\name
101 EXTERN_ASM\name:
102 .else
103 ELF .type \name, %function
104 FUNC .func \name
105 \name:
106 .endif
107 .endm
108
109 .macro const name, align=2, relocate=0
110 .macro endconst
111 ELF .size \name, . - \name
112 .purgem endconst
113 .endm
114 .if HAVE_SECTION_DATA_REL_RO && \relocate
115 .section .data.rel.ro
116 .else
117 .section .rodata
118 .endif
119 .align \align
120 \name:
121 .endm
122
123 #if !HAVE_ARMV6T2_EXTERNAL
124 .macro movw rd, val
125 mov \rd, \val & 255
126 orr \rd, \val & ~255
127 .endm
128 #endif
129
130 .macro mov32 rd, val
131 #if HAVE_ARMV6T2_EXTERNAL
132 movw \rd, #(\val) & 0xffff
133 .if (\val) >> 16
134 movt \rd, #(\val) >> 16
135 .endif
136 #else
137 ldr \rd, =\val
138 #endif
139 .endm
140
141 .macro put_pic num
142 put_pic_\num
143 .endm
144
145 .macro do_def_pic num, val, label
146 .macro put_pic_\num
147 .if \num
148 .altmacro
149 put_pic %(\num - 1)
150 .noaltmacro
151 .endif
152 \label: .word \val
153 .purgem put_pic_\num
154 .endm
155 .endm
156
157 .macro def_pic val, label
158 .altmacro
159 do_def_pic %.Lpic_idx, \val, \label
160 .noaltmacro
161 .set .Lpic_idx, .Lpic_idx + 1
162 .endm
163
164 .macro ldpic rd, val, indir=0
165 ldr \rd, .Lpicoff\@
166 .Lpic\@:
167 .if \indir
168 A ldr \rd, [pc, \rd]
169 T add \rd, pc
170 T ldr \rd, [\rd]
171 .else
172 add \rd, pc
173 .endif
174 def_pic \val - (.Lpic\@ + (8 >> CONFIG_THUMB)), .Lpicoff\@
175 .endm
176
177 .macro movrel rd, val
178 #if CONFIG_PIC
179 ldpic \rd, \val
180 #elif HAVE_ARMV6T2_EXTERNAL && !defined(__APPLE__)
181 movw \rd, #:lower16:\val
182 movt \rd, #:upper16:\val
183 #else
184 ldr \rd, =\val
185 #endif
186 .endm
187
188 .macro movrelx rd, val, gp
189 .ifc \rd,\gp
190 .error "movrelx needs two distinct registers"
191 .endif
192 .ifc \rd\()_\gp,r12_
193 .warning "movrelx rd=\rd without explicit set gp"
194 .endif
195 .ifc \rd\()_\gp,ip_
196 .warning "movrelx rd=\rd without explicit set gp"
197 .endif
198 #if CONFIG_PIC && defined(__ELF__)
199 .ifnb \gp
200 .if .Lpic_gp
201 .unreq gp
202 .endif
203 gp .req \gp
204 ldpic gp, _GLOBAL_OFFSET_TABLE_
205 .elseif !.Lpic_gp
206 gp .req r12
207 ldpic gp, _GLOBAL_OFFSET_TABLE_
208 .endif
209 .set .Lpic_gp, 1
210 ldr \rd, .Lpicoff\@
211 ldr \rd, [gp, \rd]
212 def_pic \val(GOT), .Lpicoff\@
213 #elif CONFIG_PIC && defined(__APPLE__)
214 ldpic \rd, .Lpic\@, indir=1
215 .non_lazy_symbol_pointer
216 .Lpic\@:
217 .indirect_symbol \val
218 .word 0
219 .text
220 #else
221 movrel \rd, \val
222 #endif
223 .endm
224
225 .macro add_sh rd, rn, rm, sh:vararg
226 A add \rd, \rn, \rm, \sh
227 T mov \rm, \rm, \sh
228 T add \rd, \rn, \rm
229 .endm
230
231 .macro ldr_pre rt, rn, rm:vararg
232 A ldr \rt, [\rn, \rm]!
233 T add \rn, \rn, \rm
234 T ldr \rt, [\rn]
235 .endm
236
237 .macro ldr_dpre rt, rn, rm:vararg
238 A ldr \rt, [\rn, -\rm]!
239 T sub \rn, \rn, \rm
240 T ldr \rt, [\rn]
241 .endm
242
243 .macro ldr_nreg rt, rn, rm:vararg
244 A ldr \rt, [\rn, -\rm]
245 T sub \rt, \rn, \rm
246 T ldr \rt, [\rt]
247 .endm
248
249 .macro ldr_post rt, rn, rm:vararg
250 A ldr \rt, [\rn], \rm
251 T ldr \rt, [\rn]
252 T add \rn, \rn, \rm
253 .endm
254
255 .macro ldrc_pre cc, rt, rn, rm:vararg
256 A ldr\cc \rt, [\rn, \rm]!
257 T itt \cc
258 T add\cc \rn, \rn, \rm
259 T ldr\cc \rt, [\rn]
260 .endm
261
262 .macro ldrd_reg rt, rt2, rn, rm
263 A ldrd \rt, \rt2, [\rn, \rm]
264 T add \rt, \rn, \rm
265 T ldrd \rt, \rt2, [\rt]
266 .endm
267
268 .macro ldrd_post rt, rt2, rn, rm
269 A ldrd \rt, \rt2, [\rn], \rm
270 T ldrd \rt, \rt2, [\rn]
271 T add \rn, \rn, \rm
272 .endm
273
274 .macro ldrh_pre rt, rn, rm
275 A ldrh \rt, [\rn, \rm]!
276 T add \rn, \rn, \rm
277 T ldrh \rt, [\rn]
278 .endm
279
280 .macro ldrh_dpre rt, rn, rm
281 A ldrh \rt, [\rn, -\rm]!
282 T sub \rn, \rn, \rm
283 T ldrh \rt, [\rn]
284 .endm
285
286 .macro ldrh_post rt, rn, rm
287 A ldrh \rt, [\rn], \rm
288 T ldrh \rt, [\rn]
289 T add \rn, \rn, \rm
290 .endm
291
292 .macro ldrb_post rt, rn, rm
293 A ldrb \rt, [\rn], \rm
294 T ldrb \rt, [\rn]
295 T add \rn, \rn, \rm
296 .endm
297
298 .macro str_post rt, rn, rm:vararg
299 A str \rt, [\rn], \rm
300 T str \rt, [\rn]
301 T add \rn, \rn, \rm
302 .endm
303
304 .macro strb_post rt, rn, rm:vararg
305 A strb \rt, [\rn], \rm
306 T strb \rt, [\rn]
307 T add \rn, \rn, \rm
308 .endm
309
310 .macro strd_post rt, rt2, rn, rm
311 A strd \rt, \rt2, [\rn], \rm
312 T strd \rt, \rt2, [\rn]
313 T add \rn, \rn, \rm
314 .endm
315
316 .macro strh_pre rt, rn, rm
317 A strh \rt, [\rn, \rm]!
318 T add \rn, \rn, \rm
319 T strh \rt, [\rn]
320 .endm
321
322 .macro strh_dpre rt, rn, rm
323 A strh \rt, [\rn, -\rm]!
324 T sub \rn, \rn, \rm
325 T strh \rt, [\rn]
326 .endm
327
328 .macro strh_post rt, rn, rm
329 A strh \rt, [\rn], \rm
330 T strh \rt, [\rn]
331 T add \rn, \rn, \rm
332 .endm
333
334 .macro strh_dpost rt, rn, rm
335 A strh \rt, [\rn], -\rm
336 T strh \rt, [\rn]
337 T sub \rn, \rn, \rm
338 .endm
339
340 #if HAVE_VFP_ARGS
341 ELF .eabi_attribute 28, 1
342 # define VFP
343 # define NOVFP @
344 #else
345 # define VFP @
346 # define NOVFP
347 #endif
348
349 #define GLUE(a, b) a ## b
350 #define JOIN(a, b) GLUE(a, b)
351 #define X(s) JOIN(EXTERN_ASM, s)