From 25346780bfffe7d6155e58ae2b01403f93eedcf1 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 58b4425b7..2d90bd1c0 100644 --- a/artiq/compiler/embedding.py +++ b/artiq/compiler/embedding.py @@ -658,9 +658,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 3d5b8dea9..b0d0fc346 100644 --- a/artiq/coredevice/comm_kernel.py +++ b/artiq/coredevice/comm_kernel.py @@ -449,12 +449,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": @@ -463,8 +463,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)