From patchwork Fri Nov 11 07:40:52 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Richard Henderson X-Patchwork-Id: 623705 Delivered-To: patch@linaro.org Received: by 2002:a17:522:c983:b0:460:3032:e3c4 with SMTP id kr3csp628823pvb; Fri, 11 Nov 2022 00:14:47 -0800 (PST) X-Google-Smtp-Source: AA0mqf4UssJloblchg9eIKmtykH8DJHYCHnfBmgzeYzNE9CZkqvGTus5N71GD2iNHtYAsxmdjRqx X-Received: by 2002:ac8:544b:0:b0:3a5:6961:e1b5 with SMTP id d11-20020ac8544b000000b003a56961e1b5mr373073qtq.598.1668154487651; Fri, 11 Nov 2022 00:14:47 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1668154487; cv=none; d=google.com; s=arc-20160816; b=AavqWyzNhoEy7ZYX7exjDxLEwljSujfs5OvgW/jB9Lra9r8Y0eLrB6svvxm9xfqPin AaNdFTkn5jlM2gEnx/JW0hqiVhjPKo5MDlcnmhSpqFWNAi4dLHotjuOMqpJOwNg7BaIl a8sSB4+/rwtlLRxOdyr8V3+bzsuqmN0IDFZlQkWa1kyahc1X2YQySfKXPFPXb6nYrL3b GGfT8VNNnHAaF1grYaorOHGwDN4LR1xx75oC4yoXdQ7F1h+4H84H7BHMWm89rixaLW3J K11ZDqNcwF9FsmTrXnUN6tcVPD2J57leHCIabUbBQOR4Ellfklk9IbmpExgT/r3dBV+5 2UNQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:content-transfer-encoding :mime-version:references:in-reply-to:message-id:date:subject:to:from :dkim-signature; bh=WlfgTKwjPzR9xe9xdxRaR9DbAja99BeWVoRkY0FKPGQ=; b=rcNo5UNzsVXqTTINgKoMzuDOGelfvdbbQpUS+gGbQNLkJo6sl7rb5K6Ey9YmENmumU 10lZ1XHvi8tR6h9UIshklEdrmOLTXl5dheBFdhN1SKdQcG51//Z9ceh0JjbxvhyxncMK ujzObPY/oewYv5iog8xE2LFsy2MXzpq5PAc4LZWprvCJFLtjL9CRVk0shOZVSR/fGUqA ijEmM6EYmYrFstvZrjtPohi7/ocKNPY46NgSaedG5XAlXaQ9vL3lBJizXzu8uEz0jNmM tO0OItpvBGuYiGvBUc9SK51ntnISeGEBh2AHc9rfDkQc/3MsL1Slv58XY2vVmg+jj0xM ve9A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=g8eNcfJA; spf=pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom="qemu-devel-bounces+patch=linaro.org@nongnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from lists.gnu.org (lists.gnu.org. [209.51.188.17]) by mx.google.com with ESMTPS id y20-20020a05620a25d400b006cf2a95a11bsi805557qko.125.2022.11.11.00.14.47 for (version=TLS1_2 cipher=ECDHE-ECDSA-CHACHA20-POLY1305 bits=256/256); Fri, 11 Nov 2022 00:14:47 -0800 (PST) Received-SPF: pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; Authentication-Results: mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=g8eNcfJA; spf=pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom="qemu-devel-bounces+patch=linaro.org@nongnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1otOgv-0007KR-1T; Fri, 11 Nov 2022 02:43:09 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1otOgL-00073D-GK for qemu-devel@nongnu.org; Fri, 11 Nov 2022 02:42:33 -0500 Received: from mail-pj1-x1036.google.com ([2607:f8b0:4864:20::1036]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1otOgJ-0000Jk-Ma for qemu-devel@nongnu.org; Fri, 11 Nov 2022 02:42:33 -0500 Received: by mail-pj1-x1036.google.com with SMTP id e7-20020a17090a77c700b00216928a3917so7068925pjs.4 for ; Thu, 10 Nov 2022 23:42:31 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:to:from:from:to:cc:subject:date:message-id :reply-to; bh=WlfgTKwjPzR9xe9xdxRaR9DbAja99BeWVoRkY0FKPGQ=; b=g8eNcfJA8o7QHJ9q+8/qadavI3KG27PFTYT04f2hN8mSqv6aWgcc5pOeYjmOWQ11kS PT0El6lsd2WeXTCI1lio6xvYv+txj2MJ6J/6AeRAQxBPGS/MeSwjIbLMIEvKzamJUr7D +vHFY1dmurCGQDlm2h9tCKHUKTkmWGV/4BjnzqYWzOXnMG9/sBe+aJkkRUQ8fqqmqjz6 eXrCmypPV1nqPK5XK5ttJ1IevIFdNwebfnjZqLbk01jYYuOrg4tbLpe4ClquBy298jkW 5r6dMaSgoD1V+85aMVhQLUQKSI+QwjMG2ReZ5tLjv0chElQckJiYrkJ5CvUIuZn3mHJp RgOg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=WlfgTKwjPzR9xe9xdxRaR9DbAja99BeWVoRkY0FKPGQ=; b=ie/IrkZZlJKbKRGX2Il6kirZzmtvZVdQHpPuZ2lX5qdjOYBDX3ehD6LIXivUQL702G 87dzugPiTZt1O239j4D7y3AfnKSEDuKrKD9AVpqoe5yJaoO2EW6Mdpcvp4xyjFuUHWXR orOJY/5gZOw4g/SVrd6/GU/Qg3F0JVUNBkmZKdpzA+CEkDayabwlouib4lNBXP1Xvdhf GMViQ36eH5IBFAsKJey96AiP/D9W4u15c2Ek9Xn7hdL5mnZL1Uj57Ij1X3jKk+Xd9r+U ZJ8ofeLTfT9sQshESGMe+raCrJJbJiKL8kUtEmEe0VjXrN6/sYU0q3OgUO2d3vd3avUA Nolg== X-Gm-Message-State: ANoB5pmkrv18oGJsp/ZjruisYgTq1FM2dpOjW6LlY96fSMM2JdNCES27 GzSLCdn/mHK/S6C7WdntJRB27QGNCfBpTyLp X-Received: by 2002:a17:902:da90:b0:187:12cc:d6f1 with SMTP id j16-20020a170902da9000b0018712ccd6f1mr1146761plx.63.1668152550164; Thu, 10 Nov 2022 23:42:30 -0800 (PST) Received: from stoup.lan (2001-44b8-2176-c800-0aef-59aa-1faf-7e61.static.ipv6.internode.on.net. [2001:44b8:2176:c800:aef:59aa:1faf:7e61]) by smtp.gmail.com with ESMTPSA id o15-20020a170902d4cf00b001754fa42065sm976069plg.143.2022.11.10.23.42.28 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 10 Nov 2022 23:42:29 -0800 (PST) From: Richard Henderson To: qemu-devel@nongnu.org Subject: [PATCH for-8.0 v3 36/45] include/qemu/int128: Use Int128 structure for TCI Date: Fri, 11 Nov 2022 17:40:52 +1000 Message-Id: <20221111074101.2069454-37-richard.henderson@linaro.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20221111074101.2069454-1-richard.henderson@linaro.org> References: <20221111074101.2069454-1-richard.henderson@linaro.org> MIME-Version: 1.0 Received-SPF: pass client-ip=2607:f8b0:4864:20::1036; envelope-from=richard.henderson@linaro.org; helo=mail-pj1-x1036.google.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+patch=linaro.org@nongnu.org Sender: qemu-devel-bounces+patch=linaro.org@nongnu.org We are about to allow passing Int128 to/from tcg helper functions, but libffi doesn't support __int128_t, so use the structure. In order for atomic128.h to continue working, we must provide a mechanism to frob between real __int128_t and the structure. Provide a new union, Int128Alias, for this. We cannot modify Int128 itself, as any changed alignment would also break libffi. Signed-off-by: Richard Henderson --- include/qemu/atomic128.h | 29 +++++++++++++++++++++------ include/qemu/int128.h | 25 +++++++++++++++++++++--- util/int128.c | 42 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 87 insertions(+), 9 deletions(-) diff --git a/include/qemu/atomic128.h b/include/qemu/atomic128.h index adb9a1a260..d0ba0b9c65 100644 --- a/include/qemu/atomic128.h +++ b/include/qemu/atomic128.h @@ -44,13 +44,23 @@ #if defined(CONFIG_ATOMIC128) static inline Int128 atomic16_cmpxchg(Int128 *ptr, Int128 cmp, Int128 new) { - return qatomic_cmpxchg__nocheck(ptr, cmp, new); + Int128Alias r, c, n; + + c.s = cmp; + n.s = new; + r.i = qatomic_cmpxchg__nocheck((__int128_t *)ptr, c.i, n.i); + return r.s; } # define HAVE_CMPXCHG128 1 #elif defined(CONFIG_CMPXCHG128) static inline Int128 atomic16_cmpxchg(Int128 *ptr, Int128 cmp, Int128 new) { - return __sync_val_compare_and_swap_16(ptr, cmp, new); + Int128Alias r, c, n; + + c.s = cmp; + n.s = new; + r.i = __sync_val_compare_and_swap_16((__int128_t *)ptr, c.i, n.i); + return r.s; } # define HAVE_CMPXCHG128 1 #elif defined(__aarch64__) @@ -89,12 +99,18 @@ Int128 QEMU_ERROR("unsupported atomic") #if defined(CONFIG_ATOMIC128) static inline Int128 atomic16_read(Int128 *ptr) { - return qatomic_read__nocheck(ptr); + Int128Alias r; + + r.i = qatomic_read__nocheck((__int128_t *)ptr); + return r.s; } static inline void atomic16_set(Int128 *ptr, Int128 val) { - qatomic_set__nocheck(ptr, val); + Int128Alias v; + + v.s = val; + qatomic_set__nocheck((__int128_t *)ptr, v.i); } # define HAVE_ATOMIC128 1 @@ -132,7 +148,8 @@ static inline void atomic16_set(Int128 *ptr, Int128 val) static inline Int128 atomic16_read(Int128 *ptr) { /* Maybe replace 0 with 0, returning the old value. */ - return atomic16_cmpxchg(ptr, 0, 0); + Int128 z = int128_make64(0); + return atomic16_cmpxchg(ptr, z, z); } static inline void atomic16_set(Int128 *ptr, Int128 val) @@ -141,7 +158,7 @@ static inline void atomic16_set(Int128 *ptr, Int128 val) do { cmp = old; old = atomic16_cmpxchg(ptr, cmp, val); - } while (old != cmp); + } while (int128_ne(old, cmp)); } # define HAVE_ATOMIC128 1 diff --git a/include/qemu/int128.h b/include/qemu/int128.h index d2b76ca6ac..f62a46b48c 100644 --- a/include/qemu/int128.h +++ b/include/qemu/int128.h @@ -3,7 +3,12 @@ #include "qemu/bswap.h" -#ifdef CONFIG_INT128 +/* + * With TCI, we need to use libffi for interfacing with TCG helpers. + * But libffi does not support __int128_t, and therefore cannot pass + * or return values of this type, force use of the Int128 struct. + */ +#if defined(CONFIG_INT128) && !defined(CONFIG_TCG_INTERPRETER) typedef __int128_t Int128; static inline Int128 int128_make64(uint64_t a) @@ -460,8 +465,7 @@ Int128 int128_divu(Int128, Int128); Int128 int128_remu(Int128, Int128); Int128 int128_divs(Int128, Int128); Int128 int128_rems(Int128, Int128); - -#endif /* CONFIG_INT128 */ +#endif /* CONFIG_INT128 && !CONFIG_TCG_INTERPRETER */ static inline void bswap128s(Int128 *s) { @@ -472,4 +476,19 @@ static inline void bswap128s(Int128 *s) #define INT128_MAX int128_make128(UINT64_MAX, INT64_MAX) #define INT128_MIN int128_make128(0, INT64_MIN) +/* + * When compiler supports a 128-bit type, define a combination of + * a possible structure and the native types. Ease parameter passing + * via use of the transparent union extension. + */ +#ifdef CONFIG_INT128 +typedef union { + Int128 s; + __int128_t i; + __uint128_t u; +} Int128Alias __attribute__((transparent_union)); +#else +typedef Int128 Int128Alias; +#endif /* CONFIG_INT128 */ + #endif /* INT128_H */ diff --git a/util/int128.c b/util/int128.c index ed8f25fef1..df6c6331bd 100644 --- a/util/int128.c +++ b/util/int128.c @@ -144,4 +144,46 @@ Int128 int128_rems(Int128 a, Int128 b) return r; } +#elif defined(CONFIG_TCG_INTERPRETER) + +Int128 int128_divu(Int128 a_s, Int128 b_s) +{ + Int128Alias r, a, b; + + a.s = a_s; + b.s = b_s; + r.u = a.u / b.u; + return r.s; +} + +Int128 int128_remu(Int128 a_s, Int128 b_s) +{ + Int128Alias r, a, b; + + a.s = a_s; + b.s = b_s; + r.u = a.u % b.u; + return r.s; +} + +Int128 int128_divs(Int128 a_s, Int128 b_s) +{ + Int128Alias r, a, b; + + a.s = a_s; + b.s = b_s; + r.i = a.i / b.i; + return r.s; +} + +Int128 int128_rems(Int128 a_s, Int128 b_s) +{ + Int128Alias r, a, b; + + a.s = a_s; + b.s = b_s; + r.i = a.i % b.i; + return r.s; +} + #endif