Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Speed up is_ascii_hex_digit #479

Closed
wants to merge 1 commit into from

Commits on Aug 22, 2023

  1. Speed up is_ascii_hex_digit

    It's possible to reduce the number of required comparisons by converting `c` into lowercase, so that instead of checking both lowercase and uppercase ranges, only lowercase range needs to be checked.
    
    This allows clang to generate branchless x86_64 asm (https://compiler-explorer.com/z/P6f7c8e4T). Instead of
    ```
    is_ascii_hex_digit(char):                # @is_ascii_hex_digit(char)
            lea     ecx, [rdi - 48]
            mov     al, 1
            cmp     cl, 10
            jb      .LBB0_3
            lea     ecx, [rdi - 65]
            cmp     cl, 6
            jb      .LBB0_3
            add     dil, -97
            cmp     dil, 6
            setb    al
    .LBB0_3:
            ret
    ```
    clang generates
    ```
    is_ascii_hex_digit(char):                # @is_ascii_hex_digit(char)
            lea     eax, [rdi - 48]
            cmp     al, 10
            setb    cl
            or      dil, 32
            add     dil, -97
            cmp     dil, 6
            setb    al
            or      al, cl
            ret
    ```
    that is 2 instructions shorter and contains no branches.
    ttsugriy authored Aug 22, 2023
    Configuration menu
    Copy the full SHA
    be7bef3 View commit details
    Browse the repository at this point in the history