From patchwork Sun May 16 12:33:48 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Richard Henderson X-Patchwork-Id: 439847 Delivered-To: patch@linaro.org Received: by 2002:a02:7a1b:0:0:0:0:0 with SMTP id a27csp256870jac; Sun, 16 May 2021 05:41:52 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxlTDWgU/gIedgJhcFfoqZvK9Jh3kY7Sbu6yc8JSm3QaegsMU5mayj+JrN0Lv6jzmmaHtFl X-Received: by 2002:a05:6602:221a:: with SMTP id n26mr27556190ion.205.1621168912644; Sun, 16 May 2021 05:41:52 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1621168912; cv=none; d=google.com; s=arc-20160816; b=SIdcUtl+ZJl2dKPeup5d12JEB5OBf222SK3n5UxTs4wyl9pLxOJZs32g8/zLd7tsjm L8tZe7catdUDprgUvVmxxdDr9d97mri0yD5aVnZlUboyCMYQ8GMgOsrkDkDh6BPaBkrZ /EC8RDg+CzO6lo3tn5qaBYNQ1TkP6pYn7uH4TFSoLygRQyCk3BMuVIqkojc1HGldnNTB Dk0F4GqlL2y/Oa1fkk+Le/pQmamXN/wUVrdeVo0RxJh76ZOrg3YX09o81nNl+HR5QXL0 gp0LItBGgpzeJ9k4NLlxnz49e4orOtPFlhj92HvHTmsKLwVVF7nOU3vEWKhrSAv4glvM Rv1Q== 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:content-transfer-encoding :mime-version:references:in-reply-to:message-id:date:subject:to:from :dkim-signature; bh=lkUZA1QUxVx6D7Ez3r9MoCYNO26DmbAtxbxbzUkDVlw=; b=f3vatmur+TqIX5X1mUQvI4NnA+agtHP9Vgt5YFYmsOqPg+r7bn4uXmh7Zxskqt9quE adP90shwfyEiZxppb+n08dwGF8mMis/nx28dRiH3CxkWKnm3ANP3O9L12K94g3imdEIx bXOBE1U6xSBBAQqAlAz87c5QGD3xpWXyhSsvVlMfSputWGN5sodVOhztUgnQ60c/PiXZ 7tEbV47h+99vXD5TTp5bQ2mz/kzb8XzC2LA8bxrRahYfO4RnehyESVFhPYhe+i4rvLAu AkG0DflMEuC2G8GqlFNds5yQlGKiNGNF6IZ1EuwXMaBIc1UtaCrEec8prqbzDWgidpcC uNNA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=hGbSRJ4c; 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 k15si13116459ilr.47.2021.05.16.05.41.52 for (version=TLS1_2 cipher=ECDHE-ECDSA-CHACHA20-POLY1305 bits=256/256); Sun, 16 May 2021 05:41:52 -0700 (PDT) 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=hGbSRJ4c; 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]:40774 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1liG5g-0001rL-3S for patch@linaro.org; Sun, 16 May 2021 08:41:52 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:42876) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1liFyg-0007AV-9S for qemu-devel@nongnu.org; Sun, 16 May 2021 08:34:38 -0400 Received: from mail-qt1-x82b.google.com ([2607:f8b0:4864:20::82b]:46726) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1liFye-0007ek-7L for qemu-devel@nongnu.org; Sun, 16 May 2021 08:34:38 -0400 Received: by mail-qt1-x82b.google.com with SMTP id m13so2984626qtk.13 for ; Sun, 16 May 2021 05:34:35 -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=lkUZA1QUxVx6D7Ez3r9MoCYNO26DmbAtxbxbzUkDVlw=; b=hGbSRJ4crDBrGo4NCYcjMQiD1/31bqIEEXSbIidbCwupwSKzlzOOhrkFRFkccAYvfs RCbns5KcpwCFYRekEQxJqsAU6NTZ8EartV33lRhigdVI6pqimsP7CgKXgkbZ7WEQKaJF 860vV1QkgVZD0KDqF9Pm8L/CGJc5ATTcp3xF4MV/9xUk8eFg31Y6OHO3PBiaHIKBYb3r 8ChkafloMVdp1pU0CG0tOs+/1dlBvYRFDSO1a3RtPHLVvM3/LpMr1CxWBSn/7X3sC2/5 euw6/e9usJajCdY63j+V8iPHPPDctSkOug2Vzq/Y51buc0yWRrmhbKc4rKucIjZqWnqA M7sQ== 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=lkUZA1QUxVx6D7Ez3r9MoCYNO26DmbAtxbxbzUkDVlw=; b=uETi8TUu3U+pONe1saRdmbYn8qLpBxl7ZdunfXFqHaZEB9mJNU7I7IE3jKbJn5MLoa B2UqEnEGKl0QD8PUS/zMNod39CZbouMs9ppGuzRkqK0Vgi4yPYYXnXa2p+cT497ypDDh TbKyOePhjqLcjcx0novOR1XRIic/JxaIyNWSC+W5TYEg/gziiKvto/aggUxstdiaiLzK mLH8vfXHqodVxJBlJwrLd4EsvlTarN3lHH/76NH0Yo2bGV/5Ffm4ONipnU2MK8PM0L6O d2Hb17xPnb8cDafbWxBsvwnPkdPqw9oMt75hmnIyqg/Sv53UgnZ4C1UOQMA0G+zRZ4+r r8Tw== X-Gm-Message-State: AOAM531JPx3ziOjzAvsWYI9XGngF0SaT5JkzMkRqbJ5MixCiBHKHK4R9 EBF0NrECgKv5Phr8pEusuGyCWgackTfLTBtlnPU= X-Received: by 2002:ac8:450c:: with SMTP id q12mr9538723qtn.327.1621168475296; Sun, 16 May 2021 05:34:35 -0700 (PDT) Received: from localhost.localdomain (163.189-204-200.bestelclientes.com.mx. [189.204.200.163]) by smtp.gmail.com with ESMTPSA id s5sm8500553qkg.88.2021.05.16.05.34.34 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 16 May 2021 05:34:35 -0700 (PDT) From: Richard Henderson To: qemu-devel@nongnu.org Subject: [PULL 03/46] qemu/host-utils: Add wrappers for carry builtins Date: Sun, 16 May 2021 07:33:48 -0500 Message-Id: <20210516123431.718318-4-richard.henderson@linaro.org> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20210516123431.718318-1-richard.henderson@linaro.org> References: <20210516123431.718318-1-richard.henderson@linaro.org> MIME-Version: 1.0 Received-SPF: pass client-ip=2607:f8b0:4864:20::82b; envelope-from=richard.henderson@linaro.org; helo=mail-qt1-x82b.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.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: peter.maydell@linaro.org, =?utf-8?q?Alex_Benn=C3=A9e?= Errors-To: qemu-devel-bounces+patch=linaro.org@nongnu.org Sender: "Qemu-devel" These builtins came in clang 3.8, but are not present in gcc through version 11. Even in clang the optimization is only ideal on x86_64, but never worse than the hand-coding that we currently do. Reviewed-by: Alex Bennée Signed-off-by: Richard Henderson --- include/qemu/host-utils.h | 50 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) -- 2.25.1 diff --git a/include/qemu/host-utils.h b/include/qemu/host-utils.h index cb95626c7d..711b221704 100644 --- a/include/qemu/host-utils.h +++ b/include/qemu/host-utils.h @@ -26,6 +26,7 @@ #ifndef HOST_UTILS_H #define HOST_UTILS_H +#include "qemu/compiler.h" #include "qemu/bswap.h" #ifdef CONFIG_INT128 @@ -581,6 +582,55 @@ static inline bool umul64_overflow(uint64_t x, uint64_t y, uint64_t *ret) #endif } +/** + * uadd64_carry - addition with carry-in and carry-out + * @x, @y: addends + * @pcarry: in-out carry value + * + * Computes @x + @y + *@pcarry, placing the carry-out back + * into *@pcarry and returning the 64-bit sum. + */ +static inline uint64_t uadd64_carry(uint64_t x, uint64_t y, bool *pcarry) +{ +#if __has_builtin(__builtin_addcll) + unsigned long long c = *pcarry; + x = __builtin_addcll(x, y, c, &c); + *pcarry = c & 1; + return x; +#else + bool c = *pcarry; + /* This is clang's internal expansion of __builtin_addc. */ + c = uadd64_overflow(x, c, &x); + c |= uadd64_overflow(x, y, &x); + *pcarry = c; + return x; +#endif +} + +/** + * usub64_borrow - subtraction with borrow-in and borrow-out + * @x, @y: addends + * @pborrow: in-out borrow value + * + * Computes @x - @y - *@pborrow, placing the borrow-out back + * into *@pborrow and returning the 64-bit sum. + */ +static inline uint64_t usub64_borrow(uint64_t x, uint64_t y, bool *pborrow) +{ +#if __has_builtin(__builtin_subcll) + unsigned long long b = *pborrow; + x = __builtin_subcll(x, y, b, &b); + *pborrow = b & 1; + return x; +#else + bool b = *pborrow; + b = usub64_overflow(x, b, &x); + b |= usub64_overflow(x, y, &x); + *pborrow = b; + return x; +#endif +} + /* Host type specific sizes of these routines. */ #if ULONG_MAX == UINT32_MAX