1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217
|
/*
* Copyright (c) 2001-2003 The Trustees of Indiana University.
* All rights reserved.
* Copyright (c) 1998-2001 University of Notre Dame.
* All rights reserved.
* Copyright (c) 1994-1998 The Ohio State University.
* All rights reserved.
*
* This file is part of the LAM/MPI software package. For license
* information, see the LICENSE file in the top level directory of the
* LAM/MPI source distribution.
*
* $HEADER$
*
* $Id: rev.c,v 6.5 2003/07/05 21:50:25 jsquyres Exp $
*
* Function: - reverse byte ordering of data types
* - handles 4 and 8-byte types in fast manner
* - handles all other types as byte arrays
*/
#include <portable.h>
#include <t_types.h>
/*
* rev4
*
* Function: - reverse byte ordering of a 4-byte array
* - array is treated as an unsigned 4-byte integer
* Accepts: - ptr source array
* - ptr destination array
*/
void
rev4(void *src, void *dest)
{
uint4 old;
uint4 *d;
old = *((uint4 *) src);
d = (uint4 *) dest;
*d = (old & 0x000000FF) << 16;
*d = (*d | (old & 0x0000FF00)) << 8;
old >>= 8;
*d |= (old & 0x0000FF00);
old >>= 16;
*d |= (old & 0x000000FF);
}
/*
* rev8
*
* Function: - reverse byte ordering of a 8-byte array
* - array is treated as 2 unsigned 4-byte integers
* Accepts: - ptr source array
* - ptr destination array
*/
void
rev8(void *src, void *dest)
{
uint4 old;
char *d;
old = *((uint4 *) src + 1);
d = (char *) dest + 4;
rev4(src, (void *) d);
rev4((void *) &old, dest);
}
/*
* revn
*
* Function: - reverse byte ordering of a multi-byte array
* Accepts: - ptr source array
* - ptr destination array
* - # of bytes
*/
void
revn(void *src, void *dest, int nbytes)
{
char temp;
char *s, *d;
s = (char *) src;
d = (char *) dest + nbytes - 1;
if (src == dest) {
/*
* Same array, swap bytes from both ends.
*/
for (nbytes /= 2; nbytes > 0; --nbytes, ++s, --d) {
temp = *s; *s = *d; *d = temp;
}
}
else {
/*
* Different arrays, just copy the reversed order.
* This assumes the arrays do not overlap.
*/
for ( ; nbytes > 0; --nbytes, ++s, --d) {
*d = *s;
}
}
}
/*
* mrev4
*
* Function: - reverse byte ordering of an array of 4-byte elements
* Accepts: - ptr array
* - # elements in array
*/
void
mrev4(void *array, int num)
{
uint4 old;
uint4 *d;
d = (uint4 *) array;
for ( ; num > 0; --num, ++d) {
old = *d;
*d = (old & 0x000000FF) << 16;
*d = (*d | (old & 0x0000FF00)) << 8;
old >>= 8;
*d |= (old & 0x0000FF00);
old >>= 16;
*d |= (old & 0x000000FF);
}
}
/*
* mrev8
*
* Function: - reverse byte ordering of an array of 8-byte elements
* Accepts: - ptr array
* - # elements in array
*/
void
mrev8(void *array, int num)
{
uint4 old;
uint4 tmp;
uint4 *d;
for (d = (uint4 *) array; num > 0; --num, ++d) {
tmp = *d;
old = *(d+1);
*d = (old & 0x000000FF) << 16;
*d = (*d | (old & 0x0000FF00)) << 8;
old >>= 8;
*d |= (old & 0x0000FF00);
old >>= 16;
*d |= (old & 0x000000FF);
++d;
*d = (tmp & 0x000000FF) << 16;
*d = (*d | (tmp & 0x0000FF00)) << 8;
tmp >>= 8;
*d |= (tmp & 0x0000FF00);
tmp >>= 16;
*d |= (tmp & 0x000000FF);
}
}
/*
* mrevn
*
* Function: - reverse byte ordering of an array of n-byte elements
* Accepts: - ptr array
* - element size
* - # elements in array
*/
void
mrevn(void *array, int size, int num)
{
char *p, *s, *d;
char temp;
int nbytes;
p = (char *) array;
for ( ; num > 0; --num, p += size) {
/*
* Swap bytes from both ends.
*/
s = p;
d = p + size - 1;
for (nbytes = size / 2; nbytes > 0; --nbytes, ++s, --d) {
temp = *s; *s = *d; *d = temp;
}
}
}
/*
* rw
*
* Function: - reverse word byte order (old Trollius call)
* Accepts: - original word
* Returns: - word with reversed byte order
*/
uint4
rw(uint4 oword)
{
uint4 rword;
rword = (oword & 0x000000FF) << 16;
rword = (rword | (oword & 0x0000FF00)) << 8;
oword >>= 8;
rword |= (oword & 0x0000FF00);
oword >>= 16;
rword |= (oword & 0x000000FF);
return(rword);
}
|