Fixed floating point byte swapping.
This commit is contained in:
parent
208b3aa85c
commit
5ccfe7534b
|
@ -22,6 +22,21 @@ enum UTFEncoding : ubyte
|
||||||
UTF_32
|
UTF_32
|
||||||
}
|
}
|
||||||
|
|
||||||
|
unittest
|
||||||
|
{
|
||||||
|
const ints = [314, -101];
|
||||||
|
int[2] intsSwapBuffer = ints;
|
||||||
|
swapByteOrder(intsSwapBuffer[]);
|
||||||
|
swapByteOrder(intsSwapBuffer[]);
|
||||||
|
assert(ints == intsSwapBuffer, "Lost information when swapping byte order");
|
||||||
|
|
||||||
|
const floats = [3.14f, 10.1f];
|
||||||
|
float[2] floatsSwapBuffer = floats;
|
||||||
|
swapByteOrder(floatsSwapBuffer[]);
|
||||||
|
swapByteOrder(floatsSwapBuffer[]);
|
||||||
|
assert(floats == floatsSwapBuffer, "Lost information when swapping byte order");
|
||||||
|
}
|
||||||
|
|
||||||
@system pure nothrow @nogc:
|
@system pure nothrow @nogc:
|
||||||
|
|
||||||
/// Swap byte order of items in an array in place.
|
/// Swap byte order of items in an array in place.
|
||||||
|
@ -43,7 +58,8 @@ void swapByteOrder(T)(T[] array)
|
||||||
}
|
}
|
||||||
else static if(T.sizeof == 4)
|
else static if(T.sizeof == 4)
|
||||||
{
|
{
|
||||||
item = bswap(cast(uint)item);
|
const swapped = bswap(*cast(uint*)&item);
|
||||||
|
item = *cast(const(T)*)&swapped;
|
||||||
}
|
}
|
||||||
else static assert(false, "Unsupported T: " ~ T.stringof);
|
else static assert(false, "Unsupported T: " ~ T.stringof);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue