Vector (and integer) API: unsigned min/max

Viswanathan, Sandhya sandhya.viswanathan at intel.com
Fri Apr 19 01:05:29 UTC 2024


Using compare the unsigned max(va, vb) could be written as:

            var va =  IntVector.fromArray(ispecies, ia, i);
            var vb =  IntVector.fromArray(ispecies, ib, i);
            var vm =  va.compare(UNSIGNED_LT, vb);
            va.blend(vb, vm).intoArray(ir, i);

Where ia and ib are input integer arrays and ir is the output integer array.

For x86_64 platforms supporting AVX512 compare method could be faster than the add with offset method. 

Best Regards,
Sandhya


-----Original Message-----
From: Paul Sandoz <paul.sandoz at oracle.com> 
Sent: Thursday, April 18, 2024 11:40 AM
To: David Lloyd <david.lloyd at redhat.com>
Cc: core-libs-dev at openjdk.org; Viswanathan, Sandhya <sandhya.viswanathan at intel.com>
Subject: Re: Vector (and integer) API: unsigned min/max

Hi David,

It’s not at all outlandish, but would caution it's more work than one might initially think.

Could you describe a little more about your use case? that can be helpful to understand the larger picture and demand. Using unsigned comparison would be my recommended approach with the current API. CC'ing Sandhya for her opinion.

Generally when we add new Vector operations we also consider the impact on the scalar types and try to fill any gaps there, so the vector operation behavior is composed from the scalar operation behavior (so like you indicated regarding symmetry). 

We are seeing demand for saturated arithmetic primarily for vector (not “vector” as in hardware vector) search, so we may do something there for specific integral types.

Paul.

> On Apr 17, 2024, at 7:13 AM, David Lloyd <david.lloyd at redhat.com> wrote:
> 
> I've been trying the the incubating Vector API and one thing I've missed on integer-typed vectors is having unsigned min/max operations. There is a signed min/max operation, and unsigned comparison, but no unsigned min/max.
> 
> I guess this is for symmetry with `Math`, which only has signed `min`/`max`. However, I would point out that while it's not very hard to implement one's own unsigned min/max for integer types using `compareUnsigned`, it is a bit harder to do so with vectors. The best I've come up with is to take one of two approaches:
> 
> 1. Zero-extend the vector to the next-larger size, perform the 
> min/max, and reduce it back down again, or 2. Add <IntType>.MIN_VALUE, 
> min/max with a value or vector also offset by <IntType>.MIN_VALUE, and 
> then subtract the offset again
> 
> I guess a third approach could be to do a comparison using unsigned compares, and then use the resultant vector mask to select items from the original two vectors, but I didn't bother to work out this solution given I already had the other two options.
> 
> Would it be feasible to add unsigned min/max operations for vectors? It looks like at least AArch64 has support for this as a single instruction, so it doesn't seem too outlandish.
> 
> And as a separate (but related) question, what about `Math.minUnsigned`/`Math.maxUnsigned` of `int` and `long` for symmetry?
> 
> --
> - DML • he/him



More information about the core-libs-dev mailing list