From patchwork Wed Apr 25 01:22:54 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Richard Henderson X-Patchwork-Id: 134206 Delivered-To: patch@linaro.org Received: by 10.46.151.6 with SMTP id r6csp251122lji; Tue, 24 Apr 2018 18:25:28 -0700 (PDT) X-Google-Smtp-Source: AB8JxZpxUPT03fZi+1LFkHsMAcF0h+6X/9tbvxxFN6JYmnR2LUIC7v4tNJov292lMZVLm4yHP0dx X-Received: by 10.55.41.66 with SMTP id p63mr5046019qkh.403.1524619528817; Tue, 24 Apr 2018 18:25:28 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1524619528; cv=none; d=google.com; s=arc-20160816; b=ktD/WwALhWmECZMDVcI7VziGoyDZ1qkOF2g227KLUNZALoMKRJByHV1PrDD+iqnyJ/ ioua45BwKWWE1NEo7NcYCsyF2Rbe3v7qxmcliKPbDwwuU5XQB5vUbNXncmk4FMTjtPoB Sr987dMc1jkFj99liXA0v1e1rmpgNFuwYItQ7L+H5K4ocLtsFsFDI1AOyBSKCuLH0gna xaKdxlgCLZ+Gv8HsIHq63FlKSRyPUcFBGU7Bv8o5R63ei12n571QOG3jZBzEPWg5HyJc V87NIQIEAylvl+ubYQ3LtExF1cbVZqy3wnqzCYew8mpED8vC9XM/tYgZrqMOpf0OAjcO gOtw== 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:references:in-reply-to :message-id:date:to:from:dkim-signature:arc-authentication-results; bh=HW2lFqjkdeznoEnZz0wGEReab483/vCLPZX1Ws3Xa70=; b=NBSakRhI/L2JHrtYRtjV31WDxMVQJbrxGbCsToplXDPZTKtiivKEMn3M0hSMygU/Pc MS0CnObU8XghGF7i6H8tpa+pRlyaFAR4NLxeWkUw2EF9aE7LB7/oPNl7d+yj12b8olG5 FSYRz1ja9t6/dahuRlkmGu1s0Bc5u9aC+IraT1cR3IxBDS4aH4HZwPWXUlAh/iG5KhyJ 5fddxUNY1mUI2J07xj1l3RYF/Z2PmknS5SHC6X1/lUU6RD8SahFuw05i4d7WMiq44KIz sCfswivjeqMBAhw0bfFqiDRi/LcFJv1NPTAoy36XY/OusfIvp/umhBFtHxeSl7g4RUi4 CdfA== ARC-Authentication-Results: i=1; mx.google.com; dkim=fail header.i=@linaro.org header.s=google header.b=Sp7hZlMD; 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 s2-v6si1591782qtj.183.2018.04.24.18.25.28 for (version=TLS1 cipher=AES128-SHA bits=128/128); Tue, 24 Apr 2018 18:25:28 -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=Sp7hZlMD; 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]:33306 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1fB9BY-0004w4-7B for patch@linaro.org; Tue, 24 Apr 2018 21:25:28 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:55403) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1fB99P-0003yu-4c for qemu-devel@nongnu.org; Tue, 24 Apr 2018 21:23:16 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1fB99N-0007HK-SG for qemu-devel@nongnu.org; Tue, 24 Apr 2018 21:23:15 -0400 Received: from mail-pg0-x244.google.com ([2607:f8b0:400e:c05::244]:40445) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1fB99N-0007FU-Lm for qemu-devel@nongnu.org; Tue, 24 Apr 2018 21:23:13 -0400 Received: by mail-pg0-x244.google.com with SMTP id e9so12132394pgr.7 for ; Tue, 24 Apr 2018 18:23:13 -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; bh=HW2lFqjkdeznoEnZz0wGEReab483/vCLPZX1Ws3Xa70=; b=Sp7hZlMDs/U5mktye+I5j7q73ma39bQNztjq3ljHg1VHhm3nkxLMbsLKe5+e7CGRNY fOwV6TFyYaoOXi0lNV4Uag2lpMFn9u9OWuo4teP359AeAX8x1reR+jpefS6Shj7vX936 vRNbyUi6eBx1oOivzb+KPCAK/NBc8KDz/KEZI= 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; bh=HW2lFqjkdeznoEnZz0wGEReab483/vCLPZX1Ws3Xa70=; b=sTcG75z9CLEML8V/CEkOMSA+hTQ3pLaRM3VI1INirh7EoA8onQFCtZoK5ihaJ46ep/ X4yDNZ8tae7lKbeLIQ45N/OnN4wQLJIdDfvWd/claY/RysIO5mnZOvnN6aattY/z5iXr RGry5irIj5ArxJ0SLE3zp50ddAoGO9lcmAFmtTKcwuGyLFLPVhg4qxadclcCuUn+FBag g9POAb8O81M4R7m82zPNVhsJa6NMCPWyFAh4bA7h0RG+uM+4Rjf1XWw+rSPySOxwos8Y Cjk9Mlz+Z6M6I+x64zQ7K+axOC3S8mhGNlQyr1TXD3W/dMCpPlrCYmF+BDhIdqqPPJFy aymg== X-Gm-Message-State: ALQs6tAD1w+Z1RHiJR4lH85WO1otteBK2e2Vt5Ow3/TEDY0mhCl51E4C cjIv3Dz8i3vnSeI+aHs2lseFiPAcf98= X-Received: by 10.99.143.22 with SMTP id n22mr21290654pgd.394.1524619392345; Tue, 24 Apr 2018 18:23:12 -0700 (PDT) Received: from cloudburst.twiddle.net.com (cpe-76-173-164-138.hawaii.res.rr.com. [76.173.164.138]) by smtp.gmail.com with ESMTPSA id z127sm27767966pfb.72.2018.04.24.18.23.10 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Tue, 24 Apr 2018 18:23:11 -0700 (PDT) From: Richard Henderson To: qemu-devel@nongnu.org Date: Tue, 24 Apr 2018 15:22:54 -1000 Message-Id: <20180425012300.14698-4-richard.henderson@linaro.org> X-Mailer: git-send-email 2.14.3 In-Reply-To: <20180425012300.14698-1-richard.henderson@linaro.org> References: <20180425012300.14698-1-richard.henderson@linaro.org> X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 2607:f8b0:400e:c05::244 Subject: [Qemu-devel] [PATCH 3/9] 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, alex.bennee@linaro.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. 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 Reviewed-by: Alex Bennée diff --git a/target/arm/helper.h b/target/arm/helper.h index b3ae394b4f..eafd5d746b 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 ea09510599..743f34bd0a 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/translate-a64.c b/target/arm/translate-a64.c index e2d11998bd..b27892d971 100644 --- a/target/arm/translate-a64.c +++ b/target/arm/translate-a64.c @@ -7181,9 +7181,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: