Skip to content

Adds conditional byteswapping intrinsics #7194

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 1 commit into from

Conversation

auroranockert
Copy link
Contributor

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.

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.
@brson
Copy link
Contributor

brson commented Jun 17, 2013

Is there an issue associated with this? I'm not clear on the motivation here. Also, I would prefer they be put somewhere else since they aren't true compiler intrinsics.

@thestinger
Copy link
Contributor

@brson: converting to a common endian is needed for sending binary data over a network or serializing to a portable forward, so it's probably common enough

freebsd/linux include these in endian.h and POSIX has ones for network byte order (big endian) in arpa/inet.h

@emberian
Copy link
Member

We already have code that does this too. It's in io, it's unconditional and can't take advantage of the llvm intrinsic. +1 for this.

@auroranockert
Copy link
Contributor Author

@cmr (or someone else) Do you happen to know what would be the best API for this if we want to be able to optimize the implementation in IO?

trait ByteSwappable {
fn to_little_endian() -> T;
fn to_big_endian() -> T;
fn byte_swap() -> T
}

where T is a [u8, ..n] or something like that?

@huonw
Copy link
Member

huonw commented Jun 23, 2013

@jensnockert -> Self is probably the only thing that works at the moment (there's no way to parameterise the n in [u8, .. n] until associated constants work).

@emberian
Copy link
Member

@jensnockert well io could use the functions in this PR as it is (and get rid of read_{be,le}_int_n and just use .read in the read_le_i16 etc).

I'm not sure what a good interface for this functionality would be. Might check how other languages handle byte swapping?

bors added a commit that referenced this pull request Jul 6, 2013
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.
@bors bors closed this Jul 6, 2013
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants