Julia, generate array of four, Int8 integers between 1:3, do I need a package?

Viewed 101

Julia 1.5.3 on Ubuntu. I'm trying to simply generate an array of four, Int8 integers, comprised of the range from 1:3. Tried a bunch of different syntax but just get errors. I can't control both the type (Int8), and the number or returns (4). Thx. J

rand(1:3, 4) # works
rand(Int8, 4) # works
rand(Int8, 1:3, 4) #error
rand(Int8, 4, 1:3) #error
rand(Int8(1:3), 3) #error
1 Answers

The most elegant and efficient would be:

rand(UnitRange{Int8}(1,3), 4)

EDIT

Benoit Pasquier pointed out Int8(1):Int8(3) and indeed he is right.

This results in a more efficient assembly code because the type check can be avoided by the compiler (of course only when 1 and 3 are concrete values).

Here is how to find out:

julia> @code_native (:)(Int8(1),Int8(3))
        .text
; ┌ @ range.jl:5 within `Colon'
        pushq   %rbp
        movq    %rsp, %rbp
        movl    %ecx, %eax
; │┌ @ range.jl:287 within `UnitRange'
; ││┌ @ range.jl:292 within `unitrange_last'
; │││┌ @ int.jl:86 within `-'
        leal    -1(%rax), %r8d
; │││└
; │││┌ @ operators.jl:352 within `>='
; ││││┌ @ int.jl:442 within `<='
        cmpb    %dl, %al
; │││└└
        movzbl  %dl, %ecx
        movzbl  %r8b, %edx
        cmovlel %ecx, %edx
; │└└
        popq    %rbp
        retq
        nopl    (%rax,%rax)
; └

Compared to:

julia> @code_native UnitRange{Int8}(1,3)                                   
        .text                                                              
; ┌ @ range.jl:287 within `UnitRange'                                      
        pushq   %rbp                                                       
        movq    %rsp, %rbp                                                 
        subq    $32, %rsp                                                  
        movq    %rcx, %rax                                                 
; │┌ @ number.jl:7 within `convert'                                        
; ││┌ @ boot.jl:749 within `Int8'                                          
; │││┌ @ boot.jl:639 within `toInt8'                                       
; ││││┌ @ boot.jl:624 within `checked_trunc_sint'                          
        movsbq  %al, %rcx                                                  
        cmpq    %rax, %rcx                                                 
        jne     L49                                                        
; │└└└└                                                                    
; │ @ boot.jl within `UnitRange'                                           
        movq    %rdx, %r8                                                  
; │ @ range.jl:287 within `UnitRange'                                      
; │┌ @ range.jl:292 within `unitrange_last'                                
; ││┌ @ int.jl:86 within `-'                                               
        leaq    -1(%rax), %rdx                                             
; ││└                                                                      
; ││┌ @ operators.jl:352 within `>='                                       
; │││┌ @ int.jl:442 within `<='                                            
        cmpq    %r8, %rax                                                  
; ││└└                                                                     
        cmovleq %r8, %rdx                                                  
; │└                                                                       
; │┌ @ number.jl:7 within `convert'                                        
; ││┌ @ boot.jl:749 within `Int8'                                          
; │││┌ @ boot.jl:639 within `toInt8'                                       
; ││││┌ @ boot.jl:624 within `checked_trunc_sint'                          
        movsbq  %dl, %rcx                                                  
        cmpq    %rdx, %rcx                                                 
        jne     L72                                                        
; │└└└└                                                                    
        addq    $32, %rsp                                                  
        popq    %rbp                                                       
        retq                                                               
; │┌ @ number.jl:7 within `convert'                                        
; ││┌ @ boot.jl:749 within `Int8'                                          
; │││┌ @ boot.jl:639 within `toInt8'                                       
; ││││┌ @ boot.jl:624 within `checked_trunc_sint'                          
L49:                                                                       
        movabsq $throw_inexacterror, %r8                                   
        movl    $243577088, %ecx                # imm = 0xE84B100          
        movq    %rax, %rdx                                                 
        callq   *%r8                                                       
        ud2                                                                
L72:                                                                       
        movabsq $throw_inexacterror, %rax                                  
        movl    $243577088, %ecx                # imm = 0xE84B100          
        callq   *%rax                                                      
        ud2                                                                
        nopl    (%rax,%rax)                                                
; └└└└└                                                                    
                                                                           
Related