@@ -25,7 +25,7 @@ def get_bits_from_int(val_int, val_size=16):
25
25
bits = [None ] * val_size
26
26
# fill bits list with bit items
27
27
for i , item in enumerate (bits ):
28
- bits [i ] = bool ((val_int >> i ) & 0x01 )
28
+ bits [i ] = bool ((val_int >> i ) & 0x01 )
29
29
# return bits list
30
30
return bits
31
31
@@ -43,7 +43,8 @@ def decode_ieee(val_int):
43
43
:returns: float result
44
44
:rtype: float
45
45
"""
46
- return struct .unpack ("f" ,struct .pack ("I" , val_int ))[0 ]
46
+ return struct .unpack ("f" , struct .pack ("I" , val_int ))[0 ]
47
+
47
48
48
49
def encode_ieee (val_float ):
49
50
"""Encode Python float to int (32 bits integer) as an IEEE single precision
@@ -55,7 +56,7 @@ def encode_ieee(val_float):
55
56
:returns: IEEE 32 bits (single precision) as Python int
56
57
:rtype: int
57
58
"""
58
- return struct .unpack ("I" ,struct .pack ("f" , val_float ))[0 ]
59
+ return struct .unpack ("I" , struct .pack ("f" , val_float ))[0 ]
59
60
60
61
61
62
################################
@@ -64,28 +65,55 @@ def encode_ieee(val_float):
64
65
def word_list_to_long (val_list , big_endian = True ):
65
66
"""Word list (16 bits int) to long list (32 bits int)
66
67
67
- By default word_list2long () use big endian order. For use little endian, set
68
+ By default word_list_to_long () use big endian order. For use little endian, set
68
69
big_endian param to False.
69
70
70
71
:param val_list: list of 16 bits int value
71
72
:type val_list: list
72
73
:param big_endian: True for big endian/False for little (optional)
73
74
:type big_endian: bool
74
- :returns: 2's complement result
75
+ :returns: list of 32 bits int value
75
76
:rtype: list
76
77
"""
77
78
# allocate list for long int
78
- long_list = [None ] * int (len (val_list )/ 2 )
79
+ long_list = [None ] * int (len (val_list ) / 2 )
79
80
# fill registers list with register items
80
81
for i , item in enumerate (long_list ):
81
82
if big_endian :
82
- long_list [i ] = (val_list [i * 2 ] << 16 ) + val_list [(i * 2 ) + 1 ]
83
+ long_list [i ] = (val_list [i * 2 ] << 16 ) + val_list [(i * 2 ) + 1 ]
83
84
else :
84
- long_list [i ] = (val_list [(i * 2 ) + 1 ] << 16 ) + val_list [i * 2 ]
85
+ long_list [i ] = (val_list [(i * 2 ) + 1 ] << 16 ) + val_list [i * 2 ]
85
86
# return long list
86
87
return long_list
87
88
88
89
90
+ def long_list_to_word (val_list , big_endian = True ):
91
+ """Long list (32 bits int) to word list (16 bits int)
92
+
93
+ By default long_list_to_word() use big endian order. For use little endian, set
94
+ big_endian param to False.
95
+
96
+ :param val_list: list of 32 bits int value
97
+ :type val_list: list
98
+ :param big_endian: True for big endian/False for little (optional)
99
+ :type big_endian: bool
100
+ :returns: list of 16 bits int value
101
+ :rtype: list
102
+ """
103
+ # allocate list for long int
104
+ word_list = list ()
105
+ # fill registers list with register items
106
+ for i , item in enumerate (val_list ):
107
+ if big_endian :
108
+ word_list .append (val_list [i ] >> 16 )
109
+ word_list .append (val_list [i ] & 0xffff )
110
+ else :
111
+ word_list .append (val_list [i ] & 0xffff )
112
+ word_list .append (val_list [i ] >> 16 )
113
+ # return long list
114
+ return word_list
115
+
116
+
89
117
#########################################################
90
118
# 2's complement of int value (scalar and list) functions
91
119
#########################################################
@@ -100,9 +128,9 @@ def get_2comp(val_int, val_size=16):
100
128
:rtype: int
101
129
"""
102
130
# test MSBit (1 for negative)
103
- if ( val_int & ( 1 << (val_size - 1 ) )):
131
+ if val_int & ( 1 << (val_size - 1 )):
104
132
# do complement
105
- val_int = val_int - ( 1 << val_size )
133
+ val_int -= 1 << val_size
106
134
return val_int
107
135
108
136
@@ -199,4 +227,3 @@ def toggle_bit(value, offset):
199
227
"""
200
228
mask = 1 << offset
201
229
return int (value ^ mask )
202
-
0 commit comments