Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 14241fd

Browse filesBrowse files
committed
Support remainder operator for type params.
1 parent 2e76d2d commit 14241fd
Copy full SHA for 14241fd

File tree

4 files changed

+57
-7
lines changed
Filter options

4 files changed

+57
-7
lines changed

‎compiler/expressions.go

Copy file name to clipboardExpand all lines: compiler/expressions.go
+2Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -694,6 +694,8 @@ func (fc *funcContext) translateBinaryExpr(e *ast.BinaryExpr) *expression {
694694
return fc.formatExpr("%s.mul(%e, %e)", fc.typeName(t), e.X, e.Y)
695695
case token.QUO:
696696
return fc.formatExpr("%s.div(%e, %e)", fc.typeName(t), e.X, e.Y)
697+
case token.REM:
698+
return fc.formatExpr("%s.rem(%e, %e)", fc.typeName(t), e.X, e.Y)
697699
}
698700
}
699701

‎compiler/prelude/numeric.js

Copy file name to clipboardExpand all lines: compiler/prelude/numeric.js
+9Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,15 @@ const $idiv = (x, y) => {
125125
}
126126
};
127127

128+
const $irem = (x, y) => {
129+
const result = x % y;
130+
if (result === result) {
131+
return result;
132+
} else {
133+
$throwRuntimeError("integer divide by zero");
134+
}
135+
};
136+
128137
var $div64 = (x, y, returnRemainder) => {
129138
if (y.$high === 0 && y.$low === 0) {
130139
$throwRuntimeError("integer divide by zero");

‎compiler/prelude/types.js

Copy file name to clipboardExpand all lines: compiler/prelude/types.js
+12-7Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -424,20 +424,15 @@ var $newType = (size, kind, string, named, pkg, exported, constructor) => {
424424
typ.sub = (x, y) => $truncateNumber(x - y, typ);
425425
typ.mul = (x, y) => $truncateNumber(x * y, typ);
426426
typ.div = (x, y) => $idiv(x, y) >> 0;
427+
typ.rem = $irem;
427428
break;
428429
case $kindUint8:
429430
case $kindUint16:
430431
typ.add = (x, y) => $truncateNumber(x + y, typ);
431432
typ.sub = (x, y) => $truncateNumber(x - y, typ);
432433
typ.mul = (x, y) => $truncateNumber(x * y, typ);
433434
typ.div = (x, y) => $idiv(x, y) >>> 0;
434-
break;
435-
case $kindFloat32:
436-
case $kindFloat64:
437-
typ.add = (x, y) => $truncateNumber(x + y, typ);
438-
typ.sub = (x, y) => $truncateNumber(x - y, typ);
439-
typ.mul = (x, y) => $truncateNumber(x * y, typ);
440-
typ.div = (x, y) => $truncateNumber(x / y, typ);
435+
typ.rem = $irem;
441436
break;
442437
case $kindUint:
443438
case $kindUint32:
@@ -446,20 +441,30 @@ var $newType = (size, kind, string, named, pkg, exported, constructor) => {
446441
typ.sub = (x, y) => $truncateNumber(x - y, typ);
447442
typ.mul = (x, y) => $imul(x, y) >>> 0;
448443
typ.div = (x, y) => $idiv(x, y) >>> 0;
444+
typ.rem = $irem;
449445
break;
450446
case $kindInt:
451447
case $kindInt32:
452448
typ.add = (x, y) => $truncateNumber(x + y, typ);
453449
typ.sub = (x, y) => $truncateNumber(x - y, typ);
454450
typ.mul = (x, y) => $imul(x, y);
455451
typ.div = (x, y) => $idiv(x, y) >> 0;
452+
typ.rem = $irem;
456453
break;
457454
case $kindInt64:
458455
case $kindUint64:
459456
typ.add = (x, y) => new typ(x.$high + y.$high, x.$low + y.$low);
460457
typ.sub = (x, y) => new typ(x.$high - y.$high, x.$low - y.$low);
461458
typ.mul = (x, y) => $mul64(x, y);
462459
typ.div = (x, y) => $div64(x, y, false);
460+
typ.rem = (x, y) => $div64(x, y, true);
461+
break;
462+
case $kindFloat32:
463+
case $kindFloat64:
464+
typ.add = (x, y) => $truncateNumber(x + y, typ);
465+
typ.sub = (x, y) => $truncateNumber(x - y, typ);
466+
typ.mul = (x, y) => $truncateNumber(x * y, typ);
467+
typ.div = (x, y) => $truncateNumber(x / y, typ);
463468
break;
464469
case $kindComplex64:
465470
case $kindComplex128:

‎tests/typeparams/arithmetics_test.go

Copy file name to clipboardExpand all lines: tests/typeparams/arithmetics_test.go
+34Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,3 +198,37 @@ func TestDiv(t *testing.T) {
198198
t.Run(test.String(), test.Run)
199199
}
200200
}
201+
202+
func rem[T constraints.Integer](x, y T) T {
203+
return x % y
204+
}
205+
206+
func remTC[T constraints.Integer](x, y, want T) *testCase[T] {
207+
return &testCase[T]{
208+
op: rem[T],
209+
opName: token.REM,
210+
x: x,
211+
y: y,
212+
want: want,
213+
}
214+
}
215+
216+
func TestRemainder(t *testing.T) {
217+
tests := []testCaseI{
218+
remTC[int](7, 2, 1),
219+
remTC[uint](7, 2, 1),
220+
remTC[uintptr](7, 2, 1),
221+
remTC[int8](7, 2, 1),
222+
remTC[int16](7, 2, 1),
223+
remTC[int32](7, 2, 1),
224+
remTC[uint8](7, 2, 1),
225+
remTC[uint16](7, 2, 1),
226+
remTC[uint32](7, 2, 1),
227+
remTC[int64](0x0000006500000003, 0x0000003200000001, 0x100000001),
228+
remTC[uint64](0x0000006500000003, 0x0000003200000001, 0x100000001),
229+
}
230+
231+
for _, test := range tests {
232+
t.Run(test.String(), test.Run)
233+
}
234+
}

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.