template eqabs(a, b): bool = a * a == b * b
    
    proc t(x: int32): bool = eqabs(x, 5) or eqabs(x, 9)
    
    echo t(10)
    

nim -d:release c x. Assembly with gcc -S -fverbose-asm shows:
    
    
    # /home/d067158/nimcache/x.c:50:    T1_ = ((NI32)(x * x) == ((NI32) 25));
            imull       %edi, %edi      # x, _1
            movl        $1, %eax        #, <retval>
    # /home/d067158/nimcache/x.c:51:    if (T1_) goto LA2_;
            cmpl        $25, %edi       #, _1
            je  .L3     #,
    # /home/d067158/nimcache/x.c:52:    T1_ = ((NI32)(x * x) == ((NI32) 81));
            cmpl        $81, %edi       #, _1
            sete        %al     #, <retval>
    .L3:
    # /home/d067158/nimcache/x.c:56: }
            ret
    

Which looks fine. So if you want to depend on compiler optimizations, check the 
assembly output. If you don't want that, do the optimization manually.

Reply via email to