Skip to content

Commit 56e30bb

Browse files
Yu-zhclaude
andauthored
fix(builtin): uint16 aligned fastpath returns UInt, not UInt16 (#3468)
* fix(builtin): uint16 fastpath returns UInt, not UInt16 The uint16 aligned fastpaths now return UInt (matching the wider unsafe_extract_uint_le/be convention), so callers don't have to widen. Drops the .to_uint() step from the int16 variants and the test cross-checks. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com> * test(builtin): swap deprecated UInt::to_int for reinterpret_as_int Four inspect lines still called the deprecated UInt.to_int() on the uint16 fastpath results after they started returning UInt. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com> * style: moon fmt Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
1 parent 4ad1fcc commit 56e30bb

2 files changed

Lines changed: 32 additions & 20 deletions

File tree

builtin/bitstring.mbt

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1077,11 +1077,11 @@ pub fn ArrayView::unsafe_extract_byte_signed_aligned(
10771077
pub fn ArrayView::unsafe_extract_uint16_le_aligned(
10781078
bs : ArrayView[Byte],
10791079
byte_offset : Int,
1080-
) -> UInt16 {
1080+
) -> UInt {
10811081
fixedarray_read_uint16_le(
10821082
buffer_to_fixedarray(bs.buf()),
10831083
bs.start() + byte_offset,
1084-
)
1084+
).to_uint()
10851085
}
10861086

10871087
///|
@@ -1090,11 +1090,11 @@ pub fn ArrayView::unsafe_extract_uint16_le_aligned(
10901090
pub fn ArrayView::unsafe_extract_uint16_be_aligned(
10911091
bs : ArrayView[Byte],
10921092
byte_offset : Int,
1093-
) -> UInt16 {
1093+
) -> UInt {
10941094
fixedarray_read_uint16_be(
10951095
buffer_to_fixedarray(bs.buf()),
10961096
bs.start() + byte_offset,
1097-
)
1097+
).to_uint()
10981098
}
10991099

11001100
///|
@@ -1104,7 +1104,7 @@ pub fn ArrayView::unsafe_extract_int16_le_aligned(
11041104
bs : ArrayView[Byte],
11051105
byte_offset : Int,
11061106
) -> Int {
1107-
bs.unsafe_extract_uint16_le_aligned(byte_offset).to_uint().extend_sign(16)
1107+
bs.unsafe_extract_uint16_le_aligned(byte_offset).extend_sign(16)
11081108
}
11091109

11101110
///|
@@ -1114,7 +1114,7 @@ pub fn ArrayView::unsafe_extract_int16_be_aligned(
11141114
bs : ArrayView[Byte],
11151115
byte_offset : Int,
11161116
) -> Int {
1117-
bs.unsafe_extract_uint16_be_aligned(byte_offset).to_uint().extend_sign(16)
1117+
bs.unsafe_extract_uint16_be_aligned(byte_offset).extend_sign(16)
11181118
}
11191119

11201120
///|
@@ -1239,8 +1239,8 @@ pub fn BytesView::unsafe_extract_byte_signed_aligned(
12391239
pub fn BytesView::unsafe_extract_uint16_le_aligned(
12401240
bs : BytesView,
12411241
byte_offset : Int,
1242-
) -> UInt16 {
1243-
bs.unsafe_read_uint16_le(byte_offset)
1242+
) -> UInt {
1243+
bs.unsafe_read_uint16_le(byte_offset).to_uint()
12441244
}
12451245

12461246
///|
@@ -1249,8 +1249,8 @@ pub fn BytesView::unsafe_extract_uint16_le_aligned(
12491249
pub fn BytesView::unsafe_extract_uint16_be_aligned(
12501250
bs : BytesView,
12511251
byte_offset : Int,
1252-
) -> UInt16 {
1253-
bs.unsafe_read_uint16_be(byte_offset)
1252+
) -> UInt {
1253+
bs.unsafe_read_uint16_be(byte_offset).to_uint()
12541254
}
12551255

12561256
///|
@@ -1260,7 +1260,7 @@ pub fn BytesView::unsafe_extract_int16_le_aligned(
12601260
bs : BytesView,
12611261
byte_offset : Int,
12621262
) -> Int {
1263-
bs.unsafe_read_uint16_le(byte_offset).to_uint().extend_sign(16)
1263+
bs.unsafe_extract_uint16_le_aligned(byte_offset).extend_sign(16)
12641264
}
12651265

12661266
///|
@@ -1270,7 +1270,7 @@ pub fn BytesView::unsafe_extract_int16_be_aligned(
12701270
bs : BytesView,
12711271
byte_offset : Int,
12721272
) -> Int {
1273-
bs.unsafe_read_uint16_be(byte_offset).to_uint().extend_sign(16)
1273+
bs.unsafe_extract_uint16_be_aligned(byte_offset).extend_sign(16)
12741274
}
12751275

12761276
///|

builtin/bitstring_test.mbt

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -704,19 +704,19 @@ test "aligned fastpath cross-check" {
704704
// uint16 (2 bytes)
705705
for b in 0..<=(bytes.length() - 2) {
706706
assert_eq(
707-
aview.unsafe_extract_uint16_le_aligned(b).to_uint(),
707+
aview.unsafe_extract_uint16_le_aligned(b),
708708
aview.unsafe_extract_uint_le(b * 8, 16),
709709
)
710710
assert_eq(
711-
aview.unsafe_extract_uint16_be_aligned(b).to_uint(),
711+
aview.unsafe_extract_uint16_be_aligned(b),
712712
aview.unsafe_extract_uint_be(b * 8, 16),
713713
)
714714
assert_eq(
715-
bview.unsafe_extract_uint16_le_aligned(b).to_uint(),
715+
bview.unsafe_extract_uint16_le_aligned(b),
716716
bview.unsafe_extract_uint_le(b * 8, 16),
717717
)
718718
assert_eq(
719-
bview.unsafe_extract_uint16_be_aligned(b).to_uint(),
719+
bview.unsafe_extract_uint16_be_aligned(b),
720720
bview.unsafe_extract_uint_be(b * 8, 16),
721721
)
722722
assert_eq(
@@ -819,10 +819,22 @@ test "aligned fastpath known values" {
819819
inspect(bview.unsafe_extract_uint_be_aligned(0), content="16909060") // 0x01020304
820820
inspect(aview.unsafe_extract_uint_le_aligned(0), content="67305985")
821821
inspect(aview.unsafe_extract_uint_be_aligned(0), content="16909060")
822-
inspect(bview.unsafe_extract_uint16_le_aligned(0).to_int(), content="513") // 0x0201
823-
inspect(bview.unsafe_extract_uint16_be_aligned(0).to_int(), content="258") // 0x0102
824-
inspect(aview.unsafe_extract_uint16_le_aligned(0).to_int(), content="513")
825-
inspect(aview.unsafe_extract_uint16_be_aligned(0).to_int(), content="258")
822+
inspect(
823+
bview.unsafe_extract_uint16_le_aligned(0).reinterpret_as_int(),
824+
content="513",
825+
) // 0x0201
826+
inspect(
827+
bview.unsafe_extract_uint16_be_aligned(0).reinterpret_as_int(),
828+
content="258",
829+
) // 0x0102
830+
inspect(
831+
aview.unsafe_extract_uint16_le_aligned(0).reinterpret_as_int(),
832+
content="513",
833+
)
834+
inspect(
835+
aview.unsafe_extract_uint16_be_aligned(0).reinterpret_as_int(),
836+
content="258",
837+
)
826838
inspect(
827839
bview.unsafe_extract_uint64_le_aligned(0) == 0x0807060504030201UL,
828840
content="true",

0 commit comments

Comments
 (0)