1 /*-------------- Telecommunications & Signal Processing Lab ---------------
2 McGill University
3
4 Routine:
5 void VRswapBytes (const void *BuffI, void *BuffO, int Size, int Nelem)
6
7 Purpose:
8 Swap bytes in an array of values
9
10 Description:
11 This routine does a swap of the bytes making up each word in an array of
12 values. Swapping bytes involves reversing the order of the constituent bytes
13 in each word.
14
15 Parameters:
16 -> const void *BuffI
17 Array of values, each element having wordsize Size bytes
18 <- void *BuffO
19 Output array of byte swapped values. The output buffer can occupy the
20 same memory as the input buffer.
21 -> int Size
22 Size of each element in bytes. This value must be 1 (no-op), 2, 4 or 8.
23 -> int Nelem
24 Number of values to be byte-swapped
25
26 Author / revision:
27 P. Kabal Copyright (C) 2003
28 $Revision: 1.21 $ $Date: 2003/05/09 03:33:52 $
29
30 -------------------------------------------------------------------------*/
31
32 #include <stdlib.h>
33 #include <string.h>
34
35 #include <libtsp.h>
36 #include <libtsp/nucleus.h>
37 #include <libtsp/UTtypes.h>
38 #include <libtsp/VRmsg.h>
39
40
41 void
VRswapBytes(const void * BuffI,void * BuffO,int Size,int Nelem)42 VRswapBytes (const void *BuffI, void *BuffO, int Size, int Nelem)
43
44 {
45 const unsigned char *cp;
46 unsigned char *sp;
47 const int2_t *B2;
48 int2_t *S2;
49 const int4_t *B4;
50 int4_t *S4;
51 const double8_t *B8;
52 double8_t *S8;
53 unsigned char t;
54
55 switch (Size) {
56 case (1):
57 memcpy (BuffO, BuffI, (size_t) ((long int) Size * Nelem));
58 break;
59
60 case (2):
61 B2 = (const int2_t *) BuffI;
62 S2 = (int2_t *) BuffO;
63 while (Nelem-- > 0) {
64 cp = (const unsigned char *) B2;
65 sp = (unsigned char *) S2;
66 t = cp[1]; sp[1] = cp[0]; sp[0] = t;
67 ++B2;
68 ++S2;
69 }
70 break;
71
72 case (4):
73 B4 = (const int4_t *) BuffI;
74 S4 = (int4_t *) BuffO;
75 while (Nelem-- > 0) {
76 cp = (const unsigned char *) B4;
77 sp = (unsigned char *) S4;
78 t = cp[3]; sp[3] = cp[0]; sp[0] = t;
79 t = cp[2]; sp[2] = cp[1]; sp[1] = t;
80 ++B4;
81 ++S4;
82 }
83 break;
84
85 case (8):
86 B8 = (const double8_t *) BuffI;
87 S8 = (double8_t *) BuffO;
88 while (Nelem-- > 0) {
89 cp = (const unsigned char *) B8;
90 sp = (unsigned char *) S8;
91 t = cp[7]; sp[7] = cp[0]; sp[0] = t;
92 t = cp[6]; sp[6] = cp[1]; sp[1] = t;
93 t = cp[5]; sp[5] = cp[2]; sp[2] = t;
94 t = cp[4]; sp[4] = cp[3]; sp[3] = t;
95 ++B8;
96 ++S8;
97 }
98 break;
99
100 default:
101 UThalt ("VRswapBytes: %s", VRM_BadSize);
102 break;
103 }
104 return;
105 }
106