1 /*
2  * Some macro trick to make both 32bit and 64bit siphash implementation
3  * coexit.
4  *
5  * The original implementation of Sam Trenholme is written so that you
6  * can just switch 32bit and 64bit versions with the same interface.
7  *
8  * For us, however, we need 32bit version available across platforms
9  * for the potable-hash.  We do want 64bit version on 64bit platform
10  * for the default-hash.
11  *
12  * We modify the original source (dws32hash.c, dwsiphash.c) adding
13  * a bit of #includes and #ifdefs, at the beginning and the end, so that the
14  * files shall provide the following API publicly.
15  *
16  *                      32bit platform           64bit platform
17  *
18  * dws32hash.c       Scm__DwSipDefaultHash
19  *                   Scm__DwSipPortableHash    Scm__DwSipPortableHash
20  *
21  * dwsiphash.c            (none)               Scm__DwSipDefaultHash
22  */
23 
24 #undef GC_PTHREADS		/* avoid GC_PTHREADS redefinition warning */
25 #include <gauche/config.h>	/* for SIZEOF_LONG */
26 
27 #ifdef SCM_DWSIPHASH_INTERFACE
28 
29 /* This part is used in hash.c */
30 #if SIZEOF_LONG == 4
31 u_long Scm__DwSipDefaultHash(uint8_t *str, uint32_t len,
32                              u_long k1, u_long k2);
33 uint32_t Scm__DwSipPortableHash(uint8_t *str, uint32_t len,
34                                 uint32_t k1, uint32_t k2);
35 #else  /*SIZEOF_LONG > 4*/
36 u_long Scm__DwSipDefaultHash(uint8_t *str, uint32_t len,
37                              u_long k1, u_long k2);
38 uint32_t Scm__DwSipPortableHash(uint8_t *str, uint32_t len,
39                                 uint32_t k1, uint32_t k2);
40 #endif /*SIZEOF_LONG > 4*/
41 
42 #else  /* !SCM_DWSIPHASH_INTERFACE */
43 
44 /* This part is used in dwsiphash.c and dws32hash.c */
45 #if DwSH_BWIDTH == 32
46 /* We're compiling dws32hash.c */
47 #define DwSip_round Scm__DwSip32_round
48 #define DwSip_ksetup Scm__DwSip32_ksetup
49 #define DwSip_getword Scm__DwSip32_getword
50 #define DwSip_hash Scm__DwSip32_hash
51 #else  /* DwSH_BWIDTH == 64 */
52 #define DwSip_round Scm__DwSip64_round
53 #define DwSip_ksetup Scm__DwSip64_ksetup
54 #define DwSip_getword Scm__DwSip64_getword
55 #define DwSip_hash Scm__DwSip64_hash
56 #endif /* DwSH_BWIDTH == 64 */
57 
58 /* forward declaration to make these file-scope */
59 #if (DwSH_BWIDTH == 32 || SIZEOF_LONG > 4)
60 static void DwSip_round(DwSH_WORD *v0, DwSH_WORD *v1,
61                         DwSH_WORD *v2, DwSH_WORD *v3);
62 static void DwSip_ksetup(DwSH_WORD *k0, DwSH_WORD *k1,
63                          DwSH_WORD *v0, DwSH_WORD *v1,
64                          DwSH_WORD *v2, DwSH_WORD *v3, DwSH_WORD *fx);
65 static DwSH_WORD DwSip_getword(uint32_t *offset, uint8_t *str, uint32_t len);
66 static DwSH_WORD DwSip_hash(uint8_t *str, uint32_t len,
67                             DwSH_WORD k1, DwSH_WORD k2);
68 #endif /* (DwSH_BWIDTH == 32 || SIZEOF_LONG > 4) */
69 
70 #if SIZEOF_LONG == 4
71 
72 #if   DwSH_BWIDTH == 32
Scm__DwSipDefaultHash(uint8_t * str,uint32_t len,uint32_t k1,uint32_t k2)73 uint32_t Scm__DwSipDefaultHash(uint8_t *str, uint32_t len,
74                                uint32_t k1, uint32_t k2)
75 {
76     return Scm__DwSip32_hash(str, len, k1, k2);
77 }
78 
Scm__DwSipPortableHash(uint8_t * str,uint32_t len,uint32_t k1,uint32_t k2)79 uint32_t Scm__DwSipPortableHash(uint8_t *str, uint32_t len,
80                                 uint32_t k1, uint32_t k2)
81 {
82     return Scm__DwSip32_hash(str, len, k1, k2);
83 }
84 #else    /*DwSH_BWIDTH == 64*/
85 /* nothing */
86 #endif   /*DwSH_BWIDTH == 64*/
87 
88 #else  /*SIZEOF_LONG > 4*/
89 
90 #if  DwSH_BWIDTH == 32
Scm__DwSipPortableHash(uint8_t * str,uint32_t len,uint32_t k1,uint32_t k2)91 uint32_t Scm__DwSipPortableHash(uint8_t *str, uint32_t len,
92                                 uint32_t k1, uint32_t k2)
93 {
94     return Scm__DwSip32_hash(str, len, k1, k2);
95 }
96 #else    /*DwSH_BWIDTH == 64*/
Scm__DwSipDefaultHash(uint8_t * str,uint32_t len,uint64_t k1,uint64_t k2)97 uint64_t Scm__DwSipDefaultHash(uint8_t *str, uint32_t len,
98                                uint64_t k1, uint64_t k2)
99 {
100     return Scm__DwSip64_hash(str, len, k1, k2);
101 }
102 #endif   /*DwSH_BWIDTH == 64*/
103 
104 #endif /*SIZEOF_LONG > 4*/
105 
106 #endif /*!SCM_DWSIPHASH_INTERFACE*/
107