summaryrefslogtreecommitdiffstats
path: root/funtools/util/swap.c
blob: 320b24121b5080a106844f552a266d3999b22739 (plain)
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
218
219
220
221
222
223
224
225
226
227
228
/*
 *	Copyright (c) 1999-2003 Smithsonian Astrophysical Observatory
 */

/*
 *
 * swap.c -- swap data between native and IEEE format
 *
 *
 */

#include <stdio.h>
#include <prsetup.h>
#include <swap.h>

/*
 *
 * swap.c -- routines to swap parts of data
 *
 */

#define _idx(x, n)    ((unsigned char *) x)[n]

/* from Harbison&Steele by way of GNU cinfigure ...
   returns 1 for bigendian, 0 for littleendian */
#ifdef ANSI_FUNC
int
is_bigendian (void)
#else
int is_bigendian()
#endif
{
  union
  {
    long l;
    char c[sizeof (long)];
  } u;
  u.l = 1;
  return(u.c[sizeof (long) - 1] == 1);
}

#ifdef ANSI_FUNC
void
swap_short (short *buf, int n)
#else
void swap_short(buf, n)
     short *buf;
     int n;
#endif
{
  int i;
  register short *ptr;
  short val;

  ptr = buf;
  for(i=0; i<n; i++){
    _idx(&val, 0) = _idx(ptr, 1);
    _idx(&val, 1) = _idx(ptr, 0);
    *ptr++ = val;
  }
}

#ifdef ANSI_FUNC
void
swap_ushort (unsigned short *buf, int n)
#else
void swap_ushort(buf, n)
     unsigned short *buf;
     int n;
#endif
{
  int i;
  register unsigned short *ptr;
  unsigned short val;

  ptr = buf;
  for(i=0; i<n; i++){
    _idx(&val, 0) = _idx(ptr, 1);
    _idx(&val, 1) = _idx(ptr, 0);
    *ptr++ = val;
  }
}

#ifdef ANSI_FUNC
void
swap_int (int *buf, int n)
#else
void swap_int(buf, n)
     int *buf;
     int n;
#endif
{
  int i;
  register int *ptr;
  int val;

  ptr = buf;
  for(i=0; i<n; i++){
    _idx(&val, 0) = _idx(ptr, 3);
    _idx(&val, 1) = _idx(ptr, 2);
    _idx(&val, 2) = _idx(ptr, 1);
    _idx(&val, 3) = _idx(ptr, 0);
    *ptr++ = val;
  }
}

#ifdef ANSI_FUNC
void
swap_uint (unsigned int *buf, int n)
#else
void swap_uint(buf, n)
     unsigned int *buf;
     int n;
#endif
{
  int i;
  register unsigned int *ptr;
  unsigned int val;

  ptr = buf;
  for(i=0; i<n; i++){
    _idx(&val, 0) = _idx(ptr, 3);
    _idx(&val, 1) = _idx(ptr, 2);
    _idx(&val, 2) = _idx(ptr, 1);
    _idx(&val, 3) = _idx(ptr, 0);
    *ptr++ = val;
  }
}

#ifdef ANSI_FUNC
void
swap_float (float *buf, int n)
#else
void swap_float(buf, n)
     float *buf;
     int n;
#endif
{
  int i;
  register float *ptr;
  float val;

  ptr = buf;
  for(i=0; i<n; i++){
    _idx(&val, 0) = _idx(ptr, 3);
    _idx(&val, 1) = _idx(ptr, 2);
    _idx(&val, 2) = _idx(ptr, 1);
    _idx(&val, 3) = _idx(ptr, 0);
    *ptr++ = val;
  }
}

#ifdef ANSI_FUNC
void
swap_double (double *buf, int n)
#else
void swap_double(buf, n)
     double *buf;
     int n;
#endif
{
  int i;
  register double *ptr;
  double val;

  ptr = buf;
  for(i=0; i<n; i++){
    _idx(&val, 0) = _idx(ptr, 7);
    _idx(&val, 1) = _idx(ptr, 6);
    _idx(&val, 2) = _idx(ptr, 5);
    _idx(&val, 3) = _idx(ptr, 4);
    _idx(&val, 4) = _idx(ptr, 3);
    _idx(&val, 5) = _idx(ptr, 2);
    _idx(&val, 6) = _idx(ptr, 1);
    _idx(&val, 7) = _idx(ptr, 0);
    *ptr++ = val;
  }
}

/*
 *

 *
 */
#ifdef ANSI_FUNC
void
swap_data(void *buf, int len, int dtype)
#else
void swap_data(buf, len, dtype)
     void *buf;
     int len;
     int dtype;
#endif
{
  /* set up input data pointer */
  switch(dtype){
  case TY_CHAR:
  case 'B':
    break;
  case TY_SHORT:
  case 'I':
    swap_short((short *)buf, len);
    break;
  case TY_USHORT:
  case 'U':
    swap_ushort((unsigned short *)buf, len);
    break;
  case TY_INT:
  case 'J':
    swap_int((int *)buf, len);
    break;
  case 'V':
    swap_uint((unsigned int *)buf, len);
    break;
  case TY_FLOAT:
  case 'E':
    swap_float((float *)buf, len);
    break;
  case TY_DOUBLE:
  case 'F':
    swap_double((double *)buf, len);
    break;
  default:
    fprintf(stderr, "ERROR: unknown input data type %d\n", dtype);
    break;
  }
}