Skip to content

Commit 3abc633

Browse files
committed
Add byteswap intrinsics for converting from big/little to host endian
These are similar to the ntoh[s|l] functions in C and have the style of from_[be|le][16|32|64].
1 parent c34ef5d commit 3abc633

File tree

1 file changed

+13
-0
lines changed

1 file changed

+13
-0
lines changed

src/libstd/unstable/intrinsics.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -500,6 +500,19 @@ extern "rust-intrinsic" {
500500
#[cfg(target_endian = "little")] pub fn to_be64(x: i64) -> i64 { unsafe { bswap64(x) } }
501501
#[cfg(target_endian = "big")] pub fn to_be64(x: i64) -> i64 { x }
502502

503+
#[cfg(target_endian = "little")] pub fn from_le16(x: i16) -> i16 { x }
504+
#[cfg(target_endian = "big")] pub fn from_le16(x: i16) -> i16 { unsafe { bswap16(x) } }
505+
#[cfg(target_endian = "little")] pub fn from_le32(x: i32) -> i32 { x }
506+
#[cfg(target_endian = "big")] pub fn from_le32(x: i32) -> i32 { unsafe { bswap32(x) } }
507+
#[cfg(target_endian = "little")] pub fn from_le64(x: i64) -> i64 { x }
508+
#[cfg(target_endian = "big")] pub fn from_le64(x: i64) -> i64 { unsafe { bswap64(x) } }
509+
510+
#[cfg(target_endian = "little")] pub fn from_be16(x: i16) -> i16 { unsafe { bswap16(x) } }
511+
#[cfg(target_endian = "big")] pub fn from_be16(x: i16) -> i16 { x }
512+
#[cfg(target_endian = "little")] pub fn from_be32(x: i32) -> i32 { unsafe { bswap32(x) } }
513+
#[cfg(target_endian = "big")] pub fn from_be32(x: i32) -> i32 { x }
514+
#[cfg(target_endian = "little")] pub fn from_be64(x: i64) -> i64 { unsafe { bswap64(x) } }
515+
#[cfg(target_endian = "big")] pub fn from_be64(x: i64) -> i64 { x }
503516

504517
/// `TypeId` represents a globally unique identifier for a type
505518
#[lang="type_id"] // This needs to be kept in lockstep with the code in trans/intrinsic.rs and

0 commit comments

Comments
 (0)