Re: Byte Order Swapping Function

2011-07-22 Thread Andrei Alexandrescu
On 7/22/11 10:47 AM, Jonathan M Davis wrote: On Wednesday 13 July 2011 23:37:02 Andrew Wiley wrote: Hey, does anyone else thing a function like this belongs in Phobos, and if so, where do you think it should go? T ntoh(T)(T val) if (__traits(isArithmetic, T)) { version(BigEndian) { return val

Re: Byte Order Swapping Function

2011-07-22 Thread Jonathan M Davis
On Wednesday 13 July 2011 23:37:02 Andrew Wiley wrote: > Hey, does anyone else thing a function like this belongs in Phobos, and if > so, where do you think it should go? > > T ntoh(T)(T val) if (__traits(isArithmetic, T)) { > version(BigEndian) { > return val; > } > else version (LittleEndian) {

Re: Byte Order Swapping Function

2011-07-18 Thread Steven Schveighoffer
On Sat, 16 Jul 2011 17:51:50 -0400, Jonathan M Davis wrote: I think that the problem is a combination of NaN and real. If I change the first three tests (which all test using init) to use is, then all of the tests pass until real is tested with T.max, and it's doesn't survive being swap

Re: Byte Order Swapping Function

2011-07-17 Thread Andrei Alexandrescu
On 7/17/11 4:27 AM, Timon Gehr wrote: Imposing meaning to bytes is the only point for data types to exist whatsoever. How do you distinguish between an integer and a network byte order integer at function boundaries? Good point. Then probably the functions swapping bytes away from native shoul

Re: Byte Order Swapping Function

2011-07-17 Thread Christian Manning
Christian Manning wrote: > How about this?: > > private template Eqsize(T) { > static if (T.sizeof == 4) { > alias uint S; > } > else static if (T.sizeof == 8) { > alias ulong S; > } > } > > private union IntFloat { > uint _int; > float _float; > } > > pr

Re: Byte Order Swapping Function

2011-07-17 Thread Christian Manning
How about this?: private template Eqsize(T) { static if (T.sizeof == 4) { alias uint S; } else static if (T.sizeof == 8) { alias ulong S; } } private union IntFloat { uint _int; float _float; } private union IntDouble { ulong _long; double _double;

Re: Byte Order Swapping Function

2011-07-17 Thread Piotr Szturmaj
Jonathan M Davis wrote: private T swapEndianImpl(T)(T val) if(is(Unqual!T == ulong)) { return ((val& 0xff00UL)>> 56) | ((val& 0x00ffUL)>> 40) | ((val& 0xff00UL)>> 24) | ((val& 0x00ffUL)>> 8) |

Re: Byte Order Swapping Function

2011-07-17 Thread Timon Gehr
Jonathan M Davis wrote: > On Sunday 17 July 2011 09:40:06 Alix Pexton wrote: >> On 17/07/2011 07:42, Jonathan M Davis wrote: >> > On Saturday 16 July 2011 23:31:09 Andrew Wiley wrote: >> [snip] >> >> >> Take a look at http://www.dmh2000.com/cpp/dswap.shtml . It made the >> >> odd >> >> behavior mak

Re: Byte Order Swapping Function

2011-07-17 Thread Jonathan M Davis
On Sunday 17 July 2011 01:48:37 Jonathan M Davis wrote: > On Sunday 17 July 2011 09:40:06 Alix Pexton wrote: > > On 17/07/2011 07:42, Jonathan M Davis wrote: > > > On Saturday 16 July 2011 23:31:09 Andrew Wiley wrote: > > [snip] > > > > >> Take a look at http://www.dmh2000.com/cpp/dswap.shtml . It

Re: Byte Order Swapping Function

2011-07-17 Thread Jonathan M Davis
On Sunday 17 July 2011 09:40:06 Alix Pexton wrote: > On 17/07/2011 07:42, Jonathan M Davis wrote: > > On Saturday 16 July 2011 23:31:09 Andrew Wiley wrote: > [snip] > > >> Take a look at http://www.dmh2000.com/cpp/dswap.shtml . It made the > >> odd > >> behavior make a lot more sense to me. > > >

Re: Byte Order Swapping Function

2011-07-17 Thread Alix Pexton
On 17/07/2011 07:42, Jonathan M Davis wrote: On Saturday 16 July 2011 23:31:09 Andrew Wiley wrote: [snip] Take a look at http://www.dmh2000.com/cpp/dswap.shtml . It made the odd behavior make a lot more sense to me. Okay. Good to know. In other words, we can't have swapEndian work with floati

Re: Byte Order Swapping Function

2011-07-17 Thread Jonathan M Davis
On Sunday 17 July 2011 16:09:13 KennyTM~ wrote: > On Jul 17, 11 15:44, Jonathan M Davis wrote: > > On Sunday 17 July 2011 15:31:36 KennyTM~ wrote: > >> On Jul 17, 11 05:51, Jonathan M Davis wrote: > >>> On Saturday 16 July 2011 15:38:29 Andrei Alexandrescu wrote: > Just paste the code here. >

Re: Byte Order Swapping Function

2011-07-17 Thread KennyTM~
On Jul 17, 11 15:44, Jonathan M Davis wrote: On Sunday 17 July 2011 15:31:36 KennyTM~ wrote: On Jul 17, 11 05:51, Jonathan M Davis wrote: On Saturday 16 July 2011 15:38:29 Andrei Alexandrescu wrote: Just paste the code here. This is what I have at the moment: import core.bitop; [snip] p

Re: Byte Order Swapping Function

2011-07-17 Thread Jonathan M Davis
On Sunday 17 July 2011 15:31:36 KennyTM~ wrote: > On Jul 17, 11 05:51, Jonathan M Davis wrote: > > On Saturday 16 July 2011 15:38:29 Andrei Alexandrescu wrote: > >> Just paste the code here. > > > > This is what I have at the moment: > > > > import core.bitop; > > [snip] > > > private T swapEnd

Re: Byte Order Swapping Function

2011-07-17 Thread KennyTM~
On Jul 17, 11 05:51, Jonathan M Davis wrote: On Saturday 16 July 2011 15:38:29 Andrei Alexandrescu wrote: Just paste the code here. This is what I have at the moment: import core.bitop; [snip] private T swapEndianImpl(T)(T val) if(is(Unqual!T == ulong)) { return ((val& 0xff

Re: Byte Order Swapping Function

2011-07-16 Thread Jonathan M Davis
On Saturday 16 July 2011 23:31:09 Andrew Wiley wrote: > On Sat, Jul 16, 2011 at 2:51 PM, Jonathan M Davis wrote: > > On Saturday 16 July 2011 15:38:29 Andrei Alexandrescu wrote: > > > Just paste the code here. > > > > This is what I have at the moment: > > > > import core.bitop; > > > > /++ > >

Re: Byte Order Swapping Function

2011-07-16 Thread Andrew Wiley
On Sat, Jul 16, 2011 at 2:51 PM, Jonathan M Davis wrote: > On Saturday 16 July 2011 15:38:29 Andrei Alexandrescu wrote: > > Just paste the code here. > > This is what I have at the moment: > > import core.bitop; > > /++ >Swaps the endianness of the given value. Any integral value, >charact

Re: Byte Order Swapping Function

2011-07-16 Thread Jonathan M Davis
On Saturday 16 July 2011 15:38:29 Andrei Alexandrescu wrote: > Just paste the code here. This is what I have at the moment: import core.bitop; /++ Swaps the endianness of the given value. Any integral value, character, or floating point value is accepted. +/ T swapEndian(T)(T val)

Re: Byte Order Swapping Function

2011-07-16 Thread Andrei Alexandrescu
On 7/16/11 3:39 AM, Jonathan M Davis wrote: On Thursday 14 July 2011 06:27:47 Andrei Alexandrescu wrote: On 7/14/11 5:51 AM, Regan Heath wrote: That's my point. I need 8/16/32/64/128 bit versions and it really would be better if there were general variants. My version are less than optimal, but

Re: Byte Order Swapping Function

2011-07-16 Thread Christian Manning
Andrew Wiley wrote: > On Sat, Jul 16, 2011 at 6:21 AM, Christian Manning > wrote: > >> Jonathan M Davis wrote: >> >> > On Thursday 14 July 2011 06:27:47 Andrei Alexandrescu wrote: >> >> On 7/14/11 5:51 AM, Regan Heath wrote: >> >> > That's my point. I need 8/16/32/64/128 bit versions and it reall

Re: Byte Order Swapping Function

2011-07-16 Thread Christian Manning
Andrew Wiley wrote: > On Sat, Jul 16, 2011 at 6:21 AM, Christian Manning > wrote: > >> Jonathan M Davis wrote: >> >> > On Thursday 14 July 2011 06:27:47 Andrei Alexandrescu wrote: >> >> On 7/14/11 5:51 AM, Regan Heath wrote: >> >> > That's my point. I need 8/16/32/64/128 bit versions and it reall

Re: Byte Order Swapping Function

2011-07-16 Thread Andrew Wiley
On Sat, Jul 16, 2011 at 6:21 AM, Christian Manning wrote: > Jonathan M Davis wrote: > > > On Thursday 14 July 2011 06:27:47 Andrei Alexandrescu wrote: > >> On 7/14/11 5:51 AM, Regan Heath wrote: > >> > That's my point. I need 8/16/32/64/128 bit versions and it really > would > >> > be better if th

Re: Byte Order Swapping Function

2011-07-16 Thread Christian Manning
Jonathan M Davis wrote: > On Thursday 14 July 2011 06:27:47 Andrei Alexandrescu wrote: >> On 7/14/11 5:51 AM, Regan Heath wrote: >> > That's my point. I need 8/16/32/64/128 bit versions and it really would >> > be better if there were general variants. My version are less than >> > optimal, but do

Re: Byte Order Swapping Function

2011-07-16 Thread Jonathan M Davis
On Thursday 14 July 2011 06:27:47 Andrei Alexandrescu wrote: > On 7/14/11 5:51 AM, Regan Heath wrote: > > That's my point. I need 8/16/32/64/128 bit versions and it really would > > be better if there were general variants. My version are less than > > optimal, but do use intrinsics where possible.

Re: Byte Order Swapping Function

2011-07-15 Thread Christian Manning
Christian Manning wrote: > Jonathan M Davis wrote: > >> On 2011-07-15 14:23, Christian Manning wrote: >>> Andrei Alexandrescu wrote: >>> > On 7/14/11 5:51 AM, Regan Heath wrote: >>> >> That's my point. I need 8/16/32/64/128 bit versions and it really >>> >> would be better if there were general v

Re: Byte Order Swapping Function

2011-07-15 Thread Christian Manning
Jonathan M Davis wrote: > On 2011-07-15 14:23, Christian Manning wrote: >> Andrei Alexandrescu wrote: >> > On 7/14/11 5:51 AM, Regan Heath wrote: >> >> That's my point. I need 8/16/32/64/128 bit versions and it really >> >> would be better if there were general variants. My version are less >> >>

Re: Byte Order Swapping Function

2011-07-15 Thread Andrei Alexandrescu
On 7/15/11 4:23 PM, Christian Manning wrote: Andrei Alexandrescu wrote: On 7/14/11 5:51 AM, Regan Heath wrote: That's my point. I need 8/16/32/64/128 bit versions and it really would be better if there were general variants. My version are less than optimal, but do use intrinsics where possibl

Re: Byte Order Swapping Function

2011-07-15 Thread Jonathan M Davis
On 2011-07-15 14:23, Christian Manning wrote: > Andrei Alexandrescu wrote: > > On 7/14/11 5:51 AM, Regan Heath wrote: > >> That's my point. I need 8/16/32/64/128 bit versions and it really would > >> be better if there were general variants. My version are less than > >> optimal, but do use intrins

Re: Byte Order Swapping Function

2011-07-15 Thread Christian Manning
Andrei Alexandrescu wrote: > On 7/14/11 5:51 AM, Regan Heath wrote: >> That's my point. I need 8/16/32/64/128 bit versions and it really would >> be better if there were general variants. My version are less than >> optimal, but do use intrinsics where possible. Someone else can do a far >> better

Re: Byte Order Swapping Function

2011-07-14 Thread Andrew Wiley
On Thu, Jul 14, 2011 at 10:45 AM, Jonathan M Davis wrote: > On 2011-07-14 10:26, Andrew Wiley wrote: > > On Thu, Jul 14, 2011 at 9:02 AM, Jonathan M Davis > wrote: > > > On Thursday 14 July 2011 06:27:47 Andrei Alexandrescu wrote: > > > > On 7/14/11 5:51 AM, Regan Heath wrote: > > > > > That's my

Re: Byte Order Swapping Function

2011-07-14 Thread Jonathan M Davis
On 2011-07-14 10:26, Andrew Wiley wrote: > On Thu, Jul 14, 2011 at 9:02 AM, Jonathan M Davis wrote: > > On Thursday 14 July 2011 06:27:47 Andrei Alexandrescu wrote: > > > On 7/14/11 5:51 AM, Regan Heath wrote: > > > > That's my point. I need 8/16/32/64/128 bit versions and it really > > > > would

Re: Byte Order Swapping Function

2011-07-14 Thread Andrew Wiley
On Thu, Jul 14, 2011 at 9:02 AM, Jonathan M Davis wrote: > On Thursday 14 July 2011 06:27:47 Andrei Alexandrescu wrote: > > On 7/14/11 5:51 AM, Regan Heath wrote: > > > That's my point. I need 8/16/32/64/128 bit versions and it really would > > > be better if there were general variants. My versio

Re: Byte Order Swapping Function

2011-07-14 Thread Jonathan M Davis
On Thursday 14 July 2011 09:02:12 Jonathan M Davis wrote: > On Thursday 14 July 2011 06:27:47 Andrei Alexandrescu wrote: > > On 7/14/11 5:51 AM, Regan Heath wrote: > > > That's my point. I need 8/16/32/64/128 bit versions and it really > > > would > > > be better if there were general variants. My

Re: Byte Order Swapping Function

2011-07-14 Thread Jonathan M Davis
On Thursday 14 July 2011 06:27:47 Andrei Alexandrescu wrote: > On 7/14/11 5:51 AM, Regan Heath wrote: > > That's my point. I need 8/16/32/64/128 bit versions and it really would > > be better if there were general variants. My version are less than > > optimal, but do use intrinsics where possible.

Re: Byte Order Swapping Function

2011-07-14 Thread Regan Heath
On Thu, 14 Jul 2011 12:27:47 +0100, Andrei Alexandrescu wrote: On 7/14/11 5:51 AM, Regan Heath wrote: That's my point. I need 8/16/32/64/128 bit versions and it really would be better if there were general variants. My version are less than optimal, but do use intrinsics where possible. Some

Re: Byte Order Swapping Function

2011-07-14 Thread Regan Heath
On Thu, 14 Jul 2011 14:20:24 +0100, Piotr Szturmaj wrote: I implemented all of those except for Whirlpool. I still have the originals, which I started to tidy for inclusion into phobos .. at least that was the plan, I don't have a lot of spare time for this soft of thing unfortunately. I w

Re: Byte Order Swapping Function

2011-07-14 Thread Piotr Szturmaj
Regan Heath wrote: On Thu, 14 Jul 2011 11:44:23 +0100, Piotr Szturmaj wrote: Regan Heath wrote: On Thu, 14 Jul 2011 08:14:10 +0100, Jonathan M Davis wrote: I did have to create a 64-bit version for std.datetime, so it has a private function called endianSwap64 to do the job. So, it's not li

Re: Byte Order Swapping Function

2011-07-14 Thread Piotr Szturmaj
Steven Schveighoffer wrote: On Thu, 14 Jul 2011 06:12:00 -0400, Piotr Szturmaj wrote: Btw. How compiler intrinsics work? I see there's only bswap declaration (without body) in core.bitop. Where can I find compiler code which actually substitutes bswap() into real instructions? A complier in

Re: Byte Order Swapping Function

2011-07-14 Thread Andrei Alexandrescu
On 7/14/11 5:51 AM, Regan Heath wrote: That's my point. I need 8/16/32/64/128 bit versions and it really would be better if there were general variants. My version are less than optimal, but do use intrinsics where possible. Someone else can do a far better job than I, and it really should be don

Re: Byte Order Swapping Function

2011-07-14 Thread Steven Schveighoffer
On Thu, 14 Jul 2011 06:12:00 -0400, Piotr Szturmaj wrote: Btw. How compiler intrinsics work? I see there's only bswap declaration (without body) in core.bitop. Where can I find compiler code which actually substitutes bswap() into real instructions? A complier intrinsic is a special fun

Re: Byte Order Swapping Function

2011-07-14 Thread Steven Schveighoffer
On Thu, 14 Jul 2011 06:16:37 -0400, David Nadlinger wrote: On 7/14/11 12:05 PM, Jonathan M Davis wrote: On Thursday 14 July 2011 10:55:49 Regan Heath wrote: On Thu, 14 Jul 2011 08:14:10 +0100, Jonathan M Davis wrote: I did have to create a 64-bit version for std.datetime, so it has a pr

Re: Byte Order Swapping Function

2011-07-14 Thread Regan Heath
On Thu, 14 Jul 2011 11:44:23 +0100, Piotr Szturmaj wrote: Regan Heath wrote: On Thu, 14 Jul 2011 08:14:10 +0100, Jonathan M Davis wrote: I did have to create a 64-bit version for std.datetime, so it has a private function called endianSwap64 to do the job. So, it's not like I'm saying that

Re: Byte Order Swapping Function

2011-07-14 Thread Jonathan M Davis
On Thursday 14 July 2011 11:51:14 Regan Heath wrote: > On Thu, 14 Jul 2011 11:05:07 +0100, Jonathan M Davis > > wrote: > > On Thursday 14 July 2011 10:55:49 Regan Heath wrote: > >> On Thu, 14 Jul 2011 08:14:10 +0100, Jonathan M Davis > >> > >> > >> wrote: > >> > On Thursday 14 July 2011 00:03:2

Re: Byte Order Swapping Function

2011-07-14 Thread Regan Heath
On Thu, 14 Jul 2011 11:05:07 +0100, Jonathan M Davis wrote: On Thursday 14 July 2011 10:55:49 Regan Heath wrote: On Thu, 14 Jul 2011 08:14:10 +0100, Jonathan M Davis wrote: > On Thursday 14 July 2011 00:03:23 Andrew Wiley wrote: >> On Wed, Jul 13, 2011 at 11:59 PM, Jonathan M Davis > > w

Re: Byte Order Swapping Function

2011-07-14 Thread Piotr Szturmaj
Regan Heath wrote: On Thu, 14 Jul 2011 08:14:10 +0100, Jonathan M Davis wrote: I did have to create a 64-bit version for std.datetime, so it has a private function called endianSwap64 to do the job. So, it's not like I'm saying that the situation couldn't be improved, but druntime and Phobos do

Re: Byte Order Swapping Function

2011-07-14 Thread David Nadlinger
On 7/14/11 12:05 PM, Jonathan M Davis wrote: On Thursday 14 July 2011 10:55:49 Regan Heath wrote: On Thu, 14 Jul 2011 08:14:10 +0100, Jonathan M Davis wrote: I did have to create a 64-bit version for std.datetime, so it has a private function called endianSwap64 to do the job. So, it's not lik

Re: Byte Order Swapping Function

2011-07-14 Thread David Nadlinger
On 7/14/11 12:12 PM, Piotr Szturmaj wrote: Yesterday, I had to write my own bswap for ulong because intrinsic version supports only uints. Is there any implementation obstacle preventing ulong and ushort bswap in core.bitop? Btw. How compiler intrinsics work? I see there's only bswap declaration

Re: Byte Order Swapping Function

2011-07-14 Thread Piotr Szturmaj
Jonathan M Davis wrote: I did have to create a 64-bit version for std.datetime, so it has a private function called endianSwap64 to do the job. So, it's not like I'm saying that the situation couldn't be improved, but druntime and Phobos do currently give you the exact same thing that C and C++ d

Re: Byte Order Swapping Function

2011-07-14 Thread Jonathan M Davis
On Thursday 14 July 2011 10:55:49 Regan Heath wrote: > On Thu, 14 Jul 2011 08:14:10 +0100, Jonathan M Davis > > wrote: > > On Thursday 14 July 2011 00:03:23 Andrew Wiley wrote: > >> On Wed, Jul 13, 2011 at 11:59 PM, Jonathan M Davis > > > > wrote: > >> > On Wednesday 13 July 2011 23:37:02 Andrew

Re: Byte Order Swapping Function

2011-07-14 Thread Regan Heath
On Thu, 14 Jul 2011 08:14:10 +0100, Jonathan M Davis wrote: On Thursday 14 July 2011 00:03:23 Andrew Wiley wrote: On Wed, Jul 13, 2011 at 11:59 PM, Jonathan M Davis wrote: > On Wednesday 13 July 2011 23:37:02 Andrew Wiley wrote: > > Hey, does anyone else thing a function like this belongs

Re: Byte Order Swapping Function

2011-07-14 Thread Johannes Pfau
Andrew Wiley wrote: >Hey, does anyone else thing a function like this belongs in Phobos, >and if so, where do you think it should go? > >T ntoh(T)(T val) if (__traits(isArithmetic, T)) { >version(BigEndian) { > return val; >} >else version (LittleEndian) { > ubyte[] arr = (cast(ubyte*)&val)[0 .. T.

Re: Byte Order Swapping Function

2011-07-14 Thread Jonathan M Davis
On Thursday 14 July 2011 00:03:23 Andrew Wiley wrote: > On Wed, Jul 13, 2011 at 11:59 PM, Jonathan M Davis wrote: > > On Wednesday 13 July 2011 23:37:02 Andrew Wiley wrote: > > > Hey, does anyone else thing a function like this belongs in Phobos, > > > and > > > > if > > > > > so, where do you t

Re: Byte Order Swapping Function

2011-07-14 Thread Andrew Wiley
On Thu, Jul 14, 2011 at 12:03 AM, Andrew Wiley wrote: > On Wed, Jul 13, 2011 at 11:59 PM, Jonathan M Davis wrote: > >> On Wednesday 13 July 2011 23:37:02 Andrew Wiley wrote: >> > Hey, does anyone else thing a function like this belongs in Phobos, and >> if >> > so, where do you think it should go?

Re: Byte Order Swapping Function

2011-07-14 Thread Andrew Wiley
On Wed, Jul 13, 2011 at 11:59 PM, Jonathan M Davis wrote: > On Wednesday 13 July 2011 23:37:02 Andrew Wiley wrote: > > Hey, does anyone else thing a function like this belongs in Phobos, and > if > > so, where do you think it should go? > > > > T ntoh(T)(T val) if (__traits(isArithmetic, T)) { > >

Re: Byte Order Swapping Function

2011-07-13 Thread Jonathan M Davis
On Wednesday 13 July 2011 23:37:02 Andrew Wiley wrote: > Hey, does anyone else thing a function like this belongs in Phobos, and if > so, where do you think it should go? > > T ntoh(T)(T val) if (__traits(isArithmetic, T)) { > version(BigEndian) { > return val; > } > else version (LittleEndian) {

Byte Order Swapping Function

2011-07-13 Thread Andrew Wiley
Hey, does anyone else thing a function like this belongs in Phobos, and if so, where do you think it should go? T ntoh(T)(T val) if (__traits(isArithmetic, T)) { version(BigEndian) { return val; } else version (LittleEndian) { ubyte[] arr = (cast(ubyte*)&val)[0 .. T.sizeof]; ubyte temp; for(int