1*3b4b8bdaSRobert Mustacchi /* 2*3b4b8bdaSRobert Mustacchi * This file and its contents are supplied under the terms of the 3*3b4b8bdaSRobert Mustacchi * Common Development and Distribution License ("CDDL"), version 1.0. 4*3b4b8bdaSRobert Mustacchi * You may only use this file in accordance with the terms of version 5*3b4b8bdaSRobert Mustacchi * 1.0 of the CDDL. 6*3b4b8bdaSRobert Mustacchi * 7*3b4b8bdaSRobert Mustacchi * A full copy of the text of the CDDL should have accompanied this 8*3b4b8bdaSRobert Mustacchi * source. A copy of the CDDL is also available via the Internet at 9*3b4b8bdaSRobert Mustacchi * http://www.illumos.org/license/CDDL. 10*3b4b8bdaSRobert Mustacchi */ 11*3b4b8bdaSRobert Mustacchi 12*3b4b8bdaSRobert Mustacchi /* 13*3b4b8bdaSRobert Mustacchi * Copyright 2016 Joyent, Inc. 14*3b4b8bdaSRobert Mustacchi */ 15*3b4b8bdaSRobert Mustacchi 16*3b4b8bdaSRobert Mustacchi /* 17*3b4b8bdaSRobert Mustacchi * Test endian(3C). 18*3b4b8bdaSRobert Mustacchi */ 19*3b4b8bdaSRobert Mustacchi 20*3b4b8bdaSRobert Mustacchi #include <sys/types.h> 21*3b4b8bdaSRobert Mustacchi #include <endian.h> 22*3b4b8bdaSRobert Mustacchi #include <sys/debug.h> 23*3b4b8bdaSRobert Mustacchi 24*3b4b8bdaSRobert Mustacchi #ifndef BIG_ENDIAN 25*3b4b8bdaSRobert Mustacchi #error "Missing BIG_ENDIAN definition" 26*3b4b8bdaSRobert Mustacchi #endif 27*3b4b8bdaSRobert Mustacchi 28*3b4b8bdaSRobert Mustacchi #ifndef LITTLE_ENDIAN 29*3b4b8bdaSRobert Mustacchi #error "Missing LITTLE_ENDIAN definition" 30*3b4b8bdaSRobert Mustacchi #endif 31*3b4b8bdaSRobert Mustacchi 32*3b4b8bdaSRobert Mustacchi static void 33*3b4b8bdaSRobert Mustacchi endian_fromhost(void) 34*3b4b8bdaSRobert Mustacchi { 35*3b4b8bdaSRobert Mustacchi uint16_t val16 = 0x1122; 36*3b4b8bdaSRobert Mustacchi uint32_t val32 = 0x11223344; 37*3b4b8bdaSRobert Mustacchi uint64_t val64 = 0x1122334455667788ULL; 38*3b4b8bdaSRobert Mustacchi uint16_t ebe16, ele16, test16; 39*3b4b8bdaSRobert Mustacchi uint32_t ebe32, ele32, test32; 40*3b4b8bdaSRobert Mustacchi uint64_t ebe64, ele64, test64; 41*3b4b8bdaSRobert Mustacchi 42*3b4b8bdaSRobert Mustacchi #ifdef _LITTLE_ENDIAN 43*3b4b8bdaSRobert Mustacchi ebe16 = 0x2211; 44*3b4b8bdaSRobert Mustacchi ebe32 = 0x44332211UL; 45*3b4b8bdaSRobert Mustacchi ebe64 = 0x8877665544332211ULL; 46*3b4b8bdaSRobert Mustacchi ele16 = 0x1122; 47*3b4b8bdaSRobert Mustacchi ele32 = 0x11223344UL; 48*3b4b8bdaSRobert Mustacchi ele64 = 0x1122334455667788ULL; 49*3b4b8bdaSRobert Mustacchi #elif _BIG_ENDIAN 50*3b4b8bdaSRobert Mustacchi ele16 = 0x2211; 51*3b4b8bdaSRobert Mustacchi ele32 = 0x44332211UL; 52*3b4b8bdaSRobert Mustacchi ele64 = 0x8877665544332211ULL; 53*3b4b8bdaSRobert Mustacchi ebe16 = 0x1122; 54*3b4b8bdaSRobert Mustacchi ebe32 = 0x11223344UL; 55*3b4b8bdaSRobert Mustacchi ebe64 = 0x1122334455667788ULL; 56*3b4b8bdaSRobert Mustacchi #else 57*3b4b8bdaSRobert Mustacchi #error "Unknown byte order" 58*3b4b8bdaSRobert Mustacchi #endif /* _LITTLE_ENDIAN */ 59*3b4b8bdaSRobert Mustacchi 60*3b4b8bdaSRobert Mustacchi test16 = htobe16(val16); 61*3b4b8bdaSRobert Mustacchi VERIFY3U(test16, ==, ebe16); 62*3b4b8bdaSRobert Mustacchi test32 = htobe32(val32); 63*3b4b8bdaSRobert Mustacchi VERIFY3U(test32, ==, ebe32); 64*3b4b8bdaSRobert Mustacchi test64 = htobe64(val64); 65*3b4b8bdaSRobert Mustacchi VERIFY3U(test64, ==, ebe64); 66*3b4b8bdaSRobert Mustacchi 67*3b4b8bdaSRobert Mustacchi test16 = htole16(val16); 68*3b4b8bdaSRobert Mustacchi VERIFY3U(test16, ==, ele16); 69*3b4b8bdaSRobert Mustacchi test32 = htole32(val32); 70*3b4b8bdaSRobert Mustacchi VERIFY3U(test32, ==, ele32); 71*3b4b8bdaSRobert Mustacchi test64 = htole64(val64); 72*3b4b8bdaSRobert Mustacchi VERIFY3U(test64, ==, ele64); 73*3b4b8bdaSRobert Mustacchi } 74*3b4b8bdaSRobert Mustacchi 75*3b4b8bdaSRobert Mustacchi static void 76*3b4b8bdaSRobert Mustacchi endian_frombig(void) 77*3b4b8bdaSRobert Mustacchi { 78*3b4b8bdaSRobert Mustacchi uint16_t val16 = 0x1122; 79*3b4b8bdaSRobert Mustacchi uint32_t val32 = 0x11223344; 80*3b4b8bdaSRobert Mustacchi uint64_t val64 = 0x1122334455667788ULL; 81*3b4b8bdaSRobert Mustacchi uint16_t e16, test16; 82*3b4b8bdaSRobert Mustacchi uint32_t e32, test32; 83*3b4b8bdaSRobert Mustacchi uint64_t e64, test64; 84*3b4b8bdaSRobert Mustacchi 85*3b4b8bdaSRobert Mustacchi #ifdef _LITTLE_ENDIAN 86*3b4b8bdaSRobert Mustacchi e16 = 0x2211; 87*3b4b8bdaSRobert Mustacchi e32 = 0x44332211UL; 88*3b4b8bdaSRobert Mustacchi e64 = 0x8877665544332211ULL; 89*3b4b8bdaSRobert Mustacchi #elif _BIG_ENDIAN 90*3b4b8bdaSRobert Mustacchi e16 = 0x1122; 91*3b4b8bdaSRobert Mustacchi e32 = 0x11223344UL; 92*3b4b8bdaSRobert Mustacchi e64 = 0x1122334455667788ULL; 93*3b4b8bdaSRobert Mustacchi #else 94*3b4b8bdaSRobert Mustacchi #error "Unknown byte order" 95*3b4b8bdaSRobert Mustacchi #endif /* _LITTLE_ENDIAN */ 96*3b4b8bdaSRobert Mustacchi 97*3b4b8bdaSRobert Mustacchi test16 = be16toh(val16); 98*3b4b8bdaSRobert Mustacchi VERIFY3U(test16, ==, e16); 99*3b4b8bdaSRobert Mustacchi test16 = betoh16(val16); 100*3b4b8bdaSRobert Mustacchi VERIFY3U(test16, ==, e16); 101*3b4b8bdaSRobert Mustacchi 102*3b4b8bdaSRobert Mustacchi test32 = be32toh(val32); 103*3b4b8bdaSRobert Mustacchi VERIFY3U(test32, ==, e32); 104*3b4b8bdaSRobert Mustacchi test32 = betoh32(val32); 105*3b4b8bdaSRobert Mustacchi VERIFY3U(test32, ==, e32); 106*3b4b8bdaSRobert Mustacchi 107*3b4b8bdaSRobert Mustacchi test64 = be64toh(val64); 108*3b4b8bdaSRobert Mustacchi VERIFY3U(test64, ==, e64); 109*3b4b8bdaSRobert Mustacchi test64 = betoh64(val64); 110*3b4b8bdaSRobert Mustacchi VERIFY3U(test64, ==, e64); 111*3b4b8bdaSRobert Mustacchi } 112*3b4b8bdaSRobert Mustacchi 113*3b4b8bdaSRobert Mustacchi static void 114*3b4b8bdaSRobert Mustacchi endian_fromlittle(void) 115*3b4b8bdaSRobert Mustacchi { 116*3b4b8bdaSRobert Mustacchi uint16_t val16 = 0x1122; 117*3b4b8bdaSRobert Mustacchi uint32_t val32 = 0x11223344; 118*3b4b8bdaSRobert Mustacchi uint64_t val64 = 0x1122334455667788ULL; 119*3b4b8bdaSRobert Mustacchi uint16_t e16, test16; 120*3b4b8bdaSRobert Mustacchi uint32_t e32, test32; 121*3b4b8bdaSRobert Mustacchi uint64_t e64, test64; 122*3b4b8bdaSRobert Mustacchi 123*3b4b8bdaSRobert Mustacchi #ifdef _LITTLE_ENDIAN 124*3b4b8bdaSRobert Mustacchi e16 = 0x1122; 125*3b4b8bdaSRobert Mustacchi e32 = 0x11223344UL; 126*3b4b8bdaSRobert Mustacchi e64 = 0x1122334455667788ULL; 127*3b4b8bdaSRobert Mustacchi #elif _BIG_ENDIAN 128*3b4b8bdaSRobert Mustacchi e16 = 0x2211; 129*3b4b8bdaSRobert Mustacchi e32 = 0x44332211UL; 130*3b4b8bdaSRobert Mustacchi e64 = 0x8877665544332211ULL; 131*3b4b8bdaSRobert Mustacchi #else 132*3b4b8bdaSRobert Mustacchi #error "Unknown byte order" 133*3b4b8bdaSRobert Mustacchi #endif /* _LITTLE_ENDIAN */ 134*3b4b8bdaSRobert Mustacchi 135*3b4b8bdaSRobert Mustacchi test16 = le16toh(val16); 136*3b4b8bdaSRobert Mustacchi VERIFY3U(test16, ==, e16); 137*3b4b8bdaSRobert Mustacchi test16 = letoh16(val16); 138*3b4b8bdaSRobert Mustacchi VERIFY3U(test16, ==, e16); 139*3b4b8bdaSRobert Mustacchi 140*3b4b8bdaSRobert Mustacchi test32 = le32toh(val32); 141*3b4b8bdaSRobert Mustacchi VERIFY3U(test32, ==, e32); 142*3b4b8bdaSRobert Mustacchi test32 = letoh32(val32); 143*3b4b8bdaSRobert Mustacchi VERIFY3U(test32, ==, e32); 144*3b4b8bdaSRobert Mustacchi 145*3b4b8bdaSRobert Mustacchi test64 = le64toh(val64); 146*3b4b8bdaSRobert Mustacchi VERIFY3U(test64, ==, e64); 147*3b4b8bdaSRobert Mustacchi test64 = letoh64(val64); 148*3b4b8bdaSRobert Mustacchi VERIFY3U(test64, ==, e64); 149*3b4b8bdaSRobert Mustacchi } 150*3b4b8bdaSRobert Mustacchi 151*3b4b8bdaSRobert Mustacchi int 152*3b4b8bdaSRobert Mustacchi main(void) 153*3b4b8bdaSRobert Mustacchi { 154*3b4b8bdaSRobert Mustacchi endian_fromhost(); 155*3b4b8bdaSRobert Mustacchi endian_frombig(); 156*3b4b8bdaSRobert Mustacchi endian_fromlittle(); 157*3b4b8bdaSRobert Mustacchi return (0); 158*3b4b8bdaSRobert Mustacchi } 159