forked from gpujs/gpu.js
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmath-object.js
More file actions
237 lines (193 loc) · 5.32 KB
/
math-object.js
File metadata and controls
237 lines (193 loc) · 5.32 KB
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
const { assert, skip, test, module: describe, only } = require('qunit');
const { GPU } = require('../../src');
describe('features: math object');
function mathProps(mode) {
const props = ['E','LN10','LN2','LOG10E','LOG2E','PI','SQRT1_2','SQRT2'];
const gpu = new GPU({ mode });
for (let i = 0; i < props.length; i++) {
const prop = props[i];
const kernel = gpu.createKernel(`function() {
return Math.${prop};
}`, { output: [1] });
assert.equal(kernel()[0].toFixed(6), Math[prop].toFixed(6));
}
gpu.destroy();
}
test('All Math properties auto', () => {
mathProps();
});
test('All Math properties gpu', () => {
mathProps('gpu');
});
(GPU.isWebGLSupported ? test : skip)('All Math properties webgl', () => {
mathProps('webgl');
});
(GPU.isWebGL2Supported ? test : skip)('All Math properties webgl2', () => {
mathProps('webgl2');
});
(GPU.isHeadlessGLSupported ? test : skip)('All Math properties headlessgl', () => {
mathProps('headlessgl');
});
test('All Math properties cpu', () => {
mathProps('cpu');
});
function singleArgumentMathMethods(mode) {
const methods = [
'abs',
'acos',
'acosh',
'asin',
'asinh',
'atan',
'atanh',
'cbrt',
'ceil',
// 'clz32', // not supported, bits directly are hard
'cos',
'cosh',
'exp',
'expm1',
'floor',
'fround',
// 'hypot', // not supported, dynamically sized
'log',
'log10',
'log1p',
'log2',
'round',
'sign',
'sin',
'sinh',
'sqrt',
'tan',
'tanh',
'trunc',
];
const gpu = new GPU({ mode });
for (let i = 0; i < methods.length; i++) {
const method = methods[i];
const kernel = gpu.createKernel(`function(value) {
return Math.${method}(value);
}`, { output: [1] });
for (let j = 0; j < 10; j++) {
assert.equal(kernel(j / 10)[0].toFixed(3), Math[method](j / 10).toFixed(3), `Math.${method}(${j / 10})`);
}
}
gpu.destroy();
}
test('Single argument Math methods auto', () => {
singleArgumentMathMethods();
});
test('Single argument Math methods gpu', () => {
singleArgumentMathMethods('gpu');
});
(GPU.isWebGLSupported ? test : skip)('Single argument Math methods webgl', () => {
singleArgumentMathMethods('webgl');
});
(GPU.isWebGL2Supported ? test : skip)('Single argument Math methods webgl2', () => {
singleArgumentMathMethods('webgl2');
});
(GPU.isHeadlessGLSupported ? test : skip)('Single argument Math methods headlessgl', () => {
singleArgumentMathMethods('headlessgl');
});
test('Single argument Math methods cpu', () => {
singleArgumentMathMethods('cpu');
});
function twoArgumentMathMethods(mode) {
const methods = [
'atan2',
'imul',
'max',
'min',
'pow',
];
const gpu = new GPU({ mode });
for (let i = 0; i < methods.length; i++) {
const method = methods[i];
const kernel = gpu.createKernel(`function(value1, value2) {
return Math.${method}(value1, value2);
}`, { output: [1] });
for (let j = 0; j < 10; j++) {
const value1 = j / 10;
const value2 = value1;
assert.equal(kernel(value1, value2)[0].toFixed(3), Math[method](value1, value2).toFixed(3), `Math.${method}(${value1}, ${value2})`);
}
}
gpu.destroy();
}
test('Two argument Math methods auto', () => {
twoArgumentMathMethods();
});
test('Two argument Math methods gpu', () => {
twoArgumentMathMethods('gpu');
});
(GPU.isWebGLSupported ? test : skip)('Two argument Math methods webgl', () => {
twoArgumentMathMethods('webgl');
});
(GPU.isWebGL2Supported ? test : skip)('Two argument Math methods webgl2', () => {
twoArgumentMathMethods('webgl2');
});
(GPU.isHeadlessGLSupported ? test : skip)('Two argument Math methods headlessgl', () => {
twoArgumentMathMethods('headlessgl');
});
test('Two argument Math methods cpu', () => {
twoArgumentMathMethods('cpu');
});
function sqrtABTest(mode) {
const gpu = new GPU({ mode });
const f = gpu.createKernel(function(a, b) {
return Math.sqrt(a[this.thread.x] * b[this.thread.x]);
}, {
output : [6]
});
const a = [3, 4, 5, 6, 7, 8];
const b = [3, 4, 5, 6, 7, 8];
const res = f(a,b);
const exp = [3, 4, 5, 6, 7, 8];
assert.deepEqual(Array.from(res), exp);
gpu.destroy();
}
test('sqrtAB auto', () => {
sqrtABTest(null);
});
test('sqrtAB gpu', () => {
sqrtABTest('gpu');
});
(GPU.isWebGLSupported ? test : skip)('sqrtAB webgl', () => {
sqrtABTest('webgl');
});
(GPU.isWebGL2Supported ? test : skip)('sqrtAB webgl2', () => {
sqrtABTest('webgl2');
});
(GPU.isHeadlessGLSupported ? test : skip)('sqrtAB headlessgl', () => {
sqrtABTest('headlessgl');
});
test('sqrtAB cpu', () => {
sqrtABTest('cpu');
});
function mathRandom(mode) {
const gpu = new GPU({ mode });
const kernel = gpu.createKernel(function() {
return Math.random();
}, { output: [1] });
const result = kernel();
assert.ok(result[0] > 0 && result[0] < 1, `value was expected to be between o and 1, but was ${result[0]}`);
}
test('random auto', () => {
mathRandom();
});
test('random gpu', () => {
mathRandom('gpu');
});
(GPU.isWebGLSupported ? test : skip)('random webgl', () => {
mathRandom('webgl');
});
(GPU.isWebGL2Supported ? test : skip)('random webgl2', () => {
mathRandom('webgl2');
});
(GPU.isHeadlessGLSupported ? test : skip)('random headlessgl', () => {
mathRandom('headlessgl');
});
test('random cpu', () => {
mathRandom('cpu');
});