Skip to content

Commit bc6848d

Browse files
committed
Adds conditional byteswapping intrinsics
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 (they should be exposed as a single function / trait) I thought they would fit here.
1 parent a4cc34f commit bc6848d

File tree

1 file changed

+14
-0
lines changed

1 file changed

+14
-0
lines changed

src/libstd/unstable/intrinsics.rs

+14
Original file line numberDiff line numberDiff line change
@@ -238,3 +238,17 @@ pub extern "rust-intrinsic" {
238238
pub fn bswap32(x: i32) -> i32;
239239
pub fn bswap64(x: i64) -> i64;
240240
}
241+
242+
#[cfg(target_endian = "little")] pub fn to_le16(x: i16) -> i16 { x }
243+
#[cfg(target_endian = "big")] pub fn to_le16(x: i16) -> i16 { unsafe { bswap16(x) } }
244+
#[cfg(target_endian = "little")] pub fn to_le32(x: i32) -> i32 { x }
245+
#[cfg(target_endian = "big")] pub fn to_le32(x: i32) -> i32 { unsafe { bswap32(x) } }
246+
#[cfg(target_endian = "little")] pub fn to_le64(x: i64) -> i64 { x }
247+
#[cfg(target_endian = "big")] pub fn to_le64(x: i64) -> i64 { unsafe { bswap64(x) } }
248+
249+
#[cfg(target_endian = "little")] pub fn to_be16(x: i16) -> i16 { unsafe { bswap16(x) } }
250+
#[cfg(target_endian = "big")] pub fn to_be16(x: i16) -> i16 { x }
251+
#[cfg(target_endian = "little")] pub fn to_be32(x: i32) -> i32 { unsafe { bswap32(x) } }
252+
#[cfg(target_endian = "big")] pub fn to_be32(x: i32) -> i32 { x }
253+
#[cfg(target_endian = "little")] pub fn to_be64(x: i64) -> i64 { unsafe { bswap64(x) } }
254+
#[cfg(target_endian = "big")] pub fn to_be64(x: i64) -> i64 { x }

0 commit comments

Comments
 (0)