00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
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
00297
00298
00299
00300
00301
00302
00303
00304
00305
00306
00307
00308
00309
00310
00311
00312
00313
00314
00315
00316
00317
00318
00319
00320
00321
00322
00323
00324
00325
00326
00327
00328
00329
00330
00331
00332
00333
00334
00335
00336
00337
00338
00339
00340
00341
00342
00343 #define IEEE754_FLOAT_BIAS (127)
00344 #define IEEE754_DOUBLE_BIAS (1023)
00345
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 }
00449
00450 #endif
00451