byteorder.h

Go to the documentation of this file.
00001 /*
00002  * Copyright (c) 2005 Jakub Jermar
00003  * All rights reserved.
00004  *
00005  * Redistribution and use in source and binary forms, with or without
00006  * modification, are permitted provided that the following conditions
00007  * are met:
00008  *
00009  * - Redistributions of source code must retain the above copyright
00010  *   notice, this list of conditions and the following disclaimer.
00011  * - Redistributions in binary form must reproduce the above copyright
00012  *   notice, this list of conditions and the following disclaimer in the
00013  *   documentation and/or other materials provided with the distribution.
00014  * - The name of the author may not be used to endorse or promote products
00015  *   derived from this software without specific prior written permission.
00016  *
00017  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
00018  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
00019  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
00020  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
00021  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
00022  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
00023  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
00024  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00025  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
00026  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00027  */
00028 
00035 #ifndef LIBC_BYTEORDER_H_
00036 #define LIBC_BYTEORDER_H_
00037 
00038 #include <stdint.h>
00039 
00040 #if !(defined(__BE__) ^ defined(__LE__))
00041         #error The architecture must be either big-endian or little-endian.
00042 #endif
00043 
00044 #ifdef __BE__
00045 
00046 #define uint16_t_le2host(n)  (uint16_t_byteorder_swap(n))
00047 #define uint32_t_le2host(n)  (uint32_t_byteorder_swap(n))
00048 #define uint64_t_le2host(n)  (uint64_t_byteorder_swap(n))
00049 
00050 #define uint16_t_be2host(n)  (n)
00051 #define uint32_t_be2host(n)  (n)
00052 #define uint64_t_be2host(n)  (n)
00053 
00054 #define host2uint16_t_le(n)  (uint16_t_byteorder_swap(n))
00055 #define host2uint32_t_le(n)  (uint32_t_byteorder_swap(n))
00056 #define host2uint64_t_le(n)  (uint64_t_byteorder_swap(n))
00057 
00058 #define host2uint16_t_be(n)  (n)
00059 #define host2uint32_t_be(n)  (n)
00060 #define host2uint64_t_be(n)  (n)
00061 
00062 #else
00063 
00064 #define uint16_t_le2host(n)  (n)
00065 #define uint32_t_le2host(n)  (n)
00066 #define uint64_t_le2host(n)  (n)
00067 
00068 #define uint16_t_be2host(n)  (uint16_t_byteorder_swap(n))
00069 #define uint32_t_be2host(n)  (uint32_t_byteorder_swap(n))
00070 #define uint64_t_be2host(n)  (uint64_t_byteorder_swap(n))
00071 
00072 #define host2uint16_t_le(n)  (n)
00073 #define host2uint32_t_le(n)  (n)
00074 #define host2uint64_t_le(n)  (n)
00075 
00076 #define host2uint16_t_be(n)  (uint16_t_byteorder_swap(n))
00077 #define host2uint32_t_be(n)  (uint32_t_byteorder_swap(n))
00078 #define host2uint64_t_be(n)  (uint64_t_byteorder_swap(n))
00079 
00080 #endif
00081 
00082 #define htons(n)  host2uint16_t_be((n))
00083 #define htonl(n)  host2uint32_t_be((n))
00084 #define ntohs(n)  uint16_t_be2host((n))
00085 #define ntohl(n)  uint32_t_be2host((n))
00086 
00087 static inline uint64_t uint64_t_byteorder_swap(uint64_t n)
00088 {
00089         return ((n & 0xff) << 56) |
00090             ((n & 0xff00) << 40) |
00091             ((n & 0xff0000) << 24) |
00092             ((n & 0xff000000LL) << 8) |
00093             ((n & 0xff00000000LL) >> 8) |
00094             ((n & 0xff0000000000LL) >> 24) |
00095             ((n & 0xff000000000000LL) >> 40) |
00096             ((n & 0xff00000000000000LL) >> 56);
00097 }
00098 
00099 static inline uint32_t uint32_t_byteorder_swap(uint32_t n)
00100 {
00101         return ((n & 0xff) << 24) |
00102             ((n & 0xff00) << 8) |
00103             ((n & 0xff0000) >> 8) |
00104             ((n & 0xff000000) >> 24);
00105 }
00106 
00107 static inline uint16_t uint16_t_byteorder_swap(uint16_t n)
00108 {
00109         return ((n & 0xff) << 8) |
00110             ((n & 0xff00) >> 8);
00111 }
00112 
00113 #endif
00114 

Generated on Thu Jun 2 07:45:46 2011 for HelenOS/USB by  doxygen 1.4.7