Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions packages/typegpu/src/std/numeric.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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] };
},
Expand Down
18 changes: 16 additions & 2 deletions packages/typegpu/tests/std/numeric/intdiv.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(() => {});

Expand Down Expand Up @@ -136,7 +150,7 @@ test('intdiv throws with vector arguments', () => {
- <root>
- fn*:undefined
- fn*:<unnamed>()
- 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(() =>
Expand All @@ -152,6 +166,6 @@ test('intdiv throws with vector arguments', () => {
- <root>
- fn*:undefined
- fn*:<unnamed>()
- 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.]
`);
});