Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1rpaxq-007UC5-VX for pgsql-hackers@arkaria.postgresql.org; Wed, 27 Mar 2024 21:37:43 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.94.2) (envelope-from ) id 1rpaxq-002t0i-2E for pgsql-hackers@arkaria.postgresql.org; Wed, 27 Mar 2024 21:37:42 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1rpaxp-002t0U-RB for pgsql-hackers@lists.postgresql.org; Wed, 27 Mar 2024 21:37:41 +0000 Received: from mail-il1-x12d.google.com ([2607:f8b0:4864:20::12d]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.94.2) (envelope-from ) id 1rpaxl-006ler-O4 for pgsql-hackers@postgresql.org; Wed, 27 Mar 2024 21:37:40 +0000 Received: by mail-il1-x12d.google.com with SMTP id e9e14a558f8ab-36867dfe940so788175ab.3 for ; Wed, 27 Mar 2024 14:37:38 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1711575458; x=1712180258; darn=postgresql.org; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:from:to:cc:subject:date:message-id:reply-to; bh=RrckcrfgNfhnCegKlA/Z4rr1Vm1FKgE1Y1d0hrMvbkA=; b=ZsF2EE9xfHNv2ENChoSHVkhNuZQ4kFVGZNeE5SJYqfbWT9C5BXnkiT7iOP92Bue0Vz vrO5i56JkPSda/hh05b6qdllqklqxPhyGq7YUcIwj9JOpQZF5iyQ/JN5yFrGMJiZWVjO 2+1s8JBexo13LdGkVg4FwK499ztEwEb+ktSFxNB4h9ic6vJ9SLirpqB0OqI2t+INHB3q DDdWY+jZurV6TCMy6bH12sxM/xjM6L1AMB2+6RevG18drY+oN1pAcPxLOEJkiY4JAtnF IAK2tWlTZ0s5Af1bpJvlHGKReTFdFwihcrfWxCya+nACCPH1g7FpxTIOgRNRcKf9s7xz IoOw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1711575458; x=1712180258; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=RrckcrfgNfhnCegKlA/Z4rr1Vm1FKgE1Y1d0hrMvbkA=; b=vukEXKYu1zVq5o5IUHbyvaYpKE+W9/OdbGvW8ZeI6p2u1VGJOqQ/sHNGj76S20ANnW yM1RkihAd6lgwHVLZKYja1lvPyEhUlK9qYXcVD5SYWi2efB9MoWOmd5nxO3EeW8+gcT4 hIY85CrhlmEtICx+Z/gb0HIYdjetf+Mim+abXunU1aYJz0Zup2NmAkpX7gvqF7PT81A/ dfybZfJ9zVF+N8YToa1i1YNQUHP4TbmcSCu1Vdbccp7QkJIan+y5H+KhJqGupO4Gi8NQ tNSpNQpwyyQRzQGF3PUYSt/1eYh6iMP6y4xFF8DOG54qmcDrGF9XiMEy8vq8cmrdKoel NFKQ== X-Forwarded-Encrypted: i=1; AJvYcCVyUCqM6uu0XcFRu8JkBMEVyZbUnkgZq0hh3jxKuRCiAoOBNlRWLt7yOBD+6e/xo6JD2nbcvQzwHKYMf1Jk7c2vYChGq1hBsTsRBftt X-Gm-Message-State: AOJu0YxX+iT2/tZCDhDQOwpsTuJGld1gP5w5E2MrU1t0FqV/jmaLKYxq mcWsoEE7+MRLkDZ8FaKmgCEaKuguano/cmXbUXd/b/KAd2U2VXOZ X-Google-Smtp-Source: AGHT+IFym5FaXqdk+GTJ9z1pCqU/glVB3OGMwEsuQhtTrrgJsXYCCXvXz/ec5NZwcGWu3bUKU+Lz6Q== X-Received: by 2002:a92:ce8d:0:b0:368:7147:250c with SMTP id r13-20020a92ce8d000000b003687147250cmr1448047ilo.14.1711575457982; Wed, 27 Mar 2024 14:37:37 -0700 (PDT) Received: from nathanxps13 (162-195-168-172.lightspeed.stlsmo.sbcglobal.net. [162.195.168.172]) by smtp.gmail.com with ESMTPSA id a67-20020a029449000000b0047c0b84c260sm2703146jai.106.2024.03.27.14.37.37 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 27 Mar 2024 14:37:37 -0700 (PDT) Date: Wed, 27 Mar 2024 16:37:35 -0500 From: Nathan Bossart To: Tom Lane Cc: John Naylor , Ants Aasma , pgsql-hackers@postgresql.org Subject: Re: add AVX2 support to simd.h Message-ID: <20240327213735.GA95234@nathanxps13> References: <20240325152121.GB2662364@nathanxps13> <20240325213754.GA3094030@nathanxps13> <20240326190904.GA3700674@nathanxps13> <3189995.1711495704@sss.pgh.pa.us> <20240326235554.GA3766883@nathanxps13> <20240327013616.GA3940109@nathanxps13> <3206056.1711504137@sss.pgh.pa.us> <20240327185716.GA4188857@nathanxps13> <3442907.1711573813@sss.pgh.pa.us> MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="r5Pyd7+fXNt84Ff3" Content-Disposition: inline In-Reply-To: <3442907.1711573813@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk --r5Pyd7+fXNt84Ff3 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline On Wed, Mar 27, 2024 at 05:10:13PM -0400, Tom Lane wrote: > Shouldn't "i" be declared uint32, since nelem is? Yes, that's a mistake. > BTW, I wonder why these functions don't declare their array > arguments like "const uint32 *base". They probably should. I don't see any reason not to, and my compiler doesn't complain, either. > LGTM otherwise, and I like the fact that the #if structure > gets a lot less messy. Thanks for reviewing. I've attached a v2 that I intend to commit when I get a chance. -- Nathan Bossart Amazon Web Services: https://aws.amazon.com --r5Pyd7+fXNt84Ff3 Content-Type: text/x-diff; charset=us-ascii Content-Disposition: attachment; filename="v2-0001-improve-style-of-pg_lfind32.patch" From 6f2577779917230bf70284f0ec1186bed17d9b4a Mon Sep 17 00:00:00 2001 From: Nathan Bossart Date: Wed, 27 Mar 2024 13:50:17 -0500 Subject: [PATCH v2 1/1] improve style of pg_lfind32() --- src/include/port/pg_lfind.h | 62 ++++++++++++++++--------------------- 1 file changed, 26 insertions(+), 36 deletions(-) diff --git a/src/include/port/pg_lfind.h b/src/include/port/pg_lfind.h index 33e8471b03..4b1431ed00 100644 --- a/src/include/port/pg_lfind.h +++ b/src/include/port/pg_lfind.h @@ -80,6 +80,24 @@ pg_lfind8_le(uint8 key, uint8 *base, uint32 nelem) return false; } +/* + * pg_lfind32_one_by_one_helper + * + * Searches the array of integers one-by-one. The caller is responsible for + * ensuring that there are at least "nelem" integers in the array. + */ +static inline bool +pg_lfind32_one_by_one_helper(uint32 key, const uint32 *base, uint32 nelem) +{ + for (uint32 i = 0; i < nelem; i++) + { + if (key == base[i]) + return true; + } + + return false; +} + #ifndef USE_NO_SIMD /* * pg_lfind32_simd_helper @@ -88,7 +106,7 @@ pg_lfind8_le(uint8 key, uint8 *base, uint32 nelem) * ensuring that there are at least 4-registers-worth of integers remaining. */ static inline bool -pg_lfind32_simd_helper(const Vector32 keys, uint32 *base) +pg_lfind32_simd_helper(const Vector32 keys, const uint32 *base) { const uint32 nelem_per_vector = sizeof(Vector32) / sizeof(uint32); Vector32 vals1, @@ -132,11 +150,10 @@ pg_lfind32_simd_helper(const Vector32 keys, uint32 *base) * return false. */ static inline bool -pg_lfind32(uint32 key, uint32 *base, uint32 nelem) +pg_lfind32(uint32 key, const uint32 *base, uint32 nelem) { - uint32 i = 0; - #ifndef USE_NO_SIMD + uint32 i = 0; /* * For better instruction-level parallelism, each loop iteration operates @@ -150,25 +167,15 @@ pg_lfind32(uint32 key, uint32 *base, uint32 nelem) const uint32 tail_idx = nelem & ~(nelem_per_iteration - 1); #if defined(USE_ASSERT_CHECKING) - bool assert_result = false; - - /* pre-compute the result for assert checking */ - for (int j = 0; j < nelem; j++) - { - if (key == base[j]) - { - assert_result = true; - break; - } - } + bool assert_result = pg_lfind32_one_by_one_helper(key, base, nelem); #endif /* - * If there aren't enough elements for the SIMD code, jump to the standard + * If there aren't enough elements for the SIMD code, use the standard * one-by-one linear search code. */ if (nelem < nelem_per_iteration) - goto one_by_one; + return pg_lfind32_one_by_one_helper(key, base, nelem); /* * Process as many elements as possible with a block of 4 registers. @@ -193,27 +200,10 @@ pg_lfind32(uint32 key, uint32 *base, uint32 nelem) */ Assert(assert_result == pg_lfind32_simd_helper(keys, &base[nelem - nelem_per_iteration])); return pg_lfind32_simd_helper(keys, &base[nelem - nelem_per_iteration]); - -one_by_one: - -#endif /* ! USE_NO_SIMD */ - +#else /* Process the elements one at a time. */ - for (; i < nelem; i++) - { - if (key == base[i]) - { -#ifndef USE_NO_SIMD - Assert(assert_result == true); + return pg_lfind32_one_by_one_helper(key, base, nelem); #endif - return true; - } - } - -#ifndef USE_NO_SIMD - Assert(assert_result == false); -#endif - return false; } #endif /* PG_LFIND_H */ -- 2.25.1 --r5Pyd7+fXNt84Ff3--