iPXE
byteswap.h
Go to the documentation of this file.
00001 #ifndef BYTESWAP_H
00002 #define BYTESWAP_H
00003 
00004 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
00005 
00006 #include <stdint.h>
00007 #include <endian.h>
00008 #include <bits/byteswap.h>
00009 
00010 /**
00011  * Byte-swap a 16-bit constant
00012  *
00013  * @v value             Constant value
00014  * @ret swapped         Byte-swapped value
00015  */
00016 #define __bswap_constant_16( value )                                    \
00017         ( ( ( (value) & 0x00ff ) << 8 ) |                               \
00018           ( ( (value) & 0xff00 ) >> 8 ) )
00019 
00020 /**
00021  * Byte-swap a 32-bit constant
00022  *
00023  * @v value             Constant value
00024  * @ret swapped         Byte-swapped value
00025  */
00026 #define __bswap_constant_32( value ) \
00027         ( ( ( (value) & 0x000000ffUL ) << 24 ) |                        \
00028           ( ( (value) & 0x0000ff00UL ) <<  8 ) |                        \
00029           ( ( (value) & 0x00ff0000UL ) >>  8 ) |                        \
00030           ( ( (value) & 0xff000000UL ) >> 24 ) )
00031 
00032 /**
00033  * Byte-swap a 64-bit constant
00034  *
00035  * @v value             Constant value
00036  * @ret swapped         Byte-swapped value
00037  */
00038 #define __bswap_constant_64( value )                                    \
00039         ( ( ( (value) & 0x00000000000000ffULL ) << 56 ) |               \
00040           ( ( (value) & 0x000000000000ff00ULL ) << 40 ) |               \
00041           ( ( (value) & 0x0000000000ff0000ULL ) << 24 ) |               \
00042           ( ( (value) & 0x00000000ff000000ULL ) <<  8 ) |               \
00043           ( ( (value) & 0x000000ff00000000ULL ) >>  8 ) |               \
00044           ( ( (value) & 0x0000ff0000000000ULL ) >> 24 ) |               \
00045           ( ( (value) & 0x00ff000000000000ULL ) >> 40 ) |               \
00046           ( ( (value) & 0xff00000000000000ULL ) >> 56 ) )
00047 
00048 /**
00049  * Byte-swap a 16-bit value
00050  *
00051  * @v value             Value
00052  * @ret swapped         Byte-swapped value
00053  */
00054 #define __bswap_16( value )                                             \
00055         ( __builtin_constant_p (value) ?                                \
00056           ( ( uint16_t ) __bswap_constant_16 ( ( uint16_t ) (value) ) ) \
00057           : __bswap_variable_16 (value) )
00058 #define bswap_16( value ) __bswap_16 (value)
00059 
00060 /**
00061  * Byte-swap a 32-bit value
00062  *
00063  * @v value             Value
00064  * @ret swapped         Byte-swapped value
00065  */
00066 #define __bswap_32( value )                                             \
00067         ( __builtin_constant_p (value) ?                                \
00068           ( ( uint32_t ) __bswap_constant_32 ( ( uint32_t ) (value) ) ) \
00069           : __bswap_variable_32 (value) )
00070 #define bswap_32( value ) __bswap_32 (value)
00071 
00072 /**
00073  * Byte-swap a 64-bit value
00074  *
00075  * @v value             Value
00076  * @ret swapped         Byte-swapped value
00077  */
00078 #define __bswap_64( value )                                             \
00079         ( __builtin_constant_p (value) ?                                \
00080           ( ( uint64_t ) __bswap_constant_64 ( ( uint64_t ) (value) ) ) \
00081           : __bswap_variable_64 (value) )
00082 #define bswap_64( value ) __bswap_64 (value)
00083 
00084 #if __BYTE_ORDER == __LITTLE_ENDIAN
00085 #define __cpu_to_leNN( bits, value ) (value)
00086 #define __cpu_to_beNN( bits, value ) __bswap_ ## bits (value)
00087 #define __leNN_to_cpu( bits, value ) (value)
00088 #define __beNN_to_cpu( bits, value ) __bswap_ ## bits (value)
00089 #define __cpu_to_leNNs( bits, ptr ) do { } while ( 0 )
00090 #define __cpu_to_beNNs( bits, ptr ) __bswap_ ## bits ## s (ptr)
00091 #define __leNN_to_cpus( bits, ptr ) do { } while ( 0 )
00092 #define __beNN_to_cpus( bits, ptr ) __bswap_ ## bits ## s (ptr)
00093 #endif
00094 
00095 #if __BYTE_ORDER == __BIG_ENDIAN
00096 #define __cpu_to_leNN( bits, value ) __bswap_ ## bits (value)
00097 #define __cpu_to_beNN( bits, value ) (value)
00098 #define __leNN_to_cpu( bits, value ) __bswap_ ## bits (value)
00099 #define __beNN_to_cpu( bits, value ) (value)
00100 #define __cpu_to_leNNs( bits, ptr ) __bswap_ ## bits ## s (ptr)
00101 #define __cpu_to_beNNs( bits, ptr ) do { } while ( 0 )
00102 #define __leNN_to_cpus( bits, ptr ) __bswap_ ## bits ## s (ptr)
00103 #define __beNN_to_cpus( bits, ptr ) do { } while ( 0 )
00104 #endif
00105 
00106 #define cpu_to_le16( value ) __cpu_to_leNN ( 16, value )
00107 #define cpu_to_le32( value ) __cpu_to_leNN ( 32, value )
00108 #define cpu_to_le64( value ) __cpu_to_leNN ( 64, value )
00109 #define cpu_to_be16( value ) __cpu_to_beNN ( 16, value )
00110 #define cpu_to_be32( value ) __cpu_to_beNN ( 32, value )
00111 #define cpu_to_be64( value ) __cpu_to_beNN ( 64, value )
00112 #define le16_to_cpu( value ) __leNN_to_cpu ( 16, value )
00113 #define le32_to_cpu( value ) __leNN_to_cpu ( 32, value )
00114 #define le64_to_cpu( value ) __leNN_to_cpu ( 64, value )
00115 #define be16_to_cpu( value ) __beNN_to_cpu ( 16, value )
00116 #define be32_to_cpu( value ) __beNN_to_cpu ( 32, value )
00117 #define be64_to_cpu( value ) __beNN_to_cpu ( 64, value )
00118 #define cpu_to_le16s( ptr ) __cpu_to_leNNs ( 16, ptr )
00119 #define cpu_to_le32s( ptr ) __cpu_to_leNNs ( 32, ptr )
00120 #define cpu_to_le64s( ptr ) __cpu_to_leNNs ( 64, ptr )
00121 #define cpu_to_be16s( ptr ) __cpu_to_beNNs ( 16, ptr )
00122 #define cpu_to_be32s( ptr ) __cpu_to_beNNs ( 32, ptr )
00123 #define cpu_to_be64s( ptr ) __cpu_to_beNNs ( 64, ptr )
00124 #define le16_to_cpus( ptr ) __leNN_to_cpus ( 16, ptr )
00125 #define le32_to_cpus( ptr ) __leNN_to_cpus ( 32, ptr )
00126 #define le64_to_cpus( ptr ) __leNN_to_cpus ( 64, ptr )
00127 #define be16_to_cpus( ptr ) __beNN_to_cpus ( 16, ptr )
00128 #define be32_to_cpus( ptr ) __beNN_to_cpus ( 32, ptr )
00129 #define be64_to_cpus( ptr ) __beNN_to_cpus ( 64, ptr )
00130 
00131 #define htonll( value ) cpu_to_be64 (value)
00132 #define ntohll( value ) be64_to_cpu (value)
00133 #define htonl( value ) cpu_to_be32 (value)
00134 #define ntohl( value ) be32_to_cpu (value)
00135 #define htons( value ) cpu_to_be16 (value)
00136 #define ntohs( value ) be16_to_cpu (value)
00137 
00138 #endif /* BYTESWAP_H */