Skip to content
This repository has been archived by the owner on Jan 26, 2022. It is now read-only.

Unsigned types don't have "neg" #310

Open
bnjbvr opened this issue Dec 23, 2015 · 10 comments
Open

Unsigned types don't have "neg" #310

bnjbvr opened this issue Dec 23, 2015 · 10 comments

Comments

@bnjbvr
Copy link
Contributor

bnjbvr commented Dec 23, 2015

The spec says that all integral types (signed and unsigned) should implement neg, although the polyfill doesn't have them. Is it that the polyfill is missing them, or is it that the spec shouldn't define them for unsigned types?

As noted by Jakob Olesen in https://bugzilla.mozilla.org/show_bug.cgi?id=1233111#c17 , they can be implemented with modulo arithmetic.

@stoklund
Copy link

I would recommend keeping unsigned neg in the spec and adding it to the polyfill. It is the same for all integer types: neg(x) = sub(splat(0), x).

@littledan
Copy link
Member

Let's get rid of neg from unsigned types. It was a typo on my part to include them; I don't think it was anyone's intention.

@stoklund
Copy link

Currently, the signed and unsigned integer types support the same set of operations. This is a desirable property of a compilation target.

Would it make sense to remove neg from the signed integer types too? I don't think it provides any benefit. It is more important to have a floating point neg.

@stoklund
Copy link

I did a quick survey of programming languages that provide fixed-size unsigned integer types:

@juj
Copy link

juj commented Dec 23, 2015

ARM Neon has a hardware negate instruction for the 64-bit types int8x8_t, int16x4_t, int32x2_t and float32x2_t, and the 128-bit types int8x16_t, int16x8_t, int32x4_t and float32x4_t. I'd favor keeping the negate instruction on signed and floating point types, since that can map directly to this ARM Neon instruction (even though x86 SSE doesn't have one) and be more expressive that way.

@jfbastien
Copy link

This is pretty much a continuation of #191: SIMD really expresses operations, some of which are signed / unsigned and some of which are sign-agnostic. The type system is expressed in terms of signed / unsigned, and it sometimes maps weirdly.

IMO in this case neg should either:

  1. Exist for both signed and unsigned. On unsigned should just do the same as neg does on the same bit-represented signed.
  2. As @stoklund suggested drop neg from signed and unsigned, keep it only for floating-point.

@littledan
Copy link
Member

OK, I'm convinced, let's keep neg for unsigned and fix the polyfill and tests.

@oli-obk
Copy link

oli-obk commented Dec 25, 2015

Rust has unsigned types with a unary - operator that currently generate an error: https://doc.rust-lang.org/stable/reference.html#unary-operator-expressions. Looks like it will be fixed in rust-lang/rust#30538.

I'm not fixing it. I'm making sure it errors always. We don't have unary negation. What we have is unary not, so we do !x + 1, which is equivalent to ~x + 1 in c/c++.

Ada's modular types are like Rust's Wrapping types, and those do support unary negation. Ada's Positive type does not support unary negation, it will raise an exception.

So if your types are wrapping (unsigned::MAX + 1 == unsigned::MIN) then it's probably correct to support them. If that's not happening, this might be very suprising in some situations that unary negation works.

@nmostafa
Copy link
Contributor

nmostafa commented Feb 9, 2016

Seems the consensus here is to have neg for both sign/unsigned ? Polyfill/tests still need to be fixed.

@littledan
Copy link
Member

Yes, I believe that was the consensus on the last call where we discussed it.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants