endianswap.h 4.64 KB
Newer Older
Berk Onat's avatar
Berk Onat committed
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
/***************************************************************************
 *cr
 *cr            (C) Copyright 1995-2016 The Board of Trustees of the
 *cr                        University of Illinois
 *cr                         All Rights Reserved
 *cr
 ***************************************************************************/
/***************************************************************************
 * RCS INFORMATION:
 *
 *      $RCSfile: endianswap.h,v $
 *      $Author: johns $       $Locker:  $             $State: Exp $
 *      $Revision: 1.7 $       $Date: 2016/11/28 05:01:53 $
 *
 ***************************************************************************
 * DESCRIPTION:
 *   Byte swapping routines used in various plugins
 *   There are two versions of each routine, one that's safe to use in
 *   all cases (but is slow) and one that is only safe to use on memory 
 *   addresses that are aligned to the word size that's being byte-swapped
 *   but are much much much faster.  Use the aligned versions of these
 *   routines whenever possible.  The 'ndata' length count parameters and
 *   internal loops should be safe to use on huge memory arrays on 64-bit
 *   machines.
 *
 ***************************************************************************/

#ifndef ENDIAN_SWAP_H
#define ENDIAN_SWAP_H

/* works on unaligned 2-byte quantities */
static void swap2_unaligned(void *v, long ndata) {
  long i;
  char * dataptr = (char *) v;
  char tmp;

  for (i = 0; i < ndata-1; i += 2) {
    tmp = dataptr[i];
    dataptr[i] = dataptr[i+1];
    dataptr[i+1] = tmp;
  }
}


/* works on unaligned 4-byte quantities */
static void swap4_unaligned(void *v, long ndata) {
  long i;
  char *dataptr;
  char tmp;

  dataptr = (char *) v; 
  for (i=0; i<ndata; i++) {
    tmp = dataptr[0];
    dataptr[0] = dataptr[3];
    dataptr[3] = tmp;
    tmp = dataptr[1];
    dataptr[1] = dataptr[2];
    dataptr[2] = tmp;
    dataptr += 4;
  }
}


/* works on unaligned 8-byte quantities */
static void swap8_unaligned(void *v, long ndata) {
  char *data = (char *) v;
  long i;
  char byteArray[8];
  char *bytePointer;

  for (i=0; i<ndata; i++) {
    bytePointer = data + (i<<3);
    byteArray[0]  =  *bytePointer;
    byteArray[1]  =  *(bytePointer+1);
    byteArray[2]  =  *(bytePointer+2);
    byteArray[3]  =  *(bytePointer+3);
    byteArray[4]  =  *(bytePointer+4);
    byteArray[5]  =  *(bytePointer+5);
    byteArray[6]  =  *(bytePointer+6);
    byteArray[7]  =  *(bytePointer+7);

    *bytePointer     = byteArray[7];
    *(bytePointer+1) = byteArray[6];
    *(bytePointer+2) = byteArray[5];
    *(bytePointer+3) = byteArray[4];
    *(bytePointer+4) = byteArray[3];
    *(bytePointer+5) = byteArray[2];
    *(bytePointer+6) = byteArray[1];
    *(bytePointer+7) = byteArray[0];
  }
}


/* Only works with aligned 2-byte quantities, will cause a bus error */
/* on some platforms if used on unaligned data.                      */
static void swap2_aligned(void *v, long ndata) {
  short *data = (short *) v;
  long i;
  short *N; 

  for (i=0; i<ndata; i++) {
    N = data + i;
    *N=(((*N>>8)&0xff) | ((*N&0xff)<<8));  
  }
}


/* Only works with aligned 4-byte quantities, will cause a bus error */
/* on some platforms if used on unaligned data.                      */
static void swap4_aligned(void *v, long ndata) {
  int *data = (int *) v;
  long i;
  int *N;
  for (i=0; i<ndata; i++) {
    N = data + i;
    *N=(((*N>>24)&0xff) | ((*N&0xff)<<24) | 
        ((*N>>8)&0xff00) | ((*N&0xff00)<<8));
  }
}


/* Only works with aligned 8-byte quantities, will cause a bus error */
/* on some platforms if used on unaligned data.                      */
static void swap8_aligned(void *v, long ndata) {
  /* Use int* internally to prevent bugs caused by some compilers */
  /* and hardware that would potentially load data into an FP reg */
  /* and hose everything, such as the old "jmemcpy()" bug in NAMD */
  int *data = (int *) v;  
  long i;
  int *N; 
  int t0, t1;

  for (i=0; i<ndata; i++) {
    N = data + (i<<1);
    t0 = N[0];
    t0=(((t0>>24)&0xff) | ((t0&0xff)<<24) | 
        ((t0>>8)&0xff00) | ((t0&0xff00)<<8));

    t1 = N[1];
    t1=(((t1>>24)&0xff) | ((t1&0xff)<<24) | 
        ((t1>>8)&0xff00) | ((t1&0xff00)<<8));

    N[0] = t1; 
    N[1] = t0; 
  }
}

#if 0
/* Other implementations that might be faster in some cases */

/* swaps the endianism of an eight byte word. */
void mdio_swap8(double *i) {
        char c;
        char *n;
        n = (char *) i;
        c = n[0];
        n[0] = n[7];
        n[7] = c;
        c = n[1];
        n[1] = n[6];
        n[6] = c;
        c = n[2];
        n[2] = n[5];
        n[5] = c;
        c = n[3];
        n[3] = n[4];
        n[4] = c;
}

#endif

#endif