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 1rpYSk-007Jng-GU for pgsql-hackers@arkaria.postgresql.org; Wed, 27 Mar 2024 18:57:27 +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 1rpYSi-001KFq-FX for pgsql-hackers@arkaria.postgresql.org; Wed, 27 Mar 2024 18:57:24 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1rpYSi-001KFi-60 for pgsql-hackers@lists.postgresql.org; Wed, 27 Mar 2024 18:57:24 +0000 Received: from mail-il1-x133.google.com ([2607:f8b0:4864:20::133]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.94.2) (envelope-from ) id 1rpYSe-006pwd-Vg for pgsql-hackers@postgresql.org; Wed, 27 Mar 2024 18:57:23 +0000 Received: by mail-il1-x133.google.com with SMTP id e9e14a558f8ab-3678908266dso374385ab.0 for ; Wed, 27 Mar 2024 11:57:20 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1711565839; x=1712170639; 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=1QRROEMqEc+erqZMeBMK0AZ1TvppvXKnlv/xzobbgOg=; b=NVa7NRHMEDtS6cpZ2ERvFdbtCbANS6BUHaoHtTsb8gt/F0ddv8li4yI2dIaC5ZHDjL hUNeiO4D/m6xZUmoYTUn6hnJ/Qtuu1DlNdxaqgWJMaa/plJnxZm66msjfeJBJnZQwrIG NBrXoL/yOqj3/eFUWZLcf00Z8P8skVkEVlXlX/6p/AwHAYdKyvpWuOgVf0Vcbr4NJCS8 81hXZFYGqPioyBdzmUz9IK195rd6lU/YA3740GuHsgyO0pflWhYBg4p6STcC3EwEL1Lj zmgLQ8Eg+sqj5CrukGe1SqkzNrXp6wJ+xTn8aXBpvaIrP+esI061dS5ogie+argmQklO gVOw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1711565839; x=1712170639; 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=1QRROEMqEc+erqZMeBMK0AZ1TvppvXKnlv/xzobbgOg=; b=La8MFUAJ7mdQQLu1DzsqhlJ9Ifkb+Gi/gXFUNFHXLiWqCSLl935SG7wQRSWt68gmbW NQHkbWxmcufp+EKh0EkK7zKmoCtigQ3Alv2WF9fscscTOzp5jrdKvLjs9S9c4HRBetw6 L+xKdiy1cStQM+xLaYY2D1D5TGzL3yncqMyJFXU2xmv93/ZM/It7s898RO++MS3iyiXj DaocyCytEe0CJA09ITjjQ/srgq5Bh0wUN/WqmsFxLhHn9TeLado7kK8VH+x/oDZjdYv4 mnctvyXj+yY5kHwAxOt16Qk5aE536Cjx+8/99e64t+AnnT5De1Pd+GyX2A6u75+68CVg oArA== X-Forwarded-Encrypted: i=1; AJvYcCXdsquNpAAc6aG4LwJgod2gddVaE8Sg+c76n8ACgjYY/NL8iX0FXlB4V69xwiav496OzdqTIIhgBf7iBJzKL5478aMfXv2Lx2b1hvfv X-Gm-Message-State: AOJu0Yy2/TbDHe6umimw0ZJkVTo3CGDlnjhEfdVEL340m58tMGU5739J djsICKgUkRS3HyHQS40EtZWzhhlO3NrqqKy4FMsGDT3abcIZ9M8d X-Google-Smtp-Source: AGHT+IF9sFKOqhcWELMa/YJz2wnh+lqwhltFI705SlCRRx2kKnKlvUc+wFHBDjFcVPJbEynB5JARgg== X-Received: by 2002:a05:6e02:1c86:b0:368:8d92:3253 with SMTP id w6-20020a056e021c8600b003688d923253mr1040276ill.23.1711565839191; Wed, 27 Mar 2024 11:57:19 -0700 (PDT) Received: from nathanxps13 (162-195-168-172.lightspeed.stlsmo.sbcglobal.net. [162.195.168.172]) by smtp.gmail.com with ESMTPSA id 11-20020a92c64b000000b00366acd12487sm3681234ill.80.2024.03.27.11.57.17 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 27 Mar 2024 11:57:17 -0700 (PDT) Date: Wed, 27 Mar 2024 13:57:16 -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: <20240327185716.GA4188857@nathanxps13> References: <20240321170944.GA1767527@nathanxps13> <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> MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="huq684BweRXVnRxX" Content-Disposition: inline In-Reply-To: <3206056.1711504137@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk --huq684BweRXVnRxX Content-Type: text/plain; charset=us-ascii Content-Disposition: inline On Tue, Mar 26, 2024 at 09:48:57PM -0400, Tom Lane wrote: > Nathan Bossart writes: >> I just did the minimal fix for now, i.e., I moved the new label into the >> SIMD section of the function. I think it would be better stylistically to >> move the one-by-one logic to an inline helper function, but I didn't do >> that just in case it might negatively impact performance. I'll look into >> this and will follow up with another patch if it looks good. > > Sounds like a plan. Here's what I had in mind. My usual benchmark seems to indicate that this shouldn't impact performance. -- Nathan Bossart Amazon Web Services: https://aws.amazon.com --huq684BweRXVnRxX Content-Type: text/x-diff; charset=us-ascii Content-Disposition: attachment; filename="v1-0001-improve-style-of-pg_lfind32.patch" From c3f163753246c5ec82dd8c5dba70232cbeebbf2a Mon Sep 17 00:00:00 2001 From: Nathan Bossart Date: Wed, 27 Mar 2024 13:50:17 -0500 Subject: [PATCH v1 1/1] improve style of pg_lfind32() --- src/include/port/pg_lfind.h | 58 +++++++++++++++---------------------- 1 file changed, 24 insertions(+), 34 deletions(-) diff --git a/src/include/port/pg_lfind.h b/src/include/port/pg_lfind.h index 33e8471b03..5b76cc8937 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, uint32 *base, uint32 nelem) +{ + for (int i = 0; i < nelem; i++) + { + if (key == base[i]) + return true; + } + + return false; +} + #ifndef USE_NO_SIMD /* * pg_lfind32_simd_helper @@ -134,9 +152,8 @@ pg_lfind32_simd_helper(const Vector32 keys, uint32 *base) static inline bool pg_lfind32(uint32 key, 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 --huq684BweRXVnRxX--