Skip to content

Commit ee634e3

Browse files
committedJan 2, 2014
auto merge of #11265 : c-a/rust/byteswap_from, r=alexcrichton
This patchset adds intrinsics similar to the to_[be|le][16|32|64] intrinsics but for going in the reverse direction, e.g. from big/little endian to host endian. Implementation wise they do exactly the same as the corresponding to_* functions but I think it anyway make sense to have them since using the to_* functions in the reverse direction is not entirely intuitive. The first patch adds the intrinsics and the two following changes instances of bswap* to use the [to|from]_* intrinsics instead.
2 parents e34b1ea + 1c3c010 commit ee634e3

File tree

3 files changed

+17
-15
lines changed

3 files changed

+17
-15
lines changed
 

‎src/libextra/ebml.rs

+2-9
Original file line numberDiff line numberDiff line change
@@ -122,11 +122,9 @@ pub mod reader {
122122
fail!("vint too big");
123123
}
124124

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

131129
if data.len() - start < 4 {
132130
return vuint_at_slow(data, start);
@@ -136,7 +134,7 @@ pub mod reader {
136134
let (ptr, _): (*u8, uint) = transmute(data);
137135
let ptr = offset(ptr, start as int);
138136
let ptr: *i32 = transmute(ptr);
139-
let val = bswap32(*ptr);
137+
let val = from_be32(*ptr);
140138
let val: u32 = transmute(val);
141139
if (val & 0x80000000) != 0 {
142140
Res {
@@ -162,11 +160,6 @@ pub mod reader {
162160
}
163161
}
164162

165-
#[cfg(not(target_arch = "x86"), not(target_arch = "x86_64"))]
166-
pub fn vuint_at(data: &[u8], start: uint) -> Res {
167-
vuint_at_slow(data, start)
168-
}
169-
170163
pub fn Doc<'a>(data: &'a [u8]) -> Doc<'a> {
171164
Doc { data: data, start: 0u, end: data.len() }
172165
}

‎src/libnative/io/net.rs

+2-6
Original file line numberDiff line numberDiff line change
@@ -26,15 +26,11 @@ use super::file::keep_going;
2626
#[cfg(windows)] pub type sock_t = libc::SOCKET;
2727
#[cfg(unix)] pub type sock_t = super::file::fd_t;
2828

29-
#[cfg(target_endian = "big")] pub fn htons(x: u16) -> u16 { x }
30-
#[cfg(target_endian = "big")] pub fn ntohs(x: u16) -> u16 { x }
31-
#[cfg(target_endian = "little")]
3229
pub fn htons(u: u16) -> u16 {
33-
unsafe { intrinsics::bswap16(u as i16) as u16 }
30+
intrinsics::to_be16(u as i16) as u16
3431
}
35-
#[cfg(target_endian = "little")]
3632
pub fn ntohs(u: u16) -> u16 {
37-
unsafe { intrinsics::bswap16(u as i16) as u16 }
33+
intrinsics::from_be16(u as i16) as u16
3834
}
3935

4036
enum InAddr {

‎src/libstd/unstable/intrinsics.rs

+13
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)