132
|
1 /*******************************************************************************
|
|
2
|
|
3 copyright: Copyright (c) 2004 Kris Bell. All rights reserved
|
|
4
|
|
5 license: BSD style: $(LICENSE)
|
|
6
|
|
7 version: Initial release: October 2004
|
|
8
|
|
9 version: Feb 20th 2005 - Asm version removed by Aleksey Bobnev
|
|
10
|
|
11 author: Kris, Aleksey Bobnev
|
|
12
|
|
13 *******************************************************************************/
|
|
14
|
|
15 module tango.core.ByteSwap;
|
|
16
|
|
17 import tango.core.BitManip;
|
|
18
|
|
19 /*******************************************************************************
|
|
20
|
|
21 Reverse byte order for specific datum sizes. Note that the
|
|
22 byte-swap approach avoids alignment issues, so is probably
|
|
23 faster overall than a traditional 'shift' implementation.
|
|
24
|
|
25 *******************************************************************************/
|
|
26
|
|
27 struct ByteSwap
|
|
28 {
|
|
29 /***********************************************************************
|
|
30
|
|
31 ***********************************************************************/
|
|
32
|
|
33 final static void swap16 (void *dst, uint bytes)
|
|
34 {
|
|
35 ubyte* p = cast(ubyte*) dst;
|
|
36 while (bytes)
|
|
37 {
|
|
38 ubyte b = p[0];
|
|
39 p[0] = p[1];
|
|
40 p[1] = b;
|
|
41
|
|
42 p += short.sizeof;
|
|
43 bytes -= short.sizeof;
|
|
44 }
|
|
45 }
|
|
46
|
|
47 /***********************************************************************
|
|
48
|
|
49 ***********************************************************************/
|
|
50
|
|
51 final static void swap32 (void *dst, uint bytes)
|
|
52 {
|
|
53 uint* p = cast(uint*) dst;
|
|
54 while (bytes)
|
|
55 {
|
|
56 *p = bswap(*p);
|
|
57 p ++;
|
|
58 bytes -= int.sizeof;
|
|
59 }
|
|
60 }
|
|
61
|
|
62 /***********************************************************************
|
|
63
|
|
64 ***********************************************************************/
|
|
65
|
|
66 final static void swap64 (void *dst, uint bytes)
|
|
67 {
|
|
68 uint* p = cast(uint*) dst;
|
|
69 while (bytes)
|
|
70 {
|
|
71 uint i = p[0];
|
|
72 p[0] = bswap(p[1]);
|
|
73 p[1] = bswap(i);
|
|
74
|
|
75 p += (long.sizeof / int.sizeof);
|
|
76 bytes -= long.sizeof;
|
|
77 }
|
|
78 }
|
|
79
|
|
80 /***********************************************************************
|
|
81
|
|
82 ***********************************************************************/
|
|
83
|
|
84 final static void swap80 (void *dst, uint bytes)
|
|
85 {
|
|
86 ubyte* p = cast(ubyte*) dst;
|
|
87 while (bytes)
|
|
88 {
|
|
89 ubyte b = p[0];
|
|
90 p[0] = p[9];
|
|
91 p[9] = b;
|
|
92
|
|
93 b = p[1];
|
|
94 p[1] = p[8];
|
|
95 p[8] = b;
|
|
96
|
|
97 b = p[2];
|
|
98 p[2] = p[7];
|
|
99 p[7] = b;
|
|
100
|
|
101 b = p[3];
|
|
102 p[3] = p[6];
|
|
103 p[6] = b;
|
|
104
|
|
105 b = p[4];
|
|
106 p[4] = p[5];
|
|
107 p[5] = b;
|
|
108
|
|
109 p += real.sizeof;
|
|
110 bytes -= real.sizeof;
|
|
111 }
|
|
112 }
|
|
113 }
|
|
114
|
|
115
|
|
116
|
|
117
|