zoukankan      html  css  js  c++  java
  • 编译器为简单分支语句生成的代码质量很高

    编译器对于简单分支的处理,感觉非常高效。

    比如这段C代码

    void sign3_c (uint8_t* s0, uint8_t *s1, int8_t *d, int width, int height, int stride) {
        int i,j;
        for(j = 0; j < height; j++) {
            int offset = j*stride;
            for(i = 0; i < width; i ++) {
                uint8_t c0 = s0[offset + i], c1 = s1[offset + i];
                int8_t v = 0;
                if (c0 < c1)
                    v = -1;
                if (c0 > c1)
                    v = 1;
                d[offset + i] = v;
            }
        }
    }

    其内层循环体编译出来的代码为

    .L6:
            movzbl  0(%ebp,%eax), %ecx
            movzbl  (%edi,%eax), %edx
            cmpb    %dl, %cl
            sbbl    %ebx, %ebx
            cmpb    %dl, %cl
            movl    $1, %edx
            cmova   %edx, %ebx
            movb    %bl, (%esi,%eax)
            addl    $1, %eax
            cmpl    44(%esp), %eax
            jne     .L6

    虽然我们在C语言的层面上有两个判断,但是编译到x86上居然使用条件移动等质量将分支去掉了,不得不赞一个

  • 相关阅读:
    BZOJ 3330 分数
    FR #11题解
    BZOJ 1857 传送带
    BZOJ 4757 Building a Tall Barn
    FR #10题解
    BZOJ 4393 Fruit Feast
    BZOJ 3126 Photo
    BZOJ 1312 Hard Life
    BZOJ 2039 employ人员雇佣
    Count SIN Numbers
  • 原文地址:https://www.cnblogs.com/mathlover/p/3290415.html
Copyright © 2011-2022 走看看