Skip to content

Bitwise casts

bitwise_casts

A collection of helpers for reinterpreting bits. Used in lowering and interpreting low-level dialects.

convert_f16_to_u16(value: float) -> int

Convert an IEEE 754 float to a raw unsigned integer representation.

Source code in xdsl/utils/bitwise_casts.py
10
11
12
13
14
15
def convert_f16_to_u16(value: float) -> int:
    """
    Convert an IEEE 754 float to a raw unsigned integer representation.
    """
    # using struct library as ctypes does not support half-precision floats
    return struct.unpack("<H", struct.pack("<e", value))[0]

convert_u16_to_f16(value: int) -> int

Convert an IEEE 754 float to a raw unsigned integer representation.

Source code in xdsl/utils/bitwise_casts.py
18
19
20
21
22
23
def convert_u16_to_f16(value: int) -> int:
    """
    Convert an IEEE 754 float to a raw unsigned integer representation.
    """
    # using struct library as ctypes does not support half-precision floats
    return struct.unpack("<e", struct.pack("<H", value))[0]

convert_f32_to_u32(value: float) -> int

Convert an IEEE 754 float to a raw unsigned integer representation.

Source code in xdsl/utils/bitwise_casts.py
26
27
28
29
30
31
32
def convert_f32_to_u32(value: float) -> int:
    """
    Convert an IEEE 754 float to a raw unsigned integer representation.
    """
    raw_float = ctypes.c_float(value)
    raw_int = ctypes.c_uint32.from_buffer(raw_float).value
    return raw_int

convert_u32_to_f32(value: int) -> float

Convert a raw 32-bit unsigned integer to IEEE 754 float representation.

Source code in xdsl/utils/bitwise_casts.py
35
36
37
38
39
40
41
def convert_u32_to_f32(value: int) -> float:
    """
    Convert a raw 32-bit unsigned integer to IEEE 754 float representation.
    """
    raw_int = ctypes.c_uint32(value)
    raw_float = ctypes.c_float.from_buffer(raw_int).value
    return raw_float

convert_f64_to_u64(value: float) -> int

Convert an IEEE 754 float to a raw unsigned integer representation.

Source code in xdsl/utils/bitwise_casts.py
44
45
46
47
48
49
50
def convert_f64_to_u64(value: float) -> int:
    """
    Convert an IEEE 754 float to a raw unsigned integer representation.
    """
    raw_float = ctypes.c_double(value)
    raw_int = ctypes.c_uint64.from_buffer(raw_float).value
    return raw_int

convert_u64_to_f64(value: int) -> float

Convert a raw 32-bit unsigned integer to IEEE 754 float representation.

Source code in xdsl/utils/bitwise_casts.py
53
54
55
56
57
58
59
def convert_u64_to_f64(value: int) -> float:
    """
    Convert a raw 32-bit unsigned integer to IEEE 754 float representation.
    """
    raw_int = ctypes.c_uint64(value)
    raw_float = ctypes.c_double.from_buffer(raw_int).value
    return raw_float

is_power_of_two(value: int) -> bool

Return True if an integer is a power of two. Powers of two have only one bit set to one

Source code in xdsl/utils/bitwise_casts.py
62
63
64
65
66
67
def is_power_of_two(value: int) -> bool:
    """
    Return True if an integer is a power of two.
    Powers of two have only one bit set to one
    """
    return (value > 0) and (value.bit_count() == 1)