Commit | Line | Data |
---|---|---|
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 | ||
a0d4efe9 PE |
22 | _GL_INLINE_HEADER_BEGIN |
23 | #ifndef _GL_U64_INLINE | |
24 | # define _GL_U64_INLINE _GL_INLINE | |
25 | #endif | |
26 | ||
3ce9d0d4 LL |
27 | /* Return X rotated left by N bits, where 0 < N < 64. */ |
28 | #define u64rol(x, n) u64or (u64shl (x, n), u64shr (x, 64 - n)) | |
29 | ||
30 | #ifdef UINT64_MAX | |
31 | ||
32 | /* Native implementations are trivial. See below for comments on what | |
33 | these operations do. */ | |
34 | typedef uint64_t u64; | |
35 | # define u64hilo(hi, lo) ((u64) (((u64) (hi) << 32) + (lo))) | |
36 | # define u64init(hi, lo) u64hilo (hi, lo) | |
37 | # define u64lo(x) ((u64) (x)) | |
caf8a9b2 | 38 | # define u64size(x) u64lo (x) |
3ce9d0d4 LL |
39 | # define u64lt(x, y) ((x) < (y)) |
40 | # define u64and(x, y) ((x) & (y)) | |
41 | # define u64or(x, y) ((x) | (y)) | |
42 | # define u64xor(x, y) ((x) ^ (y)) | |
43 | # define u64plus(x, y) ((x) + (y)) | |
44 | # define u64shl(x, n) ((x) << (n)) | |
45 | # define u64shr(x, n) ((x) >> (n)) | |
46 | ||
47 | #else | |
48 | ||
49 | /* u64 is a 64-bit unsigned integer value. | |
50 | u64init (HI, LO), is like u64hilo (HI, LO), but for use in | |
51 | initializer contexts. */ | |
52 | # ifdef WORDS_BIGENDIAN | |
53 | typedef struct { uint32_t hi, lo; } u64; | |
54 | # define u64init(hi, lo) { hi, lo } | |
55 | # else | |
56 | typedef struct { uint32_t lo, hi; } u64; | |
57 | # define u64init(hi, lo) { lo, hi } | |
58 | # endif | |
59 | ||
60 | /* Given the high and low-order 32-bit quantities HI and LO, return a u64 | |
61 | value representing (HI << 32) + LO. */ | |
a0d4efe9 | 62 | _GL_U64_INLINE u64 |
3ce9d0d4 LL |
63 | u64hilo (uint32_t hi, uint32_t lo) |
64 | { | |
65 | u64 r; | |
66 | r.hi = hi; | |
67 | r.lo = lo; | |
68 | return r; | |
69 | } | |
70 | ||
71 | /* Return a u64 value representing LO. */ | |
a0d4efe9 | 72 | _GL_U64_INLINE u64 |
3ce9d0d4 LL |
73 | u64lo (uint32_t lo) |
74 | { | |
75 | u64 r; | |
76 | r.hi = 0; | |
77 | r.lo = lo; | |
78 | return r; | |
79 | } | |
80 | ||
caf8a9b2 | 81 | /* Return a u64 value representing SIZE. */ |
a0d4efe9 | 82 | _GL_U64_INLINE u64 |
caf8a9b2 PE |
83 | u64size (size_t size) |
84 | { | |
85 | u64 r; | |
86 | r.hi = size >> 31 >> 1; | |
87 | r.lo = size; | |
88 | return r; | |
89 | } | |
90 | ||
3ce9d0d4 | 91 | /* Return X < Y. */ |
a0d4efe9 | 92 | _GL_U64_INLINE int |
3ce9d0d4 LL |
93 | u64lt (u64 x, u64 y) |
94 | { | |
95 | return x.hi < y.hi || (x.hi == y.hi && x.lo < y.lo); | |
96 | } | |
97 | ||
98 | /* Return X & Y. */ | |
a0d4efe9 | 99 | _GL_U64_INLINE u64 |
3ce9d0d4 LL |
100 | u64and (u64 x, u64 y) |
101 | { | |
102 | u64 r; | |
103 | r.hi = x.hi & y.hi; | |
104 | r.lo = x.lo & y.lo; | |
105 | return r; | |
106 | } | |
107 | ||
108 | /* Return X | Y. */ | |
a0d4efe9 | 109 | _GL_U64_INLINE u64 |
3ce9d0d4 LL |
110 | u64or (u64 x, u64 y) |
111 | { | |
112 | u64 r; | |
113 | r.hi = x.hi | y.hi; | |
114 | r.lo = x.lo | y.lo; | |
115 | return r; | |
116 | } | |
117 | ||
118 | /* Return X ^ Y. */ | |
a0d4efe9 | 119 | _GL_U64_INLINE u64 |
3ce9d0d4 LL |
120 | u64xor (u64 x, u64 y) |
121 | { | |
122 | u64 r; | |
123 | r.hi = x.hi ^ y.hi; | |
124 | r.lo = x.lo ^ y.lo; | |
125 | return r; | |
126 | } | |
127 | ||
128 | /* Return X + Y. */ | |
a0d4efe9 | 129 | _GL_U64_INLINE u64 |
3ce9d0d4 LL |
130 | u64plus (u64 x, u64 y) |
131 | { | |
132 | u64 r; | |
133 | r.lo = x.lo + y.lo; | |
134 | r.hi = x.hi + y.hi + (r.lo < x.lo); | |
135 | return r; | |
136 | } | |
137 | ||
138 | /* Return X << N. */ | |
a0d4efe9 | 139 | _GL_U64_INLINE u64 |
3ce9d0d4 LL |
140 | u64shl (u64 x, int n) |
141 | { | |
142 | u64 r; | |
143 | if (n < 32) | |
144 | { | |
145 | r.hi = (x.hi << n) | (x.lo >> (32 - n)); | |
146 | r.lo = x.lo << n; | |
147 | } | |
148 | else | |
149 | { | |
150 | r.hi = x.lo << (n - 32); | |
151 | r.lo = 0; | |
152 | } | |
153 | return r; | |
154 | } | |
155 | ||
156 | /* Return X >> N. */ | |
a0d4efe9 | 157 | _GL_U64_INLINE u64 |
3ce9d0d4 LL |
158 | u64shr (u64 x, int n) |
159 | { | |
160 | u64 r; | |
161 | if (n < 32) | |
162 | { | |
163 | r.hi = x.hi >> n; | |
164 | r.lo = (x.hi << (32 - n)) | (x.lo >> n); | |
165 | } | |
166 | else | |
167 | { | |
168 | r.hi = 0; | |
169 | r.lo = x.hi >> (n - 32); | |
170 | } | |
171 | return r; | |
172 | } | |
173 | ||
174 | #endif | |
a0d4efe9 PE |
175 | |
176 | _GL_INLINE_HEADER_END |