From patchwork Mon Jul 23 20:17:22 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Michael Roth X-Patchwork-Id: 142658 Delivered-To: patch@linaro.org Received: by 2002:a2e:9754:0:0:0:0:0 with SMTP id f20-v6csp6514337ljj; Mon, 23 Jul 2018 14:28:49 -0700 (PDT) X-Google-Smtp-Source: AAOMgpdpeOv9r/p9BJnDROCqEc7URgifdCQA3mZbl+VnF8YiMs0mS7K/5mObGRJ4QIX0xxwCkDKK X-Received: by 2002:a37:d61b:: with SMTP id t27-v6mr12611058qki.244.1532381329639; Mon, 23 Jul 2018 14:28:49 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1532381329; cv=none; d=google.com; s=arc-20160816; b=Tg5JV4IlrpOTcFX4QECbrwLtT4gVhLngU3RyVmCs2TpfVDSuPIdigH2tgOfZAr1kKi tPYG7xHwnqQzIQBAWncsuTj78OlnMrd2lZ57aolmBdoZxh3zu29CbNXN7IZyE65SdZmD 2ii3wbEY26kxeguy/yfp8Tjuae8hggd3jWf8UUCFVH35jUCXnwTLaQVtwTg2WuvRXGic sLXjFRLGG7t9+Ue7LM+lvIk9VZV8WWN6+HUMLQ2fVbeJUlLt2QE1W3X8xHx3L7LOfnnd 2dvey3nwXWvbK8YOU3WiacvKf8whTPUbwnSHgevCC/kPTyBznhqtHVdO4hqMsj6XPs5R yC2Q== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:cc:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:subject :content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:to:from:dkim-signature:arc-authentication-results; bh=EAAvhRG8C54tuD9t5OWl5Oz5gtLAuNU9fBik2fQBfos=; b=pDxOGTvSppJiI9iEYMpYVau5bcjZkWSYiekEsj9yVHvdbccdij8NXxvQxT3UsNomJd kJq8HBzbGbcsT0725GHLXMJlNmOzitjzPlkhcnumA2j7zj++QBCA/q7pWytqfhN784FH u1vjukjZx44FOKBv1gSo/hNhT5Skl0EBLEmfntCMQDxiyD5ujcrj9XhFw/GlsJMsw0Xz FtnyR3rZmJd/IP5WtxmaWugSUVCtl49znOvCOiDygzyJGJm6MsvrWmFrXbVQzfBApbTA NEZ7dhJbmMmHX1kQE09k6tE1E0bcIrSXi4HT7Uo9TvackilQglOUuRJPxUFcIbsuzVa4 xvPg== ARC-Authentication-Results: i=1; mx.google.com; dkim=fail header.i=@gmail.com header.s=20161025 header.b=i9QYhJA8; spf=pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 2001:4830:134:3::11 as permitted sender) smtp.mailfrom="qemu-devel-bounces+patch=linaro.org@nongnu.org"; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=ibm.com Return-Path: Received: from lists.gnu.org (lists.gnu.org. [2001:4830:134:3::11]) by mx.google.com with ESMTPS id h128-v6si9737252qkc.16.2018.07.23.14.28.49 for (version=TLS1 cipher=AES128-SHA bits=128/128); Mon, 23 Jul 2018 14:28:49 -0700 (PDT) Received-SPF: pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 2001:4830:134:3::11 as permitted sender) client-ip=2001:4830:134:3::11; Authentication-Results: mx.google.com; dkim=fail header.i=@gmail.com header.s=20161025 header.b=i9QYhJA8; spf=pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 2001:4830:134:3::11 as permitted sender) smtp.mailfrom="qemu-devel-bounces+patch=linaro.org@nongnu.org"; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=ibm.com Received: from localhost ([::1]:36773 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1fhiNt-0004vm-1p for patch@linaro.org; Mon, 23 Jul 2018 17:28:49 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:42955) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1fhhKW-0003kj-Ba for qemu-devel@nongnu.org; Mon, 23 Jul 2018 16:21:17 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1fhhKV-0005Hd-1k for qemu-devel@nongnu.org; Mon, 23 Jul 2018 16:21:16 -0400 Received: from mail-oi0-x241.google.com ([2607:f8b0:4003:c06::241]:33593) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1fhhKU-0005HP-Qq; Mon, 23 Jul 2018 16:21:14 -0400 Received: by mail-oi0-x241.google.com with SMTP id l10-v6so3506629oii.0; Mon, 23 Jul 2018 13:21:14 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=sender:from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=EAAvhRG8C54tuD9t5OWl5Oz5gtLAuNU9fBik2fQBfos=; b=i9QYhJA8myxOL6pHdntg9tDJu3YeDv3eBLkR8rNKL54/Xw6XVxlTwVAJmCxhE8I1YQ MvhYqVdFU1HbCt1slUvnKG5neHyX0YD/SEZKOinSboJD2UnA7eOqgErLQNOuKtAaej+I TH9DRi2idC1jN7Qc6qqraYuKgZ4YZfTKfJerwKgR6xcg6JotmG6b35fT/ebAxzYiPU9X hrcOK12+xhYLtY97ZPHtAgZcQsUwmg5D0NvDU1y+lN7+jazBTqrh3bq+B6eVpRcGBI2Z J5r9OEy3FLPmx/FllPtrfaKlZmn3aDWFdfYzUWsfw0Z+3W6hbys0sCUF0nSJlIdoeizD gjhQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:sender:from:to:cc:subject:date:message-id :in-reply-to:references:mime-version:content-transfer-encoding; bh=EAAvhRG8C54tuD9t5OWl5Oz5gtLAuNU9fBik2fQBfos=; b=qwivXi6ZXmj59suiXSkLxniI7n1sritkte8fngNXRImjRqGf4JHsviLwpKbb5sYWld UmNeUSrOKKUTLikqvyCsVYoOFXe9t3i76CICDSbHPtjWnEM2qoUXSvWuakSfPvM9LMJY YA6Gy9llOt2CCN5Cw/fMqbtpjZ0cVyKMw08zOdKnEF+0YitFRJRq1wCzKPYBJyp4zUFn QT2PSUsjfGaTjRmp4dfWsWowKH8oWLhEgs58M5xzcaEgcCR67+PwxubgRGhV0OoP4ZVf A35EqSv3mtY07BmVpsoetrmT6K9hcIGSqdEhicAi4r2ZLOclsv8SHzjAAUCp4onrn8sB Nm0A== X-Gm-Message-State: AOUpUlF6ZFmSRJPg9aMA13ZbSZO7NAbMuQitRW1FG9IPkdDw+WWfgRST jwgzYVm4SjnYGmZPFgj9D6K7nl1TbThpxg== X-Received: by 2002:aca:5003:: with SMTP id e3-v6mr315979oib.89.1532377273609; Mon, 23 Jul 2018 13:21:13 -0700 (PDT) Received: from localhost (76-251-165-188.lightspeed.austtx.sbcglobal.net. [76.251.165.188]) by smtp.gmail.com with ESMTPSA id t131-v6sm8734870oie.34.2018.07.23.13.21.12 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Mon, 23 Jul 2018 13:21:12 -0700 (PDT) From: Michael Roth To: qemu-devel@nongnu.org Date: Mon, 23 Jul 2018 15:17:22 -0500 Message-Id: <20180723201748.25573-74-mdroth@linux.vnet.ibm.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20180723201748.25573-1-mdroth@linux.vnet.ibm.com> References: <20180723201748.25573-1-mdroth@linux.vnet.ibm.com> MIME-Version: 1.0 X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 2607:f8b0:4003:c06::241 Subject: [Qemu-devel] [PATCH 73/99] target/arm: Fix float16 to/from int16 X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Peter Maydell , Richard Henderson , qemu-stable@nongnu.org Errors-To: qemu-devel-bounces+patch=linaro.org@nongnu.org Sender: "Qemu-devel" From: Richard Henderson The instruction "ucvtf v0.4h, v04h, #2", with input 0x8000u, overflows the intermediate float16 to infinity before we have a chance to scale the output. Use float64 as the intermediate type so that no input argument (uint32_t in this case) can overflow or round before scaling. Given the declared argument, the signed int32_t function has the same problem. When converting from float16 to integer, using u/int32_t instead of u/int16_t means that the bounding is incorrect. Cc: qemu-stable@nongnu.org Reviewed-by: Alex Bennée Signed-off-by: Richard Henderson Message-id: 20180502221552.3873-4-richard.henderson@linaro.org Signed-off-by: Peter Maydell (cherry picked from commit 88808a022c06f98d81cd3f2d105a5734c5614839) Signed-off-by: Michael Roth --- target/arm/helper.c | 53 ++++++++++++++++++++++++++++++++++++-- target/arm/helper.h | 4 +-- target/arm/translate-a64.c | 4 +-- 3 files changed, 55 insertions(+), 6 deletions(-) -- 2.17.1 diff --git a/target/arm/helper.c b/target/arm/helper.c index b14fdab140..c07c1d7f48 100644 --- a/target/arm/helper.c +++ b/target/arm/helper.c @@ -11409,11 +11409,60 @@ VFP_CONV_FIX_A64(sq, s, 32, 64, int64) VFP_CONV_FIX(uh, s, 32, 32, uint16) VFP_CONV_FIX(ul, s, 32, 32, uint32) VFP_CONV_FIX_A64(uq, s, 32, 64, uint64) -VFP_CONV_FIX_A64(sl, h, 16, 32, int32) -VFP_CONV_FIX_A64(ul, h, 16, 32, uint32) + #undef VFP_CONV_FIX #undef VFP_CONV_FIX_FLOAT #undef VFP_CONV_FLOAT_FIX_ROUND +#undef VFP_CONV_FIX_A64 + +/* Conversion to/from f16 can overflow to infinity before/after scaling. + * Therefore we convert to f64 (which does not round), scale, + * and then convert f64 to f16 (which may round). + */ + +static float16 do_postscale_fp16(float64 f, int shift, float_status *fpst) +{ + return float64_to_float16(float64_scalbn(f, -shift, fpst), true, fpst); +} + +float16 HELPER(vfp_sltoh)(uint32_t x, uint32_t shift, void *fpst) +{ + return do_postscale_fp16(int32_to_float64(x, fpst), shift, fpst); +} + +float16 HELPER(vfp_ultoh)(uint32_t x, uint32_t shift, void *fpst) +{ + return do_postscale_fp16(uint32_to_float64(x, fpst), shift, fpst); +} + +static float64 do_prescale_fp16(float16 f, int shift, float_status *fpst) +{ + if (unlikely(float16_is_any_nan(f))) { + float_raise(float_flag_invalid, fpst); + return 0; + } else { + int old_exc_flags = get_float_exception_flags(fpst); + float64 ret; + + ret = float16_to_float64(f, true, fpst); + ret = float64_scalbn(ret, shift, fpst); + old_exc_flags |= get_float_exception_flags(fpst) + & float_flag_input_denormal; + set_float_exception_flags(old_exc_flags, fpst); + + return ret; + } +} + +uint32_t HELPER(vfp_toshh)(float16 x, uint32_t shift, void *fpst) +{ + return float64_to_int16(do_prescale_fp16(x, shift, fpst), fpst); +} + +uint32_t HELPER(vfp_touhh)(float16 x, uint32_t shift, void *fpst) +{ + return float64_to_uint16(do_prescale_fp16(x, shift, fpst), fpst); +} /* Set the current fp rounding mode and return the old one. * The argument is a softfloat float_round_ value. diff --git a/target/arm/helper.h b/target/arm/helper.h index 34e8cc8904..1969b37f2d 100644 --- a/target/arm/helper.h +++ b/target/arm/helper.h @@ -149,8 +149,8 @@ DEF_HELPER_3(vfp_toshd_round_to_zero, i64, f64, i32, ptr) DEF_HELPER_3(vfp_tosld_round_to_zero, i64, f64, i32, ptr) DEF_HELPER_3(vfp_touhd_round_to_zero, i64, f64, i32, ptr) DEF_HELPER_3(vfp_tould_round_to_zero, i64, f64, i32, ptr) -DEF_HELPER_3(vfp_toulh, i32, f16, i32, ptr) -DEF_HELPER_3(vfp_toslh, i32, f16, i32, ptr) +DEF_HELPER_3(vfp_touhh, i32, f16, i32, ptr) +DEF_HELPER_3(vfp_toshh, i32, f16, i32, ptr) DEF_HELPER_3(vfp_toshs, i32, f32, i32, ptr) DEF_HELPER_3(vfp_tosls, i32, f32, i32, ptr) DEF_HELPER_3(vfp_tosqs, i64, f32, i32, ptr) diff --git a/target/arm/translate-a64.c b/target/arm/translate-a64.c index 61735dc185..3c4c9b9fdc 100644 --- a/target/arm/translate-a64.c +++ b/target/arm/translate-a64.c @@ -7263,9 +7263,9 @@ static void handle_simd_shift_fpint_conv(DisasContext *s, bool is_scalar, switch (size) { case MO_16: if (is_u) { - fn = gen_helper_vfp_toulh; + fn = gen_helper_vfp_touhh; } else { - fn = gen_helper_vfp_toslh; + fn = gen_helper_vfp_toshh; } break; case MO_32: