qemu/target/s390x/fpu_helper.c
<<
>>
Prefs
   1/*
   2 *  S/390 FPU helper routines
   3 *
   4 *  Copyright (c) 2009 Ulrich Hecht
   5 *  Copyright (c) 2009 Alexander Graf
   6 *
   7 * This library is free software; you can redistribute it and/or
   8 * modify it under the terms of the GNU Lesser General Public
   9 * License as published by the Free Software Foundation; either
  10 * version 2.1 of the License, or (at your option) any later version.
  11 *
  12 * This library is distributed in the hope that it will be useful,
  13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  15 * Lesser General Public License for more details.
  16 *
  17 * You should have received a copy of the GNU Lesser General Public
  18 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
  19 */
  20
  21#include "qemu/osdep.h"
  22#include "cpu.h"
  23#include "internal.h"
  24#include "tcg_s390x.h"
  25#include "exec/exec-all.h"
  26#include "exec/cpu_ldst.h"
  27#include "exec/helper-proto.h"
  28#include "fpu/softfloat.h"
  29
  30/* #define DEBUG_HELPER */
  31#ifdef DEBUG_HELPER
  32#define HELPER_LOG(x...) qemu_log(x)
  33#else
  34#define HELPER_LOG(x...)
  35#endif
  36
  37#define RET128(F) (env->retxl = F.low, F.high)
  38
  39uint8_t s390_softfloat_exc_to_ieee(unsigned int exc)
  40{
  41    uint8_t s390_exc = 0;
  42
  43    s390_exc |= (exc & float_flag_invalid) ? S390_IEEE_MASK_INVALID : 0;
  44    s390_exc |= (exc & float_flag_divbyzero) ? S390_IEEE_MASK_DIVBYZERO : 0;
  45    s390_exc |= (exc & float_flag_overflow) ? S390_IEEE_MASK_OVERFLOW : 0;
  46    s390_exc |= (exc & float_flag_underflow) ? S390_IEEE_MASK_UNDERFLOW : 0;
  47    s390_exc |= (exc & float_flag_inexact) ? S390_IEEE_MASK_INEXACT : 0;
  48
  49    return s390_exc;
  50}
  51
  52/* Should be called after any operation that may raise IEEE exceptions.  */
  53static void handle_exceptions(CPUS390XState *env, bool XxC, uintptr_t retaddr)
  54{
  55    unsigned s390_exc, qemu_exc;
  56
  57    /* Get the exceptions raised by the current operation.  Reset the
  58       fpu_status contents so that the next operation has a clean slate.  */
  59    qemu_exc = env->fpu_status.float_exception_flags;
  60    if (qemu_exc == 0) {
  61        return;
  62    }
  63    env->fpu_status.float_exception_flags = 0;
  64    s390_exc = s390_softfloat_exc_to_ieee(qemu_exc);
  65
  66    /*
  67     * IEEE-Underflow exception recognition exists if a tininess condition
  68     * (underflow) exists and
  69     * - The mask bit in the FPC is zero and the result is inexact
  70     * - The mask bit in the FPC is one
  71     * So tininess conditions that are not inexact don't trigger any
  72     * underflow action in case the mask bit is not one.
  73     */
  74    if (!(s390_exc & S390_IEEE_MASK_INEXACT) &&
  75        !((env->fpc >> 24) & S390_IEEE_MASK_UNDERFLOW)) {
  76        s390_exc &= ~S390_IEEE_MASK_UNDERFLOW;
  77    }
  78
  79    /*
  80     * FIXME:
  81     * 1. Right now, all inexact conditions are inidicated as
  82     *    "truncated" (0) and never as "incremented" (1) in the DXC.
  83     * 2. Only traps due to invalid/divbyzero are suppressing. Other traps
  84     *    are completing, meaning the target register has to be written!
  85     *    This, however will mean that we have to write the register before
  86     *    triggering the trap - impossible right now.
  87     */
  88
  89    /*
  90     * invalid/divbyzero cannot coexist with other conditions.
  91     * overflow/underflow however can coexist with inexact, we have to
  92     * handle it separatly.
  93     */
  94    if (s390_exc & ~S390_IEEE_MASK_INEXACT) {
  95        if (s390_exc & ~S390_IEEE_MASK_INEXACT & env->fpc >> 24) {
  96            /* trap condition - inexact reported along */
  97            tcg_s390_data_exception(env, s390_exc, retaddr);
  98        }
  99        /* nontrap condition - inexact handled differently */
 100        env->fpc |= (s390_exc & ~S390_IEEE_MASK_INEXACT) << 16;
 101    }
 102
 103    /* inexact handling */
 104    if (s390_exc & S390_IEEE_MASK_INEXACT && !XxC) {
 105        /* trap condition - overflow/underflow _not_ reported along */
 106        if (s390_exc & S390_IEEE_MASK_INEXACT & env->fpc >> 24) {
 107            tcg_s390_data_exception(env, s390_exc & S390_IEEE_MASK_INEXACT,
 108                                    retaddr);
 109        }
 110        /* nontrap condition */
 111        env->fpc |= (s390_exc & S390_IEEE_MASK_INEXACT) << 16;
 112    }
 113}
 114
 115static inline int float_comp_to_cc(CPUS390XState *env, int float_compare)
 116{
 117    S390CPU *cpu = s390_env_get_cpu(env);
 118
 119    switch (float_compare) {
 120    case float_relation_equal:
 121        return 0;
 122    case float_relation_less:
 123        return 1;
 124    case float_relation_greater:
 125        return 2;
 126    case float_relation_unordered:
 127        return 3;
 128    default:
 129        cpu_abort(CPU(cpu), "unknown return value for float compare\n");
 130    }
 131}
 132
 133/* condition codes for unary FP ops */
 134uint32_t set_cc_nz_f32(float32 v)
 135{
 136    if (float32_is_any_nan(v)) {
 137        return 3;
 138    } else if (float32_is_zero(v)) {
 139        return 0;
 140    } else if (float32_is_neg(v)) {
 141        return 1;
 142    } else {
 143        return 2;
 144    }
 145}
 146
 147uint32_t set_cc_nz_f64(float64 v)
 148{
 149    if (float64_is_any_nan(v)) {
 150        return 3;
 151    } else if (float64_is_zero(v)) {
 152        return 0;
 153    } else if (float64_is_neg(v)) {
 154        return 1;
 155    } else {
 156        return 2;
 157    }
 158}
 159
 160uint32_t set_cc_nz_f128(float128 v)
 161{
 162    if (float128_is_any_nan(v)) {
 163        return 3;
 164    } else if (float128_is_zero(v)) {
 165        return 0;
 166    } else if (float128_is_neg(v)) {
 167        return 1;
 168    } else {
 169        return 2;
 170    }
 171}
 172
 173static inline uint8_t round_from_m34(uint32_t m34)
 174{
 175    return extract32(m34, 0, 4);
 176}
 177
 178static inline bool xxc_from_m34(uint32_t m34)
 179{
 180    /* XxC is bit 1 of m4 */
 181    return extract32(m34, 4 + 3 - 1, 1);
 182}
 183
 184/* 32-bit FP addition */
 185uint64_t HELPER(aeb)(CPUS390XState *env, uint64_t f1, uint64_t f2)
 186{
 187    float32 ret = float32_add(f1, f2, &env->fpu_status);
 188    handle_exceptions(env, false, GETPC());
 189    return ret;
 190}
 191
 192/* 64-bit FP addition */
 193uint64_t HELPER(adb)(CPUS390XState *env, uint64_t f1, uint64_t f2)
 194{
 195    float64 ret = float64_add(f1, f2, &env->fpu_status);
 196    handle_exceptions(env, false, GETPC());
 197    return ret;
 198}
 199
 200/* 128-bit FP addition */
 201uint64_t HELPER(axb)(CPUS390XState *env, uint64_t ah, uint64_t al,
 202                     uint64_t bh, uint64_t bl)
 203{
 204    float128 ret = float128_add(make_float128(ah, al),
 205                                make_float128(bh, bl),
 206                                &env->fpu_status);
 207    handle_exceptions(env, false, GETPC());
 208    return RET128(ret);
 209}
 210
 211/* 32-bit FP subtraction */
 212uint64_t HELPER(seb)(CPUS390XState *env, uint64_t f1, uint64_t f2)
 213{
 214    float32 ret = float32_sub(f1, f2, &env->fpu_status);
 215    handle_exceptions(env, false, GETPC());
 216    return ret;
 217}
 218
 219/* 64-bit FP subtraction */
 220uint64_t HELPER(sdb)(CPUS390XState *env, uint64_t f1, uint64_t f2)
 221{
 222    float64 ret = float64_sub(f1, f2, &env->fpu_status);
 223    handle_exceptions(env, false, GETPC());
 224    return ret;
 225}
 226
 227/* 128-bit FP subtraction */
 228uint64_t HELPER(sxb)(CPUS390XState *env, uint64_t ah, uint64_t al,
 229                     uint64_t bh, uint64_t bl)
 230{
 231    float128 ret = float128_sub(make_float128(ah, al),
 232                                make_float128(bh, bl),
 233                                &env->fpu_status);
 234    handle_exceptions(env, false, GETPC());
 235    return RET128(ret);
 236}
 237
 238/* 32-bit FP division */
 239uint64_t HELPER(deb)(CPUS390XState *env, uint64_t f1, uint64_t f2)
 240{
 241    float32 ret = float32_div(f1, f2, &env->fpu_status);
 242    handle_exceptions(env, false, GETPC());
 243    return ret;
 244}
 245
 246/* 64-bit FP division */
 247uint64_t HELPER(ddb)(CPUS390XState *env, uint64_t f1, uint64_t f2)
 248{
 249    float64 ret = float64_div(f1, f2, &env->fpu_status);
 250    handle_exceptions(env, false, GETPC());
 251    return ret;
 252}
 253
 254/* 128-bit FP division */
 255uint64_t HELPER(dxb)(CPUS390XState *env, uint64_t ah, uint64_t al,
 256                     uint64_t bh, uint64_t bl)
 257{
 258    float128 ret = float128_div(make_float128(ah, al),
 259                                make_float128(bh, bl),
 260                                &env->fpu_status);
 261    handle_exceptions(env, false, GETPC());
 262    return RET128(ret);
 263}
 264
 265/* 32-bit FP multiplication */
 266uint64_t HELPER(meeb)(CPUS390XState *env, uint64_t f1, uint64_t f2)
 267{
 268    float32 ret = float32_mul(f1, f2, &env->fpu_status);
 269    handle_exceptions(env, false, GETPC());
 270    return ret;
 271}
 272
 273/* 64-bit FP multiplication */
 274uint64_t HELPER(mdb)(CPUS390XState *env, uint64_t f1, uint64_t f2)
 275{
 276    float64 ret = float64_mul(f1, f2, &env->fpu_status);
 277    handle_exceptions(env, false, GETPC());
 278    return ret;
 279}
 280
 281/* 64/32-bit FP multiplication */
 282uint64_t HELPER(mdeb)(CPUS390XState *env, uint64_t f1, uint64_t f2)
 283{
 284    float64 ret = float32_to_float64(f2, &env->fpu_status);
 285    ret = float64_mul(f1, ret, &env->fpu_status);
 286    handle_exceptions(env, false, GETPC());
 287    return ret;
 288}
 289
 290/* 128-bit FP multiplication */
 291uint64_t HELPER(mxb)(CPUS390XState *env, uint64_t ah, uint64_t al,
 292                     uint64_t bh, uint64_t bl)
 293{
 294    float128 ret = float128_mul(make_float128(ah, al),
 295                                make_float128(bh, bl),
 296                                &env->fpu_status);
 297    handle_exceptions(env, false, GETPC());
 298    return RET128(ret);
 299}
 300
 301/* 128/64-bit FP multiplication */
 302uint64_t HELPER(mxdb)(CPUS390XState *env, uint64_t ah, uint64_t al,
 303                      uint64_t f2)
 304{
 305    float128 ret = float64_to_float128(f2, &env->fpu_status);
 306    ret = float128_mul(make_float128(ah, al), ret, &env->fpu_status);
 307    handle_exceptions(env, false, GETPC());
 308    return RET128(ret);
 309}
 310
 311/* convert 32-bit float to 64-bit float */
 312uint64_t HELPER(ldeb)(CPUS390XState *env, uint64_t f2)
 313{
 314    float64 ret = float32_to_float64(f2, &env->fpu_status);
 315    handle_exceptions(env, false, GETPC());
 316    return ret;
 317}
 318
 319/* convert 128-bit float to 64-bit float */
 320uint64_t HELPER(ldxb)(CPUS390XState *env, uint64_t ah, uint64_t al,
 321                      uint32_t m34)
 322{
 323    int old_mode = s390_swap_bfp_rounding_mode(env, round_from_m34(m34));
 324    float64 ret = float128_to_float64(make_float128(ah, al), &env->fpu_status);
 325
 326    s390_restore_bfp_rounding_mode(env, old_mode);
 327    handle_exceptions(env, xxc_from_m34(m34), GETPC());
 328    return ret;
 329}
 330
 331/* convert 64-bit float to 128-bit float */
 332uint64_t HELPER(lxdb)(CPUS390XState *env, uint64_t f2)
 333{
 334    float128 ret = float64_to_float128(f2, &env->fpu_status);
 335    handle_exceptions(env, false, GETPC());
 336    return RET128(ret);
 337}
 338
 339/* convert 32-bit float to 128-bit float */
 340uint64_t HELPER(lxeb)(CPUS390XState *env, uint64_t f2)
 341{
 342    float128 ret = float32_to_float128(f2, &env->fpu_status);
 343    handle_exceptions(env, false, GETPC());
 344    return RET128(ret);
 345}
 346
 347/* convert 64-bit float to 32-bit float */
 348uint64_t HELPER(ledb)(CPUS390XState *env, uint64_t f2, uint32_t m34)
 349{
 350    int old_mode = s390_swap_bfp_rounding_mode(env, round_from_m34(m34));
 351    float32 ret = float64_to_float32(f2, &env->fpu_status);
 352
 353    s390_restore_bfp_rounding_mode(env, old_mode);
 354    handle_exceptions(env, xxc_from_m34(m34), GETPC());
 355    return ret;
 356}
 357
 358/* convert 128-bit float to 32-bit float */
 359uint64_t HELPER(lexb)(CPUS390XState *env, uint64_t ah, uint64_t al,
 360                      uint32_t m34)
 361{
 362    int old_mode = s390_swap_bfp_rounding_mode(env, round_from_m34(m34));
 363    float32 ret = float128_to_float32(make_float128(ah, al), &env->fpu_status);
 364
 365    s390_restore_bfp_rounding_mode(env, old_mode);
 366    handle_exceptions(env, xxc_from_m34(m34), GETPC());
 367    return ret;
 368}
 369
 370/* 32-bit FP compare */
 371uint32_t HELPER(ceb)(CPUS390XState *env, uint64_t f1, uint64_t f2)
 372{
 373    int cmp = float32_compare_quiet(f1, f2, &env->fpu_status);
 374    handle_exceptions(env, false, GETPC());
 375    return float_comp_to_cc(env, cmp);
 376}
 377
 378/* 64-bit FP compare */
 379uint32_t HELPER(cdb)(CPUS390XState *env, uint64_t f1, uint64_t f2)
 380{
 381    int cmp = float64_compare_quiet(f1, f2, &env->fpu_status);
 382    handle_exceptions(env, false, GETPC());
 383    return float_comp_to_cc(env, cmp);
 384}
 385
 386/* 128-bit FP compare */
 387uint32_t HELPER(cxb)(CPUS390XState *env, uint64_t ah, uint64_t al,
 388                     uint64_t bh, uint64_t bl)
 389{
 390    int cmp = float128_compare_quiet(make_float128(ah, al),
 391                                     make_float128(bh, bl),
 392                                     &env->fpu_status);
 393    handle_exceptions(env, false, GETPC());
 394    return float_comp_to_cc(env, cmp);
 395}
 396
 397int s390_swap_bfp_rounding_mode(CPUS390XState *env, int m3)
 398{
 399    int ret = env->fpu_status.float_rounding_mode;
 400
 401    switch (m3) {
 402    case 0:
 403        /* current mode */
 404        break;
 405    case 1:
 406        /* round to nearest with ties away from 0 */
 407        set_float_rounding_mode(float_round_ties_away, &env->fpu_status);
 408        break;
 409    case 3:
 410        /* round to prepare for shorter precision */
 411        set_float_rounding_mode(float_round_to_odd, &env->fpu_status);
 412        break;
 413    case 4:
 414        /* round to nearest with ties to even */
 415        set_float_rounding_mode(float_round_nearest_even, &env->fpu_status);
 416        break;
 417    case 5:
 418        /* round to zero */
 419        set_float_rounding_mode(float_round_to_zero, &env->fpu_status);
 420        break;
 421    case 6:
 422        /* round to +inf */
 423        set_float_rounding_mode(float_round_up, &env->fpu_status);
 424        break;
 425    case 7:
 426        /* round to -inf */
 427        set_float_rounding_mode(float_round_down, &env->fpu_status);
 428        break;
 429    default:
 430        g_assert_not_reached();
 431    }
 432    return ret;
 433}
 434
 435void s390_restore_bfp_rounding_mode(CPUS390XState *env, int old_mode)
 436{
 437    set_float_rounding_mode(old_mode, &env->fpu_status);
 438}
 439
 440/* convert 64-bit int to 32-bit float */
 441uint64_t HELPER(cegb)(CPUS390XState *env, int64_t v2, uint32_t m34)
 442{
 443    int old_mode = s390_swap_bfp_rounding_mode(env, round_from_m34(m34));
 444    float32 ret = int64_to_float32(v2, &env->fpu_status);
 445
 446    s390_restore_bfp_rounding_mode(env, old_mode);
 447    handle_exceptions(env, xxc_from_m34(m34), GETPC());
 448    return ret;
 449}
 450
 451/* convert 64-bit int to 64-bit float */
 452uint64_t HELPER(cdgb)(CPUS390XState *env, int64_t v2, uint32_t m34)
 453{
 454    int old_mode = s390_swap_bfp_rounding_mode(env, round_from_m34(m34));
 455    float64 ret = int64_to_float64(v2, &env->fpu_status);
 456
 457    s390_restore_bfp_rounding_mode(env, old_mode);
 458    handle_exceptions(env, xxc_from_m34(m34), GETPC());
 459    return ret;
 460}
 461
 462/* convert 64-bit int to 128-bit float */
 463uint64_t HELPER(cxgb)(CPUS390XState *env, int64_t v2, uint32_t m34)
 464{
 465    int old_mode = s390_swap_bfp_rounding_mode(env, round_from_m34(m34));
 466    float128 ret = int64_to_float128(v2, &env->fpu_status);
 467
 468    s390_restore_bfp_rounding_mode(env, old_mode);
 469    handle_exceptions(env, xxc_from_m34(m34), GETPC());
 470    return RET128(ret);
 471}
 472
 473/* convert 64-bit uint to 32-bit float */
 474uint64_t HELPER(celgb)(CPUS390XState *env, uint64_t v2, uint32_t m34)
 475{
 476    int old_mode = s390_swap_bfp_rounding_mode(env, round_from_m34(m34));
 477    float32 ret = uint64_to_float32(v2, &env->fpu_status);
 478
 479    s390_restore_bfp_rounding_mode(env, old_mode);
 480    handle_exceptions(env, xxc_from_m34(m34), GETPC());
 481    return ret;
 482}
 483
 484/* convert 64-bit uint to 64-bit float */
 485uint64_t HELPER(cdlgb)(CPUS390XState *env, uint64_t v2, uint32_t m34)
 486{
 487    int old_mode = s390_swap_bfp_rounding_mode(env, round_from_m34(m34));
 488    float64 ret = uint64_to_float64(v2, &env->fpu_status);
 489
 490    s390_restore_bfp_rounding_mode(env, old_mode);
 491    handle_exceptions(env, xxc_from_m34(m34), GETPC());
 492    return ret;
 493}
 494
 495/* convert 64-bit uint to 128-bit float */
 496uint64_t HELPER(cxlgb)(CPUS390XState *env, uint64_t v2, uint32_t m34)
 497{
 498    int old_mode = s390_swap_bfp_rounding_mode(env, round_from_m34(m34));
 499    float128 ret = uint64_to_float128(v2, &env->fpu_status);
 500
 501    s390_restore_bfp_rounding_mode(env, old_mode);
 502    handle_exceptions(env, xxc_from_m34(m34), GETPC());
 503    return RET128(ret);
 504}
 505
 506/* convert 32-bit float to 64-bit int */
 507uint64_t HELPER(cgeb)(CPUS390XState *env, uint64_t v2, uint32_t m34)
 508{
 509    int old_mode = s390_swap_bfp_rounding_mode(env, round_from_m34(m34));
 510    int64_t ret = float32_to_int64(v2, &env->fpu_status);
 511
 512    s390_restore_bfp_rounding_mode(env, old_mode);
 513    handle_exceptions(env, xxc_from_m34(m34), GETPC());
 514    return ret;
 515}
 516
 517/* convert 64-bit float to 64-bit int */
 518uint64_t HELPER(cgdb)(CPUS390XState *env, uint64_t v2, uint32_t m34)
 519{
 520    int old_mode = s390_swap_bfp_rounding_mode(env, round_from_m34(m34));
 521    int64_t ret = float64_to_int64(v2, &env->fpu_status);
 522
 523    s390_restore_bfp_rounding_mode(env, old_mode);
 524    handle_exceptions(env, xxc_from_m34(m34), GETPC());
 525    return ret;
 526}
 527
 528/* convert 128-bit float to 64-bit int */
 529uint64_t HELPER(cgxb)(CPUS390XState *env, uint64_t h, uint64_t l, uint32_t m34)
 530{
 531    int old_mode = s390_swap_bfp_rounding_mode(env, round_from_m34(m34));
 532    float128 v2 = make_float128(h, l);
 533    int64_t ret = float128_to_int64(v2, &env->fpu_status);
 534
 535    s390_restore_bfp_rounding_mode(env, old_mode);
 536    handle_exceptions(env, xxc_from_m34(m34), GETPC());
 537    return ret;
 538}
 539
 540/* convert 32-bit float to 32-bit int */
 541uint64_t HELPER(cfeb)(CPUS390XState *env, uint64_t v2, uint32_t m34)
 542{
 543    int old_mode = s390_swap_bfp_rounding_mode(env, round_from_m34(m34));
 544    int32_t ret = float32_to_int32(v2, &env->fpu_status);
 545
 546    s390_restore_bfp_rounding_mode(env, old_mode);
 547    handle_exceptions(env, xxc_from_m34(m34), GETPC());
 548    return ret;
 549}
 550
 551/* convert 64-bit float to 32-bit int */
 552uint64_t HELPER(cfdb)(CPUS390XState *env, uint64_t v2, uint32_t m34)
 553{
 554    int old_mode = s390_swap_bfp_rounding_mode(env, round_from_m34(m34));
 555    int32_t ret = float64_to_int32(v2, &env->fpu_status);
 556
 557    s390_restore_bfp_rounding_mode(env, old_mode);
 558    handle_exceptions(env, xxc_from_m34(m34), GETPC());
 559    return ret;
 560}
 561
 562/* convert 128-bit float to 32-bit int */
 563uint64_t HELPER(cfxb)(CPUS390XState *env, uint64_t h, uint64_t l, uint32_t m34)
 564{
 565    int old_mode = s390_swap_bfp_rounding_mode(env, round_from_m34(m34));
 566    float128 v2 = make_float128(h, l);
 567    int32_t ret = float128_to_int32(v2, &env->fpu_status);
 568
 569    s390_restore_bfp_rounding_mode(env, old_mode);
 570    handle_exceptions(env, xxc_from_m34(m34), GETPC());
 571    return ret;
 572}
 573
 574/* convert 32-bit float to 64-bit uint */
 575uint64_t HELPER(clgeb)(CPUS390XState *env, uint64_t v2, uint32_t m34)
 576{
 577    int old_mode = s390_swap_bfp_rounding_mode(env, round_from_m34(m34));
 578    uint64_t ret;
 579
 580    v2 = float32_to_float64(v2, &env->fpu_status);
 581    ret = float64_to_uint64(v2, &env->fpu_status);
 582    s390_restore_bfp_rounding_mode(env, old_mode);
 583    handle_exceptions(env, xxc_from_m34(m34), GETPC());
 584    return ret;
 585}
 586
 587/* convert 64-bit float to 64-bit uint */
 588uint64_t HELPER(clgdb)(CPUS390XState *env, uint64_t v2, uint32_t m34)
 589{
 590    int old_mode = s390_swap_bfp_rounding_mode(env, round_from_m34(m34));
 591    uint64_t ret = float64_to_uint64(v2, &env->fpu_status);
 592
 593    s390_restore_bfp_rounding_mode(env, old_mode);
 594    handle_exceptions(env, xxc_from_m34(m34), GETPC());
 595    return ret;
 596}
 597
 598/* convert 128-bit float to 64-bit uint */
 599uint64_t HELPER(clgxb)(CPUS390XState *env, uint64_t h, uint64_t l, uint32_t m34)
 600{
 601    int old_mode = s390_swap_bfp_rounding_mode(env, round_from_m34(m34));
 602    uint64_t ret = float128_to_uint64(make_float128(h, l), &env->fpu_status);
 603
 604    s390_restore_bfp_rounding_mode(env, old_mode);
 605    handle_exceptions(env, xxc_from_m34(m34), GETPC());
 606    return ret;
 607}
 608
 609/* convert 32-bit float to 32-bit uint */
 610uint64_t HELPER(clfeb)(CPUS390XState *env, uint64_t v2, uint32_t m34)
 611{
 612    int old_mode = s390_swap_bfp_rounding_mode(env, round_from_m34(m34));
 613    uint32_t ret = float32_to_uint32(v2, &env->fpu_status);
 614
 615    s390_restore_bfp_rounding_mode(env, old_mode);
 616    handle_exceptions(env, xxc_from_m34(m34), GETPC());
 617    return ret;
 618}
 619
 620/* convert 64-bit float to 32-bit uint */
 621uint64_t HELPER(clfdb)(CPUS390XState *env, uint64_t v2, uint32_t m34)
 622{
 623    int old_mode = s390_swap_bfp_rounding_mode(env, round_from_m34(m34));
 624    uint32_t ret = float64_to_uint32(v2, &env->fpu_status);
 625
 626    s390_restore_bfp_rounding_mode(env, old_mode);
 627    handle_exceptions(env, xxc_from_m34(m34), GETPC());
 628    return ret;
 629}
 630
 631/* convert 128-bit float to 32-bit uint */
 632uint64_t HELPER(clfxb)(CPUS390XState *env, uint64_t h, uint64_t l, uint32_t m34)
 633{
 634    int old_mode = s390_swap_bfp_rounding_mode(env, round_from_m34(m34));
 635    uint32_t ret = float128_to_uint32(make_float128(h, l), &env->fpu_status);
 636
 637    s390_restore_bfp_rounding_mode(env, old_mode);
 638    handle_exceptions(env, xxc_from_m34(m34), GETPC());
 639    return ret;
 640}
 641
 642/* round to integer 32-bit */
 643uint64_t HELPER(fieb)(CPUS390XState *env, uint64_t f2, uint32_t m34)
 644{
 645    int old_mode = s390_swap_bfp_rounding_mode(env, round_from_m34(m34));
 646    float32 ret = float32_round_to_int(f2, &env->fpu_status);
 647
 648    s390_restore_bfp_rounding_mode(env, old_mode);
 649    handle_exceptions(env, xxc_from_m34(m34), GETPC());
 650    return ret;
 651}
 652
 653/* round to integer 64-bit */
 654uint64_t HELPER(fidb)(CPUS390XState *env, uint64_t f2, uint32_t m34)
 655{
 656    int old_mode = s390_swap_bfp_rounding_mode(env, round_from_m34(m34));
 657    float64 ret = float64_round_to_int(f2, &env->fpu_status);
 658
 659    s390_restore_bfp_rounding_mode(env, old_mode);
 660    handle_exceptions(env, xxc_from_m34(m34), GETPC());
 661    return ret;
 662}
 663
 664/* round to integer 128-bit */
 665uint64_t HELPER(fixb)(CPUS390XState *env, uint64_t ah, uint64_t al,
 666                      uint32_t m34)
 667{
 668    int old_mode = s390_swap_bfp_rounding_mode(env, round_from_m34(m34));
 669    float128 ret = float128_round_to_int(make_float128(ah, al),
 670                                         &env->fpu_status);
 671
 672    s390_restore_bfp_rounding_mode(env, old_mode);
 673    handle_exceptions(env, xxc_from_m34(m34), GETPC());
 674    return RET128(ret);
 675}
 676
 677/* 32-bit FP compare and signal */
 678uint32_t HELPER(keb)(CPUS390XState *env, uint64_t f1, uint64_t f2)
 679{
 680    int cmp = float32_compare(f1, f2, &env->fpu_status);
 681    handle_exceptions(env, false, GETPC());
 682    return float_comp_to_cc(env, cmp);
 683}
 684
 685/* 64-bit FP compare and signal */
 686uint32_t HELPER(kdb)(CPUS390XState *env, uint64_t f1, uint64_t f2)
 687{
 688    int cmp = float64_compare(f1, f2, &env->fpu_status);
 689    handle_exceptions(env, false, GETPC());
 690    return float_comp_to_cc(env, cmp);
 691}
 692
 693/* 128-bit FP compare and signal */
 694uint32_t HELPER(kxb)(CPUS390XState *env, uint64_t ah, uint64_t al,
 695                     uint64_t bh, uint64_t bl)
 696{
 697    int cmp = float128_compare(make_float128(ah, al),
 698                               make_float128(bh, bl),
 699                               &env->fpu_status);
 700    handle_exceptions(env, false, GETPC());
 701    return float_comp_to_cc(env, cmp);
 702}
 703
 704/* 32-bit FP multiply and add */
 705uint64_t HELPER(maeb)(CPUS390XState *env, uint64_t f1,
 706                      uint64_t f2, uint64_t f3)
 707{
 708    float32 ret = float32_muladd(f2, f3, f1, 0, &env->fpu_status);
 709    handle_exceptions(env, false, GETPC());
 710    return ret;
 711}
 712
 713/* 64-bit FP multiply and add */
 714uint64_t HELPER(madb)(CPUS390XState *env, uint64_t f1,
 715                      uint64_t f2, uint64_t f3)
 716{
 717    float64 ret = float64_muladd(f2, f3, f1, 0, &env->fpu_status);
 718    handle_exceptions(env, false, GETPC());
 719    return ret;
 720}
 721
 722/* 32-bit FP multiply and subtract */
 723uint64_t HELPER(mseb)(CPUS390XState *env, uint64_t f1,
 724                      uint64_t f2, uint64_t f3)
 725{
 726    float32 ret = float32_muladd(f2, f3, f1, float_muladd_negate_c,
 727                                 &env->fpu_status);
 728    handle_exceptions(env, false, GETPC());
 729    return ret;
 730}
 731
 732/* 64-bit FP multiply and subtract */
 733uint64_t HELPER(msdb)(CPUS390XState *env, uint64_t f1,
 734                      uint64_t f2, uint64_t f3)
 735{
 736    float64 ret = float64_muladd(f2, f3, f1, float_muladd_negate_c,
 737                                 &env->fpu_status);
 738    handle_exceptions(env, false, GETPC());
 739    return ret;
 740}
 741
 742/* The rightmost bit has the number 11. */
 743static inline uint16_t dcmask(int bit, bool neg)
 744{
 745    return 1 << (11 - bit - neg);
 746}
 747
 748#define DEF_FLOAT_DCMASK(_TYPE) \
 749static uint16_t _TYPE##_dcmask(CPUS390XState *env, _TYPE f1)       \
 750{                                                                  \
 751    const bool neg = _TYPE##_is_neg(f1);                           \
 752                                                                   \
 753    /* Sorted by most common cases - only one class is possible */ \
 754    if (_TYPE##_is_normal(f1)) {                                   \
 755        return dcmask(2, neg);                                     \
 756    } else if (_TYPE##_is_zero(f1)) {                              \
 757        return dcmask(0, neg);                                     \
 758    } else if (_TYPE##_is_denormal(f1)) {                          \
 759        return dcmask(4, neg);                                     \
 760    } else if (_TYPE##_is_infinity(f1)) {                          \
 761        return dcmask(6, neg);                                     \
 762    } else if (_TYPE##_is_quiet_nan(f1, &env->fpu_status)) {       \
 763        return dcmask(8, neg);                                     \
 764    }                                                              \
 765    /* signaling nan, as last remaining case */                    \
 766    return dcmask(10, neg);                                        \
 767}
 768DEF_FLOAT_DCMASK(float32)
 769DEF_FLOAT_DCMASK(float64)
 770DEF_FLOAT_DCMASK(float128)
 771
 772/* test data class 32-bit */
 773uint32_t HELPER(tceb)(CPUS390XState *env, uint64_t f1, uint64_t m2)
 774{
 775    return (m2 & float32_dcmask(env, f1)) != 0;
 776}
 777
 778/* test data class 64-bit */
 779uint32_t HELPER(tcdb)(CPUS390XState *env, uint64_t v1, uint64_t m2)
 780{
 781    return (m2 & float64_dcmask(env, v1)) != 0;
 782}
 783
 784/* test data class 128-bit */
 785uint32_t HELPER(tcxb)(CPUS390XState *env, uint64_t ah, uint64_t al, uint64_t m2)
 786{
 787    return (m2 & float128_dcmask(env, make_float128(ah, al))) != 0;
 788}
 789
 790/* square root 32-bit */
 791uint64_t HELPER(sqeb)(CPUS390XState *env, uint64_t f2)
 792{
 793    float32 ret = float32_sqrt(f2, &env->fpu_status);
 794    handle_exceptions(env, false, GETPC());
 795    return ret;
 796}
 797
 798/* square root 64-bit */
 799uint64_t HELPER(sqdb)(CPUS390XState *env, uint64_t f2)
 800{
 801    float64 ret = float64_sqrt(f2, &env->fpu_status);
 802    handle_exceptions(env, false, GETPC());
 803    return ret;
 804}
 805
 806/* square root 128-bit */
 807uint64_t HELPER(sqxb)(CPUS390XState *env, uint64_t ah, uint64_t al)
 808{
 809    float128 ret = float128_sqrt(make_float128(ah, al), &env->fpu_status);
 810    handle_exceptions(env, false, GETPC());
 811    return RET128(ret);
 812}
 813
 814static const int fpc_to_rnd[8] = {
 815    float_round_nearest_even,
 816    float_round_to_zero,
 817    float_round_up,
 818    float_round_down,
 819    -1,
 820    -1,
 821    -1,
 822    float_round_to_odd,
 823};
 824
 825/* set fpc */
 826void HELPER(sfpc)(CPUS390XState *env, uint64_t fpc)
 827{
 828    if (fpc_to_rnd[fpc & 0x7] == -1 || fpc & 0x03030088u ||
 829        (!s390_has_feat(S390_FEAT_FLOATING_POINT_EXT) && fpc & 0x4)) {
 830        s390_program_interrupt(env, PGM_SPECIFICATION, ILEN_AUTO, GETPC());
 831    }
 832
 833    /* Install everything in the main FPC.  */
 834    env->fpc = fpc;
 835
 836    /* Install the rounding mode in the shadow fpu_status.  */
 837    set_float_rounding_mode(fpc_to_rnd[fpc & 0x7], &env->fpu_status);
 838}
 839
 840/* set fpc and signal */
 841void HELPER(sfas)(CPUS390XState *env, uint64_t fpc)
 842{
 843    uint32_t signalling = env->fpc;
 844    uint32_t s390_exc;
 845
 846    if (fpc_to_rnd[fpc & 0x7] == -1 || fpc & 0x03030088u ||
 847        (!s390_has_feat(S390_FEAT_FLOATING_POINT_EXT) && fpc & 0x4)) {
 848        s390_program_interrupt(env, PGM_SPECIFICATION, ILEN_AUTO, GETPC());
 849    }
 850
 851    /*
 852     * FPC is set to the FPC operand with a bitwise OR of the signalling
 853     * flags.
 854     */
 855    env->fpc = fpc | (signalling & 0x00ff0000);
 856    set_float_rounding_mode(fpc_to_rnd[fpc & 0x7], &env->fpu_status);
 857
 858    /*
 859     * If any signaling flag is enabled in the new FPC mask, a
 860     * simulated-iee-exception exception occurs.
 861     */
 862    s390_exc = (signalling >> 16) & (fpc >> 24);
 863    if (s390_exc) {
 864        if (s390_exc & S390_IEEE_MASK_INVALID) {
 865            s390_exc = S390_IEEE_MASK_INVALID;
 866        } else if (s390_exc & S390_IEEE_MASK_DIVBYZERO) {
 867            s390_exc = S390_IEEE_MASK_DIVBYZERO;
 868        } else if (s390_exc & S390_IEEE_MASK_OVERFLOW) {
 869            s390_exc &= (S390_IEEE_MASK_OVERFLOW | S390_IEEE_MASK_INEXACT);
 870        } else if (s390_exc & S390_IEEE_MASK_UNDERFLOW) {
 871            s390_exc &= (S390_IEEE_MASK_UNDERFLOW | S390_IEEE_MASK_INEXACT);
 872        } else if (s390_exc & S390_IEEE_MASK_INEXACT) {
 873            s390_exc = S390_IEEE_MASK_INEXACT;
 874        } else if (s390_exc & S390_IEEE_MASK_QUANTUM) {
 875            s390_exc = S390_IEEE_MASK_QUANTUM;
 876        }
 877        tcg_s390_data_exception(env, s390_exc | 3, GETPC());
 878    }
 879}
 880
 881/* set bfp rounding mode */
 882void HELPER(srnm)(CPUS390XState *env, uint64_t rnd)
 883{
 884    if (rnd > 0x7 || fpc_to_rnd[rnd & 0x7] == -1) {
 885        s390_program_interrupt(env, PGM_SPECIFICATION, ILEN_AUTO, GETPC());
 886    }
 887
 888    env->fpc = deposit32(env->fpc, 0, 3, rnd);
 889    set_float_rounding_mode(fpc_to_rnd[rnd & 0x7], &env->fpu_status);
 890}
 891