Skip to content

Commit

Permalink
Move the logic to SIMD so we can call it with whatever scalar we want
Browse files Browse the repository at this point in the history
Signed-off-by: Manuel Saelices <[email protected]>
  • Loading branch information
msaelices committed Dec 16, 2024
1 parent b0bc485 commit 776b4df
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 17 deletions.
19 changes: 3 additions & 16 deletions stdlib/src/builtin/int.mojo
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,14 @@ from math import Ceilable, CeilDivable, Floorable, Truncable
from sys import bitwidthof

from builtin.io import _snprintf
from memory import UnsafePointer
from memory import Span, UnsafePointer
from python import Python, PythonObject
from python._cpython import Py_ssize_t
from memory import memcpy, UnsafePointer

from sys import is_big_endian, bitwidthof

from utils import Span, Writable, Writer
from utils import Writable, Writer
from utils._select import _select_register_value as select
from utils._visualizers import lldb_formatter_wrapping_type

Expand Down Expand Up @@ -1233,20 +1233,7 @@ struct Int(
Returns:
The integer value.
"""
if D.sizeof() != len(bytes):
raise Error("Byte array size does not match the integer size.")

var ptr: UnsafePointer[Scalar[D]] = bytes.unsafe_ptr().bitcast[
Scalar[D]
]()
var value = ptr[]

@parameter
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)
return int(Scalar[D].from_bytes[big_endian](bytes))

fn as_bytes[D: DType, big_endian: Bool = False](self) -> List[Byte]:
"""Convert the integer to a byte array.
Expand Down
33 changes: 32 additions & 1 deletion stdlib/src/builtin/simd.mojo
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ from sys import (
bitwidthof,
has_neon,
is_amd_gpu,
is_big_endian,
is_gpu,
is_nvidia_gpu,
is_x86,
Expand All @@ -44,7 +45,7 @@ from sys import (
from sys._assembly import inlined_assembly
from sys.info import _current_arch, _is_sm_8x, _is_sm_9x

from bit import pop_count
from bit import pop_count, byte_swap
from builtin._format_float import _write_float
from builtin.dtype import _uint_type_of_width
from builtin.format_int import _try_write_int
Expand Down Expand Up @@ -1879,6 +1880,36 @@ struct SIMD[type: DType, size: Int](

return bitcast[_integral_type_of[type](), size](self).cast[int_dtype]()

@staticmethod
fn from_bytes[
big_endian: Bool = False
](bytes: Span[Byte]) raises -> Scalar[type]:
"""Converts a byte array to an integer.
Args:
bytes: The byte array to convert.
Parameters:
big_endian: Whether the byte array is big-endian.
Returns:
The integer value.
"""
if type.sizeof() != len(bytes):
raise Error("Byte array size does not match the integer size.")

var ptr: UnsafePointer[Scalar[type]] = bytes.unsafe_ptr().bitcast[
Scalar[type]
]()
var value = ptr[]

@parameter
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 value

fn _floor_ceil_trunc_impl[intrinsic: StringLiteral](self) -> Self:
constrained[
intrinsic == "llvm.floor"
Expand Down

0 comments on commit 776b4df

Please sign in to comment.