xref: /titanic_41/usr/src/lib/libast/common/string/swapmem.c (revision bbb1277b6ec1b0daad4e3ed1a2b891d3e2ece2eb)
1 /***********************************************************************
2 *                                                                      *
3 *               This software is part of the ast package               *
4 *          Copyright (c) 1985-2009 AT&T Intellectual Property          *
5 *                      and is licensed under the                       *
6 *                  Common Public License, Version 1.0                  *
7 *                    by AT&T Intellectual Property                     *
8 *                                                                      *
9 *                A copy of the License is available at                 *
10 *            http://www.opensource.org/licenses/cpl1.0.txt             *
11 *         (with md5 checksum 059e8cd6165cb4c31e351f2b69388fd9)         *
12 *                                                                      *
13 *              Information and Software Systems Research               *
14 *                            AT&T Research                             *
15 *                           Florham Park NJ                            *
16 *                                                                      *
17 *                 Glenn Fowler <gsf@research.att.com>                  *
18 *                  David Korn <dgk@research.att.com>                   *
19 *                   Phong Vo <kpv@research.att.com>                    *
20 *                                                                      *
21 ***********************************************************************/
22 #pragma prototyped
23 /*
24  * Glenn Fowler
25  * AT&T Research
26  *
27  * internal representation conversion support
28  */
29 
30 #include <ast.h>
31 #include <swap.h>
32 
33 /*
34  * swap n bytes according to op
35  * from==to is ok
36  */
37 
38 void*
39 swapmem(int op, const void* from, void* to, register size_t n)
40 {
41 	register char*	f = (char*)from;
42 	register char*	t = (char*)to;
43 	register int	c;
44 
45 	switch (op & (n - 1))
46 	{
47 	case 0:
48 		if (t != f)
49 			memcpy(t, f, n);
50 		break;
51 	case 1:
52 		for (n >>= 1; n--; f += 2, t += 2)
53 		{
54 			c = f[0]; t[0] = f[1]; t[1] = c;
55 		}
56 		break;
57 	case 2:
58 		for (n >>= 2; n--; f += 4, t += 4)
59 		{
60 			c = f[0]; t[0] = f[2]; t[2] = c;
61 			c = f[1]; t[1] = f[3]; t[3] = c;
62 		}
63 		break;
64 	case 3:
65 		for (n >>= 2; n--; f += 4, t += 4)
66 		{
67 			c = f[0]; t[0] = f[3]; t[3] = c;
68 			c = f[1]; t[1] = f[2]; t[2] = c;
69 		}
70 		break;
71 	case 4:
72 		for (n >>= 3; n--; f += 8, t += 8)
73 		{
74 			c = f[0]; t[0] = f[4]; t[4] = c;
75 			c = f[1]; t[1] = f[5]; t[5] = c;
76 			c = f[2]; t[2] = f[6]; t[6] = c;
77 			c = f[3]; t[3] = f[7]; t[7] = c;
78 		}
79 		break;
80 	case 5:
81 		for (n >>= 3; n--; f += 8, t += 8)
82 		{
83 			c = f[0]; t[0] = f[5]; t[5] = c;
84 			c = f[1]; t[1] = f[4]; t[4] = c;
85 			c = f[2]; t[2] = f[7]; t[7] = c;
86 			c = f[3]; t[3] = f[6]; t[6] = c;
87 		}
88 		break;
89 	case 6:
90 		for (n >>= 3; n--; f += 8, t += 8)
91 		{
92 			c = f[0]; t[0] = f[6]; t[6] = c;
93 			c = f[1]; t[1] = f[7]; t[7] = c;
94 			c = f[2]; t[2] = f[4]; t[4] = c;
95 			c = f[3]; t[3] = f[5]; t[5] = c;
96 		}
97 		break;
98 	case 7:
99 		for (n >>= 3; n--; f += 8, t += 8)
100 		{
101 			c = f[0]; t[0] = f[7]; t[7] = c;
102 			c = f[1]; t[1] = f[6]; t[6] = c;
103 			c = f[2]; t[2] = f[5]; t[5] = c;
104 			c = f[3]; t[3] = f[4]; t[4] = c;
105 		}
106 		break;
107 	}
108 	return to;
109 }
110