 |
Changeset 3161
- Timestamp:
- 02/10/08 20:37:56
(10 months ago)
- Author:
- kris
- Message:
fixes #846 : ByteSwap? usability
thanks to Mandel
-
Files:
-
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
| r2356 |
r3161 |
|
| 23 | 23 | faster overall than a traditional 'shift' implementation. |
|---|
| 24 | 24 | |
|---|
| | 25 | ubyte[] x = [0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08]; |
|---|
| | 26 | |
|---|
| | 27 | swap16 (x.ptr, 8) -> 02 01 04 03 06 05 08 07 |
|---|
| | 28 | swap32 (x.ptr, 8) -> 04 03 02 01 08 07 06 05 |
|---|
| | 29 | swap64 (x.ptr, 8) -> 08 07 06 05 04 03 02 01 |
|---|
| | 30 | |
|---|
| 25 | 31 | *******************************************************************************/ |
|---|
| 26 | 32 | |
|---|
| … | … | |
| 29 | 35 | /*********************************************************************** |
|---|
| 30 | 36 | |
|---|
| | 37 | Reverses two-byte sequences. Parameter bytes specifies the |
|---|
| | 38 | number of bytes, which should be a multiple of 2 |
|---|
| | 39 | |
|---|
| | 40 | ***********************************************************************/ |
|---|
| | 41 | |
|---|
| | 42 | final static void swap16 (void[] dst) |
|---|
| | 43 | { |
|---|
| | 44 | swap16 (dst.ptr, dst.length); |
|---|
| | 45 | } |
|---|
| | 46 | |
|---|
| | 47 | /*********************************************************************** |
|---|
| | 48 | |
|---|
| | 49 | Reverses four-byte sequences. Parameter bytes specifies the |
|---|
| | 50 | number of bytes, which should be a multiple of 4 |
|---|
| | 51 | |
|---|
| | 52 | ***********************************************************************/ |
|---|
| | 53 | |
|---|
| | 54 | final static void swap32 (void[] dst) |
|---|
| | 55 | { |
|---|
| | 56 | swap32 (dst.ptr, dst.length); |
|---|
| | 57 | } |
|---|
| | 58 | |
|---|
| | 59 | /*********************************************************************** |
|---|
| | 60 | |
|---|
| | 61 | Reverse eight-byte sequences. Parameter bytes specifies the |
|---|
| | 62 | number of bytes, which should be a multiple of 8 |
|---|
| | 63 | |
|---|
| | 64 | ***********************************************************************/ |
|---|
| | 65 | |
|---|
| | 66 | final static void swap64 (void[] dst) |
|---|
| | 67 | { |
|---|
| | 68 | swap64 (dst.ptr, dst.length); |
|---|
| | 69 | } |
|---|
| | 70 | |
|---|
| | 71 | /*********************************************************************** |
|---|
| | 72 | |
|---|
| | 73 | Reverse ten-byte sequences. Parameter bytes specifies the |
|---|
| | 74 | number of bytes, which should be a multiple of 10 |
|---|
| | 75 | |
|---|
| | 76 | ***********************************************************************/ |
|---|
| | 77 | |
|---|
| | 78 | final static void swap80 (void[] dst) |
|---|
| | 79 | { |
|---|
| | 80 | swap80 (dst.ptr, dst.length); |
|---|
| | 81 | } |
|---|
| | 82 | |
|---|
| | 83 | /*********************************************************************** |
|---|
| | 84 | |
|---|
| | 85 | Reverses two-byte sequences. Parameter bytes specifies the |
|---|
| | 86 | number of bytes, which should be a multiple of 2 |
|---|
| | 87 | |
|---|
| 31 | 88 | ***********************************************************************/ |
|---|
| 32 | 89 | |
|---|
| 33 | 90 | final static void swap16 (void *dst, uint bytes) |
|---|
| 34 | 91 | { |
|---|
| 35 | | ubyte* p = cast(ubyte*) dst; |
|---|
| | 92 | assert (bytes & 0x01 is 0); |
|---|
| | 93 | |
|---|
| | 94 | auto p = cast(ubyte*) dst; |
|---|
| 36 | 95 | while (bytes) |
|---|
| 37 | 96 | { |
|---|
| … | … | |
| 47 | 106 | /*********************************************************************** |
|---|
| 48 | 107 | |
|---|
| | 108 | Reverses four-byte sequences. Parameter bytes specifies the |
|---|
| | 109 | number of bytes, which should be a multiple of 4 |
|---|
| | 110 | |
|---|
| 49 | 111 | ***********************************************************************/ |
|---|
| 50 | 112 | |
|---|
| 51 | 113 | final static void swap32 (void *dst, uint bytes) |
|---|
| 52 | 114 | { |
|---|
| 53 | | uint* p = cast(uint*) dst; |
|---|
| | 115 | assert (bytes & 0x03 is 0); |
|---|
| | 116 | |
|---|
| | 117 | auto p = cast(uint*) dst; |
|---|
| 54 | 118 | while (bytes) |
|---|
| 55 | 119 | { |
|---|
| 56 | 120 | *p = bswap(*p); |
|---|
| 57 | | p ++; |
|---|
| | 121 | ++p; |
|---|
| 58 | 122 | bytes -= int.sizeof; |
|---|
| 59 | 123 | } |
|---|
| … | … | |
| 62 | 126 | /*********************************************************************** |
|---|
| 63 | 127 | |
|---|
| | 128 | Reverse eight-byte sequences. Parameter bytes specifies the |
|---|
| | 129 | number of bytes, which should be a multiple of 8 |
|---|
| | 130 | |
|---|
| 64 | 131 | ***********************************************************************/ |
|---|
| 65 | 132 | |
|---|
| 66 | 133 | final static void swap64 (void *dst, uint bytes) |
|---|
| 67 | 134 | { |
|---|
| 68 | | uint* p = cast(uint*) dst; |
|---|
| | 135 | assert (bytes & 0x07 is 0); |
|---|
| | 136 | |
|---|
| | 137 | auto p = cast(uint*) dst; |
|---|
| 69 | 138 | while (bytes) |
|---|
| 70 | 139 | { |
|---|
| … | … | |
| 80 | 149 | /*********************************************************************** |
|---|
| 81 | 150 | |
|---|
| | 151 | Reverse ten-byte sequences. Parameter bytes specifies the |
|---|
| | 152 | number of bytes, which should be a multiple of 10 |
|---|
| | 153 | |
|---|
| 82 | 154 | ***********************************************************************/ |
|---|
| 83 | 155 | |
|---|
| 84 | 156 | final static void swap80 (void *dst, uint bytes) |
|---|
| 85 | 157 | { |
|---|
| 86 | | ubyte* p = cast(ubyte*) dst; |
|---|
| | 158 | assert ((bytes % 10) is 0); |
|---|
| | 159 | |
|---|
| | 160 | auto p = cast(ubyte*) dst; |
|---|
| 87 | 161 | while (bytes) |
|---|
| 88 | 162 | { |
|---|
Download in other formats:
|
 |
 |
|
 |
Copyright © 2006-2008 Tango. All Rights Reserved. | Page Width:
Static or
Dynamic