-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmod.y
More file actions
27 lines (22 loc) · 742 Bytes
/
mod.y
File metadata and controls
27 lines (22 loc) · 742 Bytes
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
/**********************************************************************
AUTHOR: Kenneth Pollick <me@kennethpollick.com>
COPYRIGHT: 2022-2023 Kenneth Pollick
DATE: 2023-03-10
**********************************************************************/
mod[n]: dt sdt
{
dt#0 val;
ctor(dt#0 d)
{
if (n < 2)
err "Type mod must have a size abstraction greater than one";
this.val = d % n;
}
static become operator dt#0 unary_*() { return this.val; }
static operator dt#0 mod[n] binary_+(dt#0 r)
{
// don't need to modulo beforehand if n is a power of 2 (might remove for dynamically allocated numbers or separate implementations)
constant dt#0 rp = ternary{(popcount(n) == 1), r, (r % n)};
return ctor(this.val + rp);
}
}