From patchwork Mon Apr 8 17:49:27 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Richard Henderson X-Patchwork-Id: 786956 Delivered-To: patch@linaro.org Received: by 2002:adf:fb4d:0:b0:346:15ad:a2a with SMTP id c13csp2624wrs; Mon, 8 Apr 2024 10:58:11 -0700 (PDT) X-Forwarded-Encrypted: i=2; AJvYcCVKYhGg31xsse2JnG+zJlRYihxC+BRIxHRqt5XnvLzpbD92RAnEsRehb3YxlGk2e7swjm6TjfPzyrqcsfBQ1IPq X-Google-Smtp-Source: AGHT+IFgwSt3sUkCvkb5f7eaksKPfvB9KVuU0qomSWDSz1m9XznxZwN/Bslfso4bpi6K0QalOJMB X-Received: by 2002:a81:bf4f:0:b0:615:4e88:c036 with SMTP id s15-20020a81bf4f000000b006154e88c036mr8504500ywk.41.1712599091374; Mon, 08 Apr 2024 10:58:11 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1712599091; cv=none; d=google.com; s=arc-20160816; b=O1lscWwtT4Dtn4epa5gmq4y4tjCVnPglhSP74JX7m78nx3XDhSjkDJpFLLlGu4+brt qFIeSavAuqnWjLbSbDejwsfjACpF3KiLVaIztVRe01UMwOnOOUPfxMkqo2w5dRz/R+SN 7CLiy+yad7sQrFiMpe6KFDp5kvdWV81HPm35u9vT8Vo39HbGJJR0x3Gwk6lKh4f0dYSj mqizMGSt9P8ITXrc25jE8DmdNQYpSuxW2TZbjbWEHwlTPyLkZUeT67IUR/K5Mescd+y1 L00LFERsw6A7GAFiSLdNhJQrEo1PUx0yF+7uqxp90Lb8KDOZeSYIRoIsEw2LsAOLB/1Z zDrg== 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=m6Ty8XllMR28noTCDsk0N/pyr7ynqbZ4X/2NsrzAk7g=; fh=PnYt+qEB9tAfMKoqBm2xjKOFpYyFFGPudh5cVIoieJM=; b=Nn0847gadTfaQsQKahujLqKs495XfOCKGY01zDSRQvn+bn42rh5bYH08ZVtoofYyaF 3XoPJABuUMCCWJNtvP09cJBhiQISvpHhJPKcwZkKDXWx1hm/oXi3xdkDciFUWAyuT+1o c6yY6kUPQM7gPONGI4ZzgU6ns/hmimS6iizF1pgoWzqsklzs4265s6Dn63lntSnqQVjk piCKAheaUjVT2pSQ7D+UARqQYby6eyTjtAAnqfLJ+JuxijiWUkiYy2vGR13W6wc6m61e ornsoh429/W57ZEmHvpzDcIbmh+hyVFvXOYIROekcnSRBIuBTjcZlnL6nVCNw2pJmYUh r1Ig==; dara=google.com ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=sJflrMlq; 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 t15-20020ac8588f000000b00434c6092762si808801qta.258.2024.04.08.10.58.11 for (version=TLS1_2 cipher=ECDHE-ECDSA-CHACHA20-POLY1305 bits=256/256); Mon, 08 Apr 2024 10:58:11 -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=sJflrMlq; 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 1rtt8T-0002Ab-HP; Mon, 08 Apr 2024 13:50:25 -0400 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 1rtt8S-0002A0-Cg for qemu-devel@nongnu.org; Mon, 08 Apr 2024 13:50:24 -0400 Received: from mail-pj1-x102e.google.com ([2607:f8b0:4864:20::102e]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1rtt8Q-0003oJ-Cj for qemu-devel@nongnu.org; Mon, 08 Apr 2024 13:50:23 -0400 Received: by mail-pj1-x102e.google.com with SMTP id 98e67ed59e1d1-2a07b092c4fso3408368a91.0 for ; Mon, 08 Apr 2024 10:50:22 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1712598621; x=1713203421; darn=nongnu.org; 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=m6Ty8XllMR28noTCDsk0N/pyr7ynqbZ4X/2NsrzAk7g=; b=sJflrMlqcP36MdD6XvaIJdiSEir919w8Po7V8VrvYJTya2KgryI/f4cj3euDp0N7Rc uhzTpGdRjptRJFo5gbJi331yPEcOOfmKqBvt0OyDsuPzHk5We7jdjSLuTTTgl/9fgCeH TNi6IMmM1x8wM8rInfKolAYFdqoQrI2Cuhb7aNHYLjORfJ0xhSnabFsnsateq+zxXuCs F3UCzyuHF61hwDZdyLB8bSUEIPD4YF3BOjFWmZse5ET3+njKU7fJli1rinmYCSRvaKwZ rSd1v48mOiFxDWL6i9SHepqm7EeW4ezZukb21ZRRmN9+yJHLasxnfqM702+kSSogZOyy ak5w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1712598621; x=1713203421; 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=m6Ty8XllMR28noTCDsk0N/pyr7ynqbZ4X/2NsrzAk7g=; b=BVydChQ2WP/TL7xpKzoO8Th1WUG2q+lZx0CJ5HbooXOEMCLSwxdpmM7CyzvKyPEf2V ewhNDuU4bs9dBIOvQIGStaLBxXn24Ru2uhq5FGq8mKP8kn8Db1VamB9LXXlo96So0fzk iUAW5bcHO+MIaXRFuYzXfaBDmiC5FDqqNFaf4fVyAXtH+qkJ1Pxw7GnhZ5c0krDSaR4r zxYXQXi5whr3sPcpYNFvCTdldbc/dhhO+aeNT68WtqH0l1isp2Puuq/Wxy65prXL7Ior c53ec9J5ltYOtSEUV7cYh59L3U/vDgsd/smIstk/KqT1Nljo/B4oFdZSMYbTYibVT7BI UcRA== X-Gm-Message-State: AOJu0Yx73NTBB49QdgzIJuvE9pyFUvr/8/zdnquuVqovrAfFd/hovo6M kzsSokt5gaK4XpeV+i6Y0niKzS8IYjNuFc6CbRs6UobZRa8K31ZUdZa6FW415rPnUrxkLAzDuth v X-Received: by 2002:a17:90b:1bc9:b0:2a2:9b37:367a with SMTP id oa9-20020a17090b1bc900b002a29b37367amr7922512pjb.39.1712598620788; Mon, 08 Apr 2024 10:50:20 -0700 (PDT) Received: from stoup.. (098-147-007-212.res.spectrum.com. [98.147.7.212]) by smtp.gmail.com with ESMTPSA id ga15-20020a17090b038f00b0029c3bac0aa8sm8658432pjb.4.2024.04.08.10.50.19 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 08 Apr 2024 10:50:20 -0700 (PDT) From: Richard Henderson To: qemu-devel@nongnu.org Subject: [PULL 33/35] util/bufferiszero: Improve scalar variant Date: Mon, 8 Apr 2024 07:49:27 -1000 Message-Id: <20240408174929.862917-34-richard.henderson@linaro.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240408174929.862917-1-richard.henderson@linaro.org> References: <20240408174929.862917-1-richard.henderson@linaro.org> MIME-Version: 1.0 Received-SPF: pass client-ip=2607:f8b0:4864:20::102e; envelope-from=richard.henderson@linaro.org; helo=mail-pj1-x102e.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 Split less-than and greater-than 256 cases. Use unaligned accesses for head and tail. Avoid using out-of-bounds pointers in loop boundary conditions. Signed-off-by: Richard Henderson --- util/bufferiszero.c | 85 +++++++++++++++++++++++++++------------------ 1 file changed, 51 insertions(+), 34 deletions(-) diff --git a/util/bufferiszero.c b/util/bufferiszero.c index 02df82b4ff..c9a7ded016 100644 --- a/util/bufferiszero.c +++ b/util/bufferiszero.c @@ -28,40 +28,57 @@ static bool (*buffer_is_zero_accel)(const void *, size_t); -static bool buffer_is_zero_integer(const void *buf, size_t len) +static bool buffer_is_zero_int_lt256(const void *buf, size_t len) { - if (unlikely(len < 8)) { - /* For a very small buffer, simply accumulate all the bytes. */ - const unsigned char *p = buf; - const unsigned char *e = buf + len; - unsigned char t = 0; + uint64_t t; + const uint64_t *p, *e; - do { - t |= *p++; - } while (p < e); - - return t == 0; - } else { - /* Otherwise, use the unaligned memory access functions to - handle the beginning and end of the buffer, with a couple - of loops handling the middle aligned section. */ - uint64_t t = ldq_he_p(buf); - const uint64_t *p = (uint64_t *)(((uintptr_t)buf + 8) & -8); - const uint64_t *e = (uint64_t *)(((uintptr_t)buf + len) & -8); - - for (; p + 8 <= e; p += 8) { - if (t) { - return false; - } - t = p[0] | p[1] | p[2] | p[3] | p[4] | p[5] | p[6] | p[7]; - } - while (p < e) { - t |= *p++; - } - t |= ldq_he_p(buf + len - 8); - - return t == 0; + /* + * Use unaligned memory access functions to handle + * the beginning and end of the buffer. + */ + if (unlikely(len <= 8)) { + return (ldl_he_p(buf) | ldl_he_p(buf + len - 4)) == 0; } + + t = ldq_he_p(buf) | ldq_he_p(buf + len - 8); + p = QEMU_ALIGN_PTR_DOWN(buf + 8, 8); + e = QEMU_ALIGN_PTR_DOWN(buf + len - 1, 8); + + /* Read 0 to 31 aligned words from the middle. */ + while (p < e) { + t |= *p++; + } + return t == 0; +} + +static bool buffer_is_zero_int_ge256(const void *buf, size_t len) +{ + /* + * Use unaligned memory access functions to handle + * the beginning and end of the buffer. + */ + uint64_t t = ldq_he_p(buf) | ldq_he_p(buf + len - 8); + const uint64_t *p = QEMU_ALIGN_PTR_DOWN(buf + 8, 8); + const uint64_t *e = QEMU_ALIGN_PTR_DOWN(buf + len - 1, 8); + + /* Collect a partial block at the tail end. */ + t |= e[-7] | e[-6] | e[-5] | e[-4] | e[-3] | e[-2] | e[-1]; + + /* + * Loop over 64 byte blocks. + * With the head and tail removed, e - p >= 30, + * so the loop must iterate at least 3 times. + */ + do { + if (t) { + return false; + } + t = p[0] | p[1] | p[2] | p[3] | p[4] | p[5] | p[6] | p[7]; + p += 8; + } while (p < e - 7); + + return t == 0; } #if defined(CONFIG_AVX2_OPT) || defined(__SSE2__) @@ -173,7 +190,7 @@ select_accel_cpuinfo(unsigned info) { CPUINFO_AVX2, buffer_zero_avx2 }, #endif { CPUINFO_SSE2, buffer_zero_sse2 }, - { CPUINFO_ALWAYS, buffer_is_zero_integer }, + { CPUINFO_ALWAYS, buffer_is_zero_int_ge256 }, }; for (unsigned i = 0; i < ARRAY_SIZE(all); ++i) { @@ -211,7 +228,7 @@ bool test_buffer_is_zero_next_accel(void) return false; } -#define INIT_ACCEL buffer_is_zero_integer +#define INIT_ACCEL buffer_is_zero_int_ge256 #endif static bool (*buffer_is_zero_accel)(const void *, size_t) = INIT_ACCEL; @@ -232,7 +249,7 @@ bool buffer_is_zero_ool(const void *buf, size_t len) if (likely(len >= 256)) { return buffer_is_zero_accel(buf, len); } - return buffer_is_zero_integer(buf, len); + return buffer_is_zero_int_lt256(buf, len); } bool buffer_is_zero_ge256(const void *buf, size_t len)