Print this page
5007142 Add ntohll and htonll to sys/byteorder.h
6717509 Need to use bswap/bswapq for byte swap of 64-bit integer on x32/x64
PSARC 2008/474


   3  *
   4  * The contents of this file are subject to the terms of the
   5  * Common Development and Distribution License (the "License").
   6  * You may not use this file except in compliance with the License.
   7  *
   8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
   9  * or http://www.opensolaris.org/os/licensing.
  10  * See the License for the specific language governing permissions
  11  * and limitations under the License.
  12  *
  13  * When distributing Covered Code, include this CDDL HEADER in each
  14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
  15  * If applicable, add the following below this CDDL HEADER, with the
  16  * fields enclosed by brackets "[]" replaced with your own identifying
  17  * information: Portions Copyright [yyyy] [name of copyright owner]
  18  *
  19  * CDDL HEADER END
  20  */
  21 
  22 /*
  23  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
  24  * Use is subject to license terms.
  25  */
  26 
  27 /*      Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T */
  28 /*        All Rights Reserved   */
  29 
  30 /*
  31  * University Copyright- Copyright (c) 1982, 1986, 1988
  32  * The Regents of the University of California
  33  * All Rights Reserved
  34  *
  35  * University Acknowledgment- Portions of this document are derived from
  36  * software developed by the University of California, Berkeley, and its
  37  * contributors.
  38  */
  39 
  40 #ifndef _SYS_BYTEORDER_H
  41 #define _SYS_BYTEORDER_H
  42 
  43 #pragma ident   "%Z%%M% %I%     %E% SMI"
  44 
  45 #include <sys/isa_defs.h>
  46 #include <sys/int_types.h>
  47 
  48 #if defined(__GNUC__) && defined(_ASM_INLINES) && \
  49         (defined(__i386) || defined(__amd64))
  50 #include <asm/byteorder.h>
  51 #endif
  52 
  53 #ifdef  __cplusplus
  54 extern "C" {
  55 #endif
  56 
  57 /*
  58  * macros for conversion between host and (internet) network byte order
  59  */
  60 
  61 #if defined(_BIG_ENDIAN) && !defined(ntohl) && !defined(__lint)
  62 /* big-endian */
  63 #define ntohl(x)        (x)

  64 #define ntohs(x)        (x)
  65 #define htonl(x)        (x)

  66 #define htons(x)        (x)
  67 
  68 #elif !defined(ntohl) /* little-endian */
  69 
  70 #ifndef _IN_PORT_T
  71 #define _IN_PORT_T
  72 typedef uint16_t in_port_t;
  73 #endif
  74 
  75 #ifndef _IN_ADDR_T
  76 #define _IN_ADDR_T
  77 typedef uint32_t in_addr_t;
  78 #endif
  79 
  80 #if !defined(_XPG4_2) || defined(__EXTENSIONS__) || defined(_XPG5)
  81 extern  uint32_t htonl(uint32_t);
  82 extern  uint16_t htons(uint16_t);
  83 extern  uint32_t ntohl(uint32_t);
  84 extern  uint16_t ntohs(uint16_t);
  85 #else
  86 extern  in_addr_t htonl(in_addr_t);
  87 extern  in_port_t htons(in_port_t);
  88 extern  in_addr_t ntohl(in_addr_t);
  89 extern  in_port_t ntohs(in_port_t);
  90 #endif  /* !defined(_XPG4_2) || defined(__EXTENSIONS__) || defined(_XPG5) */




  91 #endif
  92 
  93 #if !defined(_XPG4_2) || defined(__EXTENSIONS__)
  94 
  95 /*
  96  * Macros to reverse byte order
  97  */
  98 #define BSWAP_8(x)      ((x) & 0xff)
  99 #define BSWAP_16(x)     ((BSWAP_8(x) << 8) | BSWAP_8((x) >> 8))
 100 #define BSWAP_32(x)     ((BSWAP_16(x) << 16) | BSWAP_16((x) >> 16))
 101 #define BSWAP_64(x)     ((BSWAP_32(x) << 32) | BSWAP_32((x) >> 32))
 102 
 103 #define BMASK_8(x)      ((x) & 0xff)
 104 #define BMASK_16(x)     ((x) & 0xffff)
 105 #define BMASK_32(x)     ((x) & 0xffffffff)
 106 #define BMASK_64(x)     (x)
 107 
 108 /*
 109  * Macros to convert from a specific byte order to/from native byte order
 110  */




   3  *
   4  * The contents of this file are subject to the terms of the
   5  * Common Development and Distribution License (the "License").
   6  * You may not use this file except in compliance with the License.
   7  *
   8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
   9  * or http://www.opensolaris.org/os/licensing.
  10  * See the License for the specific language governing permissions
  11  * and limitations under the License.
  12  *
  13  * When distributing Covered Code, include this CDDL HEADER in each
  14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
  15  * If applicable, add the following below this CDDL HEADER, with the
  16  * fields enclosed by brackets "[]" replaced with your own identifying
  17  * information: Portions Copyright [yyyy] [name of copyright owner]
  18  *
  19  * CDDL HEADER END
  20  */
  21 
  22 /*
  23  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
  24  * Use is subject to license terms.
  25  */
  26 
  27 /*      Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T */
  28 /*        All Rights Reserved   */
  29 
  30 /*
  31  * University Copyright- Copyright (c) 1982, 1986, 1988
  32  * The Regents of the University of California
  33  * All Rights Reserved
  34  *
  35  * University Acknowledgment- Portions of this document are derived from
  36  * software developed by the University of California, Berkeley, and its
  37  * contributors.
  38  */
  39 
  40 #ifndef _SYS_BYTEORDER_H
  41 #define _SYS_BYTEORDER_H
  42 


  43 #include <sys/isa_defs.h>
  44 #include <sys/int_types.h>
  45 
  46 #if defined(__GNUC__) && defined(_ASM_INLINES) && \
  47         (defined(__i386) || defined(__amd64))
  48 #include <asm/byteorder.h>
  49 #endif
  50 
  51 #ifdef  __cplusplus
  52 extern "C" {
  53 #endif
  54 
  55 /*
  56  * macros for conversion between host and (internet) network byte order
  57  */
  58 
  59 #if defined(_BIG_ENDIAN) && !defined(ntohl) && !defined(__lint)
  60 /* big-endian */
  61 #define ntohl(x)        (x)
  62 #define ntohll(x)       (x)
  63 #define ntohs(x)        (x)
  64 #define htonl(x)        (x)
  65 #define htonll(x)       (x)
  66 #define htons(x)        (x)
  67 
  68 #elif !defined(ntohl) /* little-endian */
  69 
  70 #ifndef _IN_PORT_T
  71 #define _IN_PORT_T
  72 typedef uint16_t in_port_t;
  73 #endif
  74 
  75 #ifndef _IN_ADDR_T
  76 #define _IN_ADDR_T
  77 typedef uint32_t in_addr_t;
  78 #endif
  79 
  80 #if !defined(_XPG4_2) || defined(__EXTENSIONS__) || defined(_XPG5)
  81 extern  uint32_t htonl(uint32_t);
  82 extern  uint16_t htons(uint16_t);
  83 extern  uint32_t ntohl(uint32_t);
  84 extern  uint16_t ntohs(uint16_t);
  85 #else
  86 extern  in_addr_t htonl(in_addr_t);
  87 extern  in_port_t htons(in_port_t);
  88 extern  in_addr_t ntohl(in_addr_t);
  89 extern  in_port_t ntohs(in_port_t);
  90 #endif  /* !defined(_XPG4_2) || defined(__EXTENSIONS__) || defined(_XPG5) */
  91 #if !(defined(_XPG4_2) || defined(_XPG5)) || defined(__EXTENSIONS__)
  92 extern  uint64_t htonll(uint64_t);
  93 extern  uint64_t ntohll(uint64_t);
  94 #endif  /* !(_XPG4_2||_XPG5) || __EXTENSIONS__ */
  95 #endif
  96 
  97 #if !defined(_XPG4_2) || defined(__EXTENSIONS__)
  98 
  99 /*
 100  * Macros to reverse byte order
 101  */
 102 #define BSWAP_8(x)      ((x) & 0xff)
 103 #define BSWAP_16(x)     ((BSWAP_8(x) << 8) | BSWAP_8((x) >> 8))
 104 #define BSWAP_32(x)     ((BSWAP_16(x) << 16) | BSWAP_16((x) >> 16))
 105 #define BSWAP_64(x)     ((BSWAP_32(x) << 32) | BSWAP_32((x) >> 32))
 106 
 107 #define BMASK_8(x)      ((x) & 0xff)
 108 #define BMASK_16(x)     ((x) & 0xffff)
 109 #define BMASK_32(x)     ((x) & 0xffffffff)
 110 #define BMASK_64(x)     (x)
 111 
 112 /*
 113  * Macros to convert from a specific byte order to/from native byte order
 114  */