The Design and Implementation of the FreeBSD Operating System, Second Edition
Now available: The Design and Implementation of the FreeBSD Operating System (Second Edition)


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]

FreeBSD/Linux Kernel Cross Reference
sys/sys/endian.h

Version: -  FREEBSD  -  FREEBSD-13-STABLE  -  FREEBSD-13-0  -  FREEBSD-12-STABLE  -  FREEBSD-12-0  -  FREEBSD-11-STABLE  -  FREEBSD-11-0  -  FREEBSD-10-STABLE  -  FREEBSD-10-0  -  FREEBSD-9-STABLE  -  FREEBSD-9-0  -  FREEBSD-8-STABLE  -  FREEBSD-8-0  -  FREEBSD-7-STABLE  -  FREEBSD-7-0  -  FREEBSD-6-STABLE  -  FREEBSD-6-0  -  FREEBSD-5-STABLE  -  FREEBSD-5-0  -  FREEBSD-4-STABLE  -  FREEBSD-3-STABLE  -  FREEBSD22  -  l41  -  OPENBSD  -  linux-2.6  -  MK84  -  PLAN9  -  xnu-8792 
SearchContext: -  none  -  3  -  10 

    1 /*      $NetBSD: endian.h,v 1.15 2005/02/03 19:16:10 perry Exp $        */
    2 
    3 /*
    4  * Copyright (c) 1987, 1991, 1993
    5  *      The Regents of the University of California.  All rights reserved.
    6  *
    7  * Redistribution and use in source and binary forms, with or without
    8  * modification, are permitted provided that the following conditions
    9  * are met:
   10  * 1. Redistributions of source code must retain the above copyright
   11  *    notice, this list of conditions and the following disclaimer.
   12  * 2. Redistributions in binary form must reproduce the above copyright
   13  *    notice, this list of conditions and the following disclaimer in the
   14  *    documentation and/or other materials provided with the distribution.
   15  * 3. Neither the name of the University nor the names of its contributors
   16  *    may be used to endorse or promote products derived from this software
   17  *    without specific prior written permission.
   18  *
   19  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
   20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
   21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
   22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
   23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
   24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
   25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
   26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
   28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   29  * SUCH DAMAGE.
   30  *
   31  *      @(#)endian.h    8.1 (Berkeley) 6/11/93
   32  */
   33 
   34 #ifndef _SYS_ENDIAN_H_
   35 #define _SYS_ENDIAN_H_
   36 
   37 #include <sys/featuretest.h>
   38 
   39 /*
   40  * Definitions for byte order, according to byte significance from low
   41  * address to high.
   42  */
   43 #define _LITTLE_ENDIAN  1234    /* LSB first: i386, vax */
   44 #define _BIG_ENDIAN     4321    /* MSB first: 68000, ibm, net */
   45 #define _PDP_ENDIAN     3412    /* LSB first in word, MSW first in long */
   46 
   47 
   48 #if defined(_XOPEN_SOURCE) || defined(_NETBSD_SOURCE)
   49 #ifndef _LOCORE
   50 
   51 /* C-family endian-ness definitions */
   52 
   53 #include <sys/ansi.h>
   54 #include <sys/cdefs.h>
   55 #include <sys/types.h>
   56 
   57 #ifndef in_addr_t
   58 typedef __in_addr_t     in_addr_t;
   59 #define in_addr_t       __in_addr_t
   60 #endif
   61 
   62 #ifndef in_port_t
   63 typedef __in_port_t     in_port_t;
   64 #define in_port_t       __in_port_t
   65 #endif
   66 
   67 __BEGIN_DECLS
   68 uint32_t htonl(uint32_t) __attribute__((__const__));
   69 uint16_t htons(uint16_t) __attribute__((__const__));
   70 uint32_t ntohl(uint32_t) __attribute__((__const__));
   71 uint16_t ntohs(uint16_t) __attribute__((__const__));
   72 __END_DECLS
   73 
   74 #endif /* !_LOCORE */
   75 #endif /* _XOPEN_SOURCE || _NETBSD_SOURCE */
   76 
   77 
   78 #include <machine/endian_machdep.h>
   79 
   80 /*
   81  * Define the order of 32-bit words in 64-bit words.
   82  */
   83 #if _BYTE_ORDER == _LITTLE_ENDIAN
   84 #define _QUAD_HIGHWORD 1
   85 #define _QUAD_LOWWORD 0
   86 #endif
   87 
   88 #if _BYTE_ORDER == _BIG_ENDIAN
   89 #define _QUAD_HIGHWORD 0
   90 #define _QUAD_LOWWORD 1
   91 #endif
   92 
   93 
   94 #if defined(_XOPEN_SOURCE) || defined(_NETBSD_SOURCE)
   95 /*
   96  *  Traditional names for byteorder.  These are defined as the numeric
   97  *  sequences so that third party code can "#define XXX_ENDIAN" and not
   98  *  cause errors.
   99  */
  100 #define LITTLE_ENDIAN   1234            /* LSB first: i386, vax */
  101 #define BIG_ENDIAN      4321            /* MSB first: 68000, ibm, net */
  102 #define PDP_ENDIAN      3412            /* LSB first in word, MSW first in long */
  103 #define BYTE_ORDER      _BYTE_ORDER
  104 
  105 #ifndef _LOCORE
  106 
  107 /*
  108  * Macros for network/external number representation conversion.
  109  */
  110 #if BYTE_ORDER == BIG_ENDIAN && !defined(__lint__)
  111 #define ntohl(x)        (x)
  112 #define ntohs(x)        (x)
  113 #define htonl(x)        (x)
  114 #define htons(x)        (x)
  115 
  116 #define NTOHL(x)        (void) (x)
  117 #define NTOHS(x)        (void) (x)
  118 #define HTONL(x)        (void) (x)
  119 #define HTONS(x)        (void) (x)
  120 
  121 #else   /* LITTLE_ENDIAN || !defined(__lint__) */
  122 
  123 #define NTOHL(x)        (x) = ntohl((uint32_t)(x))
  124 #define NTOHS(x)        (x) = ntohs((uint16_t)(x))
  125 #define HTONL(x)        (x) = htonl((uint32_t)(x))
  126 #define HTONS(x)        (x) = htons((uint16_t)(x))
  127 #endif  /* LITTLE_ENDIAN || !defined(__lint__) */
  128 
  129 /*
  130  * Macros to convert to a specific endianness.
  131  */
  132 
  133 #include <machine/bswap.h>
  134 
  135 #if BYTE_ORDER == BIG_ENDIAN
  136 
  137 #define htobe16(x)      (x)
  138 #define htobe32(x)      (x)
  139 #define htobe64(x)      (x)
  140 #define htole16(x)      bswap16((u_int16_t)(x))
  141 #define htole32(x)      bswap32((u_int32_t)(x))
  142 #define htole64(x)      bswap64((u_int64_t)(x))
  143 
  144 #define HTOBE16(x)      (void) (x)
  145 #define HTOBE32(x)      (void) (x)
  146 #define HTOBE64(x)      (void) (x)
  147 #define HTOLE16(x)      (x) = bswap16((u_int16_t)(x))
  148 #define HTOLE32(x)      (x) = bswap32((u_int32_t)(x))
  149 #define HTOLE64(x)      (x) = bswap64((u_int64_t)(x))
  150 
  151 #else   /* LITTLE_ENDIAN */
  152 
  153 #define htobe16(x)      bswap16((u_int16_t)(x))
  154 #define htobe32(x)      bswap32((u_int32_t)(x))
  155 #define htobe64(x)      bswap64((u_int64_t)(x))
  156 #define htole16(x)      (x)
  157 #define htole32(x)      (x)
  158 #define htole64(x)      (x)
  159 
  160 #define HTOBE16(x)      (x) = bswap16((u_int16_t)(x))
  161 #define HTOBE32(x)      (x) = bswap32((u_int32_t)(x))
  162 #define HTOBE64(x)      (x) = bswap64((u_int64_t)(x))
  163 #define HTOLE16(x)      (void) (x)
  164 #define HTOLE32(x)      (void) (x)
  165 #define HTOLE64(x)      (void) (x)
  166 
  167 #endif  /* LITTLE_ENDIAN */
  168 
  169 #define be16toh(x)      htobe16(x)
  170 #define be32toh(x)      htobe32(x)
  171 #define be64toh(x)      htobe64(x)
  172 #define le16toh(x)      htole16(x)
  173 #define le32toh(x)      htole32(x)
  174 #define le64toh(x)      htole64(x)
  175 
  176 #define BE16TOH(x)      HTOBE16(x)
  177 #define BE32TOH(x)      HTOBE32(x)
  178 #define BE64TOH(x)      HTOBE64(x)
  179 #define LE16TOH(x)      HTOLE16(x)
  180 #define LE32TOH(x)      HTOLE32(x)
  181 #define LE64TOH(x)      HTOLE64(x)
  182 
  183 /*
  184  * Routines to encode/decode big- and little-endian multi-octet values
  185  * to/from an octet stream.
  186  */
  187 
  188 static __inline void __unused
  189 be16enc(void *buf, uint16_t u)
  190 {
  191         uint8_t *p = (uint8_t *)buf;
  192 
  193         p[0] = ((unsigned)u >> 8) & 0xff;
  194         p[1] = u & 0xff;
  195 }
  196 
  197 static __inline void __unused
  198 le16enc(void *buf, uint16_t u)
  199 {
  200         uint8_t *p = (uint8_t *)buf;
  201 
  202         p[0] = u & 0xff;
  203         p[1] = ((unsigned)u >> 8) & 0xff;
  204 }
  205 
  206 static __inline uint16_t __unused
  207 be16dec(const void *buf)
  208 {
  209         const uint8_t *p = (const uint8_t *)buf;
  210 
  211         return ((p[0] << 8) | p[1]);
  212 }
  213 
  214 static __inline uint16_t __unused
  215 le16dec(const void *buf)
  216 {
  217         const uint8_t *p = (const uint8_t *)buf;
  218 
  219         return ((p[1] << 8) | p[0]);
  220 }
  221 
  222 static __inline void __unused
  223 be32enc(void *buf, uint32_t u)
  224 {
  225         uint8_t *p = (uint8_t *)buf;
  226 
  227         p[0] = (u >> 24) & 0xff;
  228         p[1] = (u >> 16) & 0xff;
  229         p[2] = (u >> 8) & 0xff;
  230         p[3] = u & 0xff;
  231 }
  232 
  233 static __inline void __unused
  234 le32enc(void *buf, uint32_t u)
  235 {
  236         uint8_t *p = (uint8_t *)buf;
  237 
  238         p[0] = u & 0xff;
  239         p[1] = (u >> 8) & 0xff;
  240         p[2] = (u >> 16) & 0xff;
  241         p[3] = (u >> 24) & 0xff;
  242 }
  243 
  244 static __inline uint32_t __unused
  245 be32dec(const void *buf)
  246 {
  247         const uint8_t *p = (const uint8_t *)buf;
  248 
  249         return ((p[0] << 24) | (p[1] << 16) | (p[2] << 8) | p[3]);
  250 }
  251 
  252 static __inline uint32_t __unused
  253 le32dec(const void *buf)
  254 {
  255         const uint8_t *p = (const uint8_t *)buf;
  256 
  257         return ((p[3] << 24) | (p[2] << 16) | (p[1] << 8) | p[0]);
  258 }
  259 
  260 static __inline void __unused
  261 be64enc(void *buf, uint64_t u)
  262 {
  263         uint8_t *p = (uint8_t *)buf;
  264 
  265         be32enc(p, (uint32_t)(u >> 32));
  266         be32enc(p + 4, (uint32_t)(u & 0xffffffffULL));
  267 }
  268 
  269 static __inline void __unused
  270 le64enc(void *buf, uint64_t u)
  271 {
  272         uint8_t *p = (uint8_t *)buf;
  273 
  274         le32enc(p, (uint32_t)(u & 0xffffffffULL));
  275         le32enc(p + 4, (uint32_t)(u >> 32));
  276 }
  277 
  278 static __inline uint64_t __unused
  279 be64dec(const void *buf)
  280 {
  281         const uint8_t *p = (const uint8_t *)buf;
  282 
  283         return (((uint64_t)be32dec(p) << 32) | be32dec(p + 4));
  284 }
  285 
  286 static __inline uint64_t __unused
  287 le64dec(const void *buf)
  288 {
  289         const uint8_t *p = (const uint8_t *)buf;
  290 
  291         return (le32dec(p) | ((uint64_t)le32dec(p + 4) << 32));
  292 }
  293 
  294 #endif /* !_LOCORE */
  295 #endif /* _XOPEN_SOURCE || _NETBSD_SOURCE */
  296 #endif /* !_SYS_ENDIAN_H_ */

Cache object: 66cf2631cbdb22c63409fbde2035688b


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]


This page is part of the FreeBSD/Linux Linux Kernel Cross-Reference, and was automatically generated using a modified version of the LXR engine.