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 1guegF-0003H9-Eh for pgsql-hackers@arkaria.postgresql.org; Fri, 15 Feb 2019 14:41:31 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1guegE-0008FW-5N for pgsql-hackers@arkaria.postgresql.org; Fri, 15 Feb 2019 14:41:30 +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 1guegD-0008E7-LA for pgsql-hackers@lists.postgresql.org; Fri, 15 Feb 2019 14:41:29 +0000 Received: from mail-qt1-x843.google.com ([2607:f8b0:4864:20::843]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1guegA-00026T-KG for pgsql-hackers@lists.postgresql.org; Fri, 15 Feb 2019 14:41:28 +0000 Received: by mail-qt1-x843.google.com with SMTP id 2so11087555qtb.5 for ; Fri, 15 Feb 2019 06:41:26 -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=QfOIE+HhEreHhW1g2/qQ1PdP2W90sAwHJQHKmQwE2UI=; b=Iq+8VU4Ce6P4rCeSoRfdh2Ud6/R/OJfLy5L6hHEiuzfI6wYYanfWNjFpFRbFs5apvu MopmsDI/K97kDiAr//ZWzoVg7adQpYij5CNOsG0KOGTVvFeZxtrhpnsgcCMscCZ9i8/J BRqTzf0GhTYCigHFkL856G92N2xqefdp3FU+iEFHTBY3YrNRgH1YsoSrLsgAb4xwPnth WiDjVvTMKfEZiVroHSxb5hXhV610E55MnehCjfKEsbv5I2D2+eT8WKhnhzY9TVF3UpWE lMRlxnTtFwgDWfCtqT342jISzokipAbVUCTxZbM6DlvJJCiXJte93Z9fMh4DXLq4QvNe YOVw== 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=QfOIE+HhEreHhW1g2/qQ1PdP2W90sAwHJQHKmQwE2UI=; b=ZCaa5bo0lkFeedPnyzQFd4d7ukS85Gd0pzHzagtU40AfZVkiyAeLFtCyBHoZu8YcaX fKfP9ZQfk7SragEfPtXgVzerjI/bC11VPP+nrDbJ4NSU9TNjoO3RzNhrHCqlkTMUHbG/ tFk6ElNibW7AX9ftK3WrUfcUq1nr0jT7lfd9Wsp3F9FmQ38bzOCrrArKL5tiKZoaaXOt YYNKX9tmOidDABoqDoeBSjeDJaSvX0JX2IVPSYUIZDrDPcajOJK9p6cg/QFlh9hR8e1r 4XM+53YIrDXSJd0uirdNR9NH0pPqxw/zTdMkWLFVjwj/w+k70NVh2+LR04qx8FE1WaBX rtHA== X-Gm-Message-State: AHQUAuZAisRepzKA1a1QX+mX8d3OXwGV91N9R4rdFkssUkgxEmErBB5K iCakBdLXrnFwcIFGle1F7TdXsw== X-Google-Smtp-Source: AHgI3IbDWlCGB5p6zfeT3jLaEWgh4cRtLVTOEe8QU9aGFYSAQgfaEpvEypQqWtYXwNg/5oviVhjpQQ== X-Received: by 2002:ac8:2739:: with SMTP id g54mr8047135qtg.177.1550241685722; Fri, 15 Feb 2019 06:41:25 -0800 (PST) Received: from nimloth.alvh.no-ip.org ([190.121.29.3]) by smtp.gmail.com with ESMTPSA id f36sm3117337qtb.67.2019.02.15.06.41.25 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Fri, 15 Feb 2019 06:41:25 -0800 (PST) Received: by nimloth.alvh.no-ip.org (Postfix, from userid 1000) id 52BCE122611; Fri, 15 Feb 2019 11:41:23 -0300 (-03) Date: Fri, 15 Feb 2019 11:41:23 -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: <20190215144123.GA31671@alvherre.pgsql> MIME-Version: 1.0 Content-Type: text/plain; charset=iso-8859-1 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: <20190215142842.GA29812@alvherre.pgsql> 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, Alvaro Herrera wrote: > On 2019-Feb-15, Kyotaro HORIGUCHI wrote: > > 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. Ah, I understand it now: https://stackoverflow.com/questions/25683690/confusion-about-bsr-and-lzcnt/43443701#43443701 if you call LZCNT/TZCNT on a CPU that doesn't support it, it won't raise SIGILL or anything ... it'll just silently compute the wrong result. That's certainly not what I call a fallback! I think David's code was correct because it was testing CPUID for instruction support before using the specialized code (well, except that he forgot to add the right compiler option to *enable* the LZCNT/TZCNT instructions in the first place); but given subsequent discussion that the instruction is not worth it anyway, we might as well ignore it. -- Álvaro Herrera https://www.2ndQuadrant.com/ PostgreSQL Development, 24x7 Support, Remote DBA, Training & Services