Skip to content

Instantly share code, notes, and snippets.

@edenhill
Created February 11, 2014 02:34
Show Gist options
  • Save edenhill/8928294 to your computer and use it in GitHub Desktop.
Save edenhill/8928294 to your computer and use it in GitHub Desktop.
gcc vs clang constant math compilation
clang 3.4-1
0000000000000000 <hashit>:
0: b8 7e f5 35 6d mov $0x6d35f57e,%eax
5: c3 retq
gcc 4.8.2-14
0000000000000000 <hashit>:
0: 8b 15 00 00 00 00 mov 0x0(%rip),%edx # 6 <hashit+0x6>
6: 89 d0 mov %edx,%eax
8: c1 f8 10 sar $0x10,%eax
b: 31 d0 xor %edx,%eax
d: 69 d0 3b 9f 5d 04 imul $0x45d9f3b,%eax,%edx
13: 89 d0 mov %edx,%eax
15: c1 f8 10 sar $0x10,%eax
18: 31 d0 xor %edx,%eax
1a: 69 d0 3b 9f 5d 04 imul $0x45d9f3b,%eax,%edx
20: 89 d0 mov %edx,%eax
22: c1 f8 10 sar $0x10,%eax
25: 31 d0 xor %edx,%eax
27: c3 retq
int hashit (void) {
const char *str = "abcd";
int x = *(const int *)str;
x = ((x >> 16) ^ x) * 0x45d9f3b;
x = ((x >> 16) ^ x) * 0x45d9f3b;
x = ((x >> 16) ^ x);
return x;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment