thi-ng / umbrella

⛱ Broadly scoped ecosystem & mono-repository of 198 TypeScript projects (and ~175 examples) for general purpose, functional, data driven development
https://thi.ng
Apache License 2.0
3.31k stars 144 forks source link

[shader-ast] Bitwise shift operation #434

Open sylee957 opened 7 months ago

sylee957 commented 7 months ago

I can't find bitwise shift operators >>, << in shader-ast. Is it not implemented yet? I'm trying the workaround

export const lshift = <T extends "int" | "uint" | IVec | UVec>(
  a: Term<T>,
  b: IntTerm | UintTerm
) => op2("<<", a, b) as Op2<T>;
export const rshift = <T extends "int" | "uint" | IVec | UVec>(
  a: Term<T>,
  b: IntTerm | UintTerm
) => op2(">>", a, b) as Op2<T>;
postspectacular commented 7 months ago

I'm really surprised these weren't included yet, I will have to add a few more variations (function overrides) for these, but in general, yes, the above is the correct approach...

postspectacular commented 7 months ago

The 2 relevant commits, just for reference:

https://github.com/thi-ng/umbrella/commit/a986766c93e11faa16a6d455b1a2ff6502dfd847

https://github.com/thi-ng/umbrella/commit/a78d31350df80151daa4c6b2492442bb5d1aa219

sylee957 commented 7 months ago

Unfortunately, I still see some problems for types if one operand is signed and the other operand is unsigned

I found this from specification:

One operand can be signed while the other is unsigned.

const a = sym("int");
const b = sym("uint");
lshift(a, b);
const a = sym("uint");
const b = sym("int");
lshift(a, b);

And I think that same applies for vectors, such as

const a = sym("uvec2");
const b = sym("int");
lshift(a, b);
const a = sym("uvec2");
const b = sym("ivec2");
lshift(a, b);