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