1 ; NOTE: Assertions have been autogenerated by utils/update_llc_test_checks.py
2 ; RUN: llc -mtriple=riscv32 -verify-machineinstrs < %s | FileCheck -check-prefix=RV32I %s
3 ; RUN: llc -mtriple=riscv64 -verify-machineinstrs < %s | FileCheck -check-prefix=RV64I %s
6 ; fixed avg(x,y) = add(and(x,y),lshr(xor(x,y),1))
8 ; ext avg(x,y) = trunc(lshr(add(zext(x),zext(y)),1))
11 define i8 @test_fixed_i8(i8 %a0, i8 %a1) nounwind {
12 ; RV32I-LABEL: test_fixed_i8:
14 ; RV32I-NEXT: andi a1, a1, 255
15 ; RV32I-NEXT: andi a0, a0, 255
16 ; RV32I-NEXT: add a0, a0, a1
17 ; RV32I-NEXT: srli a0, a0, 1
20 ; RV64I-LABEL: test_fixed_i8:
22 ; RV64I-NEXT: andi a1, a1, 255
23 ; RV64I-NEXT: andi a0, a0, 255
24 ; RV64I-NEXT: add a0, a0, a1
25 ; RV64I-NEXT: srli a0, a0, 1
27 %and = and i8 %a0, %a1
28 %xor = xor i8 %a0, %a1
29 %shift = lshr i8 %xor, 1
30 %res = add i8 %and, %shift
34 define i8 @test_ext_i8(i8 %a0, i8 %a1) nounwind {
35 ; RV32I-LABEL: test_ext_i8:
37 ; RV32I-NEXT: andi a1, a1, 255
38 ; RV32I-NEXT: andi a0, a0, 255
39 ; RV32I-NEXT: add a0, a0, a1
40 ; RV32I-NEXT: srli a0, a0, 1
43 ; RV64I-LABEL: test_ext_i8:
45 ; RV64I-NEXT: andi a1, a1, 255
46 ; RV64I-NEXT: andi a0, a0, 255
47 ; RV64I-NEXT: add a0, a0, a1
48 ; RV64I-NEXT: srli a0, a0, 1
50 %x0 = zext i8 %a0 to i16
51 %x1 = zext i8 %a1 to i16
52 %sum = add i16 %x0, %x1
53 %shift = lshr i16 %sum, 1
54 %res = trunc i16 %shift to i8
58 define i16 @test_fixed_i16(i16 %a0, i16 %a1) nounwind {
59 ; RV32I-LABEL: test_fixed_i16:
61 ; RV32I-NEXT: lui a2, 16
62 ; RV32I-NEXT: addi a2, a2, -1
63 ; RV32I-NEXT: and a1, a1, a2
64 ; RV32I-NEXT: and a0, a0, a2
65 ; RV32I-NEXT: add a0, a0, a1
66 ; RV32I-NEXT: srli a0, a0, 1
69 ; RV64I-LABEL: test_fixed_i16:
71 ; RV64I-NEXT: lui a2, 16
72 ; RV64I-NEXT: addiw a2, a2, -1
73 ; RV64I-NEXT: and a1, a1, a2
74 ; RV64I-NEXT: and a0, a0, a2
75 ; RV64I-NEXT: add a0, a0, a1
76 ; RV64I-NEXT: srli a0, a0, 1
78 %and = and i16 %a0, %a1
79 %xor = xor i16 %a0, %a1
80 %shift = lshr i16 %xor, 1
81 %res = add i16 %and, %shift
85 define i16 @test_ext_i16(i16 %a0, i16 %a1) nounwind {
86 ; RV32I-LABEL: test_ext_i16:
88 ; RV32I-NEXT: lui a2, 16
89 ; RV32I-NEXT: addi a2, a2, -1
90 ; RV32I-NEXT: and a1, a1, a2
91 ; RV32I-NEXT: and a0, a0, a2
92 ; RV32I-NEXT: add a0, a0, a1
93 ; RV32I-NEXT: srli a0, a0, 1
96 ; RV64I-LABEL: test_ext_i16:
98 ; RV64I-NEXT: lui a2, 16
99 ; RV64I-NEXT: addiw a2, a2, -1
100 ; RV64I-NEXT: and a1, a1, a2
101 ; RV64I-NEXT: and a0, a0, a2
102 ; RV64I-NEXT: add a0, a0, a1
103 ; RV64I-NEXT: srli a0, a0, 1
105 %x0 = zext i16 %a0 to i32
106 %x1 = zext i16 %a1 to i32
107 %sum = add i32 %x0, %x1
108 %shift = lshr i32 %sum, 1
109 %res = trunc i32 %shift to i16
113 define i32 @test_fixed_i32(i32 %a0, i32 %a1) nounwind {
114 ; RV32I-LABEL: test_fixed_i32:
116 ; RV32I-NEXT: and a2, a0, a1
117 ; RV32I-NEXT: xor a0, a0, a1
118 ; RV32I-NEXT: srli a0, a0, 1
119 ; RV32I-NEXT: add a0, a2, a0
122 ; RV64I-LABEL: test_fixed_i32:
124 ; RV64I-NEXT: slli a1, a1, 32
125 ; RV64I-NEXT: srli a1, a1, 32
126 ; RV64I-NEXT: slli a0, a0, 32
127 ; RV64I-NEXT: srli a0, a0, 32
128 ; RV64I-NEXT: add a0, a0, a1
129 ; RV64I-NEXT: srli a0, a0, 1
131 %and = and i32 %a0, %a1
132 %xor = xor i32 %a1, %a0
133 %shift = lshr i32 %xor, 1
134 %res = add i32 %and, %shift
138 define i32 @test_ext_i32(i32 %a0, i32 %a1) nounwind {
139 ; RV32I-LABEL: test_ext_i32:
141 ; RV32I-NEXT: and a2, a0, a1
142 ; RV32I-NEXT: xor a0, a0, a1
143 ; RV32I-NEXT: srli a0, a0, 1
144 ; RV32I-NEXT: add a0, a2, a0
147 ; RV64I-LABEL: test_ext_i32:
149 ; RV64I-NEXT: slli a1, a1, 32
150 ; RV64I-NEXT: srli a1, a1, 32
151 ; RV64I-NEXT: slli a0, a0, 32
152 ; RV64I-NEXT: srli a0, a0, 32
153 ; RV64I-NEXT: add a0, a0, a1
154 ; RV64I-NEXT: srli a0, a0, 1
156 %x0 = zext i32 %a0 to i64
157 %x1 = zext i32 %a1 to i64
158 %sum = add i64 %x0, %x1
159 %shift = lshr i64 %sum, 1
160 %res = trunc i64 %shift to i32
164 define i64 @test_fixed_i64(i64 %a0, i64 %a1) nounwind {
165 ; RV32I-LABEL: test_fixed_i64:
167 ; RV32I-NEXT: and a4, a1, a3
168 ; RV32I-NEXT: xor a1, a1, a3
169 ; RV32I-NEXT: srli a3, a1, 1
170 ; RV32I-NEXT: add a3, a4, a3
171 ; RV32I-NEXT: slli a1, a1, 31
172 ; RV32I-NEXT: xor a4, a0, a2
173 ; RV32I-NEXT: srli a4, a4, 1
174 ; RV32I-NEXT: or a1, a4, a1
175 ; RV32I-NEXT: and a2, a0, a2
176 ; RV32I-NEXT: add a0, a2, a1
177 ; RV32I-NEXT: sltu a1, a0, a2
178 ; RV32I-NEXT: add a1, a3, a1
181 ; RV64I-LABEL: test_fixed_i64:
183 ; RV64I-NEXT: and a2, a0, a1
184 ; RV64I-NEXT: xor a0, a0, a1
185 ; RV64I-NEXT: srli a0, a0, 1
186 ; RV64I-NEXT: add a0, a2, a0
188 %and = and i64 %a0, %a1
189 %xor = xor i64 %a1, %a0
190 %shift = lshr i64 %xor, 1
191 %res = add i64 %and, %shift
195 define i64 @test_ext_i64(i64 %a0, i64 %a1) nounwind {
196 ; RV32I-LABEL: test_ext_i64:
198 ; RV32I-NEXT: and a4, a1, a3
199 ; RV32I-NEXT: xor a1, a1, a3
200 ; RV32I-NEXT: srli a3, a1, 1
201 ; RV32I-NEXT: add a3, a4, a3
202 ; RV32I-NEXT: slli a1, a1, 31
203 ; RV32I-NEXT: xor a4, a0, a2
204 ; RV32I-NEXT: srli a4, a4, 1
205 ; RV32I-NEXT: or a1, a4, a1
206 ; RV32I-NEXT: and a2, a0, a2
207 ; RV32I-NEXT: add a0, a2, a1
208 ; RV32I-NEXT: sltu a1, a0, a2
209 ; RV32I-NEXT: add a1, a3, a1
212 ; RV64I-LABEL: test_ext_i64:
214 ; RV64I-NEXT: and a2, a0, a1
215 ; RV64I-NEXT: xor a0, a0, a1
216 ; RV64I-NEXT: srli a0, a0, 1
217 ; RV64I-NEXT: add a0, a2, a0
219 %x0 = zext i64 %a0 to i128
220 %x1 = zext i64 %a1 to i128
221 %sum = add i128 %x0, %x1
222 %shift = lshr i128 %sum, 1
223 %res = trunc i128 %shift to i64