diff --git a/packages/typegpu/src/std/numeric.ts b/packages/typegpu/src/std/numeric.ts index 4471af4449..b839d67f90 100644 --- a/packages/typegpu/src/std/numeric.ts +++ b/packages/typegpu/src/std/numeric.ts @@ -1227,9 +1227,9 @@ function cpuIntdiv(lhs: number, rhs: number): number { export const intdiv = dualImpl({ name: 'intdiv', signature: (lhs, rhs) => { - const unified = unify([lhs, rhs], [u32, i32]); + const unified = unify([lhs, rhs], [i32, u32]); if (!unified) { - throw new SignatureNotSupportedError([lhs, rhs], [u32, i32, abstractInt]); + throw new SignatureNotSupportedError([lhs, rhs], [i32, u32, abstractInt]); } return { argTypes: unified, returnType: unified[0] }; }, diff --git a/packages/typegpu/tests/std/numeric/intdiv.test.ts b/packages/typegpu/tests/std/numeric/intdiv.test.ts index 21d38d61e8..0c8385c792 100644 --- a/packages/typegpu/tests/std/numeric/intdiv.test.ts +++ b/packages/typegpu/tests/std/numeric/intdiv.test.ts @@ -95,6 +95,20 @@ test('intdiv with u32 mixed with i32', () => { `); }); +test('intdiv preserves signed runtime float operands', () => { + using warnSpy = vi.spyOn(console, 'warn').mockImplementation(() => {}); + + const foo = tgpu.fn([d.f32, d.f32], d.i32)((lhs, rhs) => std.intdiv(lhs, rhs)); + + expect(foo(-5.9, 2.1)).toBe(-2); + expect(tgpu.resolve([foo])).toMatchInlineSnapshot(` + "fn foo(lhs: f32, rhs: f32) -> i32 { + return (i32(lhs) / i32(rhs)); + }" + `); + expect(warnSpy.mock.calls).toHaveLength(2); +}); + test('intdiv coerces float arguments to integers', () => { using warnSpy = vi.spyOn(console, 'warn').mockImplementation(() => {}); @@ -136,7 +150,7 @@ test('intdiv throws with vector arguments', () => { - - fn*:undefined - fn*:() - - fn:intdiv: Unsupported data types: vec3u, abstractInt. Supported types are: u32, i32, abstractInt.] + - fn:intdiv: Unsupported data types: vec3u, abstractInt. Supported types are: i32, u32, abstractInt.] `); expect(() => @@ -152,6 +166,6 @@ test('intdiv throws with vector arguments', () => { - - fn*:undefined - fn*:() - - fn:intdiv: Unsupported data types: abstractInt, vec3i. Supported types are: u32, i32, abstractInt.] + - fn:intdiv: Unsupported data types: abstractInt, vec3i. Supported types are: i32, u32, abstractInt.] `); });