Skip to content

Commit

Permalink
[stdlib] Move the Int.from_bytes() big_endian arg to a comptime param
Browse files Browse the repository at this point in the history
Signed-off-by: Manuel Saelices <[email protected]>
  • Loading branch information
msaelices committed Nov 13, 2024
1 parent 36be16f commit 15fb5be
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 20 deletions.
16 changes: 7 additions & 9 deletions stdlib/src/builtin/int.mojo
Original file line number Diff line number Diff line change
Expand Up @@ -1178,16 +1178,16 @@ struct Int(

@staticmethod
fn from_bytes[
type: DType
](bytes: Span[Byte], big_endian: Bool = False) raises -> Self:
type: DType, big_endian: Bool = False
](bytes: Span[Byte]) raises -> Self:
"""Converts a byte array to an integer.
Args:
bytes: The byte array to convert.
big_endian: Whether the byte array is big-endian.
Parameters:
type: The type of the integer.
big_endian: Whether the byte array is big-endian.
Returns:
The integer value.
Expand All @@ -1199,12 +1199,10 @@ struct Int(
var value = type_ptr[]

@parameter
if is_big_endian():
if not big_endian:
value = byte_swap(value)
else:
if big_endian:
value = byte_swap(value)
if is_big_endian() and not big_endian:
value = byte_swap(value)
elif not is_big_endian() and big_endian:
value = byte_swap(value)
return int(value)

@always_inline("nodebug")
Expand Down
22 changes: 11 additions & 11 deletions stdlib/test/builtin/test_int.mojo
Original file line number Diff line number Diff line change
Expand Up @@ -249,40 +249,40 @@ def test_conversion_from_python():


def test_from_bytes():
assert_equal(Int.from_bytes[DType.int16](Bytes(0, 16), big_endian=True), 16)
assert_equal(Int.from_bytes[DType.int16, big_endian=True](Bytes(0, 16)), 16)
assert_equal(
Int.from_bytes[DType.int16](Bytes(0, 16), big_endian=False), 4096
Int.from_bytes[DType.int16, big_endian=False](Bytes(0, 16)), 4096
)
assert_equal(
Int.from_bytes[DType.int16](Bytes(252, 0), big_endian=True), -1024
Int.from_bytes[DType.int16, big_endian=True](Bytes(252, 0)), -1024
)
assert_equal(
Int.from_bytes[DType.uint16](Bytes(252, 0), big_endian=True), 64512
Int.from_bytes[DType.uint16, big_endian=True](Bytes(252, 0)), 64512
)
assert_equal(
Int.from_bytes[DType.int16](Bytes(252, 0), big_endian=False), 252
Int.from_bytes[DType.int16, big_endian=False](Bytes(252, 0)), 252
)
assert_equal(
Int.from_bytes[DType.int32](Bytes(0, 0, 0, 1), big_endian=True), 1
Int.from_bytes[DType.int32, big_endian=True](Bytes(0, 0, 0, 1)), 1
)
assert_equal(
Int.from_bytes[DType.int32](Bytes(0, 0, 0, 1), big_endian=False),
Int.from_bytes[DType.int32, big_endian=False](Bytes(0, 0, 0, 1)),
16777216,
)
assert_equal(
Int.from_bytes[DType.int32](Bytes(1, 0, 0, 0), big_endian=True),
Int.from_bytes[DType.int32, big_endian=True](Bytes(1, 0, 0, 0)),
16777216,
)
assert_equal(
Int.from_bytes[DType.int32](Bytes(1, 0, 0, 1), big_endian=True),
Int.from_bytes[DType.int32, big_endian=True](Bytes(1, 0, 0, 1)),
16777217,
)
assert_equal(
Int.from_bytes[DType.int32](Bytes(1, 0, 0, 1), big_endian=False),
Int.from_bytes[DType.int32, big_endian=False](Bytes(1, 0, 0, 1)),
16777217,
)
assert_equal(
Int.from_bytes[DType.int32](Bytes(255, 0, 0, 0), big_endian=True),
Int.from_bytes[DType.int32, big_endian=True](Bytes(255, 0, 0, 0)),
-16777216,
)

Expand Down

0 comments on commit 15fb5be

Please sign in to comment.