I've noticed that the function div(a::Int, b::Int) results in a very large 
amount number of instructions, whereas rem(a::Int, b::Int) produces only a 
third as many. Is there an integer division function that emits code more 
like rem?

Here are the results:

# First rem()
julia> g(a::Int, b::Int) = rem(a,b)
julia> code_native(g, (Int, Int))
.section __TEXT,__text,regular,pure_instructions
Filename: none
Source line: 1
push RBP
mov RBP, RSP
Source line: 1
mov RAX, RDI
cqo
idiv RSI
mov RAX, RDX
pop RBP
ret

# Next div()
julia> f(a::Int, b::Int) = div(a,b)
julia> code_native(f, (Int, Int))
.section __TEXT,__text,regular,pure_instructions
Filename: none
Source line: 1
push RBP
mov RBP, RSP
movabs RAX, -9223372036854775808
cmp RDI, RAX
Source line: 1
setne AL
cmp RSI, -1
setne CL
test RSI, RSI
je 18
or CL, AL
je 10
mov RAX, RDI
cqo
idiv RSI
pop RBP
ret
movabs RAX, 4307890720
mov RDI, QWORD PTR [RAX]
movabs RAX, 4295633840
mov ESI, 1
call RAX

(For some reason, code_native() does not work on div and rem directly.)

Reply via email to