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 1rrj3c-002JPA-8m for pgsql-hackers@arkaria.postgresql.org; Tue, 02 Apr 2024 18:40:28 +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 1rrj3b-00ArtZ-7t for pgsql-hackers@arkaria.postgresql.org; Tue, 02 Apr 2024 18:40:27 +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 1rrj3a-00ArtQ-Uy for pgsql-hackers@lists.postgresql.org; Tue, 02 Apr 2024 18:40:26 +0000 Received: from mail-il1-x129.google.com ([2607:f8b0:4864:20::129]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.94.2) (envelope-from ) id 1rrj3Y-000EPn-FB for pgsql-hackers@lists.postgresql.org; Tue, 02 Apr 2024 18:40:25 +0000 Received: by mail-il1-x129.google.com with SMTP id e9e14a558f8ab-368954f98deso25040805ab.2 for ; Tue, 02 Apr 2024 11:40:24 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1712083224; x=1712688024; darn=lists.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=hE6oh/aaMePwBJ52OU+IKQBs6xWNKzcc7mPrwsiVBDg=; b=fS+HxgqgFWsT+fIT+POIuxawgJwNBO2TLerw9oUyD5y+/CbQPhMRcaBvQ2lBtT0ZRg dNJKkSQssW0eYbmlrqTb9vhRMdUzRfRa99wB+L7HHZnMgInIvoo+wm3DpTwEoLCPIXQb H4MEfwI+h9gI9qqhjBnNvY/b3youThso4l6XKrDtiUOvAhDM6+CTauui3KQf4TV9Qn4A fTi9q6h1nKqLxv4T1+pyoWU5Y6PUyeC+PuGsB2cmVJVhp+Tn+H4cLFKwMrPc4NWU4lo6 qwEdlJ/vpO2l4wDRL9qJpPLNTl1mp6+JQhOWDTJQvkvR+mrHpwNfpRBD38QUnSbKJ772 01jQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1712083224; x=1712688024; 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=hE6oh/aaMePwBJ52OU+IKQBs6xWNKzcc7mPrwsiVBDg=; b=KyMCKvDrb/HX+g7Qce5ZLrClzKiFmjRtuQeRge954BsjrFjByhx/S4vrCBcWUMOmfx +4shM2oVK/CsateR8GZVsyva+7QcdRKNzT0SyhOH6QemNJkPDcKORJ2KVUhWxTgOGEhO zaMUU78AQw5sN6hgJ0jKInlcHRjm3qVB1WfR9djcqnViT3xLVXG/KVMCMZ0f+TkiOrcP YZJuCREX6zOnrvacyV8yrasGctSLwGRmF9QuFkmm8Mdr9Ur/Opkb0dpo37V72I6xOa0a 4vH17Y2G2WSU2v+/94UR1lHQ1ME8icNqGatx5FIrzxEIExgcgx2xpM+pCVZYI4pTDKBw Of+g== X-Forwarded-Encrypted: i=1; AJvYcCVtFFyUUJdOXOm/2liAd52FAs5k5jRdRoVhsVaVKhcdcw7YI1N5JZ0Y76PfUL3p6S4LlOsIF3kHf9RvbZREP6Hjd3xPImLKyxzvRXzzMiu45MrU X-Gm-Message-State: AOJu0Yx/UxBOgWMaiHyNFNBAqNSzQ89PLL8Ft5eELoWMz8wzzIQ4LNp6 nHlwHy9qU8nPm/nwek+nmobBvCVIhxQgFB8QR9CkuBJZIojDHtdz X-Google-Smtp-Source: AGHT+IE5uuXIbbUyATlY1AvtSpy/ES8GGZzH64V0ZaK1qgUkTmvM42QVlww+Ex90PWnCskL2riaKag== X-Received: by 2002:a05:6e02:198a:b0:368:4c5e:7ae with SMTP id g10-20020a056e02198a00b003684c5e07aemr18103006ilf.7.1712083223767; Tue, 02 Apr 2024 11:40:23 -0700 (PDT) Received: from nathanxps13 ([63.78.151.2]) by smtp.gmail.com with ESMTPSA id du8-20020a056638604800b0047eeae60fa8sm2000635jab.89.2024.04.02.11.40.23 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 02 Apr 2024 11:40:23 -0700 (PDT) Date: Tue, 2 Apr 2024 13:40:21 -0500 From: Nathan Bossart To: Tom Lane Cc: Alvaro Herrera , Ants Aasma , "Amonson, Paul D" , David Rowley , Andres Freund , "Shankaran, Akash" , Noah Misch , Matthias van de Meent , "pgsql-hackers@lists.postgresql.org" Subject: Re: Popcount optimization using AVX512 Message-ID: <20240402184021.GA2802541@nathanxps13> References: <202404021734.635tuluozzn3@alvherre.pgsql> <1666732.1712079828@sss.pgh.pa.us> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <1666732.1712079828@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On Tue, Apr 02, 2024 at 01:43:48PM -0400, Tom Lane wrote: > Alvaro Herrera writes: >> On 2024-Apr-02, Nathan Bossart wrote: >>> Another idea I had is to turn pg_popcount() into a macro that just uses the >>> pg_number_of_ones array when called for few bytes: >>> >>> static inline uint64 >>> pg_popcount_inline(const char *buf, int bytes) >>> { >>> uint64 popcnt = 0; >>> >>> while (bytes--) >>> popcnt += pg_number_of_ones[(unsigned char) *buf++]; >>> >>> return popcnt; >>> } >>> >>> #define pg_popcount(buf, bytes) \ >>> ((bytes < 64) ? \ >>> pg_popcount_inline(buf, bytes) : \ >>> pg_popcount_optimized(buf, bytes)) >>> >>> But again, I'm not sure this is really worth it for the current use-cases. > >> Eh, that seems simple enough, and then you can forget about that case. > > I don't like the double evaluation of the macro argument. Seems like > you could get the same results more safely with > > static inline uint64 > pg_popcount(const char *buf, int bytes) > { > if (bytes < 64) > { > uint64 popcnt = 0; > > while (bytes--) > popcnt += pg_number_of_ones[(unsigned char) *buf++]; > > return popcnt; > } > return pg_popcount_optimized(buf, bytes); > } Yeah, I like that better. I'll do some testing to see what the threshold really should be before posting an actual patch. -- Nathan Bossart Amazon Web Services: https://aws.amazon.com