Skip to content

[mypyc] Emit unsigned casts for i8 and i16 operands - #22479

Open
Jahongir-Qurbonov wants to merge 2 commits into
python:masterfrom
Jahongir-Qurbonov:mypyc-unsigned-cast-narrow-ints
Open

Jahongir-Qurbonov wants to merge 2 commits into
python:masterfrom
Jahongir-Qurbonov:mypyc-unsigned-cast-narrow-ints

Conversation

@Jahongir-Qurbonov

Copy link
Copy Markdown
Contributor

emit_unsigned_int_cast only handled 32-bit and 64-bit operands, so an unsigned comparison (ULT etc.) or a zero-extending Extend with an i8 or i16 operand was emitted without a cast. C then compared or extended the value as signed: for example, an i8 value of -1 was zero-"extended" to 0xffffffffffffffff instead of 0xff, and -1 < 5 was true for an unsigned comparison.

The IR builder doesn't currently generate such ops, but they are valid IR and easy to produce when adding new native int operations (I ran into this while working on #22471, which uses signed comparisons to avoid it). This casts to uint8_t and uint16_t, like we already do for the wider types.

Added emitfunc tests for i8/i16 unsigned comparisons and zero extension.

emit_unsigned_int_cast only handled 32-bit and 64-bit operands, so an
unsigned comparison (ULT etc.) or a zero-extending Extend with an i8 or
i16 operand was emitted without a cast. C then compared or extended the
value as signed, e.g. an i8 value of -1 extended to 0xffffffffffffffff
instead of 0xff, and -1 < 5 was true for an unsigned comparison.

The current IR builder doesn't generate such ops, but they are valid IR
and easy to produce when adding new native int operations, so cast to
uint8_t and uint16_t like we already do for the wider types.
p-sawicki pushed a commit that referenced this pull request Nov 20, 2026
`test_long_unsigned`, `test_long_signed` and `test_extend` in
`test_emitfunc.py` created their own `int64`/`int32` registers named
`a`, although `setUp` already defines `self.i64` and `self.i32`
registers for this purpose and the other tests use them. Use those
instead. This is a test-only cleanup with no functional changes.

(This touches the same lines in `test_extend` as #22479, so I'll merge
master into whichever lands second.)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant