Yes, this is an issue. It's due to LLVM returning essentially a random number 
when you integer divide by zero – but only when it is statically known to be 
zero. It's really, really annoying.

> On Dec 29, 2013, at 4:56 AM, Ivar Nesje <[email protected]> wrote:
> 
> I remember something that might be releated to this was discussed on Github 
> recently, but https://github.com/JuliaLang/julia/pull/5103 was the closest I 
> found. Discussion on commits disappare if the commits are rebased.
> 
> Ivar
> 
> kl. 03:20:07 UTC+1 søndag 29. desember 2013 skrev Marcus Urban følgende:
>> 
>> 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