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