Skip to content

Commit 2c4ef75

Browse files
feat: #166 finally bitwise operators
1 parent 49a06ba commit 2c4ef75

File tree

10 files changed

+3186
-1246
lines changed

10 files changed

+3186
-1246
lines changed

bin/gpu-browser-core.js

Lines changed: 517 additions & 249 deletions
Large diffs are not rendered by default.

bin/gpu-browser-core.min.js

Lines changed: 518 additions & 250 deletions
Large diffs are not rendered by default.

bin/gpu-browser.js

Lines changed: 517 additions & 249 deletions
Large diffs are not rendered by default.

bin/gpu-browser.min.js

Lines changed: 518 additions & 250 deletions
Large diffs are not rendered by default.

src/backend/function-node.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -342,12 +342,22 @@ class FunctionNode {
342342
case '>':
343343
case '<':
344344
return 'Boolean';
345+
case '&':
346+
case '|':
347+
case '^':
348+
case '<<':
349+
case '>>':
350+
case '>>>':
351+
return 'Integer';
345352
}
346353
const type = this.getType(ast.left);
347354
return typeLookupMap[type] || type;
348355
case 'UpdateExpression':
349356
return this.getType(ast.argument);
350357
case 'UnaryExpression':
358+
if (ast.operator === '~') {
359+
return 'Integer';
360+
}
351361
return this.getType(ast.argument);
352362
case 'VariableDeclaration':
353363
return this.getType(ast.declarations[0]);
@@ -956,6 +966,11 @@ class FunctionNode {
956966
* @returns {Array} the append retArr
957967
*/
958968
astUnaryExpression(uNode, retArr) {
969+
const unaryResult = this.checkAndUpconvertBitwiseUnary(uNode, retArr);
970+
if (unaryResult) {
971+
return retArr;
972+
}
973+
959974
if (uNode.prefix) {
960975
retArr.push(uNode.operator);
961976
this.astGeneric(uNode.argument, retArr);
@@ -966,6 +981,9 @@ class FunctionNode {
966981

967982
return retArr;
968983
}
984+
985+
checkAndUpconvertBitwiseUnary(uNode, retArr) {}
986+
969987
/**
970988
* @desc Parses the abstract syntax tree for *Update* Expression
971989
* @param {Object} uNode - An ast Node

src/backend/web-gl/fragment-shader.js

Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,129 @@ float round(float x) {
1818
return floor(x + 0.5);
1919
}
2020
21+
const int BIT_COUNT = 32;
22+
int modi(int x, int y) {
23+
return x - y * (x / y);
24+
}
25+
26+
int bitwiseOr(int a, int b) {
27+
int result = 0;
28+
int n = 1;
29+
30+
for (int i = 0; i < BIT_COUNT; i++) {
31+
if ((modi(a, 2) == 1) || (modi(b, 2) == 1)) {
32+
result += n;
33+
}
34+
a = a / 2;
35+
b = b / 2;
36+
n = n * 2;
37+
if(!(a > 0 || b > 0)) {
38+
break;
39+
}
40+
}
41+
return result;
42+
}
43+
int bitwiseXOR(int a, int b) {
44+
int result = 0;
45+
int n = 1;
46+
47+
for (int i = 0; i < BIT_COUNT; i++) {
48+
if ((modi(a, 2) == 1) != (modi(b, 2) == 1)) {
49+
result += n;
50+
}
51+
a = a / 2;
52+
b = b / 2;
53+
n = n * 2;
54+
if(!(a > 0 || b > 0)) {
55+
break;
56+
}
57+
}
58+
return result;
59+
}
60+
int bitwiseAnd(int a, int b) {
61+
int result = 0;
62+
int n = 1;
63+
for (int i = 0; i < BIT_COUNT; i++) {
64+
if ((modi(a, 2) == 1) && (modi(b, 2) == 1)) {
65+
result += n;
66+
}
67+
a = a / 2;
68+
b = b / 2;
69+
n = n * 2;
70+
if(!(a > 0 && b > 0)) {
71+
break;
72+
}
73+
}
74+
return result;
75+
}
76+
int bitwiseNot(int a) {
77+
int result = 0;
78+
int n = 1;
79+
80+
for (int i = 0; i < BIT_COUNT; i++) {
81+
if (modi(a, 2) == 0) {
82+
result += n;
83+
}
84+
a = a / 2;
85+
n = n * 2;
86+
}
87+
return result;
88+
}
89+
int bitwiseZeroFillLeftShift(int n, int shift) {
90+
int maxBytes = BIT_COUNT;
91+
for (int i = 0; i < BIT_COUNT; i++) {
92+
if (maxBytes >= n) {
93+
break;
94+
}
95+
maxBytes *= 2;
96+
}
97+
for (int i = 0; i < BIT_COUNT; i++) {
98+
if (i >= shift) {
99+
break;
100+
}
101+
n *= 2;
102+
}
103+
104+
int result = 0;
105+
int byteVal = 1;
106+
for (int i = 0; i < BIT_COUNT; i++) {
107+
if (i >= maxBytes) break;
108+
if (modi(n, 2) > 0) { result += byteVal; }
109+
n = int(n / 2);
110+
byteVal *= 2;
111+
}
112+
return result;
113+
}
114+
115+
int bitwiseSignedRightShift(int num, int shifts) {
116+
return int(floor(float(num) / pow(2.0, float(shifts))));
117+
}
118+
119+
int bitwiseZeroFillRightShift(int n, int shift) {
120+
int maxBytes = BIT_COUNT;
121+
for (int i = 0; i < BIT_COUNT; i++) {
122+
if (maxBytes >= n) {
123+
break;
124+
}
125+
maxBytes *= 2;
126+
}
127+
for (int i = 0; i < BIT_COUNT; i++) {
128+
if (i >= shift) {
129+
break;
130+
}
131+
n /= 2;
132+
}
133+
int result = 0;
134+
int byteVal = 1;
135+
for (int i = 0; i < BIT_COUNT; i++) {
136+
if (i >= maxBytes) break;
137+
if (modi(n, 2) > 0) { result += byteVal; }
138+
n = int(n / 2);
139+
byteVal *= 2;
140+
}
141+
return result;
142+
}
143+
21144
vec2 integerMod(vec2 x, float y) {
22145
vec2 res = floor(mod(x, y));
23146
return res * step(1.0 - floor(y), -res);

0 commit comments

Comments
 (0)