Skip to content

Commit 74191f5

Browse files
committed
move parity computation to shared header
1 parent 2cac686 commit 74191f5

File tree

2 files changed

+18
-10
lines changed

2 files changed

+18
-10
lines changed

djl_os.hxx

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -394,4 +394,21 @@ inline char printable( uint8_t x )
394394
} //wcslen
395395
#endif
396396

397+
inline bool is_parity_even8( uint8_t x )
398+
{
399+
// use popcnt if possible. It's not available on the Q9650 and other older Intel CPUs. use fallback code below instead if needed.
400+
401+
#if defined( __GNUC__ ) || defined( __clang__ )
402+
return ( ! ( __builtin_popcount( x ) & 1 ) );
403+
#elif defined( _MSC_VER )
404+
return ( ! ( __popcnt16( x ) & 1 ) );
405+
#elif defined( __aarch64__ )
406+
return ( ! ( std::bitset<8>( x ).count() & 1 ) );
407+
#else
408+
x ^= ( x >> 4 );
409+
x ^= ( x >> 2 );
410+
x ^= ( x >> 1 );
411+
return ! ( x & 1 );
412+
#endif
413+
} //is_parity_even8
397414

x80.cxx

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -175,16 +175,7 @@ static uint16_t pcword() { uint16_t r = mword( reg.pc ); reg.pc += 2; return r;
175175
static void pushword( uint16_t val ) { reg.sp -= 2; setmword( reg.sp, val ); }
176176
static uint16_t popword() { uint16_t val = mword( reg.sp ); reg.sp += 2; return val; }
177177

178-
bool is_parity_even( uint8_t x )
179-
{
180-
#if defined(_M_AMD64) && !defined(__GNUC__)
181-
return ( ! ( __popcnt16( x ) & 1 ) ); // less portable, but faster
182-
#else
183-
return ( ! ( std::bitset<8>( x ).count() & 1 ) );
184-
#endif
185-
} //is_parity_even
186-
187-
void set_parity( uint8_t x ) { reg.fParityEven_Overflow = is_parity_even( x ); }
178+
void set_parity( uint8_t x ) { reg.fParityEven_Overflow = is_parity_even8( x ); }
188179

189180
void set_sign_zero( uint8_t x )
190181
{

0 commit comments

Comments
 (0)