Skip to content

Commit

Permalink
Remove the DType parameter from Int.from_bytes() and Int.as_bytes()
Browse files Browse the repository at this point in the history
Signed-off-by: Manuel Saelices <[email protected]>
  • Loading branch information
msaelices committed Dec 21, 2024
1 parent 40b9fc0 commit 98c29fc
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 28 deletions.
14 changes: 6 additions & 8 deletions stdlib/src/builtin/int.mojo
Original file line number Diff line number Diff line change
Expand Up @@ -1215,35 +1215,33 @@ struct Int(

@staticmethod
fn from_bytes[
D: DType, big_endian: Bool = False
](bytes: InlineArray[Byte, D.sizeof()]) -> Self:
big_endian: Bool = False
](bytes: InlineArray[Byte, DType.int64.sizeof()]) -> Self:
"""Converts a byte array to an integer.
Args:
bytes: The byte array to convert.
Parameters:
D: The type of the integer.
big_endian: Whether the byte array is big-endian.
Returns:
The integer value.
"""
return int(Scalar[D].from_bytes[big_endian](bytes))
return int(Scalar[DType.int64].from_bytes[big_endian](bytes))

fn as_bytes[
D: DType, big_endian: Bool = False
](self) -> InlineArray[Byte, D.sizeof()]:
big_endian: Bool = False
](self) -> InlineArray[Byte, DType.int64.sizeof()]:
"""Convert the integer to a byte array.
Parameters:
D: The type of the integer.
big_endian: Whether the byte array should be big-endian.
Returns:
The byte array.
"""
var value = Scalar[D](self)
var value = Scalar[DType.int64](self)
return value.as_bytes[big_endian]()

@always_inline("nodebug")
Expand Down
41 changes: 21 additions & 20 deletions stdlib/test/builtin/test_int.mojo
Original file line number Diff line number Diff line change
Expand Up @@ -246,57 +246,58 @@ def test_conversion_from_python():


def test_from_bytes_as_bytes():
alias TwoBytes = InlineArray[Byte, DType.int16.sizeof()]
alias TwoUBytes = InlineArray[Byte, DType.uint16.sizeof()]
alias FourBytes = InlineArray[Byte, DType.int32.sizeof()]
alias EightBytes = InlineArray[Byte, DType.int64.sizeof()]

assert_equal(
Int.from_bytes[DType.int16, big_endian=True](TwoBytes(0, 16)), 16
Int.from_bytes[big_endian=True](EightBytes(0, 0, 0, 0, 0, 0, 0, 16)), 16
)
assert_equal(
Int.from_bytes[DType.int16, big_endian=False](TwoBytes(0, 16)), 4096
Int.from_bytes[big_endian=False](EightBytes(0, 0, 0, 0, 0, 0, 1, 0)),
281474976710656,
)
assert_equal(
Int.from_bytes[DType.int16, big_endian=True](TwoBytes(252, 0)), -1024
Int.from_bytes[big_endian=False](EightBytes(0, 16, 0, 0, 0, 0, 0, 0)),
4096,
)
assert_equal(
Int.from_bytes[DType.uint16, big_endian=True](TwoUBytes(252, 0)), 64512
Int.from_bytes[big_endian=False](EightBytes(252, 0, 0, 0, 0, 0, 0, 0)),
252,
)
assert_equal(
Int.from_bytes[DType.int16, big_endian=False](TwoBytes(252, 0)), 252
Int.from_bytes[big_endian=True](EightBytes(102, 0, 0, 0, 0, 0, 0, 0)),
7349874591868649472,
)
assert_equal(
Int.from_bytes[DType.int32, big_endian=True](FourBytes(0, 0, 0, 1)), 1
Int.from_bytes[big_endian=False](EightBytes(252, 0, 0, 0, 0, 0, 0, 0)),
252,
)
assert_equal(
Int.from_bytes[DType.int32, big_endian=False](FourBytes(0, 0, 0, 1)),
Int.from_bytes[big_endian=False](EightBytes(0, 0, 0, 1, 0, 0, 0, 0)),
16777216,
)
assert_equal(
Int.from_bytes[DType.int32, big_endian=True](FourBytes(1, 0, 0, 0)),
16777216,
Int.from_bytes[big_endian=True](EightBytes(1, 0, 0, 0, 0, 0, 0, 0)),
72057594037927936,
)
assert_equal(
Int.from_bytes[DType.int32, big_endian=True](FourBytes(1, 0, 0, 1)),
16777217,
Int.from_bytes[big_endian=True](EightBytes(1, 0, 0, 1, 0, 0, 0, 0)),
72057598332895232,
)
assert_equal(
Int.from_bytes[DType.int32, big_endian=False](FourBytes(1, 0, 0, 1)),
Int.from_bytes[big_endian=False](EightBytes(1, 0, 0, 1, 0, 0, 0, 0)),
16777217,
)
assert_equal(
Int.from_bytes[DType.int32, big_endian=True](FourBytes(255, 0, 0, 0)),
-16777216,
Int.from_bytes[big_endian=True](EightBytes(255, 0, 0, 0, 0, 0, 0, 0)),
-72057594037927936,
)
for x_ref in List[Int](10, 100, -12, 0, 1, -1, 1000, -1000):
x = x_ref[]

@parameter
for b in range(2):
assert_equal(
Int.from_bytes[DType.int16, big_endian=b](
Int(x).as_bytes[DType.int16, big_endian=b]()
),
Int.from_bytes[big_endian=b](Int(x).as_bytes[big_endian=b]()),
x,
)

Expand Down

0 comments on commit 98c29fc

Please sign in to comment.