| 1 |
/* uint64_t-like operations that work even on hosts lacking uint64_t |
| 2 |
|
| 3 |
Copyright (C) 2006, 2009-2024 Free Software Foundation, Inc. |
| 4 |
|
| 5 |
This file is free software: you can redistribute it and/or modify |
| 6 |
it under the terms of the GNU Lesser General Public License as |
| 7 |
published by the Free Software Foundation; either version 2.1 of the |
| 8 |
License, or (at your option) any later version. |
| 9 |
|
| 10 |
This file 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 Lesser General Public License for more details. |
| 14 |
|
| 15 |
You should have received a copy of the GNU Lesser General Public License |
| 16 |
along with this program. If not, see <https://www.gnu.org/licenses/>. */ |
| 17 |
|
| 18 |
/* Written by Paul Eggert. */ |
| 19 |
|
| 20 |
/* This file uses _GL_INLINE_HEADER_BEGIN, _GL_INLINE. */ |
| 21 |
#if !_GL_CONFIG_H_INCLUDED |
| 22 |
#error "Please include config.h first." |
| 23 |
#endif |
| 24 |
|
| 25 |
#include <stddef.h> |
| 26 |
#include <stdint.h> |
| 27 |
|
| 28 |
#include <byteswap.h> |
| 29 |
|
| 30 |
_GL_INLINE_HEADER_BEGIN |
| 31 |
#ifndef _GL_U64_INLINE |
| 32 |
# define _GL_U64_INLINE _GL_INLINE |
| 33 |
#endif |
| 34 |
|
| 35 |
#ifdef __cplusplus |
| 36 |
extern "C" { |
| 37 |
#endif |
| 38 |
|
| 39 |
|
| 40 |
#ifdef UINT64_MAX |
| 41 |
|
| 42 |
/* Native implementations are trivial. See below for comments on what |
| 43 |
these operations do. */ |
| 44 |
typedef uint64_t u64; |
| 45 |
# define u64hilo(hi, lo) ((u64) (((u64) (hi) << 32) + (lo))) |
| 46 |
# define u64init(hi, lo) u64hilo (hi, lo) |
| 47 |
# define u64lo(x) ((u64) (x)) |
| 48 |
# define u64size(x) u64lo (x) |
| 49 |
# define u64lt(x, y) ((x) < (y)) |
| 50 |
# define u64and(x, y) ((x) & (y)) |
| 51 |
# define u64or(x, y) ((x) | (y)) |
| 52 |
# define u64xor(x, y) ((x) ^ (y)) |
| 53 |
# define u64plus(x, y) ((x) + (y)) |
| 54 |
# define u64shl(x, n) ((x) << (n)) |
| 55 |
# define u64shr(x, n) ((x) >> (n)) |
| 56 |
# define u64bswap(x) bswap_64 (x) |
| 57 |
|
| 58 |
#else |
| 59 |
|
| 60 |
# define _GL_U64_MASK32 0xfffffffful /* 2**32 - 1. */ |
| 61 |
|
| 62 |
/* u64 represents a 64-bit unsigned integer value equal to (HI << 32) + LO. |
| 63 |
Implement it with unsigned int, which the GNU coding standards say |
| 64 |
is wide enough to hold 32 bits, and which does not signal an error |
| 65 |
when adding (theoretically possible with types like uint_fast32_t). |
| 66 |
u64init (HI, LO), is like u64hilo (HI, LO), but for use in |
| 67 |
initializer contexts. */ |
| 68 |
# ifdef WORDS_BIGENDIAN |
| 69 |
typedef struct { unsigned int hi, lo; } u64; |
| 70 |
# define u64init(hi, lo) { hi, lo } |
| 71 |
# else |
| 72 |
typedef struct { unsigned int lo, hi; } u64; |
| 73 |
# define u64init(hi, lo) { lo, hi } |
| 74 |
# endif |
| 75 |
|
| 76 |
/* Given the high and low-order 32-bit quantities HI and LO, return a u64 |
| 77 |
value representing (HI << 32) + LO. */ |
| 78 |
_GL_U64_INLINE u64 |
| 79 |
u64hilo (unsigned int hi, unsigned int lo) |
| 80 |
{ |
| 81 |
u64 r; |
| 82 |
r.hi = hi; |
| 83 |
r.lo = lo; |
| 84 |
return r; |
| 85 |
} |
| 86 |
|
| 87 |
/* Return a u64 value representing the 32-bit quantity LO. */ |
| 88 |
_GL_U64_INLINE u64 |
| 89 |
u64lo (unsigned int lo) |
| 90 |
{ |
| 91 |
u64 r; |
| 92 |
r.hi = 0; |
| 93 |
r.lo = lo; |
| 94 |
return r; |
| 95 |
} |
| 96 |
|
| 97 |
/* Return a u64 value representing SIZE, where 0 <= SIZE < 2**64. */ |
| 98 |
_GL_U64_INLINE u64 |
| 99 |
u64size (size_t size) |
| 100 |
{ |
| 101 |
u64 r; |
| 102 |
r.hi = size >> 31 >> 1; |
| 103 |
r.lo = size & _GL_U64_MASK32; |
| 104 |
return r; |
| 105 |
} |
| 106 |
|
| 107 |
/* Return X < Y. */ |
| 108 |
_GL_U64_INLINE bool |
| 109 |
u64lt (u64 x, u64 y) |
| 110 |
{ |
| 111 |
return x.hi < y.hi || (x.hi == y.hi && x.lo < y.lo); |
| 112 |
} |
| 113 |
|
| 114 |
/* Return X & Y. */ |
| 115 |
_GL_U64_INLINE u64 |
| 116 |
u64and (u64 x, u64 y) |
| 117 |
{ |
| 118 |
u64 r; |
| 119 |
r.hi = x.hi & y.hi; |
| 120 |
r.lo = x.lo & y.lo; |
| 121 |
return r; |
| 122 |
} |
| 123 |
|
| 124 |
/* Return X | Y. */ |
| 125 |
_GL_U64_INLINE u64 |
| 126 |
u64or (u64 x, u64 y) |
| 127 |
{ |
| 128 |
u64 r; |
| 129 |
r.hi = x.hi | y.hi; |
| 130 |
r.lo = x.lo | y.lo; |
| 131 |
return r; |
| 132 |
} |
| 133 |
|
| 134 |
/* Return X ^ Y. */ |
| 135 |
_GL_U64_INLINE u64 |
| 136 |
u64xor (u64 x, u64 y) |
| 137 |
{ |
| 138 |
u64 r; |
| 139 |
r.hi = x.hi ^ y.hi; |
| 140 |
r.lo = x.lo ^ y.lo; |
| 141 |
return r; |
| 142 |
} |
| 143 |
|
| 144 |
/* Return X + Y, wrapping around on overflow. */ |
| 145 |
_GL_U64_INLINE u64 |
| 146 |
u64plus (u64 x, u64 y) |
| 147 |
{ |
| 148 |
u64 r; |
| 149 |
r.lo = (x.lo + y.lo) & _GL_U64_MASK32; |
| 150 |
r.hi = (x.hi + y.hi + (r.lo < x.lo)) & _GL_U64_MASK32; |
| 151 |
return r; |
| 152 |
} |
| 153 |
|
| 154 |
/* Return X << N, where 0 <= N < 64. */ |
| 155 |
_GL_U64_INLINE u64 |
| 156 |
u64shl (u64 x, int n) |
| 157 |
{ |
| 158 |
u64 r; |
| 159 |
if (n < 32) |
| 160 |
{ |
| 161 |
r.hi = (x.hi << n & _GL_U64_MASK32) | x.lo >> (32 - n); |
| 162 |
r.lo = x.lo << n & _GL_U64_MASK32; |
| 163 |
} |
| 164 |
else |
| 165 |
{ |
| 166 |
r.hi = x.lo << (n - 32) & _GL_U64_MASK32; |
| 167 |
r.lo = 0; |
| 168 |
} |
| 169 |
return r; |
| 170 |
} |
| 171 |
|
| 172 |
/* Return X >> N. */ |
| 173 |
_GL_U64_INLINE u64 |
| 174 |
u64shr (u64 x, int n) |
| 175 |
{ |
| 176 |
u64 r; |
| 177 |
if (n < 32) |
| 178 |
{ |
| 179 |
r.hi = x.hi >> n; |
| 180 |
r.lo = (x.hi << (32 - n) & _GL_U64_MASK32) | x.lo >> n; |
| 181 |
} |
| 182 |
else |
| 183 |
{ |
| 184 |
r.hi = 0; |
| 185 |
r.lo = x.hi >> (n - 32); |
| 186 |
} |
| 187 |
return r; |
| 188 |
} |
| 189 |
|
| 190 |
/* Return X with bytes in reverse order. */ |
| 191 |
_GL_U64_INLINE u64 |
| 192 |
u64bswap (u64 x) |
| 193 |
{ |
| 194 |
return u64hilo (bswap_32 (x.lo), bswap_32 (x.hi)); |
| 195 |
} |
| 196 |
|
| 197 |
#endif |
| 198 |
|
| 199 |
/* Return X rotated left by N bits, where 0 < N < 64. */ |
| 200 |
_GL_U64_INLINE u64 |
| 201 |
u64rol (u64 x, int n) |
| 202 |
{ |
| 203 |
return u64or (u64shl (x, n), u64shr (x, 64 - n)); |
| 204 |
} |
| 205 |
|
| 206 |
|
| 207 |
#ifdef __cplusplus |
| 208 |
} |
| 209 |
#endif |
| 210 |
|
| 211 |
_GL_INLINE_HEADER_END |