xref: /netbsd/sys/lib/libsa/byteorder.c (revision 1c038e68)
1*1c038e68Sisaki /*	$NetBSD: byteorder.c,v 1.3 2007/11/24 13:20:54 isaki Exp $	*/
274eebee3Sthorpej 
374eebee3Sthorpej /*
474eebee3Sthorpej  * Copyright 2001 Wasabi Systems, Inc.
574eebee3Sthorpej  * All rights reserved.
674eebee3Sthorpej  *
774eebee3Sthorpej  * Written by Jason R. Thorpe for Wasabi Systems, Inc.
874eebee3Sthorpej  *
974eebee3Sthorpej  * Redistribution and use in source and binary forms, with or without
1074eebee3Sthorpej  * modification, are permitted provided that the following conditions
1174eebee3Sthorpej  * are met:
1274eebee3Sthorpej  * 1. Redistributions of source code must retain the above copyright
1374eebee3Sthorpej  *    notice, this list of conditions and the following disclaimer.
1474eebee3Sthorpej  * 2. Redistributions in binary form must reproduce the above copyright
1574eebee3Sthorpej  *    notice, this list of conditions and the following disclaimer in the
1674eebee3Sthorpej  *    documentation and/or other materials provided with the distribution.
1774eebee3Sthorpej  * 3. All advertising materials mentioning features or use of this software
1874eebee3Sthorpej  *    must display the following acknowledgement:
1974eebee3Sthorpej  *	This product includes software developed for the NetBSD Project by
2074eebee3Sthorpej  *	Wasabi Systems, Inc.
2174eebee3Sthorpej  * 4. The name of Wasabi Systems, Inc. may not be used to endorse
2274eebee3Sthorpej  *    or promote products derived from this software without specific prior
2374eebee3Sthorpej  *    written permission.
2474eebee3Sthorpej  *
2574eebee3Sthorpej  * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
2674eebee3Sthorpej  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
2774eebee3Sthorpej  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
2874eebee3Sthorpej  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL WASABI SYSTEMS, INC
2974eebee3Sthorpej  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
3074eebee3Sthorpej  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
3174eebee3Sthorpej  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
3274eebee3Sthorpej  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
3374eebee3Sthorpej  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
3474eebee3Sthorpej  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
3574eebee3Sthorpej  * POSSIBILITY OF SUCH DAMAGE.
3674eebee3Sthorpej  */
3774eebee3Sthorpej 
3874eebee3Sthorpej #include "byteorder.h"
3974eebee3Sthorpej 
4074eebee3Sthorpej typedef union {
4174eebee3Sthorpej 	uint16_t val;
4274eebee3Sthorpej 	uint8_t bytes[2];
4374eebee3Sthorpej } un16;
4474eebee3Sthorpej 
4574eebee3Sthorpej typedef union {
4674eebee3Sthorpej 	uint32_t val;
4774eebee3Sthorpej 	uint8_t bytes[4];
4874eebee3Sthorpej } un32;
4974eebee3Sthorpej 
5090aee759Sthorpej typedef union {
5190aee759Sthorpej 	uint64_t val;
5290aee759Sthorpej 	uint32_t words[2];
5390aee759Sthorpej } un64;
5490aee759Sthorpej 
5574eebee3Sthorpej /* 16-bit */
5674eebee3Sthorpej 
5774eebee3Sthorpej uint16_t
sa_htobe16(uint16_t val)5874eebee3Sthorpej sa_htobe16(uint16_t val)
5974eebee3Sthorpej {
6074eebee3Sthorpej 	un16 un;
6174eebee3Sthorpej 
6274eebee3Sthorpej 	un.bytes[1] = val & 0xff;
6374eebee3Sthorpej 	un.bytes[0] = (val >> 8) & 0xff;
6474eebee3Sthorpej 
65*1c038e68Sisaki 	return un.val;
6674eebee3Sthorpej }
6774eebee3Sthorpej 
6874eebee3Sthorpej uint16_t
sa_htole16(uint16_t val)6974eebee3Sthorpej sa_htole16(uint16_t val)
7074eebee3Sthorpej {
7174eebee3Sthorpej 	un16 un;
7274eebee3Sthorpej 
7374eebee3Sthorpej 	un.bytes[0] = val & 0xff;
7474eebee3Sthorpej 	un.bytes[1] = (val >> 8) & 0xff;
7574eebee3Sthorpej 
76*1c038e68Sisaki 	return un.val;
7774eebee3Sthorpej }
7874eebee3Sthorpej 
7974eebee3Sthorpej uint16_t
sa_be16toh(uint16_t val)8074eebee3Sthorpej sa_be16toh(uint16_t val)
8174eebee3Sthorpej {
8274eebee3Sthorpej 	un16 un;
8374eebee3Sthorpej 
8474eebee3Sthorpej 	un.val = val;
8574eebee3Sthorpej 
8674eebee3Sthorpej 	return ((un.bytes[0] << 8) |
8774eebee3Sthorpej 	         un.bytes[1]);
8874eebee3Sthorpej }
8974eebee3Sthorpej 
9074eebee3Sthorpej uint16_t
sa_le16toh(uint16_t val)9174eebee3Sthorpej sa_le16toh(uint16_t val)
9274eebee3Sthorpej {
9374eebee3Sthorpej 	un16 un;
9474eebee3Sthorpej 
9574eebee3Sthorpej 	un.val = val;
9674eebee3Sthorpej 
9774eebee3Sthorpej 	return ((un.bytes[1] << 8) |
9874eebee3Sthorpej 	         un.bytes[0]);
9974eebee3Sthorpej }
10074eebee3Sthorpej 
10174eebee3Sthorpej /* 32-bit */
10274eebee3Sthorpej 
10374eebee3Sthorpej uint32_t
sa_htobe32(uint32_t val)10474eebee3Sthorpej sa_htobe32(uint32_t val)
10574eebee3Sthorpej {
10674eebee3Sthorpej 	un32 un;
10774eebee3Sthorpej 
10874eebee3Sthorpej 	un.bytes[3] = val & 0xff;
10974eebee3Sthorpej 	un.bytes[2] = (val >> 8) & 0xff;
11074eebee3Sthorpej 	un.bytes[1] = (val >> 16) & 0xff;
11174eebee3Sthorpej 	un.bytes[0] = (val >> 24) & 0xff;
11274eebee3Sthorpej 
113*1c038e68Sisaki 	return un.val;
11474eebee3Sthorpej }
11574eebee3Sthorpej 
11674eebee3Sthorpej uint32_t
sa_htole32(uint32_t val)11774eebee3Sthorpej sa_htole32(uint32_t val)
11874eebee3Sthorpej {
11974eebee3Sthorpej 	un32 un;
12074eebee3Sthorpej 
12174eebee3Sthorpej 	un.bytes[0] = val & 0xff;
12274eebee3Sthorpej 	un.bytes[1] = (val >> 8) & 0xff;
12374eebee3Sthorpej 	un.bytes[2] = (val >> 16) & 0xff;
12474eebee3Sthorpej 	un.bytes[3] = (val >> 24) & 0xff;
12574eebee3Sthorpej 
126*1c038e68Sisaki 	return un.val;
12774eebee3Sthorpej }
12874eebee3Sthorpej 
12974eebee3Sthorpej uint32_t
sa_be32toh(uint32_t val)13074eebee3Sthorpej sa_be32toh(uint32_t val)
13174eebee3Sthorpej {
13274eebee3Sthorpej 	un32 un;
13374eebee3Sthorpej 
13474eebee3Sthorpej 	un.val = val;
13574eebee3Sthorpej 
13674eebee3Sthorpej 	return ((un.bytes[0] << 24) |
13774eebee3Sthorpej 	        (un.bytes[1] << 16) |
13874eebee3Sthorpej 	        (un.bytes[2] << 8) |
13974eebee3Sthorpej 	         un.bytes[3]);
14074eebee3Sthorpej }
14174eebee3Sthorpej 
14274eebee3Sthorpej uint32_t
sa_le32toh(uint32_t val)14374eebee3Sthorpej sa_le32toh(uint32_t val)
14474eebee3Sthorpej {
14574eebee3Sthorpej 	un32 un;
14674eebee3Sthorpej 
14774eebee3Sthorpej 	un.val = val;
14874eebee3Sthorpej 
14974eebee3Sthorpej 	return ((un.bytes[3] << 24) |
15074eebee3Sthorpej 	        (un.bytes[2] << 16) |
15174eebee3Sthorpej 	        (un.bytes[1] << 8) |
15274eebee3Sthorpej 	         un.bytes[0]);
15374eebee3Sthorpej }
15490aee759Sthorpej 
15590aee759Sthorpej /* 64-bit */
15690aee759Sthorpej 
15790aee759Sthorpej uint64_t
sa_htobe64(uint64_t val)15890aee759Sthorpej sa_htobe64(uint64_t val)
15990aee759Sthorpej {
16090aee759Sthorpej 	un64 un;
16190aee759Sthorpej 
16290aee759Sthorpej 	un.words[BE64_HI] = sa_htobe32(val >> 32);
16390aee759Sthorpej 	un.words[BE64_LO] = sa_htobe32(val & 0xffffffffU);
16490aee759Sthorpej 
165*1c038e68Sisaki 	return un.val;
16690aee759Sthorpej }
16790aee759Sthorpej 
16890aee759Sthorpej uint64_t
sa_htole64(uint64_t val)16990aee759Sthorpej sa_htole64(uint64_t val)
17090aee759Sthorpej {
17190aee759Sthorpej 	un64 un;
17290aee759Sthorpej 
17390aee759Sthorpej 	un.words[LE64_HI] = sa_htole32(val >> 32);
17490aee759Sthorpej 	un.words[LE64_LO] = sa_htole32(val & 0xffffffffU);
17590aee759Sthorpej 
176*1c038e68Sisaki 	return un.val;
17790aee759Sthorpej }
17890aee759Sthorpej 
17990aee759Sthorpej uint64_t
sa_be64toh(uint64_t val)18090aee759Sthorpej sa_be64toh(uint64_t val)
18190aee759Sthorpej {
18290aee759Sthorpej 	un64 un;
18390aee759Sthorpej 	uint64_t rv;
18490aee759Sthorpej 
18590aee759Sthorpej 	un.val = val;
18690aee759Sthorpej 	un.words[BE64_HI] = sa_be32toh(un.words[BE64_HI]);
18790aee759Sthorpej 	un.words[BE64_LO] = sa_be32toh(un.words[BE64_LO]);
18890aee759Sthorpej 
18990aee759Sthorpej 	rv = (((uint64_t)un.words[BE64_HI]) << 32) |
19090aee759Sthorpej 	     un.words[BE64_LO];
19190aee759Sthorpej 
192*1c038e68Sisaki 	return rv;
19390aee759Sthorpej }
19490aee759Sthorpej 
19590aee759Sthorpej uint64_t
sa_le64toh(uint64_t val)19690aee759Sthorpej sa_le64toh(uint64_t val)
19790aee759Sthorpej {
19890aee759Sthorpej 	un64 un;
19990aee759Sthorpej 	uint64_t rv;
20090aee759Sthorpej 
20190aee759Sthorpej 	un.val = val;
20290aee759Sthorpej 	un.words[LE64_HI] = sa_le32toh(un.words[LE64_HI]);
20390aee759Sthorpej 	un.words[LE64_LO] = sa_le32toh(un.words[LE64_LO]);
20490aee759Sthorpej 
20590aee759Sthorpej 	rv = (((uint64_t)un.words[LE64_HI]) << 32) |
20690aee759Sthorpej 	     un.words[LE64_LO];
20790aee759Sthorpej 
208*1c038e68Sisaki 	return rv;
20990aee759Sthorpej }
210