SIONlib  1.7.7
Scalable I/O library for parallel access to task-local files
sion_convert.c
Go to the documentation of this file.
1 /****************************************************************************
2 ** SIONLIB http://www.fz-juelich.de/jsc/sionlib **
3 *****************************************************************************
4 ** Copyright (c) 2008-2019 **
5 ** Forschungszentrum Juelich, Juelich Supercomputing Centre **
6 ** **
7 ** See the file COPYRIGHT in the package base directory for details **
8 ****************************************************************************/
9 
19 #define _XOPEN_SOURCE 700
20 
21 #include <stdlib.h>
22 #include <stdio.h>
23 #include <string.h>
24 #include <stdarg.h>
25 #include <assert.h>
26 
27 #include "sion.h"
28 #include "sion_debug.h"
29 
30 
44 void sion_swap(void *target, void *source, int size, int n, int do_swap)
45 {
46  int i, j;
47  char *iptr, *optr;
48 
49  optr = target;
50  iptr = source;
51 
52  if (iptr == optr) {
53  if (!do_swap)
54  return;
55  /*
56  * in-place swapping
57  * unroll loop for the common cases 2,4,8 Byte
58  *
59  */
60  DPRINTF((4, "sion_swap: swapping %d x %d Bytes in place =%ld\n", n, size, (sion_int32) *iptr));
61 
62  char tmp;
63  switch (size) {
64  case 1:
65  /* nothing to do */
66  break;
67  case 2:
68  for (i = 0; i < n; i++) {
69  tmp = iptr[0];
70  iptr[0] = iptr[1];
71  iptr[1] = tmp;
72  iptr += 2;
73  }
74  break;
75  case 4:
76  for (i = 0; i < n; i++) {
77  tmp = iptr[0];
78  iptr[0] = iptr[3];
79  iptr[3] = tmp;
80 
81  tmp = iptr[1];
82  iptr[1] = iptr[2];
83  iptr[2] = tmp;
84 
85  iptr += 4;
86  }
87  break;
88  case 8:
89  for (i = 0; i < n; i++) {
90  tmp = iptr[0];
91  iptr[0] = iptr[7];
92  iptr[7] = tmp;
93 
94  tmp = iptr[1];
95  iptr[1] = iptr[6];
96  iptr[6] = tmp;
97 
98  tmp = iptr[2];
99  iptr[2] = iptr[5];
100  iptr[5] = tmp;
101 
102  tmp = iptr[3];
103  iptr[3] = iptr[4];
104  iptr[4] = tmp;
105 
106  iptr += 8;
107  }
108  break;
109  default:
110  for (i = 0; i < n; i++) {
111  for (j = 0; j < size / 2; j++) {
112  tmp = iptr[j];
113  iptr[j] = iptr[size - j - 1];
114  iptr[size - j - 1] = tmp;
115  }
116  iptr += size;
117  }
118  }
119  DPRINTF((4, "sion_swap: swapped %d x %d Bytes in place =%ld\n", n, size, *optr));
120 
121  }
122  else {
123  if (!do_swap) {
124  memcpy(optr, iptr, size * n);
125  return;
126  }
127  /*
128  * swap into a different non-overlapping buffer
129  * unroll common cases again
130  */
131  DPRINTF((4, "sion_swap: swapping %d x %d Bytes\n", n, size));
132 
133  switch (size) {
134  case 1:
135  memcpy(optr, iptr, size * n);
136  break;
137  case 2:
138  for (i = 0; i < n; i++) {
139  optr[0] = iptr[1];
140  optr[1] = iptr[0];
141 
142  iptr += 2;
143  optr += 2;
144  }
145  break;
146  case 4:
147  for (i = 0; i < n; i++) {
148  optr[0] = iptr[3];
149  optr[1] = iptr[2];
150  optr[2] = iptr[1];
151  optr[3] = iptr[0];
152 
153  iptr += 4;
154  optr += 4;
155  }
156  break;
157  case 8:
158  for (i = 0; i < n; i++) {
159  optr[0] = iptr[7];
160  optr[1] = iptr[6];
161  optr[2] = iptr[5];
162  optr[3] = iptr[4];
163  optr[4] = iptr[3];
164  optr[5] = iptr[2];
165  optr[6] = iptr[1];
166  optr[7] = iptr[0];
167 
168  iptr += 8;
169  optr += 8;
170  }
171  break;
172  default:
173  for (i = 0; i < n; i++) {
174  for (j = 0; j < size; j++) {
175  optr[j] = iptr[size - j - 1];
176  }
177  iptr += size;
178  optr += size;
179  }
180  }
181  }
182 }
void sion_swap(void *target, void *source, int size, int n, int do_swap)
Definition: sion_convert.c:44