1 // Inferno libmach/swap.c
2 // http://code.google.com/p/inferno-os/source/browse/utils/libmach/swap.c
3 //
4 // Copyright © 1994-1999 Lucent Technologies Inc.
5 // Power PC support Copyright © 1995-2004 C H Forsyth (forsyth@terzarima.net).
6 // Portions Copyright © 1997-1999 Vita Nuova Limited.
7 // Portions Copyright © 2000-2007 Vita Nuova Holdings Limited (www.vitanuova.com).
8 // Revisions Copyright © 2000-2004 Lucent Technologies Inc. and others.
9 // Portions Copyright © 2009 The Go Authors. All rights reserved.
10 //
11 // Permission is hereby granted, free of charge, to any person obtaining a copy
12 // of this software and associated documentation files (the "Software"), to deal
13 // in the Software without restriction, including without limitation the rights
14 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
15 // copies of the Software, and to permit persons to whom the Software is
16 // furnished to do so, subject to the following conditions:
17 //
18 // The above copyright notice and this permission notice shall be included in
19 // all copies or substantial portions of the Software.
20 //
21 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
24 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
26 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
27 // THE SOFTWARE.
28
29 #include <u.h>
30
31 /*
32 * big-endian short
33 */
34 ushort
35 beswab(ushort s)
36 {
37 uchar *p;
38
39 p = (uchar*)&s;
40 return (p[0]<<8) | p[1];
41 }
42
43 /*
44 * big-endian int32
45 */
46 uint32
47 beswal(uint32 l)
48 {
49 uchar *p;
50
51 p = (uchar*)&l;
52 return (p[0]<<24) | (p[1]<<16) | (p[2]<<8) | p[3];
53 }
54
55 /*
56 * big-endian vlong
57 */
58 uvlong
59 beswav(uvlong v)
60 {
61 uchar *p;
62
63 p = (uchar*)&v;
64 return ((uvlong)p[0]<<56) | ((uvlong)p[1]<<48) | ((uvlong)p[2]<<40)
65 | ((uvlong)p[3]<<32) | ((uvlong)p[4]<<24)
66 | ((uvlong)p[5]<<16) | ((uvlong)p[6]<<8)
67 | (uvlong)p[7];
68 }
69
70 /*
71 * little-endian short
72 */
73 ushort
74 leswab(ushort s)
75 {
76 uchar *p;
77
78 p = (uchar*)&s;
79 return (p[1]<<8) | p[0];
80 }
81
82 /*
83 * little-endian int32
84 */
85 uint32
86 leswal(uint32 l)
87 {
88 uchar *p;
89
90 p = (uchar*)&l;
91 return (p[3]<<24) | (p[2]<<16) | (p[1]<<8) | p[0];
92 }
93
94 /*
95 * little-endian vlong
96 */
97 uvlong
98 leswav(uvlong v)
99 {
100 uchar *p;
101
102 p = (uchar*)&v;
103 return ((uvlong)p[7]<<56) | ((uvlong)p[6]<<48) | ((uvlong)p[5]<<40)
104 | ((uvlong)p[4]<<32) | ((uvlong)p[3]<<24)
105 | ((uvlong)p[2]<<16) | ((uvlong)p[1]<<8)
106 | (uvlong)p[0];
107 }