This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Fix the UTO*_LE macros
[perl5.git] / hv_macro.h
CommitLineData
1c4b2386
YO
1#ifndef PERL_SEEN_HV_MACRO_H /* compile once */
2#define PERL_SEEN_HV_MACRO_H
3
4#if IVSIZE == 8
5#define CAN64BITHASH
6#endif
7
8/*-----------------------------------------------------------------------------
e8864dba 9 * Endianess and util macros
1c4b2386
YO
10 *
11 * The following 3 macros are defined in this section. The other macros defined
12 * are only needed to help derive these 3.
13 *
14 * U8TO16_LE(x) Read a little endian unsigned 32-bit int
15 * U8TO32_LE(x) Read a little endian unsigned 32-bit int
16 * U8TO28_LE(x) Read a little endian unsigned 32-bit int
17 * ROTL32(x,r) Rotate x left by r bits
18 * ROTL64(x,r) Rotate x left by r bits
19 * ROTR32(x,r) Rotate x right by r bits
20 * ROTR64(x,r) Rotate x right by r bits
21 */
22
e8864dba 23#ifndef U8TO16_LE
1c4b2386 24 #if (BYTEORDER == 0x1234 || BYTEORDER == 0x12345678)
e8864dba
MT
25 #define U8TO16_LE(ptr) ((U32)(ptr)[0]|(U32)(ptr)[1]<<8)
26 #define U8TO32_LE(ptr) ((U32)(ptr)[0]|(U32)(ptr)[1]<<8|(U32)(ptr)[2]<<16|(U32)(ptr)[3]<<24)
27 #define U8TO64_LE(ptr) ((U64)(ptr)[0]|(U64)(ptr)[1]<<8|(U64)(ptr)[2]<<16|(U64)(ptr)[3]<<24|\
28 (U64)(ptr)[4]<<32|(U64)(ptr)[5]<<40|\
29 (U64)(ptr)[6]<<48|(U64)(ptr)[7]<<56)
783bd734
MT
30 #elif (BYTEORDER == 0x4321 || BYTEORDER == 0x87654321)
31 #define U8TO16_LE(ptr) ((U32)(ptr)[1]|(U32)(ptr)[0]<<8)
32 #define U8TO32_LE(ptr) ((U32)(ptr)[3]|(U32)(ptr)[2]<<8|(U32)(ptr)[1]<<16|(U32)(ptr)[0]<<24)
33 #define U8TO64_LE(ptr) ((U64)(ptr)[7]|(U64)(ptr)[6]<<8|(U64)(ptr)[5]<<16|(U64)(ptr)[4]<<24|\
34 (U64)(ptr)[3]<<32|(U64)(ptr)[2]<<40|\
35 (U64)(ptr)[1]<<48|(U64)(ptr)[0]<<56)
1c4b2386
YO
36 #endif
37#endif
38
1c4b2386
YO
39#ifdef CAN64BITHASH
40 #ifndef U64TYPE
41 /* This probably isn't going to work, but failing with a compiler error due to
42 lack of uint64_t is no worse than failing right now with an #error. */
43 #define U64 uint64_t
44 #endif
45#endif
46
47/* Find best way to ROTL32/ROTL64 */
48#if defined(_MSC_VER)
49 #include <stdlib.h> /* Microsoft put _rotl declaration in here */
50 #define ROTL32(x,r) _rotl(x,r)
51 #define ROTR32(x,r) _rotr(x,r)
52 #define ROTL64(x,r) _rotl64(x,r)
53 #define ROTR64(x,r) _rotr64(x,r)
54#else
55 /* gcc recognises this code and generates a rotate instruction for CPUs with one */
56 #define ROTL32(x,r) (((U32)(x) << (r)) | ((U32)(x) >> (32 - (r))))
57 #define ROTR32(x,r) (((U32)(x) << (32 - (r))) | ((U32)(x) >> (r)))
58 #define ROTL64(x,r) ( ( (U64)(x) << (r) ) | ( (U64)(x) >> ( 64 - (r) ) ) )
59 #define ROTR64(x,r) ( ( (U64)(x) << ( 64 - (r) ) ) | ( (U64)(x) >> (r) ) )
60#endif
61
62
63#ifdef UV_IS_QUAD
64#define ROTL_UV(x,r) ROTL64(x,r)
65#define ROTR_UV(x,r) ROTL64(x,r)
66#else
67#define ROTL_UV(x,r) ROTL32(x,r)
68#define ROTR_UV(x,r) ROTR32(x,r)
69#endif
70#if IVSIZE == 8
71#define CAN64BITHASH
72#endif
73
74#endif