1 /* uint64_t-like operations that work even on hosts lacking uint64_t
3 Copyright (C) 2006, 2009-2013 Free Software Foundation, Inc.
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, either version 3 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>. */
18 /* Written by Paul Eggert. */
22 #ifndef _GL_INLINE_HEADER_BEGIN
23 #error "Please include config.h first."
25 _GL_INLINE_HEADER_BEGIN
26 #ifndef _GL_U64_INLINE
27 # define _GL_U64_INLINE _GL_INLINE
30 /* Return X rotated left by N bits, where 0 < N < 64. */
31 #define u64rol(x, n) u64or (u64shl (x, n), u64shr (x, 64 - n))
35 /* Native implementations are trivial. See below for comments on what
36 these operations do. */
38 # define u64hilo(hi, lo) ((u64) (((u64) (hi) << 32) + (lo)))
39 # define u64init(hi, lo) u64hilo (hi, lo)
40 # define u64lo(x) ((u64) (x))
41 # define u64size(x) u64lo (x)
42 # define u64lt(x, y) ((x) < (y))
43 # define u64and(x, y) ((x) & (y))
44 # define u64or(x, y) ((x) | (y))
45 # define u64xor(x, y) ((x) ^ (y))
46 # define u64plus(x, y) ((x) + (y))
47 # define u64shl(x, n) ((x) << (n))
48 # define u64shr(x, n) ((x) >> (n))
52 /* u64 is a 64-bit unsigned integer value.
53 u64init (HI, LO), is like u64hilo (HI, LO), but for use in
54 initializer contexts. */
55 # ifdef WORDS_BIGENDIAN
56 typedef struct { uint32_t hi
, lo
; } u64
;
57 # define u64init(hi, lo) { hi, lo }
59 typedef struct { uint32_t lo
, hi
; } u64
;
60 # define u64init(hi, lo) { lo, hi }
63 /* Given the high and low-order 32-bit quantities HI and LO, return a u64
64 value representing (HI << 32) + LO. */
66 u64hilo (uint32_t hi
, uint32_t lo
)
74 /* Return a u64 value representing LO. */
84 /* Return a u64 value representing SIZE. */
89 r
.hi
= size
>> 31 >> 1;
98 return x
.hi
< y
.hi
|| (x
.hi
== y
.hi
&& x
.lo
< y
.lo
);
103 u64and (u64 x
, u64 y
)
123 u64xor (u64 x
, u64 y
)
133 u64plus (u64 x
, u64 y
)
137 r
.hi
= x
.hi
+ y
.hi
+ (r
.lo
< x
.lo
);
143 u64shl (u64 x
, int n
)
148 r
.hi
= (x
.hi
<< n
) | (x
.lo
>> (32 - n
));
153 r
.hi
= x
.lo
<< (n
- 32);
161 u64shr (u64 x
, int n
)
167 r
.lo
= (x
.hi
<< (32 - n
)) | (x
.lo
>> n
);
172 r
.lo
= x
.hi
>> (n
- 32);
179 _GL_INLINE_HEADER_END