RE: Vector (and integer) API: unsigned min/max

2024-04-18 Thread Viswanathan, Sandhya
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  
Sent: Thursday, April 18, 2024 11:40 AM
To: David Lloyd 
Cc: core-libs-dev@openjdk.org; Viswanathan, Sandhya 

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  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 .MIN_VALUE, 
> min/max with a value or vector also offset by .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



Re: Vector (and integer) API: unsigned min/max

2024-04-18 Thread John Rose
On Apr 17, 2024, at 7:14 AM, David Lloyd  wrote:
> 
> 2. Add .MIN_VALUE, min/max with a value or vector also offset by 
> .MIN_VALUE, and then subtract the offset again

I think that’s the path of least resistance. It’s just a vxor on each operand, 
with a constant mask. That can be done in Java code. CPU’s that implement 
native unsigned cmp can peephole optimize. 

Re: Vector (and integer) API: unsigned min/max

2024-04-18 Thread David Lloyd
Presently, I'm implementing operations for a WASM emulator/runtime, so it's
more a case of transitively acquiring use cases from WASM, though I'm also
doing this specifically to practice accomplishing useful things with the
vector API.

Regarding saturating arithmetic: WASM has operations for that as well (both
signed and unsigned), so on that basis alone it would be a welcome addition
as far as I'm concerned. FWIW, another simple use case for saturating
arithmetic is audio processing/mixing (when processing many channels at
once) because saturation closely matches analog clipping, and also perhaps
image compositing for similar reasons.

On Thu, Apr 18, 2024 at 1:40 PM Paul Sandoz  wrote:

> 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  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 .MIN_VALUE, min/max with a value or vector also offset
> by .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
>
>

-- 
- DML • he/him


Re: Vector (and integer) API: unsigned min/max

2024-04-18 Thread Paul Sandoz
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  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 .MIN_VALUE, min/max with a value or vector also offset by 
> .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



Vector (and integer) API: unsigned min/max

2024-04-17 Thread David Lloyd
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 .MIN_VALUE, min/max with a value or vector also offset by
.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