From patchwork Thu May 10 09:42:04 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Alex_Benn=C3=A9e?= X-Patchwork-Id: 135400 Delivered-To: patch@linaro.org Received: by 10.46.151.6 with SMTP id r6csp785154lji; Thu, 10 May 2018 02:46:30 -0700 (PDT) X-Google-Smtp-Source: AB8JxZpUTfahr1KNIgmSMnBZpFbO4owUujccZuBNMOdMNCoqd91BidC9zuZ98tK1MTYvQGvlBs2p X-Received: by 2002:a0c:8adc:: with SMTP id 28-v6mr501957qvw.241.1525945590429; Thu, 10 May 2018 02:46:30 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1525945590; cv=none; d=google.com; s=arc-20160816; b=q4xuo1PnR6rtTq8oBS6OQfP/T+0Y3kpbEqkfnuZBCwozuK9rt+G6NuUlG2XtqTsoWU QANDjEQ0G8c6n0JJdhCNgY/GdRGK+4KSLLxEa+6kTnSoOZuh5G37ooYNLVyk6HRNymNQ NPSLT4BDRIHj7dTzQlSxdmLftquSpQLBZTwkN72sAIRRIipRrx3U/A+oI07kiAT/rbGt Bv8WW3+fEJwQlx4uSynfeSto79djlSNVz7jWAB+ZkhdJsFUixCE0fSy8XJLG753U9An5 QUe2xwLUegUu6PkomwtoI1dNAf76mSfAJR4ujme6NXzqX7kedQlGsVNtisIMLwU/PMM8 LpyA== 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=XibGir2o+sywcUzQXuh0/9bGi5rtuKwMYtOfJuZIbUs=; b=OVNEhoGpPei3XCu/uOXv6SH8BIEvd2W2TqJ0DD7y7MuMiFT8QvjuvGZkOZ/ZXR8OhT LiAUi4j3sq5XZ6LdXSpRcHzhXYvSYjSNkWG/C0MAx3YKXoOjD0gCZoGdmt1tU5JkwX6z RyyQWXD3rauOK8E1ZAgzckTmIeVXPXTv9izulch1A1ZdSjcz/aPgFw6zcNw2WM2QWhR9 7rrihrZwsDLqL6wi0bngYVp5LJKXZ8wJhGke+VQUbv68jwuFhGKct+fHlBJwbAZLhwOS wzM/lyeCgRSlaMvO8MzqzSR7zifXOD6PgWPu5ZawOXAICBNnopHdofprRVysfhgvT2S+ fU8w== ARC-Authentication-Results: i=1; mx.google.com; dkim=fail header.i=@linaro.org header.s=google header.b=Dut2xg86; 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 m9-v6si356172qki.378.2018.05.10.02.46.30 for (version=TLS1 cipher=AES128-SHA bits=128/128); Thu, 10 May 2018 02:46:30 -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=Dut2xg86; 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]:32774 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1fGi9d-0008Im-TI for patch@linaro.org; Thu, 10 May 2018 05:46:29 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:46172) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1fGi5W-0002FH-Ef for qemu-devel@nongnu.org; Thu, 10 May 2018 05:42:15 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1fGi5U-0004Tc-KI for qemu-devel@nongnu.org; Thu, 10 May 2018 05:42:14 -0400 Received: from mail-wm0-x243.google.com ([2a00:1450:400c:c09::243]:37296) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1fGi5U-0004T3-A5 for qemu-devel@nongnu.org; Thu, 10 May 2018 05:42:12 -0400 Received: by mail-wm0-x243.google.com with SMTP id l1-v6so3226123wmb.2 for ; Thu, 10 May 2018 02:42:12 -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=XibGir2o+sywcUzQXuh0/9bGi5rtuKwMYtOfJuZIbUs=; b=Dut2xg86s1vHOvuPuH1PkbOFeUBb4ba4OfcPPwzwYrOkZj7VjJ/CH299LMg8axUD4Y Pr4at2zEldJZtuy3ddvqII7Qr5VkB4mPWzgB5attqyBwHH5UYsVZ44K8BSOdh/2qnPOT 7pT6OyapzqOjLugaK42ULE0NBb+YGuBIUi5EQ= 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=XibGir2o+sywcUzQXuh0/9bGi5rtuKwMYtOfJuZIbUs=; b=FB+vnwgapBbWvHpf8CynUhK1GZIeRJ5qbLvdvPA0yt2yht1tZyoLom6HQuba3a/rZo 3HjBPJ+RIAh/cmSTXYqbS+KJ8t4QW6Upba4H7yuBlE4qDkfznHYu+iV1H59SkfojZUEt 2VIdRIcGuTXS+YFLFIq/+XDxmBbW4X/xr+C4khMhWBaZ9OdwySq4oMzqSqX4yEsYI/mq XlSj6eu9mgwjPUh7cvmRRXFTxwHurOfp6N5WSifBDxgLWxz19pfduDj7MP6aTScXwOtu J6dz7bTxnEnjwbTqfQzSi7PrD0U72Qj2Qk1D9iV+cRu/NGLfEfSmoNNtvljqRXdEk9+g 2mSA== X-Gm-Message-State: ALKqPweRQbKi61a1xYN4wrFxFCrkv7n04vZqzZiKafkl7W1wklumV+W2 PqCkdhZszymXxm9WBTQX/RB7Hg== X-Received: by 2002:a1c:5cd5:: with SMTP id q204-v6mr714160wmb.158.1525945331075; Thu, 10 May 2018 02:42:11 -0700 (PDT) Received: from zen.linaro.local ([81.128.185.34]) by smtp.gmail.com with ESMTPSA id 19-v6sm899577wrz.7.2018.05.10.02.42.07 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Thu, 10 May 2018 02:42:08 -0700 (PDT) Received: from zen.linaroharston (localhost [127.0.0.1]) by zen.linaro.local (Postfix) with ESMTP id 3002E3E03C0; Thu, 10 May 2018 10:42:07 +0100 (BST) From: =?utf-8?q?Alex_Benn=C3=A9e?= To: peter.maydell@linaro.org Date: Thu, 10 May 2018 10:42:04 +0100 Message-Id: <20180510094206.15354-4-alex.bennee@linaro.org> X-Mailer: git-send-email 2.17.0 In-Reply-To: <20180510094206.15354-1-alex.bennee@linaro.org> References: <20180510094206.15354-1-alex.bennee@linaro.org> MIME-Version: 1.0 X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 2a00:1450:400c:c09::243 Subject: [Qemu-devel] [PATCH v3 3/5] fpu/softfloat: support ARM Alternative half-precision 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: =?utf-8?q?Alex_Benn=C3=A9e?= , qemu-arm@nongnu.org, richard.henderson@linaro.org, qemu-devel@nongnu.org, Aurelien Jarno Errors-To: qemu-devel-bounces+patch=linaro.org@nongnu.org Sender: "Qemu-devel" For float16 ARM supports an alternative half-precision format which sacrifices the ability to represent NaN/Inf in return for a higher dynamic range. To support this I've added an additional FloatFmt (float16_params_ahp). The new FloatFmt flag (arm_althp) is then used to modify the behaviour of canonicalize and round_canonical with respect to representation and exception raising. Finally the float16_to_floatN and floatN_to_float16 conversion routines select the new alternative FloatFmt when !ieee. Signed-off-by: Alex Bennée Reviewed-by: Richard Henderson --- v3 - squash NaN to 0 if destination is AHP F16 --- fpu/softfloat.c | 108 +++++++++++++++++++++++++++++++++++++----------- 1 file changed, 85 insertions(+), 23 deletions(-) -- 2.17.0 diff --git a/fpu/softfloat.c b/fpu/softfloat.c index 042e5c901d..79ebc998d3 100644 --- a/fpu/softfloat.c +++ b/fpu/softfloat.c @@ -225,6 +225,8 @@ typedef struct { * frac_lsb: least significant bit of fraction * fram_lsbm1: the bit bellow the least significant bit (for rounding) * round_mask/roundeven_mask: masks used for rounding + * The following optional modifiers are available: + * arm_althp: handle ARM Alternative Half Precision */ typedef struct { int exp_size; @@ -236,6 +238,7 @@ typedef struct { uint64_t frac_lsbm1; uint64_t round_mask; uint64_t roundeven_mask; + bool arm_althp; } FloatFmt; /* Expand fields based on the size of exponent and fraction */ @@ -248,12 +251,17 @@ typedef struct { .frac_lsb = 1ull << (DECOMPOSED_BINARY_POINT - F), \ .frac_lsbm1 = 1ull << ((DECOMPOSED_BINARY_POINT - F) - 1), \ .round_mask = (1ull << (DECOMPOSED_BINARY_POINT - F)) - 1, \ - .roundeven_mask = (2ull << (DECOMPOSED_BINARY_POINT - F)) - 1 + .roundeven_mask = (2ull << (DECOMPOSED_BINARY_POINT - F)) - 1, static const FloatFmt float16_params = { FLOAT_PARAMS(5, 10) }; +static const FloatFmt float16_params_ahp = { + FLOAT_PARAMS(5, 10) + .arm_althp = true +}; + static const FloatFmt float32_params = { FLOAT_PARAMS(8, 23) }; @@ -317,7 +325,7 @@ static inline float64 float64_pack_raw(FloatParts p) static FloatParts canonicalize(FloatParts part, const FloatFmt *parm, float_status *status) { - if (part.exp == parm->exp_max) { + if (part.exp == parm->exp_max && !parm->arm_althp) { if (part.frac == 0) { part.cls = float_class_inf; } else { @@ -403,8 +411,9 @@ static FloatParts round_canonical(FloatParts p, float_status *s, exp += parm->exp_bias; if (likely(exp > 0)) { + bool maybe_inexact = false; if (frac & round_mask) { - flags |= float_flag_inexact; + maybe_inexact = true; frac += inc; if (frac & DECOMPOSED_OVERFLOW_BIT) { frac >>= 1; @@ -413,14 +422,26 @@ static FloatParts round_canonical(FloatParts p, float_status *s, } frac >>= frac_shift; - if (unlikely(exp >= exp_max)) { - flags |= float_flag_overflow | float_flag_inexact; - if (overflow_norm) { - exp = exp_max - 1; - frac = -1; - } else { - p.cls = float_class_inf; - goto do_inf; + if (parm->arm_althp) { + if (unlikely(exp >= exp_max + 1)) { + flags |= float_flag_invalid; + frac = -1; + exp = exp_max; + } else if (maybe_inexact) { + flags |= float_flag_inexact; + } + } else { + if (unlikely(exp >= exp_max)) { + flags |= float_flag_overflow | float_flag_inexact; + if (overflow_norm) { + exp = exp_max - 1; + frac = -1; + } else { + p.cls = float_class_inf; + goto do_inf; + } + } else if (maybe_inexact) { + flags |= float_flag_inexact; } } } else if (s->flush_to_zero) { @@ -465,7 +486,13 @@ static FloatParts round_canonical(FloatParts p, float_status *s, case float_class_inf: do_inf: exp = exp_max; - frac = 0; + if (parm->arm_althp) { + flags |= float_flag_invalid; + /* Alt HP returns result = sign:Ones(M-1) */ + frac = -1; + } else { + frac = 0; + } break; case float_class_qnan: @@ -483,12 +510,21 @@ static FloatParts round_canonical(FloatParts p, float_status *s, return p; } +/* Explicit FloatFmt version */ +static FloatParts float16a_unpack_canonical(const FloatFmt *params, + float16 f, float_status *s) +{ + return canonicalize(float16_unpack_raw(f), params, s); +} + static FloatParts float16_unpack_canonical(float16 f, float_status *s) { - return canonicalize(float16_unpack_raw(f), &float16_params, s); + return float16a_unpack_canonical(&float16_params, f, s); } -static float16 float16_round_pack_canonical(FloatParts p, float_status *s) + +static float16 float16a_round_pack_canonical(const FloatFmt *params, + FloatParts p, float_status *s) { switch (p.cls) { case float_class_dnan: @@ -496,11 +532,16 @@ static float16 float16_round_pack_canonical(FloatParts p, float_status *s) case float_class_msnan: return float16_maybe_silence_nan(float16_pack_raw(p), s); default: - p = round_canonical(p, s, &float16_params); + p = round_canonical(p, s, params); return float16_pack_raw(p); } } +static float16 float16_round_pack_canonical(FloatParts p, float_status *s) +{ + return float16a_round_pack_canonical(&float16_params, p, s); +} + static FloatParts float32_unpack_canonical(float32 f, float_status *s) { return canonicalize(float32_unpack_raw(f), &float32_params, s); @@ -1206,6 +1247,17 @@ static FloatParts float_to_float(FloatParts a, s->float_exception_flags |= float_flag_invalid; } + if (dstf->arm_althp) { + /* There is no NaN in the destination format: raise Invalid + * and return a zero with the sign of the input NaN. + */ + s->float_exception_flags |= float_flag_invalid; + a.cls = float_class_zero; + a.frac = 0; + a.exp = 0; + return a; + } + if (s->default_nan_mode) { a.cls = float_class_dnan; return a; @@ -1226,25 +1278,34 @@ static FloatParts float_to_float(FloatParts a, return a; } +/* + * Currently non-ieee implies ARM Alternative Half Precision handling + * for float16 values. If more are needed we'll need to expand the API + * into softfloat. + */ + float32 float16_to_float32(float16 a, bool ieee, float_status *s) { - FloatParts p = float16_unpack_canonical(a, s); - FloatParts pr = float_to_float(p, &float16_params, &float32_params, s); + const FloatFmt *fmt16 = ieee ? &float16_params : &float16_params_ahp; + FloatParts p = float16a_unpack_canonical(fmt16, a, s); + FloatParts pr = float_to_float(p, fmt16, &float32_params, s); return float32_round_pack_canonical(pr, s); } float64 float16_to_float64(float16 a, bool ieee, float_status *s) { - FloatParts p = float16_unpack_canonical(a, s); - FloatParts pr = float_to_float(p, &float16_params, &float64_params, s); + const FloatFmt *fmt16 = ieee ? &float16_params : &float16_params_ahp; + FloatParts p = float16a_unpack_canonical(fmt16, a, s); + FloatParts pr = float_to_float(p, fmt16, &float64_params, s); return float64_round_pack_canonical(pr, s); } float16 float32_to_float16(float32 a, bool ieee, float_status *s) { + const FloatFmt *fmt16 = ieee ? &float16_params : &float16_params_ahp; FloatParts p = float32_unpack_canonical(a, s); - FloatParts pr = float_to_float(p, &float32_params, &float16_params, s); - return float16_round_pack_canonical(pr, s); + FloatParts pr = float_to_float(p, &float32_params, fmt16, s); + return float16a_round_pack_canonical(fmt16, pr, s); } float64 float32_to_float64(float32 a, float_status *s) @@ -1256,9 +1317,10 @@ float64 float32_to_float64(float32 a, float_status *s) float16 float64_to_float16(float64 a, bool ieee, float_status *s) { + const FloatFmt *fmt16 = ieee ? &float16_params : &float16_params_ahp; FloatParts p = float64_unpack_canonical(a, s); - FloatParts pr = float_to_float(p, &float64_params, &float16_params, s); - return float16_round_pack_canonical(pr, s); + FloatParts pr = float_to_float(p, &float64_params, fmt16, s); + return float16a_round_pack_canonical(fmt16, pr, s); } float32 float64_to_float32(float64 a, float_status *s)