GNU Radio 3.4.2 C++ API
usrp_bytesex.h
Go to the documentation of this file.
00001 /* -*- c++ -*- */
00002 /*
00003  * Copyright 2004 Free Software Foundation, Inc.
00004  * 
00005  * This file is part of GNU Radio
00006  * 
00007  * GNU Radio is free software; you can redistribute it and/or modify
00008  * it under the terms of the GNU General Public License as published by
00009  * the Free Software Foundation; either version 3, or (at your option)
00010  * any later version.
00011  * 
00012  * GNU Radio is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015  * GNU General Public License for more details.
00016  * 
00017  * You should have received a copy of the GNU General Public License
00018  * along with GNU Radio; see the file COPYING.  If not, write to
00019  * the Free Software Foundation, Inc., 51 Franklin Street,
00020  * Boston, MA 02110-1301, USA.
00021  */
00022 #ifndef INCLUDED_USRP_BYTESEX_H
00023 #define INCLUDED_USRP_BYTESEX_H
00024 
00025 /*!
00026  * \brief routines for convertering between host and usrp byte order
00027  *
00028  * Prior to including this file, the user must include "config.h"
00029  * which will or won't define WORDS_BIGENDIAN based on the
00030  * result of the AC_C_BIGENDIAN autoconf test.
00031  */
00032 
00033 #ifdef HAVE_BYTESWAP_H
00034 #include <byteswap.h>
00035 #else
00036 
00037 #warning Using non-portable code (likely wrong other than ILP32).
00038 
00039 static inline unsigned short int
00040 bswap_16 (unsigned short int x)
00041 {
00042   return ((((x) >> 8) & 0xff) | (((x) & 0xff) << 8));
00043 }
00044 
00045 static inline unsigned int
00046 bswap_32 (unsigned int x)
00047 {
00048   return ((((x) & 0xff000000) >> 24) | (((x) & 0x00ff0000) >>  8) \
00049         | (((x) & 0x0000ff00) <<  8) | (((x) & 0x000000ff) << 24));
00050 }
00051 #endif
00052 
00053 
00054 #ifdef WORDS_BIGENDIAN
00055 
00056 static inline unsigned int
00057 host_to_usrp_u32 (unsigned int x)
00058 {
00059   return bswap_32(x);
00060 }
00061 
00062 static inline unsigned int
00063 usrp_to_host_u32 (unsigned int x)
00064 {
00065   return bswap_32(x);
00066 }
00067 
00068 static inline short int
00069 host_to_usrp_short (short int x)
00070 {
00071   return bswap_16 (x);
00072 }
00073 
00074 static inline short int
00075 usrp_to_host_short (short int x)
00076 {
00077   return bswap_16 (x);
00078 }
00079 
00080 #else
00081 
00082 static inline unsigned int
00083 host_to_usrp_u32 (unsigned int x)
00084 {
00085   return x;
00086 }
00087 
00088 static inline unsigned int
00089 usrp_to_host_u32 (unsigned int x)
00090 {
00091   return x;
00092 }
00093 
00094 static inline short int
00095 host_to_usrp_short (short int x)
00096 {
00097   return x;
00098 }
00099 
00100 static inline short int
00101 usrp_to_host_short (unsigned short int x)
00102 {
00103   return x;
00104 }
00105 
00106 #endif
00107 
00108 #endif /* INCLUDED_USRP_BYTESEX_H */