Skip to content

Commit bc63661

Browse files
committed
feat: Flesh out Bytes type
1 parent 51703aa commit bc63661

File tree

4 files changed

+27
-26
lines changed

4 files changed

+27
-26
lines changed

core/src/fr/hammons/slinc/Bytes.scala

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,12 @@ object Bytes:
1010
extension (a: Bytes)
1111
inline def +(b: Bytes): Bytes = a + b
1212
inline def *(i: Int): Bytes = a * i
13+
inline def *(b: Bytes): Bytes = a * b
14+
inline def %(b: Bytes): Bytes = a % b
15+
inline def -(b: Bytes): Bytes = a - b
1316
inline def toLong: Long = a
1417
inline def toBits: Long = a * 8
1518

19+
given Numeric[Bytes] = Numeric.LongIsIntegral
1620
given ToExpr[Bytes] with
1721
def apply(t: Bytes)(using Quotes) = ToExpr.LongToExpr[Long].apply(t)

core/src/fr/hammons/slinc/Ptr.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ class Ptr[A](private[slinc] val mem: Mem, private[slinc] val offset: Bytes):
1212
def asArray(size: Int)(using
1313
ClassTag[A]
1414
)(using DescriptorOf[A], DescriptorModule)(using r: ReceiveBulk[A]) =
15-
r.from(mem.resize(Bytes(DescriptorOf[A].size.toLong * size)), offset, size)
15+
r.from(mem.resize(DescriptorOf[A].size * size), offset, size)
1616

1717
def `unary_!_=`(value: A)(using send: Send[A]) = send.to(mem, offset, value)
1818
def apply(bytes: Bytes) = Ptr[A](mem, offset + bytes)

j17/src/fr/hammons/slinc/modules/DescriptorModule17.scala

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -35,29 +35,29 @@ given descriptorModule17: DescriptorModule with
3535

3636
def genLayoutList(
3737
layouts: Seq[MemoryLayout],
38-
alignment: Long
38+
alignment: Bytes
3939
): Seq[MemoryLayout] =
4040
val (vector, currentLocation) =
41-
layouts.foldLeft(Seq.empty[MemoryLayout] -> 0L) {
41+
layouts.foldLeft(Seq.empty[MemoryLayout] -> Bytes(0L)) {
4242
case ((vector, currentLocation), layout) =>
43-
val thisAlignment = layout.byteAlignment()
43+
val thisAlignment = Bytes(layout.byteAlignment())
4444
val misalignment = currentLocation % thisAlignment
4545
val toAdd =
46-
if misalignment == 0 then Seq(layout)
46+
if misalignment == Bytes(0) then Seq(layout)
4747
else
4848
val paddingNeeded = thisAlignment - misalignment
4949

5050
Seq(
51-
MemoryLayout.paddingLayout(paddingNeeded * 8).nn,
51+
MemoryLayout.paddingLayout(paddingNeeded.toBits).nn,
5252
layout
5353
)
54-
(vector ++ toAdd, currentLocation + toAdd.view.map(_.byteSize()).sum)
54+
(vector ++ toAdd, currentLocation + Bytes(toAdd.view.map(_.byteSize()).sum))
5555
}
5656
val misalignment = currentLocation % alignment
5757
vector ++ (
58-
if misalignment != 0 then
58+
if misalignment != Bytes(0) then
5959
Seq(
60-
MemoryLayout.paddingLayout((alignment - misalignment) * 8).nn
60+
MemoryLayout.paddingLayout((alignment - misalignment).toBits).nn
6161
)
6262
else Seq.empty
6363
)
@@ -75,15 +75,15 @@ given descriptorModule17: DescriptorModule with
7575

7676
override def alignmentOf(td: TypeDescriptor): Bytes = td match
7777
case s: StructDescriptor =>
78-
Bytes(s.members.view.map(_.descriptor).map(alignmentOf).map(_.toLong).max)
78+
s.members.view.map(_.descriptor).map(alignmentOf).max
7979
case _ => sizeOf(td)
8080

8181
override def memberOffsets(sd: StructDescriptor): IArray[Bytes] =
8282
offsets.getOrElseUpdate(
8383
sd, {
8484
val ll = genLayoutList(
8585
sd.members.map(toMemoryLayout),
86-
sd.members.view.map(_.descriptor).map(alignmentOf).map(_.toLong).max
86+
sd.members.view.map(_.descriptor).map(alignmentOf).max
8787
)
8888
IArray.from(
8989
ll match
@@ -125,7 +125,7 @@ given descriptorModule17: DescriptorModule with
125125
val alignment = alignmentOf(sd)
126126
val offsets = memberOffsets(sd)
127127
MemoryLayout
128-
.structLayout(genLayoutList(originalMembers, alignment.toLong)*)
128+
.structLayout(genLayoutList(originalMembers, alignment)*)
129129
.nn
130130
.withName(sd.clazz.getName())
131131
.nn

j19/src/fr/hammons/slinc/modules/DescriptorModule19.scala

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ given descriptorModule19: DescriptorModule with
3232
.structLayout(
3333
genLayoutList(
3434
sd.members.map(toMemoryLayout),
35-
alignmentOf(sd).toLong
35+
alignmentOf(sd)
3636
)*
3737
)
3838
.nn
@@ -41,29 +41,29 @@ given descriptorModule19: DescriptorModule with
4141

4242
def genLayoutList(
4343
layouts: Seq[MemoryLayout],
44-
alignment: Long
44+
alignment: Bytes
4545
): Seq[MemoryLayout] =
4646
val (vector, currentLocation) =
47-
layouts.foldLeft(Seq.empty[MemoryLayout] -> 0L) {
47+
layouts.foldLeft(Seq.empty[MemoryLayout] -> Bytes(0)) {
4848
case ((vector, currentLocation), layout) =>
49-
val thisAlignment = layout.byteAlignment()
49+
val thisAlignment = Bytes(layout.byteAlignment())
5050
val misalignment = currentLocation % thisAlignment
5151
val toAdd =
52-
if misalignment == 0 then Seq(layout)
52+
if misalignment == Bytes(0) then Seq(layout)
5353
else
5454
val paddingNeeded = thisAlignment - misalignment
5555

5656
Seq(
57-
MemoryLayout.paddingLayout(paddingNeeded * 8).nn,
57+
MemoryLayout.paddingLayout(paddingNeeded.toBits).nn,
5858
layout
5959
)
60-
(vector ++ toAdd, currentLocation + toAdd.view.map(_.byteSize()).sum)
60+
(vector ++ toAdd, currentLocation + Bytes(toAdd.view.map(_.byteSize()).sum))
6161
}
6262
val misalignment = currentLocation % alignment
6363
vector ++ (
64-
if misalignment != 0 then
64+
if misalignment != Bytes(0) then
6565
Seq(
66-
MemoryLayout.paddingLayout((alignment - misalignment) * 8).nn
66+
MemoryLayout.paddingLayout((alignment - misalignment).toBits).nn
6767
)
6868
else Seq.empty
6969
)
@@ -83,7 +83,7 @@ given descriptorModule19: DescriptorModule with
8383
sd, {
8484
val ll = genLayoutList(
8585
sd.members.view.map(toMemoryLayout).toSeq,
86-
sd.members.view.map(_.descriptor).map(alignmentOf).map(_.toLong).max
86+
sd.members.view.map(_.descriptor).map(alignmentOf).max
8787
)
8888
IArray.from(
8989
ll match
@@ -125,7 +125,4 @@ given descriptorModule19: DescriptorModule with
125125
case FloatDescriptor => Bytes(4)
126126
case DoubleDescriptor => Bytes(8)
127127
case PtrDescriptor => Bytes(ValueLayout.ADDRESS.nn.byteAlignment())
128-
case sd: StructDescriptor =>
129-
Bytes(
130-
sd.members.view.map(_.descriptor).map(alignmentOf).map(_.toLong).max
131-
)
128+
case sd: StructDescriptor => sd.members.view.map(_.descriptor).map(alignmentOf).max

0 commit comments

Comments
 (0)