Skip to content

Instantly share code, notes, and snippets.

@Centrinia
Last active April 18, 2016 05:19
Show Gist options
  • Save Centrinia/332629c1afe45e1392699902c0b6c628 to your computer and use it in GitHub Desktop.
Save Centrinia/332629c1afe45e1392699902c0b6c628 to your computer and use it in GitHub Desktop.
Flip Case
#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;
}
.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