Skip to content

Commit

Permalink
feat: add UInt::reinterpret_as_float() (#1130)
Browse files Browse the repository at this point in the history
  • Loading branch information
qazxcdswe123 authored Oct 17, 2024
1 parent 6f91a8e commit e5edbd4
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 0 deletions.
1 change: 1 addition & 0 deletions builtin/builtin.mbti
Original file line number Diff line number Diff line change
Expand Up @@ -493,6 +493,7 @@ impl UInt {
op_shr(UInt, Int) -> UInt
op_sub(UInt, UInt) -> UInt
popcnt(UInt) -> Int
reinterpret_as_float(UInt) -> Float
reinterpret_as_int(UInt) -> Int
shl(UInt, Int) -> UInt //deprecated
shr(UInt, Int) -> UInt //deprecated
Expand Down
2 changes: 2 additions & 0 deletions builtin/intrinsics.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,8 @@ pub fn Int::to_float(self : Int) -> Float = "%i32.to_f32"

pub fn Int::reinterpret_as_float(self : Int) -> Float = "%i32.to_f32_reinterpret"

pub fn UInt::reinterpret_as_float(self : UInt) -> Float = "%i32.to_f32_reinterpret"

pub fn Byte::to_float(self : Byte) -> Float = "%byte.to_f32"

pub fn Double::to_float(self : Double) -> Float = "%f64.to_f32"
Expand Down
21 changes: 21 additions & 0 deletions builtin/intrinsics_test.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -412,3 +412,24 @@ test "Int::to_uint64 - Random Values" {
inspect!(Int::to_uint64(1234567890), content="1234567890")
inspect!(Int::to_uint64(-1234567890), content="18446744072474983726")
}

test "UInt::reinterpret_as_float roundtrip" {
let values = [
0x00000000U, // zero
0x80000000U, // negative zero
0x3F800000U, // 1.0
0xBF800000U, // -1.0
0x7F800000U, // positive infinity
0xFF800000U, // negative infinity
0x7FC00000U, // NaN
0x00800000U, // smallest positive normal float
0x7F7FFFFFU, // largest positive normal float
0x00000001U, // smallest positive subnormal float
0x007FFFFFU, // largest positive subnormal float
]
for value in values {
let float_value = value.reinterpret_as_float()
let roundtrip_value = Float::reinterpret_as_uint(float_value)
assert_eq!(roundtrip_value, value)
}
}

0 comments on commit e5edbd4

Please sign in to comment.