Skip to content

Commit

Permalink
auto merge of #7194 : jensnockert/rust/endian, r=cmr
Browse files Browse the repository at this point in the history
They simply byte-swap an integer to a specific endian, like the hton* functions in C.

These intrinsics are synthesized, so maybe they should be in another file. But since they are just a single line of code each, based on the bswap intrinsics and aren't really intended for public consumption I thought they would fit in the intrinsics file.

The next step working on this could be to expose a trait / generic function for byteswapping.
  • Loading branch information
bors committed Jul 6, 2013
2 parents e9897cd + bc6848d commit 3e933b1
Showing 1 changed file with 14 additions and 0 deletions.
14 changes: 14 additions & 0 deletions src/libstd/unstable/intrinsics.rs
Expand Up @@ -417,3 +417,17 @@ pub extern "rust-intrinsic" {
pub fn bswap32(x: i32) -> i32;
pub fn bswap64(x: i64) -> i64;
}

#[cfg(target_endian = "little")] pub fn to_le16(x: i16) -> i16 { x }
#[cfg(target_endian = "big")] pub fn to_le16(x: i16) -> i16 { unsafe { bswap16(x) } }
#[cfg(target_endian = "little")] pub fn to_le32(x: i32) -> i32 { x }
#[cfg(target_endian = "big")] pub fn to_le32(x: i32) -> i32 { unsafe { bswap32(x) } }
#[cfg(target_endian = "little")] pub fn to_le64(x: i64) -> i64 { x }
#[cfg(target_endian = "big")] pub fn to_le64(x: i64) -> i64 { unsafe { bswap64(x) } }

#[cfg(target_endian = "little")] pub fn to_be16(x: i16) -> i16 { unsafe { bswap16(x) } }
#[cfg(target_endian = "big")] pub fn to_be16(x: i16) -> i16 { x }
#[cfg(target_endian = "little")] pub fn to_be32(x: i32) -> i32 { unsafe { bswap32(x) } }
#[cfg(target_endian = "big")] pub fn to_be32(x: i32) -> i32 { x }
#[cfg(target_endian = "little")] pub fn to_be64(x: i64) -> i64 { unsafe { bswap64(x) } }
#[cfg(target_endian = "big")] pub fn to_be64(x: i64) -> i64 { x }

0 comments on commit 3e933b1

Please sign in to comment.