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 2006 Sun Microsystems, Inc.  All rights reserved.
  24  * Use is subject to license terms.
  25  */
  26 
  27 #ifndef _MD5_BYTESWAP_H
  28 #define _MD5_BYTESWAP_H
  29 
  30 #pragma ident   "%Z%%M% %I%     %E% SMI"
  31 
  32 /*
  33  * definitions for inline functions for little-endian loads.
  34  *
  35  * This file has special definitions for UltraSPARC architectures,
  36  * which have a special address space identifier for loading 32 and 16 bit
  37  * integers in little-endian byte order.
  38  *
  39  * This file and common/crypto/md5/sparc/sun4[uv]/byteswap.il implement the
  40  * same thing and must be changed together.
  41  */
  42 

  43 #if defined(__sparc)
  44 #include <v9/sys/asi.h>


  45 #endif
  46 
  47 #ifdef  __cplusplus
  48 extern "C" {
  49 #endif
  50 
  51 #if defined(_LITTLE_ENDIAN)
  52 
  53 /*
  54  * Little-endian optimization:  I don't need to do any weirdness.   On
  55  * some little-endian boxen, I'll have to do alignment checks, but I can do
  56  * that below.
  57  */
  58 
  59 #if !defined(__i386) && !defined(__amd64)
  60 /*
  61  * i386 and amd64 don't require aligned 4-byte loads.  The symbol
  62  * _MD5_CHECK_ALIGNMENT indicates below whether the MD5Transform function
  63  * requires alignment checking.
  64  */


  83 #if defined(sun4u)
  84 
  85 /* Define alignment check because we can 4-byte load as little endian. */
  86 #define _MD5_CHECK_ALIGNMENT
  87 #define LOAD_LITTLE_32(addr)    load_little_32((uint32_t *)(addr))
  88 
  89 #if !defined(__lint) && defined(__GNUC__)
  90 
  91 static __inline__ uint32_t
  92 load_little_32(uint32_t *addr)
  93 {
  94         uint32_t value;
  95 
  96         __asm__(
  97             "lduwa      [%1] %2, %0\n\t"
  98         : "=r" (value)
  99         : "r" (addr), "i" (ASI_PL));
 100 
 101         return (value);
 102 }
 103 
 104 static __inline__ uint16_t
 105 load_little_16(uint16_t *addr)
 106 {
 107         uint16_t value;
 108 
 109         __asm__(
 110             "lduha      [%1] %2, %0\n\t"
 111         : "=r" (value)
 112         : "r" (addr), "i" (ASI_PL));
 113 
 114         return (value);
 115 }
 116 
 117 #endif  /* !__lint && __GNUC__ */
 118 
 119 #if !defined(__GNUC__)
 120 extern  uint32_t load_little_32(uint32_t *);
 121 #endif  /* !__GNUC__ */
 122 
 123 /* Placate lint */
 124 #if defined(__lint)
 125 uint32_t
 126 load_little_32(uint32_t *addr)
 127 {
 128         return (*addr);
 129 }
 130 #endif  /* __lint */
 131 
 132 #else   /* !sun4u */

 133 

 134 /* big endian -- will work on little endian, but slowly */
 135 /* Since we do byte operations, we don't have to check for alignment. */
 136 #define LOAD_LITTLE_32(addr)    \
 137         ((addr)[0] | ((addr)[1] << 8) | ((addr)[2] << 16) | ((addr)[3] << 24))
 138 
 139 #endif  /* sun4u */
 140 
 141 #if defined(sun4v)
 142 
 143 /*
 144  * For N1 want to minimize number of arithmetic operations. This is best
 145  * achieved by using the %asi register to specify ASI for the lduwa operations.
 146  * Also, have a separate inline template for each word, so can utilize the
 147  * immediate offset in lduwa, without relying on the compiler to do the right
 148  * thing.
 149  *
 150  * Moving to 64-bit loads might also be beneficial.
 151  */
 152 #define LOAD_LITTLE_32_0(addr)  load_little_32_0((uint32_t *)(addr))
 153 #define LOAD_LITTLE_32_1(addr)  load_little_32_1((uint32_t *)(addr))
 154 #define LOAD_LITTLE_32_2(addr)  load_little_32_2((uint32_t *)(addr))
 155 #define LOAD_LITTLE_32_3(addr)  load_little_32_3((uint32_t *)(addr))
 156 #define LOAD_LITTLE_32_4(addr)  load_little_32_4((uint32_t *)(addr))
 157 #define LOAD_LITTLE_32_5(addr)  load_little_32_5((uint32_t *)(addr))
 158 #define LOAD_LITTLE_32_6(addr)  load_little_32_6((uint32_t *)(addr))




   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 #ifndef _MD5_BYTESWAP_H
  28 #define _MD5_BYTESWAP_H
  29 


  30 /*
  31  * definitions for inline functions for little-endian loads.
  32  *
  33  * This file has special definitions for UltraSPARC architectures,
  34  * which have a special address space identifier for loading 32 and 16 bit
  35  * integers in little-endian byte order.
  36  *
  37  * This file and common/crypto/md5/sparc/sun4[uv]/byteswap.il implement the
  38  * same thing and must be changed together.
  39  */
  40 
  41 #include <sys/types.h>
  42 #if defined(__sparc)
  43 #include <v9/sys/asi.h>
  44 #elif defined(_LITTLE_ENDIAN)
  45 #include <sys/byteorder.h>
  46 #endif
  47 
  48 #ifdef  __cplusplus
  49 extern "C" {
  50 #endif
  51 
  52 #if defined(_LITTLE_ENDIAN)
  53 
  54 /*
  55  * Little-endian optimization:  I don't need to do any weirdness.   On
  56  * some little-endian boxen, I'll have to do alignment checks, but I can do
  57  * that below.
  58  */
  59 
  60 #if !defined(__i386) && !defined(__amd64)
  61 /*
  62  * i386 and amd64 don't require aligned 4-byte loads.  The symbol
  63  * _MD5_CHECK_ALIGNMENT indicates below whether the MD5Transform function
  64  * requires alignment checking.
  65  */


  84 #if defined(sun4u)
  85 
  86 /* Define alignment check because we can 4-byte load as little endian. */
  87 #define _MD5_CHECK_ALIGNMENT
  88 #define LOAD_LITTLE_32(addr)    load_little_32((uint32_t *)(addr))
  89 
  90 #if !defined(__lint) && defined(__GNUC__)
  91 
  92 static __inline__ uint32_t
  93 load_little_32(uint32_t *addr)
  94 {
  95         uint32_t value;
  96 
  97         __asm__(
  98             "lduwa      [%1] %2, %0\n\t"
  99             : "=r" (value)
 100             : "r" (addr), "i" (ASI_PL));
 101 
 102         return (value);
 103 }














 104 #endif  /* !__lint && __GNUC__ */
 105 
 106 #if !defined(__GNUC__)
 107 extern  uint32_t load_little_32(uint32_t *);
 108 #endif  /* !__GNUC__ */
 109 
 110 /* Placate lint */
 111 #if defined(__lint)
 112 uint32_t
 113 load_little_32(uint32_t *addr)
 114 {
 115         return (*addr);
 116 }
 117 #endif  /* __lint */
 118 
 119 #elif defined(_LITTLE_ENDIAN)
 120 #define LOAD_LITTLE_32(addr)    htonl(addr)
 121 
 122 #else
 123 /* big endian -- will work on little endian, but slowly */
 124 /* Since we do byte operations, we don't have to check for alignment. */
 125 #define LOAD_LITTLE_32(addr)    \
 126         ((addr)[0] | ((addr)[1] << 8) | ((addr)[2] << 16) | ((addr)[3] << 24))

 127 #endif  /* sun4u */
 128 
 129 #if defined(sun4v)
 130 
 131 /*
 132  * For N1 want to minimize number of arithmetic operations. This is best
 133  * achieved by using the %asi register to specify ASI for the lduwa operations.
 134  * Also, have a separate inline template for each word, so can utilize the
 135  * immediate offset in lduwa, without relying on the compiler to do the right
 136  * thing.
 137  *
 138  * Moving to 64-bit loads might also be beneficial.
 139  */
 140 #define LOAD_LITTLE_32_0(addr)  load_little_32_0((uint32_t *)(addr))
 141 #define LOAD_LITTLE_32_1(addr)  load_little_32_1((uint32_t *)(addr))
 142 #define LOAD_LITTLE_32_2(addr)  load_little_32_2((uint32_t *)(addr))
 143 #define LOAD_LITTLE_32_3(addr)  load_little_32_3((uint32_t *)(addr))
 144 #define LOAD_LITTLE_32_4(addr)  load_little_32_4((uint32_t *)(addr))
 145 #define LOAD_LITTLE_32_5(addr)  load_little_32_5((uint32_t *)(addr))
 146 #define LOAD_LITTLE_32_6(addr)  load_little_32_6((uint32_t *)(addr))