VectorMask.firstTrue()/lastTrue()

Paul Sandoz paul.sandoz at oracle.com
Thu Apr 15 16:40:55 UTC 2021



> On Apr 15, 2021, at 4:21 AM, Vladimir Ivanov <vladimir.x.ivanov at oracle.com> wrote:
> 
> 
> 
>> I would like to add intrinsic operations for counting leading and trailing zeros (in addition to popcount), then use those operations for implementing first/lastTrue.
>> firstTrue == count-leading-zeros
>> lastTrue == length - count-trailing-zeros - 1
>> The above should help answer your question about the return value. But it does make me wonder if the result for "not found" is non-obvious and we should at least clarify in the Javadoc. The method names likely do not help in this regard.
> 
> In order to rely on leading/trailing zeroes, you have to convert the mask in canonical form (bit per element) first.
> 
> There's already VectorMask.toLong() present which has to be intrinsified first. Then you can rely on existing intrinsics Long.numberOfTrailingZeros()/numberOfTrailingZeros().
> 
> (Also, VectorMask.toLong() is not SVE-friendly where a predicate register can hold a value up to 256-bit in size.)
> 

Right, I am hesitant to rely on an intrinsified VectorMask.toLong() because of the potential size limitation. Better to add internal bit level reduction operations? e.g. supported by VectorSupport.reductionCoerced?

Paul.

> Best regards,
> Vladimir Ivanov
> 
>>> On Apr 14, 2021, at 5:35 AM, Remi Forax <forax at univ-mlv.fr> wrote:
>>> 
>>> There is a way to implement firstTrue and lastTrue using only vector operations.
>>> 
>>> The idea is to use to mask iota with the mask and then do a reduceLanes with MIN or MAX.
>>> By example to for firstTrue() that returns -1 if the mask is empty,
>>> first create iotaPlusOne, blend with zero, then reduceLanes with MIN, then substract 1
>>> 
>>>  VectorMask mask = ...
>>>  var iotaPlusOne = ByteVector.broadcast(SPECIES, 1).addIndex(1);
>>>  ByteVector.zero(SPECIES).blend(iotaPlusOne, mask).reduceLanes(VectorOperators.MIN) - 1;
>>> 
>>> BTW, i wonder why firstTrue return the mask length instead of -1.
>>> 
>>> regards,
>>> Rémi



More information about the panama-dev mailing list