Path: utzoo!attcan!uunet!husc6!mailrus!ncar!tank!uxc!uxc.cso.uiuc.edu!urbsdc!aglew From: aglew@urbsdc.Urbana.Gould.COM Newsgroups: comp.arch Subject: Re: Leading zero and pop count Message-ID: <28200233@urbsdc> Date: 5 Nov 88 22:15:00 GMT References: <10882@hall.cray.com> Lines: 35 Nf-ID: #R:hall.cray.com:10882:urbsdc:28200233:000:1351 Nf-From: urbsdc.Urbana.Gould.COM!aglew Nov 5 16:15:00 1988 >This is the "leading zero count" instruction common to Cray instruction sets. >The CFT77 compiler provides an intrinsic LEADZ function to get at this >instruction. The Cray-2 also has a vector version. > >The leading zero count and population count (add up the 1 bits) instructions are >fertile ground for some nifty bit-twisting tricks. For example, the code >sequence I generate for the C logical negation operator ("!") is: > > S1 ZS0 ; take leading zero count of argument > S1 S1>6 ; shift right six bits > >This turns 0 into 1 and everything else into 0 on a 64-bit machine. > >Population count can be handy when applied to vector masks. >After constructing an iota vector from a mask, the pop count gives you >its length. > >I've never seen these useful operations in another architecture, though. >Have you? I'd be interested in hearing about it. I'm ignorant of HW design, >but they can't be too hard to implement. > >-Peter Klausler / Cray Research compiler development / Mendota Heights, MN Gould had the SACZ (shift and count zeroes) instruction, which actually shifted on early models, but decoded in one cycle on high end models. MC680x0s have BFF0 (bit find first 0) (I always get the mnemonic for this wrong). Many other machines have similar. POPulation count instructions are found in most machines that have had NSA interest.