software-mansion / software-mansion/TypeGPU

bug: Integer division generates floating point division WGSL

Open
#2,293 3 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
TypeScript
Stars
3.2k
Forks
122
Avg merge
3d 5h
Merged PRs (30d)
34

Description

The following function silently casts the arguments to f32 in the generated WGSL, resulting in loss of precision:

function integerDiv(a: number, b: number){
	"use gpu";
	return d.u32(a) / d.u32(b);
}

I expected it to work like this (working) function:

const integerDiv = tgpu.fn([d.u32, d.u32], d.u32)`(l: u32, r: u32) -> u32 {
	return l / r;
}`;

TypeGPU version: 0.10.2

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Reproduce the issue with the integerDiv function using d.u32(a) / d.u32(b) and compare its generated WGSL with the explicitly typed tgpu.fn example. Trace the TypeGPU TypeScript-to-WGSL path responsible for division type handling; done means u32 division remains integer and does not silently become f32 or lose precision.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
compilers
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.