Skip to content

Add byteswap intrinsics for converting from big/little to host endian #11265

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 2 additions & 9 deletions src/libextra/ebml.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,11 +122,9 @@ pub mod reader {
fail!("vint too big");
}

#[cfg(target_arch = "x86")]
#[cfg(target_arch = "x86_64")]
pub fn vuint_at(data: &[u8], start: uint) -> Res {
use std::ptr::offset;
use std::unstable::intrinsics::bswap32;
use std::unstable::intrinsics::from_be32;

if data.len() - start < 4 {
return vuint_at_slow(data, start);
Expand All @@ -136,7 +134,7 @@ pub mod reader {
let (ptr, _): (*u8, uint) = transmute(data);
let ptr = offset(ptr, start as int);
let ptr: *i32 = transmute(ptr);
let val = bswap32(*ptr);
let val = from_be32(*ptr);
let val: u32 = transmute(val);
if (val & 0x80000000) != 0 {
Res {
Expand All @@ -162,11 +160,6 @@ pub mod reader {
}
}

#[cfg(not(target_arch = "x86"), not(target_arch = "x86_64"))]
pub fn vuint_at(data: &[u8], start: uint) -> Res {
vuint_at_slow(data, start)
}

pub fn Doc<'a>(data: &'a [u8]) -> Doc<'a> {
Doc { data: data, start: 0u, end: data.len() }
}
Expand Down
8 changes: 2 additions & 6 deletions src/libnative/io/net.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,11 @@ use super::file::keep_going;
#[cfg(windows)] pub type sock_t = libc::SOCKET;
#[cfg(unix)] pub type sock_t = super::file::fd_t;

#[cfg(target_endian = "big")] pub fn htons(x: u16) -> u16 { x }
#[cfg(target_endian = "big")] pub fn ntohs(x: u16) -> u16 { x }
#[cfg(target_endian = "little")]
pub fn htons(u: u16) -> u16 {
unsafe { intrinsics::bswap16(u as i16) as u16 }
intrinsics::to_be16(u as i16) as u16
}
#[cfg(target_endian = "little")]
pub fn ntohs(u: u16) -> u16 {
unsafe { intrinsics::bswap16(u as i16) as u16 }
intrinsics::from_be16(u as i16) as u16
}

enum InAddr {
Expand Down
13 changes: 13 additions & 0 deletions src/libstd/unstable/intrinsics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -500,6 +500,19 @@ extern "rust-intrinsic" {
#[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 }

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

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

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