Last active
April 18, 2016 05:19
-
-
Save Centrinia/332629c1afe45e1392699902c0b6c628 to your computer and use it in GitHub Desktop.
Flip Case
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#if 0 | |
unsigned int flip_case(unsigned int c) | |
{ | |
unsigned int lower = ((('A' - 1 - c) & (c - 'Z' - 1)) >> 8) & 1; | |
unsigned int upper = ((('a' - 1 - c) & (c - 'z' - 1)) >> 8) & 1; | |
unsigned int to_lower = (-upper) & ('A' - 'a'); | |
unsigned int to_upper = (-lower) & ('a' - 'A'); | |
return c + to_lower + to_upper; | |
} | |
#endif | |
int main() | |
{ | |
int c; | |
for (c = 0; c < 256; c++) { | |
if (c != flip_case(c)) { | |
fprintf(stderr, "%.2x %.2x : %c %c\n", c, flip_case(c), c, | |
flip_case(c)); | |
} | |
} | |
return 0; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
.globl flip_case | |
flip_case: | |
movl %edi, %eax | |
subb $0x41, %al | |
cmpb $26, %al | |
setb %dl | |
addb $(0x41-0x61), %al | |
cmpb $26, %al | |
setb %dh | |
addb $0x61, %al | |
negb %dh | |
negb %dl | |
andb $(0x61-0x41), %dl | |
andb $(0x41-0x61), %dh | |
addb %dl, %dh | |
addb %dh, %al | |
movzx %al, %eax | |
ret |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment