NCEPLIBS-bacio  2.5.0
byteswap.c
Go to the documentation of this file.
1 
17 // no byteswap.h on Apple
18 #ifdef APPLE
19 #include <libkern/OSByteOrder.h>
20 #define bswap_16(x) OSSwapInt16(x)
21 #define bswap_32(x) OSSwapInt32(x)
22 #define bswap_64(x) OSSwapInt64(x)
23 #else
24 #include <byteswap.h>
25 #endif
26 #include <stdint.h>
27 #include <stdlib.h>
28 #include <stdio.h>
29 
30 static int send_errors = 1;
38 void
40 {
41  send_errors=flag;
42 }
43 
52 static int
53 simple_swap_32(void *data,size_t len)
54 {
55  size_t i;
56  uint32_t *udata;
57  if ((size_t)data & 0x3)
58  {
59  if (send_errors)
60  fprintf(stderr,"ERROR: pointer to 32-bit integer is not 32-bit aligned (pointer is 0x%llx)\n",(long long)data);
61  return 0;
62  }
63  udata=data;
64  for(i=0;i<len;i++)
65  udata[i]=
66  ( (udata[i]>>24)&0xff ) |
67  ( (udata[i]>>8)&0xff00 ) |
68  ( (udata[i]<<8)&0xff0000 ) |
69  ( (udata[i]<<24)&0xff000000 );
70  return 1;
71 }
72 
81 static int
82 simple_swap_16(void *data,size_t len)
83 {
84  size_t i;
85  uint16_t *udata;
86  if ((size_t)data & 0x1)
87  {
88  if (send_errors)
89  fprintf(stderr,"ERROR: pointer to 16-bit integer is not 16-bit aligned (pointer is 0x%llx)\n",(long long)data);
90  return 0;
91  }
92  udata=data;
93  for(i=0;i<len;i++)
94  udata[i]=
95  ( (udata[i]>>8)&0xff ) |
96  ( (udata[i]<<8)&0xff00 );
97  return 1;
98 }
99 
109 static int
110 macro_swap_64(void *data,size_t len)
111 {
112  size_t i;
113  uint64_t *udata;
114  if ((size_t)data & 0x5)
115  {
116  if (send_errors)
117  fprintf(stderr,"ERROR: pointer to 64-bit integer is not 64-bit aligned (pointer is 0x%llx)\n",(long long)data);
118  return 0;
119  }
120  udata=data;
121  for(i=0;i<len;i++)
122  udata[i]=bswap_64(udata[i]);
123  return 1;
124 }
125 
135 int
136 fast_byteswap(void *data,int bytes,size_t count)
137 {
138  switch(bytes) {
139  case 1: return 1;
140  case 2: return simple_swap_16(data,count);
141  case 4: return simple_swap_32(data,count);
142  case 8: return macro_swap_64(data,count);
143  default: return 0;
144  }
145 }
146 
147 /* Include the C library file for definition/control */
148 #include "clib.h"
149 #include "stdio.h"
150 #include "fast-byteswap.h"
151 
160 void
161 byteswap_(char *data, int *nbyte, int *nnum)
162 {
163  int i, j;
164  char swap[256];
165  int nb = *nbyte;
166  int nn = *nnum;
167  size_t count = *nnum;
168 
169  if (!fast_byteswap(data, nb, count))
170  {
171  fprintf(stderr,"ERROR NOT ALIGNED SLOW CODE USED (nb and count %9d %9lu )\n",nb, count);
172  /* It failed. No data was byteswapped because it is not aligned */
173  for (j = 0; j < nn; j++)
174  {
175  for (i = 0; i < nb; i++)
176  swap[i] = data[j * nb + i];
177  for (i = 0; i < nb; i++)
178  data[j * nb + i] = swap[nb - i - 1];
179  }
180  }
181 }
static int macro_swap_64(void *data, size_t len)
Use the GNU macros, which are specialized byteswap ASM instructions.
Definition: byteswap.c:110
static int simple_swap_16(void *data, size_t len)
Simple single-value loops.
Definition: byteswap.c:82
void fast_byteswap_errors(int flag)
Set a flag to turn warnings off for non-aligned pointers.
Definition: byteswap.c:39
static int simple_swap_32(void *data, size_t len)
Simple single-value loops.
Definition: byteswap.c:53
int fast_byteswap(void *data, int bytes, size_t count)
Fast byteswap.
Definition: byteswap.c:136
static int send_errors
If non-zero, warn about non-aligned pointers.
Definition: byteswap.c:30
void byteswap_(char *data, int *nbyte, int *nnum)
Byteswap.
Definition: byteswap.c:161
Include file to define variables for Fortran to C interface(s) revision history.
Header file for byteswap functions.