extract.h (313537) | extract.h (327234) |
---|---|
1/* 2 * Copyright (c) 1992, 1993, 1994, 1995, 1996 3 * The Regents of the University of California. All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that: (1) source code distributions 7 * retain the above copyright notice and this paragraph in its entirety, (2) 8 * distributions including binary code include the above copyright notice and --- 6 unchanged lines hidden (view full) --- 15 * or promote products derived from this software without specific prior 16 * written permission. 17 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED 18 * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF 19 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. 20 */ 21 22/* | 1/* 2 * Copyright (c) 1992, 1993, 1994, 1995, 1996 3 * The Regents of the University of California. All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that: (1) source code distributions 7 * retain the above copyright notice and this paragraph in its entirety, (2) 8 * distributions including binary code include the above copyright notice and --- 6 unchanged lines hidden (view full) --- 15 * or promote products derived from this software without specific prior 16 * written permission. 17 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED 18 * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF 19 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. 20 */ 21 22/* |
23 * Macros to extract possibly-unaligned big-endian integral values. | 23 * For 8-bit values; provided for the sake of completeness. Byte order 24 * isn't relevant, and alignment isn't an issue. |
24 */ | 25 */ |
26#define EXTRACT_8BITS(p) (*(p)) 27#define EXTRACT_LE_8BITS(p) (*(p)) 28 29/* 30 * Inline functions or macros to extract possibly-unaligned big-endian 31 * integral values. 32 */ 33#include "funcattrs.h" 34 35/* 36 * If we have versions of GCC or Clang that support an __attribute__ 37 * to say "if we're building with unsigned behavior sanitization, 38 * don't complain about undefined behavior in this function", we 39 * label these functions with that attribute - we *know* it's undefined 40 * in the C standard, but we *also* know it does what we want with 41 * the ISA we're targeting and the compiler we're using. 42 * 43 * For GCC 4.9.0 and later, we use __attribute__((no_sanitize_undefined)); 44 * pre-5.0 GCC doesn't have __has_attribute, and I'm not sure whether 45 * GCC or Clang first had __attribute__((no_sanitize(XXX)). 46 * 47 * For Clang, we check for __attribute__((no_sanitize(XXX)) with 48 * __has_attribute, as there are versions of Clang that support 49 * __attribute__((no_sanitize("undefined")) but don't support 50 * __attribute__((no_sanitize_undefined)). 51 * 52 * We define this here, rather than in funcattrs.h, because we 53 * only want it used here, we don't want it to be broadly used. 54 * (Any printer will get this defined, but this should at least 55 * make it harder for people to find.) 56 */ 57#if defined(__GNUC__) && ((__GNUC__ * 100 + __GNUC_MINOR__) >= 409) 58#define UNALIGNED_OK __attribute__((no_sanitize_undefined)) 59#elif __has_attribute(no_sanitize) 60#define UNALIGNED_OK __attribute__((no_sanitize("undefined"))) 61#else 62#define UNALIGNED_OK 63#endif 64 |
|
25#ifdef LBL_ALIGN 26/* 27 * The processor doesn't natively handle unaligned loads. 28 */ 29#if defined(__GNUC__) && defined(HAVE___ATTRIBUTE__) && \ 30 (defined(__alpha) || defined(__alpha__) || \ 31 defined(__mips) || defined(__mips__)) 32 33/* | 65#ifdef LBL_ALIGN 66/* 67 * The processor doesn't natively handle unaligned loads. 68 */ 69#if defined(__GNUC__) && defined(HAVE___ATTRIBUTE__) && \ 70 (defined(__alpha) || defined(__alpha__) || \ 71 defined(__mips) || defined(__mips__)) 72 73/* |
34 * This is a GCC-compatible compiler and we have __attribute__, which | 74* This is a GCC-compatible compiler and we have __attribute__, which |
35 * we assume that mean we have __attribute__((packed)), and this is 36 * MIPS or Alpha, which has instructions that can help when doing 37 * unaligned loads. 38 * 39 * Declare packed structures containing a uint16_t and a uint32_t, 40 * cast the pointer to point to one of those, and fetch through it; 41 * the GCC manual doesn't appear to explicitly say that 42 * __attribute__((packed)) causes the compiler to generate unaligned-safe --- 40 unchanged lines hidden (view full) --- 83typedef struct { 84 uint16_t val; 85} __attribute__((packed)) unaligned_uint16_t; 86 87typedef struct { 88 uint32_t val; 89} __attribute__((packed)) unaligned_uint32_t; 90 | 75 * we assume that mean we have __attribute__((packed)), and this is 76 * MIPS or Alpha, which has instructions that can help when doing 77 * unaligned loads. 78 * 79 * Declare packed structures containing a uint16_t and a uint32_t, 80 * cast the pointer to point to one of those, and fetch through it; 81 * the GCC manual doesn't appear to explicitly say that 82 * __attribute__((packed)) causes the compiler to generate unaligned-safe --- 40 unchanged lines hidden (view full) --- 123typedef struct { 124 uint16_t val; 125} __attribute__((packed)) unaligned_uint16_t; 126 127typedef struct { 128 uint32_t val; 129} __attribute__((packed)) unaligned_uint32_t; 130 |
91static inline uint16_t | 131UNALIGNED_OK static inline uint16_t |
92EXTRACT_16BITS(const void *p) 93{ 94 return ((uint16_t)ntohs(((const unaligned_uint16_t *)(p))->val)); 95} 96 | 132EXTRACT_16BITS(const void *p) 133{ 134 return ((uint16_t)ntohs(((const unaligned_uint16_t *)(p))->val)); 135} 136 |
97static inline uint32_t | 137UNALIGNED_OK static inline uint32_t |
98EXTRACT_32BITS(const void *p) 99{ 100 return ((uint32_t)ntohl(((const unaligned_uint32_t *)(p))->val)); 101} 102 | 138EXTRACT_32BITS(const void *p) 139{ 140 return ((uint32_t)ntohl(((const unaligned_uint32_t *)(p))->val)); 141} 142 |
103static inline uint64_t | 143UNALIGNED_OK static inline uint64_t |
104EXTRACT_64BITS(const void *p) 105{ 106 return ((uint64_t)(((uint64_t)ntohl(((const unaligned_uint32_t *)(p) + 0)->val)) << 32 | 107 ((uint64_t)ntohl(((const unaligned_uint32_t *)(p) + 1)->val)) << 0)); 108} 109 110#else /* have to do it a byte at a time */ 111/* --- 21 unchanged lines hidden (view full) --- 133 ((uint64_t)(*((const uint8_t *)(p) + 6)) << 8) | \ 134 ((uint64_t)(*((const uint8_t *)(p) + 7)) << 0))) 135#endif /* must special-case unaligned accesses */ 136#else /* LBL_ALIGN */ 137/* 138 * The processor natively handles unaligned loads, so we can just 139 * cast the pointer and fetch through it. 140 */ | 144EXTRACT_64BITS(const void *p) 145{ 146 return ((uint64_t)(((uint64_t)ntohl(((const unaligned_uint32_t *)(p) + 0)->val)) << 32 | 147 ((uint64_t)ntohl(((const unaligned_uint32_t *)(p) + 1)->val)) << 0)); 148} 149 150#else /* have to do it a byte at a time */ 151/* --- 21 unchanged lines hidden (view full) --- 173 ((uint64_t)(*((const uint8_t *)(p) + 6)) << 8) | \ 174 ((uint64_t)(*((const uint8_t *)(p) + 7)) << 0))) 175#endif /* must special-case unaligned accesses */ 176#else /* LBL_ALIGN */ 177/* 178 * The processor natively handles unaligned loads, so we can just 179 * cast the pointer and fetch through it. 180 */ |
141static inline uint16_t | 181static inline uint16_t UNALIGNED_OK |
142EXTRACT_16BITS(const void *p) 143{ 144 return ((uint16_t)ntohs(*(const uint16_t *)(p))); 145} 146 | 182EXTRACT_16BITS(const void *p) 183{ 184 return ((uint16_t)ntohs(*(const uint16_t *)(p))); 185} 186 |
147static inline uint32_t | 187static inline uint32_t UNALIGNED_OK |
148EXTRACT_32BITS(const void *p) 149{ 150 return ((uint32_t)ntohl(*(const uint32_t *)(p))); 151} 152 | 188EXTRACT_32BITS(const void *p) 189{ 190 return ((uint32_t)ntohl(*(const uint32_t *)(p))); 191} 192 |
153static inline uint64_t | 193static inline uint64_t UNALIGNED_OK |
154EXTRACT_64BITS(const void *p) 155{ 156 return ((uint64_t)(((uint64_t)ntohl(*((const uint32_t *)(p) + 0))) << 32 | 157 ((uint64_t)ntohl(*((const uint32_t *)(p) + 1))) << 0)); 158 159} 160 161#endif /* LBL_ALIGN */ --- 26 unchanged lines hidden (view full) --- 188 ((uint64_t)(*((const uint8_t *)(p) + 4)) << 16) | \ 189 ((uint64_t)(*((const uint8_t *)(p) + 5)) << 8) | \ 190 ((uint64_t)(*((const uint8_t *)(p) + 6)) << 0))) 191 192/* 193 * Macros to extract possibly-unaligned little-endian integral values. 194 * XXX - do loads on little-endian machines that support unaligned loads? 195 */ | 194EXTRACT_64BITS(const void *p) 195{ 196 return ((uint64_t)(((uint64_t)ntohl(*((const uint32_t *)(p) + 0))) << 32 | 197 ((uint64_t)ntohl(*((const uint32_t *)(p) + 1))) << 0)); 198 199} 200 201#endif /* LBL_ALIGN */ --- 26 unchanged lines hidden (view full) --- 228 ((uint64_t)(*((const uint8_t *)(p) + 4)) << 16) | \ 229 ((uint64_t)(*((const uint8_t *)(p) + 5)) << 8) | \ 230 ((uint64_t)(*((const uint8_t *)(p) + 6)) << 0))) 231 232/* 233 * Macros to extract possibly-unaligned little-endian integral values. 234 * XXX - do loads on little-endian machines that support unaligned loads? 235 */ |
196#define EXTRACT_LE_8BITS(p) (*(p)) | |
197#define EXTRACT_LE_16BITS(p) \ 198 ((uint16_t)(((uint16_t)(*((const uint8_t *)(p) + 1)) << 8) | \ 199 ((uint16_t)(*((const uint8_t *)(p) + 0)) << 0))) 200#define EXTRACT_LE_32BITS(p) \ 201 ((uint32_t)(((uint32_t)(*((const uint8_t *)(p) + 3)) << 24) | \ 202 ((uint32_t)(*((const uint8_t *)(p) + 2)) << 16) | \ 203 ((uint32_t)(*((const uint8_t *)(p) + 1)) << 8) | \ 204 ((uint32_t)(*((const uint8_t *)(p) + 0)) << 0))) --- 32 unchanged lines hidden (view full) --- 237#define ND_TTEST_48BITS(p) ND_TTEST2(*(p), 6) 238#define ND_TCHECK_48BITS(p) ND_TCHECK2(*(p), 6) 239 240#define ND_TTEST_56BITS(p) ND_TTEST2(*(p), 7) 241#define ND_TCHECK_56BITS(p) ND_TCHECK2(*(p), 7) 242 243#define ND_TTEST_64BITS(p) ND_TTEST2(*(p), 8) 244#define ND_TCHECK_64BITS(p) ND_TCHECK2(*(p), 8) | 236#define EXTRACT_LE_16BITS(p) \ 237 ((uint16_t)(((uint16_t)(*((const uint8_t *)(p) + 1)) << 8) | \ 238 ((uint16_t)(*((const uint8_t *)(p) + 0)) << 0))) 239#define EXTRACT_LE_32BITS(p) \ 240 ((uint32_t)(((uint32_t)(*((const uint8_t *)(p) + 3)) << 24) | \ 241 ((uint32_t)(*((const uint8_t *)(p) + 2)) << 16) | \ 242 ((uint32_t)(*((const uint8_t *)(p) + 1)) << 8) | \ 243 ((uint32_t)(*((const uint8_t *)(p) + 0)) << 0))) --- 32 unchanged lines hidden (view full) --- 276#define ND_TTEST_48BITS(p) ND_TTEST2(*(p), 6) 277#define ND_TCHECK_48BITS(p) ND_TCHECK2(*(p), 6) 278 279#define ND_TTEST_56BITS(p) ND_TTEST2(*(p), 7) 280#define ND_TCHECK_56BITS(p) ND_TCHECK2(*(p), 7) 281 282#define ND_TTEST_64BITS(p) ND_TTEST2(*(p), 8) 283#define ND_TCHECK_64BITS(p) ND_TCHECK2(*(p), 8) |
284 285#define ND_TTEST_128BITS(p) ND_TTEST2(*(p), 16) 286#define ND_TCHECK_128BITS(p) ND_TCHECK2(*(p), 16) |
|