dmake: do not set MAKEFLAGS=k
[unleashed/tickless.git] / usr / src / stand / lib / xdr / byteorder.c
blobc035ad66a9295c9222705d148fcf0dc3c2c0a921
1 /*
2 * CDDL HEADER START
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
19 * CDDL HEADER END
22 * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
23 * Use is subject to license terms.
26 #include <sys/types.h>
27 #include <netinet/in.h>
30 * htonll(), ntohll(), htonl(), ntohl(), htons(), ntohs()
32 * On little endian machines these functions reverse the byte order of the
33 * input parameter and returns the result. This is to convert the byte order
34 * from host byte order (little endian) to network byte order (big endian),
35 * or vice versa.
37 * On big endian machines these functions just return the input parameter,
38 * as the host byte order is the same as the network byte order (big endian).
42 #ifdef _LITTLE_ENDIAN
43 uint64_t
44 htonll(uint64_t in)
46 return ((uint64_t)htonl((in >> 32) & 0xffffffff) |
47 ((uint64_t)htonl(in & 0xffffffff) << 32));
50 uint64_t
51 ntohll(uint64_t in)
53 return ((uint64_t)ntohl((in >> 32) & 0xffffffff) |
54 ((uint64_t)ntohl(in & 0xffffffff) << 32));
57 uint32_t
58 htonl(uint32_t in)
60 uint32_t i;
62 i = (uint32_t)((in & (uint32_t)0xff000000) >> 24) +
63 (uint32_t)((in & (uint32_t)0x00ff0000) >> 8) +
64 (uint32_t)((in & (uint32_t)0x0000ff00) << 8) +
65 (uint32_t)((in & (uint32_t)0x000000ff) << 24);
66 return (i);
69 uint32_t
70 ntohl(uint32_t in)
72 return (htonl(in));
75 uint16_t
76 htons(uint16_t in)
78 register int arg = (int)in;
79 uint16_t i;
81 i = (uint16_t)(((arg & 0xff00) >> 8) & 0xff);
82 i |= (uint16_t)((arg & 0xff) << 8);
83 return ((uint16_t)i);
86 uint16_t
87 ntohs(uint16_t in)
89 return (htons(in));
92 #else /* _LITTLE_ENDIAN */
94 #endif /* _LITTLE_ENDIAN */