From a901ab74b5d0c44265b5e2033cff89d532489d18 Mon Sep 17 00:00:00 2001 From: Florian Agbuya Date: Fri, 7 Jun 2024 12:56:55 +0800 Subject: [PATCH] compiler: fix int boundary checks Signed-off-by: Florian Agbuya --- artiq/compiler/embedding.py | 4 ++-- artiq/compiler/transforms/int_monomorphizer.py | 4 ++-- artiq/coredevice/comm_kernel.py | 8 ++++---- 3 files changed, 8 insertions(+), 8 deletions(-) diff --git a/artiq/compiler/embedding.py b/artiq/compiler/embedding.py index 380656e44..1c08bbdfe 100644 --- a/artiq/compiler/embedding.py +++ b/artiq/compiler/embedding.py @@ -747,9 +747,9 @@ class StitchingInferencer(Inferencer): if elt.__class__ == float: state |= IS_FLOAT elif elt.__class__ == int: - if -2**31 < elt < 2**31-1: + if -2**31 <= elt <= 2**31-1: state |= IS_INT32 - elif -2**63 < elt < 2**63-1: + elif -2**63 <= elt <= 2**63-1: state |= IS_INT64 else: state = -1 diff --git a/artiq/compiler/transforms/int_monomorphizer.py b/artiq/compiler/transforms/int_monomorphizer.py index adab3b165..5c7d22dce 100644 --- a/artiq/compiler/transforms/int_monomorphizer.py +++ b/artiq/compiler/transforms/int_monomorphizer.py @@ -14,9 +14,9 @@ class IntMonomorphizer(algorithm.Visitor): def visit_NumT(self, node): if builtins.is_int(node.type): if types.is_var(node.type["width"]): - if -2**31 < node.n < 2**31-1: + if -2**31 <= node.n <= 2**31-1: width = 32 - elif -2**63 < node.n < 2**63-1: + elif -2**63 <= node.n <= 2**63-1: width = 64 else: diag = diagnostic.Diagnostic("error", diff --git a/artiq/coredevice/comm_kernel.py b/artiq/coredevice/comm_kernel.py index b6ffb8ee7..73a480ef4 100644 --- a/artiq/coredevice/comm_kernel.py +++ b/artiq/coredevice/comm_kernel.py @@ -465,12 +465,12 @@ class CommKernel: self._write_bool(value) elif tag == "i": check(isinstance(value, (int, numpy.int32)) and - (-2**31 <= value < 2**31), + (-2**31 <= value <= 2**31-1), lambda: "32-bit int") self._write_int32(value) elif tag == "I": check(isinstance(value, (int, numpy.int32, numpy.int64)) and - (-2**63 <= value < 2**63), + (-2**63 <= value <= 2**63-1), lambda: "64-bit int") self._write_int64(value) elif tag == "f": @@ -479,8 +479,8 @@ class CommKernel: self._write_float64(value) elif tag == "F": check(isinstance(value, Fraction) and - (-2**63 <= value.numerator < 2**63) and - (-2**63 <= value.denominator < 2**63), + (-2**63 <= value.numerator <= 2**63-1) and + (-2**63 <= value.denominator <= 2**63-1), lambda: "64-bit Fraction") self._write_int64(value.numerator) self._write_int64(value.denominator)