Advertisement
Guest User

Untitled

a guest
Oct 16th, 2017
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.19 KB | None | 0 0
  1. #include <string>
  2. #include <cstdlib>
  3. #include <stdio.h>
  4.  
  5. typedef unsigned char u8;
  6. typedef unsigned short u16;
  7.  
  8.     void escape(void *p) {
  9.       asm volatile("" : : "g"(p) : "memory");
  10.     }
  11.  
  12.     void clobber() {
  13.       asm volatile("" : : : "memory");
  14.     }
  15.  
  16.     class additive_hasher{
  17.     public:
  18.         /* returns rounded up to capacity size */
  19.         static size_t hash(const u8 *keyc, size_t len, size_t capacity){
  20.             const u16 *key2;
  21. //            const u8 *keyc = (const u8 *)key;
  22.             size_t hash = len, i;
  23.             if((len % 2)){
  24.                 key2 = (const u16 *)(keyc + 1);
  25.                 len--;
  26.                 hash += keyc[0];
  27.             }else{
  28.                 key2 = (const u16 *)(keyc);
  29.             }
  30.             len /= 2;
  31.  
  32.             for(i=0; i<len; ++i)
  33.                 hash += key2[i];
  34.             return (hash & (capacity - 1));
  35.         }
  36.     };
  37.  
  38. int main(){
  39.         srand(time(NULL));
  40.         size_t len;
  41.         scanf("%lu", &len);
  42.         u8 x[len];
  43.         for(size_t i = 0; i < len; i++)
  44.                 x[i] = rand();
  45.         escape(x);
  46.         clobber();
  47.  
  48.         printf("out %lu\n", additive_hasher::hash(x, len, 100));
  49. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement