xref: /reactos/sdk/lib/ucrt/stdlib/byteswap.cpp (revision 04e0dc4a)
1 //
2 // byteswap.cpp
3 //
4 //      Copyright (c) Microsoft Corporation. All rights reserved.
5 //
6 // Defines functions that swap the bytes of an unsigned integer.
7 //
8 #include <stdlib.h>
9 
10 
11 
12 #pragma function(_byteswap_ulong, _byteswap_uint64, _byteswap_ushort)
13 
14 /***
15 *unsigned long _byteswap_ulong(i) - long byteswap
16 *
17 *Purpose:
18 *       Performs a byte swap on an unsigned integer.
19 *
20 *Entry:
21 *       unsigned long i:        value to swap
22 *
23 *Exit:
24 *       returns swaped
25 *
26 *Exceptions:
27 *       None.
28 *
29 *******************************************************************************/
30 
_byteswap_ulong(unsigned long const i)31 extern "C" unsigned long __cdecl _byteswap_ulong(unsigned long const i)
32 {
33     unsigned int j;
34     j =  (i << 24);
35     j += (i <<  8) & 0x00FF0000;
36     j += (i >>  8) & 0x0000FF00;
37     j += (i >> 24);
38     return j;
39 }
40 
_byteswap_ushort(unsigned short const i)41 extern "C" unsigned short __cdecl _byteswap_ushort(unsigned short const i)
42 {
43     unsigned short j;
44     j =  (i << 8);
45     j += (i >> 8);
46     return j;
47 }
48 
_byteswap_uint64(unsigned __int64 const i)49 extern "C" unsigned __int64 __cdecl _byteswap_uint64(unsigned __int64 const i)
50 {
51     unsigned __int64 j;
52     j =  (i << 56);
53     j += (i << 40) & 0x00FF000000000000;
54     j += (i << 24) & 0x0000FF0000000000;
55     j += (i <<  8) & 0x000000FF00000000;
56     j += (i >>  8) & 0x00000000FF000000;
57     j += (i >> 24) & 0x0000000000FF0000;
58     j += (i >> 40) & 0x000000000000FF00;
59     j += (i >> 56);
60     return j;
61 
62 }
63