summaryrefslogtreecommitdiffstats
path: root/dom/webgpu/tests/cts/checkout/src/webgpu/shader/execution/expression/binary/i32_arithmetic.spec.ts
blob: bbdb260a2d0305c68ecd98a55bc1e0f957637a8b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
export const description = `
Execution Tests for the i32 arithmetic binary expression operations
`;

import { makeTestGroup } from '../../../../../common/framework/test_group.js';
import { GPUTest } from '../../../../gpu_test.js';
import { kValue } from '../../../../util/constants.js';
import { TypeI32 } from '../../../../util/conversion.js';
import { fullI32Range } from '../../../../util/math.js';
import { makeCaseCache } from '../case_cache.js';
import { allInputSources, generateBinaryToI32Cases, run } from '../expression.js';

import { binary } from './binary.js';

export const g = makeTestGroup(GPUTest);

export const d = makeCaseCache('binary/i32_arithmetic', {
  addition: () => {
    return generateBinaryToI32Cases(fullI32Range(), fullI32Range(), (x, y) => {
      return x + y;
    });
  },
  subtraction: () => {
    return generateBinaryToI32Cases(fullI32Range(), fullI32Range(), (x, y) => {
      return x - y;
    });
  },
  multiplication: () => {
    return generateBinaryToI32Cases(fullI32Range(), fullI32Range(), (x, y) => {
      return Math.imul(x, y);
    });
  },
  division_non_const: () => {
    return generateBinaryToI32Cases(fullI32Range(), fullI32Range(), (x, y) => {
      if (y === 0) {
        return x;
      }
      if (x === kValue.i32.negative.min && y === -1) {
        return x;
      }
      return x / y;
    });
  },
  division_const: () => {
    return generateBinaryToI32Cases(fullI32Range(), fullI32Range(), (x, y) => {
      if (y === 0) {
        return undefined;
      }
      if (x === kValue.i32.negative.min && y === -1) {
        return undefined;
      }
      return x / y;
    });
  },
  remainder_non_const: () => {
    return generateBinaryToI32Cases(fullI32Range(), fullI32Range(), (x, y) => {
      if (y === 0) {
        return 0;
      }
      if (x === kValue.i32.negative.min && y === -1) {
        return 0;
      }
      return x % y;
    });
  },
  remainder_const: () => {
    return generateBinaryToI32Cases(fullI32Range(), fullI32Range(), (x, y) => {
      if (y === 0) {
        return undefined;
      }
      if (x === kValue.i32.negative.min && y === -1) {
        return undefined;
      }
      return x % y;
    });
  },
});

g.test('addition')
  .specURL('https://www.w3.org/TR/WGSL/#floating-point-evaluation')
  .desc(
    `
Expression: x + y
`
  )
  .params(u =>
    u.combine('inputSource', allInputSources).combine('vectorize', [undefined, 2, 3, 4] as const)
  )
  .fn(async t => {
    const cases = await d.get('addition');
    await run(t, binary('+'), [TypeI32, TypeI32], TypeI32, t.params, cases);
  });

g.test('subtraction')
  .specURL('https://www.w3.org/TR/WGSL/#floating-point-evaluation')
  .desc(
    `
Expression: x - y
`
  )
  .params(u =>
    u.combine('inputSource', allInputSources).combine('vectorize', [undefined, 2, 3, 4] as const)
  )
  .fn(async t => {
    const cases = await d.get('subtraction');
    await run(t, binary('-'), [TypeI32, TypeI32], TypeI32, t.params, cases);
  });

g.test('multiplication')
  .specURL('https://www.w3.org/TR/WGSL/#floating-point-evaluation')
  .desc(
    `
Expression: x * y
`
  )
  .params(u =>
    u.combine('inputSource', allInputSources).combine('vectorize', [undefined, 2, 3, 4] as const)
  )
  .fn(async t => {
    const cases = await d.get('multiplication');
    await run(t, binary('*'), [TypeI32, TypeI32], TypeI32, t.params, cases);
  });

g.test('division')
  .specURL('https://www.w3.org/TR/WGSL/#floating-point-evaluation')
  .desc(
    `
Expression: x / y
`
  )
  .params(u =>
    u.combine('inputSource', allInputSources).combine('vectorize', [undefined, 2, 3, 4] as const)
  )
  .fn(async t => {
    const cases = await d.get(
      t.params.inputSource === 'const' ? 'division_const' : 'division_non_const'
    );
    await run(t, binary('/'), [TypeI32, TypeI32], TypeI32, t.params, cases);
  });

g.test('remainder')
  .specURL('https://www.w3.org/TR/WGSL/#floating-point-evaluation')
  .desc(
    `
Expression: x % y
`
  )
  .params(u =>
    u.combine('inputSource', allInputSources).combine('vectorize', [undefined, 2, 3, 4] as const)
  )
  .fn(async t => {
    const cases = await d.get(
      t.params.inputSource === 'const' ? 'remainder_const' : 'remainder_non_const'
    );
    await run(t, binary('%'), [TypeI32, TypeI32], TypeI32, t.params, cases);
  });