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 1roUqL-001pQS-GO for pgsql-hackers@arkaria.postgresql.org; Sun, 24 Mar 2024 20:53:26 +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 1roUqK-00FOjU-3P for pgsql-hackers@arkaria.postgresql.org; Sun, 24 Mar 2024 20:53: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 1roUqJ-00FOjM-Ob for pgsql-hackers@lists.postgresql.org; Sun, 24 Mar 2024 20:53:24 +0000 Received: from mail-il1-x12e.google.com ([2607:f8b0:4864:20::12e]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.94.2) (envelope-from ) id 1roUqH-006Hoa-7V for pgsql-hackers@postgresql.org; Sun, 24 Mar 2024 20:53:23 +0000 Received: by mail-il1-x12e.google.com with SMTP id e9e14a558f8ab-3684bee9ddfso18130695ab.1 for ; Sun, 24 Mar 2024 13:53:21 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1711313599; x=1711918399; 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=U3uIzYwd1gxP6uG2yuDes52JZfBfXCPVA1Yksqyg6lE=; b=bA6s/fcII4I1hdmmJrpMueslExLMjkMSfAm+GwmSY/i6a5QKSGi6vJcIzFXjl9yoa/ bzpzuxgaFde1CrW1NnGcK9o+jn7LFWbfAQVeAy7mKTOOXs414qLWdeJ43kMWh9RK+elv u5DDeKYBlwSCY5WphBjvXFd1eaYZArKe/K1nZOGke5vpAqZ4BBIprpO+r/PcDXuGIkuv opU5k1bamV2D0DuNG4Aq/+5FD+Rc4ixmuV2qMmhVmLLjRIAuV3NvfYsF8ALflo9XF6LF xtGSzlunRKGqzGJTJ6ZcfkJrCgnZyVHJKZC2rCsYxFh5sd2k5aIy9uyJbt1iYiDCc/o/ sswQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1711313599; x=1711918399; 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=U3uIzYwd1gxP6uG2yuDes52JZfBfXCPVA1Yksqyg6lE=; b=Q+lqCXfwI3+FT0N3LOWEdV8uaqTv4qojOSWWWuJdIVe4w+DLYrSv9cw1gnfEHLYLF7 7uXtnOfCspcwxNyhG8QGKTBOyhszwwbik1lKe+md64pr4uSOj+4Fn/5rR8iXpljIlT3D jBOZFoE3uVDAeUAwwuE7FoN943uKXzc0+stDJY8ys9eT3ZUvDt0pKh0hOO4lE2OPVkHV CCWk0ErBaGxDwHo5pHa5/99b19QRKc0ykkR0oK+yMTZGzuKfiWhvjjs9OJyUvQQnkMmx hXu+6zcus/a9ZAEB+yrhio8EVe34rBVCCTJ3v8DqXuAa1CwN7uOqZYVTDrtvOvptIknu W5Sw== X-Forwarded-Encrypted: i=1; AJvYcCVdGBO9QNNHYK4uF0LYi13Cz3IEkmVHQWpn3zNuCleipkELtO5mLyfbUZJwqYtJbvfo/lfC+Ze8mG55qQoT2UTZ1AQGCRPWd6IeCxnY X-Gm-Message-State: AOJu0Yy6hbB4J9mufOwy4qT3Sq5fExyh9VKpnqEr1IzQFk37y92StVH/ W++D1LWFyqmq2eraLXohDJKDQq93+OP0uEzVQZsRkx9sF1ialBvn X-Google-Smtp-Source: AGHT+IF8PG/WaLDP+QXQag8FpiVRBhLq/NsTtg0lKz7zWVImUMy3RzXOYyi3OUd6HdWrsIck2iYJ3w== X-Received: by 2002:a6b:6c16:0:b0:7cc:fbd:270f with SMTP id a22-20020a6b6c16000000b007cc0fbd270fmr5576050ioh.6.1711313599382; Sun, 24 Mar 2024 13:53: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 p7-20020a5d8d07000000b007cc0dcc4994sm1951032ioj.14.2024.03.24.13.53.18 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 24 Mar 2024 13:53:18 -0700 (PDT) Date: Sun, 24 Mar 2024 15:53:17 -0500 From: Nathan Bossart To: John Naylor Cc: Ants Aasma , pgsql-hackers@postgresql.org Subject: Re: add AVX2 support to simd.h Message-ID: <20240324205317.GA2558640@nathanxps13> References: <20240319031601.GA828141@nathanxps13> <20240319163033.GA952199@nathanxps13> <20240320143116.GA1313857@nathanxps13> <20240320195513.GA1618413@nathanxps13> <20240321170944.GA1767527@nathanxps13> <20240321183823.GA1800896@nathanxps13> MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="sm4nu43k4a2Rpi4c" Content-Disposition: inline In-Reply-To: <20240321183823.GA1800896@nathanxps13> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk --sm4nu43k4a2Rpi4c Content-Type: text/plain; charset=us-ascii Content-Disposition: inline Here's a new version of 0001 with some added #ifdefs that cfbot revealed were missing. -- Nathan Bossart Amazon Web Services: https://aws.amazon.com --sm4nu43k4a2Rpi4c Content-Type: text/x-diff; charset=us-ascii Content-Disposition: attachment; filename="v7-0001-pg_lfind32-add-overlap-code-for-remaining-element.patch" From cc2bc5ca5b49cd8641af8b2231a34a1aa5091bb9 Mon Sep 17 00:00:00 2001 From: Nathan Bossart Date: Wed, 20 Mar 2024 14:20:24 -0500 Subject: [PATCH v7 1/1] pg_lfind32(): add "overlap" code for remaining elements --- src/include/port/pg_lfind.h | 105 ++++++++++++++++++++++++------------ 1 file changed, 72 insertions(+), 33 deletions(-) diff --git a/src/include/port/pg_lfind.h b/src/include/port/pg_lfind.h index b8dfa66eef..5830cc7cb3 100644 --- a/src/include/port/pg_lfind.h +++ b/src/include/port/pg_lfind.h @@ -80,6 +80,51 @@ pg_lfind8_le(uint8 key, uint8 *base, uint32 nelem) return false; } +#ifndef USE_NO_SIMD +/* + * pg_lfind32_helper + * + * Searches one 4-register-block of integers. The caller is responsible for + * ensuring that there are at least 4-registers-worth of integers remaining. + */ +static inline bool +pg_lfind32_helper(const Vector32 keys, uint32 *base) +{ + const uint32 nelem_per_vector = sizeof(Vector32) / sizeof(uint32); + Vector32 vals1, + vals2, + vals3, + vals4, + result1, + result2, + result3, + result4, + tmp1, + tmp2, + result; + + /* load the next block into 4 registers */ + vector32_load(&vals1, base); + vector32_load(&vals2, &base[nelem_per_vector]); + vector32_load(&vals3, &base[nelem_per_vector * 2]); + vector32_load(&vals4, &base[nelem_per_vector * 3]); + + /* compare each value to the key */ + result1 = vector32_eq(keys, vals1); + result2 = vector32_eq(keys, vals2); + result3 = vector32_eq(keys, vals3); + result4 = vector32_eq(keys, vals4); + + /* combine the results into a single variable */ + tmp1 = vector32_or(result1, result2); + tmp2 = vector32_or(result3, result4); + result = vector32_or(tmp1, tmp2); + + /* return whether there was a match */ + return vector32_is_highbit_set(result); +} +#endif /* ! USE_NO_SIMD */ + /* * pg_lfind32 * @@ -119,46 +164,40 @@ pg_lfind32(uint32 key, uint32 *base, uint32 nelem) } #endif - for (i = 0; i < tail_idx; i += nelem_per_iteration) + /* + * If there aren't enough elements for the SIMD code, jump to the standard + * one-by-one linear search code. + */ + if (nelem <= nelem_per_iteration) + goto one_by_one; + + /* + * Process as many elements as possible with a block of 4 registers. + */ + do { - Vector32 vals1, - vals2, - vals3, - vals4, - result1, - result2, - result3, - result4, - tmp1, - tmp2, - result; - - /* load the next block into 4 registers */ - vector32_load(&vals1, &base[i]); - vector32_load(&vals2, &base[i + nelem_per_vector]); - vector32_load(&vals3, &base[i + nelem_per_vector * 2]); - vector32_load(&vals4, &base[i + nelem_per_vector * 3]); - - /* compare each value to the key */ - result1 = vector32_eq(keys, vals1); - result2 = vector32_eq(keys, vals2); - result3 = vector32_eq(keys, vals3); - result4 = vector32_eq(keys, vals4); - - /* combine the results into a single variable */ - tmp1 = vector32_or(result1, result2); - tmp2 = vector32_or(result3, result4); - result = vector32_or(tmp1, tmp2); - - /* see if there was a match */ - if (vector32_is_highbit_set(result)) + if (pg_lfind32_helper(keys, &base[i])) { Assert(assert_result == true); return true; } - } + + i += nelem_per_iteration; + + } while (i < tail_idx); + + /* + * Process the last 'nelem_per_iteration' elements in the array with a + * 4-register block. This will cause us to check some of the elements + * more than once, but that won't affect correctness, and testing has + * demonstrated that this helps more cases than it harms. + */ + Assert(assert_result == pg_lfind32_helper(keys, &base[nelem - nelem_per_iteration])); + return pg_lfind32_helper(keys, &base[nelem - nelem_per_iteration]); + #endif /* ! USE_NO_SIMD */ +one_by_one: /* Process the remaining elements one at a time. */ for (; i < nelem; i++) { -- 2.25.1 --sm4nu43k4a2Rpi4c--