@@ -369,6 +369,9 @@ def __init__(self, name, type_xml):
369
369
self ._maximum = self ._calculate_maximum ()
370
370
self ._size_in_bits = self ._calculate_size_in_bits ()
371
371
self ._data_mask = (1 << self ._size_in_bits ) - 1
372
+ self ._word_length_mask = (1 << self ._word_length ) - 1
373
+ if self ._signed :
374
+ self ._signed_bit_mask = 1 << (self ._word_length - 1 )
372
375
373
376
@property
374
377
def datatype (self ):
@@ -439,7 +442,7 @@ def unpack_data(self, data):
439
442
440
443
if self ._signed :
441
444
data = self ._integer_twos_comp (data )
442
- decimal_value = Decimal ( data * self ._delta )
445
+ decimal_value = data * self ._delta
443
446
if self ._overflow_enabled :
444
447
return (overflow , decimal_value )
445
448
else :
@@ -457,14 +460,13 @@ def _get_overflow_value(self, data):
457
460
def _remove_overflow_bit (self , data ):
458
461
""" This helper method masks out all bits not inside the word length,
459
462
ultimately returning a value of data without the overflow bit. """
460
- return data & ( 2 ** ( self ._word_length ) - 1 )
463
+ return data & self ._word_length_mask
461
464
462
465
def _integer_twos_comp (self , data ):
463
466
""" Checks the signed bit and determines if the value is negative, If
464
467
so take the twos complement of the input."""
465
- signed_bit_mask = 2 ** (self ._word_length - 1 )
466
- if data & signed_bit_mask > 0 :
467
- data = data ^ (2 ** (self ._word_length ) - 1 )
468
+ if data & self ._signed_bit_mask > 0 :
469
+ data = data ^ self ._word_length_mask
468
470
data += 1
469
471
data *= - 1
470
472
return data
@@ -485,9 +487,8 @@ def pack_data(self, data_to_pack, packed_data):
485
487
if self ._signed and data < 0 :
486
488
fxp_representation = self ._integer_twos_comp (fxp_representation )
487
489
488
- if self ._overflow_enabled :
489
- if overflow :
490
- fxp_representation += 2 ** (self ._word_length )
490
+ if overflow :
491
+ fxp_representation += 2 ** (self ._word_length )
491
492
492
493
packed_data <<= self ._size_in_bits
493
494
packed_data |= fxp_representation
0 commit comments