1 ;; Copyright (C) 2011-2013 Free Software Foundation, Inc.
3 ;; This file is part of GCC.
5 ;; GCC is free software; you can redistribute it and/or modify it
6 ;; under the terms of the GNU General Public License as published
7 ;; by the Free Software Foundation; either version 3, or (at your
8 ;; option) any later version.
10 ;; GCC is distributed in the hope that it will be useful, but WITHOUT
11 ;; ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12 ;; or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
13 ;; License for more details.
15 ;; You should have received a copy of the GNU General Public License
16 ;; along with GCC; see the file COPYING3. If not see
17 ;; <http://www.gnu.org/licenses/>.
19 ;; This file contains ARM instructions that support fixed-point operations.
21 (define_insn "add<mode>3"
22 [(set (match_operand:FIXED 0 "s_register_operand" "=l,r")
23 (plus:FIXED (match_operand:FIXED 1 "s_register_operand" "l,r")
24 (match_operand:FIXED 2 "s_register_operand" "l,r")))]
27 [(set_attr "predicable" "yes")
28 (set_attr "predicable_short_it" "yes,no")])
30 (define_insn "add<mode>3"
31 [(set (match_operand:ADDSUB 0 "s_register_operand" "=r")
32 (plus:ADDSUB (match_operand:ADDSUB 1 "s_register_operand" "r")
33 (match_operand:ADDSUB 2 "s_register_operand" "r")))]
35 "sadd<qaddsub_suf>%?\\t%0, %1, %2"
36 [(set_attr "predicable" "yes")
37 (set_attr "predicable_short_it" "no")])
39 (define_insn "usadd<mode>3"
40 [(set (match_operand:UQADDSUB 0 "s_register_operand" "=r")
41 (us_plus:UQADDSUB (match_operand:UQADDSUB 1 "s_register_operand" "r")
42 (match_operand:UQADDSUB 2 "s_register_operand" "r")))]
44 "uqadd<qaddsub_suf>%?\\t%0, %1, %2"
45 [(set_attr "predicable" "yes")
46 (set_attr "predicable_short_it" "no")])
48 (define_insn "ssadd<mode>3"
49 [(set (match_operand:QADDSUB 0 "s_register_operand" "=r")
50 (ss_plus:QADDSUB (match_operand:QADDSUB 1 "s_register_operand" "r")
51 (match_operand:QADDSUB 2 "s_register_operand" "r")))]
53 "qadd<qaddsub_suf>%?\\t%0, %1, %2"
54 [(set_attr "predicable" "yes")
55 (set_attr "predicable_short_it" "no")])
57 (define_insn "sub<mode>3"
58 [(set (match_operand:FIXED 0 "s_register_operand" "=l,r")
59 (minus:FIXED (match_operand:FIXED 1 "s_register_operand" "l,r")
60 (match_operand:FIXED 2 "s_register_operand" "l,r")))]
63 [(set_attr "predicable" "yes")
64 (set_attr "predicable_short_it" "yes,no")])
66 (define_insn "sub<mode>3"
67 [(set (match_operand:ADDSUB 0 "s_register_operand" "=r")
68 (minus:ADDSUB (match_operand:ADDSUB 1 "s_register_operand" "r")
69 (match_operand:ADDSUB 2 "s_register_operand" "r")))]
71 "ssub<qaddsub_suf>%?\\t%0, %1, %2"
72 [(set_attr "predicable" "yes")
73 (set_attr "predicable_short_it" "no")])
75 (define_insn "ussub<mode>3"
76 [(set (match_operand:UQADDSUB 0 "s_register_operand" "=r")
78 (match_operand:UQADDSUB 1 "s_register_operand" "r")
79 (match_operand:UQADDSUB 2 "s_register_operand" "r")))]
81 "uqsub<qaddsub_suf>%?\\t%0, %1, %2"
82 [(set_attr "predicable" "yes")
83 (set_attr "predicable_short_it" "no")])
85 (define_insn "sssub<mode>3"
86 [(set (match_operand:QADDSUB 0 "s_register_operand" "=r")
87 (ss_minus:QADDSUB (match_operand:QADDSUB 1 "s_register_operand" "r")
88 (match_operand:QADDSUB 2 "s_register_operand" "r")))]
90 "qsub<qaddsub_suf>%?\\t%0, %1, %2"
91 [(set_attr "predicable" "yes")
92 (set_attr "predicable_short_it" "no")])
94 ;; Fractional multiplies.
96 ; Note: none of these do any rounding.
98 (define_expand "mulqq3"
99 [(set (match_operand:QQ 0 "s_register_operand" "")
100 (mult:QQ (match_operand:QQ 1 "s_register_operand" "")
101 (match_operand:QQ 2 "s_register_operand" "")))]
102 "TARGET_DSP_MULTIPLY && arm_arch_thumb2"
104 rtx tmp1 = gen_reg_rtx (HImode);
105 rtx tmp2 = gen_reg_rtx (HImode);
106 rtx tmp3 = gen_reg_rtx (SImode);
108 emit_insn (gen_extendqihi2 (tmp1, gen_lowpart (QImode, operands[1])));
109 emit_insn (gen_extendqihi2 (tmp2, gen_lowpart (QImode, operands[2])));
110 emit_insn (gen_mulhisi3 (tmp3, tmp1, tmp2));
111 emit_insn (gen_extv (gen_lowpart (SImode, operands[0]), tmp3, GEN_INT (8),
116 (define_expand "mulhq3"
117 [(set (match_operand:HQ 0 "s_register_operand" "")
118 (mult:HQ (match_operand:HQ 1 "s_register_operand" "")
119 (match_operand:HQ 2 "s_register_operand" "")))]
120 "TARGET_DSP_MULTIPLY && arm_arch_thumb2"
122 rtx tmp = gen_reg_rtx (SImode);
124 emit_insn (gen_mulhisi3 (tmp, gen_lowpart (HImode, operands[1]),
125 gen_lowpart (HImode, operands[2])));
126 /* We're doing a s.15 * s.15 multiplication, getting an s.30 result. Extract
127 an s.15 value from that. This won't overflow/saturate for _Fract
129 emit_insn (gen_extv (gen_lowpart (SImode, operands[0]), tmp,
130 GEN_INT (16), GEN_INT (15)));
134 (define_expand "mulsq3"
135 [(set (match_operand:SQ 0 "s_register_operand" "")
136 (mult:SQ (match_operand:SQ 1 "s_register_operand" "")
137 (match_operand:SQ 2 "s_register_operand" "")))]
138 "TARGET_32BIT && arm_arch3m"
140 rtx tmp1 = gen_reg_rtx (DImode);
141 rtx tmp2 = gen_reg_rtx (SImode);
142 rtx tmp3 = gen_reg_rtx (SImode);
144 /* s.31 * s.31 -> s.62 multiplication. */
145 emit_insn (gen_mulsidi3 (tmp1, gen_lowpart (SImode, operands[1]),
146 gen_lowpart (SImode, operands[2])));
147 emit_insn (gen_lshrsi3 (tmp2, gen_lowpart (SImode, tmp1), GEN_INT (31)));
148 emit_insn (gen_ashlsi3 (tmp3, gen_highpart (SImode, tmp1), GEN_INT (1)));
149 emit_insn (gen_iorsi3 (gen_lowpart (SImode, operands[0]), tmp2, tmp3));
154 ;; Accumulator multiplies.
156 (define_expand "mulsa3"
157 [(set (match_operand:SA 0 "s_register_operand" "")
158 (mult:SA (match_operand:SA 1 "s_register_operand" "")
159 (match_operand:SA 2 "s_register_operand" "")))]
160 "TARGET_32BIT && arm_arch3m"
162 rtx tmp1 = gen_reg_rtx (DImode);
163 rtx tmp2 = gen_reg_rtx (SImode);
164 rtx tmp3 = gen_reg_rtx (SImode);
166 emit_insn (gen_mulsidi3 (tmp1, gen_lowpart (SImode, operands[1]),
167 gen_lowpart (SImode, operands[2])));
168 emit_insn (gen_lshrsi3 (tmp2, gen_lowpart (SImode, tmp1), GEN_INT (15)));
169 emit_insn (gen_ashlsi3 (tmp3, gen_highpart (SImode, tmp1), GEN_INT (17)));
170 emit_insn (gen_iorsi3 (gen_lowpart (SImode, operands[0]), tmp2, tmp3));
175 (define_expand "mulusa3"
176 [(set (match_operand:USA 0 "s_register_operand" "")
177 (mult:USA (match_operand:USA 1 "s_register_operand" "")
178 (match_operand:USA 2 "s_register_operand" "")))]
179 "TARGET_32BIT && arm_arch3m"
181 rtx tmp1 = gen_reg_rtx (DImode);
182 rtx tmp2 = gen_reg_rtx (SImode);
183 rtx tmp3 = gen_reg_rtx (SImode);
185 emit_insn (gen_umulsidi3 (tmp1, gen_lowpart (SImode, operands[1]),
186 gen_lowpart (SImode, operands[2])));
187 emit_insn (gen_lshrsi3 (tmp2, gen_lowpart (SImode, tmp1), GEN_INT (16)));
188 emit_insn (gen_ashlsi3 (tmp3, gen_highpart (SImode, tmp1), GEN_INT (16)));
189 emit_insn (gen_iorsi3 (gen_lowpart (SImode, operands[0]), tmp2, tmp3));
194 ;; The code sequence emitted by this insn pattern uses the Q flag, which GCC
195 ;; doesn't generally know about, so we don't bother expanding to individual
196 ;; instructions. It may be better to just use an out-of-line asm libcall for
199 (define_insn "ssmulsa3"
200 [(set (match_operand:SA 0 "s_register_operand" "=r")
201 (ss_mult:SA (match_operand:SA 1 "s_register_operand" "r")
202 (match_operand:SA 2 "s_register_operand" "r")))
203 (clobber (match_scratch:DI 3 "=r"))
204 (clobber (match_scratch:SI 4 "=r"))
205 (clobber (reg:CC CC_REGNUM))]
206 "TARGET_32BIT && arm_arch6"
208 /* s16.15 * s16.15 -> s32.30. */
209 output_asm_insn ("smull\\t%Q3, %R3, %1, %2", operands);
212 output_asm_insn ("msr\\tAPSR_nzcvq, #0", operands);
215 output_asm_insn ("mov\\t%4, #0", operands);
216 output_asm_insn ("msr\\tAPSR_nzcvq, %4", operands);
220 31 high word 0 31 low word 0
222 [ S i i .... i i i ] [ i f f f ... f f ]
225 [ S i ... i f ... f f ]
227 Need 16 integral bits, so saturate at 15th bit of high word. */
229 output_asm_insn ("ssat\\t%R3, #15, %R3", operands);
230 output_asm_insn ("mrs\\t%4, APSR", operands);
231 output_asm_insn ("tst\\t%4, #1<<27", operands);
234 output_asm_insn ("mvn\\t%4, %R3, asr #32", operands);
235 output_asm_insn ("it\\tne", operands);
236 output_asm_insn ("movne\\t%Q3, %4", operands);
241 output_asm_insn ("it\\tne", operands);
242 output_asm_insn ("mvnne\\t%Q3, %R3, asr #32", operands);
244 output_asm_insn ("mov\\t%0, %Q3, lsr #15", operands);
245 output_asm_insn ("orr\\t%0, %0, %R3, asl #17", operands);
248 [(set_attr "conds" "clob")
250 (if_then_else (eq_attr "is_thumb" "yes")
251 (if_then_else (match_test "arm_restrict_it")
256 ;; Same goes for this.
258 (define_insn "usmulusa3"
259 [(set (match_operand:USA 0 "s_register_operand" "=r")
260 (us_mult:USA (match_operand:USA 1 "s_register_operand" "r")
261 (match_operand:USA 2 "s_register_operand" "r")))
262 (clobber (match_scratch:DI 3 "=r"))
263 (clobber (match_scratch:SI 4 "=r"))
264 (clobber (reg:CC CC_REGNUM))]
265 "TARGET_32BIT && arm_arch6"
267 /* 16.16 * 16.16 -> 32.32. */
268 output_asm_insn ("umull\\t%Q3, %R3, %1, %2", operands);
271 output_asm_insn ("msr\\tAPSR_nzcvq, #0", operands);
274 output_asm_insn ("mov\\t%4, #0", operands);
275 output_asm_insn ("msr\\tAPSR_nzcvq, %4", operands);
279 31 high word 0 31 low word 0
281 [ i i i .... i i i ] [ f f f f ... f f ]
284 [ i i ... i f ... f f ]
286 Need 16 integral bits, so saturate at 16th bit of high word. */
288 output_asm_insn ("usat\\t%R3, #16, %R3", operands);
289 output_asm_insn ("mrs\\t%4, APSR", operands);
290 output_asm_insn ("tst\\t%4, #1<<27", operands);
293 output_asm_insn ("sbfx\\t%4, %R3, #15, #1", operands);
294 output_asm_insn ("it\\tne", operands);
295 output_asm_insn ("movne\\t%Q3, %4", operands);
300 output_asm_insn ("it\\tne", operands);
301 output_asm_insn ("sbfxne\\t%Q3, %R3, #15, #1", operands);
303 output_asm_insn ("lsr\\t%0, %Q3, #16", operands);
304 output_asm_insn ("orr\\t%0, %0, %R3, asl #16", operands);
307 [(set_attr "conds" "clob")
309 (if_then_else (eq_attr "is_thumb" "yes")
310 (if_then_else (match_test "arm_restrict_it")
315 (define_expand "mulha3"
316 [(set (match_operand:HA 0 "s_register_operand" "")
317 (mult:HA (match_operand:HA 1 "s_register_operand" "")
318 (match_operand:HA 2 "s_register_operand" "")))]
319 "TARGET_DSP_MULTIPLY && arm_arch_thumb2"
321 rtx tmp = gen_reg_rtx (SImode);
323 emit_insn (gen_mulhisi3 (tmp, gen_lowpart (HImode, operands[1]),
324 gen_lowpart (HImode, operands[2])));
325 emit_insn (gen_extv (gen_lowpart (SImode, operands[0]), tmp, GEN_INT (16),
331 (define_expand "muluha3"
332 [(set (match_operand:UHA 0 "s_register_operand" "")
333 (mult:UHA (match_operand:UHA 1 "s_register_operand" "")
334 (match_operand:UHA 2 "s_register_operand" "")))]
335 "TARGET_DSP_MULTIPLY"
337 rtx tmp1 = gen_reg_rtx (SImode);
338 rtx tmp2 = gen_reg_rtx (SImode);
339 rtx tmp3 = gen_reg_rtx (SImode);
341 /* 8.8 * 8.8 -> 16.16 multiply. */
342 emit_insn (gen_zero_extendhisi2 (tmp1, gen_lowpart (HImode, operands[1])));
343 emit_insn (gen_zero_extendhisi2 (tmp2, gen_lowpart (HImode, operands[2])));
344 emit_insn (gen_mulsi3 (tmp3, tmp1, tmp2));
345 emit_insn (gen_extzv (gen_lowpart (SImode, operands[0]), tmp3,
346 GEN_INT (16), GEN_INT (8)));
351 (define_expand "ssmulha3"
352 [(set (match_operand:HA 0 "s_register_operand" "")
353 (ss_mult:HA (match_operand:HA 1 "s_register_operand" "")
354 (match_operand:HA 2 "s_register_operand" "")))]
355 "TARGET_32BIT && TARGET_DSP_MULTIPLY && arm_arch6"
357 rtx tmp = gen_reg_rtx (SImode);
360 emit_insn (gen_mulhisi3 (tmp, gen_lowpart (HImode, operands[1]),
361 gen_lowpart (HImode, operands[2])));
363 rshift = gen_rtx_ASHIFTRT (SImode, tmp, GEN_INT (7));
365 emit_insn (gen_rtx_SET (VOIDmode, gen_lowpart (HImode, operands[0]),
366 gen_rtx_SS_TRUNCATE (HImode, rshift)));
371 (define_expand "usmuluha3"
372 [(set (match_operand:UHA 0 "s_register_operand" "")
373 (us_mult:UHA (match_operand:UHA 1 "s_register_operand" "")
374 (match_operand:UHA 2 "s_register_operand" "")))]
377 rtx tmp1 = gen_reg_rtx (SImode);
378 rtx tmp2 = gen_reg_rtx (SImode);
379 rtx tmp3 = gen_reg_rtx (SImode);
380 rtx rshift_tmp = gen_reg_rtx (SImode);
382 /* Note: there's no smul[bt][bt] equivalent for unsigned multiplies. Use a
383 normal 32x32->32-bit multiply instead. */
384 emit_insn (gen_zero_extendhisi2 (tmp1, gen_lowpart (HImode, operands[1])));
385 emit_insn (gen_zero_extendhisi2 (tmp2, gen_lowpart (HImode, operands[2])));
387 emit_insn (gen_mulsi3 (tmp3, tmp1, tmp2));
389 /* The operand to "usat" is signed, so we cannot use the "..., asr #8"
390 form of that instruction since the multiplication result TMP3 may have the
391 top bit set, thus be negative and saturate to zero. Use a separate
392 logical right-shift instead. */
393 emit_insn (gen_lshrsi3 (rshift_tmp, tmp3, GEN_INT (8)));
394 emit_insn (gen_arm_usatsihi (gen_lowpart (HImode, operands[0]), rshift_tmp));
399 (define_insn "arm_ssatsihi_shift"
400 [(set (match_operand:HI 0 "s_register_operand" "=r")
401 (ss_truncate:HI (match_operator:SI 1 "sat_shift_operator"
402 [(match_operand:SI 2 "s_register_operand" "r")
403 (match_operand:SI 3 "immediate_operand" "I")])))]
404 "TARGET_32BIT && arm_arch6"
405 "ssat%?\\t%0, #16, %2%S1"
406 [(set_attr "predicable" "yes")
407 (set_attr "predicable_short_it" "no")
408 (set_attr "shift" "1")
409 (set_attr "type" "arlo_shift")])
411 (define_insn "arm_usatsihi"
412 [(set (match_operand:HI 0 "s_register_operand" "=r")
413 (us_truncate:HI (match_operand:SI 1 "s_register_operand")))]
415 "usat%?\\t%0, #16, %1"
416 [(set_attr "predicable" "yes")
417 (set_attr "predicable_short_it" "no")]