Skip to content

Commit

Permalink
ObjexxFCL::bit bit_ashift tweak to avoid left shift of negative values
Browse files Browse the repository at this point in the history
  • Loading branch information
DeadParrot committed Aug 24, 2015
1 parent 8c3bd55 commit e12cec5
Showing 1 changed file with 20 additions and 2 deletions.
22 changes: 20 additions & 2 deletions third_party/ObjexxFCL/src/ObjexxFCL/bit.hh
Expand Up @@ -291,12 +291,12 @@ bit_cshift( T const & x, S const & shift ) // x<0 behavior varies in Fortran ISH
}

// Bit Arithmetic Shifted
template< typename T, typename S >
template< typename T, typename S, class = typename std::enable_if< std::is_unsigned< T >::value >::type >
inline
T
bit_ashift( T const & x, S const & shift )
{
static auto const x_bits( bit_size( x ) );
auto const x_bits( std::numeric_limits< T >::digits );
if ( shift >= S( 0 ) ) {
return ( shift < x_bits ? x << shift : T( 0 ) );
} else { // Negative (right) shift
Expand All @@ -308,6 +308,24 @@ bit_ashift( T const & x, S const & shift )
}
}

// Bit Arithmetic Shifted
template< typename T, typename S, class = typename std::enable_if< std::is_signed< T >::value >::type, typename = void >
inline
T
bit_ashift( T const & x, S const & shift )
{
auto const x_bits( std::numeric_limits< T >::digits + 1 );
if ( shift >= S( 0 ) ) {
return ( shift < x_bits ? *reinterpret_cast< typename std::make_unsigned< T const >::type * >( &x ) << shift : T( 0 ) );
} else { // Negative (right) shift
if ( x >= T( 0 ) ) {
return ( -shift < x_bits ? x >> -shift : T( 0 ) );
} else {
return ( -shift < x_bits ? x >> -shift | ~( ( S( 1 ) << ( x_bits + shift ) ) - 1 ) : T( 0 ) );
}
}
}

// Bit Right Arithmetic Shifted
template< typename T, typename S >
inline
Expand Down

4 comments on commit e12cec5

@nrel-bot
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Autodesk_Performance (DeadParrot) - x86_64-MacOS-10.9-clang: OK (1792 of 1792 tests passed)

Build Badge Test Badge

@nrel-bot
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Autodesk_Performance (DeadParrot) - i386-Windows-7-VisualStudio-12: OK (1798 of 1798 tests passed)

Build Badge Test Badge

@nrel-bot
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Autodesk_Performance (DeadParrot) - Win64-Windows-7-VisualStudio-12: Tests Failed

Build Badge Test Badge

@DeadParrot
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Visual C++ failures are not reproduced locally: forced rebuild/retest.

Please sign in to comment.