Skip to content

Commit

Permalink
Optimize Integer#/
Browse files Browse the repository at this point in the history
  • Loading branch information
k0kubun committed Mar 6, 2023
1 parent d380692 commit 34f2ab1
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 5 deletions.
31 changes: 26 additions & 5 deletions lib/ruby_vm/mjit/insn_compiler.rb
Expand Up @@ -1694,8 +1694,9 @@ def jit_rb_obj_not(jit, ctx, asm, argc)
asm.mov(:rax, Qfalse)
asm.mov(:rcx, Qtrue)
asm.cmovz(:rax, :rcx)
asm.mov(ctx.stack_push, :rax)

stack_ret = ctx.stack_push
asm.mov(stack_ret, :rax)
true
end

Expand Down Expand Up @@ -1738,13 +1739,11 @@ def jit_rb_int_equal(jit, ctx, asm, argc)
def jit_rb_int_mul(jit, ctx, asm, argc)
return false if argc != 1
return false unless two_fixnums_on_stack?(jit)
asm.comment('rb_int_mul')

side_exit = side_exit(jit, ctx)
guard_two_fixnums(jit, ctx, asm, side_exit)

jit_prepare_routine_call(jit, ctx, asm)

asm.comment('rb_int_mul')
y_opnd = ctx.stack_pop
x_opnd = ctx.stack_pop
asm.mov(C_ARGS[0], x_opnd)
Expand All @@ -1753,7 +1752,29 @@ def jit_rb_int_mul(jit, ctx, asm, argc)

ret_opnd = ctx.stack_push
asm.mov(ret_opnd, C_RET)
true
end

def jit_rb_int_div(jit, ctx, asm, argc)
return false if argc != 1
return false unless two_fixnums_on_stack?(jit)

side_exit = side_exit(jit, ctx)
guard_two_fixnums(jit, ctx, asm, side_exit)

asm.comment('rb_int_div')
y_opnd = ctx.stack_pop
x_opnd = ctx.stack_pop
asm.mov(:rax, y_opnd)
asm.cmp(:rax, C.to_value(0))
asm.je(side_exit)

asm.mov(C_ARGS[0], x_opnd)
asm.mov(C_ARGS[1], :rax)
asm.call(C.rb_fix_div_fix)

ret_opnd = ctx.stack_push
asm.mov(ret_opnd, C_RET)
true
end

Expand All @@ -1774,7 +1795,6 @@ def jit_rb_ary_push(jit, ctx, asm, argc)

ret_opnd = ctx.stack_push
asm.mov(ret_opnd, C_RET)

true
end

Expand All @@ -1789,6 +1809,7 @@ def register_cfunc_codegen_funcs
register_cfunc_method(Integer, :==, :jit_rb_int_equal)
register_cfunc_method(Integer, :===, :jit_rb_int_equal)
register_cfunc_method(Integer, :*, :jit_rb_int_mul)
register_cfunc_method(Integer, :/, :jit_rb_int_div)

register_cfunc_method(Array, :<<, :jit_rb_ary_push)
end
Expand Down
4 changes: 4 additions & 0 deletions mjit_c.rb
Expand Up @@ -289,6 +289,10 @@ def rb_fix_mul_fix
Primitive.cexpr! 'SIZET2NUM((size_t)rb_fix_mul_fix)'
end

def rb_fix_div_fix
Primitive.cexpr! 'SIZET2NUM((size_t)rb_fix_div_fix)'
end

def rb_ary_push
Primitive.cexpr! 'SIZET2NUM((size_t)rb_ary_push)'
end
Expand Down

0 comments on commit 34f2ab1

Please sign in to comment.