14edb46e9SPaul Traina /* 24edb46e9SPaul Traina * Copyright (c) 1992, 1993, 1994, 1995, 1996 34edb46e9SPaul Traina * The Regents of the University of California. All rights reserved. 44edb46e9SPaul Traina * 54edb46e9SPaul Traina * Redistribution and use in source and binary forms, with or without 64edb46e9SPaul Traina * modification, are permitted provided that: (1) source code distributions 74edb46e9SPaul Traina * retain the above copyright notice and this paragraph in its entirety, (2) 84edb46e9SPaul Traina * distributions including binary code include the above copyright notice and 94edb46e9SPaul Traina * this paragraph in its entirety in the documentation or other materials 104edb46e9SPaul Traina * provided with the distribution, and (3) all advertising materials mentioning 114edb46e9SPaul Traina * features or use of this software display the following acknowledgement: 124edb46e9SPaul Traina * ``This product includes software developed by the University of California, 134edb46e9SPaul Traina * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of 144edb46e9SPaul Traina * the University nor the names of its contributors may be used to endorse 154edb46e9SPaul Traina * or promote products derived from this software without specific prior 164edb46e9SPaul Traina * written permission. 174edb46e9SPaul Traina * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED 184edb46e9SPaul Traina * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF 194edb46e9SPaul Traina * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. 204edb46e9SPaul Traina */ 214edb46e9SPaul Traina 221de50e9fSSam Leffler /* 231de50e9fSSam Leffler * Macros to extract possibly-unaligned big-endian integral values. 241de50e9fSSam Leffler */ 254edb46e9SPaul Traina #ifdef LBL_ALIGN 265b0fe478SBruce M Simpson /* 275b0fe478SBruce M Simpson * The processor doesn't natively handle unaligned loads. 285b0fe478SBruce M Simpson */ 293c602fabSXin LI #if defined(__GNUC__) && defined(HAVE___ATTRIBUTE__) && \ 303c602fabSXin LI (defined(__alpha) || defined(__alpha__) || \ 313c602fabSXin LI defined(__mips) || defined(__mips__)) 323c602fabSXin LI 335b0fe478SBruce M Simpson /* 343c602fabSXin LI * This is a GCC-compatible compiler and we have __attribute__, which 353c602fabSXin LI * we assume that mean we have __attribute__((packed)), and this is 363c602fabSXin LI * MIPS or Alpha, which has instructions that can help when doing 373c602fabSXin LI * unaligned loads. 383c602fabSXin LI * 393c602fabSXin LI * Declare packed structures containing a uint16_t and a uint32_t, 405b0fe478SBruce M Simpson * cast the pointer to point to one of those, and fetch through it; 415b0fe478SBruce M Simpson * the GCC manual doesn't appear to explicitly say that 425b0fe478SBruce M Simpson * __attribute__((packed)) causes the compiler to generate unaligned-safe 435b0fe478SBruce M Simpson * code, but it apppears to do so. 445b0fe478SBruce M Simpson * 453c602fabSXin LI * We do this in case the compiler can generate code using those 463c602fabSXin LI * instructions to do an unaligned load and pass stuff to "ntohs()" or 473c602fabSXin LI * "ntohl()", which might be better than than the code to fetch the 483c602fabSXin LI * bytes one at a time and assemble them. (That might not be the 493c602fabSXin LI * case on a little-endian platform, such as DEC's MIPS machines and 503c602fabSXin LI * Alpha machines, where "ntohs()" and "ntohl()" might not be done 513c602fabSXin LI * inline.) 523c602fabSXin LI * 533c602fabSXin LI * We do this only for specific architectures because, for example, 543c602fabSXin LI * at least some versions of GCC, when compiling for 64-bit SPARC, 553c602fabSXin LI * generate code that assumes alignment if we do this. 563c602fabSXin LI * 573c602fabSXin LI * XXX - add other architectures and compilers as possible and 583c602fabSXin LI * appropriate. 593c602fabSXin LI * 603c602fabSXin LI * HP's C compiler, indicated by __HP_cc being defined, supports 613c602fabSXin LI * "#pragma unaligned N" in version A.05.50 and later, where "N" 623c602fabSXin LI * specifies a number of bytes at which the typedef on the next 633c602fabSXin LI * line is aligned, e.g. 643c602fabSXin LI * 653c602fabSXin LI * #pragma unalign 1 663c602fabSXin LI * typedef uint16_t unaligned_uint16_t; 673c602fabSXin LI * 683c602fabSXin LI * to define unaligned_uint16_t as a 16-bit unaligned data type. 693c602fabSXin LI * This could be presumably used, in sufficiently recent versions of 703c602fabSXin LI * the compiler, with macros similar to those below. This would be 713c602fabSXin LI * useful only if that compiler could generate better code for PA-RISC 723c602fabSXin LI * or Itanium than would be generated by a bunch of shifts-and-ORs. 733c602fabSXin LI * 743c602fabSXin LI * DEC C, indicated by __DECC being defined, has, at least on Alpha, 753c602fabSXin LI * an __unaligned qualifier that can be applied to pointers to get the 763c602fabSXin LI * compiler to generate code that does unaligned loads and stores when 773c602fabSXin LI * dereferencing the pointer in question. 783c602fabSXin LI * 793c602fabSXin LI * XXX - what if the native C compiler doesn't support 803c602fabSXin LI * __attribute__((packed))? How can we get it to generate unaligned 813c602fabSXin LI * accesses for *specific* items? 825b0fe478SBruce M Simpson */ 835b0fe478SBruce M Simpson typedef struct { 843c602fabSXin LI uint16_t val; 853c602fabSXin LI } __attribute__((packed)) unaligned_uint16_t; 865b0fe478SBruce M Simpson 875b0fe478SBruce M Simpson typedef struct { 883c602fabSXin LI uint32_t val; 893c602fabSXin LI } __attribute__((packed)) unaligned_uint32_t; 905b0fe478SBruce M Simpson 913c602fabSXin LI static inline uint16_t 92340b3427SPedro F. Giffuni EXTRACT_16BITS(const void *p) 93340b3427SPedro F. Giffuni { 943c602fabSXin LI return ((uint16_t)ntohs(((const unaligned_uint16_t *)(p))->val)); 95340b3427SPedro F. Giffuni } 96340b3427SPedro F. Giffuni 973c602fabSXin LI static inline uint32_t 98340b3427SPedro F. Giffuni EXTRACT_32BITS(const void *p) 99340b3427SPedro F. Giffuni { 1003c602fabSXin LI return ((uint32_t)ntohl(((const unaligned_uint32_t *)(p))->val)); 101340b3427SPedro F. Giffuni } 102340b3427SPedro F. Giffuni 1033c602fabSXin LI static inline uint64_t 104340b3427SPedro F. Giffuni EXTRACT_64BITS(const void *p) 105340b3427SPedro F. Giffuni { 1063c602fabSXin LI return ((uint64_t)(((uint64_t)ntohl(((const unaligned_uint32_t *)(p) + 0)->val)) << 32 | \ 1073c602fabSXin LI ((uint64_t)ntohl(((const unaligned_uint32_t *)(p) + 1)->val)) << 0)); 108340b3427SPedro F. Giffuni } 1091de50e9fSSam Leffler 1103c602fabSXin LI #else /* have to do it a byte at a time */ 1115b0fe478SBruce M Simpson /* 1123c602fabSXin LI * This isn't a GCC-compatible compiler, we don't have __attribute__, 1133c602fabSXin LI * or we do but we don't know of any better way with this instruction 1143c602fabSXin LI * set to do unaligned loads, so do unaligned loads of big-endian 1155b0fe478SBruce M Simpson * quantities the hard way - fetch the bytes one at a time and 1165b0fe478SBruce M Simpson * assemble them. 1175b0fe478SBruce M Simpson */ 1185b0fe478SBruce M Simpson #define EXTRACT_16BITS(p) \ 119*8bdc5a62SPatrick Kelsey ((uint16_t)(((uint16_t)(*((const uint8_t *)(p) + 0)) << 8) | \ 120*8bdc5a62SPatrick Kelsey ((uint16_t)(*((const uint8_t *)(p) + 1)) << 0))) 1215b0fe478SBruce M Simpson #define EXTRACT_32BITS(p) \ 122*8bdc5a62SPatrick Kelsey ((uint32_t)(((uint32_t)(*((const uint8_t *)(p) + 0)) << 24) | \ 123*8bdc5a62SPatrick Kelsey ((uint32_t)(*((const uint8_t *)(p) + 1)) << 16) | \ 124*8bdc5a62SPatrick Kelsey ((uint32_t)(*((const uint8_t *)(p) + 2)) << 8) | \ 125*8bdc5a62SPatrick Kelsey ((uint32_t)(*((const uint8_t *)(p) + 3)) << 0))) 1261de50e9fSSam Leffler #define EXTRACT_64BITS(p) \ 127*8bdc5a62SPatrick Kelsey ((uint64_t)(((uint64_t)(*((const uint8_t *)(p) + 0)) << 56) | \ 128*8bdc5a62SPatrick Kelsey ((uint64_t)(*((const uint8_t *)(p) + 1)) << 48) | \ 129*8bdc5a62SPatrick Kelsey ((uint64_t)(*((const uint8_t *)(p) + 2)) << 40) | \ 130*8bdc5a62SPatrick Kelsey ((uint64_t)(*((const uint8_t *)(p) + 3)) << 32) | \ 131*8bdc5a62SPatrick Kelsey ((uint64_t)(*((const uint8_t *)(p) + 4)) << 24) | \ 132*8bdc5a62SPatrick Kelsey ((uint64_t)(*((const uint8_t *)(p) + 5)) << 16) | \ 133*8bdc5a62SPatrick Kelsey ((uint64_t)(*((const uint8_t *)(p) + 6)) << 8) | \ 134*8bdc5a62SPatrick Kelsey ((uint64_t)(*((const uint8_t *)(p) + 7)) << 0))) 1353c602fabSXin LI #endif /* must special-case unaligned accesses */ 1365b0fe478SBruce M Simpson #else /* LBL_ALIGN */ 1375b0fe478SBruce M Simpson /* 1385b0fe478SBruce M Simpson * The processor natively handles unaligned loads, so we can just 1395b0fe478SBruce M Simpson * cast the pointer and fetch through it. 1405b0fe478SBruce M Simpson */ 1413c602fabSXin LI static inline uint16_t 142340b3427SPedro F. Giffuni EXTRACT_16BITS(const void *p) 143340b3427SPedro F. Giffuni { 1443c602fabSXin LI return ((uint16_t)ntohs(*(const uint16_t *)(p))); 145340b3427SPedro F. Giffuni } 146340b3427SPedro F. Giffuni 1473c602fabSXin LI static inline uint32_t 148340b3427SPedro F. Giffuni EXTRACT_32BITS(const void *p) 149340b3427SPedro F. Giffuni { 1503c602fabSXin LI return ((uint32_t)ntohl(*(const uint32_t *)(p))); 151340b3427SPedro F. Giffuni } 152340b3427SPedro F. Giffuni 1533c602fabSXin LI static inline uint64_t 154340b3427SPedro F. Giffuni EXTRACT_64BITS(const void *p) 155340b3427SPedro F. Giffuni { 1563c602fabSXin LI return ((uint64_t)(((uint64_t)ntohl(*((const uint32_t *)(p) + 0))) << 32 | \ 1573c602fabSXin LI ((uint64_t)ntohl(*((const uint32_t *)(p) + 1))) << 0)); 158340b3427SPedro F. Giffuni 159340b3427SPedro F. Giffuni } 160340b3427SPedro F. Giffuni 1615b0fe478SBruce M Simpson #endif /* LBL_ALIGN */ 1624edb46e9SPaul Traina 1634edb46e9SPaul Traina #define EXTRACT_24BITS(p) \ 164*8bdc5a62SPatrick Kelsey ((uint32_t)(((uint32_t)(*((const uint8_t *)(p) + 0)) << 16) | \ 165*8bdc5a62SPatrick Kelsey ((uint32_t)(*((const uint8_t *)(p) + 1)) << 8) | \ 166*8bdc5a62SPatrick Kelsey ((uint32_t)(*((const uint8_t *)(p) + 2)) << 0))) 1673c602fabSXin LI 1683c602fabSXin LI #define EXTRACT_40BITS(p) \ 169*8bdc5a62SPatrick Kelsey ((uint64_t)(((uint64_t)(*((const uint8_t *)(p) + 0)) << 32) | \ 170*8bdc5a62SPatrick Kelsey ((uint64_t)(*((const uint8_t *)(p) + 1)) << 24) | \ 171*8bdc5a62SPatrick Kelsey ((uint64_t)(*((const uint8_t *)(p) + 2)) << 16) | \ 172*8bdc5a62SPatrick Kelsey ((uint64_t)(*((const uint8_t *)(p) + 3)) << 8) | \ 173*8bdc5a62SPatrick Kelsey ((uint64_t)(*((const uint8_t *)(p) + 4)) << 0))) 1743c602fabSXin LI 1753c602fabSXin LI #define EXTRACT_48BITS(p) \ 176*8bdc5a62SPatrick Kelsey ((uint64_t)(((uint64_t)(*((const uint8_t *)(p) + 0)) << 40) | \ 177*8bdc5a62SPatrick Kelsey ((uint64_t)(*((const uint8_t *)(p) + 1)) << 32) | \ 178*8bdc5a62SPatrick Kelsey ((uint64_t)(*((const uint8_t *)(p) + 2)) << 24) | \ 179*8bdc5a62SPatrick Kelsey ((uint64_t)(*((const uint8_t *)(p) + 3)) << 16) | \ 180*8bdc5a62SPatrick Kelsey ((uint64_t)(*((const uint8_t *)(p) + 4)) << 8) | \ 181*8bdc5a62SPatrick Kelsey ((uint64_t)(*((const uint8_t *)(p) + 5)) << 0))) 1823c602fabSXin LI 1833c602fabSXin LI #define EXTRACT_56BITS(p) \ 184*8bdc5a62SPatrick Kelsey ((uint64_t)(((uint64_t)(*((const uint8_t *)(p) + 0)) << 48) | \ 185*8bdc5a62SPatrick Kelsey ((uint64_t)(*((const uint8_t *)(p) + 1)) << 40) | \ 186*8bdc5a62SPatrick Kelsey ((uint64_t)(*((const uint8_t *)(p) + 2)) << 32) | \ 187*8bdc5a62SPatrick Kelsey ((uint64_t)(*((const uint8_t *)(p) + 3)) << 24) | \ 188*8bdc5a62SPatrick Kelsey ((uint64_t)(*((const uint8_t *)(p) + 4)) << 16) | \ 189*8bdc5a62SPatrick Kelsey ((uint64_t)(*((const uint8_t *)(p) + 5)) << 8) | \ 190*8bdc5a62SPatrick Kelsey ((uint64_t)(*((const uint8_t *)(p) + 6)) << 0))) 1914edb46e9SPaul Traina 1921de50e9fSSam Leffler /* 1931de50e9fSSam Leffler * Macros to extract possibly-unaligned little-endian integral values. 1941de50e9fSSam Leffler * XXX - do loads on little-endian machines that support unaligned loads? 1951de50e9fSSam Leffler */ 1964edb46e9SPaul Traina #define EXTRACT_LE_8BITS(p) (*(p)) 1974edb46e9SPaul Traina #define EXTRACT_LE_16BITS(p) \ 198*8bdc5a62SPatrick Kelsey ((uint16_t)(((uint16_t)(*((const uint8_t *)(p) + 1)) << 8) | \ 199*8bdc5a62SPatrick Kelsey ((uint16_t)(*((const uint8_t *)(p) + 0)) << 0))) 2004edb46e9SPaul Traina #define EXTRACT_LE_32BITS(p) \ 201*8bdc5a62SPatrick Kelsey ((uint32_t)(((uint32_t)(*((const uint8_t *)(p) + 3)) << 24) | \ 202*8bdc5a62SPatrick Kelsey ((uint32_t)(*((const uint8_t *)(p) + 2)) << 16) | \ 203*8bdc5a62SPatrick Kelsey ((uint32_t)(*((const uint8_t *)(p) + 1)) << 8) | \ 204*8bdc5a62SPatrick Kelsey ((uint32_t)(*((const uint8_t *)(p) + 0)) << 0))) 205a5779b6eSRui Paulo #define EXTRACT_LE_24BITS(p) \ 206*8bdc5a62SPatrick Kelsey ((uint32_t)(((uint32_t)(*((const uint8_t *)(p) + 2)) << 16) | \ 207*8bdc5a62SPatrick Kelsey ((uint32_t)(*((const uint8_t *)(p) + 1)) << 8) | \ 208*8bdc5a62SPatrick Kelsey ((uint32_t)(*((const uint8_t *)(p) + 0)) << 0))) 2091de50e9fSSam Leffler #define EXTRACT_LE_64BITS(p) \ 210*8bdc5a62SPatrick Kelsey ((uint64_t)(((uint64_t)(*((const uint8_t *)(p) + 7)) << 56) | \ 211*8bdc5a62SPatrick Kelsey ((uint64_t)(*((const uint8_t *)(p) + 6)) << 48) | \ 212*8bdc5a62SPatrick Kelsey ((uint64_t)(*((const uint8_t *)(p) + 5)) << 40) | \ 213*8bdc5a62SPatrick Kelsey ((uint64_t)(*((const uint8_t *)(p) + 4)) << 32) | \ 214*8bdc5a62SPatrick Kelsey ((uint64_t)(*((const uint8_t *)(p) + 3)) << 24) | \ 215*8bdc5a62SPatrick Kelsey ((uint64_t)(*((const uint8_t *)(p) + 2)) << 16) | \ 216*8bdc5a62SPatrick Kelsey ((uint64_t)(*((const uint8_t *)(p) + 1)) << 8) | \ 217*8bdc5a62SPatrick Kelsey ((uint64_t)(*((const uint8_t *)(p) + 0)) << 0))) 218