From patchwork Wed May 2 22:15:41 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Richard Henderson X-Patchwork-Id: 134869 Delivered-To: patch@linaro.org Received: by 10.46.151.6 with SMTP id r6csp1206242lji; Wed, 2 May 2018 15:23:58 -0700 (PDT) X-Google-Smtp-Source: AB8JxZrTr/YKRniEnYXwhyceJOZXlyFdjEvKBuQzxjw1NotuaQfuPj9Ag/kVqR4YBTH+vrLhalmy X-Received: by 2002:a0c:e002:: with SMTP id j2-v6mr1629047qvk.62.1525299838721; Wed, 02 May 2018 15:23:58 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1525299838; cv=none; d=google.com; s=arc-20160816; b=oXevlrDbercjEn4I3mMtAbb3moCAxlX2P2GW8YhSKTZTLwUo4B6UFwNRVG7fRu5YoI sZ2wnFPp+XPdkDktVwoPycq2DGsm67yrzIVC+ykKw94qXcPs60VZvADPXbnKpTsPFApL XiaOqRufjqcTVb+2yE0tA5yhRysh085RsGdOMLEGY/81xZ9jl5cIg8+LoFALWPqg8+q7 +Wpq6oIOR8Q8Y1j3JB1rXpZ1cZoazMpZt2OxpdlPj3DiqKbmS7rrxbz8F7DWcQioWYAW jyvY1LfaZD6FdunB1EvLF9pbqjbjstYuuWrbLlUqhvNJP+UzDRalV22ztmC754MzpYxj 0M6Q== 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=6YuI3pbqAJMHNcfp1+D7vk2rPkV7s0a8qv8kSCgMDvI=; b=gBFc/6FtUIp7RTXxfPHZumko9CB/S3+q1DkY5IfSiPqQx0kXE9TJS3V3YlFogetTdp hgP8dvFjc/fMZEKtjCZakGr+LNhHdys6tm2xmM0JSq9oojBCcmo+4s6x3+fCIgOa3TIA yLGdli4S3Jt8BkT/7nkVao/OhVtDZUQXu26RZBgoHEwn1TghNGqfSL8iOJSle5gFdHdw dWI6nKOlfpJPubdB8xrjxfTm4CCCMRjtA6DiWgWdd0AYy4AyQtUIvUVuCQ7cSWrvhIxu DeuNXdNvncsWNyKeUNtG/9tYoGONJRbDNG6jmxsyibRwkQ2ExNr5baAAszKjvjg7YrGf 9tpQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=fail header.i=@linaro.org header.s=google header.b=AWA4CST/; 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=linaro.org Return-Path: Received: from lists.gnu.org (lists.gnu.org. [2001:4830:134:3::11]) by mx.google.com with ESMTPS id c40-v6si11508578qte.106.2018.05.02.15.23.58 for (version=TLS1 cipher=AES128-SHA bits=128/128); Wed, 02 May 2018 15:23:58 -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=@linaro.org header.s=google header.b=AWA4CST/; 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=linaro.org Received: from localhost ([::1]:52835 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1fE0AI-000780-7P for patch@linaro.org; Wed, 02 May 2018 18:23:58 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:58653) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1fE02b-0000bc-GE for qemu-devel@nongnu.org; Wed, 02 May 2018 18:16:02 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1fE02a-0007rZ-5N for qemu-devel@nongnu.org; Wed, 02 May 2018 18:16:01 -0400 Received: from mail-pf0-x242.google.com ([2607:f8b0:400e:c00::242]:44362) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1fE02Z-0007qu-Sc for qemu-devel@nongnu.org; Wed, 02 May 2018 18:16:00 -0400 Received: by mail-pf0-x242.google.com with SMTP id q22so12956785pff.11 for ; Wed, 02 May 2018 15:15:59 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=6YuI3pbqAJMHNcfp1+D7vk2rPkV7s0a8qv8kSCgMDvI=; b=AWA4CST/V4XCKqnOsjV9w1VyzjERzkP1xDePT8NlgfNdxDk9ChufuoSwMynN0P4SLu le0sI22Sb7t1R0nyITHGwMxO0wKgSlSecZU/we1mT034xGAt7nAh/LVQvEnAmrAv4R9A edZT8vltTh/9JKqX/+gt6udT65adO8eqN3dTo= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=6YuI3pbqAJMHNcfp1+D7vk2rPkV7s0a8qv8kSCgMDvI=; b=LMp7Tad9ijqFDkcGcWcPQcZzdwXU5x+6OUMaHIi09GUYB6ec2t9r6H16yc9heDm3ox g3/CDAo6RN3Dqr6od5G4xgnRaK02igsb+gHf7hxib/Hqeh/CruFLnW6r/mjc9mGUMkNU NpaVzkC1SocCGpn2N8M8+Ph+eOz4bHOQT4y2WtXdz1hIeqdSfVtq29pPMXqz7hzBKesF /hMJZx+VdsFXeg2x6WDljtbJnLGoTxQ331u4IJZQwq/Ih7Hmn4k7h3Yi8IDnzOiaKGXW cQ6nwVdbxxFcqFLLZSFa6KYLoixJ+F5sJsir7/qqqQffGCxPHbf1N25BMD6dW2NmCIWl Jzgg== X-Gm-Message-State: ALQs6tDl/BIcDEm0m+M0Wwu8EbyTNLfGDxyfmyG5zgxpGnChs17W5A+b kvPOtEHUwZ6XScrGqCCKil3YxOrk04Y= X-Received: by 2002:a63:5fd1:: with SMTP id t200-v6mr17819822pgb.246.1525299358617; Wed, 02 May 2018 15:15:58 -0700 (PDT) Received: from cloudburst.twiddle.net (97-113-2-170.tukw.qwest.net. [97.113.2.170]) by smtp.gmail.com with ESMTPSA id 65sm26170145pft.74.2018.05.02.15.15.57 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Wed, 02 May 2018 15:15:57 -0700 (PDT) From: Richard Henderson To: qemu-devel@nongnu.org Date: Wed, 2 May 2018 15:15:41 -0700 Message-Id: <20180502221552.3873-4-richard.henderson@linaro.org> X-Mailer: git-send-email 2.14.3 In-Reply-To: <20180502221552.3873-1-richard.henderson@linaro.org> References: <20180502221552.3873-1-richard.henderson@linaro.org> MIME-Version: 1.0 X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 2607:f8b0:400e:c00::242 Subject: [Qemu-devel] [PATCH v2 03/14] 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@linaro.org, qemu-arm@nongnu.org, qemu-stable@nongnu.org Errors-To: qemu-devel-bounces+patch=linaro.org@nongnu.org Sender: "Qemu-devel" 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 --- target/arm/helper.h | 4 ++-- target/arm/helper.c | 53 ++++++++++++++++++++++++++++++++++++++++++++-- target/arm/translate-a64.c | 4 ++-- 3 files changed, 55 insertions(+), 6 deletions(-) -- 2.14.3 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/helper.c b/target/arm/helper.c index 52a88e0297..ba04cddda0 100644 --- a/target/arm/helper.c +++ b/target/arm/helper.c @@ -11420,11 +11420,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/translate-a64.c b/target/arm/translate-a64.c index a64673575a..7021a31b89 100644 --- a/target/arm/translate-a64.c +++ b/target/arm/translate-a64.c @@ -7269,9 +7269,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: