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
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.
13 * Copyright 2016 Joyent, Inc.
20 #include <sys/types.h>
22 #include <sys/debug.h>
25 #error "Missing BIG_ENDIAN definition"
29 #error "Missing LITTLE_ENDIAN definition"
35 uint16_t val16
= 0x1122;
36 uint32_t val32
= 0x11223344;
37 uint64_t val64
= 0x1122334455667788ULL
;
38 uint16_t ebe16
, ele16
, test16
;
39 uint32_t ebe32
, ele32
, test32
;
40 uint64_t ebe64
, ele64
, test64
;
42 #if defined(_LITTLE_ENDIAN)
45 ebe64
= 0x8877665544332211ULL
;
48 ele64
= 0x1122334455667788ULL
;
49 #elif defined(_BIG_ENDIAN)
52 ele64
= 0x8877665544332211ULL
;
55 ebe64
= 0x1122334455667788ULL
;
57 #error "Unknown byte order"
58 #endif /* _LITTLE_ENDIAN */
60 test16
= htobe16(val16
);
61 VERIFY3U(test16
, ==, ebe16
);
62 test32
= htobe32(val32
);
63 VERIFY3U(test32
, ==, ebe32
);
64 test64
= htobe64(val64
);
65 VERIFY3U(test64
, ==, ebe64
);
67 test16
= htole16(val16
);
68 VERIFY3U(test16
, ==, ele16
);
69 test32
= htole32(val32
);
70 VERIFY3U(test32
, ==, ele32
);
71 test64
= htole64(val64
);
72 VERIFY3U(test64
, ==, ele64
);
78 uint16_t val16
= 0x1122;
79 uint32_t val32
= 0x11223344;
80 uint64_t val64
= 0x1122334455667788ULL
;
85 #if defined(_LITTLE_ENDIAN)
88 e64
= 0x8877665544332211ULL
;
89 #elif defined(_BIG_ENDIAN)
92 e64
= 0x1122334455667788ULL
;
94 #error "Unknown byte order"
95 #endif /* _LITTLE_ENDIAN */
97 test16
= be16toh(val16
);
98 VERIFY3U(test16
, ==, e16
);
99 test16
= betoh16(val16
);
100 VERIFY3U(test16
, ==, e16
);
102 test32
= be32toh(val32
);
103 VERIFY3U(test32
, ==, e32
);
104 test32
= betoh32(val32
);
105 VERIFY3U(test32
, ==, e32
);
107 test64
= be64toh(val64
);
108 VERIFY3U(test64
, ==, e64
);
109 test64
= betoh64(val64
);
110 VERIFY3U(test64
, ==, e64
);
114 endian_fromlittle(void)
116 uint16_t val16
= 0x1122;
117 uint32_t val32
= 0x11223344;
118 uint64_t val64
= 0x1122334455667788ULL
;
119 uint16_t e16
, test16
;
120 uint32_t e32
, test32
;
121 uint64_t e64
, test64
;
123 #if defined(_LITTLE_ENDIAN)
126 e64
= 0x1122334455667788ULL
;
127 #elif defined(_BIG_ENDIAN)
130 e64
= 0x8877665544332211ULL
;
132 #error "Unknown byte order"
133 #endif /* _LITTLE_ENDIAN */
135 test16
= le16toh(val16
);
136 VERIFY3U(test16
, ==, e16
);
137 test16
= letoh16(val16
);
138 VERIFY3U(test16
, ==, e16
);
140 test32
= le32toh(val32
);
141 VERIFY3U(test32
, ==, e32
);
142 test32
= letoh32(val32
);
143 VERIFY3U(test32
, ==, e32
);
145 test64
= le64toh(val64
);
146 VERIFY3U(test64
, ==, e64
);
147 test64
= letoh64(val64
);
148 VERIFY3U(test64
, ==, e64
);