Get rid of ugly if statements

后端 未结 25 2206
借酒劲吻你
借酒劲吻你 2020-12-02 05:59

I have this ugly code:

if ( v > 10 ) size = 6;
if ( v > 22 ) size = 5;
if ( v > 51 ) size = 4;
if ( v > 68 ) size = 3;
if ( v > 117 ) size = 2         


        
25条回答
  •  醉酒成梦
    2020-12-02 06:37

    You could rewrite it in ARM code. It's only 7 cycles worst case and a slender 164 bytes. Hope that helps. (note: this is untested)

    ; On entry
    ;   r0 - undefined
    ;   r1 - value to test
    ;   lr - return address
    ; On exit
    ;   r0 - new value or preserved
    ;   r1 - corrupted
    ;
    wtf
            SUBS    r1, r1, #10
            MOVLE   pc, lr
            CMP     r1, #135
            MOVGT   r0, #1
            ADRLE   r0, lut
            LDRLEB  r0, [r0, r1]
            MOV     pc, lr
    ;
    ; Look-up-table
    lut
            DCB     0   ; padding
            DCB     6   ; r1 = 11 on entry
            DCB     6
            DCB     6
            DCB     6
            DCB     6
            DCB     6
            DCB     6
            DCB     6
            DCB     6
            DCB     6
            DCB     6
            DCB     6
            DCB     5   ; r1 = 23 on entry
            DCB     5
            ...
            ALIGN
    

提交回复
热议问题