]>
Commit | Line | Data |
---|---|---|
d3e35a1f AG |
1 | /* |
2 | * AArch64 specific helpers | |
3 | * | |
4 | * Copyright (c) 2013 Alexander Graf <[email protected]> | |
5 | * | |
6 | * This library 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 of the License, or (at your option) any later version. | |
10 | * | |
11 | * This library 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 this library; if not, see <http://www.gnu.org/licenses/>. | |
18 | */ | |
19 | ||
74c21bd0 | 20 | #include "qemu/osdep.h" |
d3e35a1f AG |
21 | #include "cpu.h" |
22 | #include "exec/gdbstub.h" | |
2ef6175a | 23 | #include "exec/helper-proto.h" |
d3e35a1f | 24 | #include "qemu/host-utils.h" |
63c91552 | 25 | #include "qemu/log.h" |
d3e35a1f AG |
26 | #include "sysemu/sysemu.h" |
27 | #include "qemu/bitops.h" | |
52e60cdd | 28 | #include "internals.h" |
130f2e7d | 29 | #include "qemu/crc32c.h" |
1dd089d0 EC |
30 | #include "exec/exec-all.h" |
31 | #include "exec/cpu_ldst.h" | |
32 | #include "qemu/int128.h" | |
1ec182c3 | 33 | #include "qemu/atomic128.h" |
1dd089d0 | 34 | #include "tcg.h" |
24f91e81 | 35 | #include "fpu/softfloat.h" |
130f2e7d | 36 | #include <zlib.h> /* For crc32 */ |
8220e911 AG |
37 | |
38 | /* C2.4.7 Multiply and divide */ | |
39 | /* special cases for 0 and LLONG_MIN are mandated by the standard */ | |
40 | uint64_t HELPER(udiv64)(uint64_t num, uint64_t den) | |
41 | { | |
42 | if (den == 0) { | |
43 | return 0; | |
44 | } | |
45 | return num / den; | |
46 | } | |
47 | ||
48 | int64_t HELPER(sdiv64)(int64_t num, int64_t den) | |
49 | { | |
50 | if (den == 0) { | |
51 | return 0; | |
52 | } | |
53 | if (num == LLONG_MIN && den == -1) { | |
54 | return LLONG_MIN; | |
55 | } | |
56 | return num / den; | |
57 | } | |
680ead21 | 58 | |
82e14b02 AG |
59 | uint64_t HELPER(rbit64)(uint64_t x) |
60 | { | |
42fedbca | 61 | return revbit64(x); |
82e14b02 | 62 | } |
da7dafe7 CF |
63 | |
64 | /* Convert a softfloat float_relation_ (as returned by | |
65 | * the float*_compare functions) to the correct ARM | |
66 | * NZCV flag state. | |
67 | */ | |
68 | static inline uint32_t float_rel_to_flags(int res) | |
69 | { | |
70 | uint64_t flags; | |
71 | switch (res) { | |
72 | case float_relation_equal: | |
73 | flags = PSTATE_Z | PSTATE_C; | |
74 | break; | |
75 | case float_relation_less: | |
76 | flags = PSTATE_N; | |
77 | break; | |
78 | case float_relation_greater: | |
79 | flags = PSTATE_C; | |
80 | break; | |
81 | case float_relation_unordered: | |
82 | default: | |
83 | flags = PSTATE_C | PSTATE_V; | |
84 | break; | |
85 | } | |
86 | return flags; | |
87 | } | |
88 | ||
6c2be133 | 89 | uint64_t HELPER(vfp_cmph_a64)(uint32_t x, uint32_t y, void *fp_status) |
7a192925 AB |
90 | { |
91 | return float_rel_to_flags(float16_compare_quiet(x, y, fp_status)); | |
92 | } | |
93 | ||
6c2be133 | 94 | uint64_t HELPER(vfp_cmpeh_a64)(uint32_t x, uint32_t y, void *fp_status) |
7a192925 AB |
95 | { |
96 | return float_rel_to_flags(float16_compare(x, y, fp_status)); | |
97 | } | |
98 | ||
da7dafe7 CF |
99 | uint64_t HELPER(vfp_cmps_a64)(float32 x, float32 y, void *fp_status) |
100 | { | |
101 | return float_rel_to_flags(float32_compare_quiet(x, y, fp_status)); | |
102 | } | |
103 | ||
104 | uint64_t HELPER(vfp_cmpes_a64)(float32 x, float32 y, void *fp_status) | |
105 | { | |
106 | return float_rel_to_flags(float32_compare(x, y, fp_status)); | |
107 | } | |
108 | ||
109 | uint64_t HELPER(vfp_cmpd_a64)(float64 x, float64 y, void *fp_status) | |
110 | { | |
111 | return float_rel_to_flags(float64_compare_quiet(x, y, fp_status)); | |
112 | } | |
113 | ||
114 | uint64_t HELPER(vfp_cmped_a64)(float64 x, float64 y, void *fp_status) | |
115 | { | |
116 | return float_rel_to_flags(float64_compare(x, y, fp_status)); | |
117 | } | |
7c51048f | 118 | |
f5e51e7f PM |
119 | float32 HELPER(vfp_mulxs)(float32 a, float32 b, void *fpstp) |
120 | { | |
121 | float_status *fpst = fpstp; | |
122 | ||
dabf0058 XH |
123 | a = float32_squash_input_denormal(a, fpst); |
124 | b = float32_squash_input_denormal(b, fpst); | |
125 | ||
f5e51e7f PM |
126 | if ((float32_is_zero(a) && float32_is_infinity(b)) || |
127 | (float32_is_infinity(a) && float32_is_zero(b))) { | |
128 | /* 2.0 with the sign bit set to sign(A) XOR sign(B) */ | |
129 | return make_float32((1U << 30) | | |
130 | ((float32_val(a) ^ float32_val(b)) & (1U << 31))); | |
131 | } | |
132 | return float32_mul(a, b, fpst); | |
133 | } | |
134 | ||
135 | float64 HELPER(vfp_mulxd)(float64 a, float64 b, void *fpstp) | |
136 | { | |
137 | float_status *fpst = fpstp; | |
138 | ||
dabf0058 XH |
139 | a = float64_squash_input_denormal(a, fpst); |
140 | b = float64_squash_input_denormal(b, fpst); | |
141 | ||
f5e51e7f PM |
142 | if ((float64_is_zero(a) && float64_is_infinity(b)) || |
143 | (float64_is_infinity(a) && float64_is_zero(b))) { | |
144 | /* 2.0 with the sign bit set to sign(A) XOR sign(B) */ | |
145 | return make_float64((1ULL << 62) | | |
146 | ((float64_val(a) ^ float64_val(b)) & (1ULL << 63))); | |
147 | } | |
148 | return float64_mul(a, b, fpst); | |
149 | } | |
150 | ||
7c51048f MM |
151 | uint64_t HELPER(simd_tbl)(CPUARMState *env, uint64_t result, uint64_t indices, |
152 | uint32_t rn, uint32_t numregs) | |
153 | { | |
154 | /* Helper function for SIMD TBL and TBX. We have to do the table | |
155 | * lookup part for the 64 bits worth of indices we're passed in. | |
156 | * result is the initial results vector (either zeroes for TBL | |
157 | * or some guest values for TBX), rn the register number where | |
158 | * the table starts, and numregs the number of registers in the table. | |
159 | * We return the results of the lookups. | |
160 | */ | |
161 | int shift; | |
162 | ||
163 | for (shift = 0; shift < 64; shift += 8) { | |
164 | int index = extract64(indices, shift, 8); | |
165 | if (index < 16 * numregs) { | |
166 | /* Convert index (a byte offset into the virtual table | |
167 | * which is a series of 128-bit vectors concatenated) | |
9a2b5256 | 168 | * into the correct register element plus a bit offset |
7c51048f MM |
169 | * into that element, bearing in mind that the table |
170 | * can wrap around from V31 to V0. | |
171 | */ | |
172 | int elt = (rn * 2 + (index >> 3)) % 64; | |
173 | int bitidx = (index & 7) * 8; | |
9a2b5256 RH |
174 | uint64_t *q = aa64_vfp_qreg(env, elt >> 1); |
175 | uint64_t val = extract64(q[elt & 1], bitidx, 8); | |
7c51048f MM |
176 | |
177 | result = deposit64(result, shift, 8, val); | |
178 | } | |
179 | } | |
180 | return result; | |
181 | } | |
8908f4d1 AB |
182 | |
183 | /* 64bit/double versions of the neon float compare functions */ | |
184 | uint64_t HELPER(neon_ceq_f64)(float64 a, float64 b, void *fpstp) | |
185 | { | |
186 | float_status *fpst = fpstp; | |
187 | return -float64_eq_quiet(a, b, fpst); | |
188 | } | |
189 | ||
190 | uint64_t HELPER(neon_cge_f64)(float64 a, float64 b, void *fpstp) | |
191 | { | |
192 | float_status *fpst = fpstp; | |
193 | return -float64_le(b, a, fpst); | |
194 | } | |
195 | ||
196 | uint64_t HELPER(neon_cgt_f64)(float64 a, float64 b, void *fpstp) | |
197 | { | |
198 | float_status *fpst = fpstp; | |
199 | return -float64_lt(b, a, fpst); | |
200 | } | |
057d5f62 PM |
201 | |
202 | /* Reciprocal step and sqrt step. Note that unlike the A32/T32 | |
203 | * versions, these do a fully fused multiply-add or | |
204 | * multiply-add-and-halve. | |
205 | */ | |
026e2d6e AB |
206 | #define float16_two make_float16(0x4000) |
207 | #define float16_three make_float16(0x4200) | |
208 | #define float16_one_point_five make_float16(0x3e00) | |
209 | ||
057d5f62 PM |
210 | #define float32_two make_float32(0x40000000) |
211 | #define float32_three make_float32(0x40400000) | |
212 | #define float32_one_point_five make_float32(0x3fc00000) | |
213 | ||
214 | #define float64_two make_float64(0x4000000000000000ULL) | |
215 | #define float64_three make_float64(0x4008000000000000ULL) | |
216 | #define float64_one_point_five make_float64(0x3FF8000000000000ULL) | |
217 | ||
6c2be133 | 218 | uint32_t HELPER(recpsf_f16)(uint32_t a, uint32_t b, void *fpstp) |
026e2d6e AB |
219 | { |
220 | float_status *fpst = fpstp; | |
221 | ||
222 | a = float16_squash_input_denormal(a, fpst); | |
223 | b = float16_squash_input_denormal(b, fpst); | |
224 | ||
225 | a = float16_chs(a); | |
226 | if ((float16_is_infinity(a) && float16_is_zero(b)) || | |
227 | (float16_is_infinity(b) && float16_is_zero(a))) { | |
228 | return float16_two; | |
229 | } | |
230 | return float16_muladd(a, b, float16_two, 0, fpst); | |
231 | } | |
232 | ||
057d5f62 PM |
233 | float32 HELPER(recpsf_f32)(float32 a, float32 b, void *fpstp) |
234 | { | |
235 | float_status *fpst = fpstp; | |
236 | ||
a8eb6e19 PM |
237 | a = float32_squash_input_denormal(a, fpst); |
238 | b = float32_squash_input_denormal(b, fpst); | |
239 | ||
057d5f62 PM |
240 | a = float32_chs(a); |
241 | if ((float32_is_infinity(a) && float32_is_zero(b)) || | |
242 | (float32_is_infinity(b) && float32_is_zero(a))) { | |
243 | return float32_two; | |
244 | } | |
245 | return float32_muladd(a, b, float32_two, 0, fpst); | |
246 | } | |
247 | ||
248 | float64 HELPER(recpsf_f64)(float64 a, float64 b, void *fpstp) | |
249 | { | |
250 | float_status *fpst = fpstp; | |
251 | ||
a8eb6e19 PM |
252 | a = float64_squash_input_denormal(a, fpst); |
253 | b = float64_squash_input_denormal(b, fpst); | |
254 | ||
057d5f62 PM |
255 | a = float64_chs(a); |
256 | if ((float64_is_infinity(a) && float64_is_zero(b)) || | |
257 | (float64_is_infinity(b) && float64_is_zero(a))) { | |
258 | return float64_two; | |
259 | } | |
260 | return float64_muladd(a, b, float64_two, 0, fpst); | |
261 | } | |
262 | ||
6c2be133 | 263 | uint32_t HELPER(rsqrtsf_f16)(uint32_t a, uint32_t b, void *fpstp) |
026e2d6e AB |
264 | { |
265 | float_status *fpst = fpstp; | |
266 | ||
267 | a = float16_squash_input_denormal(a, fpst); | |
268 | b = float16_squash_input_denormal(b, fpst); | |
269 | ||
270 | a = float16_chs(a); | |
271 | if ((float16_is_infinity(a) && float16_is_zero(b)) || | |
272 | (float16_is_infinity(b) && float16_is_zero(a))) { | |
273 | return float16_one_point_five; | |
274 | } | |
275 | return float16_muladd(a, b, float16_three, float_muladd_halve_result, fpst); | |
276 | } | |
277 | ||
057d5f62 PM |
278 | float32 HELPER(rsqrtsf_f32)(float32 a, float32 b, void *fpstp) |
279 | { | |
280 | float_status *fpst = fpstp; | |
281 | ||
a8eb6e19 PM |
282 | a = float32_squash_input_denormal(a, fpst); |
283 | b = float32_squash_input_denormal(b, fpst); | |
284 | ||
057d5f62 PM |
285 | a = float32_chs(a); |
286 | if ((float32_is_infinity(a) && float32_is_zero(b)) || | |
287 | (float32_is_infinity(b) && float32_is_zero(a))) { | |
288 | return float32_one_point_five; | |
289 | } | |
290 | return float32_muladd(a, b, float32_three, float_muladd_halve_result, fpst); | |
291 | } | |
292 | ||
293 | float64 HELPER(rsqrtsf_f64)(float64 a, float64 b, void *fpstp) | |
294 | { | |
295 | float_status *fpst = fpstp; | |
296 | ||
a8eb6e19 PM |
297 | a = float64_squash_input_denormal(a, fpst); |
298 | b = float64_squash_input_denormal(b, fpst); | |
299 | ||
057d5f62 PM |
300 | a = float64_chs(a); |
301 | if ((float64_is_infinity(a) && float64_is_zero(b)) || | |
302 | (float64_is_infinity(b) && float64_is_zero(a))) { | |
303 | return float64_one_point_five; | |
304 | } | |
305 | return float64_muladd(a, b, float64_three, float_muladd_halve_result, fpst); | |
306 | } | |
6781fa11 PM |
307 | |
308 | /* Pairwise long add: add pairs of adjacent elements into | |
309 | * double-width elements in the result (eg _s8 is an 8x8->16 op) | |
310 | */ | |
311 | uint64_t HELPER(neon_addlp_s8)(uint64_t a) | |
312 | { | |
313 | uint64_t nsignmask = 0x0080008000800080ULL; | |
314 | uint64_t wsignmask = 0x8000800080008000ULL; | |
315 | uint64_t elementmask = 0x00ff00ff00ff00ffULL; | |
316 | uint64_t tmp1, tmp2; | |
317 | uint64_t res, signres; | |
318 | ||
319 | /* Extract odd elements, sign extend each to a 16 bit field */ | |
320 | tmp1 = a & elementmask; | |
321 | tmp1 ^= nsignmask; | |
322 | tmp1 |= wsignmask; | |
323 | tmp1 = (tmp1 - nsignmask) ^ wsignmask; | |
324 | /* Ditto for the even elements */ | |
325 | tmp2 = (a >> 8) & elementmask; | |
326 | tmp2 ^= nsignmask; | |
327 | tmp2 |= wsignmask; | |
328 | tmp2 = (tmp2 - nsignmask) ^ wsignmask; | |
329 | ||
330 | /* calculate the result by summing bits 0..14, 16..22, etc, | |
331 | * and then adjusting the sign bits 15, 23, etc manually. | |
332 | * This ensures the addition can't overflow the 16 bit field. | |
333 | */ | |
334 | signres = (tmp1 ^ tmp2) & wsignmask; | |
335 | res = (tmp1 & ~wsignmask) + (tmp2 & ~wsignmask); | |
336 | res ^= signres; | |
337 | ||
338 | return res; | |
339 | } | |
340 | ||
341 | uint64_t HELPER(neon_addlp_u8)(uint64_t a) | |
342 | { | |
343 | uint64_t tmp; | |
344 | ||
345 | tmp = a & 0x00ff00ff00ff00ffULL; | |
346 | tmp += (a >> 8) & 0x00ff00ff00ff00ffULL; | |
347 | return tmp; | |
348 | } | |
349 | ||
350 | uint64_t HELPER(neon_addlp_s16)(uint64_t a) | |
351 | { | |
352 | int32_t reslo, reshi; | |
353 | ||
354 | reslo = (int32_t)(int16_t)a + (int32_t)(int16_t)(a >> 16); | |
355 | reshi = (int32_t)(int16_t)(a >> 32) + (int32_t)(int16_t)(a >> 48); | |
356 | ||
357 | return (uint32_t)reslo | (((uint64_t)reshi) << 32); | |
358 | } | |
359 | ||
360 | uint64_t HELPER(neon_addlp_u16)(uint64_t a) | |
361 | { | |
362 | uint64_t tmp; | |
363 | ||
364 | tmp = a & 0x0000ffff0000ffffULL; | |
365 | tmp += (a >> 16) & 0x0000ffff0000ffffULL; | |
366 | return tmp; | |
367 | } | |
8f0c6758 AB |
368 | |
369 | /* Floating-point reciprocal exponent - see FPRecpX in ARM ARM */ | |
6c2be133 | 370 | uint32_t HELPER(frecpx_f16)(uint32_t a, void *fpstp) |
98695028 AB |
371 | { |
372 | float_status *fpst = fpstp; | |
373 | uint16_t val16, sbit; | |
374 | int16_t exp; | |
375 | ||
376 | if (float16_is_any_nan(a)) { | |
377 | float16 nan = a; | |
378 | if (float16_is_signaling_nan(a, fpst)) { | |
379 | float_raise(float_flag_invalid, fpst); | |
d7ecc062 | 380 | nan = float16_silence_nan(a, fpst); |
98695028 AB |
381 | } |
382 | if (fpst->default_nan_mode) { | |
383 | nan = float16_default_nan(fpst); | |
384 | } | |
385 | return nan; | |
386 | } | |
387 | ||
2cfbf36e PM |
388 | a = float16_squash_input_denormal(a, fpst); |
389 | ||
98695028 AB |
390 | val16 = float16_val(a); |
391 | sbit = 0x8000 & val16; | |
392 | exp = extract32(val16, 10, 5); | |
393 | ||
394 | if (exp == 0) { | |
395 | return make_float16(deposit32(sbit, 10, 5, 0x1e)); | |
396 | } else { | |
397 | return make_float16(deposit32(sbit, 10, 5, ~exp)); | |
398 | } | |
399 | } | |
400 | ||
8f0c6758 AB |
401 | float32 HELPER(frecpx_f32)(float32 a, void *fpstp) |
402 | { | |
403 | float_status *fpst = fpstp; | |
404 | uint32_t val32, sbit; | |
405 | int32_t exp; | |
406 | ||
407 | if (float32_is_any_nan(a)) { | |
408 | float32 nan = a; | |
af39bc8c | 409 | if (float32_is_signaling_nan(a, fpst)) { |
8f0c6758 | 410 | float_raise(float_flag_invalid, fpst); |
d7ecc062 | 411 | nan = float32_silence_nan(a, fpst); |
8f0c6758 AB |
412 | } |
413 | if (fpst->default_nan_mode) { | |
af39bc8c | 414 | nan = float32_default_nan(fpst); |
8f0c6758 AB |
415 | } |
416 | return nan; | |
417 | } | |
418 | ||
2cfbf36e PM |
419 | a = float32_squash_input_denormal(a, fpst); |
420 | ||
8f0c6758 AB |
421 | val32 = float32_val(a); |
422 | sbit = 0x80000000ULL & val32; | |
423 | exp = extract32(val32, 23, 8); | |
424 | ||
425 | if (exp == 0) { | |
426 | return make_float32(sbit | (0xfe << 23)); | |
427 | } else { | |
428 | return make_float32(sbit | (~exp & 0xff) << 23); | |
429 | } | |
430 | } | |
431 | ||
432 | float64 HELPER(frecpx_f64)(float64 a, void *fpstp) | |
433 | { | |
434 | float_status *fpst = fpstp; | |
435 | uint64_t val64, sbit; | |
436 | int64_t exp; | |
437 | ||
438 | if (float64_is_any_nan(a)) { | |
439 | float64 nan = a; | |
af39bc8c | 440 | if (float64_is_signaling_nan(a, fpst)) { |
8f0c6758 | 441 | float_raise(float_flag_invalid, fpst); |
d7ecc062 | 442 | nan = float64_silence_nan(a, fpst); |
8f0c6758 AB |
443 | } |
444 | if (fpst->default_nan_mode) { | |
af39bc8c | 445 | nan = float64_default_nan(fpst); |
8f0c6758 AB |
446 | } |
447 | return nan; | |
448 | } | |
449 | ||
2cfbf36e PM |
450 | a = float64_squash_input_denormal(a, fpst); |
451 | ||
8f0c6758 AB |
452 | val64 = float64_val(a); |
453 | sbit = 0x8000000000000000ULL & val64; | |
454 | exp = extract64(float64_val(a), 52, 11); | |
455 | ||
456 | if (exp == 0) { | |
457 | return make_float64(sbit | (0x7feULL << 52)); | |
458 | } else { | |
459 | return make_float64(sbit | (~exp & 0x7ffULL) << 52); | |
460 | } | |
461 | } | |
5553955e PM |
462 | |
463 | float32 HELPER(fcvtx_f64_to_f32)(float64 a, CPUARMState *env) | |
464 | { | |
465 | /* Von Neumann rounding is implemented by using round-to-zero | |
466 | * and then setting the LSB of the result if Inexact was raised. | |
467 | */ | |
468 | float32 r; | |
469 | float_status *fpst = &env->vfp.fp_status; | |
470 | float_status tstat = *fpst; | |
471 | int exflags; | |
472 | ||
473 | set_float_rounding_mode(float_round_to_zero, &tstat); | |
474 | set_float_exception_flags(0, &tstat); | |
475 | r = float64_to_float32(a, &tstat); | |
5553955e PM |
476 | exflags = get_float_exception_flags(&tstat); |
477 | if (exflags & float_flag_inexact) { | |
478 | r = make_float32(float32_val(r) | 1); | |
479 | } | |
480 | exflags |= get_float_exception_flags(fpst); | |
481 | set_float_exception_flags(exflags, fpst); | |
482 | return r; | |
483 | } | |
52e60cdd | 484 | |
130f2e7d PM |
485 | /* 64-bit versions of the CRC helpers. Note that although the operation |
486 | * (and the prototypes of crc32c() and crc32() mean that only the bottom | |
487 | * 32 bits of the accumulator and result are used, we pass and return | |
488 | * uint64_t for convenience of the generated code. Unlike the 32-bit | |
489 | * instruction set versions, val may genuinely have 64 bits of data in it. | |
490 | * The upper bytes of val (above the number specified by 'bytes') must have | |
491 | * been zeroed out by the caller. | |
492 | */ | |
493 | uint64_t HELPER(crc32_64)(uint64_t acc, uint64_t val, uint32_t bytes) | |
494 | { | |
495 | uint8_t buf[8]; | |
496 | ||
497 | stq_le_p(buf, val); | |
498 | ||
499 | /* zlib crc32 converts the accumulator and output to one's complement. */ | |
500 | return crc32(acc ^ 0xffffffff, buf, bytes) ^ 0xffffffff; | |
501 | } | |
502 | ||
503 | uint64_t HELPER(crc32c_64)(uint64_t acc, uint64_t val, uint32_t bytes) | |
504 | { | |
505 | uint8_t buf[8]; | |
506 | ||
507 | stq_le_p(buf, val); | |
508 | ||
509 | /* Linux crc32c converts the output to one's complement. */ | |
510 | return crc32c(acc, buf, bytes) ^ 0xffffffff; | |
511 | } | |
1dd089d0 | 512 | |
1ec182c3 RH |
513 | uint64_t HELPER(paired_cmpxchg64_le)(CPUARMState *env, uint64_t addr, |
514 | uint64_t new_lo, uint64_t new_hi) | |
1dd089d0 | 515 | { |
1ec182c3 RH |
516 | Int128 cmpv = int128_make128(env->exclusive_val, env->exclusive_high); |
517 | Int128 newv = int128_make128(new_lo, new_hi); | |
518 | Int128 oldv; | |
519 | uintptr_t ra = GETPC(); | |
520 | uint64_t o0, o1; | |
1dd089d0 EC |
521 | bool success; |
522 | ||
1dd089d0 | 523 | #ifdef CONFIG_USER_ONLY |
1ec182c3 RH |
524 | /* ??? Enforce alignment. */ |
525 | uint64_t *haddr = g2h(addr); | |
526 | ||
527 | helper_retaddr = ra; | |
528 | o0 = ldq_le_p(haddr + 0); | |
529 | o1 = ldq_le_p(haddr + 1); | |
530 | oldv = int128_make128(o0, o1); | |
531 | ||
532 | success = int128_eq(oldv, cmpv); | |
533 | if (success) { | |
534 | stq_le_p(haddr + 0, int128_getlo(newv)); | |
535 | stq_le_p(haddr + 1, int128_gethi(newv)); | |
536 | } | |
537 | helper_retaddr = 0; | |
1dd089d0 | 538 | #else |
1ec182c3 RH |
539 | int mem_idx = cpu_mmu_index(env, false); |
540 | TCGMemOpIdx oi0 = make_memop_idx(MO_LEQ | MO_ALIGN_16, mem_idx); | |
541 | TCGMemOpIdx oi1 = make_memop_idx(MO_LEQ, mem_idx); | |
542 | ||
543 | o0 = helper_le_ldq_mmu(env, addr + 0, oi0, ra); | |
544 | o1 = helper_le_ldq_mmu(env, addr + 8, oi1, ra); | |
545 | oldv = int128_make128(o0, o1); | |
546 | ||
547 | success = int128_eq(oldv, cmpv); | |
548 | if (success) { | |
549 | helper_le_stq_mmu(env, addr + 0, int128_getlo(newv), oi1, ra); | |
550 | helper_le_stq_mmu(env, addr + 8, int128_gethi(newv), oi1, ra); | |
1dd089d0 | 551 | } |
1ec182c3 | 552 | #endif |
1dd089d0 EC |
553 | |
554 | return !success; | |
555 | } | |
556 | ||
2399d4e7 EC |
557 | uint64_t HELPER(paired_cmpxchg64_le_parallel)(CPUARMState *env, uint64_t addr, |
558 | uint64_t new_lo, uint64_t new_hi) | |
1dd089d0 | 559 | { |
1dd089d0 | 560 | Int128 oldv, cmpv, newv; |
1ec182c3 | 561 | uintptr_t ra = GETPC(); |
1dd089d0 | 562 | bool success; |
1ec182c3 RH |
563 | int mem_idx; |
564 | TCGMemOpIdx oi; | |
1dd089d0 | 565 | |
1ec182c3 | 566 | if (!HAVE_CMPXCHG128) { |
1dd089d0 | 567 | cpu_loop_exit_atomic(ENV_GET_CPU(env), ra); |
1dd089d0 EC |
568 | } |
569 | ||
1ec182c3 RH |
570 | mem_idx = cpu_mmu_index(env, false); |
571 | oi = make_memop_idx(MO_LEQ | MO_ALIGN_16, mem_idx); | |
572 | ||
573 | cmpv = int128_make128(env->exclusive_val, env->exclusive_high); | |
574 | newv = int128_make128(new_lo, new_hi); | |
575 | oldv = helper_atomic_cmpxchgo_le_mmu(env, addr, cmpv, newv, oi, ra); | |
576 | ||
577 | success = int128_eq(oldv, cmpv); | |
1dd089d0 EC |
578 | return !success; |
579 | } | |
2399d4e7 EC |
580 | |
581 | uint64_t HELPER(paired_cmpxchg64_be)(CPUARMState *env, uint64_t addr, | |
582 | uint64_t new_lo, uint64_t new_hi) | |
583 | { | |
1ec182c3 RH |
584 | /* |
585 | * High and low need to be switched here because this is not actually a | |
586 | * 128bit store but two doublewords stored consecutively | |
587 | */ | |
588 | Int128 cmpv = int128_make128(env->exclusive_val, env->exclusive_high); | |
589 | Int128 newv = int128_make128(new_lo, new_hi); | |
590 | Int128 oldv; | |
591 | uintptr_t ra = GETPC(); | |
592 | uint64_t o0, o1; | |
593 | bool success; | |
594 | ||
595 | #ifdef CONFIG_USER_ONLY | |
596 | /* ??? Enforce alignment. */ | |
597 | uint64_t *haddr = g2h(addr); | |
598 | ||
599 | helper_retaddr = ra; | |
600 | o1 = ldq_be_p(haddr + 0); | |
601 | o0 = ldq_be_p(haddr + 1); | |
602 | oldv = int128_make128(o0, o1); | |
603 | ||
604 | success = int128_eq(oldv, cmpv); | |
605 | if (success) { | |
606 | stq_be_p(haddr + 0, int128_gethi(newv)); | |
607 | stq_be_p(haddr + 1, int128_getlo(newv)); | |
608 | } | |
609 | helper_retaddr = 0; | |
610 | #else | |
611 | int mem_idx = cpu_mmu_index(env, false); | |
612 | TCGMemOpIdx oi0 = make_memop_idx(MO_BEQ | MO_ALIGN_16, mem_idx); | |
613 | TCGMemOpIdx oi1 = make_memop_idx(MO_BEQ, mem_idx); | |
614 | ||
615 | o1 = helper_be_ldq_mmu(env, addr + 0, oi0, ra); | |
616 | o0 = helper_be_ldq_mmu(env, addr + 8, oi1, ra); | |
617 | oldv = int128_make128(o0, o1); | |
618 | ||
619 | success = int128_eq(oldv, cmpv); | |
620 | if (success) { | |
621 | helper_be_stq_mmu(env, addr + 0, int128_gethi(newv), oi1, ra); | |
622 | helper_be_stq_mmu(env, addr + 8, int128_getlo(newv), oi1, ra); | |
623 | } | |
624 | #endif | |
625 | ||
626 | return !success; | |
2399d4e7 EC |
627 | } |
628 | ||
629 | uint64_t HELPER(paired_cmpxchg64_be_parallel)(CPUARMState *env, uint64_t addr, | |
1ec182c3 | 630 | uint64_t new_lo, uint64_t new_hi) |
2399d4e7 | 631 | { |
1ec182c3 RH |
632 | Int128 oldv, cmpv, newv; |
633 | uintptr_t ra = GETPC(); | |
634 | bool success; | |
635 | int mem_idx; | |
636 | TCGMemOpIdx oi; | |
637 | ||
638 | if (!HAVE_CMPXCHG128) { | |
639 | cpu_loop_exit_atomic(ENV_GET_CPU(env), ra); | |
640 | } | |
641 | ||
642 | mem_idx = cpu_mmu_index(env, false); | |
643 | oi = make_memop_idx(MO_BEQ | MO_ALIGN_16, mem_idx); | |
644 | ||
645 | /* | |
646 | * High and low need to be switched here because this is not actually a | |
647 | * 128bit store but two doublewords stored consecutively | |
648 | */ | |
649 | cmpv = int128_make128(env->exclusive_high, env->exclusive_val); | |
650 | newv = int128_make128(new_hi, new_lo); | |
651 | oldv = helper_atomic_cmpxchgo_be_mmu(env, addr, cmpv, newv, oi, ra); | |
652 | ||
653 | success = int128_eq(oldv, cmpv); | |
654 | return !success; | |
2399d4e7 | 655 | } |
807cdd50 | 656 | |
44ac14b0 RH |
657 | /* Writes back the old data into Rs. */ |
658 | void HELPER(casp_le_parallel)(CPUARMState *env, uint32_t rs, uint64_t addr, | |
659 | uint64_t new_lo, uint64_t new_hi) | |
660 | { | |
44ac14b0 | 661 | Int128 oldv, cmpv, newv; |
1ec182c3 RH |
662 | uintptr_t ra = GETPC(); |
663 | int mem_idx; | |
664 | TCGMemOpIdx oi; | |
665 | ||
666 | if (!HAVE_CMPXCHG128) { | |
667 | cpu_loop_exit_atomic(ENV_GET_CPU(env), ra); | |
668 | } | |
669 | ||
670 | mem_idx = cpu_mmu_index(env, false); | |
671 | oi = make_memop_idx(MO_LEQ | MO_ALIGN_16, mem_idx); | |
44ac14b0 RH |
672 | |
673 | cmpv = int128_make128(env->xregs[rs], env->xregs[rs + 1]); | |
674 | newv = int128_make128(new_lo, new_hi); | |
44ac14b0 RH |
675 | oldv = helper_atomic_cmpxchgo_le_mmu(env, addr, cmpv, newv, oi, ra); |
676 | ||
677 | env->xregs[rs] = int128_getlo(oldv); | |
678 | env->xregs[rs + 1] = int128_gethi(oldv); | |
44ac14b0 RH |
679 | } |
680 | ||
681 | void HELPER(casp_be_parallel)(CPUARMState *env, uint32_t rs, uint64_t addr, | |
682 | uint64_t new_hi, uint64_t new_lo) | |
683 | { | |
44ac14b0 | 684 | Int128 oldv, cmpv, newv; |
1ec182c3 RH |
685 | uintptr_t ra = GETPC(); |
686 | int mem_idx; | |
687 | TCGMemOpIdx oi; | |
688 | ||
689 | if (!HAVE_CMPXCHG128) { | |
690 | cpu_loop_exit_atomic(ENV_GET_CPU(env), ra); | |
691 | } | |
692 | ||
693 | mem_idx = cpu_mmu_index(env, false); | |
694 | oi = make_memop_idx(MO_LEQ | MO_ALIGN_16, mem_idx); | |
44ac14b0 RH |
695 | |
696 | cmpv = int128_make128(env->xregs[rs + 1], env->xregs[rs]); | |
697 | newv = int128_make128(new_lo, new_hi); | |
44ac14b0 RH |
698 | oldv = helper_atomic_cmpxchgo_be_mmu(env, addr, cmpv, newv, oi, ra); |
699 | ||
700 | env->xregs[rs + 1] = int128_getlo(oldv); | |
701 | env->xregs[rs] = int128_gethi(oldv); | |
44ac14b0 RH |
702 | } |
703 | ||
807cdd50 AB |
704 | /* |
705 | * AdvSIMD half-precision | |
706 | */ | |
707 | ||
708 | #define ADVSIMD_HELPER(name, suffix) HELPER(glue(glue(advsimd_, name), suffix)) | |
709 | ||
710 | #define ADVSIMD_HALFOP(name) \ | |
6c2be133 | 711 | uint32_t ADVSIMD_HELPER(name, h)(uint32_t a, uint32_t b, void *fpstp) \ |
807cdd50 AB |
712 | { \ |
713 | float_status *fpst = fpstp; \ | |
714 | return float16_ ## name(a, b, fpst); \ | |
715 | } | |
716 | ||
37208734 AB |
717 | ADVSIMD_HALFOP(add) |
718 | ADVSIMD_HALFOP(sub) | |
719 | ADVSIMD_HALFOP(mul) | |
720 | ADVSIMD_HALFOP(div) | |
807cdd50 AB |
721 | ADVSIMD_HALFOP(min) |
722 | ADVSIMD_HALFOP(max) | |
723 | ADVSIMD_HALFOP(minnum) | |
724 | ADVSIMD_HALFOP(maxnum) | |
d32adeae | 725 | |
6089030c AB |
726 | #define ADVSIMD_TWOHALFOP(name) \ |
727 | uint32_t ADVSIMD_HELPER(name, 2h)(uint32_t two_a, uint32_t two_b, void *fpstp) \ | |
728 | { \ | |
729 | float16 a1, a2, b1, b2; \ | |
730 | uint32_t r1, r2; \ | |
731 | float_status *fpst = fpstp; \ | |
732 | a1 = extract32(two_a, 0, 16); \ | |
733 | a2 = extract32(two_a, 16, 16); \ | |
734 | b1 = extract32(two_b, 0, 16); \ | |
735 | b2 = extract32(two_b, 16, 16); \ | |
736 | r1 = float16_ ## name(a1, b1, fpst); \ | |
737 | r2 = float16_ ## name(a2, b2, fpst); \ | |
738 | return deposit32(r1, 16, 16, r2); \ | |
739 | } | |
740 | ||
741 | ADVSIMD_TWOHALFOP(add) | |
742 | ADVSIMD_TWOHALFOP(sub) | |
743 | ADVSIMD_TWOHALFOP(mul) | |
744 | ADVSIMD_TWOHALFOP(div) | |
745 | ADVSIMD_TWOHALFOP(min) | |
746 | ADVSIMD_TWOHALFOP(max) | |
747 | ADVSIMD_TWOHALFOP(minnum) | |
748 | ADVSIMD_TWOHALFOP(maxnum) | |
749 | ||
2deb992b | 750 | /* Data processing - scalar floating-point and advanced SIMD */ |
6089030c | 751 | static float16 float16_mulx(float16 a, float16 b, void *fpstp) |
2deb992b AB |
752 | { |
753 | float_status *fpst = fpstp; | |
754 | ||
755 | a = float16_squash_input_denormal(a, fpst); | |
756 | b = float16_squash_input_denormal(b, fpst); | |
757 | ||
758 | if ((float16_is_zero(a) && float16_is_infinity(b)) || | |
759 | (float16_is_infinity(a) && float16_is_zero(b))) { | |
760 | /* 2.0 with the sign bit set to sign(A) XOR sign(B) */ | |
761 | return make_float16((1U << 14) | | |
762 | ((float16_val(a) ^ float16_val(b)) & (1U << 15))); | |
763 | } | |
764 | return float16_mul(a, b, fpst); | |
765 | } | |
766 | ||
6089030c AB |
767 | ADVSIMD_HALFOP(mulx) |
768 | ADVSIMD_TWOHALFOP(mulx) | |
769 | ||
2deb992b | 770 | /* fused multiply-accumulate */ |
6c2be133 RH |
771 | uint32_t HELPER(advsimd_muladdh)(uint32_t a, uint32_t b, uint32_t c, |
772 | void *fpstp) | |
2deb992b AB |
773 | { |
774 | float_status *fpst = fpstp; | |
775 | return float16_muladd(a, b, c, 0, fpst); | |
776 | } | |
777 | ||
6089030c AB |
778 | uint32_t HELPER(advsimd_muladd2h)(uint32_t two_a, uint32_t two_b, |
779 | uint32_t two_c, void *fpstp) | |
780 | { | |
781 | float_status *fpst = fpstp; | |
782 | float16 a1, a2, b1, b2, c1, c2; | |
783 | uint32_t r1, r2; | |
784 | a1 = extract32(two_a, 0, 16); | |
785 | a2 = extract32(two_a, 16, 16); | |
786 | b1 = extract32(two_b, 0, 16); | |
787 | b2 = extract32(two_b, 16, 16); | |
788 | c1 = extract32(two_c, 0, 16); | |
789 | c2 = extract32(two_c, 16, 16); | |
790 | r1 = float16_muladd(a1, b1, c1, 0, fpst); | |
791 | r2 = float16_muladd(a2, b2, c2, 0, fpst); | |
792 | return deposit32(r1, 16, 16, r2); | |
793 | } | |
794 | ||
d32adeae AB |
795 | /* |
796 | * Floating point comparisons produce an integer result. Softfloat | |
797 | * routines return float_relation types which we convert to the 0/-1 | |
798 | * Neon requires. | |
799 | */ | |
800 | ||
801 | #define ADVSIMD_CMPRES(test) (test) ? 0xffff : 0 | |
802 | ||
6c2be133 | 803 | uint32_t HELPER(advsimd_ceq_f16)(uint32_t a, uint32_t b, void *fpstp) |
d32adeae AB |
804 | { |
805 | float_status *fpst = fpstp; | |
806 | int compare = float16_compare_quiet(a, b, fpst); | |
807 | return ADVSIMD_CMPRES(compare == float_relation_equal); | |
808 | } | |
809 | ||
6c2be133 | 810 | uint32_t HELPER(advsimd_cge_f16)(uint32_t a, uint32_t b, void *fpstp) |
d32adeae AB |
811 | { |
812 | float_status *fpst = fpstp; | |
813 | int compare = float16_compare(a, b, fpst); | |
814 | return ADVSIMD_CMPRES(compare == float_relation_greater || | |
815 | compare == float_relation_equal); | |
816 | } | |
817 | ||
6c2be133 | 818 | uint32_t HELPER(advsimd_cgt_f16)(uint32_t a, uint32_t b, void *fpstp) |
d32adeae AB |
819 | { |
820 | float_status *fpst = fpstp; | |
821 | int compare = float16_compare(a, b, fpst); | |
822 | return ADVSIMD_CMPRES(compare == float_relation_greater); | |
823 | } | |
824 | ||
6c2be133 | 825 | uint32_t HELPER(advsimd_acge_f16)(uint32_t a, uint32_t b, void *fpstp) |
d32adeae AB |
826 | { |
827 | float_status *fpst = fpstp; | |
828 | float16 f0 = float16_abs(a); | |
829 | float16 f1 = float16_abs(b); | |
830 | int compare = float16_compare(f0, f1, fpst); | |
831 | return ADVSIMD_CMPRES(compare == float_relation_greater || | |
832 | compare == float_relation_equal); | |
833 | } | |
834 | ||
6c2be133 | 835 | uint32_t HELPER(advsimd_acgt_f16)(uint32_t a, uint32_t b, void *fpstp) |
d32adeae AB |
836 | { |
837 | float_status *fpst = fpstp; | |
838 | float16 f0 = float16_abs(a); | |
839 | float16 f1 = float16_abs(b); | |
840 | int compare = float16_compare(f0, f1, fpst); | |
841 | return ADVSIMD_CMPRES(compare == float_relation_greater); | |
842 | } | |
6109aea2 AB |
843 | |
844 | /* round to integral */ | |
6c2be133 | 845 | uint32_t HELPER(advsimd_rinth_exact)(uint32_t x, void *fp_status) |
6109aea2 AB |
846 | { |
847 | return float16_round_to_int(x, fp_status); | |
848 | } | |
849 | ||
6c2be133 | 850 | uint32_t HELPER(advsimd_rinth)(uint32_t x, void *fp_status) |
6109aea2 AB |
851 | { |
852 | int old_flags = get_float_exception_flags(fp_status), new_flags; | |
853 | float16 ret; | |
854 | ||
855 | ret = float16_round_to_int(x, fp_status); | |
856 | ||
857 | /* Suppress any inexact exceptions the conversion produced */ | |
858 | if (!(old_flags & float_flag_inexact)) { | |
859 | new_flags = get_float_exception_flags(fp_status); | |
860 | set_float_exception_flags(new_flags & ~float_flag_inexact, fp_status); | |
861 | } | |
862 | ||
863 | return ret; | |
864 | } | |
2df58130 AB |
865 | |
866 | /* | |
867 | * Half-precision floating point conversion functions | |
868 | * | |
869 | * There are a multitude of conversion functions with various | |
870 | * different rounding modes. This is dealt with by the calling code | |
871 | * setting the mode appropriately before calling the helper. | |
872 | */ | |
873 | ||
6c2be133 | 874 | uint32_t HELPER(advsimd_f16tosinth)(uint32_t a, void *fpstp) |
2df58130 AB |
875 | { |
876 | float_status *fpst = fpstp; | |
877 | ||
878 | /* Invalid if we are passed a NaN */ | |
879 | if (float16_is_any_nan(a)) { | |
880 | float_raise(float_flag_invalid, fpst); | |
881 | return 0; | |
882 | } | |
883 | return float16_to_int16(a, fpst); | |
884 | } | |
885 | ||
6c2be133 | 886 | uint32_t HELPER(advsimd_f16touinth)(uint32_t a, void *fpstp) |
2df58130 AB |
887 | { |
888 | float_status *fpst = fpstp; | |
889 | ||
890 | /* Invalid if we are passed a NaN */ | |
891 | if (float16_is_any_nan(a)) { | |
892 | float_raise(float_flag_invalid, fpst); | |
893 | return 0; | |
894 | } | |
895 | return float16_to_uint16(a, fpst); | |
896 | } | |
b96a54c7 AB |
897 | |
898 | /* | |
899 | * Square Root and Reciprocal square root | |
900 | */ | |
901 | ||
6c2be133 | 902 | uint32_t HELPER(sqrt_f16)(uint32_t a, void *fpstp) |
b96a54c7 AB |
903 | { |
904 | float_status *s = fpstp; | |
905 | ||
906 | return float16_sqrt(a, s); | |
907 | } | |
908 | ||
909 |