MJIT: Refactor BitField dereference byte and bitmask (#6955)

Prefer Array#unpack1 and Enumerable#sum.
I think the bitmask formula ``2 ** @width - 1`` would be clearer, but not faster for such small integers.
This commit is contained in:
Mau Magnaguagno 2022-12-17 18:06:50 -03:00 committed by GitHub
parent 6e3bc67103
commit 632beec01f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
Notes: git 2022-12-17 21:07:09 +00:00
Merged-By: k0kubun <takashikkbn@gmail.com>

View File

@ -282,12 +282,12 @@ module RubyVM::MJIT
# Dereference
def *
byte = Fiddle::Pointer.new(@addr)[0, Fiddle::SIZEOF_CHAR].unpack('c').first
byte = Fiddle::Pointer.new(@addr)[0, Fiddle::SIZEOF_CHAR].unpack1('c')
if @width == 1
bit = (1 & (byte >> @offset))
bit == 1
elsif @width <= 8 && @offset == 0
bitmask = @width.times.map { |i| 1 << i }.sum
bitmask = @width.times.sum { |i| 1 << i }
byte & bitmask
else
raise NotImplementedError.new("not-implemented bit field access: width=#{@width} offset=#{@offset}")