2 Commits 158ed26d36 ... e71b18b50f

Author SHA1 Message Date
  Michael Buesch e71b18b50f Fix warning: iteration 1073741823 invokes undefined behavior 5 years ago
  Michael Buesch 15be643d0d Add more type conversions 5 years ago
3 changed files with 76 additions and 4 deletions
  1. 24 0
      awlsim/common/datatypehelpers.pxd.in
  2. 51 3
      awlsim/common/datatypehelpers.py
  3. 1 1
      awlsim/core/insnmeas.py

+ 24 - 0
awlsim/common/datatypehelpers.pxd.in

@@ -146,11 +146,35 @@ cdef inline _Bool isString(object value):
 cdef inline uint32_t len_u32(object obj):
 	return <uint32_t>(min(len(obj), <object>0xFFFFFFFFu))
 
+cdef inline uint16_t len_u16(object obj):
+	return <uint16_t>(min(len(obj), <object>0xFFFFu))
+
+cdef inline uint8_t len_u8(object obj):
+	return <uint8_t>(min(len(obj), <object>0xFFu))
+
 cdef inline int32_t len_s32(object obj):
 	return <int32_t>(min(len(obj), <object>0x7FFFFFFFu))
 
+cdef inline int16_t len_s16(object obj):
+	return <int16_t>(min(len(obj), <object>0x7FFFu))
+
+cdef inline int8_t len_s8(object obj):
+	return <int8_t>(min(len(obj), <object>0x7Fu))
+
 cdef inline int32_t u32_to_s32(uint32_t value):
 	return <int32_t>(min(value, 0x7FFFFFFFu))
 
+cdef inline int16_t u32_to_s16(uint32_t value):
+	return <int16_t>(min(value, 0x7FFFu))
+
+cdef inline int8_t u32_to_s8(uint32_t value):
+	return <int8_t>(min(value, 0x7Fu))
+
 cdef inline uint32_t s32_to_u32(int32_t value):
 	return <uint32_t>(max(value, 0))
+
+cdef inline uint16_t s32_to_u16(int32_t value):
+	return <uint16_t>(min(max(value, 0), 0xFFFF))
+
+cdef inline uint8_t s32_to_u8(int32_t value):
+	return <uint8_t>(min(max(value, 0), 0xFF))

+ 51 - 3
awlsim/common/datatypehelpers.py

@@ -51,9 +51,17 @@ __all__ = [
 	"isInteger",
 	"isString",
 	"len_u32",
+	"len_u16",
+	"len_u8",
 	"len_s32",
+	"len_s16",
+	"len_s8",
 	"u32_to_s32",
+	"u32_to_s16",
+	"u32_to_s8",
 	"s32_to_u32",
+	"s32_to_u16",
+	"s32_to_u8",
 ]
 
 
@@ -302,17 +310,57 @@ isString = py23(__isString_python2,			#@nocy
 def len_u32(obj):					#@nocy
 	return min(len(obj), 0xFFFFFFFF)		#@nocy
 
+# Get the len() of obj and restrict to uint16_t.
+# The Cython variant of this function is defined in .pxd.in
+def len_u16(obj):					#@nocy
+	return min(len(obj), 0xFFFF)			#@nocy
+
+# Get the len() of obj and restrict to uint8_t.
+# The Cython variant of this function is defined in .pxd.in
+def len_u8(obj):					#@nocy
+	return min(len(obj), 0xFF)			#@nocy
+
 # Get the len() of obj and restrict to int32_t.
 # The Cython variant of this function is defined in .pxd.in
 def len_s32(obj):					#@nocy
 	return min(len(obj), 0x7FFFFFFF)		#@nocy
 
-# Restrict an int32_t to uint32_t range.
+# Get the len() of obj and restrict to int16_t.
+# The Cython variant of this function is defined in .pxd.in
+def len_s16(obj):					#@nocy
+	return min(len(obj), 0x7FFF)			#@nocy
+
+# Get the len() of obj and restrict to int8_t.
+# The Cython variant of this function is defined in .pxd.in
+def len_s8(obj):					#@nocy
+	return min(len(obj), 0x7F)			#@nocy
+
+# Restrict an uint32_t to int32_t range.
 # The Cython variant of this function is defined in .pxd.in
 def u32_to_s32(value):					#@nocy
 	return min(value, 0x7FFFFFFF)			#@nocy
 
-# Restrict an uint32_t to int32_t range.
+# Restrict an uint32_t to int16_t range.
+# The Cython variant of this function is defined in .pxd.in
+def u32_to_s16(value):					#@nocy
+	return min(value, 0x7FFF)			#@nocy
+
+# Restrict an uint32_t to int8_t range.
+# The Cython variant of this function is defined in .pxd.in
+def u32_to_s8(value):					#@nocy
+	return min(value, 0x7F)				#@nocy
+
+# Restrict an int32_t to uint32_t range.
 # The Cython variant of this function is defined in .pxd.in
 def s32_to_u32(value):					#@nocy
-	return max(value, 0)				#@nocy
+	return min(max(value, 0), 0x7FFFFFFF)		#@nocy
+
+# Restrict an int32_t to uint16_t range.
+# The Cython variant of this function is defined in .pxd.in
+def s32_to_u16(value):					#@nocy
+	return min(max(value, 0), 0xFFFF)		#@nocy
+
+# Restrict an int32_t to uint8_t range.
+# The Cython variant of this function is defined in .pxd.in
+def s32_to_u8(value):					#@nocy
+	return min(max(value, 0), 0xFF)			#@nocy

+ 1 - 1
awlsim/core/insnmeas.py

@@ -74,7 +74,7 @@ class InsnMeas(object): #+cdef
 	def __init__(self):
 		self.__perf_counter = time.perf_counter
 
-		self.__data = [None] * u32_to_s32(AwlInsnTypes.NR_TYPES + 1) #+suffix-u
+		self.__data = [None] * u32_to_s16(AwlInsnTypes.NR_TYPES + 1) #+suffix-u
 		for i in range(AwlInsnTypes.NR_TYPES + 1):
 			self.__data[i] = InsnMeasData()