Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1gueTy-0002W9-Vw for pgsql-hackers@arkaria.postgresql.org; Fri, 15 Feb 2019 14:28:51 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1gueTx-0008WH-FW for pgsql-hackers@arkaria.postgresql.org; Fri, 15 Feb 2019 14:28:49 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1gueTx-0008UH-5G for pgsql-hackers@lists.postgresql.org; Fri, 15 Feb 2019 14:28:49 +0000 Received: from mail-qt1-x841.google.com ([2607:f8b0:4864:20::841]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1gueTu-0001TR-5J for pgsql-hackers@lists.postgresql.org; Fri, 15 Feb 2019 14:28:48 +0000 Received: by mail-qt1-x841.google.com with SMTP id n32so10978211qte.11 for ; Fri, 15 Feb 2019 06:28:46 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=2ndquadrant-com.20150623.gappssmtp.com; s=20150623; h=date:from:to:cc:subject:message-id:mime-version:content-disposition :content-transfer-encoding:in-reply-to:user-agent; bh=4WgeVRCE/r0Ilb2OmG4QntD3lvHn2FVi9zmXkryPebY=; b=Ey+45YV+DnPS9smbK8bI4mLTORiaQBWjiGTvlLSpjeumuUF4iGpoKB4oydyDY+BDro bZjcXhsDasNfBsXjtvDv+5K7hENZNAtK6DeV2j5KnMvvRUv/rQPrsSrQj1QBOUl3viaK oc+3kwFiZo0qt4m8JcOMhnP+nCCCqJCIXzWeiVcnf6Xo7DgCmwY8bU/7NF4AszIGhuet xRAyEU3AzLCZyJT2hRZpikA1l706BltQ4/2M3w/rWp2wf4IZojxPb+SRAMyFsZPZ8bTM +z5szcP+SPynVMkEGPz3DEqv6osOOxZVb6IoSDp3ezk/jIIONakSYiUXyqIz0omQCdlF F25A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:cc:subject:message-id:mime-version :content-disposition:content-transfer-encoding:in-reply-to :user-agent; bh=4WgeVRCE/r0Ilb2OmG4QntD3lvHn2FVi9zmXkryPebY=; b=k2c7YbyilL/v/Pid7Ucso9Mc/zYEVzVGAnt9k6OdxKg1vPe4FCnQOY2WFQ8biXWEjK TG9XX+ZNdZ7RbLmOKjkgQtKAOeQAXEd9COnC6uaUNKufHhe0G6n/EM5AHjfs9ZiY9TCR YcRtm+d9E3A+xL2yv9yRzRPDQpYIvZX7vKyDh14hQBetYqy8eDqAbVIPxv1kJ+x57YWR FlzWIJCQlNXD6uIqmfUyPAlsT/aqbj081LheO2fbu2EBQ0bA8Zwc0ZKL6TzcrdLG1J/u Gh5EvfMQ98QU70WaxzoOx+VCHdPa3T9Mb9eSJhfn2zzwgwfUXE8ldfW6IjjBfjzQf1aE 28Cw== X-Gm-Message-State: AHQUAub4wiUFfHG/dQUib2mmDGpZaoHk3Rd7QVbNdm0ZANRZgRvaBMuT fZ2mdSLmGzQbepaQiWWHapbrZQ== X-Google-Smtp-Source: AHgI3IayZEi/R0/CDSwIxnWlOWTOfUKJ9mSPpqR8aXuikkRpvvlElU29QOIfvV8zdNWkJUGG0uyCDg== X-Received: by 2002:a0c:8afa:: with SMTP id 55mr7606953qvw.73.1550240925370; Fri, 15 Feb 2019 06:28:45 -0800 (PST) Received: from nimloth.alvh.no-ip.org ([190.121.29.3]) by smtp.gmail.com with ESMTPSA id p63sm2924203qkd.52.2019.02.15.06.28.44 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Fri, 15 Feb 2019 06:28:44 -0800 (PST) Received: by nimloth.alvh.no-ip.org (Postfix, from userid 1000) id 62F3D122611; Fri, 15 Feb 2019 11:28:42 -0300 (-03) Date: Fri, 15 Feb 2019 11:28:42 -0300 From: Alvaro Herrera To: Kyotaro HORIGUCHI Cc: tgl@sss.pgh.pa.us, andres@anarazel.de, thomas.munro@enterprisedb.com, andrew@tao11.riddles.org.uk, david.rowley@2ndquadrant.com, pgsql-hackers@lists.postgresql.org Subject: Re: Using POPCNT and other advanced bit manipulation instructions Message-ID: <20190215142842.GA29812@alvherre.pgsql> MIME-Version: 1.0 Content-Type: text/plain; charset=iso-8859-1 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: <20190215.104601.261252390.horiguchi.kyotaro@lab.ntt.co.jp> User-Agent: Mutt/1.9.4 (2018-02-28) List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk On 2019-Feb-15, Kyotaro HORIGUCHI wrote: > I understand that the behavior of __builtin_c[tl]z(0) is > undefined from the reason, they convert to bs[rf]. So if we use > these builtins, additional check is required. > > https://gcc.gnu.org/onlinedocs/gcc/Other-Builtins.html Okay -- the functions check for a 0 argument: +static inline int +pg_rightmost_one32(uint32 word) +{ + int result = 0; + + Assert(word != 0); + +#ifdef HAVE__BUILTIN_CTZ + result = __builtin_ctz(word); +#else + while ((word & 255) == 0) + { + word >>= 8; + result += 8; + } + result += rightmost_one_pos[word & 255]; +#endif /* HAVE__BUILTIN_CTZ */ + + return result; +} so we're fine. > And even worse lzcntx is accidentially "fallback"s to bsrx on > unsupported CPUs, which leads to bogus results. > __builtin_clzll(8) = 3, which should be 60. I'm not sure I understand this point. Are you saying that if we use -mlzcnt to compile, then the compiler builtin is broken in platforms that don't support the lzcnt instruction? That's horrible. Let's stay away from -mlzcnt then. -- Álvaro Herrera https://www.2ndQuadrant.com/ PostgreSQL Development, 24x7 Support, Remote DBA, Training & Services