2 * AArch64 specific helpers
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.
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.
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/>.
20 #include "qemu/osdep.h"
22 #include "exec/gdbstub.h"
23 #include "exec/helper-proto.h"
24 #include "qemu/host-utils.h"
26 #include "sysemu/sysemu.h"
27 #include "qemu/bitops.h"
28 #include "internals.h"
29 #include "qemu/crc32c.h"
30 #include "exec/exec-all.h"
31 #include "exec/cpu_ldst.h"
32 #include "qemu/int128.h"
34 #include "fpu/softfloat.h"
35 #include <zlib.h> /* For crc32 */
37 /* C2.4.7 Multiply and divide */
38 /* special cases for 0 and LLONG_MIN are mandated by the standard */
39 uint64_t HELPER(udiv64)(uint64_t num, uint64_t den)
47 int64_t HELPER(sdiv64)(int64_t num, int64_t den)
52 if (num == LLONG_MIN && den == -1) {
58 uint64_t HELPER(rbit64)(uint64_t x)
63 /* Convert a softfloat float_relation_ (as returned by
64 * the float*_compare functions) to the correct ARM
67 static inline uint32_t float_rel_to_flags(int res)
71 case float_relation_equal:
72 flags = PSTATE_Z | PSTATE_C;
74 case float_relation_less:
77 case float_relation_greater:
80 case float_relation_unordered:
82 flags = PSTATE_C | PSTATE_V;
88 uint64_t HELPER(vfp_cmps_a64)(float32 x, float32 y, void *fp_status)
90 return float_rel_to_flags(float32_compare_quiet(x, y, fp_status));
93 uint64_t HELPER(vfp_cmpes_a64)(float32 x, float32 y, void *fp_status)
95 return float_rel_to_flags(float32_compare(x, y, fp_status));
98 uint64_t HELPER(vfp_cmpd_a64)(float64 x, float64 y, void *fp_status)
100 return float_rel_to_flags(float64_compare_quiet(x, y, fp_status));
103 uint64_t HELPER(vfp_cmped_a64)(float64 x, float64 y, void *fp_status)
105 return float_rel_to_flags(float64_compare(x, y, fp_status));
108 float32 HELPER(vfp_mulxs)(float32 a, float32 b, void *fpstp)
110 float_status *fpst = fpstp;
112 a = float32_squash_input_denormal(a, fpst);
113 b = float32_squash_input_denormal(b, fpst);
115 if ((float32_is_zero(a) && float32_is_infinity(b)) ||
116 (float32_is_infinity(a) && float32_is_zero(b))) {
117 /* 2.0 with the sign bit set to sign(A) XOR sign(B) */
118 return make_float32((1U << 30) |
119 ((float32_val(a) ^ float32_val(b)) & (1U << 31)));
121 return float32_mul(a, b, fpst);
124 float64 HELPER(vfp_mulxd)(float64 a, float64 b, void *fpstp)
126 float_status *fpst = fpstp;
128 a = float64_squash_input_denormal(a, fpst);
129 b = float64_squash_input_denormal(b, fpst);
131 if ((float64_is_zero(a) && float64_is_infinity(b)) ||
132 (float64_is_infinity(a) && float64_is_zero(b))) {
133 /* 2.0 with the sign bit set to sign(A) XOR sign(B) */
134 return make_float64((1ULL << 62) |
135 ((float64_val(a) ^ float64_val(b)) & (1ULL << 63)));
137 return float64_mul(a, b, fpst);
140 uint64_t HELPER(simd_tbl)(CPUARMState *env, uint64_t result, uint64_t indices,
141 uint32_t rn, uint32_t numregs)
143 /* Helper function for SIMD TBL and TBX. We have to do the table
144 * lookup part for the 64 bits worth of indices we're passed in.
145 * result is the initial results vector (either zeroes for TBL
146 * or some guest values for TBX), rn the register number where
147 * the table starts, and numregs the number of registers in the table.
148 * We return the results of the lookups.
152 for (shift = 0; shift < 64; shift += 8) {
153 int index = extract64(indices, shift, 8);
154 if (index < 16 * numregs) {
155 /* Convert index (a byte offset into the virtual table
156 * which is a series of 128-bit vectors concatenated)
157 * into the correct register element plus a bit offset
158 * into that element, bearing in mind that the table
159 * can wrap around from V31 to V0.
161 int elt = (rn * 2 + (index >> 3)) % 64;
162 int bitidx = (index & 7) * 8;
163 uint64_t *q = aa64_vfp_qreg(env, elt >> 1);
164 uint64_t val = extract64(q[elt & 1], bitidx, 8);
166 result = deposit64(result, shift, 8, val);
172 /* 64bit/double versions of the neon float compare functions */
173 uint64_t HELPER(neon_ceq_f64)(float64 a, float64 b, void *fpstp)
175 float_status *fpst = fpstp;
176 return -float64_eq_quiet(a, b, fpst);
179 uint64_t HELPER(neon_cge_f64)(float64 a, float64 b, void *fpstp)
181 float_status *fpst = fpstp;
182 return -float64_le(b, a, fpst);
185 uint64_t HELPER(neon_cgt_f64)(float64 a, float64 b, void *fpstp)
187 float_status *fpst = fpstp;
188 return -float64_lt(b, a, fpst);
191 /* Reciprocal step and sqrt step. Note that unlike the A32/T32
192 * versions, these do a fully fused multiply-add or
193 * multiply-add-and-halve.
195 #define float16_two make_float16(0x4000)
196 #define float16_three make_float16(0x4200)
197 #define float16_one_point_five make_float16(0x3e00)
199 #define float32_two make_float32(0x40000000)
200 #define float32_three make_float32(0x40400000)
201 #define float32_one_point_five make_float32(0x3fc00000)
203 #define float64_two make_float64(0x4000000000000000ULL)
204 #define float64_three make_float64(0x4008000000000000ULL)
205 #define float64_one_point_five make_float64(0x3FF8000000000000ULL)
207 float16 HELPER(recpsf_f16)(float16 a, float16 b, void *fpstp)
209 float_status *fpst = fpstp;
211 a = float16_squash_input_denormal(a, fpst);
212 b = float16_squash_input_denormal(b, fpst);
215 if ((float16_is_infinity(a) && float16_is_zero(b)) ||
216 (float16_is_infinity(b) && float16_is_zero(a))) {
219 return float16_muladd(a, b, float16_two, 0, fpst);
222 float32 HELPER(recpsf_f32)(float32 a, float32 b, void *fpstp)
224 float_status *fpst = fpstp;
226 a = float32_squash_input_denormal(a, fpst);
227 b = float32_squash_input_denormal(b, fpst);
230 if ((float32_is_infinity(a) && float32_is_zero(b)) ||
231 (float32_is_infinity(b) && float32_is_zero(a))) {
234 return float32_muladd(a, b, float32_two, 0, fpst);
237 float64 HELPER(recpsf_f64)(float64 a, float64 b, void *fpstp)
239 float_status *fpst = fpstp;
241 a = float64_squash_input_denormal(a, fpst);
242 b = float64_squash_input_denormal(b, fpst);
245 if ((float64_is_infinity(a) && float64_is_zero(b)) ||
246 (float64_is_infinity(b) && float64_is_zero(a))) {
249 return float64_muladd(a, b, float64_two, 0, fpst);
252 float16 HELPER(rsqrtsf_f16)(float16 a, float16 b, void *fpstp)
254 float_status *fpst = fpstp;
256 a = float16_squash_input_denormal(a, fpst);
257 b = float16_squash_input_denormal(b, fpst);
260 if ((float16_is_infinity(a) && float16_is_zero(b)) ||
261 (float16_is_infinity(b) && float16_is_zero(a))) {
262 return float16_one_point_five;
264 return float16_muladd(a, b, float16_three, float_muladd_halve_result, fpst);
267 float32 HELPER(rsqrtsf_f32)(float32 a, float32 b, void *fpstp)
269 float_status *fpst = fpstp;
271 a = float32_squash_input_denormal(a, fpst);
272 b = float32_squash_input_denormal(b, fpst);
275 if ((float32_is_infinity(a) && float32_is_zero(b)) ||
276 (float32_is_infinity(b) && float32_is_zero(a))) {
277 return float32_one_point_five;
279 return float32_muladd(a, b, float32_three, float_muladd_halve_result, fpst);
282 float64 HELPER(rsqrtsf_f64)(float64 a, float64 b, void *fpstp)
284 float_status *fpst = fpstp;
286 a = float64_squash_input_denormal(a, fpst);
287 b = float64_squash_input_denormal(b, fpst);
290 if ((float64_is_infinity(a) && float64_is_zero(b)) ||
291 (float64_is_infinity(b) && float64_is_zero(a))) {
292 return float64_one_point_five;
294 return float64_muladd(a, b, float64_three, float_muladd_halve_result, fpst);
297 /* Pairwise long add: add pairs of adjacent elements into
298 * double-width elements in the result (eg _s8 is an 8x8->16 op)
300 uint64_t HELPER(neon_addlp_s8)(uint64_t a)
302 uint64_t nsignmask = 0x0080008000800080ULL;
303 uint64_t wsignmask = 0x8000800080008000ULL;
304 uint64_t elementmask = 0x00ff00ff00ff00ffULL;
306 uint64_t res, signres;
308 /* Extract odd elements, sign extend each to a 16 bit field */
309 tmp1 = a & elementmask;
312 tmp1 = (tmp1 - nsignmask) ^ wsignmask;
313 /* Ditto for the even elements */
314 tmp2 = (a >> 8) & elementmask;
317 tmp2 = (tmp2 - nsignmask) ^ wsignmask;
319 /* calculate the result by summing bits 0..14, 16..22, etc,
320 * and then adjusting the sign bits 15, 23, etc manually.
321 * This ensures the addition can't overflow the 16 bit field.
323 signres = (tmp1 ^ tmp2) & wsignmask;
324 res = (tmp1 & ~wsignmask) + (tmp2 & ~wsignmask);
330 uint64_t HELPER(neon_addlp_u8)(uint64_t a)
334 tmp = a & 0x00ff00ff00ff00ffULL;
335 tmp += (a >> 8) & 0x00ff00ff00ff00ffULL;
339 uint64_t HELPER(neon_addlp_s16)(uint64_t a)
341 int32_t reslo, reshi;
343 reslo = (int32_t)(int16_t)a + (int32_t)(int16_t)(a >> 16);
344 reshi = (int32_t)(int16_t)(a >> 32) + (int32_t)(int16_t)(a >> 48);
346 return (uint32_t)reslo | (((uint64_t)reshi) << 32);
349 uint64_t HELPER(neon_addlp_u16)(uint64_t a)
353 tmp = a & 0x0000ffff0000ffffULL;
354 tmp += (a >> 16) & 0x0000ffff0000ffffULL;
358 /* Floating-point reciprocal exponent - see FPRecpX in ARM ARM */
359 float32 HELPER(frecpx_f32)(float32 a, void *fpstp)
361 float_status *fpst = fpstp;
362 uint32_t val32, sbit;
365 if (float32_is_any_nan(a)) {
367 if (float32_is_signaling_nan(a, fpst)) {
368 float_raise(float_flag_invalid, fpst);
369 nan = float32_maybe_silence_nan(a, fpst);
371 if (fpst->default_nan_mode) {
372 nan = float32_default_nan(fpst);
377 val32 = float32_val(a);
378 sbit = 0x80000000ULL & val32;
379 exp = extract32(val32, 23, 8);
382 return make_float32(sbit | (0xfe << 23));
384 return make_float32(sbit | (~exp & 0xff) << 23);
388 float64 HELPER(frecpx_f64)(float64 a, void *fpstp)
390 float_status *fpst = fpstp;
391 uint64_t val64, sbit;
394 if (float64_is_any_nan(a)) {
396 if (float64_is_signaling_nan(a, fpst)) {
397 float_raise(float_flag_invalid, fpst);
398 nan = float64_maybe_silence_nan(a, fpst);
400 if (fpst->default_nan_mode) {
401 nan = float64_default_nan(fpst);
406 val64 = float64_val(a);
407 sbit = 0x8000000000000000ULL & val64;
408 exp = extract64(float64_val(a), 52, 11);
411 return make_float64(sbit | (0x7feULL << 52));
413 return make_float64(sbit | (~exp & 0x7ffULL) << 52);
417 float32 HELPER(fcvtx_f64_to_f32)(float64 a, CPUARMState *env)
419 /* Von Neumann rounding is implemented by using round-to-zero
420 * and then setting the LSB of the result if Inexact was raised.
423 float_status *fpst = &env->vfp.fp_status;
424 float_status tstat = *fpst;
427 set_float_rounding_mode(float_round_to_zero, &tstat);
428 set_float_exception_flags(0, &tstat);
429 r = float64_to_float32(a, &tstat);
430 r = float32_maybe_silence_nan(r, &tstat);
431 exflags = get_float_exception_flags(&tstat);
432 if (exflags & float_flag_inexact) {
433 r = make_float32(float32_val(r) | 1);
435 exflags |= get_float_exception_flags(fpst);
436 set_float_exception_flags(exflags, fpst);
440 /* 64-bit versions of the CRC helpers. Note that although the operation
441 * (and the prototypes of crc32c() and crc32() mean that only the bottom
442 * 32 bits of the accumulator and result are used, we pass and return
443 * uint64_t for convenience of the generated code. Unlike the 32-bit
444 * instruction set versions, val may genuinely have 64 bits of data in it.
445 * The upper bytes of val (above the number specified by 'bytes') must have
446 * been zeroed out by the caller.
448 uint64_t HELPER(crc32_64)(uint64_t acc, uint64_t val, uint32_t bytes)
454 /* zlib crc32 converts the accumulator and output to one's complement. */
455 return crc32(acc ^ 0xffffffff, buf, bytes) ^ 0xffffffff;
458 uint64_t HELPER(crc32c_64)(uint64_t acc, uint64_t val, uint32_t bytes)
464 /* Linux crc32c converts the output to one's complement. */
465 return crc32c(acc, buf, bytes) ^ 0xffffffff;
468 /* Returns 0 on success; 1 otherwise. */
469 static uint64_t do_paired_cmpxchg64_le(CPUARMState *env, uint64_t addr,
470 uint64_t new_lo, uint64_t new_hi,
471 bool parallel, uintptr_t ra)
473 Int128 oldv, cmpv, newv;
476 cmpv = int128_make128(env->exclusive_val, env->exclusive_high);
477 newv = int128_make128(new_lo, new_hi);
480 #ifndef CONFIG_ATOMIC128
481 cpu_loop_exit_atomic(ENV_GET_CPU(env), ra);
483 int mem_idx = cpu_mmu_index(env, false);
484 TCGMemOpIdx oi = make_memop_idx(MO_LEQ | MO_ALIGN_16, mem_idx);
485 oldv = helper_atomic_cmpxchgo_le_mmu(env, addr, cmpv, newv, oi, ra);
486 success = int128_eq(oldv, cmpv);
491 #ifdef CONFIG_USER_ONLY
492 /* ??? Enforce alignment. */
493 uint64_t *haddr = g2h(addr);
496 o0 = ldq_le_p(haddr + 0);
497 o1 = ldq_le_p(haddr + 1);
498 oldv = int128_make128(o0, o1);
500 success = int128_eq(oldv, cmpv);
502 stq_le_p(haddr + 0, int128_getlo(newv));
503 stq_le_p(haddr + 1, int128_gethi(newv));
507 int mem_idx = cpu_mmu_index(env, false);
508 TCGMemOpIdx oi0 = make_memop_idx(MO_LEQ | MO_ALIGN_16, mem_idx);
509 TCGMemOpIdx oi1 = make_memop_idx(MO_LEQ, mem_idx);
511 o0 = helper_le_ldq_mmu(env, addr + 0, oi0, ra);
512 o1 = helper_le_ldq_mmu(env, addr + 8, oi1, ra);
513 oldv = int128_make128(o0, o1);
515 success = int128_eq(oldv, cmpv);
517 helper_le_stq_mmu(env, addr + 0, int128_getlo(newv), oi1, ra);
518 helper_le_stq_mmu(env, addr + 8, int128_gethi(newv), oi1, ra);
526 uint64_t HELPER(paired_cmpxchg64_le)(CPUARMState *env, uint64_t addr,
527 uint64_t new_lo, uint64_t new_hi)
529 return do_paired_cmpxchg64_le(env, addr, new_lo, new_hi, false, GETPC());
532 uint64_t HELPER(paired_cmpxchg64_le_parallel)(CPUARMState *env, uint64_t addr,
533 uint64_t new_lo, uint64_t new_hi)
535 return do_paired_cmpxchg64_le(env, addr, new_lo, new_hi, true, GETPC());
538 static uint64_t do_paired_cmpxchg64_be(CPUARMState *env, uint64_t addr,
539 uint64_t new_lo, uint64_t new_hi,
540 bool parallel, uintptr_t ra)
542 Int128 oldv, cmpv, newv;
545 /* high and low need to be switched here because this is not actually a
546 * 128bit store but two doublewords stored consecutively
548 cmpv = int128_make128(env->exclusive_high, env->exclusive_val);
549 newv = int128_make128(new_hi, new_lo);
552 #ifndef CONFIG_ATOMIC128
553 cpu_loop_exit_atomic(ENV_GET_CPU(env), ra);
555 int mem_idx = cpu_mmu_index(env, false);
556 TCGMemOpIdx oi = make_memop_idx(MO_BEQ | MO_ALIGN_16, mem_idx);
557 oldv = helper_atomic_cmpxchgo_be_mmu(env, addr, cmpv, newv, oi, ra);
558 success = int128_eq(oldv, cmpv);
563 #ifdef CONFIG_USER_ONLY
564 /* ??? Enforce alignment. */
565 uint64_t *haddr = g2h(addr);
568 o1 = ldq_be_p(haddr + 0);
569 o0 = ldq_be_p(haddr + 1);
570 oldv = int128_make128(o0, o1);
572 success = int128_eq(oldv, cmpv);
574 stq_be_p(haddr + 0, int128_gethi(newv));
575 stq_be_p(haddr + 1, int128_getlo(newv));
579 int mem_idx = cpu_mmu_index(env, false);
580 TCGMemOpIdx oi0 = make_memop_idx(MO_BEQ | MO_ALIGN_16, mem_idx);
581 TCGMemOpIdx oi1 = make_memop_idx(MO_BEQ, mem_idx);
583 o1 = helper_be_ldq_mmu(env, addr + 0, oi0, ra);
584 o0 = helper_be_ldq_mmu(env, addr + 8, oi1, ra);
585 oldv = int128_make128(o0, o1);
587 success = int128_eq(oldv, cmpv);
589 helper_be_stq_mmu(env, addr + 0, int128_gethi(newv), oi1, ra);
590 helper_be_stq_mmu(env, addr + 8, int128_getlo(newv), oi1, ra);
598 uint64_t HELPER(paired_cmpxchg64_be)(CPUARMState *env, uint64_t addr,
599 uint64_t new_lo, uint64_t new_hi)
601 return do_paired_cmpxchg64_be(env, addr, new_lo, new_hi, false, GETPC());
604 uint64_t HELPER(paired_cmpxchg64_be_parallel)(CPUARMState *env, uint64_t addr,
605 uint64_t new_lo, uint64_t new_hi)
607 return do_paired_cmpxchg64_be(env, addr, new_lo, new_hi, true, GETPC());
611 * AdvSIMD half-precision
614 #define ADVSIMD_HELPER(name, suffix) HELPER(glue(glue(advsimd_, name), suffix))
616 #define ADVSIMD_HALFOP(name) \
617 float16 ADVSIMD_HELPER(name, h)(float16 a, float16 b, void *fpstp) \
619 float_status *fpst = fpstp; \
620 return float16_ ## name(a, b, fpst); \
629 ADVSIMD_HALFOP(minnum)
630 ADVSIMD_HALFOP(maxnum)
632 /* Data processing - scalar floating-point and advanced SIMD */
633 float16 HELPER(advsimd_mulxh)(float16 a, float16 b, void *fpstp)
635 float_status *fpst = fpstp;
637 a = float16_squash_input_denormal(a, fpst);
638 b = float16_squash_input_denormal(b, fpst);
640 if ((float16_is_zero(a) && float16_is_infinity(b)) ||
641 (float16_is_infinity(a) && float16_is_zero(b))) {
642 /* 2.0 with the sign bit set to sign(A) XOR sign(B) */
643 return make_float16((1U << 14) |
644 ((float16_val(a) ^ float16_val(b)) & (1U << 15)));
646 return float16_mul(a, b, fpst);
649 /* fused multiply-accumulate */
650 float16 HELPER(advsimd_muladdh)(float16 a, float16 b, float16 c, void *fpstp)
652 float_status *fpst = fpstp;
653 return float16_muladd(a, b, c, 0, fpst);
657 * Floating point comparisons produce an integer result. Softfloat
658 * routines return float_relation types which we convert to the 0/-1
662 #define ADVSIMD_CMPRES(test) (test) ? 0xffff : 0
664 uint32_t HELPER(advsimd_ceq_f16)(float16 a, float16 b, void *fpstp)
666 float_status *fpst = fpstp;
667 int compare = float16_compare_quiet(a, b, fpst);
668 return ADVSIMD_CMPRES(compare == float_relation_equal);
671 uint32_t HELPER(advsimd_cge_f16)(float16 a, float16 b, void *fpstp)
673 float_status *fpst = fpstp;
674 int compare = float16_compare(a, b, fpst);
675 return ADVSIMD_CMPRES(compare == float_relation_greater ||
676 compare == float_relation_equal);
679 uint32_t HELPER(advsimd_cgt_f16)(float16 a, float16 b, void *fpstp)
681 float_status *fpst = fpstp;
682 int compare = float16_compare(a, b, fpst);
683 return ADVSIMD_CMPRES(compare == float_relation_greater);
686 uint32_t HELPER(advsimd_acge_f16)(float16 a, float16 b, void *fpstp)
688 float_status *fpst = fpstp;
689 float16 f0 = float16_abs(a);
690 float16 f1 = float16_abs(b);
691 int compare = float16_compare(f0, f1, fpst);
692 return ADVSIMD_CMPRES(compare == float_relation_greater ||
693 compare == float_relation_equal);
696 uint32_t HELPER(advsimd_acgt_f16)(float16 a, float16 b, void *fpstp)
698 float_status *fpst = fpstp;
699 float16 f0 = float16_abs(a);
700 float16 f1 = float16_abs(b);
701 int compare = float16_compare(f0, f1, fpst);
702 return ADVSIMD_CMPRES(compare == float_relation_greater);