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
00093 uint8_t be_to_host( uint8_t x);
00094
00103 int8_t be_to_host( int8_t x);
00104
00108 uint64_t host_to_be( uint64_t x);
00109
00113 int64_t host_to_be( int64_t x);
00114
00118 uint32_t host_to_be( uint32_t x);
00119
00123 int32_t host_to_be( int32_t x);
00124
00128 uint16_t host_to_be( uint16_t x);
00129
00133 int16_t host_to_be( int16_t x);
00134
00143 uint8_t host_to_be( uint8_t x);
00144
00153 int8_t host_to_be( int8_t x);
00154
00158 uint64_t le_to_host( uint64_t x);
00159
00163 int64_t le_to_host( int64_t x);
00164
00168 uint32_t le_to_host( uint32_t x);
00169
00173 int32_t le_to_host( int32_t x);
00174
00178 uint16_t le_to_host( uint16_t x);
00179
00183 int16_t le_to_host( int16_t x);
00184
00193 uint8_t le_to_host( uint8_t x);
00194
00203 int8_t le_to_host( int8_t x);
00204
00208 uint64_t host_to_le( uint64_t x);
00209
00213 int64_t host_to_le( int64_t x);
00214
00218 uint32_t host_to_le( uint32_t x);
00219
00223 int32_t host_to_le( int32_t x);
00224
00228 uint16_t host_to_le( uint16_t x);
00229
00233 int16_t host_to_le( int16_t x);
00234
00243 uint8_t host_to_le( uint8_t x);
00244
00253 int8_t host_to_le( int8_t x);
00254
00258 uint64_t net_to_host( uint64_t x);
00259
00263 int64_t net_to_host( int64_t x);
00264
00268 uint32_t net_to_host( uint32_t x);
00269
00273 int32_t net_to_host( int32_t x);
00274
00278 uint16_t net_to_host( uint16_t x);
00279
00283 int16_t net_to_host( int16_t x);
00284
00293 uint8_t net_to_host( uint8_t x);
00294
00303 int8_t net_to_host( int8_t x);
00304
00308 uint64_t host_to_net( uint64_t x);
00309
00313 int64_t host_to_net( int64_t x);
00314
00318 uint32_t host_to_net( uint32_t x);
00319
00323 int32_t host_to_net( int32_t x);
00324
00328 uint16_t host_to_net( uint16_t x);
00329
00333 int16_t host_to_net( int16_t x);
00334
00343 uint8_t host_to_net( uint8_t x);
00344
00353 int8_t host_to_net( int8_t x);
00354
00358 uint64_t le_to_be( uint64_t x);
00359
00363 int64_t le_to_be( int64_t x);
00364
00368 uint32_t le_to_be( uint32_t x);
00369
00373 int32_t le_to_be( int32_t x);
00374
00378 uint16_t le_to_be( uint16_t x);
00379
00383 int16_t le_to_be( int16_t x);
00384
00393 uint8_t le_to_be( uint8_t x);
00394
00403 int8_t le_to_be( int8_t x);
00404
00408 uint64_t le_to_net( uint64_t x);
00409
00413 int64_t le_to_net( int64_t x);
00414
00418 uint32_t le_to_net( uint32_t x);
00419
00423 int32_t le_to_net( int32_t x);
00424
00428 uint16_t le_to_net( uint16_t x);
00429
00433 int16_t le_to_net( int16_t x);
00434
00443 uint8_t le_to_net( uint8_t x);
00444
00453 int8_t le_to_net( int8_t x);
00454
00458 uint64_t be_to_le( uint64_t x);
00459
00463 int64_t be_to_le( int64_t x);
00464
00468 uint32_t be_to_le( uint32_t x);
00469
00473 int32_t be_to_le( int32_t x);
00474
00478 uint16_t be_to_le( uint16_t x);
00479
00483 int16_t be_to_le( int16_t x);
00484
00493 uint8_t be_to_le( uint8_t x);
00494
00503 int8_t be_to_le( int8_t x);
00504
00508 uint64_t net_to_le( uint64_t x);
00509
00513 int64_t net_to_le( int64_t x);
00514
00518 uint32_t net_to_le( uint32_t x);
00519
00523 int32_t net_to_le( int32_t x);
00524
00528 uint16_t net_to_le( uint16_t x);
00529
00533 int16_t net_to_le( int16_t x);
00534
00543 uint8_t net_to_le( uint8_t x);
00544
00553 int8_t net_to_le( int8_t x);
00554
00558 double be_to_host( double x);
00559
00563 double le_to_host( double x);
00564
00568 double net_to_host( double x);
00569
00573 float be_to_host( float x);
00574
00578 float le_to_host( float x);
00579
00583 float net_to_host( float x);
00584
00588 long double be_to_host( long double x);
00589
00593 long double le_to_host( long double x);
00594
00598 long double net_to_host( long double x);
00599
00603 double host_to_be( double x);
00604
00608 double host_to_le( double x);
00609
00613 double host_to_net( double x);
00614
00618 float host_to_be( float x);
00619
00623 float host_to_le( float x);
00624
00628 float host_to_net( float x);
00629
00633 long double host_to_be( long double x);
00634
00638 long double host_to_le( long double x);
00639
00643 long double host_to_net( long double x);
00644
00645
00649 double le_to_be( double x);
00650
00654 float le_to_be( float x);
00655
00659 long double le_to_be( long double x);
00660
00664 double le_to_net( double x);
00665
00669 float le_to_net( float x);
00670
00674 long double le_to_net( long double x);
00675
00679 double be_to_le( double x);
00680
00684 float be_to_le( float x);
00685
00689 long double be_to_le( long double x);
00690
00694 double net_to_le( double x);
00695
00699 float net_to_le( float x);
00700
00704 long double net_to_le( long double x);
00705
00706
00707
00708
00709
00710
00711
00712
00713
00714
00715
00716
00717
00718
00719
00720
00721
00722
00723
00724
00725
00726
00727
00728
00729
00730
00731
00732
00733
00734
00735
00736
00737
00738
00739
00740
00741
00742
00743
00744
00745
00746
00747
00748
00749
00750
00751
00752
00753
00754 #define IEEE754_SINGLE_BIAS (127)
00755 #define IEEE754_DOUBLE_BIAS (1023)
00756 #define IEEE754_QUADRUPLE_BIAS (16383)
00757
00758 #define LOG_2_BASE_10 (0.30102999566398119521)
00759
00760 union le_single_IEEE754
00761 {
00762 float f;
00763 struct {
00764 unsigned int mantissa : 23;
00765 unsigned int biased_exponent : 8;
00766 unsigned int sign : 1;
00767 } mpn;
00768 };
00769
00770 union le_double_IEEE754
00771 {
00772 double d;
00773 struct {
00774 unsigned int mantissa_low : 32;
00775 unsigned int mantissa_high : 20;
00776 unsigned int biased_exponent : 11;
00777 unsigned int sign : 1;
00778 } mpn;
00779 };
00780
00781 union le_quadruple_IEEE754
00782 {
00783 long double q;
00784 struct {
00785 unsigned int mantissa_0 : 32;
00786 unsigned int mantissa_1 : 32;
00787 unsigned int mantissa_2 : 32;
00788 unsigned int mantissa_3 : 16;
00789 unsigned int biased_exponent : 15;
00790 unsigned int sign : 1;
00791 } mpn;
00792 };
00793
00794 union be_single_IEEE754
00795 {
00796 float f;
00797 struct {
00798 unsigned int sign : 1;
00799 unsigned int biased_exponent : 8;
00800 unsigned int mantissa : 23;
00801 } mpn;
00802 };
00803
00804 union be_double_IEEE754
00805 {
00806 double d;
00807 struct {
00808 unsigned int sign : 1;
00809 unsigned int biased_exponent : 11;
00810 unsigned int mantissa_high : 20;
00811 unsigned int mantissa_low : 32;
00812 } mpn;
00813 };
00814
00815 union be_quadruple_IEEE754
00816 {
00817 long double q;
00818 struct {
00819 unsigned int sign : 1;
00820 unsigned int biased_exponent : 15;
00821 unsigned int mantissa_3 : 16;
00822 unsigned int mantissa_2 : 32;
00823 unsigned int mantissa_1 : 32;
00824 unsigned int mantissa_0 : 32;
00825 } mpn;
00826 };
00827
00832 size_t octet_ceiling(size_t bits);
00833
00838 size_t octet_floor(size_t bits);
00839
00844 void* starting_octet(void* p, size_t bits);
00845
00850 void* ending_octet(void* p, size_t bits);
00851
00857 size_t last_octet_upper_bits(size_t bits);
00858
00864 size_t last_octet_lower_bits(size_t bits);
00865
00867 void* left_shift(void* buffer, size_t buffer_size, size_t lshift_bits);
00868
00870 void* right_shift(void* buffer, size_t buffer_size, size_t rshift_bits);
00871
00872 std::string hex_string( void* buffer, size_t buffer_size,
00873 bool uppercase=true,
00874 std::string separator="",
00875 std::string prefix="0x",
00876 std::string postfix=""
00877 );
00878
00879 std::string binary_string( void* buffer, size_t buffer_size,
00880 std::string separator="",
00881 size_t separator_digits=8,
00882 std::string prefix="",
00883 std::string postfix=""
00884 );
00885
00886 }
00887
00888 #endif
00889