summaryrefslogtreecommitdiff
path: root/hv_macro.h
blob: 938301733b4a4323e5e4a34d636dd91250a63bcf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#ifndef PERL_SEEN_HV_MACRO_H /* compile once */
#define PERL_SEEN_HV_MACRO_H

#if IVSIZE == 8
#define CAN64BITHASH
#endif

#ifdef CAN64BITHASH
  #ifndef U64TYPE
  /* This probably isn't going to work, but failing with a compiler error due to
   lack of uint64_t is no worse than failing right now with an #error.  */
  #define U64 uint64_t
  #endif
#endif


/*-----------------------------------------------------------------------------
 * Endianess and util macros
 *
 * The following 3 macros are defined in this section. The other macros defined
 * are only needed to help derive these 3.
 *
 * U8TO16_LE(x)   Read a little endian unsigned 32-bit int
 * U8TO32_LE(x)   Read a little endian unsigned 32-bit int
 * U8TO28_LE(x)   Read a little endian unsigned 32-bit int
 * ROTL32(x,r)      Rotate x left by r bits
 * ROTL64(x,r)      Rotate x left by r bits
 * ROTR32(x,r)      Rotate x right by r bits
 * ROTR64(x,r)      Rotate x right by r bits
 */

#ifndef U8TO16_LE
  #define _shifted_octet(type,ptr,idx,shift) (((type)(((U8*)(ptr))[(idx)]))<<(shift))
  #if (BYTEORDER == 0x1234 || BYTEORDER == 0x12345678)
    #ifdef USE_UNALIGNED_PTR_DEREF
        #define U8TO16_LE(ptr)   (*((const U16*)(ptr)))
        #define U8TO32_LE(ptr)   (*((const U32*)(ptr)))
        #define U8TO64_LE(ptr)   (*((const U64*)(ptr)))
    #else
        #define U8TO16_LE(ptr)   (_shifted_octet(U16,(ptr),0, 0)|\
                                  _shifted_octet(U16,(ptr),1, 8))

        #define U8TO32_LE(ptr)   (_shifted_octet(U32,(ptr),0, 0)|\
                                  _shifted_octet(U32,(ptr),1, 8)|\
                                  _shifted_octet(U32,(ptr),2,16)|\
                                  _shifted_octet(U32,(ptr),3,24))

        #define U8TO64_LE(ptr)   (_shifted_octet(U64,(ptr),0, 0)|\
                                  _shifted_octet(U64,(ptr),1, 8)|\
                                  _shifted_octet(U64,(ptr),2,16)|\
                                  _shifted_octet(U64,(ptr),3,24)|\
                                  _shifted_octet(U64,(ptr),4,32)|\
                                  _shifted_octet(U64,(ptr),5,40)|\
                                  _shifted_octet(U64,(ptr),6,48)|\
                                  _shifted_octet(U64,(ptr),7,56))
    #endif
  #elif (BYTEORDER == 0x4321 || BYTEORDER == 0x87654321)
        #define U8TO16_LE(ptr)   (_shifted_octet(U16,(ptr),1, 0)|\
                                  _shifted_octet(U16,(ptr),0, 8))

        #define U8TO32_LE(ptr)   (_shifted_octet(U32,(ptr),3, 0)|\
                                  _shifted_octet(U32,(ptr),2, 8)|\
                                  _shifted_octet(U32,(ptr),1,16)|\
                                  _shifted_octet(U32,(ptr),0,24))

        #define U8TO64_LE(ptr)   (_shifted_octet(U64,(ptr),7, 0)|\
                                  _shifted_octet(U64,(ptr),6, 8)|\
                                  _shifted_octet(U64,(ptr),5,16)|\
                                  _shifted_octet(U64,(ptr),4,24)|\
                                  _shifted_octet(U64,(ptr),3,32)|\
                                  _shifted_octet(U64,(ptr),2,40)|\
                                  _shifted_octet(U64,(ptr),1,48)|\
                                  _shifted_octet(U64,(ptr),0,56))
  #endif
#endif

/* Find best way to ROTL32/ROTL64 */
#if defined(_MSC_VER)
  #include <stdlib.h>  /* Microsoft put _rotl declaration in here */
  #define ROTL32(x,r)  _rotl(x,r)
  #define ROTR32(x,r)  _rotr(x,r)
  #define ROTL64(x,r)  _rotl64(x,r)
  #define ROTR64(x,r)  _rotr64(x,r)
#else
  /* gcc recognises this code and generates a rotate instruction for CPUs with one */
  #define ROTL32(x,r)  (((U32)(x) << (r)) | ((U32)(x) >> (32 - (r))))
  #define ROTR32(x,r)  (((U32)(x) << (32 - (r))) | ((U32)(x) >> (r)))
  #define ROTL64(x,r)  ( ( (U64)(x) << (r) ) | ( (U64)(x) >> ( 64 - (r) ) ) )
  #define ROTR64(x,r)  ( ( (U64)(x) << ( 64 - (r) ) ) | ( (U64)(x) >> (r) ) )
#endif


#ifdef UV_IS_QUAD
#define ROTL_UV(x,r) ROTL64(x,r)
#define ROTR_UV(x,r) ROTL64(x,r)
#else
#define ROTL_UV(x,r) ROTL32(x,r)
#define ROTR_UV(x,r) ROTR32(x,r)
#endif
#if IVSIZE == 8
#define CAN64BITHASH
#endif

#endif