sirisian / ecmascript-types

ECMAScript Optional Static Typing Proposal http://sirisian.github.io/ecmascript-types/
453 stars 4 forks source link

Switch to generic syntax for SIMD types while including the original aliases #70

Open sirisian opened 2 years ago

sirisian commented 2 years ago

WIP issue with thoughts.

Currently the SIMD types are:

boolean8x16, boolean16x8, boolean32x4, boolean64x2, boolean8x32, boolean16x16, boolean32x8, boolean64x4
int8x16, int16x8, int32x4, int64x2, int8x32, int16x16, int32x8, int64x4
uint8x16, uint16x8, uint32x4, uint64x2, uint8x32, uint16x16, uint32x8, uint64x4
float32x4, float64x2, float32x8, float64x4

Been thinking they could just be aliases. They're really nice to have as they make math code compact. One of the main reasons I originally defined them as separate types is because they need component accessors in the form of:

position: x, y, z, w
color: r, g, b, a
texture: s, t, p, q

These would be part of the swizzling operators on vectors of size 2 and 4.

There are vectors with sizes 2, 4, 8, 16, and 32. These could all be defined like:

type float32x8 = vector<float32, 8>;

We'd need to make the boolean bitflag types real: boolean8, boolean16, boolean32, boolean64. These array syntax to access bitflags probably.

const a:boolean8 = 2;
a[1]; // 1
sirisian commented 1 year ago

More thoughts related to this.

In the generics extension I refer to the point there's no int or uint base type. This can be remedied by changing the types to be uint<N> and int<N> where both extend from uint and int respectively.

Expand for all the ```int``` and ```uint``` types defined by default. ```js type int8 = int<8>; type int16 = int<16>; type int32 = int<32>; type int64 = int<64>; type uint8 = uint<8>; type uint16 = uint<16>; type uint32 = uint<32>; type uint64 = uint<64>; ```

boolean at least for SIMD masks can be thought of as uint<1>.

type boolean1 = uint<1>;
type boolean8 = vector<boolean1, 8>;
type boolean8x16 = vector<boolean8, 16>; // vector<vector<uint<1>, 8>, 16>
Expand for all the SIMD types defined by default. ```js type boolean1 = uint<1>; type boolean8 = vector; type boolean16 = vector; type boolean32 = vector; type boolean64 = vector; type boolean8x16 = vector; type boolean16x8 = vector, 8>; type boolean32x4 = vector, 4>; type boolean64x2 = vector, 2>; type boolean8x32 = vector, 32>; type boolean16x16 = vector, 16>; type boolean32x8 = vector, 8>; type boolean64x4 = vector, 4>; type int8x16 = vector; type int16x8 = vector; type int32x4 = vector; type int64x2 = vector; type int8x32 = vector; type int16x16 = vector; type int32x8 = vector; type int64x4 = vector; type uint8x16 = vector; type uint16x8 = vector; type uint32x4 = vector; type uint64x2 = vector; type uint8x32 = vector; type uint16x16 = vector; type uint32x8 = vector; type uint64x4 = vector; type float32x4 = vector; type float64x2 = vector; type float32x8 = vector; type float64x4 = vector; ```

Further down in the current spec I mention allowing bit fields. Weirdly enough ECMAScript is seeing use in embedded systems. Sometimes it's things like devices having web interfaces. As computing power continues to increase there are more low level libraries being used by developers.

class Vector2 {
  x:uint<4>; // 4 bits
  y:uint<4>; // 4 bits
}

It's important to quickly mock up such bitfield syntax and ensure the language defines the memory layout as it can't be changed later.

WIP: Continue this later...

sirisian commented 1 year ago

Updated https://github.com/sirisian/ecmascript-types/blob/master/README.md#types-proposed with these changes.

TODO: Update the bit-fields section and the memory layout sections to support bit alignment and sizes optionally.