PageRenderTime 24ms CodeModel.GetById 1ms app.highlight 18ms RepoModel.GetById 2ms app.codeStats 0ms

/core/endian.d

http://github.com/wilkie/djehuty
D | 171 lines | 133 code | 24 blank | 14 comment | 0 complexity | d7017aea3bdca642b93fe507afde4470 MD5 | raw file
  1/*
  2 * endian.d
  3 *
  4 * This file contains some simple code to switch endian.
  5 *
  6 * Author: Dave Wilkinson
  7 *
  8 */
  9
 10module core.endian;
 11
 12
 13// IntType FromBigEndian(IntType)
 14// --------------------------------
 15//   converts big endian input to native byte order
 16
 17ulong FromBigEndian64(ulong input)
 18{
 19	version(LittleEndian)
 20	{
 21		return (input >> 56) | ((input >> 40) & 0xFF00) | ((input >> 24) & 0xFF0000) | ((input >> 8) & 0xFF000000) | ((input << 8) & 0xFF00000000UL) | ((input << 24) & 0xFF0000000000UL) | ((input << 40) & 0xFF000000000000UL) | ((input << 56) & 0xFF00000000000000UL);
 22	}
 23	else
 24	{
 25		return input;
 26	}
 27}
 28
 29uint FromBigEndian32(uint input)
 30{
 31	version(LittleEndian)
 32	{
 33		return (input >> 24) | ((input >> 8) & 0xFF00) | ((input << 8) & 0xFF0000) | ((input << 24) & 0xFF000000);
 34	}
 35	else
 36	{
 37		return input;
 38	}
 39}
 40
 41ushort FromBigEndian16(ushort input)
 42{
 43	version(LittleEndian)
 44	{
 45		return cast(ushort)((cast(uint)input >> 8) | (cast(uint)input << 8));
 46	}
 47	else
 48	{
 49		return input;
 50	}
 51}
 52
 53
 54
 55
 56
 57
 58
 59// IntType FromLittleEndian(IntType)
 60// -----------------------------------
 61//   converts little endian input to native byte order
 62
 63ulong FromLittleEndian64(ulong input)
 64{
 65	version(LittleEndian)
 66	{
 67		return input;
 68	}
 69	else
 70	{
 71		return (input >> 56) | ((input >> 40) & 0xFF00) | ((input >> 24) & 0xFF0000) | ((input >> 8) & 0xFF000000) | ((input << 8) & 0xFF00000000) | ((input << 24) & 0xFF0000000000) | ((input << 40) & 0xFF000000000000) | ((input << 56) & 0xFF00000000000000);
 72	}
 73}
 74
 75uint FromLittleEndian32(uint input)
 76{
 77	version(LittleEndian)
 78	{
 79		return input;
 80	}
 81	else
 82	{
 83		return (input >> 24) | ((input >> 8) & 0xFF00) | ((input << 8) & 0xFF0000) | ((input << 24) & 0xFF000000);
 84	}
 85}
 86
 87ushort FromLittleEndian16(ushort input)
 88{
 89	version(LittleEndian)
 90	{
 91		return input;
 92	}
 93	else
 94	{
 95		return (input >> 8) | (input << 8);
 96	}
 97}
 98
 99
100
101ulong NativeToLE64(ulong input)
102{
103	version (LittleEndian)
104	{
105		return input;
106	}
107	else
108	{
109		return FromLittleEndian64(input);
110	}
111}
112
113uint NativeToLE32(uint input)
114{
115	version (LittleEndian)
116	{
117		return input;
118	}
119	else
120	{
121		return FromLittleEndian32(input);
122	}
123}
124
125ushort NativeToLE16(ushort input)
126{
127	version (LittleEndian)
128	{
129		return input;
130	}
131	else
132	{
133		return FromLittleEndian16(input);
134	}
135}
136
137ulong NativeToBE64(ulong input)
138{
139	version (LittleEndian)
140	{
141		return FromBigEndian64(input);
142	}
143	else
144	{
145		return input;
146	}
147}
148
149uint NativeToBE32(uint input)
150{
151	version (LittleEndian)
152	{
153		return FromBigEndian32(input);
154	}
155	else
156	{
157		return input;
158	}
159}
160
161ushort NativeToBE16(ushort input)
162{
163	version (LittleEndian)
164	{
165		return FromBigEndian16(input);
166	}
167	else
168	{
169		return input;
170	}
171}