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