MJIT: Refactor Compiler#cast_offset (#6967)

Subtract max value from offset when sign bit is set, without string operations.
This commit is contained in:
Mau Magnaguagno 2022-12-21 02:28:48 -03:00 committed by GitHub
parent 502ca37dde
commit 1e989c49a8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
Notes: git 2022-12-21 05:29:07 +00:00
Merged-By: k0kubun <takashikkbn@gmail.com>

View File

@ -817,9 +817,8 @@ class RubyVM::MJIT::Compiler
# Interpret unsigned long as signed long (VALUE -> OFFSET)
def cast_offset(offset)
bits = "%0#{8 * Fiddle::SIZEOF_VOIDP}d" % offset.to_s(2)
if bits[0] == '1' # negative
offset = -bits.chars.map { |i| i == '0' ? '1' : '0' }.join.to_i(2) - 1
if offset >= 1 << 8 * Fiddle::SIZEOF_VOIDP - 1 # negative
offset -= 1 << 8 * Fiddle::SIZEOF_VOIDP
end
offset
end