Skip to content

Commit

Permalink
Thy branchy code is faster than the branch-less code in method lookup…
Browse files Browse the repository at this point in the history
…Hex(char).
  • Loading branch information
wrandelshofer committed Feb 26, 2023
1 parent a8c0bdc commit 52d3686
Showing 1 changed file with 6 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,9 @@ protected static char charAt(CharSequence str, int i, int endIndex) {
* @return the hex value or a value < 0.
*/
protected static int lookupHex(byte ch) {
return CHAR_TO_HEX_MAP[ch & 127] | (ch >> 31);
// The branchy code is faster than the branch-less code.
// Branch-less code: return CHAR_TO_HEX_MAP[ch & 127] | (ch >> 31);
return ch > 0 ? CHAR_TO_HEX_MAP[ch] : -1;
}

/**
Expand All @@ -117,7 +119,9 @@ protected static int lookupHex(byte ch) {
* @return the hex value or a value < 0.
*/
protected static int lookupHex(char ch) {
return CHAR_TO_HEX_MAP[ch & 127] | (127 - ch) >> 31;
// The branchy code is faster than the branch-less code.
// Branch-less code: return CHAR_TO_HEX_MAP[ch & 127] | (127 - ch) >> 31;
return ch < 128 ? CHAR_TO_HEX_MAP[ch] : -1;
}

}

0 comments on commit 52d3686

Please sign in to comment.