xref: /titanic_44/usr/src/lib/libc/sparc/gen/endian.c (revision 577bdc16588431dcec0742144b1ba2ef4b0581fe)
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
htole16(uint16_t in)21 htole16(uint16_t in)
22 {
23 	return (((in & 0xff) << 8) | ((in & 0xff00) >> 8));
24 }
25 
26 uint32_t
htole32(uint32_t in)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
htole64(uint64_t in)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
letoh16(uint16_t in)49 letoh16(uint16_t in)
50 {
51 	return (((in & 0xff) << 8) | ((in & 0xff00) >> 8));
52 }
53 
54 uint16_t
le16toh(uint16_t in)55 le16toh(uint16_t in)
56 {
57 	return (((in & 0xff) << 8) | ((in & 0xff00) >> 8));
58 }
59 
60 uint32_t
letoh32(uint32_t in)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
le32toh(uint32_t in)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
letoh64(uint64_t in)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
le64toh(uint64_t in)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
htobe16(uint16_t in)107 htobe16(uint16_t in)
108 {
109 	return (in);
110 }
111 
112 uint32_t
htobe32(uint32_t in)113 htobe32(uint32_t in)
114 {
115 	return (in);
116 }
117 
118 uint64_t
htobe64(uint64_t in)119 htobe64(uint64_t in)
120 {
121 	return (in);
122 }
123 
124 uint16_t
betoh16(uint16_t in)125 betoh16(uint16_t in)
126 {
127 	return (in);
128 }
129 
130 uint16_t
be16toh(uint16_t in)131 be16toh(uint16_t in)
132 {
133 	return (in);
134 }
135 
136 uint32_t
betoh32(uint32_t in)137 betoh32(uint32_t in)
138 {
139 	return (in);
140 }
141 
142 uint32_t
be32toh(uint32_t in)143 be32toh(uint32_t in)
144 {
145 	return (in);
146 }
147 
148 uint64_t
betoh64(uint64_t in)149 betoh64(uint64_t in)
150 {
151 	return (in);
152 }
153 
154 uint64_t
be64toh(uint64_t in)155 be64toh(uint64_t in)
156 {
157 	return (in);
158 }
159