Merge from gnulib.
[bpt/emacs.git] / lib / u64.h
CommitLineData
3ce9d0d4
LL
1/* uint64_t-like operations that work even on hosts lacking uint64_t
2
ab422c4d 3 Copyright (C) 2006, 2009-2013 Free Software Foundation, Inc.
3ce9d0d4
LL
4
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.
9
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.
14
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/>. */
17
18/* Written by Paul Eggert. */
19
20#include <stdint.h>
21
230fe2a5
PE
22#ifndef _GL_INLINE_HEADER_BEGIN
23 #error "Please include config.h first."
24#endif
a0d4efe9
PE
25_GL_INLINE_HEADER_BEGIN
26#ifndef _GL_U64_INLINE
27# define _GL_U64_INLINE _GL_INLINE
28#endif
29
3ce9d0d4
LL
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))
32
33#ifdef UINT64_MAX
34
35/* Native implementations are trivial. See below for comments on what
36 these operations do. */
37typedef uint64_t u64;
38# define u64hilo(hi, lo) ((u64) (((u64) (hi) << 32) + (lo)))
39# define u64init(hi, lo) u64hilo (hi, lo)
40# define u64lo(x) ((u64) (x))
caf8a9b2 41# define u64size(x) u64lo (x)
3ce9d0d4
LL
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))
49
50#else
51
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
56typedef struct { uint32_t hi, lo; } u64;
57# define u64init(hi, lo) { hi, lo }
58# else
59typedef struct { uint32_t lo, hi; } u64;
60# define u64init(hi, lo) { lo, hi }
61# endif
62
63/* Given the high and low-order 32-bit quantities HI and LO, return a u64
64 value representing (HI << 32) + LO. */
a0d4efe9 65_GL_U64_INLINE u64
3ce9d0d4
LL
66u64hilo (uint32_t hi, uint32_t lo)
67{
68 u64 r;
69 r.hi = hi;
70 r.lo = lo;
71 return r;
72}
73
74/* Return a u64 value representing LO. */
a0d4efe9 75_GL_U64_INLINE u64
3ce9d0d4
LL
76u64lo (uint32_t lo)
77{
78 u64 r;
79 r.hi = 0;
80 r.lo = lo;
81 return r;
82}
83
caf8a9b2 84/* Return a u64 value representing SIZE. */
a0d4efe9 85_GL_U64_INLINE u64
caf8a9b2
PE
86u64size (size_t size)
87{
88 u64 r;
89 r.hi = size >> 31 >> 1;
90 r.lo = size;
91 return r;
92}
93
3ce9d0d4 94/* Return X < Y. */
a0d4efe9 95_GL_U64_INLINE int
3ce9d0d4
LL
96u64lt (u64 x, u64 y)
97{
98 return x.hi < y.hi || (x.hi == y.hi && x.lo < y.lo);
99}
100
101/* Return X & Y. */
a0d4efe9 102_GL_U64_INLINE u64
3ce9d0d4
LL
103u64and (u64 x, u64 y)
104{
105 u64 r;
106 r.hi = x.hi & y.hi;
107 r.lo = x.lo & y.lo;
108 return r;
109}
110
111/* Return X | Y. */
a0d4efe9 112_GL_U64_INLINE u64
3ce9d0d4
LL
113u64or (u64 x, u64 y)
114{
115 u64 r;
116 r.hi = x.hi | y.hi;
117 r.lo = x.lo | y.lo;
118 return r;
119}
120
121/* Return X ^ Y. */
a0d4efe9 122_GL_U64_INLINE u64
3ce9d0d4
LL
123u64xor (u64 x, u64 y)
124{
125 u64 r;
126 r.hi = x.hi ^ y.hi;
127 r.lo = x.lo ^ y.lo;
128 return r;
129}
130
131/* Return X + Y. */
a0d4efe9 132_GL_U64_INLINE u64
3ce9d0d4
LL
133u64plus (u64 x, u64 y)
134{
135 u64 r;
136 r.lo = x.lo + y.lo;
137 r.hi = x.hi + y.hi + (r.lo < x.lo);
138 return r;
139}
140
141/* Return X << N. */
a0d4efe9 142_GL_U64_INLINE u64
3ce9d0d4
LL
143u64shl (u64 x, int n)
144{
145 u64 r;
146 if (n < 32)
147 {
148 r.hi = (x.hi << n) | (x.lo >> (32 - n));
149 r.lo = x.lo << n;
150 }
151 else
152 {
153 r.hi = x.lo << (n - 32);
154 r.lo = 0;
155 }
156 return r;
157}
158
159/* Return X >> N. */
a0d4efe9 160_GL_U64_INLINE u64
3ce9d0d4
LL
161u64shr (u64 x, int n)
162{
163 u64 r;
164 if (n < 32)
165 {
166 r.hi = x.hi >> n;
167 r.lo = (x.hi << (32 - n)) | (x.lo >> n);
168 }
169 else
170 {
171 r.hi = 0;
172 r.lo = x.hi >> (n - 32);
173 }
174 return r;
175}
176
177#endif
a0d4efe9
PE
178
179_GL_INLINE_HEADER_END