Skip to content

Conversation

ttsugriy
Copy link
Owner

This results in shorter and branchless x86_64 assembly: https://godbolt.org/z/5jqTbYWWh

Original:

convert_hex_to_binary(char):             # @convert_hex_to_binary(char)
        mov     eax, edi
        cmp     al, 57
        jg      .LBB0_2
        add     eax, -48
        ret
.LBB0_2:
        xor     ecx, ecx
        cmp     al, 97
        setb    cl
        shl     ecx, 5
        add     eax, ecx
        add     eax, -87
        ret

and new version:

convert_hex_to_binary(char):             # @convert_hex_to_binary(char)
        xor     ecx, ecx
        cmp     dil, 97
        setl    cl
        shl     ecx, 5
        add     ecx, -87
        cmp     dil, 58
        mov     eax, -48
        cmovge  eax, ecx
        add     eax, edi
        ret

This results in shorter and branchless x86_64 assembly:
https://godbolt.org/z/5jqTbYWWh

Original:
```
convert_hex_to_binary(char):             # @convert_hex_to_binary(char)
        mov     eax, edi
        cmp     al, 57
        jg      .LBB0_2
        add     eax, -48
        ret
.LBB0_2:
        xor     ecx, ecx
        cmp     al, 97
        setb    cl
        shl     ecx, 5
        add     eax, ecx
        add     eax, -87
        ret
```
and new version:
```
convert_hex_to_binary(char):             # @convert_hex_to_binary(char)
        xor     ecx, ecx
        cmp     dil, 97
        setl    cl
        shl     ecx, 5
        add     ecx, -87
        cmp     dil, 58
        mov     eax, -48
        cmovge  eax, ecx
        add     eax, edi
        ret
```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant