Skip to content

Commit 4ad1fcc

Browse files
Yu-zhclaude
authored andcommitted
refactor(builtin): move bitstring fastpath helpers into bytes_unsafe.mbt
The private buffer_to_fixedarray (%identity) + intrinsic-backed fixedarray_read_* helpers now live next to the rest of the unsafe byte I/O, keeping bitstring.mbt focused on the bit-level extract surface. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
1 parent b0b3c97 commit 4ad1fcc

2 files changed

Lines changed: 77 additions & 67 deletions

File tree

builtin/bitstring.mbt

Lines changed: 4 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -1041,75 +1041,12 @@ pub fn Bytes::unsafe_extract_bytesview(
10411041
// Fixed-size reads at a byte offset, used when the bit offset is statically
10421042
// known to be a multiple of 8. These take `byte_offset` (in bytes), not bits.
10431043
//
1044-
// Provided for `ArrayView[Byte]` and `BytesView`.
1044+
// Provided for `ArrayView[Byte]` and `BytesView`. The private
1045+
// `buffer_to_fixedarray` + `fixedarray_read_*` helpers that back the
1046+
// `ArrayView[Byte]` side live in `bytes_unsafe.mbt` alongside the other
1047+
// intrinsic-backed byte I/O.
10451048
//--------------------------------
10461049

1047-
///|
1048-
/// Zero-cost reinterpretation of the backing buffer as `FixedArray[Byte]`, so
1049-
/// the intrinsic-backed `fixedarray_read_*` helpers below can run against the
1050-
/// underlying storage of an `ArrayView[Byte]`.
1051-
fn buffer_to_fixedarray(buf : UninitializedArray[Byte]) -> FixedArray[Byte] = "%identity"
1052-
1053-
///|
1054-
#intrinsic("%bytes.unsafe_read_uint16_le")
1055-
fn fixedarray_read_uint16_le(bytes : FixedArray[Byte], index : Int) -> UInt16 {
1056-
for i in 0..<=1; result = (0 : UInt16) {
1057-
continue result | (bytes.unsafe_get(i + index).to_uint16() << (8 * i))
1058-
} nobreak {
1059-
result
1060-
}
1061-
}
1062-
1063-
///|
1064-
#intrinsic("%bytes.unsafe_read_uint16_be")
1065-
fn fixedarray_read_uint16_be(bytes : FixedArray[Byte], index : Int) -> UInt16 {
1066-
for i in 0..<=1; result = (0 : UInt16) {
1067-
continue result | (bytes.unsafe_get(i + index).to_uint16() << (8 * (1 - i)))
1068-
} nobreak {
1069-
result
1070-
}
1071-
}
1072-
1073-
///|
1074-
#intrinsic("%bytes.unsafe_read_uint32_le")
1075-
fn fixedarray_read_uint32_le(bytes : FixedArray[Byte], index : Int) -> UInt {
1076-
for i in 0..<=3; result = (0 : UInt) {
1077-
continue result | (bytes.unsafe_get(i + index).to_uint() << (8 * i))
1078-
} nobreak {
1079-
result
1080-
}
1081-
}
1082-
1083-
///|
1084-
#intrinsic("%bytes.unsafe_read_uint32_be")
1085-
fn fixedarray_read_uint32_be(bytes : FixedArray[Byte], index : Int) -> UInt {
1086-
for i in 0..<=3; result = (0 : UInt) {
1087-
continue result | (bytes.unsafe_get(i + index).to_uint() << (8 * (3 - i)))
1088-
} nobreak {
1089-
result
1090-
}
1091-
}
1092-
1093-
///|
1094-
#intrinsic("%bytes.unsafe_read_uint64_le")
1095-
fn fixedarray_read_uint64_le(bytes : FixedArray[Byte], index : Int) -> UInt64 {
1096-
for i in 0..<=7; result = (0 : UInt64) {
1097-
continue result | (bytes.unsafe_get(i + index).to_uint64() << (8 * i))
1098-
} nobreak {
1099-
result
1100-
}
1101-
}
1102-
1103-
///|
1104-
#intrinsic("%bytes.unsafe_read_uint64_be")
1105-
fn fixedarray_read_uint64_be(bytes : FixedArray[Byte], index : Int) -> UInt64 {
1106-
for i in 0..<=7; result = (0 : UInt64) {
1107-
continue result | (bytes.unsafe_get(i + index).to_uint64() << (8 * (7 - i)))
1108-
} nobreak {
1109-
result
1110-
}
1111-
}
1112-
11131050
//--------------------------------
11141051
// ArrayView[Byte] fastpaths
11151052
//--------------------------------

builtin/bytes_unsafe.mbt

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -532,3 +532,76 @@ pub fn BytesView::unsafe_read_uint16_be(
532532
}
533533

534534
// #endregion
535+
// #region FixedArray fastpath helpers
536+
//
537+
// Private intrinsic-backed reads on FixedArray[Byte], used from
538+
// `bitstring.mbt` to power the byte-aligned fastpath methods on
539+
// ArrayView[Byte]. Not exported.
540+
541+
///|
542+
/// Zero-cost reinterpretation of the backing buffer as `FixedArray[Byte]`, so
543+
/// the intrinsic-backed `fixedarray_read_*` helpers below can run against the
544+
/// underlying storage of an `ArrayView[Byte]`.
545+
fn buffer_to_fixedarray(buf : UninitializedArray[Byte]) -> FixedArray[Byte] = "%identity"
546+
547+
///|
548+
#intrinsic("%bytes.unsafe_read_uint16_le")
549+
fn fixedarray_read_uint16_le(bytes : FixedArray[Byte], index : Int) -> UInt16 {
550+
for i in 0..<=1; result = (0 : UInt16) {
551+
continue result | (bytes.unsafe_get(i + index).to_uint16() << (8 * i))
552+
} nobreak {
553+
result
554+
}
555+
}
556+
557+
///|
558+
#intrinsic("%bytes.unsafe_read_uint16_be")
559+
fn fixedarray_read_uint16_be(bytes : FixedArray[Byte], index : Int) -> UInt16 {
560+
for i in 0..<=1; result = (0 : UInt16) {
561+
continue result | (bytes.unsafe_get(i + index).to_uint16() << (8 * (1 - i)))
562+
} nobreak {
563+
result
564+
}
565+
}
566+
567+
///|
568+
#intrinsic("%bytes.unsafe_read_uint32_le")
569+
fn fixedarray_read_uint32_le(bytes : FixedArray[Byte], index : Int) -> UInt {
570+
for i in 0..<=3; result = (0 : UInt) {
571+
continue result | (bytes.unsafe_get(i + index).to_uint() << (8 * i))
572+
} nobreak {
573+
result
574+
}
575+
}
576+
577+
///|
578+
#intrinsic("%bytes.unsafe_read_uint32_be")
579+
fn fixedarray_read_uint32_be(bytes : FixedArray[Byte], index : Int) -> UInt {
580+
for i in 0..<=3; result = (0 : UInt) {
581+
continue result | (bytes.unsafe_get(i + index).to_uint() << (8 * (3 - i)))
582+
} nobreak {
583+
result
584+
}
585+
}
586+
587+
///|
588+
#intrinsic("%bytes.unsafe_read_uint64_le")
589+
fn fixedarray_read_uint64_le(bytes : FixedArray[Byte], index : Int) -> UInt64 {
590+
for i in 0..<=7; result = (0 : UInt64) {
591+
continue result | (bytes.unsafe_get(i + index).to_uint64() << (8 * i))
592+
} nobreak {
593+
result
594+
}
595+
}
596+
597+
///|
598+
#intrinsic("%bytes.unsafe_read_uint64_be")
599+
fn fixedarray_read_uint64_be(bytes : FixedArray[Byte], index : Int) -> UInt64 {
600+
for i in 0..<=7; result = (0 : UInt64) {
601+
continue result | (bytes.unsafe_get(i + index).to_uint64() << (8 * (7 - i)))
602+
} nobreak {
603+
result
604+
}
605+
}
606+
607+
// #endregion

0 commit comments

Comments
 (0)