utility.h

Go to the documentation of this file.
00001 /***************************************************************************
00002 *   Copyright (C) 2001 by Rick L. Vinyard, Jr.                            *
00003 *   rvinyard@cs.nmsu.edu                                                  *
00004 *                                                                         *
00005 *   This program is free software; you can redistribute it and/or modify  *
00006 *   it under the terms of the GNU Lesser General Public License as        *
00007 *   published by the Free Software Foundation version 2.1.                *
00008 *                                                                         *
00009 *   This program is distributed in the hope that it will be useful,       *
00010 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00011 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
00012 *   GNU General Public License for more details.                          *
00013 *                                                                         *
00014 *   You should have received a copy of the GNU Lesser General Public      *
00015 *   License along with this library; if not, write to the                 *
00016 *   Free Software Foundation, Inc.,                                       *
00017 *   51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA              *
00018 ***************************************************************************/
00019 #ifndef BITUTILITY_H
00020 #define BITUTILITY_H
00021 
00022 #include <sys/types.h>
00023 #include <stdint.h>
00024 #include <string>
00025 
00053 namespace bit {
00054 
00058 uint64_t be_to_host( uint64_t x);
00059 
00063 int64_t be_to_host( int64_t x);
00064 
00068 uint32_t be_to_host( uint32_t x);
00069 
00073 int32_t be_to_host( int32_t x);
00074 
00078 uint16_t be_to_host( uint16_t x);
00079 
00083 int16_t be_to_host( int16_t x);
00084 
00088 uint64_t host_to_be( uint64_t x);
00089 
00093 int64_t host_to_be( int64_t x);
00094 
00098 uint32_t host_to_be( uint32_t x);
00099 
00103 int32_t host_to_be( int32_t x);
00104 
00108 uint16_t host_to_be( uint16_t x);
00109 
00113 int16_t host_to_be( int16_t x);
00114 
00118 uint64_t le_to_host( uint64_t x);
00119 
00123 int64_t le_to_host( int64_t x);
00124 
00128 uint32_t le_to_host( uint32_t x);
00129 
00133 int32_t le_to_host( int32_t x);
00134 
00138 uint16_t le_to_host( uint16_t x);
00139 
00143 int16_t le_to_host( int16_t x);
00144 
00148 uint64_t host_to_le( uint64_t x);
00149 
00153 int64_t host_to_le( int64_t x);
00154 
00158 uint32_t host_to_le( uint32_t x);
00159 
00163 int32_t host_to_le( int32_t x);
00164 
00168 uint16_t host_to_le( uint16_t x);
00169 
00173 int16_t host_to_le( int16_t x);
00174 
00178 uint64_t net_to_host( uint64_t x);
00179 
00183 int64_t net_to_host( int64_t x);
00184 
00188 uint32_t net_to_host( uint32_t x);
00189 
00193 int32_t net_to_host( int32_t x);
00194 
00198 uint16_t net_to_host( uint16_t x);
00199 
00203 int16_t net_to_host( int16_t x);
00204 
00208 uint64_t host_to_net( uint64_t x);
00209 
00213 int64_t host_to_net( int64_t x);
00214 
00218 uint32_t host_to_net( uint32_t x);
00219 
00223 int32_t host_to_net( int32_t x);
00224 
00228 uint16_t host_to_net( uint16_t x);
00229 
00233 int16_t host_to_net( int16_t x);
00234 
00238 double be_to_host( double x);
00239 
00243 double le_to_host( double x);
00244 
00248 double net_to_host( double x);
00249 
00253 float be_to_host( float x);
00254 
00258 float le_to_host( float x);
00259 
00263 float net_to_host( float x);
00264 
00268 double host_to_be( double x);
00269 
00273 double host_to_le( double x);
00274 
00278 double host_to_net( double x);
00279 
00283 float host_to_be( float x);
00284 
00288 float host_to_le( float x);
00289 
00293 float host_to_net( float x);
00294 
00295 
00296 /* Modified from GLIB gtypes.h */
00297 
00298 /* GLIB - Library of useful routines for C programming
00299  * Copyright (C) 1995-1997  Peter Mattis, Spencer Kimball and Josh MacDonald
00300  *
00301  * This library is free software; you can redistribute it and/or
00302  * modify it under the terms of the GNU Lesser General Public
00303  * License as published by the Free Software Foundation; either
00304  * version 2 of the License, or (at your option) any later version.
00305  *
00306  * This library is distributed in the hope that it will be useful,
00307  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00308  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00309  * Lesser General Public License for more details.
00310  *
00311  * You should have received a copy of the GNU Lesser General Public
00312  * License along with this library; if not, write to the
00313  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
00314  * Boston, MA 02111-1307, USA.
00315  */
00316 
00317 /*
00318  * Modified by the GLib Team and others 1997-2000.  See the AUTHORS
00319  * file for a list of people on the GLib Team.  See the ChangeLog
00320  * files for a list of changes.  These files are distributed with
00321  * GLib at ftp://ftp.gtk.org/pub/gtk/.
00322  */
00323 
00324 
00325 
00326 /* IEEE Standard 754 Single Precision Storage Format (gfloat):
00327  *
00328  *        31 30           23 22            0
00329  * +--------+---------------+---------------+
00330  * | s 1bit | e[30:23] 8bit | f[22:0] 23bit |
00331  * +--------+---------------+---------------+
00332  * B0------------------->B1------->B2-->B3-->
00333  *
00334  * IEEE Standard 754 Double Precision Storage Format (gdouble):
00335  *
00336  *        63 62            52 51            32   31            0
00337  * +--------+----------------+----------------+ +---------------+
00338  * | s 1bit | e[62:52] 11bit | f[51:32] 20bit | | f[31:0] 32bit |
00339  * +--------+----------------+----------------+ +---------------+
00340  * B0--------------->B1---------->B2--->B3---->  B4->B5->B6->B7->
00341  */
00342 /* subtract from biased_exponent to form base2 exponent (normal numbers) */
00343 #define IEEE754_FLOAT_BIAS  (127)
00344 #define IEEE754_DOUBLE_BIAS (1023)
00345 /* multiply with base2 exponent to get base10 exponent (normal numbers) */
00346 #define LOG_2_BASE_10   (0.30102999566398119521)
00347 
00348 union lefloatIEEE754
00349 {
00350   float f;
00351   struct {
00352     unsigned int mantissa : 23;
00353     unsigned int biased_exponent : 8;
00354     unsigned int sign : 1;
00355   } mpn;
00356 };
00357 
00358 union ledoubleIEEE754
00359 {
00360   double d;
00361   struct {
00362     unsigned int mantissa_low : 32;
00363     unsigned int mantissa_high : 20;
00364     unsigned int biased_exponent : 11;
00365     unsigned int sign : 1;
00366   } mpn;
00367 };
00368 
00369 union befloatIEEE754
00370 {
00371   float f;
00372   struct {
00373     unsigned int sign : 1;
00374     unsigned int biased_exponent : 8;
00375     unsigned int mantissa : 23;
00376   } mpn;
00377 };
00378 
00379 union bedoubleIEEE754
00380 {
00381   double d;
00382   struct {
00383     unsigned int sign : 1;
00384     unsigned int biased_exponent : 11;
00385     unsigned int mantissa_high : 20;
00386     unsigned int mantissa_low : 32;
00387   } mpn;
00388 };
00389 
00394 size_t octet_ceiling(size_t bits);
00395 
00400 size_t octet_floor(size_t bits);
00401 
00406 void* starting_octet(void* p, size_t bits);
00407 
00412 void* ending_octet(void* p, size_t bits);
00413 
00419 size_t last_octet_upper_bits(size_t bits);
00420 
00426 size_t last_octet_lower_bits(size_t bits);
00427 
00429 void* left_shift(void* buffer, size_t buffer_size, size_t lshift_bits);
00430 
00432 void* right_shift(void* buffer, size_t buffer_size, size_t rshift_bits);
00433 
00434 std::string hex_string( void* buffer, size_t buffer_size,
00435                         bool uppercase=true,
00436                         std::string separator="",
00437                         std::string prefix="0x",
00438                         std::string postfix=""
00439                       );
00440 
00441 std::string binary_string( void* buffer, size_t buffer_size,
00442                            std::string separator="",
00443                            size_t separator_digits=8,
00444                            std::string prefix="",
00445                            std::string postfix=""
00446                          );
00447 
00448 } // namespace bit
00449 
00450 #endif
00451 

Generated on Thu Jul 6 14:38:08 2006 by  doxygen 1.4.6