From patchwork Tue Jan 9 12:22:52 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: 123918 Delivered-To: patch@linaro.org Received: by 10.140.22.227 with SMTP id 90csp3967826qgn; Tue, 9 Jan 2018 04:35:24 -0800 (PST) X-Google-Smtp-Source: ACJfBotxKTcgI6Ppk2uFWGQbWL+CUWDZdvdVs9FYWiMIGRlSQxnMa5dVfE78kq9AhSP2+oUONobo X-Received: by 10.129.173.71 with SMTP id l7mr3529198ywk.229.1515501324589; Tue, 09 Jan 2018 04:35:24 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1515501324; cv=none; d=google.com; s=arc-20160816; b=BJLTob0iirt23RgBQl3SsZGB7xQdJtEoHsWmP1p9B4htaTAMNgZFOqiGAMuJIiK46/ OYNJ/12Dx/f+ZAwQcBwliBNBFELa5M0l/RPruHmgWKx3gq2zNxNcpLRV+4r0dIZmHAYH C65LMGRAHwF0Qn1jx2G8VJOU6Sx48nAO0ueQ+4YFsUKQqGxrZpn2Ddv6bVPbrMmuU4cR dD/9X6wFbLqBkbW5ZfyXtW1jYtiaHNIE4zWQD44WlxIToGpJpoFC91KWsJ1cu6TnDcJ/ x5XMw8+Y65wRg6CUfiUesK//WYIznd4yGr9UTq+z+wUs/v3asaYUze3jo9G+xc673HcR HRbA== 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=9EVuYq8Svz+r+ohBrFBZAJHxebiz/Vagrbylnmdb36U=; b=cG9KDcxQ6+0RBUNJzdEyCEn3fHxXu0rGfKLxooBKrEX+YBmGn1lFZV8gJsAFIED74K 945mUXstzOmkQTWMACD0/Gpdqs5QNQ2mDLBJ5/ZR1p9+uHDBNGYWGfE9qZY3YM8vEsQO F24JtM+QtO9fasoTNzaKD+Z4joMJwFXTPKWeK55oG70sNoqlYt4JAIahW+ymGRspQElN 2REJ1D/LilHNoSdv0wTXdUwTeXkU0xyW+O0AgGXL7nWJEt7dNJ/pVjMqUVUuSKvV/qa9 gosCd+EjahWU3LRO/g9q0/U/dKi6l1nkz6OavGEiq1N7cDgqmSXZVmkepx/fAHqB1Uoi h6Sw== ARC-Authentication-Results: i=1; mx.google.com; dkim=fail header.i=@linaro.org header.s=google header.b=a7aH0Ox0; 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 141si956977ybe.589.2018.01.09.04.35.24 for (version=TLS1 cipher=AES128-SHA bits=128/128); Tue, 09 Jan 2018 04:35:24 -0800 (PST) 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=a7aH0Ox0; 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]:49554 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1eYt7k-0007we-0f for patch@linaro.org; Tue, 09 Jan 2018 07:35:24 -0500 Received: from eggs.gnu.org ([2001:4830:134:3::10]:53777) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1eYt16-0002bY-Rs for qemu-devel@nongnu.org; Tue, 09 Jan 2018 07:28:34 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1eYt15-0002Dd-BM for qemu-devel@nongnu.org; Tue, 09 Jan 2018 07:28:32 -0500 Received: from mail-wm0-x242.google.com ([2a00:1450:400c:c09::242]:39343) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1eYt14-0002Ct-UQ for qemu-devel@nongnu.org; Tue, 09 Jan 2018 07:28:31 -0500 Received: by mail-wm0-x242.google.com with SMTP id i11so20264249wmf.4 for ; Tue, 09 Jan 2018 04:28:30 -0800 (PST) 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=9EVuYq8Svz+r+ohBrFBZAJHxebiz/Vagrbylnmdb36U=; b=a7aH0Ox0oTTXWCMmc8nOnjrnHXa8kRKcgfpfBFiD1fkUCCbhroZFva90+tBS53dph9 z489C1Nob/zCMWTvCLbFObbABgK+u/NJv7C0ZnI/s87Ejhh8FS/ZQN5/FZXPzSSQPL33 /ib0g0wgyvDi2nTfVhxPRpU8VYCqloLBfh5sc= 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=9EVuYq8Svz+r+ohBrFBZAJHxebiz/Vagrbylnmdb36U=; b=AGGxOKMuv9Wwo4QoV9sL0m4YrZY2gZ2JggwJoend937rrX8qIvhKTWQNSK59ITBqUu nLJAdR+R2Es6fY51qhPZ7evSDlngnExwhpVvEvsU0UlO6cnExqfvx5cCBrEzFlZ/daYB 0r9hIMvE65LuePEsbbJfBDrZNUp+TsKOgHmj0hjQmh4a+Qx4qeV77958U6M40jTMs9EI J9mLScuy1aAlrgNz+QtKdAT8goGPnryFFXrmQybsxNFmisfoDyHdyGf0wB+qtVOT3c9Q j/0IIZgeSYv5h6HigTG7iJ/H1SCyFGZtNL7sWWqo0KQQdpxp10HIQbUIqz2uDLbyUddh 5yQw== X-Gm-Message-State: AKGB3mLp4GZUiTbutykByIPvc+vmbDqhNN7Qwfs3Kdp7158vyyUrMSYY /Wvst8pTNALvjQdXbaEbGZiw6w== X-Received: by 10.28.212.72 with SMTP id l69mr11050418wmg.66.1515500909840; Tue, 09 Jan 2018 04:28:29 -0800 (PST) Received: from zen.linaro.local ([81.128.185.34]) by smtp.gmail.com with ESMTPSA id v195sm13000897wmf.25.2018.01.09.04.28.25 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 09 Jan 2018 04:28:28 -0800 (PST) Received: from zen.linaroharston (localhost [127.0.0.1]) by zen.linaro.local (Postfix) with ESMTP id 9DCC73E2B56; Tue, 9 Jan 2018 12:22:53 +0000 (GMT) From: =?utf-8?q?Alex_Benn=C3=A9e?= To: richard.henderson@linaro.org, peter.maydell@linaro.org, laurent@vivier.eu, bharata@linux.vnet.ibm.com, andrew@andrewdutcher.com Date: Tue, 9 Jan 2018 12:22:52 +0000 Message-Id: <20180109122252.17670-21-alex.bennee@linaro.org> X-Mailer: git-send-email 2.15.1 In-Reply-To: <20180109122252.17670-1-alex.bennee@linaro.org> References: <20180109122252.17670-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::242 Subject: [Qemu-devel] [PATCH v2 20/20] fpu/softfloat: re-factor compare 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-devel@nongnu.org, Aurelien Jarno Errors-To: qemu-devel-bounces+patch=linaro.org@nongnu.org Sender: "Qemu-devel" The compare function was already expanded from a macro. I keep the macro expansion but move most of the logic into a compare_decomposed. Signed-off-by: Alex Bennée --- v2 - minor re-factor for better inf handling --- fpu/softfloat.c | 134 +++++++++++++++++++++++++++++------------------- include/fpu/softfloat.h | 2 + 2 files changed, 82 insertions(+), 54 deletions(-) -- 2.15.1 Reviewed-by: Richard Henderson diff --git a/fpu/softfloat.c b/fpu/softfloat.c index 1dd9bd5972..8eda35acd5 100644 --- a/fpu/softfloat.c +++ b/fpu/softfloat.c @@ -1791,6 +1791,86 @@ MINMAX(64, maxnummag, false, true, true) #undef MINMAX +/* Floating point compare */ +static int compare_decomposed(decomposed_parts a, decomposed_parts b, + bool is_quiet, float_status *s) +{ + if (a.cls >= float_class_qnan + || + b.cls >= float_class_qnan) { + if (!is_quiet || + a.cls == float_class_snan || + b.cls == float_class_snan) { + s->float_exception_flags |= float_flag_invalid; + } + return float_relation_unordered; + } + + if (a.cls == float_class_zero) { + if (b.cls == float_class_zero) { + return float_relation_equal; + } + return b.sign ? float_relation_greater : float_relation_less; + } else if (b.cls == float_class_zero) { + return a.sign ? float_relation_less : float_relation_greater; + } + + /* The only really important thing about infinity is its sign. If + * both are infinities the sign marks the smallest of the two. + */ + if (a.cls == float_class_inf) { + if ((b.cls == float_class_inf) && (a.sign == b.sign)) { + return float_relation_equal; + } + return a.sign ? float_relation_less : float_relation_greater; + } else if (b.cls == float_class_inf) { + return b.sign ? float_relation_greater : float_relation_less; + } + + if (a.sign != b.sign) { + return a.sign ? float_relation_less : float_relation_greater; + } + + if (a.exp == b.exp) { + if (a.frac == b.frac) { + return float_relation_equal; + } + if (a.sign) { + return a.frac > b.frac ? + float_relation_less : float_relation_greater; + } else { + return a.frac > b.frac ? + float_relation_greater : float_relation_less; + } + } else { + if (a.sign) { + return a.exp > b.exp ? float_relation_less : float_relation_greater; + } else { + return a.exp > b.exp ? float_relation_greater : float_relation_less; + } + } +} + +#define COMPARE(sz) \ +int float ## sz ## _compare(float ## sz a, float ## sz b, float_status *s) \ +{ \ + decomposed_parts pa = float ## sz ## _unpack_canonical(a, s); \ + decomposed_parts pb = float ## sz ## _unpack_canonical(b, s); \ + return compare_decomposed(pa, pb, false, s); \ +} \ +int float ## sz ## _compare_quiet(float ## sz a, float ## sz b, float_status *s) \ +{ \ + decomposed_parts pa = float ## sz ## _unpack_canonical(a, s); \ + decomposed_parts pb = float ## sz ## _unpack_canonical(b, s); \ + return compare_decomposed(pa, pb, true, s); \ +} + +COMPARE(16) +COMPARE(32) +COMPARE(64) + +#undef COMPARE + /* Multiply A by 2 raised to the power N. */ static decomposed_parts scalbn_decomposed(decomposed_parts a, int n, float_status *s) @@ -6892,60 +6972,6 @@ int float128_unordered_quiet(float128 a, float128 b, float_status *status) return 0; } -#define COMPARE(s, nan_exp) \ -static inline int float ## s ## _compare_internal(float ## s a, float ## s b,\ - int is_quiet, float_status *status) \ -{ \ - flag aSign, bSign; \ - uint ## s ## _t av, bv; \ - a = float ## s ## _squash_input_denormal(a, status); \ - b = float ## s ## _squash_input_denormal(b, status); \ - \ - if (( ( extractFloat ## s ## Exp( a ) == nan_exp ) && \ - extractFloat ## s ## Frac( a ) ) || \ - ( ( extractFloat ## s ## Exp( b ) == nan_exp ) && \ - extractFloat ## s ## Frac( b ) )) { \ - if (!is_quiet || \ - float ## s ## _is_signaling_nan(a, status) || \ - float ## s ## _is_signaling_nan(b, status)) { \ - float_raise(float_flag_invalid, status); \ - } \ - return float_relation_unordered; \ - } \ - aSign = extractFloat ## s ## Sign( a ); \ - bSign = extractFloat ## s ## Sign( b ); \ - av = float ## s ## _val(a); \ - bv = float ## s ## _val(b); \ - if ( aSign != bSign ) { \ - if ( (uint ## s ## _t) ( ( av | bv )<<1 ) == 0 ) { \ - /* zero case */ \ - return float_relation_equal; \ - } else { \ - return 1 - (2 * aSign); \ - } \ - } else { \ - if (av == bv) { \ - return float_relation_equal; \ - } else { \ - return 1 - 2 * (aSign ^ ( av < bv )); \ - } \ - } \ -} \ - \ -int float ## s ## _compare(float ## s a, float ## s b, float_status *status) \ -{ \ - return float ## s ## _compare_internal(a, b, 0, status); \ -} \ - \ -int float ## s ## _compare_quiet(float ## s a, float ## s b, \ - float_status *status) \ -{ \ - return float ## s ## _compare_internal(a, b, 1, status); \ -} - -COMPARE(32, 0xff) -COMPARE(64, 0x7ff) - static inline int floatx80_compare_internal(floatx80 a, floatx80 b, int is_quiet, float_status *status) { diff --git a/include/fpu/softfloat.h b/include/fpu/softfloat.h index c948727bbb..e5aa8d65f9 100644 --- a/include/fpu/softfloat.h +++ b/include/fpu/softfloat.h @@ -333,6 +333,8 @@ float16 float16_minnum(float16, float16, float_status *status); float16 float16_maxnum(float16, float16, float_status *status); float16 float16_minnummag(float16, float16, float_status *status); float16 float16_maxnummag(float16, float16, float_status *status); +int float16_compare(float16, float16, float_status *status); +int float16_compare_quiet(float16, float16, float_status *status); int float16_is_quiet_nan(float16, float_status *status); int float16_is_signaling_nan(float16, float_status *status);