Mesh Oriented datABase  (version 5.4.1)
Array-based unstructured mesh datastructure
SysUtil.cpp
Go to the documentation of this file.
00001 #include "SysUtil.hpp"
00002 
00003 #include <fstream>
00004 #include <algorithm>
00005 #include <cassert>
00006 
00007 #include "moab/EntityHandle.hpp"
00008 
00009 namespace moab
00010 {
00011 
00012 namespace SysUtil
00013 {
00014 
00015     void setmem( void* mem, const void* value, unsigned value_size, size_t num_elem )
00016     {
00017         if( !num_elem ) return;
00018 
00019         char* array = reinterpret_cast< char* >( mem );
00020         memcpy( array, value, value_size );
00021         size_t count;
00022         for( count = 1; count * 2 < num_elem; count *= 2 )
00023             memcpy( array + count * value_size, array, count * value_size );
00024         memcpy( array + count * value_size, array, ( num_elem - count ) * value_size );
00025     }
00026 
00027     long filesize( FILE* filp )
00028     {
00029         long curr_pos = ftell( filp );
00030         if( fseek( filp, 0, SEEK_END ) ) return -1;
00031 
00032         long length = ftell( filp );
00033         if( fseek( filp, curr_pos, SEEK_SET ) )
00034         {
00035             assert( 0 );
00036             return -2;
00037         }
00038 
00039         return length;
00040     }
00041 
00042     long filesize( std::ifstream& str )
00043     {
00044         std::istream::pos_type curr_pos = str.tellg();
00045         if( !str.seekg( 0, std::ios_base::end ) ) return -1;
00046 
00047         long length = static_cast< long >( str.tellg() );
00048         if( !str.seekg( curr_pos, std::ios_base::beg ) )
00049         {
00050             assert( 0 );
00051             return -2;
00052         }
00053 
00054         return length;
00055     }
00056 
00057     void byteswap( void* data, unsigned value_size, size_t num_elem )
00058     {
00059         char* mem       = reinterpret_cast< char* >( data );
00060         char* const end = mem + value_size * num_elem;
00061         for( ; mem < end; mem += value_size )
00062         {
00063             unsigned i = 0, j = value_size - 1;
00064             while( i < j )
00065                 std::swap( mem[i++], mem[j--] );
00066         }
00067     }
00068 
00069     inline static uint16_t swap_bytes( uint16_t value )
00070     {
00071         return ( value >> 8 ) | ( value << 8 );
00072     }
00073 
00074     inline static uint32_t swap_bytes( uint32_t value )
00075     {
00076         return ( ( value /*& (uint32_t)0xFF000000*/ ) >> 24 ) | ( ( value & (uint32_t)0x00FF0000 ) >> 8 ) |
00077                ( ( value & (uint32_t)0x0000FF00 ) << 8 ) | ( ( value /*& (uint32_t)0X000000FF*/ ) << 24 );
00078     }
00079 
00080     const uint64_t m64b1 = 0xFF;
00081     const uint64_t m64b2 = m64b1 << 8;
00082     const uint64_t m64b3 = m64b1 << 16;
00083     const uint64_t m64b4 = m64b1 << 24;
00084     const uint64_t m64b5 = m64b1 << 32;
00085     const uint64_t m64b6 = m64b1 << 40;
00086     const uint64_t m64b7 = m64b1 << 48;
00087     // const uint64_t m64b8 = m64b1 << 56;
00088 
00089     inline static uint64_t swap_bytes( uint64_t value )
00090     {
00091         return ( ( value /*& m64b8*/ ) >> 56 ) | ( ( value & m64b7 ) >> 40 ) | ( ( value & m64b6 ) >> 24 ) |
00092                ( ( value & m64b5 ) >> 8 ) | ( ( value & m64b4 ) << 8 ) | ( ( value & m64b3 ) << 24 ) |
00093                ( ( value & m64b2 ) << 40 ) | ( ( value /*& m64b1*/ ) << 56 );
00094     }
00095     /*
00096     inline static uint32_t swap_byte_pairs( uint32_t value )
00097     {
00098       return ((value & (uint32_t)0xFF000000) >> 8) |
00099              ((value & (uint32_t)0x00FF0000) << 8) |
00100              ((value & (uint32_t)0x0000FF00) >> 8) |
00101              ((value & (uint32_t)0X000000FF) << 8);
00102     }
00103 
00104     inline static uint64_t swap_byte_quads( uint64_t value )
00105     {
00106       return ((value & m64b8) >> 24) |
00107              ((value & m64b7) >>  8) |
00108              ((value & m64b6) <<  8) |
00109              ((value & m64b5) << 24) |
00110              ((value & m64b4) >> 24) |
00111              ((value & m64b3) >>  8) |
00112              ((value & m64b2) <<  8) |
00113              ((value & m64b1) << 24);
00114     }
00115 
00116     inline static uint64_t swap_byte_pairs( uint64_t value )
00117     {
00118       return ((value & m64b8) >> 8) |
00119              ((value & m64b7) << 8) |
00120              ((value & m64b6) >> 8) |
00121              ((value & m64b5) << 8) |
00122              ((value & m64b4) >> 8) |
00123              ((value & m64b3) << 8) |
00124              ((value & m64b2) >> 8) |
00125              ((value & m64b1) << 8);
00126     }
00127     */
00128     void byteswap2( void* data, size_t num_elem )
00129     {
00130         uint16_t* mem = reinterpret_cast< uint16_t* >( data );
00131         uint16_t* end = mem + num_elem;
00132         for( ; mem < end; ++mem )
00133             *mem = swap_bytes( *mem );
00134     }
00135 
00136     void byteswap4( void* data, size_t num_elem )
00137     {
00138         uint32_t* mem = reinterpret_cast< uint32_t* >( data );
00139         uint32_t* end = mem + num_elem;
00140         for( ; mem < end; ++mem )
00141             *mem = swap_bytes( *mem );
00142     }
00143 
00144     void byteswap8( void* data, size_t num_elem )
00145     {
00146         if( sizeof( void* ) >= 8 )
00147         {
00148             uint64_t* mem = reinterpret_cast< uint64_t* >( data );
00149             uint64_t* end = mem + num_elem;
00150             for( ; mem < end; ++mem )
00151                 *mem = swap_bytes( *mem );
00152         }
00153         else
00154         {
00155             uint32_t* mem = reinterpret_cast< uint32_t* >( data );
00156             uint32_t* end = mem + 2 * num_elem;
00157             for( ; mem < end; mem += 2 )
00158             {
00159                 uint32_t tmp = swap_bytes( mem[0] );
00160                 mem[0]       = swap_bytes( mem[1] );
00161                 mem[1]       = tmp;
00162             }
00163         }
00164     }
00165 
00166 }  // namespace SysUtil
00167 
00168 }  // namespace moab
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines