From patchwork Sat Feb 17 00:39:14 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Richard Henderson X-Patchwork-Id: 773706 Delivered-To: patch@linaro.org Received: by 2002:a5d:4943:0:b0:33b:4db1:f5b3 with SMTP id r3csp206784wrs; Fri, 16 Feb 2024 16:41:21 -0800 (PST) X-Forwarded-Encrypted: i=2; AJvYcCXAB4QJZYHGfgVtsa1nBWv8o61yRgmqFOWWRWC0AuiRsMPC4M8jLMbDkpSxPOQ5UuNDyq1t8UBzqzEamExHV8Y6 X-Google-Smtp-Source: AGHT+IFMj1dL63E/6BRZOAQkeWWyHhGdwgTYDFz5AjlUHzPL0e8yRpiGCjZcz+oXhhVNJWEmNolI X-Received: by 2002:ae9:c20d:0:b0:787:3aee:32dd with SMTP id j13-20020ae9c20d000000b007873aee32ddmr5529528qkg.57.1708130480835; Fri, 16 Feb 2024 16:41:20 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1708130480; cv=none; d=google.com; s=arc-20160816; b=vKwN4kY5qOOeRjIkDZoe2rdPNi9uyI/OfnwfPcGcpItIJUKEDWO66eGFD75gxsLO3L rsDzvhI792Q5/ezrzjl69XiTO1qekaSf4d3niEG3W6wLhNh3D8diuAUE/lOW3N7FHQJO XBeLVEz4PE8ZOHFmBHG9xo7iHhfWJk5F5c2mmKanubeULJiyyOCXYTjvfGtCPZQ4ZPns V8hMxhF5SRXAlYGfdNNwl7/XqHmB0VmMe0ly95WMrdAnmmVhYzyjjPJDJCCx4jfeupzX sX6/iM2pcEcJhDHI358byd28faqxbyRco2DrYrJ05UZQJ9p4LPFXoDMDCIYnYD6aOYYm ohgA== 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=kPtJ2gcWQWVvl3eYL4tk0YOKmkXHenThqZDsJAXs+wo=; fh=bXD9UGo7d05OYjVg0qF2wA28p2gxs496M6ftxgeDKVY=; b=JwkB6FF+yAouTg+yFAqwTIH4ltvhNh2hK4pnWLZg6q46hWORbZMrMi5Z8n+LfK3lv7 EiVqID6+B2a01CIBeOPh4/nVYDYcirrNocDI548DDdawxWg55BN+JKam8Lg36hfAD5yE 9gR9cf+ZQltXgQPpD9mGtzrXLFDwSFUQkuuGbi3Sk9aH9j3KVIc0xBihVJ975TMJjHE3 Zf8N2ujU0LCJnpy+FDTZMxtBkHX8RqPtqrLttfEImLyzyZItG36sGOA9vlu6ZQzaFGvK qLzH3H+yqDGkjLDYMVRE2y/2EpO5Zp75qe/yzA/b1eSnNrg9fm87w1BLyIlzQsQXIYn4 yE+Q==; dara=google.com ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=w9v4enFk; 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 m23-20020ae9e017000000b00784aae26822si1026323qkk.583.2024.02.16.16.41.20 for (version=TLS1_2 cipher=ECDHE-ECDSA-CHACHA20-POLY1305 bits=256/256); Fri, 16 Feb 2024 16:41:20 -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=w9v4enFk; 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 1rb8jv-00068D-2n; Fri, 16 Feb 2024 19:39:35 -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 1rb8js-00066B-RI for qemu-devel@nongnu.org; Fri, 16 Feb 2024 19:39:32 -0500 Received: from mail-pl1-x62e.google.com ([2607:f8b0:4864:20::62e]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1rb8jr-0008Gf-5d for qemu-devel@nongnu.org; Fri, 16 Feb 2024 19:39:32 -0500 Received: by mail-pl1-x62e.google.com with SMTP id d9443c01a7336-1d780a392fdso13142115ad.3 for ; Fri, 16 Feb 2024 16:39:30 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1708130369; x=1708735169; darn=nongnu.org; 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=kPtJ2gcWQWVvl3eYL4tk0YOKmkXHenThqZDsJAXs+wo=; b=w9v4enFkGQirt/QzMAfau+pLcveXFzQyKo0fgxliLLY8+uiEAfDWgtaKZZJz9DGtyN CUyqks8yqI7JYZHLVafKx7zZEvN1zRfZiZ1h3DNtwH58xo4oOl6y+a7B74em2SaZdULs XpYvsLDhyMfr9rF1F+EGX7jAeUrFRZ5U8lniS72CYdA1gYZgrgO9TBR7YbMKM0W45Gwd OiePKYghCstYwPSQX7ZNGMv4QG6PgaaijaKBHFL5dw6O+nqNxDxSMNY+fKcDWdHK6EHF Jukl1ahAZTEa+Mw9aW/HEVeRose0O6C/QhUuWp0Spb5qyzwlsTgkOCtwXSozHNznDqxm yAuw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1708130369; x=1708735169; 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=kPtJ2gcWQWVvl3eYL4tk0YOKmkXHenThqZDsJAXs+wo=; b=L4/IUPgj0kmHhv0SWXQeBReX0TdCq3uju10HPIZFABDGzfQtYBpYVe9wlaBCdh1O2m 8iLCvCWllWx5Md+R1VdtX21AA/siRDHJHOzEh5oyNq0RU7UjcAVKkbvNQzKxGUKiCvrP vQdp498s0WxxxjzOjRJGFRgvfLfB/N7TeIadtWq81u5uOs8PqDUhfAdP7a4erG1zwJz4 tNcKdDEIHgURsaCLI1dDgJOvDTDn5Pm/I7qGZseXsjc7ArG7dfMczp9KFclyedXhYq1Z AZJ++8irXfYCRmnNAxJ8DCwY4aA+ItFsKAyn1sz09K0FV1nlxe9e4lrGdjh6TlzP/Wly AjAQ== X-Gm-Message-State: AOJu0YxVayY8aR9G1xe1DHtE8vdcE3/KO2YseCmJf0llMnjPQS2X1Kk3 2luTQlF5Obc/O4JOu5acbYWhJ58oE21zfqeEGhbqgTuLPxmOAna0iPgPhHJ3Rkb2rtrkkOgpGlZ F X-Received: by 2002:a17:903:2352:b0:1d9:bf90:2f1b with SMTP id c18-20020a170903235200b001d9bf902f1bmr7600846plh.53.1708130369517; Fri, 16 Feb 2024 16:39:29 -0800 (PST) Received: from stoup.. (173-197-098-125.biz.spectrum.com. [173.197.98.125]) by smtp.gmail.com with ESMTPSA id z6-20020a170902ee0600b001d90306bdcfsm419325plb.65.2024.02.16.16.39.28 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 16 Feb 2024 16:39:29 -0800 (PST) From: Richard Henderson To: qemu-devel@nongnu.org Cc: amonakov@ispras.ru, mmromanov@ispras.ru Subject: [PATCH v5 06/10] util/bufferiszero: Improve scalar variant Date: Fri, 16 Feb 2024 14:39:14 -1000 Message-Id: <20240217003918.52229-7-richard.henderson@linaro.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240217003918.52229-1-richard.henderson@linaro.org> References: <20240217003918.52229-1-richard.henderson@linaro.org> MIME-Version: 1.0 Received-SPF: pass client-ip=2607:f8b0:4864:20::62e; envelope-from=richard.henderson@linaro.org; helo=mail-pl1-x62e.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, T_SCC_BODY_TEXT_LINE=-0.01 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 | 86 +++++++++++++++++++++++++++------------------ 1 file changed, 52 insertions(+), 34 deletions(-) diff --git a/util/bufferiszero.c b/util/bufferiszero.c index 02df82b4ff..a904b747c7 100644 --- a/util/bufferiszero.c +++ b/util/bufferiszero.c @@ -28,40 +28,58 @@ 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, with a couple + * of loops handling the middle aligned section. + */ + 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); + + 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, with a couple + * of loops handling the middle aligned section. + */ + 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 +191,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 +229,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 +250,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)