-
-
Notifications
You must be signed in to change notification settings - Fork 36
feat: Accept multiple arguments in min and max #1924
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
aleksanderkatan
wants to merge
8
commits into
main
Choose a base branch
from
feat/accept-multiple-arguments-in-min
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 7 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
dcec300
Add some basic working vararg min
560fa97
Add min tests
5612b16
Refactor
48f112b
More tests
fc0f652
Update max
a57b069
Add `variadicUnifySignature`
0abb7c6
Use variadic min and max in examples
b99196b
smol
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,94 @@ | ||
| import { describe, expect, it } from 'vitest'; | ||
| import tgpu from '../../../src/index.ts'; | ||
| import * as d from '../../../src/data/index.ts'; | ||
| import * as std from '../../../src/std/index.ts'; | ||
|
|
||
| describe('max', () => { | ||
| it('acts as identity when called with one argument', () => { | ||
| const myMax = tgpu.fn([d.f32], d.f32)((a: number) => { | ||
| 'use gpu'; | ||
| return std.max(a); | ||
| }); | ||
|
|
||
| expect(myMax(6)).toBe(6); | ||
| expect(tgpu.resolve([myMax])).toMatchInlineSnapshot(` | ||
| "fn myMax(a: f32) -> f32 { | ||
| return a; | ||
| }" | ||
| `); | ||
| }); | ||
|
|
||
| it('works with two arguments', () => { | ||
| const myMax = tgpu.fn([d.f32, d.f32], d.f32)((a, b) => { | ||
| 'use gpu'; | ||
| return std.max(a, b); | ||
| }); | ||
|
|
||
| expect(myMax(1, 2)).toBe(2); | ||
| expect(tgpu.resolve([myMax])).toMatchInlineSnapshot(` | ||
| "fn myMax(a: f32, b: f32) -> f32 { | ||
| return max(a, b); | ||
| }" | ||
| `); | ||
| }); | ||
|
|
||
| it('works with multiple arguments', () => { | ||
| const myMax = tgpu.fn([d.f32, d.f32, d.f32, d.f32], d.f32)( | ||
| (a, b, c, d) => { | ||
| 'use gpu'; | ||
| return std.max(a, b, c, d); | ||
| }, | ||
| ); | ||
|
|
||
| expect(myMax(2, 1, 4, 5)).toBe(5); | ||
| expect(tgpu.resolve([myMax])).toMatchInlineSnapshot(` | ||
| "fn myMax(a: f32, b: f32, c: f32, d2: f32) -> f32 { | ||
| return max(max(max(a, b), c), d2); | ||
| }" | ||
| `); | ||
| }); | ||
|
|
||
| it('unifies arguments', () => { | ||
| const myMax = tgpu.fn([], d.f32)(() => { | ||
| 'use gpu'; | ||
| const a = d.u32(9); | ||
| const b = d.i32(1); | ||
| const c = d.f32(4); | ||
| return std.max(a, b, 3.3, c, 7); | ||
| }); | ||
|
|
||
| expect(myMax()).toBe(9); | ||
| expect(tgpu.resolve([myMax])).toMatchInlineSnapshot(` | ||
| "fn myMax() -> f32 { | ||
| const a = 9u; | ||
| const b = 1i; | ||
| const c = 4f; | ||
| return max(max(max(max(f32(a), f32(b)), 3.3f), c), 7f); | ||
| }" | ||
| `); | ||
| }); | ||
|
|
||
| it('works with vectors', () => { | ||
| const myMax = tgpu.fn([d.vec3u, d.vec3u], d.vec3u)((a, b) => { | ||
| 'use gpu'; | ||
| return std.max(a, b); | ||
| }); | ||
|
|
||
| expect(myMax(d.vec3u(1, 2, 3), d.vec3u(3, 2, 1))) | ||
| .toStrictEqual(d.vec3u(3, 2, 3)); | ||
| expect(tgpu.resolve([myMax])).toMatchInlineSnapshot(` | ||
| "fn myMax(a: vec3u, b: vec3u) -> vec3u { | ||
| return max(a, b); | ||
| }" | ||
| `); | ||
| }); | ||
|
|
||
| it('cannot be called with invalid arguments', () => { | ||
| // @ts-expect-error | ||
| (() => std.max()); | ||
| // @ts-expect-error | ||
| (() => std.max(1, d.vec2f())); | ||
| // @ts-expect-error | ||
| (() => std.max(d.vec3f(), d.vec2f())); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,94 @@ | ||
| import { describe, expect, it } from 'vitest'; | ||
| import tgpu from '../../../src/index.ts'; | ||
| import * as d from '../../../src/data/index.ts'; | ||
| import * as std from '../../../src/std/index.ts'; | ||
|
|
||
| describe('min', () => { | ||
| it('acts as identity when called with one argument', () => { | ||
| const myMin = tgpu.fn([d.f32], d.f32)((a: number) => { | ||
| 'use gpu'; | ||
| return std.min(a); | ||
| }); | ||
|
|
||
| expect(myMin(6)).toBe(6); | ||
| expect(tgpu.resolve([myMin])).toMatchInlineSnapshot(` | ||
| "fn myMin(a: f32) -> f32 { | ||
| return a; | ||
| }" | ||
| `); | ||
| }); | ||
|
|
||
| it('works with two arguments', () => { | ||
| const myMin = tgpu.fn([d.f32, d.f32], d.f32)((a, b) => { | ||
| 'use gpu'; | ||
| return std.min(a, b); | ||
| }); | ||
|
|
||
| expect(myMin(1, 2)).toBe(1); | ||
| expect(tgpu.resolve([myMin])).toMatchInlineSnapshot(` | ||
| "fn myMin(a: f32, b: f32) -> f32 { | ||
| return min(a, b); | ||
| }" | ||
| `); | ||
| }); | ||
|
|
||
| it('works with multiple arguments', () => { | ||
| const myMin = tgpu.fn([d.f32, d.f32, d.f32, d.f32], d.f32)( | ||
| (a, b, c, d) => { | ||
| 'use gpu'; | ||
| return std.min(a, b, c, d); | ||
| }, | ||
| ); | ||
|
|
||
| expect(myMin(2, 1, 4, 5)).toBe(1); | ||
| expect(tgpu.resolve([myMin])).toMatchInlineSnapshot(` | ||
| "fn myMin(a: f32, b: f32, c: f32, d2: f32) -> f32 { | ||
| return min(min(min(a, b), c), d2); | ||
| }" | ||
| `); | ||
| }); | ||
|
|
||
| it('unifies arguments', () => { | ||
| const myMin = tgpu.fn([], d.f32)(() => { | ||
| 'use gpu'; | ||
| const a = d.u32(9); | ||
| const b = d.i32(1); | ||
| const c = d.f32(4); | ||
| return std.min(a, b, 3.3, c, 7); | ||
| }); | ||
|
|
||
| expect(myMin()).toBe(1); | ||
| expect(tgpu.resolve([myMin])).toMatchInlineSnapshot(` | ||
| "fn myMin() -> f32 { | ||
| const a = 9u; | ||
| const b = 1i; | ||
| const c = 4f; | ||
| return min(min(min(min(f32(a), f32(b)), 3.3f), c), 7f); | ||
| }" | ||
| `); | ||
| }); | ||
|
|
||
| it('works with vectors', () => { | ||
| const myMin = tgpu.fn([d.vec3u, d.vec3u], d.vec3u)((a, b) => { | ||
| 'use gpu'; | ||
| return std.min(a, b); | ||
| }); | ||
|
|
||
| expect(myMin(d.vec3u(1, 2, 3), d.vec3u(3, 2, 1))) | ||
| .toStrictEqual(d.vec3u(1, 2, 1)); | ||
| expect(tgpu.resolve([myMin])).toMatchInlineSnapshot(` | ||
| "fn myMin(a: vec3u, b: vec3u) -> vec3u { | ||
| return min(a, b); | ||
| }" | ||
| `); | ||
| }); | ||
|
|
||
| it('cannot be called with invalid arguments', () => { | ||
| // @ts-expect-error | ||
| (() => std.min()); | ||
| // @ts-expect-error | ||
| (() => std.min(1, d.vec2f())); | ||
| // @ts-expect-error | ||
| (() => std.min(d.vec3f(), d.vec2f())); | ||
| }); | ||
| }); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.