xref: /titanic_44/usr/src/lib/libc/sparc/gen/endian.c (revision 87308b749adb04ab3f564c765b089962af3edb4d)
1 /*
2  * This file and its contents are supplied under the terms of the
3  * Common Development and Distribution License ("CDDL"), version 1.0.
4  * You may only use this file in accordance with the terms of version
5  * 1.0 of the CDDL.
6  *
7  * A full copy of the text of the CDDL should have accompanied this
8  * source.  A copy of the CDDL is also available via the Internet at
9  * http://www.illumos.org/license/CDDL.
10  */
11 
12 /*
13  * Copyright (c) 2015, Joyent, Inc.
14  */
15 
16 /*
17  * endian(3C) routines
18  */
19 
20 uint16_t
21 htole16(uint16_t in)
22 {
23 	return (((in & 0xff) << 8) | ((in & 0xff00) >> 8));
24 }
25 
26 uint32_t
27 htole32(uint32_t in)
28 {
29 	return (((in & 0xffUL) << 24) |
30 	    (in & 0xff00UL) << 8 |
31 	    (in & 0xff0000UL) >> 8 |
32 	    ((in & 0xff000000UL) >> 24));
33 }
34 
35 uint64_t
36 htole64(uint64_t in)
37 {
38 	return (((in & 0xffULL) << 56) |
39 	    ((in & 0xff00ULL) << 40) |
40 	    ((in & 0xff0000ULL) << 24) |
41 	    ((in & 0xff000000ULL) << 8) |
42 	    ((in & 0xff00000000ULL) >> 8) |
43 	    ((in & 0xff0000000000ULL) >> 24) |
44 	    ((in & 0xff000000000000ULL) >> 40) |
45 	    ((in & 0xff00000000000000ULL) >> 56));
46 }
47 
48 uint16_t
49 letoh16(uint16_t in)
50 {
51 	return (((in & 0xff) << 8) | ((in & 0xff00) >> 8));
52 }
53 
54 uint16_t
55 le16toh(uint16_t in)
56 {
57 	return (((in & 0xff) << 8) | ((in & 0xff00) >> 8));
58 }
59 
60 uint32_t
61 letoh32(uint32_t in)
62 {
63 	return (((in & 0xffUL) << 24) |
64 	    (in & 0xff00UL) << 8 |
65 	    (in & 0xff0000UL) >> 8 |
66 	    ((in & 0xff000000UL) >> 24));
67 }
68 
69 uint32_t
70 le32toh(uint32_t in)
71 {
72 	return (((in & 0xffUL) << 24) |
73 	    (in & 0xff00UL) << 8 |
74 	    (in & 0xff0000UL) >> 8 |
75 	    ((in & 0xff000000UL) >> 24));
76 }
77 
78 uint64_t
79 letoh64(uint64_t in)
80 {
81 	return (((in & 0xffULL) << 56) |
82 	    ((in & 0xff00ULL) << 40) |
83 	    ((in & 0xff0000ULL) << 24) |
84 	    ((in & 0xff000000ULL) << 8) |
85 	    ((in & 0xff00000000ULL) >> 8) |
86 	    ((in & 0xff0000000000ULL) >> 24) |
87 	    ((in & 0xff000000000000ULL) >> 40) |
88 	    ((in & 0xff00000000000000ULL) >> 56));
89 }
90 
91 uint64_t
92 le64toh(uint64_t in)
93 {
94 	return (((in & 0xffULL) << 56) |
95 	    ((in & 0xff00ULL) << 40) |
96 	    ((in & 0xff0000ULL) << 24) |
97 	    ((in & 0xff000000ULL) << 8) |
98 	    ((in & 0xff00000000ULL) >> 8) |
99 	    ((in & 0xff0000000000ULL) >> 24) |
100 	    ((in & 0xff000000000000ULL) >> 40) |
101 	    ((in & 0xff00000000000000ULL) >> 56));
102 }
103 
104 /* Anything to or from big-endian is a no-op */
105 
106 uint16_t
107 htobe16(uint16_t in)
108 {
109 	return (in);
110 }
111 
112 uint32_t
113 htobe32(uint32_t in)
114 {
115 	return (in);
116 }
117 
118 uint64_t
119 htobe64(uint64_t in)
120 {
121 	return (in);
122 }
123 
124 uint16_t
125 betoh16(uint16_t in)
126 {
127 	return (in);
128 }
129 
130 uint16_t
131 be16toh(uint16_t in)
132 {
133 	return (in);
134 }
135 
136 uint32_t
137 betoh32(uint32_t in)
138 {
139 	return (in);
140 }
141 
142 uint32_t
143 be32toh(uint32_t in)
144 {
145 	return (in);
146 }
147 
148 uint64_t
149 betoh64(uint64_t in)
150 {
151 	return (in);
152 }
153 
154 uint64_t
155 be64toh(uint64_t in)
156 {
157 	return (in);
158 }
159