From patchwork Thu Jan 26 04:37:56 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Richard Henderson X-Patchwork-Id: 647076 Delivered-To: patch@linaro.org Received: by 2002:a17:522:b9de:b0:4b9:b062:db3b with SMTP id fj30csp59061pvb; Wed, 25 Jan 2023 20:43:44 -0800 (PST) X-Google-Smtp-Source: AMrXdXvMau3L9B2DE/jTfuZuDIB44hKgKUnW4aoAj86PlQNPRfoFrP0th4uagCO6ecgqJOmZjvVR X-Received: by 2002:ac8:5158:0:b0:3b6:36f8:a63a with SMTP id h24-20020ac85158000000b003b636f8a63amr22014776qtn.63.1674708223764; Wed, 25 Jan 2023 20:43:43 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1674708223; cv=none; d=google.com; s=arc-20160816; b=IttFUPeDTprrAsf5vfs/8MJpVxhmB0rNl4o+FhwvCS3cN+1aMAAPXRIfEtf8rKWVgi suRHFAPILdzaN5FV9b0t+XKzA7Te86vW9QfCXDlRhA3a74tyBJB2G9ZhwZMyJsMs2nPT RxlDyLu7W2h755CcSe+MiaNmPU5YKkeSLTdM+QN1vxnD5qE4+7KkAlO1hLMy62gzYvok 34Xuvc0G/O8wyYaUCHT09zp/vYKzmgY681oHa5+A9+VgknoTDqLrLinsVDah2n7uG91J bIWB74EqLMnE+TzNeNzGf78Q1XUyzJdAKRHGZxNE5/6whpmLOlKrDwPn6CvCjwL9PiNo YsPQ== 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:cc:to :from:dkim-signature; bh=5Hbm3ifGwuCI4UMEGM6ZiBOelwkHZlrSPUn3TtCQ2uk=; b=kI94la3uvkjTdBI+IGYVsmDM2ehkTvAqpLxHvQmMPW6DFzPEuTBh5VxkiVf+uFmfZF AmY56dk8NLM+x/uRZZRiIBQ3Er97rT0z9hHavxJyW9ZTCHvnWrPjaM5P1ocVhcBBr3t7 TI4hoE2uREhSiywOhK32NMFasnd6vrfGT2FSJmw8mHgfNkBrQxVkIRN6ru1qxNukLsdd KQR7V+soP7wUeU5XDsEmQJJDJmAMG7al8gDHZ1qZ4sru/GWV1Fz45ulz1Y45jjaG+x9p OzOXfgLwd5IYukGToTLpkWzQI5H3Zrj1qUMwd43a3xy0vzULqm8Dn26RaX861S396ZCU s9Sg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=flaj75ZQ; 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 b2-20020ac85bc2000000b003b7ed09e5fcsi109139qtb.604.2023.01.25.20.43.43 for (version=TLS1_2 cipher=ECDHE-ECDSA-CHACHA20-POLY1305 bits=256/256); Wed, 25 Jan 2023 20:43:43 -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=flaj75ZQ; 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 1pKu2A-0000AH-PR; Wed, 25 Jan 2023 23:38:46 -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 1pKu27-00008a-JA for qemu-devel@nongnu.org; Wed, 25 Jan 2023 23:38:43 -0500 Received: from mail-pj1-x102b.google.com ([2607:f8b0:4864:20::102b]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1pKu25-0004Qk-PK for qemu-devel@nongnu.org; Wed, 25 Jan 2023 23:38:43 -0500 Received: by mail-pj1-x102b.google.com with SMTP id v6-20020a17090ad58600b00229eec90a7fso5407419pju.0 for ; Wed, 25 Jan 2023 20:38:41 -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:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=5Hbm3ifGwuCI4UMEGM6ZiBOelwkHZlrSPUn3TtCQ2uk=; b=flaj75ZQRCZCDy3NcxKoufINa8DhnySheEHIGtoJ0s+p045IVhYzYzH2+P3f3a/1Qg D+SOE9VzQcHcWUZ4GzbwAbiuvmS3egjAjbQ3BoCC4wm4YkmILdTZ1RFnnuizmginrGjm kosNrZNO5vPYT+TVBwPNdz5elkksuQ1e0ANi/gyhZGhrrcWd3/dA+U2rFnANGUC303SA G3WFFv+KLqqZmzQycV1TpaWk4HGHVRvvPzl4MU9Ys7lEdpoMzcdiRmbgTUzwrgVzktHR tby7YSJqiL4kGb88RjRfM5eeKt3ksk2dJRH+CVH1MVJdRTZGDik12FZN5HBRtxPegr0e 6slQ== 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:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=5Hbm3ifGwuCI4UMEGM6ZiBOelwkHZlrSPUn3TtCQ2uk=; b=EGBaHgENvTpNvYb3Zfqnd9AnMLwT2MQsU8W7C9/BHxKiVfCAvhpq3xgXZAPg6wd9BP OOoTezaJi91BKSoGmrIwOmfbwgigCRhyc3gZzMyroE3/llTxSWA33h7qQ0/NKcO2OLIi VFe//QpL1Ylnl+xCX5E5nWscElQmU5onZ2H9sFTCXH91+X4kgL3HTFuQ5amQOnVEoi73 O/sGJ0SyKxGORghfsH4MCFrNK+hFMobRn+rnwmheq5gjqNxCWyzCxgHukRvy3Ku7JyO4 h8QwFjlrB3hfACNRDJ93uuQAut2gOGiYVdvU5M19EQ4Mb/weLi+W3LPgJAZyhhWW6nTR oRHg== X-Gm-Message-State: AFqh2krEGunsv04jFUshSv7DoeUNNb4IwNIeR+oaaCpq2EHbVhYPpiv6 RVe45VjveSa59bbFOSNjrr7UdNP7UtjnQTS2C7c= X-Received: by 2002:a17:90b:2395:b0:226:f35f:923b with SMTP id mr21-20020a17090b239500b00226f35f923bmr36287673pjb.2.1674707920316; Wed, 25 Jan 2023 20:38:40 -0800 (PST) Received: from stoup.. (rrcs-173-197-98-118.west.biz.rr.com. [173.197.98.118]) by smtp.gmail.com with ESMTPSA id s62-20020a17090a69c400b00228e56d375asm233376pjj.33.2023.01.25.20.38.38 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 25 Jan 2023 20:38:39 -0800 (PST) From: Richard Henderson To: qemu-devel@nongnu.org Cc: alex.bennee@linaro.org, =?utf-8?q?Philippe_Mathieu-Daud=C3=A9?= Subject: [PATCH v5 08/36] include/qemu/int128: Use Int128 structure for TCI Date: Wed, 25 Jan 2023 18:37:56 -1000 Message-Id: <20230126043824.54819-9-richard.henderson@linaro.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230126043824.54819-1-richard.henderson@linaro.org> References: <20230126043824.54819-1-richard.henderson@linaro.org> MIME-Version: 1.0 Received-SPF: pass client-ip=2607:f8b0:4864:20::102b; envelope-from=richard.henderson@linaro.org; helo=mail-pj1-x102b.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. Reviewed-by: Philippe Mathieu-Daudé Signed-off-by: Richard Henderson Reviewed-by: Alex Bennée --- 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