fork(2) download
  1. #include <stdio.h>
  2.  
  3. #define U64 unsigned long long
  4. #define C64(x) x##ULL
  5.  
  6. U64 flipDiagA1H8(U64 x) {
  7. U64 t;
  8. const U64 k1 = C64(0x5500550055005500);
  9. const U64 k2 = C64(0x3333000033330000);
  10. const U64 k4 = C64(0x0f0f0f0f00000000);
  11. t = k4 & (x ^ (x << 28));
  12. x ^= t ^ (t >> 28) ;
  13. t = k2 & (x ^ (x << 14));
  14. x ^= t ^ (t >> 14) ;
  15. t = k1 & (x ^ (x << 7));
  16. x ^= t ^ (t >> 7) ;
  17. return x;
  18. }
  19.  
  20. U64 flipVertical(U64 x) {
  21. return ( (x << 56) ) |
  22. ( (x << 40) & C64(0x00ff000000000000) ) |
  23. ( (x << 24) & C64(0x0000ff0000000000) ) |
  24. ( (x << 8) & C64(0x000000ff00000000) ) |
  25. ( (x >> 8) & C64(0x00000000ff000000) ) |
  26. ( (x >> 24) & C64(0x0000000000ff0000) ) |
  27. ( (x >> 40) & C64(0x000000000000ff00) ) |
  28. ( (x >> 56) );
  29. }
  30.  
  31. U64 hashword(U64 x)
  32. {
  33. // Made up of four parts
  34. // Flip along antidiagonal
  35. U64 x1 = flipDiagA1H8(x);
  36.  
  37. // Flip vertically
  38. x1 = flipVertical(x1);
  39.  
  40. // XOR
  41. x1 = x ^ x1;
  42.  
  43. // Add one
  44.  
  45. x1++;
  46.  
  47. return x1;
  48. }
  49.  
  50. int main()
  51. {
  52. U64 input = 0;
  53. U64 output = 0;
  54. int collisions = 0;
  55.  
  56. printf("Small values:\n");
  57. for (input = 0; input < 16; input++) {
  58. printf("Input: %016llx\n", input);
  59. output = hashword(input);
  60. printf("Output: %016llx\n\n", output);
  61. }
  62.  
  63. printf("Large values:\n");
  64. for (input = 0xFFFFFFFFFF; input < 0xFFFFFFFFFF + 16; input++) {
  65. printf("Input: %016llx\n", input);
  66. output = hashword(input);
  67. printf("Output: %016llx\n\n", output);
  68. }
  69.  
  70. return 0;
  71. }
Success #stdin #stdout 0s 3340KB
stdin
Standard input is empty
stdout
Small values:
Input:  0000000000000000
Output: 0000000000000001

Input:  0000000000000001
Output: 0100000000000002

Input:  0000000000000002
Output: 0001000000000003

Input:  0000000000000003
Output: 0101000000000004

Input:  0000000000000004
Output: 0000010000000005

Input:  0000000000000005
Output: 0100010000000006

Input:  0000000000000006
Output: 0001010000000007

Input:  0000000000000007
Output: 0101010000000008

Input:  0000000000000008
Output: 0000000100000009

Input:  0000000000000009
Output: 010000010000000a

Input:  000000000000000a
Output: 000100010000000b

Input:  000000000000000b
Output: 010100010000000c

Input:  000000000000000c
Output: 000001010000000d

Input:  000000000000000d
Output: 010001010000000e

Input:  000000000000000e
Output: 000101010000000f

Input:  000000000000000f
Output: 0101010100000010

Large values:
Input:  000000ffffffffff
Output: 1f1f1fe0e0e0e0e1

Input:  0000010000000000
Output: 2000010000000001

Input:  0000010000000001
Output: 2100010000000002

Input:  0000010000000002
Output: 2001010000000003

Input:  0000010000000003
Output: 2101010000000004

Input:  0000010000000004
Output: 2000000000000005

Input:  0000010000000005
Output: 2100000000000006

Input:  0000010000000006
Output: 2001000000000007

Input:  0000010000000007
Output: 2101000000000008

Input:  0000010000000008
Output: 2000010100000009

Input:  0000010000000009
Output: 210001010000000a

Input:  000001000000000a
Output: 200101010000000b

Input:  000001000000000b
Output: 210101010000000c

Input:  000001000000000c
Output: 200000010000000d

Input:  000001000000000d
Output: 210000010000000e

Input:  000001000000000e
Output: 200100010000000f