Skip to content

Commit c6df48a

Browse files
committed
Support bitwise or operator for type params.
1 parent 2260a57 commit c6df48a

File tree

3 files changed

+41
-0
lines changed

3 files changed

+41
-0
lines changed

compiler/expressions.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -698,6 +698,8 @@ func (fc *funcContext) translateBinaryExpr(e *ast.BinaryExpr) *expression {
698698
return fc.formatExpr("%s.rem(%e, %e)", fc.typeName(t), e.X, e.Y)
699699
case token.AND:
700700
return fc.formatExpr("%s.and(%e, %e)", fc.typeName(t), e.X, e.Y)
701+
case token.OR:
702+
return fc.formatExpr("%s.or(%e, %e)", fc.typeName(t), e.X, e.Y)
701703
}
702704
}
703705

compiler/prelude/types.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -426,6 +426,7 @@ var $newType = (size, kind, string, named, pkg, exported, constructor) => {
426426
typ.div = (x, y) => $idiv(x, y) >> 0;
427427
typ.rem = $irem;
428428
typ.and = (x, y) => (x & y);
429+
typ.or = (x, y) => (x | y);
429430
break;
430431
case $kindUint8:
431432
case $kindUint16:
@@ -435,6 +436,7 @@ var $newType = (size, kind, string, named, pkg, exported, constructor) => {
435436
typ.div = (x, y) => $idiv(x, y) >>> 0;
436437
typ.rem = $irem;
437438
typ.and = (x, y) => (x & y) >>> 0;
439+
typ.or = (x, y) => (x | y) >>> 0;
438440
break;
439441
case $kindUint:
440442
case $kindUint32:
@@ -445,6 +447,7 @@ var $newType = (size, kind, string, named, pkg, exported, constructor) => {
445447
typ.div = (x, y) => $idiv(x, y) >>> 0;
446448
typ.rem = $irem;
447449
typ.and = (x, y) => (x & y) >>> 0;
450+
typ.or = (x, y) => (x | y) >>> 0;
448451
break;
449452
case $kindInt:
450453
case $kindInt32:
@@ -454,6 +457,7 @@ var $newType = (size, kind, string, named, pkg, exported, constructor) => {
454457
typ.div = (x, y) => $idiv(x, y) >> 0;
455458
typ.rem = $irem;
456459
typ.and = (x, y) => (x & y);
460+
typ.or = (x, y) => (x | y);
457461
break;
458462
case $kindInt64:
459463
case $kindUint64:
@@ -463,6 +467,7 @@ var $newType = (size, kind, string, named, pkg, exported, constructor) => {
463467
typ.div = (x, y) => $div64(x, y, false);
464468
typ.rem = (x, y) => $div64(x, y, true);
465469
typ.and = (x, y) => new typ(x.$high & y.$high, (x.$low & y.$low) >>> 0);
470+
typ.or = (x, y) => new typ(x.$high | y.$high, (x.$low | y.$low) >>> 0);
466471
break;
467472
case $kindFloat32:
468473
case $kindFloat64:

tests/typeparams/arithmetics_test.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -266,3 +266,37 @@ func TestBitwiseAnd(t *testing.T) {
266266
t.Run(test.String(), test.Run)
267267
}
268268
}
269+
270+
func or[T constraints.Integer](x, y T) T {
271+
return x | y
272+
}
273+
274+
func orTC[T constraints.Integer](x, y, want T) *testCase[T] {
275+
return &testCase[T]{
276+
op: or[T],
277+
opName: token.OR,
278+
x: x,
279+
y: y,
280+
want: want,
281+
}
282+
}
283+
284+
func TestBitwiseOr(t *testing.T) {
285+
tests := []testCaseI{
286+
orTC[int](0x0011, 0x0101, 0x0111),
287+
orTC[uint](0x0011, 0x0101, 0x0111),
288+
orTC[uintptr](0x0011, 0x0101, 0x0111),
289+
orTC[int8](0x11, 0x01, 0x11),
290+
orTC[int16](0x0011, 0x0101, 0x0111),
291+
orTC[int32](0x0011, 0x0101, 0x0111),
292+
orTC[uint8](0x11, 0x01, 0x11),
293+
orTC[uint16](0x0011, 0x0101, 0x0111),
294+
orTC[uint32](0x0011, 0x0101, 0x0111),
295+
orTC[int64](0x0000001100000011, 0x0000010100000101, 0x0000011100000111),
296+
orTC[uint64](0x0000001100000011, 0x0000010100000101, 0x0000011100000111),
297+
}
298+
299+
for _, test := range tests {
300+
t.Run(test.String(), test.Run)
301+
}
302+
}

0 commit comments

Comments
 (0)